]> git.sur5r.net Git - cc65/blob - src/cc65/pragma.c
Renamed struct expent
[cc65] / src / cc65 / pragma.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 pragma.c                                  */
4 /*                                                                           */
5 /*                  Pragma handling for the cc65 C compiler                  */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2001 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
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 #include <stdlib.h>
37 #include <string.h>
38
39 /* cc65 */
40 #include "codegen.h"
41 #include "error.h"
42 #include "expr.h"
43 #include "global.h"
44 #include "litpool.h"
45 #include "scanner.h"
46 #include "segments.h"
47 #include "symtab.h"
48 #include "pragma.h"
49
50
51
52 /*****************************************************************************/
53 /*                                   data                                    */
54 /*****************************************************************************/
55
56
57
58 /* Tokens for the #pragmas */
59 typedef enum {
60     PR_BSSSEG,
61     PR_CHECKSTACK,
62     PR_CODESEG,
63     PR_DATASEG,
64     PR_REGVARADDR,
65     PR_RODATASEG,
66     PR_SIGNEDCHARS,
67     PR_STATICLOCALS,
68     PR_ZPSYM,
69     PR_ILLEGAL
70 } pragma_t;
71
72 /* Pragma table */
73 static const struct Pragma {
74     const char* Key;            /* Keyword */
75     pragma_t    Tok;            /* Token */
76 } Pragmas[] = {
77     {   "bssseg",       PR_BSSSEG       },
78     {   "checkstack",   PR_CHECKSTACK   },
79     {   "codeseg",      PR_CODESEG      },
80     {   "dataseg",      PR_DATASEG      },
81     {   "regvaraddr",   PR_REGVARADDR   },
82     {   "rodataseg",    PR_RODATASEG    },
83     {   "signedchars",  PR_SIGNEDCHARS  },
84     {   "staticlocals", PR_STATICLOCALS },
85     {   "zpsym",        PR_ZPSYM        },
86 };
87
88 /* Number of pragmas */
89 #define PRAGMA_COUNT    (sizeof(Pragmas) / sizeof(Pragmas[0]))
90
91
92
93 /*****************************************************************************/
94 /*                                   Code                                    */
95 /*****************************************************************************/
96
97
98
99 static int CmpKey (const void* Key, const void* Elem)
100 /* Compare function for bsearch */
101 {
102     return strcmp ((const char*) Key, ((const struct Pragma*) Elem)->Key);
103 }
104
105
106
107 static pragma_t FindPragma (const char* Key)
108 /* Find a pragma and return the token. Return PR_ILLEGAL if the keyword is
109  * not a valid pragma.
110  */
111 {
112     struct Pragma* P;
113     P = bsearch (Key, Pragmas, PRAGMA_COUNT, sizeof (Pragmas[0]), CmpKey);
114     return P? P->Tok : PR_ILLEGAL;
115 }
116
117
118
119 static void StringPragma (void (*Func) (const char*))
120 /* Handle a pragma that expects a string parameter */
121 {
122     if (CurTok.Tok != TOK_SCONST) {
123         Error ("String literal expected");
124     } else {
125         /* Get the string */
126         const char* Name = GetLiteral (CurTok.IVal);
127
128         /* Call the given function with the string argument */
129         Func (Name);
130
131         /* Reset the string pointer, removing the string from the pool */
132         ResetLiteralPoolOffs (CurTok.IVal);
133     }
134
135     /* Skip the string (or error) token */
136     NextToken ();
137 }
138
139
140
141 static void SegNamePragma (segment_t Seg)
142 /* Handle a pragma that expects a segment name parameter */
143 {
144     if (CurTok.Tok != TOK_SCONST) {
145         Error ("String literal expected");
146     } else {
147         /* Get the segment name */
148         const char* Name = GetLiteral (CurTok.IVal);
149
150         /* Check if the name is valid */
151         if (ValidSegName (Name)) {
152
153             /* Set the new name */
154             g_segname (Seg, Name);
155
156         } else {
157
158             /* Segment name is invalid */
159             Error ("Illegal segment name: `%s'", Name);
160
161         }
162
163         /* Reset the string pointer, removing the string from the pool */
164         ResetLiteralPoolOffs (CurTok.IVal);
165     }
166
167     /* Skip the string (or error) token */
168     NextToken ();
169 }
170
171
172
173 static void FlagPragma (unsigned char* Flag)
174 /* Handle a pragma that expects a boolean paramater */
175 {
176     /* Read a constant expression */
177     ExprDesc val;
178     constexpr (&val);
179
180     /* Store the value into the flag parameter */
181     *Flag = (val.e_const != 0);
182 }
183
184
185
186 void DoPragma (void)
187 /* Handle pragmas */
188 {
189     pragma_t Pragma;
190
191     /* Skip the token itself */
192     NextToken ();
193
194     /* Identifier must follow */
195     if (CurTok.Tok != TOK_IDENT) {
196         Error ("Identifier expected");
197         return;
198     }
199
200     /* Search for the name, then skip the identifier */
201     Pragma = FindPragma (CurTok.Ident);
202     NextToken ();
203
204     /* Do we know this pragma? */
205     if (Pragma == PR_ILLEGAL) {
206         /* According to the ANSI standard, we're not allowed to generate errors
207          * for unknown pragmas, however, we're allowed to warn - and we will
208          * do so. Otherwise one typo may give you hours of bug hunting...
209          */
210         Warning ("Unknown #pragma `%s'", CurTok.Ident);
211         return;
212     }
213
214     /* Check for an open paren */
215     ConsumeLParen ();
216
217     /* Switch for the different pragmas */
218     switch (Pragma) {
219
220         case PR_BSSSEG:
221             SegNamePragma (SEG_BSS);
222             break;
223
224         case PR_CHECKSTACK:
225             FlagPragma (&CheckStack);
226             break;
227
228         case PR_CODESEG:
229             SegNamePragma (SEG_CODE);
230             break;
231
232         case PR_DATASEG:
233             SegNamePragma (SEG_DATA);
234             break;
235
236         case PR_REGVARADDR:
237             FlagPragma (&AllowRegVarAddr);
238             break;
239
240         case PR_RODATASEG:
241             SegNamePragma (SEG_RODATA);
242             break;
243
244         case PR_SIGNEDCHARS:
245             FlagPragma (&SignedChars);
246             break;
247
248         case PR_STATICLOCALS:
249             FlagPragma (&StaticLocals);
250             break;
251
252         case PR_ZPSYM:
253             StringPragma (MakeZPSym);
254             break;
255
256         default:
257             Internal ("Invalid pragma");
258     }
259
260     /* Closing paren needed */
261     ConsumeRParen ();
262 }
263
264
265