1 /*****************************************************************************/
5 /* Conditional assembly support for ca65 */
9 /* (C) 2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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. */
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: */
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 */
32 /*****************************************************************************/
44 /*****************************************************************************/
46 /*****************************************************************************/
50 /* Maximum count of nested .ifs */
53 /* Set of bitmapped flags for the if descriptor */
55 ifNone = 0x0000, /* No flag */
56 ifCond = 0x0001, /* IF condition was true */
57 ifElse = 0x0002, /* We had a .ELSE branch */
58 ifNeedTerm = 0x0004 /* Need .ENDIF termination */
63 /*****************************************************************************/
65 /*****************************************************************************/
69 /* One .IF descriptor */
70 typedef struct IfDesc_ IfDesc;
72 unsigned Flags; /* Bitmapped flags, see above */
73 FilePos Pos; /* File position of the .IF */
74 const char* Name; /* Name of the directive */
78 static IfDesc IfStack [MAX_IFS];
79 static unsigned IfCount = 0;
83 static IfDesc* AllocIf (const char* Directive, int NeedTerm)
84 /* Alloc a new element from the .IF stack */
88 /* Check for stack overflow */
89 if (IfCount >= MAX_IFS) {
90 Error (ERR_IF_NESTING);
93 /* Alloc one element */
94 ID = &IfStack [IfCount++];
96 /* Initialize elements */
97 ID->Flags = NeedTerm? ifNeedTerm : ifNone;
101 /* Return the result */
107 static IfDesc* GetCurrentIf (void)
108 /* Return the current .IF descriptor */
113 return &IfStack [IfCount-1];
119 static void FreeIf (void)
120 /* Free all .IF descriptors until we reach one with the NeedTerm bit set */
124 IfDesc* D = GetCurrentIf();
126 Error (ERR_UNEXPECTED, ".ENDIF");
128 Done = (D->Flags & ifNeedTerm) != 0;
136 static int GetCurrentIfCond (void)
137 /* Return the current condition based on all conditions on the stack */
140 for (Count = 0; Count < IfCount; ++Count) {
141 if ((IfStack[Count].Flags & ifCond) == 0) {
150 static void SetIfCond (IfDesc* ID, int C)
151 /* Set the .IF condition */
156 ID->Flags &= ~ifCond;
162 static void InvertIfCond (IfDesc* ID)
163 /* Invert the current condition */
170 static int GetElse (const IfDesc* ID)
171 /* Return true if we had a .ELSE */
173 return (ID->Flags & ifElse) != 0;
178 static void SetElse (IfDesc* ID, int E)
179 /* Set the .ELSE flag */
184 ID->Flags &= ~ifElse;
190 /*****************************************************************************/
192 /*****************************************************************************/
196 void DoConditionals (void)
197 /* Catch all for conditional directives */
201 int IfCond = GetCurrentIfCond ();
209 Error (ERR_UNEXPECTED, ".ELSE");
210 } else if (GetElse(D)) {
211 /* We already had a .ELSE ! */
212 Error (ERR_DUPLICATE_ELSE);
219 IfCond = GetCurrentIfCond ();
227 Error (ERR_UNEXPECTED, ".ELSEIF");
228 } else if (GetElse(D)) {
229 /* We already had a .ELSE */
230 Error (ERR_DUPLICATE_ELSE);
232 /* Handle as if there was an .ELSE first */
236 /* Allocate and prepare a new descriptor */
237 D = AllocIf (".ELSEIF", 0);
240 /* Ignore the new condition if we are inside a false .ELSE
241 * branch. This way we won't get any errors about undefined
242 * symbols or similar...
245 SetIfCond (D, ConstExpression ());
248 /* Get the new overall condition */
249 IfCond = GetCurrentIfCond ();
254 /* We're done with this .IF.. - remove the descriptor(s) */
257 /* Be sure not to read the next token until the .IF stack
258 * has been cleanup up, since we may be at end of file.
262 /* Get the new overall condition */
263 IfCond = GetCurrentIfCond ();
267 D = AllocIf (".IF", 1);
270 SetIfCond (D, ConstExpression ());
272 IfCond = GetCurrentIfCond ();
276 D = AllocIf (".IFBLANK", 1);
279 SetIfCond (D, Tok == TOK_SEP);
281 IfCond = GetCurrentIfCond ();
285 D = AllocIf (".IFCONST", 1);
288 ExprNode* Expr = Expression();
289 SetIfCond (D, IsConstExpr (Expr));
292 IfCond = GetCurrentIfCond ();
296 D = AllocIf (".IFDEF", 1);
299 if (Tok != TOK_IDENT) {
300 ErrorSkip (ERR_IDENT_EXPECTED);
302 SetIfCond (D, SymIsDef (SVal));
306 IfCond = GetCurrentIfCond ();
310 D = AllocIf (".IFNBLANK", 1);
313 SetIfCond (D, Tok != TOK_SEP);
315 IfCond = GetCurrentIfCond ();
319 D = AllocIf (".IFNCONST", 1);
322 ExprNode* Expr = Expression();
323 SetIfCond (D, !IsConstExpr (Expr));
326 IfCond = GetCurrentIfCond ();
330 D = AllocIf (".IFNDEF", 1);
333 if (Tok != TOK_IDENT) {
334 ErrorSkip (ERR_IDENT_EXPECTED);
336 SetIfCond (D, !SymIsDef (SVal));
340 IfCond = GetCurrentIfCond ();
344 D = AllocIf (".IFNREF", 1);
347 if (Tok != TOK_IDENT) {
348 ErrorSkip (ERR_IDENT_EXPECTED);
350 SetIfCond (D, !SymIsRef (SVal));
354 IfCond = GetCurrentIfCond ();
367 D = AllocIf (".IFREF", 1);
370 if (Tok != TOK_IDENT) {
371 ErrorSkip (ERR_IDENT_EXPECTED);
373 SetIfCond (D, SymIsRef (SVal));
377 IfCond = GetCurrentIfCond ();
386 } while (IfCond == 0 && Tok != TOK_EOF);
391 void CheckOpenIfs (void)
392 /* Called from the scanner before closing an input file. Will check for any
393 * open .ifs in this file.
397 /* Get the current file number and check if the topmost entry on the
398 * .IF stack was inserted with this file number
400 IfDesc* D = GetCurrentIf ();
402 /* There are no open .IFs */
406 if (D->Pos.Name != CurPos.Name) {
407 /* The .if is from another file, bail out */
411 /* Start of .if is in the file we're about to leave */
412 PError (&D->Pos, ERR_OPEN_IF);