rgx86.pas 15 KB

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