tgobj.pas 25 KB

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