symtype.pas 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  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:TSymStr;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>'
  271. end;
  272. function tdef.typesymbolprettyname:string;
  273. begin
  274. result:=OwnerHierarchyName;
  275. if assigned(typesym) then
  276. result:=result+typesym.prettyname
  277. else
  278. result:=result+'<no type symbol>'
  279. end;
  280. function tdef.mangledparaname:string;
  281. begin
  282. result:=OwnerHierarchyName;
  283. if assigned(typesym) then
  284. mangledparaname:=result+typesym.name
  285. else
  286. mangledparaname:=result+getmangledparaname;
  287. end;
  288. function tdef.getmangledparaname:TSymStr;
  289. begin
  290. result:='<unknown type>';
  291. end;
  292. function tdef.unique_id_str: string;
  293. begin
  294. if (defid=defid_not_registered) or
  295. (defid=defid_registered_nost) then
  296. begin
  297. if not assigned(current_module) then
  298. internalerror(2015102505);
  299. current_module.deflist.Add(self);
  300. { invert the defid to indicate that it was only set because we
  301. needed a unique number -- then add defid_not_registered so we
  302. don't get the values between defid_registered and 0 }
  303. defid:=-(current_module.deflist.Count-1)+defid_not_registered-1;
  304. end;
  305. { use deflist_index so that it will remain the same if def first gets a
  306. defid just for the unique id (as above) and later it gets registered
  307. because it must be saved to the ppu }
  308. result:=hexstr(deflist_index,sizeof(defid)*2);
  309. end;
  310. function tdef.getparentdef:tdef;
  311. begin
  312. result:=nil;
  313. end;
  314. function tdef.geTSymtable(t:tgeTSymtable):TSymtable;
  315. begin
  316. result:=nil;
  317. end;
  318. function tdef.packedbitsize:asizeint;
  319. begin
  320. result:=size * 8;
  321. end;
  322. function tdef.structalignment: shortint;
  323. begin
  324. result:=alignment;
  325. end;
  326. procedure tdef.ChangeOwner(st:TSymtable);
  327. begin
  328. // if assigned(Owner) then
  329. // Owner.DefList.List[i]:=nil;
  330. Owner:=st;
  331. Owner.DefList.Add(self);
  332. end;
  333. function tdef.getreusablesymtab: tsymtable;
  334. var
  335. origowner: TSymtable;
  336. begin
  337. { if the original def was in a localsymtable, don't create a
  338. reusable copy in the unit's staticsymtable since the localsymtable
  339. won't be saved to the ppu and as a result we can get unreachable
  340. defs when reloading the derived ones from the ppu }
  341. origowner:=owner;
  342. while not(origowner.symtabletype in [localsymtable,staticsymtable,globalsymtable,stt_excepTSymtable]) do
  343. origowner:=origowner.defowner.owner;
  344. { if the def is in an exceptionsymtable, we can't create a reusable
  345. def because the original one will be freed when the (always
  346. temprary) exceptionsymtable is freed }
  347. if origowner.symtabletype=stt_excepTSymtable then
  348. internalerror(2015111701)
  349. else if origowner.symtabletype=localsymtable then
  350. result:=origowner
  351. else if assigned(current_module.localsymtable) then
  352. result:=current_module.localsymtable
  353. else
  354. result:=current_module.globalsymtable;
  355. end;
  356. procedure tdef.register_created_object_type;
  357. begin
  358. end;
  359. function tdef.get_top_level_symtable: tsymtable;
  360. begin
  361. result:=owner;
  362. while assigned(result) and
  363. assigned(result.defowner) do
  364. result:=tdef(result.defowner).owner;
  365. end;
  366. function tdef.deflist_index: longint;
  367. begin
  368. if defid<defid_not_registered then
  369. result:=-(defid-defid_not_registered+1)
  370. else if defid>=0 then
  371. result:=defid
  372. else
  373. internalerror(2015102502)
  374. end;
  375. {****************************************************************************
  376. TSYM (base for all symtypes)
  377. ****************************************************************************}
  378. function tsym.registered: boolean;
  379. begin
  380. result:=symid>symid_not_registered;
  381. end;
  382. constructor tsym.create(st:tsymtyp;const aname:string);
  383. begin
  384. inherited CreateNotOwned;
  385. realname:=aname;
  386. typ:=st;
  387. RefList:=nil;
  388. symoptions:=[];
  389. fileinfo:=current_tokenpos;
  390. isdbgwritten := false;
  391. visibility:=vis_public;
  392. deprecatedmsg:=nil;
  393. symid:=symid_not_registered;
  394. end;
  395. destructor Tsym.destroy;
  396. begin
  397. stringdispose(deprecatedmsg);
  398. if assigned(RefList) then
  399. RefList.Free;
  400. inherited Destroy;
  401. end;
  402. procedure Tsym.IncRefCount;
  403. begin
  404. inc(refs);
  405. if cs_browser in current_settings.moduleswitches then
  406. begin
  407. MaybeCreateRefList;
  408. AddRef;
  409. end;
  410. end;
  411. procedure Tsym.IncRefCountBy(AValue : longint);
  412. begin
  413. inc(refs,AValue);
  414. end;
  415. procedure Tsym.MaybeCreateRefList;
  416. begin
  417. if not assigned(reflist) then
  418. reflist:=TRefLinkedList.create;
  419. end;
  420. procedure Tsym.AddRef;
  421. var
  422. RefItem: TRefItem;
  423. begin
  424. RefItem:=TRefItem.Create(current_tokenpos);
  425. RefList.Concat(RefItem);
  426. end;
  427. procedure Tsym.buildderef;
  428. begin
  429. end;
  430. procedure Tsym.deref;
  431. begin
  432. end;
  433. function tsym.mangledname : TSymStr;
  434. begin
  435. internalerror(200204171);
  436. result:='';
  437. end;
  438. function tsym.prettyname : string;
  439. begin
  440. result:=realname;
  441. end;
  442. procedure tsym.ChangeOwner(st:TSymtable);
  443. begin
  444. Owner:=st;
  445. inherited ChangeOwner(Owner.SymList);
  446. end;
  447. {****************************************************************************
  448. tpropaccesslist
  449. ****************************************************************************}
  450. constructor tpropaccesslist.create;
  451. begin
  452. procdef:=nil; { needed for procedures }
  453. firstsym:=nil;
  454. lastsym:=nil;
  455. end;
  456. destructor tpropaccesslist.destroy;
  457. begin
  458. clear;
  459. end;
  460. function tpropaccesslist.empty:boolean;
  461. begin
  462. empty:=(firstsym=nil);
  463. end;
  464. function tpropaccesslist.getcopy: tpropaccesslist;
  465. var
  466. hp, dest : ppropaccesslistitem;
  467. begin
  468. result:=tpropaccesslist.create;
  469. result.procdef:=procdef;
  470. hp:=firstsym;
  471. while assigned(hp) do
  472. begin
  473. new(dest);
  474. dest^:=hp^;
  475. dest^.next:=nil;
  476. if not assigned(result.firstsym) then
  477. result.firstsym:=dest;
  478. if assigned(result.lastsym) then
  479. result.lastsym^.next:=dest;
  480. result.lastsym:=dest;
  481. hp:=hp^.next;
  482. end;
  483. end;
  484. procedure tpropaccesslist.clear;
  485. var
  486. hp : ppropaccesslistitem;
  487. begin
  488. while assigned(firstsym) do
  489. begin
  490. hp:=firstsym;
  491. firstsym:=firstsym^.next;
  492. dispose(hp);
  493. end;
  494. firstsym:=nil;
  495. lastsym:=nil;
  496. procdef:=nil;
  497. end;
  498. procedure tpropaccesslist.addsym(slt:tsltype;p:tsym);
  499. var
  500. hp : ppropaccesslistitem;
  501. begin
  502. new(hp);
  503. fillchar(hp^,sizeof(tpropaccesslistitem),0);
  504. hp^.sltype:=slt;
  505. hp^.sym:=p;
  506. hp^.symderef.reset;
  507. if assigned(lastsym) then
  508. lastsym^.next:=hp
  509. else
  510. firstsym:=hp;
  511. lastsym:=hp;
  512. end;
  513. procedure tpropaccesslist.addconst(slt:tsltype;v:TConstExprInt;d:tdef);
  514. var
  515. hp : ppropaccesslistitem;
  516. begin
  517. new(hp);
  518. fillchar(hp^,sizeof(tpropaccesslistitem),0);
  519. hp^.sltype:=slt;
  520. hp^.value:=v;
  521. hp^.valuedef:=d;
  522. hp^.valuedefderef.reset;
  523. if assigned(lastsym) then
  524. lastsym^.next:=hp
  525. else
  526. firstsym:=hp;
  527. lastsym:=hp;
  528. end;
  529. procedure tpropaccesslist.addtype(slt:tsltype;d:tdef);
  530. var
  531. hp : ppropaccesslistitem;
  532. begin
  533. new(hp);
  534. fillchar(hp^,sizeof(tpropaccesslistitem),0);
  535. hp^.sltype:=slt;
  536. hp^.def:=d;
  537. hp^.defderef.reset;
  538. if assigned(lastsym) then
  539. lastsym^.next:=hp
  540. else
  541. firstsym:=hp;
  542. lastsym:=hp;
  543. end;
  544. procedure tpropaccesslist.addsymderef(slt:tsltype;d:tderef);
  545. begin
  546. addsym(slt,nil);
  547. lastsym^.symderef:=d;
  548. end;
  549. procedure tpropaccesslist.addconstderef(slt:tsltype;v:TConstExprInt;d:tderef);
  550. begin
  551. addconst(slt,v,nil);
  552. lastsym^.valuedefderef:=d;
  553. end;
  554. procedure tpropaccesslist.addtypederef(slt:tsltype;d:tderef);
  555. begin
  556. addtype(slt,nil);
  557. lastsym^.defderef:=d;
  558. end;
  559. procedure tpropaccesslist.resolve;
  560. var
  561. hp : ppropaccesslistitem;
  562. begin
  563. procdef:=tdef(procdefderef.resolve);
  564. hp:=firstsym;
  565. while assigned(hp) do
  566. begin
  567. case hp^.sltype of
  568. sl_call,
  569. sl_load,
  570. sl_subscript :
  571. hp^.sym:=tsym(hp^.symderef.resolve);
  572. sl_absolutetype,
  573. sl_typeconv :
  574. hp^.def:=tdef(hp^.defderef.resolve);
  575. sl_vec:
  576. hp^.valuedef:=tdef(hp^.valuedefderef.resolve);
  577. else
  578. internalerror(200110205);
  579. end;
  580. hp:=hp^.next;
  581. end;
  582. end;
  583. procedure tpropaccesslist.buildderef;
  584. var
  585. hp : ppropaccesslistitem;
  586. begin
  587. procdefderef.build(procdef);
  588. hp:=firstsym;
  589. while assigned(hp) do
  590. begin
  591. case hp^.sltype of
  592. sl_call,
  593. sl_load,
  594. sl_subscript :
  595. hp^.symderef.build(hp^.sym);
  596. sl_absolutetype,
  597. sl_typeconv :
  598. hp^.defderef.build(hp^.def);
  599. sl_vec:
  600. hp^.valuedefderef.build(hp^.valuedef);
  601. else
  602. internalerror(200110205);
  603. end;
  604. hp:=hp^.next;
  605. end;
  606. end;
  607. {****************************************************************************
  608. Tderef
  609. ****************************************************************************}
  610. procedure tderef.reset;
  611. begin
  612. dataidx:=-1;
  613. end;
  614. procedure tderef.build(s:TObject);
  615. var
  616. len : byte;
  617. st : TSymtable;
  618. data : array[0..255] of byte;
  619. idx : word;
  620. begin
  621. { skip length byte }
  622. len:=1;
  623. if assigned(s) then
  624. begin
  625. { TODO: ugly hack}
  626. if s is tsym then
  627. begin
  628. { if it has been registered but it wasn't put in a symbol table,
  629. this symbol shouldn't be written to a ppu }
  630. if tsym(s).SymId=symid_registered_nost then
  631. Internalerror(2015102504);
  632. if not tsym(s).registered then
  633. tsym(s).register_sym;
  634. st:=FindUnitSymtable(tsym(s).owner)
  635. end
  636. else
  637. begin
  638. { same as above }
  639. if tdef(s).defid=defid_registered_nost then
  640. Internalerror(2015102505);
  641. if not tdef(s).registered then
  642. tdef(s).register_def;
  643. st:=FindUnitSymtable(tdef(s).owner);
  644. end;
  645. if not st.iscurrentunit then
  646. begin
  647. { register that the unit is needed for resolving }
  648. data[len]:=ord(deref_unit);
  649. idx:=current_module.derefidx_unit(st.moduleid);
  650. data[len+1]:=idx shr 8 and $ff;
  651. data[len+2]:=idx and $ff;
  652. inc(len,3);
  653. end;
  654. if s is tsym then
  655. begin
  656. data[len]:=ord(deref_symid);
  657. data[len+1]:=tsym(s).symid shr 24 and $ff;
  658. data[len+2]:=tsym(s).symid shr 16 and $ff;
  659. data[len+3]:=tsym(s).symid shr 8 and $ff;
  660. data[len+4]:=tsym(s).symid and $ff;
  661. inc(len,5);
  662. end
  663. else
  664. begin
  665. data[len]:=ord(deref_defid);
  666. data[len+1]:=tdef(s).defid shr 24 and $ff;
  667. data[len+2]:=tdef(s).defid shr 16 and $ff;
  668. data[len+3]:=tdef(s).defid shr 8 and $ff;
  669. data[len+4]:=tdef(s).defid and $ff;
  670. inc(len,5);
  671. end;
  672. end
  673. else
  674. begin
  675. { nil pointer }
  676. data[len]:=ord(deref_nil);
  677. inc(len);
  678. end;
  679. { store data length in first byte }
  680. data[0]:=len-1;
  681. { store index and write to derefdata }
  682. dataidx:=current_module.derefdata.size;
  683. current_module.derefdata.write(data,len);
  684. end;
  685. function tderef.resolve:TObject;
  686. var
  687. pm : tmodule;
  688. typ : tdereftype;
  689. idx : longint;
  690. i : aint;
  691. len : byte;
  692. data : array[0..255] of byte;
  693. begin
  694. result:=nil;
  695. { not initialized or error }
  696. if dataidx<0 then
  697. internalerror(200306067);
  698. { read data }
  699. current_module.derefdata.seek(dataidx);
  700. if current_module.derefdata.read(len,1)<>1 then
  701. internalerror(200310221);
  702. if len>0 then
  703. begin
  704. if current_module.derefdata.read(data,len)<>len then
  705. internalerror(200310222);
  706. end;
  707. { process data }
  708. pm:=current_module;
  709. i:=0;
  710. while (i<len) do
  711. begin
  712. typ:=tdereftype(data[i]);
  713. inc(i);
  714. case typ of
  715. deref_unit :
  716. begin
  717. idx:=(data[i] shl 8) or data[i+1];
  718. inc(i,2);
  719. pm:=current_module.resolve_unit(idx);
  720. end;
  721. deref_defid :
  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:=tdef(pm.deflist[idx]);
  726. end;
  727. deref_symid :
  728. begin
  729. idx:=longint((data[i] shl 24) or (data[i+1] shl 16) or (data[i+2] shl 8) or data[i+3]);
  730. inc(i,4);
  731. result:=tsym(pm.symlist[idx]);
  732. end;
  733. deref_nil :
  734. begin
  735. result:=nil;
  736. { Only allowed when no other deref is available }
  737. if len<>1 then
  738. internalerror(200306232);
  739. end;
  740. else
  741. internalerror(200212277);
  742. end;
  743. end;
  744. end;
  745. {*****************************************************************************
  746. TCompilerPPUFile
  747. *****************************************************************************}
  748. procedure tcompilerppufile.checkerror;
  749. begin
  750. if error then
  751. Message(unit_f_ppu_read_error);
  752. end;
  753. procedure tcompilerppufile.getguid(var g: tguid);
  754. begin
  755. longint(g.d1):=getlongint;
  756. g.d2:=getword;
  757. g.d3:=getword;
  758. getdata(g.d4,sizeof(g.d4));
  759. end;
  760. function tcompilerppufile.getexprint:Tconstexprint;
  761. begin
  762. getexprint.overflow:=false;
  763. getexprint.signed:=boolean(getbyte);
  764. getexprint.svalue:=getint64;
  765. end;
  766. function tcompilerppufile.getPtrUInt:TConstPtrUInt;
  767. begin
  768. {$if sizeof(TConstPtrUInt)=8}
  769. result:=tconstptruint(getint64);
  770. {$else}
  771. result:=TConstPtrUInt(getlongint);
  772. {$endif}
  773. end;
  774. procedure tcompilerppufile.getposinfo(var p:tfileposinfo);
  775. var
  776. info : byte;
  777. begin
  778. {
  779. info byte layout in bits:
  780. 0-1 - amount of bytes for fileindex
  781. 2-3 - amount of bytes for line
  782. 4-5 - amount of bytes for column
  783. }
  784. info:=getbyte;
  785. case (info and $03) of
  786. 0 : p.fileindex:=getbyte;
  787. 1 : p.fileindex:=getword;
  788. 2 : p.fileindex:=(getbyte shl 16) or getword;
  789. 3 : p.fileindex:=getlongint;
  790. end;
  791. case ((info shr 2) and $03) of
  792. 0 : p.line:=getbyte;
  793. 1 : p.line:=getword;
  794. 2 : p.line:=(getbyte shl 16) or getword;
  795. 3 : p.line:=getlongint;
  796. end;
  797. case ((info shr 4) and $03) of
  798. 0 : p.column:=getbyte;
  799. 1 : p.column:=getword;
  800. 2 : p.column:=(getbyte shl 16) or getword;
  801. 3 : p.column:=getlongint;
  802. end;
  803. p.moduleindex:=current_module.unit_index;
  804. end;
  805. procedure tcompilerppufile.getderef(var d:tderef);
  806. begin
  807. d.dataidx:=getlongint;
  808. end;
  809. function tcompilerppufile.getpropaccesslist:tpropaccesslist;
  810. var
  811. hderef : tderef;
  812. slt : tsltype;
  813. idx : longint;
  814. p : tpropaccesslist;
  815. begin
  816. p:=tpropaccesslist.create;
  817. getderef(p.procdefderef);
  818. repeat
  819. slt:=tsltype(getbyte);
  820. case slt of
  821. sl_none :
  822. break;
  823. sl_call,
  824. sl_load,
  825. sl_subscript :
  826. begin
  827. getderef(hderef);
  828. p.addsymderef(slt,hderef);
  829. end;
  830. sl_absolutetype,
  831. sl_typeconv :
  832. begin
  833. getderef(hderef);
  834. p.addtypederef(slt,hderef);
  835. end;
  836. sl_vec :
  837. begin
  838. idx:=getlongint;
  839. getderef(hderef);
  840. p.addconstderef(slt,idx,hderef);
  841. end;
  842. else
  843. internalerror(200110204);
  844. end;
  845. until false;
  846. getpropaccesslist:=tpropaccesslist(p);
  847. end;
  848. function tcompilerppufile.getasmsymbol:tasmsymbol;
  849. begin
  850. getlongint;
  851. getasmsymbol:=nil;
  852. end;
  853. procedure tcompilerppufile.putposinfo(const p:tfileposinfo);
  854. var
  855. oldcrc : boolean;
  856. info : byte;
  857. begin
  858. { posinfo is not relevant for changes in PPU }
  859. oldcrc:=do_crc;
  860. do_crc:=false;
  861. {
  862. info byte layout in bits:
  863. 0-1 - amount of bytes for fileindex
  864. 2-3 - amount of bytes for line
  865. 4-5 - amount of bytes for column
  866. }
  867. info:=0;
  868. { calculate info byte }
  869. if (p.fileindex>$ff) then
  870. begin
  871. info:=info or $1;
  872. { uncomment this code if tfileposinfo.fileindex type was changed
  873. if (p.fileindex<=$ffff) then
  874. info:=info or $1
  875. else
  876. if (p.fileindex<=$ffffff) then
  877. info:=info or $2
  878. else
  879. info:=info or $3;
  880. }
  881. end;
  882. if (p.line>$ff) then
  883. begin
  884. if (p.line<=$ffff) then
  885. info:=info or $4
  886. else
  887. if (p.line<=$ffffff) then
  888. info:=info or $8
  889. else
  890. info:=info or $c;
  891. end;
  892. if (p.column>$ff) then
  893. begin
  894. info:=info or $10;
  895. { uncomment this code if tfileposinfo.column type was changed
  896. if (p.column<=$ffff) then
  897. info:=info or $10
  898. else
  899. if (p.column<=$ffffff) then
  900. info:=info or $20
  901. else
  902. info:=info or $30;
  903. }
  904. end;
  905. { write data }
  906. putbyte(info);
  907. case (info and $03) of
  908. 0 : putbyte(p.fileindex);
  909. 1 : putword(p.fileindex);
  910. 2 : begin
  911. putbyte(p.fileindex shr 16);
  912. putword(p.fileindex and $ffff);
  913. end;
  914. 3 : putlongint(p.fileindex);
  915. end;
  916. case ((info shr 2) and $03) of
  917. 0 : putbyte(p.line);
  918. 1 : putword(p.line);
  919. 2 : begin
  920. putbyte(p.line shr 16);
  921. putword(p.line and $ffff);
  922. end;
  923. 3 : putlongint(p.line);
  924. end;
  925. case ((info shr 4) and $03) of
  926. 0 : putbyte(p.column);
  927. 1 : putword(p.column);
  928. 2 : begin
  929. putbyte(p.column shr 16);
  930. putword(p.column and $ffff);
  931. end;
  932. 3 : putlongint(p.column);
  933. end;
  934. do_crc:=oldcrc;
  935. end;
  936. procedure tcompilerppufile.putguid(const g: tguid);
  937. begin
  938. putlongint(longint(g.d1));
  939. putword(g.d2);
  940. putword(g.d3);
  941. putdata(g.d4,sizeof(g.d4));
  942. end;
  943. procedure Tcompilerppufile.putexprint(const v:Tconstexprint);
  944. begin
  945. if v.overflow then
  946. internalerror(200706102);
  947. putbyte(byte(v.signed));
  948. putint64(v.svalue);
  949. end;
  950. procedure tcompilerppufile.PutPtrUInt(v:TConstPtrUInt);
  951. begin
  952. {$if sizeof(TConstPtrUInt)=8}
  953. putint64(int64(v));
  954. {$else}
  955. putlongint(longint(v));
  956. {$endif}
  957. end;
  958. procedure tcompilerppufile.putderef(const d:tderef);
  959. var
  960. oldcrc : boolean;
  961. begin
  962. oldcrc:=do_crc;
  963. do_crc:=false;
  964. putlongint(d.dataidx);
  965. do_crc:=oldcrc;
  966. end;
  967. procedure tcompilerppufile.putpropaccesslist(p:tpropaccesslist);
  968. var
  969. hp : ppropaccesslistitem;
  970. begin
  971. putderef(p.procdefderef);
  972. hp:=p.firstsym;
  973. while assigned(hp) do
  974. begin
  975. putbyte(byte(hp^.sltype));
  976. case hp^.sltype of
  977. sl_call,
  978. sl_load,
  979. sl_subscript :
  980. putderef(hp^.symderef);
  981. sl_absolutetype,
  982. sl_typeconv :
  983. putderef(hp^.defderef);
  984. sl_vec :
  985. begin
  986. putlongint(int64(hp^.value));
  987. putderef(hp^.valuedefderef);
  988. end;
  989. else
  990. internalerror(200110205);
  991. end;
  992. hp:=hp^.next;
  993. end;
  994. putbyte(byte(sl_none));
  995. end;
  996. procedure tcompilerppufile.putasmsymbol(s:tasmsymbol);
  997. begin
  998. putlongint(0);
  999. end;
  1000. {$ifdef MEMDEBUG}
  1001. initialization
  1002. memmanglednames:=TMemDebug.create('Manglednames');
  1003. memmanglednames.stop;
  1004. memprocpara:=TMemDebug.create('ProcPara');
  1005. memprocpara.stop;
  1006. memprocparast:=TMemDebug.create('ProcParaSt');
  1007. memprocparast.stop;
  1008. memproclocalst:=TMemDebug.create('ProcLocalSt');
  1009. memproclocalst.stop;
  1010. memprocnodetree:=TMemDebug.create('ProcNodeTree');
  1011. memprocnodetree.stop;
  1012. finalization
  1013. memmanglednames.free;
  1014. memprocpara.free;
  1015. memprocparast.free;
  1016. memproclocalst.free;
  1017. memprocnodetree.free;
  1018. {$endif MEMDEBUG}
  1019. end.