temp_gen.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. { generates temporary variables }
  33. procedure resettempgen;
  34. procedure setfirsttemp(l : longint);
  35. function gettempsize : longint;
  36. function gettempofsize(size : longint) : longint;
  37. { special call for inlined procedures }
  38. function gettempofsizepersistant(size : longint) : longint;
  39. { for parameter func returns }
  40. procedure persistanttemptonormal(pos : longint);
  41. procedure ungettemp(pos : longint;size : longint);
  42. procedure ungetpersistanttemp(pos : longint;size : longint);
  43. procedure gettempofsizereference(l : longint;var ref : treference);
  44. procedure gettempansistringreference(var ref : treference);
  45. function istemp(const ref : treference) : boolean;
  46. procedure ungetiftemp(const ref : treference);
  47. implementation
  48. uses
  49. scanner
  50. {$ifdef i386}
  51. ,cgai386
  52. {$endif i386}
  53. {$ifdef m68k}
  54. ,cga68k
  55. {$endif m68k}
  56. ;
  57. type
  58. pfreerecord = ^tfreerecord;
  59. tfreerecord = record
  60. next : pfreerecord;
  61. pos : longint;
  62. size : longint;
  63. persistant : boolean; { used for inlined procedures }
  64. {$ifdef EXTDEBUG}
  65. posinfo,releaseposinfo : tfileposinfo;
  66. {$endif}
  67. end;
  68. var
  69. tmpfreelist : pfreerecord;
  70. templist : pfreerecord;
  71. {$ifdef EXTDEBUG}
  72. tempfreedlist : pfreerecord;
  73. {$endif}
  74. lastoccupied : longint;
  75. firsttemp, maxtemp : longint;
  76. procedure resettempgen;
  77. var
  78. hp : pfreerecord;
  79. begin
  80. while assigned(tmpfreelist) do
  81. begin
  82. hp:=tmpfreelist;
  83. tmpfreelist:=hp^.next;
  84. dispose(hp);
  85. end;
  86. while assigned(templist) do
  87. begin
  88. {$ifdef EXTDEBUG}
  89. Comment(V_Warning,'temporary assignment of size '
  90. +tostr(templist^.size)+' from pos '+tostr(templist^.posinfo.line)
  91. +':'+tostr(templist^.posinfo.column)
  92. +' at pos '+tostr(templist^.pos)+
  93. ' not freed at the end of the procedure');
  94. {$endif}
  95. hp:=templist;
  96. templist:=hp^.next;
  97. dispose(hp);
  98. end;
  99. {$ifdef EXTDEBUG}
  100. while assigned(tempfreedlist) do
  101. begin
  102. hp:=tempfreedlist;
  103. tempfreedlist:=hp^.next;
  104. dispose(hp);
  105. end;
  106. {$endif}
  107. firsttemp:=0;
  108. maxtemp:=0;
  109. lastoccupied:=0;
  110. end;
  111. procedure setfirsttemp(l : longint);
  112. begin
  113. { this is a negative value normally }
  114. if l < 0 then
  115. Begin
  116. if odd(l) then
  117. Dec(l);
  118. end
  119. else
  120. Begin
  121. if odd(l) then
  122. Inc(l);
  123. end;
  124. firsttemp:=l;
  125. maxtemp:=l;
  126. lastoccupied:=l;
  127. end;
  128. function gettempofsize(size : longint) : longint;
  129. var
  130. tl,last,hp : pfreerecord;
  131. ofs : longint;
  132. begin
  133. { this code comes from the heap management of FPC ... }
  134. if (size mod 4)<>0 then
  135. size:=size+(4-(size mod 4));
  136. ofs:=0;
  137. if assigned(tmpfreelist) then
  138. begin
  139. last:=nil;
  140. hp:=tmpfreelist;
  141. while assigned(hp) do
  142. begin
  143. { first fit }
  144. if hp^.size>=size then
  145. begin
  146. ofs:=hp^.pos;
  147. if hp^.pos-size < maxtemp then
  148. maxtemp := hp^.size-size;
  149. { the whole block is needed ? }
  150. if hp^.size>size then
  151. begin
  152. hp^.size:=hp^.size-size;
  153. hp^.pos:=hp^.pos-size;
  154. end
  155. else
  156. begin
  157. if assigned(last) then
  158. last^.next:=hp^.next
  159. else
  160. tmpfreelist:=nil;
  161. dispose(hp);
  162. end;
  163. break;
  164. end;
  165. last:=hp;
  166. hp:=hp^.next;
  167. end;
  168. end;
  169. { nothing free is big enough : expand temp }
  170. if ofs=0 then
  171. begin
  172. ofs:=lastoccupied-size;
  173. lastoccupied:=lastoccupied-size;
  174. if lastoccupied < maxtemp then
  175. maxtemp := lastoccupied;
  176. end;
  177. new(tl);
  178. tl^.pos:=ofs;
  179. tl^.size:=size;
  180. tl^.next:=templist;
  181. tl^.persistant:=false;
  182. templist:=tl;
  183. {$ifdef EXTDEBUG}
  184. tl^.posinfo:=aktfilepos;
  185. {$endif}
  186. gettempofsize:=ofs;
  187. end;
  188. function gettempofsizepersistant(size : longint) : longint;
  189. var
  190. l : longint;
  191. begin
  192. l:=gettempofsize(size);
  193. templist^.persistant:=true;
  194. {$ifdef EXTDEBUG}
  195. Comment(V_Debug,'temp managment : call to gettempofsizepersistant()'+
  196. ' with size '+tostr(size)+' returned '+tostr(l));
  197. {$endif}
  198. gettempofsizepersistant:=l;
  199. end;
  200. function gettempsize : longint;
  201. begin
  202. {$ifdef i386}
  203. { align local data to dwords }
  204. if (maxtemp mod 4)<>0 then
  205. dec(maxtemp,4+(maxtemp mod 4));
  206. {$endif}
  207. {$ifdef m68k}
  208. { we only push words and we want to stay on }
  209. { even stack addresses }
  210. { maxtemp is negative }
  211. if (maxtemp mod 2)<>0 then
  212. dec(maxtemp);
  213. {$endif}
  214. gettempsize:=-maxtemp;
  215. end;
  216. procedure gettempofsizereference(l : longint;var ref : treference);
  217. begin
  218. { do a reset, because the reference isn't used }
  219. reset_reference(ref);
  220. ref.offset:=gettempofsize(l);
  221. ref.base:=procinfo.framepointer;
  222. end;
  223. procedure gettempansistringreference(var ref : treference);
  224. begin
  225. { do a reset, because the reference isn't used }
  226. reset_reference(ref);
  227. ref.offset:=gettempofsize(4);
  228. ref.base:=procinfo.framepointer;
  229. end;
  230. function istemp(const ref : treference) : boolean;
  231. begin
  232. { ref.index = R_NO was missing
  233. led to problems with local arrays
  234. with lower bound > 0 (PM) }
  235. istemp:=((ref.base=procinfo.framepointer) and
  236. (ref.offset<firsttemp) and (ref.index=R_NO));
  237. end;
  238. procedure persistanttemptonormal(pos : longint);
  239. var hp : pfreerecord;
  240. begin
  241. hp:=templist;
  242. while assigned(hp) do
  243. if (hp^.persistant) and (hp^.pos=pos) then
  244. begin
  245. {$ifdef EXTDEBUG}
  246. Comment(V_Debug,'temp managment : persistanttemptonormal()'+
  247. ' at pos '+tostr(pos)+ ' found !');
  248. {$endif}
  249. hp^.persistant:=false;
  250. exit;
  251. end
  252. else
  253. hp:=hp^.next;
  254. {$ifdef EXTDEBUG}
  255. Comment(V_Debug,'temp managment problem : persistanttemptonormal()'+
  256. ' at pos '+tostr(pos)+ ' not found !');
  257. {$endif}
  258. end;
  259. procedure ungetpersistanttemp(pos : longint;size : longint);
  260. var
  261. prev,hp : pfreerecord;
  262. begin
  263. ungettemp(pos,size);
  264. prev:=nil;
  265. hp:=templist;
  266. while assigned(hp) do
  267. begin
  268. if (hp^.persistant) and (hp^.pos=pos) and (hp^.size=size) then
  269. begin
  270. if assigned(prev) then
  271. prev^.next:=hp^.next
  272. else
  273. templist:=hp^.next;
  274. {$ifdef EXTDEBUG}
  275. Comment(V_Debug,'temp managment : ungetpersistanttemp()'+
  276. ' at pos '+tostr(pos)+ ' found !');
  277. hp^.next:=tempfreedlist;
  278. tempfreedlist:=hp;
  279. hp^.releaseposinfo:=aktfilepos;
  280. {$else}
  281. dispose(hp);
  282. {$endif}
  283. exit;
  284. end;
  285. prev:=hp;
  286. hp:=hp^.next;
  287. end;
  288. {$ifdef EXTDEBUG}
  289. Comment(V_Warning,'temp managment problem : ungetpersistanttemp()'+
  290. ' at pos '+tostr(pos)+ ' not found !');
  291. {$endif}
  292. end;
  293. procedure ungettemp(pos : longint;size : longint);
  294. var
  295. hp,newhp : pfreerecord;
  296. begin
  297. if (size mod 4)<>0 then
  298. size:=size+(4-(size mod 4));
  299. if size = 0 then
  300. exit;
  301. if pos<=lastoccupied then
  302. if pos=lastoccupied then
  303. begin
  304. lastoccupied:=pos+size;
  305. hp:=tmpfreelist;
  306. newhp:=nil;
  307. while assigned(hp) do
  308. begin
  309. { conneting a free block }
  310. if hp^.pos=lastoccupied then
  311. begin
  312. if assigned(newhp) then newhp^.next:=nil
  313. else tmpfreelist:=nil;
  314. lastoccupied:=lastoccupied+hp^.size;
  315. dispose(hp);
  316. break;
  317. end;
  318. newhp:=hp;
  319. hp:=hp^.next;
  320. end;
  321. end
  322. else
  323. begin
  324. {$ifdef EXTDEBUG}
  325. Comment(V_Warning,'temp managment problem : ungettemp()'+
  326. 'pos '+tostr(pos)+ '< lastoccupied '+tostr(lastoccupied)+' !');
  327. {$endif}
  328. end
  329. else
  330. begin
  331. new(newhp);
  332. { size can be allways set }
  333. newhp^.size:=size;
  334. newhp^.pos := pos;
  335. { if there is no free list }
  336. if not assigned(tmpfreelist) then
  337. begin
  338. { then generate one }
  339. tmpfreelist:=newhp;
  340. newhp^.next:=nil;
  341. exit;
  342. end;
  343. { search the position to insert }
  344. hp:=tmpfreelist;
  345. while assigned(hp) do
  346. begin
  347. { conneting two blocks ? }
  348. if hp^.pos+hp^.size=pos then
  349. begin
  350. inc(hp^.size,size);
  351. dispose(newhp);
  352. break;
  353. end
  354. { if the end is reached, then concat }
  355. else if hp^.next=nil then
  356. begin
  357. hp^.next:=newhp;
  358. newhp^.next:=nil;
  359. break;
  360. end
  361. { falls der n„chste Zeiger gr”áer ist, dann }
  362. { Einh„ngen }
  363. else if hp^.next^.pos<=pos+size then
  364. begin
  365. { concat two blocks ? }
  366. if pos+size=hp^.next^.pos then
  367. begin
  368. newhp^.next:=hp^.next^.next;
  369. inc(newhp^.size,hp^.next^.size);
  370. dispose(hp^.next);
  371. hp^.next:=newhp;
  372. end
  373. else
  374. begin
  375. newhp^.next:=hp^.next;
  376. hp^.next:=newhp;
  377. end;
  378. break;
  379. end;
  380. hp:=hp^.next;
  381. end;
  382. end;
  383. end;
  384. procedure ungetiftemp(const ref : treference);
  385. var
  386. tl,prev : pfreerecord;
  387. begin
  388. if istemp(ref) then
  389. begin
  390. prev:=nil;
  391. tl:=templist;
  392. while assigned(tl) do
  393. begin
  394. { no release of persistant blocks this way!! }
  395. if tl^.persistant then
  396. if (ref.offset>=tl^.pos) and
  397. (ref.offset<tl^.pos+tl^.size) then
  398. begin
  399. {$ifdef EXTDEBUG}
  400. Comment(V_Debug,'temp '+
  401. ' at pos '+tostr(ref.offset)+ ' not released because persistant !');
  402. {$endif}
  403. exit;
  404. end;
  405. if (ref.offset=tl^.pos) then
  406. begin
  407. ungettemp(ref.offset,tl^.size);
  408. {$ifdef TEMPDEBUG}
  409. Comment(V_Debug,'temp managment : ungettemp()'+
  410. ' at pos '+tostr(tl^.pos)+ ' found !');
  411. {$endif}
  412. if assigned(prev) then
  413. prev^.next:=tl^.next
  414. else
  415. templist:=tl^.next;
  416. {$ifdef EXTDEBUG}
  417. tl^.next:=tempfreedlist;
  418. tempfreedlist:=tl;
  419. tl^.releaseposinfo:=aktfilepos;
  420. {$else}
  421. dispose(tl);
  422. {$endif}
  423. exit;
  424. end
  425. else
  426. begin
  427. prev:=tl;
  428. tl:=tl^.next;
  429. end;
  430. end;
  431. {$ifdef EXTDEBUG}
  432. Comment(V_Warning,'Internal: temp managment problem : '+
  433. 'temp not found for release at offset '+tostr(ref.offset));
  434. tl:=tempfreedlist;
  435. while assigned(tl) do
  436. begin
  437. if (ref.offset=tl^.pos) then
  438. begin
  439. Comment(V_Warning,'Last temporary assignment of size '
  440. +tostr(tl^.size)+' from pos '+tostr(tl^.posinfo.line)
  441. +':'+tostr(tl^.posinfo.column)
  442. +' at pos '+tostr(tl^.pos)+
  443. ' has been already freed at '
  444. +tostr(tl^.releaseposinfo.line)
  445. +':'+tostr(tl^.releaseposinfo.column)
  446. );
  447. Exit;
  448. end;
  449. tl:=tl^.next;
  450. end;
  451. {$endIf}
  452. end;
  453. end;
  454. begin
  455. tmpfreelist:=nil;
  456. templist:=nil;
  457. end.
  458. {
  459. $Log$
  460. Revision 1.9 1999-02-22 02:15:56 peter
  461. * updates for ag386bin
  462. Revision 1.8 1999/02/11 09:35:19 pierre
  463. * ExtDebug conditionnal infinite loop on temp problem removed
  464. Revision 1.7 1999/02/02 23:52:33 florian
  465. * problem with calls to method pointers in methods fixed
  466. - double ansistrings temp management removed
  467. Revision 1.6 1999/01/15 11:34:23 pierre
  468. + better info for temp allocation debugging
  469. Revision 1.5 1998/11/30 09:43:24 pierre
  470. * some range check bugs fixed (still not working !)
  471. + added DLL writing support for win32 (also accepts variables)
  472. + TempAnsi for code that could be used for Temporary ansi strings
  473. handling
  474. Revision 1.4 1998/10/09 08:56:32 pierre
  475. * several memory leaks fixed
  476. Revision 1.3 1998/07/16 08:01:42 pierre
  477. * small bug correction due to newinput
  478. (only with tempdebug conditionnal)
  479. Revision 1.2 1998/07/10 10:51:05 peter
  480. * m68k updates
  481. Revision 1.1 1998/06/08 16:07:41 pierre
  482. * temp_gen contains all temporary var functions
  483. (processor independent)
  484. }