1 /*****************************************************************************/
5 /* Pragma handling for the cc65 C compiler */
9 /* (C) 1998-2009, 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;
270 /* Try to read an identifier */
271 if (SB_GetSym (B, &Ident, 0)) {
273 /* Check if we have a first argument named "pop" */
274 if (SB_CompareStr (&Ident, "pop") == 0) {
278 /* Check if we have a first argument named "push" */
279 } else if (SB_CompareStr (&Ident, "push") == 0) {
283 /* Skip the following comma */
290 /* Unknown keyword */
291 Error ("Invalid pragma arguments");
296 /* Free the string buffer and return the result */
303 static void PopInt (IntStack* S)
304 /* Pops an integer from an IntStack. Prints an error if the stack is empty */
306 if (IS_GetCount (S) < 2) {
307 Error ("Cannot pop, stack is empty");
315 static void PushInt (IntStack* S, long Val)
316 /* Pushes an integer onto an IntStack. Prints an error if the stack is full */
319 Error ("Cannot push: stack overflow");
327 static int BoolKeyword (StrBuf* Ident)
328 /* Check if the identifier in Ident is a keyword for a boolean value. Currently
329 * accepted are true/false/on/off.
332 if (SB_CompareStr (Ident, "true") == 0) {
335 if (SB_CompareStr (Ident, "on") == 0) {
338 if (SB_CompareStr (Ident, "false") == 0) {
341 if (SB_CompareStr (Ident, "off") == 0) {
346 Error ("Pragma argument must be one of `on', `off', `true' or `false'");
352 /*****************************************************************************/
353 /* Pragma handling functions */
354 /*****************************************************************************/
358 static void StringPragma (StrBuf* B, void (*Func) (const char*))
359 /* Handle a pragma that expects a string parameter */
361 StrBuf S = AUTO_STRBUF_INITIALIZER;
363 /* We expect a string here */
364 if (GetString (B, &S)) {
365 /* Call the given function with the string argument */
366 Func (SB_GetConstBuf (&S));
369 /* Call the string buf destructor */
375 static void SegNamePragma (StrBuf* B, segment_t Seg)
376 /* Handle a pragma that expects a segment name parameter */
378 StrBuf S = AUTO_STRBUF_INITIALIZER;
381 /* Check for the "push" or "pop" keywords */
383 switch (ParsePushPop (B)) {
393 /* Pop the old value and output it */
405 Internal ("Invalid result from ParsePushPop");
409 /* A string argument must follow */
410 if (!GetString (B, &S)) {
415 Name = SB_GetConstBuf (&S);
417 /* Check if the name is valid */
418 if (ValidSegName (Name)) {
420 /* Set the new name */
422 PushSegName (Seg, Name);
424 SetSegName (Seg, Name);
430 /* Segment name is invalid */
431 Error ("Illegal segment name: `%s'", Name);
436 /* Call the string buf destructor */
442 static void CharMapPragma (StrBuf* B)
443 /* Change the character map */
447 /* Read the character index */
448 if (!GetNumber (B, &Index)) {
451 if (Index < 1 || Index > 255) {
454 Error ("Remapping 0 is not allowed");
456 Error ("Character index out of range");
466 /* Read the character code */
467 if (!GetNumber (B, &C)) {
470 if (C < 1 || C > 255) {
473 Error ("Remapping 0 is not allowed");
475 Error ("Character code out of range");
480 /* Remap the character */
481 TgtTranslateSet ((unsigned) Index, (unsigned char) C);
486 static void WarnPragma (StrBuf* B)
487 /* Enable/disable warnings */
492 /* A warning name must follow */
493 IntStack* S =GetWarning (B);
503 /* Check for the "push" or "pop" keywords */
504 switch (ParsePushPop (B)) {
515 /* Pop the old value and bail out */
524 Internal ("Invalid result from ParsePushPop");
527 /* Boolean argument follows */
528 if (HasStr (B, "true") || HasStr (B, "on")) {
530 } else if (HasStr (B, "false") || HasStr (B, "off")) {
532 } else if (!SB_GetNumber (B, &Val)) {
533 Error ("Invalid pragma argument");
537 /* Set/push the new value */
547 static void FlagPragma (StrBuf* B, IntStack* Stack)
548 /* Handle a pragma that expects a boolean paramater */
550 StrBuf Ident = AUTO_STRBUF_INITIALIZER;
555 /* Try to read an identifier */
556 int IsIdent = SB_GetSym (B, &Ident, 0);
558 /* Check if we have a first argument named "pop" */
559 if (IsIdent && SB_CompareStr (&Ident, "pop") == 0) {
561 /* No other arguments allowed */
565 /* Check if we have a first argument named "push" */
566 if (IsIdent && SB_CompareStr (&Ident, "push") == 0) {
571 IsIdent = SB_GetSym (B, &Ident, 0);
576 /* Boolean argument follows */
578 Val = BoolKeyword (&Ident);
579 } else if (!GetNumber (B, &Val)) {
583 /* Set/push the new value */
585 PushInt (Stack, Val);
591 /* Free the identifier */
597 static void IntPragma (StrBuf* B, IntStack* Stack, long Low, long High)
598 /* Handle a pragma that expects an int paramater */
603 /* Check for the "push" or "pop" keywords */
604 switch (ParsePushPop (B)) {
615 /* Pop the old value and bail out */
624 Internal ("Invalid result from ParsePushPop");
628 /* Integer argument follows */
629 if (!GetNumber (B, &Val)) {
633 /* Check the argument */
634 if (Val < Low || Val > High) {
635 Error ("Pragma argument out of bounds (%ld-%ld)", Low, High);
639 /* Set/push the new value */
641 PushInt (Stack, Val);
649 static void ParsePragma (void)
650 /* Parse the contents of the _Pragma statement */
653 StrBuf Ident = AUTO_STRBUF_INITIALIZER;
655 /* Create a string buffer from the string literal */
656 StrBuf B = AUTO_STRBUF_INITIALIZER;
657 SB_Append (&B, GetLiteralStrBuf (CurTok.SVal));
659 /* Skip the string token */
662 /* Get the pragma name from the string */
664 if (!SB_GetSym (&B, &Ident, "-")) {
665 Error ("Invalid pragma");
669 /* Search for the name */
670 Pragma = FindPragma (&Ident);
672 /* Do we know this pragma? */
673 if (Pragma == PRAGMA_ILLEGAL) {
674 /* According to the ANSI standard, we're not allowed to generate errors
675 * for unknown pragmas, but warn about them if enabled (the default).
677 if (IS_Get (&WarnUnknownPragma)) {
678 Warning ("Unknown pragma `%s'", SB_GetConstBuf (&Ident));
683 /* Check for an open paren */
685 if (SB_Get (&B) != '(') {
686 Error ("'(' expected");
690 /* Skip white space before the argument */
693 /* Switch for the different pragmas */
697 Warning ("#pragma bssseg is obsolete, please use #pragma bss-name instead");
699 case PRAGMA_BSS_NAME:
700 SegNamePragma (&B, SEG_BSS);
707 case PRAGMA_CHECKSTACK:
708 Warning ("#pragma checkstack is obsolete, please use #pragma check-stack instead");
710 case PRAGMA_CHECK_STACK:
711 FlagPragma (&B, &CheckStack);
715 Warning ("#pragma codeseg is obsolete, please use #pragma code-name instead");
717 case PRAGMA_CODE_NAME:
718 SegNamePragma (&B, SEG_CODE);
721 case PRAGMA_CODESIZE:
722 IntPragma (&B, &CodeSizeFactor, 10, 1000);
726 Warning ("#pragma dataseg is obsolete, please use #pragma data-name instead");
728 case PRAGMA_DATA_NAME:
729 SegNamePragma (&B, SEG_DATA);
732 case PRAGMA_LOCAL_STRINGS:
733 FlagPragma (&B, &LocalStrings);
736 case PRAGMA_OPTIMIZE:
737 FlagPragma (&B, &Optimize);
740 case PRAGMA_REGVARADDR:
741 FlagPragma (&B, &AllowRegVarAddr);
745 Warning ("#pragma regvars is obsolete, please use #pragma register-vars instead");
747 case PRAGMA_REGISTER_VARS:
748 FlagPragma (&B, &EnableRegVars);
751 case PRAGMA_RODATASEG:
752 Warning ("#pragma rodataseg is obsolete, please use #pragma rodata-name instead");
754 case PRAGMA_RODATA_NAME:
755 SegNamePragma (&B, SEG_RODATA);
758 case PRAGMA_SIGNEDCHARS:
759 Warning ("#pragma signedchars is obsolete, please use #pragma signed-chars instead");
761 case PRAGMA_SIGNED_CHARS:
762 FlagPragma (&B, &SignedChars);
765 case PRAGMA_STATICLOCALS:
766 Warning ("#pragma staticlocals is obsolete, please use #pragma static-locals instead");
768 case PRAGMA_STATIC_LOCALS:
769 FlagPragma (&B, &StaticLocals);
776 case PRAGMA_WRITABLE_STRINGS:
777 FlagPragma (&B, &WritableStrings);
781 StringPragma (&B, MakeZPSym);
785 Internal ("Invalid pragma");
788 /* Closing paren expected */
790 if (SB_Get (&B) != ')') {
791 Error ("')' expected");
796 /* Allow an optional semicolon to be compatible with the old syntax */
797 if (SB_Peek (&B) == ';') {
802 /* Make sure nothing follows */
803 if (SB_Peek (&B) != '\0') {
804 Error ("Unexpected input following pragma directive");
808 /* Release the string buffers */
816 /* Handle pragmas. These come always in form of the new C99 _Pragma() operator. */
818 /* Skip the token itself */
821 /* We expect an opening paren */
822 if (!ConsumeLParen ()) {
827 if (CurTok.Tok != TOK_SCONST) {
829 /* Print a diagnostic */
830 Error ("String literal expected");
832 /* Try some smart error recovery: Skip tokens until we reach the
833 * enclosing paren, or a semicolon.
839 /* Parse the _Pragma statement */
843 /* Closing paren needed */