tgeni386.pas 18 KB

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