1 /*****************************************************************************/
5 /* Pragma handling for the cc65 C compiler */
9 /* (C) 1998 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 /*****************************************************************************/
50 /*****************************************************************************/
52 /*****************************************************************************/
56 /* Tokens for the #pragmas */
70 static const struct Pragma {
71 const char* Key; /* Keyword */
72 pragma_t Tok; /* Token */
74 { "bssseg", PR_BSSSEG },
75 { "codeseg", PR_CODESEG },
76 { "dataseg", PR_DATASEG },
77 { "regvaraddr", PR_REGVARADDR },
78 { "rodataseg", PR_RODATASEG },
79 { "signedchars", PR_SIGNEDCHARS },
80 { "staticlocals", PR_STATICLOCALS },
81 { "zpsym", PR_ZPSYM },
84 /* Number of pragmas */
85 #define PRAGMA_COUNT (sizeof(Pragmas) / sizeof(Pragmas[0]))
89 /*****************************************************************************/
91 /*****************************************************************************/
95 static int CmpKey (const void* Key, const void* Elem)
96 /* Compare function for bsearch */
98 return strcmp ((const char*) Key, ((const struct Pragma*) Elem)->Key);
103 static pragma_t FindPragma (const char* Key)
104 /* Find a pragma and return the token. Return PR_ILLEGAL if the keyword is
105 * not a valid pragma.
109 P = bsearch (Key, Pragmas, PRAGMA_COUNT, sizeof (Pragmas[0]), CmpKey);
110 return P? P->Tok : PR_ILLEGAL;
115 static void StringPragma (void (*Func) (const char*))
116 /* Handle a pragma that expects a string parameter */
118 if (curtok != TOK_SCONST) {
119 Error (ERR_STRLIT_EXPECTED);
122 const char* Name = GetLiteral (curval);
124 /* Call the given function with the string argument */
127 /* Reset the string pointer, removing the string from the pool */
128 ResetLiteralOffs (curval);
131 /* Skip the string (or error) token */
137 static void FlagPragma (unsigned char* Flag)
138 /* Handle a pragma that expects a boolean paramater */
140 /* Read a constant expression */
144 /* Store the value into the flag parameter */
145 *Flag = (val.e_const != 0);
155 /* Skip the token itself */
158 /* Identifier must follow */
159 if (curtok != TOK_IDENT) {
160 Error (ERR_IDENT_EXPECTED);
164 /* Do we know this pragma? */
165 Pragma = FindPragma (CurTok.Ident);
166 if (Pragma == PR_ILLEGAL) {
167 /* According to the ANSI standard, we're not allowed to generate errors
168 * for unknown pragmas, however, we're allowed to warn - and we will
169 * do so. Otherwise one typo may give you hours of bug hunting...
171 Warning (WARN_UNKNOWN_PRAGMA);
175 /* Skip the identifier and check for an open paren */
179 /* Switch for the different pragmas */
183 StringPragma (g_bssname);
187 StringPragma (g_codename);
191 StringPragma (g_dataname);
195 FlagPragma (&AllowRegVarAddr);
199 StringPragma (g_rodataname);
203 FlagPragma (&SignedChars);
206 case PR_STATICLOCALS:
207 FlagPragma (&StaticLocals);
211 StringPragma (MakeZPSym);
215 Internal ("Invalid pragma");
218 /* Closing paren needed */