Much extended StudyExpr
git-svn-id: svn://svn.cc65.org/cc65/trunk@2681 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
@@ -1132,6 +1132,8 @@ long ConstExpression (void)
|
|||||||
* not constant.
|
* not constant.
|
||||||
*/
|
*/
|
||||||
{
|
{
|
||||||
|
long Val;
|
||||||
|
|
||||||
#if 1
|
#if 1
|
||||||
/* Read the expression */
|
/* Read the expression */
|
||||||
ExprNode* Expr = Expr0 ();
|
ExprNode* Expr = Expr0 ();
|
||||||
@@ -1142,18 +1144,23 @@ long ConstExpression (void)
|
|||||||
|
|
||||||
/* Study the expression */
|
/* Study the expression */
|
||||||
ExprDesc D;
|
ExprDesc D;
|
||||||
InitExprDesc (&D);
|
ED_Init (&D);
|
||||||
StudyExpr (Expr, &D, 1);
|
StudyExpr (Expr, &D);
|
||||||
|
|
||||||
/* Check if the expression is constant */
|
/* Check if the expression is constant */
|
||||||
if (!ExprDescIsConst (&D)) {
|
if (ED_IsConst (&D)) {
|
||||||
|
Val = D.Val;
|
||||||
|
} else {
|
||||||
Error ("Constant expression expected");
|
Error ("Constant expression expected");
|
||||||
D.Val = 0;
|
Val = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Free the expression tree and return the value */
|
/* Free the expression tree and allocated memory for D */
|
||||||
FreeExpr (Expr);
|
FreeExpr (Expr);
|
||||||
return D.Val;
|
ED_Done (&D);
|
||||||
|
|
||||||
|
/* Return the value */
|
||||||
|
return Val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1177,17 +1184,20 @@ ExprNode* SimplifyExpr (ExprNode* Expr)
|
|||||||
|
|
||||||
/* Create an expression description and initialize it */
|
/* Create an expression description and initialize it */
|
||||||
ExprDesc D;
|
ExprDesc D;
|
||||||
InitExprDesc (&D);
|
ED_Init (&D);
|
||||||
|
|
||||||
/* Study the expression */
|
/* Study the expression */
|
||||||
StudyExpr (Expr, &D, 1);
|
StudyExpr (Expr, &D);
|
||||||
|
|
||||||
/* Now check if we can generate a literal value */
|
/* Now check if we can generate a literal value */
|
||||||
if (ExprDescIsConst (&D)) {
|
if (ED_IsConst (&D)) {
|
||||||
/* No external references */
|
/* No external references */
|
||||||
FreeExpr (Expr);
|
FreeExpr (Expr);
|
||||||
Expr = GenLiteralExpr (D.Val);
|
Expr = GenLiteralExpr (D.Val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Free allocated memory */
|
||||||
|
ED_Done (&D);
|
||||||
}
|
}
|
||||||
return Expr;
|
return Expr;
|
||||||
}
|
}
|
||||||
@@ -1381,20 +1391,22 @@ int IsConstExpr (ExprNode* Expr, long* Val)
|
|||||||
* expression is constant, the constant value is stored here.
|
* expression is constant, the constant value is stored here.
|
||||||
*/
|
*/
|
||||||
{
|
{
|
||||||
|
int IsConst;
|
||||||
|
|
||||||
/* Study the expression */
|
/* Study the expression */
|
||||||
ExprDesc D;
|
ExprDesc D;
|
||||||
InitExprDesc (&D);
|
ED_Init (&D);
|
||||||
StudyExpr (Expr, &D, 1);
|
StudyExpr (Expr, &D);
|
||||||
|
|
||||||
/* Check if the expression is constant */
|
/* Check if the expression is constant */
|
||||||
if (ExprDescIsConst (&D)) {
|
IsConst = ED_IsConst (&D);
|
||||||
if (Val) {
|
if (IsConst && Val != 0) {
|
||||||
*Val = D.Val;
|
*Val = D.Val;
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Delete allocated memory and return the result */
|
||||||
|
ED_Done (&D);
|
||||||
|
return IsConst;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1452,16 +1464,10 @@ static void CheckAddrSize (const ExprNode* N, unsigned char* AddrSize)
|
|||||||
|
|
||||||
case EXPR_WORD0:
|
case EXPR_WORD0:
|
||||||
case EXPR_WORD1:
|
case EXPR_WORD1:
|
||||||
case EXPR_FORCEWORD:
|
|
||||||
/* No need to look at the expression */
|
/* No need to look at the expression */
|
||||||
*AddrSize = ADDR_SIZE_ABS;
|
*AddrSize = ADDR_SIZE_ABS;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case EXPR_FORCEFAR:
|
|
||||||
/* No need to look at the expression */
|
|
||||||
*AddrSize = ADDR_SIZE_FAR;
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
CheckAddrSize (N->Left, AddrSize);
|
CheckAddrSize (N->Left, AddrSize);
|
||||||
break;
|
break;
|
||||||
|
|||||||
1398
src/ca65/studyexpr.c
1398
src/ca65/studyexpr.c
File diff suppressed because it is too large
Load Diff
@@ -49,20 +49,61 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Flags */
|
||||||
|
#define ED_OK 0x00 /* Nothing special */
|
||||||
|
#define ED_TOO_COMPLEX 0x01 /* Expression is too complex */
|
||||||
|
|
||||||
|
/* Symbol reference */
|
||||||
|
typedef struct ED_SymRef ED_SymRef;
|
||||||
|
struct ED_SymRef {
|
||||||
|
long Count; /* Number of references */
|
||||||
|
struct SymEntry* Ref; /* Actual reference */
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Section reference */
|
||||||
|
typedef struct ED_SecRef ED_SecRef;
|
||||||
|
struct ED_SecRef {
|
||||||
|
long Count; /* Number of references */
|
||||||
|
unsigned Ref; /* Actual reference */
|
||||||
|
};
|
||||||
|
|
||||||
/* Structure for parsing expression trees */
|
/* Structure for parsing expression trees */
|
||||||
typedef struct ExprDesc ExprDesc;
|
typedef struct ExprDesc ExprDesc;
|
||||||
struct ExprDesc {
|
struct ExprDesc {
|
||||||
|
unsigned short Flags; /* See ED_xxx */
|
||||||
|
unsigned char AddrSize; /* Address size of the expression */
|
||||||
long Val; /* The offset value */
|
long Val; /* The offset value */
|
||||||
long Left; /* Left value for StudyBinaryExpr */
|
long Right; /* Right value for StudyBinaryExpr */
|
||||||
int TooComplex; /* Expression is too complex to evaluate */
|
|
||||||
long SymCount; /* Symbol reference count */
|
/* Symbol reference management */
|
||||||
long SecCount; /* Section reference count */
|
unsigned SymCount; /* Number of symbols referenced */
|
||||||
struct SymEntry* SymRef; /* Symbol reference if any */
|
unsigned SymLimit; /* Memory allocated */
|
||||||
unsigned SecRef; /* Section reference if any */
|
ED_SymRef* SymRef; /* Symbol references */
|
||||||
|
|
||||||
|
/* Section reference management */
|
||||||
|
unsigned SecCount; /* Number of sections referenced */
|
||||||
|
unsigned SecLimit; /* Memory allocated */
|
||||||
|
ED_SecRef* SecRef; /* Section references */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*****************************************************************************/
|
||||||
|
/* struct ExprDesc */
|
||||||
|
/*****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
ExprDesc* ED_Init (ExprDesc* ED);
|
||||||
|
/* Initialize an ExprDesc structure for use with StudyExpr */
|
||||||
|
|
||||||
|
void ED_Done (ExprDesc* ED);
|
||||||
|
/* Delete allocated memory for an ExprDesc. */
|
||||||
|
|
||||||
|
int ED_IsConst (const ExprDesc* ED);
|
||||||
|
/* Return true if the expression is constant */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
/* Code */
|
/* Code */
|
||||||
@@ -70,13 +111,7 @@ struct ExprDesc {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
ExprDesc* InitExprDesc (ExprDesc* ED);
|
void StudyExpr (ExprNode* Expr, ExprDesc* D);
|
||||||
/* Initialize an ExprDesc structure for use with StudyExpr */
|
|
||||||
|
|
||||||
int ExprDescIsConst (const ExprDesc* ED);
|
|
||||||
/* Return true if the expression is constant */
|
|
||||||
|
|
||||||
void StudyExpr (ExprNode* Expr, ExprDesc* D, int Sign);
|
|
||||||
/* Study an expression tree and place the contents into D */
|
/* Study an expression tree and place the contents into D */
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user