1 /*****************************************************************************/
5 /* Pragma handling for the cc65 C compiler */
9 /* (C) 1998-2004 Ullrich von Bassewitz */
11 /* D-70794 Filderstadt */
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 /*****************************************************************************/
50 #include "scanstrbuf.h"
56 /*****************************************************************************/
58 /*****************************************************************************/
62 /* Tokens for the #pragmas */
80 static const struct Pragma {
81 const char* Key; /* Keyword */
82 pragma_t Tok; /* Token */
83 } Pragmas[PR_COUNT] = {
84 { "bssseg", PR_BSSSEG },
85 { "charmap", PR_CHARMAP },
86 { "checkstack", PR_CHECKSTACK },
87 { "codeseg", PR_CODESEG },
88 { "dataseg", PR_DATASEG },
89 { "regvaraddr", PR_REGVARADDR },
90 { "regvars", PR_REGVARS },
91 { "rodataseg", PR_RODATASEG },
92 { "signedchars", PR_SIGNEDCHARS },
93 { "staticlocals", PR_STATICLOCALS },
94 { "zpsym", PR_ZPSYM },
99 /*****************************************************************************/
101 /*****************************************************************************/
105 static void PragmaErrorSkip (void)
106 /* Called in case of an error, skips tokens until the closing paren or a
107 * semicolon is reached.
110 static const token_t TokenList[] = { TOK_RPAREN, TOK_SEMI };
111 SkipTokens (TokenList, sizeof(TokenList) / sizeof(TokenList[0]));
116 static int CmpKey (const void* Key, const void* Elem)
117 /* Compare function for bsearch */
119 return strcmp ((const char*) Key, ((const struct Pragma*) Elem)->Key);
124 static pragma_t FindPragma (const char* Key)
125 /* Find a pragma and return the token. Return PR_ILLEGAL if the keyword is
126 * not a valid pragma.
130 P = bsearch (Key, Pragmas, PR_COUNT, sizeof (Pragmas[0]), CmpKey);
131 return P? P->Tok : PR_ILLEGAL;
136 static void StringPragma (StrBuf* B, void (*Func) (const char*))
137 /* Handle a pragma that expects a string parameter */
141 /* We expect a string here */
142 if (SB_GetString (B, &S)) {
143 /* Call the given function with the string argument */
144 Func (SB_GetConstBuf (&S));
146 Error ("String literal expected");
149 /* Call the string buf destructor */
155 static void SegNamePragma (StrBuf* B, segment_t Seg)
156 /* Handle a pragma that expects a segment name parameter */
160 if (SB_GetString (B, &S)) {
163 const char* Name = SB_GetConstBuf (&S);
165 /* Check if the name is valid */
166 if (ValidSegName (Name)) {
168 /* Set the new name */
169 g_segname (Seg, Name);
173 /* Segment name is invalid */
174 Error ("Illegal segment name: `%s'", Name);
179 Error ("String literal expected");
182 /* Call the string buf destructor */
188 static void CharMapPragma (StrBuf* B)
189 /* Change the character map */
193 /* Read the character index */
194 if (!SB_GetNumber (B, &Index)) {
197 if (Index < 1 || Index > 255) {
198 Error ("Character index out of range");
204 if (SB_Get (B) != ',') {
205 Error ("Comma expected");
210 /* Read the character code */
211 if (!SB_GetNumber (B, &C)) {
214 if (C < 1 || C > 255) {
215 Error ("Character code out of range");
219 /* Remap the character */
220 TgtTranslateSet ((unsigned) Index, (unsigned char) C);
225 static void FlagPragma (StrBuf* B, IntStack* Stack)
226 /* Handle a pragma that expects a boolean paramater */
232 /* Try to read an identifier */
233 int IsIdent = SB_GetSym (B, Ident);
235 /* Check if we have a first argument named "pop" */
236 if (IsIdent && strcmp (Ident, "pop") == 0) {
237 if (IS_GetCount (Stack) < 2) {
238 Error ("Cannot pop, stack is empty");
240 (void) IS_Pop (Stack);
242 /* No other arguments allowed */
246 /* Check if we have a first argument named "push" */
247 if (IsIdent && strcmp (Ident, "push") == 0) {
250 if (SB_Get (B) != ',') {
251 Error ("Comma expected");
255 IsIdent = SB_GetSym (B, Ident);
260 /* Boolean argument follows */
262 if (strcmp (Ident, "true") == 0 || strcmp (Ident, "on") == 0) {
264 } else if (strcmp (Ident, "false") == 0 || strcmp (Ident, "off") == 0) {
267 Error ("Pragma argument must be one of `on', `off', `true' or `false'");
269 } else if (!SB_GetNumber (B, &Val)) {
270 Error ("Invalid pragma argument");
274 /* Set/push the new value */
276 if (IS_IsFull (Stack)) {
277 Error ("Cannot push: stack overflow");
279 IS_Push (Stack, Val);
288 static void ParsePragma (void)
289 /* Parse the contents of the _Pragma statement */
294 /* Create a string buffer from the string literal */
295 StrBuf B = AUTO_STRBUF_INITIALIZER;
296 GetLiteralStrBuf (&B, CurTok.IVal);
298 /* Reset the string pointer, effectivly clearing the string from the
299 * string table. Since we're working with one token lookahead, this
300 * will fail if the next token is also a string token, but that's a
301 * syntax error anyway, because we expect a right paren.
303 ResetLiteralPoolOffs (CurTok.IVal);
305 /* Skip the string token */
308 /* Get the pragma name from the string */
310 if (!SB_GetSym (&B, Ident)) {
311 Error ("Invalid pragma");
315 /* Search for the name */
316 Pragma = FindPragma (Ident);
318 /* Do we know this pragma? */
319 if (Pragma == PR_ILLEGAL) {
320 /* According to the ANSI standard, we're not allowed to generate errors
321 * for unknown pragmas, however, we're allowed to warn - and we will
322 * do so. Otherwise one typo may give you hours of bug hunting...
324 Warning ("Unknown pragma `%s'", Ident);
328 /* Check for an open paren */
330 if (SB_Get (&B) != '(') {
331 Error ("'(' expected");
335 /* Skip white space before the argument */
338 /* Switch for the different pragmas */
342 SegNamePragma (&B, SEG_BSS);
350 FlagPragma (&B, &CheckStack);
354 SegNamePragma (&B, SEG_CODE);
358 SegNamePragma (&B, SEG_DATA);
362 FlagPragma (&B, &AllowRegVarAddr);
366 FlagPragma (&B, &EnableRegVars);
370 SegNamePragma (&B, SEG_RODATA);
374 FlagPragma (&B, &SignedChars);
377 case PR_STATICLOCALS:
378 FlagPragma (&B, &StaticLocals);
382 StringPragma (&B, MakeZPSym);
386 Internal ("Invalid pragma");
389 /* Closing paren expected */
391 if (SB_Get (&B) != ')') {
392 Error ("')' expected");
397 /* Allow an optional semicolon to be compatible with the old syntax */
398 if (SB_Peek (&B) == ';') {
403 /* Make sure nothing follows */
404 if (SB_Peek (&B) != '\0') {
405 Error ("Unexpected input following pragma directive");
408 /* Release the StrBuf */
415 /* Handle pragmas. These come always in form of the new C99 _Pragma() operator. */
417 /* Skip the token itself */
420 /* We expect an opening paren */
421 if (!ConsumeLParen ()) {
426 if (CurTok.Tok != TOK_SCONST) {
428 /* Print a diagnostic */
429 Error ("String literal expected");
431 /* Try some smart error recovery: Skip tokens until we reach the
432 * enclosing paren, or a semicolon.
438 /* Parse the _Pragma statement */
442 /* Closing paren needed */