symbase.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. datasize : longint;
  76. symindex,
  77. defindex : TIndexArray;
  78. symsearch : Tdictionary;
  79. next : tsymtable;
  80. defowner : tdefentry; { for records and objects }
  81. { only used for parameter symtable to determine the offset relative }
  82. { to the frame pointer and for local inline }
  83. address_fixup : longint;
  84. symtabletype : tsymtabletype;
  85. { each symtable gets a number }
  86. unitid : word;
  87. { level of symtable, used for nested procedures }
  88. symtablelevel : byte;
  89. dataalignment : byte;
  90. constructor Create(const s:string);
  91. destructor destroy;override;
  92. procedure clear;virtual;
  93. function rename(const olds,news : stringid):tsymentry;
  94. procedure foreach(proc2call : tnamedindexcallback;arg:pointer);
  95. procedure foreach_static(proc2call : tnamedindexstaticcallback;arg:pointer);
  96. procedure insert(sym : tsymentry);virtual;
  97. procedure replace(oldsym,newsym:tsymentry);
  98. procedure insertvardata(sym : tsymentry);virtual;abstract;
  99. procedure insertconstdata(sym : tsymentry);virtual;abstract;
  100. function search(const s : stringid) : tsymentry;
  101. function speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;virtual;
  102. procedure registerdef(p : tdefentry);
  103. {$ifdef EXTDEBUG}
  104. procedure dump;
  105. {$endif EXTDEBUG}
  106. function getdefnr(l : longint) : tdefentry;
  107. function getsymnr(l : longint) : tsymentry;
  108. {$ifdef GDB}
  109. function getnewtypecount : word; virtual;
  110. {$endif GDB}
  111. end;
  112. var
  113. registerdef : boolean; { true, when defs should be registered }
  114. defaultsymtablestack : tsymtable; { symtablestack after default units have been loaded }
  115. symtablestack : tsymtable; { linked list of symtables }
  116. aktrecordsymtable : tsymtable; { current record read from ppu symtable }
  117. aktstaticsymtable : tsymtable; { current static for local ppu symtable }
  118. aktglobalsymtable : tsymtable; { current global for local ppu symtable }
  119. aktlocalsymtable : tsymtable; { current proc local for local ppu symtable }
  120. implementation
  121. uses
  122. verbose;
  123. {****************************************************************************
  124. TSYMTABLE
  125. ****************************************************************************}
  126. constructor tsymtable.Create(const s:string);
  127. begin
  128. if s<>'' then
  129. begin
  130. name:=stringdup(upper(s));
  131. realname:=stringdup(s);
  132. end
  133. else
  134. begin
  135. name:=nil;
  136. realname:=nil;
  137. end;
  138. symtabletype:=abstractsymtable;
  139. symtablelevel:=0;
  140. defowner:=nil;
  141. next:=nil;
  142. symindex:=tindexarray.create(indexgrowsize);
  143. defindex:=TIndexArray.create(indexgrowsize);
  144. symsearch:=tdictionary.create;
  145. symsearch.noclear:=true;
  146. unitid:=0;
  147. address_fixup:=0;
  148. datasize:=0;
  149. dataalignment:=1;
  150. end;
  151. destructor tsymtable.destroy;
  152. begin
  153. stringdispose(name);
  154. stringdispose(realname);
  155. symindex.destroy;
  156. defindex.destroy;
  157. { symsearch can already be disposed or set to nil for withsymtable }
  158. if assigned(symsearch) then
  159. begin
  160. symsearch.destroy;
  161. symsearch:=nil;
  162. end;
  163. end;
  164. {$ifdef EXTDEBUG}
  165. procedure tsymtable.dumpsym(p : TNamedIndexItem;arg:pointer);
  166. begin
  167. writeln(p.name);
  168. end;
  169. procedure tsymtable.dump;
  170. begin
  171. if assigned(name) then
  172. writeln('Symtable ',name^)
  173. else
  174. writeln('Symtable <not named>');
  175. symsearch.foreach({$ifdef FPCPROCVAR}@{$endif}dumpsym,nil);
  176. end;
  177. {$endif EXTDEBUG}
  178. procedure tsymtable.registerdef(p : tdefentry);
  179. begin
  180. defindex.insert(p);
  181. { set def owner and indexnb }
  182. p.owner:=self;
  183. end;
  184. procedure tsymtable.foreach(proc2call : tnamedindexcallback;arg:pointer);
  185. begin
  186. symindex.foreach(proc2call,arg);
  187. end;
  188. procedure tsymtable.foreach_static(proc2call : tnamedindexstaticcallback;arg:pointer);
  189. begin
  190. symindex.foreach_static(proc2call,arg);
  191. end;
  192. {***********************************************
  193. Table Access
  194. ***********************************************}
  195. procedure tsymtable.clear;
  196. begin
  197. symindex.clear;
  198. defindex.clear;
  199. end;
  200. procedure tsymtable.insert(sym:tsymentry);
  201. begin
  202. sym.owner:=self;
  203. { insert in index and search hash }
  204. symindex.insert(sym);
  205. symsearch.insert(sym);
  206. end;
  207. procedure tsymtable.replace(oldsym,newsym:tsymentry);
  208. begin
  209. { Replace the entry in the dictionary, this checks
  210. the name }
  211. if not symsearch.replace(oldsym,newsym) then
  212. internalerror(200209061);
  213. { replace in index }
  214. symindex.replace(oldsym,newsym);
  215. { set owner of new symb }
  216. newsym.owner:=self;
  217. end;
  218. function tsymtable.search(const s : stringid) : tsymentry;
  219. begin
  220. search:=speedsearch(s,getspeedvalue(s));
  221. end;
  222. function tsymtable.speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;
  223. begin
  224. speedsearch:=tsymentry(symsearch.speedsearch(s,speedvalue));
  225. end;
  226. function tsymtable.rename(const olds,news : stringid):tsymentry;
  227. begin
  228. rename:=tsymentry(symsearch.rename(olds,news));
  229. end;
  230. function tsymtable.getsymnr(l : longint) : tsymentry;
  231. var
  232. hp : tsymentry;
  233. begin
  234. hp:=tsymentry(symindex.search(l));
  235. if hp=nil then
  236. internalerror(10999);
  237. getsymnr:=hp;
  238. end;
  239. function tsymtable.getdefnr(l : longint) : tdefentry;
  240. var
  241. hp : tdefentry;
  242. begin
  243. hp:=tdefentry(defindex.search(l));
  244. if hp=nil then
  245. internalerror(10998);
  246. getdefnr:=hp;
  247. end;
  248. {$ifdef GDB}
  249. function tsymtable.getnewtypecount : word;
  250. begin
  251. getnewtypecount:=0;
  252. end;
  253. {$endif GDB}
  254. end.
  255. {
  256. $Log$
  257. Revision 1.13 2003-06-07 20:26:32 peter
  258. * re-resolving added instead of reloading from ppu
  259. * tderef object added to store deref info for resolving
  260. Revision 1.12 2003/04/27 11:21:34 peter
  261. * aktprocdef renamed to current_procdef
  262. * procinfo renamed to current_procinfo
  263. * procinfo will now be stored in current_module so it can be
  264. cleaned up properly
  265. * gen_main_procsym changed to create_main_proc and release_main_proc
  266. to also generate a tprocinfo structure
  267. * fixed unit implicit initfinal
  268. Revision 1.11 2003/04/27 07:29:51 peter
  269. * current_procdef cleanup, current_procdef is now always nil when parsing
  270. a new procdef declaration
  271. * aktprocsym removed
  272. * lexlevel removed, use symtable.symtablelevel instead
  273. * implicit init/final code uses the normal genentry/genexit
  274. * funcret state checking updated for new funcret handling
  275. Revision 1.10 2002/12/07 14:27:09 carl
  276. * 3% memory optimization
  277. * changed some types
  278. + added type checking with different size for call node and for
  279. parameters
  280. Revision 1.9 2002/10/02 20:51:59 peter
  281. * tsymtable.dump to dump the names in a symtable to stdout
  282. Revision 1.8 2002/09/09 17:34:15 peter
  283. * tdicationary.replace added to replace and item in a dictionary. This
  284. is only allowed for the same name
  285. * varsyms are inserted in symtable before the types are parsed. This
  286. fixes the long standing "var longint : longint" bug
  287. - consume_idlist and idstringlist removed. The loops are inserted
  288. at the callers place and uses the symtable for duplicate id checking
  289. Revision 1.7 2002/08/25 19:25:20 peter
  290. * sym.insert_in_data removed
  291. * symtable.insertvardata/insertconstdata added
  292. * removed insert_in_data call from symtable.insert, it needs to be
  293. called separatly. This allows to deref the address calculation
  294. * procedures now calculate the parast addresses after the procedure
  295. directives are parsed. This fixes the cdecl parast problem
  296. * push_addr_param has an extra argument that specifies if cdecl is used
  297. or not
  298. Revision 1.6 2002/05/18 13:34:18 peter
  299. * readded missing revisions
  300. Revision 1.5 2002/05/16 19:46:44 carl
  301. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  302. + try to fix temp allocation (still in ifdef)
  303. + generic constructor calls
  304. + start of tassembler / tmodulebase class cleanup
  305. Revision 1.3 2002/05/12 16:53:10 peter
  306. * moved entry and exitcode to ncgutil and cgobj
  307. * foreach gets extra argument for passing local data to the
  308. iterator function
  309. * -CR checks also class typecasts at runtime by changing them
  310. into as
  311. * fixed compiler to cycle with the -CR option
  312. * fixed stabs with elf writer, finally the global variables can
  313. be watched
  314. * removed a lot of routines from cga unit and replaced them by
  315. calls to cgobj
  316. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  317. u32bit then the other is typecasted also to u32bit without giving
  318. a rangecheck warning/error.
  319. * fixed pascal calling method with reversing also the high tree in
  320. the parast, detected by tcalcst3 test
  321. }