tgeni386.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. uses
  67. globtype;
  68. procedure pushusedregisters(var pushed : tpushed;b : byte);
  69. var
  70. r : tregister;
  71. {$ifdef SUPPORT_MMX}
  72. hr : preference;
  73. {$endif SUPPORT_MMX}
  74. begin
  75. usedinproc:=usedinproc or b;
  76. for r:=R_EAX to R_EBX do
  77. begin
  78. pushed[r]:=false;
  79. { if the register is used by the calling subroutine }
  80. if ((b and ($80 shr byte(r)))<>0) then
  81. begin
  82. { and is present in use }
  83. if not(r in unused) then
  84. begin
  85. { then save it }
  86. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,r)));
  87. { here was a big problem !!!!!}
  88. { you cannot do that for a register that is
  89. globally assigned to a var
  90. this also means that you must push it much more
  91. often, but there must be a better way
  92. maybe by putting the value back to the stack !! }
  93. if not(is_reg_var[r]) then
  94. unused:=unused+[r];
  95. pushed[r]:=true;
  96. end;
  97. end;
  98. end;
  99. {$ifdef SUPPORT_MMX}
  100. for r:=R_MM0 to R_MM6 do
  101. begin
  102. pushed[r]:=false;
  103. { if the mmx register is in use, save it }
  104. if not(r in unused) then
  105. begin
  106. exprasmlist^.concat(new(pai386,op_const_reg(
  107. A_SUB,S_L,8,R_ESP)));
  108. new(hr);
  109. reset_reference(hr^);
  110. hr^.base:=R_ESP;
  111. exprasmlist^.concat(new(pai386,op_reg_ref(
  112. A_MOVQ,S_NO,r,hr)));
  113. if not(is_reg_var[r]) then
  114. unused:=unused+[r];
  115. pushed[r]:=true;
  116. end;
  117. end;
  118. {$endif SUPPORT_MMX}
  119. end;
  120. procedure popusedregisters(const pushed : tpushed);
  121. var
  122. r : tregister;
  123. {$ifdef SUPPORT_MMX}
  124. hr : preference;
  125. {$endif SUPPORT_MMX}
  126. begin
  127. { restore in reverse order: }
  128. {$ifdef SUPPORT_MMX}
  129. for r:=R_MM6 downto R_MM0 do
  130. begin
  131. if pushed[r] then
  132. begin
  133. new(hr);
  134. reset_reference(hr^);
  135. hr^.base:=R_ESP;
  136. exprasmlist^.concat(new(pai386,op_ref_reg(
  137. A_MOVQ,S_NO,hr,r)));
  138. exprasmlist^.concat(new(pai386,op_const_reg(
  139. A_ADD,S_L,8,R_ESP)));
  140. unused:=unused-[r];
  141. end;
  142. end;
  143. {$endif SUPPORT_MMX}
  144. for r:=R_EBX downto R_EAX do
  145. if pushed[r] then
  146. begin
  147. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,r)));
  148. unused:=unused-[r];
  149. end;
  150. end;
  151. procedure ungetregister(r : tregister);
  152. begin
  153. if r in [R_EAX,R_ECX,R_EDX,R_EBX,R_ESP,R_EBP,R_ESI,R_EDI] then
  154. ungetregister32(r)
  155. else if r in [R_AX,R_CX,R_DX,R_BX,R_SP,R_BP,R_SI,R_DI] then
  156. ungetregister32(reg16toreg32(r))
  157. else if r in [R_AL,R_BL,R_CL,R_DL] then
  158. ungetregister32(reg8toreg32(r))
  159. {$ifdef SUPPORT_MMX}
  160. else if r in [R_MM0..R_MM6] then
  161. ungetregistermmx(r)
  162. {$endif SUPPORT_MMX}
  163. else internalerror(18);
  164. end;
  165. procedure ungetregister32(r : tregister);
  166. begin
  167. if cs_regalloc in aktglobalswitches then
  168. begin
  169. { takes much time }
  170. if not(r in usableregs) then
  171. exit;
  172. unused:=unused+[r];
  173. inc(usablereg32);
  174. end
  175. else
  176. begin
  177. if not(r in [R_EAX,R_EBX,R_ECX,R_EDX]) then
  178. exit;
  179. unused:=unused+[r];
  180. inc(usablereg32);
  181. end;
  182. exprasmlist^.concat(new(pairegdealloc,init(r)));
  183. end;
  184. {$ifdef SUPPORT_MMX}
  185. function getregistermmx : tregister;
  186. var
  187. r : tregister;
  188. begin
  189. dec(usableregmmx);
  190. for r:=R_MM0 to R_MM6 do
  191. if r in unused then
  192. begin
  193. unused:=unused-[r];
  194. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  195. getregistermmx:=r;
  196. exit;
  197. end;
  198. internalerror(10);
  199. end;
  200. procedure ungetregistermmx(r : tregister);
  201. begin
  202. if cs_regalloc in aktglobalswitches then
  203. begin
  204. { takes much time }
  205. if not(r in usableregs) then
  206. exit;
  207. unused:=unused+[r];
  208. inc(usableregmmx);
  209. end
  210. else
  211. begin
  212. unused:=unused+[r];
  213. inc(usableregmmx);
  214. end;
  215. end;
  216. {$endif SUPPORT_MMX}
  217. procedure del_reference(const ref : treference);
  218. begin
  219. if ref.isintvalue then
  220. exit;
  221. ungetregister32(ref.base);
  222. ungetregister32(ref.index);
  223. { ref.segment:=R_DEFAULT_SEG; }
  224. end;
  225. procedure del_locref(const location : tlocation);
  226. begin
  227. if (location.loc<>loc_mem) and (location.loc<>loc_reference) then
  228. exit;
  229. if location.reference.isintvalue then
  230. exit;
  231. ungetregister32(location.reference.base);
  232. ungetregister32(location.reference.index);
  233. { ref.segment:=R_DEFAULT_SEG; }
  234. end;
  235. function getregister32 : tregister;
  236. begin
  237. dec(usablereg32);
  238. if R_EAX in unused then
  239. begin
  240. unused:=unused-[R_EAX];
  241. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  242. getregister32:=R_EAX;
  243. exprasmlist^.concat(new(pairegalloc,init(R_EAX)));
  244. end
  245. else if R_EDX in unused then
  246. begin
  247. unused:=unused-[R_EDX];
  248. usedinproc:=usedinproc or ($80 shr byte(R_EDX));
  249. getregister32:=R_EDX;
  250. exprasmlist^.concat(new(pairegalloc,init(R_EDX)));
  251. end
  252. else if R_EBX in unused then
  253. begin
  254. unused:=unused-[R_EBX];
  255. usedinproc:=usedinproc or ($80 shr byte(R_EBX));
  256. getregister32:=R_EBX;
  257. exprasmlist^.concat(new(pairegalloc,init(R_EBX)));
  258. end
  259. else if R_ECX in unused then
  260. begin
  261. unused:=unused-[R_ECX];
  262. usedinproc:=usedinproc or ($80 shr byte(R_ECX));
  263. getregister32:=R_ECX;
  264. exprasmlist^.concat(new(pairegalloc,init(R_ECX)));
  265. end
  266. else internalerror(10);
  267. end;
  268. function getexplicitregister32(r : tregister) : tregister;
  269. begin
  270. if r in unused then
  271. begin
  272. dec(usablereg32);
  273. unused:=unused-[r];
  274. usedinproc:=usedinproc or ($80 shr byte(r));
  275. getexplicitregister32:=R_ECX;
  276. exprasmlist^.concat(new(pairegalloc,init(r)));
  277. getexplicitregister32:=r;
  278. end
  279. else
  280. getexplicitregister32:=getregister32;
  281. end;
  282. procedure cleartempgen;
  283. begin
  284. unused:=usableregs;
  285. usablereg32:=c_usableregs;
  286. end;
  287. procedure clearregistercount;
  288. var
  289. regi : tregister;
  290. begin
  291. {$ifdef SUPPORT_MMX}
  292. for regi:=R_EAX to R_MM6 do
  293. begin
  294. reg_pushes[regi]:=0;
  295. is_reg_var[regi]:=false;
  296. end;
  297. {$else SUPPORT_MMX}
  298. for regi:=R_EAX to R_EDI do
  299. begin
  300. reg_pushes[regi]:=0;
  301. is_reg_var[regi]:=false;
  302. end;
  303. {$endif SUPPORT_MMX}
  304. end;
  305. procedure resetusableregisters;
  306. begin
  307. {$ifdef SUPPORT_MMX}
  308. usableregs:=[R_EAX,R_EBX,R_ECX,R_EDX,R_MM0..R_MM6];
  309. c_usableregs:=4;
  310. usableregmmx:=8;
  311. {$else}
  312. usableregs:=[R_EAX,R_EBX,R_ECX,R_EDX];
  313. c_usableregs:=4;
  314. {$endif SUPPORT_MMX}
  315. end;
  316. begin
  317. resetusableregisters;
  318. end.
  319. {
  320. $Log$
  321. Revision 1.17 1998-12-11 23:36:09 florian
  322. + again more stuff for int64/qword:
  323. - comparision operators
  324. - code generation for: str, read(ln), write(ln)
  325. Revision 1.16 1998/12/11 17:22:40 florian
  326. * fixed previous commit bug fix of getexplicitregister32
  327. (usableregs32 was decremented twice, thnaks Pierre for that hint)
  328. Revision 1.15 1998/12/11 16:10:13 florian
  329. + shifting for 64 bit ints added
  330. * bug in getexplicitregister32 fixed: usableregs wasn't decremented !!
  331. Revision 1.14 1998/12/11 00:03:59 peter
  332. + globtype,tokens,version unit splitted from globals
  333. Revision 1.13 1998/10/21 08:40:03 florian
  334. + ansistring operator +
  335. + $h and string[n] for n>255 added
  336. * small problem with TP fixed
  337. Revision 1.12 1998/09/20 17:11:24 jonas
  338. * released REGALLOC
  339. Revision 1.11 1998/09/16 17:58:33 jonas
  340. * fixed -dRegAlloc and -dDRegalloc problems
  341. Revision 1.10 1998/09/01 09:03:47 peter
  342. + resetregistercount, resetusableregisters
  343. Revision 1.9 1998/08/19 16:07:56 jonas
  344. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  345. Revision 1.8 1998/08/10 14:50:34 peter
  346. + localswitches, moduleswitches, globalswitches splitting
  347. Revision 1.7 1998/06/08 13:13:47 pierre
  348. + temporary variables now in temp_gen.pas unit
  349. because it is processor independent
  350. * mppc68k.bat modified to undefine i386 and support_mmx
  351. (which are defaults for i386)
  352. Revision 1.6 1998/05/20 09:42:38 pierre
  353. + UseTokenInfo now default
  354. * unit in interface uses and implementation uses gives error now
  355. * only one error for unknown symbol (uses lastsymknown boolean)
  356. the problem came from the label code !
  357. + first inlined procedures and function work
  358. (warning there might be allowed cases were the result is still wrong !!)
  359. * UseBrower updated gives a global list of all position of all used symbols
  360. with switch -gb
  361. Revision 1.5 1998/05/11 13:07:58 peter
  362. + $ifdef NEWPPU for the new ppuformat
  363. + $define GDB not longer required
  364. * removed all warnings and stripped some log comments
  365. * no findfirst/findnext anymore to remove smartlink *.o files
  366. Revision 1.4 1998/04/29 10:34:08 pierre
  367. + added some code for ansistring (not complete nor working yet)
  368. * corrected operator overloading
  369. * corrected nasm output
  370. + started inline procedures
  371. + added starstarn : use ** for exponentiation (^ gave problems)
  372. + started UseTokenInfo cond to get accurate positions
  373. Revision 1.3 1998/04/09 22:16:36 florian
  374. * problem with previous REGALLOC solved
  375. * improved property support
  376. Revision 1.2 1998/04/09 15:46:39 florian
  377. + register allocation tracing stuff added
  378. }