tgobj.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the base object for temp. generator
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {#@abstract(Temporary reference allocator unit)
  18. Temporary reference allocator unit. This unit contains
  19. all which is related to allocating temporary memory
  20. space on the stack, as required, by the code generator.
  21. }
  22. unit tgobj;
  23. {$i fpcdefs.inc}
  24. interface
  25. uses
  26. cclasses,
  27. globals,globtype,
  28. symtype,
  29. cpubase,cpuinfo,cgbase,cgutils,
  30. aasmbase,aasmtai,aasmdata;
  31. type
  32. ptemprecord = ^ttemprecord;
  33. ttemprecord = record
  34. temptype : ttemptype;
  35. pos : longint;
  36. size : longint;
  37. def : tdef;
  38. next : ptemprecord;
  39. nextfree : ptemprecord; { for faster freeblock checking }
  40. {$ifdef EXTDEBUG}
  41. posinfo,
  42. releaseposinfo : tfileposinfo;
  43. {$endif}
  44. end;
  45. {# Generates temporary variables }
  46. ttgobj = class
  47. private
  48. { contains all free temps using nextfree links }
  49. tempfreelist : ptemprecord;
  50. function alloctemp(list: TAsmList; size,alignment : longint; temptype : ttemptype; def:tdef) : longint;
  51. procedure freetemp(list: TAsmList; pos:longint;temptypes:ttemptypeset);
  52. public
  53. { contains all temps }
  54. templist : ptemprecord;
  55. { Offsets of the first/last temp }
  56. firsttemp,
  57. lasttemp : longint;
  58. direction : shortint;
  59. constructor create;
  60. {# Clear and free the complete linked list of temporary memory
  61. locations. The list is set to nil.}
  62. procedure resettempgen;
  63. {# Sets the first offset from the frame pointer or stack pointer where
  64. the temporary references will be allocated. It is to note that this
  65. value should always be negative.
  66. @param(l start offset where temps will start in stack)
  67. }
  68. procedure setfirsttemp(l : longint);
  69. procedure gettemp(list: TAsmList; size : longint;temptype:ttemptype;out ref : treference);
  70. procedure gettemptyped(list: TAsmList; def:tdef;temptype:ttemptype;out ref : treference);
  71. procedure ungettemp(list: TAsmList; const ref : treference);
  72. function sizeoftemp(list: TAsmList; const ref: treference): longint;
  73. function changetemptype(list: TAsmList; const ref:treference;temptype:ttemptype):boolean;
  74. {# Returns TRUE if the reference ref is allocated in temporary volatile memory space,
  75. otherwise returns FALSE.
  76. @param(ref reference to verify)
  77. }
  78. function istemp(const ref : treference) : boolean;
  79. {# Frees a reference @var(ref) which was allocated in the volatile temporary memory space.
  80. The freed space can later be reallocated and reused. If this reference
  81. is not in the temporary memory, it is simply not freed.
  82. }
  83. procedure ungetiftemp(list: TAsmList; const ref : treference);
  84. { Allocate space for a local }
  85. procedure getlocal(list: TAsmList; size : longint;def:tdef;var ref : treference);
  86. procedure getlocal(list: TAsmList; size : longint; alignment : shortint; def:tdef;var ref : treference);
  87. procedure UnGetLocal(list: TAsmList; const ref : treference);
  88. end;
  89. var
  90. tg: ttgobj;
  91. procedure location_freetemp(list:TAsmList; const l : tlocation);
  92. implementation
  93. uses
  94. cutils,
  95. systems,verbose,
  96. procinfo
  97. ;
  98. const
  99. FreeTempTypes = [tt_free,tt_freenoreuse];
  100. {$ifdef EXTDEBUG}
  101. TempTypeStr : array[ttemptype] of string[18] = (
  102. '<none>',
  103. 'free','normal','persistant',
  104. 'noreuse','freenoreuse'
  105. );
  106. {$endif EXTDEBUG}
  107. Used2Free : array[ttemptype] of ttemptype = (
  108. tt_none,
  109. tt_none,tt_free,tt_free,
  110. tt_freenoreuse,tt_none
  111. );
  112. {*****************************************************************************
  113. Helpers
  114. *****************************************************************************}
  115. procedure location_freetemp(list:TAsmList; const l : tlocation);
  116. begin
  117. if (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  118. tg.ungetiftemp(list,l.reference);
  119. end;
  120. {*****************************************************************************
  121. TTGOBJ
  122. *****************************************************************************}
  123. constructor ttgobj.create;
  124. begin
  125. tempfreelist:=nil;
  126. templist:=nil;
  127. { we could create a new child class for this but I don't if it is worth the effort (FK) }
  128. {$if defined(powerpc) or defined(powerpc64)}
  129. direction:=1;
  130. {$else}
  131. direction:=-1;
  132. {$endif}
  133. end;
  134. procedure ttgobj.resettempgen;
  135. var
  136. hp : ptemprecord;
  137. begin
  138. { Clear the old templist }
  139. while assigned(templist) do
  140. begin
  141. {$ifdef EXTDEBUG}
  142. if not(templist^.temptype in FreeTempTypes) then
  143. begin
  144. Comment(V_Warning,'tgobj: (ResetTempgen) temp at pos '+tostr(templist^.pos)+
  145. ' with size '+tostr(templist^.size)+' and type '+TempTypeStr[templist^.temptype]+
  146. ' from pos '+tostr(templist^.posinfo.line)+':'+tostr(templist^.posinfo.column)+
  147. ' not freed at the end of the procedure');
  148. end;
  149. {$endif EXTDEBUG}
  150. hp:=templist;
  151. templist:=hp^.next;
  152. dispose(hp);
  153. end;
  154. templist:=nil;
  155. tempfreelist:=nil;
  156. firsttemp:=0;
  157. lasttemp:=0;
  158. end;
  159. procedure ttgobj.setfirsttemp(l : longint);
  160. begin
  161. { this is a negative value normally }
  162. if l*direction>=0 then
  163. begin
  164. if odd(l) then
  165. inc(l,direction);
  166. end
  167. else
  168. internalerror(200204221);
  169. firsttemp:=l;
  170. lasttemp:=l;
  171. end;
  172. function ttgobj.AllocTemp(list: TAsmList; size,alignment : longint; temptype : ttemptype;def : tdef) : longint;
  173. var
  174. tl,htl,
  175. bestslot,bestprev,
  176. hprev,hp : ptemprecord;
  177. freetype : ttemptype;
  178. bestatend,
  179. fitatbegin,
  180. fitatend : boolean;
  181. begin
  182. AllocTemp:=0;
  183. bestprev:=nil;
  184. bestslot:=nil;
  185. tl:=nil;
  186. bestatend:=false;
  187. if size=0 then
  188. begin
  189. {$ifdef EXTDEBUG}
  190. Comment(V_Warning,'tgobj: (AllocTemp) temp of size 0 requested, allocating 4 bytes');
  191. {$endif}
  192. size:=4;
  193. end;
  194. freetype:=Used2Free[temptype];
  195. if freetype=tt_none then
  196. internalerror(200208201);
  197. size:=align(size,alignment);
  198. { First check the tmpfreelist, but not when
  199. we don't want to reuse an already allocated block }
  200. if assigned(tempfreelist) and
  201. (temptype<>tt_noreuse) then
  202. begin
  203. hprev:=nil;
  204. hp:=tempfreelist;
  205. while assigned(hp) do
  206. begin
  207. {$ifdef EXTDEBUG}
  208. if not(hp^.temptype in FreeTempTypes) then
  209. Comment(V_Warning,'tgobj: (AllocTemp) temp at pos '+tostr(hp^.pos)+ ' in freelist is not set to tt_free !');
  210. {$endif}
  211. { Check only slots that are
  212. - free
  213. - share the same type
  214. - contain enough space
  215. - has a correct alignment }
  216. if (hp^.temptype=freetype) and
  217. (hp^.def=def) and
  218. (hp^.size>=size) and
  219. ((hp^.pos=align(hp^.pos,alignment)) or
  220. (hp^.pos+hp^.size-size = align(hp^.pos+hp^.size-size,alignment))) then
  221. begin
  222. { Slot is the same size then leave immediatly }
  223. if (hp^.size=size) then
  224. begin
  225. bestprev:=hprev;
  226. bestslot:=hp;
  227. break;
  228. end
  229. else
  230. begin
  231. { we can fit a smaller block either at the begin or at }
  232. { the end of a block. For direction=-1 we prefer the }
  233. { end, for direction=1 we prefer the begin (i.e., }
  234. { always closest to the source). We also try to use }
  235. { the block with the worst possible alignment that }
  236. { still suffices. And we pick the block which will }
  237. { have the best alignmenment after this new block is }
  238. { substracted from it. }
  239. fitatend:=(hp^.pos+hp^.size-size)=align(hp^.pos+hp^.size-size,alignment);
  240. fitatbegin:=hp^.pos=align(hp^.pos,alignment);
  241. if assigned(bestslot) then
  242. begin
  243. fitatend:=fitatend and
  244. ((not bestatend and
  245. (direction=-1)) or
  246. (bestatend and
  247. isbetteralignedthan(abs(bestslot^.pos+hp^.size-size),abs(hp^.pos+hp^.size-size),current_settings.alignment.localalignmax)));
  248. fitatbegin:=fitatbegin and
  249. (not bestatend or
  250. (direction=1)) and
  251. isbetteralignedthan(abs(hp^.pos+size),abs(bestslot^.pos+size),current_settings.alignment.localalignmax);
  252. end;
  253. if fitatend and
  254. fitatbegin then
  255. if isbetteralignedthan(abs(hp^.pos+hp^.size-size),abs(hp^.pos+size),current_settings.alignment.localalignmax) then
  256. fitatbegin:=false
  257. else if isbetteralignedthan(abs(hp^.pos+size),abs(hp^.pos+hp^.size-size),current_settings.alignment.localalignmax) then
  258. fitatend:=false
  259. else if (direction=1) then
  260. fitatend:=false
  261. else
  262. fitatbegin:=false;
  263. if fitatend or
  264. fitatbegin then
  265. begin
  266. bestprev:=hprev;
  267. bestslot:=hp;
  268. bestatend:=fitatend;
  269. end;
  270. end;
  271. end;
  272. hprev:=hp;
  273. hp:=hp^.nextfree;
  274. end;
  275. end;
  276. { Reuse an old temp ? }
  277. if assigned(bestslot) then
  278. begin
  279. if bestslot^.size=size then
  280. begin
  281. tl:=bestslot;
  282. { Remove from the tempfreelist }
  283. if assigned(bestprev) then
  284. bestprev^.nextfree:=tl^.nextfree
  285. else
  286. tempfreelist:=tl^.nextfree;
  287. end
  288. else
  289. begin
  290. { Duplicate bestlost and the block in the list }
  291. new(tl);
  292. move(bestslot^,tl^,sizeof(ttemprecord));
  293. tl^.next:=bestslot^.next;
  294. bestslot^.next:=tl;
  295. { Now we split the block in 2 parts. Depending on the direction
  296. we need to resize the newly inserted block or the old reused block.
  297. For direction=1 we can use tl for the new block. For direction=-1 we
  298. will be reusing bestslot and resize the new block, that means we need
  299. to swap the pointers }
  300. if (direction=-1) xor
  301. bestatend then
  302. begin
  303. htl:=tl;
  304. tl:=bestslot;
  305. bestslot:=htl;
  306. { Update the tempfreelist to point to the new block }
  307. if assigned(bestprev) then
  308. bestprev^.nextfree:=bestslot
  309. else
  310. tempfreelist:=bestslot;
  311. end;
  312. if not bestatend then
  313. inc(bestslot^.pos,size)
  314. else
  315. inc(tl^.pos,tl^.size-size);
  316. { Create new block and resize the old block }
  317. tl^.size:=size;
  318. tl^.nextfree:=nil;
  319. { Resize the old block }
  320. dec(bestslot^.size,size);
  321. end;
  322. tl^.temptype:=temptype;
  323. tl^.def:=def;
  324. tl^.nextfree:=nil;
  325. end
  326. else
  327. begin
  328. { now we can create the templist entry }
  329. new(tl);
  330. tl^.temptype:=temptype;
  331. tl^.def:=def;
  332. { Extend the temp }
  333. if direction=-1 then
  334. begin
  335. lasttemp:=(-align(-lasttemp,alignment))-size;
  336. tl^.pos:=lasttemp;
  337. end
  338. else
  339. begin
  340. tl^.pos:=align(lasttemp,alignment);
  341. lasttemp:=tl^.pos+size;
  342. end;
  343. tl^.size:=size;
  344. tl^.next:=templist;
  345. tl^.nextfree:=nil;
  346. templist:=tl;
  347. end;
  348. {$ifdef EXTDEBUG}
  349. tl^.posinfo:=current_filepos;
  350. if assigned(tl^.def) then
  351. list.concat(tai_tempalloc.allocinfo(tl^.pos,tl^.size,'allocated with type '+TempTypeStr[tl^.temptype]+' for def '+tl^.def.typename))
  352. else
  353. list.concat(tai_tempalloc.allocinfo(tl^.pos,tl^.size,'allocated with type '+TempTypeStr[tl^.temptype]));
  354. {$else}
  355. list.concat(tai_tempalloc.alloc(tl^.pos,tl^.size));
  356. {$endif}
  357. AllocTemp:=tl^.pos;
  358. end;
  359. procedure ttgobj.FreeTemp(list: TAsmList; pos:longint;temptypes:ttemptypeset);
  360. var
  361. hp,hnext,hprev,hprevfree : ptemprecord;
  362. begin
  363. hp:=templist;
  364. hprev:=nil;
  365. hprevfree:=nil;
  366. while assigned(hp) do
  367. begin
  368. if (hp^.pos=pos) then
  369. begin
  370. { check if already freed }
  371. if hp^.temptype in FreeTempTypes then
  372. begin
  373. {$ifdef EXTDEBUG}
  374. Comment(V_Warning,'tgobj: (FreeTemp) temp at pos '+tostr(pos)+ ' is already free !');
  375. list.concat(tai_tempalloc.allocinfo(hp^.pos,hp^.size,'temp is already freed'));
  376. {$endif}
  377. exit;
  378. end;
  379. { check type that are allowed to be released }
  380. if not(hp^.temptype in temptypes) then
  381. begin
  382. {$ifdef EXTDEBUG}
  383. Comment(V_Debug,'tgobj: (Freetemp) temp at pos '+tostr(pos)+ ' has different type ('+TempTypeStr[hp^.temptype]+'), not releasing');
  384. list.concat(tai_tempalloc.allocinfo(hp^.pos,hp^.size,'temp has wrong type ('+TempTypeStr[hp^.temptype]+') not releasing'));
  385. {$endif}
  386. exit;
  387. end;
  388. list.concat(tai_tempalloc.dealloc(hp^.pos,hp^.size));
  389. { set this block to free }
  390. hp^.temptype:=Used2Free[hp^.temptype];
  391. { Update tempfreelist }
  392. if assigned(hprevfree) then
  393. begin
  394. { Concat blocks when the previous block is free and
  395. there is no block assigned for a tdef }
  396. if assigned(hprev) and
  397. (hp^.temptype=tt_free) and
  398. not assigned(hp^.def) and
  399. (hprev^.temptype=tt_free) and
  400. not assigned(hprev^.def) then
  401. begin
  402. inc(hprev^.size,hp^.size);
  403. if direction=1 then
  404. hprev^.pos:=hp^.pos;
  405. hprev^.next:=hp^.next;
  406. dispose(hp);
  407. hp:=hprev;
  408. end
  409. else
  410. hprevfree^.nextfree:=hp;
  411. end
  412. else
  413. begin
  414. hp^.nextfree:=tempfreelist;
  415. tempfreelist:=hp;
  416. end;
  417. { Concat blocks when the next block is free and
  418. there is no block assigned for a tdef }
  419. hnext:=hp^.next;
  420. if assigned(hnext) and
  421. (hp^.temptype=tt_free) and
  422. not assigned(hp^.def) and
  423. (hnext^.temptype=tt_free) and
  424. not assigned(hnext^.def) then
  425. begin
  426. inc(hp^.size,hnext^.size);
  427. if direction=1 then
  428. hp^.pos:=hnext^.pos;
  429. hp^.nextfree:=hnext^.nextfree;
  430. hp^.next:=hnext^.next;
  431. dispose(hnext);
  432. end;
  433. { Stop }
  434. exit;
  435. end;
  436. if (hp^.temptype=tt_free) then
  437. hprevfree:=hp;
  438. hprev:=hp;
  439. hp:=hp^.next;
  440. end;
  441. end;
  442. procedure ttgobj.gettemp(list: TAsmList; size : longint;temptype:ttemptype;out ref : treference);
  443. var
  444. varalign : shortint;
  445. begin
  446. varalign:=size_2_align(size);
  447. varalign:=used_align(varalign,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax);
  448. { can't use reference_reset_base, because that will let tgobj depend
  449. on cgobj (PFV) }
  450. fillchar(ref,sizeof(ref),0);
  451. ref.base:=current_procinfo.framepointer;
  452. ref.offset:=alloctemp(list,size,varalign,temptype,nil);
  453. end;
  454. procedure ttgobj.gettemptyped(list: TAsmList; def:tdef;temptype:ttemptype;out ref : treference);
  455. var
  456. varalign : shortint;
  457. begin
  458. varalign:=def.alignment;
  459. varalign:=used_align(varalign,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax);
  460. { can't use reference_reset_base, because that will let tgobj depend
  461. on cgobj (PFV) }
  462. fillchar(ref,sizeof(ref),0);
  463. ref.base:=current_procinfo.framepointer;
  464. ref.offset:=alloctemp(list,def.size,varalign,temptype,def);
  465. end;
  466. function ttgobj.istemp(const ref : treference) : boolean;
  467. begin
  468. { ref.index = R_NO was missing
  469. led to problems with local arrays
  470. with lower bound > 0 (PM) }
  471. if direction = 1 then
  472. begin
  473. istemp:=(ref.base=current_procinfo.framepointer) and
  474. (ref.index=NR_NO) and
  475. (ref.offset>=firsttemp);
  476. end
  477. else
  478. begin
  479. istemp:=(ref.base=current_procinfo.framepointer) and
  480. (ref.index=NR_NO) and
  481. (ref.offset<firsttemp);
  482. end;
  483. end;
  484. function ttgobj.sizeoftemp(list: TAsmList; const ref: treference): longint;
  485. var
  486. hp : ptemprecord;
  487. begin
  488. SizeOfTemp := -1;
  489. hp:=templist;
  490. while assigned(hp) do
  491. begin
  492. if (hp^.pos=ref.offset) then
  493. begin
  494. sizeoftemp := hp^.size;
  495. exit;
  496. end;
  497. hp := hp^.next;
  498. end;
  499. {$ifdef EXTDEBUG}
  500. comment(v_debug,'tgobj: (SizeOfTemp) temp at pos '+tostr(ref.offset)+' not found !');
  501. list.concat(tai_tempalloc.allocinfo(ref.offset,0,'temp not found'));
  502. {$endif}
  503. end;
  504. function ttgobj.ChangeTempType(list: TAsmList; const ref:treference;temptype:ttemptype):boolean;
  505. var
  506. hp : ptemprecord;
  507. begin
  508. ChangeTempType:=false;
  509. hp:=templist;
  510. while assigned(hp) do
  511. begin
  512. if (hp^.pos=ref.offset) then
  513. begin
  514. if hp^.temptype<>tt_free then
  515. begin
  516. {$ifdef EXTDEBUG}
  517. if hp^.temptype=temptype then
  518. Comment(V_Warning,'tgobj: (ChangeTempType) temp'+
  519. ' at pos '+tostr(ref.offset)+ ' is already of the correct type !');
  520. list.concat(tai_tempalloc.allocinfo(hp^.pos,hp^.size,'type changed to '+TempTypeStr[temptype]));
  521. {$endif}
  522. ChangeTempType:=true;
  523. hp^.temptype:=temptype;
  524. end
  525. else
  526. begin
  527. {$ifdef EXTDEBUG}
  528. Comment(V_Warning,'tgobj: (ChangeTempType) temp'+
  529. ' at pos '+tostr(ref.offset)+ ' is already freed !');
  530. list.concat(tai_tempalloc.allocinfo(hp^.pos,hp^.size,'temp is already freed'));
  531. {$endif}
  532. end;
  533. exit;
  534. end;
  535. hp:=hp^.next;
  536. end;
  537. {$ifdef EXTDEBUG}
  538. Comment(V_Warning,'tgobj: (ChangeTempType) temp'+
  539. ' at pos '+tostr(ref.offset)+ ' not found !');
  540. list.concat(tai_tempalloc.allocinfo(ref.offset,0,'temp not found'));
  541. {$endif}
  542. end;
  543. procedure ttgobj.UnGetTemp(list: TAsmList; const ref : treference);
  544. begin
  545. FreeTemp(list,ref.offset,[tt_normal,tt_noreuse,tt_persistent]);
  546. end;
  547. procedure ttgobj.UnGetIfTemp(list: TAsmList; const ref : treference);
  548. begin
  549. if istemp(ref) then
  550. FreeTemp(list,ref.offset,[tt_normal]);
  551. end;
  552. procedure ttgobj.getlocal(list: TAsmList; size : longint;def:tdef;var ref : treference);
  553. begin
  554. getlocal(list, size, def.alignment, def, ref);
  555. end;
  556. procedure ttgobj.getlocal(list: TAsmList; size : longint; alignment : shortint; def:tdef;var ref : treference);
  557. begin
  558. alignment:=used_align(alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax);
  559. { can't use reference_reset_base, because that will let tgobj depend
  560. on cgobj (PFV) }
  561. fillchar(ref,sizeof(ref),0);
  562. ref.base:=current_procinfo.framepointer;
  563. ref.offset:=alloctemp(list,size,alignment,tt_persistent,nil);
  564. end;
  565. procedure ttgobj.UnGetLocal(list: TAsmList; const ref : treference);
  566. begin
  567. FreeTemp(list,ref.offset,[tt_persistent]);
  568. end;
  569. end.