ncpumat.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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,ncgmat;
  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. tSparcnotnode = class(tcgnotnode)
  33. procedure second_boolean;override;
  34. end;
  35. implementation
  36. uses
  37. globtype,systems,
  38. cutils,verbose,globals,
  39. symconst,symdef,
  40. aasmbase,aasmcpu,aasmtai,
  41. defutil,
  42. cgbase,cgobj,pass_1,pass_2,
  43. ncon,
  44. cpubase,cpuinfo,cginfo,
  45. ncgutil,cgcpu,cg64f32,rgobj;
  46. {*****************************************************************************
  47. TSparcMODDIVNODE
  48. *****************************************************************************}
  49. procedure tSparcmoddivnode.pass_2;
  50. const
  51. { signed overflow }
  52. divops: array[boolean, boolean] of tasmop =
  53. ((A_SDIV,A_UDIV),(A_SDIVcc,A_UDIVcc));
  54. var
  55. power,
  56. l1, l2 : longint;
  57. op : tasmop;
  58. tmpreg,
  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,OS_INT);
  79. resultreg := location.register;
  80. end;
  81. if (nodetype = modn) then
  82. begin
  83. resultreg := cg.get_scratch_reg_int(exprasmlist,OS_INT);
  84. end;
  85. if (nodetype = divn) and
  86. (right.nodetype = ordconstn) and
  87. ispowerof2(tordconstnode(right).value,power) then
  88. begin
  89. tmpreg:=cg.get_scratch_reg_int(exprasmlist,OS_INT);
  90. cg.a_op_const_reg_reg(exprasmlist,OP_SAR,OS_INT,31,numerator,tmpreg);
  91. { if signed, tmpreg=right value-1, otherwise 0 }
  92. cg.a_op_const_reg(exprasmlist,OP_AND,OS_INT,tordconstnode(right).value-1,tmpreg);
  93. { add to the left value }
  94. cg.a_op_reg_reg(exprasmlist,OP_ADD,OS_INT,tmpreg,numerator);
  95. cg.free_scratch_reg(exprasmlist,tmpreg);
  96. cg.a_op_const_reg_reg(exprasmlist,OP_SAR,OS_INT,aword(power),numerator,resultreg);
  97. end
  98. else
  99. begin
  100. { load divider in a register if necessary }
  101. location_force_reg(exprasmlist,right.location,
  102. def_cgsize(right.resulttype.def),true);
  103. divider := right.location.register;
  104. { needs overflow checking, (-maxlongint-1) div (-1) overflows! }
  105. { And on Sparc, the only way to catch a div-by-0 is by checking }
  106. { the overflow flag (JM) }
  107. op := divops[is_signed(right.resulttype.def),
  108. cs_check_overflow in aktlocalswitches];
  109. exprasmlist.concat(taicpu.op_reg_reg_reg(op,resultreg,numerator,
  110. divider));
  111. if (nodetype = modn) then
  112. begin
  113. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SMUL,resultreg,
  114. divider,resultreg));
  115. rg.UnGetRegisterInt(exprasmlist,divider);
  116. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SUB,location.register,
  117. numerator,resultreg));
  118. cg.free_scratch_reg(exprasmlist,resultreg);
  119. resultreg := location.register;
  120. end
  121. else
  122. rg.UnGetRegisterInt(exprasmlist,divider);
  123. end;
  124. { free used registers }
  125. if numerator.number <> resultreg.number then
  126. rg.ungetregisterint(exprasmlist,numerator);
  127. { set result location }
  128. location.loc:=LOC_REGISTER;
  129. location.register:=resultreg;
  130. cg.g_overflowcheck(exprasmlist,Location,ResultType.Def);
  131. end;
  132. {*****************************************************************************
  133. TSparcSHLRSHRNODE
  134. *****************************************************************************}
  135. function TSparcShlShrNode.first_shlshr64bitint:TNode;
  136. begin
  137. result := nil;
  138. end;
  139. procedure tSparcshlshrnode.pass_2;
  140. var
  141. resultreg, hregister1,hregister2,
  142. hregisterhigh,hregisterlow : tregister;
  143. op : topcg;
  144. asmop1, asmop2: tasmop;
  145. shiftval: aword;
  146. saved : tmaybesave;
  147. r:Tregister;
  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)
  154. then
  155. begin
  156. location_force_reg(exprasmlist,left.location,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)
  161. then
  162. begin
  163. location.loc := LOC_REGISTER;
  164. location.registerhigh := rg.getregisterint(exprasmlist,OS_INT);
  165. location.registerlow := rg.getregisterint(exprasmlist,OS_INT);
  166. end;
  167. if (right.nodetype = ordconstn) then
  168. begin
  169. shiftval := tordconstnode(right).value;
  170. if tordconstnode(right).value > 31 then
  171. begin
  172. if nodetype = shln then
  173. begin
  174. if (shiftval and 31) <> 0 then
  175. cg.a_op_const_reg_reg(exprasmlist,OP_SHL,OS_32,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,shiftval and 31,hregisterhigh,location.registerlow);
  182. cg.a_load_const_reg(exprasmlist,OS_32,0,location.registerhigh);
  183. end;
  184. end
  185. else
  186. begin
  187. {$warning TODO shl 64bit const}
  188. if nodetype = shln then
  189. begin
  190. {exprasmlist.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,location.registerhigh,hregisterhigh,shiftval,0,31-shiftval));
  191. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,location.registerhigh,hregisterlow,shiftval,32-shiftval,31));
  192. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,location.registerlow,hregisterlow,shiftval,0,31-shiftval));}
  193. end
  194. else
  195. begin
  196. {exprasmlist.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,location.registerlow,hregisterlow,32-shiftval,shiftval,31));
  197. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,location.registerlow,hregisterhigh,32-shiftval,0,shiftval-1));
  198. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,location.registerhigh,hregisterhigh,32-shiftval,shiftval,31));}
  199. end;
  200. end;
  201. end
  202. else
  203. { no constant shiftcount }
  204. begin
  205. location_force_reg(exprasmlist,right.location,OS_S32,true);
  206. hregister1 := right.location.register;
  207. if nodetype = shln
  208. then
  209. begin
  210. asmop1 := A_SLL;
  211. asmop2 := A_SRL;
  212. end
  213. else
  214. begin
  215. asmop1 := A_SRL;
  216. asmop2 := A_SLL;
  217. resultreg := location.registerhigh;
  218. location.registerhigh := location.registerlow;
  219. location.registerlow := resultreg;
  220. end;
  221. //rg.getexplicitregisterint(exprasmlist,NR_O0);
  222. r.enum:=R_INTREGISTER;
  223. r.number:=NR_O0;
  224. {$warning TODO shl 64bit no-const}
  225. { exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_0,hregister1,32));
  226. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,location.registerhigh,hregisterhigh,hregister1));
  227. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop2,R_0,hregisterlow,R_0));
  228. exprasmlist.concat(taicpu.op_reg_reg_reg(A_OR,location.registerhigh,location.registerhigh,R_0));
  229. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBI,R_0,hregister1,32));
  230. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,R_0,hregisterlow,R_0));
  231. exprasmlist.concat(taicpu.op_reg_reg_reg(A_OR,location.registerhigh,location.registerhigh,R_0));
  232. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,location.registerlow,hregisterlow,hregister1));}
  233. rg.UnGetRegisterInt(exprasmlist,r);
  234. if right.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]
  235. then
  236. cg.free_scratch_reg(exprasmlist,hregister1)
  237. else
  238. rg.UnGetRegisterInt(exprasmlist,hregister1);
  239. end
  240. end
  241. else
  242. begin
  243. { load left operators in a register }
  244. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  245. location_copy(location,left.location);
  246. resultreg := location.register;
  247. hregister1 := location.register;
  248. if (location.loc = LOC_CREGISTER) then
  249. begin
  250. location.loc := LOC_REGISTER;
  251. resultreg := rg.getregisterint(exprasmlist,OS_INT);
  252. location.register := resultreg;
  253. end;
  254. { determine operator }
  255. if nodetype=shln then
  256. op:=OP_SHL
  257. else
  258. op:=OP_SHR;
  259. { shifting by a constant directly coded: }
  260. if (right.nodetype=ordconstn) then
  261. cg.a_op_const_reg_reg(exprasmlist,op,OS_32,tordconstnode(right).value and 31,hregister1,resultreg)
  262. else
  263. begin
  264. { load shift count in a register if necessary }
  265. location_force_reg(exprasmlist,right.location,def_cgsize(right.resulttype.def),true);
  266. hregister2 := right.location.register;
  267. cg.a_op_reg_reg_reg(exprasmlist,op,OS_32,hregister2,hregister1,resultreg);
  268. rg.UnGetRegisterInt(exprasmlist,hregister2);
  269. end;
  270. end;
  271. end;
  272. {*****************************************************************************
  273. TSPARCNOTNODE
  274. *****************************************************************************}
  275. procedure tsparcnotnode.second_boolean;
  276. var
  277. hl : tasmlabel;
  278. zeroreg : tregister;
  279. begin
  280. { if the location is LOC_JUMP, we do the secondpass after the
  281. labels are allocated
  282. }
  283. if left.expectloc=LOC_JUMP then
  284. begin
  285. hl:=truelabel;
  286. truelabel:=falselabel;
  287. falselabel:=hl;
  288. secondpass(left);
  289. maketojumpbool(exprasmlist,left,lr_load_regvars);
  290. hl:=truelabel;
  291. truelabel:=falselabel;
  292. falselabel:=hl;
  293. location.loc:=LOC_JUMP;
  294. end
  295. else
  296. begin
  297. secondpass(left);
  298. case left.location.loc of
  299. LOC_FLAGS :
  300. begin
  301. location_copy(location,left.location);
  302. inverse_flags(location.resflags);
  303. end;
  304. LOC_REGISTER, LOC_CREGISTER, LOC_REFERENCE, LOC_CREFERENCE :
  305. begin
  306. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  307. zeroreg.enum:=R_INTREGISTER;
  308. zeroreg.number:=NR_G0;
  309. exprasmlist.concat(taicpu.op_reg_const_reg(A_SUBcc,left.location.register,0,zeroreg));
  310. location_release(exprasmlist,left.location);
  311. location_reset(location,LOC_FLAGS,OS_NO);
  312. location.resflags:=F_E;
  313. end;
  314. else
  315. internalerror(2003042401);
  316. end;
  317. end;
  318. end;
  319. begin
  320. cmoddivnode:=tSparcmoddivnode;
  321. cshlshrnode:=tSparcshlshrnode;
  322. cnotnode:=tSparcnotnode;
  323. end.
  324. {
  325. $Log$
  326. Revision 1.10 2003-06-04 20:59:37 mazen
  327. + added size of destination in code gen methods
  328. + making g_overflowcheck declaration same as
  329. ancestor's method declaration
  330. Revision 1.9 2003/06/01 21:38:07 peter
  331. * getregisterfpu size parameter added
  332. * op_const_reg size parameter added
  333. * sparc updates
  334. Revision 1.8 2003/05/30 23:57:08 peter
  335. * more sparc cleanup
  336. * accumulator removed, splitted in function_return_reg (called) and
  337. function_result_reg (caller)
  338. Revision 1.7 2003/03/15 22:51:58 mazen
  339. * remaking sparc rtl compile
  340. Revision 1.6 2003/03/10 21:59:54 mazen
  341. * fixing index overflow in handling new registers arrays.
  342. Revision 1.5 2003/02/19 22:00:17 daniel
  343. * Code generator converted to new register notation
  344. - Horribily outdated todo.txt removed
  345. Revision 1.4 2003/02/04 21:50:54 mazen
  346. * fixing internal errors related to notn when compiling RTL
  347. Revision 1.3 2003/01/08 18:43:58 daniel
  348. * Tregister changed into a record
  349. Revision 1.2 2002/12/30 21:17:22 mazen
  350. - unit cga no more used in sparc compiler.
  351. Revision 1.1 2002/12/21 23:22:59 mazen
  352. + added shift support
  353. Revision 1.20 2002/11/25 17:43:28 peter
  354. * splitted defbase in defutil,symutil,defcmp
  355. * merged isconvertable and is_equal into compare_defs(_ext)
  356. * made operator search faster by walking the list only once
  357. Revision 1.19 2002/09/10 21:21:29 jonas
  358. * fixed unary minus of 64bit values
  359. Revision 1.18 2002/09/07 15:25:14 peter
  360. * old logs removed and tabs fixed
  361. Revision 1.17 2002/08/15 15:15:55 carl
  362. * jmpbuf size allocation for exceptions is now cpu specific (as it should)
  363. * more generic nodes for maths
  364. * several fixes for better m68k support
  365. Revision 1.16 2002/08/10 17:15:31 jonas
  366. * various fixes and optimizations
  367. Revision 1.15 2002/07/26 10:48:34 jonas
  368. * fixed bug in shl/shr code
  369. Revision 1.14 2002/07/20 11:58:05 florian
  370. * types.pas renamed to defbase.pas because D6 contains a types
  371. unit so this would conflicts if D6 programms are compiled
  372. + Willamette/SSE2 instructions to assembler added
  373. Revision 1.13 2002/07/11 07:41:27 jonas
  374. * fixed tSparcmoddivnode
  375. * fixed 64bit parts of tSparcshlshrnode
  376. Revision 1.12 2002/07/09 19:45:01 jonas
  377. * unarynminus and shlshr node fixed for 32bit and smaller ordinals
  378. * small fixes in the assembler writer
  379. * changed scratch registers, because they were used by the linker (r11
  380. and r12) and by the abi under linux (r31)
  381. Revision 1.11 2002/07/07 09:44:32 florian
  382. * powerpc target fixed, very simple units can be compiled
  383. Revision 1.10 2002/05/20 13:30:42 carl
  384. * bugfix of hdisponen (base must be set, not index)
  385. * more portability fixes
  386. Revision 1.9 2002/05/18 13:34:26 peter
  387. * readded missing revisions
  388. Revision 1.8 2002/05/16 19:46:53 carl
  389. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  390. + try to fix temp allocation (still in ifdef)
  391. + generic constructor calls
  392. + start of tassembler / tmodulebase class cleanup
  393. Revision 1.5 2002/05/13 19:52:46 peter
  394. * a ppcSparc can be build again
  395. Revision 1.4 2002/04/21 15:48:39 carl
  396. * some small updates according to i386 version
  397. Revision 1.3 2002/04/06 18:13:02 jonas
  398. * several powerpc-related additions and fixes
  399. Revision 1.2 2002/01/03 14:57:52 jonas
  400. * completed (not compilale yet though)
  401. }