tgobj.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. { finalize this temp if it's a managed type }
  36. fini : boolean;
  37. pos : longint;
  38. size : longint;
  39. def : tdef;
  40. next : ptemprecord;
  41. nextfree : ptemprecord; { for faster freeblock checking }
  42. {$ifdef EXTDEBUG}
  43. posinfo,
  44. releaseposinfo : tfileposinfo;
  45. {$endif}
  46. end;
  47. {# Generates temporary variables }
  48. ttgobj = class
  49. protected
  50. { contains all free temps using nextfree links }
  51. tempfreelist : ptemprecord;
  52. procedure alloctemp(list: TAsmList; size,alignment : longint; temptype : ttemptype; def:tdef; fini: boolean; out ref: treference); virtual;
  53. procedure freetemp(list: TAsmList; pos:longint;temptypes:ttemptypeset);virtual;
  54. procedure gettempinternal(list: TAsmList; size, alignment : longint;temptype:ttemptype;def: tdef; fini: boolean; out ref : treference);
  55. public
  56. { contains all temps }
  57. templist : ptemprecord;
  58. { Offsets of the first/last temp }
  59. firsttemp,
  60. lasttemp,
  61. { Offset of temp base register relative to guaranteed stack alignment
  62. (note: currently only behaves as expected if it's a power of 2,
  63. and if all requested alignments are also a power of 2) }
  64. alignmismatch: longint;
  65. direction : shortint;
  66. constructor create;virtual;reintroduce;
  67. {# Clear and free the complete linked list of temporary memory
  68. locations. The list is set to nil.}
  69. procedure resettempgen;
  70. {# Sets the first offset from the frame pointer or stack pointer where
  71. the temporary references will be allocated. It is to note that this
  72. value should always be negative.
  73. @param(l start offset where temps will start in stack)
  74. }
  75. procedure setfirsttemp(l : longint); virtual;
  76. procedure setalignmentmismatch(l : longint); virtual;
  77. { version of gettemp that is compatible with hlcg-based targets;
  78. always use in common code, only use gettemp in cgobj and
  79. architecture-specific backends.
  80. the forcesize parameter is so that it can be used for defs that
  81. don't have an inherent size (e.g., array of const) }
  82. procedure gethltemp(list: TAsmList; def: tdef; forcesize: asizeint; temptype: ttemptype; out ref: treference); virtual;
  83. procedure gethltempmanaged(list: TAsmList; def: tdef; temptype: ttemptype; out ref: treference); virtual;
  84. procedure gettemp(list: TAsmList; size, alignment : longint;temptype:ttemptype;out ref : treference);
  85. procedure gettempmanaged(list: TAsmList; def:tdef;temptype:ttemptype;out ref : treference);
  86. procedure ungettemp(list: TAsmList; const ref : treference);
  87. function sizeoftemp(list: TAsmList; const ref: treference): longint;
  88. function changetemptype(list: TAsmList; const ref:treference;temptype:ttemptype):boolean;
  89. function gettypeoftemp(const ref:treference): ttemptype;
  90. {# Returns TRUE if the reference ref is allocated in temporary volatile memory space,
  91. otherwise returns FALSE.
  92. @param(ref reference to verify)
  93. }
  94. function istemp(const ref : treference) : boolean; virtual;
  95. {# Frees a reference @var(ref) which was allocated in the volatile temporary memory space.
  96. The freed space can later be reallocated and reused. If this reference
  97. is not in the temporary memory, it is simply not freed.
  98. }
  99. procedure ungetiftemp(list: TAsmList; const ref : treference); virtual;
  100. { Allocate space for a local }
  101. procedure getlocal(list: TAsmList; size : longint;def:tdef;var ref : treference);
  102. procedure getlocal(list: TAsmList; size : longint; alignment : shortint; def:tdef;var ref : treference); virtual;
  103. procedure UnGetLocal(list: TAsmList; const ref : treference);
  104. end;
  105. ttgobjclass = class of ttgobj;
  106. var
  107. tg: ttgobj;
  108. tgobjclass: ttgobjclass = ttgobj;
  109. procedure location_freetemp(list:TAsmList; const l : tlocation);
  110. implementation
  111. uses
  112. cutils,
  113. systems,verbose,
  114. procinfo,
  115. symconst
  116. ;
  117. const
  118. FreeTempTypes = [tt_free,tt_freenoreuse,tt_freeregallocator];
  119. {$ifdef EXTDEBUG}
  120. TempTypeStr : array[ttemptype] of string[18] = (
  121. '<none>',
  122. 'free','normal','persistent',
  123. 'noreuse','freenoreuse',
  124. 'regallocator','freeregallocator'
  125. );
  126. {$endif EXTDEBUG}
  127. Used2Free : array[ttemptype] of ttemptype = (
  128. tt_none,
  129. tt_none,tt_free,tt_free,
  130. tt_freenoreuse,tt_none,
  131. tt_freeregallocator,tt_none
  132. );
  133. {*****************************************************************************
  134. Helpers
  135. *****************************************************************************}
  136. procedure location_freetemp(list:TAsmList; const l : tlocation);
  137. begin
  138. if (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  139. tg.ungetiftemp(list,l.reference);
  140. end;
  141. {*****************************************************************************
  142. TTGOBJ
  143. *****************************************************************************}
  144. constructor ttgobj.create;
  145. begin
  146. tempfreelist:=nil;
  147. templist:=nil;
  148. { we could create a new child class for this but I don't if it is worth the effort (FK) }
  149. {$if defined(powerpc) or defined(powerpc64) or defined(avr) or defined(jvm)}
  150. direction:=1;
  151. {$else}
  152. direction:=-1;
  153. {$endif}
  154. end;
  155. procedure ttgobj.resettempgen;
  156. var
  157. hp : ptemprecord;
  158. begin
  159. { Clear the old templist }
  160. while assigned(templist) do
  161. begin
  162. {$ifdef EXTDEBUG}
  163. if not(templist^.temptype in FreeTempTypes) then
  164. begin
  165. Comment(V_Warning,'tgobj: (ResetTempgen) temp at pos '+tostr(templist^.pos)+
  166. ' with size '+tostr(templist^.size)+' and type '+TempTypeStr[templist^.temptype]+
  167. ' from pos '+tostr(templist^.posinfo.line)+':'+tostr(templist^.posinfo.column)+
  168. ' not freed at the end of the procedure');
  169. end;
  170. {$endif EXTDEBUG}
  171. hp:=templist;
  172. templist:=hp^.next;
  173. dispose(hp);
  174. end;
  175. templist:=nil;
  176. tempfreelist:=nil;
  177. firsttemp:=0;
  178. lasttemp:=0;
  179. alignmismatch:=0;
  180. end;
  181. procedure ttgobj.setfirsttemp(l : longint);
  182. begin
  183. { this is a negative value normally }
  184. if l*direction>=0 then
  185. begin
  186. if odd(l) then
  187. inc(l,direction);
  188. end
  189. else
  190. internalerror(200204221);
  191. firsttemp:=l;
  192. lasttemp:=l;
  193. end;
  194. procedure ttgobj.setalignmentmismatch(l: longint);
  195. begin
  196. alignmismatch:=l*direction;
  197. end;
  198. procedure ttgobj.alloctemp(list: TAsmList; size,alignment : longint; temptype : ttemptype;def : tdef; fini: boolean; out ref: treference);
  199. var
  200. tl,htl,
  201. bestslot,bestprev,
  202. hprev,hp : ptemprecord;
  203. freetype : ttemptype;
  204. adjustedpos : longint;
  205. bestatend,
  206. fitatbegin,
  207. fitatend : boolean;
  208. begin
  209. bestprev:=nil;
  210. bestslot:=nil;
  211. tl:=nil;
  212. bestatend:=false;
  213. current_procinfo.updatestackalignment(alignment);
  214. if size=0 then
  215. begin
  216. {$ifdef EXTDEBUG}
  217. Comment(V_Warning,'tgobj: (AllocTemp) temp of size 0 requested, allocating 4 bytes');
  218. {$endif}
  219. size:=4;
  220. end;
  221. freetype:=Used2Free[temptype];
  222. if freetype=tt_none then
  223. internalerror(200208201);
  224. size:=align(size,alignment);
  225. { First check the tmpfreelist, but not when
  226. we don't want to reuse an already allocated block }
  227. if assigned(tempfreelist) and
  228. (temptype<>tt_noreuse) then
  229. begin
  230. hprev:=nil;
  231. hp:=tempfreelist;
  232. while assigned(hp) do
  233. begin
  234. {$ifdef EXTDEBUG}
  235. if not(hp^.temptype in FreeTempTypes) then
  236. Comment(V_Warning,'tgobj: (AllocTemp) temp at pos '+tostr(hp^.pos)+ ' in freelist is not set to tt_free !');
  237. {$endif}
  238. { Check only slots that are
  239. - free
  240. - share the same type if either has to be finalised
  241. - contain enough space
  242. - has a correct alignment }
  243. adjustedpos:=hp^.pos+alignmismatch;
  244. if (hp^.temptype=freetype) and
  245. (hp^.fini=fini) and
  246. ((hp^.def=def) or
  247. not fini) and
  248. (hp^.size>=size) and
  249. ((adjustedpos=align(adjustedpos,alignment)) or
  250. (adjustedpos+hp^.size-size = align(adjustedpos+hp^.size-size,alignment))) then
  251. begin
  252. { Slot is the same size then leave immediatly }
  253. if (hp^.size=size) then
  254. begin
  255. bestprev:=hprev;
  256. bestslot:=hp;
  257. break;
  258. end
  259. else
  260. begin
  261. { we can fit a smaller block either at the begin or at }
  262. { the end of a block. For direction=-1 we prefer the }
  263. { end, for direction=1 we prefer the begin (i.e., }
  264. { always closest to the source). We also try to use }
  265. { the block with the worst possible alignment that }
  266. { still suffices. And we pick the block which will }
  267. { have the best alignmenment after this new block is }
  268. { substracted from it. }
  269. fitatend:=(adjustedpos+hp^.size-size)=align(adjustedpos+hp^.size-size,alignment);
  270. fitatbegin:=adjustedpos=align(adjustedpos,alignment);
  271. if assigned(bestslot) then
  272. begin
  273. fitatend:=fitatend and
  274. ((not bestatend and
  275. (direction=-1)) or
  276. (bestatend and
  277. isbetteralignedthan(abs(bestslot^.pos+hp^.size-size),abs(adjustedpos+hp^.size-size),current_settings.alignment.localalignmax)));
  278. fitatbegin:=fitatbegin and
  279. (not bestatend or
  280. (direction=1)) and
  281. isbetteralignedthan(abs(adjustedpos+size),abs(bestslot^.pos+size),current_settings.alignment.localalignmax);
  282. end;
  283. if fitatend and
  284. fitatbegin then
  285. if isbetteralignedthan(abs(adjustedpos+hp^.size-size),abs(adjustedpos+size),current_settings.alignment.localalignmax) then
  286. fitatbegin:=false
  287. else if isbetteralignedthan(abs(adjustedpos+size),abs(adjustedpos+hp^.size-size),current_settings.alignment.localalignmax) then
  288. fitatend:=false
  289. else if (direction=1) then
  290. fitatend:=false
  291. else
  292. fitatbegin:=false;
  293. if fitatend or
  294. fitatbegin then
  295. begin
  296. bestprev:=hprev;
  297. bestslot:=hp;
  298. bestatend:=fitatend;
  299. end;
  300. end;
  301. end;
  302. hprev:=hp;
  303. hp:=hp^.nextfree;
  304. end;
  305. end;
  306. { Reuse an old temp ? }
  307. if assigned(bestslot) then
  308. begin
  309. if bestslot^.size=size then
  310. begin
  311. tl:=bestslot;
  312. { Remove from the tempfreelist }
  313. if assigned(bestprev) then
  314. bestprev^.nextfree:=tl^.nextfree
  315. else
  316. tempfreelist:=tl^.nextfree;
  317. end
  318. else
  319. begin
  320. { Duplicate bestlost and the block in the list }
  321. new(tl);
  322. move(bestslot^,tl^,sizeof(ttemprecord));
  323. tl^.next:=bestslot^.next;
  324. bestslot^.next:=tl;
  325. { Now we split the block in 2 parts. Depending on the direction
  326. we need to resize the newly inserted block or the old reused block.
  327. For direction=1 we can use tl for the new block. For direction=-1 we
  328. will be reusing bestslot and resize the new block, that means we need
  329. to swap the pointers }
  330. if (direction=-1) xor
  331. bestatend then
  332. begin
  333. htl:=tl;
  334. tl:=bestslot;
  335. bestslot:=htl;
  336. { Update the tempfreelist to point to the new block }
  337. if assigned(bestprev) then
  338. bestprev^.nextfree:=bestslot
  339. else
  340. tempfreelist:=bestslot;
  341. end;
  342. if not bestatend then
  343. inc(bestslot^.pos,size)
  344. else
  345. inc(tl^.pos,tl^.size-size);
  346. { Create new block and resize the old block }
  347. tl^.fini:=fini;
  348. tl^.size:=size;
  349. tl^.nextfree:=nil;
  350. { Resize the old block }
  351. dec(bestslot^.size,size);
  352. end;
  353. tl^.temptype:=temptype;
  354. tl^.def:=def;
  355. tl^.nextfree:=nil;
  356. end
  357. else
  358. begin
  359. { now we can create the templist entry }
  360. new(tl);
  361. tl^.temptype:=temptype;
  362. tl^.def:=def;
  363. { Extend the temp }
  364. if direction=-1 then
  365. begin
  366. lasttemp:=(-align(-lasttemp-alignmismatch,alignment))-size-alignmismatch;
  367. tl^.pos:=lasttemp;
  368. end
  369. else
  370. begin
  371. tl^.pos:=align(lasttemp+alignmismatch,alignment)-alignmismatch;
  372. lasttemp:=tl^.pos+size;
  373. end;
  374. tl^.fini:=fini;
  375. tl^.size:=size;
  376. tl^.next:=templist;
  377. tl^.nextfree:=nil;
  378. templist:=tl;
  379. end;
  380. {$ifdef EXTDEBUG}
  381. tl^.posinfo:=current_filepos;
  382. if assigned(tl^.def) then
  383. list.concat(tai_tempalloc.allocinfo(tl^.pos,tl^.size,'allocated with type '+TempTypeStr[tl^.temptype]+' for def '+tl^.def.typename))
  384. else
  385. list.concat(tai_tempalloc.allocinfo(tl^.pos,tl^.size,'allocated with type '+TempTypeStr[tl^.temptype]));
  386. {$else}
  387. list.concat(tai_tempalloc.alloc(tl^.pos,tl^.size));
  388. {$endif}
  389. reference_reset_base(ref,current_procinfo.framepointer,tl^.pos,alignment);
  390. end;
  391. procedure ttgobj.FreeTemp(list: TAsmList; pos:longint;temptypes:ttemptypeset);
  392. var
  393. hp,hnext,hprev,hprevfree : ptemprecord;
  394. begin
  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 if already freed }
  403. if hp^.temptype in FreeTempTypes then
  404. begin
  405. {$ifdef EXTDEBUG}
  406. Comment(V_Warning,'tgobj: (FreeTemp) temp at pos '+tostr(pos)+ ' is already free !');
  407. list.concat(tai_tempalloc.allocinfo(hp^.pos,hp^.size,'temp is already freed'));
  408. {$endif}
  409. exit;
  410. end;
  411. { check type that are allowed to be released }
  412. if not(hp^.temptype in temptypes) then
  413. begin
  414. {$ifdef EXTDEBUG}
  415. Comment(V_Debug,'tgobj: (Freetemp) temp at pos '+tostr(pos)+ ' has different type ('+TempTypeStr[hp^.temptype]+'), not releasing');
  416. list.concat(tai_tempalloc.allocinfo(hp^.pos,hp^.size,'temp has wrong type ('+TempTypeStr[hp^.temptype]+') not releasing'));
  417. {$endif}
  418. exit;
  419. end;
  420. list.concat(tai_tempalloc.dealloc(hp^.pos,hp^.size));
  421. { set this block to free }
  422. hp^.temptype:=Used2Free[hp^.temptype];
  423. { Update tempfreelist }
  424. if assigned(hprevfree) then
  425. begin
  426. { Concat blocks when the previous block is free and
  427. there is no block assigned for a tdef }
  428. if assigned(hprev) and
  429. (hp^.temptype=tt_free) and
  430. not assigned(hp^.def) and
  431. (hprev^.temptype=tt_free) and
  432. not assigned(hprev^.def) then
  433. begin
  434. inc(hprev^.size,hp^.size);
  435. if direction=1 then
  436. hprev^.pos:=hp^.pos;
  437. hprev^.next:=hp^.next;
  438. dispose(hp);
  439. hp:=hprev;
  440. end
  441. else
  442. begin
  443. hp^.nextfree:=hprevfree^.nextfree;
  444. hprevfree^.nextfree:=hp;
  445. end;
  446. end
  447. else
  448. begin
  449. hp^.nextfree:=tempfreelist;
  450. tempfreelist:=hp;
  451. end;
  452. { Concat blocks when the next block is free and
  453. there is no block assigned for a tdef }
  454. hnext:=hp^.next;
  455. if assigned(hnext) and
  456. (hp^.temptype=tt_free) and
  457. not assigned(hp^.def) and
  458. (hnext^.temptype=tt_free) and
  459. not assigned(hnext^.def) then
  460. begin
  461. inc(hp^.size,hnext^.size);
  462. if direction=1 then
  463. hp^.pos:=hnext^.pos;
  464. hp^.nextfree:=hnext^.nextfree;
  465. hp^.next:=hnext^.next;
  466. dispose(hnext);
  467. end;
  468. { Stop }
  469. exit;
  470. end;
  471. if (hp^.temptype=tt_free) then
  472. hprevfree:=hp;
  473. hprev:=hp;
  474. hp:=hp^.next;
  475. end;
  476. end;
  477. procedure ttgobj.gethltemp(list: TAsmList; def: tdef; forcesize: asizeint; temptype: ttemptype; out ref: treference);
  478. begin
  479. gettemp(list,forcesize,def.alignment,temptype,ref);
  480. end;
  481. procedure ttgobj.gethltempmanaged(list: TAsmList; def: tdef; temptype: ttemptype; out ref: treference);
  482. begin
  483. gettempmanaged(list,def,temptype,ref);
  484. end;
  485. procedure ttgobj.gettemp(list: TAsmList; size, alignment : longint;temptype:ttemptype;out ref : treference);
  486. begin
  487. gettempinternal(list,size,alignment,temptype,nil,false,ref);
  488. end;
  489. procedure ttgobj.gettempinternal(list: TAsmList; size, alignment : longint;temptype:ttemptype;def: tdef; fini: boolean; out ref : treference);
  490. var
  491. varalign : shortint;
  492. begin
  493. varalign:=used_align(alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax);
  494. alloctemp(list,size,varalign,temptype,def,fini,ref);
  495. end;
  496. procedure ttgobj.gettempmanaged(list: TAsmList; def:tdef;temptype:ttemptype;out ref : treference);
  497. begin
  498. gettempinternal(list,def.size,def.alignment,temptype,def,true,ref);
  499. end;
  500. function ttgobj.istemp(const ref : treference) : boolean;
  501. begin
  502. { ref.index = R_NO was missing
  503. led to problems with local arrays
  504. with lower bound > 0 (PM) }
  505. if direction = 1 then
  506. begin
  507. istemp:=(ref.base=current_procinfo.framepointer) and
  508. (ref.index=NR_NO) and
  509. (ref.offset>=firsttemp);
  510. end
  511. else
  512. begin
  513. istemp:=(ref.base=current_procinfo.framepointer) and
  514. (ref.index=NR_NO) and
  515. (ref.offset<firsttemp);
  516. end;
  517. end;
  518. function ttgobj.sizeoftemp(list: TAsmList; const ref: treference): longint;
  519. var
  520. hp : ptemprecord;
  521. begin
  522. SizeOfTemp := -1;
  523. hp:=templist;
  524. while assigned(hp) do
  525. begin
  526. if (hp^.pos=ref.offset) then
  527. begin
  528. sizeoftemp := hp^.size;
  529. exit;
  530. end;
  531. hp := hp^.next;
  532. end;
  533. {$ifdef EXTDEBUG}
  534. comment(v_debug,'tgobj: (SizeOfTemp) temp at pos '+tostr(ref.offset)+' not found !');
  535. list.concat(tai_tempalloc.allocinfo(ref.offset,0,'temp not found'));
  536. {$endif}
  537. end;
  538. function ttgobj.changetemptype(list: tasmList; const ref:treference; temptype:ttemptype):boolean;
  539. var
  540. hp : ptemprecord;
  541. begin
  542. ChangeTempType:=false;
  543. hp:=templist;
  544. while assigned(hp) do
  545. begin
  546. if (hp^.pos=ref.offset) then
  547. begin
  548. if hp^.temptype<>tt_free then
  549. begin
  550. {$ifdef EXTDEBUG}
  551. if hp^.temptype=temptype then
  552. Comment(V_Warning,'tgobj: (ChangeTempType) temp'+
  553. ' at pos '+tostr(ref.offset)+ ' is already of the correct type !');
  554. list.concat(tai_tempalloc.allocinfo(hp^.pos,hp^.size,'type changed to '+TempTypeStr[temptype]));
  555. {$endif}
  556. ChangeTempType:=true;
  557. hp^.temptype:=temptype;
  558. end
  559. else
  560. begin
  561. {$ifdef EXTDEBUG}
  562. Comment(V_Warning,'tgobj: (ChangeTempType) temp'+
  563. ' at pos '+tostr(ref.offset)+ ' is already freed !');
  564. list.concat(tai_tempalloc.allocinfo(hp^.pos,hp^.size,'temp is already freed'));
  565. {$endif}
  566. end;
  567. exit;
  568. end;
  569. hp:=hp^.next;
  570. end;
  571. {$ifdef EXTDEBUG}
  572. Comment(V_Warning,'tgobj: (ChangeTempType) temp'+
  573. ' at pos '+tostr(ref.offset)+ ' not found !');
  574. list.concat(tai_tempalloc.allocinfo(ref.offset,0,'temp not found'));
  575. {$endif}
  576. end;
  577. function ttgobj.gettypeoftemp(const ref:treference): ttemptype;
  578. var
  579. hp : ptemprecord;
  580. begin
  581. hp:=templist;
  582. while assigned(hp) do
  583. begin
  584. if (hp^.pos=ref.offset) then
  585. begin
  586. if hp^.temptype<>tt_free then
  587. result:=hp^.temptype
  588. else
  589. internalerror(2007020810);
  590. exit;
  591. end;
  592. hp:=hp^.next;
  593. end;
  594. result:=tt_none;
  595. end;
  596. procedure ttgobj.UnGetTemp(list: TAsmList; const ref : treference);
  597. begin
  598. FreeTemp(list,ref.offset,[tt_normal,tt_noreuse,tt_persistent,tt_regallocator]);
  599. end;
  600. procedure ttgobj.UnGetIfTemp(list: TAsmList; const ref : treference);
  601. begin
  602. if istemp(ref) then
  603. FreeTemp(list,ref.offset,[tt_normal]);
  604. end;
  605. procedure ttgobj.getlocal(list: TAsmList; size : longint;def:tdef;var ref : treference);
  606. begin
  607. getlocal(list, size, def.alignment, def, ref);
  608. end;
  609. procedure ttgobj.getlocal(list: TAsmList; size : longint; alignment : shortint; def:tdef;var ref : treference);
  610. begin
  611. alignment:=used_align(alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax);
  612. alloctemp(list,size,alignment,tt_persistent,def,false,ref);
  613. end;
  614. procedure ttgobj.UnGetLocal(list: TAsmList; const ref : treference);
  615. begin
  616. FreeTemp(list,ref.offset,[tt_persistent]);
  617. end;
  618. end.