rgcpu.pas 16 KB

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