1 /*****************************************************************************/
5 /* Pragma handling for the cc65 C compiler */
9 /* (C) 1998-2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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. */
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: */
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 */
32 /*****************************************************************************/
53 /*****************************************************************************/
55 /*****************************************************************************/
59 /* Tokens for the #pragmas */
73 static const struct Pragma {
74 const char* Key; /* Keyword */
75 pragma_t Tok; /* Token */
77 { "bssseg", PR_BSSSEG },
78 { "codeseg", PR_CODESEG },
79 { "dataseg", PR_DATASEG },
80 { "regvaraddr", PR_REGVARADDR },
81 { "rodataseg", PR_RODATASEG },
82 { "signedchars", PR_SIGNEDCHARS },
83 { "staticlocals", PR_STATICLOCALS },
84 { "zpsym", PR_ZPSYM },
87 /* Number of pragmas */
88 #define PRAGMA_COUNT (sizeof(Pragmas) / sizeof(Pragmas[0]))
92 /*****************************************************************************/
94 /*****************************************************************************/
98 static int CmpKey (const void* Key, const void* Elem)
99 /* Compare function for bsearch */
101 return strcmp ((const char*) Key, ((const struct Pragma*) Elem)->Key);
106 static pragma_t FindPragma (const char* Key)
107 /* Find a pragma and return the token. Return PR_ILLEGAL if the keyword is
108 * not a valid pragma.
112 P = bsearch (Key, Pragmas, PRAGMA_COUNT, sizeof (Pragmas[0]), CmpKey);
113 return P? P->Tok : PR_ILLEGAL;
118 static void StringPragma (void (*Func) (const char*))
119 /* Handle a pragma that expects a string parameter */
121 if (curtok != TOK_SCONST) {
122 Error ("String literal expected");
125 const char* Name = GetLiteral (curval);
127 /* Call the given function with the string argument */
130 /* Reset the string pointer, removing the string from the pool */
131 ResetLiteralOffs (curval);
134 /* Skip the string (or error) token */
140 static void SegNamePragma (void (*Func) (const char*))
141 /* Handle a pragma that expects a segment name parameter */
143 if (curtok != TOK_SCONST) {
144 Error ("String literal expected");
146 /* Get the segment name */
147 const char* Name = GetLiteral (curval);
149 /* Check if the name is valid */
150 if (ValidSegName (Name)) {
152 /* Call the given function to set the name */
157 /* Segment name is invalid */
158 Error ("Illegal segment name: `%s'", Name);
162 /* Reset the string pointer, removing the string from the pool */
163 ResetLiteralOffs (curval);
166 /* Skip the string (or error) token */
172 static void FlagPragma (unsigned char* Flag)
173 /* Handle a pragma that expects a boolean paramater */
175 /* Read a constant expression */
179 /* Store the value into the flag parameter */
180 *Flag = (val.e_const != 0);
190 /* Skip the token itself */
193 /* Identifier must follow */
194 if (curtok != TOK_IDENT) {
195 Error ("Identifier expected");
199 /* Do we know this pragma? */
200 Pragma = FindPragma (CurTok.Ident);
201 if (Pragma == PR_ILLEGAL) {
202 /* According to the ANSI standard, we're not allowed to generate errors
203 * for unknown pragmas, however, we're allowed to warn - and we will
204 * do so. Otherwise one typo may give you hours of bug hunting...
206 Warning ("Unknown #pragma `%s'", CurTok.Ident);
210 /* Skip the identifier and check for an open paren */
214 /* Switch for the different pragmas */
218 SegNamePragma (g_bssname);
222 SegNamePragma (g_codename);
226 SegNamePragma (g_dataname);
230 FlagPragma (&AllowRegVarAddr);
234 SegNamePragma (g_rodataname);
238 FlagPragma (&SignedChars);
241 case PR_STATICLOCALS:
242 FlagPragma (&StaticLocals);
246 StringPragma (MakeZPSym);
250 Internal ("Invalid pragma");
253 /* Closing paren needed */