symtype.pas 41 KB

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