rgcpu.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the i386 specific class for the register
  5. allocator
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit rgcpu;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. cpubase,
  24. cpuinfo,
  25. aasmbase,aasmtai,
  26. cclasses,globtype,cgbase,rgobj;
  27. type
  28. trgcpu = class(trgobj)
  29. fpuvaroffset : byte;
  30. { to keep the same allocation order as with the old routines }
  31. procedure add_constraints(reg:Tregister);override;
  32. function getregisterfpu(list: taasmoutput;size:TCGSize) : tregister; override;
  33. procedure ungetregisterfpu(list: taasmoutput; r : tregister;size:TCGSize); override;
  34. {# Returns a subset register of the register r with the specified size.
  35. WARNING: There is no clearing of the upper parts of the register,
  36. if a 8-bit / 16-bit register is converted to a 32-bit register.
  37. It is up to the code generator to correctly zero fill the register
  38. }
  39. function makeregsize(reg: tregister; size: tcgsize): tregister; override;
  40. { pushes and restores registers }
  41. {$ifdef SUPPORT_MMX}
  42. procedure pushusedotherregisters(list:Taasmoutput;
  43. var pushed:Tpushedsavedother;
  44. const s:Totherregisterset);
  45. {$endif SUPPORT_MMX}
  46. {$ifdef SUPPORT_MMX}
  47. procedure popusedotherregisters(list:Taasmoutput;
  48. const pushed:Tpushedsavedother);
  49. {$endif SUPPORT_MMX}
  50. procedure saveusedotherregisters(list:Taasmoutput;
  51. var saved:Tpushedsavedother;
  52. const s:Totherregisterset);override;
  53. procedure restoreusedotherregisters(list:Taasmoutput;
  54. const saved:Tpushedsavedother);override;
  55. procedure resetusableregisters;override;
  56. { corrects the fpu stack register by ofs }
  57. function correct_fpuregister(r : tregister;ofs : byte) : tregister;
  58. end;
  59. implementation
  60. uses
  61. systems,
  62. globals,verbose;
  63. {************************************************************************}
  64. { trgcpu }
  65. {************************************************************************}
  66. procedure Trgcpu.add_constraints(reg:Tregister);
  67. var
  68. supreg : tsuperregister;
  69. begin
  70. if getsubreg(reg) in [R_SUBL,R_SUBH] then
  71. begin
  72. supreg:=getsupreg(reg);
  73. {These registers have no 8-bit subregister, so add interferences.}
  74. add_edge(supreg,RS_ESI);
  75. add_edge(supreg,RS_EDI);
  76. add_edge(supreg,RS_EBP);
  77. end;
  78. end;
  79. function trgcpu.getregisterfpu(list: taasmoutput;size: TCGSize) : tregister;
  80. begin
  81. { note: don't return R_ST0, see comments above implementation of }
  82. { a_loadfpu_* methods in cgcpu (JM) }
  83. result:=NR_ST;
  84. end;
  85. procedure trgcpu.ungetregisterfpu(list : taasmoutput; r : tregister;size:TCGSize);
  86. begin
  87. { nothing to do, fpu stack management is handled by the load/ }
  88. { store operations in cgcpu (JM) }
  89. end;
  90. {$ifdef SUPPORT_MMX}
  91. procedure trgcpu.pushusedotherregisters(list:Taasmoutput;
  92. var pushed:Tpushedsavedother;
  93. const s:Totherregisterset);
  94. { var r:Toldregister;
  95. r2:Tregister;
  96. hr:Treference;}
  97. begin
  98. (* used_in_proc_other:=used_in_proc_other+s;
  99. for r:=R_MM0 to R_MM6 do
  100. begin
  101. pushed[r].pushed:=false;
  102. { if the register is used by the calling subroutine }
  103. if not is_reg_var_other[r] and
  104. (r in s) and
  105. { and is present in use }
  106. not(r in unusedregsmm) then
  107. begin
  108. r2.enum:=R_INTREGISTER;
  109. r2.number:=NR_ESP;
  110. list.concat(Taicpu.Op_const_reg(A_SUB,S_L,8,r2));
  111. reference_reset_base(hr,r2,0);
  112. r2.enum:=r;
  113. list.concat(Taicpu.Op_reg_ref(A_MOVQ,S_NO,r2,hr));
  114. include(unusedregsmm,r);
  115. pushed[r].pushed:=true;
  116. end;
  117. end;*)
  118. {$ifdef TEMPREGDEBUG}
  119. testregisters;
  120. {$endif TEMPREGDEBUG}
  121. end;
  122. {$endif SUPPORT_MMX}
  123. {$ifdef SUPPORT_MMX}
  124. procedure trgcpu.popusedotherregisters(list:Taasmoutput;
  125. const pushed:Tpushedsavedother);
  126. { var r:Toldregister;
  127. r2,r3:Tregister;
  128. hr:Treference;}
  129. begin
  130. { restore in reverse order: }
  131. { for r:=R_MM6 downto R_MM0 do
  132. if pushed[r].pushed then
  133. begin
  134. r2.enum:=R_INTREGISTER;
  135. r2.number:=NR_ESP;
  136. reference_reset_base(hr,r2,0);
  137. r3.enum:=r;
  138. list.concat(Taicpu.op_ref_reg(A_MOVQ,S_NO,hr,r3));
  139. list.concat(Taicpu.op_const_reg(A_ADD,S_L,8,r2));
  140. exclude(unusedregsmm,r);
  141. end;}
  142. {$ifdef TEMPREGDEBUG}
  143. testregisters;
  144. {$endif TEMPREGDEBUG}
  145. end;
  146. {$endif SUPPORT_MMX}
  147. procedure trgcpu.saveusedotherregisters(list:Taasmoutput;var saved:Tpushedsavedother;
  148. const s:totherregisterset);
  149. begin
  150. {$ifdef SUPPORT_MMX}
  151. if (aktoptprocessor in [class386,classP5]) or
  152. (CS_LittleSize in aktglobalswitches) then
  153. pushusedotherregisters(list,saved,s)
  154. else
  155. {$endif SUPPORT_MMX}
  156. inherited saveusedotherregisters(list,saved,s);
  157. end;
  158. procedure trgcpu.restoreusedotherregisters(list:Taasmoutput;
  159. const saved:tpushedsavedother);
  160. begin
  161. {$ifdef SUPPORT_MMX}
  162. if (aktoptprocessor in [class386,classP5]) or
  163. (CS_LittleSize in aktglobalswitches) then
  164. popusedotherregisters(list,saved)
  165. else
  166. {$endif SUPPORT_MMX}
  167. inherited restoreusedotherregisters(list,saved);
  168. end;
  169. procedure trgcpu.resetusableregisters;
  170. begin
  171. inherited resetusableregisters;
  172. fpuvaroffset := 0;
  173. end;
  174. function trgcpu.correct_fpuregister(r : tregister;ofs : byte) : tregister;
  175. begin
  176. correct_fpuregister:=r;
  177. setsupreg(correct_fpuregister,ofs);
  178. end;
  179. function trgcpu.makeregsize(reg: tregister; size: tcgsize): tregister;
  180. var
  181. subreg : tsubregister;
  182. begin
  183. if getregtype(reg)<>R_INTREGISTER then
  184. internalerror(200306032);
  185. subreg:=cgsize2subreg(size);
  186. result:=reg;
  187. setsubreg(result,subreg);
  188. add_constraints(result);
  189. end;
  190. end.
  191. {
  192. $Log$
  193. Revision 1.36 2003-10-01 20:34:49 peter
  194. * procinfo unit contains tprocinfo
  195. * cginfo renamed to cgbase
  196. * moved cgmessage to verbose
  197. * fixed ppc and sparc compiles
  198. Revision 1.35 2003/09/11 11:55:00 florian
  199. * improved arm code generation
  200. * move some protected and private field around
  201. * the temp. register for register parameters/arguments are now released
  202. before the move to the parameter register is done. This improves
  203. the code in a lot of cases.
  204. Revision 1.34 2003/09/09 20:59:27 daniel
  205. * Adding register allocation order
  206. Revision 1.33 2003/09/07 22:09:35 peter
  207. * preparations for different default calling conventions
  208. * various RA fixes
  209. Revision 1.32 2003/09/03 15:55:01 peter
  210. * NEWRA branch merged
  211. Revision 1.31.2.3 2003/08/31 13:50:16 daniel
  212. * Remove sorting and use pregenerated indexes
  213. * Some work on making things compile
  214. Revision 1.31.2.2 2003/08/28 18:35:08 peter
  215. * tregister changed to cardinal
  216. Revision 1.31.2.1 2003/08/27 19:55:54 peter
  217. * first tregister patch
  218. Revision 1.31 2003/08/20 09:07:00 daniel
  219. * New register coding now mandatory, some more convert_registers calls
  220. removed.
  221. Revision 1.30 2003/08/17 08:48:02 daniel
  222. * Another register allocator bug fixed.
  223. * cpu_registers set to 6 for i386
  224. Revision 1.29 2003/06/17 16:51:30 peter
  225. * cycle fixes
  226. Revision 1.28 2003/06/17 16:34:44 jonas
  227. * lots of newra fixes (need getfuncretparaloc implementation for i386)!
  228. * renamed all_intregisters to volatile_intregisters and made it
  229. processor dependent
  230. Revision 1.27 2003/06/13 21:19:31 peter
  231. * current_procdef removed, use current_procinfo.procdef instead
  232. Revision 1.26 2003/06/12 21:12:20 peter
  233. * size para for ungetregisterfpu
  234. Revision 1.25 2003/06/03 21:11:09 peter
  235. * cg.a_load_* get a from and to size specifier
  236. * makeregsize only accepts newregister
  237. * i386 uses generic tcgnotnode,tcgunaryminus
  238. Revision 1.24 2003/06/03 13:01:59 daniel
  239. * Register allocator finished
  240. Revision 1.23 2003/06/01 21:38:06 peter
  241. * getregisterfpu size parameter added
  242. * op_const_reg size parameter added
  243. * sparc updates
  244. Revision 1.22 2003/05/16 14:33:31 peter
  245. * regvar fixes
  246. Revision 1.21 2003/04/25 08:25:26 daniel
  247. * Ifdefs around a lot of calls to cleartempgen
  248. * Fixed registers that are allocated but not freed in several nodes
  249. * Tweak to register allocator to cause less spills
  250. * 8-bit registers now interfere with esi,edi and ebp
  251. Compiler can now compile rtl successfully when using new register
  252. allocator
  253. Revision 1.20 2003/04/23 14:42:08 daniel
  254. * Further register allocator work. Compiler now smaller with new
  255. allocator than without.
  256. * Somebody forgot to adjust ppu version number
  257. Revision 1.19 2003/04/22 10:09:35 daniel
  258. + Implemented the actual register allocator
  259. + Scratch registers unavailable when new register allocator used
  260. + maybe_save/maybe_restore unavailable when new register allocator used
  261. Revision 1.18 2003/04/21 19:16:50 peter
  262. * count address regs separate
  263. Revision 1.17 2003/03/28 19:16:57 peter
  264. * generic constructor working for i386
  265. * remove fixed self register
  266. * esi added as address register for i386
  267. Revision 1.16 2003/03/17 15:52:57 peter
  268. * SUPPORT_MMX define compile fix
  269. Revision 1.15 2003/03/08 13:59:17 daniel
  270. * Work to handle new register notation in ag386nsm
  271. + Added newra version of Ti386moddivnode
  272. Revision 1.14 2003/03/08 08:59:07 daniel
  273. + $define newra will enable new register allocator
  274. + getregisterint will return imaginary registers with $newra
  275. + -sr switch added, will skip register allocation so you can see
  276. the direct output of the code generator before register allocation
  277. Revision 1.13 2003/03/07 21:57:53 daniel
  278. * Improved getregisterint
  279. Revision 1.12 2003/02/19 22:00:16 daniel
  280. * Code generator converted to new register notation
  281. - Horribily outdated todo.txt removed
  282. Revision 1.11 2003/01/08 18:43:57 daniel
  283. * Tregister changed into a record
  284. Revision 1.10 2002/10/05 12:43:29 carl
  285. * fixes for Delphi 6 compilation
  286. (warning : Some features do not work under Delphi)
  287. Revision 1.9 2002/08/17 09:23:48 florian
  288. * first part of procinfo rewrite
  289. Revision 1.8 2002/07/01 18:46:34 peter
  290. * internal linker
  291. * reorganized aasm layer
  292. Revision 1.7 2002/05/16 19:46:52 carl
  293. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  294. + try to fix temp allocation (still in ifdef)
  295. + generic constructor calls
  296. + start of tassembler / tmodulebase class cleanup
  297. Revision 1.6 2002/05/12 16:53:18 peter
  298. * moved entry and exitcode to ncgutil and cgobj
  299. * foreach gets extra argument for passing local data to the
  300. iterator function
  301. * -CR checks also class typecasts at runtime by changing them
  302. into as
  303. * fixed compiler to cycle with the -CR option
  304. * fixed stabs with elf writer, finally the global variables can
  305. be watched
  306. * removed a lot of routines from cga unit and replaced them by
  307. calls to cgobj
  308. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  309. u32bit then the other is typecasted also to u32bit without giving
  310. a rangecheck warning/error.
  311. * fixed pascal calling method with reversing also the high tree in
  312. the parast, detected by tcalcst3 test
  313. Revision 1.5 2002/04/21 15:43:32 carl
  314. * changeregsize -> rg.makeregsize
  315. * changeregsize moved from cpubase to here
  316. Revision 1.4 2002/04/15 19:44:22 peter
  317. * fixed stackcheck that would be called recursively when a stack
  318. error was found
  319. * generic changeregsize(reg,size) for i386 register resizing
  320. * removed some more routines from cga unit
  321. * fixed returnvalue handling
  322. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  323. Revision 1.3 2002/04/04 19:06:13 peter
  324. * removed unused units
  325. * use tlocation.size in cg.a_*loc*() routines
  326. Revision 1.2 2002/04/02 17:11:39 peter
  327. * tlocation,treference update
  328. * LOC_CONSTANT added for better constant handling
  329. * secondadd splitted in multiple routines
  330. * location_force_reg added for loading a location to a register
  331. of a specified size
  332. * secondassignment parses now first the right and then the left node
  333. (this is compatible with Kylix). This saves a lot of push/pop especially
  334. with string operations
  335. * adapted some routines to use the new cg methods
  336. Revision 1.1 2002/03/31 20:26:40 jonas
  337. + a_loadfpu_* and a_loadmm_* methods in tcg
  338. * register allocation is now handled by a class and is mostly processor
  339. independent (+rgobj.pas and i386/rgcpu.pas)
  340. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  341. * some small improvements and fixes to the optimizer
  342. * some register allocation fixes
  343. * some fpuvaroffset fixes in the unary minus node
  344. * push/popusedregisters is now called rg.save/restoreusedregisters and
  345. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  346. also better optimizable)
  347. * fixed and optimized register saving/restoring for new/dispose nodes
  348. * LOC_FPU locations now also require their "register" field to be set to
  349. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  350. - list field removed of the tnode class because it's not used currently
  351. and can cause hard-to-find bugs
  352. }