tgobj.pas 27 KB

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