]> git.sur5r.net Git - cc65/blobdiff - src/ca65/expr.c
Error message for negative array sizes.
[cc65] / src / ca65 / expr.c
index 44c7524d138e02b6fc3b60c7f147fcbf7bd4295a..0c638c6422fb04a27e3a50f1893d9ed8f43a6343 100644 (file)
@@ -6,7 +6,7 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998-2000 Ullrich von Bassewitz                                       */
+/* (C) 1998-2003 Ullrich von Bassewitz                                       */
 /*               Wacholderweg 14                                             */
 /*               D-70597 Stuttgart                                           */
 /* EMail:        uz@musoftware.de                                            */
 
 
 
-#include "../common/exprdefs.h"
-#include "../common/xmalloc.h"
+#include <string.h>
+#include <time.h>
 
+/* common */
+#include "check.h"
+#include "exprdefs.h"
+#include "print.h"
+#include "tgttrans.h"
+#include "xmalloc.h"
+
+/* ca65 */
 #include "error.h"
 #include "global.h"
 #include "instr.h"
@@ -113,145 +121,6 @@ static void FreeExprNode (ExprNode* E)
 
 
 
-/*****************************************************************************/
-/*             Dump an expression tree on stdout for debugging              */
-/*****************************************************************************/
-
-            
-
-static void InternalDumpExpr (ExprNode* Expr)
-/* Dump an expression in UPN */
-{
-    if (Expr == 0) {
-       return;
-    }
-    InternalDumpExpr (Expr->Left);
-    InternalDumpExpr (Expr->Right);
-
-    switch (Expr->Op) {
-
-       case EXPR_LITERAL:
-       case EXPR_ULABEL:
-           printf (" $%04lX", Expr->V.Val & 0xFFFF);
-           break;
-
-       case EXPR_SYMBOL:
-                   printf (" %s", GetSymName (Expr->V.Sym));
-           break;
-
-       case EXPR_SEGMENT:
-           printf (" SEG");
-           break;
-
-               case EXPR_PLUS:
-           printf (" +");
-           break;
-
-               case EXPR_MINUS:
-           printf (" -");
-           break;
-
-               case EXPR_MUL:
-           printf (" *");
-           break;
-
-               case EXPR_DIV:
-           printf (" /");
-           break;
-
-               case EXPR_MOD:
-           printf (" %%");
-           break;
-
-       case EXPR_OR:
-           printf (" OR");
-           break;
-
-       case EXPR_XOR:
-           printf (" XOR");
-           break;
-
-       case EXPR_AND:
-           printf (" AND");
-           break;
-
-       case EXPR_SHL:
-           printf (" SHL");
-           break;
-
-       case EXPR_SHR:
-           printf (" SHR");
-           break;
-
-               case EXPR_EQ:
-           printf (" =");
-           break;
-
-               case EXPR_NE:
-           printf ("<>");
-           break;
-
-               case EXPR_LT:
-           printf (" <");
-           break;
-
-               case EXPR_GT:
-           printf (" >");
-           break;
-
-               case EXPR_UNARY_MINUS:
-           printf (" NEG");
-           break;
-
-               case EXPR_NOT:
-           printf (" ~");
-           break;
-
-               case EXPR_LOBYTE:
-           printf (" LO");
-           break;
-
-               case EXPR_HIBYTE:
-           printf (" HI");
-           break;
-
-               case EXPR_SWAP:
-           printf (" SWAP");
-           break;
-
-       case EXPR_BAND:
-           printf (" BOOL_AND");
-           break;
-
-       case EXPR_BOR:
-           printf (" BOOL_OR");
-           break;
-
-       case EXPR_BXOR:
-           printf (" BOOL_XOR");
-           break;
-
-       case EXPR_BNOT:
-           printf (" BOOL_NOT");
-           break;
-
-        default:
-                   Internal ("Unknown Op type: %u", Expr->Op);
-
-    }
-}
-
-
-
-void DumpExpr (ExprNode* Expr)
-/* Dump an expression tree to stdout */
-{
-    InternalDumpExpr (Expr);
-    printf ("\n");
-}
-
-
-
 /*****************************************************************************/
 /*                                          Code                                    */
 /*****************************************************************************/
