nppcmat.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. defbase,
  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. begin
  66. secondpass(left);
  67. maybe_save(exprasmlist,right.registers32,left.location,saved);
  68. secondpass(right);
  69. maybe_restore(exprasmlist,left.location,saved);
  70. location_copy(location,left.location);
  71. { put numerator in register }
  72. location_force_reg(exprasmlist,left.location,
  73. def_cgsize(left.resulttype.def),true);
  74. location_copy(location,left.location);
  75. numerator := location.register;
  76. resultreg := location.register;
  77. if (location.loc = LOC_CREGISTER) then
  78. begin
  79. location.loc := LOC_REGISTER;
  80. location.register := rg.getregisterint(exprasmlist);
  81. resultreg := location.register;
  82. end;
  83. if (nodetype = modn) then
  84. begin
  85. resultreg := cg.get_scratch_reg_int(exprasmlist);
  86. end;
  87. if (nodetype = divn) and
  88. (right.nodetype = ordconstn) and
  89. ispowerof2(tordconstnode(right).value,power) then
  90. begin
  91. { From "The PowerPC Compiler Writer's Guide": }
  92. { This code uses the fact that, in the PowerPC architecture, }
  93. { the shift right algebraic instructions set the Carry bit if }
  94. { the source register contains a negative number and one or }
  95. { more 1-bits are shifted out. Otherwise, the carry bit is }
  96. { cleared. The addze instruction corrects the quotient, if }
  97. { necessary, when the dividend is negative. For example, if }
  98. { n = -13, (0xFFFF_FFF3), and k = 2, after executing the srawi }
  99. { instruction, q = -4 (0xFFFF_FFFC) and CA = 1. After executing }
  100. { the addze instruction, q = -3, the correct quotient. }
  101. cg.a_op_const_reg_reg(exprasmlist,OP_SAR,OS_32,aword(power),
  102. numerator,resultreg);
  103. exprasmlist.concat(taicpu.op_reg_reg(A_ADDZE,resultreg,resultreg));
  104. end
  105. else
  106. begin
  107. { load divider in a register if necessary }
  108. location_force_reg(exprasmlist,right.location,
  109. def_cgsize(right.resulttype.def),true);
  110. divider := right.location.register;
  111. { needs overflow checking, (-maxlongint-1) div (-1) overflows! }
  112. { And on PPC, the only way to catch a div-by-0 is by checking }
  113. { the overflow flag (JM) }
  114. op := divops[is_signed(right.resulttype.def),
  115. cs_check_overflow in aktlocalswitches];
  116. exprasmlist.concat(taicpu.op_reg_reg_reg(op,resultreg,numerator,
  117. divider));
  118. if (nodetype = modn) then
  119. begin
  120. exprasmlist.concat(taicpu.op_reg_reg_reg(A_MULLW,resultreg,
  121. divider,resultreg));
  122. rg.ungetregister(exprasmlist,divider);
  123. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SUB,location.register,
  124. numerator,resultreg));
  125. cg.free_scratch_reg(exprasmlist,resultreg);
  126. resultreg := location.register;
  127. end
  128. else
  129. rg.ungetregister(exprasmlist,divider);
  130. end;
  131. { free used registers }
  132. if numerator <> resultreg then
  133. rg.ungetregisterint(exprasmlist,numerator);
  134. { set result location }
  135. location.loc:=LOC_REGISTER;
  136. location.register:=resultreg;
  137. cg.g_overflowcheck(exprasmlist,self);
  138. end;
  139. {*****************************************************************************
  140. TPPCSHLRSHRNODE
  141. *****************************************************************************}
  142. function tppcshlshrnode.first_shlshr64bitint: tnode;
  143. begin
  144. result := nil;
  145. end;
  146. procedure tppcshlshrnode.pass_2;
  147. var
  148. resultreg, hregister1,hregister2,
  149. hregisterhigh,hregisterlow : tregister;
  150. op : topcg;
  151. asmop1, asmop2: tasmop;
  152. shiftval: aword;
  153. saved : tmaybesave;
  154. begin
  155. secondpass(left);
  156. maybe_save(exprasmlist,right.registers32,left.location,saved);
  157. secondpass(right);
  158. maybe_restore(exprasmlist,left.location,saved);
  159. if is_64bitint(left.resulttype.def) then
  160. begin
  161. location_force_reg(exprasmlist,left.location,
  162. def_cgsize(left.resulttype.def),true);
  163. location_copy(location,left.location);
  164. hregisterhigh := location.registerhigh;
  165. hregisterlow := location.registerlow;
  166. if (location.loc = LOC_CREGISTER) then
  167. begin
  168. location.loc := LOC_REGISTER;
  169. location.registerhigh := rg.getregisterint(exprasmlist);
  170. location.registerlow := rg.getregisterint(exprasmlist);
  171. end;
  172. if (right.nodetype = ordconstn) then
  173. begin
  174. shiftval := tordconstnode(right).value;
  175. if tordconstnode(right).value > 31 then
  176. begin
  177. if nodetype = shln then
  178. begin
  179. if (shiftval and 31) <> 0 then
  180. cg.a_op_const_reg_reg(exprasmlist,OP_SHL,OS_32,
  181. shiftval and 31,hregisterlow,location.registerhigh);
  182. cg.a_load_const_reg(exprasmlist,OS_32,0,location.registerlow);
  183. end
  184. else
  185. begin
  186. if (shiftval and 31) <> 0 then
  187. cg.a_op_const_reg_reg(exprasmlist,OP_SHR,OS_32,
  188. shiftval and 31,hregisterhigh,location.registerlow);
  189. cg.a_load_const_reg(exprasmlist,OS_32,0,location.registerhigh);
  190. end;
  191. end
  192. else
  193. begin
  194. if nodetype = shln then
  195. begin
  196. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  197. A_RLWINM,location.registerhigh,hregisterhigh,shiftval,
  198. 0,31-shiftval));
  199. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  200. A_RLWIMI,location.registerhigh,hregisterlow,shiftval,
  201. 32-shiftval,31));
  202. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  203. A_RLWINM,location.registerlow,hregisterlow,shiftval,
  204. 0,31-shiftval));
  205. end
  206. else
  207. begin
  208. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  209. A_RLWINM,location.registerlow,hregisterlow,32-shiftval,
  210. shiftval,31));
  211. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  212. A_RLWIMI,location.registerlow,hregisterhigh,32-shiftval,
  213. 0,shiftval-1));
  214. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  215. A_RLWINM,location.registerhigh,hregisterhigh,32-shiftval,
  216. shiftval,31));
  217. end;
  218. end;
  219. end
  220. else
  221. { no constant shiftcount }
  222. begin
  223. location_force_reg(exprasmlist,right.location,OS_S32,true);
  224. hregister1 := right.location.register;
  225. if nodetype = shln then
  226. begin
  227. asmop1 := A_SLW;
  228. asmop2 := A_SRW;
  229. end
  230. else
  231. begin
  232. asmop1 := A_SRW;
  233. asmop2 := A_SLW;
  234. resultreg := location.registerhigh;
  235. location.registerhigh := location.registerlow;
  236. location.registerlow := resultreg;
  237. end;
  238. rg.getexplicitregisterint(exprasmlist,R_0);
  239. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  240. R_0,hregister1,32));
  241. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,
  242. location.registerhigh,hregisterhigh,hregister1));
  243. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop2,
  244. R_0,hregisterlow,R_0));
  245. exprasmlist.concat(taicpu.op_reg_reg_reg(A_OR,
  246. location.registerhigh,location.registerhigh,R_0));
  247. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBI,
  248. R_0,hregister1,32));
  249. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,
  250. R_0,hregisterlow,R_0));
  251. exprasmlist.concat(taicpu.op_reg_reg_reg(A_OR,
  252. location.registerhigh,location.registerhigh,R_0));
  253. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,
  254. location.registerlow,hregisterlow,hregister1));
  255. rg.ungetregister(exprasmlist,R_0);
  256. if right.location.loc in [LOC_CREFERENCE,LOC_REFERENCE] then
  257. cg.free_scratch_reg(exprasmlist,hregister1)
  258. else
  259. rg.ungetregister(exprasmlist,hregister1);
  260. end
  261. end
  262. else
  263. begin
  264. { load left operators in a register }
  265. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  266. location_copy(location,left.location);
  267. resultreg := location.register;
  268. hregister1 := location.register;
  269. if (location.loc = LOC_CREGISTER) then
  270. begin
  271. location.loc := LOC_REGISTER;
  272. resultreg := rg.getregisterint(exprasmlist);
  273. location.register := resultreg;
  274. end;
  275. { determine operator }
  276. if nodetype=shln then
  277. op:=OP_SHL
  278. else
  279. op:=OP_SHR;
  280. { shifting by a constant directly coded: }
  281. if (right.nodetype=ordconstn) then
  282. cg.a_op_const_reg_reg(exprasmlist,op,OS_32,
  283. tordconstnode(right).value and 31,hregister1,resultreg)
  284. else
  285. begin
  286. { load shift count in a register if necessary }
  287. location_force_reg(exprasmlist,right.location,def_cgsize(right.resulttype.def),true);
  288. hregister2 := right.location.register;
  289. cg.a_op_reg_reg_reg(exprasmlist,op,OS_32,hregister2,
  290. hregister1,resultreg);
  291. rg.ungetregister(exprasmlist,hregister2);
  292. end;
  293. end;
  294. end;
  295. {*****************************************************************************
  296. TPPCUNARYMINUSNODE
  297. *****************************************************************************}
  298. procedure tppcunaryminusnode.pass_2;
  299. var
  300. src1, src2, tmp: tregister;
  301. op: tasmop;
  302. begin
  303. secondpass(left);
  304. if is_64bitint(left.resulttype.def) then
  305. begin
  306. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),false);
  307. location_copy(location,left.location);
  308. exprasmlist.concat(taicpu.op_reg_reg(A_NEG,location.registerlow,
  309. location.registerlow));
  310. cg.a_op_reg_reg(exprasmlist,OP_NOT,OS_32,location.registerhigh,location.registerhigh);
  311. tmp := cg.get_scratch_reg_int(exprasmlist);
  312. cg.a_op_const_reg_reg(exprasmlist,OP_SAR,OS_32,31,location.registerlow,
  313. tmp);
  314. if not(cs_check_overflow in aktlocalswitches) then
  315. cg.a_op_reg_reg(exprasmlist,OP_ADD,OS_32,location.registerhigh,
  316. tmp)
  317. else
  318. exprasmlist.concat(taicpu.op_reg_reg_reg(A_ADDO_,tmp,
  319. location.registerhigh,tmp));
  320. cg.free_scratch_reg(exprasmlist,tmp);
  321. end
  322. else
  323. begin
  324. location_copy(location,left.location);
  325. location.loc:=LOC_REGISTER;
  326. case left.location.loc of
  327. LOC_FPUREGISTER, LOC_REGISTER:
  328. begin
  329. src1 := left.location.register;
  330. location.register := src1;
  331. end;
  332. LOC_CFPUREGISTER, LOC_CREGISTER:
  333. begin
  334. src1 := left.location.register;
  335. if left.location.loc = LOC_CREGISTER then
  336. location.register := rg.getregisterint(exprasmlist)
  337. else
  338. location.register := rg.getregisterfpu(exprasmlist);
  339. end;
  340. LOC_REFERENCE,LOC_CREFERENCE:
  341. begin
  342. if (left.resulttype.def.deftype=floatdef) then
  343. begin
  344. src1 := rg.getregisterfpu(exprasmlist);
  345. location.register := src1;
  346. cg.a_loadfpu_ref_reg(exprasmlist,
  347. def_cgsize(left.resulttype.def),
  348. left.location.reference,src1);
  349. end
  350. else
  351. begin
  352. src1 := rg.getregisterint(exprasmlist);
  353. location.register:= src1;
  354. cg.a_load_ref_reg(exprasmlist,OS_32,
  355. left.location.reference,src1);
  356. end;
  357. reference_release(exprasmlist,left.location.reference);
  358. end;
  359. end;
  360. { choose appropriate operand }
  361. if left.resulttype.def.deftype <> floatdef then
  362. begin
  363. if not(cs_check_overflow in aktlocalswitches) then
  364. op := A_NEG
  365. else
  366. op := A_NEGO_;
  367. location.loc := LOC_REGISTER;
  368. end
  369. else
  370. begin
  371. op := A_FNEG;
  372. location.loc := LOC_FPUREGISTER;
  373. end;
  374. { emit operation }
  375. exprasmlist.concat(taicpu.op_reg_reg(op,location.register,src1));
  376. end;
  377. { Here was a problem... }
  378. { Operand to be negated always }
  379. { seems to be converted to signed }
  380. { 32-bit before doing neg!! }
  381. { So this is useless... }
  382. { that's not true: -2^31 gives an overflow error if it is negated (FK) }
  383. cg.g_overflowcheck(exprasmlist,self);
  384. end;
  385. {*****************************************************************************
  386. TPPCNOTNODE
  387. *****************************************************************************}
  388. procedure tppcnotnode.pass_2;
  389. var
  390. hl : tasmlabel;
  391. regl, regh: tregister;
  392. begin
  393. if is_boolean(resulttype.def) then
  394. begin
  395. { the second pass could change the location of left }
  396. { if it is a register variable, so we've to do }
  397. { this before the case statement }
  398. if left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,
  399. LOC_FLAGS,LOC_REGISTER,LOC_CREGISTER] then
  400. secondpass(left);
  401. case left.location.loc of
  402. LOC_JUMP :
  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. end;
  413. LOC_FLAGS :
  414. begin
  415. location_copy(location,left.location);
  416. inverse_flags(location.resflags);
  417. end;
  418. LOC_REGISTER, LOC_CREGISTER, LOC_REFERENCE, LOC_CREFERENCE :
  419. begin
  420. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  421. exprasmlist.concat(taicpu.op_reg_const(A_CMPWI,left.location.register,0));
  422. location_release(exprasmlist,left.location);
  423. location_reset(location,LOC_FLAGS,OS_NO);
  424. location.resflags.cr:=r_cr0;
  425. location.resflags.flag:=F_EQ;
  426. end;
  427. end;
  428. end
  429. else if is_64bitint(left.resulttype.def) then
  430. begin
  431. secondpass(left);
  432. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),false);
  433. location_copy(location,left.location);
  434. { perform the NOT operation }
  435. exprasmlist.concat(taicpu.op_reg_reg(A_NOT,location.registerhigh,
  436. location.registerhigh));
  437. exprasmlist.concat(taicpu.op_reg_reg(A_NOT,location.registerlow,
  438. location.registerlow));
  439. end
  440. else
  441. begin
  442. secondpass(left);
  443. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),false);
  444. location_copy(location,left.location);
  445. if location.loc=LOC_CREGISTER then
  446. location.register := rg.getregisterint(exprasmlist);
  447. { perform the NOT operation }
  448. exprasmlist.concat(taicpu.op_reg_reg(A_NOT,location.register,
  449. left.location.register));
  450. end;
  451. end;
  452. begin
  453. cmoddivnode:=tppcmoddivnode;
  454. cshlshrnode:=tppcshlshrnode;
  455. cunaryminusnode:=tppcunaryminusnode;
  456. cnotnode:=tppcnotnode;
  457. end.
  458. {
  459. $Log$
  460. Revision 1.18 2002-09-07 15:25:14 peter
  461. * old logs removed and tabs fixed
  462. Revision 1.17 2002/08/15 15:15:55 carl
  463. * jmpbuf size allocation for exceptions is now cpu specific (as it should)
  464. * more generic nodes for maths
  465. * several fixes for better m68k support
  466. Revision 1.16 2002/08/10 17:15:31 jonas
  467. * various fixes and optimizations
  468. Revision 1.15 2002/07/26 10:48:34 jonas
  469. * fixed bug in shl/shr code
  470. Revision 1.14 2002/07/20 11:58:05 florian
  471. * types.pas renamed to defbase.pas because D6 contains a types
  472. unit so this would conflicts if D6 programms are compiled
  473. + Willamette/SSE2 instructions to assembler added
  474. Revision 1.13 2002/07/11 07:41:27 jonas
  475. * fixed tppcmoddivnode
  476. * fixed 64bit parts of tppcshlshrnode
  477. Revision 1.12 2002/07/09 19:45:01 jonas
  478. * unarynminus and shlshr node fixed for 32bit and smaller ordinals
  479. * small fixes in the assembler writer
  480. * changed scratch registers, because they were used by the linker (r11
  481. and r12) and by the abi under linux (r31)
  482. Revision 1.11 2002/07/07 09:44:32 florian
  483. * powerpc target fixed, very simple units can be compiled
  484. Revision 1.10 2002/05/20 13:30:42 carl
  485. * bugfix of hdisponen (base must be set, not index)
  486. * more portability fixes
  487. Revision 1.9 2002/05/18 13:34:26 peter
  488. * readded missing revisions
  489. Revision 1.8 2002/05/16 19:46:53 carl
  490. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  491. + try to fix temp allocation (still in ifdef)
  492. + generic constructor calls
  493. + start of tassembler / tmodulebase class cleanup
  494. Revision 1.5 2002/05/13 19:52:46 peter
  495. * a ppcppc can be build again
  496. Revision 1.4 2002/04/21 15:48:39 carl
  497. * some small updates according to i386 version
  498. Revision 1.3 2002/04/06 18:13:02 jonas
  499. * several powerpc-related additions and fixes
  500. Revision 1.2 2002/01/03 14:57:52 jonas
  501. * completed (not compilale yet though)
  502. }