symbase.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl, Pierre Muller
  4. This unit handles the symbol tables
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit symbase;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. { common }
  23. cutils,cclasses,
  24. { global }
  25. globtype,globals,
  26. { symtable }
  27. symconst
  28. ;
  29. {************************************************
  30. Some internal constants
  31. ************************************************}
  32. const
  33. hasharraysize = 256;
  34. indexgrowsize = 64;
  35. {$ifdef GDB}
  36. memsizeinc = 2048; { for long stabstrings }
  37. {$endif GDB}
  38. {************************************************
  39. Needed forward pointers
  40. ************************************************}
  41. type
  42. tsymtable = class;
  43. {************************************************
  44. TSymtableEntry
  45. ************************************************}
  46. tsymtableentry = class(TNamedIndexItem)
  47. owner : tsymtable;
  48. end;
  49. {************************************************
  50. TDefEntry
  51. ************************************************}
  52. tdefentry = class(tsymtableentry)
  53. deftype : tdeftype;
  54. end;
  55. {************************************************
  56. TSymEntry
  57. ************************************************}
  58. { this object is the base for all symbol objects }
  59. tsymentry = class(tsymtableentry)
  60. typ : tsymtyp;
  61. end;
  62. {************************************************
  63. TSymtable
  64. ************************************************}
  65. tsearchhasharray = array[0..hasharraysize-1] of tsymentry;
  66. psearchhasharray = ^tsearchhasharray;
  67. tsymtable = class
  68. {$ifdef EXTDEBUG}
  69. private
  70. procedure dumpsym(p : TNamedIndexItem;arg:pointer);
  71. {$endif EXTDEBUG}
  72. public
  73. name : pstring;
  74. realname : pstring;
  75. symtabletype : tsymtabletype;
  76. { each symtable gets a number }
  77. unitid : word{integer give range check errors PM};
  78. datasize : longint;
  79. dataalignment : longint;
  80. symindex,
  81. defindex : TIndexArray;
  82. symsearch : Tdictionary;
  83. next : tsymtable;
  84. defowner : tdefentry; { for records and objects }
  85. { only used for parameter symtable to determine the offset relative }
  86. { to the frame pointer and for local inline }
  87. address_fixup : longint;
  88. { this saves all definition to allow a proper clean up }
  89. { separate lexlevel from symtable type }
  90. symtablelevel : byte;
  91. constructor Create(const s:string);
  92. destructor destroy;override;
  93. procedure clear;virtual;
  94. function rename(const olds,news : stringid):tsymentry;
  95. procedure foreach(proc2call : tnamedindexcallback;arg:pointer);
  96. procedure foreach_static(proc2call : tnamedindexstaticcallback;arg:pointer);
  97. procedure insert(sym : tsymentry);virtual;
  98. procedure replace(oldsym,newsym:tsymentry);
  99. procedure insertvardata(sym : tsymentry);virtual;abstract;
  100. procedure insertconstdata(sym : tsymentry);virtual;abstract;
  101. function search(const s : stringid) : tsymentry;
  102. function speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;virtual;
  103. procedure registerdef(p : tdefentry);
  104. {$ifdef EXTDEBUG}
  105. procedure dump;
  106. {$endif EXTDEBUG}
  107. function getdefnr(l : longint) : tdefentry;
  108. function getsymnr(l : longint) : tsymentry;
  109. {$ifdef GDB}
  110. function getnewtypecount : word; virtual;
  111. {$endif GDB}
  112. end;
  113. {************************************************
  114. TDeref
  115. ************************************************}
  116. tderef = class
  117. dereftype : tdereftype;
  118. index : word;
  119. next : tderef;
  120. constructor create(typ:tdereftype;i:word);
  121. destructor destroy;override;
  122. end;
  123. var
  124. registerdef : boolean; { true, when defs should be registered }
  125. defaultsymtablestack : tsymtable; { symtablestack after default units have been loaded }
  126. symtablestack : tsymtable; { linked list of symtables }
  127. aktrecordsymtable : tsymtable; { current record read from ppu symtable }
  128. aktstaticsymtable : tsymtable; { current static for local ppu symtable }
  129. aktlocalsymtable : tsymtable; { current proc local for local ppu symtable }
  130. implementation
  131. uses
  132. verbose;
  133. {****************************************************************************
  134. TSYMTABLE
  135. ****************************************************************************}
  136. constructor tsymtable.Create(const s:string);
  137. begin
  138. if s<>'' then
  139. begin
  140. name:=stringdup(upper(s));
  141. realname:=stringdup(s);
  142. end
  143. else
  144. begin
  145. name:=nil;
  146. realname:=nil;
  147. end;
  148. symtabletype:=abstractsymtable;
  149. symtablelevel:=0;
  150. defowner:=nil;
  151. next:=nil;
  152. symindex:=tindexarray.create(indexgrowsize);
  153. defindex:=TIndexArray.create(indexgrowsize);
  154. symsearch:=tdictionary.create;
  155. symsearch.noclear:=true;
  156. unitid:=0;
  157. address_fixup:=0;
  158. datasize:=0;
  159. dataalignment:=1;
  160. end;
  161. destructor tsymtable.destroy;
  162. begin
  163. stringdispose(name);
  164. stringdispose(realname);
  165. symindex.destroy;
  166. defindex.destroy;
  167. { symsearch can already be disposed or set to nil for withsymtable }
  168. if assigned(symsearch) then
  169. begin
  170. symsearch.destroy;
  171. symsearch:=nil;
  172. end;
  173. end;
  174. {$ifdef EXTDEBUG}
  175. procedure tsymtable.dumpsym(p : TNamedIndexItem;arg:pointer);
  176. begin
  177. writeln(p.name);
  178. end;
  179. procedure tsymtable.dump;
  180. begin
  181. if assigned(name) then
  182. writeln('Symtable ',name^)
  183. else
  184. writeln('Symtable <not named>');
  185. symsearch.foreach({$ifdef FPCPROCVAR}@{$endif}dumpsym,nil);
  186. end;
  187. {$endif EXTDEBUG}
  188. procedure tsymtable.registerdef(p : tdefentry);
  189. begin
  190. defindex.insert(p);
  191. { set def owner and indexnb }
  192. p.owner:=self;
  193. end;
  194. procedure tsymtable.foreach(proc2call : tnamedindexcallback;arg:pointer);
  195. begin
  196. symindex.foreach(proc2call,arg);
  197. end;
  198. procedure tsymtable.foreach_static(proc2call : tnamedindexstaticcallback;arg:pointer);
  199. begin
  200. symindex.foreach_static(proc2call,arg);
  201. end;
  202. {***********************************************
  203. Table Access
  204. ***********************************************}
  205. procedure tsymtable.clear;
  206. begin
  207. symindex.clear;
  208. defindex.clear;
  209. end;
  210. procedure tsymtable.insert(sym:tsymentry);
  211. begin
  212. sym.owner:=self;
  213. { insert in index and search hash }
  214. symindex.insert(sym);
  215. symsearch.insert(sym);
  216. end;
  217. procedure tsymtable.replace(oldsym,newsym:tsymentry);
  218. begin
  219. { Replace the entry in the dictionary, this checks
  220. the name }
  221. if not symsearch.replace(oldsym,newsym) then
  222. internalerror(200209061);
  223. { replace in index }
  224. symindex.replace(oldsym,newsym);
  225. { set owner of new symb }
  226. newsym.owner:=self;
  227. end;
  228. function tsymtable.search(const s : stringid) : tsymentry;
  229. begin
  230. search:=speedsearch(s,getspeedvalue(s));
  231. end;
  232. function tsymtable.speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;
  233. begin
  234. speedsearch:=tsymentry(symsearch.speedsearch(s,speedvalue));
  235. end;
  236. function tsymtable.rename(const olds,news : stringid):tsymentry;
  237. begin
  238. rename:=tsymentry(symsearch.rename(olds,news));
  239. end;
  240. function tsymtable.getsymnr(l : longint) : tsymentry;
  241. var
  242. hp : tsymentry;
  243. begin
  244. hp:=tsymentry(symindex.search(l));
  245. if hp=nil then
  246. internalerror(10999);
  247. getsymnr:=hp;
  248. end;
  249. function tsymtable.getdefnr(l : longint) : tdefentry;
  250. var
  251. hp : tdefentry;
  252. begin
  253. hp:=tdefentry(defindex.search(l));
  254. if hp=nil then
  255. internalerror(10998);
  256. getdefnr:=hp;
  257. end;
  258. {$ifdef GDB}
  259. function tsymtable.getnewtypecount : word;
  260. begin
  261. getnewtypecount:=0;
  262. end;
  263. {$endif GDB}
  264. {****************************************************************************
  265. TDeref
  266. ****************************************************************************}
  267. constructor tderef.create(typ:tdereftype;i:word);
  268. begin
  269. dereftype:=typ;
  270. index:=i;
  271. next:=nil;
  272. end;
  273. destructor tderef.destroy;
  274. begin
  275. end;
  276. end.
  277. {
  278. $Log$
  279. Revision 1.9 2002-10-02 20:51:59 peter
  280. * tsymtable.dump to dump the names in a symtable to stdout
  281. Revision 1.8 2002/09/09 17:34:15 peter
  282. * tdicationary.replace added to replace and item in a dictionary. This
  283. is only allowed for the same name
  284. * varsyms are inserted in symtable before the types are parsed. This
  285. fixes the long standing "var longint : longint" bug
  286. - consume_idlist and idstringlist removed. The loops are inserted
  287. at the callers place and uses the symtable for duplicate id checking
  288. Revision 1.7 2002/08/25 19:25:20 peter
  289. * sym.insert_in_data removed
  290. * symtable.insertvardata/insertconstdata added
  291. * removed insert_in_data call from symtable.insert, it needs to be
  292. called separatly. This allows to deref the address calculation
  293. * procedures now calculate the parast addresses after the procedure
  294. directives are parsed. This fixes the cdecl parast problem
  295. * push_addr_param has an extra argument that specifies if cdecl is used
  296. or not
  297. Revision 1.6 2002/05/18 13:34:18 peter
  298. * readded missing revisions
  299. Revision 1.5 2002/05/16 19:46:44 carl
  300. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  301. + try to fix temp allocation (still in ifdef)
  302. + generic constructor calls
  303. + start of tassembler / tmodulebase class cleanup
  304. Revision 1.3 2002/05/12 16:53:10 peter
  305. * moved entry and exitcode to ncgutil and cgobj
  306. * foreach gets extra argument for passing local data to the
  307. iterator function
  308. * -CR checks also class typecasts at runtime by changing them
  309. into as
  310. * fixed compiler to cycle with the -CR option
  311. * fixed stabs with elf writer, finally the global variables can
  312. be watched
  313. * removed a lot of routines from cga unit and replaced them by
  314. calls to cgobj
  315. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  316. u32bit then the other is typecasted also to u32bit without giving
  317. a rangecheck warning/error.
  318. * fixed pascal calling method with reversing also the high tree in
  319. the parast, detected by tcalcst3 test
  320. }