symtype.pas 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Pierre Muller
  3. This unit handles the symbol tables
  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. unit symtype;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cutils,
  23. cclasses,
  24. { global }
  25. globtype,globals,constexp,
  26. { symtable }
  27. symconst,symbase,
  28. { aasm }
  29. aasmbase,ppu
  30. ;
  31. type
  32. {************************************************
  33. Required Forwards
  34. ************************************************}
  35. tsym = class;
  36. Tcompilerppufile=class;
  37. {************************************************
  38. TDef
  39. ************************************************}
  40. tgetsymtable = (gs_none,gs_record,gs_local,gs_para);
  41. tdef = class(TDefEntry)
  42. protected
  43. { whether this def is already registered in the unit's def list }
  44. function registered : boolean;
  45. public
  46. typesym : tsym; { which type the definition was generated this def }
  47. { stabs debugging }
  48. stab_number : word;
  49. dbg_state : tdefdbgstatus;
  50. defoptions : tdefoptions;
  51. defstates : tdefstates;
  52. constructor create(dt:tdeftyp);
  53. procedure buildderef;virtual;abstract;
  54. procedure buildderefimpl;virtual;abstract;
  55. procedure deref;virtual;abstract;
  56. procedure derefimpl;virtual;abstract;
  57. function typename:string;
  58. function fulltypename:string;
  59. function GetTypeName:string;virtual;
  60. function typesymbolprettyname:string;virtual;
  61. function mangledparaname:string;
  62. function getmangledparaname:TSymStr;virtual;
  63. function rtti_mangledname(rt:trttitype):TSymStr;virtual;abstract;
  64. function OwnerHierarchyName: string; virtual; abstract;
  65. function fullownerhierarchyname(skipprocparams:boolean):TSymStr;virtual;abstract;
  66. function unique_id_str: string;
  67. function size:asizeint;virtual;abstract;
  68. function packedbitsize:asizeint;virtual;
  69. function alignment:shortint;virtual;abstract;
  70. { alignment when this type appears in a record/class/... }
  71. function structalignment:shortint;virtual;
  72. function aggregatealignment:shortint;virtual;
  73. function getvardef:longint;virtual;abstract;
  74. function getparentdef:tdef;virtual;
  75. function getsymtable(t:tgetsymtable):TSymtable;virtual;
  76. function is_publishable:boolean;virtual;abstract;
  77. function needs_inittable:boolean;virtual;abstract;
  78. { contains a (managed) child that is not initialized to 0/Nil }
  79. function has_non_trivial_init_child(check_parent:boolean):boolean;virtual;abstract;
  80. function needs_separate_initrtti:boolean;virtual;abstract;
  81. procedure ChangeOwner(st:TSymtable);
  82. function getreusablesymtab: tsymtable;
  83. procedure register_created_object_type;virtual;
  84. function get_top_level_symtable: tsymtable;
  85. { only valid for registered defs and defs for which a unique id string
  86. has been requested; otherwise, first call register_def }
  87. function deflist_index: longint;
  88. procedure register_def; virtual; abstract;
  89. property is_registered: boolean read registered;
  90. end;
  91. {************************************************
  92. TSym
  93. ************************************************}
  94. { this object is the base for all symbol objects }
  95. { tsym }
  96. tsym = class(TSymEntry)
  97. protected
  98. function registered : boolean;
  99. public
  100. fileinfo : tfileposinfo;
  101. { size of fileinfo is 10 bytes, so if a >word aligned type would follow,
  102. two bytes of memory would be wasted, so we put two one byte fields over here }
  103. visibility : tvisibility;
  104. isdbgwritten : boolean;
  105. symoptions : tsymoptions;
  106. refs : longint;
  107. reflist : TLinkedList;
  108. { deprecated optionally can have a message }
  109. deprecatedmsg: pshortstring;
  110. constructor create(st:tsymtyp;const aname:string);
  111. destructor destroy;override;
  112. function mangledname:TSymStr; virtual;
  113. function prettyname:string; virtual;
  114. procedure buildderef;virtual;
  115. procedure deref;virtual;
  116. procedure ChangeOwner(st:TSymtable);
  117. procedure IncRefCount;
  118. procedure IncRefCountBy(AValue : longint);
  119. procedure MaybeCreateRefList;
  120. procedure AddRef;
  121. procedure register_sym; virtual; abstract;
  122. property is_registered:boolean read registered;
  123. end;
  124. tsymarr = array[0..maxlongint div sizeof(pointer)-1] of tsym;
  125. psymarr = ^tsymarr;
  126. {************************************************
  127. TDeref
  128. ************************************************}
  129. tderef = object
  130. dataidx : longint;
  131. procedure reset;
  132. procedure build(s:TObject);
  133. function resolve:TObject;
  134. end;
  135. pderef = ^tderef;
  136. {************************************************
  137. tpropaccesslist
  138. ************************************************}
  139. ppropaccesslistitem = ^tpropaccesslistitem;
  140. tpropaccesslistitem = record
  141. sltype : tsltype;
  142. next : ppropaccesslistitem;
  143. case byte of
  144. 0 : (sym : tsym; symderef : tderef);
  145. 1 : (value : TConstExprInt; valuedef: tdef; valuedefderef:tderef);
  146. 2 : (def: tdef; defderef:tderef);
  147. end;
  148. tpropaccesslist = class
  149. procdef : tdef;
  150. procdefderef : tderef;
  151. firstsym,
  152. lastsym : ppropaccesslistitem;
  153. constructor create;
  154. destructor destroy;override;
  155. function empty:boolean;
  156. function getcopy: tpropaccesslist;
  157. procedure addsym(slt:tsltype;p:tsym);
  158. procedure addconst(slt:tsltype;const v:TConstExprInt;d:tdef);
  159. procedure addtype(slt:tsltype;d:tdef);
  160. procedure addsymderef(slt:tsltype;d:tderef);
  161. procedure addconstderef(slt:tsltype;const v:TConstExprInt;d:tderef);
  162. procedure addtypederef(slt:tsltype;d:tderef);
  163. procedure clear;
  164. procedure resolve;
  165. procedure buildderef;
  166. end;
  167. {************************************************
  168. Tcompilerppufile
  169. ************************************************}
  170. tcompilerppufile=class(tppufile)
  171. public
  172. procedure checkerror;
  173. procedure getguid(var g: tguid);
  174. function getexprint:Tconstexprint;
  175. procedure getposinfo(var p:tfileposinfo);
  176. procedure getderef(var d:tderef);
  177. function getpropaccesslist:tpropaccesslist;
  178. function getasmsymbol:tasmsymbol;
  179. procedure putguid(const g: tguid);
  180. procedure putexprint(const v:tconstexprint);
  181. procedure putposinfo(const p:tfileposinfo);
  182. procedure putderef(const d:tderef);
  183. procedure putpropaccesslist(p:tpropaccesslist);
  184. procedure putasmsymbol(s:tasmsymbol);
  185. protected
  186. procedure RaiseAssertion(Code: Longint); override;
  187. end;
  188. {$ifdef MEMDEBUG}
  189. var
  190. memmanglednames,
  191. memprocpara,
  192. memprocparast,
  193. memproclocalst,
  194. memprocnodetree : tmemdebug;
  195. {$endif MEMDEBUG}
  196. function FindUnitSymtable(st:TSymtable):TSymtable;
  197. implementation
  198. uses
  199. crefs,
  200. verbose,
  201. fmodule
  202. ;
  203. {****************************************************************************
  204. Utils
  205. ****************************************************************************}
  206. function FindUnitSymtable(st:TSymtable):TSymtable;
  207. begin
  208. result:=nil;
  209. repeat
  210. if not assigned(st) then
  211. internalerror(200602034);
  212. case st.symtabletype of
  213. localmacrosymtable,
  214. exportedmacrosymtable,
  215. staticsymtable,
  216. globalsymtable :
  217. begin
  218. result:=st;
  219. exit;
  220. end;
  221. recordsymtable,
  222. enumsymtable,
  223. arraysymtable,
  224. localsymtable,
  225. parasymtable,
  226. ObjectSymtable :
  227. st:=st.defowner.owner;
  228. else
  229. internalerror(200602035);
  230. end;
  231. until false;
  232. end;
  233. {****************************************************************************
  234. Tdef
  235. ****************************************************************************}
  236. function tdef.registered: boolean;
  237. begin
  238. result:=defid>defid_not_registered;
  239. end;
  240. constructor tdef.create(dt:tdeftyp);
  241. begin
  242. inherited create;
  243. typ:=dt;
  244. owner := nil;
  245. typesym := nil;
  246. defoptions:=[];
  247. dbg_state:=dbg_state_unused;
  248. stab_number:=0;
  249. defid:=defid_not_registered;
  250. end;
  251. function tdef.typename:string;
  252. begin
  253. result:=OwnerHierarchyName;
  254. if assigned(typesym) and
  255. not(typ in [procvardef,procdef]) and
  256. (typesym.realname[1]<>'$') then
  257. result:=result+typesym.realname
  258. else
  259. result:=result+GetTypeName;
  260. end;
  261. function tdef.fulltypename:string;
  262. begin
  263. result:=fullownerhierarchyname(false);
  264. if assigned(typesym) and
  265. not(typ in [procvardef,procdef]) and
  266. (typesym.realname[1]<>'$') then
  267. result:=result+typesym.realname
  268. else
  269. result:=result+GetTypeName;
  270. end;
  271. function tdef.GetTypeName : string;
  272. begin
  273. GetTypeName:='<unknown type>'
  274. end;
  275. function tdef.typesymbolprettyname:string;
  276. begin
  277. result:=OwnerHierarchyName;
  278. if assigned(typesym) then
  279. result:=result+typesym.prettyname
  280. else
  281. result:=result+'<no type symbol>'
  282. end;
  283. function tdef.mangledparaname:string;
  284. begin
  285. result:=OwnerHierarchyName;
  286. if assigned(typesym) then
  287. mangledparaname:=result+typesym.name
  288. else
  289. mangledparaname:=result+getmangledparaname;
  290. end;
  291. function tdef.getmangledparaname:TSymStr;
  292. begin
  293. result:='<unknown type>';
  294. end;
  295. function tdef.unique_id_str: string;
  296. begin
  297. if (defid=defid_not_registered) or
  298. (defid=defid_registered_nost) then
  299. begin
  300. if not assigned(current_module) then
  301. internalerror(2015102505);
  302. current_module.deflist.Add(self);
  303. { invert the defid to indicate that it was only set because we
  304. needed a unique number -- then add defid_not_registered so we
  305. don't get the values between defid_registered and 0 }
  306. defid:=-(current_module.deflist.Count-1)+defid_not_registered-1;
  307. end;
  308. { use deflist_index so that it will remain the same if def first gets a
  309. defid just for the unique id (as above) and later it gets registered
  310. because it must be saved to the ppu }
  311. result:=hexstr(deflist_index,sizeof(defid)*2);
  312. end;
  313. function tdef.getparentdef:tdef;
  314. begin
  315. result:=nil;
  316. end;
  317. function tdef.getsymtable(t:tgetsymtable):TSymtable;
  318. begin
  319. result:=nil;
  320. end;
  321. function tdef.packedbitsize:asizeint;
  322. begin
  323. result:=size * 8;
  324. end;
  325. function tdef.structalignment: shortint;
  326. begin
  327. result:=alignment;
  328. end;
  329. function tdef.aggregatealignment: shortint;
  330. begin
  331. if Assigned(Owner) and Assigned(Owner.defowner) and (Owner.defowner is TDef) and (Owner.defowner <> Self) then
  332. Result := max(structalignment, TDef(Owner.defowner).aggregatealignment)
  333. else
  334. Result := structalignment;
  335. end;
  336. procedure tdef.ChangeOwner(st:TSymtable);
  337. begin
  338. // if assigned(Owner) then
  339. // Owner.DefList.List[i]:=nil;
  340. Owner:=st;
  341. Owner.DefList.Add(self);
  342. end;
  343. function tdef.getreusablesymtab: tsymtable;
  344. var
  345. origowner: TSymtable;
  346. begin
  347. { if the original def was in a localsymtable, don't create a
  348. reusable copy in the unit's staticsymtable since the localsymtable
  349. won't be saved to the ppu and as a result we can get unreachable
  350. defs when reloading the derived ones from the ppu }
  351. origowner:=owner;
  352. while not(origowner.symtabletype in [localsymtable,staticsymtable,globalsymtable,stt_excepTSymtable]) do
  353. origowner:=origowner.defowner.owner;
  354. { if the def is in an exceptionsymtable, we can't create a reusable
  355. def because the original one will be freed when the (always
  356. temprary) exceptionsymtable is freed }
  357. if origowner.symtabletype=stt_excepTSymtable then
  358. internalerror(2015111701)
  359. else if origowner.symtabletype=localsymtable then
  360. result:=origowner
  361. else if assigned(current_module.localsymtable) then
  362. result:=current_module.localsymtable
  363. else
  364. result:=current_module.globalsymtable;
  365. end;
  366. procedure tdef.register_created_object_type;
  367. begin
  368. end;
  369. function tdef.get_top_level_symtable: tsymtable;
  370. begin
  371. result:=owner;
  372. while assigned(result) and
  373. assigned(result.defowner) do
  374. result:=tdef(result.defowner).owner;
  375. end;
  376. function tdef.deflist_index: longint;
  377. begin
  378. if defid<defid_not_registered then
  379. result:=-(defid-defid_not_registered+1)
  380. else if defid>=0 then
  381. result:=defid
  382. else
  383. internalerror(2015102502)
  384. end;
  385. {****************************************************************************
  386. TSYM (base for all symtypes)
  387. ****************************************************************************}
  388. function tsym.registered: boolean;
  389. begin
  390. result:=symid>symid_not_registered;
  391. end;
  392. constructor tsym.create(st:tsymtyp;const aname:string);
  393. begin
  394. inherited CreateNotOwned;
  395. realname:=aname;
  396. typ:=st;
  397. RefList:=nil;
  398. symoptions:=[];
  399. fileinfo:=current_tokenpos;
  400. isdbgwritten := false;
  401. visibility:=vis_public;
  402. deprecatedmsg:=nil;
  403. symid:=symid_not_registered;
  404. end;
  405. destructor Tsym.destroy;
  406. begin
  407. stringdispose(deprecatedmsg);
  408. if assigned(RefList) then
  409. RefList.Free;
  410. inherited Destroy;
  411. end;
  412. procedure Tsym.IncRefCount;
  413. begin
  414. inc(refs);
  415. if cs_browser in current_settings.moduleswitches then
  416. begin
  417. MaybeCreateRefList;
  418. AddRef;
  419. end;
  420. end;
  421. procedure Tsym.IncRefCountBy(AValue : longint);
  422. begin
  423. inc(refs,AValue);
  424. end;
  425. procedure Tsym.MaybeCreateRefList;
  426. begin
  427. if not assigned(reflist) then
  428. reflist:=TRefLinkedList.create;
  429. end;
  430. procedure Tsym.AddRef;
  431. var
  432. RefItem: TRefItem;
  433. begin
  434. RefItem:=TRefItem.Create(current_tokenpos);
  435. RefList.Concat(RefItem);
  436. end;
  437. procedure Tsym.buildderef;
  438. begin
  439. end;
  440. procedure Tsym.deref;
  441. begin
  442. end;
  443. function tsym.mangledname : TSymStr;
  444. begin
  445. internalerror(200204171);
  446. result:='';
  447. end;
  448. function tsym.prettyname : string;
  449. begin
  450. result:=realname;
  451. end;
  452. procedure tsym.ChangeOwner(st:TSymtable);
  453. begin
  454. Owner:=st;
  455. inherited ChangeOwner(Owner.SymList);
  456. end;
  457. {****************************************************************************
  458. tpropaccesslist
  459. ****************************************************************************}
  460. constructor tpropaccesslist.create;
  461. begin
  462. procdef:=nil; { needed for procedures }
  463. firstsym:=nil;
  464. lastsym:=nil;
  465. end;
  466. destructor tpropaccesslist.destroy;
  467. begin
  468. clear;
  469. end;
  470. function tpropaccesslist.empty:boolean;
  471. begin
  472. empty:=(firstsym=nil);
  473. end;
  474. function tpropaccesslist.getcopy: tpropaccesslist;
  475. var
  476. hp, dest : ppropaccesslistitem;
  477. begin
  478. result:=tpropaccesslist.create;
  479. result.procdef:=procdef;
  480. hp:=firstsym;
  481. while assigned(hp) do
  482. begin
  483. new(dest);
  484. dest^:=hp^;
  485. dest^.next:=nil;
  486. if not assigned(result.firstsym) then
  487. result.firstsym:=dest;
  488. if assigned(result.lastsym) then
  489. result.lastsym^.next:=dest;
  490. result.lastsym:=dest;
  491. hp:=hp^.next;
  492. end;
  493. end;
  494. procedure tpropaccesslist.clear;
  495. var
  496. hp : ppropaccesslistitem;
  497. begin
  498. while assigned(firstsym) do
  499. begin
  500. hp:=firstsym;
  501. firstsym:=firstsym^.next;
  502. dispose(hp);
  503. end;
  504. firstsym:=nil;
  505. lastsym:=nil;
  506. procdef:=nil;
  507. end;
  508. procedure tpropaccesslist.addsym(slt:tsltype;p:tsym);
  509. var
  510. hp : ppropaccesslistitem;
  511. begin
  512. new(hp);
  513. fillchar(hp^,sizeof(tpropaccesslistitem),0);
  514. hp^.sltype:=slt;
  515. hp^.sym:=p;
  516. hp^.symderef.reset;
  517. if assigned(lastsym) then
  518. lastsym^.next:=hp
  519. else
  520. firstsym:=hp;
  521. lastsym:=hp;
  522. end;
  523. procedure tpropaccesslist.addconst(slt:tsltype;const v:TConstExprInt;d:tdef);
  524. var
  525. hp : ppropaccesslistitem;
  526. begin
  527. new(hp);
  528. fillchar(hp^,sizeof(tpropaccesslistitem),0);
  529. hp^.sltype:=slt;
  530. hp^.value:=v;
  531. hp^.valuedef:=d;
  532. hp^.valuedefderef.reset;
  533. if assigned(lastsym) then
  534. lastsym^.next:=hp
  535. else
  536. firstsym:=hp;
  537. lastsym:=hp;
  538. end;
  539. procedure tpropaccesslist.addtype(slt:tsltype;d:tdef);
  540. var
  541. hp : ppropaccesslistitem;
  542. begin
  543. new(hp);
  544. fillchar(hp^,sizeof(tpropaccesslistitem),0);
  545. hp^.sltype:=slt;
  546. hp^.def:=d;
  547. hp^.defderef.reset;
  548. if assigned(lastsym) then
  549. lastsym^.next:=hp
  550. else
  551. firstsym:=hp;
  552. lastsym:=hp;
  553. end;
  554. procedure tpropaccesslist.addsymderef(slt:tsltype;d:tderef);
  555. begin
  556. addsym(slt,nil);
  557. lastsym^.symderef:=d;
  558. end;
  559. procedure tpropaccesslist.addconstderef(slt:tsltype;const v:TConstExprInt;d:tderef);
  560. begin
  561. addconst(slt,v,nil);
  562. lastsym^.valuedefderef:=d;
  563. end;
  564. procedure tpropaccesslist.addtypederef(slt:tsltype;d:tderef);
  565. begin
  566. addtype(slt,nil);
  567. lastsym^.defderef:=d;
  568. end;
  569. procedure tpropaccesslist.resolve;
  570. var
  571. hp : ppropaccesslistitem;
  572. begin
  573. procdef:=tdef(procdefderef.resolve);
  574. hp:=firstsym;
  575. while assigned(hp) do
  576. begin
  577. case hp^.sltype of
  578. sl_call,
  579. sl_load,
  580. sl_subscript :
  581. hp^.sym:=tsym(hp^.symderef.resolve);
  582. sl_absolutetype,
  583. sl_typeconv :
  584. hp^.def:=tdef(hp^.defderef.resolve);
  585. sl_vec:
  586. hp^.valuedef:=tdef(hp^.valuedefderef.resolve);
  587. else
  588. internalerror(200110205);
  589. end;
  590. hp:=hp^.next;
  591. end;
  592. end;
  593. procedure tpropaccesslist.buildderef;
  594. var
  595. hp : ppropaccesslistitem;
  596. begin
  597. procdefderef.build(procdef);
  598. hp:=firstsym;
  599. while assigned(hp) do
  600. begin
  601. case hp^.sltype of
  602. sl_call,
  603. sl_load,
  604. sl_subscript :
  605. hp^.symderef.build(hp^.sym);
  606. sl_absolutetype,
  607. sl_typeconv :
  608. hp^.defderef.build(hp^.def);
  609. sl_vec:
  610. hp^.valuedefderef.build(hp^.valuedef);
  611. else
  612. internalerror(200110205);
  613. end;
  614. hp:=hp^.next;
  615. end;
  616. end;
  617. {****************************************************************************
  618. Tderef
  619. ****************************************************************************}
  620. procedure tderef.reset;
  621. begin
  622. dataidx:=-1;
  623. end;
  624. procedure tderef.build(s:TObject);
  625. var
  626. len : byte;
  627. st : TSymtable;
  628. data : array[0..255] of byte;
  629. idx : word;
  630. begin
  631. { skip length byte }
  632. len:=1;
  633. if assigned(s) then
  634. begin
  635. { TODO: ugly hack}
  636. if s is tsym then
  637. begin
  638. { if it has been registered but it wasn't put in a symbol table,
  639. this symbol shouldn't be written to a ppu }
  640. if tsym(s).SymId=symid_registered_nost then
  641. Internalerror(2015102504);
  642. if not tsym(s).registered then
  643. tsym(s).register_sym;
  644. st:=FindUnitSymtable(tsym(s).owner)
  645. end
  646. else if s is tdef then
  647. begin
  648. { same as above }
  649. if tdef(s).defid=defid_registered_nost then
  650. Internalerror(2015102505);
  651. if not tdef(s).registered then
  652. tdef(s).register_def;
  653. st:=FindUnitSymtable(tdef(s).owner);
  654. end
  655. else
  656. internalerror(2016090201);
  657. if not st.iscurrentunit then
  658. begin
  659. { register that the unit is needed for resolving }
  660. data[len]:=ord(deref_unit);
  661. idx:=current_module.derefidx_unit(st.moduleid);
  662. data[len+1]:=idx shr 8 and $ff;
  663. data[len+2]:=idx and $ff;
  664. inc(len,3);
  665. end;
  666. if s is tsym then
  667. begin
  668. data[len]:=ord(deref_symid);
  669. data[len+1]:=tsym(s).symid shr 24 and $ff;
  670. data[len+2]:=tsym(s).symid shr 16 and $ff;
  671. data[len+3]:=tsym(s).symid shr 8 and $ff;
  672. data[len+4]:=tsym(s).symid and $ff;
  673. inc(len,5);
  674. end
  675. else
  676. begin
  677. data[len]:=ord(deref_defid);
  678. data[len+1]:=tdef(s).defid shr 24 and $ff;
  679. data[len+2]:=tdef(s).defid shr 16 and $ff;
  680. data[len+3]:=tdef(s).defid shr 8 and $ff;
  681. data[len+4]:=tdef(s).defid and $ff;
  682. inc(len,5);
  683. end;
  684. end
  685. else
  686. begin
  687. { nil pointer }
  688. data[len]:=ord(deref_nil);
  689. inc(len);
  690. end;
  691. { store data length in first byte }
  692. data[0]:=len-1;
  693. { store index and write to derefdata }
  694. dataidx:=current_module.derefdata.size;
  695. current_module.derefdata.write(data,len);
  696. end;
  697. function tderef.resolve:TObject;
  698. var
  699. pm : tmodule;
  700. typ : tdereftype;
  701. idx : longint;
  702. i : aint;
  703. len : byte;
  704. data : array[0..255] of byte;
  705. begin
  706. result:=nil;
  707. { not initialized or error }
  708. if dataidx<0 then
  709. internalerror(200306067);
  710. { read data }
  711. current_module.derefdata.seek(dataidx);
  712. if current_module.derefdata.read(len,1)<>1 then
  713. internalerror(200310221);
  714. if len>0 then
  715. begin
  716. if current_module.derefdata.read(data,len)<>len then
  717. internalerror(200310222);
  718. end;
  719. { process data }
  720. pm:=current_module;
  721. i:=0;
  722. while (i<len) do
  723. begin
  724. typ:=tdereftype(data[i]);
  725. inc(i);
  726. case typ of
  727. deref_unit :
  728. begin
  729. idx:=(data[i] shl 8) or data[i+1];
  730. inc(i,2);
  731. pm:=current_module.resolve_unit(idx);
  732. end;
  733. deref_defid :
  734. begin
  735. idx:=longint((data[i] shl 24) or (data[i+1] shl 16) or (data[i+2] shl 8) or data[i+3]);
  736. inc(i,4);
  737. result:=tdef(pm.deflist[idx]);
  738. end;
  739. deref_symid :
  740. begin
  741. idx:=longint((data[i] shl 24) or (data[i+1] shl 16) or (data[i+2] shl 8) or data[i+3]);
  742. inc(i,4);
  743. result:=tsym(pm.symlist[idx]);
  744. end;
  745. deref_nil :
  746. begin
  747. result:=nil;
  748. { Only allowed when no other deref is available }
  749. if len<>1 then
  750. internalerror(200306232);
  751. end;
  752. end;
  753. end;
  754. end;
  755. {*****************************************************************************
  756. TCompilerPPUFile
  757. *****************************************************************************}
  758. procedure tcompilerppufile.checkerror;
  759. begin
  760. if error then
  761. Message(unit_f_ppu_read_error);
  762. end;
  763. procedure tcompilerppufile.RaiseAssertion(Code: Longint);
  764. begin
  765. InternalError(Code);
  766. end;
  767. procedure tcompilerppufile.getguid(var g: tguid);
  768. begin
  769. longint(g.d1):=getlongint;
  770. g.d2:=getword;
  771. g.d3:=getword;
  772. getdata(g.d4,sizeof(g.d4));
  773. end;
  774. function tcompilerppufile.getexprint:Tconstexprint;
  775. begin
  776. getexprint.overflow:=false;
  777. getexprint.signed:=getboolean;
  778. getexprint.svalue:=getint64;
  779. end;
  780. procedure tcompilerppufile.getposinfo(var p:tfileposinfo);
  781. var
  782. info : byte;
  783. begin
  784. {
  785. info byte layout in bits:
  786. 0-1 - amount of bytes for fileindex
  787. 2-3 - amount of bytes for line
  788. 4-5 - amount of bytes for column
  789. }
  790. info:=getbyte;
  791. case (info and $03) of
  792. 0 : p.fileindex:=getbyte;
  793. 1 : p.fileindex:=getword;
  794. 2 : p.fileindex:=(getbyte shl 16) or getword;
  795. 3 : p.fileindex:=getlongint;
  796. end;
  797. case ((info shr 2) and $03) of
  798. 0 : p.line:=getbyte;
  799. 1 : p.line:=getword;
  800. 2 : p.line:=(getbyte shl 16) or getword;
  801. 3 : p.line:=getlongint;
  802. end;
  803. case ((info shr 4) and $03) of
  804. 0 : p.column:=getbyte;
  805. 1 : p.column:=getword;
  806. 2 : p.column:=(getbyte shl 16) or getword;
  807. 3 : p.column:=getlongint;
  808. end;
  809. p.moduleindex:=current_module.unit_index;
  810. end;
  811. procedure tcompilerppufile.getderef(var d:tderef);
  812. begin
  813. d.dataidx:=getlongint;
  814. end;
  815. function tcompilerppufile.getpropaccesslist:tpropaccesslist;
  816. var
  817. hderef : tderef;
  818. slt : tsltype;
  819. idx : longint;
  820. p : tpropaccesslist;
  821. begin
  822. p:=tpropaccesslist.create;
  823. getderef(p.procdefderef);
  824. repeat
  825. slt:=tsltype(getbyte);
  826. case slt of
  827. sl_none :
  828. break;
  829. sl_call,
  830. sl_load,
  831. sl_subscript :
  832. begin
  833. getderef(hderef);
  834. p.addsymderef(slt,hderef);
  835. end;
  836. sl_absolutetype,
  837. sl_typeconv :
  838. begin
  839. getderef(hderef);
  840. p.addtypederef(slt,hderef);
  841. end;
  842. sl_vec :
  843. begin
  844. idx:=getlongint;
  845. getderef(hderef);
  846. p.addconstderef(slt,idx,hderef);
  847. end;
  848. end;
  849. until false;
  850. getpropaccesslist:=tpropaccesslist(p);
  851. end;
  852. function tcompilerppufile.getasmsymbol:tasmsymbol;
  853. begin
  854. getlongint;
  855. getasmsymbol:=nil;
  856. end;
  857. procedure tcompilerppufile.putposinfo(const p:tfileposinfo);
  858. var
  859. oldcrc : boolean;
  860. info : byte;
  861. begin
  862. { posinfo is not relevant for changes in PPU }
  863. oldcrc:=do_crc;
  864. do_crc:=false;
  865. {
  866. info byte layout in bits:
  867. 0-1 - amount of bytes for fileindex
  868. 2-3 - amount of bytes for line
  869. 4-5 - amount of bytes for column
  870. }
  871. info:=0;
  872. { calculate info byte }
  873. if (p.fileindex>$ff) then
  874. begin
  875. info:=info or $1;
  876. { uncomment this code if tfileposinfo.fileindex type was changed
  877. if (p.fileindex<=$ffff) then
  878. info:=info or $1
  879. else
  880. if (p.fileindex<=$ffffff) then
  881. info:=info or $2
  882. else
  883. info:=info or $3;
  884. }
  885. end;
  886. if (p.line>$ff) then
  887. begin
  888. if (p.line<=$ffff) then
  889. info:=info or $4
  890. else
  891. if (p.line<=$ffffff) then
  892. info:=info or $8
  893. else
  894. info:=info or $c;
  895. end;
  896. if (p.column>$ff) then
  897. begin
  898. info:=info or $10;
  899. { uncomment this code if tfileposinfo.column type was changed
  900. if (p.column<=$ffff) then
  901. info:=info or $10
  902. else
  903. if (p.column<=$ffffff) then
  904. info:=info or $20
  905. else
  906. info:=info or $30;
  907. }
  908. end;
  909. { write data }
  910. putbyte(info);
  911. case (info and $03) of
  912. 0 : putbyte(p.fileindex);
  913. 1 : putword(p.fileindex);
  914. 2 : begin
  915. putbyte(p.fileindex shr 16);
  916. putword(p.fileindex and $ffff);
  917. end;
  918. 3 : putlongint(p.fileindex);
  919. end;
  920. case ((info shr 2) and $03) of
  921. 0 : putbyte(p.line);
  922. 1 : putword(p.line);
  923. 2 : begin
  924. putbyte(p.line shr 16);
  925. putword(p.line and $ffff);
  926. end;
  927. 3 : putlongint(p.line);
  928. end;
  929. case ((info shr 4) and $03) of
  930. 0 : putbyte(p.column);
  931. 1 : putword(p.column);
  932. 2 : begin
  933. putbyte(p.column shr 16);
  934. putword(p.column and $ffff);
  935. end;
  936. 3 : putlongint(p.column);
  937. end;
  938. do_crc:=oldcrc;
  939. end;
  940. procedure tcompilerppufile.putguid(const g: tguid);
  941. begin
  942. putlongint(longint(g.d1));
  943. putword(g.d2);
  944. putword(g.d3);
  945. putdata(g.d4,sizeof(g.d4));
  946. end;
  947. procedure Tcompilerppufile.putexprint(const v:Tconstexprint);
  948. begin
  949. if v.overflow then
  950. internalerror(200706102);
  951. putboolean(v.signed);
  952. putint64(v.svalue);
  953. end;
  954. procedure tcompilerppufile.putderef(const d:tderef);
  955. var
  956. oldcrc : boolean;
  957. begin
  958. oldcrc:=do_crc;
  959. do_crc:=false;
  960. if d.dataidx=-1 then
  961. internalerror(2019022201)
  962. else
  963. putlongint(d.dataidx);
  964. do_crc:=oldcrc;
  965. end;
  966. procedure tcompilerppufile.putpropaccesslist(p:tpropaccesslist);
  967. var
  968. hp : ppropaccesslistitem;
  969. begin
  970. putderef(p.procdefderef);
  971. hp:=p.firstsym;
  972. while assigned(hp) do
  973. begin
  974. putbyte(byte(hp^.sltype));
  975. case hp^.sltype of
  976. sl_call,
  977. sl_load,
  978. sl_subscript :
  979. putderef(hp^.symderef);
  980. sl_absolutetype,
  981. sl_typeconv :
  982. putderef(hp^.defderef);
  983. sl_vec :
  984. begin
  985. putlongint(int64(hp^.value));
  986. putderef(hp^.valuedefderef);
  987. end;
  988. else
  989. internalerror(200110205);
  990. end;
  991. hp:=hp^.next;
  992. end;
  993. putbyte(byte(sl_none));
  994. end;
  995. procedure tcompilerppufile.putasmsymbol(s:tasmsymbol);
  996. begin
  997. putlongint(0);
  998. end;
  999. {$ifdef MEMDEBUG}
  1000. initialization
  1001. memmanglednames:=TMemDebug.create('Manglednames');
  1002. memmanglednames.stop;
  1003. memprocpara:=TMemDebug.create('ProcPara');
  1004. memprocpara.stop;
  1005. memprocparast:=TMemDebug.create('ProcParaSt');
  1006. memprocparast.stop;
  1007. memproclocalst:=TMemDebug.create('ProcLocalSt');
  1008. memproclocalst.stop;
  1009. memprocnodetree:=TMemDebug.create('ProcNodeTree');
  1010. memprocnodetree.stop;
  1011. finalization
  1012. memmanglednames.free;
  1013. memprocpara.free;
  1014. memprocparast.free;
  1015. memproclocalst.free;
  1016. memprocnodetree.free;
  1017. {$endif MEMDEBUG}
  1018. end.