nppcmat.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Generate PowerPC assembler for math nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit nppcmat;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,nmat;
  23. type
  24. tppcmoddivnode = class(tmoddivnode)
  25. procedure pass_2;override;
  26. end;
  27. tppcshlshrnode = class(tshlshrnode)
  28. procedure pass_2;override;
  29. end;
  30. tppcunaryminusnode = class(tunaryminusnode)
  31. procedure pass_2;override;
  32. end;
  33. tppcnotnode = class(tnotnode)
  34. procedure pass_2;override;
  35. end;
  36. implementation
  37. uses
  38. globtype,systems,
  39. cutils,verbose,globals,
  40. symconst,symdef,
  41. aasmbase,aasmcpu,aasmtai,
  42. types,
  43. cgbase,cgobj,pass_1,pass_2,
  44. ncon,
  45. cpubase,cpuinfo,cginfo,
  46. ncgutil,cga,cgcpu,cg64f32,rgobj;
  47. {*****************************************************************************
  48. TPPCMODDIVNODE
  49. *****************************************************************************}
  50. procedure tppcmoddivnode.pass_2;
  51. const
  52. { signed overflow }
  53. divops: array[boolean, boolean] of tasmop =
  54. ((A_DIVWU,A_DIVWUO_),(A_DIVW,A_DIVWO_));
  55. var
  56. power,
  57. l1, l2 : longint;
  58. op : tasmop;
  59. numerator,
  60. divider,
  61. resultreg : tregister;
  62. saved : tmaybesave;
  63. begin
  64. secondpass(left);
  65. maybe_save(exprasmlist,right.registers32,left.location,saved);
  66. secondpass(right);
  67. maybe_restore(exprasmlist,left.location,saved);
  68. location_copy(location,left.location);
  69. resultreg := R_NO;
  70. { put numerator in register }
  71. if (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  72. begin
  73. reference_release(exprasmlist,left.location.reference);
  74. numerator := rg.getregisterint(exprasmlist);
  75. { OS_32 because everything is always converted to longint/ }
  76. { cardinal in the resulttype pass (JM) }
  77. cg.a_load_ref_reg(exprasmlist,OS_32,left.location.reference,
  78. numerator);
  79. resultreg := numerator;
  80. end
  81. else
  82. begin
  83. numerator := left.location.register;
  84. if left.location.loc = LOC_CREGISTER then
  85. resultreg := rg.getregisterint(exprasmlist)
  86. else
  87. resultreg := numerator;
  88. end;
  89. if (nodetype = divn) and
  90. (right.nodetype = ordconstn) and
  91. ispowerof2(tordconstnode(right).value,power) then
  92. begin
  93. { From "The PowerPC Compiler Writer's Guide": }
  94. { This code uses the fact that, in the PowerPC architecture, }
  95. { the shift right algebraic instructions set the Carry bit if }
  96. { the source register contains a negative number and one or }
  97. { more 1-bits are shifted out. Otherwise, the carry bit is }
  98. { cleared. The addze instruction corrects the quotient, if }
  99. { necessary, when the dividend is negative. For example, if }
  100. { n = -13, (0xFFFF_FFF3), and k = 2, after executing the srawi }
  101. { instruction, q = -4 (0xFFFF_FFFC) and CA = 1. After executing }
  102. { the addze instruction, q = -3, the correct quotient. }
  103. cg.a_op_const_reg_reg(exprasmlist,OP_SAR,OS_32,aword(power),
  104. numerator,resultreg);
  105. exprasmlist.concat(taicpu.op_reg_reg(A_ADDZE,resultreg,resultreg));
  106. end
  107. else
  108. begin
  109. { load divider in a register if necessary }
  110. case right.location.loc of
  111. LOC_CREGISTER, LOC_REGISTER:
  112. divider := right.location.register;
  113. LOC_REFERENCE, LOC_CREFERENCE:
  114. begin
  115. divider := cg.get_scratch_reg_int(exprasmlist);
  116. cg.a_load_ref_reg(exprasmlist,OS_32,
  117. right.location.reference,divider);
  118. reference_release(exprasmlist,right.location.reference);
  119. end;
  120. end;
  121. { needs overflow checking, (-maxlongint-1) div (-1) overflows! }
  122. { And on PPC, the only way to catch a div-by-0 is by checking }
  123. { the overflow flag (JM) }
  124. op := divops[is_signed(right.resulttype.def),
  125. cs_check_overflow in aktlocalswitches];
  126. exprasmlist.concat(taicpu.op_reg_reg_reg(op,resultreg,numerator,
  127. divider))
  128. end;
  129. { free used registers }
  130. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  131. cg.free_scratch_reg(exprasmlist,divider)
  132. else
  133. rg.ungetregister(exprasmlist,divider);
  134. if numerator <> resultreg then
  135. rg.ungetregisterint(exprasmlist,numerator);
  136. { set result location }
  137. location.loc:=LOC_REGISTER;
  138. location.register:=resultreg;
  139. cg.g_overflowcheck(exprasmlist,self);
  140. end;
  141. {*****************************************************************************
  142. TPPCSHLRSHRNODE
  143. *****************************************************************************}
  144. procedure tppcshlshrnode.pass_2;
  145. var
  146. resultreg, hregister1,hregister2,
  147. hregisterhigh,hregisterlow : tregister;
  148. op : topcg;
  149. asmop1, asmop2: tasmop;
  150. shiftval: aword;
  151. saved : tmaybesave;
  152. begin
  153. secondpass(left);
  154. maybe_save(exprasmlist,right.registers32,left.location,saved);
  155. secondpass(right);
  156. maybe_restore(exprasmlist,left.location,saved);
  157. if is_64bitint(left.resulttype.def) then
  158. begin
  159. case left.location.loc of
  160. LOC_REGISTER, LOC_CREGISTER:
  161. begin
  162. hregisterhigh := left.location.registerhigh;
  163. hregisterlow := left.location.registerlow;
  164. if left.location.loc = LOC_REGISTER then
  165. begin
  166. location.registerhigh := hregisterhigh;
  167. location.registerlow := hregisterlow
  168. end
  169. else
  170. begin
  171. location.registerhigh := rg.getregisterint(exprasmlist);
  172. location.registerlow := rg.getregisterint(exprasmlist);
  173. end;
  174. end;
  175. LOC_REFERENCE,LOC_CREFERENCE:
  176. begin
  177. { !!!!!!!! not good, registers are release too soon this way !!!! (JM) }
  178. reference_release(exprasmlist,left.location.reference);
  179. hregisterhigh := rg.getregisterint(exprasmlist);
  180. location.registerhigh := hregisterhigh;
  181. hregisterlow := rg.getregisterint(exprasmlist);
  182. location.registerlow := hregisterlow;
  183. tcg64f32(cg).a_load64_ref_reg(exprasmlist,
  184. left.location.reference,joinreg64(hregisterlow,hregisterhigh));
  185. end;
  186. end;
  187. if (right.nodetype = ordconstn) then
  188. begin
  189. shiftval := tordconstnode(right).value;
  190. if tordconstnode(right).value > 31 then
  191. begin
  192. if nodetype = shln then
  193. begin
  194. if (shiftval and 31) <> 0 then
  195. cg.a_op_const_reg_reg(exprasmlist,OP_SHL,OS_32,
  196. shiftval and 31,hregisterlow,location.registerhigh);
  197. cg.a_load_const_reg(exprasmlist,OS_32,0,location.registerlow);
  198. end
  199. else
  200. begin
  201. if (shiftval and 31) <> 0 then
  202. cg.a_op_const_reg_reg(exprasmlist,OP_SHR,OS_32,
  203. shiftval and 31,hregisterhigh,location.registerlow);
  204. cg.a_load_const_reg(exprasmlist,OS_32,0,location.registerhigh);
  205. end;
  206. end
  207. else
  208. begin
  209. if nodetype = shln then
  210. begin
  211. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  212. A_RLWINM,location.registerhigh,hregisterhigh,shiftval,
  213. 0,31-shiftval));
  214. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  215. A_RLWIMI,location.registerhigh,hregisterlow,shiftval,
  216. 32-shiftval,31));
  217. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  218. A_RLWINM,location.registerlow,hregisterlow,shiftval,
  219. 0,31-shiftval));
  220. end
  221. else
  222. begin
  223. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  224. A_RLWINM,location.registerlow,hregisterlow,32-shiftval,
  225. shiftval,31));
  226. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  227. A_RLWIMI,location.registerlow,hregisterhigh,32-shiftval,
  228. 0,shiftval-1));
  229. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  230. A_RLWINM,location.registerhigh,hregisterhigh,32-shiftval,
  231. shiftval,31));
  232. end;
  233. end;
  234. end
  235. else
  236. { no constant shiftcount }
  237. begin
  238. case right.location.loc of
  239. LOC_REGISTER,LOC_CREGISTER:
  240. begin
  241. hregister1 := right.location.register;
  242. end;
  243. LOC_REFERENCE,LOC_CREFERENCE:
  244. begin
  245. hregister1 := cg.get_scratch_reg_int(exprasmlist);
  246. cg.a_load_ref_reg(exprasmlist,OS_S32,
  247. right.location.reference,hregister1);
  248. end;
  249. end;
  250. if nodetype = shln then
  251. begin
  252. asmop1 := A_SLW;
  253. asmop2 := A_SRW;
  254. end
  255. else
  256. begin
  257. asmop1 := A_SRW;
  258. asmop2 := A_SLW;
  259. resultreg := location.registerhigh;
  260. location.registerhigh := location.registerlow;
  261. location.registerlow := resultreg;
  262. end;
  263. rg.getexplicitregisterint(exprasmlist,R_0);
  264. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  265. R_0,hregister1,32));
  266. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,
  267. location.registerhigh,hregisterhigh,hregister1));
  268. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop2,
  269. R_0,hregisterlow,R_0));
  270. exprasmlist.concat(taicpu.op_reg_reg_reg(A_OR,
  271. location.registerhigh,location.registerhigh,R_0));
  272. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBI,
  273. R_0,hregister1,32));
  274. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,
  275. R_0,hregisterlow,R_0));
  276. exprasmlist.concat(taicpu.op_reg_reg_reg(A_OR,
  277. location.registerhigh,location.registerhigh,R_0));
  278. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,
  279. location.registerlow,hregisterlow,hregister1));
  280. rg.ungetregister(exprasmlist,R_0);
  281. if right.location.loc in [LOC_CREFERENCE,LOC_REFERENCE] then
  282. cg.free_scratch_reg(exprasmlist,hregister1)
  283. else
  284. rg.ungetregister(exprasmlist,hregister1);
  285. end
  286. end
  287. else
  288. begin
  289. { load left operators in a register }
  290. if (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  291. begin
  292. reference_release(exprasmlist,left.location.reference);
  293. hregister1 := rg.getregisterint(exprasmlist);
  294. { OS_32 because everything is always converted to longint/ }
  295. { cardinal in the resulttype pass (JM) }
  296. cg.a_load_ref_reg(exprasmlist,OS_32,left.location.reference,
  297. hregister1);
  298. resultreg := hregister1;
  299. end
  300. else
  301. begin
  302. hregister1 := left.location.register;
  303. if left.location.loc = LOC_CREGISTER then
  304. resultreg := rg.getregisterint(exprasmlist)
  305. else
  306. resultreg := hregister1;
  307. end;
  308. { determine operator }
  309. if nodetype=shln then
  310. op:=OP_SHL
  311. else
  312. op:=OP_SHR;
  313. { shifting by a constant directly coded: }
  314. if (right.nodetype=ordconstn) then
  315. cg.a_op_const_reg_reg(exprasmlist,op,OS_32,
  316. tordconstnode(right).value and 31,hregister1,resultreg)
  317. else
  318. begin
  319. { load shift count in a register if necessary }
  320. case right.location.loc of
  321. LOC_CREGISTER, LOC_REGISTER:
  322. hregister2 := right.location.register;
  323. LOC_REFERENCE, LOC_CREFERENCE:
  324. begin
  325. hregister2 := cg.get_scratch_reg_int(exprasmlist);
  326. cg.a_load_ref_reg(exprasmlist,OS_32,
  327. right.location.reference,hregister2);
  328. reference_release(exprasmlist,right.location.reference);
  329. end;
  330. end;
  331. tcgppc(cg).a_op_reg_reg_reg(exprasmlist,op,OS_32,hregister1,
  332. hregister2,resultreg);
  333. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  334. cg.free_scratch_reg(exprasmlist,hregister2)
  335. else
  336. rg.ungetregister(exprasmlist,hregister2);
  337. end;
  338. { set result location }
  339. location.loc:=LOC_REGISTER;
  340. location.register:=resultreg;
  341. end;
  342. end;
  343. {*****************************************************************************
  344. TPPCUNARYMINUSNODE
  345. *****************************************************************************}
  346. procedure tppcunaryminusnode.pass_2;
  347. var
  348. src1, src2, tmp: tregister;
  349. op: tasmop;
  350. begin
  351. secondpass(left);
  352. if is_64bitint(left.resulttype.def) then
  353. begin
  354. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),false);
  355. location_copy(location,left.location);
  356. exprasmlist.concat(taicpu.op_reg_reg(A_NEG,location.registerlow,
  357. src1));
  358. cg.a_op_reg_reg(exprasmlist,OP_NOT,OS_32,src2,location.registerhigh);
  359. tmp := cg.get_scratch_reg_int(exprasmlist);
  360. cg.a_op_const_reg_reg(exprasmlist,OP_SAR,OS_32,31,location.registerlow,
  361. tmp);
  362. if not(cs_check_overflow in aktlocalswitches) then
  363. cg.a_op_reg_reg(exprasmlist,OP_ADD,OS_32,location.registerhigh,
  364. tmp)
  365. else
  366. exprasmlist.concat(taicpu.op_reg_reg_reg(A_ADDO_,tmp,
  367. location.registerhigh,tmp));
  368. cg.free_scratch_reg(exprasmlist,tmp);
  369. end
  370. else
  371. begin
  372. location.loc:=LOC_REGISTER;
  373. case left.location.loc of
  374. LOC_FPUREGISTER, LOC_REGISTER:
  375. begin
  376. src1 := left.location.register;
  377. location.register := src1;
  378. end;
  379. LOC_CFPUREGISTER, LOC_CREGISTER:
  380. begin
  381. src1 := left.location.register;
  382. if left.location.loc = LOC_CREGISTER then
  383. location.register := rg.getregisterint(exprasmlist)
  384. else
  385. location.register := rg.getregisterfpu(exprasmlist);
  386. end;
  387. LOC_REFERENCE,LOC_CREFERENCE:
  388. begin
  389. reference_release(exprasmlist,left.location.reference);
  390. if (left.resulttype.def.deftype=floatdef) then
  391. begin
  392. src1 := rg.getregisterfpu(exprasmlist);
  393. location.register := src1;
  394. cg.a_loadfpu_ref_reg(exprasmlist,
  395. def_cgsize(left.resulttype.def),
  396. left.location.reference,src1);
  397. end
  398. else
  399. begin
  400. src1 := rg.getregisterint(exprasmlist);
  401. location.register:= src1;
  402. cg.a_load_ref_reg(exprasmlist,OS_32,
  403. left.location.reference,src1);
  404. end;
  405. end;
  406. end;
  407. { choose appropriate operand }
  408. if left.resulttype.def.deftype <> floatdef then
  409. if not(cs_check_overflow in aktlocalswitches) then
  410. op := A_NEG
  411. else
  412. op := A_NEGO_
  413. else
  414. op := A_FNEG;
  415. { emit operation }
  416. exprasmlist.concat(taicpu.op_reg_reg(op,location.register,src1));
  417. end;
  418. { Here was a problem... }
  419. { Operand to be negated always }
  420. { seems to be converted to signed }
  421. { 32-bit before doing neg!! }
  422. { So this is useless... }
  423. { that's not true: -2^31 gives an overflow error if it is negated (FK) }
  424. cg.g_overflowcheck(exprasmlist,self);
  425. end;
  426. {*****************************************************************************
  427. TPPCNOTNODE
  428. *****************************************************************************}
  429. procedure tppcnotnode.pass_2;
  430. var
  431. hl : tasmlabel;
  432. regl, regh: tregister;
  433. begin
  434. if is_boolean(resulttype.def) then
  435. begin
  436. { the second pass could change the location of left }
  437. { if it is a register variable, so we've to do }
  438. { this before the case statement }
  439. if left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,
  440. LOC_FLAGS,LOC_REGISTER,LOC_CREGISTER] then
  441. secondpass(left);
  442. case left.location.loc of
  443. LOC_JUMP :
  444. begin
  445. hl:=truelabel;
  446. truelabel:=falselabel;
  447. falselabel:=hl;
  448. secondpass(left);
  449. maketojumpbool(exprasmlist,left,lr_load_regvars);
  450. hl:=truelabel;
  451. truelabel:=falselabel;
  452. falselabel:=hl;
  453. end;
  454. LOC_FLAGS :
  455. begin
  456. location.resflags:=left.location.resflags;
  457. {$warning !!!}
  458. // inverse_flags(left.location.resflags);
  459. end;
  460. LOC_REGISTER, LOC_CREGISTER, LOC_REFERENCE, LOC_CREFERENCE :
  461. begin
  462. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  463. regl := left.location.register
  464. else
  465. begin
  466. regl := rg.getregisterint(exprasmlist);
  467. cg.a_load_ref_reg(exprasmlist,def_cgsize(left.resulttype.def),
  468. left.location.reference,regl);
  469. end;
  470. location.loc:=LOC_FLAGS;
  471. location.resflags.cr:=r_cr0;
  472. location.resflags.flag:=F_EQ;
  473. exprasmlist.concat(taicpu.op_reg_const(A_CMPWI,regl,0));
  474. rg.ungetregister(exprasmlist,regl);
  475. end;
  476. end;
  477. end
  478. else if is_64bitint(left.resulttype.def) then
  479. begin
  480. secondpass(left);
  481. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),false);
  482. location_copy(location,left.location);
  483. { perform the NOT operation }
  484. exprasmlist.concat(taicpu.op_reg_reg(A_NOT,location.registerhigh,
  485. regh));
  486. exprasmlist.concat(taicpu.op_reg_reg(A_NOT,location.registerlow,
  487. regl));
  488. end
  489. else
  490. begin
  491. secondpass(left);
  492. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),false);
  493. location_copy(location,left.location);
  494. if location.loc=LOC_CREGISTER then
  495. location.register := rg.getregisterint(exprasmlist);
  496. { perform the NOT operation }
  497. exprasmlist.concat(taicpu.op_reg_reg(A_NOT,location.register,
  498. left.location.register));
  499. end;
  500. end;
  501. begin
  502. cmoddivnode:=tppcmoddivnode;
  503. cshlshrnode:=tppcshlshrnode;
  504. cunaryminusnode:=tppcunaryminusnode;
  505. cnotnode:=tppcnotnode;
  506. end.
  507. {
  508. $Log$
  509. Revision 1.11 2002-07-07 09:44:32 florian
  510. * powerpc target fixed, very simple units can be compiled
  511. Revision 1.10 2002/05/20 13:30:42 carl
  512. * bugfix of hdisponen (base must be set, not index)
  513. * more portability fixes
  514. Revision 1.9 2002/05/18 13:34:26 peter
  515. * readded missing revisions
  516. Revision 1.8 2002/05/16 19:46:53 carl
  517. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  518. + try to fix temp allocation (still in ifdef)
  519. + generic constructor calls
  520. + start of tassembler / tmodulebase class cleanup
  521. Revision 1.5 2002/05/13 19:52:46 peter
  522. * a ppcppc can be build again
  523. Revision 1.4 2002/04/21 15:48:39 carl
  524. * some small updates according to i386 version
  525. Revision 1.3 2002/04/06 18:13:02 jonas
  526. * several powerpc-related additions and fixes
  527. Revision 1.2 2002/01/03 14:57:52 jonas
  528. * completed (not compilale yet though)
  529. }