ncpumat.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. {$ifdef sparc64}
  54. procedure tSparcmoddivnode.pass_generate_code;
  55. const
  56. { 64 bit signed overflow }
  57. divops: array[boolean, boolean, boolean] of tasmop =
  58. (((A_UDIV,A_UDIVcc),(A_SDIV,A_SDIVcc)),
  59. ((A_UDIVX,A_NOP),(A_SDIVX,A_NOP))
  60. );
  61. var
  62. power : longint;
  63. op : tasmop;
  64. tmpreg,
  65. numerator,
  66. divider,
  67. resultreg : tregister;
  68. overflowlabel : tasmlabel;
  69. ai : taicpu;
  70. begin
  71. secondpass(left);
  72. secondpass(right);
  73. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  74. location.register:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  75. { put numerator in register }
  76. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  77. numerator := left.location.register;
  78. resultreg := location.register;
  79. if is_64bitint(resultdef) then
  80. begin
  81. if (nodetype = divn) and
  82. (right.nodetype = ordconstn) and
  83. ispowerof2(tordconstnode(right).value.svalue,power) and
  84. (not (cs_check_overflow in current_settings.localswitches)) then
  85. begin
  86. if is_signed(left.resultdef) Then
  87. begin
  88. tmpreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  89. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SAR,OS_INT,63,numerator,tmpreg);
  90. { if signed, tmpreg=right value-1, otherwise 0 }
  91. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_AND,OS_INT,tordconstnode(right).value.svalue-1,tmpreg);
  92. { add to the left value }
  93. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_INT,numerator,tmpreg);
  94. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SAR,OS_INT,aword(power),tmpreg,resultreg);
  95. end
  96. else
  97. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_INT,aword(power),numerator,resultreg);
  98. end
  99. else
  100. begin
  101. { load divider in a register if necessary }
  102. divider:=NR_NO;
  103. if (right.location.loc<>LOC_CONSTANT) or
  104. (right.location.value<simm13lo) or
  105. (right.location.value>simm13hi) then
  106. begin
  107. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,
  108. right.resultdef,right.resultdef,true);
  109. divider:=right.location.register;
  110. end;
  111. op := divops[true, is_signed(right.resultdef),
  112. cs_check_overflow in current_settings.localswitches];
  113. if (divider<>NR_NO) then
  114. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,numerator,divider,resultreg))
  115. else
  116. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const_reg(op,numerator,right.location.value,resultreg));
  117. if (nodetype = modn) then
  118. begin
  119. current_asmdata.getjumplabel(overflowlabel);
  120. ai:=taicpu.op_cond_sym(A_Bxx,C_VS,overflowlabel);
  121. ai.delayslot_annulled:=true;
  122. current_asmdata.CurrAsmList.concat(ai);
  123. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_NOT,resultreg));
  124. cg.a_label(current_asmdata.CurrAsmList,overflowlabel);
  125. if (divider<>NR_NO) then
  126. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULX,resultreg,divider,resultreg))
  127. else
  128. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const_reg(A_MULX,resultreg,right.location.value,resultreg));
  129. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUB,numerator,resultreg,resultreg));
  130. end;
  131. end;
  132. end
  133. else
  134. begin
  135. if (nodetype = divn) and
  136. (right.nodetype = ordconstn) and
  137. ispowerof2(tordconstnode(right).value.svalue,power) and
  138. (not (cs_check_overflow in current_settings.localswitches)) then
  139. begin
  140. if is_signed(left.resultdef) Then
  141. begin
  142. tmpreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  143. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SAR,OS_INT,31,numerator,tmpreg);
  144. { if signed, tmpreg=right value-1, otherwise 0 }
  145. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_AND,OS_INT,tordconstnode(right).value.svalue-1,tmpreg);
  146. { add to the left value }
  147. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_INT,numerator,tmpreg);
  148. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SAR,OS_INT,aword(power),tmpreg,resultreg);
  149. end
  150. else
  151. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_INT,aword(power),numerator,resultreg);
  152. end
  153. else
  154. begin
  155. { load divider in a register if necessary }
  156. divider:=NR_NO;
  157. if (right.location.loc<>LOC_CONSTANT) or
  158. (right.location.value<simm13lo) or
  159. (right.location.value>simm13hi) then
  160. begin
  161. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,
  162. right.resultdef,right.resultdef,true);
  163. divider:=right.location.register;
  164. end;
  165. { needs overflow checking, (-maxlongint-1) div (-1) overflows! }
  166. { And on Sparc, the only way to catch a div-by-0 is by checking }
  167. { the overflow flag (JM) }
  168. { Fill %y with the -1 or 0 depending on the highest bit }
  169. if is_signed(left.resultdef) then
  170. begin
  171. tmpreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  172. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const_reg(A_SRA,numerator,31,tmpreg));
  173. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MOV,tmpreg,NR_Y));
  174. end
  175. else
  176. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MOV,NR_G0,NR_Y));
  177. { wait 3 instructions slots before we can read %y }
  178. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_NOP));
  179. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_NOP));
  180. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_NOP));
  181. op := divops[false, is_signed(right.resultdef),
  182. cs_check_overflow in current_settings.localswitches];
  183. if (divider<>NR_NO) then
  184. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,numerator,divider,resultreg))
  185. else
  186. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const_reg(op,numerator,right.location.value,resultreg));
  187. if (nodetype = modn) then
  188. begin
  189. current_asmdata.getjumplabel(overflowlabel);
  190. ai:=taicpu.op_cond_sym(A_Bxx,C_VS,overflowlabel);
  191. ai.delayslot_annulled:=true;
  192. current_asmdata.CurrAsmList.concat(ai);
  193. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_NOT,resultreg));
  194. cg.a_label(current_asmdata.CurrAsmList,overflowlabel);
  195. if (divider<>NR_NO) then
  196. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SMUL,resultreg,divider,resultreg))
  197. else
  198. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const_reg(A_SMUL,resultreg,right.location.value,resultreg));
  199. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUB,numerator,resultreg,resultreg));
  200. end;
  201. end;
  202. end;
  203. { set result location }
  204. location.loc:=LOC_REGISTER;
  205. location.register:=resultreg;
  206. cg.g_overflowcheck(current_asmdata.CurrAsmList,Location,resultdef);
  207. end;
  208. {$else sparc64}
  209. procedure tSparcmoddivnode.pass_generate_code;
  210. const
  211. { signed overflow }
  212. divops: array[boolean, boolean] of tasmop =
  213. ((A_UDIV,A_UDIVcc),(A_SDIV,A_SDIVcc));
  214. var
  215. power : longint;
  216. op : tasmop;
  217. tmpreg,
  218. numerator,
  219. divider,
  220. resultreg : tregister;
  221. overflowlabel : tasmlabel;
  222. ai : taicpu;
  223. begin
  224. secondpass(left);
  225. secondpass(right);
  226. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  227. location.register:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  228. { put numerator in register }
  229. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  230. numerator := left.location.register;
  231. resultreg := location.register;
  232. if (nodetype = divn) and
  233. (right.nodetype = ordconstn) and
  234. ispowerof2(tordconstnode(right).value.svalue,power) and
  235. (not (cs_check_overflow in current_settings.localswitches)) then
  236. begin
  237. if is_signed(left.resultdef) Then
  238. begin
  239. tmpreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  240. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SAR,OS_INT,31,numerator,tmpreg);
  241. { if signed, tmpreg=right value-1, otherwise 0 }
  242. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_AND,OS_INT,tordconstnode(right).value.svalue-1,tmpreg);
  243. { add to the left value }
  244. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_INT,numerator,tmpreg);
  245. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SAR,OS_INT,aword(power),tmpreg,resultreg);
  246. end
  247. else
  248. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_INT,aword(power),numerator,resultreg);
  249. end
  250. else
  251. begin
  252. { load divider in a register if necessary }
  253. divider:=NR_NO;
  254. if (right.location.loc<>LOC_CONSTANT) or
  255. (right.location.value<simm13lo) or
  256. (right.location.value>simm13hi) then
  257. begin
  258. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,
  259. right.resultdef,right.resultdef,true);
  260. divider:=right.location.register;
  261. end;
  262. { needs overflow checking, (-maxlongint-1) div (-1) overflows! }
  263. { And on Sparc, the only way to catch a div-by-0 is by checking }
  264. { the overflow flag (JM) }
  265. { Fill %y with the -1 or 0 depending on the highest bit }
  266. if is_signed(left.resultdef) then
  267. begin
  268. tmpreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  269. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const_reg(A_SRA,numerator,31,tmpreg));
  270. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MOV,tmpreg,NR_Y));
  271. end
  272. else
  273. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MOV,NR_G0,NR_Y));
  274. { wait 3 instructions slots before we can read %y }
  275. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_NOP));
  276. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_NOP));
  277. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_NOP));
  278. op := divops[is_signed(right.resultdef),
  279. cs_check_overflow in current_settings.localswitches];
  280. if (divider<>NR_NO) then
  281. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,numerator,divider,resultreg))
  282. else
  283. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const_reg(op,numerator,right.location.value,resultreg));
  284. if (nodetype = modn) then
  285. begin
  286. current_asmdata.getjumplabel(overflowlabel);
  287. ai:=taicpu.op_cond_sym(A_Bxx,C_VS,overflowlabel);
  288. ai.delayslot_annulled:=true;
  289. current_asmdata.CurrAsmList.concat(ai);
  290. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_NOT,resultreg));
  291. cg.a_label(current_asmdata.CurrAsmList,overflowlabel);
  292. if (divider<>NR_NO) then
  293. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SMUL,resultreg,divider,resultreg))
  294. else
  295. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const_reg(A_SMUL,resultreg,right.location.value,resultreg));
  296. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUB,numerator,resultreg,resultreg));
  297. end;
  298. end;
  299. { set result location }
  300. location.loc:=LOC_REGISTER;
  301. location.register:=resultreg;
  302. cg.g_overflowcheck(current_asmdata.CurrAsmList,Location,resultdef);
  303. end;
  304. {$endif sparc64}
  305. {*****************************************************************************
  306. TSparcSHLRSHRNODE
  307. *****************************************************************************}
  308. {$ifndef SPARC64}
  309. function TSparcShlShrNode.first_shlshr64bitint:TNode;
  310. begin
  311. { 64bit without constants need a helper }
  312. if is_64bit(left.resultdef) and
  313. (right.nodetype<>ordconstn) then
  314. begin
  315. result:=inherited first_shlshr64bitint;
  316. exit;
  317. end;
  318. result := nil;
  319. end;
  320. procedure tSparcshlshrnode.second_64bit;
  321. var
  322. hregister,hreg64hi,hreg64lo : tregister;
  323. op : topcg;
  324. shiftval: aword;
  325. const
  326. ops: array [boolean] of topcg = (OP_SHR,OP_SHL);
  327. begin
  328. { 64bit without constants need a helper, and is
  329. already replaced in pass1 }
  330. if (right.nodetype<>ordconstn) then
  331. internalerror(200405301);
  332. location_reset(location, LOC_REGISTER, def_cgsize(resultdef));
  333. { load left operator in a register }
  334. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,true);
  335. hreg64hi:=left.location.register64.reghi;
  336. hreg64lo:=left.location.register64.reglo;
  337. shiftval := tordconstnode(right).value.svalue and 63;
  338. op := ops[nodetype=shln];
  339. location.register64.reglo:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_32);
  340. location.register64.reghi:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_32);
  341. { Emitting "left shl 1" as "left+left" is twice shorter }
  342. if (nodetype=shln) and (shiftval=1) then
  343. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_64,left.location.register64,left.location.register64,location.register64)
  344. else if shiftval > 31 then
  345. begin
  346. if nodetype = shln then
  347. begin
  348. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,location.register64.reglo);
  349. { if shiftval and 31 = 0, it will optimize to MOVE }
  350. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHL, OS_32, shiftval and 31, hreg64lo, location.register64.reghi);
  351. end
  352. else
  353. begin
  354. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,location.register64.reghi);
  355. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_32, shiftval and 31, hreg64hi, location.register64.reglo);
  356. end;
  357. end
  358. else
  359. begin
  360. hregister := cg.getintregister(current_asmdata.CurrAsmList, OS_32);
  361. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, op, OS_32, shiftval, hreg64hi, location.register64.reghi);
  362. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, op, OS_32, shiftval, hreg64lo, location.register64.reglo);
  363. if shiftval <> 0 then
  364. begin
  365. if nodetype = shln then
  366. begin
  367. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_32, 32-shiftval, hreg64lo, hregister);
  368. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_32, hregister, location.register64.reghi, location.register64.reghi);
  369. end
  370. else
  371. begin
  372. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHL, OS_32, 32-shiftval, hreg64hi, hregister);
  373. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_32, hregister, location.register64.reglo, location.register64.reglo);
  374. end;
  375. end;
  376. end;
  377. end;
  378. {$endif SPARC64}
  379. {*****************************************************************************
  380. TSPARCNOTNODE
  381. *****************************************************************************}
  382. procedure tsparcnotnode.second_boolean;
  383. begin
  384. if not handle_locjump then
  385. begin
  386. secondpass(left);
  387. case left.location.loc of
  388. LOC_FLAGS :
  389. begin
  390. location_copy(location,left.location);
  391. inverse_flags(location.resflags);
  392. end;
  393. LOC_REGISTER, LOC_CREGISTER,
  394. LOC_REFERENCE, LOC_CREFERENCE,
  395. LOC_SUBSETREG, LOC_CSUBSETREG,
  396. LOC_SUBSETREF, LOC_CSUBSETREF:
  397. begin
  398. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  399. {$ifndef SPARC64}
  400. if is_64bit(left.resultdef) then
  401. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ORcc,
  402. left.location.register64.reglo,left.location.register64.reghi,NR_G0))
  403. else
  404. {$endif SPARC64}
  405. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const_reg(A_SUBcc,left.location.register,0,NR_G0));
  406. location_reset(location,LOC_FLAGS,OS_NO);
  407. location.resflags.Init(NR_ICC,F_E);
  408. end;
  409. else
  410. internalerror(2003042401);
  411. end;
  412. end;
  413. end;
  414. {*****************************************************************************
  415. TSPARCUNARYMINUSNODE
  416. *****************************************************************************}
  417. procedure tsparcunaryminusnode.second_float;
  418. begin
  419. secondpass(left);
  420. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  421. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  422. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  423. case location.size of
  424. OS_F32:
  425. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FNEGs,left.location.register,location.register));
  426. OS_F64:
  427. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FNEGd,left.location.register,location.register));
  428. OS_F128:
  429. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FNEGq,left.location.register,location.register));
  430. else
  431. internalerror(2013030501);
  432. end;
  433. end;
  434. begin
  435. cmoddivnode:=tSparcmoddivnode;
  436. cshlshrnode:=tSparcshlshrnode;
  437. cnotnode:=tSparcnotnode;
  438. cunaryminusnode:=tsparcunaryminusnode;
  439. end.