]> git.sur5r.net Git - cc65/blob - src/cc65/pragma.c
21203944f731f814ab4b2e6c5b33bf060f248387
[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     if (SB_GetString (B, &S)) {
140         /* Call the given function with the string argument */
141         Func (SB_GetConstBuf (&S));
142     } else {
143         Error ("String literal expected");
144     }
145 }
146
147
148
149 static void SegNamePragma (StrBuf* B, segment_t Seg)
150 /* Handle a pragma that expects a segment name parameter */
151 {
152     StrBuf S;
153
154     if (SB_GetString (B, &S)) {
155
156         /* Get the string */
157         const char* Name = SB_GetConstBuf (&S);
158
159         /* Check if the name is valid */
160         if (ValidSegName (Name)) {
161
162             /* Set the new name */
163             g_segname (Seg, Name);
164
165         } else {
166
167             /* Segment name is invalid */
168             Error ("Illegal segment name: `%s'", Name);
169
170         }
171
172     } else {
173         Error ("String literal expected");
174     }
175 }
176
177
178
179 static void CharMapPragma (StrBuf* B)
180 /* Change the character map */
181 {
182     unsigned Index, C;
183
184     ExprDesc Val;
185
186     /* Read the character index */
187     ConstIntExpr (&Val);
188     if (Val.ConstVal < 1 || Val.ConstVal > 255) {
189         Error ("Character index out of range");
190         Index = 'A';
191     } else {
192         Index = Val.ConstVal;
193     }
194
195     /* Comma follows */
196     ConsumeComma ();
197
198     /* Read the character code */
199     ConstIntExpr (&Val);
200     if (Val.ConstVal < 1 || Val.ConstVal > 255) {
201         Error ("Character code out of range");
202         C = 'A';
203     } else {
204         C = Val.ConstVal;
205     }
206
207     /* Remap the character */
208     TgtTranslateSet (Index, C);
209 }
210
211
212
213 static void FlagPragma (StrBuf* B, unsigned char* Flag)
214 /* Handle a pragma that expects a boolean paramater */
215 {
216     ident Ident;
217
218     if (SB_Peek (B) == '0') {
219         SB_Skip (B);
220         *Flag = 0;
221     } else if (SB_Peek (B) == '1') {
222         SB_Skip (B);
223         *Flag = 1;
224     } else if (SB_GetSym (B, Ident)) {
225         if (strcmp (Ident, "true") == 0 || strcmp (Ident, "on") == 0) {
226             *Flag = 1;
227         } else if (strcmp (Ident, "false") == 0 || strcmp (Ident, "off") == 0) {
228             *Flag = 0;
229         } else {
230             Error ("Pragma argument must be one of `on', `off', `true' or `false'");
231         }
232     } else {
233         Error ("Invalid pragma argument");
234     }
235 }
236
237
238
239 static void ParsePragma (void)
240 /* Parse the contents of the _Pragma statement */
241 {
242     pragma_t Pragma;
243     ident    Ident;
244
245     /* Create a string buffer from the string literal */
246     StrBuf B = AUTO_STRBUF_INITIALIZER;
247     GetLiteralStrBuf (&B, CurTok.IVal);
248
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.
253      */
254     ResetLiteralPoolOffs (CurTok.IVal);
255
256     /* Skip the string token */
257     NextToken ();
258
259     /* Get the pragma name from the string */
260     SB_SkipWhite (&B);
261     if (!SB_GetSym (&B, Ident)) {
262         Error ("Invalid pragma");
263         return;
264     }
265
266     /* Search for the name */
267     Pragma = FindPragma (Ident);
268
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...
274          */
275         Warning ("Unknown pragma `%s'", Ident);
276         return;
277     }
278
279     /* Check for an open paren */
280     SB_SkipWhite (&B);
281     if (SB_Get (&B) != '(') {
282         Error ("'(' expected");
283         return;
284     }
285
286     /* Skip white space before the argument */
287     SB_SkipWhite (&B);
288
289     /* Switch for the different pragmas */
290     switch (Pragma) {
291
292         case PR_BSSSEG:
293             SegNamePragma (&B, SEG_BSS);
294             break;
295
296         case PR_CHARMAP:
297             CharMapPragma (&B);
298             break;
299
300         case PR_CHECKSTACK:
301             FlagPragma (&B, &CheckStack);
302             break;
303
304         case PR_CODESEG:
305             SegNamePragma (&B, SEG_CODE);
306             break;
307
308         case PR_DATASEG:
309             SegNamePragma (&B, SEG_DATA);
310             break;
311
312         case PR_REGVARADDR:
313             FlagPragma (&B, &AllowRegVarAddr);
314             break;
315
316         case PR_RODATASEG:
317             SegNamePragma (&B, SEG_RODATA);
318             break;
319
320         case PR_SIGNEDCHARS:
321             FlagPragma (&B, &SignedChars);
322             break;
323
324         case PR_STATICLOCALS:
325             FlagPragma (&B, &StaticLocals);
326             break;
327
328         case PR_ZPSYM:
329             StringPragma (&B, MakeZPSym);
330             break;
331
332         default:
333             Internal ("Invalid pragma");
334     }
335
336     /* Closing paren expected */
337     SB_SkipWhite (&B);
338     if (SB_Get (&B) != ')') {
339         Error ("')' expected");
340         return;
341     }
342
343     /* Make sure nothing follows */
344     SB_SkipWhite (&B);
345     if (SB_Peek (&B) != '\0') {
346         Error ("Unexpected input following pragma directive");
347     }
348 }
349
350
351
352 void DoPragma (void)
353 /* Handle pragmas. These come always in form of the new C99 _Pragma() operator. */
354 {
355     /* Skip the token itself */
356     NextToken ();
357
358     /* We expect an opening paren */
359     if (!ConsumeLParen ()) {
360         return;
361     }
362
363     /* String literal */
364     if (CurTok.Tok != TOK_SCONST) {
365
366         /* Print a diagnostic */
367         Error ("String literal expected");
368
369         /* Try some smart error recovery: Skip tokens until we reach the
370          * enclosing paren, or a semicolon.
371          */
372         PragmaErrorSkip ();
373
374     } else {
375
376         /* Parse the _Pragma statement */
377         ParsePragma ();
378     }
379
380     /* Closing paren needed */
381     ConsumeRParen ();
382 }
383
384
385