tgeni386.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. if usablereg32=0 then
  238. internalerror(10);
  239. dec(usablereg32);
  240. if R_EAX in unused then
  241. begin
  242. unused:=unused-[R_EAX];
  243. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  244. getregister32:=R_EAX;
  245. exprasmlist^.concat(new(pairegalloc,init(R_EAX)));
  246. end
  247. else if R_EDX in unused then
  248. begin
  249. unused:=unused-[R_EDX];
  250. usedinproc:=usedinproc or ($80 shr byte(R_EDX));
  251. getregister32:=R_EDX;
  252. exprasmlist^.concat(new(pairegalloc,init(R_EDX)));
  253. end
  254. else if R_EBX in unused then
  255. begin
  256. unused:=unused-[R_EBX];
  257. usedinproc:=usedinproc or ($80 shr byte(R_EBX));
  258. getregister32:=R_EBX;
  259. exprasmlist^.concat(new(pairegalloc,init(R_EBX)));
  260. end
  261. else if R_ECX in unused then
  262. begin
  263. unused:=unused-[R_ECX];
  264. usedinproc:=usedinproc or ($80 shr byte(R_ECX));
  265. getregister32:=R_ECX;
  266. exprasmlist^.concat(new(pairegalloc,init(R_ECX)));
  267. end
  268. else internalerror(10);
  269. end;
  270. function getexplicitregister32(r : tregister) : tregister;
  271. begin
  272. if r in unused then
  273. begin
  274. dec(usablereg32);
  275. unused:=unused-[r];
  276. usedinproc:=usedinproc or ($80 shr byte(r));
  277. getexplicitregister32:=R_ECX;
  278. exprasmlist^.concat(new(pairegalloc,init(r)));
  279. getexplicitregister32:=r;
  280. end
  281. else
  282. getexplicitregister32:=getregister32;
  283. end;
  284. procedure cleartempgen;
  285. begin
  286. unused:=usableregs;
  287. usablereg32:=c_usableregs;
  288. end;
  289. procedure clearregistercount;
  290. var
  291. regi : tregister;
  292. begin
  293. {$ifdef SUPPORT_MMX}
  294. for regi:=R_EAX to R_MM6 do
  295. begin
  296. reg_pushes[regi]:=0;
  297. is_reg_var[regi]:=false;
  298. end;
  299. {$else SUPPORT_MMX}
  300. for regi:=R_EAX to R_EDI do
  301. begin
  302. reg_pushes[regi]:=0;
  303. is_reg_var[regi]:=false;
  304. end;
  305. {$endif SUPPORT_MMX}
  306. end;
  307. procedure resetusableregisters;
  308. begin
  309. {$ifdef SUPPORT_MMX}
  310. usableregs:=[R_EAX,R_EBX,R_ECX,R_EDX,R_MM0..R_MM6];
  311. c_usableregs:=4;
  312. usableregmmx:=8;
  313. {$else}
  314. usableregs:=[R_EAX,R_EBX,R_ECX,R_EDX];
  315. c_usableregs:=4;
  316. {$endif SUPPORT_MMX}
  317. end;
  318. begin
  319. resetusableregisters;
  320. end.
  321. {
  322. $Log$
  323. Revision 1.18 1999-01-18 16:02:20 pierre
  324. * better error info with -Co
  325. Revision 1.17 1998/12/11 23:36:09 florian
  326. + again more stuff for int64/qword:
  327. - comparision operators
  328. - code generation for: str, read(ln), write(ln)
  329. Revision 1.16 1998/12/11 17:22:40 florian
  330. * fixed previous commit bug fix of getexplicitregister32
  331. (usableregs32 was decremented twice, thnaks Pierre for that hint)
  332. Revision 1.15 1998/12/11 16:10:13 florian
  333. + shifting for 64 bit ints added
  334. * bug in getexplicitregister32 fixed: usableregs wasn't decremented !!
  335. Revision 1.14 1998/12/11 00:03:59 peter
  336. + globtype,tokens,version unit splitted from globals
  337. Revision 1.13 1998/10/21 08:40:03 florian
  338. + ansistring operator +
  339. + $h and string[n] for n>255 added
  340. * small problem with TP fixed
  341. Revision 1.12 1998/09/20 17:11:24 jonas
  342. * released REGALLOC
  343. Revision 1.11 1998/09/16 17:58:33 jonas
  344. * fixed -dRegAlloc and -dDRegalloc problems
  345. Revision 1.10 1998/09/01 09:03:47 peter
  346. + resetregistercount, resetusableregisters
  347. Revision 1.9 1998/08/19 16:07:56 jonas
  348. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  349. Revision 1.8 1998/08/10 14:50:34 peter
  350. + localswitches, moduleswitches, globalswitches splitting
  351. Revision 1.7 1998/06/08 13:13:47 pierre
  352. + temporary variables now in temp_gen.pas unit
  353. because it is processor independent
  354. * mppc68k.bat modified to undefine i386 and support_mmx
  355. (which are defaults for i386)
  356. Revision 1.6 1998/05/20 09:42:38 pierre
  357. + UseTokenInfo now default
  358. * unit in interface uses and implementation uses gives error now
  359. * only one error for unknown symbol (uses lastsymknown boolean)
  360. the problem came from the label code !
  361. + first inlined procedures and function work
  362. (warning there might be allowed cases were the result is still wrong !!)
  363. * UseBrower updated gives a global list of all position of all used symbols
  364. with switch -gb
  365. Revision 1.5 1998/05/11 13:07:58 peter
  366. + $ifdef NEWPPU for the new ppuformat
  367. + $define GDB not longer required
  368. * removed all warnings and stripped some log comments
  369. * no findfirst/findnext anymore to remove smartlink *.o files
  370. Revision 1.4 1998/04/29 10:34:08 pierre
  371. + added some code for ansistring (not complete nor working yet)
  372. * corrected operator overloading
  373. * corrected nasm output
  374. + started inline procedures
  375. + added starstarn : use ** for exponentiation (^ gave problems)
  376. + started UseTokenInfo cond to get accurate positions
  377. Revision 1.3 1998/04/09 22:16:36 florian
  378. * problem with previous REGALLOC solved
  379. * improved property support
  380. Revision 1.2 1998/04/09 15:46:39 florian
  381. + register allocation tracing stuff added
  382. }