Started to add a new .BANK instruction that allows access to a memory area

attribute named "bank". Some error checks and a lot of testing is required.
Don't use for now.


git-svn-id: svn://svn.cc65.org/cc65/trunk@5375 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
uz
2012-01-03 21:41:34 +00:00
parent 74be82e77e
commit 9e68be3842
18 changed files with 325 additions and 97 deletions

View File

@@ -6,7 +6,7 @@
/* */
/* */
/* */
/* (C) 1998-2011, Ullrich von Bassewitz */
/* (C) 1998-2012, Ullrich von Bassewitz */
/* Roemerstrasse 52 */
/* D-70794 Filderstadt */
/* EMail: uz@cc65.org */
@@ -65,7 +65,7 @@
/*****************************************************************************/
/* Data */
/* Data */
/*****************************************************************************/
@@ -238,6 +238,19 @@ static ExprNode* HiByte (ExprNode* Operand)
static ExprNode* Bank (ExprNode* Operand)
/* Return the bank of the given segmented expression */
{
/* Generate the bank expression */
ExprNode* Expr = NewExprNode (EXPR_BANKRAW);
Expr->Left = Operand;
/* Return the result */
return Expr;
}
static ExprNode* BankByte (ExprNode* Operand)
/* Return the bank byte of the given expression */
{
@@ -321,6 +334,14 @@ static ExprNode* Symbol (SymEntry* S)
ExprNode* FuncBank (void)
/* Handle the .BANK builtin function */
{
return Bank (Expression ());
}
ExprNode* FuncBankByte (void)
/* Handle the .BANKBYTE builtin function */
{
@@ -446,7 +467,7 @@ static ExprNode* DoMatch (enum TC EqualityLevel)
if (TokIsSep (CurTok.Tok)) {
Error ("Unexpected end of line");
return GenLiteral0 ();
}
}
/* Get a node with this token */
Node = NewTokNode ();
@@ -923,7 +944,8 @@ static ExprNode* Factor (void)
N = HiByte (Factor ());
break;
case TOK_BANK:
case TOK_XOR:
/* ^ means the bank byte of an expression */
NextTok ();
N = BankByte (Factor ());
break;
@@ -934,6 +956,10 @@ static ExprNode* Factor (void)
ConsumeRParen ();
break;
case TOK_BANK:
N = Function (FuncBank);
break;
case TOK_BANKBYTE:
N = Function (FuncBankByte);
break;
@@ -1532,6 +1558,16 @@ static ExprNode* GenSectionExpr (unsigned SecNum)
static ExprNode* GenBankExpr (unsigned SecNum)
/* Return an expression node for the given bank */
{
ExprNode* Expr = NewExprNode (EXPR_BANK);
Expr->V.SecNum = SecNum;
return Expr;
}
ExprNode* GenAddExpr (ExprNode* Left, ExprNode* Right)
/* Generate an addition from the two operands */
{
@@ -1739,6 +1775,10 @@ ExprNode* CloneExpr (ExprNode* Expr)
Clone = GenSectionExpr (Expr->V.SecNum);
break;
case EXPR_BANK:
Clone = GenBankExpr (Expr->V.SecNum);
break;
default:
/* Generate a new node */
Clone = NewExprNode (Expr->Op);
@@ -1754,6 +1794,70 @@ ExprNode* CloneExpr (ExprNode* Expr)
ExprNode* FinalizeExpr (ExprNode* Expr, const Collection* LineInfos)
/* Finalize an expression tree before it is written to the file. This will
* replace EXPR_BANKRAW nodes by EXPR_BANK nodes, and replace constant
* expressions by their result. The LineInfos are used when diagnosing errors.
* Beware: The expression tree may get replaced in future versions, so don't
* use Expr after calling this function.
*/
{
ExprDesc ED;
/* Check the type code */
switch (EXPR_NODETYPE (Expr->Op)) {
case EXPR_LEAFNODE:
/* Nothing to do for leaf nodes */
break;
case EXPR_BINARYNODE:
Expr->Left = FinalizeExpr (Expr->Left, LineInfos);
Expr->Right = FinalizeExpr (Expr->Right, LineInfos);
/* FALLTHROUGH */
case EXPR_UNARYNODE:
Expr->Left = FinalizeExpr (Expr->Left, LineInfos);
/* Special handling for BANKRAW */
if (Expr->Op == EXPR_BANKRAW) {
/* Study the expression */
ED_Init (&ED);
StudyExpr (Expr->Left, &ED);
/* The expression must be ok and must have exactly one segment
* reference.
*/
if (ED.Flags & ED_TOO_COMPLEX) {
LIError (LineInfos,
"Cannot evaluate expression");
} else if (ED.SecCount == 0) {
LIError (LineInfos,
".BANK expects a segment reference");
} else if (ED.SecCount > 1 || ED.SecRef[0].Count > 1) {
LIError (LineInfos,
"Too many segment references in argument to .BANK");
} else {
FreeExpr (Expr->Left);
Expr->Op = EXPR_BANK;
Expr->Left = 0;
Expr->V.SecNum = ED.SecRef[0].Ref;
}
/* Cleanup */
ED_Done (&ED);
}
break;
}
/* Return the (partial) tree */
return Expr;
}
void WriteExpr (ExprNode* Expr)
/* Write the given expression to the object file */
{
@@ -1784,13 +1888,18 @@ void WriteExpr (ExprNode* Expr)
case EXPR_SECTION:
ObjWrite8 (EXPR_SECTION);
ObjWrite8 (Expr->V.SecNum);
ObjWriteVar (Expr->V.SecNum);
break;
case EXPR_ULABEL:
WriteExpr (ULabResolve (Expr->V.IVal));
break;
case EXPR_BANK:
ObjWrite8 (EXPR_BANK);
ObjWriteVar (Expr->V.SecNum);
break;
default:
/* Not a leaf node */
ObjWrite8 (Expr->Op);
@@ -1818,7 +1927,7 @@ void ExprGuessedAddrSize (const ExprNode* Expr, unsigned char AddrSize)
}
/* Check the type code */
switch (Expr->Op & EXPR_TYPEMASK) {
switch (EXPR_NODETYPE (Expr->Op)) {
case EXPR_LEAFNODE:
if (Expr->Op == EXPR_SYMBOL) {

View File

@@ -6,7 +6,7 @@
/* */
/* */
/* */
/* (C) 1998-2011, Ullrich von Bassewitz */
/* (C) 1998-2012, Ullrich von Bassewitz */
/* Roemerstrasse 52 */
/* D-70794 Filderstadt */
/* EMail: uz@cc65.org */
@@ -39,6 +39,7 @@
/* common */
#include "coll.h"
#include "exprdefs.h"
@@ -140,6 +141,14 @@ ExprNode* CloneExpr (ExprNode* Expr);
* nodes, it will not resolve them.
*/
ExprNode* FinalizeExpr (ExprNode* Expr, const Collection* LineInfos);
/* Finalize an expression tree before it is written to the file. This will
* replace EXPR_BANKRAW nodes by EXPR_BANK nodes, and replace constant
* expressions by their result. The LineInfos are used when diagnosing errors.
* Beware: The expression tree may get replaced in future versions, so don't
* use Expr after calling this function.
*/
void WriteExpr (ExprNode* Expr);
/* Write the given expression to the object file */

View File

@@ -1061,7 +1061,7 @@ int main (int argc, char* argv [])
SegDone ();
}
/* If we didn't have any errors, check the assertions */
/* If we didn't have any errors, check the assertions */
if (ErrorCount == 0) {
CheckAssertions ();
}

View File

@@ -1990,6 +1990,7 @@ static CtrlDesc CtrlCmdTab [] = {
{ ccNone, DoASCIIZ },
{ ccNone, DoAssert },
{ ccNone, DoAutoImport },
{ ccNone, DoUnexpected }, /* .BANK */
{ ccNone, DoUnexpected }, /* .BANKBYTE */
{ ccNone, DoBankBytes },
{ ccNone, DoUnexpected }, /* .BLANK */

View File

@@ -6,7 +6,7 @@
/* */
/* */
/* */
/* (C) 1998-2011, Ullrich von Bassewitz */
/* (C) 1998-2012, Ullrich von Bassewitz */
/* Roemerstrasse 52 */
/* D-70794 Filderstadt */
/* EMail: uz@cc65.org */
@@ -141,6 +141,7 @@ struct DotKeyword {
{ ".ASCIIZ", TOK_ASCIIZ },
{ ".ASSERT", TOK_ASSERT },
{ ".AUTOIMPORT", TOK_AUTOIMPORT },
{ ".BANK", TOK_BANK },
{ ".BANKBYTE", TOK_BANKBYTE },
{ ".BANKBYTES", TOK_BANKBYTES },
{ ".BITAND", TOK_AND },

View File

@@ -407,8 +407,11 @@ void SegDone (void)
} else {
/* Finalize the expression */
F->V.Expr = FinalizeExpr (F->V.Expr, &F->LI);
/* Simplify the expression */
F->V.Expr = SimplifyExpr (F->V.Expr, &ED);
/* ### F->V.Expr = SimplifyExpr (F->V.Expr, &ED); */
/* We cannot evaluate the expression now, leave the job for
* the linker. However, we can check if the address size

View File

@@ -1095,6 +1095,33 @@ static void StudyBoolNot (ExprNode* Expr, ExprDesc* D)
static void StudyBank (ExprNode* Expr, ExprDesc* D)
/* Study an EXPR_BANK expression node */
{
/* Get the section reference */
ED_SecRef* SecRef = ED_GetSecRef (D, Expr->V.SecNum);
/* Update the data and the address size */
++SecRef->Count;
/* The expression is always linker evaluated, so invalidate it */
ED_Invalidate (D);
}
static void StudyBankRaw (ExprNode* Expr, ExprDesc* D)
/* Study an EXPR_BANKRAW expression node */
{
/* Study the expression extracting section references */
StudyExprInternal (Expr->Left, D);
/* The expression is always linker evaluated, so invalidate it */
ED_Invalidate (D);
}
static void StudyByte0 (ExprNode* Expr, ExprDesc* D)
/* Study an EXPR_BYTE0 expression node */
{
@@ -1231,6 +1258,10 @@ static void StudyExprInternal (ExprNode* Expr, ExprDesc* D)
StudyULabel (Expr, D);
break;
case EXPR_BANK:
StudyBank (Expr, D);
break;
case EXPR_PLUS:
StudyPlus (Expr, D);
break;
@@ -1331,6 +1362,10 @@ static void StudyExprInternal (ExprNode* Expr, ExprDesc* D)
StudyBoolNot (Expr, D);
break;
case EXPR_BANKRAW:
StudyBankRaw (Expr, D);
break;
case EXPR_BYTE0:
StudyByte0 (Expr, D);
break;

View File

@@ -6,7 +6,7 @@
/* */
/* */
/* */
/* (C) 2007-2011, Ullrich von Bassewitz */
/* (C) 2007-2012, Ullrich von Bassewitz */
/* Roemerstrasse 52 */
/* D-70794 Filderstadt */
/* EMail: uz@cc65.org */
@@ -90,9 +90,8 @@ typedef enum token_t {
TOK_STAR = TOK_MUL, /* Alias */
TOK_DIV, /* / */
TOK_MOD, /* ! */
TOK_OR, /* | */
TOK_OR, /* | */
TOK_XOR, /* ^ */
TOK_BANK = TOK_XOR, /* Alias */
TOK_AND, /* & */
TOK_SHL, /* << */
TOK_SHR, /* >> */
@@ -128,6 +127,7 @@ typedef enum token_t {
TOK_ASCIIZ,
TOK_ASSERT,
TOK_AUTOIMPORT,
TOK_BANK,
TOK_BANKBYTE,
TOK_BANKBYTES,
TOK_BLANK,