rgcpu.pas 14 KB

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