1 /*****************************************************************************/
5 /* Conditional assembly support for ca65 */
9 /* (C) 2000-2011, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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 /*****************************************************************************/
48 /*****************************************************************************/
50 /*****************************************************************************/
54 /* Maximum count of nested .ifs */
57 /* Set of bitmapped flags for the if descriptor */
59 ifNone = 0x0000, /* No flag */
60 ifCond = 0x0001, /* IF condition was true */
61 ifElse = 0x0002, /* We had a .ELSE branch */
62 ifNeedTerm = 0x0004, /* Need .ENDIF termination */
65 /* The overall .IF condition */
70 /*****************************************************************************/
72 /*****************************************************************************/
76 /* One .IF descriptor */
77 typedef struct IfDesc IfDesc;
79 unsigned Flags; /* Bitmapped flags, see above */
80 Collection LineInfos; /* File position of the .IF */
81 const char* Name; /* Name of the directive */
85 static IfDesc IfStack [MAX_IFS];
86 static unsigned IfCount = 0;
90 static IfDesc* AllocIf (const char* Directive, int NeedTerm)
91 /* Alloc a new element from the .IF stack */
95 /* Check for stack overflow */
96 if (IfCount >= MAX_IFS) {
97 Fatal ("Too many nested .IFs");
100 /* Alloc one element */
101 ID = &IfStack[IfCount++];
103 /* Initialize elements */
104 ID->Flags = NeedTerm? ifNeedTerm : ifNone;
105 ID->LineInfos = EmptyCollection;
106 GetFullLineInfo (&ID->LineInfos, 0);
107 ID->Name = Directive;
109 /* Return the result */
115 static IfDesc* GetCurrentIf (void)
116 /* Return the current .IF descriptor */
121 return &IfStack[IfCount-1];
127 static void FreeIf (void)
128 /* Free all .IF descriptors until we reach one with the NeedTerm bit set */
132 IfDesc* ID = GetCurrentIf();
134 Error (" Unexpected .ENDIF");
137 Done = (ID->Flags & ifNeedTerm) != 0;
145 static void CalcOverallIfCond (void)
146 /* Caclulate the overall condition based on all conditions on the stack */
150 for (Count = 0; Count < IfCount; ++Count) {
151 if ((IfStack[Count].Flags & ifCond) == 0) {
160 static void SetIfCond (IfDesc* ID, int C)
161 /* Set the .IF condition */
166 ID->Flags &= ~ifCond;
172 static void ElseClause (IfDesc* ID, const char* Directive)
173 /* Enter an .ELSE clause */
175 /* Check if we have an open .IF - otherwise .ELSE is not allowed */
177 Error ("Unexpected %s", Directive);
181 /* Check for a duplicate else, then remember that we had one */
182 if (ID->Flags & ifElse) {
183 /* We already had a .ELSE ! */
184 Error ("Duplicate .ELSE");
188 /* Condition is inverted now */
194 /*****************************************************************************/
196 /*****************************************************************************/
200 void DoConditionals (void)
201 /* Catch all for conditional directives */
207 switch (CurTok.Tok) {
213 ElseClause (D, ".ELSE");
215 /* Remember the data for the .ELSE */
216 GetFullLineInfo (&D->LineInfos, 0);
219 /* Calculate the new overall condition */
220 CalcOverallIfCond ();
229 /* Handle as if there was an .ELSE first */
230 ElseClause (D, ".ELSEIF");
232 /* Allocate and prepare a new descriptor */
233 D = AllocIf (".ELSEIF", 0);
236 /* Ignore the new condition if we are inside a false .ELSE
237 * branch. This way we won't get any errors about undefined
238 * symbols or similar...
241 SetIfCond (D, ConstExpression ());
245 /* Get the new overall condition */
246 CalcOverallIfCond ();
250 /* We're done with this .IF.. - remove the descriptor(s) */
253 /* Be sure not to read the next token until the .IF stack
254 * has been cleanup up, since we may be at end of file.
259 /* Get the new overall condition */
260 CalcOverallIfCond ();
264 D = AllocIf (".IF", 1);
267 SetIfCond (D, ConstExpression ());
270 CalcOverallIfCond ();
274 D = AllocIf (".IFBLANK", 1);
277 if (TokIsSep (CurTok.Tok)) {
284 CalcOverallIfCond ();
288 D = AllocIf (".IFCONST", 1);
291 ExprNode* Expr = Expression();
292 SetIfCond (D, IsConstExpr (Expr, 0));
296 CalcOverallIfCond ();
300 D = AllocIf (".IFDEF", 1);
303 SymEntry* Sym = ParseAnySymName (SYM_FIND_EXISTING);
304 SetIfCond (D, Sym != 0 && SymIsDef (Sym));
306 CalcOverallIfCond ();
310 D = AllocIf (".IFNBLANK", 1);
313 if (TokIsSep (CurTok.Tok)) {
320 CalcOverallIfCond ();
324 D = AllocIf (".IFNCONST", 1);
327 ExprNode* Expr = Expression();
328 SetIfCond (D, !IsConstExpr (Expr, 0));
332 CalcOverallIfCond ();
336 D = AllocIf (".IFNDEF", 1);
339 SymEntry* Sym = ParseAnySymName (SYM_FIND_EXISTING);
340 SetIfCond (D, Sym == 0 || !SymIsDef (Sym));
343 CalcOverallIfCond ();
347 D = AllocIf (".IFNREF", 1);
350 SymEntry* Sym = ParseAnySymName (SYM_FIND_EXISTING);
351 SetIfCond (D, Sym == 0 || !SymIsRef (Sym));
354 CalcOverallIfCond ();
358 D = AllocIf (".IFP02", 1);
361 SetIfCond (D, GetCPU() == CPU_6502);
364 CalcOverallIfCond ();
368 D = AllocIf (".IFP816", 1);
371 SetIfCond (D, GetCPU() == CPU_65816);
374 CalcOverallIfCond ();
378 D = AllocIf (".IFPC02", 1);
381 SetIfCond (D, GetCPU() == CPU_65C02);
384 CalcOverallIfCond ();
388 D = AllocIf (".IFPSC02", 1);
391 SetIfCond (D, GetCPU() == CPU_65SC02);
394 CalcOverallIfCond ();
398 D = AllocIf (".IFREF", 1);
401 SymEntry* Sym = ParseAnySymName (SYM_FIND_EXISTING);
402 SetIfCond (D, Sym != 0 && SymIsRef (Sym));
405 CalcOverallIfCond ();
414 } while (IfCond == 0 && CurTok.Tok != TOK_EOF);
419 int CheckConditionals (void)
420 /* Check if the current token is one that starts a conditional directive, and
421 * call DoConditionals if so. Return true if a conditional directive was found,
422 * return false otherwise.
425 switch (CurTok.Tok) {
452 void CheckOpenIfs (void)
453 /* Called from the scanner before closing an input file. Will check for any
454 * open .ifs in this file.
460 /* Get the current file number and check if the topmost entry on the
461 * .IF stack was inserted with this file number
463 IfDesc* D = GetCurrentIf ();
465 /* There are no open .IFs */
469 LI = CollConstAt (&D->LineInfos, 0);
470 if (LI->Pos.Name != CurTok.Pos.Name) {
471 /* The .if is from another file, bail out */
475 /* Start of .if is in the file we're about to leave */
476 LIError (&D->LineInfos, "Conditional assembly branch was never closed");
480 /* Calculate the new overall .IF condition */
481 CalcOverallIfCond ();
486 unsigned GetIfStack (void)
487 /* Get the current .IF stack pointer */
494 void CleanupIfStack (unsigned SP)
495 /* Cleanup the .IF stack, remove anything above the given stack pointer */
497 while (IfCount > SP) {
501 /* Calculate the new overall .IF condition */
502 CalcOverallIfCond ();