symtype.pas 36 KB

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