temp_gen.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. {$i defines.inc}
  20. interface
  21. uses
  22. cpubase,cpuinfo,cobjects,globals,
  23. hcodegen,verbose,fmodule,aasm;
  24. {$ifdef newcg}
  25. const
  26. countusableregint : byte = c_countusableregsint;
  27. countusableregfpu : byte = c_countusableregsfpu;
  28. countusableregmm : byte = c_countusableregsmm;
  29. {$endif newcg}
  30. type
  31. ttemptype = (tt_none,tt_free,tt_normal,tt_persistant,
  32. tt_ansistring,tt_freeansistring,tt_widestring,tt_freewidestring,
  33. tt_interfacecom,tt_freeinterfacecom);
  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. procedure gettempofsizereferencepersistant(l : longint;var ref : treference);
  64. { for parameter func returns }
  65. procedure normaltemptopersistant(pos : longint);
  66. procedure persistanttemptonormal(pos : longint);
  67. {procedure ungettemp(pos : longint;size : longint);}
  68. procedure ungetpersistanttemp(pos : longint);
  69. procedure ungetpersistanttempreference(const ref : treference);
  70. procedure gettempofsizereference(l : longint;var ref : treference);
  71. function istemp(const ref : treference) : boolean;
  72. procedure ungetiftemp(const ref : treference);
  73. function getsizeoftemp(const ref: treference): longint;
  74. function ungetiftempansi(const ref : treference) : boolean;
  75. procedure gettempansistringreference(var ref : treference);
  76. function ungetiftempintfcom(const ref : treference) : boolean;
  77. procedure gettempintfcomreference(var ref : treference);
  78. implementation
  79. uses
  80. cutils,systems;
  81. procedure resettempgen;
  82. var
  83. hp : ptemprecord;
  84. begin
  85. { Clear the old templist }
  86. while assigned(templist) do
  87. begin
  88. {$ifdef EXTDEBUG}
  89. case tempList^.temptype of
  90. tt_normal,
  91. tt_persistant :
  92. Comment(V_Warning,'temporary assignment of size '+
  93. tostr(templist^.size)+' from pos '+tostr(templist^.posinfo.line)+
  94. ':'+tostr(templist^.posinfo.column)+
  95. ' at pos '+tostr(templist^.pos)+
  96. ' not freed at the end of the procedure');
  97. tt_ansistring :
  98. Comment(V_Warning,'temporary ANSI assignment of size '+
  99. tostr(templist^.size)+' from pos '+tostr(templist^.posinfo.line)+
  100. ':'+tostr(templist^.posinfo.column)+
  101. ' at pos '+tostr(templist^.pos)+
  102. ' not freed at the end of the procedure');
  103. end;
  104. {$endif}
  105. hp:=templist;
  106. templist:=hp^.next;
  107. dispose(hp);
  108. end;
  109. templist:=nil;
  110. tempfreelist:=nil;
  111. firsttemp:=0;
  112. lasttemp:=0;
  113. end;
  114. procedure setfirsttemp(l : longint);
  115. begin
  116. { this is a negative value normally }
  117. if l < 0 then
  118. Begin
  119. if odd(l) then
  120. Dec(l);
  121. end
  122. else
  123. Begin
  124. if odd(l) then
  125. Inc(l);
  126. end;
  127. firsttemp:=l;
  128. lasttemp:=l;
  129. end;
  130. function newtempofsize(size : longint) : longint;
  131. var
  132. tl : ptemprecord;
  133. begin
  134. { Just extend the temp, everything below has been use
  135. already }
  136. dec(lasttemp,size);
  137. { now we can create the templist entry }
  138. new(tl);
  139. tl^.temptype:=tt_normal;
  140. tl^.pos:=lasttemp;
  141. tl^.size:=size;
  142. tl^.next:=templist;
  143. tl^.nextfree:=nil;
  144. templist:=tl;
  145. newtempofsize:=tl^.pos;
  146. end;
  147. const
  148. lasttempofsize : ptemprecord = nil;
  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. tl:=templist;
  233. end;
  234. lasttempofsize:=tl;
  235. {$ifdef EXTDEBUG}
  236. tl^.posinfo:=aktfilepos;
  237. {$endif}
  238. exprasmList.concat(Taitempalloc.alloc(ofs,size));
  239. gettempofsize:=ofs;
  240. end;
  241. function gettempofsizepersistant(size : longint) : longint;
  242. var
  243. l : longint;
  244. begin
  245. l:=gettempofsize(size);
  246. lasttempofsize^.temptype:=tt_persistant;
  247. {$ifdef EXTDEBUG}
  248. Comment(V_Debug,'temp managment : call to gettempofsizepersistant()'+
  249. ' with size '+tostr(size)+' returned '+tostr(l));
  250. {$endif}
  251. gettempofsizepersistant:=l;
  252. end;
  253. function gettempsize : longint;
  254. var
  255. _align : longint;
  256. begin
  257. { align to 4 bytes at least
  258. otherwise all those subl $2,%esp are meaningless PM }
  259. _align:=target_os.stackalignment;
  260. if _align<4 then
  261. _align:=4;
  262. gettempsize:=Align(-lasttemp,_align);
  263. end;
  264. procedure gettempofsizereference(l : longint;var ref : treference);
  265. begin
  266. { do a reset, because the reference isn't used }
  267. reset_reference(ref);
  268. ref.offset:=gettempofsize(l);
  269. ref.base:=procinfo^.framepointer;
  270. end;
  271. procedure gettempofsizereferencepersistant(l : longint;var ref : treference);
  272. begin
  273. { do a reset, because the reference isn't used }
  274. reset_reference(ref);
  275. ref.offset:=gettempofsizepersistant(l);
  276. ref.base:=procinfo^.framepointer;
  277. end;
  278. procedure gettemppointerreferencefortype(var ref : treference; const usedtype, freetype: ttemptype);
  279. var
  280. foundslot,tl : ptemprecord;
  281. begin
  282. { do a reset, because the reference isn't used }
  283. reset_reference(ref);
  284. ref.base:=procinfo^.framepointer;
  285. { Reuse old slot ? }
  286. foundslot:=nil;
  287. tl:=templist;
  288. while assigned(tl) do
  289. begin
  290. if tl^.temptype=freetype then
  291. begin
  292. foundslot:=tl;
  293. {$ifdef EXTDEBUG}
  294. tl^.posinfo:=aktfilepos;
  295. {$endif}
  296. break;
  297. end;
  298. tl:=tl^.next;
  299. end;
  300. if assigned(foundslot) then
  301. begin
  302. foundslot^.temptype:=usedtype;
  303. ref.offset:=foundslot^.pos;
  304. end
  305. else
  306. begin
  307. ref.offset:=newtempofsize(target_os.size_of_pointer);
  308. {$ifdef EXTDEBUG}
  309. templist^.posinfo:=aktfilepos;
  310. {$endif}
  311. templist^.temptype:=usedtype;
  312. end;
  313. exprasmList.concat(Taitempalloc.alloc(ref.offset,target_os.size_of_pointer));
  314. end;
  315. function ungettemppointeriftype(const ref : treference; const usedtype, freetype: ttemptype) : boolean;
  316. var
  317. tl : ptemprecord;
  318. begin
  319. ungettemppointeriftype:=false;
  320. tl:=templist;
  321. while assigned(tl) do
  322. begin
  323. if tl^.pos=ref.offset then
  324. begin
  325. if tl^.temptype=usedtype then
  326. begin
  327. tl^.temptype:=freetype;
  328. ungettemppointeriftype:=true;
  329. exprasmList.concat(Taitempalloc.dealloc(tl^.pos,tl^.size));
  330. exit;
  331. {$ifdef EXTDEBUG}
  332. end
  333. else if (tl^.temptype=freetype) then
  334. begin
  335. Comment(V_Debug,'temp managment problem : ungettemppointeriftype()'+
  336. ' at pos '+tostr(ref.offset)+ ' already free !');
  337. {$endif}
  338. end;
  339. end;
  340. tl:=tl^.next;
  341. end;
  342. end;
  343. procedure gettempansistringreference(var ref : treference);
  344. begin
  345. gettemppointerreferencefortype(ref,tt_ansistring,tt_freeansistring);
  346. end;
  347. function ungetiftempansi(const ref : treference) : boolean;
  348. begin
  349. ungetiftempansi:=ungettemppointeriftype(ref,tt_ansistring,tt_freeansistring);
  350. end;
  351. procedure gettempintfcomreference(var ref : treference);
  352. begin
  353. gettemppointerreferencefortype(ref,tt_interfacecom,tt_freeinterfacecom);
  354. end;
  355. function ungetiftempintfcom(const ref : treference) : boolean;
  356. begin
  357. ungetiftempintfcom:=ungettemppointeriftype(ref,tt_ansistring,tt_freeansistring);
  358. end;
  359. function istemp(const ref : treference) : boolean;
  360. begin
  361. { ref.index = R_NO was missing
  362. led to problems with local arrays
  363. with lower bound > 0 (PM) }
  364. istemp:=((ref.base=procinfo^.framepointer) and
  365. {$ifdef i386}
  366. (ref.index=R_NO) and
  367. {$endif}
  368. (ref.offset<firsttemp));
  369. end;
  370. procedure persistanttemptonormal(pos : longint);
  371. var
  372. hp : ptemprecord;
  373. begin
  374. hp:=templist;
  375. while assigned(hp) do
  376. if (hp^.pos=pos) and (hp^.temptype=tt_persistant) then
  377. begin
  378. {$ifdef EXTDEBUG}
  379. Comment(V_Debug,'temp managment : persistanttemptonormal()'+
  380. ' at pos '+tostr(pos)+ ' found !');
  381. {$endif}
  382. hp^.temptype:=tt_normal;
  383. exit;
  384. end
  385. else
  386. hp:=hp^.next;
  387. {$ifdef EXTDEBUG}
  388. Comment(V_Debug,'temp managment problem : persistanttemptonormal()'+
  389. ' at pos '+tostr(pos)+ ' not found !');
  390. {$endif}
  391. end;
  392. procedure normaltemptopersistant(pos : longint);
  393. var
  394. hp : ptemprecord;
  395. begin
  396. hp:=templist;
  397. while assigned(hp) do
  398. if (hp^.pos=pos) and (hp^.temptype=tt_normal) then
  399. begin
  400. {$ifdef EXTDEBUG}
  401. Comment(V_Debug,'temp managment : normaltemptopersistant()'+
  402. ' at pos '+tostr(pos)+ ' found !');
  403. {$endif}
  404. hp^.temptype:=tt_persistant;
  405. exit;
  406. end
  407. else
  408. hp:=hp^.next;
  409. {$ifdef EXTDEBUG}
  410. Comment(V_Debug,'temp managment problem : normaltemptopersistant()'+
  411. ' at pos '+tostr(pos)+ ' not found !');
  412. {$endif}
  413. end;
  414. function ungettemp(pos:longint;allowtype:ttemptype):ttemptype;
  415. var
  416. hp,hnext,hprev,hprevfree : ptemprecord;
  417. begin
  418. ungettemp:=tt_none;
  419. hp:=templist;
  420. hprev:=nil;
  421. hprevfree:=nil;
  422. while assigned(hp) do
  423. begin
  424. if (hp^.pos=pos) then
  425. begin
  426. { check type }
  427. ungettemp:=hp^.temptype;
  428. if hp^.temptype<>allowtype then
  429. begin
  430. exit;
  431. end;
  432. exprasmList.concat(Taitempalloc.dealloc(hp^.pos,hp^.size));
  433. { set this block to free }
  434. hp^.temptype:=tt_free;
  435. { Update tempfreelist }
  436. if assigned(hprevfree) then
  437. begin
  438. { Connect with previous? }
  439. if assigned(hprev) and (hprev^.temptype=tt_free) then
  440. begin
  441. inc(hprev^.size,hp^.size);
  442. hprev^.next:=hp^.next;
  443. dispose(hp);
  444. hp:=hprev;
  445. end
  446. else
  447. hprevfree^.nextfree:=hp;
  448. end
  449. else
  450. begin
  451. hp^.nextfree:=tempfreelist;
  452. tempfreelist:=hp;
  453. end;
  454. { Next block free ? Yes, then concat }
  455. hnext:=hp^.next;
  456. if assigned(hnext) and (hnext^.temptype=tt_free) then
  457. begin
  458. inc(hp^.size,hnext^.size);
  459. hp^.nextfree:=hnext^.nextfree;
  460. hp^.next:=hnext^.next;
  461. dispose(hnext);
  462. end;
  463. exit;
  464. end;
  465. if (hp^.temptype=tt_free) then
  466. hprevfree:=hp;
  467. hprev:=hp;
  468. hp:=hp^.next;
  469. end;
  470. ungettemp:=tt_none;
  471. end;
  472. function getsizeoftemp(const ref: treference): longint;
  473. var
  474. hp : ptemprecord;
  475. begin
  476. hp:=templist;
  477. while assigned(hp) do
  478. begin
  479. if (hp^.pos=ref.offset) then
  480. begin
  481. getsizeoftemp := hp^.size;
  482. exit;
  483. end;
  484. hp := hp^.next;
  485. end;
  486. getsizeoftemp := -1;
  487. end;
  488. procedure ungetpersistanttemp(pos : longint);
  489. begin
  490. {$ifdef EXTDEBUG}
  491. if ungettemp(pos,tt_persistant)<>tt_persistant then
  492. Comment(V_Warning,'temp managment problem : ungetpersistanttemp()'+
  493. ' at pos '+tostr(pos)+ ' not found !');
  494. {$else}
  495. ungettemp(pos,tt_persistant);
  496. {$endif}
  497. end;
  498. procedure ungetpersistanttempreference(const ref : treference);
  499. begin
  500. ungetpersistanttemp(ref.offset);
  501. end;
  502. procedure ungetiftemp(const ref : treference);
  503. {$ifdef EXTDEBUG}
  504. var
  505. tt : ttemptype;
  506. {$endif}
  507. begin
  508. if istemp(ref) then
  509. begin
  510. { first check if ansistring }
  511. if ungetiftempansi(ref) then
  512. exit;
  513. {$ifndef EXTDEBUG}
  514. ungettemp(ref.offset,tt_normal);
  515. {$else}
  516. tt:=ungettemp(ref.offset,tt_normal);
  517. if tt=tt_persistant then
  518. Comment(V_Debug,'temp at pos '+tostr(ref.offset)+ ' not released because persistant!');
  519. if tt=tt_none then
  520. Comment(V_Warning,'temp not found for release at offset '+tostr(ref.offset));
  521. {$endif}
  522. end;
  523. end;
  524. procedure inittemps;
  525. begin
  526. tempfreelist:=nil;
  527. templist:=nil;
  528. end;
  529. begin
  530. InitTemps;
  531. end.
  532. {
  533. $Log$
  534. Revision 1.11 2001-01-05 17:36:58 florian
  535. * the info about exception frames is stored now on the stack
  536. instead on the heap
  537. Revision 1.10 2000/12/31 11:04:43 jonas
  538. + sizeoftemp() function
  539. Revision 1.9 2000/12/25 00:07:30 peter
  540. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  541. tlinkedlist objects)
  542. Revision 1.8 2000/11/30 22:16:50 florian
  543. * moved to i386
  544. Revision 1.7 2000/11/29 00:30:42 florian
  545. * unused units removed from uses clause
  546. * some changes for widestrings
  547. Revision 1.6 2000/11/04 14:25:22 florian
  548. + merged Attila's changes for interfaces, not tested yet
  549. Revision 1.5 2000/09/30 16:08:45 peter
  550. * more cg11 updates
  551. Revision 1.4 2000/09/24 15:06:31 peter
  552. * use defines.inc
  553. Revision 1.3 2000/08/27 16:11:55 peter
  554. * moved some util functions from globals,cobjects to cutils
  555. * splitted files into finput,fmodule
  556. Revision 1.2 2000/07/13 11:32:52 michael
  557. + removed logs
  558. }