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