tgobj.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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. globals,globtype,
  27. symtype,
  28. cpubase,cgbase,cgutils,
  29. aasmtai,aasmdata;
  30. type
  31. ptemprecord = ^ttemprecord;
  32. ttemprecord = record
  33. temptype : ttemptype;
  34. { finalize this temp if it's a managed type }
  35. fini : boolean;
  36. alignment : shortint;
  37. pos : asizeint;
  38. size : asizeint;
  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: asizeint; alignment: shortint; temptype: ttemptype; def: tdef; fini: boolean; out ref: treference); virtual;
  53. procedure freetemp(list: TAsmList; pos: treftemppos; temptypes: ttemptypeset);virtual;
  54. procedure gettempinternal(list: TAsmList; size: asizeint; alignment: shortint; 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: asizeint); virtual;
  76. procedure setalignmentmismatch(l: shortint); 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: asizeint; alignment: shortint; 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): asizeint;
  88. function changetemptype(list: TAsmList; const ref:treference;temptype:ttemptype):boolean;
  89. function gettypeoftemp(const ref:treference): ttemptype;
  90. {# Returns a reference corresponding to a temp }
  91. procedure temp_to_ref(p: ptemprecord; out ref: treference); virtual;
  92. {# Returns TRUE if the reference ref is allocated in temporary volatile memory space,
  93. otherwise returns FALSE.
  94. @param(ref reference to verify)
  95. }
  96. function istemp(const ref : treference) : boolean; virtual;
  97. {# Frees a reference @var(ref) which was allocated in the volatile temporary memory space.
  98. The freed space can later be reallocated and reused. If this reference
  99. is not in the temporary memory, it is simply not freed.
  100. }
  101. procedure ungetiftemp(list: TAsmList; const ref : treference); virtual;
  102. { Allocate space for a local }
  103. procedure getlocal(list: TAsmList; size: asizeint; def: tdef; var ref : treference);
  104. procedure getlocal(list: TAsmList; size: asizeint; alignment: shortint; def: tdef; var ref : treference); virtual;
  105. procedure UnGetLocal(list: TAsmList; const ref : treference);
  106. end;
  107. ttgobjclass = class of ttgobj;
  108. var
  109. tg: ttgobj;
  110. tgobjclass: ttgobjclass = ttgobj;
  111. procedure location_freetemp(list:TAsmList; const l : tlocation);
  112. implementation
  113. uses
  114. cutils,
  115. verbose,
  116. procinfo;
  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) or defined(aarch64)}
  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: asizeint);
  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: shortint);
  195. begin
  196. alignmismatch:=l*direction;
  197. end;
  198. procedure ttgobj.alloctemp(list: TAsmList; size: asizeint; alignment: shortint; 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^.alignment:=alignment;
  350. tl^.nextfree:=nil;
  351. { Resize the old block }
  352. dec(bestslot^.size,size);
  353. end;
  354. tl^.temptype:=temptype;
  355. tl^.def:=def;
  356. tl^.nextfree:=nil;
  357. end
  358. else
  359. begin
  360. { now we can create the templist entry }
  361. new(tl);
  362. tl^.temptype:=temptype;
  363. tl^.def:=def;
  364. {$push}
  365. {$r-}
  366. {$warn 6018 off}
  367. {$warn 4044 off}
  368. { Extend the temp }
  369. if direction=-1 then
  370. begin
  371. if qword(align(-lasttemp-alignmismatch,alignment))+size+alignmismatch>high(tl^.pos) then
  372. CGMessage(cg_e_localsize_too_big);
  373. lasttemp:=(-align(-lasttemp-alignmismatch,alignment))-size-alignmismatch;
  374. tl^.pos:=lasttemp;
  375. end
  376. else
  377. begin
  378. tl^.pos:=align(lasttemp+alignmismatch,alignment)-alignmismatch;
  379. if qword(tl^.pos)+size>high(tl^.pos) then
  380. CGMessage(cg_e_localsize_too_big);
  381. lasttemp:=tl^.pos+size;
  382. end;
  383. {$pop}
  384. tl^.fini:=fini;
  385. tl^.alignment:=alignment;
  386. tl^.size:=size;
  387. tl^.next:=templist;
  388. tl^.nextfree:=nil;
  389. templist:=tl;
  390. end;
  391. {$ifdef EXTDEBUG}
  392. tl^.posinfo:=current_filepos;
  393. if assigned(tl^.def) then
  394. list.concat(tai_tempalloc.allocinfo(tl^.pos,tl^.size,'allocated with type '+TempTypeStr[tl^.temptype]+' for def '+tl^.def.typename))
  395. else
  396. list.concat(tai_tempalloc.allocinfo(tl^.pos,tl^.size,'allocated with type '+TempTypeStr[tl^.temptype]));
  397. {$else}
  398. list.concat(tai_tempalloc.alloc(tl^.pos,tl^.size));
  399. {$endif}
  400. temp_to_ref(tl,ref);
  401. end;
  402. procedure ttgobj.FreeTemp(list: TAsmList; pos: treftemppos; temptypes: ttemptypeset);
  403. var
  404. hp,hnext,hprev,hprevfree : ptemprecord;
  405. begin
  406. hp:=templist;
  407. hprev:=nil;
  408. hprevfree:=nil;
  409. while assigned(hp) do
  410. begin
  411. if (hp^.pos=pos.val) then
  412. begin
  413. { check if already freed }
  414. if hp^.temptype in FreeTempTypes then
  415. begin
  416. {$ifdef EXTDEBUG}
  417. Comment(V_Warning,'tgobj: (FreeTemp) temp at pos '+tostr(pos.val)+ ' is already free !');
  418. list.concat(tai_tempalloc.allocinfo(hp^.pos,hp^.size,'temp is already freed'));
  419. {$endif}
  420. exit;
  421. end;
  422. { check type that are allowed to be released }
  423. if not(hp^.temptype in temptypes) then
  424. begin
  425. {$ifdef EXTDEBUG}
  426. Comment(V_Debug,'tgobj: (Freetemp) temp at pos '+tostr(pos.val)+ ' has different type ('+TempTypeStr[hp^.temptype]+'), not releasing');
  427. list.concat(tai_tempalloc.allocinfo(hp^.pos,hp^.size,'temp has wrong type ('+TempTypeStr[hp^.temptype]+') not releasing'));
  428. {$endif}
  429. exit;
  430. end;
  431. list.concat(tai_tempalloc.dealloc(hp^.pos,hp^.size));
  432. { set this block to free }
  433. hp^.temptype:=Used2Free[hp^.temptype];
  434. { Update tempfreelist }
  435. if assigned(hprevfree) then
  436. begin
  437. { Concat blocks when the previous block is free and
  438. there is no block assigned for a tdef }
  439. if assigned(hprev) and
  440. (hp^.temptype=tt_free) and
  441. not assigned(hp^.def) and
  442. (hprev^.temptype=tt_free) and
  443. not assigned(hprev^.def) then
  444. begin
  445. inc(hprev^.size,hp^.size);
  446. if direction=1 then
  447. hprev^.pos:=hp^.pos;
  448. hprev^.next:=hp^.next;
  449. dispose(hp);
  450. hp:=hprev;
  451. end
  452. else
  453. begin
  454. hp^.nextfree:=hprevfree^.nextfree;
  455. hprevfree^.nextfree:=hp;
  456. end;
  457. end
  458. else
  459. begin
  460. hp^.nextfree:=tempfreelist;
  461. tempfreelist:=hp;
  462. end;
  463. { Concat blocks when the next block is free and
  464. there is no block assigned for a tdef }
  465. hnext:=hp^.next;
  466. if assigned(hnext) and
  467. (hp^.temptype=tt_free) and
  468. not assigned(hp^.def) and
  469. (hnext^.temptype=tt_free) and
  470. not assigned(hnext^.def) then
  471. begin
  472. inc(hp^.size,hnext^.size);
  473. if direction=1 then
  474. hp^.pos:=hnext^.pos;
  475. hp^.nextfree:=hnext^.nextfree;
  476. hp^.next:=hnext^.next;
  477. dispose(hnext);
  478. end;
  479. { Stop }
  480. exit;
  481. end;
  482. if (hp^.temptype=tt_free) then
  483. hprevfree:=hp;
  484. hprev:=hp;
  485. hp:=hp^.next;
  486. end;
  487. end;
  488. procedure ttgobj.gethltemp(list: TAsmList; def: tdef; forcesize: asizeint; temptype: ttemptype; out ref: treference);
  489. begin
  490. gettemp(list,forcesize,def.alignment,temptype,ref);
  491. end;
  492. procedure ttgobj.gethltempmanaged(list: TAsmList; def: tdef; temptype: ttemptype; out ref: treference);
  493. begin
  494. gettempmanaged(list,def,temptype,ref);
  495. end;
  496. procedure ttgobj.gettemp(list: TAsmList; size: asizeint; alignment: shortint; temptype: ttemptype; out ref : treference);
  497. begin
  498. gettempinternal(list,size,alignment,temptype,nil,false,ref);
  499. end;
  500. procedure ttgobj.gettempinternal(list: TAsmList; size: asizeint; alignment: shortint; temptype: ttemptype; def: tdef; fini: boolean; out ref : treference);
  501. var
  502. varalign : shortint;
  503. begin
  504. varalign:=used_align(alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax);
  505. alloctemp(list,size,varalign,temptype,def,fini,ref);
  506. end;
  507. procedure ttgobj.gettempmanaged(list: TAsmList; def:tdef;temptype:ttemptype;out ref : treference);
  508. begin
  509. gettempinternal(list,def.size,def.alignment,temptype,def,true,ref);
  510. end;
  511. function ttgobj.istemp(const ref : treference) : boolean;
  512. begin
  513. istemp:=ref.temppos.val<>ctempposinvalid.val;
  514. end;
  515. function ttgobj.sizeoftemp(list: TAsmList; const ref: treference): asizeint;
  516. var
  517. hp : ptemprecord;
  518. begin
  519. SizeOfTemp := -1;
  520. hp:=templist;
  521. while assigned(hp) do
  522. begin
  523. if (hp^.pos=ref.temppos.val) then
  524. begin
  525. sizeoftemp := hp^.size;
  526. exit;
  527. end;
  528. hp := hp^.next;
  529. end;
  530. {$ifdef EXTDEBUG}
  531. comment(v_debug,'tgobj: (SizeOfTemp) temp at pos '+tostr(ref.temppos.val)+' not found !');
  532. list.concat(tai_tempalloc.allocinfo(ref.temppos.val,0,'temp not found'));
  533. {$endif}
  534. end;
  535. function ttgobj.changetemptype(list: tasmList; const ref:treference; temptype:ttemptype):boolean;
  536. var
  537. hp : ptemprecord;
  538. begin
  539. ChangeTempType:=false;
  540. hp:=templist;
  541. while assigned(hp) do
  542. begin
  543. if (hp^.pos=ref.temppos.val) then
  544. begin
  545. if hp^.temptype<>tt_free then
  546. begin
  547. {$ifdef EXTDEBUG}
  548. if hp^.temptype=temptype then
  549. Comment(V_Warning,'tgobj: (ChangeTempType) temp'+
  550. ' at pos '+tostr(ref.temppos.val)+ ' is already of the correct type !');
  551. list.concat(tai_tempalloc.allocinfo(hp^.pos,hp^.size,'type changed to '+TempTypeStr[temptype]));
  552. {$endif}
  553. ChangeTempType:=true;
  554. hp^.temptype:=temptype;
  555. end
  556. else
  557. begin
  558. {$ifdef EXTDEBUG}
  559. Comment(V_Warning,'tgobj: (ChangeTempType) temp'+
  560. ' at pos '+tostr(ref.temppos.val)+ ' is already freed !');
  561. list.concat(tai_tempalloc.allocinfo(hp^.pos,hp^.size,'temp is already freed'));
  562. {$endif}
  563. end;
  564. exit;
  565. end;
  566. hp:=hp^.next;
  567. end;
  568. {$ifdef EXTDEBUG}
  569. Comment(V_Warning,'tgobj: (ChangeTempType) temp'+
  570. ' at pos '+tostr(ref.temppos.val)+ ' not found !');
  571. list.concat(tai_tempalloc.allocinfo(ref.temppos.val,0,'temp not found'));
  572. {$endif}
  573. end;
  574. function ttgobj.gettypeoftemp(const ref:treference): ttemptype;
  575. var
  576. hp : ptemprecord;
  577. begin
  578. hp:=templist;
  579. while assigned(hp) do
  580. begin
  581. if (hp^.pos=ref.temppos.val) then
  582. begin
  583. if hp^.temptype<>tt_free then
  584. result:=hp^.temptype
  585. else
  586. internalerror(2007020810);
  587. exit;
  588. end;
  589. hp:=hp^.next;
  590. end;
  591. result:=tt_none;
  592. end;
  593. procedure ttgobj.temp_to_ref(p: ptemprecord; out ref: treference);
  594. var
  595. t: treftemppos;
  596. begin
  597. t.val:=p^.pos;
  598. reference_reset_base(ref,current_procinfo.framepointer,p^.pos,t,p^.alignment,[]);
  599. end;
  600. procedure ttgobj.UnGetTemp(list: TAsmList; const ref : treference);
  601. begin
  602. FreeTemp(list,ref.temppos,[tt_normal,tt_noreuse,tt_persistent,tt_regallocator]);
  603. end;
  604. procedure ttgobj.UnGetIfTemp(list: TAsmList; const ref : treference);
  605. begin
  606. if istemp(ref) then
  607. FreeTemp(list,ref.temppos,[tt_normal]);
  608. end;
  609. procedure ttgobj.getlocal(list: TAsmList; size: asizeint; def: tdef; var ref : treference);
  610. begin
  611. getlocal(list, size, def.alignment, def, ref);
  612. end;
  613. procedure ttgobj.getlocal(list: TAsmList; size: asizeint; alignment: shortint; def: tdef; var ref : treference);
  614. begin
  615. alignment:=used_align(alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax);
  616. alloctemp(list,size,alignment,tt_persistent,def,false,ref);
  617. end;
  618. procedure ttgobj.UnGetLocal(list: TAsmList; const ref : treference);
  619. begin
  620. FreeTemp(list,ref.temppos,[tt_persistent]);
  621. end;
  622. end.