symtype.pas 33 KB

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