@@ -291,7 +160,7 @@ static int FuncBlank (void)
     } else {
        /* Skip any tokens */
        int Braces = 0;
-       while (Tok != TOK_SEP && Tok != TOK_EOF) {
+       while (!TokIsSep (Tok)) {
            if (Tok == TOK_LPAREN) {
                ++Braces;
            } else if (Tok == TOK_RPAREN) {
@@ -331,18 +200,69 @@ static int FuncConst (void)
 static int FuncDefined (void)
 /* Handle the .DEFINED builtin function */
 {
+    static const char* Keys[] = {
+               "ANY",
+       "GLOBAL",
+        "LOCAL",
+    };
+
+    char Name [sizeof (SVal)];
     int Result = 0;
+    int Scope;
 
+    /* First argument is a symbol name */
     if (Tok != TOK_IDENT) {
        Error (ERR_IDENT_EXPECTED);
        if (Tok != TOK_RPAREN) {
            NextTok ();
        }
+        return 0;
+    }
+
+    /* Remember the name, then skip it */
+    strcpy (Name, SVal);
+    NextTok ();
+
+    /* Comma and scope spec may follow */
+    if (Tok == TOK_COMMA) {
+
+        /* Skip the comma */
+        NextTok ();
+
+        /* An identifier must follow */
+        if (Tok != TOK_IDENT) {
+            Error (ERR_IDENT_EXPECTED);
+            return 0;
+        }
+
+        /* Get the scope, then skip it */
+        Scope = GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]));
+        NextTok ();
+
+        /* Check if we got a valid keyword */
+        if (Scope < 0) {
+            Error (ERR_ILLEGAL_SCOPE);
+            return 0;
+        }
+
+        /* Map the scope */
+        switch (Scope) {
+            case 0:     Scope = SCOPE_ANY;    break;
+            case 1:     Scope = SCOPE_GLOBAL; break;
+            case 2:     Scope = SCOPE_LOCAL;  break;
+            default:    Internal ("Invalid scope: %d", Scope);
+        }
+
     } else {
-       Result = SymIsDef (SVal);
-       NextTok ();
+
+        /* Any scope */
+        Scope = SCOPE_ANY;
+
     }
 
+    /* Search for the symbol */
+    Result = SymIsDef (SVal, Scope);
+
     /* Done */
     return Result;
 }
@@ -364,7 +284,7 @@ static int DoMatch (enum TC EqualityLevel)
     while (Tok != TOK_COMMA) {
 
        /* We may not end-of-line of end-of-file here */
-       if (Tok == TOK_SEP || Tok == TOK_EOF) {
+       if (TokIsSep (Tok)) {
            Error (ERR_UNEXPECTED_EOL);
            return 0;
        }
@@ -395,7 +315,7 @@ static int DoMatch (enum TC EqualityLevel)
     while (Tok != TOK_RPAREN) {
 
        /* We may not end-of-line of end-of-file here */
-       if (Tok == TOK_SEP || Tok == TOK_EOF) {
+       if (TokIsSep (Tok)) {
            Error (ERR_UNEXPECTED_EOL);
            return 0;
        }
@@ -457,7 +377,7 @@ static int FuncReferenced (void)
            NextTok ();
        }
     } else {
-       Result = SymIsRef (SVal);
+       Result = SymIsRef (SVal, SCOPE_ANY);
        NextTok ();
     }
 
@@ -467,6 +387,111 @@ static int FuncReferenced (void)
 
 
 
+static int FuncStrAt (void)
+/* Handle the .STRAT function */
+{
+    char Str [sizeof(SVal)];
+    long Index;
+
+    /* String constant expected */
+    if (Tok != TOK_STRCON) {
+       Error (ERR_STRCON_EXPECTED);
+       NextTok ();
+               return 0;
+
+    }
+
+    /* Remember the string and skip it */
+    strcpy (Str, SVal);
+    NextTok ();
+
+    /* Comma must follow */
+    ConsumeComma ();
+
+    /* Expression expected */
+    Index = ConstExpression ();
+
+    /* Must be a valid index */
+    if (Index >= (long) strlen (Str)) {
+       Error (ERR_RANGE);
+       return 0;
+    }
+
+    /* Return the char, handle as unsigned. Be sure to translate it into
+     * the target character set.
+     */
+    return (unsigned char) TgtTranslateChar (Str [(size_t)Index]);
+}
+
+
+
+static int FuncStrLen (void)
+/* Handle the .STRLEN function */
+{
+    /* String constant expected */
+    if (Tok != TOK_STRCON) {
+
+       Error (ERR_STRCON_EXPECTED);
+       /* Smart error recovery */
+       if (Tok != TOK_RPAREN) {
+           NextTok ();
+       }
+               return 0;
+
+    } else {
+
+        /* Get the length of the string */
+       int Len = strlen (SVal);
+
+       /* Skip the string */
+       NextTok ();
+
+       /* Return the length */
+       return Len;
+
+    }
+}
+
+
+
+static int FuncTCount (void)
+/* Handle the .TCOUNT function */
+{
+    /* We have a list of tokens that ends with the closing paren. Skip
+     * the tokens, handling nested braces and count them.
+     */
+    int      Count  = 0;
+    unsigned Parens = 0;
+    while (Parens != 0 || Tok != TOK_RPAREN) {
+
+       /* Check for end of line or end of input. Since the calling function
+        * will check for the closing paren, we don't need to print an error
+        * here, just bail out.
+        */
+       if (TokIsSep (Tok)) {
+           break;
+       }
+
+       /* One more token */
+       ++Count;
+
+       /* Keep track of the nesting level */
+       switch (Tok) {
+           case TOK_LPAREN:    ++Parens;       break;
+           case TOK_RPAREN:    --Parens;       break;
+           default:                            break;
+       }
+
+       /* Skip the token */
+       NextTok ();
+    }
+
+    /* Return the number of tokens */
+    return Count;
+}
+
+
+
 static int FuncXMatch (void)
 /* Handle the .XMATCH function */
 {
@@ -492,7 +517,7 @@ static ExprNode* Function (int (*F) (void))
     NextTok ();
 
     /* Call the function itself */
-    Result = (F () != 0);
+    Result = F ();
 
     /* Closing brace must follow */
     ConsumeRParen ();
@@ -511,8 +536,12 @@ static ExprNode* Factor (void)
     switch (Tok) {
 
        case TOK_INTCON:
+           N = LiteralExpr (IVal);
+                   NextTok ();
+           break;
+
        case TOK_CHARCON:
-           N = LiteralExpr (IVal);
+           N = LiteralExpr (TgtTranslateChar (IVal));
                    NextTok ();
            break;
 
@@ -522,7 +551,7 @@ static ExprNode* Factor (void)
                Error (ERR_IDENT_EXPECTED);
                N = LiteralExpr (0);    /* Dummy */
            } else {
-               S = SymRefGlobal (SVal);
+               S = SymRef (SVal, SCOPE_GLOBAL);
                if (SymIsConst (S)) {
                    /* Use the literal value instead */
                    N = LiteralExpr (GetSymVal (S));
@@ -530,14 +559,14 @@ static ExprNode* Factor (void)
                    /* Create symbol node */
                    N = NewExprNode ();
                    N->Op    = EXPR_SYMBOL;
-                   N->V.Sym = S;
+                   N->V.Sym = S;
                }
                NextTok ();
            }
            break;
 
         case TOK_IDENT:
-           S = SymRef (SVal);
+           S = SymRef (SVal, SCOPE_LOCAL);
            if (SymIsConst (S)) {
                /* Use the literal value instead */
                N = LiteralExpr (GetSymVal (S));
@@ -579,14 +608,14 @@ static ExprNode* Factor (void)
            NextTok ();
            N = NewExprNode ();
            N->Left = Factor ();
-           N->Op   = EXPR_LOBYTE;
+           N->Op   = EXPR_BYTE0;
            break;
 
        case TOK_GT:
            NextTok ();
            N = NewExprNode ();
            N->Left = Factor ();
-           N->Op   = EXPR_HIBYTE;
+           N->Op   = EXPR_BYTE1;
            break;
 
        case TOK_LPAREN:
@@ -620,13 +649,35 @@ static ExprNode* Factor (void)
            N = Function (FuncReferenced);
            break;
 
+       case TOK_STRAT:
+           N = Function (FuncStrAt);
+           break;
+
+       case TOK_STRLEN:
+           N = Function (FuncStrLen);
+           break;
+
+       case TOK_TCOUNT:
+           N = Function (FuncTCount);
+           break;
+
+       case TOK_TIME:
+           N = LiteralExpr (time (0));
+           NextTok ();
+           break;
+
        case TOK_XMATCH:
            N = Function (FuncXMatch);
            break;
 
        default:
-           N = LiteralExpr (0);        /* Dummy */
-           Error (ERR_SYNTAX);
+           if (LooseCharTerm && Tok == TOK_STRCON && strlen(SVal) == 1) {
+               /* A character constant */
+               N = LiteralExpr (TgtTranslateChar (SVal[0]));
+           } else {
+               N = LiteralExpr (0);    /* Dummy */
+               Error (ERR_SYNTAX);
+           }
            NextTok ();
            break;
     }
@@ -920,7 +971,7 @@ ExprNode* CurrentPC (void)
     if (RelocMode) {
        /* Create SegmentBase + Offset */
        Left = NewExprNode ();
-       Left->Op = EXPR_SEGMENT;
+       Left->Op = EXPR_SECTION;
        Left->V.SegNum = GetSegNum ();
 
        Root = NewExprNode ();
@@ -960,7 +1011,7 @@ ExprNode* BranchExpr (unsigned Offs)
     /* Create *+Offs */
     if (RelocMode) {
        Left = NewExprNode ();
-       Left->Op = EXPR_SEGMENT;
+       Left->Op = EXPR_SECTION;
        Left->V.SegNum = GetSegNum ();
 
        N = NewExprNode ();
@@ -1043,7 +1094,7 @@ int IsConstExpr (ExprNode* Root)
            case EXPR_SYMBOL:
                Sym = Root->V.Sym;
                if (SymHasUserMark (Sym)) {
-                   if (Verbose) {
+                   if (Verbosity > 0) {
                        DumpExpr (Root);
                    }
                    PError (GetSymPos (Sym), ERR_CIRCULAR_REFERENCE);
@@ -1116,13 +1167,16 @@ static void CheckByteExpr (const ExprNode* N, int* IsByte)
            case EXPR_LEAFNODE:
                switch (N->Op) {
 
-                   case EXPR_SYMBOL:
-                       if (SymIsZP (N->V.Sym)) {
-                           *IsByte = 1;
-                       }
+                   case EXPR_SYMBOL:
+                       if (SymIsZP (N->V.Sym)) {
+                           *IsByte = 1;
+                       } else if (SymHasExpr (N->V.Sym)) {
+                           /* Check if this expression is a byte expression */
+                           *IsByte = IsByteExpr (GetSymExpr (N->V.Sym));
+                       }
                        break;
 
-                   case EXPR_SEGMENT:
+                   case EXPR_SECTION:
                        if (GetSegType (N->V.SegNum) == SEGTYPE_ZP) {
                            *IsByte = 1;
                        }
@@ -1158,7 +1212,8 @@ int IsByteExpr (ExprNode* Root)
            SimplifyExpr (Root);
        }
                return IsByteRange (GetExprVal (Root));
-    } else if (Root->Op == EXPR_LOBYTE || Root->Op == EXPR_HIBYTE) {
+    } else if (Root->Op == EXPR_BYTE0 || Root->Op == EXPR_BYTE1 ||
+              Root->Op == EXPR_BYTE2 || Root->Op == EXPR_BYTE3) {
        /* Symbol forced to have byte range */
                IsByte = 1;
     } else {
@@ -1255,12 +1310,18 @@ long GetExprVal (ExprNode* Expr)
                case EXPR_NOT:
            return ~GetExprVal (Expr->Left);
 
-               case EXPR_LOBYTE:
+               case EXPR_BYTE0:
            return GetExprVal (Expr->Left) & 0xFF;
 
-               case EXPR_HIBYTE:
+               case EXPR_BYTE1:
            return (GetExprVal (Expr->Left) >> 8) & 0xFF;
 
+               case EXPR_BYTE2:
+           return (GetExprVal (Expr->Left) >> 16) & 0xFF;
+
+               case EXPR_BYTE3:
+           return (GetExprVal (Expr->Left) >> 24) & 0xFF;
+
         case EXPR_SWAP:
            Left = GetExprVal (Expr->Left);
            return ((Left >> 8) & 0x00FF) | ((Left << 8) & 0xFF00);
@@ -1308,7 +1369,7 @@ static ExprNode* RemoveSyms (ExprNode* Expr, int MustClone)
                SymEntry* Sym = Expr->V.Sym;
                if (SymHasUserMark (Sym)) {
                    /* Circular definition */
-                   if (Verbose) {
+                   if (Verbosity) {
                        DumpExpr (Expr);
                    }
                    PError (GetSymPos (Sym), ERR_CIRCULAR_REFERENCE);
@@ -1355,7 +1416,7 @@ static ExprNode* RemoveSyms (ExprNode* Expr, int MustClone)
                Clone->V.Sym = Expr->V.Sym;
                break;
 
-           case EXPR_SEGMENT:
+           case EXPR_SECTION:
                Clone->V.SegNum = Expr->V.SegNum;
                break;
 
@@ -1416,7 +1477,7 @@ static ExprNode* ConstExtract (ExprNode* Expr, long* Val, int Sign)
            return Left;
        } else {
            /* Check for SEG - SEG which is now possible */
-           if (Left->Op == EXPR_SEGMENT && Right->Op == EXPR_SEGMENT &&
+           if (Left->Op == EXPR_SECTION && Right->Op == EXPR_SECTION &&
                Left->V.SegNum == Right->V.SegNum) {
                /* SEG - SEG, remove it completely */
                FreeExprNode (Left);
@@ -1502,7 +1563,7 @@ ExprNode* CloneExpr (ExprNode* Expr)
            Clone->V.Sym = Expr->V.Sym;
            break;
 
-       case EXPR_SEGMENT:
+       case EXPR_SECTION:
            Clone->V.SegNum = Expr->V.SegNum;
            break;
 
@@ -1545,7 +1606,7 @@ void WriteExpr (ExprNode* Expr)
            ObjWrite16 (GetSymIndex (Expr->V.Sym));
            break;
 
-        case EXPR_SEGMENT:
+        case EXPR_SECTION:
            ObjWrite8 (Expr->V.SegNum);
            break;