temp_gen.pas 19 KB

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