temp_gen.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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 temp_gen;
  19. interface
  20. uses
  21. {$ifdef i386}
  22. {$ifdef ag386bin}
  23. i386base,i386asm,
  24. {$else}
  25. i386,
  26. {$endif}
  27. {$endif i386}
  28. {$ifdef m68k}
  29. m68k,
  30. {$endif m68k}
  31. cobjects,globals,tree,hcodegen,verbose,files,aasm;
  32. type
  33. { this saves some memory }
  34. {$ifdef TEST_MINENUMSIZE}
  35. {$ifdef FPC}
  36. {$minenumsize 1}
  37. {$endif FPC}
  38. {$endif TEST_MINENUMSIZE}
  39. ttemptype = (tt_normal,tt_ansistring,tt_widestring);
  40. {$ifdef TEST_MINENUMSIZE}
  41. {$ifdef FPC}
  42. {$minenumsize default}
  43. {$endif FPC}
  44. {$endif TEST_MINENUMSIZE}
  45. { generates temporary variables }
  46. procedure resettempgen;
  47. procedure setfirsttemp(l : longint);
  48. function gettempsize : longint;
  49. function gettempofsize(size : longint) : longint;
  50. { special call for inlined procedures }
  51. function gettempofsizepersistant(size : longint) : longint;
  52. { for parameter func returns }
  53. procedure persistanttemptonormal(pos : longint);
  54. {procedure ungettemp(pos : longint;size : longint);}
  55. procedure ungetpersistanttemp(pos : longint;size : longint);
  56. procedure gettempofsizereference(l : longint;var ref : treference);
  57. procedure gettempslotreference(slottype : ttemptype;var ref : treference);
  58. function istemp(const ref : treference) : boolean;
  59. procedure ungetiftemp(const ref : treference);
  60. function ungetiftempansi(const ref : treference) : boolean;
  61. procedure gettempansistringreference(var ref : treference);
  62. type
  63. pfreerecord = ^tfreerecord;
  64. tfreerecord = record
  65. next : pfreerecord;
  66. pos : longint;
  67. size : longint;
  68. persistant : boolean; { used for inlined procedures }
  69. is_ansistring : boolean;
  70. is_freeansistring : boolean;
  71. temptype : ttemptype;
  72. {$ifdef EXTDEBUG}
  73. posinfo,releaseposinfo : tfileposinfo;
  74. {$endif}
  75. end;
  76. var
  77. tempansilist : pfreerecord;
  78. implementation
  79. uses
  80. scanner,systems
  81. {$ifdef i386}
  82. ,cgai386
  83. {$endif i386}
  84. {$ifdef m68k}
  85. ,cga68k
  86. {$endif m68k}
  87. ;
  88. var
  89. { contains all free temps }
  90. tmpfreelist : pfreerecord;
  91. { contains all used temps }
  92. templist : pfreerecord;
  93. { contains the slots for ansi/wide string temps }
  94. reftempslots : pfreerecord;
  95. {$ifdef EXTDEBUG}
  96. tempfreedlist : pfreerecord;
  97. {$endif}
  98. lastoccupied : longint;
  99. firsttemp, maxtemp : longint;
  100. procedure resettempgen;
  101. var
  102. hp : pfreerecord;
  103. begin
  104. while assigned(tmpfreelist) do
  105. begin
  106. hp:=tmpfreelist;
  107. tmpfreelist:=hp^.next;
  108. dispose(hp);
  109. end;
  110. while assigned(templist) do
  111. begin
  112. {$ifdef EXTDEBUG}
  113. Comment(V_Warning,'temporary assignment of size '
  114. +tostr(templist^.size)+' from pos '+tostr(templist^.posinfo.line)
  115. +':'+tostr(templist^.posinfo.column)
  116. +' at pos '+tostr(templist^.pos)+
  117. ' not freed at the end of the procedure');
  118. {$endif}
  119. hp:=templist;
  120. templist:=hp^.next;
  121. dispose(hp);
  122. end;
  123. {$ifdef EXTDEBUG}
  124. while assigned(tempfreedlist) do
  125. begin
  126. hp:=tempfreedlist;
  127. tempfreedlist:=hp^.next;
  128. dispose(hp);
  129. end;
  130. {$endif}
  131. while assigned(tempansilist) do
  132. begin
  133. hp:=tempansilist;
  134. tempansilist:=hp^.next;
  135. dispose(hp);
  136. end;
  137. firsttemp:=0;
  138. maxtemp:=0;
  139. lastoccupied:=0;
  140. end;
  141. procedure setfirsttemp(l : longint);
  142. begin
  143. { this is a negative value normally }
  144. if l < 0 then
  145. Begin
  146. if odd(l) then
  147. Dec(l);
  148. end
  149. else
  150. Begin
  151. if odd(l) then
  152. Inc(l);
  153. end;
  154. firsttemp:=l;
  155. maxtemp:=l;
  156. lastoccupied:=l;
  157. end;
  158. function gettempofsize(size : longint) : longint;
  159. var
  160. tl,last,hp : pfreerecord;
  161. ofs : longint;
  162. begin
  163. { this code comes from the heap management of FPC ... }
  164. if (size mod 4)<>0 then
  165. size:=size+(4-(size mod 4));
  166. ofs:=0;
  167. if assigned(tmpfreelist) then
  168. begin
  169. last:=nil;
  170. hp:=tmpfreelist;
  171. while assigned(hp) do
  172. begin
  173. { first fit }
  174. if hp^.size>=size then
  175. begin
  176. ofs:=hp^.pos;
  177. { the whole block is needed ? }
  178. if hp^.size>size then
  179. begin
  180. dec(hp^.size,size);
  181. { the value is <0 so we need to add the size
  182. instead of sub (PFV) }
  183. inc(hp^.pos,size);
  184. end
  185. else
  186. begin
  187. if assigned(last) then
  188. last^.next:=hp^.next
  189. else
  190. tmpfreelist:=nil;
  191. dispose(hp);
  192. end;
  193. break;
  194. end;
  195. last:=hp;
  196. hp:=hp^.next;
  197. end;
  198. end;
  199. { nothing free is big enough : expand temp }
  200. if ofs=0 then
  201. begin
  202. ofs:=lastoccupied-size;
  203. lastoccupied:=lastoccupied-size;
  204. if lastoccupied < maxtemp then
  205. maxtemp := lastoccupied;
  206. end;
  207. new(tl);
  208. tl^.pos:=ofs;
  209. tl^.size:=size;
  210. tl^.next:=templist;
  211. tl^.persistant:=false;
  212. tl^.temptype:=tt_normal;
  213. templist:=tl;
  214. {$ifdef EXTDEBUG}
  215. tl^.posinfo:=aktfilepos;
  216. {$endif}
  217. exprasmlist^.concat(new(paitempalloc,alloc(ofs,size)));
  218. gettempofsize:=ofs;
  219. end;
  220. function gettempofsizepersistant(size : longint) : longint;
  221. var
  222. l : longint;
  223. begin
  224. l:=gettempofsize(size);
  225. templist^.persistant:=true;
  226. {$ifdef EXTDEBUG}
  227. Comment(V_Debug,'temp managment : call to gettempofsizepersistant()'+
  228. ' with size '+tostr(size)+' returned '+tostr(l));
  229. {$endif}
  230. gettempofsizepersistant:=l;
  231. end;
  232. function gettempsize : longint;
  233. begin
  234. {$ifdef i386}
  235. { align local data to dwords }
  236. if (maxtemp mod 4)<>0 then
  237. dec(maxtemp,4+(maxtemp mod 4));
  238. {$endif}
  239. {$ifdef m68k}
  240. { we only push words and we want to stay on }
  241. { even stack addresses }
  242. { maxtemp is negative }
  243. if (maxtemp mod 2)<>0 then
  244. dec(maxtemp);
  245. {$endif}
  246. gettempsize:=-maxtemp;
  247. end;
  248. procedure gettempofsizereference(l : longint;var ref : treference);
  249. begin
  250. { do a reset, because the reference isn't used }
  251. reset_reference(ref);
  252. ref.offset:=gettempofsize(l);
  253. ref.base:=procinfo.framepointer;
  254. end;
  255. function gettempansioffset : longint;
  256. var
  257. ofs : longint;
  258. tl : pfreerecord;
  259. begin
  260. tl:=tempansilist;
  261. while assigned(tl) do
  262. begin
  263. if tl^.is_freeansistring then
  264. break;
  265. tl:=tl^.next;
  266. end;
  267. if assigned(tl) then
  268. begin
  269. tl^.is_freeansistring:=false;
  270. ofs:=tl^.pos;
  271. end
  272. else
  273. begin
  274. if lastoccupied<>maxtemp then
  275. begin
  276. { we cannnot use already used temp
  277. so we need to convert that space into
  278. a tempfreeitem ! }
  279. new(tl);
  280. tl^.pos:=maxtemp;
  281. tl^.size:=lastoccupied-maxtemp;
  282. tl^.next:=tmpfreelist;
  283. lastoccupied:=maxtemp;
  284. tl^.persistant:=false;
  285. tl^.is_ansistring:=false;
  286. tl^.is_freeansistring:=false;
  287. tmpfreelist:=tl;
  288. end;
  289. ofs:=maxtemp-target_os.size_of_pointer;
  290. maxtemp:=maxtemp-target_os.size_of_pointer;
  291. new(tl);
  292. tl^.pos:=ofs;
  293. tl^.size:=target_os.size_of_pointer;
  294. tl^.next:=tempansilist;
  295. tl^.persistant:=false;
  296. tl^.is_ansistring:=true;
  297. tl^.is_freeansistring:=false;
  298. tempansilist:=tl;
  299. end;
  300. gettempansioffset:=ofs;
  301. end;
  302. procedure gettempansistringreference(var ref : treference);
  303. begin
  304. { do a reset, because the reference isn't used }
  305. reset_reference(ref);
  306. ref.offset:=gettempansioffset;
  307. ref.base:=procinfo.framepointer;
  308. end;
  309. function ungetiftempansi(const ref : treference) : boolean;
  310. var
  311. tl : pfreerecord;
  312. begin
  313. ungetiftempansi:=false;
  314. tl:=tempansilist;
  315. while assigned(tl) do
  316. begin
  317. if tl^.pos=ref.offset then
  318. if tl^.is_ansistring and not tl^.is_freeansistring then
  319. begin
  320. tl^.is_freeansistring:=true;
  321. ungetiftempansi:=true;
  322. exit;
  323. {$ifdef EXTDEBUG}
  324. end
  325. else
  326. begin
  327. Comment(V_Debug,'temp ansi managment problem : ungetiftempansi()'+
  328. ' at pos '+tostr(ref.offset)+ ' already free !');
  329. {$endif}
  330. end;
  331. tl:=tl^.next;
  332. end;
  333. end;
  334. procedure gettempslotreference(slottype : ttemptype;var ref : treference);
  335. begin
  336. { do a reset, because the reference isn't used }
  337. reset_reference(ref);
  338. { this is not enough in my opinion PM }
  339. { because it still can mix different types !! }
  340. ref.offset:=gettempofsize(4);
  341. ref.base:=procinfo.framepointer;
  342. templist^.temptype:=slottype;
  343. end;
  344. function istemp(const ref : treference) : boolean;
  345. begin
  346. { ref.index = R_NO was missing
  347. led to problems with local arrays
  348. with lower bound > 0 (PM) }
  349. istemp:=((ref.base=procinfo.framepointer) and
  350. (ref.offset<firsttemp) and (ref.index=R_NO));
  351. end;
  352. procedure persistanttemptonormal(pos : longint);
  353. var hp : pfreerecord;
  354. begin
  355. hp:=templist;
  356. while assigned(hp) do
  357. if (hp^.persistant) and (hp^.pos=pos) then
  358. begin
  359. {$ifdef EXTDEBUG}
  360. Comment(V_Debug,'temp managment : persistanttemptonormal()'+
  361. ' at pos '+tostr(pos)+ ' found !');
  362. {$endif}
  363. hp^.persistant:=false;
  364. exit;
  365. end
  366. else
  367. hp:=hp^.next;
  368. {$ifdef EXTDEBUG}
  369. Comment(V_Debug,'temp managment problem : persistanttemptonormal()'+
  370. ' at pos '+tostr(pos)+ ' not found !');
  371. {$endif}
  372. end;
  373. procedure ungettemp(pos : longint;size : longint);
  374. var
  375. hp,newhp : pfreerecord;
  376. begin
  377. if (size mod 4)<>0 then
  378. size:=size+(4-(size mod 4));
  379. if size = 0 then
  380. exit;
  381. exprasmlist^.concat(new(paitempalloc,dealloc(pos,size)));
  382. if pos<=lastoccupied then
  383. if pos=lastoccupied then
  384. begin
  385. lastoccupied:=pos+size;
  386. hp:=tmpfreelist;
  387. newhp:=nil;
  388. while assigned(hp) do
  389. begin
  390. { conneting a free block }
  391. if hp^.pos=lastoccupied then
  392. begin
  393. if assigned(newhp) then newhp^.next:=nil
  394. else tmpfreelist:=nil;
  395. lastoccupied:=lastoccupied+hp^.size;
  396. dispose(hp);
  397. break;
  398. end;
  399. newhp:=hp;
  400. hp:=hp^.next;
  401. end;
  402. end
  403. else
  404. begin
  405. {$ifdef EXTDEBUG}
  406. Comment(V_Warning,'temp managment problem : ungettemp()'+
  407. 'pos '+tostr(pos)+ '< lastoccupied '+tostr(lastoccupied)+' !');
  408. {$endif}
  409. end
  410. else
  411. begin
  412. new(newhp);
  413. { size can be allways set }
  414. newhp^.size:=size;
  415. newhp^.pos := pos;
  416. { if there is no free list }
  417. if not assigned(tmpfreelist) then
  418. begin
  419. { then generate one }
  420. tmpfreelist:=newhp;
  421. newhp^.next:=nil;
  422. exit;
  423. end;
  424. { search the position to insert }
  425. hp:=tmpfreelist;
  426. while assigned(hp) do
  427. begin
  428. { conneting two blocks ? }
  429. if hp^.pos+hp^.size=pos then
  430. begin
  431. inc(hp^.size,size);
  432. dispose(newhp);
  433. break;
  434. end
  435. { if the end is reached, then concat }
  436. else if hp^.next=nil then
  437. begin
  438. hp^.next:=newhp;
  439. newhp^.next:=nil;
  440. break;
  441. end
  442. { falls der n„chste Zeiger gr”áer ist, dann }
  443. { Einh„ngen }
  444. else if hp^.next^.pos<=pos+size then
  445. begin
  446. { concat two blocks ? }
  447. if pos+size=hp^.next^.pos then
  448. begin
  449. newhp^.next:=hp^.next^.next;
  450. inc(newhp^.size,hp^.next^.size);
  451. dispose(hp^.next);
  452. hp^.next:=newhp;
  453. end
  454. else
  455. begin
  456. newhp^.next:=hp^.next;
  457. hp^.next:=newhp;
  458. end;
  459. break;
  460. end;
  461. hp:=hp^.next;
  462. end;
  463. end;
  464. end;
  465. procedure ungetpersistanttemp(pos : longint;size : longint);
  466. var
  467. prev,hp : pfreerecord;
  468. begin
  469. ungettemp(pos,size);
  470. prev:=nil;
  471. hp:=templist;
  472. while assigned(hp) do
  473. begin
  474. if (hp^.persistant) and (hp^.pos=pos) and (hp^.size=size) then
  475. begin
  476. if assigned(prev) then
  477. prev^.next:=hp^.next
  478. else
  479. templist:=hp^.next;
  480. {$ifdef EXTDEBUG}
  481. Comment(V_Debug,'temp managment : ungetpersistanttemp()'+
  482. ' at pos '+tostr(pos)+ ' found !');
  483. hp^.next:=tempfreedlist;
  484. tempfreedlist:=hp;
  485. hp^.releaseposinfo:=aktfilepos;
  486. {$else}
  487. dispose(hp);
  488. {$endif}
  489. exit;
  490. end;
  491. prev:=hp;
  492. hp:=hp^.next;
  493. end;
  494. {$ifdef EXTDEBUG}
  495. Comment(V_Warning,'temp managment problem : ungetpersistanttemp()'+
  496. ' at pos '+tostr(pos)+ ' not found !');
  497. {$endif}
  498. end;
  499. procedure ungetiftemp(const ref : treference);
  500. var
  501. tl,prev : pfreerecord;
  502. begin
  503. if istemp(ref) then
  504. begin
  505. { first check if ansistring }
  506. if ungetiftempansi(ref) then
  507. exit;
  508. prev:=nil;
  509. tl:=templist;
  510. while assigned(tl) do
  511. begin
  512. { no release of persistant blocks this way!! }
  513. if (tl^.persistant) or (tl^.temptype<>tt_normal) then
  514. if (ref.offset>=tl^.pos) and
  515. (ref.offset<tl^.pos+tl^.size) then
  516. begin
  517. {$ifdef EXTDEBUG}
  518. Comment(V_Debug,'temp '+
  519. ' at pos '+tostr(ref.offset)+ ' not released because persistant or slot!');
  520. {$endif}
  521. exit;
  522. end;
  523. if (ref.offset=tl^.pos) then
  524. begin
  525. ungettemp(ref.offset,tl^.size);
  526. {$ifdef TEMPDEBUG}
  527. Comment(V_Debug,'temp managment : ungettemp()'+
  528. ' at pos '+tostr(tl^.pos)+ ' found !');
  529. {$endif}
  530. if assigned(prev) then
  531. prev^.next:=tl^.next
  532. else
  533. templist:=tl^.next;
  534. {$ifdef EXTDEBUG}
  535. tl^.next:=tempfreedlist;
  536. tempfreedlist:=tl;
  537. tl^.releaseposinfo:=aktfilepos;
  538. {$else}
  539. dispose(tl);
  540. {$endif}
  541. exit;
  542. end
  543. else
  544. begin
  545. prev:=tl;
  546. tl:=tl^.next;
  547. end;
  548. end;
  549. {$ifdef EXTDEBUG}
  550. Comment(V_Warning,'Internal: temp managment problem : '+
  551. 'temp not found for release at offset '+tostr(ref.offset));
  552. tl:=tempfreedlist;
  553. while assigned(tl) do
  554. begin
  555. if (ref.offset=tl^.pos) then
  556. begin
  557. Comment(V_Warning,'Last temporary assignment of size '
  558. +tostr(tl^.size)+' from pos '+tostr(tl^.posinfo.line)
  559. +':'+tostr(tl^.posinfo.column)
  560. +' at pos '+tostr(tl^.pos)+
  561. ' has been already freed at '
  562. +tostr(tl^.releaseposinfo.line)
  563. +':'+tostr(tl^.releaseposinfo.column)
  564. );
  565. Exit;
  566. end;
  567. tl:=tl^.next;
  568. end;
  569. {$endIf}
  570. end;
  571. end;
  572. procedure inittemps;
  573. begin
  574. { hp:=temp }
  575. end;
  576. begin
  577. tmpfreelist:=nil;
  578. templist:=nil;
  579. reftempslots:=nil;
  580. end.
  581. {
  582. $Log$
  583. Revision 1.17 1999-04-16 11:49:45 peter
  584. + tempalloc
  585. + -at to show temp alloc info in .s file
  586. Revision 1.16 1999/04/14 09:10:46 peter
  587. * fixed tempansi which set wrong pos in free temp
  588. Revision 1.15 1999/04/09 13:05:45 pierre
  589. * Minenumsize=1 under TEST_ENUMSIZE cond because buggy
  590. Revision 1.14 1999/04/09 09:55:20 peter
  591. * typo fixed
  592. Revision 1.13 1999/04/09 08:39:20 peter
  593. * fixed reuse position
  594. Revision 1.12 1999/04/08 23:52:59 pierre
  595. + tempansilist and gettempansistringreference
  596. Revision 1.11 1999/04/08 20:59:44 florian
  597. * fixed problem with default properties which are a class
  598. * case bug (from the mailing list with -O2) fixed, the
  599. distance of the case labels can be greater than the positive
  600. range of a longint => it is now a dword for fpc
  601. Revision 1.10 1999/04/06 11:19:49 peter
  602. * fixed temp reuse
  603. Revision 1.9 1999/02/22 02:15:56 peter
  604. * updates for ag386bin
  605. Revision 1.8 1999/02/11 09:35:19 pierre
  606. * ExtDebug conditionnal infinite loop on temp problem removed
  607. Revision 1.7 1999/02/02 23:52:33 florian
  608. * problem with calls to method pointers in methods fixed
  609. - double ansistrings temp management removed
  610. Revision 1.6 1999/01/15 11:34:23 pierre
  611. + better info for temp allocation debugging
  612. Revision 1.5 1998/11/30 09:43:24 pierre
  613. * some range check bugs fixed (still not working !)
  614. + added DLL writing support for win32 (also accepts variables)
  615. + TempAnsi for code that could be used for Temporary ansi strings
  616. handling
  617. Revision 1.4 1998/10/09 08:56:32 pierre
  618. * several memory leaks fixed
  619. Revision 1.3 1998/07/16 08:01:42 pierre
  620. * small bug correction due to newinput
  621. (only with tempdebug conditionnal)
  622. Revision 1.2 1998/07/10 10:51:05 peter
  623. * m68k updates
  624. Revision 1.1 1998/06/08 16:07:41 pierre
  625. * temp_gen contains all temporary var functions
  626. (processor independent)
  627. }