tgeni386.pas 14 KB

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