temp_gen.pas 14 KB

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