]> git.sur5r.net Git - cc65/blob - src/ld65/cfgexpr.c
Separate processing the linker config file into two phases: The config file is
[cc65] / src / ld65 / cfgexpr.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 cfgexpr.c                                 */
4 /*                                                                           */
5 /*          Simple expressions for use with in configuration file            */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2005-2010, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
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 /* common */
37 #include "addrsize.h"
38 #include "strbuf.h"
39
40 /* ld65 */
41 #include "cfgexpr.h"
42 #include "error.h"
43 #include "exports.h"
44 #include "expr.h"
45 #include "scanner.h"
46 #include "spool.h"
47
48
49
50 /*****************************************************************************/
51 /*                                   Code                                    */
52 /*****************************************************************************/
53
54
55
56 static ExprNode* Factor (void)
57 /* Read and return a factor */
58 {
59     ExprNode* N = 0;            /* Initialize to avoid compiler warnings */
60     Export*   E;
61     unsigned  Name;
62
63
64     switch (CfgTok) {
65
66         case CFGTOK_IDENT:
67             /* Get the name as an id */
68             Name = GetStrBufId (&CfgSVal);
69
70             /* Check if we know the symbol already */
71             E = FindExport (Name);
72             if (E != 0 && IsConstExport (E)) {
73                 N = LiteralExpr (GetExportVal (E), 0);
74             } else {
75                 N = NewExprNode (0, EXPR_SYMBOL);
76                 N->V.Imp = InsertImport (GenImport (Name, ADDR_SIZE_ABS));
77             }
78
79             /* Skip the symbol name */
80             CfgNextTok ();
81             break;
82
83         case CFGTOK_INTCON:
84             /* An integer constant */
85             N = LiteralExpr (CfgIVal, 0);
86             CfgNextTok ();
87             break;
88
89         case CFGTOK_PLUS:
90             /* Unary plus */
91             CfgNextTok ();
92             N = Factor ();
93             break;
94
95         case CFGTOK_MINUS:
96             /* Unary minus */
97             CfgNextTok ();
98             N = NewExprNode (0, EXPR_UNARY_MINUS);
99             N->Left = Factor ();
100             break;
101
102         case CFGTOK_LPAR:
103             /* Left parenthesis */
104             CfgNextTok ();
105             N = CfgExpr ();
106             CfgConsume (CFGTOK_RPAR, "')' expected");
107             break;
108
109         default:
110             CfgError ("Invalid expression: %d", CfgTok);
111             break;
112     }
113
114     /* Return the new expression node */
115     return N;
116 }
117
118
119
120 static ExprNode* Term (void)
121 /* Multiplicative operators: * and / */
122 {
123     /* Read left hand side */
124     ExprNode* Root = Factor ();
125
126     /* Handle multiplicative operators */
127     while (CfgTok == CFGTOK_MUL || CfgTok == CFGTOK_DIV) {
128
129         ExprNode* Left;
130         ExprNode* Right;
131         unsigned char Op;
132
133         /* Remember the token, then skip it */
134         cfgtok_t Tok = CfgTok;
135         CfgNextTok ();
136
137         /* Move root to left side, then read right side */
138         Left = Root;
139         Right = Factor ();
140
141         /* Handle the operation */
142         switch (Tok) {
143             case CFGTOK_MUL:    Op = EXPR_MUL;  break;
144             case CFGTOK_DIV:    Op = EXPR_DIV;  break;
145             default:            Internal ("Unhandled token in Term: %d", Tok);
146         }
147         Root = NewExprNode (0, Op);
148         Root->Left = Left;
149         Root->Right = Right;
150     }
151
152     /* Return the expression tree we've created */
153     return Root;
154 }
155
156
157
158 static ExprNode* SimpleExpr (void)
159 /* Additive operators: + and - */
160 {
161     /* Read left hand side */
162     ExprNode* Root = Term ();
163
164     /* Handle additive operators */
165     while (CfgTok == CFGTOK_PLUS || CfgTok == CFGTOK_MINUS) {
166
167         ExprNode* Left;
168         ExprNode* Right;
169         unsigned char Op;
170
171         /* Remember the token, then skip it */
172         cfgtok_t Tok = CfgTok;
173         CfgNextTok ();
174
175         /* Move root to left side, then read right side */
176         Left = Root;
177         Right = Term ();
178
179         /* Handle the operation */
180         switch (Tok) {
181             case CFGTOK_PLUS:   Op = EXPR_PLUS;         break;
182             case CFGTOK_MINUS:  Op = EXPR_MINUS;        break;
183             default:            Internal ("Unhandled token in SimpleExpr: %d", Tok);
184         }
185         Root = NewExprNode (0, Op);
186         Root->Left = Left;
187         Root->Right = Right;
188     }
189
190     /* Return the expression tree we've created */
191     return Root;
192 }
193
194
195
196 ExprNode* CfgExpr (void)
197 /* Full expression */
198 {
199     return SimpleExpr ();
200 }
201
202
203
204 long CfgConstExpr (void)
205 /* Read an integer expression, make sure its constant and return its value */
206 {
207     long Val;
208
209     /* Parse the expression */
210     ExprNode* Expr = CfgExpr ();
211
212     /* Check that it's const */
213     if (!IsConstExpr (Expr)) {
214         CfgError ("Constant expression expected");
215     }
216
217     /* Get the value */
218     Val = GetExprVal (Expr);
219
220     /* Cleanup E */
221     FreeExpr (Expr);
222
223     /* Return the value */
224     return Val;
225 }
226
227
228
229 long CfgCheckedConstExpr (long Min, long Max)
230 /* Read an expression, make sure it's an int and in range, then return its
231  * value.
232  */
233 {
234     /* Get the value */
235     long Val = CfgConstExpr ();
236
237     /* Check the range */
238     if (Val < Min || Val > Max) {
239         CfgError ("Range error");
240     }
241
242     /* Return the value */
243     return Val;
244 }
245
246
247