symtype.pas 41 KB

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