symtype.pas 33 KB

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