]> git.sur5r.net Git - cc65/blobdiff - src/ca65/pseudo.c
Allow optional assignments in .export and .exportzp statements.
[cc65] / src / ca65 / pseudo.c
index d0aaff6dfea43580c9dbc299421a7bd4c559ca94..e7e5d1a6eaec6edbf568786f1ad9d1dcb385d83d 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998-2004 Ullrich von Bassewitz                                       */
-/*               Römerstraße 52                                              */
-/*               D-70794 Filderstadt                                         */
-/* EMail:        uz@cc65.org                                                 */
+/* (C) 1998-2007, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
@@ -165,6 +165,33 @@ static void SetBoolOption (unsigned char* Flag)
 
 
 
+static void ExportWithAssign (SymEntry* Sym, unsigned char AddrSize, unsigned Flags)
+/* Allow to assign the value of an export in an .export statement */
+{
+    /* The name and optional address size spec may be followed by an assignment
+     * or equal token.
+     */
+    if (Tok == TOK_ASSIGN || Tok == TOK_EQ) {
+
+        /* Assignment means the symbol is a label */
+        if (Tok == TOK_ASSIGN) {
+            Flags |= SF_LABEL;
+        }
+
+        /* Skip the assignment token */
+        NextTok ();
+
+        /* Define the symbol with the expression following the '=' */
+        SymDef (Sym, Expression(), ADDR_SIZE_DEFAULT, Flags);
+
+    }
+
+    /* Now export the symbol */
+    SymExport (Sym, AddrSize, Flags);
+}
+
+
+
 static void ExportImport (void (*Func) (SymEntry*, unsigned char, unsigned),
                           unsigned char DefAddrSize, unsigned Flags)
 /* Export or import symbols */
@@ -384,8 +411,8 @@ static void DoAssert (void)
         "ERROR"
     };
 
-    int Action;
-
+    int      Action;
+    unsigned Msg;
 
     /* First we have the expression that has to evaluated */
     ExprNode* Expr = Expression ();
@@ -414,15 +441,36 @@ static void DoAssert (void)
             Error ("Illegal assert action specifier");
     }
     NextTok ();
-    ConsumeComma ();
 
-    /* Read the message */
-    if (Tok != TOK_STRCON) {
-       ErrorSkip ("String constant expected");
-    } else {
-        AddAssertion (Expr, Action, GetStringId (SVal));
+    /* We can have an optional message. If no message is present, use
+     * "Assertion failed".
+     */
+    if (Tok == TOK_COMMA) {
+
+        /* Skip the comma */
         NextTok ();
+
+        /* Read the message */
+        if (Tok != TOK_STRCON) {
+            ErrorSkip ("String constant expected");
+            return;
+        }
+
+        /* Translate the message into a string id. We can then skip the input
+         * string.
+         */
+        Msg = GetStringId (SVal);
+        NextTok ();
+
+    } else {
+
+        /* Use "Assertion failed" */
+        Msg = GetStringId ("Assertion failed");
+
     }
+
+    /* Remember the assertion */
+    AddAssertion (Expr, Action, Msg);
 }
 
 
@@ -456,7 +504,7 @@ static void DoByte (void)
        } else {
            EmitByte (Expression ());
        }
