tgeni386.pas 18 KB

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