symtype.pas 32 KB

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