1 /*****************************************************************************/
5 /* Pragma handling for the cc65 C compiler */
9 /* (C) 1998-2001 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@cc65.org */
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 /*****************************************************************************/
52 /*****************************************************************************/
54 /*****************************************************************************/
58 /* Tokens for the #pragmas */
73 static const struct Pragma {
74 const char* Key; /* Keyword */
75 pragma_t Tok; /* Token */
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 },
88 /* Number of pragmas */
89 #define PRAGMA_COUNT (sizeof(Pragmas) / sizeof(Pragmas[0]))
93 /*****************************************************************************/
95 /*****************************************************************************/
99 static int CmpKey (const void* Key, const void* Elem)
100 /* Compare function for bsearch */
102 return strcmp ((const char*) Key, ((const struct Pragma*) Elem)->Key);
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.
113 P = bsearch (Key, Pragmas, PRAGMA_COUNT, sizeof (Pragmas[0]), CmpKey);
114 return P? P->Tok : PR_ILLEGAL;
119 static void StringPragma (void (*Func) (const char*))
120 /* Handle a pragma that expects a string parameter */
122 if (CurTok.Tok != TOK_SCONST) {
123 Error ("String literal expected");
126 const char* Name = GetLiteral (CurTok.IVal);
128 /* Call the given function with the string argument */
131 /* Reset the string pointer, removing the string from the pool */
132 ResetLiteralPoolOffs (CurTok.IVal);
135 /* Skip the string (or error) token */
141 static void SegNamePragma (segment_t Seg)
142 /* Handle a pragma that expects a segment name parameter */
144 if (CurTok.Tok != TOK_SCONST) {
145 Error ("String literal expected");
147 /* Get the segment name */
148 const char* Name = GetLiteral (CurTok.IVal);
150 /* Check if the name is valid */
151 if (ValidSegName (Name)) {
153 /* Set the new name */
154 g_segname (Seg, Name);
158 /* Segment name is invalid */
159 Error ("Illegal segment name: `%s'", Name);
163 /* Reset the string pointer, removing the string from the pool */
164 ResetLiteralPoolOffs (CurTok.IVal);
167 /* Skip the string (or error) token */
173 static void FlagPragma (unsigned char* Flag)
174 /* Handle a pragma that expects a boolean paramater */
176 /* Read a constant expression */
180 /* Store the value into the flag parameter */
181 *Flag = (val.ConstVal != 0);
191 /* Skip the token itself */
194 /* Identifier must follow */
195 if (CurTok.Tok != TOK_IDENT) {
196 Error ("Identifier expected");
200 /* Search for the name, then skip the identifier */
201 Pragma = FindPragma (CurTok.Ident);
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...
210 Warning ("Unknown #pragma `%s'", CurTok.Ident);
214 /* Check for an open paren */
217 /* Switch for the different pragmas */
221 SegNamePragma (SEG_BSS);
225 FlagPragma (&CheckStack);
229 SegNamePragma (SEG_CODE);
233 SegNamePragma (SEG_DATA);
237 FlagPragma (&AllowRegVarAddr);
241 SegNamePragma (SEG_RODATA);
245 FlagPragma (&SignedChars);
248 case PR_STATICLOCALS:
249 FlagPragma (&StaticLocals);
253 StringPragma (MakeZPSym);
257 Internal ("Invalid pragma");
260 /* Closing paren needed */