temp_gen.pas 13 KB

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