symtype.pas 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl, Pierre Muller
  4. This unit handles the symbol tables
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit symtype;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. { common }
  23. cutils,
  24. {$ifdef MEMDEBUG}
  25. cclasses,
  26. {$endif MEMDEBUG}
  27. { global }
  28. globtype,globals,
  29. { symtable }
  30. symconst,symbase,
  31. { aasm }
  32. aasmbase,ppu,cpuinfo
  33. ;
  34. type
  35. {************************************************
  36. Required Forwards
  37. ************************************************}
  38. tsym = class;
  39. Tcompilerppufile=class;
  40. {************************************************
  41. TRef
  42. ************************************************}
  43. tref = class
  44. nextref : tref;
  45. posinfo : tfileposinfo;
  46. moduleindex : longint;
  47. is_written : boolean;
  48. constructor create(ref:tref;pos:pfileposinfo);
  49. procedure freechain;
  50. destructor destroy;override;
  51. end;
  52. {************************************************
  53. TDef
  54. ************************************************}
  55. tgetsymtable = (gs_none,gs_record,gs_local,gs_para);
  56. tdef = class(tdefentry)
  57. typesym : tsym; { which type the definition was generated this def }
  58. defoptions : tdefoptions;
  59. constructor create;
  60. procedure buildderef;virtual;abstract;
  61. procedure buildderefimpl;virtual;abstract;
  62. procedure deref;virtual;abstract;
  63. procedure derefimpl;virtual;abstract;
  64. function typename:string;
  65. function gettypename:string;virtual;
  66. function mangledparaname:string;
  67. function getmangledparaname:string;virtual;abstract;
  68. function size:longint;virtual;abstract;
  69. function alignment:longint;virtual;abstract;
  70. function getparentdef:tdef;virtual;
  71. function getsymtable(t:tgetsymtable):tsymtable;virtual;
  72. function is_publishable:boolean;virtual;abstract;
  73. function needs_inittable:boolean;virtual;abstract;
  74. end;
  75. {************************************************
  76. TSym
  77. ************************************************}
  78. { this object is the base for all symbol objects }
  79. tsym = class(tsymentry)
  80. protected
  81. public
  82. _realname : pstring;
  83. fileinfo : tfileposinfo;
  84. symoptions : tsymoptions;
  85. refs : longint;
  86. lastref,
  87. defref,
  88. lastwritten : tref;
  89. refcount : longint;
  90. {$ifdef GDB}
  91. isstabwritten : boolean;
  92. function get_var_value(const s:string):string;
  93. function stabstr_evaluate(const s:string;vars:array of string):Pchar;
  94. function stabstring : pchar;virtual;
  95. {$endif GDB}
  96. constructor create(const n : string);
  97. constructor loadsym(ppufile:tcompilerppufile);
  98. destructor destroy;override;
  99. procedure ppuwrite(ppufile:tcompilerppufile);virtual;abstract;
  100. procedure writesym(ppufile:tcompilerppufile);
  101. function realname:string;
  102. procedure buildderef;virtual;
  103. { procedure buildderefimpl;virtual;abstract;}
  104. procedure deref;virtual;
  105. { procedure derefimpl;virtual;abstract;}
  106. function gettypedef:tdef;virtual;
  107. procedure load_references(ppufile:tcompilerppufile;locals:boolean);virtual;
  108. function write_references(ppufile:tcompilerppufile;locals:boolean):boolean;virtual;
  109. function is_visible_for_object(currobjdef:Tdef):boolean;
  110. end;
  111. {************************************************
  112. TDeref
  113. ************************************************}
  114. tderef = object
  115. dataidx : longint;
  116. procedure reset;
  117. procedure build(s:tsymtableentry);
  118. function resolve:tsymtableentry;
  119. end;
  120. {************************************************
  121. TType
  122. ************************************************}
  123. ttype = object
  124. def : tdef;
  125. sym : tsym;
  126. deref : tderef;
  127. procedure reset;
  128. procedure setdef(p:tdef);
  129. procedure setsym(p:tsym);
  130. procedure resolve;
  131. procedure buildderef;
  132. end;
  133. {************************************************
  134. TSymList
  135. ************************************************}
  136. psymlistitem = ^tsymlistitem;
  137. tsymlistitem = record
  138. sltype : tsltype;
  139. next : psymlistitem;
  140. case byte of
  141. 0 : (sym : tsym; symderef : tderef);
  142. 1 : (value : longint);
  143. 2 : (tt : ttype);
  144. end;
  145. tsymlist = class
  146. procdef : tdef;
  147. procdefderef : tderef;
  148. firstsym,
  149. lastsym : psymlistitem;
  150. constructor create;
  151. destructor destroy;override;
  152. function empty:boolean;
  153. procedure addsym(slt:tsltype;p:tsym);
  154. procedure addsymderef(slt:tsltype;const d:tderef);
  155. procedure addconst(slt:tsltype;v:longint);
  156. procedure addtype(slt:tsltype;const tt:ttype);
  157. procedure clear;
  158. function getcopy:tsymlist;
  159. procedure resolve;
  160. procedure buildderef;
  161. end;
  162. {************************************************
  163. Tcompilerppufile
  164. ************************************************}
  165. tcompilerppufile=class(tppufile)
  166. public
  167. procedure checkerror;
  168. procedure getguid(var g: tguid);
  169. function getexprint:tconstexprint;
  170. function getptruint:TConstPtrUInt;
  171. procedure getposinfo(var p:tfileposinfo);
  172. procedure getderef(var d:tderef);
  173. function getsymlist:tsymlist;
  174. procedure gettype(var t:ttype);
  175. function getasmsymbol:tasmsymbol;
  176. procedure putguid(const g: tguid);
  177. procedure putexprint(v:tconstexprint);
  178. procedure PutPtrUInt(v:TConstPtrUInt);
  179. procedure putposinfo(const p:tfileposinfo);
  180. procedure putderef(const d:tderef);
  181. procedure putsymlist(p:tsymlist);
  182. procedure puttype(const t:ttype);
  183. procedure putasmsymbol(s:tasmsymbol);
  184. end;
  185. {$ifdef MEMDEBUG}
  186. var
  187. membrowser,
  188. memrealnames,
  189. memmanglednames,
  190. memprocpara,
  191. memprocparast,
  192. memproclocalst,
  193. memprocnodetree : tmemdebug;
  194. {$endif MEMDEBUG}
  195. const
  196. current_object_option : tsymoptions = [sp_public];
  197. implementation
  198. uses
  199. verbose,
  200. fmodule,
  201. symdef
  202. {$ifdef GDB}
  203. ,gdb
  204. {$endif GDB}
  205. ;
  206. {****************************************************************************
  207. Tdef
  208. ****************************************************************************}
  209. constructor tdef.create;
  210. begin
  211. inherited create;
  212. deftype:=abstractdef;
  213. owner := nil;
  214. typesym := nil;
  215. defoptions:=[];
  216. end;
  217. function tdef.typename:string;
  218. begin
  219. if assigned(typesym) and
  220. not(deftype in [procvardef,procdef]) and
  221. assigned(typesym._realname) and
  222. (typesym._realname^[1]<>'$') then
  223. typename:=typesym._realname^
  224. else
  225. typename:=gettypename;
  226. end;
  227. function tdef.gettypename : string;
  228. begin
  229. gettypename:='<unknown type>'
  230. end;
  231. function tdef.mangledparaname:string;
  232. begin
  233. if assigned(typesym) then
  234. mangledparaname:=typesym.name
  235. else
  236. mangledparaname:=getmangledparaname;
  237. end;
  238. function tdef.getparentdef:tdef;
  239. begin
  240. result:=nil;
  241. end;
  242. function tdef.getsymtable(t:tgetsymtable):tsymtable;
  243. begin
  244. getsymtable:=nil;
  245. end;
  246. {****************************************************************************
  247. TSYM (base for all symtypes)
  248. ****************************************************************************}
  249. constructor tsym.create(const n : string);
  250. begin
  251. if n[1]='$' then
  252. inherited createname(copy(n,2,255))
  253. else
  254. inherited createname(upper(n));
  255. _realname:=stringdup(n);
  256. typ:=abstractsym;
  257. symoptions:=[];
  258. defref:=nil;
  259. refs:=0;
  260. lastwritten:=nil;
  261. refcount:=0;
  262. fileinfo:=akttokenpos;
  263. if (cs_browser in aktmoduleswitches) and make_ref then
  264. begin
  265. defref:=tref.create(defref,@akttokenpos);
  266. inc(refcount);
  267. end;
  268. lastref:=defref;
  269. {$ifdef GDB}
  270. isstabwritten := false;
  271. {$endif GDB}
  272. symoptions:=current_object_option;
  273. end;
  274. constructor tsym.loadsym(ppufile:tcompilerppufile);
  275. var
  276. s : string;
  277. nr : word;
  278. begin
  279. nr:=ppufile.getword;
  280. s:=ppufile.getstring;
  281. if s[1]='$' then
  282. inherited createname(copy(s,2,255))
  283. else
  284. inherited createname(upper(s));
  285. _realname:=stringdup(s);
  286. typ:=abstractsym;
  287. { force the correct indexnr. must be after create! }
  288. indexnr:=nr;
  289. ppufile.getposinfo(fileinfo);
  290. ppufile.getsmallset(symoptions);
  291. lastref:=nil;
  292. defref:=nil;
  293. refs:=0;
  294. lastwritten:=nil;
  295. refcount:=0;
  296. {$ifdef GDB}
  297. isstabwritten := false;
  298. {$endif GDB}
  299. end;
  300. destructor tsym.destroy;
  301. begin
  302. {$ifdef MEMDEBUG}
  303. memrealnames.start;
  304. {$endif MEMDEBUG}
  305. stringdispose(_realname);
  306. {$ifdef MEMDEBUG}
  307. memrealnames.stop;
  308. {$endif MEMDEBUG}
  309. inherited destroy;
  310. end;
  311. procedure Tsym.writesym(ppufile:tcompilerppufile);
  312. begin
  313. ppufile.putword(indexnr);
  314. ppufile.putstring(_realname^);
  315. ppufile.putposinfo(fileinfo);
  316. ppufile.putsmallset(symoptions);
  317. end;
  318. procedure Tsym.buildderef;
  319. begin
  320. end;
  321. procedure Tsym.deref;
  322. begin
  323. end;
  324. {$ifdef GDB}
  325. function Tsym.get_var_value(const s:string):string;
  326. begin
  327. if s='name' then
  328. get_var_value:=name
  329. else if s='ownername' then
  330. get_var_value:=owner.name^
  331. else if s='line' then
  332. get_var_value:=tostr(fileinfo.line)
  333. else if s='N_LSYM' then
  334. get_var_value:=tostr(N_LSYM)
  335. else if s='N_LCSYM' then
  336. get_var_value:=tostr(N_LCSYM)
  337. else if s='N_RSYM' then
  338. get_var_value:=tostr(N_RSYM)
  339. else if s='N_TSYM' then
  340. get_var_value:=tostr(N_TSYM)
  341. else if s='N_STSYM' then
  342. get_var_value:=tostr(N_STSYM)
  343. else if s='N_FUNCTION' then
  344. get_var_value:=tostr(N_FUNCTION)
  345. else
  346. internalerror(200401152);
  347. end;
  348. function Tsym.stabstr_evaluate(const s:string;vars:array of string):Pchar;
  349. begin
  350. stabstr_evaluate:=string_evaluate(s,@get_var_value,vars);
  351. end;
  352. function Tsym.stabstring : pchar;
  353. begin
  354. stabstring:=nil;
  355. end;
  356. {$endif GDB}
  357. function tsym.realname : string;
  358. begin
  359. if assigned(_realname) then
  360. realname:=_realname^
  361. else
  362. realname:=name;
  363. end;
  364. function tsym.gettypedef:tdef;
  365. begin
  366. gettypedef:=nil;
  367. end;
  368. procedure Tsym.load_references(ppufile:tcompilerppufile;locals:boolean);
  369. var
  370. pos : tfileposinfo;
  371. move_last : boolean;
  372. begin
  373. move_last:=lastwritten=lastref;
  374. while (not ppufile.endofentry) do
  375. begin
  376. ppufile.getposinfo(pos);
  377. inc(refcount);
  378. lastref:=tref.create(lastref,@pos);
  379. lastref.is_written:=true;
  380. if refcount=1 then
  381. defref:=lastref;
  382. end;
  383. if move_last then
  384. lastwritten:=lastref;
  385. end;
  386. { big problem here :
  387. wrong refs were written because of
  388. interface parsing of other units PM
  389. moduleindex must be checked !! }
  390. function Tsym.write_references(ppufile:tcompilerppufile;locals:boolean):boolean;
  391. var
  392. d : tderef;
  393. ref : tref;
  394. symref_written,move_last : boolean;
  395. begin
  396. write_references:=false;
  397. if lastwritten=lastref then
  398. exit;
  399. { should we update lastref }
  400. move_last:=true;
  401. symref_written:=false;
  402. { write symbol refs }
  403. d.reset;
  404. if assigned(lastwritten) then
  405. ref:=lastwritten
  406. else
  407. ref:=defref;
  408. while assigned(ref) do
  409. begin
  410. if ref.moduleindex=current_module.unit_index then
  411. begin
  412. { write address to this symbol }
  413. if not symref_written then
  414. begin
  415. d.build(self);
  416. ppufile.putderef(d);
  417. symref_written:=true;
  418. end;
  419. ppufile.putposinfo(ref.posinfo);
  420. ref.is_written:=true;
  421. if move_last then
  422. lastwritten:=ref;
  423. end
  424. else if not ref.is_written then
  425. move_last:=false
  426. else if move_last then
  427. lastwritten:=ref;
  428. ref:=ref.nextref;
  429. end;
  430. if symref_written then
  431. ppufile.writeentry(ibsymref);
  432. write_references:=symref_written;
  433. end;
  434. function Tsym.is_visible_for_object(currobjdef:Tdef):boolean;
  435. begin
  436. is_visible_for_object:=false;
  437. { private symbols are allowed when we are in the same
  438. module as they are defined }
  439. if (sp_private in symoptions) and
  440. assigned(owner.defowner) and
  441. (owner.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  442. (owner.defowner.owner.unitid<>0) then
  443. exit;
  444. { protected symbols are vissible in the module that defines them and
  445. also visible to related objects }
  446. if (sp_protected in symoptions) and
  447. (
  448. (
  449. assigned(owner.defowner) and
  450. (owner.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  451. (owner.defowner.owner.unitid<>0)
  452. ) and
  453. not(
  454. assigned(currobjdef) and
  455. Tobjectdef(currobjdef).is_related(tobjectdef(owner.defowner))
  456. )
  457. ) then
  458. exit;
  459. is_visible_for_object:=true;
  460. end;
  461. {****************************************************************************
  462. TRef
  463. ****************************************************************************}
  464. constructor tref.create(ref :tref;pos : pfileposinfo);
  465. begin
  466. nextref:=nil;
  467. if pos<>nil then
  468. posinfo:=pos^;
  469. if assigned(current_module) then
  470. moduleindex:=current_module.unit_index;
  471. if assigned(ref) then
  472. ref.nextref:=self;
  473. is_written:=false;
  474. end;
  475. procedure tref.freechain;
  476. var
  477. p,q : tref;
  478. begin
  479. p:=nextref;
  480. nextref:=nil;
  481. while assigned(p) do
  482. begin
  483. q:=p.nextref;
  484. p.free;
  485. p:=q;
  486. end;
  487. end;
  488. destructor tref.destroy;
  489. begin
  490. nextref:=nil;
  491. end;
  492. {****************************************************************************
  493. TType
  494. ****************************************************************************}
  495. procedure ttype.reset;
  496. begin
  497. def:=nil;
  498. sym:=nil;
  499. end;
  500. procedure ttype.setdef(p:tdef);
  501. begin
  502. def:=p;
  503. sym:=nil;
  504. end;
  505. procedure ttype.setsym(p:tsym);
  506. begin
  507. sym:=p;
  508. def:=p.gettypedef;
  509. if not assigned(def) then
  510. internalerror(1234005);
  511. end;
  512. procedure ttype.resolve;
  513. var
  514. p : tsymtableentry;
  515. begin
  516. p:=deref.resolve;
  517. if assigned(p) then
  518. begin
  519. if p is tsym then
  520. begin
  521. setsym(tsym(p));
  522. if not assigned(def) then
  523. internalerror(200212272);
  524. end
  525. else
  526. begin
  527. setdef(tdef(p));
  528. end;
  529. end
  530. else
  531. reset;
  532. end;
  533. procedure ttype.buildderef;
  534. begin
  535. { Write symbol references when the symbol is a redefine,
  536. but don't write symbol references for the current unit
  537. and for the system unit }
  538. if assigned(sym) and
  539. (
  540. (sym<>def.typesym) or
  541. ((sym.owner.unitid<>0) and
  542. (sym.owner.unitid<>1))
  543. ) then
  544. deref.build(sym)
  545. else
  546. deref.build(def);
  547. end;
  548. {****************************************************************************
  549. TSymList
  550. ****************************************************************************}
  551. constructor tsymlist.create;
  552. begin
  553. procdef:=nil; { needed for procedures }
  554. firstsym:=nil;
  555. lastsym:=nil;
  556. end;
  557. destructor tsymlist.destroy;
  558. begin
  559. clear;
  560. end;
  561. function tsymlist.empty:boolean;
  562. begin
  563. empty:=(firstsym=nil);
  564. end;
  565. procedure tsymlist.clear;
  566. var
  567. hp : psymlistitem;
  568. begin
  569. while assigned(firstsym) do
  570. begin
  571. hp:=firstsym;
  572. firstsym:=firstsym^.next;
  573. dispose(hp);
  574. end;
  575. firstsym:=nil;
  576. lastsym:=nil;
  577. procdef:=nil;
  578. end;
  579. procedure tsymlist.addsym(slt:tsltype;p:tsym);
  580. var
  581. hp : psymlistitem;
  582. begin
  583. if not assigned(p) then
  584. internalerror(200110203);
  585. new(hp);
  586. fillchar(hp^,sizeof(tsymlistitem),0);
  587. hp^.sltype:=slt;
  588. hp^.sym:=p;
  589. hp^.symderef.reset;
  590. if assigned(lastsym) then
  591. lastsym^.next:=hp
  592. else
  593. firstsym:=hp;
  594. lastsym:=hp;
  595. end;
  596. procedure tsymlist.addsymderef(slt:tsltype;const d:tderef);
  597. var
  598. hp : psymlistitem;
  599. begin
  600. new(hp);
  601. fillchar(hp^,sizeof(tsymlistitem),0);
  602. hp^.sltype:=slt;
  603. hp^.symderef:=d;
  604. if assigned(lastsym) then
  605. lastsym^.next:=hp
  606. else
  607. firstsym:=hp;
  608. lastsym:=hp;
  609. end;
  610. procedure tsymlist.addconst(slt:tsltype;v:longint);
  611. var
  612. hp : psymlistitem;
  613. begin
  614. new(hp);
  615. fillchar(hp^,sizeof(tsymlistitem),0);
  616. hp^.sltype:=slt;
  617. hp^.value:=v;
  618. if assigned(lastsym) then
  619. lastsym^.next:=hp
  620. else
  621. firstsym:=hp;
  622. lastsym:=hp;
  623. end;
  624. procedure tsymlist.addtype(slt:tsltype;const tt:ttype);
  625. var
  626. hp : psymlistitem;
  627. begin
  628. new(hp);
  629. fillchar(hp^,sizeof(tsymlistitem),0);
  630. hp^.sltype:=slt;
  631. hp^.tt:=tt;
  632. if assigned(lastsym) then
  633. lastsym^.next:=hp
  634. else
  635. firstsym:=hp;
  636. lastsym:=hp;
  637. end;
  638. function tsymlist.getcopy:tsymlist;
  639. var
  640. hp : tsymlist;
  641. hp2 : psymlistitem;
  642. hpn : psymlistitem;
  643. begin
  644. hp:=tsymlist.create;
  645. hp.procdef:=procdef;
  646. hp2:=firstsym;
  647. while assigned(hp2) do
  648. begin
  649. new(hpn);
  650. hpn^:=hp2^;
  651. hpn^.next:=nil;
  652. if assigned(hp.lastsym) then
  653. hp.lastsym^.next:=hpn
  654. else
  655. hp.firstsym:=hpn;
  656. hp.lastsym:=hpn;
  657. hp2:=hp2^.next;
  658. end;
  659. getcopy:=hp;
  660. end;
  661. procedure tsymlist.resolve;
  662. var
  663. hp : psymlistitem;
  664. begin
  665. procdef:=tdef(procdefderef.resolve);
  666. hp:=firstsym;
  667. while assigned(hp) do
  668. begin
  669. case hp^.sltype of
  670. sl_call,
  671. sl_load,
  672. sl_subscript :
  673. hp^.sym:=tsym(hp^.symderef.resolve);
  674. sl_typeconv :
  675. hp^.tt.resolve;
  676. sl_vec :
  677. ;
  678. else
  679. internalerror(200110205);
  680. end;
  681. hp:=hp^.next;
  682. end;
  683. end;
  684. procedure tsymlist.buildderef;
  685. var
  686. hp : psymlistitem;
  687. begin
  688. procdefderef.build(procdef);
  689. hp:=firstsym;
  690. while assigned(hp) do
  691. begin
  692. case hp^.sltype of
  693. sl_call,
  694. sl_load,
  695. sl_subscript :
  696. hp^.symderef.build(hp^.sym);
  697. sl_typeconv :
  698. hp^.tt.buildderef;
  699. sl_vec :
  700. ;
  701. else
  702. internalerror(200110205);
  703. end;
  704. hp:=hp^.next;
  705. end;
  706. end;
  707. {****************************************************************************
  708. Tderef
  709. ****************************************************************************}
  710. procedure tderef.reset;
  711. begin
  712. dataidx:=-1;
  713. end;
  714. procedure tderef.build(s:tsymtableentry);
  715. var
  716. len : byte;
  717. data : array[0..255] of byte;
  718. function is_child(currdef,ownerdef:tdef):boolean;
  719. begin
  720. while assigned(currdef) and
  721. (currdef<>ownerdef) do
  722. currdef:=currdef.getparentdef;
  723. result:=assigned(currdef);
  724. end;
  725. procedure addowner(s:tsymtableentry);
  726. begin
  727. if not assigned(s.owner) then
  728. internalerror(200306063);
  729. case s.owner.symtabletype of
  730. globalsymtable :
  731. begin
  732. if s.owner.unitid=0 then
  733. begin
  734. data[len]:=ord(deref_aktglobal);
  735. inc(len);
  736. end
  737. else
  738. begin
  739. { check if the unit is available in the uses
  740. clause, else it's an error }
  741. if s.owner.unitid=$ffff then
  742. internalerror(200306063);
  743. data[len]:=ord(deref_unit);
  744. data[len+1]:=s.owner.unitid shr 8;
  745. data[len+2]:=s.owner.unitid and $ff;
  746. inc(len,3);
  747. end;
  748. end;
  749. staticsymtable :
  750. begin
  751. { only references to the current static symtable are allowed }
  752. if s.owner<>current_module.localsymtable then
  753. internalerror(200306233);
  754. data[len]:=ord(deref_aktstatic);
  755. inc(len);
  756. end;
  757. localsymtable :
  758. begin
  759. addowner(s.owner.defowner);
  760. data[len]:=ord(deref_def);
  761. data[len+1]:=s.owner.defowner.indexnr shr 8;
  762. data[len+2]:=s.owner.defowner.indexnr and $ff;
  763. data[len+3]:=ord(deref_local);
  764. inc(len,4);
  765. end;
  766. parasymtable :
  767. begin
  768. addowner(s.owner.defowner);
  769. data[len]:=ord(deref_def);
  770. data[len+1]:=s.owner.defowner.indexnr shr 8;
  771. data[len+2]:=s.owner.defowner.indexnr and $ff;
  772. data[len+3]:=ord(deref_para);
  773. inc(len,4);
  774. end;
  775. objectsymtable,
  776. recordsymtable :
  777. begin
  778. addowner(s.owner.defowner);
  779. data[len]:=ord(deref_def);
  780. data[len+1]:=s.owner.defowner.indexnr shr 8;
  781. data[len+2]:=s.owner.defowner.indexnr and $ff;
  782. data[len+3]:=ord(deref_record);
  783. inc(len,4);
  784. end;
  785. else
  786. internalerror(200306065);
  787. end;
  788. if len>252 then
  789. internalerror(200306062);
  790. end;
  791. procedure addparentobject(currdef,ownerdef:tdef);
  792. var
  793. nextdef : tdef;
  794. begin
  795. if not assigned(currdef) then
  796. internalerror(200306185);
  797. { Already handled by derefaktrecordindex }
  798. if currdef=ownerdef then
  799. internalerror(200306188);
  800. { Generate a direct reference to the top parent
  801. class available in the current unit, this is required because
  802. the parent class is maybe not resolved yet and therefor
  803. has the childof value not available yet }
  804. while (currdef<>ownerdef) do
  805. begin
  806. nextdef:=currdef.getparentdef;
  807. { objects are only allowed in globalsymtable,staticsymtable this check is
  808. needed because we need the unitid }
  809. if not(nextdef.owner.symtabletype in [globalsymtable,staticsymtable]) then
  810. internalerror(200306187);
  811. { Next parent is in a different unit, then stop }
  812. if nextdef.owner.unitid<>0 then
  813. break;
  814. currdef:=nextdef;
  815. end;
  816. { Add reference where to start the parent lookup }
  817. if currdef=aktrecordsymtable.defowner then
  818. begin
  819. data[len]:=ord(deref_aktrecord);
  820. inc(len);
  821. end
  822. else
  823. begin
  824. if currdef.owner.symtabletype=globalsymtable then
  825. data[len]:=ord(deref_aktglobal)
  826. else
  827. data[len]:=ord(deref_aktstatic);
  828. data[len+1]:=ord(deref_def);
  829. data[len+2]:=currdef.indexnr shr 8;
  830. data[len+3]:=currdef.indexnr and $ff;
  831. data[len+4]:=ord(deref_record);
  832. inc(len,5);
  833. end;
  834. { When the current found parent in this module is not the owner we
  835. add derefs for the parent classes not available in this unit }
  836. while (currdef<>ownerdef) do
  837. begin
  838. data[len]:=ord(deref_parent_object);
  839. inc(len);
  840. currdef:=currdef.getparentdef;
  841. { It should be valid as it is checked by is_child }
  842. if not assigned(currdef) then
  843. internalerror(200306186);
  844. end;
  845. end;
  846. begin
  847. { skip length byte }
  848. len:=1;
  849. if assigned(s) then
  850. begin
  851. { Static symtable of current unit ? }
  852. if (s.owner.symtabletype=staticsymtable) and
  853. (s.owner.unitid=0) then
  854. begin
  855. data[len]:=ord(deref_aktstatic);
  856. inc(len);
  857. end
  858. { Global symtable of current unit ? }
  859. else if (s.owner.symtabletype=globalsymtable) and
  860. (s.owner.unitid=0) then
  861. begin
  862. data[len]:=ord(deref_aktglobal);
  863. inc(len);
  864. end
  865. { Current record/object symtable ? }
  866. else if (s.owner=aktrecordsymtable) then
  867. begin
  868. data[len]:=ord(deref_aktrecord);
  869. inc(len);
  870. end
  871. { Current local symtable ? }
  872. else if (s.owner=aktlocalsymtable) then
  873. begin
  874. data[len]:=ord(deref_aktlocal);
  875. inc(len);
  876. end
  877. { Current para symtable ? }
  878. else if (s.owner=aktparasymtable) then
  879. begin
  880. data[len]:=ord(deref_aktpara);
  881. inc(len);
  882. end
  883. { Parent class? }
  884. else if assigned(aktrecordsymtable) and
  885. (aktrecordsymtable.symtabletype=objectsymtable) and
  886. (s.owner.symtabletype=objectsymtable) and
  887. is_child(tdef(aktrecordsymtable.defowner),tdef(s.owner.defowner)) then
  888. begin
  889. addparentobject(tdef(aktrecordsymtable.defowner),tdef(s.owner.defowner));
  890. end
  891. else
  892. { Default, start by building from unit symtable }
  893. begin
  894. addowner(s);
  895. end;
  896. { Add index of the symbol/def }
  897. if s is tsym then
  898. data[len]:=ord(deref_sym)
  899. else
  900. data[len]:=ord(deref_def);
  901. data[len+1]:=s.indexnr shr 8;
  902. data[len+2]:=s.indexnr and $ff;
  903. inc(len,3);
  904. end
  905. else
  906. begin
  907. { nil pointer }
  908. data[len]:=0;
  909. inc(len);
  910. end;
  911. { store data length in first byte }
  912. data[0]:=len-1;
  913. { store index and write to derefdata }
  914. dataidx:=current_module.derefdata.size;
  915. current_module.derefdata.write(data,len);
  916. end;
  917. function tderef.resolve:tsymtableentry;
  918. var
  919. pd : tdef;
  920. pm : tmodule;
  921. typ : tdereftype;
  922. st : tsymtable;
  923. idx : word;
  924. i : longint;
  925. len : byte;
  926. data : array[0..255] of byte;
  927. begin
  928. result:=nil;
  929. { not initialized }
  930. if dataidx=-1 then
  931. internalerror(200306067);
  932. { read data }
  933. current_module.derefdata.seek(dataidx);
  934. if current_module.derefdata.read(len,1)<>1 then
  935. internalerror(200310221);
  936. if len>0 then
  937. begin
  938. if current_module.derefdata.read(data,len)<>len then
  939. internalerror(200310222);
  940. end;
  941. { process data }
  942. st:=nil;
  943. i:=0;
  944. while (i<len) do
  945. begin
  946. typ:=tdereftype(data[i]);
  947. inc(i);
  948. case typ of
  949. deref_nil :
  950. begin
  951. result:=nil;
  952. { Only allowed when no other deref is available }
  953. if len<>1 then
  954. internalerror(200306232);
  955. end;
  956. deref_sym :
  957. begin
  958. if not assigned(st) then
  959. internalerror(200309141);
  960. idx:=(data[i] shl 8) or data[i+1];
  961. inc(i,2);
  962. result:=st.getsymnr(idx);
  963. end;
  964. deref_def :
  965. begin
  966. if not assigned(st) then
  967. internalerror(200309142);
  968. idx:=(data[i] shl 8) or data[i+1];
  969. inc(i,2);
  970. result:=st.getdefnr(idx);
  971. end;
  972. deref_aktrecord :
  973. st:=aktrecordsymtable;
  974. deref_aktstatic :
  975. st:=current_module.localsymtable;
  976. deref_aktglobal :
  977. st:=current_module.globalsymtable;
  978. deref_aktlocal :
  979. st:=aktlocalsymtable;
  980. deref_aktpara :
  981. st:=aktparasymtable;
  982. deref_unit :
  983. begin
  984. idx:=(data[i] shl 8) or data[i+1];
  985. inc(i,2);
  986. if idx>current_module.mapsize then
  987. internalerror(200306231);
  988. pm:=current_module.map[idx].u;
  989. if not assigned(pm) then
  990. internalerror(200212273);
  991. st:=pm.globalsymtable;
  992. end;
  993. deref_local :
  994. begin
  995. if not assigned(result) then
  996. internalerror(200306069);
  997. st:=tdef(result).getsymtable(gs_local);
  998. result:=nil;
  999. if not assigned(st) then
  1000. internalerror(200212275);
  1001. end;
  1002. deref_para :
  1003. begin
  1004. if not assigned(result) then
  1005. internalerror(2003060610);
  1006. st:=tdef(result).getsymtable(gs_para);
  1007. result:=nil;
  1008. if not assigned(st) then
  1009. internalerror(200212276);
  1010. end;
  1011. deref_record :
  1012. begin
  1013. if not assigned(result) then
  1014. internalerror(200306068);
  1015. st:=tdef(result).getsymtable(gs_record);
  1016. result:=nil;
  1017. if not assigned(st) then
  1018. internalerror(200212274);
  1019. end;
  1020. deref_parent_object :
  1021. begin
  1022. { load current object symtable if no
  1023. symtable is available yet }
  1024. if st=nil then
  1025. begin
  1026. st:=aktrecordsymtable;
  1027. if not assigned(st) then
  1028. internalerror(200306068);
  1029. end;
  1030. if st.symtabletype<>objectsymtable then
  1031. internalerror(200306189);
  1032. pd:=tdef(st.defowner).getparentdef;
  1033. if not assigned(pd) then
  1034. internalerror(200306184);
  1035. st:=pd.getsymtable(gs_record);
  1036. if not assigned(st) then
  1037. internalerror(200212274);
  1038. end;
  1039. else
  1040. internalerror(200212277);
  1041. end;
  1042. end;
  1043. end;
  1044. {*****************************************************************************
  1045. TCompilerPPUFile
  1046. *****************************************************************************}
  1047. procedure tcompilerppufile.checkerror;
  1048. begin
  1049. if error then
  1050. Message(unit_f_ppu_read_error);
  1051. end;
  1052. procedure tcompilerppufile.getguid(var g: tguid);
  1053. begin
  1054. getdata(g,sizeof(g));
  1055. end;
  1056. function tcompilerppufile.getexprint:tconstexprint;
  1057. begin
  1058. if sizeof(tconstexprint)=8 then
  1059. result:=tconstexprint(getint64)
  1060. else
  1061. result:=tconstexprint(getlongint);
  1062. end;
  1063. function tcompilerppufile.getPtrUInt:TConstPtrUInt;
  1064. begin
  1065. if sizeof(TConstPtrUInt)=8 then
  1066. result:=tconstptruint(getint64)
  1067. else
  1068. result:=TConstPtrUInt(getlongint);
  1069. end;
  1070. procedure tcompilerppufile.getposinfo(var p:tfileposinfo);
  1071. var
  1072. info : byte;
  1073. begin
  1074. {
  1075. info byte layout in bits:
  1076. 0-1 - amount of bytes for fileindex
  1077. 2-3 - amount of bytes for line
  1078. 4-5 - amount of bytes for column
  1079. }
  1080. info:=getbyte;
  1081. case (info and $03) of
  1082. 0 : p.fileindex:=getbyte;
  1083. 1 : p.fileindex:=getword;
  1084. 2 : p.fileindex:=(getbyte shl 16) or getword;
  1085. 3 : p.fileindex:=getlongint;
  1086. end;
  1087. case ((info shr 2) and $03) of
  1088. 0 : p.line:=getbyte;
  1089. 1 : p.line:=getword;
  1090. 2 : p.line:=(getbyte shl 16) or getword;
  1091. 3 : p.line:=getlongint;
  1092. end;
  1093. case ((info shr 4) and $03) of
  1094. 0 : p.column:=getbyte;
  1095. 1 : p.column:=getword;
  1096. 2 : p.column:=(getbyte shl 16) or getword;
  1097. 3 : p.column:=getlongint;
  1098. end;
  1099. end;
  1100. procedure tcompilerppufile.getderef(var d:tderef);
  1101. begin
  1102. d.dataidx:=getlongint;
  1103. end;
  1104. function tcompilerppufile.getsymlist:tsymlist;
  1105. var
  1106. symderef : tderef;
  1107. tt : ttype;
  1108. slt : tsltype;
  1109. idx : longint;
  1110. p : tsymlist;
  1111. begin
  1112. p:=tsymlist.create;
  1113. getderef(p.procdefderef);
  1114. repeat
  1115. slt:=tsltype(getbyte);
  1116. case slt of
  1117. sl_none :
  1118. break;
  1119. sl_call,
  1120. sl_load,
  1121. sl_subscript :
  1122. begin
  1123. getderef(symderef);
  1124. p.addsymderef(slt,symderef);
  1125. end;
  1126. sl_typeconv :
  1127. begin
  1128. gettype(tt);
  1129. p.addtype(slt,tt);
  1130. end;
  1131. sl_vec :
  1132. begin
  1133. idx:=getlongint;
  1134. p.addconst(slt,idx);
  1135. end;
  1136. else
  1137. internalerror(200110204);
  1138. end;
  1139. until false;
  1140. getsymlist:=tsymlist(p);
  1141. end;
  1142. procedure tcompilerppufile.gettype(var t:ttype);
  1143. begin
  1144. getderef(t.deref);
  1145. t.def:=nil;
  1146. t.sym:=nil;
  1147. end;
  1148. function tcompilerppufile.getasmsymbol:tasmsymbol;
  1149. begin
  1150. getasmsymbol:=tasmsymbol(pointer(getlongint));
  1151. end;
  1152. procedure tcompilerppufile.putposinfo(const p:tfileposinfo);
  1153. var
  1154. oldcrc : boolean;
  1155. info : byte;
  1156. begin
  1157. { posinfo is not relevant for changes in PPU }
  1158. oldcrc:=do_crc;
  1159. do_crc:=false;
  1160. {
  1161. info byte layout in bits:
  1162. 0-1 - amount of bytes for fileindex
  1163. 2-3 - amount of bytes for line
  1164. 4-5 - amount of bytes for column
  1165. }
  1166. info:=0;
  1167. { calculate info byte }
  1168. if (p.fileindex>$ff) then
  1169. begin
  1170. if (p.fileindex<=$ffff) then
  1171. info:=info or $1
  1172. else
  1173. if (p.fileindex<=$ffffff) then
  1174. info:=info or $2
  1175. else
  1176. info:=info or $3;
  1177. end;
  1178. if (p.line>$ff) then
  1179. begin
  1180. if (p.line<=$ffff) then
  1181. info:=info or $4
  1182. else
  1183. if (p.line<=$ffffff) then
  1184. info:=info or $8
  1185. else
  1186. info:=info or $c;
  1187. end;
  1188. if (p.column>$ff) then
  1189. begin
  1190. if (p.column<=$ffff) then
  1191. info:=info or $10
  1192. else
  1193. if (p.column<=$ffffff) then
  1194. info:=info or $20
  1195. else
  1196. info:=info or $30;
  1197. end;
  1198. { write data }
  1199. putbyte(info);
  1200. case (info and $03) of
  1201. 0 : putbyte(p.fileindex);
  1202. 1 : putword(p.fileindex);
  1203. 2 : begin
  1204. putbyte(p.fileindex shr 16);
  1205. putword(p.fileindex and $ffff);
  1206. end;
  1207. 3 : putlongint(p.fileindex);
  1208. end;
  1209. case ((info shr 2) and $03) of
  1210. 0 : putbyte(p.line);
  1211. 1 : putword(p.line);
  1212. 2 : begin
  1213. putbyte(p.line shr 16);
  1214. putword(p.line and $ffff);
  1215. end;
  1216. 3 : putlongint(p.line);
  1217. end;
  1218. case ((info shr 4) and $03) of
  1219. 0 : putbyte(p.column);
  1220. 1 : putword(p.column);
  1221. 2 : begin
  1222. putbyte(p.column shr 16);
  1223. putword(p.column and $ffff);
  1224. end;
  1225. 3 : putlongint(p.column);
  1226. end;
  1227. do_crc:=oldcrc;
  1228. end;
  1229. procedure tcompilerppufile.putguid(const g: tguid);
  1230. begin
  1231. putdata(g,sizeof(g));
  1232. end;
  1233. procedure tcompilerppufile.putexprint(v:tconstexprint);
  1234. begin
  1235. if sizeof(TConstExprInt)=8 then
  1236. putint64(int64(v))
  1237. else if sizeof(TConstExprInt)=4 then
  1238. putlongint(longint(v))
  1239. else
  1240. internalerror(2002082601);
  1241. end;
  1242. procedure tcompilerppufile.PutPtrUInt(v:TConstPtrUInt);
  1243. begin
  1244. if sizeof(TConstPtrUInt)=8 then
  1245. putint64(int64(v))
  1246. else if sizeof(TConstPtrUInt)=4 then
  1247. putlongint(longint(v))
  1248. else
  1249. internalerror(2002082601);
  1250. end;
  1251. procedure tcompilerppufile.putderef(const d:tderef);
  1252. var
  1253. oldcrc : boolean;
  1254. begin
  1255. oldcrc:=do_crc;
  1256. do_crc:=false;
  1257. putlongint(d.dataidx);
  1258. do_crc:=oldcrc;
  1259. end;
  1260. procedure tcompilerppufile.putsymlist(p:tsymlist);
  1261. var
  1262. hp : psymlistitem;
  1263. begin
  1264. putderef(p.procdefderef);
  1265. hp:=p.firstsym;
  1266. while assigned(hp) do
  1267. begin
  1268. putbyte(byte(hp^.sltype));
  1269. case hp^.sltype of
  1270. sl_call,
  1271. sl_load,
  1272. sl_subscript :
  1273. putderef(hp^.symderef);
  1274. sl_typeconv :
  1275. puttype(hp^.tt);
  1276. sl_vec :
  1277. putlongint(hp^.value);
  1278. else
  1279. internalerror(200110205);
  1280. end;
  1281. hp:=hp^.next;
  1282. end;
  1283. putbyte(byte(sl_none));
  1284. end;
  1285. procedure tcompilerppufile.puttype(const t:ttype);
  1286. begin
  1287. putderef(t.deref);
  1288. end;
  1289. procedure tcompilerppufile.putasmsymbol(s:tasmsymbol);
  1290. begin
  1291. if assigned(s) then
  1292. begin
  1293. if s.ppuidx=-1 then
  1294. begin
  1295. inc(objectlibrary.asmsymbolppuidx);
  1296. s.ppuidx:=objectlibrary.asmsymbolppuidx;
  1297. end;
  1298. putlongint(s.ppuidx);
  1299. end
  1300. else
  1301. putlongint(0);
  1302. end;
  1303. {$ifdef MEMDEBUG}
  1304. initialization
  1305. membrowser:=TMemDebug.create('BrowserRefs');
  1306. membrowser.stop;
  1307. memrealnames:=TMemDebug.create('Realnames');
  1308. memrealnames.stop;
  1309. memmanglednames:=TMemDebug.create('Manglednames');
  1310. memmanglednames.stop;
  1311. memprocpara:=TMemDebug.create('ProcPara');
  1312. memprocpara.stop;
  1313. memprocparast:=TMemDebug.create('ProcParaSt');
  1314. memprocparast.stop;
  1315. memproclocalst:=TMemDebug.create('ProcLocalSt');
  1316. memproclocalst.stop;
  1317. memprocnodetree:=TMemDebug.create('ProcNodeTree');
  1318. memprocnodetree.stop;
  1319. finalization
  1320. membrowser.free;
  1321. memrealnames.free;
  1322. memmanglednames.free;
  1323. memprocpara.free;
  1324. memprocparast.free;
  1325. memproclocalst.free;
  1326. memprocnodetree.free;
  1327. {$endif MEMDEBUG}
  1328. end.
  1329. {
  1330. $Log$
  1331. Revision 1.44 2004-07-09 22:17:32 peter
  1332. * revert has_localst patch
  1333. * replace aktstaticsymtable/aktglobalsymtable with current_module
  1334. Revision 1.43 2004/06/20 08:55:30 florian
  1335. * logs truncated
  1336. Revision 1.42 2004/06/16 20:07:10 florian
  1337. * dwarf branch merged
  1338. Revision 1.41 2004/05/23 15:23:30 peter
  1339. * fixed qword(longint) that removed sign from the number
  1340. * removed code in the compiler that relied on wrong qword(longint)
  1341. code generation
  1342. Revision 1.40.2.1 2004/04/12 14:45:11 peter
  1343. * tai_const_symbol and tai_const merged
  1344. Revision 1.40 2004/02/27 13:04:22 daniel
  1345. * Removed unused concatstabto
  1346. Revision 1.39 2004/02/11 19:59:06 peter
  1347. * fix compilation without GDB
  1348. }
  1349. end.