]> git.sur5r.net Git - cc65/blobdiff - src/ca65/condasm.c
Rename symbol index => import id because that's what it really is.
[cc65] / src / ca65 / condasm.c
index 5b699e574ccb5861773390fe906e5b2f4d8452e8..e89bcf62b52f708129d75637d62db0d0eacde71e 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2000      Ullrich von Bassewitz                                       */
-/*               Wacholderweg 14                                             */
-/*               D-70597 Stuttgart                                           */
-/* EMail:        uz@musoftware.de                                            */
+/* (C) 2000-2010, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
 
 
 
+/* ca65 */
 #include "error.h"
 #include "expr.h"
+#include "instr.h"
 #include "nexttok.h"
+#include "symbol.h"
 #include "symtab.h"
 #include "condasm.h"
 
@@ -48,7 +51,7 @@
 
 
 /* Maximum count of nested .ifs */
-#define MAX_IFS                32
+#define MAX_IFS                256
 
 /* Set of bitmapped flags for the if descriptor */
 enum {
@@ -67,8 +70,8 @@ enum {
 
 
 /* One .IF descriptor */
-typedef struct IfDesc_ IfDesc;
-struct IfDesc_ {
+typedef struct IfDesc IfDesc;
+struct IfDesc {
     unsigned           Flags;          /* Bitmapped flags, see above */
     FilePos            Pos;            /* File position of the .IF */
     const char* Name;          /* Name of the directive */
@@ -87,7 +90,7 @@ static IfDesc* AllocIf (const char* Directive, int NeedTerm)
 
     /* Check for stack overflow */
     if (IfCount >= MAX_IFS) {
-               Error (ERR_IF_NESTING);
+               Fatal ("Too many nested .IFs");
     }
 
     /* Alloc one element */
@@ -119,11 +122,11 @@ static IfDesc* GetCurrentIf (void)
 static void FreeIf (void)
 /* Free all .IF descriptors until we reach one with the NeedTerm bit set */
 {
-    int Done = 0;
+    int Done;
     do {
                IfDesc* D = GetCurrentIf();
                if (D == 0) {
-                   Error (ERR_UNEXPECTED, ".ENDIF");
+                   Error (" Unexpected .ENDIF");
            Done = 1;
                } else {
                    Done = (D->Flags & ifNeedTerm) != 0;
@@ -207,10 +210,10 @@ void DoConditionals (void)
            case TOK_ELSE:
                        D = GetCurrentIf ();
                if (D == 0) {
-                   Error (ERR_UNEXPECTED, ".ELSE");
+                           Error ("Unexpected .ELSE");
                        } else if (GetElse(D)) {
                    /* We already had a .ELSE ! */
-                   Error (ERR_DUPLICATE_ELSE);
+                   Error ("Duplicate .ELSE");
                        } else {
                    /* Allow an .ELSE */
                    InvertIfCond (D);
@@ -220,15 +223,16 @@ void DoConditionals (void)
                    IfCond = GetCurrentIfCond ();
                }
                NextTok ();
+               ExpectSep ();
                        break;
 
                    case TOK_ELSEIF:
                D = GetCurrentIf ();
                if (D == 0) {
-                   Error (ERR_UNEXPECTED, ".ELSEIF");
+                   Error ("Unexpected .ELSEIF");
                } else if (GetElse(D)) {
                    /* We already had a .ELSE */
-                   Error (ERR_DUPLICATE_ELSE);
+                   Error ("Duplicate .ELSE");
                } else {
                    /* Handle as if there was an .ELSE first */
                    InvertIfCond (D);
@@ -244,6 +248,7 @@ void DoConditionals (void)
                     */
                    if (IfCond == 0) {
                        SetIfCond (D, ConstExpression ());
+                       ExpectSep ();
                    }
 
                    /* Get the new overall condition */
@@ -259,6 +264,7 @@ void DoConditionals (void)
                 * has been cleanup up, since we may be at end of file.
                 */
                NextTok ();
+               ExpectSep ();
 
                /* Get the new overall condition */
                IfCond = GetCurrentIfCond ();
@@ -269,6 +275,7 @@ void DoConditionals (void)
                NextTok ();
                if (IfCond) {
                    SetIfCond (D, ConstExpression ());
+                   ExpectSep ();
                }
                IfCond = GetCurrentIfCond ();
                break;
@@ -277,7 +284,12 @@ void DoConditionals (void)
                D = AllocIf (".IFBLANK", 1);
                NextTok ();
                if (IfCond) {
-                   SetIfCond (D, Tok == TOK_SEP);
+                    if (TokIsSep (Tok)) {
+                        SetIfCond (D, 1);
+                    } else {
+                       SetIfCond (D, 0);
+                        SkipUntilSep ();
+                    }
                }
                IfCond = GetCurrentIfCond ();
                break;
@@ -287,8 +299,9 @@ void DoConditionals (void)
                NextTok ();
                if (IfCond) {
                    ExprNode* Expr = Expression();
-                   SetIfCond (D, IsConstExpr (Expr));
+                   SetIfCond (D, IsConstExpr (Expr, 0));
                    FreeExpr (Expr);
+                   ExpectSep ();
                }
                IfCond = GetCurrentIfCond ();
                break;
@@ -297,22 +310,23 @@ void DoConditionals (void)
                D = AllocIf (".IFDEF", 1);
                NextTok ();
                if (IfCond) {
-                   if (Tok != TOK_IDENT) {
-                       ErrorSkip (ERR_IDENT_EXPECTED);
-                   } else {
-                       SetIfCond (D, SymIsDef (SVal));
-                       NextTok ();
-                   }
+                           SymEntry* Sym = ParseAnySymName (SYM_FIND_EXISTING);
+                   SetIfCond (D, Sym != 0 && SymIsDef (Sym));
                }
                IfCond = GetCurrentIfCond ();
                break;
 
            case TOK_IFNBLANK:
-               D = AllocIf (".IFNBLANK", 1);
+               D = AllocIf (".IFNBLANK", 1);
                NextTok ();
                if (IfCond) {
-                   SetIfCond (D, Tok != TOK_SEP);
-               }
+                    if (TokIsSep (Tok)) {
+                        SetIfCond (D, 0);
+                    } else {
+                       SetIfCond (D, 1);
+                        SkipUntilSep ();
+                    }
+               }
                IfCond = GetCurrentIfCond ();
                break;
 
@@ -321,8 +335,9 @@ void DoConditionals (void)
                NextTok ();
                if (IfCond) {
                    ExprNode* Expr = Expression();
-                   SetIfCond (D, !IsConstExpr (Expr));
+                   SetIfCond (D, !IsConstExpr (Expr, 0));
                    FreeExpr (Expr);
+                   ExpectSep ();
                }
                IfCond = GetCurrentIfCond ();
                break;
@@ -331,12 +346,9 @@ void DoConditionals (void)
                D = AllocIf (".IFNDEF", 1);
                NextTok ();
                if (IfCond) {
-                   if (Tok != TOK_IDENT) {
-                       ErrorSkip (ERR_IDENT_EXPECTED);
-                   } else {
-                       SetIfCond (D, !SymIsDef (SVal));
-                       NextTok ();
-                   }
+                   SymEntry* Sym = ParseAnySymName (SYM_FIND_EXISTING);
+                   SetIfCond (D, Sym == 0 || !SymIsDef (Sym));
+                   ExpectSep ();
                }
                IfCond = GetCurrentIfCond ();
                break;
@@ -345,41 +357,66 @@ void DoConditionals (void)
                D = AllocIf (".IFNREF", 1);
                NextTok ();
                if (IfCond) {
-                   if (Tok != TOK_IDENT) {
-                       ErrorSkip (ERR_IDENT_EXPECTED);
-                   } else {
-                       SetIfCond (D, !SymIsRef (SVal));
-                       NextTok ();
-                   }
-               }
+                   SymEntry* Sym = ParseAnySymName (SYM_FIND_EXISTING);
+                   SetIfCond (D, Sym == 0 || !SymIsRef (Sym));
+                   ExpectSep ();
+               }
                IfCond = GetCurrentIfCond ();
                break;
 
            case TOK_IFP02:
+                       D = AllocIf (".IFP02", 1);
+               NextTok ();
+               if (IfCond) {
+                   SetIfCond (D, GetCPU() == CPU_6502);
+               }
+               IfCond = GetCurrentIfCond ();
+               ExpectSep ();
                break;
 
            case TOK_IFP816:
+               D = AllocIf (".IFP816", 1);
+               NextTok ();
+               if (IfCond) {
+                           SetIfCond (D, GetCPU() == CPU_65816);
+               }
+               IfCond = GetCurrentIfCond ();
+               ExpectSep ();
                break;
 
            case TOK_IFPC02:
+               D = AllocIf (".IFPC02", 1);
+               NextTok ();
+               if (IfCond) {
+                           SetIfCond (D, GetCPU() == CPU_65C02);
+               }
+               IfCond = GetCurrentIfCond ();
+               ExpectSep ();
+               break;
+
+           case TOK_IFPSC02:
+               D = AllocIf (".IFPSC02", 1);
+               NextTok ();
+               if (IfCond) {
+                           SetIfCond (D, GetCPU() == CPU_65SC02);
+               }
+               IfCond = GetCurrentIfCond ();
+               ExpectSep ();
                break;
 
            case TOK_IFREF:
                D = AllocIf (".IFREF", 1);
                NextTok ();
                if (IfCond) {
-                   if (Tok != TOK_IDENT) {
-                       ErrorSkip (ERR_IDENT_EXPECTED);
-                   } else {
-                       SetIfCond (D, SymIsRef (SVal));
-                       NextTok ();
-                   }
+                   SymEntry* Sym = ParseAnySymName (SYM_FIND_EXISTING);
+                   SetIfCond (D, Sym != 0 && SymIsRef (Sym));
+                   ExpectSep ();
                }
                IfCond = GetCurrentIfCond ();
                break;
 
            default:
-               // Skip tokens
+               /* Skip tokens */
                NextTok ();
 
        }
@@ -389,6 +426,39 @@ void DoConditionals (void)
 
 
 
+int CheckConditionals (void)
+/* Check if the current token is one that starts a conditional directive, and
+ * call DoConditionals if so. Return true if a conditional directive was found,
+ * return false otherwise.
+ */
+{
+    switch (Tok) {
+        case TOK_ELSE:
+        case TOK_ELSEIF:
+        case TOK_ENDIF:
+        case TOK_IF:
+        case TOK_IFBLANK:
+        case TOK_IFCONST:
+        case TOK_IFDEF:
+        case TOK_IFNBLANK:
+        case TOK_IFNCONST:
+        case TOK_IFNDEF:
+        case TOK_IFNREF:
+        case TOK_IFP02:
+        case TOK_IFP816:
+        case TOK_IFPC02:
+        case TOK_IFPSC02:
+        case TOK_IFREF:
+            DoConditionals ();
+            return 1;
+
+        default:
+            return 0;
+    }
+}
+
+
+
 void CheckOpenIfs (void)
 /* Called from the scanner before closing an input file. Will check for any
  * open .ifs in this file.
@@ -410,7 +480,7 @@ void CheckOpenIfs (void)
        }
 
                /* Start of .if is in the file we're about to leave */
-       PError (&D->Pos, ERR_OPEN_IF);
+       PError (&D->Pos, "Conditional assembly branch was never closed");
        FreeIf ();
     }
 }