1 /*****************************************************************************/
5 /* Pragma handling for the cc65 C compiler */
9 /* (C) 1998-2010, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
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 /*****************************************************************************/
51 #include "scanstrbuf.h"
57 /*****************************************************************************/
59 /*****************************************************************************/
63 /* Tokens for the #pragmas */
67 PRAGMA_BSSSEG, /* obsolete */
70 PRAGMA_CHECKSTACK, /* obsolete */
72 PRAGMA_CODESEG, /* obsolete */
75 PRAGMA_DATASEG, /* obsolete */
80 PRAGMA_REGVARS, /* obsolete */
82 PRAGMA_RODATASEG, /* obsolete */
84 PRAGMA_SIGNEDCHARS, /* obsolete */
86 PRAGMA_STATICLOCALS, /* obsolete */
88 PRAGMA_WRITABLE_STRINGS,
94 static const struct Pragma {
95 const char* Key; /* Keyword */
96 pragma_t Tok; /* Token */
97 } Pragmas[PRAGMA_COUNT] = {
98 { "bss-name", PRAGMA_BSS_NAME },
99 { "bssseg", PRAGMA_BSSSEG }, /* obsolete */
100 { "charmap", PRAGMA_CHARMAP },
101 { "check-stack", PRAGMA_CHECK_STACK },
102 { "checkstack", PRAGMA_CHECKSTACK }, /* obsolete */
103 { "code-name", PRAGMA_CODE_NAME },
104 { "codeseg", PRAGMA_CODESEG }, /* obsolete */
105 { "codesize", PRAGMA_CODESIZE },
106 { "data-name", PRAGMA_DATA_NAME },
107 { "dataseg", PRAGMA_DATASEG }, /* obsolete */
108 { "local-strings", PRAGMA_LOCAL_STRINGS },
109 { "optimize", PRAGMA_OPTIMIZE },
110 { "register-vars", PRAGMA_REGISTER_VARS },
111 { "regvaraddr", PRAGMA_REGVARADDR },
112 { "regvars", PRAGMA_REGVARS }, /* obsolete */
113 { "rodata-name", PRAGMA_RODATA_NAME },
114 { "rodataseg", PRAGMA_RODATASEG }, /* obsolete */
115 { "signed-chars", PRAGMA_SIGNED_CHARS },
116 { "signedchars", PRAGMA_SIGNEDCHARS }, /* obsolete */
117 { "static-locals", PRAGMA_STATIC_LOCALS },
118 { "staticlocals", PRAGMA_STATICLOCALS }, /* obsolete */
119 { "warn", PRAGMA_WARN },
120 { "writable-strings", PRAGMA_WRITABLE_STRINGS },
121 { "zpsym", PRAGMA_ZPSYM },
124 /* Result of ParsePushPop */
134 /*****************************************************************************/
135 /* Helper functions */
136 /*****************************************************************************/
140 static void PragmaErrorSkip (void)
141 /* Called in case of an error, skips tokens until the closing paren or a
142 * semicolon is reached.
145 static const token_t TokenList[] = { TOK_RPAREN, TOK_SEMI };
146 SkipTokens (TokenList, sizeof(TokenList) / sizeof(TokenList[0]));
151 static int CmpKey (const void* Key, const void* Elem)
152 /* Compare function for bsearch */
154 return strcmp ((const char*) Key, ((const struct Pragma*) Elem)->Key);
159 static pragma_t FindPragma (const StrBuf* Key)
160 /* Find a pragma and return the token. Return PRAGMA_ILLEGAL if the keyword is
161 * not a valid pragma.
165 P = bsearch (SB_GetConstBuf (Key), Pragmas, PRAGMA_COUNT, sizeof (Pragmas[0]), CmpKey);
166 return P? P->Tok : PRAGMA_ILLEGAL;
171 static int GetComma (StrBuf* B)
172 /* Expects and skips a comma in B. Prints an error and returns zero if no
173 * comma is found. Return a value <> 0 otherwise.
177 if (SB_Get (B) != ',') {
178 Error ("Comma expected");
187 static int GetString (StrBuf* B, StrBuf* S)
188 /* Expects and skips a string in B. Prints an error and returns zero if no
189 * string is found. Returns a value <> 0 otherwise.
192 if (!SB_GetString (B, S)) {
193 Error ("String literal expected");
201 static int GetNumber (StrBuf* B, long* Val)
202 /* Expects and skips a number in B. Prints an eror and returns zero if no
203 * number is found. Returns a value <> 0 otherwise.
206 if (!SB_GetNumber (B, Val)) {
207 Error ("Constant integer expected");
215 static IntStack* GetWarning (StrBuf* B)
216 /* Get a warning name from the string buffer. Returns a pointer to the intstack
217 * that holds the state of the warning, and NULL in case of errors. The
218 * function will output error messages in case of problems.
222 StrBuf W = AUTO_STRBUF_INITIALIZER;
224 /* The warning name is a symbol but the '-' char is allowed within */
225 if (SB_GetSym (B, &W, "-")) {
227 /* Map the warning name to an IntStack that contains its state */
228 S = FindWarning (SB_GetConstBuf (&W));
232 Error ("Pragma expects a warning name as first argument");
236 /* Deallocate the string */
245 static int HasStr (StrBuf* B, const char* E)
246 /* Checks if E follows in B. If so, skips it and returns true */
248 unsigned Len = strlen (E);
249 if (SB_GetLen (B) - SB_GetIndex (B) >= Len) {
250 if (strncmp (SB_GetConstBuf (B) + SB_GetIndex (B), E, Len) == 0) {
252 SB_SkipMultiple (B, Len);
261 static PushPopResult ParsePushPop (StrBuf* B)
262 /* Check for and parse the "push" and "pop" keywords. In case of "push", a
263 * following comma is expected and skipped.
266 StrBuf Ident = AUTO_STRBUF_INITIALIZER;
267 PushPopResult Res = PP_NONE;
269 /* Remember the current string index, so we can go back in case of errors */
270 unsigned Index = SB_GetIndex (B);
272 /* Try to read an identifier */
273 if (SB_GetSym (B, &Ident, 0)) {
275 /* Check if we have a first argument named "pop" */
276 if (SB_CompareStr (&Ident, "pop") == 0) {
280 /* Check if we have a first argument named "push" */
281 } else if (SB_CompareStr (&Ident, "push") == 0) {
285 /* Skip the following comma */
287 /* Error already flagged by GetComma */
293 /* Unknown keyword, roll back */
294 SB_SetIndex (B, Index);
298 /* Free the string buffer and return the result */
305 static void PopInt (IntStack* S)
306 /* Pops an integer from an IntStack. Prints an error if the stack is empty */
308 if (IS_GetCount (S) < 2) {
309 Error ("Cannot pop, stack is empty");
317 static void PushInt (IntStack* S, long Val)
318 /* Pushes an integer onto an IntStack. Prints an error if the stack is full */
321 Error ("Cannot push: stack overflow");
329 static int BoolKeyword (StrBuf* Ident)
330 /* Check if the identifier in Ident is a keyword for a boolean value. Currently
331 * accepted are true/false/on/off.
334 if (SB_CompareStr (Ident, "true") == 0) {
337 if (SB_CompareStr (Ident, "on") == 0) {
340 if (SB_CompareStr (Ident, "false") == 0) {
343 if (SB_CompareStr (Ident, "off") == 0) {
348 Error ("Pragma argument must be one of `on', `off', `true' or `false'");
354 /*****************************************************************************/
355 /* Pragma handling functions */
356 /*****************************************************************************/
360 static void StringPragma (StrBuf* B, void (*Func) (const char*))
361 /* Handle a pragma that expects a string parameter */
363 StrBuf S = AUTO_STRBUF_INITIALIZER;
365 /* We expect a string here */
366 if (GetString (B, &S)) {
367 /* Call the given function with the string argument */
368 Func (SB_GetConstBuf (&S));
371 /* Call the string buf destructor */
377 static void SegNamePragma (StrBuf* B, segment_t Seg)
378 /* Handle a pragma that expects a segment name parameter */
380 StrBuf S = AUTO_STRBUF_INITIALIZER;
383 /* Check for the "push" or "pop" keywords */
385 switch (ParsePushPop (B)) {
395 /* Pop the old value and output it */
407 Internal ("Invalid result from ParsePushPop");
411 /* A string argument must follow */
412 if (!GetString (B, &S)) {
417 Name = SB_GetConstBuf (&S);
419 /* Check if the name is valid */
420 if (ValidSegName (Name)) {
422 /* Set the new name */
424 PushSegName (Seg, Name);
426 SetSegName (Seg, Name);
432 /* Segment name is invalid */
433 Error ("Illegal segment name: `%s'", Name);
438 /* Call the string buf destructor */
444 static void CharMapPragma (StrBuf* B)
445 /* Change the character map */
449 /* Read the character index */
450 if (!GetNumber (B, &Index)) {
453 if (Index < 1 || Index > 255) {
456 Error ("Remapping 0 is not allowed");
458 Error ("Character index out of range");
468 /* Read the character code */
469 if (!GetNumber (B, &C)) {
472 if (C < 1 || C > 255) {
475 Error ("Remapping 0 is not allowed");
477 Error ("Character code out of range");
482 /* Remap the character */
483 TgtTranslateSet ((unsigned) Index, (unsigned char) C);
488 static void WarnPragma (StrBuf* B)
489 /* Enable/disable warnings */
494 /* A warning name must follow */
495 IntStack* S = GetWarning (B);
505 /* Check for the "push" or "pop" keywords */
506 switch (ParsePushPop (B)) {
517 /* Pop the old value and bail out */
526 Internal ("Invalid result from ParsePushPop");
529 /* Boolean argument follows */
530 if (HasStr (B, "true") || HasStr (B, "on")) {
532 } else if (HasStr (B, "false") || HasStr (B, "off")) {
534 } else if (!SB_GetNumber (B, &Val)) {
535 Error ("Invalid pragma argument");
539 /* Set/push the new value */
549 static void FlagPragma (StrBuf* B, IntStack* Stack)
550 /* Handle a pragma that expects a boolean paramater */
552 StrBuf Ident = AUTO_STRBUF_INITIALIZER;
557 /* Try to read an identifier */
558 int IsIdent = SB_GetSym (B, &Ident, 0);
560 /* Check if we have a first argument named "pop" */
561 if (IsIdent && SB_CompareStr (&Ident, "pop") == 0) {
563 /* No other arguments allowed */
567 /* Check if we have a first argument named "push" */
568 if (IsIdent && SB_CompareStr (&Ident, "push") == 0) {
573 IsIdent = SB_GetSym (B, &Ident, 0);
578 /* Boolean argument follows */
580 Val = BoolKeyword (&Ident);
581 } else if (!GetNumber (B, &Val)) {
585 /* Set/push the new value */
587 PushInt (Stack, Val);
593 /* Free the identifier */
599 static void IntPragma (StrBuf* B, IntStack* Stack, long Low, long High)
600 /* Handle a pragma that expects an int paramater */
605 /* Check for the "push" or "pop" keywords */
606 switch (ParsePushPop (B)) {
617 /* Pop the old value and bail out */
626 Internal ("Invalid result from ParsePushPop");
630 /* Integer argument follows */
631 if (!GetNumber (B, &Val)) {
635 /* Check the argument */
636 if (Val < Low || Val > High) {
637 Error ("Pragma argument out of bounds (%ld-%ld)", Low, High);
641 /* Set/push the new value */
643 PushInt (Stack, Val);
651 static void ParsePragma (void)
652 /* Parse the contents of the _Pragma statement */
655 StrBuf Ident = AUTO_STRBUF_INITIALIZER;
657 /* Create a string buffer from the string literal */
658 StrBuf B = AUTO_STRBUF_INITIALIZER;
659 SB_Append (&B, GetLiteralStrBuf (CurTok.SVal));
661 /* Skip the string token */
664 /* Get the pragma name from the string */
666 if (!SB_GetSym (&B, &Ident, "-")) {
667 Error ("Invalid pragma");
671 /* Search for the name */
672 Pragma = FindPragma (&Ident);
674 /* Do we know this pragma? */
675 if (Pragma == PRAGMA_ILLEGAL) {
676 /* According to the ANSI standard, we're not allowed to generate errors
677 * for unknown pragmas, but warn about them if enabled (the default).
679 if (IS_Get (&WarnUnknownPragma)) {
680 Warning ("Unknown pragma `%s'", SB_GetConstBuf (&Ident));
685 /* Check for an open paren */
687 if (SB_Get (&B) != '(') {
688 Error ("'(' expected");
692 /* Skip white space before the argument */
695 /* Switch for the different pragmas */
699 Warning ("#pragma bssseg is obsolete, please use #pragma bss-name instead");
701 case PRAGMA_BSS_NAME:
702 SegNamePragma (&B, SEG_BSS);
709 case PRAGMA_CHECKSTACK:
710 Warning ("#pragma checkstack is obsolete, please use #pragma check-stack instead");
712 case PRAGMA_CHECK_STACK:
713 FlagPragma (&B, &CheckStack);
717 Warning ("#pragma codeseg is obsolete, please use #pragma code-name instead");
719 case PRAGMA_CODE_NAME:
720 SegNamePragma (&B, SEG_CODE);
723 case PRAGMA_CODESIZE:
724 IntPragma (&B, &CodeSizeFactor, 10, 1000);
728 Warning ("#pragma dataseg is obsolete, please use #pragma data-name instead");
730 case PRAGMA_DATA_NAME:
731 SegNamePragma (&B, SEG_DATA);
734 case PRAGMA_LOCAL_STRINGS:
735 FlagPragma (&B, &LocalStrings);
738 case PRAGMA_OPTIMIZE:
739 FlagPragma (&B, &Optimize);
742 case PRAGMA_REGVARADDR:
743 FlagPragma (&B, &AllowRegVarAddr);
747 Warning ("#pragma regvars is obsolete, please use #pragma register-vars instead");
749 case PRAGMA_REGISTER_VARS:
750 FlagPragma (&B, &EnableRegVars);
753 case PRAGMA_RODATASEG:
754 Warning ("#pragma rodataseg is obsolete, please use #pragma rodata-name instead");
756 case PRAGMA_RODATA_NAME:
757 SegNamePragma (&B, SEG_RODATA);
760 case PRAGMA_SIGNEDCHARS:
761 Warning ("#pragma signedchars is obsolete, please use #pragma signed-chars instead");
763 case PRAGMA_SIGNED_CHARS:
764 FlagPragma (&B, &SignedChars);
767 case PRAGMA_STATICLOCALS:
768 Warning ("#pragma staticlocals is obsolete, please use #pragma static-locals instead");
770 case PRAGMA_STATIC_LOCALS:
771 FlagPragma (&B, &StaticLocals);
778 case PRAGMA_WRITABLE_STRINGS:
779 FlagPragma (&B, &WritableStrings);
783 StringPragma (&B, MakeZPSym);
787 Internal ("Invalid pragma");
790 /* Closing paren expected */
792 if (SB_Get (&B) != ')') {
793 Error ("')' expected");
798 /* Allow an optional semicolon to be compatible with the old syntax */
799 if (SB_Peek (&B) == ';') {
804 /* Make sure nothing follows */
805 if (SB_Peek (&B) != '\0') {
806 Error ("Unexpected input following pragma directive");
810 /* Release the string buffers */
818 /* Handle pragmas. These come always in form of the new C99 _Pragma() operator. */
820 /* Skip the token itself */
823 /* We expect an opening paren */
824 if (!ConsumeLParen ()) {
829 if (CurTok.Tok != TOK_SCONST) {
831 /* Print a diagnostic */
832 Error ("String literal expected");
834 /* Try some smart error recovery: Skip tokens until we reach the
835 * enclosing paren, or a semicolon.
841 /* Parse the _Pragma statement */
845 /* Closing paren needed */