temp_gen.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. { the whole block is needed ? }
  148. if hp^.size>size then
  149. begin
  150. hp^.size:=hp^.size-size;
  151. hp^.pos:=hp^.pos-size;
  152. end
  153. else
  154. begin
  155. if assigned(last) then
  156. last^.next:=hp^.next
  157. else
  158. tmpfreelist:=nil;
  159. dispose(hp);
  160. end;
  161. break;
  162. end;
  163. last:=hp;
  164. hp:=hp^.next;
  165. end;
  166. end;
  167. { nothing free is big enough : expand temp }
  168. if ofs=0 then
  169. begin
  170. ofs:=lastoccupied-size;
  171. lastoccupied:=lastoccupied-size;
  172. if lastoccupied < maxtemp then
  173. maxtemp := lastoccupied;
  174. end;
  175. new(tl);
  176. tl^.pos:=ofs;
  177. tl^.size:=size;
  178. tl^.next:=templist;
  179. tl^.persistant:=false;
  180. templist:=tl;
  181. {$ifdef EXTDEBUG}
  182. tl^.posinfo:=aktfilepos;
  183. {$endif}
  184. gettempofsize:=ofs;
  185. end;
  186. function gettempofsizepersistant(size : longint) : longint;
  187. var
  188. l : longint;
  189. begin
  190. l:=gettempofsize(size);
  191. templist^.persistant:=true;
  192. {$ifdef EXTDEBUG}
  193. Comment(V_Debug,'temp managment : call to gettempofsizepersistant()'+
  194. ' with size '+tostr(size)+' returned '+tostr(l));
  195. {$endif}
  196. gettempofsizepersistant:=l;
  197. end;
  198. function gettempsize : longint;
  199. begin
  200. {$ifdef i386}
  201. { align local data to dwords }
  202. if (maxtemp mod 4)<>0 then
  203. dec(maxtemp,4+(maxtemp mod 4));
  204. {$endif}
  205. {$ifdef m68k}
  206. { we only push words and we want to stay on }
  207. { even stack addresses }
  208. { maxtemp is negative }
  209. if (maxtemp mod 2)<>0 then
  210. dec(maxtemp);
  211. {$endif}
  212. gettempsize:=-maxtemp;
  213. end;
  214. procedure gettempofsizereference(l : longint;var ref : treference);
  215. begin
  216. { do a reset, because the reference isn't used }
  217. reset_reference(ref);
  218. ref.offset:=gettempofsize(l);
  219. ref.base:=procinfo.framepointer;
  220. end;
  221. procedure gettempansistringreference(var ref : treference);
  222. begin
  223. { do a reset, because the reference isn't used }
  224. reset_reference(ref);
  225. ref.offset:=gettempofsize(4);
  226. ref.base:=procinfo.framepointer;
  227. end;
  228. function istemp(const ref : treference) : boolean;
  229. begin
  230. { ref.index = R_NO was missing
  231. led to problems with local arrays
  232. with lower bound > 0 (PM) }
  233. istemp:=((ref.base=procinfo.framepointer) and
  234. (ref.offset<firsttemp) and (ref.index=R_NO));
  235. end;
  236. procedure persistanttemptonormal(pos : longint);
  237. var hp : pfreerecord;
  238. begin
  239. hp:=templist;
  240. while assigned(hp) do
  241. if (hp^.persistant) and (hp^.pos=pos) then
  242. begin
  243. {$ifdef EXTDEBUG}
  244. Comment(V_Debug,'temp managment : persistanttemptonormal()'+
  245. ' at pos '+tostr(pos)+ ' found !');
  246. {$endif}
  247. hp^.persistant:=false;
  248. exit;
  249. end
  250. else
  251. hp:=hp^.next;
  252. {$ifdef EXTDEBUG}
  253. Comment(V_Debug,'temp managment problem : persistanttemptonormal()'+
  254. ' at pos '+tostr(pos)+ ' not found !');
  255. {$endif}
  256. end;
  257. procedure ungetpersistanttemp(pos : longint;size : longint);
  258. var
  259. prev,hp : pfreerecord;
  260. begin
  261. ungettemp(pos,size);
  262. prev:=nil;
  263. hp:=templist;
  264. while assigned(hp) do
  265. begin
  266. if (hp^.persistant) and (hp^.pos=pos) and (hp^.size=size) then
  267. begin
  268. if assigned(prev) then
  269. prev^.next:=hp^.next
  270. else
  271. templist:=hp^.next;
  272. {$ifdef EXTDEBUG}
  273. Comment(V_Debug,'temp managment : ungetpersistanttemp()'+
  274. ' at pos '+tostr(pos)+ ' found !');
  275. hp^.next:=tempfreedlist;
  276. tempfreedlist:=hp;
  277. hp^.releaseposinfo:=aktfilepos;
  278. {$else}
  279. dispose(hp);
  280. {$endif}
  281. exit;
  282. end;
  283. prev:=hp;
  284. hp:=hp^.next;
  285. end;
  286. {$ifdef EXTDEBUG}
  287. Comment(V_Warning,'temp managment problem : ungetpersistanttemp()'+
  288. ' at pos '+tostr(pos)+ ' not found !');
  289. {$endif}
  290. end;
  291. procedure ungettemp(pos : longint;size : longint);
  292. var
  293. hp,newhp : pfreerecord;
  294. begin
  295. if (size mod 4)<>0 then
  296. size:=size+(4-(size mod 4));
  297. if size = 0 then
  298. exit;
  299. if pos<=lastoccupied then
  300. if pos=lastoccupied then
  301. begin
  302. lastoccupied:=pos+size;
  303. hp:=tmpfreelist;
  304. newhp:=nil;
  305. while assigned(hp) do
  306. begin
  307. { conneting a free block }
  308. if hp^.pos=lastoccupied then
  309. begin
  310. if assigned(newhp) then newhp^.next:=nil
  311. else tmpfreelist:=nil;
  312. lastoccupied:=lastoccupied+hp^.size;
  313. dispose(hp);
  314. break;
  315. end;
  316. newhp:=hp;
  317. hp:=hp^.next;
  318. end;
  319. end
  320. else
  321. begin
  322. {$ifdef EXTDEBUG}
  323. Comment(V_Warning,'temp managment problem : ungettemp()'+
  324. 'pos '+tostr(pos)+ '< lastoccupied '+tostr(lastoccupied)+' !');
  325. {$endif}
  326. end
  327. else
  328. begin
  329. new(newhp);
  330. { size can be allways set }
  331. newhp^.size:=size;
  332. newhp^.pos := pos;
  333. { if there is no free list }
  334. if not assigned(tmpfreelist) then
  335. begin
  336. { then generate one }
  337. tmpfreelist:=newhp;
  338. newhp^.next:=nil;
  339. exit;
  340. end;
  341. { search the position to insert }
  342. hp:=tmpfreelist;
  343. while assigned(hp) do
  344. begin
  345. { conneting two blocks ? }
  346. if hp^.pos+hp^.size=pos then
  347. begin
  348. inc(hp^.size,size);
  349. dispose(newhp);
  350. break;
  351. end
  352. { if the end is reached, then concat }
  353. else if hp^.next=nil then
  354. begin
  355. hp^.next:=newhp;
  356. newhp^.next:=nil;
  357. break;
  358. end
  359. { falls der n„chste Zeiger gr”áer ist, dann }
  360. { Einh„ngen }
  361. else if hp^.next^.pos<=pos+size then
  362. begin
  363. { concat two blocks ? }
  364. if pos+size=hp^.next^.pos then
  365. begin
  366. newhp^.next:=hp^.next^.next;
  367. inc(newhp^.size,hp^.next^.size);
  368. dispose(hp^.next);
  369. hp^.next:=newhp;
  370. end
  371. else
  372. begin
  373. newhp^.next:=hp^.next;
  374. hp^.next:=newhp;
  375. end;
  376. break;
  377. end;
  378. hp:=hp^.next;
  379. end;
  380. end;
  381. end;
  382. procedure ungetiftemp(const ref : treference);
  383. var
  384. tl,prev : pfreerecord;
  385. begin
  386. if istemp(ref) then
  387. begin
  388. prev:=nil;
  389. tl:=templist;
  390. while assigned(tl) do
  391. begin
  392. { no release of persistant blocks this way!! }
  393. if tl^.persistant then
  394. if (ref.offset>=tl^.pos) and
  395. (ref.offset<tl^.pos+tl^.size) then
  396. begin
  397. {$ifdef EXTDEBUG}
  398. Comment(V_Debug,'temp '+
  399. ' at pos '+tostr(ref.offset)+ ' not released because persistant !');
  400. {$endif}
  401. exit;
  402. end;
  403. if (ref.offset=tl^.pos) then
  404. begin
  405. ungettemp(ref.offset,tl^.size);
  406. {$ifdef TEMPDEBUG}
  407. Comment(V_Debug,'temp managment : ungettemp()'+
  408. ' at pos '+tostr(tl^.pos)+ ' found !');
  409. {$endif}
  410. if assigned(prev) then
  411. prev^.next:=tl^.next
  412. else
  413. templist:=tl^.next;
  414. {$ifdef EXTDEBUG}
  415. tl^.next:=tempfreedlist;
  416. tempfreedlist:=tl;
  417. tl^.releaseposinfo:=aktfilepos;
  418. {$else}
  419. dispose(tl);
  420. {$endif}
  421. exit;
  422. end
  423. else
  424. begin
  425. prev:=tl;
  426. tl:=tl^.next;
  427. end;
  428. end;
  429. {$ifdef EXTDEBUG}
  430. Comment(V_Warning,'Internal: temp managment problem : '+
  431. 'temp not found for release at offset '+tostr(ref.offset));
  432. tl:=tempfreedlist;
  433. while assigned(tl) do
  434. begin
  435. if (ref.offset=tl^.pos) then
  436. begin
  437. Comment(V_Warning,'Last temporary assignment of size '
  438. +tostr(tl^.size)+' from pos '+tostr(tl^.posinfo.line)
  439. +':'+tostr(tl^.posinfo.column)
  440. +' at pos '+tostr(tl^.pos)+
  441. ' has been already freed at '
  442. +tostr(tl^.releaseposinfo.line)
  443. +':'+tostr(tl^.releaseposinfo.column)
  444. );
  445. Exit;
  446. end;
  447. tl:=tl^.next;
  448. end;
  449. {$endIf}
  450. end;
  451. end;
  452. begin
  453. tmpfreelist:=nil;
  454. templist:=nil;
  455. end.
  456. {
  457. $Log$
  458. Revision 1.10 1999-04-06 11:19:49 peter
  459. * fixed temp reuse
  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. }