ncpumat.pas 16 KB

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