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