ncpumat.pas 16 KB

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