tgeni386.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. {
  2. $Id$
  3. Copyright (C) 1993-98 by Florian Klaempfl
  4. This unit handles the temporary variables stuff for i386
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit tgeni386;
  19. interface
  20. uses
  21. cobjects,globals,tree,hcodegen,verbose,files,aasm
  22. {$ifdef i386}
  23. ,i386
  24. {$endif}
  25. ;
  26. type
  27. tregisterset = set of tregister;
  28. tpushed = array[R_EAX..R_MM6] of boolean;
  29. const
  30. usablereg32 : byte = 4;
  31. {$ifdef SUPPORT_MMX}
  32. usableregmmx : byte = 8;
  33. {$endif SUPPORT_MMX}
  34. function getregister32 : tregister;
  35. procedure ungetregister32(r : tregister);
  36. { tries to allocate the passed register, if possible }
  37. function getexplicitregister32(r : tregister) : tregister;
  38. {$ifdef SUPPORT_MMX}
  39. function getregistermmx : tregister;
  40. procedure ungetregistermmx(r : tregister);
  41. {$endif SUPPORT_MMX}
  42. procedure ungetregister(r : tregister);
  43. procedure cleartempgen;
  44. procedure del_reference(const ref : treference);
  45. procedure del_locref(const location : tlocation);
  46. { pushs and restores registers }
  47. procedure pushusedregisters(var pushed : tpushed;b : byte);
  48. procedure popusedregisters(const pushed : tpushed);
  49. procedure clearregistercount;
  50. procedure resetusableregisters;
  51. var
  52. unused,usableregs : tregisterset;
  53. c_usableregs : longint;
  54. { uses only 1 byte while a set uses in FPC 32 bytes }
  55. usedinproc : byte;
  56. { count, how much a register must be pushed if it is used as register }
  57. { variable }
  58. {$ifdef SUPPORT_MMX}
  59. reg_pushes : array[R_EAX..R_MM6] of longint;
  60. is_reg_var : array[R_EAX..R_MM6] of boolean;
  61. {$else SUPPORT_MMX}
  62. reg_pushes : array[R_EAX..R_EDI] of longint;
  63. is_reg_var : array[R_EAX..R_EDI] of boolean;
  64. {$endif SUPPORT_MMX}
  65. implementation
  66. procedure pushusedregisters(var pushed : tpushed;b : byte);
  67. var
  68. r : tregister;
  69. {$ifdef SUPPORT_MMX}
  70. hr : preference;
  71. {$endif SUPPORT_MMX}
  72. begin
  73. usedinproc:=usedinproc or b;
  74. for r:=R_EAX to R_EBX do
  75. begin
  76. pushed[r]:=false;
  77. { if the register is used by the calling subroutine }
  78. if ((b and ($80 shr byte(r)))<>0) then
  79. begin
  80. { and is present in use }
  81. if not(r in unused) then
  82. begin
  83. { then save it }
  84. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,r)));
  85. { here was a big problem !!!!!}
  86. { you cannot do that for a register that is
  87. globally assigned to a var
  88. this also means that you must push it much more
  89. often, but there must be a better way
  90. maybe by putting the value back to the stack !! }
  91. if not(is_reg_var[r]) then
  92. unused:=unused+[r];
  93. pushed[r]:=true;
  94. end;
  95. end;
  96. end;
  97. {$ifdef SUPPORT_MMX}
  98. for r:=R_MM0 to R_MM6 do
  99. begin
  100. pushed[r]:=false;
  101. { if the mmx register is in use, save it }
  102. if not(r in unused) then
  103. begin
  104. exprasmlist^.concat(new(pai386,op_const_reg(
  105. A_SUB,S_L,8,R_ESP)));
  106. new(hr);
  107. reset_reference(hr^);
  108. hr^.base:=R_ESP;
  109. exprasmlist^.concat(new(pai386,op_reg_ref(
  110. A_MOVQ,S_NO,r,hr)));
  111. if not(is_reg_var[r]) then
  112. unused:=unused+[r];
  113. pushed[r]:=true;
  114. end;
  115. end;
  116. {$endif SUPPORT_MMX}
  117. end;
  118. procedure popusedregisters(const pushed : tpushed);
  119. var
  120. r : tregister;
  121. {$ifdef SUPPORT_MMX}
  122. hr : preference;
  123. {$endif SUPPORT_MMX}
  124. begin
  125. { restore in reverse order: }
  126. {$ifdef SUPPORT_MMX}
  127. for r:=R_MM6 downto R_MM0 do
  128. begin
  129. if pushed[r] then
  130. begin
  131. new(hr);
  132. reset_reference(hr^);
  133. hr^.base:=R_ESP;
  134. exprasmlist^.concat(new(pai386,op_ref_reg(
  135. A_MOVQ,S_NO,hr,r)));
  136. exprasmlist^.concat(new(pai386,op_const_reg(
  137. A_ADD,S_L,8,R_ESP)));
  138. unused:=unused-[r];
  139. end;
  140. end;
  141. {$endif SUPPORT_MMX}
  142. for r:=R_EBX downto R_EAX do
  143. if pushed[r] then
  144. begin
  145. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,r)));
  146. unused:=unused-[r];
  147. end;
  148. end;
  149. procedure ungetregister(r : tregister);
  150. begin
  151. if r in [R_EAX,R_ECX,R_EDX,R_EBX,R_ESP,R_EBP,R_ESI,R_EDI] then
  152. ungetregister32(r)
  153. else if r in [R_AX,R_CX,R_DX,R_BX,R_SP,R_BP,R_SI,R_DI] then
  154. ungetregister32(reg16toreg32(r))
  155. else if r in [R_AL,R_BL,R_CL,R_DL] then
  156. ungetregister32(reg8toreg32(r))
  157. {$ifdef SUPPORT_MMX}
  158. else if r in [R_MM0..R_MM6] then
  159. ungetregistermmx(r)
  160. {$endif SUPPORT_MMX}
  161. else internalerror(18);
  162. end;
  163. procedure ungetregister32(r : tregister);
  164. begin
  165. if cs_regalloc in aktglobalswitches then
  166. begin
  167. { takes much time }
  168. if not(r in usableregs) then
  169. exit;
  170. unused:=unused+[r];
  171. inc(usablereg32);
  172. end
  173. else
  174. begin
  175. if not(r in [R_EAX,R_EBX,R_ECX,R_EDX]) then
  176. exit;
  177. unused:=unused+[r];
  178. inc(usablereg32);
  179. end;
  180. exprasmlist^.concat(new(pairegdealloc,init(r)));
  181. end;
  182. {$ifdef SUPPORT_MMX}
  183. function getregistermmx : tregister;
  184. var
  185. r : tregister;
  186. begin
  187. dec(usableregmmx);
  188. for r:=R_MM0 to R_MM6 do
  189. if r in unused then
  190. begin
  191. unused:=unused-[r];
  192. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  193. getregistermmx:=r;
  194. exit;
  195. end;
  196. internalerror(10);
  197. end;
  198. procedure ungetregistermmx(r : tregister);
  199. begin
  200. if cs_regalloc in aktglobalswitches then
  201. begin
  202. { takes much time }
  203. if not(r in usableregs) then
  204. exit;
  205. unused:=unused+[r];
  206. inc(usableregmmx);
  207. end
  208. else
  209. begin
  210. unused:=unused+[r];
  211. inc(usableregmmx);
  212. end;
  213. end;
  214. {$endif SUPPORT_MMX}
  215. procedure del_reference(const ref : treference);
  216. begin
  217. if ref.isintvalue then
  218. exit;
  219. ungetregister32(ref.base);
  220. ungetregister32(ref.index);
  221. { ref.segment:=R_DEFAULT_SEG; }
  222. end;
  223. procedure del_locref(const location : tlocation);
  224. begin
  225. if (location.loc<>loc_mem) and (location.loc<>loc_reference) then
  226. exit;
  227. if location.reference.isintvalue then
  228. exit;
  229. ungetregister32(location.reference.base);
  230. ungetregister32(location.reference.index);
  231. { ref.segment:=R_DEFAULT_SEG; }
  232. end;
  233. function getregister32 : tregister;
  234. begin
  235. dec(usablereg32);
  236. if R_EAX in unused then
  237. begin
  238. unused:=unused-[R_EAX];
  239. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  240. getregister32:=R_EAX;
  241. exprasmlist^.concat(new(pairegalloc,init(R_EAX)));
  242. end
  243. else if R_EDX in unused then
  244. begin
  245. unused:=unused-[R_EDX];
  246. usedinproc:=usedinproc or ($80 shr byte(R_EDX));
  247. getregister32:=R_EDX;
  248. exprasmlist^.concat(new(pairegalloc,init(R_EDX)));
  249. end
  250. else if R_EBX in unused then
  251. begin
  252. unused:=unused-[R_EBX];
  253. usedinproc:=usedinproc or ($80 shr byte(R_EBX));
  254. getregister32:=R_EBX;
  255. exprasmlist^.concat(new(pairegalloc,init(R_EBX)));
  256. end
  257. else if R_ECX in unused then
  258. begin
  259. unused:=unused-[R_ECX];
  260. usedinproc:=usedinproc or ($80 shr byte(R_ECX));
  261. getregister32:=R_ECX;
  262. exprasmlist^.concat(new(pairegalloc,init(R_ECX)));
  263. end
  264. else internalerror(10);
  265. end;
  266. function getexplicitregister32(r : tregister) : tregister;
  267. begin
  268. if r in unused then
  269. begin
  270. unused:=unused-[r];
  271. usedinproc:=usedinproc or ($80 shr byte(r));
  272. getexplicitregister32:=R_ECX;
  273. exprasmlist^.concat(new(pairegalloc,init(r)));
  274. getexplicitregister32:=r;
  275. end
  276. else
  277. getexplicitregister32:=getregister32;
  278. end;
  279. procedure cleartempgen;
  280. begin
  281. unused:=usableregs;
  282. usablereg32:=c_usableregs;
  283. end;
  284. procedure clearregistercount;
  285. var
  286. regi : tregister;
  287. begin
  288. {$ifdef SUPPORT_MMX}
  289. for regi:=R_EAX to R_MM6 do
  290. begin
  291. reg_pushes[regi]:=0;
  292. is_reg_var[regi]:=false;
  293. end;
  294. {$else SUPPORT_MMX}
  295. for regi:=R_EAX to R_EDI do
  296. begin
  297. reg_pushes[regi]:=0;
  298. is_reg_var[regi]:=false;
  299. end;
  300. {$endif SUPPORT_MMX}
  301. end;
  302. procedure resetusableregisters;
  303. begin
  304. {$ifdef SUPPORT_MMX}
  305. usableregs:=[R_EAX,R_EBX,R_ECX,R_EDX,R_MM0..R_MM6];
  306. c_usableregs:=4;
  307. usableregmmx:=8;
  308. {$else}
  309. usableregs:=[R_EAX,R_EBX,R_ECX,R_EDX];
  310. c_usableregs:=4;
  311. {$endif SUPPORT_MMX}
  312. end;
  313. begin
  314. resetusableregisters;
  315. end.
  316. {
  317. $Log$
  318. Revision 1.13 1998-10-21 08:40:03 florian
  319. + ansistring operator +
  320. + $h and string[n] for n>255 added
  321. * small problem with TP fixed
  322. Revision 1.12 1998/09/20 17:11:24 jonas
  323. * released REGALLOC
  324. Revision 1.11 1998/09/16 17:58:33 jonas
  325. * fixed -dRegAlloc and -dDRegalloc problems
  326. Revision 1.10 1998/09/01 09:03:47 peter
  327. + resetregistercount, resetusableregisters
  328. Revision 1.9 1998/08/19 16:07:56 jonas
  329. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  330. Revision 1.8 1998/08/10 14:50:34 peter
  331. + localswitches, moduleswitches, globalswitches splitting
  332. Revision 1.7 1998/06/08 13:13:47 pierre
  333. + temporary variables now in temp_gen.pas unit
  334. because it is processor independent
  335. * mppc68k.bat modified to undefine i386 and support_mmx
  336. (which are defaults for i386)
  337. Revision 1.6 1998/05/20 09:42:38 pierre
  338. + UseTokenInfo now default
  339. * unit in interface uses and implementation uses gives error now
  340. * only one error for unknown symbol (uses lastsymknown boolean)
  341. the problem came from the label code !
  342. + first inlined procedures and function work
  343. (warning there might be allowed cases were the result is still wrong !!)
  344. * UseBrower updated gives a global list of all position of all used symbols
  345. with switch -gb
  346. Revision 1.5 1998/05/11 13:07:58 peter
  347. + $ifdef NEWPPU for the new ppuformat
  348. + $define GDB not longer required
  349. * removed all warnings and stripped some log comments
  350. * no findfirst/findnext anymore to remove smartlink *.o files
  351. Revision 1.4 1998/04/29 10:34:08 pierre
  352. + added some code for ansistring (not complete nor working yet)
  353. * corrected operator overloading
  354. * corrected nasm output
  355. + started inline procedures
  356. + added starstarn : use ** for exponentiation (^ gave problems)
  357. + started UseTokenInfo cond to get accurate positions
  358. Revision 1.3 1998/04/09 22:16:36 florian
  359. * problem with previous REGALLOC solved
  360. * improved property support
  361. Revision 1.2 1998/04/09 15:46:39 florian
  362. + register allocation tracing stuff added
  363. }