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 /*****************************************************************************/
51 /*****************************************************************************/
53 /*****************************************************************************/
57 /* Tokens for the #pragmas */
71 static const struct Pragma {
72 const char* Key; /* Keyword */
73 pragma_t Tok; /* Token */
75 { "bssseg", PR_BSSSEG },
76 { "codeseg", PR_CODESEG },
77 { "dataseg", PR_DATASEG },
78 { "regvaraddr", PR_REGVARADDR },
79 { "rodataseg", PR_RODATASEG },
80 { "signedchars", PR_SIGNEDCHARS },
81 { "staticlocals", PR_STATICLOCALS },
82 { "zpsym", PR_ZPSYM },
85 /* Number of pragmas */
86 #define PRAGMA_COUNT (sizeof(Pragmas) / sizeof(Pragmas[0]))
90 /*****************************************************************************/
92 /*****************************************************************************/
96 static int CmpKey (const void* Key, const void* Elem)
97 /* Compare function for bsearch */
99 return strcmp ((const char*) Key, ((const struct Pragma*) Elem)->Key);
104 static pragma_t FindPragma (const char* Key)
105 /* Find a pragma and return the token. Return PR_ILLEGAL if the keyword is
106 * not a valid pragma.
110 P = bsearch (Key, Pragmas, PRAGMA_COUNT, sizeof (Pragmas[0]), CmpKey);
111 return P? P->Tok : PR_ILLEGAL;
116 static void StringPragma (void (*Func) (const char*))
117 /* Handle a pragma that expects a string parameter */
119 if (curtok != TOK_SCONST) {
120 Error ("String literal expected");
123 const char* Name = GetLiteral (curval);
125 /* Call the given function with the string argument */
128 /* Reset the string pointer, removing the string from the pool */
129 ResetLiteralOffs (curval);
132 /* Skip the string (or error) token */
138 static void SegNamePragma (void (*Func) (const char*))
139 /* Handle a pragma that expects a segment name parameter */
141 if (curtok != TOK_SCONST) {
142 Error ("String literal expected");
144 /* Get the segment name */
145 const char* Name = GetLiteral (curval);
147 /* Check if the name is valid */
148 if (ValidSegName (Name)) {
150 /* Call the given function to set the name */
155 /* Segment name is invalid */
156 Error ("Illegal segment name: `%s'", Name);
160 /* Reset the string pointer, removing the string from the pool */
161 ResetLiteralOffs (curval);
164 /* Skip the string (or error) token */
170 static void FlagPragma (unsigned char* Flag)
171 /* Handle a pragma that expects a boolean paramater */
173 /* Read a constant expression */
177 /* Store the value into the flag parameter */
178 *Flag = (val.e_const != 0);
188 /* Skip the token itself */
191 /* Identifier must follow */
192 if (curtok != TOK_IDENT) {
193 Error ("Identifier expected");
197 /* Do we know this pragma? */
198 Pragma = FindPragma (CurTok.Ident);
199 if (Pragma == PR_ILLEGAL) {
200 /* According to the ANSI standard, we're not allowed to generate errors
201 * for unknown pragmas, however, we're allowed to warn - and we will
202 * do so. Otherwise one typo may give you hours of bug hunting...
204 Warning ("Unknown #pragma `%s'", CurTok.Ident);
208 /* Skip the identifier and check for an open paren */
212 /* Switch for the different pragmas */
216 SegNamePragma (g_bssname);
220 SegNamePragma (g_codename);
224 SegNamePragma (g_dataname);
228 FlagPragma (&AllowRegVarAddr);
232 SegNamePragma (g_rodataname);
236 FlagPragma (&SignedChars);
239 case PR_STATICLOCALS:
240 FlagPragma (&StaticLocals);
244 StringPragma (MakeZPSym);
248 Internal ("Invalid pragma");
251 /* Closing paren needed */