symtype.pas 30 KB

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