1 /*****************************************************************************/
5 /* Pragma handling for the cc65 C compiler */
9 /* (C) 1998-2002 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 /*****************************************************************************/
55 /*****************************************************************************/
57 /*****************************************************************************/
61 /* Tokens for the #pragmas */
78 static const struct Pragma {
79 const char* Key; /* Keyword */
80 pragma_t Tok; /* Token */
81 } Pragmas[PR_COUNT] = {
82 { "bssseg", PR_BSSSEG },
83 { "charmap", PR_CHARMAP },
84 { "checkstack", PR_CHECKSTACK },
85 { "codeseg", PR_CODESEG },
86 { "dataseg", PR_DATASEG },
87 { "regvaraddr", PR_REGVARADDR },
88 { "rodataseg", PR_RODATASEG },
89 { "signedchars", PR_SIGNEDCHARS },
90 { "staticlocals", PR_STATICLOCALS },
91 { "zpsym", PR_ZPSYM },
96 /*****************************************************************************/
98 /*****************************************************************************/
102 static int CmpKey (const void* Key, const void* Elem)
103 /* Compare function for bsearch */
105 return strcmp ((const char*) Key, ((const struct Pragma*) Elem)->Key);
110 static pragma_t FindPragma (const char* Key)
111 /* Find a pragma and return the token. Return PR_ILLEGAL if the keyword is
112 * not a valid pragma.
116 P = bsearch (Key, Pragmas, PR_COUNT, sizeof (Pragmas[0]), CmpKey);
117 return P? P->Tok : PR_ILLEGAL;
122 static void StringPragma (void (*Func) (const char*))
123 /* Handle a pragma that expects a string parameter */
125 if (CurTok.Tok != TOK_SCONST) {
126 Error ("String literal expected");
129 const char* Name = GetLiteral (CurTok.IVal);
131 /* Call the given function with the string argument */
134 /* Reset the string pointer, removing the string from the pool */
135 ResetLiteralPoolOffs (CurTok.IVal);
138 /* Skip the string (or error) token */
144 static void SegNamePragma (segment_t Seg)
145 /* Handle a pragma that expects a segment name parameter */
147 if (CurTok.Tok != TOK_SCONST) {
148 Error ("String literal expected");
150 /* Get the segment name */
151 const char* Name = GetLiteral (CurTok.IVal);
153 /* Check if the name is valid */
154 if (ValidSegName (Name)) {
156 /* Set the new name */
157 g_segname (Seg, Name);
161 /* Segment name is invalid */
162 Error ("Illegal segment name: `%s'", Name);
166 /* Reset the string pointer, removing the string from the pool */
167 ResetLiteralPoolOffs (CurTok.IVal);
170 /* Skip the string (or error) token */
176 static void CharMapPragma (void)
177 /* Change the character map */
183 /* Read the character index */
185 if (Val.ConstVal < 1 || Val.ConstVal > 255) {
186 Error ("Character index out of range");
189 Index = Val.ConstVal;
195 /* Read the character code */
197 if (Val.ConstVal < 1 || Val.ConstVal > 255) {
198 Error ("Character code out of range");
204 /* Remap the character */
205 TgtTranslateSet (Index, C);
210 static void FlagPragma (unsigned char* Flag)
211 /* Handle a pragma that expects a boolean paramater */
213 /* Read a constant integer expression */
217 /* Store the value into the flag parameter */
218 *Flag = (Val.ConstVal != 0);
228 /* Skip the token itself */
231 /* Identifier must follow */
232 if (CurTok.Tok != TOK_IDENT) {
233 Error ("Identifier expected");
237 /* Search for the name, then skip the identifier */
238 Pragma = FindPragma (CurTok.Ident);
241 /* Do we know this pragma? */
242 if (Pragma == PR_ILLEGAL) {
243 /* According to the ANSI standard, we're not allowed to generate errors
244 * for unknown pragmas, however, we're allowed to warn - and we will
245 * do so. Otherwise one typo may give you hours of bug hunting...
247 Warning ("Unknown #pragma `%s'", CurTok.Ident);
251 /* Check for an open paren */
254 /* Switch for the different pragmas */
258 SegNamePragma (SEG_BSS);
266 FlagPragma (&CheckStack);
270 SegNamePragma (SEG_CODE);
274 SegNamePragma (SEG_DATA);
278 FlagPragma (&AllowRegVarAddr);
282 SegNamePragma (SEG_RODATA);
286 FlagPragma (&SignedChars);
289 case PR_STATICLOCALS:
290 FlagPragma (&StaticLocals);
294 StringPragma (MakeZPSym);
298 Internal ("Invalid pragma");
301 /* Closing paren needed */