nppcmat.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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. { everything will be handled in pass_2 }
  30. function first_shlshr64bitint: tnode; override;
  31. end;
  32. tppcunaryminusnode = class(tunaryminusnode)
  33. procedure pass_2;override;
  34. end;
  35. tppcnotnode = class(tnotnode)
  36. procedure pass_2;override;
  37. end;
  38. implementation
  39. uses
  40. globtype,systems,
  41. cutils,verbose,globals,
  42. symconst,symdef,
  43. aasmbase,aasmcpu,aasmtai,
  44. defutil,
  45. cgbase,cgobj,pass_1,pass_2,
  46. ncon,
  47. cpubase,cpuinfo,cginfo,
  48. ncgutil,cga,cgcpu,cg64f32,rgobj;
  49. {*****************************************************************************
  50. TPPCMODDIVNODE
  51. *****************************************************************************}
  52. procedure tppcmoddivnode.pass_2;
  53. const
  54. { signed overflow }
  55. divops: array[boolean, boolean] of tasmop =
  56. ((A_DIVWU,A_DIVWUO_),(A_DIVW,A_DIVWO_));
  57. var
  58. power,
  59. l1, l2 : longint;
  60. op : tasmop;
  61. numerator,
  62. divider,
  63. resultreg : tregister;
  64. saved : tmaybesave;
  65. size : Tcgsize;
  66. begin
  67. secondpass(left);
  68. maybe_save(exprasmlist,right.registers32,left.location,saved);
  69. secondpass(right);
  70. maybe_restore(exprasmlist,left.location,saved);
  71. location_copy(location,left.location);
  72. { put numerator in register }
  73. size:=def_cgsize(left.resulttype.def);
  74. location_force_reg(exprasmlist,left.location,
  75. size,true);
  76. location_copy(location,left.location);
  77. numerator := location.register;
  78. resultreg := location.register;
  79. if (location.loc = LOC_CREGISTER) then
  80. begin
  81. location.loc := LOC_REGISTER;
  82. location.register := rg.getregisterint(exprasmlist,size);
  83. resultreg := location.register;
  84. end;
  85. if (nodetype = modn) then
  86. begin
  87. resultreg := cg.get_scratch_reg_int(exprasmlist,size);
  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. location_force_reg(exprasmlist,right.location,
  111. def_cgsize(right.resulttype.def),true);
  112. divider := right.location.register;
  113. { needs overflow checking, (-maxlongint-1) div (-1) overflows! }
  114. { And on PPC, the only way to catch a div-by-0 is by checking }
  115. { the overflow flag (JM) }
  116. op := divops[is_signed(right.resulttype.def),
  117. cs_check_overflow in aktlocalswitches];
  118. exprasmlist.concat(taicpu.op_reg_reg_reg(op,resultreg,numerator,
  119. divider));
  120. if (nodetype = modn) then
  121. begin
  122. exprasmlist.concat(taicpu.op_reg_reg_reg(A_MULLW,resultreg,
  123. divider,resultreg));
  124. rg.ungetregisterint(exprasmlist,divider);
  125. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SUB,location.register,
  126. numerator,resultreg));
  127. cg.free_scratch_reg(exprasmlist,resultreg);
  128. resultreg := location.register;
  129. end
  130. else
  131. rg.ungetregisterint(exprasmlist,divider);
  132. end;
  133. { free used registers }
  134. if numerator.number <> resultreg.number 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. function tppcshlshrnode.first_shlshr64bitint: tnode;
  145. begin
  146. result := nil;
  147. end;
  148. procedure tppcshlshrnode.pass_2;
  149. var
  150. resultreg, hregister1,hregister2,
  151. hregisterhigh,hregisterlow : tregister;
  152. op : topcg;
  153. asmop1, asmop2: tasmop;
  154. shiftval: aword;
  155. saved : tmaybesave;
  156. r : Tregister;
  157. begin
  158. secondpass(left);
  159. maybe_save(exprasmlist,right.registers32,left.location,saved);
  160. secondpass(right);
  161. maybe_restore(exprasmlist,left.location,saved);
  162. if is_64bitint(left.resulttype.def) then
  163. begin
  164. location_force_reg(exprasmlist,left.location,
  165. def_cgsize(left.resulttype.def),true);
  166. location_copy(location,left.location);
  167. hregisterhigh := location.registerhigh;
  168. hregisterlow := location.registerlow;
  169. if (location.loc = LOC_CREGISTER) then
  170. begin
  171. location.loc := LOC_REGISTER;
  172. location.registerhigh := rg.getregisterint(exprasmlist,OS_32);
  173. location.registerlow := rg.getregisterint(exprasmlist,OS_32);
  174. end;
  175. if (right.nodetype = ordconstn) then
  176. begin
  177. shiftval := tordconstnode(right).value;
  178. if tordconstnode(right).value > 31 then
  179. begin
  180. if nodetype = shln then
  181. begin
  182. if (shiftval and 31) <> 0 then
  183. cg.a_op_const_reg_reg(exprasmlist,OP_SHL,OS_32,
  184. shiftval and 31,hregisterlow,location.registerhigh);
  185. cg.a_load_const_reg(exprasmlist,OS_32,0,location.registerlow);
  186. end
  187. else
  188. begin
  189. if (shiftval and 31) <> 0 then
  190. cg.a_op_const_reg_reg(exprasmlist,OP_SHR,OS_32,
  191. shiftval and 31,hregisterhigh,location.registerlow);
  192. cg.a_load_const_reg(exprasmlist,OS_32,0,location.registerhigh);
  193. end;
  194. end
  195. else
  196. begin
  197. if nodetype = shln then
  198. begin
  199. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  200. A_RLWINM,location.registerhigh,hregisterhigh,shiftval,
  201. 0,31-shiftval));
  202. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  203. A_RLWIMI,location.registerhigh,hregisterlow,shiftval,
  204. 32-shiftval,31));
  205. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  206. A_RLWINM,location.registerlow,hregisterlow,shiftval,
  207. 0,31-shiftval));
  208. end
  209. else
  210. begin
  211. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  212. A_RLWINM,location.registerlow,hregisterlow,32-shiftval,
  213. shiftval,31));
  214. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  215. A_RLWIMI,location.registerlow,hregisterhigh,32-shiftval,
  216. 0,shiftval-1));
  217. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  218. A_RLWINM,location.registerhigh,hregisterhigh,32-shiftval,
  219. shiftval,31));
  220. end;
  221. end;
  222. end
  223. else
  224. { no constant shiftcount }
  225. begin
  226. location_force_reg(exprasmlist,right.location,OS_S32,true);
  227. hregister1 := right.location.register;
  228. if nodetype = shln then
  229. begin
  230. asmop1 := A_SLW;
  231. asmop2 := A_SRW;
  232. end
  233. else
  234. begin
  235. asmop1 := A_SRW;
  236. asmop2 := A_SLW;
  237. resultreg := location.registerhigh;
  238. location.registerhigh := location.registerlow;
  239. location.registerlow := resultreg;
  240. end;
  241. rg.getexplicitregisterint(exprasmlist,NR_R0);
  242. r.enum:=R_INTREGISTER;
  243. r.number:=NR_R0;
  244. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  245. r,hregister1,32));
  246. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,
  247. location.registerhigh,hregisterhigh,hregister1));
  248. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop2,
  249. r,hregisterlow,r));
  250. exprasmlist.concat(taicpu.op_reg_reg_reg(A_OR,
  251. location.registerhigh,location.registerhigh,r));
  252. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBI,
  253. r,hregister1,32));
  254. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,
  255. r,hregisterlow,r));
  256. exprasmlist.concat(taicpu.op_reg_reg_reg(A_OR,
  257. location.registerhigh,location.registerhigh,r));
  258. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,
  259. location.registerlow,hregisterlow,hregister1));
  260. rg.ungetregisterint(exprasmlist,r);
  261. if right.location.loc in [LOC_CREFERENCE,LOC_REFERENCE] then
  262. cg.free_scratch_reg(exprasmlist,hregister1)
  263. else
  264. rg.ungetregisterint(exprasmlist,hregister1);
  265. end
  266. end
  267. else
  268. begin
  269. { load left operators in a register }
  270. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  271. location_copy(location,left.location);
  272. resultreg := location.register;
  273. hregister1 := location.register;
  274. if (location.loc = LOC_CREGISTER) then
  275. begin
  276. location.loc := LOC_REGISTER;
  277. resultreg := rg.getregisterint(exprasmlist,OS_32);
  278. location.register := resultreg;
  279. end;
  280. { determine operator }
  281. if nodetype=shln then
  282. op:=OP_SHL
  283. else
  284. op:=OP_SHR;
  285. { shifting by a constant directly coded: }
  286. if (right.nodetype=ordconstn) then
  287. cg.a_op_const_reg_reg(exprasmlist,op,OS_32,
  288. tordconstnode(right).value and 31,hregister1,resultreg)
  289. else
  290. begin
  291. { load shift count in a register if necessary }
  292. location_force_reg(exprasmlist,right.location,def_cgsize(right.resulttype.def),true);
  293. hregister2 := right.location.register;
  294. cg.a_op_reg_reg_reg(exprasmlist,op,OS_32,hregister2,
  295. hregister1,resultreg);
  296. rg.ungetregisterint(exprasmlist,hregister2);
  297. end;
  298. end;
  299. end;
  300. {*****************************************************************************
  301. TPPCUNARYMINUSNODE
  302. *****************************************************************************}
  303. procedure tppcunaryminusnode.pass_2;
  304. var
  305. src1, src2, tmp: tregister;
  306. op: tasmop;
  307. begin
  308. secondpass(left);
  309. if is_64bitint(left.resulttype.def) then
  310. begin
  311. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  312. location_copy(location,left.location);
  313. if (location.loc = LOC_CREGISTER) then
  314. begin
  315. location.registerlow := rg.getregisterint(exprasmlist,OS_INT);
  316. location.registerhigh := rg.getregisterint(exprasmlist,OS_INT);
  317. location.loc := LOC_CREGISTER;
  318. end;
  319. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  320. location.registerlow,left.location.registerlow,0));
  321. if not(cs_check_overflow in aktlocalswitches) then
  322. exprasmlist.concat(taicpu.op_reg_reg(A_SUBFZE,
  323. location.registerhigh,left.location.registerhigh))
  324. else
  325. exprasmlist.concat(taicpu.op_reg_reg(A_SUBFZEO_,
  326. location.registerhigh,left.location.registerhigh));
  327. end
  328. else
  329. begin
  330. location_copy(location,left.location);
  331. location.loc:=LOC_REGISTER;
  332. case left.location.loc of
  333. LOC_FPUREGISTER, LOC_REGISTER:
  334. begin
  335. src1 := left.location.register;
  336. location.register := src1;
  337. end;
  338. LOC_CFPUREGISTER, LOC_CREGISTER:
  339. begin
  340. src1 := left.location.register;
  341. if left.location.loc = LOC_CREGISTER then
  342. location.register := rg.getregisterint(exprasmlist,OS_INT)
  343. else
  344. location.register := rg.getregisterfpu(exprasmlist);
  345. end;
  346. LOC_REFERENCE,LOC_CREFERENCE:
  347. begin
  348. if (left.resulttype.def.deftype=floatdef) then
  349. begin
  350. src1 := rg.getregisterfpu(exprasmlist);
  351. location.register := src1;
  352. cg.a_loadfpu_ref_reg(exprasmlist,
  353. def_cgsize(left.resulttype.def),
  354. left.location.reference,src1);
  355. end
  356. else
  357. begin
  358. src1 := rg.getregisterint(exprasmlist,OS_32);
  359. location.register:= src1;
  360. cg.a_load_ref_reg(exprasmlist,OS_32,
  361. left.location.reference,src1);
  362. end;
  363. reference_release(exprasmlist,left.location.reference);
  364. end;
  365. end;
  366. { choose appropriate operand }
  367. if left.resulttype.def.deftype <> floatdef then
  368. begin
  369. if not(cs_check_overflow in aktlocalswitches) then
  370. op := A_NEG
  371. else
  372. op := A_NEGO_;
  373. location.loc := LOC_REGISTER;
  374. end
  375. else
  376. begin
  377. op := A_FNEG;
  378. location.loc := LOC_FPUREGISTER;
  379. end;
  380. { emit operation }
  381. exprasmlist.concat(taicpu.op_reg_reg(op,location.register,src1));
  382. end;
  383. { Here was a problem... }
  384. { Operand to be negated always }
  385. { seems to be converted to signed }
  386. { 32-bit before doing neg!! }
  387. { So this is useless... }
  388. { that's not true: -2^31 gives an overflow error if it is negated (FK) }
  389. cg.g_overflowcheck(exprasmlist,self);
  390. end;
  391. {*****************************************************************************
  392. TPPCNOTNODE
  393. *****************************************************************************}
  394. procedure tppcnotnode.pass_2;
  395. var
  396. hl : tasmlabel;
  397. regl, regh: tregister;
  398. begin
  399. if is_boolean(resulttype.def) then
  400. begin
  401. { if the location is LOC_JUMP, we do the secondpass after the
  402. labels are allocated
  403. }
  404. if left.expectloc=LOC_JUMP then
  405. begin
  406. hl:=truelabel;
  407. truelabel:=falselabel;
  408. falselabel:=hl;
  409. secondpass(left);
  410. maketojumpbool(exprasmlist,left,lr_load_regvars);
  411. hl:=truelabel;
  412. truelabel:=falselabel;
  413. falselabel:=hl;
  414. location.loc:=LOC_JUMP;
  415. end
  416. else
  417. begin
  418. secondpass(left);
  419. case left.location.loc of
  420. LOC_FLAGS :
  421. begin
  422. location_copy(location,left.location);
  423. inverse_flags(location.resflags);
  424. end;
  425. LOC_REGISTER, LOC_CREGISTER, LOC_REFERENCE, LOC_CREFERENCE :
  426. begin
  427. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  428. exprasmlist.concat(taicpu.op_reg_const(A_CMPWI,left.location.register,0));
  429. location_release(exprasmlist,left.location);
  430. location_reset(location,LOC_FLAGS,OS_NO);
  431. location.resflags.cr:=r_cr0;
  432. location.resflags.flag:=F_EQ;
  433. end;
  434. else
  435. internalerror(2003042401);
  436. end;
  437. end;
  438. end
  439. else if is_64bitint(left.resulttype.def) then
  440. begin
  441. secondpass(left);
  442. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),false);
  443. location_copy(location,left.location);
  444. { perform the NOT operation }
  445. exprasmlist.concat(taicpu.op_reg_reg(A_NOT,location.registerhigh,
  446. location.registerhigh));
  447. exprasmlist.concat(taicpu.op_reg_reg(A_NOT,location.registerlow,
  448. location.registerlow));
  449. end
  450. else
  451. begin
  452. secondpass(left);
  453. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),false);
  454. location_copy(location,left.location);
  455. if location.loc=LOC_CREGISTER then
  456. location.register := rg.getregisterint(exprasmlist,OS_INT);
  457. { perform the NOT operation }
  458. exprasmlist.concat(taicpu.op_reg_reg(A_NOT,location.register,
  459. left.location.register));
  460. end;
  461. end;
  462. begin
  463. cmoddivnode:=tppcmoddivnode;
  464. cshlshrnode:=tppcshlshrnode;
  465. cunaryminusnode:=tppcunaryminusnode;
  466. cnotnode:=tppcnotnode;
  467. end.
  468. {
  469. $Log$
  470. Revision 1.25 2003-04-24 12:57:32 florian
  471. * fixed not node
  472. Revision 1.24 2003/03/11 21:46:24 jonas
  473. * lots of new regallocator fixes, both in generic and ppc-specific code
  474. (ppc compiler still can't compile the linux system unit though)
  475. Revision 1.23 2003/02/19 22:00:16 daniel
  476. * Code generator converted to new register notation
  477. - Horribily outdated todo.txt removed
  478. Revision 1.22 2003/01/09 20:41:10 florian
  479. * fixed broken PowerPC compiler
  480. Revision 1.21 2003/01/08 18:43:58 daniel
  481. * Tregister changed into a record
  482. Revision 1.20 2002/11/25 17:43:28 peter
  483. * splitted defbase in defutil,symutil,defcmp
  484. * merged isconvertable and is_equal into compare_defs(_ext)
  485. * made operator search faster by walking the list only once
  486. Revision 1.19 2002/09/10 21:21:29 jonas
  487. * fixed unary minus of 64bit values
  488. Revision 1.18 2002/09/07 15:25:14 peter
  489. * old logs removed and tabs fixed
  490. Revision 1.17 2002/08/15 15:15:55 carl
  491. * jmpbuf size allocation for exceptions is now cpu specific (as it should)
  492. * more generic nodes for maths
  493. * several fixes for better m68k support
  494. Revision 1.16 2002/08/10 17:15:31 jonas
  495. * various fixes and optimizations
  496. Revision 1.15 2002/07/26 10:48:34 jonas
  497. * fixed bug in shl/shr code
  498. Revision 1.14 2002/07/20 11:58:05 florian
  499. * types.pas renamed to defbase.pas because D6 contains a types
  500. unit so this would conflicts if D6 programms are compiled
  501. + Willamette/SSE2 instructions to assembler added
  502. Revision 1.13 2002/07/11 07:41:27 jonas
  503. * fixed tppcmoddivnode
  504. * fixed 64bit parts of tppcshlshrnode
  505. Revision 1.12 2002/07/09 19:45:01 jonas
  506. * unarynminus and shlshr node fixed for 32bit and smaller ordinals
  507. * small fixes in the assembler writer
  508. * changed scratch registers, because they were used by the linker (r11
  509. and r12) and by the abi under linux (r31)
  510. Revision 1.11 2002/07/07 09:44:32 florian
  511. * powerpc target fixed, very simple units can be compiled
  512. Revision 1.10 2002/05/20 13:30:42 carl
  513. * bugfix of hdisponen (base must be set, not index)
  514. * more portability fixes
  515. Revision 1.9 2002/05/18 13:34:26 peter
  516. * readded missing revisions
  517. Revision 1.8 2002/05/16 19:46:53 carl
  518. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  519. + try to fix temp allocation (still in ifdef)
  520. + generic constructor calls
  521. + start of tassembler / tmodulebase class cleanup
  522. Revision 1.5 2002/05/13 19:52:46 peter
  523. * a ppcppc can be build again
  524. Revision 1.4 2002/04/21 15:48:39 carl
  525. * some small updates according to i386 version
  526. Revision 1.3 2002/04/06 18:13:02 jonas
  527. * several powerpc-related additions and fixes
  528. Revision 1.2 2002/01/03 14:57:52 jonas
  529. * completed (not compilale yet though)
  530. }