temp_gen.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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. exprasmlist^.concat(new(paitempalloc,alloc(tl^.pos,tl^.size)));
  302. end;
  303. procedure gettempansistringreference(var ref : treference);
  304. begin
  305. { do a reset, because the reference isn't used }
  306. reset_reference(ref);
  307. ref.offset:=gettempansioffset;
  308. ref.base:=procinfo.framepointer;
  309. end;
  310. function ungetiftempansi(const ref : treference) : boolean;
  311. var
  312. tl : pfreerecord;
  313. begin
  314. ungetiftempansi:=false;
  315. tl:=tempansilist;
  316. while assigned(tl) do
  317. begin
  318. if tl^.pos=ref.offset then
  319. if tl^.is_ansistring and not tl^.is_freeansistring then
  320. begin
  321. tl^.is_freeansistring:=true;
  322. ungetiftempansi:=true;
  323. exprasmlist^.concat(new(paitempalloc,dealloc(tl^.pos,tl^.size)));
  324. exit;
  325. {$ifdef EXTDEBUG}
  326. end
  327. else
  328. begin
  329. Comment(V_Debug,'temp ansi managment problem : ungetiftempansi()'+
  330. ' at pos '+tostr(ref.offset)+ ' already free !');
  331. {$endif}
  332. end;
  333. tl:=tl^.next;
  334. end;
  335. end;
  336. procedure gettempslotreference(slottype : ttemptype;var ref : treference);
  337. begin
  338. { do a reset, because the reference isn't used }
  339. reset_reference(ref);
  340. { this is not enough in my opinion PM }
  341. { because it still can mix different types !! }
  342. ref.offset:=gettempofsize(4);
  343. ref.base:=procinfo.framepointer;
  344. templist^.temptype:=slottype;
  345. end;
  346. function istemp(const ref : treference) : boolean;
  347. begin
  348. { ref.index = R_NO was missing
  349. led to problems with local arrays
  350. with lower bound > 0 (PM) }
  351. istemp:=((ref.base=procinfo.framepointer) and
  352. (ref.offset<firsttemp) and (ref.index=R_NO));
  353. end;
  354. procedure persistanttemptonormal(pos : longint);
  355. var hp : pfreerecord;
  356. begin
  357. hp:=templist;
  358. while assigned(hp) do
  359. if (hp^.persistant) and (hp^.pos=pos) then
  360. begin
  361. {$ifdef EXTDEBUG}
  362. Comment(V_Debug,'temp managment : persistanttemptonormal()'+
  363. ' at pos '+tostr(pos)+ ' found !');
  364. {$endif}
  365. hp^.persistant:=false;
  366. exit;
  367. end
  368. else
  369. hp:=hp^.next;
  370. {$ifdef EXTDEBUG}
  371. Comment(V_Debug,'temp managment problem : persistanttemptonormal()'+
  372. ' at pos '+tostr(pos)+ ' not found !');
  373. {$endif}
  374. end;
  375. procedure ungettemp(pos : longint;size : longint);
  376. var
  377. hp,newhp : pfreerecord;
  378. begin
  379. if (size mod 4)<>0 then
  380. size:=size+(4-(size mod 4));
  381. if size = 0 then
  382. exit;
  383. exprasmlist^.concat(new(paitempalloc,dealloc(pos,size)));
  384. if pos<=lastoccupied then
  385. if pos=lastoccupied then
  386. begin
  387. lastoccupied:=pos+size;
  388. hp:=tmpfreelist;
  389. newhp:=nil;
  390. while assigned(hp) do
  391. begin
  392. { conneting a free block }
  393. if hp^.pos=lastoccupied then
  394. begin
  395. if assigned(newhp) then newhp^.next:=nil
  396. else tmpfreelist:=nil;
  397. lastoccupied:=lastoccupied+hp^.size;
  398. dispose(hp);
  399. break;
  400. end;
  401. newhp:=hp;
  402. hp:=hp^.next;
  403. end;
  404. end
  405. else
  406. begin
  407. {$ifdef EXTDEBUG}
  408. Comment(V_Warning,'temp managment problem : ungettemp()'+
  409. 'pos '+tostr(pos)+ '< lastoccupied '+tostr(lastoccupied)+' !');
  410. {$endif}
  411. end
  412. else
  413. begin
  414. new(newhp);
  415. { size can be allways set }
  416. newhp^.size:=size;
  417. newhp^.pos := pos;
  418. { if there is no free list }
  419. if not assigned(tmpfreelist) then
  420. begin
  421. { then generate one }
  422. tmpfreelist:=newhp;
  423. newhp^.next:=nil;
  424. exit;
  425. end;
  426. { search the position to insert }
  427. hp:=tmpfreelist;
  428. while assigned(hp) do
  429. begin
  430. { conneting two blocks ? }
  431. if hp^.pos+hp^.size=pos then
  432. begin
  433. inc(hp^.size,size);
  434. dispose(newhp);
  435. break;
  436. end
  437. { if the end is reached, then concat }
  438. else if hp^.next=nil then
  439. begin
  440. hp^.next:=newhp;
  441. newhp^.next:=nil;
  442. break;
  443. end
  444. { falls der n„chste Zeiger gr”áer ist, dann }
  445. { Einh„ngen }
  446. else if hp^.next^.pos<=pos+size then
  447. begin
  448. { concat two blocks ? }
  449. if pos+size=hp^.next^.pos then
  450. begin
  451. newhp^.next:=hp^.next^.next;
  452. inc(newhp^.size,hp^.next^.size);
  453. dispose(hp^.next);
  454. hp^.next:=newhp;
  455. end
  456. else
  457. begin
  458. newhp^.next:=hp^.next;
  459. hp^.next:=newhp;
  460. end;
  461. break;
  462. end;
  463. hp:=hp^.next;
  464. end;
  465. end;
  466. end;
  467. procedure ungetpersistanttemp(pos : longint;size : longint);
  468. var
  469. prev,hp : pfreerecord;
  470. begin
  471. ungettemp(pos,size);
  472. prev:=nil;
  473. hp:=templist;
  474. while assigned(hp) do
  475. begin
  476. if (hp^.persistant) and (hp^.pos=pos) and (hp^.size=size) then
  477. begin
  478. if assigned(prev) then
  479. prev^.next:=hp^.next
  480. else
  481. templist:=hp^.next;
  482. {$ifdef EXTDEBUG}
  483. Comment(V_Debug,'temp managment : ungetpersistanttemp()'+
  484. ' at pos '+tostr(pos)+ ' found !');
  485. hp^.next:=tempfreedlist;
  486. tempfreedlist:=hp;
  487. hp^.releaseposinfo:=aktfilepos;
  488. {$else}
  489. dispose(hp);
  490. {$endif}
  491. exit;
  492. end;
  493. prev:=hp;
  494. hp:=hp^.next;
  495. end;
  496. {$ifdef EXTDEBUG}
  497. Comment(V_Warning,'temp managment problem : ungetpersistanttemp()'+
  498. ' at pos '+tostr(pos)+ ' not found !');
  499. {$endif}
  500. end;
  501. procedure ungetiftemp(const ref : treference);
  502. var
  503. tl,prev : pfreerecord;
  504. begin
  505. if istemp(ref) then
  506. begin
  507. { first check if ansistring }
  508. if ungetiftempansi(ref) then
  509. exit;
  510. prev:=nil;
  511. tl:=templist;
  512. while assigned(tl) do
  513. begin
  514. { no release of persistant blocks this way!! }
  515. if (tl^.persistant) or (tl^.temptype<>tt_normal) then
  516. if (ref.offset>=tl^.pos) and
  517. (ref.offset<tl^.pos+tl^.size) then
  518. begin
  519. {$ifdef EXTDEBUG}
  520. Comment(V_Debug,'temp '+
  521. ' at pos '+tostr(ref.offset)+ ' not released because persistant or slot!');
  522. {$endif}
  523. exit;
  524. end;
  525. if (ref.offset=tl^.pos) then
  526. begin
  527. ungettemp(ref.offset,tl^.size);
  528. {$ifdef TEMPDEBUG}
  529. Comment(V_Debug,'temp managment : ungettemp()'+
  530. ' at pos '+tostr(tl^.pos)+ ' found !');
  531. {$endif}
  532. if assigned(prev) then
  533. prev^.next:=tl^.next
  534. else
  535. templist:=tl^.next;
  536. {$ifdef EXTDEBUG}
  537. tl^.next:=tempfreedlist;
  538. tempfreedlist:=tl;
  539. tl^.releaseposinfo:=aktfilepos;
  540. {$else}
  541. dispose(tl);
  542. {$endif}
  543. exit;
  544. end
  545. else
  546. begin
  547. prev:=tl;
  548. tl:=tl^.next;
  549. end;
  550. end;
  551. {$ifdef EXTDEBUG}
  552. Comment(V_Warning,'Internal: temp managment problem : '+
  553. 'temp not found for release at offset '+tostr(ref.offset));
  554. tl:=tempfreedlist;
  555. while assigned(tl) do
  556. begin
  557. if (ref.offset=tl^.pos) then
  558. begin
  559. Comment(V_Warning,'Last temporary assignment of size '
  560. +tostr(tl^.size)+' from pos '+tostr(tl^.posinfo.line)
  561. +':'+tostr(tl^.posinfo.column)
  562. +' at pos '+tostr(tl^.pos)+
  563. ' has been already freed at '
  564. +tostr(tl^.releaseposinfo.line)
  565. +':'+tostr(tl^.releaseposinfo.column)
  566. );
  567. Exit;
  568. end;
  569. tl:=tl^.next;
  570. end;
  571. {$endIf}
  572. end;
  573. end;
  574. procedure inittemps;
  575. begin
  576. { hp:=temp }
  577. end;
  578. begin
  579. tmpfreelist:=nil;
  580. templist:=nil;
  581. reftempslots:=nil;
  582. end.
  583. {
  584. $Log$
  585. Revision 1.18 1999-04-16 14:03:39 pierre
  586. * added paitempalloc for tempansi
  587. Revision 1.17 1999/04/16 11:49:45 peter
  588. + tempalloc
  589. + -at to show temp alloc info in .s file
  590. Revision 1.16 1999/04/14 09:10:46 peter
  591. * fixed tempansi which set wrong pos in free temp
  592. Revision 1.15 1999/04/09 13:05:45 pierre
  593. * Minenumsize=1 under TEST_ENUMSIZE cond because buggy
  594. Revision 1.14 1999/04/09 09:55:20 peter
  595. * typo fixed
  596. Revision 1.13 1999/04/09 08:39:20 peter
  597. * fixed reuse position
  598. Revision 1.12 1999/04/08 23:52:59 pierre
  599. + tempansilist and gettempansistringreference
  600. Revision 1.11 1999/04/08 20:59:44 florian
  601. * fixed problem with default properties which are a class
  602. * case bug (from the mailing list with -O2) fixed, the
  603. distance of the case labels can be greater than the positive
  604. range of a longint => it is now a dword for fpc
  605. Revision 1.10 1999/04/06 11:19:49 peter
  606. * fixed temp reuse
  607. Revision 1.9 1999/02/22 02:15:56 peter
  608. * updates for ag386bin
  609. Revision 1.8 1999/02/11 09:35:19 pierre
  610. * ExtDebug conditionnal infinite loop on temp problem removed
  611. Revision 1.7 1999/02/02 23:52:33 florian
  612. * problem with calls to method pointers in methods fixed
  613. - double ansistrings temp management removed
  614. Revision 1.6 1999/01/15 11:34:23 pierre
  615. + better info for temp allocation debugging
  616. Revision 1.5 1998/11/30 09:43:24 pierre
  617. * some range check bugs fixed (still not working !)
  618. + added DLL writing support for win32 (also accepts variables)
  619. + TempAnsi for code that could be used for Temporary ansi strings
  620. handling
  621. Revision 1.4 1998/10/09 08:56:32 pierre
  622. * several memory leaks fixed
  623. Revision 1.3 1998/07/16 08:01:42 pierre
  624. * small bug correction due to newinput
  625. (only with tempdebug conditionnal)
  626. Revision 1.2 1998/07/10 10:51:05 peter
  627. * m68k updates
  628. Revision 1.1 1998/06/08 16:07:41 pierre
  629. * temp_gen contains all temporary var functions
  630. (processor independent)
  631. }