1 /*****************************************************************************/
5 /* Expression evaluation for the ca65 macroassembler */
9 /* (C) 1998-2012, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
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 /*****************************************************************************/
47 /*****************************************************************************/
49 /*****************************************************************************/
57 /*****************************************************************************/
59 /*****************************************************************************/
63 ExprNode* Expression (void);
64 /* Evaluate an expression, build the expression tree on the heap and return
65 * a pointer to the root of the tree.
68 long ConstExpression (void);
69 /* Parse an expression. Check if the expression is const, and print an error
70 * message if not. Return the value of the expression, or a dummy, if it is
74 void FreeExpr (ExprNode* Root);
75 /* Free the expression tree, Root is pointing to. */
77 ExprNode* SimplifyExpr (ExprNode* Expr, const struct ExprDesc* D);
78 /* Try to simplify the given expression tree */
80 ExprNode* GenLiteralExpr (long Val);
81 /* Return an expression tree that encodes the given literal value */
83 ExprNode* GenLiteral0 (void);
84 /* Return an expression tree that encodes the the number zero */
86 ExprNode* GenSymExpr (struct SymEntry* Sym);
87 /* Return an expression node that encodes the given symbol */
89 ExprNode* GenAddExpr (ExprNode* Left, ExprNode* Right);
90 /* Generate an addition from the two operands */
92 ExprNode* GenCurrentPC (void);
93 /* Return the current program counter as expression */
95 ExprNode* GenSwapExpr (ExprNode* Expr);
96 /* Return an extended expression with lo and hi bytes swapped */
98 ExprNode* GenBranchExpr (unsigned Offs);
99 /* Return an expression that encodes the difference between current PC plus
100 * offset and the target expression (that is, Expression() - (*+Offs) ).
103 ExprNode* GenULabelExpr (unsigned Num);
104 /* Return an expression for an unnamed label with the given index */
106 ExprNode* GenByteExpr (ExprNode* Expr);
107 /* Force the given expression into a byte and return the result */
109 ExprNode* GenWordExpr (ExprNode* Expr);
110 /* Force the given expression into a word and return the result. */
112 ExprNode* GenFarAddrExpr (ExprNode* Expr);
113 /* Force the given expression into a far address and return the result. */
115 ExprNode* GenDWordExpr (ExprNode* Expr);
116 /* Force the given expression into a dword and return the result. */
118 ExprNode* GenNE (ExprNode* Expr, long Val);
119 /* Generate an expression that compares Expr and Val for inequality */
121 int IsConstExpr (ExprNode* Expr, long* Val);
122 /* Return true if the given expression is a constant expression, that is, one
123 * with no references to external symbols. If Val is not NULL and the
124 * expression is constant, the constant value is stored here.
127 int IsByteExpr (ExprNode* Root);
128 /* Return true if this is a byte expression */
130 int IsByteRange (long Val);
131 /* Return true if this is a byte value */
133 int IsWordRange (long Val);
134 /* Return true if this is a word value */
136 int IsFarRange (long Val);
137 /* Return true if this is a far (24 bit) value */
139 int IsEasyConst (const ExprNode* E, long* Val);
140 /* Do some light checking if the given node is a constant. Don't care if E is
141 * a complex expression. If E is a constant, return true and place its value
142 * into Val, provided that Val is not NULL.
145 ExprNode* CloneExpr (ExprNode* Expr);
146 /* Clone the given expression tree. The function will simply clone symbol
147 * nodes, it will not resolve them.
150 void WriteExpr (ExprNode* Expr);
151 /* Write the given expression to the object file */
153 void ExprGuessedAddrSize (const ExprNode* Expr, unsigned char AddrSize);
154 /* Mark the address size of the given expression tree as guessed. The address
155 * size passed as argument is the one NOT used, because the actual address
156 * size wasn't known. Example: Zero page addressing was not used because symbol
157 * is undefined, and absolute addressing was available.
158 * This function will actually parse the expression tree for undefined symbols,
159 * and mark these symbols accordingly.
162 ExprNode* FuncBankByte (void);
163 /* Handle the .BANKBYTE builtin function */
165 ExprNode* FuncLoByte (void);
166 /* Handle the .LOBYTE builtin function */
168 ExprNode* FuncHiByte (void);
169 /* Handle the .HIBYTE builtin function */
171 ExprNode* MakeBoundedExpr (ExprNode* Expr, unsigned Size);
172 /* Force the given expression into a specific size of ForceRange is true */
174 ExprNode* BoundedExpr (ExprNode* (*ExprFunc) (void), unsigned Size);
175 /* Parse an expression and force it within a given size if ForceRange is true */