tgobj.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. {$push}
  366. {$r-}
  367. {$warn 6018 off}
  368. {$warn 4044 off}
  369. { Extend the temp }
  370. if direction=-1 then
  371. begin
  372. if qword(align(-lasttemp-alignmismatch,alignment))+size+alignmismatch>high(tl^.pos) then
  373. CGMessage(cg_e_localsize_too_big);
  374. lasttemp:=(-align(-lasttemp-alignmismatch,alignment))-size-alignmismatch;
  375. tl^.pos:=lasttemp;
  376. end
  377. else
  378. begin
  379. tl^.pos:=align(lasttemp+alignmismatch,alignment)-alignmismatch;
  380. if qword(tl^.pos)+size>high(tl^.pos) then
  381. CGMessage(cg_e_localsize_too_big);
  382. lasttemp:=tl^.pos+size;
  383. end;
  384. {$pop}
  385. tl^.fini:=fini;
  386. tl^.alignment:=alignment;
  387. tl^.size:=size;
  388. tl^.next:=templist;
  389. tl^.nextfree:=nil;
  390. templist:=tl;
  391. end;
  392. {$ifdef EXTDEBUG}
  393. tl^.posinfo:=current_filepos;
  394. if assigned(tl^.def) then
  395. list.concat(tai_tempalloc.allocinfo(tl^.pos,tl^.size,'allocated with type '+TempTypeStr[tl^.temptype]+' for def '+tl^.def.typename))
  396. else
  397. list.concat(tai_tempalloc.allocinfo(tl^.pos,tl^.size,'allocated with type '+TempTypeStr[tl^.temptype]));
  398. {$else}
  399. list.concat(tai_tempalloc.alloc(tl^.pos,tl^.size));
  400. {$endif}
  401. reference_reset_base(ref,current_procinfo.framepointer,tl^.pos,alignment);
  402. end;
  403. procedure ttgobj.FreeTemp(list: TAsmList; pos: asizeint; temptypes: ttemptypeset);
  404. var
  405. hp,hnext,hprev,hprevfree : ptemprecord;
  406. begin
  407. hp:=templist;
  408. hprev:=nil;
  409. hprevfree:=nil;
  410. while assigned(hp) do
  411. begin
  412. if (hp^.pos=pos) then
  413. begin
  414. { check if already freed }
  415. if hp^.temptype in FreeTempTypes then
  416. begin
  417. {$ifdef EXTDEBUG}
  418. Comment(V_Warning,'tgobj: (FreeTemp) temp at pos '+tostr(pos)+ ' is already free !');
  419. list.concat(tai_tempalloc.allocinfo(hp^.pos,hp^.size,'temp is already freed'));
  420. {$endif}
  421. exit;
  422. end;
  423. { check type that are allowed to be released }
  424. if not(hp^.temptype in temptypes) then
  425. begin
  426. {$ifdef EXTDEBUG}
  427. Comment(V_Debug,'tgobj: (Freetemp) temp at pos '+tostr(pos)+ ' has different type ('+TempTypeStr[hp^.temptype]+'), not releasing');
  428. list.concat(tai_tempalloc.allocinfo(hp^.pos,hp^.size,'temp has wrong type ('+TempTypeStr[hp^.temptype]+') not releasing'));
  429. {$endif}
  430. exit;
  431. end;
  432. list.concat(tai_tempalloc.dealloc(hp^.pos,hp^.size));
  433. { set this block to free }
  434. hp^.temptype:=Used2Free[hp^.temptype];
  435. { Update tempfreelist }
  436. if assigned(hprevfree) then
  437. begin
  438. { Concat blocks when the previous block is free and
  439. there is no block assigned for a tdef }
  440. if assigned(hprev) and
  441. (hp^.temptype=tt_free) and
  442. not assigned(hp^.def) and
  443. (hprev^.temptype=tt_free) and
  444. not assigned(hprev^.def) then
  445. begin
  446. inc(hprev^.size,hp^.size);
  447. if direction=1 then
  448. hprev^.pos:=hp^.pos;
  449. hprev^.next:=hp^.next;
  450. dispose(hp);
  451. hp:=hprev;
  452. end
  453. else
  454. begin
  455. hp^.nextfree:=hprevfree^.nextfree;
  456. hprevfree^.nextfree:=hp;
  457. end;
  458. end
  459. else
  460. begin
  461. hp^.nextfree:=tempfreelist;
  462. tempfreelist:=hp;
  463. end;
  464. { Concat blocks when the next block is free and
  465. there is no block assigned for a tdef }
  466. hnext:=hp^.next;
  467. if assigned(hnext) and
  468. (hp^.temptype=tt_free) and
  469. not assigned(hp^.def) and
  470. (hnext^.temptype=tt_free) and
  471. not assigned(hnext^.def) then
  472. begin
  473. inc(hp^.size,hnext^.size);
  474. if direction=1 then
  475. hp^.pos:=hnext^.pos;
  476. hp^.nextfree:=hnext^.nextfree;
  477. hp^.next:=hnext^.next;
  478. dispose(hnext);
  479. end;
  480. { Stop }
  481. exit;
  482. end;
  483. if (hp^.temptype=tt_free) then
  484. hprevfree:=hp;
  485. hprev:=hp;
  486. hp:=hp^.next;
  487. end;
  488. end;
  489. procedure ttgobj.gethltemp(list: TAsmList; def: tdef; forcesize: asizeint; temptype: ttemptype; out ref: treference);
  490. begin
  491. gettemp(list,forcesize,def.alignment,temptype,ref);
  492. end;
  493. procedure ttgobj.gethltempmanaged(list: TAsmList; def: tdef; temptype: ttemptype; out ref: treference);
  494. begin
  495. gettempmanaged(list,def,temptype,ref);
  496. end;
  497. procedure ttgobj.gettemp(list: TAsmList; size: asizeint; alignment: shortint; temptype: ttemptype; out ref : treference);
  498. begin
  499. gettempinternal(list,size,alignment,temptype,nil,false,ref);
  500. end;
  501. procedure ttgobj.gettempinternal(list: TAsmList; size: asizeint; alignment: shortint; temptype: ttemptype; def: tdef; fini: boolean; out ref : treference);
  502. var
  503. varalign : shortint;
  504. begin
  505. varalign:=used_align(alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax);
  506. alloctemp(list,size,varalign,temptype,def,fini,ref);
  507. end;
  508. procedure ttgobj.gettempmanaged(list: TAsmList; def:tdef;temptype:ttemptype;out ref : treference);
  509. begin
  510. gettempinternal(list,def.size,def.alignment,temptype,def,true,ref);
  511. end;
  512. function ttgobj.istemp(const ref : treference) : boolean;
  513. begin
  514. { ref.index = R_NO was missing
  515. led to problems with local arrays
  516. with lower bound > 0 (PM) }
  517. if direction = 1 then
  518. begin
  519. istemp:=(ref.base=current_procinfo.framepointer) and
  520. (ref.index=NR_NO) and
  521. (ref.offset>=firsttemp);
  522. end
  523. else
  524. begin
  525. istemp:=(ref.base=current_procinfo.framepointer) and
  526. (ref.index=NR_NO) and
  527. (ref.offset<firsttemp);
  528. end;
  529. end;
  530. function ttgobj.sizeoftemp(list: TAsmList; const ref: treference): asizeint;
  531. var
  532. hp : ptemprecord;
  533. begin
  534. SizeOfTemp := -1;
  535. hp:=templist;
  536. while assigned(hp) do
  537. begin
  538. if (hp^.pos=ref.offset) then
  539. begin
  540. sizeoftemp := hp^.size;
  541. exit;
  542. end;
  543. hp := hp^.next;
  544. end;
  545. {$ifdef EXTDEBUG}
  546. comment(v_debug,'tgobj: (SizeOfTemp) temp at pos '+tostr(ref.offset)+' not found !');
  547. list.concat(tai_tempalloc.allocinfo(ref.offset,0,'temp not found'));
  548. {$endif}
  549. end;
  550. function ttgobj.changetemptype(list: tasmList; const ref:treference; temptype:ttemptype):boolean;
  551. var
  552. hp : ptemprecord;
  553. begin
  554. ChangeTempType:=false;
  555. hp:=templist;
  556. while assigned(hp) do
  557. begin
  558. if (hp^.pos=ref.offset) then
  559. begin
  560. if hp^.temptype<>tt_free then
  561. begin
  562. {$ifdef EXTDEBUG}
  563. if hp^.temptype=temptype then
  564. Comment(V_Warning,'tgobj: (ChangeTempType) temp'+
  565. ' at pos '+tostr(ref.offset)+ ' is already of the correct type !');
  566. list.concat(tai_tempalloc.allocinfo(hp^.pos,hp^.size,'type changed to '+TempTypeStr[temptype]));
  567. {$endif}
  568. ChangeTempType:=true;
  569. hp^.temptype:=temptype;
  570. end
  571. else
  572. begin
  573. {$ifdef EXTDEBUG}
  574. Comment(V_Warning,'tgobj: (ChangeTempType) temp'+
  575. ' at pos '+tostr(ref.offset)+ ' is already freed !');
  576. list.concat(tai_tempalloc.allocinfo(hp^.pos,hp^.size,'temp is already freed'));
  577. {$endif}
  578. end;
  579. exit;
  580. end;
  581. hp:=hp^.next;
  582. end;
  583. {$ifdef EXTDEBUG}
  584. Comment(V_Warning,'tgobj: (ChangeTempType) temp'+
  585. ' at pos '+tostr(ref.offset)+ ' not found !');
  586. list.concat(tai_tempalloc.allocinfo(ref.offset,0,'temp not found'));
  587. {$endif}
  588. end;
  589. function ttgobj.gettypeoftemp(const ref:treference): ttemptype;
  590. var
  591. hp : ptemprecord;
  592. begin
  593. hp:=templist;
  594. while assigned(hp) do
  595. begin
  596. if (hp^.pos=ref.offset) then
  597. begin
  598. if hp^.temptype<>tt_free then
  599. result:=hp^.temptype
  600. else
  601. internalerror(2007020810);
  602. exit;
  603. end;
  604. hp:=hp^.next;
  605. end;
  606. result:=tt_none;
  607. end;
  608. procedure ttgobj.UnGetTemp(list: TAsmList; const ref : treference);
  609. begin
  610. FreeTemp(list,ref.offset,[tt_normal,tt_noreuse,tt_persistent,tt_regallocator]);
  611. end;
  612. procedure ttgobj.UnGetIfTemp(list: TAsmList; const ref : treference);
  613. begin
  614. if istemp(ref) then
  615. FreeTemp(list,ref.offset,[tt_normal]);
  616. end;
  617. procedure ttgobj.getlocal(list: TAsmList; size: asizeint; def: tdef; var ref : treference);
  618. begin
  619. getlocal(list, size, def.alignment, def, ref);
  620. end;
  621. procedure ttgobj.getlocal(list: TAsmList; size: asizeint; alignment: shortint; def: tdef; var ref : treference);
  622. begin
  623. alignment:=used_align(alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax);
  624. alloctemp(list,size,alignment,tt_persistent,def,false,ref);
  625. end;
  626. procedure ttgobj.UnGetLocal(list: TAsmList; const ref : treference);
  627. begin
  628. FreeTemp(list,ref.offset,[tt_persistent]);
  629. end;
  630. end.