temp_gen.pas 15 KB

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