ncpumat.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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(tshlshrnode)
  27. procedure pass_generate_code;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,aasmdata,
  40. defutil,
  41. cgbase,cgobj,pass_2,procinfo,
  42. ncon,
  43. cpubase,
  44. ncgutil,cgcpu,cgutils;
  45. {*****************************************************************************
  46. TSparcMODDIVNODE
  47. *****************************************************************************}
  48. procedure tSparcmoddivnode.pass_generate_code;
  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(current_asmdata.CurrAsmList,left.location,def_cgsize(left.resultdef),true);
  68. location_copy(location,left.location);
  69. numerator := location.register;
  70. if (nodetype = modn) then
  71. resultreg := cg.GetIntRegister(current_asmdata.CurrAsmList,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(current_asmdata.CurrAsmList,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. if is_signed(left.resultdef) Then
  86. begin
  87. tmpreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  88. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SAR,OS_INT,31,numerator,tmpreg);
  89. { if signed, tmpreg=right value-1, otherwise 0 }
  90. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_AND,OS_INT,tordconstnode(right).value-1,tmpreg);
  91. { add to the left value }
  92. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_INT,numerator,tmpreg);
  93. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SAR,OS_INT,aword(power),tmpreg,resultreg);
  94. end
  95. else
  96. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_INT,aword(power),numerator,resultreg);
  97. end
  98. else
  99. begin
  100. { load divider in a register if necessary }
  101. location_force_reg(current_asmdata.CurrAsmList,right.location,
  102. def_cgsize(right.resultdef),true);
  103. divider := right.location.register;
  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. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,numerator,divider,resultreg));
  123. if (nodetype = modn) then
  124. begin
  125. current_asmdata.getjumplabel(overflowlabel);
  126. ai:=taicpu.op_cond_sym(A_Bxx,C_O,overflowlabel);
  127. ai.delayslot_annulled:=true;
  128. current_asmdata.CurrAsmList.concat(ai);
  129. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_NOT,resultreg));
  130. cg.a_label(current_asmdata.CurrAsmList,overflowlabel);
  131. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SMUL,resultreg,divider,resultreg));
  132. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUB,numerator,resultreg,resultreg));
  133. end;
  134. end;
  135. { set result location }
  136. location.loc:=LOC_REGISTER;
  137. location.register:=resultreg;
  138. cg.g_overflowcheck(current_asmdata.CurrAsmList,Location,resultdef);
  139. end;
  140. {*****************************************************************************
  141. TSparcSHLRSHRNODE
  142. *****************************************************************************}
  143. function TSparcShlShrNode.first_shlshr64bitint:TNode;
  144. begin
  145. { 64bit without constants need a helper }
  146. if is_64bit(left.resultdef) and
  147. (right.nodetype<>ordconstn) then
  148. begin
  149. result:=inherited first_shlshr64bitint;
  150. exit;
  151. end;
  152. result := nil;
  153. end;
  154. procedure tSparcshlshrnode.pass_generate_code;
  155. var
  156. hregister,resultreg,hregister1,
  157. hreg64hi,hreg64lo : tregister;
  158. op : topcg;
  159. shiftval: aword;
  160. begin
  161. { 64bit without constants need a helper, and is
  162. already replaced in pass1 }
  163. if is_64bit(left.resultdef) and
  164. (right.nodetype<>ordconstn) then
  165. internalerror(200405301);
  166. secondpass(left);
  167. secondpass(right);
  168. if is_64bit(left.resultdef) then
  169. begin
  170. location_reset(location,LOC_REGISTER,OS_64);
  171. { load left operator in a register }
  172. location_force_reg(current_asmdata.CurrAsmList,left.location,OS_64,false);
  173. hreg64hi:=left.location.register64.reghi;
  174. hreg64lo:=left.location.register64.reglo;
  175. shiftval := tordconstnode(right).value and 63;
  176. if shiftval > 31 then
  177. begin
  178. if nodetype = shln then
  179. begin
  180. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,hreg64hi);
  181. if (shiftval and 31) <> 0 then
  182. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_32,shiftval and 31,hreg64lo,hreg64lo);
  183. end
  184. else
  185. begin
  186. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,hreg64lo);
  187. if (shiftval and 31) <> 0 then
  188. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_32,shiftval and 31,hreg64hi,hreg64hi);
  189. end;
  190. location.register64.reglo:=hreg64hi;
  191. location.register64.reghi:=hreg64lo;
  192. end
  193. else
  194. begin
  195. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  196. if nodetype = shln then
  197. begin
  198. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_32,32-shiftval,hreg64lo,hregister);
  199. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_32,shiftval,hreg64hi,hreg64hi);
  200. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,hregister,hreg64hi,hreg64hi);
  201. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_32,shiftval,hreg64lo,hreg64lo);
  202. end
  203. else
  204. begin
  205. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_32,32-shiftval,hreg64hi,hregister);
  206. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_32,shiftval,hreg64lo,hreg64lo);
  207. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,hregister,hreg64lo,hreg64lo);
  208. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_32,shiftval,hreg64hi,hreg64hi);
  209. end;
  210. location.register64.reghi:=hreg64hi;
  211. location.register64.reglo:=hreg64lo;
  212. end;
  213. end
  214. else
  215. begin
  216. { load left operators in a register }
  217. location_force_reg(current_asmdata.CurrAsmList,left.location,def_cgsize(left.resultdef),true);
  218. location_copy(location,left.location);
  219. resultreg := location.register;
  220. hregister1 := location.register;
  221. if (location.loc = LOC_CREGISTER) then
  222. begin
  223. location.loc := LOC_REGISTER;
  224. resultreg := cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  225. location.register := resultreg;
  226. end;
  227. { determine operator }
  228. if nodetype=shln then
  229. op:=OP_SHL
  230. else
  231. op:=OP_SHR;
  232. { shifting by a constant directly coded: }
  233. if (right.nodetype=ordconstn) then
  234. begin
  235. if tordconstnode(right).value and 31<>0 then
  236. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,op,OS_32,tordconstnode(right).value and 31,hregister1,resultreg)
  237. end
  238. else
  239. begin
  240. { load shift count in a register if necessary }
  241. location_force_reg(current_asmdata.CurrAsmList,right.location,def_cgsize(right.resultdef),true);
  242. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,op,OS_32,right.location.register,hregister1,resultreg);
  243. end;
  244. end;
  245. end;
  246. {*****************************************************************************
  247. TSPARCNOTNODE
  248. *****************************************************************************}
  249. procedure tsparcnotnode.second_boolean;
  250. var
  251. hl : tasmlabel;
  252. begin
  253. { if the location is LOC_JUMP, we do the secondpass after the
  254. labels are allocated
  255. }
  256. if left.expectloc=LOC_JUMP then
  257. begin
  258. hl:=current_procinfo.CurrTrueLabel;
  259. current_procinfo.CurrTrueLabel:=current_procinfo.CurrFalseLabel;
  260. current_procinfo.CurrFalseLabel:=hl;
  261. secondpass(left);
  262. maketojumpbool(current_asmdata.CurrAsmList,left,lr_load_regvars);
  263. hl:=current_procinfo.CurrTrueLabel;
  264. current_procinfo.CurrTrueLabel:=current_procinfo.CurrFalseLabel;
  265. current_procinfo.CurrFalseLabel:=hl;
  266. location.loc:=LOC_JUMP;
  267. end
  268. else
  269. begin
  270. secondpass(left);
  271. case left.location.loc of
  272. LOC_FLAGS :
  273. begin
  274. location_copy(location,left.location);
  275. inverse_flags(location.resflags);
  276. end;
  277. LOC_REGISTER, LOC_CREGISTER,
  278. LOC_REFERENCE, LOC_CREFERENCE,
  279. LOC_SUBSETREG, LOC_CSUBSETREG,
  280. LOC_SUBSETREF, LOC_CSUBSETREF:
  281. begin
  282. location_force_reg(current_asmdata.CurrAsmList,left.location,def_cgsize(left.resultdef),true);
  283. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const_reg(A_SUBcc,left.location.register,0,NR_G0));
  284. location_reset(location,LOC_FLAGS,OS_NO);
  285. location.resflags:=F_E;
  286. end;
  287. else
  288. internalerror(2003042401);
  289. end;
  290. end;
  291. end;
  292. begin
  293. cmoddivnode:=tSparcmoddivnode;
  294. cshlshrnode:=tSparcshlshrnode;
  295. cnotnode:=tSparcnotnode;
  296. end.