]> git.sur5r.net Git - cc65/blob - src/cc65/pragma.c
Error handling cleanup/changes.
[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-2000 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
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 <ctype.h>
38
39 #include "codegen.h"
40 #include "error.h"
41 #include "expr.h"
42 #include "global.h"
43 #include "litpool.h"
44 #include "scanner.h"
45 #include "segname.h"
46 #include "symtab.h"
47 #include "pragma.h"
48
49
50
51 /*****************************************************************************/
52 /*                                   data                                    */
53 /*****************************************************************************/
54
55
56
57 /* Tokens for the #pragmas */
58 typedef enum {
59     PR_BSSSEG,
60     PR_CODESEG,
61     PR_DATASEG,
62     PR_REGVARADDR,
63     PR_RODATASEG,
64     PR_SIGNEDCHARS,
65     PR_STATICLOCALS,
66     PR_ZPSYM,
67     PR_ILLEGAL
68 } pragma_t;
69
70 /* Pragma table */
71 static const struct Pragma {
72     const char* Key;            /* Keyword */
73     pragma_t    Tok;            /* Token */
74 } Pragmas[] = {
75     {   "bssseg",       PR_BSSSEG       },
76     {   "codeseg",      PR_CODESEG      },
77     {   "dataseg",      PR_DATASEG      },
78     {   "regvaraddr",   PR_REGVARADDR   },
79     {   "rodataseg",    PR_RODATASEG    },
80     {   "signedchars",  PR_SIGNEDCHARS  },
81     {   "staticlocals", PR_STATICLOCALS },
82     {   "zpsym",        PR_ZPSYM        },
83 };
84
85 /* Number of pragmas */
86 #define PRAGMA_COUNT    (sizeof(Pragmas) / sizeof(Pragmas[0]))
87
88
89
90 /*****************************************************************************/
91 /*                                   Code                                    */
92 /*****************************************************************************/
93
94
95
96 static int CmpKey (const void* Key, const void* Elem)
97 /* Compare function for bsearch */
98 {
99     return strcmp ((const char*) Key, ((const struct Pragma*) Elem)->Key);
100 }
101
102
103
104 static pragma_t FindPragma (const char* Key)
105 /* Find a pragma and return the token. Return PR_ILLEGAL if the keyword is
106  * not a valid pragma.
107  */
108 {
109     struct Pragma* P;
110     P = bsearch (Key, Pragmas, PRAGMA_COUNT, sizeof (Pragmas[0]), CmpKey);
111     return P? P->Tok : PR_ILLEGAL;
112 }
113
114
115
116 static void StringPragma (void (*Func) (const char*))
117 /* Handle a pragma that expects a string parameter */
118 {
119     if (curtok != TOK_SCONST) {
120         Error (ERR_STRLIT_EXPECTED);
121     } else {
122         /* Get the string */
123         const char* Name = GetLiteral (curval);
124
125         /* Call the given function with the string argument */
126         Func (Name);
127
128         /* Reset the string pointer, removing the string from the pool */
129         ResetLiteralOffs (curval);
130     }
131
132     /* Skip the string (or error) token */
133     NextToken ();
134 }
135
136
137
138 static void SegNamePragma (void (*Func) (const char*))
139 /* Handle a pragma that expects a segment name parameter */
140 {
141     if (curtok != TOK_SCONST) {
142         Error (ERR_STRLIT_EXPECTED);
143     } else {
144         /* Get the segment name */
145         const char* Name = GetLiteral (curval);
146
147         /* Check if the name is valid */
148         if (ValidSegName (Name)) {
149
150             /* Call the given function to set the name */
151             Func (Name);
152
153         } else {
154
155             /* Segment name is invalid */
156             Error (ERR_ILLEGAL_SEG_NAME, Name);
157
158         }
159
160         /* Reset the string pointer, removing the string from the pool */
161         ResetLiteralOffs (curval);
162     }
163
164     /* Skip the string (or error) token */
165     NextToken ();
166 }
167
168
169
170 static void FlagPragma (unsigned char* Flag)
171 /* Handle a pragma that expects a boolean paramater */
172 {
173     /* Read a constant expression */
174     struct expent val;
175     constexpr (&val);
176
177     /* Store the value into the flag parameter */
178     *Flag = (val.e_const != 0);
179 }
180
181
182
183 void DoPragma (void)
184 /* Handle pragmas */
185 {
186     pragma_t Pragma;
187
188     /* Skip the token itself */
189     NextToken ();
190
191     /* Identifier must follow */
192     if (curtok != TOK_IDENT) {
193         Error (ERR_IDENT_EXPECTED);
194         return;
195     }
196
197     /* Do we know this pragma? */
198     Pragma = FindPragma (CurTok.Ident);
199     if (Pragma == PR_ILLEGAL) {
200         /* According to the ANSI standard, we're not allowed to generate errors
201          * for unknown pragmas, however, we're allowed to warn - and we will
202          * do so. Otherwise one typo may give you hours of bug hunting...
203          */
204         Warning ("Unknown #pragma `%s'", CurTok.Ident);
205         return;
206     }
207
208     /* Skip the identifier and check for an open paren */
209     NextToken ();
210     ConsumeLParen ();
211
212     /* Switch for the different pragmas */
213     switch (Pragma) {
214
215         case PR_BSSSEG:
216             SegNamePragma (g_bssname);
217             break;
218
219         case PR_CODESEG:
220             SegNamePragma (g_codename);
221             break;
222
223         case PR_DATASEG:
224             SegNamePragma (g_dataname);
225             break;
226
227         case PR_REGVARADDR:
228             FlagPragma (&AllowRegVarAddr);
229             break;
230
231         case PR_RODATASEG:
232             SegNamePragma (g_rodataname);
233             break;
234
235         case PR_SIGNEDCHARS:
236             FlagPragma (&SignedChars);
237             break;
238
239         case PR_STATICLOCALS:
240             FlagPragma (&StaticLocals);
241             break;
242
243         case PR_ZPSYM:
244             StringPragma (MakeZPSym);
245             break;
246
247         default:
248             Internal ("Invalid pragma");
249     }
250
251     /* Closing paren needed */
252     ConsumeRParen ();
253 }
254
255
256