nppcmat.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. cg.a_op_const_reg_reg(exprasmlist,OP_SHL,OS_32,
  183. shiftval and 31,hregisterlow,location.registerhigh);
  184. cg.a_load_const_reg(exprasmlist,OS_32,0,location.registerlow);
  185. end
  186. else
  187. begin
  188. cg.a_op_const_reg_reg(exprasmlist,OP_SHR,OS_32,
  189. shiftval and 31,hregisterhigh,location.registerlow);
  190. cg.a_load_const_reg(exprasmlist,OS_32,0,location.registerhigh);
  191. end;
  192. end
  193. else
  194. begin
  195. if nodetype = shln then
  196. begin
  197. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  198. A_RLWINM,location.registerhigh,hregisterhigh,shiftval,
  199. 0,31-shiftval));
  200. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  201. A_RLWIMI,location.registerhigh,hregisterlow,shiftval,
  202. 32-shiftval,31));
  203. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  204. A_RLWINM,location.registerlow,hregisterlow,shiftval,
  205. 0,31-shiftval));
  206. end
  207. else
  208. begin
  209. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  210. A_RLWINM,location.registerlow,hregisterlow,32-shiftval,
  211. shiftval,31));
  212. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  213. A_RLWIMI,location.registerlow,hregisterhigh,32-shiftval,
  214. 0,shiftval-1));
  215. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  216. A_RLWINM,location.registerhigh,hregisterhigh,32-shiftval,
  217. shiftval,31));
  218. end;
  219. end;
  220. end
  221. else
  222. { no constant shiftcount }
  223. begin
  224. location_force_reg(exprasmlist,right.location,OS_S32,true);
  225. hregister1 := right.location.register;
  226. if nodetype = shln then
  227. begin
  228. asmop1 := A_SLW;
  229. asmop2 := A_SRW;
  230. end
  231. else
  232. begin
  233. asmop1 := A_SRW;
  234. asmop2 := A_SLW;
  235. resultreg := location.registerhigh;
  236. location.registerhigh := location.registerlow;
  237. location.registerlow := resultreg;
  238. end;
  239. rg.getexplicitregisterint(exprasmlist,NR_R0);
  240. r.enum:=R_INTREGISTER;
  241. r.number:=NR_R0;
  242. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  243. r,hregister1,32));
  244. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,
  245. location.registerhigh,hregisterhigh,hregister1));
  246. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop2,
  247. r,hregisterlow,r));
  248. exprasmlist.concat(taicpu.op_reg_reg_reg(A_OR,
  249. location.registerhigh,location.registerhigh,r));
  250. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBI,
  251. r,hregister1,32));
  252. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,
  253. r,hregisterlow,r));
  254. exprasmlist.concat(taicpu.op_reg_reg_reg(A_OR,
  255. location.registerhigh,location.registerhigh,r));
  256. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,
  257. location.registerlow,hregisterlow,hregister1));
  258. rg.ungetregisterint(exprasmlist,r);
  259. if right.location.loc in [LOC_CREFERENCE,LOC_REFERENCE] then
  260. cg.free_scratch_reg(exprasmlist,hregister1)
  261. else
  262. rg.ungetregisterint(exprasmlist,hregister1);
  263. end
  264. end
  265. else
  266. begin
  267. { load left operators in a register }
  268. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  269. location_copy(location,left.location);
  270. resultreg := location.register;
  271. hregister1 := location.register;
  272. if (location.loc = LOC_CREGISTER) then
  273. begin
  274. location.loc := LOC_REGISTER;
  275. resultreg := rg.getregisterint(exprasmlist,OS_32);
  276. location.register := resultreg;
  277. end;
  278. { determine operator }
  279. if nodetype=shln then
  280. op:=OP_SHL
  281. else
  282. op:=OP_SHR;
  283. { shifting by a constant directly coded: }
  284. if (right.nodetype=ordconstn) then
  285. cg.a_op_const_reg_reg(exprasmlist,op,OS_32,
  286. tordconstnode(right).value and 31,hregister1,resultreg)
  287. else
  288. begin
  289. { load shift count in a register if necessary }
  290. location_force_reg(exprasmlist,right.location,def_cgsize(right.resulttype.def),true);
  291. hregister2 := right.location.register;
  292. cg.a_op_reg_reg_reg(exprasmlist,op,OS_32,hregister2,
  293. hregister1,resultreg);
  294. rg.ungetregisterint(exprasmlist,hregister2);
  295. end;
  296. end;
  297. end;
  298. {*****************************************************************************
  299. TPPCUNARYMINUSNODE
  300. *****************************************************************************}
  301. procedure tppcunaryminusnode.pass_2;
  302. var
  303. src1, src2, tmp: tregister;
  304. op: tasmop;
  305. begin
  306. secondpass(left);
  307. if is_64bitint(left.resulttype.def) then
  308. begin
  309. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  310. location_copy(location,left.location);
  311. if (location.loc = LOC_CREGISTER) then
  312. begin
  313. location.registerlow := rg.getregisterint(exprasmlist,OS_INT);
  314. location.registerhigh := rg.getregisterint(exprasmlist,OS_INT);
  315. location.loc := LOC_CREGISTER;
  316. end;
  317. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  318. location.registerlow,left.location.registerlow,0));
  319. if not(cs_check_overflow in aktlocalswitches) then
  320. exprasmlist.concat(taicpu.op_reg_reg(A_SUBFZE,
  321. location.registerhigh,left.location.registerhigh))
  322. else
  323. exprasmlist.concat(taicpu.op_reg_reg(A_SUBFZEO_,
  324. location.registerhigh,left.location.registerhigh));
  325. end
  326. else
  327. begin
  328. location_copy(location,left.location);
  329. location.loc:=LOC_REGISTER;
  330. case left.location.loc of
  331. LOC_FPUREGISTER, LOC_REGISTER:
  332. begin
  333. src1 := left.location.register;
  334. location.register := src1;
  335. end;
  336. LOC_CFPUREGISTER, LOC_CREGISTER:
  337. begin
  338. src1 := left.location.register;
  339. if left.location.loc = LOC_CREGISTER then
  340. location.register := rg.getregisterint(exprasmlist,OS_INT)
  341. else
  342. location.register := rg.getregisterfpu(exprasmlist);
  343. end;
  344. LOC_REFERENCE,LOC_CREFERENCE:
  345. begin
  346. if (left.resulttype.def.deftype=floatdef) then
  347. begin
  348. src1 := rg.getregisterfpu(exprasmlist);
  349. location.register := src1;
  350. cg.a_loadfpu_ref_reg(exprasmlist,
  351. def_cgsize(left.resulttype.def),
  352. left.location.reference,src1);
  353. end
  354. else
  355. begin
  356. src1 := rg.getregisterint(exprasmlist,OS_32);
  357. location.register:= src1;
  358. cg.a_load_ref_reg(exprasmlist,OS_32,
  359. left.location.reference,src1);
  360. end;
  361. reference_release(exprasmlist,left.location.reference);
  362. end;
  363. end;
  364. { choose appropriate operand }
  365. if left.resulttype.def.deftype <> floatdef then
  366. begin
  367. if not(cs_check_overflow in aktlocalswitches) then
  368. op := A_NEG
  369. else
  370. op := A_NEGO_;
  371. location.loc := LOC_REGISTER;
  372. end
  373. else
  374. begin
  375. op := A_FNEG;
  376. location.loc := LOC_FPUREGISTER;
  377. end;
  378. { emit operation }
  379. exprasmlist.concat(taicpu.op_reg_reg(op,location.register,src1));
  380. end;
  381. { Here was a problem... }
  382. { Operand to be negated always }
  383. { seems to be converted to signed }
  384. { 32-bit before doing neg!! }
  385. { So this is useless... }
  386. { that's not true: -2^31 gives an overflow error if it is negated (FK) }
  387. cg.g_overflowcheck(exprasmlist,self);
  388. end;
  389. {*****************************************************************************
  390. TPPCNOTNODE
  391. *****************************************************************************}
  392. procedure tppcnotnode.pass_2;
  393. var
  394. hl : tasmlabel;
  395. regl, regh: tregister;
  396. begin
  397. if is_boolean(resulttype.def) then
  398. begin
  399. { if the location is LOC_JUMP, we do the secondpass after the
  400. labels are allocated
  401. }
  402. if left.expectloc=LOC_JUMP then
  403. begin
  404. hl:=truelabel;
  405. truelabel:=falselabel;
  406. falselabel:=hl;
  407. secondpass(left);
  408. maketojumpbool(exprasmlist,left,lr_load_regvars);
  409. hl:=truelabel;
  410. truelabel:=falselabel;
  411. falselabel:=hl;
  412. location.loc:=LOC_JUMP;
  413. end
  414. else
  415. begin
  416. secondpass(left);
  417. case left.location.loc of
  418. LOC_FLAGS :
  419. begin
  420. location_copy(location,left.location);
  421. inverse_flags(location.resflags);
  422. end;
  423. LOC_REGISTER, LOC_CREGISTER, LOC_REFERENCE, LOC_CREFERENCE :
  424. begin
  425. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  426. exprasmlist.concat(taicpu.op_reg_const(A_CMPWI,left.location.register,0));
  427. location_release(exprasmlist,left.location);
  428. location_reset(location,LOC_FLAGS,OS_NO);
  429. location.resflags.cr:=r_cr0;
  430. location.resflags.flag:=F_EQ;
  431. end;
  432. else
  433. internalerror(2003042401);
  434. end;
  435. end;
  436. end
  437. else if is_64bitint(left.resulttype.def) then
  438. begin
  439. secondpass(left);
  440. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),false);
  441. location_copy(location,left.location);
  442. { perform the NOT operation }
  443. exprasmlist.concat(taicpu.op_reg_reg(A_NOT,location.registerhigh,
  444. location.registerhigh));
  445. exprasmlist.concat(taicpu.op_reg_reg(A_NOT,location.registerlow,
  446. location.registerlow));
  447. end
  448. else
  449. begin
  450. secondpass(left);
  451. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),false);
  452. location_copy(location,left.location);
  453. if location.loc=LOC_CREGISTER then
  454. location.register := rg.getregisterint(exprasmlist,OS_INT);
  455. { perform the NOT operation }
  456. exprasmlist.concat(taicpu.op_reg_reg(A_NOT,location.register,
  457. left.location.register));
  458. end;
  459. end;
  460. begin
  461. cmoddivnode:=tppcmoddivnode;
  462. cshlshrnode:=tppcshlshrnode;
  463. cunaryminusnode:=tppcunaryminusnode;
  464. cnotnode:=tppcnotnode;
  465. end.
  466. {
  467. $Log$
  468. Revision 1.26 2003-05-11 11:45:08 jonas
  469. * fixed shifts
  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. }