temp_gen.pas 14 KB

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