tgobj.pas 27 KB

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