ncpumat.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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_copy(location,left.location);
  69. { put numerator in register }
  70. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  71. location_copy(location,left.location);
  72. numerator := location.register;
  73. if (nodetype = modn) then
  74. resultreg := cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT)
  75. else
  76. begin
  77. if (location.loc = LOC_CREGISTER) then
  78. begin
  79. location.loc := LOC_REGISTER;
  80. location.register := cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  81. end;
  82. resultreg := location.register;
  83. end;
  84. if (nodetype = divn) and
  85. (right.nodetype = ordconstn) and
  86. ispowerof2(tordconstnode(right).value.svalue,power) then
  87. begin
  88. if is_signed(left.resultdef) Then
  89. begin
  90. tmpreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  91. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SAR,OS_INT,31,numerator,tmpreg);
  92. { if signed, tmpreg=right value-1, otherwise 0 }
  93. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_AND,OS_INT,tordconstnode(right).value.svalue-1,tmpreg);
  94. { add to the left value }
  95. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_INT,numerator,tmpreg);
  96. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SAR,OS_INT,aword(power),tmpreg,resultreg);
  97. end
  98. else
  99. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_INT,aword(power),numerator,resultreg);
  100. end
  101. else
  102. begin
  103. { load divider in a register if necessary }
  104. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,
  105. right.resultdef,right.resultdef,true);
  106. divider := right.location.register;
  107. { needs overflow checking, (-maxlongint-1) div (-1) overflows! }
  108. { And on Sparc, the only way to catch a div-by-0 is by checking }
  109. { the overflow flag (JM) }
  110. { Fill %y with the -1 or 0 depending on the highest bit }
  111. if is_signed(left.resultdef) then
  112. begin
  113. tmpreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  114. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const_reg(A_SRA,numerator,31,tmpreg));
  115. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MOV,tmpreg,NR_Y));
  116. end
  117. else
  118. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MOV,NR_G0,NR_Y));
  119. { wait 3 instructions slots before we can read %y }
  120. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_NOP));
  121. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_NOP));
  122. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_NOP));
  123. op := divops[is_signed(right.resultdef),
  124. cs_check_overflow in current_settings.localswitches];
  125. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,numerator,divider,resultreg));
  126. if (nodetype = modn) then
  127. begin
  128. current_asmdata.getjumplabel(overflowlabel);
  129. ai:=taicpu.op_cond_sym(A_Bxx,C_O,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. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SMUL,resultreg,divider,resultreg));
  135. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUB,numerator,resultreg,resultreg));
  136. end;
  137. end;
  138. { set result location }
  139. location.loc:=LOC_REGISTER;
  140. location.register:=resultreg;
  141. cg.g_overflowcheck(current_asmdata.CurrAsmList,Location,resultdef);
  142. end;
  143. {*****************************************************************************
  144. TSparcSHLRSHRNODE
  145. *****************************************************************************}
  146. function TSparcShlShrNode.first_shlshr64bitint:TNode;
  147. begin
  148. { 64bit without constants need a helper }
  149. if is_64bit(left.resultdef) and
  150. (right.nodetype<>ordconstn) then
  151. begin
  152. result:=inherited first_shlshr64bitint;
  153. exit;
  154. end;
  155. result := nil;
  156. end;
  157. procedure tSparcshlshrnode.second_64bit;
  158. var
  159. hregister,hreg64hi,hreg64lo : tregister;
  160. op : topcg;
  161. shiftval: aword;
  162. const
  163. ops: array [boolean] of topcg = (OP_SHR,OP_SHL);
  164. begin
  165. { 64bit without constants need a helper, and is
  166. already replaced in pass1 }
  167. if (right.nodetype<>ordconstn) then
  168. internalerror(200405301);
  169. location_reset(location, LOC_REGISTER, def_cgsize(resultdef));
  170. { load left operator in a register }
  171. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,true);
  172. hreg64hi:=left.location.register64.reghi;
  173. hreg64lo:=left.location.register64.reglo;
  174. shiftval := tordconstnode(right).value.svalue and 63;
  175. op := ops[nodetype=shln];
  176. if shiftval > 31 then
  177. begin
  178. if nodetype = shln then
  179. begin
  180. location.register64.reglo:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_32);
  181. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,location.register64.reglo);
  182. location.register64.reghi:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_32);
  183. { if shiftval and 31 = 0, it will optimize to MOVE }
  184. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHL, OS_32, shiftval and 31, hreg64lo, location.register64.reghi);
  185. end
  186. else
  187. begin
  188. location.register64.reghi:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_32);
  189. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,location.register64.reghi);
  190. location.register64.reglo:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_32);
  191. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_32, shiftval and 31, hreg64hi, location.register64.reglo);
  192. end;
  193. end
  194. else
  195. begin
  196. location.register64.reglo:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_32);
  197. location.register64.reghi:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_32);
  198. hregister := cg.getintregister(current_asmdata.CurrAsmList, OS_32);
  199. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, op, OS_32, shiftval, hreg64hi, location.register64.reghi);
  200. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, op, OS_32, shiftval, hreg64lo, location.register64.reglo);
  201. if shiftval <> 0 then
  202. begin
  203. if nodetype = shln then
  204. begin
  205. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_32, 32-shiftval, hreg64lo, hregister);
  206. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_32, hregister, location.register64.reghi, location.register64.reghi);
  207. end
  208. else
  209. begin
  210. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHL, OS_32, 32-shiftval, hreg64hi, hregister);
  211. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_32, hregister, location.register64.reglo, location.register64.reglo);
  212. end;
  213. end;
  214. end;
  215. end;
  216. {*****************************************************************************
  217. TSPARCNOTNODE
  218. *****************************************************************************}
  219. procedure tsparcnotnode.second_boolean;
  220. var
  221. hl : tasmlabel;
  222. begin
  223. { if the location is LOC_JUMP, we do the secondpass after the
  224. labels are allocated
  225. }
  226. if left.expectloc=LOC_JUMP then
  227. begin
  228. hl:=current_procinfo.CurrTrueLabel;
  229. current_procinfo.CurrTrueLabel:=current_procinfo.CurrFalseLabel;
  230. current_procinfo.CurrFalseLabel:=hl;
  231. secondpass(left);
  232. if left.location.loc<>LOC_JUMP then
  233. internalerror(2012081306);
  234. maketojumpbool(current_asmdata.CurrAsmList,left,lr_load_regvars);
  235. hl:=current_procinfo.CurrTrueLabel;
  236. current_procinfo.CurrTrueLabel:=current_procinfo.CurrFalseLabel;
  237. current_procinfo.CurrFalseLabel:=hl;
  238. location.loc:=LOC_JUMP;
  239. end
  240. else
  241. begin
  242. secondpass(left);
  243. case left.location.loc of
  244. LOC_FLAGS :
  245. begin
  246. location_copy(location,left.location);
  247. inverse_flags(location.resflags);
  248. end;
  249. LOC_REGISTER, LOC_CREGISTER,
  250. LOC_REFERENCE, LOC_CREFERENCE,
  251. LOC_SUBSETREG, LOC_CSUBSETREG,
  252. LOC_SUBSETREF, LOC_CSUBSETREF:
  253. begin
  254. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  255. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const_reg(A_SUBcc,left.location.register,0,NR_G0));
  256. location_reset(location,LOC_FLAGS,OS_NO);
  257. location.resflags:=F_E;
  258. end;
  259. else
  260. internalerror(2003042401);
  261. end;
  262. end;
  263. end;
  264. {*****************************************************************************
  265. TSPARCUNARYMINUSNODE
  266. *****************************************************************************}
  267. procedure tsparcunaryminusnode.second_float;
  268. begin
  269. secondpass(left);
  270. location_force_fpureg(current_asmdata.CurrAsmList,left.location,true);
  271. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  272. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  273. case location.size of
  274. OS_F32:
  275. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FNEGs,left.location.register,location.register));
  276. OS_F64:
  277. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FNEGd,left.location.register,location.register));
  278. OS_F128:
  279. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FNEGq,left.location.register,location.register));
  280. else
  281. internalerror(2013030501);
  282. end;
  283. end;
  284. begin
  285. cmoddivnode:=tSparcmoddivnode;
  286. cshlshrnode:=tSparcshlshrnode;
  287. cnotnode:=tSparcnotnode;
  288. cunaryminusnode:=tsparcunaryminusnode;
  289. end.