rgcpu.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. { to keep the same allocation order as with the old routines }
  30. function getregisterint(list:Taasmoutput;size:Tcgsize):Tregister;override;
  31. {$ifndef newra}
  32. procedure ungetregisterint(list:Taasmoutput;r:Tregister); override;
  33. function getexplicitregisterint(list:Taasmoutput;r:Tnewregister):Tregister;override;
  34. {$endif newra}
  35. function getregisterfpu(list: taasmoutput) : tregister; override;
  36. procedure ungetregisterfpu(list: taasmoutput; r : tregister); override;
  37. procedure ungetreference(list: taasmoutput; const ref : treference); override;
  38. {# Returns a subset register of the register r with the specified size.
  39. WARNING: There is no clearing of the upper parts of the register,
  40. if a 8-bit / 16-bit register is converted to a 32-bit register.
  41. It is up to the code generator to correctly zero fill the register
  42. }
  43. function makeregsize(reg: tregister; size: tcgsize): tregister; override;
  44. procedure resetusableregisters;override;
  45. { corrects the fpu stack register by ofs }
  46. function correct_fpuregister(r : tregister;ofs : byte) : tregister;
  47. fpuvaroffset : byte;
  48. end;
  49. implementation
  50. uses
  51. systems,
  52. globals,verbose,
  53. tgobj;
  54. {************************************************************************}
  55. { routine helpers }
  56. {************************************************************************}
  57. const
  58. reg2reg64 : array[firstreg..lastreg] of toldregister = (R_NO,
  59. R_RAX,R_RCX,R_RDX,R_RBX,R_RSP,R_RBP,R_RSI,R_RDI,
  60. R_R8,R_R9,R_R10,R_R11,R_R12,R_R13,R_R14,R_R15,R_RIP,
  61. R_RAX,R_RCX,R_RDX,R_RBX,R_RSP,R_RBP,R_RSI,R_RDI,
  62. R_R8,R_R9,R_R10,R_R11,R_R12,R_R13,R_R14,R_R15,
  63. R_RAX,R_RCX,R_RDX,R_RBX,R_RSP,R_RBP,R_RSI,R_RDI,
  64. R_R8,R_R9,R_R10,R_R11,R_R12,R_R13,R_R14,R_R15,
  65. R_RAX,R_RCX,R_RDX,R_RBX,R_RSP,R_RBP,R_RSI,R_RDI,
  66. R_R8,R_R9,R_R10,R_R11,R_R12,R_R13,R_R14,R_R15,
  67. R_NO,R_NO,R_NO,R_NO,
  68. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  69. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  70. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  71. R_NO,R_NO,R_NO,R_NO,
  72. R_NO,R_NO,R_NO,R_NO,R_NO,
  73. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  74. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  75. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO
  76. );
  77. reg2reg32 : array[firstreg..lastreg] of toldregister = (R_NO,
  78. R_EAX,R_ECX,R_EDX,R_EBX,R_ESP,R_EBP,R_ESI,R_EDI,
  79. R_R8D,R_R9D,R_R10D,R_R11D,R_R12D,R_R13D,R_R14D,R_R15D,R_NO,
  80. R_EAX,R_ECX,R_EDX,R_EBX,R_ESP,R_EBP,R_ESI,R_EDI,
  81. R_R8D,R_R9D,R_R10D,R_R11D,R_R12D,R_R13D,R_R14D,R_R15D,
  82. R_EAX,R_ECX,R_EDX,R_EBX,R_ESP,R_EBP,R_ESI,R_EDI,
  83. R_R8D,R_R9D,R_R10D,R_R11D,R_R12D,R_R13D,R_R14D,R_R15D,
  84. R_EAX,R_ECX,R_EDX,R_EBX,R_ESP,R_EBP,R_ESI,R_EDI,
  85. R_R8D,R_R9D,R_R10D,R_R11D,R_R12D,R_R13D,R_R14D,R_R15D,
  86. R_NO,R_NO,R_NO,R_NO,
  87. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  88. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  89. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  90. R_NO,R_NO,R_NO,R_NO,
  91. R_NO,R_NO,R_NO,R_NO,R_NO,
  92. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  93. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  94. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO
  95. );
  96. reg2reg16 : array[firstreg..lastreg] of toldregister = (R_NO,
  97. R_AX,R_CX,R_DX,R_BX,R_SP,R_BP,R_SI,R_DI,
  98. R_R8W,R_R9W,R_R10W,R_R11W,R_R12W,R_R13W,R_R14W,R_R15W,R_NO,
  99. R_AX,R_CX,R_DX,R_BX,R_SP,R_BP,R_SI,R_DI,
  100. R_R8W,R_R9W,R_R10W,R_R11W,R_R12W,R_R13W,R_R14W,R_R15W,
  101. R_AX,R_CX,R_DX,R_BX,R_SP,R_BP,R_SI,R_DI,
  102. R_R8W,R_R9W,R_R10W,R_R11W,R_R12W,R_R13W,R_R14W,R_R15W,
  103. R_AX,R_CX,R_DX,R_BX,R_SP,R_BP,R_SI,R_DI,
  104. R_R8W,R_R9W,R_R10W,R_R11W,R_R12W,R_R13W,R_R14W,R_R15W,
  105. R_NO,R_NO,R_NO,R_NO,
  106. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  107. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  108. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  109. R_NO,R_NO,R_NO,R_NO,
  110. R_NO,R_NO,R_NO,R_NO,R_NO,
  111. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  112. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  113. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO
  114. );
  115. reg2reg8 : array[firstreg..lastreg] of toldregister = (R_NO,
  116. R_AL,R_CL,R_DL,R_BL,R_SPL,R_BPL,R_SIL,R_DIL,
  117. R_R8B,R_R9B,R_R10B,R_R11B,R_R12B,R_R13B,R_R14B,R_R15B,R_NO,
  118. R_AL,R_CL,R_DL,R_BL,R_SPL,R_BPL,R_SIL,R_DIL,
  119. R_R8B,R_R9B,R_R10B,R_R11B,R_R12B,R_R13B,R_R14B,R_R15B,
  120. R_AL,R_CL,R_DL,R_BL,R_SPL,R_BPL,R_SIL,R_DIL,
  121. R_R8B,R_R9B,R_R10B,R_R11B,R_R12B,R_R13B,R_R14B,R_R15B,
  122. R_AL,R_CL,R_DL,R_BL,R_SPL,R_BPL,R_SIL,R_DIL,
  123. R_R8B,R_R9B,R_R10B,R_R11B,R_R12B,R_R13B,R_R14B,R_R15B,
  124. R_NO,R_NO,R_NO,R_NO,
  125. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  126. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  127. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  128. R_NO,R_NO,R_NO,R_NO,
  129. R_NO,R_NO,R_NO,R_NO,R_NO,
  130. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  131. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  132. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO
  133. );
  134. { convert a register to a specfied register size }
  135. function changeregsize(r:tregister;size:topsize):tregister;
  136. var
  137. reg : tregister;
  138. begin
  139. case size of
  140. S_B :
  141. reg.enum:=reg2reg8[r.enum];
  142. S_W :
  143. reg.enum:=reg2reg16[r.enum];
  144. S_L :
  145. reg.enum:=reg2reg32[r.enum];
  146. S_Q :
  147. reg.enum:=reg2reg64[r.enum];
  148. else
  149. internalerror(200204101);
  150. end;
  151. if reg.enum=R_NO then
  152. internalerror(200204102);
  153. changeregsize:=reg;
  154. end;
  155. {************************************************************************}
  156. { trgcpu }
  157. {************************************************************************}
  158. function trgcpu.getregisterint(list: taasmoutput;size:Tcgsize): tregister;
  159. var subreg:Tsubregister;
  160. begin
  161. subreg:=cgsize2subreg(size);
  162. if countunusedregsint=0 then
  163. internalerror(10);
  164. result.enum:=R_INTREGISTER;
  165. {$ifdef TEMPREGDEBUG}
  166. if curptree^.usableregsint-countunusedregsint>curptree^.registers32 then
  167. internalerror(10);
  168. {$endif TEMPREGDEBUG}
  169. {$ifdef EXTTEMPREGDEBUG}
  170. if curptree^.usableregs-countunusedregistersint>curptree^^.reallyusedregs then
  171. curptree^.reallyusedregs:=curptree^^.usableregs-countunusedregistersint;
  172. {$endif EXTTEMPREGDEBUG}
  173. if RS_RAX in unusedregsint then
  174. begin
  175. dec(countunusedregsint);
  176. exclude(unusedregsint,RS_RAX);
  177. include(used_in_proc_int,RS_RAX);
  178. result.number:=RS_RAX shl 8 or subreg;
  179. {$ifdef TEMPREGDEBUG}
  180. reg_user[R_RAX]:=curptree^;
  181. {$endif TEMPREGDEBUG}
  182. exprasmlist.concat(tai_regalloc.alloc(result));
  183. end
  184. else if RS_RDX in unusedregsint then
  185. begin
  186. dec(countunusedregsint);
  187. exclude(unusedregsint,RS_RDX);
  188. include(used_in_proc_int,RS_RDX);
  189. result.number:=RS_RDX shl 8 or subreg;
  190. {$ifdef TEMPREGDEBUG}
  191. reg_user[R_RDX]:=curptree^;
  192. {$endif TEMPREGDEBUG}
  193. exprasmlist.concat(tai_regalloc.alloc(result));
  194. end
  195. else if RS_RBX in unusedregsint then
  196. begin
  197. dec(countunusedregsint);
  198. exclude(unusedregsint,RS_RBX);
  199. include(used_in_proc_int,RS_RBX);
  200. result.number:=RS_RBX shl 8 or subreg;
  201. {$ifdef TEMPREGDEBUG}
  202. reg_user[R_RBX]:=curptree^;
  203. {$endif TEMPREGDEBUG}
  204. exprasmlist.concat(tai_regalloc.alloc(result));
  205. end
  206. else if RS_RCX in unusedregsint then
  207. begin
  208. dec(countunusedregsint);
  209. exclude(unusedregsint,RS_RCX);
  210. include(used_in_proc_int,RS_RCX);
  211. result.number:=RS_RCX shl 8 or subreg;
  212. {$ifdef TEMPREGDEBUG}
  213. reg_user[R_RCX]:=curptree^;
  214. {$endif TEMPREGDEBUG}
  215. exprasmlist.concat(tai_regalloc.alloc(result));
  216. end
  217. else
  218. internalerror(10);
  219. {$ifdef TEMPREGDEBUG}
  220. testregisters;
  221. {$endif TEMPREGDEBUG}
  222. end;
  223. procedure trgcpu.ungetregisterint(list: taasmoutput; r : tregister);
  224. var supreg:Tsuperregister;
  225. begin
  226. if r.enum=R_NO then
  227. exit;
  228. if r.enum<>R_INTREGISTER then
  229. internalerror(200301234);
  230. supreg:=r.number shr 8;
  231. if (supreg in [RS_RDI]) then
  232. begin
  233. list.concat(tai_regalloc.DeAlloc(r));
  234. exit;
  235. end;
  236. if not(supreg in [RS_RAX,RS_RBX,RS_RCX,RS_RDX,RS_RSI]) then
  237. exit;
  238. inherited ungetregisterint(list,r);
  239. end;
  240. function trgcpu.getexplicitregisterint(list: taasmoutput; r : tnewregister) : tregister;
  241. var r2:Tregister;
  242. begin
  243. if (r shr 8) in [RS_RDI] then
  244. begin
  245. r2.enum:=R_INTREGISTER;
  246. r2.number:=r;
  247. list.concat(Tai_regalloc.alloc(r2));
  248. getexplicitregisterint:=r2;
  249. exit;
  250. end;
  251. result:=inherited getexplicitregisterint(list,r);
  252. end;
  253. function trgcpu.getregisterfpu(list: taasmoutput) : tregister;
  254. begin
  255. { note: don't return R_ST0, see comments above implementation of }
  256. { a_loadfpu_* methods in cgcpu (JM) }
  257. result.enum := R_ST;
  258. end;
  259. procedure trgcpu.ungetregisterfpu(list : taasmoutput; r : tregister);
  260. begin
  261. { nothing to do, fpu stack management is handled by the load/ }
  262. { store operations in cgcpu (JM) }
  263. end;
  264. procedure trgcpu.ungetreference(list: taasmoutput; const ref : treference);
  265. begin
  266. ungetregisterint(list,ref.base);
  267. ungetregisterint(list,ref.index);
  268. end;
  269. procedure trgcpu.resetusableregisters;
  270. begin
  271. inherited resetusableregisters;
  272. fpuvaroffset := 0;
  273. end;
  274. function trgcpu.correct_fpuregister(r : tregister;ofs : byte) : tregister;
  275. begin
  276. correct_fpuregister.enum:=toldregister(longint(r.enum)+ofs);
  277. end;
  278. function trgcpu.makeregsize(reg: tregister; size: tcgsize): tregister;
  279. var
  280. _result : topsize;
  281. begin
  282. case size of
  283. OS_32,OS_S32:
  284. begin
  285. _result := S_L;
  286. end;
  287. OS_8,OS_S8:
  288. begin
  289. _result := S_B;
  290. end;
  291. OS_16,OS_S16:
  292. begin
  293. _result := S_W;
  294. end;
  295. else
  296. internalerror(2001092312);
  297. end;
  298. makeregsize := changeregsize(reg,_result);
  299. end;
  300. initialization
  301. rg := trgcpu.create(15);
  302. end.
  303. {
  304. $Log$
  305. Revision 1.5 2003-06-13 21:19:33 peter
  306. * current_procdef removed, use current_procinfo.procdef instead
  307. Revision 1.4 2002/04/25 20:15:40 florian
  308. * block nodes within expressions shouldn't release the used registers,
  309. fixed using a flag till the new rg is ready
  310. Revision 1.3 2003/01/05 13:36:54 florian
  311. * x86-64 compiles
  312. + very basic support for float128 type (x86-64 only)
  313. Revision 1.2 2002/07/25 22:55:34 florian
  314. * several fixes, small test units can be compiled
  315. Revision 1.1 2002/07/24 22:38:15 florian
  316. + initial release of x86-64 target code
  317. Revision 1.8 2002/07/01 18:46:34 peter
  318. * internal linker
  319. * reorganized aasm layer
  320. Revision 1.7 2002/05/16 19:46:52 carl
  321. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  322. + try to fix temp allocation (still in ifdef)
  323. + generic constructor calls
  324. + start of tassembler / tmodulebase class cleanup
  325. Revision 1.6 2002/05/12 16:53:18 peter
  326. * moved entry and exitcode to ncgutil and cgobj
  327. * foreach gets extra argument for passing local data to the
  328. iterator function
  329. * -CR checks also class typecasts at runtime by changing them
  330. into as
  331. * fixed compiler to cycle with the -CR option
  332. * fixed stabs with elf writer, finally the global variables can
  333. be watched
  334. * removed a lot of routines from cga unit and replaced them by
  335. calls to cgobj
  336. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  337. u32bit then the other is typecasted also to u32bit without giving
  338. a rangecheck warning/error.
  339. * fixed pascal calling method with reversing also the high tree in
  340. the parast, detected by tcalcst3 test
  341. Revision 1.5 2002/04/21 15:43:32 carl
  342. * changeregsize -> rg.makeregsize
  343. * changeregsize moved from cpubase to here
  344. Revision 1.4 2002/04/15 19:44:22 peter
  345. * fixed stackcheck that would be called recursively when a stack
  346. error was found
  347. * generic changeregsize(reg,size) for i386 register resizing
  348. * removed some more routines from cga unit
  349. * fixed returnvalue handling
  350. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  351. Revision 1.3 2002/04/04 19:06:13 peter
  352. * removed unused units
  353. * use tlocation.size in cg.a_*loc*() routines
  354. Revision 1.2 2002/04/02 17:11:39 peter
  355. * tlocation,treference update
  356. * LOC_CONSTANT added for better constant handling
  357. * secondadd splitted in multiple routines
  358. * location_force_reg added for loading a location to a register
  359. of a specified size
  360. * secondassignment parses now first the right and then the left node
  361. (this is compatible with Kylix). This saves a lot of push/pop especially
  362. with string operations
  363. * adapted some routines to use the new cg methods
  364. Revision 1.1 2002/03/31 20:26:40 jonas
  365. + a_loadfpu_* and a_loadmm_* methods in tcg
  366. * register allocation is now handled by a class and is mostly processor
  367. independent (+rgobj.pas and i386/rgcpu.pas)
  368. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  369. * some small improvements and fixes to the optimizer
  370. * some register allocation fixes
  371. * some fpuvaroffset fixes in the unary minus node
  372. * push/popusedregisters is now called rg.save/restoreusedregisters and
  373. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  374. also better optimizable)
  375. * fixed and optimized register saving/restoring for new/dispose nodes
  376. * LOC_FPU locations now also require their "register" field to be set to
  377. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  378. - list field removed of the tnode class because it's not used currently
  379. and can cause hard-to-find bugs
  380. }