temp_gen.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. {$ifndef OLDASM}
  23. i386base,i386asm,
  24. {$else}
  25. i386,
  26. {$endif}
  27. {$endif i386}
  28. {$ifdef m68k}
  29. m68k,
  30. {$endif m68k}
  31. cobjects,globals,tree,hcodegen,verbose,files,aasm;
  32. type
  33. ttemptype = (tt_none,tt_free,tt_normal,tt_persistant,tt_ansistring,tt_freeansistring,tt_widestring,tt_freewidestring);
  34. ttemptypeset = set of ttemptype;
  35. ptemprecord = ^ttemprecord;
  36. ttemprecord = record
  37. temptype : ttemptype;
  38. pos : longint;
  39. size : longint;
  40. next : ptemprecord;
  41. nextfree : ptemprecord; { for faster freeblock checking }
  42. {$ifdef EXTDEBUG}
  43. posinfo,
  44. releaseposinfo : tfileposinfo;
  45. {$endif}
  46. end;
  47. var
  48. { contains all temps }
  49. templist : ptemprecord;
  50. { contains all free temps using nextfree links }
  51. tempfreelist : ptemprecord;
  52. { Offsets of the first/last temp }
  53. firsttemp,
  54. lasttemp : longint;
  55. { generates temporary variables }
  56. procedure resettempgen;
  57. procedure setfirsttemp(l : longint);
  58. function gettempsize : longint;
  59. function newtempofsize(size : longint) : longint;
  60. function gettempofsize(size : longint) : longint;
  61. { special call for inlined procedures }
  62. function gettempofsizepersistant(size : longint) : longint;
  63. { for parameter func returns }
  64. procedure persistanttemptonormal(pos : longint);
  65. {procedure ungettemp(pos : longint;size : longint);}
  66. procedure ungetpersistanttemp(pos : longint;size : longint);
  67. procedure gettempofsizereference(l : longint;var ref : treference);
  68. function istemp(const ref : treference) : boolean;
  69. procedure ungetiftemp(const ref : treference);
  70. function ungetiftempansi(const ref : treference) : boolean;
  71. procedure gettempansistringreference(var ref : treference);
  72. implementation
  73. uses
  74. scanner,systems
  75. {$ifdef i386}
  76. ,cgai386
  77. {$endif i386}
  78. {$ifdef m68k}
  79. ,cga68k
  80. {$endif m68k}
  81. ;
  82. procedure resettempgen;
  83. var
  84. hp : ptemprecord;
  85. begin
  86. { Clear the old templist }
  87. while assigned(templist) do
  88. begin
  89. {$ifdef EXTDEBUG}
  90. case templist^.temptype of
  91. tt_normal,
  92. tt_persistant :
  93. Comment(V_Warning,'temporary assignment of size '+
  94. tostr(templist^.size)+' from pos '+tostr(templist^.posinfo.line)+
  95. ':'+tostr(templist^.posinfo.column)+
  96. ' at pos '+tostr(templist^.pos)+
  97. ' not freed at the end of the procedure');
  98. tt_ansistring :
  99. Comment(V_Warning,'temporary ANSI assignment of size '+
  100. tostr(templist^.size)+' from pos '+tostr(templist^.posinfo.line)+
  101. ':'+tostr(templist^.posinfo.column)+
  102. ' at pos '+tostr(templist^.pos)+
  103. ' not freed at the end of the procedure');
  104. end;
  105. {$endif}
  106. hp:=templist;
  107. templist:=hp^.next;
  108. dispose(hp);
  109. end;
  110. templist:=nil;
  111. tempfreelist:=nil;
  112. firsttemp:=0;
  113. lasttemp:=0;
  114. end;
  115. procedure setfirsttemp(l : longint);
  116. begin
  117. { this is a negative value normally }
  118. if l < 0 then
  119. Begin
  120. if odd(l) then
  121. Dec(l);
  122. end
  123. else
  124. Begin
  125. if odd(l) then
  126. Inc(l);
  127. end;
  128. firsttemp:=l;
  129. lasttemp:=l;
  130. end;
  131. function newtempofsize(size : longint) : longint;
  132. var
  133. tl : ptemprecord;
  134. begin
  135. { Just extend the temp, everything below has been use
  136. already }
  137. dec(lasttemp,size);
  138. { now we can create the templist entry }
  139. new(tl);
  140. tl^.temptype:=tt_normal;
  141. tl^.pos:=lasttemp;
  142. tl^.size:=size;
  143. tl^.next:=templist;
  144. tl^.nextfree:=nil;
  145. templist:=tl;
  146. newtempofsize:=tl^.pos;
  147. end;
  148. function gettempofsize(size : longint) : longint;
  149. var
  150. tl,
  151. bestslot,bestprev,
  152. hprev,hp : ptemprecord;
  153. bestsize,ofs : longint;
  154. begin
  155. bestprev:=nil;
  156. bestslot:=nil;
  157. bestsize:=0;
  158. { Align needed size on 4 bytes }
  159. if (size mod 4)<>0 then
  160. size:=size+(4-(size mod 4));
  161. { First check the tmpfreelist }
  162. if assigned(tempfreelist) then
  163. begin
  164. { Check for a slot with the same size first }
  165. hprev:=nil;
  166. hp:=tempfreelist;
  167. while assigned(hp) do
  168. begin
  169. {$ifdef EXTDEBUG}
  170. if hp^.temptype<>tt_free then
  171. Comment(V_Warning,'Temp in freelist is not set to tt_free');
  172. {$endif}
  173. if hp^.size>=size then
  174. begin
  175. { Slot is the same size, then leave immediatly }
  176. if hp^.size=size then
  177. begin
  178. bestprev:=hprev;
  179. bestslot:=hp;
  180. bestsize:=size;
  181. break;
  182. end
  183. else
  184. begin
  185. if (bestsize=0) or (hp^.size<bestsize) then
  186. begin
  187. bestprev:=hprev;
  188. bestslot:=hp;
  189. bestsize:=size;
  190. end;
  191. end;
  192. end;
  193. hprev:=hp;
  194. hp:=hp^.nextfree;
  195. end;
  196. end;
  197. { Reuse an old temp ? }
  198. if assigned(bestslot) then
  199. begin
  200. ofs:=bestslot^.pos;
  201. if bestsize=size then
  202. begin
  203. bestslot^.temptype:=tt_normal;
  204. { Remove from the tempfreelist }
  205. if assigned(bestprev) then
  206. bestprev^.nextfree:=bestslot^.nextfree
  207. else
  208. tempfreelist:=nil;
  209. end
  210. else
  211. begin
  212. { Resize the old block }
  213. dec(bestslot^.size,size);
  214. { Create new block and link after bestslot }
  215. new(tl);
  216. tl^.temptype:=tt_normal;
  217. tl^.pos:=bestslot^.pos+bestslot^.size;
  218. tl^.size:=size;
  219. tl^.nextfree:=nil;
  220. { link the new block }
  221. tl^.next:=bestslot^.next;
  222. bestslot^.next:=tl;
  223. end;
  224. end
  225. else
  226. ofs:=newtempofsize(size);
  227. {$ifdef EXTDEBUG}
  228. tl^.posinfo:=aktfilepos;
  229. {$endif}
  230. exprasmlist^.concat(new(paitempalloc,alloc(ofs,size)));
  231. gettempofsize:=ofs;
  232. end;
  233. function gettempofsizepersistant(size : longint) : longint;
  234. var
  235. l : longint;
  236. begin
  237. l:=gettempofsize(size);
  238. templist^.temptype:=tt_persistant;
  239. {$ifdef EXTDEBUG}
  240. Comment(V_Debug,'temp managment : call to gettempofsizepersistant()'+
  241. ' with size '+tostr(size)+' returned '+tostr(l));
  242. {$endif}
  243. gettempofsizepersistant:=l;
  244. end;
  245. function gettempsize : longint;
  246. begin
  247. gettempsize:=-lasttemp;
  248. end;
  249. procedure gettempofsizereference(l : longint;var ref : treference);
  250. begin
  251. { do a reset, because the reference isn't used }
  252. reset_reference(ref);
  253. ref.offset:=gettempofsize(l);
  254. ref.base:=procinfo.framepointer;
  255. end;
  256. function gettempansioffset : longint;
  257. var
  258. ofs : longint;
  259. foundslot,tl : ptemprecord;
  260. begin
  261. { Reuse old ansi slot ? }
  262. foundslot:=nil;
  263. tl:=templist;
  264. while assigned(tl) do
  265. begin
  266. if tl^.temptype=tt_freeansistring then
  267. begin
  268. foundslot:=tl;
  269. break;
  270. end;
  271. tl:=tl^.next;
  272. end;
  273. if assigned(foundslot) then
  274. begin
  275. foundslot^.temptype:=tt_ansistring;
  276. ofs:=foundslot^.pos;
  277. end
  278. else
  279. begin
  280. ofs:=newtempofsize(target_os.size_of_pointer);
  281. templist^.temptype:=tt_ansistring;
  282. end;
  283. exprasmlist^.concat(new(paitempalloc,alloc(ofs,target_os.size_of_pointer)));
  284. gettempansioffset:=ofs;
  285. end;
  286. procedure gettempansistringreference(var ref : treference);
  287. begin
  288. { do a reset, because the reference isn't used }
  289. reset_reference(ref);
  290. ref.offset:=gettempansioffset;
  291. ref.base:=procinfo.framepointer;
  292. end;
  293. function ungetiftempansi(const ref : treference) : boolean;
  294. var
  295. tl : ptemprecord;
  296. begin
  297. ungetiftempansi:=false;
  298. tl:=templist;
  299. while assigned(tl) do
  300. begin
  301. if tl^.pos=ref.offset then
  302. begin
  303. if tl^.temptype=tt_ansistring then
  304. begin
  305. tl^.temptype:=tt_freeansistring;
  306. ungetiftempansi:=true;
  307. exprasmlist^.concat(new(paitempalloc,dealloc(tl^.pos,tl^.size)));
  308. exit;
  309. {$ifdef EXTDEBUG}
  310. end
  311. else
  312. begin
  313. Comment(V_Debug,'temp ansi managment problem : ungetiftempansi()'+
  314. ' at pos '+tostr(ref.offset)+ ' already free !');
  315. {$endif}
  316. end;
  317. end;
  318. tl:=tl^.next;
  319. end;
  320. end;
  321. function istemp(const ref : treference) : boolean;
  322. begin
  323. { ref.index = R_NO was missing
  324. led to problems with local arrays
  325. with lower bound > 0 (PM) }
  326. istemp:=((ref.base=procinfo.framepointer) and
  327. (ref.index=R_NO) and
  328. (ref.offset<firsttemp));
  329. end;
  330. procedure persistanttemptonormal(pos : longint);
  331. var
  332. hp : ptemprecord;
  333. begin
  334. hp:=templist;
  335. while assigned(hp) do
  336. if (hp^.pos=pos) and (hp^.temptype=tt_persistant) then
  337. begin
  338. {$ifdef EXTDEBUG}
  339. Comment(V_Debug,'temp managment : persistanttemptonormal()'+
  340. ' at pos '+tostr(pos)+ ' found !');
  341. {$endif}
  342. hp^.temptype:=tt_normal;
  343. exit;
  344. end
  345. else
  346. hp:=hp^.next;
  347. {$ifdef EXTDEBUG}
  348. Comment(V_Debug,'temp managment problem : persistanttemptonormal()'+
  349. ' at pos '+tostr(pos)+ ' not found !');
  350. {$endif}
  351. end;
  352. function ungettemp(pos:longint;allowtype:ttemptype):ttemptype;
  353. var
  354. hp,hnext,hprev,hprevfree : ptemprecord;
  355. begin
  356. ungettemp:=tt_none;
  357. hp:=templist;
  358. hprev:=nil;
  359. hprevfree:=nil;
  360. while assigned(hp) do
  361. begin
  362. if (hp^.pos=pos) then
  363. begin
  364. { check type }
  365. if hp^.temptype<>allowtype then
  366. begin
  367. ungettemp:=hp^.temptype;
  368. exit;
  369. end;
  370. exprasmlist^.concat(new(paitempalloc,dealloc(hp^.pos,hp^.size)));
  371. { set this block to free }
  372. hp^.temptype:=tt_free;
  373. { Update tempfreelist }
  374. if assigned(hprevfree) then
  375. begin
  376. { Connect with previous? }
  377. if assigned(hprev) and (hprev^.temptype=tt_free) then
  378. begin
  379. inc(hprev^.size,hp^.size);
  380. hprev^.next:=hp^.next;
  381. dispose(hp);
  382. hp:=hprev;
  383. end
  384. else
  385. hprevfree^.nextfree:=hp;
  386. end
  387. else
  388. begin
  389. hp^.nextfree:=tempfreelist;
  390. tempfreelist:=hp;
  391. end;
  392. { Next block free ? Yes, then concat }
  393. hnext:=hp^.next;
  394. if assigned(hnext) and (hnext^.temptype=tt_free) then
  395. begin
  396. inc(hp^.size,hnext^.size);
  397. hp^.nextfree:=hnext^.nextfree;
  398. hp^.next:=hnext^.next;
  399. dispose(hnext);
  400. end;
  401. exit;
  402. end;
  403. if (hp^.temptype=tt_free) then
  404. hprevfree:=hp;
  405. hprev:=hp;
  406. hp:=hp^.next;
  407. end;
  408. ungettemp:=tt_none;
  409. end;
  410. procedure ungetpersistanttemp(pos : longint;size : longint);
  411. begin
  412. {$ifdef EXTDEBUG}
  413. if ungettemp(pos,tt_persistant)<>tt_persistant then
  414. Comment(V_Warning,'temp managment problem : ungetpersistanttemp()'+
  415. ' at pos '+tostr(pos)+ ' not found !');
  416. {$else}
  417. ungettemp(pos,tt_persistant);
  418. {$endif}
  419. end;
  420. procedure ungetiftemp(const ref : treference);
  421. var
  422. tt : ttemptype;
  423. begin
  424. if istemp(ref) then
  425. begin
  426. { first check if ansistring }
  427. if ungetiftempansi(ref) then
  428. exit;
  429. tt:=ungettemp(ref.offset,tt_normal);
  430. {$ifdef EXTDEBUG}
  431. if tt=tt_persistant then
  432. Comment(V_Debug,'temp at pos '+tostr(ref.offset)+ ' not released because persistant!');
  433. if tt=tt_none then
  434. Comment(V_Warning,'temp not found for release at offset '+tostr(ref.offset));
  435. {$endif}
  436. end;
  437. end;
  438. procedure inittemps;
  439. begin
  440. tempfreelist:=nil;
  441. templist:=nil;
  442. end;
  443. begin
  444. InitTemps;
  445. end.
  446. {
  447. $Log$
  448. Revision 1.22 1999-05-15 21:33:21 peter
  449. * redesigned temp_gen temp allocation so temp allocation for
  450. ansistring works correct. It also does a best fit instead of first fit
  451. Revision 1.21 1999/05/01 13:24:59 peter
  452. * merged nasm compiler
  453. * old asm moved to oldasm/
  454. Revision 1.20 1999/04/19 09:30:48 pierre
  455. + added warning for unreleased ANSI temp
  456. Revision 1.19 1999/04/16 20:44:38 florian
  457. * the boolean operators =;<>;xor with LOC_JUMP and LOC_FLAGS
  458. operands fixed, small things for new ansistring management
  459. Revision 1.18 1999/04/16 14:03:39 pierre
  460. * added paitempalloc for tempansi
  461. Revision 1.17 1999/04/16 11:49:45 peter
  462. + tempalloc
  463. + -at to show temp alloc info in .s file
  464. Revision 1.16 1999/04/14 09:10:46 peter
  465. * fixed tempansi which set wrong pos in free temp
  466. Revision 1.15 1999/04/09 13:05:45 pierre
  467. * Minenumsize=1 under TEST_ENUMSIZE cond because buggy
  468. Revision 1.14 1999/04/09 09:55:20 peter
  469. * typo fixed
  470. Revision 1.13 1999/04/09 08:39:20 peter
  471. * fixed reuse position
  472. Revision 1.12 1999/04/08 23:52:59 pierre
  473. + tempansilist and gettempansistringreference
  474. Revision 1.11 1999/04/08 20:59:44 florian
  475. * fixed problem with default properties which are a class
  476. * case bug (from the mailing list with -O2) fixed, the
  477. distance of the case labels can be greater than the positive
  478. range of a longint => it is now a dword for fpc
  479. Revision 1.10 1999/04/06 11:19:49 peter
  480. * fixed temp reuse
  481. Revision 1.9 1999/02/22 02:15:56 peter
  482. * updates for ag386bin
  483. Revision 1.8 1999/02/11 09:35:19 pierre
  484. * ExtDebug conditionnal infinite loop on temp problem removed
  485. Revision 1.7 1999/02/02 23:52:33 florian
  486. * problem with calls to method pointers in methods fixed
  487. - double ansistrings temp management removed
  488. Revision 1.6 1999/01/15 11:34:23 pierre
  489. + better info for temp allocation debugging
  490. Revision 1.5 1998/11/30 09:43:24 pierre
  491. * some range check bugs fixed (still not working !)
  492. + added DLL writing support for win32 (also accepts variables)
  493. + TempAnsi for code that could be used for Temporary ansi strings
  494. handling
  495. Revision 1.4 1998/10/09 08:56:32 pierre
  496. * several memory leaks fixed
  497. Revision 1.3 1998/07/16 08:01:42 pierre
  498. * small bug correction due to newinput
  499. (only with tempdebug conditionnal)
  500. Revision 1.2 1998/07/10 10:51:05 peter
  501. * m68k updates
  502. Revision 1.1 1998/06/08 16:07:41 pierre
  503. * temp_gen contains all temporary var functions
  504. (processor independent)
  505. }