ncpumat.pas 22 KB

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