symtype.pas 32 KB

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