]> git.sur5r.net Git - cc65/blob - src/cc65/pragma.c
f4c38491b003504e10ea1c7d3c3f3f25afc215b9
[cc65] / src / cc65 / pragma.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 pragma.c                                  */
4 /*                                                                           */
5 /*                  Pragma handling for the cc65 C compiler                  */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2002 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdlib.h>
37 #include <string.h>
38
39 /* common */
40 #include "tgttrans.h"
41
42 /* cc65 */
43 #include "codegen.h"
44 #include "error.h"
45 #include "expr.h"
46 #include "global.h"
47 #include "litpool.h"
48 #include "scanner.h"
49 #include "scanstrbuf.h"
50 #include "segments.h"
51 #include "symtab.h"
52 #include "pragma.h"
53
54
55
56 /*****************************************************************************/
57 /*                                   data                                    */
58 /*****************************************************************************/
59
60
61
62 /* Tokens for the #pragmas */
63 typedef enum {
64     PR_ILLEGAL = -1,
65     PR_BSSSEG,
66     PR_CHARMAP,
67     PR_CHECKSTACK,
68     PR_CODESEG,
69     PR_DATASEG,
70     PR_REGVARADDR,
71     PR_RODATASEG,
72     PR_SIGNEDCHARS,
73     PR_STATICLOCALS,
74     PR_ZPSYM,
75     PR_COUNT
76 } pragma_t;
77
78 /* Pragma table */
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        },
93 };
94
95
96
97 /*****************************************************************************/
98 /*                                   Code                                    */
99 /*****************************************************************************/
100
101
102
103 static void PragmaErrorSkip (void)
104 /* Called in case of an error, skips tokens until the closing paren or a
105  * semicolon is reached.
106  */
107 {
108     static const token_t TokenList[] = { TOK_RPAREN, TOK_SEMI };
109     SkipTokens (TokenList, sizeof(TokenList) / sizeof(TokenList[0]));
110 }
111
112
113
114 static int CmpKey (const void* Key, const void* Elem)
115 /* Compare function for bsearch */
116 {
117     return strcmp ((const char*) Key, ((const struct Pragma*) Elem)->Key);
118 }
119
120
121
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.
125  */
126 {
127     struct Pragma* P;
128     P = bsearch (Key, Pragmas, PR_COUNT, sizeof (Pragmas[0]), CmpKey);
129     return P? P->Tok : PR_ILLEGAL;
130 }
131
132
133
134 static void StringPragma (StrBuf* B, void (*Func) (const char*))
135 /* Handle a pragma that expects a string parameter */
136 {
137     StrBuf S;
138
139     /* We expect a string here */
140     if (SB_GetString (B, &S)) {
141         /* Call the given function with the string argument */
142         Func (SB_GetConstBuf (&S));
143     } else {
144         Error ("String literal expected");
145     }
146
147     /* Call the string buf destructor */
148     DoneStrBuf (&S);
149 }
150
151
152
153 static void SegNamePragma (StrBuf* B, segment_t Seg)
154 /* Handle a pragma that expects a segment name parameter */
155 {
156     StrBuf S;
157
158     if (SB_GetString (B, &S)) {
159
160         /* Get the string */
161         const char* Name = SB_GetConstBuf (&S);
162
163         /* Check if the name is valid */
164         if (ValidSegName (Name)) {
165
166             /* Set the new name */
167             g_segname (Seg, Name);
168
169         } else {
170
171             /* Segment name is invalid */
172             Error ("Illegal segment name: `%s'", Name);
173
174         }
175
176     } else {
177         Error ("String literal expected");
178     }
179
180     /* Call the string buf destructor */
181     DoneStrBuf (&S);
182 }
183
184
185
186 static void CharMapPragma (StrBuf* B)
187 /* Change the character map */
188 {
189     long Index, C;
190
191     /* Read the character index */
192     if (!SB_GetNumber (B, &Index)) {
193         return;
194     }
195     if (Index < 1 || Index > 255) {
196         Error ("Character index out of range");
197         return;
198     }
199
200     /* Comma follows */
201     SB_SkipWhite (B);
202     if (SB_Get (B) != ',') {
203         Error ("Comma expected");
204         return;
205     }
206     SB_SkipWhite (B);
207
208     /* Read the character code */
209     if (!SB_GetNumber (B, &C)) {
210         return;
211     }
212     if (C < 1 || C > 255) {
213         Error ("Character code out of range");
214         return;
215     }
216
217     /* Remap the character */
218     printf ("Translating %04lX to %04lX\n", Index, C);
219     TgtTranslateSet ((unsigned) Index, (unsigned char) C);
220 }
221
222
223
224 static void FlagPragma (StrBuf* B, unsigned char* Flag)
225 /* Handle a pragma that expects a boolean paramater */
226 {
227     ident Ident;
228
229     if (SB_Peek (B) == '0') {
230         SB_Skip (B);
231         *Flag = 0;
232     } else if (SB_Peek (B) == '1') {
233         SB_Skip (B);
234         *Flag = 1;
235     } else if (SB_GetSym (B, Ident)) {
236         if (strcmp (Ident, "true") == 0 || strcmp (Ident, "on") == 0) {
237             *Flag = 1;
238         } else if (strcmp (Ident, "false") == 0 || strcmp (Ident, "off") == 0) {
239             *Flag = 0;
240         } else {
241             Error ("Pragma argument must be one of `on', `off', `true' or `false'");
242         }
243     } else {
244         Error ("Invalid pragma argument");
245     }
246 }
247
248
249
250 static void ParsePragma (void)
251 /* Parse the contents of the _Pragma statement */
252 {
253     pragma_t Pragma;
254     ident    Ident;
255
256     /* Create a string buffer from the string literal */
257     StrBuf B = AUTO_STRBUF_INITIALIZER;
258     GetLiteralStrBuf (&B, CurTok.IVal);
259
260     /* Reset the string pointer, effectivly clearing the string from the
261      * string table. Since we're working with one token lookahead, this
262      * will fail if the next token is also a string token, but that's a
263      * syntax error anyway, because we expect a right paren.
264      */
265     ResetLiteralPoolOffs (CurTok.IVal);
266
267     /* Skip the string token */
268     NextToken ();
269
270     /* Get the pragma name from the string */
271     SB_SkipWhite (&B);
272     if (!SB_GetSym (&B, Ident)) {
273         Error ("Invalid pragma");
274         return;
275     }
276
277     /* Search for the name */
278     Pragma = FindPragma (Ident);
279
280     /* Do we know this pragma? */
281     if (Pragma == PR_ILLEGAL) {
282         /* According to the ANSI standard, we're not allowed to generate errors
283          * for unknown pragmas, however, we're allowed to warn - and we will
284          * do so. Otherwise one typo may give you hours of bug hunting...
285          */
286         Warning ("Unknown pragma `%s'", Ident);
287         return;
288     }
289
290     /* Check for an open paren */
291     SB_SkipWhite (&B);
292     if (SB_Get (&B) != '(') {
293         Error ("'(' expected");
294         return;
295     }
296
297     /* Skip white space before the argument */
298     SB_SkipWhite (&B);
299
300     /* Switch for the different pragmas */
301     switch (Pragma) {
302
303         case PR_BSSSEG:
304             SegNamePragma (&B, SEG_BSS);
305             break;
306
307         case PR_CHARMAP:
308             CharMapPragma (&B);
309             break;
310
311         case PR_CHECKSTACK:
312             FlagPragma (&B, &CheckStack);
313             break;
314
315         case PR_CODESEG:
316             SegNamePragma (&B, SEG_CODE);
317             break;
318
319         case PR_DATASEG:
320             SegNamePragma (&B, SEG_DATA);
321             break;
322
323         case PR_REGVARADDR:
324             FlagPragma (&B, &AllowRegVarAddr);
325             break;
326
327         case PR_RODATASEG:
328             SegNamePragma (&B, SEG_RODATA);
329             break;
330
331         case PR_SIGNEDCHARS:
332             FlagPragma (&B, &SignedChars);
333             break;
334
335         case PR_STATICLOCALS:
336             FlagPragma (&B, &StaticLocals);
337             break;
338
339         case PR_ZPSYM:
340             StringPragma (&B, MakeZPSym);
341             break;
342
343         default:
344             Internal ("Invalid pragma");
345     }
346
347     /* Closing paren expected */
348     SB_SkipWhite (&B);
349     if (SB_Get (&B) != ')') {
350         Error ("')' expected");
351         return;
352     }
353     SB_SkipWhite (&B);
354
355     /* Allow an optional semicolon to be compatible with the old syntax */
356     if (SB_Peek (&B) == ';') {
357         SB_Skip (&B);
358         SB_SkipWhite (&B);
359     }
360
361     /* Make sure nothing follows */
362     if (SB_Peek (&B) != '\0') {
363         Error ("Unexpected input following pragma directive");
364     }
365
366     /* Release the StrBuf */
367     DoneStrBuf (&B);
368 }
369
370
371
372 void DoPragma (void)
373 /* Handle pragmas. These come always in form of the new C99 _Pragma() operator. */
374 {
375     /* Skip the token itself */
376     NextToken ();
377
378     /* We expect an opening paren */
379     if (!ConsumeLParen ()) {
380         return;
381     }
382
383     /* String literal */
384     if (CurTok.Tok != TOK_SCONST) {
385
386         /* Print a diagnostic */
387         Error ("String literal expected");
388
389         /* Try some smart error recovery: Skip tokens until we reach the
390          * enclosing paren, or a semicolon.
391          */
392         PragmaErrorSkip ();
393
394     } else {
395
396         /* Parse the _Pragma statement */
397         ParsePragma ();
398     }
399
400     /* Closing paren needed */
401     ConsumeRParen ();
402 }
403
404
405