/* */
/* */
/* */
-/* (C) 1998-2003 Ullrich von Bassewitz */
-/* Römerstraße 52 */
-/* D-70794 Filderstadt */
-/* EMail: uz@cc65.org */
+/* (C) 1998-2011, Ullrich von Bassewitz */
+/* Roemerstrasse 52 */
+/* D-70794 Filderstadt */
+/* EMail: uz@cc65.org */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
#include "exprdefs.h"
#include "print.h"
#include "shift.h"
+#include "strbuf.h"
#include "tgttrans.h"
#include "version.h"
#include "xmalloc.h"
#include "nexttok.h"
#include "objfile.h"
#include "segment.h"
+#include "sizeof.h"
+#include "studyexpr.h"
#include "symbol.h"
#include "symtab.h"
#include "toklist.h"
*/
#define MAX_FREE_NODES 64
static ExprNode* FreeExprNodes = 0;
-static unsigned FreeNodeCount = 0;
-
-/* Structure for parsing expression trees */
-typedef struct ExprDesc ExprDesc;
-struct ExprDesc {
- 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 */
- SymEntry* SymRef; /* Symbol reference if any */
- unsigned SecRef; /* Section reference if any */
-};
-
+static unsigned FreeNodeCount = 0;
/*****************************************************************************/
-/* Helpers */
+/* Helpers */
/*****************************************************************************/
-static ExprDesc* InitExprDesc (ExprDesc* ED)
-/* Initialize an ExprDesc structure for use with StudyExpr */
-{
- ED->Val = 0;
- ED->TooComplex = 0;
- ED->SymCount = 0;
- ED->SecCount = 0;
- return ED;
-}
-
-
-
-static int ExprDescIsConst (const ExprDesc* ED)
-/* Return true if the expression is constant */
-{
- return (ED->TooComplex == 0 && ED->SymCount == 0 && ED->SecCount == 0);
-}
-
-
-
static ExprNode* NewExprNode (unsigned Op)
/* Create a new expression node */
{
int IsWordRange (long Val)
/* Return true if this is a word value */
{
- return (Val & ~0xFFFF) == 0;
+ return (Val & ~0xFFFFL) == 0;
+}
+
+
+
+int IsFarRange (long Val)
+/* Return true if this is a far (24 bit) value */
+{
+ return (Val & ~0xFFFFFFL) == 0;
}
/* Symbols resolved, check for a literal */
if (E->Op == EXPR_LITERAL) {
if (Val) {
- *Val = E->V.Val;
+ *Val = E->V.IVal;
}
return 1;
}
-static int FuncBlank (void)
+static ExprNode* LoByte (ExprNode* Operand)
+/* Return the low byte of the given expression */
+{
+ ExprNode* Expr;
+ long Val;
+
+ /* Special handling for const expressions */
+ if (IsEasyConst (Operand, &Val)) {
+ FreeExpr (Operand);
+ Expr = GenLiteralExpr (Val & 0xFF);
+ } else {
+ /* Extract byte #0 */
+ Expr = NewExprNode (EXPR_BYTE0);
+ Expr->Left = Operand;
+ }
+ return Expr;
+}
+
+
+
+static ExprNode* HiByte (ExprNode* Operand)
+/* Return the high byte of the given expression */
+{
+ ExprNode* Expr;
+ long Val;
+
+ /* Special handling for const expressions */
+ if (IsEasyConst (Operand, &Val)) {
+ FreeExpr (Operand);
+ Expr = GenLiteralExpr ((Val >> 8) & 0xFF);
+ } else {
+ /* Extract byte #1 */
+ Expr = NewExprNode (EXPR_BYTE1);
+ Expr->Left = Operand;
+ }
+ return Expr;
+}
+
+
+
+static ExprNode* BankByte (ExprNode* Operand)
+/* Return the bank byte of the given expression */
+{
+ ExprNode* Expr;
+ long Val;
+
+ /* Special handling for const expressions */
+ if (IsEasyConst (Operand, &Val)) {
+ FreeExpr (Operand);
+ Expr = GenLiteralExpr ((Val >> 16) & 0xFF);
+ } else {
+ /* Extract byte #2 */
+ Expr = NewExprNode (EXPR_BYTE2);
+ Expr->Left = Operand;
+ }
+ return Expr;
+}
+
+
+
+static ExprNode* LoWord (ExprNode* Operand)
+/* Return the low word of the given expression */
+{
+ ExprNode* Expr;
+ long Val;
+
+ /* Special handling for const expressions */
+ if (IsEasyConst (Operand, &Val)) {
+ FreeExpr (Operand);
+ Expr = GenLiteralExpr (Val & 0xFFFF);
+ } else {
+ /* Extract word #0 */
+ Expr = NewExprNode (EXPR_WORD0);
+ Expr->Left = Operand;
+ }
+ return Expr;
+}
+
+
+
+static ExprNode* HiWord (ExprNode* Operand)
+/* Return the high word of the given expression */
+{
+ ExprNode* Expr;
+ long Val;
+
+ /* Special handling for const expressions */
+ if (IsEasyConst (Operand, &Val)) {
+ FreeExpr (Operand);
+ Expr = GenLiteralExpr ((Val >> 16) & 0xFFFF);
+ } else {
+ /* Extract word #1 */
+ Expr = NewExprNode (EXPR_WORD1);
+ Expr->Left = Operand;
+ }
+ return Expr;
+}
+
+
+
+static ExprNode* Symbol (SymEntry* S)
+/* Reference a symbol and return an expression for it */
+{
+ if (S == 0) {
+ /* Some weird error happened before */
+ return GenLiteralExpr (0);
+ } else {
+ /* Mark the symbol as referenced */
+ SymRef (S);
+ /* If the symbol is a variable, return just its value, otherwise
+ * return a reference to the symbol.
+ */
+ if (SymIsVar (S)) {
+ return CloneExpr (GetSymExpr (S));
+ } else {
+ /* Create symbol node */
+ return GenSymExpr (S);
+ }
+ }
+}
+
+
+
+ExprNode* FuncBankByte (void)
+/* Handle the .BANKBYTE builtin function */
+{
+ return BankByte (Expression ());
+}
+
+
+
+static ExprNode* FuncBlank (void)
/* Handle the .BLANK builtin function */
{
- /* Assume no tokens if the closing brace follows (this is not correct in
- * all cases, since the token may be the closing brace, but this will
- * give a syntax error anyway and may not be handled by .BLANK.
+ /* We have a list of tokens that ends with the closing paren. Skip
+ * the tokens, and count them. Allow optionally curly braces.
*/
- if (Tok == TOK_RPAREN) {
- /* No tokens */
- return 1;
- } else {
- /* Skip any tokens */
- int Braces = 0;
- while (!TokIsSep (Tok)) {
- if (Tok == TOK_LPAREN) {
- ++Braces;
- } else if (Tok == TOK_RPAREN) {
- if (Braces == 0) {
- /* Done */
- break;
- } else {
- --Braces;
- }
- }
- NextTok ();
+ token_t Term = GetTokListTerm (TOK_RPAREN);
+ unsigned Count = 0;
+ while (CurTok.Tok != Term) {
+
+ /* 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 (CurTok.Tok)) {
+ break;
}
- return 0;
+
+ /* One more token */
+ ++Count;
+
+ /* Skip the token */
+ NextTok ();
+ }
+
+ /* If the list was enclosed in curly braces, skip the closing brace */
+ if (Term == TOK_RCURLY && CurTok.Tok == TOK_RCURLY) {
+ NextTok ();
}
+
+ /* Return true if the list was empty */
+ return GenLiteralExpr (Count == 0);
}
-static int FuncConst (void)
+static ExprNode* FuncConst (void)
/* Handle the .CONST builtin function */
{
/* Read an expression */
ExprNode* Expr = Expression ();
/* Check the constness of the expression */
- int Result = IsConstExpr (Expr, 0);
+ ExprNode* Result = GenLiteralExpr (IsConstExpr (Expr, 0));
/* Free the expression */
FreeExpr (Expr);
-static int FuncDefined (void)
+static ExprNode* FuncDefined (void)
/* Handle the .DEFINED builtin function */
{
/* Parse the symbol name and search for the symbol */
- SymEntry* Sym = ParseScopedSymName (SYM_FIND_EXISTING);
+ SymEntry* Sym = ParseAnySymName (SYM_FIND_EXISTING);
/* Check if the symbol is defined */
- return (Sym != 0 && SymIsDef (Sym));
+ return GenLiteralExpr (Sym != 0 && SymIsDef (Sym));
+}
+
+
+
+ExprNode* FuncHiByte (void)
+/* Handle the .HIBYTE builtin function */
+{
+ return HiByte (Expression ());
+}
+
+
+
+static ExprNode* FuncHiWord (void)
+/* Handle the .HIWORD builtin function */
+{
+ return HiWord (Expression ());
+}
+
+
+
+ExprNode* FuncLoByte (void)
+/* Handle the .LOBYTE builtin function */
+{
+ return LoByte (Expression ());
+}
+
+
+
+static ExprNode* FuncLoWord (void)
+/* Handle the .LOWORD builtin function */
+{
+ return LoWord (Expression ());
}
-static int DoMatch (enum TC EqualityLevel)
+static ExprNode* DoMatch (enum TC EqualityLevel)
/* Handle the .MATCH and .XMATCH builtin functions */
{
int Result;
TokNode* Root = 0;
TokNode* Last = 0;
- TokNode* Node = 0;
+ TokNode* Node;
/* A list of tokens follows. Read this list and remember it building a
* single linked list of tokens including attributes. The list is
- * terminated by a comma.
+ * either enclosed in curly braces, or terminated by a comma.
*/
- while (Tok != TOK_COMMA) {
+ token_t Term = GetTokListTerm (TOK_COMMA);
+ while (CurTok.Tok != Term) {
/* We may not end-of-line of end-of-file here */
- if (TokIsSep (Tok)) {
+ if (TokIsSep (CurTok.Tok)) {
Error ("Unexpected end of line");
- return 0;
+ return GenLiteral0 ();
}
/* Get a node with this token */
NextTok ();
}
- /* Skip the comma */
+ /* Skip the terminator token*/
NextTok ();
- /* Read the second list which is terminated by the right parenthesis and
- * compare each token against the one in the first list.
+ /* If the token list was enclosed in curly braces, we expect a comma */
+ if (Term == TOK_RCURLY) {
+ ConsumeComma ();
+ }
+
+ /* Read the second list which is optionally enclosed in curly braces and
+ * terminated by the right parenthesis. Compare each token against the
+ * one in the first list.
*/
+ Term = GetTokListTerm (TOK_RPAREN);
Result = 1;
Node = Root;
- while (Tok != TOK_RPAREN) {
+ while (CurTok.Tok != Term) {
/* We may not end-of-line of end-of-file here */
- if (TokIsSep (Tok)) {
+ if (TokIsSep (CurTok.Tok)) {
Error ("Unexpected end of line");
- return 0;
+ return GenLiteral0 ();
}
- /* Compare the tokens if the result is not already known */
+ /* Compare the tokens if the result is not already known */
if (Result != 0) {
if (Node == 0) {
/* The second list is larger than the first one */
NextTok ();
}
+ /* If the token list was enclosed in curly braces, eat the closing brace */
+ if (Term == TOK_RCURLY) {
+ NextTok ();
+ }
+
/* Check if there are remaining tokens in the first list */
if (Node != 0) {
Result = 0;
}
/* Done, return the result */
- return Result;
+ return GenLiteralExpr (Result);
}
-static int FuncMatch (void)
+static ExprNode* FuncMatch (void)
/* Handle the .MATCH function */
{
return DoMatch (tcSameToken);
-static int FuncReferenced (void)
+static ExprNode* FuncMax (void)
+/* Handle the .MAX function */
+{
+ ExprNode* Left;
+ ExprNode* Right;
+ ExprNode* Expr;
+ long LeftVal, RightVal;
+
+ /* Two arguments to the pseudo function */
+ Left = Expression ();
+ ConsumeComma ();
+ Right = Expression ();
+
+ /* Check if we can evaluate the value immediately */
+ if (IsEasyConst (Left, &LeftVal) && IsEasyConst (Right, &RightVal)) {
+ FreeExpr (Left);
+ FreeExpr (Right);
+ Expr = GenLiteralExpr ((LeftVal > RightVal)? LeftVal : RightVal);
+ } else {
+ /* Make an expression node */
+ Expr = NewExprNode (EXPR_MAX);
+ Expr->Left = Left;
+ Expr->Right = Right;
+ }
+ return Expr;
+}
+
+
+
+static ExprNode* FuncMin (void)
+/* Handle the .MIN function */
+{
+ ExprNode* Left;
+ ExprNode* Right;
+ ExprNode* Expr;
+ long LeftVal, RightVal;
+
+ /* Two arguments to the pseudo function */
+ Left = Expression ();
+ ConsumeComma ();
+ Right = Expression ();
+
+ /* Check if we can evaluate the value immediately */
+ if (IsEasyConst (Left, &LeftVal) && IsEasyConst (Right, &RightVal)) {
+ FreeExpr (Left);
+ FreeExpr (Right);
+ Expr = GenLiteralExpr ((LeftVal < RightVal)? LeftVal : RightVal);
+ } else {
+ /* Make an expression node */
+ Expr = NewExprNode (EXPR_MIN);
+ Expr->Left = Left;
+ Expr->Right = Right;
+ }
+ return Expr;
+}
+
+
+
+static ExprNode* FuncReferenced (void)
/* Handle the .REFERENCED builtin function */
{
/* Parse the symbol name and search for the symbol */
- SymEntry* Sym = ParseScopedSymName (SYM_FIND_EXISTING);
+ SymEntry* Sym = ParseAnySymName (SYM_FIND_EXISTING);
/* Check if the symbol is referenced */
- return (Sym != 0 && SymIsRef (Sym));
+ return GenLiteralExpr (Sym != 0 && SymIsRef (Sym));
+}
+
+
+
+static ExprNode* FuncSizeOf (void)
+/* Handle the .SIZEOF function */
+{
+ StrBuf ScopeName = STATIC_STRBUF_INITIALIZER;
+ StrBuf Name = STATIC_STRBUF_INITIALIZER;
+ SymTable* Scope;
+ SymEntry* Sym;
+ SymEntry* SizeSym;
+ long Size;
+ int NoScope;
+
+
+ /* Assume an error */
+ SizeSym = 0;
+
+ /* Check for a cheap local which needs special handling */
+ if (CurTok.Tok == TOK_LOCAL_IDENT) {
+
+ /* Cheap local symbol */
+ Sym = SymFindLocal (SymLast, &CurTok.SVal, SYM_FIND_EXISTING);
+ if (Sym == 0) {
+ Error ("Unknown symbol or scope: `%m%p'", &CurTok.SVal);
+ } else {
+ SizeSym = GetSizeOfSymbol (Sym);
+ }
+
+ /* Remember and skip SVal, terminate ScopeName so it is empty */
+ SB_Copy (&Name, &CurTok.SVal);
+ NextTok ();
+ SB_Terminate (&ScopeName);
+
+ } else {
+
+ /* Parse the scope and the name */
+ SymTable* ParentScope = ParseScopedIdent (&Name, &ScopeName);
+
+ /* Check if the parent scope is valid */
+ if (ParentScope == 0) {
+ /* No such scope */
+ SB_Done (&ScopeName);
+ SB_Done (&Name);
+ return GenLiteral0 ();
+ }
+
+ /* If ScopeName is empty, no explicit scope was specified. We have to
+ * search upper scope levels in this case.
+ */
+ NoScope = SB_IsEmpty (&ScopeName);
+
+ /* First search for a scope with the given name */
+ if (NoScope) {
+ Scope = SymFindAnyScope (ParentScope, &Name);
+ } else {
+ Scope = SymFindScope (ParentScope, &Name, SYM_FIND_EXISTING);
+ }
+
+ /* If we did find a scope with the name, read the symbol defining the
+ * size, otherwise search for a symbol entry with the name and scope.
+ */
+ if (Scope) {
+ /* Yep, it's a scope */
+ SizeSym = GetSizeOfScope (Scope);
+ } else {
+ if (NoScope) {
+ Sym = SymFindAny (ParentScope, &Name);
+ } else {
+ Sym = SymFind (ParentScope, &Name, SYM_FIND_EXISTING);
+ }
+
+ /* If we found the symbol retrieve the size, otherwise complain */
+ if (Sym) {
+ SizeSym = GetSizeOfSymbol (Sym);
+ } else {
+ Error ("Unknown symbol or scope: `%m%p%m%p'",
+ &ScopeName, &Name);
+ }
+ }
+ }
+
+ /* Check if we have a size */
+ if (SizeSym == 0 || !SymIsConst (SizeSym, &Size)) {
+ Error ("Size of `%m%p%m%p' is unknown", &ScopeName, &Name);
+ Size = 0;
+ }
+
+ /* Free the string buffers */
+ SB_Done (&ScopeName);
+ SB_Done (&Name);
+
+ /* Return the size */
+ return GenLiteralExpr (Size);
}
-static int FuncStrAt (void)
+static ExprNode* FuncStrAt (void)
/* Handle the .STRAT function */
{
- char Str [sizeof(SVal)];
+ StrBuf Str = STATIC_STRBUF_INITIALIZER;
long Index;
+ unsigned char C = 0;
/* String constant expected */
- if (Tok != TOK_STRCON) {
+ if (CurTok.Tok != TOK_STRCON) {
Error ("String constant expected");
NextTok ();
- return 0;
-
+ goto ExitPoint;
}
/* Remember the string and skip it */
- strcpy (Str, SVal);
+ SB_Copy (&Str, &CurTok.SVal);
NextTok ();
/* Comma must follow */
Index = ConstExpression ();
/* Must be a valid index */
- if (Index >= (long) strlen (Str)) {
+ if (Index >= (long) SB_GetLen (&Str)) {
Error ("Range error");
- return 0;
+ goto ExitPoint;
}
- /* Return the char, handle as unsigned. Be sure to translate it into
+ /* Get the char, handle as unsigned. Be sure to translate it into
* the target character set.
*/
- return (unsigned char) TgtTranslateChar (Str [(size_t)Index]);
+ C = TgtTranslateChar (SB_At (&Str, (unsigned)Index));
+
+ExitPoint:
+ /* Free string buffer memory */
+ SB_Done (&Str);
+
+ /* Return the char expression */
+ return GenLiteralExpr (C);
}
-static int FuncStrLen (void)
+static ExprNode* FuncStrLen (void)
/* Handle the .STRLEN function */
{
+ int Len;
+
/* String constant expected */
- if (Tok != TOK_STRCON) {
+ if (CurTok.Tok != TOK_STRCON) {
Error ("String constant expected");
/* Smart error recovery */
- if (Tok != TOK_RPAREN) {
+ if (CurTok.Tok != TOK_RPAREN) {
NextTok ();
}
- return 0;
+ Len = 0;
} else {
/* Get the length of the string */
- int Len = strlen (SVal);
+ Len = SB_GetLen (&CurTok.SVal);
/* Skip the string */
NextTok ();
-
- /* Return the length */
- return Len;
-
}
+
+ /* Return the length */
+ return GenLiteralExpr (Len);
}
-static int FuncTCount (void)
+static ExprNode* 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.
+ * the tokens, and count them. Allow optionally curly braces.
*/
- int Count = 0;
- unsigned Parens = 0;
- while (Parens != 0 || Tok != TOK_RPAREN) {
+ token_t Term = GetTokListTerm (TOK_RPAREN);
+ int Count = 0;
+ while (CurTok.Tok != Term) {
/* 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)) {
+ if (TokIsSep (CurTok.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 ();
}
+ /* If the list was enclosed in curly braces, skip the closing brace */
+ if (Term == TOK_RCURLY && CurTok.Tok == TOK_RCURLY) {
+ NextTok ();
+ }
+
/* Return the number of tokens */
- return Count;
+ return GenLiteralExpr (Count);
}
-static int FuncXMatch (void)
+static ExprNode* FuncXMatch (void)
/* Handle the .XMATCH function */
{
return DoMatch (tcIdentical);
-static ExprNode* Function (int (*F) (void))
+static ExprNode* Function (ExprNode* (*F) (void))
/* Handle builtin functions */
{
- long Result;
+ ExprNode* E;
/* Skip the keyword */
NextTok ();
/* Expression must be enclosed in braces */
- if (Tok != TOK_LPAREN) {
+ if (CurTok.Tok != TOK_LPAREN) {
Error ("'(' expected");
SkipUntilSep ();
- return GenLiteralExpr (0);
+ return GenLiteral0 ();
}
NextTok ();
/* Call the function itself */
- Result = F ();
+ E = F ();
/* Closing brace must follow */
ConsumeRParen ();
- /* Return an expression node with the boolean code */
- return GenLiteralExpr (Result);
+ /* Return the result of the actual function */
+ return E;
}
{
ExprNode* L;
ExprNode* N;
- SymEntry* S;
long Val;
- switch (Tok) {
+ switch (CurTok.Tok) {
case TOK_INTCON:
- N = GenLiteralExpr (IVal);
+ N = GenLiteralExpr (CurTok.IVal);
NextTok ();
break;
case TOK_CHARCON:
- N = GenLiteralExpr (TgtTranslateChar (IVal));
+ N = GenLiteralExpr (TgtTranslateChar (CurTok.IVal));
NextTok ();
break;
case TOK_NAMESPACE:
case TOK_IDENT:
- /* Search for the symbol */
- S = ParseScopedSymName (SYM_ALLOC_NEW);
- if (S == 0) {
- /* Some weird error happened before */
- N = GenLiteralExpr (0);
- } else {
- /* Mark the symbol as referenced */
- SymRef (S);
- /* Create symbol node */
- N = GenSymExpr (S);
- }
+ case TOK_LOCAL_IDENT:
+ N = Symbol (ParseAnySymName (SYM_ALLOC_NEW));
break;
case TOK_ULABEL:
- N = ULabRef (IVal);
+ N = ULabRef (CurTok.IVal);
NextTok ();
break;
+ case TOK_PLUS:
+ NextTok ();
+ N = Factor ();
+ break;
+
case TOK_MINUS:
NextTok ();
L = Factor ();
case TOK_LT:
NextTok ();
- L = Factor ();
- if (IsEasyConst (L, &Val)) {
- FreeExpr (L);
- N = GenLiteralExpr (Val & 0xFF);
- } else {
- N = NewExprNode (EXPR_BYTE0);
- N->Left = L;
- }
+ N = LoByte (Factor ());
break;
case TOK_GT:
NextTok ();
- L = Factor ();
- if (IsEasyConst (L, &Val)) {
- FreeExpr (L);
- N = GenLiteralExpr ((Val >> 8) & 0xFF);
- } else {
- N = NewExprNode (EXPR_BYTE1);
- N->Left = L;
- }
+ N = HiByte (Factor ());
break;
case TOK_BANK:
NextTok ();
- L = Factor ();
- if (IsEasyConst (L, &Val)) {
- FreeExpr (L);
- N = GenLiteralExpr ((Val >> 16) & 0xFF);
- } else {
- N = NewExprNode (EXPR_BYTE2);
- N->Left = L;
- }
+ N = BankByte (Factor ());
break;
case TOK_LPAREN:
ConsumeRParen ();
break;
+ case TOK_BANKBYTE:
+ N = Function (FuncBankByte);
+ break;
+
case TOK_BLANK:
N = Function (FuncBlank);
break;
N = Function (FuncDefined);
break;
+ case TOK_HIBYTE:
+ N = Function (FuncHiByte);
+ break;
+
+ case TOK_HIWORD:
+ N = Function (FuncHiWord);
+ break;
+
+ case TOK_LOBYTE:
+ N = Function (FuncLoByte);
+ break;
+
+ case TOK_LOWORD:
+ N = Function (FuncLoWord);
+ break;
+
case TOK_MATCH:
N = Function (FuncMatch);
break;
+ case TOK_MAX:
+ N = Function (FuncMax);
+ break;
+
+ case TOK_MIN:
+ N = Function (FuncMin);
+ break;
+
case TOK_REFERENCED:
N = Function (FuncReferenced);
break;
+ case TOK_SIZEOF:
+ N = Function (FuncSizeOf);
+ break;
+
case TOK_STRAT:
N = Function (FuncStrAt);
break;
break;
case TOK_VERSION:
- N = GenLiteralExpr (VERSION);
+ N = GenLiteralExpr (GetVersionAsNumber ());
NextTok ();
break;
break;
default:
- if (LooseCharTerm && Tok == TOK_STRCON && strlen(SVal) == 1) {
+ if (LooseCharTerm && CurTok.Tok == TOK_STRCON &&
+ SB_GetLen (&CurTok.SVal) == 1) {
/* A character constant */
- N = GenLiteralExpr (TgtTranslateChar (SVal[0]));
+ N = GenLiteralExpr (TgtTranslateChar (SB_At (&CurTok.SVal, 0)));
} else {
- N = GenLiteralExpr (0); /* Dummy */
+ N = GenLiteral0 (); /* Dummy */
Error ("Syntax error");
}
NextTok ();
ExprNode* Root = Factor ();
/* Handle multiplicative operations */
- while (Tok == TOK_MUL || Tok == TOK_DIV || Tok == TOK_MOD ||
- Tok == TOK_AND || Tok == TOK_XOR || Tok == TOK_SHL ||
- Tok == TOK_SHR) {
+ while (CurTok.Tok == TOK_MUL || CurTok.Tok == TOK_DIV ||
+ CurTok.Tok == TOK_MOD || CurTok.Tok == TOK_AND ||
+ CurTok.Tok == TOK_XOR || CurTok.Tok == TOK_SHL ||
+ CurTok.Tok == TOK_SHR) {
long LVal, RVal, Val;
ExprNode* Left;
ExprNode* Right;
/* Remember the token and skip it */
- enum Token T = Tok;
+ token_t T = CurTok.Tok;
NextTok ();
/* Move root to left side and read the right side */
/* Generate an expression tree */
unsigned char Op;
- switch (T) {
+ switch (T) {
case TOK_MUL: Op = EXPR_MUL; break;
case TOK_DIV: Op = EXPR_DIV; break;
case TOK_MOD: Op = EXPR_MOD; break;
ExprNode* Root = Term ();
/* Handle additive operations */
- while (Tok == TOK_PLUS || Tok == TOK_MINUS || Tok == TOK_OR) {
-
- /* Create the new node */
- ExprNode* Left = Root;
- switch (Tok) {
- case TOK_PLUS: Root = NewExprNode (EXPR_PLUS); break;
- case TOK_MINUS: Root = NewExprNode (EXPR_MINUS); break;
- case TOK_OR: Root = NewExprNode (EXPR_OR); break;
- default: Internal ("Invalid token");
- }
- Root->Left = Left;
+ while (CurTok.Tok == TOK_PLUS ||
+ CurTok.Tok == TOK_MINUS ||
+ CurTok.Tok == TOK_OR) {
- /* Skip the operator token */
- NextTok ();
+ long LVal, RVal, Val;
+ ExprNode* Left;
+ ExprNode* Right;
- /* Parse the right hand side */
- Root->Right = Term ();
+ /* Remember the token and skip it */
+ token_t T = CurTok.Tok;
+ NextTok ();
- }
+ /* Move root to left side and read the right side */
+ Left = Root;
+ Right = Term ();
- /* Return the expression tree we've created */
- return Root;
-}
+ /* If both expressions are constant, we can evaluate the term */
+ if (IsEasyConst (Left, &LVal) && IsEasyConst (Right, &RVal)) {
+
+ switch (T) {
+ case TOK_PLUS: Val = LVal + RVal; break;
+ case TOK_MINUS: Val = LVal - RVal; break;
+ case TOK_OR: Val = LVal | RVal; break;
+ default: Internal ("Invalid token");
+ }
+
+ /* Generate a literal expression and delete the old left and
+ * right sides.
+ */
+ FreeExpr (Left);
+ FreeExpr (Right);
+ Root = GenLiteralExpr (Val);
+
+ } else {
+
+ /* Generate an expression tree */
+ unsigned char Op;
+ switch (T) {
+ case TOK_PLUS: Op = EXPR_PLUS; break;
+ case TOK_MINUS: Op = EXPR_MINUS; break;
+ case TOK_OR: Op = EXPR_OR; break;
+ default: Internal ("Invalid token");
+ }
+ Root = NewExprNode (Op);
+ Root->Left = Left;
+ Root->Right = Right;
+
+ }
+ }
+
+ /* Return the expression tree we've created */
+ return Root;
+}
ExprNode* Root = SimpleExpr ();
/* Handle booleans */
- while (Tok == TOK_EQ || Tok == TOK_NE || Tok == TOK_LT ||
- Tok == TOK_GT || Tok == TOK_LE || Tok == TOK_GE) {
-
- /* Create the new node */
- ExprNode* Left = Root;
- switch (Tok) {
- case TOK_EQ: Root = NewExprNode (EXPR_EQ); break;
- case TOK_NE: Root = NewExprNode (EXPR_NE); break;
- case TOK_LT: Root = NewExprNode (EXPR_LT); break;
- case TOK_GT: Root = NewExprNode (EXPR_GT); break;
- case TOK_LE: Root = NewExprNode (EXPR_LE); break;
- case TOK_GE: Root = NewExprNode (EXPR_GE); break;
- default: Internal ("Invalid token");
- }
- Root->Left = Left;
+ while (CurTok.Tok == TOK_EQ || CurTok.Tok == TOK_NE ||
+ CurTok.Tok == TOK_LT || CurTok.Tok == TOK_GT ||
+ CurTok.Tok == TOK_LE || CurTok.Tok == TOK_GE) {
- /* Skip the operator token */
- NextTok ();
+ long LVal, RVal, Val;
+ ExprNode* Left;
+ ExprNode* Right;
+
+ /* Remember the token and skip it */
+ token_t T = CurTok.Tok;
+ NextTok ();
+
+ /* Move root to left side and read the right side */
+ Left = Root;
+ Right = SimpleExpr ();
- /* Parse the right hand side */
- Root->Right = SimpleExpr ();
+ /* If both expressions are constant, we can evaluate the term */
+ if (IsEasyConst (Left, &LVal) && IsEasyConst (Right, &RVal)) {
+
+ switch (T) {
+ case TOK_EQ: Val = (LVal == RVal); break;
+ case TOK_NE: Val = (LVal != RVal); break;
+ case TOK_LT: Val = (LVal < RVal); break;
+ case TOK_GT: Val = (LVal > RVal); break;
+ case TOK_LE: Val = (LVal <= RVal); break;
+ case TOK_GE: Val = (LVal >= RVal); break;
+ default: Internal ("Invalid token");
+ }
+
+ /* Generate a literal expression and delete the old left and
+ * right sides.
+ */
+ FreeExpr (Left);
+ FreeExpr (Right);
+ Root = GenLiteralExpr (Val);
+ } else {
+
+ /* Generate an expression tree */
+ unsigned char Op;
+ switch (T) {
+ case TOK_EQ: Op = EXPR_EQ; break;
+ case TOK_NE: Op = EXPR_NE; break;
+ case TOK_LT: Op = EXPR_LT; break;
+ case TOK_GT: Op = EXPR_GT; break;
+ case TOK_LE: Op = EXPR_LE; break;
+ case TOK_GE: Op = EXPR_GE; break;
+ default: Internal ("Invalid token");
+ }
+ Root = NewExprNode (Op);
+ Root->Left = Left;
+ Root->Right = Right;
+
+ }
}
/* Return the expression tree we've created */
ExprNode* Root = BoolExpr ();
/* Handle booleans */
- while (Tok == TOK_BOOLAND || Tok == TOK_BOOLXOR) {
+ while (CurTok.Tok == TOK_BOOLAND || CurTok.Tok == TOK_BOOLXOR) {
- /* Create the new node */
- ExprNode* Left = Root;
- switch (Tok) {
- case TOK_BOOLAND: Root = NewExprNode (EXPR_BOOLAND); break;
- case TOK_BOOLXOR: Root = NewExprNode (EXPR_BOOLXOR); break;
- default: Internal ("Invalid token");
- }
- Root->Left = Left;
+ long LVal, RVal, Val;
+ ExprNode* Left;
+ ExprNode* Right;
- /* Skip the operator token */
- NextTok ();
+ /* Remember the token and skip it */
+ token_t T = CurTok.Tok;
+ NextTok ();
+
+ /* Move root to left side and read the right side */
+ Left = Root;
+ Right = BoolExpr ();
+
+ /* If both expressions are constant, we can evaluate the term */
+ if (IsEasyConst (Left, &LVal) && IsEasyConst (Right, &RVal)) {
+
+ switch (T) {
+ case TOK_BOOLAND: Val = ((LVal != 0) && (RVal != 0)); break;
+ case TOK_BOOLXOR: Val = ((LVal != 0) ^ (RVal != 0)); break;
+ default: Internal ("Invalid token");
+ }
+
+ /* Generate a literal expression and delete the old left and
+ * right sides.
+ */
+ FreeExpr (Left);
+ FreeExpr (Right);
+ Root = GenLiteralExpr (Val);
+
+ } else {
- /* Parse the right hand side */
- Root->Right = BoolExpr ();
+ /* Generate an expression tree */
+ unsigned char Op;
+ switch (T) {
+ case TOK_BOOLAND: Op = EXPR_BOOLAND; break;
+ case TOK_BOOLXOR: Op = EXPR_BOOLXOR; break;
+ default: Internal ("Invalid token");
+ }
+ Root = NewExprNode (Op);
+ Root->Left = Left;
+ Root->Right = Right;
+ }
}
/* Return the expression tree we've created */
ExprNode* Root = Expr2 ();
/* Handle booleans */
- while (Tok == TOK_BOOLOR) {
+ while (CurTok.Tok == TOK_BOOLOR) {
- /* Create the new node */
- ExprNode* Left = Root;
- switch (Tok) {
- case TOK_BOOLOR: Root = NewExprNode (EXPR_BOOLOR); break;
- default: Internal ("Invalid token");
- }
- Root->Left = Left;
+ long LVal, RVal, Val;
+ ExprNode* Left;
+ ExprNode* Right;
- /* Skip the operator token */
- NextTok ();
+ /* Remember the token and skip it */
+ token_t T = CurTok.Tok;
+ NextTok ();
+
+ /* Move root to left side and read the right side */
+ Left = Root;
+ Right = Expr2 ();
+
+ /* If both expressions are constant, we can evaluate the term */
+ if (IsEasyConst (Left, &LVal) && IsEasyConst (Right, &RVal)) {
+
+ switch (T) {
+ case TOK_BOOLOR: Val = ((LVal != 0) || (RVal != 0)); break;
+ default: Internal ("Invalid token");
+ }
+
+ /* Generate a literal expression and delete the old left and
+ * right sides.
+ */
+ FreeExpr (Left);
+ FreeExpr (Right);
+ Root = GenLiteralExpr (Val);
+
+ } else {
- /* Parse the right hand side */
- Root->Right = Expr2 ();
+ /* Generate an expression tree */
+ unsigned char Op;
+ switch (T) {
+ case TOK_BOOLOR: Op = EXPR_BOOLOR; break;
+ default: Internal ("Invalid token");
+ }
+ Root = NewExprNode (Op);
+ Root->Left = Left;
+ Root->Right = Right;
+ }
}
/* Return the expression tree we've created */
ExprNode* Root;
/* Handle booleans */
- if (Tok == TOK_BOOLNOT) {
+ if (CurTok.Tok == TOK_BOOLNOT) {
- /* Create the new node */
- Root = NewExprNode (EXPR_BOOLNOT);
+ long Val;
+ ExprNode* Left;
/* Skip the operator token */
NextTok ();
- /* Parse the left hand side, allow more BNOTs */
- Root->Left = Expr0 ();
+ /* Read the argument */
+ Left = Expr0 ();
+
+ /* If the argument is const, evaluate it directly */
+ if (IsEasyConst (Left, &Val)) {
+ FreeExpr (Left);
+ Root = GenLiteralExpr (!Val);
+ } else {
+ Root = NewExprNode (EXPR_BOOLNOT);
+ Root->Left = Left;
+ }
} else {
-static void StudyExpr (ExprNode* Expr, ExprDesc* D, int Sign);
-static void StudyBinaryExpr (ExprNode* Expr, ExprDesc* D)
-/* Study a binary expression subtree */
-{
- StudyExpr (Expr->Left, D, 1);
- if (ExprDescIsConst (D)) {
- D->Left = D->Val;
- D->Val = 0;
- StudyExpr (Expr->Right, D, 1);
- if (!ExprDescIsConst (D)) {
- D->TooComplex = 1;
- }
- } else {
- D->TooComplex = 1;
- }
-}
-
-
-
-static void StudyExpr (ExprNode* Expr, ExprDesc* D, int Sign)
-/* Study an expression tree and place the contents into D */
-{
- SymEntry* Sym;
- unsigned Sec;
- ExprDesc SD;
- ExprDesc SD1;
-
- /* Initialize SD. This is not needed in all cases, but it's rather cheap
- * and simplifies the code below.
- */
- InitExprDesc (&SD);
-
- /* Study this expression node */
- switch (Expr->Op) {
-
- case EXPR_LITERAL:
- D->Val += (Sign * Expr->V.Val);
- break;
-
- case EXPR_SYMBOL:
- Sym = Expr->V.Sym;
- if (SymIsImport (Sym)) {
- if (D->SymCount == 0) {
- D->SymCount += Sign;
- D->SymRef = Sym;
- } else if (D->SymRef == Sym) {
- /* Same symbol */
- D->SymCount += Sign;
- } else {
- /* More than one import */
- D->TooComplex = 1;
- }
- } else if (SymHasExpr (Sym)) {
- if (SymHasUserMark (Sym)) {
- if (Verbosity > 0) {
- DumpExpr (Expr, SymResolve);
- }
- PError (GetSymPos (Sym),
- "Circular reference in definition of symbol `%s'",
- GetSymName (Sym));
- D->TooComplex = 1;
- } else {
- SymMarkUser (Sym);
- StudyExpr (GetSymExpr (Sym), D, Sign);
- SymUnmarkUser (Sym);
- }
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_SECTION:
- Sec = Expr->V.SegNum;
- if (D->SecCount == 0) {
- D->SecCount += Sign;
- D->SecRef = Sec;
- } else if (D->SecRef == Sec) {
- /* Same section */
- D->SecCount += Sign;
- } else {
- /* More than one section */
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_ULABEL:
- if (ULabCanResolve ()) {
- /* We can resolve the label */
- StudyExpr (ULabResolve (Expr->V.Val), D, Sign);
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_PLUS:
- StudyExpr (Expr->Left, D, Sign);
- StudyExpr (Expr->Right, D, Sign);
- break;
-
- case EXPR_MINUS:
- StudyExpr (Expr->Left, D, Sign);
- StudyExpr (Expr->Right, D, -Sign);
- break;
-
- case EXPR_MUL:
- InitExprDesc (&SD1);
- StudyExpr (Expr->Left, &SD, 1);
- StudyExpr (Expr->Right, &SD1, 1);
- if (SD.TooComplex == 0 && SD1.TooComplex == 0) {
- /* First calculate SD = SD*SD1 if possible */
- if (ExprDescIsConst (&SD)) {
- /* Left is a constant */
- SD1.Val *= SD.Val;
- SD1.SymCount *= SD.Val;
- SD1.SecCount *= SD.Val;
- SD = SD1;
- } else if (ExprDescIsConst (&SD1)) {
- /* Right is constant */
- SD.Val *= SD1.Val;
- SD.SymCount *= SD1.Val;
- SD.SecCount *= SD1.Val;
- } else {
- D->TooComplex = 1;
- }
- /* Now calculate D * Sign * SD */
- if (!D->TooComplex) {
- if ((D->SymCount == 0 || SD.SymCount == 0 || D->SymRef == SD.SymRef) &&
- (D->SecCount == 0 || SD.SecCount == 0 || D->SecRef == SD.SecRef)) {
- D->Val += (Sign * SD.Val);
- if (D->SymCount == 0) {
- D->SymRef = SD.SymRef;
- }
- D->SymCount += (Sign * SD.SymCount);
- if (D->SecCount == 0) {
- D->SecRef = SD.SecRef;
- }
- D->SecCount += (Sign * SD.SecCount);
- }
- } else {
- D->TooComplex = 1;
- }
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_DIV:
- StudyBinaryExpr (Expr, &SD);
- if (!SD.TooComplex) {
- if (SD.Val == 0) {
- Error ("Division by zero");
- D->TooComplex = 1;
- } else {
- D->Val += Sign * (SD.Left / SD.Val);
- }
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_MOD:
- StudyBinaryExpr (Expr, &SD);
- if (!SD.TooComplex) {
- if (SD.Val == 0) {
- Error ("Modulo operation with zero");
- D->TooComplex = 1;
- } else {
- D->Val += Sign * (SD.Left % SD.Val);
- }
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_OR:
- StudyBinaryExpr (Expr, &SD);
- if (!SD.TooComplex) {
- D->Val += Sign * (SD.Left | SD.Val);
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_XOR:
- StudyBinaryExpr (Expr, &SD);
- if (!SD.TooComplex) {
- D->Val += Sign * (SD.Left ^ SD.Val);
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_AND:
- StudyBinaryExpr (Expr, &SD);
- if (!SD.TooComplex) {
- D->Val += Sign * (SD.Left & SD.Val);
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_SHL:
- StudyBinaryExpr (Expr, &SD);
- if (!SD.TooComplex) {
- D->Val += (Sign * shl_l (SD.Left, (unsigned) SD.Val));
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_SHR:
- StudyBinaryExpr (Expr, &SD);
- if (!SD.TooComplex) {
- D->Val += (Sign * shr_l (SD.Left, (unsigned) SD.Val));
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_EQ:
- StudyBinaryExpr (Expr, &SD);
- if (!SD.TooComplex) {
- D->Val += Sign * (SD.Left == SD.Val);
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_NE:
- StudyBinaryExpr (Expr, &SD);
- if (!SD.TooComplex) {
- D->Val += Sign * (SD.Left != SD.Val);
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_LT:
- StudyBinaryExpr (Expr, &SD);
- if (!SD.TooComplex) {
- D->Val += Sign * (SD.Left < SD.Val);
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_GT:
- StudyBinaryExpr (Expr, &SD);
- if (!SD.TooComplex) {
- D->Val += Sign * (SD.Left > SD.Val);
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_LE:
- StudyBinaryExpr (Expr, &SD);
- if (!SD.TooComplex) {
- D->Val += Sign * (SD.Left <= SD.Val);
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_GE:
- StudyBinaryExpr (Expr, &SD);
- if (!SD.TooComplex) {
- D->Val += Sign * (SD.Left >= SD.Val);
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_BOOLAND:
- StudyExpr (Expr->Left, &SD, 1);
- if (ExprDescIsConst (&SD)) {
- if (SD.Val != 0) { /* Shortcut op */
- SD.Val = 0;
- StudyExpr (Expr->Right, &SD, 1);
- if (ExprDescIsConst (&SD)) {
- D->Val += Sign * (SD.Val != 0);
- } else {
- D->TooComplex = 1;
- }
- }
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_BOOLOR:
- StudyExpr (Expr->Left, &SD, 1);
- if (ExprDescIsConst (&SD)) {
- if (SD.Val == 0) { /* Shortcut op */
- StudyExpr (Expr->Right, &SD, 1);
- if (ExprDescIsConst (&SD)) {
- D->Val += Sign * (SD.Val != 0);
- } else {
- D->TooComplex = 1;
- }
- } else {
- D->Val += Sign;
- }
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_BOOLXOR:
- StudyBinaryExpr (Expr, &SD);
- if (!SD.TooComplex) {
- D->Val += Sign * ((SD.Left != 0) ^ (SD.Val != 0));
- }
- break;
-
- case EXPR_UNARY_MINUS:
- StudyExpr (Expr->Left, D, -Sign);
- break;
-
- case EXPR_NOT:
- StudyExpr (Expr->Left, &SD, 1);
- if (ExprDescIsConst (&SD)) {
- D->Val += (Sign * ~SD.Val);
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_SWAP:
- StudyExpr (Expr->Left, &SD, 1);
- if (ExprDescIsConst (&SD)) {
- D->Val += Sign * (((SD.Val >> 8) & 0x00FF) | ((SD.Val << 8) & 0xFF00));
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_BOOLNOT:
- StudyExpr (Expr->Left, &SD, 1);
- if (ExprDescIsConst (&SD)) {
- D->Val += Sign * (SD.Val != 0);
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_FORCEWORD:
- case EXPR_FORCEFAR:
- /* Ignore */
- StudyExpr (Expr->Left, D, Sign);
- break;
-
- case EXPR_BYTE0:
- StudyExpr (Expr->Left, &SD, 1);
- if (ExprDescIsConst (&SD)) {
- D->Val += Sign * (SD.Val & 0xFF);
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_BYTE1:
- StudyExpr (Expr->Left, &SD, 1);
- if (ExprDescIsConst (&SD)) {
- D->Val += Sign * ((SD.Val >> 8) & 0xFF);
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_BYTE2:
- StudyExpr (Expr->Left, &SD, 1);
- if (ExprDescIsConst (&SD)) {
- D->Val += Sign * ((SD.Val >> 16) & 0xFF);
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_BYTE3:
- StudyExpr (Expr->Left, &SD, 1);
- if (ExprDescIsConst (&SD)) {
- D->Val += Sign * ((SD.Val >> 24) & 0xFF);
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_WORD0:
- StudyExpr (Expr->Left, &SD, 1);
- if (ExprDescIsConst (&SD)) {
- D->Val += Sign * (SD.Val & 0xFFFF);
- } else {
- D->TooComplex = 1;
- }
- break;
-
- case EXPR_WORD1:
- StudyExpr (Expr->Left, &SD, 1);
- if (ExprDescIsConst (&SD)) {
- D->Val += Sign * ((SD.Val >> 16) & 0xFFFF);
- } else {
- D->TooComplex = 1;
- }
- break;
-
- default:
- Internal ("Unknown Op type: %u", Expr->Op);
- break;
- }
-}
-
-
-
-static ExprNode* SimplifyExpr (ExprNode* Expr)
-/* Try to simplify the given expression tree */
-{
- if (Expr && Expr->Op != EXPR_LITERAL) {
-
- /* Create an expression description and initialize it */
- ExprDesc D;
- InitExprDesc (&D);
-
- /* Study the expression */
- StudyExpr (Expr, &D, 1);
-
- /* Now check if we can generate a literal value */
- if (ExprDescIsConst (&D)) {
- /* No external references */
- FreeExpr (Expr);
- Expr = GenLiteralExpr (D.Val);
- }
- }
- return Expr;
-}
-
-
-
ExprNode* Expression (void)
/* Evaluate an expression, build the expression tree on the heap and return
* a pointer to the root of the tree.
*/
{
-#if 1
- return SimplifyExpr (Expr0 ());
-#else
- /* Test code */
- ExprNode* Expr = Expr0 ();
- printf ("Before: "); DumpExpr (Expr, SymResolve);
- Expr = SimplifyExpr (Expr);
- printf ("After: "); DumpExpr (Expr, SymResolve);
- return Expr;
-#endif
+ return Expr0 ();
}
* not constant.
*/
{
-#if 1
+ long Val;
+
/* Read the expression */
- ExprNode* Expr = Expr0 ();
-#else
- /* Test code */
ExprNode* Expr = Expression ();
-#endif
/* 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;
}
+ExprNode* SimplifyExpr (ExprNode* Expr, const ExprDesc* D)
+/* Try to simplify the given expression tree */
+{
+ if (Expr->Op != EXPR_LITERAL && ED_IsConst (D)) {
+ /* No external references */
+ FreeExpr (Expr);
+ Expr = GenLiteralExpr (D->Val);
+ }
+ return Expr;
+}
+
+
+
ExprNode* GenLiteralExpr (long Val)
/* Return an expression tree that encodes the given literal value */
{
ExprNode* Expr = NewExprNode (EXPR_LITERAL);
- Expr->V.Val = Val;
+ Expr->V.IVal = Val;
return Expr;
}
+ExprNode* GenLiteral0 (void)
+/* Return an expression tree that encodes the the number zero */
+{
+ return GenLiteralExpr (0);
+}
+
+
+
ExprNode* GenSymExpr (SymEntry* Sym)
/* Return an expression node that encodes the given symbol */
{
-static ExprNode* GenSectionExpr (unsigned SegNum)
+static ExprNode* GenSectionExpr (unsigned SecNum)
/* Return an expression node for the given section */
{
ExprNode* Expr = NewExprNode (EXPR_SECTION);
- Expr->V.SegNum = SegNum;
+ Expr->V.SecNum = SecNum;
return Expr;
}
+ExprNode* GenAddExpr (ExprNode* Left, ExprNode* Right)
+/* Generate an addition from the two operands */
+{
+ long Val;
+ if (IsEasyConst (Left, &Val) && Val == 0) {
+ FreeExpr (Left);
+ return Right;
+ } else if (IsEasyConst (Right, &Val) && Val == 0) {
+ FreeExpr (Right);
+ return Left;
+ } else {
+ ExprNode* Root = NewExprNode (EXPR_PLUS);
+ Root->Left = Left;
+ Root->Right = Right;
+ return Root;
+ }
+}
+
+
+
ExprNode* GenCurrentPC (void)
/* Return the current program counter as expression */
{
ExprNode* Root;
- if (RelocMode) {
+ if (GetRelocMode ()) {
/* Create SegmentBase + Offset */
- Root = NewExprNode (EXPR_PLUS);
- Root->Left = GenSectionExpr (GetCurrentSegNum ());
- Root->Right = GenLiteralExpr (GetPC ());
+ Root = GenAddExpr (GenSectionExpr (GetCurrentSegNum ()),
+ GenLiteralExpr (GetPC ()));
} else {
/* Absolute mode, just return PC value */
Root = GenLiteralExpr (GetPC ());
{
ExprNode* N;
ExprNode* Root;
+ long Val;
+
+ /* Read Expression() */
+ N = Expression ();
+
+ /* If the expression is a cheap constant, generate a simpler tree */
+ if (IsEasyConst (N, &Val)) {
+
+ /* Free the constant expression tree */
+ FreeExpr (N);
+
+ /* Generate the final expression:
+ * Val - (* + Offs)
+ * Val - ((Seg + PC) + Offs)
+ * Val - Seg - PC - Offs
+ * (Val - PC - Offs) - Seg
+ */
+ Root = GenLiteralExpr (Val - GetPC () - Offs);
+ if (GetRelocMode ()) {
+ N = Root;
+ Root = NewExprNode (EXPR_MINUS);
+ Root->Left = N;
+ Root->Right = GenSectionExpr (GetCurrentSegNum ());
+ }
- /* Create *+Offs */
- if (RelocMode) {
- N = NewExprNode (EXPR_PLUS);
- N->Left = GenSectionExpr (GetCurrentSegNum ());
- N->Right = GenLiteralExpr (GetPC () + Offs);
} else {
- N = GenLiteralExpr (GetPC () + Offs);
- }
- /* Create the root node */
- Root = NewExprNode (EXPR_MINUS);
- Root->Left = Expression ();
- Root->Right = N;
+ /* Generate the expression:
+ * N - (* + Offs)
+ * N - ((Seg + PC) + Offs)
+ * N - Seg - PC - Offs
+ * N - (PC + Offs) - Seg
+ */
+ Root = NewExprNode (EXPR_MINUS);
+ Root->Left = N;
+ Root->Right = GenLiteralExpr (GetPC () + Offs);
+ if (GetRelocMode ()) {
+ N = Root;
+ Root = NewExprNode (EXPR_MINUS);
+ Root->Left = N;
+ Root->Right = GenSectionExpr (GetCurrentSegNum ());
+ }
+ }
/* Return the result */
- return SimplifyExpr (Root);
+ return Root;
}
/* Return an expression for an unnamed label with the given index */
{
ExprNode* Node = NewExprNode (EXPR_ULABEL);
- Node->V.Val = Num;
+ Node->V.IVal = Num;
/* Return the new node */
return Node;
/* Force the given expression into a byte and return the result */
{
/* Use the low byte operator to force the expression into byte size */
- ExprNode* Root = NewExprNode (EXPR_BYTE0);
- Root->Left = Expr;
-
- /* Return the result */
- return Root;
+ return LoByte (Expr);
}
ExprNode* GenWordExpr (ExprNode* Expr)
/* Force the given expression into a word and return the result. */
{
- /* AND the expression by $FFFF to force it into word size */
- ExprNode* Root = NewExprNode (EXPR_AND);
- Root->Left = Expr;
- Root->Right = GenLiteralExpr (0xFFFF);
-
- /* Return the result */
- return Root;
+ /* Use the low byte operator to force the expression into word size */
+ return LoWord (Expr);
}
* 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) {
- *Val = D.Val;
- }
- return 1;
- } else {
- return 0;
- }
-}
-
-
-
-static void CheckByteExpr (const ExprNode* N, int* IsByte)
-/* Internal routine that is recursively called to check if there is a zeropage
- * symbol in the expression tree.
- */
-{
- if (N) {
- switch (N->Op & EXPR_TYPEMASK) {
-
- case EXPR_LEAFNODE:
- switch (N->Op) {
-
- case EXPR_SYMBOL:
- if (SymIsZP (N->V.Sym)) {
- *IsByte = 1;
- } else if (SymHasExpr (N->V.Sym)) {
- /* Check if this expression is a byte expression */
- CheckByteExpr (GetSymExpr (N->V.Sym), IsByte);
- }
- break;
-
- case EXPR_SECTION:
- if (GetSegAddrSize (N->V.SegNum) == ADDR_SIZE_ZP) {
- *IsByte = 1;
- }
- break;
-
- }
- break;
-
- case EXPR_UNARYNODE:
- CheckByteExpr (N->Left, IsByte);
- break;
-
- case EXPR_BINARYNODE:
- CheckByteExpr (N->Left, IsByte);
- CheckByteExpr (N->Right, IsByte);
- break;
-
- default:
- Internal ("Unknown expression op: %02X", N->Op);
- }
+ IsConst = ED_IsConst (&D);
+ if (IsConst && Val != 0) {
+ *Val = D.Val;
}
-}
-
-
-int IsByteExpr (ExprNode* Root)
-/* Return true if this is a byte expression */
-{
- int IsByte;
- long Val;
-
- if (IsConstExpr (Root, &Val)) {
- IsByte = IsByteRange (Val);
- } 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 {
- /* We have undefined symbols in the expression. Assume that the
- * expression is a byte expression if there is at least one symbol
- * declared as zeropage in it. Being wrong here is not a very big
- * problem since the linker knows about all symbols and detects
- * error like mixing absolute and zeropage labels.
- */
- IsByte = 0;
- CheckByteExpr (Root, &IsByte);
- }
- return IsByte;
+ /* Delete allocated memory and return the result */
+ ED_Done (&D);
+ return IsConst;
}
switch (Expr->Op) {
case EXPR_LITERAL:
- Clone = GenLiteralExpr (Expr->V.Val);
+ Clone = GenLiteralExpr (Expr->V.IVal);
break;
case EXPR_ULABEL:
- Clone = GenULabelExpr (Expr->V.Val);
+ Clone = GenULabelExpr (Expr->V.IVal);
break;
case EXPR_SYMBOL:
break;
case EXPR_SECTION:
- Clone = GenSectionExpr (Expr->V.SegNum);
+ Clone = GenSectionExpr (Expr->V.SecNum);
break;
default:
case EXPR_LITERAL:
ObjWrite8 (EXPR_LITERAL);
- ObjWrite32 (Expr->V.Val);
- break;
+ ObjWrite32 (Expr->V.IVal);
+ break;
case EXPR_SYMBOL:
- if (SymIsImport (Expr->V.Sym)) {
+ if (SymIsImport (Expr->V.Sym)) {
ObjWrite8 (EXPR_SYMBOL);
- ObjWriteVar (GetSymIndex (Expr->V.Sym));
+ ObjWriteVar (GetSymImportId (Expr->V.Sym));
} else {
WriteExpr (GetSymExpr (Expr->V.Sym));
}
case EXPR_SECTION:
ObjWrite8 (EXPR_SECTION);
- ObjWrite8 (Expr->V.SegNum);
+ ObjWrite8 (Expr->V.SecNum);
break;
case EXPR_ULABEL:
- WriteExpr (ULabResolve (Expr->V.Val));
+ WriteExpr (ULabResolve (Expr->V.IVal));
break;
default:
+void ExprGuessedAddrSize (const ExprNode* Expr, unsigned char AddrSize)
+/* Mark the address size of the given expression tree as guessed. The address
+ * size passed as argument is the one NOT used, because the actual address
+ * size wasn't known. Example: Zero page addressing was not used because symbol
+ * is undefined, and absolute addressing was available.
+ * This function will actually parse the expression tree for undefined symbols,
+ * and mark these symbols accordingly.
+ */
+{
+ /* Accept NULL expressions */
+ if (Expr == 0) {
+ return;
+ }
+
+ /* Check the type code */
+ switch (Expr->Op & EXPR_TYPEMASK) {
+
+ case EXPR_LEAFNODE:
+ if (Expr->Op == EXPR_SYMBOL) {
+ if (!SymIsDef (Expr->V.Sym)) {
+ /* Symbol is undefined, mark it */
+ SymGuessedAddrSize (Expr->V.Sym, AddrSize);
+ }
+ }
+ return;
+
+ case EXPR_BINARYNODE:
+ ExprGuessedAddrSize (Expr->Right, AddrSize);
+ /* FALLTHROUGH */
+
+ case EXPR_UNARYNODE:
+ ExprGuessedAddrSize (Expr->Left, AddrSize);
+ break;
+ }
+}
+
+