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