ncpumat.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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_generate_code;override;
  25. end;
  26. tSparcshlshrnode = class(tcgshlshrnode)
  27. procedure second_64bit;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. tsparcunaryminusnode = class(tcgunaryminusnode)
  35. procedure second_float; override;
  36. end;
  37. implementation
  38. uses
  39. globtype,systems,constexp,
  40. cutils,verbose,globals,
  41. symconst,symdef,
  42. aasmbase,aasmcpu,aasmtai,aasmdata,
  43. defutil,
  44. cgbase,cgobj,hlcgobj,pass_2,procinfo,
  45. ncon,
  46. cpubase,
  47. ncgutil,cgcpu,cgutils;
  48. {*****************************************************************************
  49. TSparcMODDIVNODE
  50. *****************************************************************************}
  51. procedure tSparcmoddivnode.pass_generate_code;
  52. const
  53. { signed overflow }
  54. divops: array[boolean, boolean] of tasmop =
  55. ((A_UDIV,A_UDIVcc),(A_SDIV,A_SDIVcc));
  56. var
  57. power : longint;
  58. op : tasmop;
  59. tmpreg,
  60. numerator,
  61. divider,
  62. resultreg : tregister;
  63. overflowlabel : tasmlabel;
  64. ai : taicpu;
  65. begin
  66. secondpass(left);
  67. secondpass(right);
  68. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  69. location.register:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  70. { put numerator in register }
  71. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  72. numerator := left.location.register;
  73. resultreg := location.register;
  74. if (nodetype = divn) and
  75. (right.nodetype = ordconstn) and
  76. ispowerof2(tordconstnode(right).value.svalue,power) and
  77. (not (cs_check_overflow in current_settings.localswitches)) then
  78. begin
  79. if is_signed(left.resultdef) Then
  80. begin
  81. tmpreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  82. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SAR,OS_INT,31,numerator,tmpreg);
  83. { if signed, tmpreg=right value-1, otherwise 0 }
  84. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_AND,OS_INT,tordconstnode(right).value.svalue-1,tmpreg);
  85. { add to the left value }
  86. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_INT,numerator,tmpreg);
  87. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SAR,OS_INT,aword(power),tmpreg,resultreg);
  88. end
  89. else
  90. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_INT,aword(power),numerator,resultreg);
  91. end
  92. else
  93. begin
  94. { load divider in a register if necessary }
  95. divider:=NR_NO;
  96. if (right.location.loc<>LOC_CONSTANT) or
  97. (right.location.value<simm13lo) or
  98. (right.location.value>simm13hi) then
  99. begin
  100. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,
  101. right.resultdef,right.resultdef,true);
  102. divider:=right.location.register;
  103. end;
  104. { needs overflow checking, (-maxlongint-1) div (-1) overflows! }
  105. { And on Sparc, the only way to catch a div-by-0 is by checking }
  106. { the overflow flag (JM) }
  107. { Fill %y with the -1 or 0 depending on the highest bit }
  108. if is_signed(left.resultdef) then
  109. begin
  110. tmpreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  111. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const_reg(A_SRA,numerator,31,tmpreg));
  112. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MOV,tmpreg,NR_Y));
  113. end
  114. else
  115. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MOV,NR_G0,NR_Y));
  116. { wait 3 instructions slots before we can read %y }
  117. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_NOP));
  118. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_NOP));
  119. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_NOP));
  120. op := divops[is_signed(right.resultdef),
  121. cs_check_overflow in current_settings.localswitches];
  122. if (divider<>NR_NO) then
  123. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,numerator,divider,resultreg))
  124. else
  125. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const_reg(op,numerator,right.location.value,resultreg));
  126. if (nodetype = modn) then
  127. begin
  128. current_asmdata.getjumplabel(overflowlabel);
  129. ai:=taicpu.op_cond_sym(A_Bxx,C_VS,overflowlabel);
  130. ai.delayslot_annulled:=true;
  131. current_asmdata.CurrAsmList.concat(ai);
  132. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_NOT,resultreg));
  133. cg.a_label(current_asmdata.CurrAsmList,overflowlabel);
  134. if (divider<>NR_NO) then
  135. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SMUL,resultreg,divider,resultreg))
  136. else
  137. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const_reg(A_SMUL,resultreg,right.location.value,resultreg));
  138. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUB,numerator,resultreg,resultreg));
  139. end;
  140. end;
  141. { set result location }
  142. location.loc:=LOC_REGISTER;
  143. location.register:=resultreg;
  144. cg.g_overflowcheck(current_asmdata.CurrAsmList,Location,resultdef);
  145. end;
  146. {*****************************************************************************
  147. TSparcSHLRSHRNODE
  148. *****************************************************************************}
  149. function TSparcShlShrNode.first_shlshr64bitint:TNode;
  150. begin
  151. { 64bit without constants need a helper }
  152. if is_64bit(left.resultdef) and
  153. (right.nodetype<>ordconstn) then
  154. begin
  155. result:=inherited first_shlshr64bitint;
  156. exit;
  157. end;
  158. result := nil;
  159. end;
  160. procedure tSparcshlshrnode.second_64bit;
  161. var
  162. hregister,hreg64hi,hreg64lo : tregister;
  163. op : topcg;
  164. shiftval: aword;
  165. const
  166. ops: array [boolean] of topcg = (OP_SHR,OP_SHL);
  167. begin
  168. { 64bit without constants need a helper, and is
  169. already replaced in pass1 }
  170. if (right.nodetype<>ordconstn) then
  171. internalerror(200405301);
  172. location_reset(location, LOC_REGISTER, def_cgsize(resultdef));
  173. { load left operator in a register }
  174. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,true);
  175. hreg64hi:=left.location.register64.reghi;
  176. hreg64lo:=left.location.register64.reglo;
  177. shiftval := tordconstnode(right).value.svalue and 63;
  178. op := ops[nodetype=shln];
  179. location.register64.reglo:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_32);
  180. location.register64.reghi:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_32);
  181. { Emitting "left shl 1" as "left+left" is twice shorter }
  182. if (nodetype=shln) and (shiftval=1) then
  183. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_64,left.location.register64,left.location.register64,location.register64)
  184. else if shiftval > 31 then
  185. begin
  186. if nodetype = shln then
  187. begin
  188. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,location.register64.reglo);
  189. { if shiftval and 31 = 0, it will optimize to MOVE }
  190. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHL, OS_32, shiftval and 31, hreg64lo, location.register64.reghi);
  191. end
  192. else
  193. begin
  194. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,location.register64.reghi);
  195. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_32, shiftval and 31, hreg64hi, location.register64.reglo);
  196. end;
  197. end
  198. else
  199. begin
  200. hregister := cg.getintregister(current_asmdata.CurrAsmList, OS_32);
  201. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, op, OS_32, shiftval, hreg64hi, location.register64.reghi);
  202. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, op, OS_32, shiftval, hreg64lo, location.register64.reglo);
  203. if shiftval <> 0 then
  204. begin
  205. if nodetype = shln then
  206. begin
  207. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_32, 32-shiftval, hreg64lo, hregister);
  208. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_32, hregister, location.register64.reghi, location.register64.reghi);
  209. end
  210. else
  211. begin
  212. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHL, OS_32, 32-shiftval, hreg64hi, hregister);
  213. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_32, hregister, location.register64.reglo, location.register64.reglo);
  214. end;
  215. end;
  216. end;
  217. end;
  218. {*****************************************************************************
  219. TSPARCNOTNODE
  220. *****************************************************************************}
  221. procedure tsparcnotnode.second_boolean;
  222. begin
  223. if not handle_locjump then
  224. begin
  225. secondpass(left);
  226. case left.location.loc of
  227. LOC_FLAGS :
  228. begin
  229. location_copy(location,left.location);
  230. inverse_flags(location.resflags);
  231. end;
  232. LOC_REGISTER, LOC_CREGISTER,
  233. LOC_REFERENCE, LOC_CREFERENCE,
  234. LOC_SUBSETREG, LOC_CSUBSETREG,
  235. LOC_SUBSETREF, LOC_CSUBSETREF:
  236. begin
  237. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  238. if is_64bit(left.resultdef) then
  239. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ORcc,
  240. left.location.register64.reglo,left.location.register64.reghi,NR_G0))
  241. else
  242. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const_reg(A_SUBcc,left.location.register,0,NR_G0));
  243. location_reset(location,LOC_FLAGS,OS_NO);
  244. location.resflags:=F_E;
  245. end;
  246. else
  247. internalerror(2003042401);
  248. end;
  249. end;
  250. end;
  251. {*****************************************************************************
  252. TSPARCUNARYMINUSNODE
  253. *****************************************************************************}
  254. procedure tsparcunaryminusnode.second_float;
  255. begin
  256. secondpass(left);
  257. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  258. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  259. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  260. case location.size of
  261. OS_F32:
  262. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FNEGs,left.location.register,location.register));
  263. OS_F64:
  264. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FNEGd,left.location.register,location.register));
  265. OS_F128:
  266. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FNEGq,left.location.register,location.register));
  267. else
  268. internalerror(2013030501);
  269. end;
  270. end;
  271. begin
  272. cmoddivnode:=tSparcmoddivnode;
  273. cshlshrnode:=tSparcshlshrnode;
  274. cnotnode:=tSparcnotnode;
  275. cunaryminusnode:=tsparcunaryminusnode;
  276. end.