symtype.pas 32 KB

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