temp_gen.pas 16 KB

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