tgeni386.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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. { generates temporary variables }
  43. procedure resettempgen;
  44. procedure setfirsttemp(l : longint);
  45. function gettempsize : longint;
  46. function gettempofsize(size : longint) : longint;
  47. procedure ungettemp(pos : longint;size : longint);
  48. procedure gettempofsizereference(l : longint;var ref : treference);
  49. function istemp(const ref : treference) : boolean;
  50. procedure ungetiftemp(const ref : treference);
  51. procedure del_reference(const ref : treference);
  52. procedure del_locref(const location : tlocation);
  53. { pushs and restores registers }
  54. procedure pushusedregisters(var pushed : tpushed;b : byte);
  55. procedure popusedregisters(const pushed : tpushed);
  56. var
  57. unused,usableregs : tregisterset;
  58. c_usableregs : longint;
  59. { uses only 1 byte while a set uses in FPC 32 bytes }
  60. usedinproc : byte;
  61. { count, how much a register must be pushed if it is used as register }
  62. { variable }
  63. {$ifdef SUPPORT_MMX}
  64. reg_pushes : array[R_EAX..R_MM6] of longint;
  65. is_reg_var : array[R_EAX..R_MM6] of boolean;
  66. {$else SUPPORT_MMX}
  67. reg_pushes : array[R_EAX..R_EDI] of longint;
  68. is_reg_var : array[R_EAX..R_EDI] of boolean;
  69. {$endif SUPPORT_MMX}
  70. implementation
  71. procedure pushusedregisters(var pushed : tpushed;b : byte);
  72. var
  73. r : tregister;
  74. {$ifdef SUPPORT_MMX}
  75. hr : preference;
  76. {$endif SUPPORT_MMX}
  77. begin
  78. usedinproc:=usedinproc or b;
  79. for r:=R_EAX to R_EBX do
  80. begin
  81. pushed[r]:=false;
  82. { if the register is used by the calling subroutine }
  83. if ((b and ($80 shr byte(r)))<>0) then
  84. begin
  85. { and is present in use }
  86. if not(r in unused) then
  87. begin
  88. { then save it }
  89. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,r)));
  90. { here was a big problem !!!!!}
  91. { you cannot do that for a register that is
  92. globally assigned to a var
  93. this also means that you must push it much more
  94. often, but there must be a better way
  95. maybe by putting the value back to the stack !! }
  96. if not(is_reg_var[r]) then
  97. unused:=unused+[r];
  98. pushed[r]:=true;
  99. end;
  100. end;
  101. end;
  102. {$ifdef SUPPORT_MMX}
  103. for r:=R_MM0 to R_MM6 do
  104. begin
  105. pushed[r]:=false;
  106. { if the mmx register is in use, save it }
  107. if not(r in unused) then
  108. begin
  109. exprasmlist^.concat(new(pai386,op_const_reg(
  110. A_SUB,S_L,8,R_ESP)));
  111. new(hr);
  112. reset_reference(hr^);
  113. hr^.base:=R_ESP;
  114. exprasmlist^.concat(new(pai386,op_reg_ref(
  115. A_MOVQ,S_NO,r,hr)));
  116. if not(is_reg_var[r]) then
  117. unused:=unused+[r];
  118. pushed[r]:=true;
  119. end;
  120. end;
  121. {$endif SUPPORT_MMX}
  122. end;
  123. procedure popusedregisters(const pushed : tpushed);
  124. var
  125. r : tregister;
  126. {$ifdef SUPPORT_MMX}
  127. hr : preference;
  128. {$endif SUPPORT_MMX}
  129. begin
  130. { restore in reverse order: }
  131. {$ifdef SUPPORT_MMX}
  132. for r:=R_MM6 downto R_MM0 do
  133. begin
  134. if pushed[r] then
  135. begin
  136. new(hr);
  137. reset_reference(hr^);
  138. hr^.base:=R_ESP;
  139. exprasmlist^.concat(new(pai386,op_ref_reg(
  140. A_MOVQ,S_NO,hr,r)));
  141. exprasmlist^.concat(new(pai386,op_const_reg(
  142. A_ADD,S_L,8,R_ESP)));
  143. unused:=unused-[r];
  144. end;
  145. end;
  146. {$endif SUPPORT_MMX}
  147. for r:=R_EBX downto R_EAX do
  148. if pushed[r] then
  149. begin
  150. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,r)));
  151. unused:=unused-[r];
  152. end;
  153. end;
  154. procedure ungetregister(r : tregister);
  155. begin
  156. if r in [R_EAX,R_ECX,R_EDX,R_EBX,R_ESP,R_EBP,R_ESI,R_EDI] then
  157. ungetregister32(r)
  158. else if r in [R_AX,R_CX,R_DX,R_BX,R_SP,R_BP,R_SI,R_DI] then
  159. ungetregister32(reg16toreg32(r))
  160. else if r in [R_AL,R_BL,R_CL,R_DL] then
  161. ungetregister32(reg8toreg32(r))
  162. {$ifdef SUPPORT_MMX}
  163. else if r in [R_MM0..R_MM6] then
  164. ungetregistermmx(r)
  165. {$endif SUPPORT_MMX}
  166. else internalerror(18);
  167. end;
  168. procedure ungetregister32(r : tregister);
  169. begin
  170. if cs_maxoptimieren in aktswitches then
  171. begin
  172. { takes much time }
  173. if not(r in usableregs) then
  174. exit;
  175. unused:=unused+[r];
  176. inc(usablereg32);
  177. end
  178. else
  179. begin
  180. if not(r in [R_EAX,R_EBX,R_ECX,R_EDX]) then
  181. exit;
  182. unused:=unused+[r];
  183. inc(usablereg32);
  184. end;
  185. {$ifdef REGALLOC}
  186. exprasmlist^.concat(new(pairegdealloc,init(r)));
  187. {$endif REGALLOC}
  188. end;
  189. {$ifdef SUPPORT_MMX}
  190. function getregistermmx : tregister;
  191. var
  192. r : tregister;
  193. begin
  194. dec(usableregmmx);
  195. for r:=R_MM0 to R_MM6 do
  196. if r in unused then
  197. begin
  198. unused:=unused-[r];
  199. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  200. getregistermmx:=r;
  201. exit;
  202. end;
  203. internalerror(10);
  204. end;
  205. procedure ungetregistermmx(r : tregister);
  206. begin
  207. if cs_maxoptimieren in aktswitches then
  208. begin
  209. { takes much time }
  210. if not(r in usableregs) then
  211. exit;
  212. unused:=unused+[r];
  213. inc(usableregmmx);
  214. end
  215. else
  216. begin
  217. unused:=unused+[r];
  218. inc(usableregmmx);
  219. end;
  220. end;
  221. {$endif SUPPORT_MMX}
  222. procedure del_reference(const ref : treference);
  223. begin
  224. if ref.isintvalue then
  225. exit;
  226. ungetregister32(ref.base);
  227. ungetregister32(ref.index);
  228. { ref.segment:=R_DEFAULT_SEG; }
  229. end;
  230. procedure del_locref(const location : tlocation);
  231. begin
  232. if (location.loc<>loc_mem) and (location.loc<>loc_reference) then
  233. exit;
  234. if location.reference.isintvalue then
  235. exit;
  236. ungetregister32(location.reference.base);
  237. ungetregister32(location.reference.index);
  238. { ref.segment:=R_DEFAULT_SEG; }
  239. end;
  240. function getregister32 : tregister;
  241. begin
  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. end
  249. else if R_EDX in unused then
  250. begin
  251. unused:=unused-[R_EDX];
  252. usedinproc:=usedinproc or ($80 shr byte(R_EDX));
  253. getregister32:=R_EDX;
  254. end
  255. else if R_EBX in unused then
  256. begin
  257. unused:=unused-[R_EBX];
  258. usedinproc:=usedinproc or ($80 shr byte(R_EBX));
  259. getregister32:=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. end
  267. else internalerror(10);
  268. end;
  269. procedure cleartempgen;
  270. begin
  271. unused:=usableregs;
  272. usablereg32:=c_usableregs;
  273. end;
  274. type
  275. pfreerecord = ^tfreerecord;
  276. tfreerecord = record
  277. next : pfreerecord;
  278. pos : longint;
  279. size : longint;
  280. {$ifdef EXTDEBUG}
  281. line : longint;
  282. {$endif}
  283. end;
  284. var
  285. tmpfreelist : pfreerecord;
  286. templist : pfreerecord;
  287. lastoccupied : longint;
  288. firsttemp, maxtemp : longint;
  289. procedure resettempgen;
  290. var
  291. hp : pfreerecord;
  292. begin
  293. while assigned(tmpfreelist) do
  294. begin
  295. hp:=tmpfreelist;
  296. tmpfreelist:=hp^.next;
  297. dispose(hp);
  298. end;
  299. while assigned(templist) do
  300. begin
  301. {$ifdef EXTDEBUG}
  302. Comment(V_Warning,'temporary assignment of size '
  303. +tostr(templist^.size)+' from '+tostr(templist^.line)+
  304. +' at pos '+tostr(templist^.pos)+
  305. ' not freed at the end of the procedure');
  306. {$endif}
  307. hp:=templist;
  308. templist:=hp^.next;
  309. {$ifndef EXTDEBUG}
  310. dispose(hp);
  311. {$endif not EXTDEBUG}
  312. end;
  313. templist:=nil;
  314. tmpfreelist:=nil;
  315. firsttemp:=0;
  316. maxtemp:=0;
  317. lastoccupied:=0;
  318. end;
  319. procedure setfirsttemp(l : longint);
  320. begin
  321. { generates problems
  322. if (l mod 4 <> 0) then dec(l,l mod 4);}
  323. firsttemp:=l;
  324. maxtemp := l;
  325. lastoccupied:=l;
  326. end;
  327. function gettempofsize(size : longint) : longint;
  328. var
  329. last,hp : pfreerecord;
  330. begin
  331. { this code comes from the heap management of FPC ... }
  332. if (size mod 4)<>0 then
  333. size:=size+(4-(size mod 4));
  334. if assigned(tmpfreelist) then
  335. begin
  336. last:=nil;
  337. hp:=tmpfreelist;
  338. while assigned(hp) do
  339. begin
  340. { first fit }
  341. if hp^.size>=size then
  342. begin
  343. gettempofsize:=hp^.pos;
  344. if hp^.pos-size < maxtemp then
  345. maxtemp := hp^.size-size;
  346. { the whole block is needed ? }
  347. if hp^.size>size then
  348. begin
  349. hp^.size:=hp^.size-size;
  350. hp^.pos:=hp^.pos-size;
  351. end
  352. else
  353. begin
  354. if assigned(last) then
  355. last^.next:=hp^.next
  356. else
  357. tmpfreelist:=nil;
  358. dispose(hp);
  359. end;
  360. exit;
  361. end;
  362. last:=hp;
  363. hp:=hp^.next;
  364. end;
  365. end;
  366. { nothing free is big enough : expand temp }
  367. gettempofsize:=lastoccupied-size;
  368. lastoccupied:=lastoccupied-size;
  369. if lastoccupied < maxtemp then
  370. maxtemp := lastoccupied;
  371. end;
  372. function gettempsize : longint;
  373. begin
  374. { align local data to dwords }
  375. if (maxtemp mod 4)<>0 then
  376. dec(maxtemp,4+(maxtemp mod 4));
  377. gettempsize:=-maxtemp;
  378. end;
  379. procedure gettempofsizereference(l : longint;var ref : treference);
  380. var
  381. tl : pfreerecord;
  382. begin
  383. { do a reset, because the reference isn't used }
  384. reset_reference(ref);
  385. ref.offset:=gettempofsize(l);
  386. ref.base:=procinfo.framepointer;
  387. new(tl);
  388. tl^.pos:=ref.offset;
  389. tl^.size:=l;
  390. tl^.next:=templist;
  391. templist:=tl;
  392. {$ifdef EXTDEBUG}
  393. tl^.line:=current_module^.current_inputfile^.line_no;
  394. {$endif}
  395. end;
  396. function istemp(const ref : treference) : boolean;
  397. begin
  398. istemp:=((ref.base=procinfo.framepointer) and
  399. (ref.offset<firsttemp));
  400. end;
  401. procedure ungettemp(pos : longint;size : longint);
  402. var
  403. hp,newhp : pfreerecord;
  404. begin
  405. if (size mod 4)<>0 then
  406. size:=size+(4-(size mod 4));
  407. if size = 0 then
  408. exit;
  409. if pos<=lastoccupied then
  410. if pos=lastoccupied then
  411. begin
  412. lastoccupied:=pos+size;
  413. hp:=tmpfreelist;
  414. newhp:=nil;
  415. while assigned(hp) do
  416. begin
  417. { conneting a free block }
  418. if hp^.pos=lastoccupied then
  419. begin
  420. if assigned(newhp) then newhp^.next:=nil
  421. else tmpfreelist:=nil;
  422. lastoccupied:=lastoccupied+hp^.size;
  423. dispose(hp);
  424. break;
  425. end;
  426. newhp:=hp;
  427. hp:=hp^.next;
  428. end;
  429. end
  430. else
  431. begin
  432. {$ifdef EXTDEBUG}
  433. Comment(V_Warning,'temp managment problem : ungettemp() pos < lastoccupied !');
  434. {$endif}
  435. end
  436. else
  437. begin
  438. new(newhp);
  439. { size can be allways set }
  440. newhp^.size:=size;
  441. newhp^.pos := pos;
  442. { if there is no free list }
  443. if not assigned(tmpfreelist) then
  444. begin
  445. { then generate one }
  446. tmpfreelist:=newhp;
  447. newhp^.next:=nil;
  448. exit;
  449. end;
  450. { search the position to insert }
  451. hp:=tmpfreelist;
  452. while assigned(hp) do
  453. begin
  454. { conneting two blocks ? }
  455. if hp^.pos+hp^.size=pos then
  456. begin
  457. inc(hp^.size,size);
  458. dispose(newhp);
  459. break;
  460. end
  461. { if the end is reached, then concat }
  462. else if hp^.next=nil then
  463. begin
  464. hp^.next:=newhp;
  465. newhp^.next:=nil;
  466. break;
  467. end
  468. { falls der n„chste Zeiger gr”áer ist, dann }
  469. { Einh„ngen }
  470. else if hp^.next^.pos<=pos+size then
  471. begin
  472. { concat two blocks ? }
  473. if pos+size=hp^.next^.pos then
  474. begin
  475. newhp^.next:=hp^.next^.next;
  476. inc(newhp^.size,hp^.next^.size);
  477. dispose(hp^.next);
  478. hp^.next:=newhp;
  479. end
  480. else
  481. begin
  482. newhp^.next:=hp^.next;
  483. hp^.next:=newhp;
  484. end;
  485. break;
  486. end;
  487. hp:=hp^.next;
  488. end;
  489. end;
  490. end;
  491. procedure ungetiftemp(const ref : treference);
  492. var
  493. tl,prev : pfreerecord;
  494. begin
  495. if istemp(ref) then
  496. begin
  497. prev:=nil;
  498. tl:=templist;
  499. while assigned(tl) do
  500. begin
  501. if ref.offset=tl^.pos then
  502. begin
  503. ungettemp(ref.offset,tl^.size);
  504. if assigned(prev) then
  505. prev^.next:=tl^.next
  506. else
  507. templist:=tl^.next;
  508. dispose(tl);
  509. exit;
  510. end
  511. else
  512. begin
  513. prev:=tl;
  514. tl:=tl^.next;
  515. end;
  516. end;
  517. {$ifdef EXTDEBUG}
  518. Comment(V_Warning,'Internal: temp managment problem : '+
  519. 'temp not found for release at offset '+tostr(ref.offset));
  520. {$endIf}
  521. end;
  522. end;
  523. begin
  524. usableregs:=[R_EAX,R_EBX,R_ECX,R_EDX];
  525. {$ifdef SUPPORT_MMX}
  526. usableregs:=usableregs+[R_MM0..R_MM6];
  527. {$endif SUPPORT_MMX}
  528. c_usableregs:=4;
  529. tmpfreelist:=nil;
  530. templist:=nil;
  531. end.
  532. {
  533. $Log$
  534. Revision 1.5 1998-05-11 13:07:58 peter
  535. + $ifdef NEWPPU for the new ppuformat
  536. + $define GDB not longer required
  537. * removed all warnings and stripped some log comments
  538. * no findfirst/findnext anymore to remove smartlink *.o files
  539. Revision 1.4 1998/04/29 10:34:08 pierre
  540. + added some code for ansistring (not complete nor working yet)
  541. * corrected operator overloading
  542. * corrected nasm output
  543. + started inline procedures
  544. + added starstarn : use ** for exponentiation (^ gave problems)
  545. + started UseTokenInfo cond to get accurate positions
  546. Revision 1.3 1998/04/09 22:16:36 florian
  547. * problem with previous REGALLOC solved
  548. * improved property support
  549. Revision 1.2 1998/04/09 15:46:39 florian
  550. + register allocation tracing stuff added
  551. }