ncpumat.pas 13 KB

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