symbase.pas 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.inc}
  20. interface
  21. uses
  22. { common }
  23. cutils,cobjects,
  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. {************************************************
  36. Needed forward pointers
  37. ************************************************}
  38. type
  39. psymtable = ^tsymtable;
  40. {************************************************
  41. TSymtableEntry
  42. ************************************************}
  43. psymtableentry = ^tsymtableentry;
  44. tsymtableentry = object(tnamedindexobject)
  45. owner : psymtable;
  46. end;
  47. {************************************************
  48. TDefEntry
  49. ************************************************}
  50. pdefentry = ^tdefentry;
  51. tdefentry = object(tsymtableentry)
  52. deftype : tdeftype;
  53. end;
  54. {************************************************
  55. TSymEntry
  56. ************************************************}
  57. { this object is the base for all symbol objects }
  58. psymentry = ^tsymentry;
  59. tsymentry = object(tsymtableentry)
  60. typ : tsymtyp;
  61. end;
  62. {************************************************
  63. TSymtable
  64. ************************************************}
  65. tsearchhasharray = array[0..hasharraysize-1] of psymentry;
  66. psearchhasharray = ^tsearchhasharray;
  67. tsymtable = object
  68. symtabletype : tsymtabletype;
  69. { each symtable gets a number }
  70. unitid : word{integer give range check errors PM};
  71. name : pstring;
  72. datasize : longint;
  73. dataalignment : longint;
  74. symindex,
  75. defindex : pindexarray;
  76. symsearch : pdictionary;
  77. next : psymtable;
  78. defowner : pdefentry; { for records and objects }
  79. { only used for parameter symtable to determine the offset relative }
  80. { to the frame pointer and for local inline }
  81. address_fixup : longint;
  82. { this saves all definition to allow a proper clean up }
  83. { separate lexlevel from symtable type }
  84. symtablelevel : byte;
  85. constructor init(t : tsymtabletype);
  86. destructor done;virtual;
  87. procedure clear;virtual;
  88. function rename(const olds,news : stringid):psymentry;
  89. procedure foreach(proc2call : tnamedindexcallback);
  90. procedure insert(sym : psymentry);virtual;
  91. function search(const s : stringid) : psymentry;
  92. function speedsearch(const s : stringid;speedvalue : longint) : psymentry;virtual;
  93. procedure registerdef(p : pdefentry);
  94. function getdefnr(l : longint) : pdefentry;
  95. function getsymnr(l : longint) : psymentry;
  96. {$ifdef GDB}
  97. function getnewtypecount : word; virtual;
  98. {$endif GDB}
  99. end;
  100. {************************************************
  101. TDeref
  102. ************************************************}
  103. pderef = ^tderef;
  104. tderef = object
  105. dereftype : tdereftype;
  106. index : word;
  107. next : pderef;
  108. constructor init(typ:tdereftype;i:word);
  109. destructor done;
  110. end;
  111. var
  112. registerdef : boolean; { true, when defs should be registered }
  113. defaultsymtablestack : psymtable; { symtablestack after default units have been loaded }
  114. symtablestack : psymtable; { linked list of symtables }
  115. aktrecordsymtable : psymtable; { current record read from ppu symtable }
  116. aktstaticsymtable : psymtable; { current static for local ppu symtable }
  117. aktlocalsymtable : psymtable; { current proc local for local ppu symtable }
  118. implementation
  119. uses
  120. verbose;
  121. {****************************************************************************
  122. TSYMTABLE
  123. ****************************************************************************}
  124. constructor tsymtable.init(t : tsymtabletype);
  125. begin
  126. symtabletype:=t;
  127. defowner:=nil;
  128. new(symindex,init(indexgrowsize));
  129. new(defindex,init(indexgrowsize));
  130. new(symsearch,init);
  131. symsearch^.noclear:=true;
  132. end;
  133. destructor tsymtable.done;
  134. begin
  135. stringdispose(name);
  136. dispose(symindex,done);
  137. dispose(defindex,done);
  138. { symsearch can already be disposed or set to nil for withsymtable }
  139. if assigned(symsearch) then
  140. begin
  141. dispose(symsearch,done);
  142. symsearch:=nil;
  143. end;
  144. end;
  145. procedure tsymtable.registerdef(p : pdefentry);
  146. begin
  147. defindex^.insert(p);
  148. { set def owner and indexnb }
  149. p^.owner:=@self;
  150. end;
  151. procedure tsymtable.foreach(proc2call : tnamedindexcallback);
  152. begin
  153. symindex^.foreach(proc2call);
  154. end;
  155. {***********************************************
  156. Table Access
  157. ***********************************************}
  158. procedure tsymtable.clear;
  159. begin
  160. symindex^.clear;
  161. defindex^.clear;
  162. end;
  163. procedure tsymtable.insert(sym:psymentry);
  164. begin
  165. sym^.owner:=@self;
  166. { insert in index and search hash }
  167. symindex^.insert(sym);
  168. symsearch^.insert(sym);
  169. end;
  170. function tsymtable.search(const s : stringid) : psymentry;
  171. begin
  172. search:=speedsearch(s,getspeedvalue(s));
  173. end;
  174. function tsymtable.speedsearch(const s : stringid;speedvalue : longint) : psymentry;
  175. begin
  176. speedsearch:=psymentry(symsearch^.speedsearch(s,speedvalue));
  177. end;
  178. function tsymtable.rename(const olds,news : stringid):psymentry;
  179. begin
  180. rename:=psymentry(symsearch^.rename(olds,news));
  181. end;
  182. function tsymtable.getsymnr(l : longint) : psymentry;
  183. var
  184. hp : psymentry;
  185. begin
  186. hp:=psymentry(symindex^.search(l));
  187. if hp=nil then
  188. internalerror(10999);
  189. getsymnr:=hp;
  190. end;
  191. function tsymtable.getdefnr(l : longint) : pdefentry;
  192. var
  193. hp : pdefentry;
  194. begin
  195. hp:=pdefentry(defindex^.search(l));
  196. if hp=nil then
  197. internalerror(10998);
  198. getdefnr:=hp;
  199. end;
  200. {$ifdef GDB}
  201. function tsymtable.getnewtypecount : word;
  202. begin
  203. getnewtypecount:=0;
  204. end;
  205. {$endif GDB}
  206. {****************************************************************************
  207. TDeref
  208. ****************************************************************************}
  209. constructor tderef.init(typ:tdereftype;i:word);
  210. begin
  211. dereftype:=typ;
  212. index:=i;
  213. next:=nil;
  214. end;
  215. destructor tderef.done;
  216. begin
  217. end;
  218. end.
  219. {
  220. $Log$
  221. Revision 1.1 2000-10-31 22:02:51 peter
  222. * symtable splitted, no real code changes
  223. }