tgobj.pas 25 KB

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