rgcpu.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. procedure add_constraints(reg:Tregister);override;
  30. end;
  31. tpushedsavedloc = record
  32. case byte of
  33. 0: (pushed: boolean);
  34. 1: (ofs: longint);
  35. end;
  36. tpushedsavedfpu = array[tsuperregister] of tpushedsavedloc;
  37. trgx86fpu = class
  38. { The "usableregsxxx" contain all registers of type "xxx" that }
  39. { aren't currently allocated to a regvar. The "unusedregsxxx" }
  40. { contain all registers of type "xxx" that aren't currently }
  41. { allocated }
  42. unusedregsfpu,usableregsfpu : Tsuperregisterset;
  43. { these counters contain the number of elements in the }
  44. { unusedregsxxx/usableregsxxx sets }
  45. countunusedregsfpu : byte;
  46. { Contains the registers which are really used by the proc itself.
  47. It doesn't take care of registers used by called procedures
  48. }
  49. used_in_proc : tcpuregisterset;
  50. {reg_pushes_other : regvarother_longintarray;
  51. is_reg_var_other : regvarother_booleanarray;
  52. regvar_loaded_other : regvarother_booleanarray;}
  53. { tries to hold the amount of times which the current tree is processed }
  54. t_times: longint;
  55. fpuvaroffset : byte;
  56. constructor create;
  57. function getregisterfpu(list: taasmoutput) : tregister;
  58. procedure ungetregisterfpu(list: taasmoutput; r : tregister);
  59. { pushes and restores registers }
  60. procedure saveusedfpuregisters(list:Taasmoutput;
  61. var saved:Tpushedsavedfpu;
  62. const s:Tcpuregisterset);
  63. procedure restoreusedfpuregisters(list:Taasmoutput;
  64. const saved:Tpushedsavedfpu);
  65. { corrects the fpu stack register by ofs }
  66. function correct_fpuregister(r : tregister;ofs : byte) : tregister;
  67. end;
  68. implementation
  69. uses
  70. systems,
  71. verbose;
  72. const
  73. { This value is used in tsaved. If the array value is equal
  74. to this, then this means that this register is not used.}
  75. reg_not_saved = $7fffffff;
  76. {************************************************************************
  77. trgcpu
  78. *************************************************************************}
  79. procedure trgcpu.add_constraints(reg:Tregister);
  80. var
  81. supreg : tsuperregister;
  82. begin
  83. if getsubreg(reg) in [R_SUBL,R_SUBH] then
  84. begin
  85. supreg:=getsupreg(reg);
  86. {These registers have no 8-bit subregister, so add interferences.}
  87. add_edge(supreg,RS_ESI);
  88. add_edge(supreg,RS_EDI);
  89. add_edge(supreg,RS_EBP);
  90. end;
  91. end;
  92. {******************************************************************************
  93. Trgobj
  94. ******************************************************************************}
  95. constructor Trgx86fpu.create;
  96. var i:Tsuperregister;
  97. begin
  98. used_in_proc:=[];
  99. t_times := 0;
  100. unusedregsfpu:=usableregsfpu;
  101. end;
  102. function trgx86fpu.getregisterfpu(list: taasmoutput) : tregister;
  103. begin
  104. { note: don't return R_ST0, see comments above implementation of }
  105. { a_loadfpu_* methods in cgcpu (JM) }
  106. result:=NR_ST;
  107. end;
  108. procedure trgx86fpu.ungetregisterfpu(list : taasmoutput; r : tregister);
  109. begin
  110. { nothing to do, fpu stack management is handled by the load/ }
  111. { store operations in cgcpu (JM) }
  112. end;
  113. function trgx86fpu.correct_fpuregister(r : tregister;ofs : byte) : tregister;
  114. begin
  115. correct_fpuregister:=r;
  116. setsupreg(correct_fpuregister,ofs);
  117. end;
  118. procedure trgx86fpu.saveusedfpuregisters(list: taasmoutput;
  119. var saved : tpushedsavedfpu;
  120. const s: tcpuregisterset);
  121. var
  122. r : tregister;
  123. hr : treference;
  124. begin
  125. used_in_proc:=used_in_proc+s;
  126. {$warning TODO firstsavefpureg}
  127. (*
  128. { don't try to save the fpu registers if not desired (e.g. for }
  129. { the 80x86) }
  130. if firstsavefpureg <> R_NO then
  131. for r.enum:=firstsavefpureg to lastsavefpureg do
  132. begin
  133. saved[r.enum].ofs:=reg_not_saved;
  134. { if the register is used by the calling subroutine and if }
  135. { it's not a regvar (those are handled separately) }
  136. if not is_reg_var_other[r.enum] and
  137. (r.enum in s) and
  138. { and is present in use }
  139. not(r.enum in unusedregsfpu) then
  140. begin
  141. { then save it }
  142. tg.GetTemp(list,extended_size,tt_persistent,hr);
  143. saved[r.enum].ofs:=hr.offset;
  144. cg.a_loadfpu_reg_ref(list,OS_FLOAT,r,hr);
  145. cg.a_reg_dealloc(list,r);
  146. include(unusedregsfpu,r.enum);
  147. inc(countunusedregsfpu);
  148. end;
  149. end;
  150. *)
  151. end;
  152. procedure trgx86fpu.restoreusedfpuregisters(list : taasmoutput;
  153. const saved : tpushedsavedfpu);
  154. var
  155. r,r2 : tregister;
  156. hr : treference;
  157. begin
  158. {$warning TODO firstsavefpureg}
  159. (*
  160. if firstsavefpureg <> R_NO then
  161. for r.enum:=lastsavefpureg downto firstsavefpureg do
  162. begin
  163. if saved[r.enum].ofs <> reg_not_saved then
  164. begin
  165. r2.enum:=R_INTREGISTER;
  166. r2.number:=NR_FRAME_POINTER_REG;
  167. reference_reset_base(hr,r2,saved[r.enum].ofs);
  168. cg.a_reg_alloc(list,r);
  169. cg.a_loadfpu_ref_reg(list,OS_FLOAT,hr,r);
  170. if not (r.enum in unusedregsfpu) then
  171. { internalerror(10)
  172. in n386cal we always save/restore the reg *state*
  173. using save/restoreunusedstate -> the current state
  174. may not be real (JM) }
  175. else
  176. begin
  177. dec(countunusedregsfpu);
  178. exclude(unusedregsfpu,r.enum);
  179. end;
  180. tg.UnGetTemp(list,hr);
  181. end;
  182. end;
  183. *)
  184. end;
  185. (*
  186. procedure Trgx86fpu.saveotherregvars(list: taasmoutput; const s: totherregisterset);
  187. var
  188. r: Tregister;
  189. begin
  190. if not(cs_regvars in aktglobalswitches) then
  191. exit;
  192. if firstsavefpureg <> NR_NO then
  193. for r.enum := firstsavefpureg to lastsavefpureg do
  194. if is_reg_var_other[r.enum] and
  195. (r.enum in s) then
  196. store_regvar(list,r);
  197. end;
  198. *)
  199. end.
  200. {
  201. $Log$
  202. Revision 1.40 2003-10-17 15:08:34 peter
  203. * commented out more obsolete constants
  204. Revision 1.39 2003/10/17 14:38:32 peter
  205. * 64k registers supported
  206. * fixed some memory leaks
  207. Revision 1.38 2003/10/10 17:48:14 peter
  208. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  209. * tregisteralloctor renamed to trgobj
  210. * removed rgobj from a lot of units
  211. * moved location_* and reference_* to cgobj
  212. * first things for mmx register allocation
  213. Revision 1.37 2003/10/09 21:31:37 daniel
  214. * Register allocator splitted, ans abstract now
  215. Revision 1.36 2003/10/01 20:34:49 peter
  216. * procinfo unit contains tprocinfo
  217. * cginfo renamed to cgbase
  218. * moved cgmessage to verbose
  219. * fixed ppc and sparc compiles
  220. Revision 1.35 2003/09/11 11:55:00 florian
  221. * improved arm code generation
  222. * move some protected and private field around
  223. * the temp. register for register parameters/arguments are now released
  224. before the move to the parameter register is done. This improves
  225. the code in a lot of cases.
  226. Revision 1.34 2003/09/09 20:59:27 daniel
  227. * Adding register allocation order
  228. Revision 1.33 2003/09/07 22:09:35 peter
  229. * preparations for different default calling conventions
  230. * various RA fixes
  231. Revision 1.32 2003/09/03 15:55:01 peter
  232. * NEWRA branch merged
  233. Revision 1.31.2.3 2003/08/31 13:50:16 daniel
  234. * Remove sorting and use pregenerated indexes
  235. * Some work on making things compile
  236. Revision 1.31.2.2 2003/08/28 18:35:08 peter
  237. * tregister changed to cardinal
  238. Revision 1.31.2.1 2003/08/27 19:55:54 peter
  239. * first tregister patch
  240. Revision 1.31 2003/08/20 09:07:00 daniel
  241. * New register coding now mandatory, some more convert_registers calls
  242. removed.
  243. Revision 1.30 2003/08/17 08:48:02 daniel
  244. * Another register allocator bug fixed.
  245. * cpu_registers set to 6 for i386
  246. Revision 1.29 2003/06/17 16:51:30 peter
  247. * cycle fixes
  248. Revision 1.28 2003/06/17 16:34:44 jonas
  249. * lots of newra fixes (need getfuncretparaloc implementation for i386)!
  250. * renamed all_intregisters to volatile_intregisters and made it
  251. processor dependent
  252. Revision 1.27 2003/06/13 21:19:31 peter
  253. * current_procdef removed, use current_procinfo.procdef instead
  254. Revision 1.26 2003/06/12 21:12:20 peter
  255. * size para for ungetregisterfpu
  256. Revision 1.25 2003/06/03 21:11:09 peter
  257. * cg.a_load_* get a from and to size specifier
  258. * makeregsize only accepts newregister
  259. * i386 uses generic tcgnotnode,tcgunaryminus
  260. Revision 1.24 2003/06/03 13:01:59 daniel
  261. * Register allocator finished
  262. Revision 1.23 2003/06/01 21:38:06 peter
  263. * getregisterfpu size parameter added
  264. * op_const_reg size parameter added
  265. * sparc updates
  266. Revision 1.22 2003/05/16 14:33:31 peter
  267. * regvar fixes
  268. Revision 1.21 2003/04/25 08:25:26 daniel
  269. * Ifdefs around a lot of calls to cleartempgen
  270. * Fixed registers that are allocated but not freed in several nodes
  271. * Tweak to register allocator to cause less spills
  272. * 8-bit registers now interfere with esi,edi and ebp
  273. Compiler can now compile rtl successfully when using new register
  274. allocator
  275. Revision 1.20 2003/04/23 14:42:08 daniel
  276. * Further register allocator work. Compiler now smaller with new
  277. allocator than without.
  278. * Somebody forgot to adjust ppu version number
  279. Revision 1.19 2003/04/22 10:09:35 daniel
  280. + Implemented the actual register allocator
  281. + Scratch registers unavailable when new register allocator used
  282. + maybe_save/maybe_restore unavailable when new register allocator used
  283. Revision 1.18 2003/04/21 19:16:50 peter
  284. * count address regs separate
  285. Revision 1.17 2003/03/28 19:16:57 peter
  286. * generic constructor working for i386
  287. * remove fixed self register
  288. * esi added as address register for i386
  289. Revision 1.16 2003/03/17 15:52:57 peter
  290. * SUPPORT_MMX define compile fix
  291. Revision 1.15 2003/03/08 13:59:17 daniel
  292. * Work to handle new register notation in ag386nsm
  293. + Added newra version of Ti386moddivnode
  294. Revision 1.14 2003/03/08 08:59:07 daniel
  295. + $define newra will enable new register allocator
  296. + getregisterint will return imaginary registers with $newra
  297. + -sr switch added, will skip register allocation so you can see
  298. the direct output of the code generator before register allocation
  299. Revision 1.13 2003/03/07 21:57:53 daniel
  300. * Improved getregisterint
  301. Revision 1.12 2003/02/19 22:00:16 daniel
  302. * Code generator converted to new register notation
  303. - Horribily outdated todo.txt removed
  304. Revision 1.11 2003/01/08 18:43:57 daniel
  305. * Tregister changed into a record
  306. Revision 1.10 2002/10/05 12:43:29 carl
  307. * fixes for Delphi 6 compilation
  308. (warning : Some features do not work under Delphi)
  309. Revision 1.9 2002/08/17 09:23:48 florian
  310. * first part of procinfo rewrite
  311. Revision 1.8 2002/07/01 18:46:34 peter
  312. * internal linker
  313. * reorganized aasm layer
  314. Revision 1.7 2002/05/16 19:46:52 carl
  315. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  316. + try to fix temp allocation (still in ifdef)
  317. + generic constructor calls
  318. + start of tassembler / tmodulebase class cleanup
  319. Revision 1.6 2002/05/12 16:53:18 peter
  320. * moved entry and exitcode to ncgutil and cgobj
  321. * foreach gets extra argument for passing local data to the
  322. iterator function
  323. * -CR checks also class typecasts at runtime by changing them
  324. into as
  325. * fixed compiler to cycle with the -CR option
  326. * fixed stabs with elf writer, finally the global variables can
  327. be watched
  328. * removed a lot of routines from cga unit and replaced them by
  329. calls to cgobj
  330. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  331. u32bit then the other is typecasted also to u32bit without giving
  332. a rangecheck warning/error.
  333. * fixed pascal calling method with reversing also the high tree in
  334. the parast, detected by tcalcst3 test
  335. Revision 1.5 2002/04/21 15:43:32 carl
  336. * changeregsize -> rg.makeregsize
  337. * changeregsize moved from cpubase to here
  338. Revision 1.4 2002/04/15 19:44:22 peter
  339. * fixed stackcheck that would be called recursively when a stack
  340. error was found
  341. * generic changeregsize(reg,size) for i386 register resizing
  342. * removed some more routines from cga unit
  343. * fixed returnvalue handling
  344. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  345. Revision 1.3 2002/04/04 19:06:13 peter
  346. * removed unused units
  347. * use tlocation.size in cg.a_*loc*() routines
  348. Revision 1.2 2002/04/02 17:11:39 peter
  349. * tlocation,treference update
  350. * LOC_CONSTANT added for better constant handling
  351. * secondadd splitted in multiple routines
  352. * location_force_reg added for loading a location to a register
  353. of a specified size
  354. * secondassignment parses now first the right and then the left node
  355. (this is compatible with Kylix). This saves a lot of push/pop especially
  356. with string operations
  357. * adapted some routines to use the new cg methods
  358. Revision 1.1 2002/03/31 20:26:40 jonas
  359. + a_loadfpu_* and a_loadmm_* methods in tcg
  360. * register allocation is now handled by a class and is mostly processor
  361. independent (+rgobj.pas and i386/rgcpu.pas)
  362. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  363. * some small improvements and fixes to the optimizer
  364. * some register allocation fixes
  365. * some fpuvaroffset fixes in the unary minus node
  366. * push/popusedregisters is now called rg.save/restoreusedregisters and
  367. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  368. also better optimizable)
  369. * fixed and optimized register saving/restoring for new/dispose nodes
  370. * LOC_FPU locations now also require their "register" field to be set to
  371. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  372. - list field removed of the tnode class because it's not used currently
  373. and can cause hard-to-find bugs
  374. }