temp_gen.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. tl:=bestslot;
  205. { Remove from the tempfreelist }
  206. if assigned(bestprev) then
  207. bestprev^.nextfree:=bestslot^.nextfree
  208. else
  209. tempfreelist:=bestslot^.nextfree;
  210. end
  211. else
  212. begin
  213. { Resize the old block }
  214. dec(bestslot^.size,size);
  215. { Create new block and link after bestslot }
  216. new(tl);
  217. tl^.temptype:=tt_normal;
  218. tl^.pos:=bestslot^.pos+bestslot^.size;
  219. tl^.size:=size;
  220. tl^.nextfree:=nil;
  221. { link the new block }
  222. tl^.next:=bestslot^.next;
  223. bestslot^.next:=tl;
  224. end;
  225. end
  226. else
  227. begin
  228. ofs:=newtempofsize(size);
  229. {$ifdef EXTDEBUG}
  230. tl:=templist;
  231. {$endif}
  232. end;
  233. {$ifdef EXTDEBUG}
  234. tl^.posinfo:=aktfilepos;
  235. {$endif}
  236. exprasmlist^.concat(new(paitempalloc,alloc(ofs,size)));
  237. gettempofsize:=ofs;
  238. end;
  239. function gettempofsizepersistant(size : longint) : longint;
  240. var
  241. l : longint;
  242. begin
  243. l:=gettempofsize(size);
  244. templist^.temptype:=tt_persistant;
  245. {$ifdef EXTDEBUG}
  246. Comment(V_Debug,'temp managment : call to gettempofsizepersistant()'+
  247. ' with size '+tostr(size)+' returned '+tostr(l));
  248. {$endif}
  249. gettempofsizepersistant:=l;
  250. end;
  251. function gettempsize : longint;
  252. begin
  253. gettempsize:=-lasttemp;
  254. end;
  255. procedure gettempofsizereference(l : longint;var ref : treference);
  256. begin
  257. { do a reset, because the reference isn't used }
  258. reset_reference(ref);
  259. ref.offset:=gettempofsize(l);
  260. ref.base:=procinfo.framepointer;
  261. end;
  262. function gettempansioffset : longint;
  263. var
  264. ofs : longint;
  265. foundslot,tl : ptemprecord;
  266. begin
  267. { Reuse old ansi slot ? }
  268. foundslot:=nil;
  269. tl:=templist;
  270. while assigned(tl) do
  271. begin
  272. if tl^.temptype=tt_freeansistring then
  273. begin
  274. foundslot:=tl;
  275. break;
  276. end;
  277. tl:=tl^.next;
  278. end;
  279. if assigned(foundslot) then
  280. begin
  281. foundslot^.temptype:=tt_ansistring;
  282. ofs:=foundslot^.pos;
  283. end
  284. else
  285. begin
  286. ofs:=newtempofsize(target_os.size_of_pointer);
  287. templist^.temptype:=tt_ansistring;
  288. end;
  289. exprasmlist^.concat(new(paitempalloc,alloc(ofs,target_os.size_of_pointer)));
  290. gettempansioffset:=ofs;
  291. end;
  292. procedure gettempansistringreference(var ref : treference);
  293. begin
  294. { do a reset, because the reference isn't used }
  295. reset_reference(ref);
  296. ref.offset:=gettempansioffset;
  297. ref.base:=procinfo.framepointer;
  298. end;
  299. function ungetiftempansi(const ref : treference) : boolean;
  300. var
  301. tl : ptemprecord;
  302. begin
  303. ungetiftempansi:=false;
  304. tl:=templist;
  305. while assigned(tl) do
  306. begin
  307. if tl^.pos=ref.offset then
  308. begin
  309. if tl^.temptype=tt_ansistring then
  310. begin
  311. tl^.temptype:=tt_freeansistring;
  312. ungetiftempansi:=true;
  313. exprasmlist^.concat(new(paitempalloc,dealloc(tl^.pos,tl^.size)));
  314. exit;
  315. {$ifdef EXTDEBUG}
  316. end
  317. else
  318. begin
  319. Comment(V_Debug,'temp ansi managment problem : ungetiftempansi()'+
  320. ' at pos '+tostr(ref.offset)+ ' already free !');
  321. {$endif}
  322. end;
  323. end;
  324. tl:=tl^.next;
  325. end;
  326. end;
  327. function istemp(const ref : treference) : boolean;
  328. begin
  329. { ref.index = R_NO was missing
  330. led to problems with local arrays
  331. with lower bound > 0 (PM) }
  332. istemp:=((ref.base=procinfo.framepointer) and
  333. (ref.index=R_NO) and
  334. (ref.offset<firsttemp));
  335. end;
  336. procedure persistanttemptonormal(pos : longint);
  337. var
  338. hp : ptemprecord;
  339. begin
  340. hp:=templist;
  341. while assigned(hp) do
  342. if (hp^.pos=pos) and (hp^.temptype=tt_persistant) then
  343. begin
  344. {$ifdef EXTDEBUG}
  345. Comment(V_Debug,'temp managment : persistanttemptonormal()'+
  346. ' at pos '+tostr(pos)+ ' found !');
  347. {$endif}
  348. hp^.temptype:=tt_normal;
  349. exit;
  350. end
  351. else
  352. hp:=hp^.next;
  353. {$ifdef EXTDEBUG}
  354. Comment(V_Debug,'temp managment problem : persistanttemptonormal()'+
  355. ' at pos '+tostr(pos)+ ' not found !');
  356. {$endif}
  357. end;
  358. function ungettemp(pos:longint;allowtype:ttemptype):ttemptype;
  359. var
  360. hp,hnext,hprev,hprevfree : ptemprecord;
  361. begin
  362. ungettemp:=tt_none;
  363. hp:=templist;
  364. hprev:=nil;
  365. hprevfree:=nil;
  366. while assigned(hp) do
  367. begin
  368. if (hp^.pos=pos) then
  369. begin
  370. { check type }
  371. ungettemp:=hp^.temptype;
  372. if hp^.temptype<>allowtype then
  373. begin
  374. exit;
  375. end;
  376. exprasmlist^.concat(new(paitempalloc,dealloc(hp^.pos,hp^.size)));
  377. { set this block to free }
  378. hp^.temptype:=tt_free;
  379. { Update tempfreelist }
  380. if assigned(hprevfree) then
  381. begin
  382. { Connect with previous? }
  383. if assigned(hprev) and (hprev^.temptype=tt_free) then
  384. begin
  385. inc(hprev^.size,hp^.size);
  386. hprev^.next:=hp^.next;
  387. dispose(hp);
  388. hp:=hprev;
  389. end
  390. else
  391. hprevfree^.nextfree:=hp;
  392. end
  393. else
  394. begin
  395. hp^.nextfree:=tempfreelist;
  396. tempfreelist:=hp;
  397. end;
  398. { Next block free ? Yes, then concat }
  399. hnext:=hp^.next;
  400. if assigned(hnext) and (hnext^.temptype=tt_free) then
  401. begin
  402. inc(hp^.size,hnext^.size);
  403. hp^.nextfree:=hnext^.nextfree;
  404. hp^.next:=hnext^.next;
  405. dispose(hnext);
  406. end;
  407. exit;
  408. end;
  409. if (hp^.temptype=tt_free) then
  410. hprevfree:=hp;
  411. hprev:=hp;
  412. hp:=hp^.next;
  413. end;
  414. ungettemp:=tt_none;
  415. end;
  416. procedure ungetpersistanttemp(pos : longint;size : longint);
  417. begin
  418. {$ifdef EXTDEBUG}
  419. if ungettemp(pos,tt_persistant)<>tt_persistant then
  420. Comment(V_Warning,'temp managment problem : ungetpersistanttemp()'+
  421. ' at pos '+tostr(pos)+ ' not found !');
  422. {$else}
  423. ungettemp(pos,tt_persistant);
  424. {$endif}
  425. end;
  426. procedure ungetiftemp(const ref : treference);
  427. var
  428. tt : ttemptype;
  429. begin
  430. if istemp(ref) then
  431. begin
  432. { first check if ansistring }
  433. if ungetiftempansi(ref) then
  434. exit;
  435. tt:=ungettemp(ref.offset,tt_normal);
  436. {$ifdef EXTDEBUG}
  437. if tt=tt_persistant then
  438. Comment(V_Debug,'temp at pos '+tostr(ref.offset)+ ' not released because persistant!');
  439. if tt=tt_none then
  440. Comment(V_Warning,'temp not found for release at offset '+tostr(ref.offset));
  441. {$endif}
  442. end;
  443. end;
  444. procedure inittemps;
  445. begin
  446. tempfreelist:=nil;
  447. templist:=nil;
  448. end;
  449. begin
  450. InitTemps;
  451. end.
  452. {
  453. $Log$
  454. Revision 1.24 1999-05-17 21:57:17 florian
  455. * new temporary ansistring handling
  456. Revision 1.23 1999/05/17 12:49:16 pierre
  457. * several problems with EXTDEBUG fixed
  458. Revision 1.22 1999/05/15 21:33:21 peter
  459. * redesigned temp_gen temp allocation so temp allocation for
  460. ansistring works correct. It also does a best fit instead of first fit
  461. Revision 1.21 1999/05/01 13:24:59 peter
  462. * merged nasm compiler
  463. * old asm moved to oldasm/
  464. Revision 1.20 1999/04/19 09:30:48 pierre
  465. + added warning for unreleased ANSI temp
  466. Revision 1.19 1999/04/16 20:44:38 florian
  467. * the boolean operators =;<>;xor with LOC_JUMP and LOC_FLAGS
  468. operands fixed, small things for new ansistring management
  469. Revision 1.18 1999/04/16 14:03:39 pierre
  470. * added paitempalloc for tempansi
  471. Revision 1.17 1999/04/16 11:49:45 peter
  472. + tempalloc
  473. + -at to show temp alloc info in .s file
  474. Revision 1.16 1999/04/14 09:10:46 peter
  475. * fixed tempansi which set wrong pos in free temp
  476. Revision 1.15 1999/04/09 13:05:45 pierre
  477. * Minenumsize=1 under TEST_ENUMSIZE cond because buggy
  478. Revision 1.14 1999/04/09 09:55:20 peter
  479. * typo fixed
  480. Revision 1.13 1999/04/09 08:39:20 peter
  481. * fixed reuse position
  482. Revision 1.12 1999/04/08 23:52:59 pierre
  483. + tempansilist and gettempansistringreference
  484. Revision 1.11 1999/04/08 20:59:44 florian
  485. * fixed problem with default properties which are a class
  486. * case bug (from the mailing list with -O2) fixed, the
  487. distance of the case labels can be greater than the positive
  488. range of a longint => it is now a dword for fpc
  489. Revision 1.10 1999/04/06 11:19:49 peter
  490. * fixed temp reuse
  491. Revision 1.9 1999/02/22 02:15:56 peter
  492. * updates for ag386bin
  493. Revision 1.8 1999/02/11 09:35:19 pierre
  494. * ExtDebug conditionnal infinite loop on temp problem removed
  495. Revision 1.7 1999/02/02 23:52:33 florian
  496. * problem with calls to method pointers in methods fixed
  497. - double ansistrings temp management removed
  498. Revision 1.6 1999/01/15 11:34:23 pierre
  499. + better info for temp allocation debugging
  500. Revision 1.5 1998/11/30 09:43:24 pierre
  501. * some range check bugs fixed (still not working !)
  502. + added DLL writing support for win32 (also accepts variables)
  503. + TempAnsi for code that could be used for Temporary ansi strings
  504. handling
  505. Revision 1.4 1998/10/09 08:56:32 pierre
  506. * several memory leaks fixed
  507. Revision 1.3 1998/07/16 08:01:42 pierre
  508. * small bug correction due to newinput
  509. (only with tempdebug conditionnal)
  510. Revision 1.2 1998/07/10 10:51:05 peter
  511. * m68k updates
  512. Revision 1.1 1998/06/08 16:07:41 pierre
  513. * temp_gen contains all temporary var functions
  514. (processor independent)
  515. }