symtype.pas 32 KB

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