temp_gen.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. { 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. procedure gettempansistringreference(var ref : treference);
  73. function ungetiftempintfcom(const ref : treference) : boolean;
  74. procedure gettempintfcomreference(var ref : treference);
  75. implementation
  76. uses
  77. cutils,systems;
  78. procedure resettempgen;
  79. var
  80. hp : ptemprecord;
  81. begin
  82. { Clear the old templist }
  83. while assigned(templist) do
  84. begin
  85. {$ifdef EXTDEBUG}
  86. case templist^.temptype of
  87. tt_normal,
  88. tt_persistant :
  89. Comment(V_Warning,'temporary assignment of size '+
  90. tostr(templist^.size)+' from pos '+tostr(templist^.posinfo.line)+
  91. ':'+tostr(templist^.posinfo.column)+
  92. ' at pos '+tostr(templist^.pos)+
  93. ' not freed at the end of the procedure');
  94. tt_ansistring :
  95. Comment(V_Warning,'temporary ANSI assignment of size '+
  96. tostr(templist^.size)+' from pos '+tostr(templist^.posinfo.line)+
  97. ':'+tostr(templist^.posinfo.column)+
  98. ' at pos '+tostr(templist^.pos)+
  99. ' not freed at the end of the procedure');
  100. end;
  101. {$endif}
  102. hp:=templist;
  103. templist:=hp^.next;
  104. dispose(hp);
  105. end;
  106. templist:=nil;
  107. tempfreelist:=nil;
  108. firsttemp:=0;
  109. lasttemp:=0;
  110. end;
  111. procedure setfirsttemp(l : longint);
  112. begin
  113. { this is a negative value normally }
  114. if l < 0 then
  115. Begin
  116. if odd(l) then
  117. Dec(l);
  118. end
  119. else
  120. Begin
  121. if odd(l) then
  122. Inc(l);
  123. end;
  124. firsttemp:=l;
  125. lasttemp:=l;
  126. end;
  127. function newtempofsize(size : longint) : longint;
  128. var
  129. tl : ptemprecord;
  130. begin
  131. { Just extend the temp, everything below has been use
  132. already }
  133. dec(lasttemp,size);
  134. { now we can create the templist entry }
  135. new(tl);
  136. tl^.temptype:=tt_normal;
  137. tl^.pos:=lasttemp;
  138. tl^.size:=size;
  139. tl^.next:=templist;
  140. tl^.nextfree:=nil;
  141. templist:=tl;
  142. newtempofsize:=tl^.pos;
  143. end;
  144. const
  145. lasttempofsize : ptemprecord = nil;
  146. function gettempofsize(size : longint) : longint;
  147. var
  148. tl,
  149. bestslot,bestprev,
  150. hprev,hp : ptemprecord;
  151. bestsize,ofs : longint;
  152. begin
  153. bestprev:=nil;
  154. bestslot:=nil;
  155. tl:=nil;
  156. bestsize:=0;
  157. { Align needed size on 4 bytes }
  158. if (size mod 4)<>0 then
  159. size:=size+(4-(size mod 4));
  160. { First check the tmpfreelist }
  161. if assigned(tempfreelist) then
  162. begin
  163. { Check for a slot with the same size first }
  164. hprev:=nil;
  165. hp:=tempfreelist;
  166. while assigned(hp) do
  167. begin
  168. {$ifdef EXTDEBUG}
  169. if hp^.temptype<>tt_free then
  170. Comment(V_Warning,'Temp in freelist is not set to tt_free');
  171. {$endif}
  172. if hp^.size>=size then
  173. begin
  174. { Slot is the same size, then leave immediatly }
  175. if hp^.size=size then
  176. begin
  177. bestprev:=hprev;
  178. bestslot:=hp;
  179. bestsize:=size;
  180. break;
  181. end
  182. else
  183. begin
  184. if (bestsize=0) or (hp^.size<bestsize) then
  185. begin
  186. bestprev:=hprev;
  187. bestslot:=hp;
  188. bestsize:=hp^.size;
  189. end;
  190. end;
  191. end;
  192. hprev:=hp;
  193. hp:=hp^.nextfree;
  194. end;
  195. end;
  196. { Reuse an old temp ? }
  197. if assigned(bestslot) then
  198. begin
  199. if bestsize=size then
  200. begin
  201. bestslot^.temptype:=tt_normal;
  202. ofs:=bestslot^.pos;
  203. tl:=bestslot;
  204. { Remove from the tempfreelist }
  205. if assigned(bestprev) then
  206. bestprev^.nextfree:=bestslot^.nextfree
  207. else
  208. tempfreelist:=bestslot^.nextfree;
  209. end
  210. else
  211. begin
  212. { Resize the old block }
  213. dec(bestslot^.size,size);
  214. { Create new block and link after bestslot }
  215. new(tl);
  216. tl^.temptype:=tt_normal;
  217. tl^.pos:=bestslot^.pos+bestslot^.size;
  218. ofs:=tl^.pos;
  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. tl:=templist;
  230. end;
  231. lasttempofsize:=tl;
  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. lasttempofsize^.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. var
  252. _align : longint;
  253. begin
  254. { align to 4 bytes at least
  255. otherwise all those subl $2,%esp are meaningless PM }
  256. _align:=target_os.stackalignment;
  257. if _align<4 then
  258. _align:=4;
  259. gettempsize:=Align(-lasttemp,_align);
  260. end;
  261. procedure gettempofsizereference(l : longint;var ref : treference);
  262. begin
  263. { do a reset, because the reference isn't used }
  264. reset_reference(ref);
  265. ref.offset:=gettempofsize(l);
  266. ref.base:=procinfo^.framepointer;
  267. end;
  268. procedure gettemppointerreferencefortype(var ref : treference; const usedtype, freetype: ttemptype);
  269. var
  270. foundslot,tl : ptemprecord;
  271. begin
  272. { do a reset, because the reference isn't used }
  273. reset_reference(ref);
  274. ref.base:=procinfo^.framepointer;
  275. { Reuse old slot ? }
  276. foundslot:=nil;
  277. tl:=templist;
  278. while assigned(tl) do
  279. begin
  280. if tl^.temptype=freetype then
  281. begin
  282. foundslot:=tl;
  283. {$ifdef EXTDEBUG}
  284. tl^.posinfo:=aktfilepos;
  285. {$endif}
  286. break;
  287. end;
  288. tl:=tl^.next;
  289. end;
  290. if assigned(foundslot) then
  291. begin
  292. foundslot^.temptype:=usedtype;
  293. ref.offset:=foundslot^.pos;
  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:=usedtype;
  302. end;
  303. exprasmlist^.concat(new(paitempalloc,alloc(ref.offset,target_os.size_of_pointer)));
  304. end;
  305. function ungettemppointeriftype(const ref : treference; const usedtype, freetype: ttemptype) : boolean;
  306. var
  307. tl : ptemprecord;
  308. begin
  309. ungettemppointeriftype:=false;
  310. tl:=templist;
  311. while assigned(tl) do
  312. begin
  313. if tl^.pos=ref.offset then
  314. begin
  315. if tl^.temptype=usedtype then
  316. begin
  317. tl^.temptype:=freetype;
  318. ungettemppointeriftype:=true;
  319. exprasmlist^.concat(new(paitempalloc,dealloc(tl^.pos,tl^.size)));
  320. exit;
  321. {$ifdef EXTDEBUG}
  322. end
  323. else if (tl^.temptype=freetype) then
  324. begin
  325. Comment(V_Debug,'temp managment problem : ungettemppointeriftype()'+
  326. ' at pos '+tostr(ref.offset)+ ' already free !');
  327. {$endif}
  328. end;
  329. end;
  330. tl:=tl^.next;
  331. end;
  332. end;
  333. procedure gettempansistringreference(var ref : treference);
  334. begin
  335. gettemppointerreferencefortype(ref,tt_ansistring,tt_freeansistring);
  336. end;
  337. function ungetiftempansi(const ref : treference) : boolean;
  338. begin
  339. ungetiftempansi:=ungettemppointeriftype(ref,tt_ansistring,tt_freeansistring);
  340. end;
  341. procedure gettempintfcomreference(var ref : treference);
  342. begin
  343. gettemppointerreferencefortype(ref,tt_interfacecom,tt_freeinterfacecom);
  344. end;
  345. function ungetiftempintfcom(const ref : treference) : boolean;
  346. begin
  347. ungetiftempintfcom:=ungettemppointeriftype(ref,tt_ansistring,tt_freeansistring);
  348. end;
  349. function istemp(const ref : treference) : boolean;
  350. begin
  351. { ref.index = R_NO was missing
  352. led to problems with local arrays
  353. with lower bound > 0 (PM) }
  354. istemp:=((ref.base=procinfo^.framepointer) and
  355. {$ifdef i386}
  356. (ref.index=R_NO) and
  357. {$endif}
  358. (ref.offset<firsttemp));
  359. end;
  360. procedure persistanttemptonormal(pos : longint);
  361. var
  362. hp : ptemprecord;
  363. begin
  364. hp:=templist;
  365. while assigned(hp) do
  366. if (hp^.pos=pos) and (hp^.temptype=tt_persistant) then
  367. begin
  368. {$ifdef EXTDEBUG}
  369. Comment(V_Debug,'temp managment : persistanttemptonormal()'+
  370. ' at pos '+tostr(pos)+ ' found !');
  371. {$endif}
  372. hp^.temptype:=tt_normal;
  373. exit;
  374. end
  375. else
  376. hp:=hp^.next;
  377. {$ifdef EXTDEBUG}
  378. Comment(V_Debug,'temp managment problem : persistanttemptonormal()'+
  379. ' at pos '+tostr(pos)+ ' not found !');
  380. {$endif}
  381. end;
  382. procedure normaltemptopersistant(pos : longint);
  383. var
  384. hp : ptemprecord;
  385. begin
  386. hp:=templist;
  387. while assigned(hp) do
  388. if (hp^.pos=pos) and (hp^.temptype=tt_normal) then
  389. begin
  390. {$ifdef EXTDEBUG}
  391. Comment(V_Debug,'temp managment : normaltemptopersistant()'+
  392. ' at pos '+tostr(pos)+ ' found !');
  393. {$endif}
  394. hp^.temptype:=tt_persistant;
  395. exit;
  396. end
  397. else
  398. hp:=hp^.next;
  399. {$ifdef EXTDEBUG}
  400. Comment(V_Debug,'temp managment problem : normaltemptopersistant()'+
  401. ' at pos '+tostr(pos)+ ' not found !');
  402. {$endif}
  403. end;
  404. function ungettemp(pos:longint;allowtype:ttemptype):ttemptype;
  405. var
  406. hp,hnext,hprev,hprevfree : ptemprecord;
  407. begin
  408. ungettemp:=tt_none;
  409. hp:=templist;
  410. hprev:=nil;
  411. hprevfree:=nil;
  412. while assigned(hp) do
  413. begin
  414. if (hp^.pos=pos) then
  415. begin
  416. { check type }
  417. ungettemp:=hp^.temptype;
  418. if hp^.temptype<>allowtype then
  419. begin
  420. exit;
  421. end;
  422. exprasmlist^.concat(new(paitempalloc,dealloc(hp^.pos,hp^.size)));
  423. { set this block to free }
  424. hp^.temptype:=tt_free;
  425. { Update tempfreelist }
  426. if assigned(hprevfree) then
  427. begin
  428. { Connect with previous? }
  429. if assigned(hprev) and (hprev^.temptype=tt_free) then
  430. begin
  431. inc(hprev^.size,hp^.size);
  432. hprev^.next:=hp^.next;
  433. dispose(hp);
  434. hp:=hprev;
  435. end
  436. else
  437. hprevfree^.nextfree:=hp;
  438. end
  439. else
  440. begin
  441. hp^.nextfree:=tempfreelist;
  442. tempfreelist:=hp;
  443. end;
  444. { Next block free ? Yes, then concat }
  445. hnext:=hp^.next;
  446. if assigned(hnext) and (hnext^.temptype=tt_free) then
  447. begin
  448. inc(hp^.size,hnext^.size);
  449. hp^.nextfree:=hnext^.nextfree;
  450. hp^.next:=hnext^.next;
  451. dispose(hnext);
  452. end;
  453. exit;
  454. end;
  455. if (hp^.temptype=tt_free) then
  456. hprevfree:=hp;
  457. hprev:=hp;
  458. hp:=hp^.next;
  459. end;
  460. ungettemp:=tt_none;
  461. end;
  462. procedure ungetpersistanttemp(pos : longint);
  463. begin
  464. {$ifdef EXTDEBUG}
  465. if ungettemp(pos,tt_persistant)<>tt_persistant then
  466. Comment(V_Warning,'temp managment problem : ungetpersistanttemp()'+
  467. ' at pos '+tostr(pos)+ ' not found !');
  468. {$else}
  469. ungettemp(pos,tt_persistant);
  470. {$endif}
  471. end;
  472. procedure ungetiftemp(const ref : treference);
  473. {$ifdef EXTDEBUG}
  474. var
  475. tt : ttemptype;
  476. {$endif}
  477. begin
  478. if istemp(ref) then
  479. begin
  480. { first check if ansistring }
  481. if ungetiftempansi(ref) then
  482. exit;
  483. {$ifndef EXTDEBUG}
  484. ungettemp(ref.offset,tt_normal);
  485. {$else}
  486. tt:=ungettemp(ref.offset,tt_normal);
  487. if tt=tt_persistant then
  488. Comment(V_Debug,'temp at pos '+tostr(ref.offset)+ ' not released because persistant!');
  489. if tt=tt_none then
  490. Comment(V_Warning,'temp not found for release at offset '+tostr(ref.offset));
  491. {$endif}
  492. end;
  493. end;
  494. procedure inittemps;
  495. begin
  496. tempfreelist:=nil;
  497. templist:=nil;
  498. end;
  499. begin
  500. InitTemps;
  501. end.
  502. {
  503. $Log$
  504. Revision 1.8 2000-11-30 22:16:50 florian
  505. * moved to i386
  506. Revision 1.7 2000/11/29 00:30:42 florian
  507. * unused units removed from uses clause
  508. * some changes for widestrings
  509. Revision 1.6 2000/11/04 14:25:22 florian
  510. + merged Attila's changes for interfaces, not tested yet
  511. Revision 1.5 2000/09/30 16:08:45 peter
  512. * more cg11 updates
  513. Revision 1.4 2000/09/24 15:06:31 peter
  514. * use defines.inc
  515. Revision 1.3 2000/08/27 16:11:55 peter
  516. * moved some util functions from globals,cobjects to cutils
  517. * splitted files into finput,fmodule
  518. Revision 1.2 2000/07/13 11:32:52 michael
  519. + removed logs
  520. }