temp_gen.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. {
  2. $Id$
  3. Copyright (C) 1998-2000 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. cpubase,cpuinfo,cobjects,globals,tree,hcodegen,verbose,files,aasm;
  22. type
  23. ttemptype = (tt_none,tt_free,tt_normal,tt_persistant,tt_ansistring,tt_freeansistring,tt_widestring,tt_freewidestring);
  24. ttemptypeset = set of ttemptype;
  25. ptemprecord = ^ttemprecord;
  26. ttemprecord = record
  27. temptype : ttemptype;
  28. pos : longint;
  29. size : longint;
  30. next : ptemprecord;
  31. nextfree : ptemprecord; { for faster freeblock checking }
  32. {$ifdef EXTDEBUG}
  33. posinfo,
  34. releaseposinfo : tfileposinfo;
  35. {$endif}
  36. end;
  37. var
  38. { contains all temps }
  39. templist : ptemprecord;
  40. { contains all free temps using nextfree links }
  41. tempfreelist : ptemprecord;
  42. { Offsets of the first/last temp }
  43. firsttemp,
  44. lasttemp : longint;
  45. { generates temporary variables }
  46. procedure resettempgen;
  47. procedure setfirsttemp(l : longint);
  48. function gettempsize : longint;
  49. function newtempofsize(size : longint) : longint;
  50. function gettempofsize(size : longint) : longint;
  51. { special call for inlined procedures }
  52. function gettempofsizepersistant(size : longint) : longint;
  53. { for parameter func returns }
  54. procedure normaltemptopersistant(pos : longint);
  55. procedure persistanttemptonormal(pos : longint);
  56. {procedure ungettemp(pos : longint;size : longint);}
  57. procedure ungetpersistanttemp(pos : longint);
  58. procedure gettempofsizereference(l : longint;var ref : treference);
  59. function istemp(const ref : treference) : boolean;
  60. procedure ungetiftemp(const ref : treference);
  61. function ungetiftempansi(const ref : treference) : boolean;
  62. procedure gettempansistringreference(var ref : treference);
  63. implementation
  64. uses
  65. scanner,systems
  66. {$ifdef i386}
  67. ,cgai386
  68. {$endif i386}
  69. {$ifdef m68k}
  70. ,cga68k
  71. {$endif m68k}
  72. ;
  73. procedure resettempgen;
  74. var
  75. hp : ptemprecord;
  76. begin
  77. { Clear the old templist }
  78. while assigned(templist) do
  79. begin
  80. {$ifdef EXTDEBUG}
  81. case templist^.temptype of
  82. tt_normal,
  83. tt_persistant :
  84. Comment(V_Warning,'temporary assignment of size '+
  85. tostr(templist^.size)+' from pos '+tostr(templist^.posinfo.line)+
  86. ':'+tostr(templist^.posinfo.column)+
  87. ' at pos '+tostr(templist^.pos)+
  88. ' not freed at the end of the procedure');
  89. tt_ansistring :
  90. Comment(V_Warning,'temporary ANSI 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. end;
  96. {$endif}
  97. hp:=templist;
  98. templist:=hp^.next;
  99. dispose(hp);
  100. end;
  101. templist:=nil;
  102. tempfreelist:=nil;
  103. firsttemp:=0;
  104. lasttemp:=0;
  105. end;
  106. procedure setfirsttemp(l : longint);
  107. begin
  108. { this is a negative value normally }
  109. if l < 0 then
  110. Begin
  111. if odd(l) then
  112. Dec(l);
  113. end
  114. else
  115. Begin
  116. if odd(l) then
  117. Inc(l);
  118. end;
  119. firsttemp:=l;
  120. lasttemp:=l;
  121. end;
  122. function newtempofsize(size : longint) : longint;
  123. var
  124. tl : ptemprecord;
  125. begin
  126. { Just extend the temp, everything below has been use
  127. already }
  128. dec(lasttemp,size);
  129. { now we can create the templist entry }
  130. new(tl);
  131. tl^.temptype:=tt_normal;
  132. tl^.pos:=lasttemp;
  133. tl^.size:=size;
  134. tl^.next:=templist;
  135. tl^.nextfree:=nil;
  136. templist:=tl;
  137. newtempofsize:=tl^.pos;
  138. end;
  139. const
  140. lasttempofsize : ptemprecord = nil;
  141. function gettempofsize(size : longint) : longint;
  142. var
  143. tl,
  144. bestslot,bestprev,
  145. hprev,hp : ptemprecord;
  146. bestsize,ofs : longint;
  147. begin
  148. bestprev:=nil;
  149. bestslot:=nil;
  150. tl:=nil;
  151. bestsize:=0;
  152. { Align needed size on 4 bytes }
  153. if (size mod 4)<>0 then
  154. size:=size+(4-(size mod 4));
  155. { First check the tmpfreelist }
  156. if assigned(tempfreelist) then
  157. begin
  158. { Check for a slot with the same size first }
  159. hprev:=nil;
  160. hp:=tempfreelist;
  161. while assigned(hp) do
  162. begin
  163. {$ifdef EXTDEBUG}
  164. if hp^.temptype<>tt_free then
  165. Comment(V_Warning,'Temp in freelist is not set to tt_free');
  166. {$endif}
  167. if hp^.size>=size then
  168. begin
  169. { Slot is the same size, then leave immediatly }
  170. if hp^.size=size then
  171. begin
  172. bestprev:=hprev;
  173. bestslot:=hp;
  174. bestsize:=size;
  175. break;
  176. end
  177. else
  178. begin
  179. if (bestsize=0) or (hp^.size<bestsize) then
  180. begin
  181. bestprev:=hprev;
  182. bestslot:=hp;
  183. bestsize:=hp^.size;
  184. end;
  185. end;
  186. end;
  187. hprev:=hp;
  188. hp:=hp^.nextfree;
  189. end;
  190. end;
  191. { Reuse an old temp ? }
  192. if assigned(bestslot) then
  193. begin
  194. if bestsize=size then
  195. begin
  196. bestslot^.temptype:=tt_normal;
  197. ofs:=bestslot^.pos;
  198. tl:=bestslot;
  199. { Remove from the tempfreelist }
  200. if assigned(bestprev) then
  201. bestprev^.nextfree:=bestslot^.nextfree
  202. else
  203. tempfreelist:=bestslot^.nextfree;
  204. end
  205. else
  206. begin
  207. { Resize the old block }
  208. dec(bestslot^.size,size);
  209. { Create new block and link after bestslot }
  210. new(tl);
  211. tl^.temptype:=tt_normal;
  212. tl^.pos:=bestslot^.pos+bestslot^.size;
  213. ofs:=tl^.pos;
  214. tl^.size:=size;
  215. tl^.nextfree:=nil;
  216. { link the new block }
  217. tl^.next:=bestslot^.next;
  218. bestslot^.next:=tl;
  219. end;
  220. end
  221. else
  222. begin
  223. ofs:=newtempofsize(size);
  224. tl:=templist;
  225. end;
  226. lasttempofsize:=tl;
  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. lasttempofsize^.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:=Align(-lasttemp,target_os.stackalignment);
  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. procedure gettempansistringreference(var ref : treference);
  257. var
  258. foundslot,tl : ptemprecord;
  259. begin
  260. { do a reset, because the reference isn't used }
  261. reset_reference(ref);
  262. ref.base:=procinfo^.framepointer;
  263. { Reuse old ansi slot ? }
  264. foundslot:=nil;
  265. tl:=templist;
  266. while assigned(tl) do
  267. begin
  268. if tl^.temptype=tt_freeansistring then
  269. begin
  270. foundslot:=tl;
  271. {$ifdef EXTDEBUG}
  272. tl^.posinfo:=aktfilepos;
  273. {$endif}
  274. break;
  275. end;
  276. tl:=tl^.next;
  277. end;
  278. if assigned(foundslot) then
  279. begin
  280. foundslot^.temptype:=tt_ansistring;
  281. ref.offset:=foundslot^.pos;
  282. { we're reusing an old slot then set the function result to true
  283. so that we can call a decr_ansistr }
  284. { we never know if a slot was used previously:
  285. imagine a loop: in the first run the slot wasn't used
  286. while in later runs it is reused (FK)
  287. gettempansistringreference:=true;
  288. }
  289. end
  290. else
  291. begin
  292. ref.offset:=newtempofsize(target_os.size_of_pointer);
  293. {$ifdef EXTDEBUG}
  294. templist^.posinfo:=aktfilepos;
  295. {$endif}
  296. templist^.temptype:=tt_ansistring;
  297. { set result to false, we don't need an decr_ansistr
  298. gettempansistringreference:=true;
  299. Not necessary, the above (FK)
  300. }
  301. end;
  302. exprasmlist^.concat(new(paitempalloc,alloc(ref.offset,target_os.size_of_pointer)));
  303. end;
  304. function ungetiftempansi(const ref : treference) : boolean;
  305. var
  306. tl : ptemprecord;
  307. begin
  308. ungetiftempansi:=false;
  309. tl:=templist;
  310. while assigned(tl) do
  311. begin
  312. if tl^.pos=ref.offset then
  313. begin
  314. if tl^.temptype=tt_ansistring then
  315. begin
  316. tl^.temptype:=tt_freeansistring;
  317. ungetiftempansi:=true;
  318. exprasmlist^.concat(new(paitempalloc,dealloc(tl^.pos,tl^.size)));
  319. exit;
  320. {$ifdef EXTDEBUG}
  321. end
  322. else if (tl^.temptype=tt_freeansistring) then
  323. begin
  324. Comment(V_Debug,'temp ansi managment problem : ungetiftempansi()'+
  325. ' at pos '+tostr(ref.offset)+ ' already free !');
  326. {$endif}
  327. end;
  328. end;
  329. tl:=tl^.next;
  330. end;
  331. end;
  332. function istemp(const ref : treference) : boolean;
  333. begin
  334. { ref.index = R_NO was missing
  335. led to problems with local arrays
  336. with lower bound > 0 (PM) }
  337. istemp:=((ref.base=procinfo^.framepointer) and
  338. {$ifndef alpha}
  339. (ref.index=R_NO) and
  340. {$endif}
  341. (ref.offset<firsttemp));
  342. end;
  343. procedure persistanttemptonormal(pos : longint);
  344. var
  345. hp : ptemprecord;
  346. begin
  347. hp:=templist;
  348. while assigned(hp) do
  349. if (hp^.pos=pos) and (hp^.temptype=tt_persistant) then
  350. begin
  351. {$ifdef EXTDEBUG}
  352. Comment(V_Debug,'temp managment : persistanttemptonormal()'+
  353. ' at pos '+tostr(pos)+ ' found !');
  354. {$endif}
  355. hp^.temptype:=tt_normal;
  356. exit;
  357. end
  358. else
  359. hp:=hp^.next;
  360. {$ifdef EXTDEBUG}
  361. Comment(V_Debug,'temp managment problem : persistanttemptonormal()'+
  362. ' at pos '+tostr(pos)+ ' not found !');
  363. {$endif}
  364. end;
  365. procedure normaltemptopersistant(pos : longint);
  366. var
  367. hp : ptemprecord;
  368. begin
  369. hp:=templist;
  370. while assigned(hp) do
  371. if (hp^.pos=pos) and (hp^.temptype=tt_normal) then
  372. begin
  373. {$ifdef EXTDEBUG}
  374. Comment(V_Debug,'temp managment : normaltemptopersistant()'+
  375. ' at pos '+tostr(pos)+ ' found !');
  376. {$endif}
  377. hp^.temptype:=tt_persistant;
  378. exit;
  379. end
  380. else
  381. hp:=hp^.next;
  382. {$ifdef EXTDEBUG}
  383. Comment(V_Debug,'temp managment problem : normaltemptopersistant()'+
  384. ' at pos '+tostr(pos)+ ' not found !');
  385. {$endif}
  386. end;
  387. function ungettemp(pos:longint;allowtype:ttemptype):ttemptype;
  388. var
  389. hp,hnext,hprev,hprevfree : ptemprecord;
  390. begin
  391. ungettemp:=tt_none;
  392. hp:=templist;
  393. hprev:=nil;
  394. hprevfree:=nil;
  395. while assigned(hp) do
  396. begin
  397. if (hp^.pos=pos) then
  398. begin
  399. { check type }
  400. ungettemp:=hp^.temptype;
  401. if hp^.temptype<>allowtype then
  402. begin
  403. exit;
  404. end;
  405. exprasmlist^.concat(new(paitempalloc,dealloc(hp^.pos,hp^.size)));
  406. { set this block to free }
  407. hp^.temptype:=tt_free;
  408. { Update tempfreelist }
  409. if assigned(hprevfree) then
  410. begin
  411. { Connect with previous? }
  412. if assigned(hprev) and (hprev^.temptype=tt_free) then
  413. begin
  414. inc(hprev^.size,hp^.size);
  415. hprev^.next:=hp^.next;
  416. dispose(hp);
  417. hp:=hprev;
  418. end
  419. else
  420. hprevfree^.nextfree:=hp;
  421. end
  422. else
  423. begin
  424. hp^.nextfree:=tempfreelist;
  425. tempfreelist:=hp;
  426. end;
  427. { Next block free ? Yes, then concat }
  428. hnext:=hp^.next;
  429. if assigned(hnext) and (hnext^.temptype=tt_free) then
  430. begin
  431. inc(hp^.size,hnext^.size);
  432. hp^.nextfree:=hnext^.nextfree;
  433. hp^.next:=hnext^.next;
  434. dispose(hnext);
  435. end;
  436. exit;
  437. end;
  438. if (hp^.temptype=tt_free) then
  439. hprevfree:=hp;
  440. hprev:=hp;
  441. hp:=hp^.next;
  442. end;
  443. ungettemp:=tt_none;
  444. end;
  445. procedure ungetpersistanttemp(pos : longint);
  446. begin
  447. {$ifdef EXTDEBUG}
  448. if ungettemp(pos,tt_persistant)<>tt_persistant then
  449. Comment(V_Warning,'temp managment problem : ungetpersistanttemp()'+
  450. ' at pos '+tostr(pos)+ ' not found !');
  451. {$else}
  452. ungettemp(pos,tt_persistant);
  453. {$endif}
  454. end;
  455. procedure ungetiftemp(const ref : treference);
  456. {$ifdef EXTDEBUG}
  457. var
  458. tt : ttemptype;
  459. {$endif}
  460. begin
  461. if istemp(ref) then
  462. begin
  463. { first check if ansistring }
  464. if ungetiftempansi(ref) then
  465. exit;
  466. {$ifndef EXTDEBUG}
  467. ungettemp(ref.offset,tt_normal);
  468. {$else}
  469. tt:=ungettemp(ref.offset,tt_normal);
  470. if tt=tt_persistant then
  471. Comment(V_Debug,'temp at pos '+tostr(ref.offset)+ ' not released because persistant!');
  472. if tt=tt_none then
  473. Comment(V_Warning,'temp not found for release at offset '+tostr(ref.offset));
  474. {$endif}
  475. end;
  476. end;
  477. procedure inittemps;
  478. begin
  479. tempfreelist:=nil;
  480. templist:=nil;
  481. end;
  482. begin
  483. InitTemps;
  484. end.
  485. {
  486. $Log$
  487. Revision 1.42 2000-02-09 13:23:08 peter
  488. * log truncated
  489. Revision 1.41 2000/01/07 01:14:47 peter
  490. * updated copyright to 2000
  491. Revision 1.40 1999/12/19 23:53:14 pierre
  492. * problem with persistant temp fixed
  493. Revision 1.39 1999/12/01 12:42:33 peter
  494. * fixed bug 698
  495. * removed some notes about unused vars
  496. Revision 1.38 1999/11/06 14:34:31 peter
  497. * truncated log to 20 revs
  498. Revision 1.37 1999/09/27 23:45:02 peter
  499. * procinfo is now a pointer
  500. * support for result setting in sub procedure
  501. Revision 1.36 1999/09/26 13:26:08 florian
  502. * exception patch of Romio nevertheless the excpetion handling
  503. needs some corections regarding register saving
  504. * gettempansistring is again a procedure
  505. Revision 1.35 1999/09/16 11:34:59 pierre
  506. * typo correction
  507. Revision 1.34 1999/08/04 00:23:46 florian
  508. * renamed i386asm and i386base to cpuasm and cpubase
  509. Revision 1.33 1999/08/02 00:34:06 michael
  510. * alpha has no index
  511. }