2003-04-15 David Carlton <carlton@math.stanford.edu>
[external/binutils.git] / gdb / block.h
1 /* Code dealing with blocks for GDB.
2
3    Copyright 2003 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #ifndef BLOCK_H
23 #define BLOCK_H
24
25 /* Opaque declarations.  */
26
27 struct symbol;
28 struct symtab;
29 struct block_namespace_info;
30 struct using_direct;
31 struct obstack;
32
33 /* All of the name-scope contours of the program
34    are represented by `struct block' objects.
35    All of these objects are pointed to by the blockvector.
36
37    Each block represents one name scope.
38    Each lexical context has its own block.
39
40    The blockvector begins with some special blocks.
41    The GLOBAL_BLOCK contains all the symbols defined in this compilation
42    whose scope is the entire program linked together.
43    The STATIC_BLOCK contains all the symbols whose scope is the
44    entire compilation excluding other separate compilations.
45    Blocks starting with the FIRST_LOCAL_BLOCK are not special.
46
47    Each block records a range of core addresses for the code that
48    is in the scope of the block.  The STATIC_BLOCK and GLOBAL_BLOCK
49    give, for the range of code, the entire range of code produced
50    by the compilation that the symbol segment belongs to.
51
52    The blocks appear in the blockvector
53    in order of increasing starting-address,
54    and, within that, in order of decreasing ending-address.
55
56    This implies that within the body of one function
57    the blocks appear in the order of a depth-first tree walk.  */
58
59 struct block
60 {
61
62   /* Addresses in the executable code that are in this block.  */
63
64   CORE_ADDR startaddr;
65   CORE_ADDR endaddr;
66
67   /* The symbol that names this block, if the block is the body of a
68      function; otherwise, zero.  */
69
70   struct symbol *function;
71
72   /* The `struct block' for the containing block, or 0 if none.
73
74      The superblock of a top-level local block (i.e. a function in the
75      case of C) is the STATIC_BLOCK.  The superblock of the
76      STATIC_BLOCK is the GLOBAL_BLOCK.  */
77
78   struct block *superblock;
79
80   /* Used for language-specific info.  */
81
82   union
83   {
84     struct
85     {
86       /* Contains information about namespace-related info relevant to
87          this block: using directives and the current namespace
88          scope.  */
89       
90       struct block_namespace_info *namespace;
91     }
92     cplus_specific;
93   }
94   language_specific;
95
96   /* Version of GCC used to compile the function corresponding
97      to this block, or 0 if not compiled with GCC.  When possible,
98      GCC should be compatible with the native compiler, or if that
99      is not feasible, the differences should be fixed during symbol
100      reading.  As of 16 Apr 93, this flag is never used to distinguish
101      between gcc2 and the native compiler.
102
103      If there is no function corresponding to this block, this meaning
104      of this flag is undefined.  */
105
106   unsigned char gcc_compile_flag;
107
108   /* The symbols for this block are either in a simple linear list or
109      in a simple hashtable.  Blocks which correspond to a function
110      (which have a list of symbols corresponding to arguments) use
111      a linear list, as do some older symbol readers (currently only
112      mdebugread and dstread).  Other blocks are hashed.
113
114      The hashtable uses the same hash function as the minsym hashtables,
115      found in minsyms.c:minsym_hash_iw.  Symbols are hashed based on
116      their demangled name if appropriate, and on their name otherwise.
117      The hash function ignores space, and stops at the beginning of the
118      argument list if any.
119
120      The table is laid out in NSYMS/5 buckets and symbols are chained via
121      their hash_next field.  */
122
123   /* If this is really a hashtable of the symbols, this flag is 1.  */
124
125   unsigned char hashtable;
126
127   /* Number of local symbols.  */
128
129   int nsyms;
130
131   /* The symbols.  If some of them are arguments, then they must be
132      in the order in which we would like to print them.  */
133
134   struct symbol *sym[1];
135 };
136
137 #define BLOCK_START(bl)         (bl)->startaddr
138 #define BLOCK_END(bl)           (bl)->endaddr
139 #define BLOCK_FUNCTION(bl)      (bl)->function
140 #define BLOCK_SUPERBLOCK(bl)    (bl)->superblock
141 #define BLOCK_GCC_COMPILED(bl)  (bl)->gcc_compile_flag
142 #define BLOCK_NAMESPACE(bl)   (bl)->language_specific.cplus_specific.namespace
143 #define BLOCK_HASHTABLE(bl)     (bl)->hashtable
144
145 /* For blocks without a hashtable (BLOCK_HASHTABLE (bl) == 0) only.  */
146 #define BLOCK_NSYMS(bl)         (bl)->nsyms
147 #define BLOCK_SYM(bl, n)        (bl)->sym[n]
148
149 /* For blocks with a hashtable, but these are valid for non-hashed blocks as
150    well - each symbol will appear to be one bucket by itself.  */
151 #define BLOCK_BUCKETS(bl)       (bl)->nsyms
152 #define BLOCK_BUCKET(bl, n)     (bl)->sym[n]
153
154 /* Macro used to set the size of a hashtable for N symbols.  */
155 #define BLOCK_HASHTABLE_SIZE(n) ((n)/5 + 1)
156
157 /* Macro to loop through all symbols in a block BL, in no particular order.
158    i counts which bucket we are in, and sym points to the current symbol.  */
159
160 #define ALL_BLOCK_SYMBOLS(bl, i, sym)                           \
161         for ((i) = 0; (i) < BLOCK_BUCKETS ((bl)); (i)++)        \
162           for ((sym) = BLOCK_BUCKET ((bl), (i)); (sym);         \
163                (sym) = (sym)->hash_next)
164
165 /* Nonzero if symbols of block BL should be sorted alphabetically.
166    Don't sort a block which corresponds to a function.  If we did the
167    sorting would have to preserve the order of the symbols for the
168    arguments.  Also don't sort any block that we chose to hash.  */
169
170 #define BLOCK_SHOULD_SORT(bl) (! BLOCK_HASHTABLE (bl) \
171                                && BLOCK_FUNCTION (bl) == NULL)
172
173 struct blockvector
174 {
175   /* Number of blocks in the list.  */
176   int nblocks;
177   /* The blocks themselves.  */
178   struct block *block[1];
179 };
180
181 #define BLOCKVECTOR_NBLOCKS(blocklist) (blocklist)->nblocks
182 #define BLOCKVECTOR_BLOCK(blocklist,n) (blocklist)->block[n]
183
184 /* Special block numbers */
185
186 #define GLOBAL_BLOCK            0
187 #define STATIC_BLOCK            1
188 #define FIRST_LOCAL_BLOCK       2
189
190 extern struct symbol *block_function (struct block *);
191
192 extern int contained_in (struct block *, struct block *);
193
194 extern struct blockvector *blockvector_for_pc (CORE_ADDR, int *);
195
196 extern struct blockvector *blockvector_for_pc_sect (CORE_ADDR, asection *,
197                                                     int *, struct symtab *);
198
199 extern struct block *block_for_pc (CORE_ADDR);
200
201 extern struct block *block_for_pc_sect (CORE_ADDR, asection *);
202
203 extern void block_set_scope (struct block *block, const char *scope,
204                              struct obstack *obstack);
205
206 extern void block_set_using (struct block *block,
207                              struct using_direct *using,
208                              struct obstack *obstack);
209
210 #endif /* BLOCK_H */