symbase.pas 11 KB

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