-       if (Tok != TOK_COMMA) {
+       if (Tok != TOK_COMMA) {
            break;
        } else {
            NextTok ();
@@ -488,7 +536,7 @@ static void DoCharMap (void)
 
     /* Read the index as numerical value */
     Index = ConstExpression ();
-    if (Index < 1 || Index > 255) {
+    if (Index < 0 || Index > 255) {
        /* Value out of range */
                ErrorSkip ("Range error");
        return;
@@ -499,10 +547,10 @@ static void DoCharMap (void)
 
     /* Read the character code */
     Code = ConstExpression ();
-    if (Code < 1 || Code > 255) {
-       /* Value out of range */
+    if (Code < 0 || Code > 255) {
+       /* Value out of range */
                ErrorSkip ("Range error");
-       return;
+       return;
     }
 
     /* Set the character translation */
@@ -761,7 +809,7 @@ static void DoExitMacro (void)
 static void DoExport (void)
 /* Export a symbol */
 {
-    ExportImport (SymExport, ADDR_SIZE_DEFAULT, SF_NONE);
+    ExportImport (ExportWithAssign, ADDR_SIZE_DEFAULT, SF_NONE);
 }
 
 
@@ -769,7 +817,7 @@ static void DoExport (void)
 static void DoExportZP (void)
 /* Export a zeropage symbol */
 {
-    ExportImport (SymExport, ADDR_SIZE_ZP, SF_NONE);
+    ExportImport (ExportWithAssign, ADDR_SIZE_ZP, SF_NONE);
 }
 
 
@@ -1085,15 +1133,11 @@ Done:
 static void DoInclude (void)
 /* Include another file */
 {
-    char Name [MAX_STR_LEN+1];
-
     /* Name must follow */
     if (Tok != TOK_STRCON) {
        ErrorSkip ("String constant expected");
     } else {
-       strcpy (Name, SVal);
-       NextTok ();
-       NewInputFile (Name);
+       NewInputFile (SVal);
     }
 }
 
@@ -1185,14 +1229,6 @@ static void DoLocalChar (void)
 static void DoMacPack (void)
 /* Insert a macro package */
 {
-    /* Macro package names */
-    static const char* Keys [] = {
-       "GENERIC",
-               "LONGBRANCH",
-        "CBM",
-        "CPU"
-    };
-
     int Package;
 
     /* We expect an identifier */
@@ -1201,19 +1237,16 @@ static void DoMacPack (void)
        return;
     }
 
-    /* Map the keyword to a number */
-    Package = GetSubKey (Keys, sizeof (Keys) / sizeof (Keys [0]));
+    /* Search for the macro package name */
+    Package = MacPackFind (SVal);
     if (Package < 0) {
        /* Not found */
        ErrorSkip ("Invalid macro package");
        return;
     }
 
-    /* Skip the package name */
-    NextTok ();
-
     /* Insert the package */
-    InsertMacPack (Package);
+    MacPackInsert (Package);
 }
 
 
@@ -1242,7 +1275,7 @@ static void DoOrg (void)
        Error ("Range error");
        return;
     }
-    SetAbsPC (PC);
+    EnterAbsoluteMode (PC);
 }
 
 
@@ -1385,7 +1418,7 @@ static void DoPushSeg (void)
 static void DoReloc (void)
 /* Enter relocatable mode */
 {
-    RelocMode = 1;
+    EnterRelocMode ();
 }
 
 
@@ -1699,6 +1732,7 @@ static CtrlDesc CtrlCmdTab [] = {
     { ccNone,          DoUnexpected    },      /* .HIWORD */
     { ccNone,          DoI16           },
     { ccNone,          DoI8            },
+    { ccNone,           DoUnexpected    },      /* .IDENT */
     { ccKeepToken,     DoConditionals  },      /* .IF */
     { ccKeepToken,     DoConditionals  },      /* .IFBLANK */
     { ccKeepToken,     DoConditionals  },      /* .IFCONST */
@@ -1749,9 +1783,11 @@ static CtrlDesc CtrlCmdTab [] = {
     { ccNone,          DoROData        },
     { ccNone,           DoScope         },
     { ccNone,          DoSegment       },
+    { ccNone,           DoUnexpected    },      /* .SET */
     { ccNone,                  DoSetCPU        },
     { ccNone,           DoUnexpected    },      /* .SIZEOF */
     { ccNone,          DoSmart         },
+    { ccNone,           DoUnexpected    },      /* .SPRINTF */
     { ccNone,          DoUnexpected    },      /* .STRAT */
     { ccNone,                  DoUnexpected    },      /* .STRING */
     { ccNone,          DoUnexpected    },      /* .STRLEN */