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