tgeni386.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. {$ifdef SUPPORT_MMX}
  37. function getregistermmx : tregister;
  38. procedure ungetregistermmx(r : tregister);
  39. {$endif SUPPORT_MMX}
  40. procedure ungetregister(r : tregister);
  41. procedure cleartempgen;
  42. procedure del_reference(const ref : treference);
  43. procedure del_locref(const location : tlocation);
  44. { pushs and restores registers }
  45. procedure pushusedregisters(var pushed : tpushed;b : byte);
  46. procedure popusedregisters(const pushed : tpushed);
  47. var
  48. unused,usableregs : tregisterset;
  49. c_usableregs : longint;
  50. { uses only 1 byte while a set uses in FPC 32 bytes }
  51. usedinproc : byte;
  52. { count, how much a register must be pushed if it is used as register }
  53. { variable }
  54. {$ifdef SUPPORT_MMX}
  55. reg_pushes : array[R_EAX..R_MM6] of longint;
  56. is_reg_var : array[R_EAX..R_MM6] of boolean;
  57. {$else SUPPORT_MMX}
  58. reg_pushes : array[R_EAX..R_EDI] of longint;
  59. is_reg_var : array[R_EAX..R_EDI] of boolean;
  60. {$endif SUPPORT_MMX}
  61. implementation
  62. procedure pushusedregisters(var pushed : tpushed;b : byte);
  63. var
  64. r : tregister;
  65. {$ifdef SUPPORT_MMX}
  66. hr : preference;
  67. {$endif SUPPORT_MMX}
  68. begin
  69. usedinproc:=usedinproc or b;
  70. for r:=R_EAX to R_EBX do
  71. begin
  72. pushed[r]:=false;
  73. { if the register is used by the calling subroutine }
  74. if ((b and ($80 shr byte(r)))<>0) then
  75. begin
  76. { and is present in use }
  77. if not(r in unused) then
  78. begin
  79. { then save it }
  80. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,r)));
  81. { here was a big problem !!!!!}
  82. { you cannot do that for a register that is
  83. globally assigned to a var
  84. this also means that you must push it much more
  85. often, but there must be a better way
  86. maybe by putting the value back to the stack !! }
  87. if not(is_reg_var[r]) then
  88. unused:=unused+[r];
  89. pushed[r]:=true;
  90. end;
  91. end;
  92. end;
  93. {$ifdef SUPPORT_MMX}
  94. for r:=R_MM0 to R_MM6 do
  95. begin
  96. pushed[r]:=false;
  97. { if the mmx register is in use, save it }
  98. if not(r in unused) then
  99. begin
  100. exprasmlist^.concat(new(pai386,op_const_reg(
  101. A_SUB,S_L,8,R_ESP)));
  102. new(hr);
  103. reset_reference(hr^);
  104. hr^.base:=R_ESP;
  105. exprasmlist^.concat(new(pai386,op_reg_ref(
  106. A_MOVQ,S_NO,r,hr)));
  107. if not(is_reg_var[r]) then
  108. unused:=unused+[r];
  109. pushed[r]:=true;
  110. end;
  111. end;
  112. {$endif SUPPORT_MMX}
  113. end;
  114. procedure popusedregisters(const pushed : tpushed);
  115. var
  116. r : tregister;
  117. {$ifdef SUPPORT_MMX}
  118. hr : preference;
  119. {$endif SUPPORT_MMX}
  120. begin
  121. { restore in reverse order: }
  122. {$ifdef SUPPORT_MMX}
  123. for r:=R_MM6 downto R_MM0 do
  124. begin
  125. if pushed[r] then
  126. begin
  127. new(hr);
  128. reset_reference(hr^);
  129. hr^.base:=R_ESP;
  130. exprasmlist^.concat(new(pai386,op_ref_reg(
  131. A_MOVQ,S_NO,hr,r)));
  132. exprasmlist^.concat(new(pai386,op_const_reg(
  133. A_ADD,S_L,8,R_ESP)));
  134. unused:=unused-[r];
  135. end;
  136. end;
  137. {$endif SUPPORT_MMX}
  138. for r:=R_EBX downto R_EAX do
  139. if pushed[r] then
  140. begin
  141. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,r)));
  142. unused:=unused-[r];
  143. end;
  144. end;
  145. procedure ungetregister(r : tregister);
  146. begin
  147. if r in [R_EAX,R_ECX,R_EDX,R_EBX,R_ESP,R_EBP,R_ESI,R_EDI] then
  148. ungetregister32(r)
  149. else if r in [R_AX,R_CX,R_DX,R_BX,R_SP,R_BP,R_SI,R_DI] then
  150. ungetregister32(reg16toreg32(r))
  151. else if r in [R_AL,R_BL,R_CL,R_DL] then
  152. ungetregister32(reg8toreg32(r))
  153. {$ifdef SUPPORT_MMX}
  154. else if r in [R_MM0..R_MM6] then
  155. ungetregistermmx(r)
  156. {$endif SUPPORT_MMX}
  157. else internalerror(18);
  158. end;
  159. procedure ungetregister32(r : tregister);
  160. begin
  161. if cs_maxoptimize in aktglobalswitches then
  162. begin
  163. { takes much time }
  164. if not(r in usableregs) then
  165. exit;
  166. unused:=unused+[r];
  167. inc(usablereg32);
  168. end
  169. else
  170. begin
  171. if not(r in [R_EAX,R_EBX,R_ECX,R_EDX]) then
  172. exit;
  173. unused:=unused+[r];
  174. inc(usablereg32);
  175. end;
  176. {$ifdef REGALLOC}
  177. exprasmlist^.concat(new(pairegdealloc,init(r)));
  178. {$endif REGALLOC}
  179. end;
  180. {$ifdef SUPPORT_MMX}
  181. function getregistermmx : tregister;
  182. var
  183. r : tregister;
  184. begin
  185. dec(usableregmmx);
  186. for r:=R_MM0 to R_MM6 do
  187. if r in unused then
  188. begin
  189. unused:=unused-[r];
  190. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  191. getregistermmx:=r;
  192. exit;
  193. end;
  194. internalerror(10);
  195. end;
  196. procedure ungetregistermmx(r : tregister);
  197. begin
  198. if cs_maxoptimize in aktglobalswitches then
  199. begin
  200. { takes much time }
  201. if not(r in usableregs) then
  202. exit;
  203. unused:=unused+[r];
  204. inc(usableregmmx);
  205. end
  206. else
  207. begin
  208. unused:=unused+[r];
  209. inc(usableregmmx);
  210. end;
  211. end;
  212. {$endif SUPPORT_MMX}
  213. procedure del_reference(const ref : treference);
  214. begin
  215. if ref.isintvalue then
  216. exit;
  217. ungetregister32(ref.base);
  218. ungetregister32(ref.index);
  219. { ref.segment:=R_DEFAULT_SEG; }
  220. end;
  221. procedure del_locref(const location : tlocation);
  222. begin
  223. if (location.loc<>loc_mem) and (location.loc<>loc_reference) then
  224. exit;
  225. if location.reference.isintvalue then
  226. exit;
  227. ungetregister32(location.reference.base);
  228. ungetregister32(location.reference.index);
  229. { ref.segment:=R_DEFAULT_SEG; }
  230. end;
  231. function getregister32 : tregister;
  232. begin
  233. dec(usablereg32);
  234. if R_EAX in unused then
  235. begin
  236. unused:=unused-[R_EAX];
  237. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  238. getregister32:=R_EAX;
  239. end
  240. else if R_EDX in unused then
  241. begin
  242. unused:=unused-[R_EDX];
  243. usedinproc:=usedinproc or ($80 shr byte(R_EDX));
  244. getregister32:=R_EDX;
  245. end
  246. else if R_EBX in unused then
  247. begin
  248. unused:=unused-[R_EBX];
  249. usedinproc:=usedinproc or ($80 shr byte(R_EBX));
  250. getregister32:=R_EBX;
  251. end
  252. else if R_ECX in unused then
  253. begin
  254. unused:=unused-[R_ECX];
  255. usedinproc:=usedinproc or ($80 shr byte(R_ECX));
  256. getregister32:=R_ECX;
  257. end
  258. else internalerror(10);
  259. end;
  260. procedure cleartempgen;
  261. begin
  262. unused:=usableregs;
  263. usablereg32:=c_usableregs;
  264. end;
  265. begin
  266. usableregs:=[R_EAX,R_EBX,R_ECX,R_EDX];
  267. {$ifdef SUPPORT_MMX}
  268. usableregs:=usableregs+[R_MM0..R_MM6];
  269. {$endif SUPPORT_MMX}
  270. c_usableregs:=4;
  271. end.
  272. {
  273. $Log$
  274. Revision 1.8 1998-08-10 14:50:34 peter
  275. + localswitches, moduleswitches, globalswitches splitting
  276. Revision 1.7 1998/06/08 13:13:47 pierre
  277. + temporary variables now in temp_gen.pas unit
  278. because it is processor independent
  279. * mppc68k.bat modified to undefine i386 and support_mmx
  280. (which are defaults for i386)
  281. Revision 1.6 1998/05/20 09:42:38 pierre
  282. + UseTokenInfo now default
  283. * unit in interface uses and implementation uses gives error now
  284. * only one error for unknown symbol (uses lastsymknown boolean)
  285. the problem came from the label code !
  286. + first inlined procedures and function work
  287. (warning there might be allowed cases were the result is still wrong !!)
  288. * UseBrower updated gives a global list of all position of all used symbols
  289. with switch -gb
  290. Revision 1.5 1998/05/11 13:07:58 peter
  291. + $ifdef NEWPPU for the new ppuformat
  292. + $define GDB not longer required
  293. * removed all warnings and stripped some log comments
  294. * no findfirst/findnext anymore to remove smartlink *.o files
  295. Revision 1.4 1998/04/29 10:34:08 pierre
  296. + added some code for ansistring (not complete nor working yet)
  297. * corrected operator overloading
  298. * corrected nasm output
  299. + started inline procedures
  300. + added starstarn : use ** for exponentiation (^ gave problems)
  301. + started UseTokenInfo cond to get accurate positions
  302. Revision 1.3 1998/04/09 22:16:36 florian
  303. * problem with previous REGALLOC solved
  304. * improved property support
  305. Revision 1.2 1998/04/09 15:46:39 florian
  306. + register allocation tracing stuff added
  307. }