rgcpu.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. cpuasm,
  26. tainst,
  27. cclasses,globtype,cgbase,aasm,cginfo,rgobj;
  28. type
  29. trgcpu = class(trgobj)
  30. { to keep the same allocation order as with the old routines }
  31. function getregisterint(list: taasmoutput): tregister; override;
  32. procedure ungetregisterint(list: taasmoutput; r : tregister); override;
  33. function getexplicitregisterint(list: taasmoutput; r : tregister) : tregister; override;
  34. function getregisterfpu(list: taasmoutput) : tregister; override;
  35. procedure ungetregisterfpu(list: taasmoutput; r : tregister); override;
  36. procedure ungetreference(list: taasmoutput; const ref : treference); override;
  37. {# Returns a subset register of the register r with the specified size.
  38. WARNING: There is no clearing of the upper parts of the register,
  39. if a 8-bit / 16-bit register is converted to a 32-bit register.
  40. It is up to the code generator to correctly zero fill the register
  41. }
  42. function makeregsize(reg: tregister; size: tcgsize): tregister; override;
  43. { pushes and restores registers }
  44. procedure pushusedregisters(list: taasmoutput;
  45. var pushed : tpushedsaved;const s: tregisterset);
  46. procedure popusedregisters(list: taasmoutput;
  47. const pushed : tpushedsaved);
  48. procedure saveusedregisters(list: taasmoutput;
  49. var saved : tpushedsaved;const s: tregisterset);override;
  50. procedure restoreusedregisters(list: taasmoutput;
  51. const saved : tpushedsaved);override;
  52. procedure resetusableregisters;override;
  53. { corrects the fpu stack register by ofs }
  54. function correct_fpuregister(r : tregister;ofs : byte) : tregister;
  55. fpuvaroffset : byte;
  56. end;
  57. implementation
  58. uses
  59. systems,
  60. globals,verbose,
  61. tgobj;
  62. {************************************************************************}
  63. { routine helpers }
  64. {************************************************************************}
  65. const
  66. reg2reg32 : array[tregister] of tregister = (R_NO,
  67. R_EAX,R_ECX,R_EDX,R_EBX,R_ESP,R_EBP,R_ESI,R_EDI,
  68. R_EAX,R_ECX,R_EDX,R_EBX,R_ESP,R_EBP,R_ESI,R_EDI,
  69. R_EAX,R_ECX,R_EDX,R_EBX,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,R_NO,R_NO,R_NO,R_NO,R_NO,
  72. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  73. R_NO,R_NO,R_NO,R_NO,
  74. 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. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO
  77. );
  78. reg2reg16 : array[tregister] of tregister = (R_NO,
  79. R_AX,R_CX,R_DX,R_BX,R_SP,R_BP,R_SI,R_DI,
  80. R_AX,R_CX,R_DX,R_BX,R_SP,R_BP,R_SI,R_DI,
  81. R_AX,R_CX,R_DX,R_BX,R_NO,R_NO,R_NO,R_NO,
  82. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  83. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  84. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  85. R_NO,R_NO,R_NO,R_NO,
  86. R_NO,R_NO,R_NO,R_NO,R_NO,
  87. R_NO,R_NO,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
  89. );
  90. reg2reg8 : array[tregister] of tregister = (R_NO,
  91. R_AL,R_CL,R_DL,R_BL,R_NO,R_NO,R_NO,R_NO,
  92. R_AL,R_CL,R_DL,R_BL,R_NO,R_NO,R_NO,R_NO,
  93. R_AL,R_CL,R_DL,R_BL,R_NO,R_NO,R_NO,R_NO,
  94. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  95. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  96. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  97. R_NO,R_NO,R_NO,R_NO,
  98. R_NO,R_NO,R_NO,R_NO,R_NO,
  99. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,
  100. R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO,R_NO
  101. );
  102. { convert a register to a specfied register size }
  103. function changeregsize(r:tregister;size:topsize):tregister;
  104. var
  105. reg : tregister;
  106. begin
  107. case size of
  108. S_B :
  109. reg:=reg2reg8[r];
  110. S_W :
  111. reg:=reg2reg16[r];
  112. S_L :
  113. reg:=reg2reg32[r];
  114. else
  115. internalerror(200204101);
  116. end;
  117. if reg=R_NO then
  118. internalerror(200204102);
  119. changeregsize:=reg;
  120. end;
  121. {************************************************************************}
  122. { trgcpu }
  123. {************************************************************************}
  124. function trgcpu.getregisterint(list: taasmoutput): tregister;
  125. begin
  126. if countunusedregsint=0 then
  127. internalerror(10);
  128. {$ifdef TEMPREGDEBUG}
  129. if curptree^.usableregsint-countunusedregsint>curptree^.registers32 then
  130. internalerror(10);
  131. {$endif TEMPREGDEBUG}
  132. {$ifdef EXTTEMPREGDEBUG}
  133. if curptree^.usableregs-countunusedregistersint>curptree^^.reallyusedregs then
  134. curptree^.reallyusedregs:=curptree^^.usableregs-countunusedregistersint;
  135. {$endif EXTTEMPREGDEBUG}
  136. dec(countunusedregsint);
  137. if R_EAX in unusedregsint then
  138. begin
  139. exclude(unusedregsint,R_EAX);
  140. include(usedinproc,R_EAX);
  141. getregisterint:=R_EAX;
  142. {$ifdef TEMPREGDEBUG}
  143. reg_user[R_EAX]:=curptree^;
  144. {$endif TEMPREGDEBUG}
  145. exprasmlist.concat(tairegalloc.alloc(R_EAX));
  146. end
  147. else if R_EDX in unusedregsint then
  148. begin
  149. exclude(unusedregsint,R_EDX);
  150. include(usedinproc,R_EDX);
  151. getregisterint:=R_EDX;
  152. {$ifdef TEMPREGDEBUG}
  153. reg_user[R_EDX]:=curptree^;
  154. {$endif TEMPREGDEBUG}
  155. exprasmlist.concat(tairegalloc.alloc(R_EDX));
  156. end
  157. else if R_EBX in unusedregsint then
  158. begin
  159. exclude(unusedregsint,R_EBX);
  160. include(usedinproc,R_EBX);
  161. getregisterint:=R_EBX;
  162. {$ifdef TEMPREGDEBUG}
  163. reg_user[R_EBX]:=curptree^;
  164. {$endif TEMPREGDEBUG}
  165. exprasmlist.concat(tairegalloc.alloc(R_EBX));
  166. end
  167. else if R_ECX in unusedregsint then
  168. begin
  169. exclude(unusedregsint,R_ECX);
  170. include(usedinproc,R_ECX);
  171. getregisterint:=R_ECX;
  172. {$ifdef TEMPREGDEBUG}
  173. reg_user[R_ECX]:=curptree^;
  174. {$endif TEMPREGDEBUG}
  175. exprasmlist.concat(tairegalloc.alloc(R_ECX));
  176. end
  177. else internalerror(10);
  178. {$ifdef TEMPREGDEBUG}
  179. testregisters;
  180. {$endif TEMPREGDEBUG}
  181. end;
  182. procedure trgcpu.ungetregisterint(list: taasmoutput; r : tregister);
  183. begin
  184. if r=R_NO then
  185. exit;
  186. r := makeregsize(r,OS_INT);
  187. if (r = R_EDI) or
  188. ((not assigned(procinfo^._class)) and (r = R_ESI)) then
  189. begin
  190. list.concat(Tairegalloc.DeAlloc(r));
  191. exit;
  192. end;
  193. if not(r in [R_EAX,R_EBX,R_ECX,R_EDX]) then
  194. exit;
  195. inherited ungetregisterint(list,r);
  196. end;
  197. function trgcpu.getexplicitregisterint(list: taasmoutput; r : tregister) : tregister;
  198. begin
  199. if r in [R_ESI,R_EDI] then
  200. begin
  201. list.concat(Tairegalloc.Alloc(r));
  202. getexplicitregisterint := r;
  203. exit;
  204. end;
  205. result := inherited getexplicitregisterint(list,r);
  206. end;
  207. function trgcpu.getregisterfpu(list: taasmoutput) : tregister;
  208. begin
  209. { note: don't return R_ST0, see comments above implementation of }
  210. { a_loadfpu_* methods in cgcpu (JM) }
  211. result := R_ST;
  212. end;
  213. procedure trgcpu.ungetregisterfpu(list : taasmoutput; r : tregister);
  214. begin
  215. { nothing to do, fpu stack management is handled by the load/ }
  216. { store operations in cgcpu (JM) }
  217. end;
  218. procedure trgcpu.ungetreference(list: taasmoutput; const ref : treference);
  219. begin
  220. ungetregisterint(list,ref.base);
  221. ungetregisterint(list,ref.index);
  222. end;
  223. procedure trgcpu.pushusedregisters(list: taasmoutput;
  224. var pushed : tpushedsaved; const s: tregisterset);
  225. var
  226. r: tregister;
  227. {$ifdef SUPPORT_MMX}
  228. hr : treference;
  229. {$endif SUPPORT_MMX}
  230. begin
  231. usedinproc:=usedinproc + s;
  232. for r:=R_EAX to R_EBX do
  233. begin
  234. pushed[r].pushed:=false;
  235. { if the register is used by the calling subroutine }
  236. if not is_reg_var[r] and
  237. (r in s) and
  238. { and is present in use }
  239. not(r in unusedregsint) then
  240. begin
  241. { then save it }
  242. list.concat(Taicpu.Op_reg(A_PUSH,S_L,r));
  243. include(unusedregsint,r);
  244. inc(countunusedregsint);
  245. pushed[r].pushed:=true;
  246. end;
  247. end;
  248. {$ifdef SUPPORT_MMX}
  249. for r:=R_MM0 to R_MM6 do
  250. begin
  251. pushed[r].pushed:=false;
  252. { if the register is used by the calling subroutine }
  253. if not is_reg_var[r] and
  254. (r in s) and
  255. { and is present in use }
  256. not(r in unusedregsmm) then
  257. begin
  258. list.concat(Taicpu.Op_const_reg(A_SUB,S_L,8,R_ESP));
  259. reference_reset_base(hr,R_ESP,0);
  260. list.concat(Taicpu.Op_reg_ref(A_MOVQ,S_NO,r,hr));
  261. include(unusedregsmm,r);
  262. inc(countunusedregsmm);
  263. pushed[r].pushed:=true;
  264. end;
  265. end;
  266. {$endif SUPPORT_MMX}
  267. {$ifdef TEMPREGDEBUG}
  268. testregisters;
  269. {$endif TEMPREGDEBUG}
  270. end;
  271. procedure trgcpu.popusedregisters(list: taasmoutput;
  272. const pushed : tpushedsaved);
  273. var
  274. r : tregister;
  275. {$ifdef SUPPORT_MMX}
  276. hr : treference;
  277. {$endif SUPPORT_MMX}
  278. begin
  279. { restore in reverse order: }
  280. {$ifdef SUPPORT_MMX}
  281. for r:=R_MM6 downto R_MM0 do
  282. if pushed[r].pushed then
  283. begin
  284. reference_reset_base(hr,R_ESP,0);
  285. list.concat(Taicpu.Op_ref_reg(
  286. A_MOVQ,S_NO,hr,r));
  287. list.concat(Taicpu.Op_const_reg(
  288. A_ADD,S_L,8,R_ESP));
  289. if not (r in unusedregsmm) then
  290. { internalerror(10)
  291. in cg386cal we always restore regs
  292. that appear as used
  293. due to a unused tmep storage PM }
  294. else
  295. dec(countunusedregsmm);
  296. exclude(unusedregsmm,r);
  297. end;
  298. {$endif SUPPORT_MMX}
  299. for r:=R_EBX downto R_EAX do
  300. if pushed[r].pushed then
  301. begin
  302. list.concat(Taicpu.Op_reg(A_POP,S_L,r));
  303. if not (r in unusedregsint) then
  304. { internalerror(10)
  305. in cg386cal we always restore regs
  306. that appear as used
  307. due to a unused tmep storage PM }
  308. else
  309. dec(countunusedregsint);
  310. exclude(unusedregsint,r);
  311. end;
  312. {$ifdef TEMPREGDEBUG}
  313. testregisters;
  314. {$endif TEMPREGDEBUG}
  315. end;
  316. procedure trgcpu.saveusedregisters(list: taasmoutput;var saved : tpushedsaved;
  317. const s: tregisterset);
  318. begin
  319. if (aktoptprocessor in [class386,classP5]) or
  320. (CS_LittleSize in aktglobalswitches) then
  321. pushusedregisters(list,saved,s)
  322. else
  323. inherited saveusedregisters(list,saved,s);
  324. end;
  325. procedure trgcpu.restoreusedregisters(list: taasmoutput;
  326. const saved : tpushedsaved);
  327. begin
  328. if (aktoptprocessor in [class386,classP5]) or
  329. (CS_LittleSize in aktglobalswitches) then
  330. popusedregisters(list,saved)
  331. else
  332. inherited restoreusedregisters(list,saved);
  333. end;
  334. procedure trgcpu.resetusableregisters;
  335. begin
  336. inherited resetusableregisters;
  337. fpuvaroffset := 0;
  338. end;
  339. function trgcpu.correct_fpuregister(r : tregister;ofs : byte) : tregister;
  340. begin
  341. correct_fpuregister:=tregister(longint(r)+ofs);
  342. end;
  343. function trgcpu.makeregsize(reg: tregister; size: tcgsize): tregister;
  344. var
  345. _result : topsize;
  346. begin
  347. case size of
  348. OS_32,OS_S32:
  349. begin
  350. _result := S_L;
  351. end;
  352. OS_8,OS_S8:
  353. begin
  354. _result := S_B;
  355. end;
  356. OS_16,OS_S16:
  357. begin
  358. _result := S_W;
  359. end;
  360. else
  361. internalerror(2001092312);
  362. end;
  363. makeregsize := changeregsize(reg,_result);
  364. end;
  365. initialization
  366. rg := trgcpu.create;
  367. end.
  368. {
  369. $Log$
  370. Revision 1.7 2002-05-16 19:46:52 carl
  371. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  372. + try to fix temp allocation (still in ifdef)
  373. + generic constructor calls
  374. + start of tassembler / tmodulebase class cleanup
  375. Revision 1.6 2002/05/12 16:53:18 peter
  376. * moved entry and exitcode to ncgutil and cgobj
  377. * foreach gets extra argument for passing local data to the
  378. iterator function
  379. * -CR checks also class typecasts at runtime by changing them
  380. into as
  381. * fixed compiler to cycle with the -CR option
  382. * fixed stabs with elf writer, finally the global variables can
  383. be watched
  384. * removed a lot of routines from cga unit and replaced them by
  385. calls to cgobj
  386. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  387. u32bit then the other is typecasted also to u32bit without giving
  388. a rangecheck warning/error.
  389. * fixed pascal calling method with reversing also the high tree in
  390. the parast, detected by tcalcst3 test
  391. Revision 1.5 2002/04/21 15:43:32 carl
  392. * changeregsize -> rg.makeregsize
  393. * changeregsize moved from cpubase to here
  394. Revision 1.4 2002/04/15 19:44:22 peter
  395. * fixed stackcheck that would be called recursively when a stack
  396. error was found
  397. * generic changeregsize(reg,size) for i386 register resizing
  398. * removed some more routines from cga unit
  399. * fixed returnvalue handling
  400. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  401. Revision 1.3 2002/04/04 19:06:13 peter
  402. * removed unused units
  403. * use tlocation.size in cg.a_*loc*() routines
  404. Revision 1.2 2002/04/02 17:11:39 peter
  405. * tlocation,treference update
  406. * LOC_CONSTANT added for better constant handling
  407. * secondadd splitted in multiple routines
  408. * location_force_reg added for loading a location to a register
  409. of a specified size
  410. * secondassignment parses now first the right and then the left node
  411. (this is compatible with Kylix). This saves a lot of push/pop especially
  412. with string operations
  413. * adapted some routines to use the new cg methods
  414. Revision 1.1 2002/03/31 20:26:40 jonas
  415. + a_loadfpu_* and a_loadmm_* methods in tcg
  416. * register allocation is now handled by a class and is mostly processor
  417. independent (+rgobj.pas and i386/rgcpu.pas)
  418. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  419. * some small improvements and fixes to the optimizer
  420. * some register allocation fixes
  421. * some fpuvaroffset fixes in the unary minus node
  422. * push/popusedregisters is now called rg.save/restoreusedregisters and
  423. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  424. also better optimizable)
  425. * fixed and optimized register saving/restoring for new/dispose nodes
  426. * LOC_FPU locations now also require their "register" field to be set to
  427. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  428. - list field removed of the tnode class because it's not used currently
  429. and can cause hard-to-find bugs
  430. }