temp_gen.pas 14 KB

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