1 /*****************************************************************************/
5 /* Pragma handling for the cc65 C compiler */
9 /* (C) 1998-2002 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
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 /*****************************************************************************/
49 #include "scanstrbuf.h"
56 /*****************************************************************************/
58 /*****************************************************************************/
62 /* Tokens for the #pragmas */
79 static const struct Pragma {
80 const char* Key; /* Keyword */
81 pragma_t Tok; /* Token */
82 } Pragmas[PR_COUNT] = {
83 { "bssseg", PR_BSSSEG },
84 { "charmap", PR_CHARMAP },
85 { "checkstack", PR_CHECKSTACK },
86 { "codeseg", PR_CODESEG },
87 { "dataseg", PR_DATASEG },
88 { "regvaraddr", PR_REGVARADDR },
89 { "rodataseg", PR_RODATASEG },
90 { "signedchars", PR_SIGNEDCHARS },
91 { "staticlocals", PR_STATICLOCALS },
92 { "zpsym", PR_ZPSYM },
97 /*****************************************************************************/
99 /*****************************************************************************/
103 static void PragmaErrorSkip (void)
104 /* Called in case of an error, skips tokens until the closing paren or a
105 * semicolon is reached.
108 static const token_t TokenList[] = { TOK_RPAREN, TOK_SEMI };
109 SkipTokens (TokenList, sizeof(TokenList) / sizeof(TokenList[0]));
114 static int CmpKey (const void* Key, const void* Elem)
115 /* Compare function for bsearch */
117 return strcmp ((const char*) Key, ((const struct Pragma*) Elem)->Key);
122 static pragma_t FindPragma (const char* Key)
123 /* Find a pragma and return the token. Return PR_ILLEGAL if the keyword is
124 * not a valid pragma.
128 P = bsearch (Key, Pragmas, PR_COUNT, sizeof (Pragmas[0]), CmpKey);
129 return P? P->Tok : PR_ILLEGAL;
134 static void StringPragma (StrBuf* B, void (*Func) (const char*))
135 /* Handle a pragma that expects a string parameter */
139 if (SB_GetString (B, &S)) {
140 /* Call the given function with the string argument */
141 Func (SB_GetConstBuf (&S));
143 Error ("String literal expected");
149 static void SegNamePragma (StrBuf* B, segment_t Seg)
150 /* Handle a pragma that expects a segment name parameter */
154 if (SB_GetString (B, &S)) {
157 const char* Name = SB_GetConstBuf (&S);
159 /* Check if the name is valid */
160 if (ValidSegName (Name)) {
162 /* Set the new name */
163 g_segname (Seg, Name);
167 /* Segment name is invalid */
168 Error ("Illegal segment name: `%s'", Name);
173 Error ("String literal expected");
179 static void CharMapPragma (StrBuf* B)
180 /* Change the character map */
186 /* Read the character index */
188 if (Val.ConstVal < 1 || Val.ConstVal > 255) {
189 Error ("Character index out of range");
192 Index = Val.ConstVal;
198 /* Read the character code */
200 if (Val.ConstVal < 1 || Val.ConstVal > 255) {
201 Error ("Character code out of range");
207 /* Remap the character */
208 TgtTranslateSet (Index, C);
213 static void FlagPragma (StrBuf* B, unsigned char* Flag)
214 /* Handle a pragma that expects a boolean paramater */
218 if (SB_Peek (B) == '0') {
221 } else if (SB_Peek (B) == '1') {
224 } else if (SB_GetSym (B, Ident)) {
225 if (strcmp (Ident, "true") == 0 || strcmp (Ident, "on") == 0) {
227 } else if (strcmp (Ident, "false") == 0 || strcmp (Ident, "off") == 0) {
230 Error ("Pragma argument must be one of `on', `off', `true' or `false'");
233 Error ("Invalid pragma argument");
239 static void ParsePragma (void)
240 /* Parse the contents of the _Pragma statement */
245 /* Create a string buffer from the string literal */
246 StrBuf B = AUTO_STRBUF_INITIALIZER;
247 GetLiteralStrBuf (&B, CurTok.IVal);
249 /* Reset the string pointer, effectivly clearing the string from the
250 * string table. Since we're working with one token lookahead, this
251 * will fail if the next token is also a string token, but that's a
252 * syntax error anyway, because we expect a right paren.
254 ResetLiteralPoolOffs (CurTok.IVal);
256 /* Skip the string token */
259 /* Get the pragma name from the string */
261 if (!SB_GetSym (&B, Ident)) {
262 Error ("Invalid pragma");
266 /* Search for the name */
267 Pragma = FindPragma (Ident);
269 /* Do we know this pragma? */
270 if (Pragma == PR_ILLEGAL) {
271 /* According to the ANSI standard, we're not allowed to generate errors
272 * for unknown pragmas, however, we're allowed to warn - and we will
273 * do so. Otherwise one typo may give you hours of bug hunting...
275 Warning ("Unknown pragma `%s'", Ident);
279 /* Check for an open paren */
281 if (SB_Get (&B) != '(') {
282 Error ("'(' expected");
286 /* Skip white space before the argument */
289 /* Switch for the different pragmas */
293 SegNamePragma (&B, SEG_BSS);
301 FlagPragma (&B, &CheckStack);
305 SegNamePragma (&B, SEG_CODE);
309 SegNamePragma (&B, SEG_DATA);
313 FlagPragma (&B, &AllowRegVarAddr);
317 SegNamePragma (&B, SEG_RODATA);
321 FlagPragma (&B, &SignedChars);
324 case PR_STATICLOCALS:
325 FlagPragma (&B, &StaticLocals);
329 StringPragma (&B, MakeZPSym);
333 Internal ("Invalid pragma");
336 /* Closing paren expected */
338 if (SB_Get (&B) != ')') {
339 Error ("')' expected");
343 /* Make sure nothing follows */
345 if (SB_Peek (&B) != '\0') {
346 Error ("Unexpected input following pragma directive");
353 /* Handle pragmas. These come always in form of the new C99 _Pragma() operator. */
355 /* Skip the token itself */
358 /* We expect an opening paren */
359 if (!ConsumeLParen ()) {
364 if (CurTok.Tok != TOK_SCONST) {
366 /* Print a diagnostic */
367 Error ("String literal expected");
369 /* Try some smart error recovery: Skip tokens until we reach the
370 * enclosing paren, or a semicolon.
376 /* Parse the _Pragma statement */
380 /* Closing paren needed */