tgobj.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. 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. Comment(V_Warning,'tgobj: (Freetemp) temp at pos '+tostr(pos.val)+ ' has different type ('+TempTypeStr[hp^.temptype]+'), not releasing');
  441. list.concat(tai_tempalloc.allocinfo(hp^.pos,hp^.size,'temp has wrong type ('+TempTypeStr[hp^.temptype]+') not releasing'));
  442. {$endif}
  443. exit;
  444. end;
  445. list.concat(tai_tempalloc.dealloc(hp^.pos,hp^.size));
  446. { set this block to free }
  447. hp^.temptype:=Used2Free[hp^.temptype];
  448. { Update tempfreelist }
  449. if assigned(hprevfree) then
  450. begin
  451. { Concat blocks when the previous block is free and
  452. there is no block assigned for a tdef }
  453. if assigned(hprev) and
  454. (hp^.temptype=tt_free) and
  455. not assigned(hp^.def) and
  456. (hprev^.temptype=tt_free) and
  457. not assigned(hprev^.def) then
  458. begin
  459. inc(hprev^.size,hp^.size);
  460. if direction=1 then
  461. hprev^.pos:=hp^.pos;
  462. hprev^.next:=hp^.next;
  463. dispose(hp);
  464. hp:=hprev;
  465. end
  466. else
  467. begin
  468. hp^.nextfree:=hprevfree^.nextfree;
  469. hprevfree^.nextfree:=hp;
  470. end;
  471. end
  472. else
  473. begin
  474. hp^.nextfree:=tempfreelist;
  475. tempfreelist:=hp;
  476. end;
  477. { Concat blocks when the next block is free and
  478. there is no block assigned for a tdef }
  479. hnext:=hp^.next;
  480. if assigned(hnext) and
  481. (hp^.temptype=tt_free) and
  482. not assigned(hp^.def) and
  483. (hnext^.temptype=tt_free) and
  484. not assigned(hnext^.def) then
  485. begin
  486. inc(hp^.size,hnext^.size);
  487. if direction=1 then
  488. hp^.pos:=hnext^.pos;
  489. hp^.nextfree:=hnext^.nextfree;
  490. hp^.next:=hnext^.next;
  491. dispose(hnext);
  492. end;
  493. { Stop }
  494. exit;
  495. end;
  496. if (hp^.temptype=tt_free) then
  497. hprevfree:=hp;
  498. hprev:=hp;
  499. hp:=hp^.next;
  500. end;
  501. end;
  502. procedure ttgobj.gethltemp(list: TAsmList; def: tdef; forcesize: asizeint; temptype: ttemptype; out ref: treference);
  503. begin
  504. gettemp(list,forcesize,def.alignment,temptype,ref);
  505. end;
  506. procedure ttgobj.gethltempmanaged(list: TAsmList; def: tdef; temptype: ttemptype; out ref: treference);
  507. begin
  508. gettempmanaged(list,def,temptype,ref);
  509. end;
  510. procedure ttgobj.gettemp(list: TAsmList; size: asizeint; alignment: shortint; temptype: ttemptype; out ref : treference);
  511. begin
  512. gettempinternal(list,size,alignment,temptype,nil,false,ref);
  513. end;
  514. procedure ttgobj.gettempinternal(list: TAsmList; size: asizeint; alignment: shortint; temptype: ttemptype; def: tdef; fini: boolean; out ref : treference);
  515. var
  516. varalign : shortint;
  517. begin
  518. varalign:=used_align(alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax);
  519. alloctemp(list,size,varalign,temptype,def,fini,ref);
  520. end;
  521. procedure ttgobj.gettempmanaged(list: TAsmList; def:tdef;temptype:ttemptype;out ref : treference);
  522. begin
  523. gettempinternal(list,def.size,def.alignment,temptype,def,true,ref);
  524. end;
  525. function ttgobj.istemp(const ref : treference) : boolean;
  526. begin
  527. istemp:=ref.temppos.val<>ctempposinvalid.val;
  528. end;
  529. function ttgobj.sizeoftemp(list: TAsmList; const ref: treference): asizeint;
  530. var
  531. hp : ptemprecord;
  532. begin
  533. SizeOfTemp := -1;
  534. hp:=templist;
  535. while assigned(hp) do
  536. begin
  537. if (hp^.pos=ref.temppos.val) then
  538. begin
  539. sizeoftemp := hp^.size;
  540. exit;
  541. end;
  542. hp := hp^.next;
  543. end;
  544. {$ifdef EXTDEBUG}
  545. comment(v_debug,'tgobj: (SizeOfTemp) temp at pos '+tostr(ref.temppos.val)+' not found !');
  546. list.concat(tai_tempalloc.allocinfo(ref.temppos.val,0,'temp not found'));
  547. {$endif}
  548. end;
  549. function ttgobj.changetemptype(list: tasmList; const ref:treference; temptype:ttemptype):boolean;
  550. var
  551. hp : ptemprecord;
  552. begin
  553. ChangeTempType:=false;
  554. hp:=templist;
  555. while assigned(hp) do
  556. begin
  557. if (hp^.pos=ref.temppos.val) then
  558. begin
  559. if hp^.temptype<>tt_free then
  560. begin
  561. {$ifdef EXTDEBUG}
  562. if hp^.temptype=temptype then
  563. Comment(V_Warning,'tgobj: (ChangeTempType) temp'+
  564. ' at pos '+tostr(ref.temppos.val)+ ' is already of the correct type !');
  565. list.concat(tai_tempalloc.allocinfo(hp^.pos,hp^.size,'type changed to '+TempTypeStr[temptype]));
  566. {$endif}
  567. ChangeTempType:=true;
  568. hp^.temptype:=temptype;
  569. end
  570. else
  571. begin
  572. {$ifdef EXTDEBUG}
  573. Comment(V_Warning,'tgobj: (ChangeTempType) temp'+
  574. ' at pos '+tostr(ref.temppos.val)+ ' is already freed !');
  575. list.concat(tai_tempalloc.allocinfo(hp^.pos,hp^.size,'temp is already freed'));
  576. {$endif}
  577. end;
  578. exit;
  579. end;
  580. hp:=hp^.next;
  581. end;
  582. {$ifdef EXTDEBUG}
  583. Comment(V_Warning,'tgobj: (ChangeTempType) temp'+
  584. ' at pos '+tostr(ref.temppos.val)+ ' not found !');
  585. list.concat(tai_tempalloc.allocinfo(ref.temppos.val,0,'temp not found'));
  586. {$endif}
  587. end;
  588. function ttgobj.gettypeoftemp(const ref:treference): ttemptype;
  589. var
  590. hp : ptemprecord;
  591. begin
  592. hp:=templist;
  593. while assigned(hp) do
  594. begin
  595. if (hp^.pos=ref.temppos.val) then
  596. begin
  597. if hp^.temptype<>tt_free then
  598. result:=hp^.temptype
  599. else
  600. internalerror(2007020810);
  601. exit;
  602. end;
  603. hp:=hp^.next;
  604. end;
  605. result:=tt_none;
  606. end;
  607. function ttgobj.isstartoftemp(const ref: treference): boolean;
  608. var
  609. hp: ptemprecord;
  610. tmpref: treference;
  611. begin
  612. hp:=templist;
  613. if ref.temppos.val=ctempposinvalid.val then
  614. begin
  615. result:=false;
  616. exit;
  617. end;
  618. while assigned(hp) do
  619. begin
  620. if (hp^.pos=ref.temppos.val) then
  621. begin
  622. temp_to_ref(hp, tmpref);
  623. result:=references_equal(ref, tmpref);
  624. exit;
  625. end;
  626. hp:=hp^.next;
  627. end;
  628. internalerror(2018042601);
  629. end;
  630. procedure ttgobj.temp_to_ref(p: ptemprecord; out ref: treference);
  631. var
  632. t: treftemppos;
  633. begin
  634. t.val:=p^.pos;
  635. reference_reset_base(ref,current_procinfo.framepointer,p^.pos,t,p^.alignment,[]);
  636. end;
  637. procedure ttgobj.UnGetTemp(list: TAsmList; const ref : treference);
  638. begin
  639. FreeTemp(list,ref.temppos,[tt_normal,tt_noreuse,tt_persistent,tt_regallocator]);
  640. end;
  641. procedure ttgobj.UnGetIfTemp(list: TAsmList; const ref : treference);
  642. begin
  643. if istemp(ref) then
  644. FreeTemp(list,ref.temppos,[tt_normal]);
  645. end;
  646. procedure ttgobj.getlocal(list: TAsmList; size: asizeint; def: tdef; var ref : treference);
  647. begin
  648. getlocal(list, size, def.alignment, def, ref);
  649. end;
  650. procedure ttgobj.getlocal(list: TAsmList; size: asizeint; alignment: shortint; def: tdef; var ref : treference);
  651. begin
  652. alignment:=used_align(alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax);
  653. alloctemp(list,size,alignment,tt_persistent,def,false,ref);
  654. end;
  655. procedure ttgobj.UnGetLocal(list: TAsmList; const ref : treference);
  656. begin
  657. FreeTemp(list,ref.temppos,[tt_persistent]);
  658. end;
  659. end.