nppcmat.pas 21 KB

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