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