symbase.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. procedure insertvardata(sym : tsymentry);virtual;abstract;
  95. procedure insertconstdata(sym : tsymentry);virtual;abstract;
  96. function search(const s : stringid) : tsymentry;
  97. function speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;virtual;
  98. procedure registerdef(p : tdefentry);
  99. function getdefnr(l : longint) : tdefentry;
  100. function getsymnr(l : longint) : tsymentry;
  101. {$ifdef GDB}
  102. function getnewtypecount : word; virtual;
  103. {$endif GDB}
  104. end;
  105. {************************************************
  106. TDeref
  107. ************************************************}
  108. tderef = class
  109. dereftype : tdereftype;
  110. index : word;
  111. next : tderef;
  112. constructor create(typ:tdereftype;i:word);
  113. destructor destroy;override;
  114. end;
  115. var
  116. registerdef : boolean; { true, when defs should be registered }
  117. defaultsymtablestack : tsymtable; { symtablestack after default units have been loaded }
  118. symtablestack : tsymtable; { linked list of symtables }
  119. aktrecordsymtable : tsymtable; { current record read from ppu symtable }
  120. aktstaticsymtable : tsymtable; { current static for local ppu symtable }
  121. aktlocalsymtable : tsymtable; { current proc local for local ppu symtable }
  122. implementation
  123. uses
  124. verbose;
  125. {****************************************************************************
  126. TSYMTABLE
  127. ****************************************************************************}
  128. constructor tsymtable.Create(const s:string);
  129. begin
  130. if s<>'' then
  131. begin
  132. name:=stringdup(upper(s));
  133. realname:=stringdup(s);
  134. end
  135. else
  136. begin
  137. name:=nil;
  138. realname:=nil;
  139. end;
  140. symtabletype:=abstractsymtable;
  141. symtablelevel:=0;
  142. defowner:=nil;
  143. next:=nil;
  144. symindex:=tindexarray.create(indexgrowsize);
  145. defindex:=TIndexArray.create(indexgrowsize);
  146. symsearch:=tdictionary.create;
  147. symsearch.noclear:=true;
  148. unitid:=0;
  149. address_fixup:=0;
  150. datasize:=0;
  151. dataalignment:=1;
  152. end;
  153. destructor tsymtable.destroy;
  154. begin
  155. stringdispose(name);
  156. stringdispose(realname);
  157. symindex.destroy;
  158. defindex.destroy;
  159. { symsearch can already be disposed or set to nil for withsymtable }
  160. if assigned(symsearch) then
  161. begin
  162. symsearch.destroy;
  163. symsearch:=nil;
  164. end;
  165. end;
  166. procedure tsymtable.registerdef(p : tdefentry);
  167. begin
  168. defindex.insert(p);
  169. { set def owner and indexnb }
  170. p.owner:=self;
  171. end;
  172. procedure tsymtable.foreach(proc2call : tnamedindexcallback;arg:pointer);
  173. begin
  174. symindex.foreach(proc2call,arg);
  175. end;
  176. procedure tsymtable.foreach_static(proc2call : tnamedindexstaticcallback;arg:pointer);
  177. begin
  178. symindex.foreach_static(proc2call,arg);
  179. end;
  180. {***********************************************
  181. Table Access
  182. ***********************************************}
  183. procedure tsymtable.clear;
  184. begin
  185. symindex.clear;
  186. defindex.clear;
  187. end;
  188. procedure tsymtable.insert(sym:tsymentry);
  189. begin
  190. sym.owner:=self;
  191. { insert in index and search hash }
  192. symindex.insert(sym);
  193. symsearch.insert(sym);
  194. end;
  195. function tsymtable.search(const s : stringid) : tsymentry;
  196. begin
  197. search:=speedsearch(s,getspeedvalue(s));
  198. end;
  199. function tsymtable.speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;
  200. begin
  201. speedsearch:=tsymentry(symsearch.speedsearch(s,speedvalue));
  202. end;
  203. function tsymtable.rename(const olds,news : stringid):tsymentry;
  204. begin
  205. rename:=tsymentry(symsearch.rename(olds,news));
  206. end;
  207. function tsymtable.getsymnr(l : longint) : tsymentry;
  208. var
  209. hp : tsymentry;
  210. begin
  211. hp:=tsymentry(symindex.search(l));
  212. if hp=nil then
  213. internalerror(10999);
  214. getsymnr:=hp;
  215. end;
  216. function tsymtable.getdefnr(l : longint) : tdefentry;
  217. var
  218. hp : tdefentry;
  219. begin
  220. hp:=tdefentry(defindex.search(l));
  221. if hp=nil then
  222. internalerror(10998);
  223. getdefnr:=hp;
  224. end;
  225. {$ifdef GDB}
  226. function tsymtable.getnewtypecount : word;
  227. begin
  228. getnewtypecount:=0;
  229. end;
  230. {$endif GDB}
  231. {****************************************************************************
  232. TDeref
  233. ****************************************************************************}
  234. constructor tderef.create(typ:tdereftype;i:word);
  235. begin
  236. dereftype:=typ;
  237. index:=i;
  238. next:=nil;
  239. end;
  240. destructor tderef.destroy;
  241. begin
  242. end;
  243. end.
  244. {
  245. $Log$
  246. Revision 1.7 2002-08-25 19:25:20 peter
  247. * sym.insert_in_data removed
  248. * symtable.insertvardata/insertconstdata added
  249. * removed insert_in_data call from symtable.insert, it needs to be
  250. called separatly. This allows to deref the address calculation
  251. * procedures now calculate the parast addresses after the procedure
  252. directives are parsed. This fixes the cdecl parast problem
  253. * push_addr_param has an extra argument that specifies if cdecl is used
  254. or not
  255. Revision 1.6 2002/05/18 13:34:18 peter
  256. * readded missing revisions
  257. Revision 1.5 2002/05/16 19:46:44 carl
  258. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  259. + try to fix temp allocation (still in ifdef)
  260. + generic constructor calls
  261. + start of tassembler / tmodulebase class cleanup
  262. Revision 1.3 2002/05/12 16:53:10 peter
  263. * moved entry and exitcode to ncgutil and cgobj
  264. * foreach gets extra argument for passing local data to the
  265. iterator function
  266. * -CR checks also class typecasts at runtime by changing them
  267. into as
  268. * fixed compiler to cycle with the -CR option
  269. * fixed stabs with elf writer, finally the global variables can
  270. be watched
  271. * removed a lot of routines from cga unit and replaced them by
  272. calls to cgobj
  273. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  274. u32bit then the other is typecasted also to u32bit without giving
  275. a rangecheck warning/error.
  276. * fixed pascal calling method with reversing also the high tree in
  277. the parast, detected by tcalcst3 test
  278. }