temp_gen.pas 19 KB

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