2
0

symbase.pas 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. public
  69. name : pstring;
  70. realname : pstring;
  71. symtabletype : tsymtabletype;
  72. { each symtable gets a number }
  73. unitid : word{integer give range check errors PM};
  74. datasize : longint;
  75. dataalignment : 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. { this saves all definition to allow a proper clean up }
  85. { separate lexlevel from symtable type }
  86. symtablelevel : byte;
  87. constructor Create(const s:string);
  88. destructor destroy;override;
  89. procedure clear;virtual;
  90. function rename(const olds,news : stringid):tsymentry;
  91. procedure foreach(proc2call : tnamedindexcallback;arg:pointer);
  92. procedure foreach_static(proc2call : tnamedindexstaticcallback;arg:pointer);
  93. procedure insert(sym : tsymentry);virtual;
  94. function search(const s : stringid) : tsymentry;
  95. function speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;virtual;
  96. procedure registerdef(p : tdefentry);
  97. function getdefnr(l : longint) : tdefentry;
  98. function getsymnr(l : longint) : tsymentry;
  99. {$ifdef GDB}
  100. function getnewtypecount : word; virtual;
  101. {$endif GDB}
  102. end;
  103. {************************************************
  104. TDeref
  105. ************************************************}
  106. tderef = class
  107. dereftype : tdereftype;
  108. index : word;
  109. next : tderef;
  110. constructor create(typ:tdereftype;i:word);
  111. destructor destroy;override;
  112. end;
  113. var
  114. registerdef : boolean; { true, when defs should be registered }
  115. defaultsymtablestack : tsymtable; { symtablestack after default units have been loaded }
  116. symtablestack : tsymtable; { linked list of symtables }
  117. aktrecordsymtable : tsymtable; { current record read from ppu symtable }
  118. aktstaticsymtable : tsymtable; { current static 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. procedure tsymtable.registerdef(p : tdefentry);
  165. begin
  166. defindex.insert(p);
  167. { set def owner and indexnb }
  168. p.owner:=self;
  169. end;
  170. procedure tsymtable.foreach(proc2call : tnamedindexcallback;arg:pointer);
  171. begin
  172. symindex.foreach(proc2call,arg);
  173. end;
  174. procedure tsymtable.foreach_static(proc2call : tnamedindexstaticcallback;arg:pointer);
  175. begin
  176. symindex.foreach_static(proc2call,arg);
  177. end;
  178. {***********************************************
  179. Table Access
  180. ***********************************************}
  181. procedure tsymtable.clear;
  182. begin
  183. symindex.clear;
  184. defindex.clear;
  185. end;
  186. procedure tsymtable.insert(sym:tsymentry);
  187. begin
  188. sym.owner:=self;
  189. { insert in index and search hash }
  190. symindex.insert(sym);
  191. symsearch.insert(sym);
  192. end;
  193. function tsymtable.search(const s : stringid) : tsymentry;
  194. begin
  195. search:=speedsearch(s,getspeedvalue(s));
  196. end;
  197. function tsymtable.speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;
  198. begin
  199. speedsearch:=tsymentry(symsearch.speedsearch(s,speedvalue));
  200. end;
  201. function tsymtable.rename(const olds,news : stringid):tsymentry;
  202. begin
  203. rename:=tsymentry(symsearch.rename(olds,news));
  204. end;
  205. function tsymtable.getsymnr(l : longint) : tsymentry;
  206. var
  207. hp : tsymentry;
  208. begin
  209. hp:=tsymentry(symindex.search(l));
  210. if hp=nil then
  211. internalerror(10999);
  212. getsymnr:=hp;
  213. end;
  214. function tsymtable.getdefnr(l : longint) : tdefentry;
  215. var
  216. hp : tdefentry;
  217. begin
  218. hp:=tdefentry(defindex.search(l));
  219. if hp=nil then
  220. internalerror(10998);
  221. getdefnr:=hp;
  222. end;
  223. {$ifdef GDB}
  224. function tsymtable.getnewtypecount : word;
  225. begin
  226. getnewtypecount:=0;
  227. end;
  228. {$endif GDB}
  229. {****************************************************************************
  230. TDeref
  231. ****************************************************************************}
  232. constructor tderef.create(typ:tdereftype;i:word);
  233. begin
  234. dereftype:=typ;
  235. index:=i;
  236. next:=nil;
  237. end;
  238. destructor tderef.destroy;
  239. begin
  240. end;
  241. end.
  242. {
  243. $Log$
  244. Revision 1.6 2002-05-18 13:34:18 peter
  245. * readded missing revisions
  246. Revision 1.5 2002/05/16 19:46:44 carl
  247. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  248. + try to fix temp allocation (still in ifdef)
  249. + generic constructor calls
  250. + start of tassembler / tmodulebase class cleanup
  251. Revision 1.3 2002/05/12 16:53:10 peter
  252. * moved entry and exitcode to ncgutil and cgobj
  253. * foreach gets extra argument for passing local data to the
  254. iterator function
  255. * -CR checks also class typecasts at runtime by changing them
  256. into as
  257. * fixed compiler to cycle with the -CR option
  258. * fixed stabs with elf writer, finally the global variables can
  259. be watched
  260. * removed a lot of routines from cga unit and replaced them by
  261. calls to cgobj
  262. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  263. u32bit then the other is typecasted also to u32bit without giving
  264. a rangecheck warning/error.
  265. * fixed pascal calling method with reversing also the high tree in
  266. the parast, detected by tcalcst3 test
  267. }