1 /*****************************************************************************/
5 /* Pragma handling for the cc65 C compiler */
9 /* (C) 1998-2008 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 /*****************************************************************************/
50 #include "scanstrbuf.h"
56 /*****************************************************************************/
58 /*****************************************************************************/
62 /* Tokens for the #pragmas */
83 static const struct Pragma {
84 const char* Key; /* Keyword */
85 pragma_t Tok; /* Token */
86 } Pragmas[PR_COUNT] = {
87 { "bssseg", PR_BSSSEG },
88 { "charmap", PR_CHARMAP },
89 { "checkstack", PR_CHECKSTACK },
90 { "codeseg", PR_CODESEG },
91 { "codesize", PR_CODESIZE },
92 { "dataseg", PR_DATASEG },
93 { "optimize", PR_OPTIMIZE },
94 { "regvaraddr", PR_REGVARADDR },
95 { "regvars", PR_REGVARS },
96 { "rodataseg", PR_RODATASEG },
97 { "signedchars", PR_SIGNEDCHARS },
98 { "staticlocals", PR_STATICLOCALS },
100 { "zpsym", PR_ZPSYM },
105 /*****************************************************************************/
107 /*****************************************************************************/
111 static void PragmaErrorSkip (void)
112 /* Called in case of an error, skips tokens until the closing paren or a
113 * semicolon is reached.
116 static const token_t TokenList[] = { TOK_RPAREN, TOK_SEMI };
117 SkipTokens (TokenList, sizeof(TokenList) / sizeof(TokenList[0]));
122 static int CmpKey (const void* Key, const void* Elem)
123 /* Compare function for bsearch */
125 return strcmp ((const char*) Key, ((const struct Pragma*) Elem)->Key);
130 static pragma_t FindPragma (const char* Key)
131 /* Find a pragma and return the token. Return PR_ILLEGAL if the keyword is
132 * not a valid pragma.
136 P = bsearch (Key, Pragmas, PR_COUNT, sizeof (Pragmas[0]), CmpKey);
137 return P? P->Tok : PR_ILLEGAL;
142 static void StringPragma (StrBuf* B, void (*Func) (const char*))
143 /* Handle a pragma that expects a string parameter */
147 /* We expect a string here */
148 if (SB_GetString (B, &S)) {
149 /* Call the given function with the string argument */
150 Func (SB_GetConstBuf (&S));
152 Error ("String literal expected");
155 /* Call the string buf destructor */
161 static void SegNamePragma (StrBuf* B, segment_t Seg)
162 /* Handle a pragma that expects a segment name parameter */
168 /* Try to read an identifier */
170 if (SB_GetSym (B, Ident)) {
172 /* Check if we have a first argument named "pop" */
173 if (strcmp (Ident, "pop") == 0) {
175 /* Pop the old value */
178 /* Set the segment name */
184 /* Check if we have a first argument named "push" */
185 } else if (strcmp (Ident, "push") == 0) {
189 if (SB_Get (B) != ',') {
190 Error ("Comma expected");
196 Error ("Invalid pragma arguments");
201 /* A string argument must follow */
202 if (!SB_GetString (B, &S)) {
203 Error ("String literal expected");
208 Name = SB_GetConstBuf (&S);
210 /* Check if the name is valid */
211 if (!ValidSegName (Name)) {
212 /* Segment name is invalid */
213 Error ("Illegal segment name: `%s'", Name);
217 /* Set the new name */
219 PushSegName (Seg, Name);
221 SetSegName (Seg, Name);
225 /* Call the string buf destructor */
231 static void CharMapPragma (StrBuf* B)
232 /* Change the character map */
236 /* Read the character index */
237 if (!SB_GetNumber (B, &Index)) {
240 if (Index < 1 || Index > 255) {
243 Error ("Remapping 0 is not allowed");
245 Error ("Character index out of range");
252 if (SB_Get (B) != ',') {
253 Error ("Comma expected");
258 /* Read the character code */
259 if (!SB_GetNumber (B, &C)) {
262 if (C < 1 || C > 255) {
265 Error ("Remapping 0 is not allowed");
267 Error ("Character code out of range");
272 /* Remap the character */
273 TgtTranslateSet ((unsigned) Index, (unsigned char) C);
278 static void FlagPragma (StrBuf* B, IntStack* Stack)
279 /* Handle a pragma that expects a boolean paramater */
285 /* Try to read an identifier */
286 int IsIdent = SB_GetSym (B, Ident);
288 /* Check if we have a first argument named "pop" */
289 if (IsIdent && strcmp (Ident, "pop") == 0) {
290 if (IS_GetCount (Stack) < 2) {
291 Error ("Cannot pop, stack is empty");
295 /* No other arguments allowed */
299 /* Check if we have a first argument named "push" */
300 if (IsIdent && strcmp (Ident, "push") == 0) {
303 if (SB_Get (B) != ',') {
304 Error ("Comma expected");
308 IsIdent = SB_GetSym (B, Ident);
313 /* Boolean argument follows */
315 if (strcmp (Ident, "true") == 0 || strcmp (Ident, "on") == 0) {
317 } else if (strcmp (Ident, "false") == 0 || strcmp (Ident, "off") == 0) {
320 Error ("Pragma argument must be one of `on', `off', `true' or `false'");
322 } else if (!SB_GetNumber (B, &Val)) {
323 Error ("Invalid pragma argument");
327 /* Set/push the new value */
329 if (IS_IsFull (Stack)) {
330 Error ("Cannot push: stack overflow");
332 IS_Push (Stack, Val);
341 static void IntPragma (StrBuf* B, IntStack* Stack, long Low, long High)
342 /* Handle a pragma that expects an int paramater */
348 /* Try to read an identifier */
349 int IsIdent = SB_GetSym (B, Ident);
351 /* Check if we have a first argument named "pop" */
352 if (IsIdent && strcmp (Ident, "pop") == 0) {
353 if (IS_GetCount (Stack) < 2) {
354 Error ("Cannot pop, stack is empty");
358 /* No other arguments allowed */
362 /* Check if we have a first argument named "push" */
363 if (IsIdent && strcmp (Ident, "push") == 0) {
366 if (SB_Get (B) != ',') {
367 Error ("Comma expected");
376 /* Integer argument follows */
377 if (IsIdent || !SB_GetNumber (B, &Val)) {
378 Error ("Pragma argument must be numeric");
382 /* Check the argument */
383 if (Val < Low || Val > High) {
384 Error ("Pragma argument out of bounds (%ld-%ld)", Low, High);
388 /* Set/push the new value */
390 if (IS_IsFull (Stack)) {
391 Error ("Cannot push: stack overflow");
393 IS_Push (Stack, Val);
402 static void ParsePragma (void)
403 /* Parse the contents of the _Pragma statement */
408 /* Create a string buffer from the string literal */
409 StrBuf B = AUTO_STRBUF_INITIALIZER;
410 GetLiteralStrBuf (&B, CurTok.IVal);
412 /* Reset the string pointer, effectivly clearing the string from the
413 * string table. Since we're working with one token lookahead, this
414 * will fail if the next token is also a string token, but that's a
415 * syntax error anyway, because we expect a right paren.
417 ResetLiteralPoolOffs (CurTok.IVal);
419 /* Skip the string token */
422 /* Get the pragma name from the string */
424 if (!SB_GetSym (&B, Ident)) {
425 Error ("Invalid pragma");
429 /* Search for the name */
430 Pragma = FindPragma (Ident);
432 /* Do we know this pragma? */
433 if (Pragma == PR_ILLEGAL) {
434 /* According to the ANSI standard, we're not allowed to generate errors
435 * for unknown pragmas, however, we're allowed to warn - and we will
436 * do so. Otherwise one typo may give you hours of bug hunting...
438 Warning ("Unknown pragma `%s'", Ident);
442 /* Check for an open paren */
444 if (SB_Get (&B) != '(') {
445 Error ("'(' expected");
449 /* Skip white space before the argument */
452 /* Switch for the different pragmas */
456 SegNamePragma (&B, SEG_BSS);
464 FlagPragma (&B, &CheckStack);
468 SegNamePragma (&B, SEG_CODE);
472 IntPragma (&B, &CodeSizeFactor, 10, 1000);
476 SegNamePragma (&B, SEG_DATA);
480 FlagPragma (&B, &Optimize);
484 FlagPragma (&B, &AllowRegVarAddr);
488 FlagPragma (&B, &EnableRegVars);
492 SegNamePragma (&B, SEG_RODATA);
496 FlagPragma (&B, &SignedChars);
499 case PR_STATICLOCALS:
500 FlagPragma (&B, &StaticLocals);
504 FlagPragma (&B, &WarnDisable);
508 StringPragma (&B, MakeZPSym);
512 Internal ("Invalid pragma");
515 /* Closing paren expected */
517 if (SB_Get (&B) != ')') {
518 Error ("')' expected");
523 /* Allow an optional semicolon to be compatible with the old syntax */
524 if (SB_Peek (&B) == ';') {
529 /* Make sure nothing follows */
530 if (SB_Peek (&B) != '\0') {
531 Error ("Unexpected input following pragma directive");
534 /* Release the StrBuf */
541 /* Handle pragmas. These come always in form of the new C99 _Pragma() operator. */
543 /* Skip the token itself */
546 /* We expect an opening paren */
547 if (!ConsumeLParen ()) {
552 if (CurTok.Tok != TOK_SCONST) {
554 /* Print a diagnostic */
555 Error ("String literal expected");
557 /* Try some smart error recovery: Skip tokens until we reach the
558 * enclosing paren, or a semicolon.
564 /* Parse the _Pragma statement */
568 /* Closing paren needed */