temp_gen.pas 21 KB

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