nppcmat.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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 := hregisterhigh;
  236. hregisterhigh := hregisterlow;
  237. hregisterlow := resultreg;
  238. resultreg := location.registerhigh;
  239. location.registerhigh := location.registerlow;
  240. location.registerlow := resultreg;
  241. end;
  242. rg.getexplicitregisterint(exprasmlist,NR_R0);
  243. r.enum:=R_INTREGISTER;
  244. r.number:=NR_R0;
  245. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  246. r,hregister1,32));
  247. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,
  248. location.registerhigh,hregisterhigh,hregister1));
  249. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop2,
  250. r,hregisterlow,r));
  251. exprasmlist.concat(taicpu.op_reg_reg_reg(A_OR,
  252. location.registerhigh,location.registerhigh,r));
  253. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBI,
  254. r,hregister1,32));
  255. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,
  256. r,hregisterlow,r));
  257. exprasmlist.concat(taicpu.op_reg_reg_reg(A_OR,
  258. location.registerhigh,location.registerhigh,r));
  259. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,
  260. location.registerlow,hregisterlow,hregister1));
  261. rg.ungetregisterint(exprasmlist,r);
  262. if nodetype = shrn then
  263. begin
  264. resultreg := location.registerhigh;
  265. location.registerhigh := location.registerlow;
  266. location.registerlow := resultreg;
  267. end;
  268. if right.location.loc in [LOC_CREFERENCE,LOC_REFERENCE] then
  269. cg.free_scratch_reg(exprasmlist,hregister1)
  270. else
  271. rg.ungetregisterint(exprasmlist,hregister1);
  272. end
  273. end
  274. else
  275. begin
  276. { load left operators in a register }
  277. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  278. location_copy(location,left.location);
  279. resultreg := location.register;
  280. hregister1 := location.register;
  281. if (location.loc = LOC_CREGISTER) then
  282. begin
  283. location.loc := LOC_REGISTER;
  284. resultreg := rg.getregisterint(exprasmlist,OS_32);
  285. location.register := resultreg;
  286. end;
  287. { determine operator }
  288. if nodetype=shln then
  289. op:=OP_SHL
  290. else
  291. op:=OP_SHR;
  292. { shifting by a constant directly coded: }
  293. if (right.nodetype=ordconstn) then
  294. cg.a_op_const_reg_reg(exprasmlist,op,OS_32,
  295. tordconstnode(right).value and 31,hregister1,resultreg)
  296. else
  297. begin
  298. { load shift count in a register if necessary }
  299. location_force_reg(exprasmlist,right.location,def_cgsize(right.resulttype.def),true);
  300. hregister2 := right.location.register;
  301. cg.a_op_reg_reg_reg(exprasmlist,op,OS_32,hregister2,
  302. hregister1,resultreg);
  303. rg.ungetregisterint(exprasmlist,hregister2);
  304. end;
  305. end;
  306. end;
  307. {*****************************************************************************
  308. TPPCUNARYMINUSNODE
  309. *****************************************************************************}
  310. procedure tppcunaryminusnode.pass_2;
  311. var
  312. src1, src2, tmp: tregister;
  313. op: tasmop;
  314. begin
  315. secondpass(left);
  316. if is_64bitint(left.resulttype.def) then
  317. begin
  318. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  319. location_copy(location,left.location);
  320. if (location.loc = LOC_CREGISTER) then
  321. begin
  322. location.registerlow := rg.getregisterint(exprasmlist,OS_INT);
  323. location.registerhigh := rg.getregisterint(exprasmlist,OS_INT);
  324. location.loc := LOC_CREGISTER;
  325. end;
  326. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  327. location.registerlow,left.location.registerlow,0));
  328. if not(cs_check_overflow in aktlocalswitches) then
  329. exprasmlist.concat(taicpu.op_reg_reg(A_SUBFZE,
  330. location.registerhigh,left.location.registerhigh))
  331. else
  332. exprasmlist.concat(taicpu.op_reg_reg(A_SUBFZEO_,
  333. location.registerhigh,left.location.registerhigh));
  334. end
  335. else
  336. begin
  337. location_copy(location,left.location);
  338. location.loc:=LOC_REGISTER;
  339. case left.location.loc of
  340. LOC_FPUREGISTER, LOC_REGISTER:
  341. begin
  342. src1 := left.location.register;
  343. location.register := src1;
  344. end;
  345. LOC_CFPUREGISTER, LOC_CREGISTER:
  346. begin
  347. src1 := left.location.register;
  348. if left.location.loc = LOC_CREGISTER then
  349. location.register := rg.getregisterint(exprasmlist,OS_INT)
  350. else
  351. location.register := rg.getregisterfpu(exprasmlist);
  352. end;
  353. LOC_REFERENCE,LOC_CREFERENCE:
  354. begin
  355. if (left.resulttype.def.deftype=floatdef) then
  356. begin
  357. src1 := rg.getregisterfpu(exprasmlist);
  358. location.register := src1;
  359. cg.a_loadfpu_ref_reg(exprasmlist,
  360. def_cgsize(left.resulttype.def),
  361. left.location.reference,src1);
  362. end
  363. else
  364. begin
  365. src1 := rg.getregisterint(exprasmlist,OS_32);
  366. location.register:= src1;
  367. cg.a_load_ref_reg(exprasmlist,OS_32,
  368. left.location.reference,src1);
  369. end;
  370. reference_release(exprasmlist,left.location.reference);
  371. end;
  372. end;
  373. { choose appropriate operand }
  374. if left.resulttype.def.deftype <> floatdef then
  375. begin
  376. if not(cs_check_overflow in aktlocalswitches) then
  377. op := A_NEG
  378. else
  379. op := A_NEGO_;
  380. location.loc := LOC_REGISTER;
  381. end
  382. else
  383. begin
  384. op := A_FNEG;
  385. location.loc := LOC_FPUREGISTER;
  386. end;
  387. { emit operation }
  388. exprasmlist.concat(taicpu.op_reg_reg(op,location.register,src1));
  389. end;
  390. { Here was a problem... }
  391. { Operand to be negated always }
  392. { seems to be converted to signed }
  393. { 32-bit before doing neg!! }
  394. { So this is useless... }
  395. { that's not true: -2^31 gives an overflow error if it is negated (FK) }
  396. cg.g_overflowcheck(exprasmlist,self);
  397. end;
  398. {*****************************************************************************
  399. TPPCNOTNODE
  400. *****************************************************************************}
  401. procedure tppcnotnode.pass_2;
  402. var
  403. hl : tasmlabel;
  404. regl, regh: tregister;
  405. begin
  406. if is_boolean(resulttype.def) then
  407. begin
  408. { if the location is LOC_JUMP, we do the secondpass after the
  409. labels are allocated
  410. }
  411. if left.expectloc=LOC_JUMP then
  412. begin
  413. hl:=truelabel;
  414. truelabel:=falselabel;
  415. falselabel:=hl;
  416. secondpass(left);
  417. maketojumpbool(exprasmlist,left,lr_load_regvars);
  418. hl:=truelabel;
  419. truelabel:=falselabel;
  420. falselabel:=hl;
  421. location.loc:=LOC_JUMP;
  422. end
  423. else
  424. begin
  425. secondpass(left);
  426. case left.location.loc of
  427. LOC_FLAGS :
  428. begin
  429. location_copy(location,left.location);
  430. inverse_flags(location.resflags);
  431. end;
  432. LOC_REGISTER, LOC_CREGISTER, LOC_REFERENCE, LOC_CREFERENCE :
  433. begin
  434. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  435. exprasmlist.concat(taicpu.op_reg_const(A_CMPWI,left.location.register,0));
  436. location_release(exprasmlist,left.location);
  437. location_reset(location,LOC_FLAGS,OS_NO);
  438. location.resflags.cr:=r_cr0;
  439. location.resflags.flag:=F_EQ;
  440. end;
  441. else
  442. internalerror(2003042401);
  443. end;
  444. end;
  445. end
  446. else if is_64bitint(left.resulttype.def) then
  447. begin
  448. secondpass(left);
  449. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),false);
  450. location_copy(location,left.location);
  451. { perform the NOT operation }
  452. exprasmlist.concat(taicpu.op_reg_reg(A_NOT,location.registerhigh,
  453. location.registerhigh));
  454. exprasmlist.concat(taicpu.op_reg_reg(A_NOT,location.registerlow,
  455. location.registerlow));
  456. end
  457. else
  458. begin
  459. secondpass(left);
  460. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),false);
  461. location_copy(location,left.location);
  462. if location.loc=LOC_CREGISTER then
  463. location.register := rg.getregisterint(exprasmlist,OS_INT);
  464. { perform the NOT operation }
  465. exprasmlist.concat(taicpu.op_reg_reg(A_NOT,location.register,
  466. left.location.register));
  467. end;
  468. end;
  469. begin
  470. cmoddivnode:=tppcmoddivnode;
  471. cshlshrnode:=tppcshlshrnode;
  472. cunaryminusnode:=tppcunaryminusnode;
  473. cnotnode:=tppcnotnode;
  474. end.
  475. {
  476. $Log$
  477. Revision 1.27 2003-05-24 19:15:29 jonas
  478. * fixed shr of 64 bit values by non-immediate value
  479. Revision 1.26 2003/05/11 11:45:08 jonas
  480. * fixed shifts
  481. Revision 1.25 2003/04/24 12:57:32 florian
  482. * fixed not node
  483. Revision 1.24 2003/03/11 21:46:24 jonas
  484. * lots of new regallocator fixes, both in generic and ppc-specific code
  485. (ppc compiler still can't compile the linux system unit though)
  486. Revision 1.23 2003/02/19 22:00:16 daniel
  487. * Code generator converted to new register notation
  488. - Horribily outdated todo.txt removed
  489. Revision 1.22 2003/01/09 20:41:10 florian
  490. * fixed broken PowerPC compiler
  491. Revision 1.21 2003/01/08 18:43:58 daniel
  492. * Tregister changed into a record
  493. Revision 1.20 2002/11/25 17:43:28 peter
  494. * splitted defbase in defutil,symutil,defcmp
  495. * merged isconvertable and is_equal into compare_defs(_ext)
  496. * made operator search faster by walking the list only once
  497. Revision 1.19 2002/09/10 21:21:29 jonas
  498. * fixed unary minus of 64bit values
  499. Revision 1.18 2002/09/07 15:25:14 peter
  500. * old logs removed and tabs fixed
  501. Revision 1.17 2002/08/15 15:15:55 carl
  502. * jmpbuf size allocation for exceptions is now cpu specific (as it should)
  503. * more generic nodes for maths
  504. * several fixes for better m68k support
  505. Revision 1.16 2002/08/10 17:15:31 jonas
  506. * various fixes and optimizations
  507. Revision 1.15 2002/07/26 10:48:34 jonas
  508. * fixed bug in shl/shr code
  509. Revision 1.14 2002/07/20 11:58:05 florian
  510. * types.pas renamed to defbase.pas because D6 contains a types
  511. unit so this would conflicts if D6 programms are compiled
  512. + Willamette/SSE2 instructions to assembler added
  513. Revision 1.13 2002/07/11 07:41:27 jonas
  514. * fixed tppcmoddivnode
  515. * fixed 64bit parts of tppcshlshrnode
  516. Revision 1.12 2002/07/09 19:45:01 jonas
  517. * unarynminus and shlshr node fixed for 32bit and smaller ordinals
  518. * small fixes in the assembler writer
  519. * changed scratch registers, because they were used by the linker (r11
  520. and r12) and by the abi under linux (r31)
  521. Revision 1.11 2002/07/07 09:44:32 florian
  522. * powerpc target fixed, very simple units can be compiled
  523. Revision 1.10 2002/05/20 13:30:42 carl
  524. * bugfix of hdisponen (base must be set, not index)
  525. * more portability fixes
  526. Revision 1.9 2002/05/18 13:34:26 peter
  527. * readded missing revisions
  528. Revision 1.8 2002/05/16 19:46:53 carl
  529. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  530. + try to fix temp allocation (still in ifdef)
  531. + generic constructor calls
  532. + start of tassembler / tmodulebase class cleanup
  533. Revision 1.5 2002/05/13 19:52:46 peter
  534. * a ppcppc can be build again
  535. Revision 1.4 2002/04/21 15:48:39 carl
  536. * some small updates according to i386 version
  537. Revision 1.3 2002/04/06 18:13:02 jonas
  538. * several powerpc-related additions and fixes
  539. Revision 1.2 2002/01/03 14:57:52 jonas
  540. * completed (not compilale yet though)
  541. }