tgobj.pas 27 KB

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