1 /*****************************************************************************/
5 /* Expression evaluation for the ca65 macroassembler */
9 /* (C) 1998-2006 Ullrich von Bassewitz */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
46 /*****************************************************************************/
48 /*****************************************************************************/
56 /*****************************************************************************/
58 /*****************************************************************************/
62 ExprNode* Expression (void);
63 /* Evaluate an expression, build the expression tree on the heap and return
64 * a pointer to the root of the tree.
67 long ConstExpression (void);
68 /* Parse an expression. Check if the expression is const, and print an error
69 * message if not. Return the value of the expression, or a dummy, if it is
73 void FreeExpr (ExprNode* Root);
74 /* Free the expression tree, Root is pointing to. */
76 ExprNode* SimplifyExpr (ExprNode* Expr, const struct ExprDesc* D);
77 /* Try to simplify the given expression tree */
79 ExprNode* GenLiteralExpr (long Val);
80 /* Return an expression tree that encodes the given literal value */
82 ExprNode* GenLiteral0 (void);
83 /* Return an expression tree that encodes the the number zero */
85 ExprNode* GenSymExpr (struct SymEntry* Sym);
86 /* Return an expression node that encodes the given symbol */
88 ExprNode* GenAddExpr (ExprNode* Left, ExprNode* Right);
89 /* Generate an addition from the two operands */
91 ExprNode* GenCurrentPC (void);
92 /* Return the current program counter as expression */
94 ExprNode* GenSwapExpr (ExprNode* Expr);
95 /* Return an extended expression with lo and hi bytes swapped */
97 ExprNode* GenBranchExpr (unsigned Offs);
98 /* Return an expression that encodes the difference between current PC plus
99 * offset and the target expression (that is, Expression() - (*+Offs) ).
102 ExprNode* GenULabelExpr (unsigned Num);
103 /* Return an expression for an unnamed label with the given index */
105 ExprNode* GenByteExpr (ExprNode* Expr);
106 /* Force the given expression into a byte and return the result */
108 ExprNode* GenWordExpr (ExprNode* Expr);
109 /* Force the given expression into a word and return the result. */
111 ExprNode* GenNE (ExprNode* Expr, long Val);
112 /* Generate an expression that compares Expr and Val for inequality */
114 int IsConstExpr (ExprNode* Expr, long* Val);
115 /* Return true if the given expression is a constant expression, that is, one
116 * with no references to external symbols. If Val is not NULL and the
117 * expression is constant, the constant value is stored here.
120 int IsByteExpr (ExprNode* Root);
121 /* Return true if this is a byte expression */
123 int IsByteRange (long Val);
124 /* Return true if this is a byte value */
126 int IsWordRange (long Val);
127 /* Return true if this is a word value */
129 int IsFarRange (long Val);
130 /* Return true if this is a far (24 bit) value */
132 ExprNode* CloneExpr (ExprNode* Expr);
133 /* Clone the given expression tree. The function will simply clone symbol
134 * nodes, it will not resolve them.
137 void WriteExpr (ExprNode* Expr);
138 /* Write the given expression to the object file */
140 void ExprGuessedAddrSize (const ExprNode* Expr, unsigned char AddrSize);
141 /* Mark the address size of the given expression tree as guessed. The address
142 * size passed as argument is the one NOT used, because the actual address
143 * size wasn't known. Example: Zero page addressing was not used because symbol
144 * is undefined, and absolute addressing was available.
145 * This function will actually parse the expression tree for undefined symbols,
146 * and mark these symbols accordingly.
149 ExprNode* FuncBankByte (void);
150 /* Handle the .BANKBYTE builtin function */
152 ExprNode* FuncLoByte (void);
153 /* Handle the .LOBYTE builtin function */
155 ExprNode* FuncHiByte (void);
156 /* Handle the .HIBYTE builtin function */