]> git.sur5r.net Git - cc65/blob - src/ca65/symtab.h
Maintain some additional information for scopes. Write a dummy scope section
[cc65] / src / ca65 / symtab.h
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 symtab.h                                  */
4 /*                                                                           */
5 /*                 Symbol table for the ca65 macroassembler                  */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2010, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #ifndef SYMTAB_H
37 #define SYMTAB_H
38
39
40
41 #include <stdio.h>
42
43 /* common */
44 #include "exprdefs.h"
45 #include "inline.h"
46
47 /* ca65 */
48 #include "segrange.h"
49 #include "symentry.h"
50
51
52
53 /*****************************************************************************/
54 /*                                   Data                                    */
55 /*****************************************************************************/
56
57
58
59 /* Symbol table flags */
60 #define ST_NONE         0x00            /* No flags */
61 #define ST_DEFINED      0x01            /* Scope has been defined */
62
63 /* Symbol table types */
64 enum {
65     ST_GLOBAL,                          /* Root level */
66     ST_PROC,                            /* .PROC */
67     ST_SCOPE,                           /* .SCOPE */
68     ST_SCOPE_HAS_DATA = ST_SCOPE,       /* Last scope that contains data */
69     ST_STRUCT,                          /* .STRUCT/.UNION */
70     ST_ENUM,                            /* .ENUM */
71     ST_UNDEF    = 0xFF
72 };
73
74 /* A symbol table */
75 typedef struct SymTable SymTable;
76 struct SymTable {                                     
77     SymTable*           Next;           /* Pointer to next table in list */
78     SymTable*           Left;           /* Pointer to smaller entry */
79     SymTable*           Right;          /* Pointer to greater entry */
80     SymTable*           Parent;         /* Link to enclosing scope if any */
81     SymTable*           Childs;         /* Pointer to child scopes */
82     Collection          SegRanges;      /* Segment ranges for this scope */
83     unsigned            Id;             /* Scope id */
84     unsigned short      Flags;          /* Symbol table flags */
85     unsigned char       AddrSize;       /* Address size */
86     unsigned char       Type;           /* Type of the scope */
87     unsigned            Level;          /* Lexical level */
88     unsigned            TableSlots;     /* Number of hash table slots */
89     unsigned            TableEntries;   /* Number of entries in the table */
90     unsigned            Name;           /* Name of the scope */
91     SymEntry*           Table[1];       /* Dynamic allocation */
92 };
93
94 /* Symbol tables */
95 extern SymTable*        CurrentScope;   /* Pointer to current symbol table */
96 extern SymTable*        RootScope;      /* Root symbol table */
97
98
99
100 /*****************************************************************************/
101 /*                                   Code                                    */
102 /*****************************************************************************/
103
104
105
106 void SymEnterLevel (const StrBuf* ScopeName, unsigned char Type, unsigned char AddrSize);
107 /* Enter a new lexical level */
108
109 void SymLeaveLevel (void);
110 /* Leave the current lexical level */
111
112 SymTable* SymFindScope (SymTable* Parent, const StrBuf* Name, int AllocNew);
113 /* Find a scope in the given enclosing scope */
114
115 SymTable* SymFindAnyScope (SymTable* Parent, const StrBuf* Name);
116 /* Find a scope in the given or any of its parent scopes. The function will
117  * never create a new symbol, since this can only be done in one specific
118  * scope.
119  */
120
121 SymEntry* SymFindLocal (SymEntry* Parent, const StrBuf* StrBuf, int AllocNew);
122 /* Find a cheap local symbol. If AllocNew is given and the entry is not
123  * found, create a new one. Return the entry found, or the new entry created,
124  * or - in case AllocNew is zero - return 0.
125  */
126
127 SymEntry* SymFind (SymTable* Scope, const StrBuf* Name, int AllocNew);
128 /* Find a new symbol table entry in the given table. If AllocNew is given and
129  * the entry is not found, create a new one. Return the entry found, or the
130  * new entry created, or - in case AllocNew is zero - return 0.
131  */
132
133 SymEntry* SymFindAny (SymTable* Scope, const StrBuf* Name);
134 /* Find a symbol in the given or any of its parent scopes. The function will
135  * never create a new symbol, since this can only be done in one specific
136  * scope.
137  */
138
139 #if defined(HAVE_INLINE)
140 INLINE unsigned char GetSymTabType (const SymTable* S)
141 /* Return the type of the given symbol table */
142 {
143     return S->Type;
144 }
145 #else
146 #  define GetSymTabType(S)      ((S)->Type)
147 #endif
148
149 unsigned char GetCurrentSymTabType ();
150 /* Return the type of the current symbol table */
151
152 void SymCheck (void);
153 /* Run through all symbols and check for anomalies and errors */
154
155 void SymDump (FILE* F);
156 /* Dump the symbol table */
157
158 void WriteImports (void);
159 /* Write the imports list to the object file */
160
161 void WriteExports (void);
162 /* Write the exports list to the object file */
163
164 void WriteDbgSyms (void);
165 /* Write a list of all symbols to the object file */
166
167 void WriteScopes (void);
168 /* Write the scope table to the object file */
169
170
171
172 /* End of symtab.h */
173
174 #endif
175
176
177