ncpumat.pas 16 KB

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