rgcpu.pas 14 KB

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