mirror of
https://github.com/cc65/cc65.git
synced 2024-11-19 06:31:31 +00:00
Much extended StudyExpr
git-svn-id: svn://svn.cc65.org/cc65/trunk@2681 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
parent
f99049f84c
commit
49f72c6d24
@ -1132,6 +1132,8 @@ long ConstExpression (void)
|
||||
* not constant.
|
||||
*/
|
||||
{
|
||||
long Val;
|
||||
|
||||
#if 1
|
||||
/* Read the expression */
|
||||
ExprNode* Expr = Expr0 ();
|
||||
@ -1142,18 +1144,23 @@ long ConstExpression (void)
|
||||
|
||||
/* Study the expression */
|
||||
ExprDesc D;
|
||||
InitExprDesc (&D);
|
||||
StudyExpr (Expr, &D, 1);
|
||||
ED_Init (&D);
|
||||
StudyExpr (Expr, &D);
|
||||
|
||||
/* Check if the expression is constant */
|
||||
if (!ExprDescIsConst (&D)) {
|
||||
if (ED_IsConst (&D)) {
|
||||
Val = D.Val;
|
||||
} else {
|
||||
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);
|
||||
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 */
|
||||
ExprDesc D;
|
||||
InitExprDesc (&D);
|
||||
ED_Init (&D);
|
||||
|
||||
/* Study the expression */
|
||||
StudyExpr (Expr, &D, 1);
|
||||
StudyExpr (Expr, &D);
|
||||
|
||||
/* Now check if we can generate a literal value */
|
||||
if (ExprDescIsConst (&D)) {
|
||||
if (ED_IsConst (&D)) {
|
||||
/* No external references */
|
||||
FreeExpr (Expr);
|
||||
Expr = GenLiteralExpr (D.Val);
|
||||
}
|
||||
|
||||
/* Free allocated memory */
|
||||
ED_Done (&D);
|
||||
}
|
||||
return Expr;
|
||||
}
|
||||
@ -1381,20 +1391,22 @@ int IsConstExpr (ExprNode* Expr, long* Val)
|
||||
* expression is constant, the constant value is stored here.
|
||||
*/
|
||||
{
|
||||
int IsConst;
|
||||
|
||||
/* Study the expression */
|
||||
ExprDesc D;
|
||||
InitExprDesc (&D);
|
||||
StudyExpr (Expr, &D, 1);
|
||||
ED_Init (&D);
|
||||
StudyExpr (Expr, &D);
|
||||
|
||||
/* Check if the expression is constant */
|
||||
if (ExprDescIsConst (&D)) {
|
||||
if (Val) {
|
||||
IsConst = ED_IsConst (&D);
|
||||
if (IsConst && Val != 0) {
|
||||
*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_WORD1:
|
||||
case EXPR_FORCEWORD:
|
||||
/* No need to look at the expression */
|
||||
*AddrSize = ADDR_SIZE_ABS;
|
||||
break;
|
||||
|
||||
case EXPR_FORCEFAR:
|
||||
/* No need to look at the expression */
|
||||
*AddrSize = ADDR_SIZE_FAR;
|
||||
break;
|
||||
|
||||
default:
|
||||
CheckAddrSize (N->Left, AddrSize);
|
||||
break;
|
||||
|
1382
src/ca65/studyexpr.c
1382
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 */
|
||||
typedef struct ExprDesc ExprDesc;
|
||||
struct ExprDesc {
|
||||
unsigned short Flags; /* See ED_xxx */
|
||||
unsigned char AddrSize; /* Address size of the expression */
|
||||
long Val; /* The offset value */
|
||||
long Left; /* Left value for StudyBinaryExpr */
|
||||
int TooComplex; /* Expression is too complex to evaluate */
|
||||
long SymCount; /* Symbol reference count */
|
||||
long SecCount; /* Section reference count */
|
||||
struct SymEntry* SymRef; /* Symbol reference if any */
|
||||
unsigned SecRef; /* Section reference if any */
|
||||
long Right; /* Right value for StudyBinaryExpr */
|
||||
|
||||
/* Symbol reference management */
|
||||
unsigned SymCount; /* Number of symbols referenced */
|
||||
unsigned SymLimit; /* Memory allocated */
|
||||
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 */
|
||||
@ -70,13 +111,7 @@ struct ExprDesc {
|
||||
|
||||
|
||||
|
||||
ExprDesc* InitExprDesc (ExprDesc* ED);
|
||||
/* 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);
|
||||
void StudyExpr (ExprNode* Expr, ExprDesc* D);
|
||||
/* Study an expression tree and place the contents into D */
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user