ncpumat.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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 ncpumat;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,nmat;
  23. type
  24. tSparcmoddivnode = class(tmoddivnode)
  25. procedure pass_2;override;
  26. end;
  27. tSparcshlshrnode = class(tshlshrnode)
  28. procedure pass_2;override;
  29. { everything will be handled in pass_2 }
  30. function first_shlshr64bitint: tnode; override;
  31. end;
  32. tSparcunaryminusnode = class(tunaryminusnode)
  33. procedure pass_2;override;
  34. end;
  35. tSparcnotnode = 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. TSparcMODDIVNODE
  51. *****************************************************************************}
  52. procedure tSparcmoddivnode.pass_2;
  53. const
  54. { signed overflow }
  55. divops: array[boolean, boolean] of tasmop =
  56. ((A_SDIV,A_UDIV),(A_SDIVcc,A_UDIVcc));
  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_ADD,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 Sparc, 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_SMUL,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. TSparcSHLRSHRNODE
  141. *****************************************************************************}
  142. function TSparcShlShrNode.first_shlshr64bitint:TNode;
  143. begin
  144. result := nil;
  145. end;
  146. procedure tSparcshlshrnode.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)
  160. then
  161. begin
  162. location_force_reg(exprasmlist,left.location,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)
  167. then
  168. begin
  169. location.loc := LOC_REGISTER;
  170. location.registerhigh := rg.getregisterint(exprasmlist);
  171. location.registerlow := rg.getregisterint(exprasmlist);
  172. end;
  173. if (right.nodetype = ordconstn)
  174. then
  175. begin
  176. shiftval := tordconstnode(right).value;
  177. if tordconstnode(right).value > 31
  178. then
  179. begin
  180. if nodetype = shln
  181. then
  182. begin
  183. if (shiftval and 31) <> 0
  184. then
  185. cg.a_op_const_reg_reg(exprasmlist,OP_SHL,OS_32,shiftval and 31,hregisterlow,location.registerhigh);
  186. cg.a_load_const_reg(exprasmlist,OS_32,0,location.registerlow);
  187. end
  188. else
  189. begin
  190. if (shiftval and 31) <> 0
  191. then
  192. cg.a_op_const_reg_reg(exprasmlist,OP_SHR,OS_32,shiftval and 31,hregisterhigh,location.registerlow);
  193. cg.a_load_const_reg(exprasmlist,OS_32,0,location.registerhigh);
  194. end;
  195. end
  196. else
  197. begin
  198. if nodetype = shln
  199. then
  200. begin
  201. {exprasmlist.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,location.registerhigh,hregisterhigh,shiftval,0,31-shiftval));
  202. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,location.registerhigh,hregisterlow,shiftval,32-shiftval,31));
  203. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,location.registerlow,hregisterlow,shiftval,0,31-shiftval));}
  204. end
  205. else
  206. begin
  207. {exprasmlist.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,location.registerlow,hregisterlow,32-shiftval,shiftval,31));
  208. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,location.registerlow,hregisterhigh,32-shiftval,0,shiftval-1));
  209. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,location.registerhigh,hregisterhigh,32-shiftval,shiftval,31));}
  210. end;
  211. end;
  212. end
  213. else
  214. { no constant shiftcount }
  215. begin
  216. location_force_reg(exprasmlist,right.location,OS_S32,true);
  217. hregister1 := right.location.register;
  218. if nodetype = shln
  219. then
  220. begin
  221. asmop1 := A_SLL;
  222. asmop2 := A_SRL;
  223. end
  224. else
  225. begin
  226. asmop1 := A_SRL;
  227. asmop2 := A_SLL;
  228. resultreg := location.registerhigh;
  229. location.registerhigh := location.registerlow;
  230. location.registerlow := resultreg;
  231. end;
  232. rg.getexplicitregisterint(exprasmlist,R_G0);
  233. { exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_0,hregister1,32));
  234. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,location.registerhigh,hregisterhigh,hregister1));
  235. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop2,R_0,hregisterlow,R_0));
  236. exprasmlist.concat(taicpu.op_reg_reg_reg(A_OR,location.registerhigh,location.registerhigh,R_0));
  237. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBI,R_0,hregister1,32));
  238. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,R_0,hregisterlow,R_0));
  239. exprasmlist.concat(taicpu.op_reg_reg_reg(A_OR,location.registerhigh,location.registerhigh,R_0));
  240. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,location.registerlow,hregisterlow,hregister1));}
  241. rg.ungetregister(exprasmlist,R_G0);
  242. if right.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]
  243. then
  244. cg.free_scratch_reg(exprasmlist,hregister1)
  245. else
  246. rg.ungetregister(exprasmlist,hregister1);
  247. end
  248. end
  249. else
  250. begin
  251. { load left operators in a register }
  252. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  253. location_copy(location,left.location);
  254. resultreg := location.register;
  255. hregister1 := location.register;
  256. if (location.loc = LOC_CREGISTER)
  257. then
  258. begin
  259. location.loc := LOC_REGISTER;
  260. resultreg := rg.getregisterint(exprasmlist);
  261. location.register := resultreg;
  262. end;
  263. { determine operator }
  264. if nodetype=shln
  265. then
  266. op:=OP_SHL
  267. else
  268. op:=OP_SHR;
  269. { shifting by a constant directly coded: }
  270. if (right.nodetype=ordconstn)
  271. then
  272. cg.a_op_const_reg_reg(exprasmlist,op,OS_32,tordconstnode(right).value and 31,hregister1,resultreg)
  273. else
  274. begin
  275. { load shift count in a register if necessary }
  276. location_force_reg(exprasmlist,right.location,def_cgsize(right.resulttype.def),true);
  277. hregister2 := right.location.register;
  278. cg.a_op_reg_reg_reg(exprasmlist,op,OS_32,hregister2,hregister1,resultreg);
  279. rg.ungetregister(exprasmlist,hregister2);
  280. end;
  281. end;
  282. end;
  283. {*****************************************************************************
  284. TSparcUNARYMINUSNODE
  285. *****************************************************************************}
  286. procedure tSparcunaryminusnode.pass_2;
  287. var
  288. src1, src2, tmp: tregister;
  289. op: tasmop;
  290. begin
  291. secondpass(left);
  292. if is_64bitint(left.resulttype.def) then
  293. begin
  294. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  295. location_copy(location,left.location);
  296. if (location.loc = LOC_CREGISTER) then
  297. begin
  298. location.registerlow := rg.getregisterint(exprasmlist);
  299. location.registerhigh := rg.getregisterint(exprasmlist);
  300. location.loc := LOC_CREGISTER;
  301. end;
  302. exprasmlist.concat(taicpu.op_reg_const_reg(A_SUB,location.registerlow,0,left.location.registerlow));
  303. if not(cs_check_overflow in aktlocalswitches) then
  304. exprasmlist.concat(taicpu.op_reg_reg(A_SUB,location.registerhigh,left.location.registerhigh))
  305. else
  306. exprasmlist.concat(taicpu.op_reg_reg(A_SUB,location.registerhigh,left.location.registerhigh));
  307. end
  308. else
  309. begin
  310. location_copy(location,left.location);
  311. location.loc:=LOC_REGISTER;
  312. case left.location.loc of
  313. LOC_FPUREGISTER, LOC_REGISTER:
  314. begin
  315. src1 := left.location.register;
  316. location.register := src1;
  317. end;
  318. LOC_CFPUREGISTER, LOC_CREGISTER:
  319. begin
  320. src1 := left.location.register;
  321. if left.location.loc = LOC_CREGISTER then
  322. location.register := rg.getregisterint(exprasmlist)
  323. else
  324. location.register := rg.getregisterfpu(exprasmlist);
  325. end;
  326. LOC_REFERENCE,LOC_CREFERENCE:
  327. begin
  328. if (left.resulttype.def.deftype=floatdef) then
  329. begin
  330. src1 := rg.getregisterfpu(exprasmlist);
  331. location.register := src1;
  332. cg.a_loadfpu_ref_reg(exprasmlist,
  333. def_cgsize(left.resulttype.def),
  334. left.location.reference,src1);
  335. end
  336. else
  337. begin
  338. src1 := rg.getregisterint(exprasmlist);
  339. location.register:= src1;
  340. cg.a_load_ref_reg(exprasmlist,OS_32,
  341. left.location.reference,src1);
  342. end;
  343. reference_release(exprasmlist,left.location.reference);
  344. end;
  345. end;
  346. { choose appropriate operand }
  347. if left.resulttype.def.deftype <> floatdef then
  348. begin
  349. if not(cs_check_overflow in aktlocalswitches) then
  350. op := A_NEG
  351. else
  352. op := A_NEG;
  353. location.loc := LOC_REGISTER;
  354. end
  355. else
  356. begin
  357. op := A_NEG;
  358. location.loc := LOC_FPUREGISTER;
  359. end;
  360. { emit operation }
  361. exprasmlist.concat(taicpu.op_reg_reg(op,location.register,src1));
  362. end;
  363. { Here was a problem... }
  364. { Operand to be negated always }
  365. { seems to be converted to signed }
  366. { 32-bit before doing neg!! }
  367. { So this is useless... }
  368. { that's not true: -2^31 gives an overflow error if it is negated (FK) }
  369. cg.g_overflowcheck(exprasmlist,self);
  370. end;
  371. {*****************************************************************************
  372. TSparcNOTNODE
  373. *****************************************************************************}
  374. procedure tSparcnotnode.pass_2;
  375. var
  376. hl : tasmlabel;
  377. regl, regh: tregister;
  378. begin
  379. if is_boolean(resulttype.def) then
  380. begin
  381. { the second pass could change the location of left }
  382. { if it is a register variable, so we've to do }
  383. { this before the case statement }
  384. if left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,
  385. LOC_FLAGS,LOC_REGISTER,LOC_CREGISTER] then
  386. secondpass(left);
  387. case left.location.loc of
  388. LOC_JUMP :
  389. begin
  390. hl:=truelabel;
  391. truelabel:=falselabel;
  392. falselabel:=hl;
  393. secondpass(left);
  394. maketojumpbool(exprasmlist,left,lr_load_regvars);
  395. hl:=truelabel;
  396. truelabel:=falselabel;
  397. falselabel:=hl;
  398. end;
  399. LOC_FLAGS :
  400. begin
  401. location_copy(location,left.location);
  402. //inverse_flags(location.resflags);
  403. end;
  404. LOC_REGISTER, LOC_CREGISTER, LOC_REFERENCE, LOC_CREFERENCE :
  405. begin
  406. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  407. exprasmlist.concat(taicpu.op_reg_const(A_SUBcc,left.location.register,0));
  408. location_release(exprasmlist,left.location);
  409. location_reset(location,LOC_FLAGS,OS_NO);
  410. //location.resflags.cr:=r_NONE;
  411. //location.resflags.flag:=F_NONE;
  412. end;
  413. end;
  414. end
  415. else if is_64bitint(left.resulttype.def) then
  416. begin
  417. secondpass(left);
  418. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),false);
  419. location_copy(location,left.location);
  420. { perform the NOT operation }
  421. exprasmlist.concat(taicpu.op_reg_reg(A_NOT,location.registerhigh,
  422. location.registerhigh));
  423. exprasmlist.concat(taicpu.op_reg_reg(A_NOT,location.registerlow,
  424. location.registerlow));
  425. end
  426. else
  427. begin
  428. secondpass(left);
  429. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),false);
  430. location_copy(location,left.location);
  431. if location.loc=LOC_CREGISTER then
  432. location.register := rg.getregisterint(exprasmlist);
  433. { perform the NOT operation }
  434. exprasmlist.concat(taicpu.op_reg_reg(A_NOT,location.register,
  435. left.location.register));
  436. end;
  437. end;
  438. begin
  439. cmoddivnode:=tSparcmoddivnode;
  440. cshlshrnode:=tSparcshlshrnode;
  441. cunaryminusnode:=tSparcunaryminusnode;
  442. cnotnode:=tSparcnotnode;
  443. end.
  444. {
  445. $Log$
  446. Revision 1.1 2002-12-21 23:22:59 mazen
  447. + added shift support
  448. Revision 1.20 2002/11/25 17:43:28 peter
  449. * splitted defbase in defutil,symutil,defcmp
  450. * merged isconvertable and is_equal into compare_defs(_ext)
  451. * made operator search faster by walking the list only once
  452. Revision 1.19 2002/09/10 21:21:29 jonas
  453. * fixed unary minus of 64bit values
  454. Revision 1.18 2002/09/07 15:25:14 peter
  455. * old logs removed and tabs fixed
  456. Revision 1.17 2002/08/15 15:15:55 carl
  457. * jmpbuf size allocation for exceptions is now cpu specific (as it should)
  458. * more generic nodes for maths
  459. * several fixes for better m68k support
  460. Revision 1.16 2002/08/10 17:15:31 jonas
  461. * various fixes and optimizations
  462. Revision 1.15 2002/07/26 10:48:34 jonas
  463. * fixed bug in shl/shr code
  464. Revision 1.14 2002/07/20 11:58:05 florian
  465. * types.pas renamed to defbase.pas because D6 contains a types
  466. unit so this would conflicts if D6 programms are compiled
  467. + Willamette/SSE2 instructions to assembler added
  468. Revision 1.13 2002/07/11 07:41:27 jonas
  469. * fixed tSparcmoddivnode
  470. * fixed 64bit parts of tSparcshlshrnode
  471. Revision 1.12 2002/07/09 19:45:01 jonas
  472. * unarynminus and shlshr node fixed for 32bit and smaller ordinals
  473. * small fixes in the assembler writer
  474. * changed scratch registers, because they were used by the linker (r11
  475. and r12) and by the abi under linux (r31)
  476. Revision 1.11 2002/07/07 09:44:32 florian
  477. * powerpc target fixed, very simple units can be compiled
  478. Revision 1.10 2002/05/20 13:30:42 carl
  479. * bugfix of hdisponen (base must be set, not index)
  480. * more portability fixes
  481. Revision 1.9 2002/05/18 13:34:26 peter
  482. * readded missing revisions
  483. Revision 1.8 2002/05/16 19:46:53 carl
  484. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  485. + try to fix temp allocation (still in ifdef)
  486. + generic constructor calls
  487. + start of tassembler / tmodulebase class cleanup
  488. Revision 1.5 2002/05/13 19:52:46 peter
  489. * a ppcSparc can be build again
  490. Revision 1.4 2002/04/21 15:48:39 carl
  491. * some small updates according to i386 version
  492. Revision 1.3 2002/04/06 18:13:02 jonas
  493. * several powerpc-related additions and fixes
  494. Revision 1.2 2002/01/03 14:57:52 jonas
  495. * completed (not compilale yet though)
  496. }