tgobj.pas 25 KB

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