ncpumat.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. 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.pass_generate_code;
  158. var
  159. hregister,resultreg,hregister1,
  160. hreg64hi,hreg64lo : tregister;
  161. op : topcg;
  162. shiftval: aword;
  163. begin
  164. { 64bit without constants need a helper, and is
  165. already replaced in pass1 }
  166. if is_64bit(left.resultdef) and
  167. (right.nodetype<>ordconstn) then
  168. internalerror(200405301);
  169. secondpass(left);
  170. secondpass(right);
  171. if is_64bit(left.resultdef) then
  172. begin
  173. location_reset(location,LOC_REGISTER,OS_64);
  174. { load left operator in a register }
  175. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,u64inttype,false);
  176. hreg64hi:=left.location.register64.reghi;
  177. hreg64lo:=left.location.register64.reglo;
  178. shiftval := tordconstnode(right).value.svalue and 63;
  179. if shiftval > 31 then
  180. begin
  181. if nodetype = shln then
  182. begin
  183. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,hreg64hi);
  184. if (shiftval and 31) <> 0 then
  185. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_32,shiftval and 31,hreg64lo,hreg64lo);
  186. end
  187. else
  188. begin
  189. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,hreg64lo);
  190. if (shiftval and 31) <> 0 then
  191. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_32,shiftval and 31,hreg64hi,hreg64hi);
  192. end;
  193. location.register64.reglo:=hreg64hi;
  194. location.register64.reghi:=hreg64lo;
  195. end
  196. else
  197. begin
  198. { shr 0 or shl 0 are noops, but generate wrong code below,
  199. so only add code if shift val is non-zero }
  200. if (shiftval <> 0) then
  201. begin
  202. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  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_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_32,shiftval,hreg64hi,hreg64hi);
  207. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,hregister,hreg64hi,hreg64hi);
  208. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_32,shiftval,hreg64lo,hreg64lo);
  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_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_32,shiftval,hreg64lo,hreg64lo);
  214. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,hregister,hreg64lo,hreg64lo);
  215. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_32,shiftval,hreg64hi,hreg64hi);
  216. end;
  217. end;
  218. location.register64.reghi:=hreg64hi;
  219. location.register64.reglo:=hreg64lo;
  220. end;
  221. end
  222. else
  223. begin
  224. { load left operators in a register }
  225. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  226. location_copy(location,left.location);
  227. resultreg := location.register;
  228. hregister1 := location.register;
  229. if (location.loc = LOC_CREGISTER) then
  230. begin
  231. location.loc := LOC_REGISTER;
  232. resultreg := cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  233. location.register := resultreg;
  234. end;
  235. { determine operator }
  236. if nodetype=shln then
  237. op:=OP_SHL
  238. else
  239. op:=OP_SHR;
  240. { shifting by a constant directly coded: }
  241. if (right.nodetype=ordconstn) then
  242. begin
  243. if tordconstnode(right).value and 31<>0 then
  244. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,op,OS_32,tordconstnode(right).value.svalue and 31,hregister1,resultreg)
  245. end
  246. else
  247. begin
  248. { load shift count in a register if necessary }
  249. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  250. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,op,OS_32,right.location.register,hregister1,resultreg);
  251. end;
  252. end;
  253. end;
  254. {*****************************************************************************
  255. TSPARCNOTNODE
  256. *****************************************************************************}
  257. procedure tsparcnotnode.second_boolean;
  258. var
  259. hl : tasmlabel;
  260. begin
  261. { if the location is LOC_JUMP, we do the secondpass after the
  262. labels are allocated
  263. }
  264. if left.expectloc=LOC_JUMP then
  265. begin
  266. hl:=current_procinfo.CurrTrueLabel;
  267. current_procinfo.CurrTrueLabel:=current_procinfo.CurrFalseLabel;
  268. current_procinfo.CurrFalseLabel:=hl;
  269. secondpass(left);
  270. if left.location.loc<>LOC_JUMP then
  271. internalerror(2012081306);
  272. maketojumpbool(current_asmdata.CurrAsmList,left,lr_load_regvars);
  273. hl:=current_procinfo.CurrTrueLabel;
  274. current_procinfo.CurrTrueLabel:=current_procinfo.CurrFalseLabel;
  275. current_procinfo.CurrFalseLabel:=hl;
  276. location.loc:=LOC_JUMP;
  277. end
  278. else
  279. begin
  280. secondpass(left);
  281. case left.location.loc of
  282. LOC_FLAGS :
  283. begin
  284. location_copy(location,left.location);
  285. inverse_flags(location.resflags);
  286. end;
  287. LOC_REGISTER, LOC_CREGISTER,
  288. LOC_REFERENCE, LOC_CREFERENCE,
  289. LOC_SUBSETREG, LOC_CSUBSETREG,
  290. LOC_SUBSETREF, LOC_CSUBSETREF:
  291. begin
  292. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  293. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const_reg(A_SUBcc,left.location.register,0,NR_G0));
  294. location_reset(location,LOC_FLAGS,OS_NO);
  295. location.resflags:=F_E;
  296. end;
  297. else
  298. internalerror(2003042401);
  299. end;
  300. end;
  301. end;
  302. {*****************************************************************************
  303. TSPARCUNARYMINUSNODE
  304. *****************************************************************************}
  305. procedure tsparcunaryminusnode.second_float;
  306. begin
  307. secondpass(left);
  308. location_force_fpureg(current_asmdata.CurrAsmList,left.location,true);
  309. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  310. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  311. case location.size of
  312. OS_F32:
  313. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FNEGs,left.location.register,location.register));
  314. OS_F64:
  315. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FNEGd,left.location.register,location.register));
  316. OS_F128:
  317. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FNEGq,left.location.register,location.register));
  318. else
  319. internalerror(2013030501);
  320. end;
  321. end;
  322. begin
  323. cmoddivnode:=tSparcmoddivnode;
  324. cshlshrnode:=tSparcshlshrnode;
  325. cnotnode:=tSparcnotnode;
  326. cunaryminusnode:=tsparcunaryminusnode;
  327. end.