temp_gen.pas 16 KB

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