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 */
162 /* Try to read an identifier */
164 if (SB_GetSym (B, Ident)) {
166 /* Check if we have a first argument named "pop" */
167 if (strcmp (Ident, "pop") == 0) {
169 /* Pop the old value */
172 /* Set the segment name */
178 /* Check if we have a first argument named "push" */
179 } else if (strcmp (Ident, "push") == 0) {
183 if (SB_Get (B) != ',') {
184 Error ("Comma expected");
190 Error ("Invalid pragma arguments");
195 /* A string argument must follow */
196 if (!SB_GetString (B, &S)) {
197 Error ("String literal expected");
202 Name = SB_GetConstBuf (&S);
204 /* Check if the name is valid */
205 if (!ValidSegName (Name)) {
206 /* Segment name is invalid */
207 Error ("Illegal segment name: `%s'", Name);
211 /* Set the new name */
213 PushSegName (Seg, Name);
215 SetSegName (Seg, Name);
219 /* Call the string buf destructor */
225 static void CharMapPragma (StrBuf* B)
226 /* Change the character map */
230 /* Read the character index */
231 if (!SB_GetNumber (B, &Index)) {
234 if (Index < 1 || Index > 255) {
235 Error ("Character index out of range");
241 if (SB_Get (B) != ',') {
242 Error ("Comma expected");
247 /* Read the character code */
248 if (!SB_GetNumber (B, &C)) {
251 if (C < 1 || C > 255) {
252 Error ("Character code out of range");
256 /* Remap the character */
257 TgtTranslateSet ((unsigned) Index, (unsigned char) C);
262 static void FlagPragma (StrBuf* B, IntStack* Stack)
263 /* Handle a pragma that expects a boolean paramater */
269 /* Try to read an identifier */
270 int IsIdent = SB_GetSym (B, Ident);
272 /* Check if we have a first argument named "pop" */
273 if (IsIdent && strcmp (Ident, "pop") == 0) {
274 if (IS_GetCount (Stack) < 2) {
275 Error ("Cannot pop, stack is empty");
279 /* No other arguments allowed */
283 /* Check if we have a first argument named "push" */
284 if (IsIdent && strcmp (Ident, "push") == 0) {
287 if (SB_Get (B) != ',') {
288 Error ("Comma expected");
292 IsIdent = SB_GetSym (B, Ident);
297 /* Boolean argument follows */
299 if (strcmp (Ident, "true") == 0 || strcmp (Ident, "on") == 0) {
301 } else if (strcmp (Ident, "false") == 0 || strcmp (Ident, "off") == 0) {
304 Error ("Pragma argument must be one of `on', `off', `true' or `false'");
306 } else if (!SB_GetNumber (B, &Val)) {
307 Error ("Invalid pragma argument");
311 /* Set/push the new value */
313 if (IS_IsFull (Stack)) {
314 Error ("Cannot push: stack overflow");
316 IS_Push (Stack, Val);
325 static void ParsePragma (void)
326 /* Parse the contents of the _Pragma statement */
331 /* Create a string buffer from the string literal */
332 StrBuf B = AUTO_STRBUF_INITIALIZER;
333 GetLiteralStrBuf (&B, CurTok.IVal);
335 /* Reset the string pointer, effectivly clearing the string from the
336 * string table. Since we're working with one token lookahead, this
337 * will fail if the next token is also a string token, but that's a
338 * syntax error anyway, because we expect a right paren.
340 ResetLiteralPoolOffs (CurTok.IVal);
342 /* Skip the string token */
345 /* Get the pragma name from the string */
347 if (!SB_GetSym (&B, Ident)) {
348 Error ("Invalid pragma");
352 /* Search for the name */
353 Pragma = FindPragma (Ident);
355 /* Do we know this pragma? */
356 if (Pragma == PR_ILLEGAL) {
357 /* According to the ANSI standard, we're not allowed to generate errors
358 * for unknown pragmas, however, we're allowed to warn - and we will
359 * do so. Otherwise one typo may give you hours of bug hunting...
361 Warning ("Unknown pragma `%s'", Ident);
365 /* Check for an open paren */
367 if (SB_Get (&B) != '(') {
368 Error ("'(' expected");
372 /* Skip white space before the argument */
375 /* Switch for the different pragmas */
379 SegNamePragma (&B, SEG_BSS);
387 FlagPragma (&B, &CheckStack);
391 SegNamePragma (&B, SEG_CODE);
395 SegNamePragma (&B, SEG_DATA);
399 FlagPragma (&B, &AllowRegVarAddr);
403 FlagPragma (&B, &EnableRegVars);
407 SegNamePragma (&B, SEG_RODATA);
411 FlagPragma (&B, &SignedChars);
414 case PR_STATICLOCALS:
415 FlagPragma (&B, &StaticLocals);
419 StringPragma (&B, MakeZPSym);
423 Internal ("Invalid pragma");
426 /* Closing paren expected */
428 if (SB_Get (&B) != ')') {
429 Error ("')' expected");
434 /* Allow an optional semicolon to be compatible with the old syntax */
435 if (SB_Peek (&B) == ';') {
440 /* Make sure nothing follows */
441 if (SB_Peek (&B) != '\0') {
442 Error ("Unexpected input following pragma directive");
445 /* Release the StrBuf */
452 /* Handle pragmas. These come always in form of the new C99 _Pragma() operator. */
454 /* Skip the token itself */
457 /* We expect an opening paren */
458 if (!ConsumeLParen ()) {
463 if (CurTok.Tok != TOK_SCONST) {
465 /* Print a diagnostic */
466 Error ("String literal expected");
468 /* Try some smart error recovery: Skip tokens until we reach the
469 * enclosing paren, or a semicolon.
475 /* Parse the _Pragma statement */
479 /* Closing paren needed */