1 /*****************************************************************************/
5 /* Expression evaluation for the ca65 macroassembler */
9 /* (C) 1998-2003 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 /*****************************************************************************/
59 #include "studyexpr.h"
67 /*****************************************************************************/
69 /*****************************************************************************/
73 /* Since all expressions are first packed into expression trees, and each
74 * expression tree node is allocated on the heap, we add some type of special
75 * purpose memory allocation here: Instead of freeing the nodes, we save some
76 * number of freed nodes for later and remember them in a single linked list
77 * using the Left link.
79 #define MAX_FREE_NODES 64
80 static ExprNode* FreeExprNodes = 0;
81 static unsigned FreeNodeCount = 0;
85 /*****************************************************************************/
87 /*****************************************************************************/
91 static ExprNode* NewExprNode (unsigned Op)
92 /* Create a new expression node */
96 /* Do we have some nodes in the list already? */
98 /* Use first node from list */
100 FreeExprNodes = N->Left;
102 /* Allocate fresh memory */
103 N = xmalloc (sizeof (ExprNode));
106 N->Left = N->Right = 0;
114 static void FreeExprNode (ExprNode* E)
118 if (E->Op == EXPR_SYMBOL) {
119 /* Remove the symbol reference */
120 SymDelExprRef (E->V.Sym, E);
122 /* Place the symbol into the free nodes list if possible */
123 if (FreeNodeCount < MAX_FREE_NODES) {
124 /* Remember this node for later */
125 E->Left = FreeExprNodes;
128 /* Free the memory */
136 /*****************************************************************************/
138 /*****************************************************************************/
142 static ExprNode* Expr0 (void);
146 int IsByteRange (long Val)
147 /* Return true if this is a byte value */
149 return (Val & ~0xFFL) == 0;
154 int IsWordRange (long Val)
155 /* Return true if this is a word value */
157 return (Val & ~0xFFFFL) == 0;
162 int IsFarRange (long Val)
163 /* Return true if this is a far (24 bit) value */
165 return (Val & ~0xFFFFFFL) == 0;
170 static int IsEasyConst (const ExprNode* E, long* Val)
171 /* Do some light checking if the given node is a constant. Don't care if E is
172 * a complex expression. If E is a constant, return true and place its value
173 * into Val, provided that Val is not NULL.
176 /* Resolve symbols, follow symbol chains */
177 while (E->Op == EXPR_SYMBOL) {
178 E = SymResolve (E->V.Sym);
180 /* Could not resolve */
185 /* Symbols resolved, check for a literal */
186 if (E->Op == EXPR_LITERAL) {
193 /* Not found to be a const according to our tests */
199 static ExprNode* Symbol (SymEntry* S)
200 /* Reference a symbol and return an expression for it */
203 /* Some weird error happened before */
204 return GenLiteralExpr (0);
206 /* Mark the symbol as referenced */
208 /* Create symbol node */
209 return GenSymExpr (S);
215 static ExprNode* FuncBlank (void)
216 /* Handle the .BLANK builtin function */
220 /* Assume no tokens if the closing brace follows (this is not correct in
221 * all cases, since the token may be the closing brace, but this will
222 * give a syntax error anyway and may not be handled by .BLANK.
224 if (Tok != TOK_RPAREN) {
225 /* Skip any tokens */
227 while (!TokIsSep (Tok)) {
228 if (Tok == TOK_LPAREN) {
230 } else if (Tok == TOK_RPAREN) {
242 return GenLiteralExpr (Result);
247 static ExprNode* FuncConst (void)
248 /* Handle the .CONST builtin function */
250 /* Read an expression */
251 ExprNode* Expr = Expression ();
253 /* Check the constness of the expression */
254 ExprNode* Result = GenLiteralExpr (IsConstExpr (Expr, 0));
256 /* Free the expression */
265 static ExprNode* FuncDefined (void)
266 /* Handle the .DEFINED builtin function */
268 /* Parse the symbol name and search for the symbol */
269 SymEntry* Sym = ParseScopedSymName (SYM_FIND_EXISTING);
271 /* Check if the symbol is defined */
272 return GenLiteralExpr (Sym != 0 && SymIsDef (Sym));
277 static ExprNode* DoMatch (enum TC EqualityLevel)
278 /* Handle the .MATCH and .XMATCH builtin functions */
285 /* A list of tokens follows. Read this list and remember it building a
286 * single linked list of tokens including attributes. The list is
287 * terminated by a comma.
289 while (Tok != TOK_COMMA) {
291 /* We may not end-of-line of end-of-file here */
292 if (TokIsSep (Tok)) {
293 Error ("Unexpected end of line");
297 /* Get a node with this token */
298 Node = NewTokNode ();
300 /* Insert the node into the list */
315 /* Read the second list which is terminated by the right parenthesis and
316 * compare each token against the one in the first list.
320 while (Tok != TOK_RPAREN) {
322 /* We may not end-of-line of end-of-file here */
323 if (TokIsSep (Tok)) {
324 Error ("Unexpected end of line");
328 /* Compare the tokens if the result is not already known */
331 /* The second list is larger than the first one */
333 } else if (TokCmp (Node) < EqualityLevel) {
334 /* Tokens do not match */
339 /* Next token in first list */
344 /* Next token in current list */
348 /* Check if there are remaining tokens in the first list */
353 /* Free the token list */
360 /* Done, return the result */
361 return GenLiteralExpr (Result);
366 static ExprNode* FuncMatch (void)
367 /* Handle the .MATCH function */
369 return DoMatch (tcSameToken);
374 static ExprNode* FuncReferenced (void)
375 /* Handle the .REFERENCED builtin function */
377 /* Parse the symbol name and search for the symbol */
378 SymEntry* Sym = ParseScopedSymName (SYM_FIND_EXISTING);
380 /* Check if the symbol is referenced */
381 return GenLiteralExpr (Sym != 0 && SymIsRef (Sym));
386 static ExprNode* FuncSizeOf (void)
387 /* Handle the .SIZEOF function */
389 StrBuf FullName = AUTO_STRBUF_INITIALIZER;
390 char Name[sizeof (SVal)];
397 /* Parse the scope and the name */
398 SymTable* ParentScope = ParseScopedIdent (Name, &FullName);
400 /* Check if the parent scope is valid */
401 if (ParentScope == 0) {
403 DoneStrBuf (&FullName);
404 return GenLiteralExpr (0);
407 /* The scope is valid, search first for a child scope, then for a symbol */
408 if ((Scope = SymFindScope (ParentScope, Name, SYM_FIND_EXISTING)) != 0) {
409 /* Yep, it's a scope */
410 SizeSym = GetSizeOfScope (Scope);
411 } else if ((Sym = SymFind (ParentScope, Name, SYM_FIND_EXISTING)) != 0) {
412 SizeSym = GetSizeOfSymbol (Sym);
414 Error ("Unknown symbol or scope: `%s'", SB_GetConstBuf (&FullName));
415 return GenLiteralExpr (0);
418 /* Check if we have a size */
419 if (SizeSym == 0 || !SymIsConst (SizeSym, &Size)) {
420 Error ("Size of `%s' is unknown", SB_GetConstBuf (&FullName));
421 return GenLiteralExpr (0);
424 /* Return the size */
425 return GenLiteralExpr (Size);
430 static ExprNode* FuncStrAt (void)
431 /* Handle the .STRAT function */
433 char Str [sizeof(SVal)];
437 /* String constant expected */
438 if (Tok != TOK_STRCON) {
439 Error ("String constant expected");
445 /* Remember the string and skip it */
449 /* Comma must follow */
452 /* Expression expected */
453 Index = ConstExpression ();
455 /* Must be a valid index */
456 if (Index >= (long) strlen (Str)) {
457 Error ("Range error");
461 /* Get the char, handle as unsigned. Be sure to translate it into
462 * the target character set.
464 C = TgtTranslateChar (Str [(size_t)Index]);
466 /* Return the char expression */
467 return GenLiteralExpr (C);
472 static ExprNode* FuncStrLen (void)
473 /* Handle the .STRLEN function */
477 /* String constant expected */
478 if (Tok != TOK_STRCON) {
480 Error ("String constant expected");
481 /* Smart error recovery */
482 if (Tok != TOK_RPAREN) {
489 /* Get the length of the string */
492 /* Skip the string */
496 /* Return the length */
497 return GenLiteralExpr (Len);
502 static ExprNode* FuncTCount (void)
503 /* Handle the .TCOUNT function */
505 /* We have a list of tokens that ends with the closing paren. Skip
506 * the tokens, handling nested braces and count them.
510 while (Parens != 0 || Tok != TOK_RPAREN) {
512 /* Check for end of line or end of input. Since the calling function
513 * will check for the closing paren, we don't need to print an error
514 * here, just bail out.
516 if (TokIsSep (Tok)) {
523 /* Keep track of the nesting level */
525 case TOK_LPAREN: ++Parens; break;
526 case TOK_RPAREN: --Parens; break;
534 /* Return the number of tokens */
535 return GenLiteralExpr (Count);
540 static ExprNode* FuncXMatch (void)
541 /* Handle the .XMATCH function */
543 return DoMatch (tcIdentical);
548 static ExprNode* Function (ExprNode* (*F) (void))
549 /* Handle builtin functions */
553 /* Skip the keyword */
556 /* Expression must be enclosed in braces */
557 if (Tok != TOK_LPAREN) {
558 Error ("'(' expected");
560 return GenLiteralExpr (0);
564 /* Call the function itself */
567 /* Closing brace must follow */
570 /* Return the result of the actual function */
576 static ExprNode* Factor (void)
585 N = GenLiteralExpr (IVal);
590 N = GenLiteralExpr (TgtTranslateChar (IVal));
596 N = Symbol (ParseScopedSymName (SYM_ALLOC_NEW));
599 case TOK_LOCAL_IDENT:
600 N = Symbol (SymFindLocal (SymLast, SVal, SYM_ALLOC_NEW));
612 if (IsEasyConst (L, &Val)) {
614 N = GenLiteralExpr (-Val);
616 N = NewExprNode (EXPR_UNARY_MINUS);
624 if (IsEasyConst (L, &Val)) {
626 N = GenLiteralExpr (~Val);
628 N = NewExprNode (EXPR_NOT);
642 if (IsEasyConst (L, &Val)) {
644 N = GenLiteralExpr (Val & 0xFF);
646 N = NewExprNode (EXPR_BYTE0);
654 if (IsEasyConst (L, &Val)) {
656 N = GenLiteralExpr ((Val >> 8) & 0xFF);
658 N = NewExprNode (EXPR_BYTE1);
666 if (IsEasyConst (L, &Val)) {
668 N = GenLiteralExpr ((Val >> 16) & 0xFF);
670 N = NewExprNode (EXPR_BYTE2);
682 N = Function (FuncBlank);
686 N = Function (FuncConst);
690 N = GenLiteralExpr (CPUIsets[CPU]);
695 N = Function (FuncDefined);
699 N = Function (FuncMatch);
703 N = Function (FuncReferenced);
707 N = Function (FuncSizeOf);
711 N = Function (FuncStrAt);
715 N = Function (FuncStrLen);
719 N = Function (FuncTCount);
723 N = GenLiteralExpr (time (0));
728 N = GenLiteralExpr (VERSION);
733 N = Function (FuncXMatch);
737 if (LooseCharTerm && Tok == TOK_STRCON && strlen(SVal) == 1) {
738 /* A character constant */
739 N = GenLiteralExpr (TgtTranslateChar (SVal[0]));
741 N = GenLiteralExpr (0); /* Dummy */
742 Error ("Syntax error");
752 static ExprNode* Term (void)
754 /* Read left hand side */
755 ExprNode* Root = Factor ();
757 /* Handle multiplicative operations */
758 while (Tok == TOK_MUL || Tok == TOK_DIV || Tok == TOK_MOD ||
759 Tok == TOK_AND || Tok == TOK_XOR || Tok == TOK_SHL ||
762 long LVal, RVal, Val;
766 /* Remember the token and skip it */
770 /* Move root to left side and read the right side */
774 /* If both expressions are constant, we can evaluate the term */
775 if (IsEasyConst (Left, &LVal) && IsEasyConst (Right, &RVal)) {
784 Error ("Division by zero");
793 Error ("Modulo operation with zero");
809 Val = shl_l (LVal, RVal);
813 Val = shr_l (LVal, RVal);
817 Internal ("Invalid token");
820 /* Generate a literal expression and delete the old left and
825 Root = GenLiteralExpr (Val);
829 /* Generate an expression tree */
832 case TOK_MUL: Op = EXPR_MUL; break;
833 case TOK_DIV: Op = EXPR_DIV; break;
834 case TOK_MOD: Op = EXPR_MOD; break;
835 case TOK_AND: Op = EXPR_AND; break;
836 case TOK_XOR: Op = EXPR_XOR; break;
837 case TOK_SHL: Op = EXPR_SHL; break;
838 case TOK_SHR: Op = EXPR_SHR; break;
839 default: Internal ("Invalid token");
841 Root = NewExprNode (Op);
849 /* Return the expression tree we've created */
855 static ExprNode* SimpleExpr (void)
857 /* Read left hand side */
858 ExprNode* Root = Term ();
860 /* Handle additive operations */
861 while (Tok == TOK_PLUS || Tok == TOK_MINUS || Tok == TOK_OR) {
863 long LVal, RVal, Val;
867 /* Remember the token and skip it */
871 /* Move root to left side and read the right side */
875 /* If both expressions are constant, we can evaluate the term */
876 if (IsEasyConst (Left, &LVal) && IsEasyConst (Right, &RVal)) {
879 case TOK_PLUS: Val = LVal + RVal; break;
880 case TOK_MINUS: Val = LVal - RVal; break;
881 case TOK_OR: Val = LVal | RVal; break;
882 default: Internal ("Invalid token");
885 /* Generate a literal expression and delete the old left and
890 Root = GenLiteralExpr (Val);
894 /* Generate an expression tree */
897 case TOK_PLUS: Op = EXPR_PLUS; break;
898 case TOK_MINUS: Op = EXPR_MINUS; break;
899 case TOK_OR: Op = EXPR_OR; break;
900 default: Internal ("Invalid token");
902 Root = NewExprNode (Op);
909 /* Return the expression tree we've created */
915 static ExprNode* BoolExpr (void)
916 /* Evaluate a boolean expression */
918 /* Read left hand side */
919 ExprNode* Root = SimpleExpr ();
921 /* Handle booleans */
922 while (Tok == TOK_EQ || Tok == TOK_NE || Tok == TOK_LT ||
923 Tok == TOK_GT || Tok == TOK_LE || Tok == TOK_GE) {
925 long LVal, RVal, Val;
929 /* Remember the token and skip it */
933 /* Move root to left side and read the right side */
935 Right = SimpleExpr ();
937 /* If both expressions are constant, we can evaluate the term */
938 if (IsEasyConst (Left, &LVal) && IsEasyConst (Right, &RVal)) {
941 case TOK_EQ: Val = (LVal == RVal); break;
942 case TOK_NE: Val = (LVal != RVal); break;
943 case TOK_LT: Val = (LVal < RVal); break;
944 case TOK_GT: Val = (LVal > RVal); break;
945 case TOK_LE: Val = (LVal <= RVal); break;
946 case TOK_GE: Val = (LVal >= RVal); break;
947 default: Internal ("Invalid token");
950 /* Generate a literal expression and delete the old left and
955 Root = GenLiteralExpr (Val);
959 /* Generate an expression tree */
962 case TOK_EQ: Op = EXPR_EQ; break;
963 case TOK_NE: Op = EXPR_NE; break;
964 case TOK_LT: Op = EXPR_LT; break;
965 case TOK_GT: Op = EXPR_GT; break;
966 case TOK_LE: Op = EXPR_LE; break;
967 case TOK_GE: Op = EXPR_GE; break;
968 default: Internal ("Invalid token");
970 Root = NewExprNode (Op);
977 /* Return the expression tree we've created */
983 static ExprNode* Expr2 (void)
984 /* Boolean operators: AND and XOR */
986 /* Read left hand side */
987 ExprNode* Root = BoolExpr ();
989 /* Handle booleans */
990 while (Tok == TOK_BOOLAND || Tok == TOK_BOOLXOR) {
992 long LVal, RVal, Val;
996 /* Remember the token and skip it */
1000 /* Move root to left side and read the right side */
1002 Right = BoolExpr ();
1004 /* If both expressions are constant, we can evaluate the term */
1005 if (IsEasyConst (Left, &LVal) && IsEasyConst (Right, &RVal)) {
1008 case TOK_BOOLAND: Val = ((LVal != 0) && (RVal != 0)); break;
1009 case TOK_BOOLXOR: Val = ((LVal != 0) ^ (RVal != 0)); break;
1010 default: Internal ("Invalid token");
1013 /* Generate a literal expression and delete the old left and
1018 Root = GenLiteralExpr (Val);
1022 /* Generate an expression tree */
1025 case TOK_BOOLAND: Op = EXPR_BOOLAND; break;
1026 case TOK_BOOLXOR: Op = EXPR_BOOLXOR; break;
1027 default: Internal ("Invalid token");
1029 Root = NewExprNode (Op);
1031 Root->Right = Right;
1036 /* Return the expression tree we've created */
1042 static ExprNode* Expr1 (void)
1043 /* Boolean operators: OR */
1045 /* Read left hand side */
1046 ExprNode* Root = Expr2 ();
1048 /* Handle booleans */
1049 while (Tok == TOK_BOOLOR) {
1051 long LVal, RVal, Val;
1055 /* Remember the token and skip it */
1059 /* Move root to left side and read the right side */
1063 /* If both expressions are constant, we can evaluate the term */
1064 if (IsEasyConst (Left, &LVal) && IsEasyConst (Right, &RVal)) {
1067 case TOK_BOOLOR: Val = ((LVal != 0) || (RVal != 0)); break;
1068 default: Internal ("Invalid token");
1071 /* Generate a literal expression and delete the old left and
1076 Root = GenLiteralExpr (Val);
1080 /* Generate an expression tree */
1083 case TOK_BOOLOR: Op = EXPR_BOOLOR; break;
1084 default: Internal ("Invalid token");
1086 Root = NewExprNode (Op);
1088 Root->Right = Right;
1093 /* Return the expression tree we've created */
1099 static ExprNode* Expr0 (void)
1100 /* Boolean operators: NOT */
1104 /* Handle booleans */
1105 if (Tok == TOK_BOOLNOT) {
1110 /* Skip the operator token */
1113 /* Read the argument */
1116 /* If the argument is const, evaluate it directly */
1117 if (IsEasyConst (Left, &Val)) {
1119 Root = GenLiteralExpr (!Val);
1121 Root = NewExprNode (EXPR_BOOLNOT);
1127 /* Read left hand side */
1132 /* Return the expression tree we've created */
1138 ExprNode* Expression (void)
1139 /* Evaluate an expression, build the expression tree on the heap and return
1140 * a pointer to the root of the tree.
1148 long ConstExpression (void)
1149 /* Parse an expression. Check if the expression is const, and print an error
1150 * message if not. Return the value of the expression, or a dummy, if it is
1156 /* Read the expression */
1157 ExprNode* Expr = Expression ();
1159 /* Study the expression */
1162 StudyExpr (Expr, &D);
1164 /* Check if the expression is constant */
1165 if (ED_IsConst (&D)) {
1168 Error ("Constant expression expected");
1172 /* Free the expression tree and allocated memory for D */
1176 /* Return the value */
1182 void FreeExpr (ExprNode* Root)
1183 /* Free the expression, Root is pointing to. */
1186 FreeExpr (Root->Left);
1187 FreeExpr (Root->Right);
1188 FreeExprNode (Root);
1194 ExprNode* SimplifyExpr (ExprNode* Expr, const ExprDesc* D)
1195 /* Try to simplify the given expression tree */
1197 if (Expr->Op != EXPR_LITERAL && ED_IsConst (D)) {
1198 /* No external references */
1200 Expr = GenLiteralExpr (D->Val);
1207 ExprNode* GenLiteralExpr (long Val)
1208 /* Return an expression tree that encodes the given literal value */
1210 ExprNode* Expr = NewExprNode (EXPR_LITERAL);
1217 ExprNode* GenSymExpr (SymEntry* Sym)
1218 /* Return an expression node that encodes the given symbol */
1220 ExprNode* Expr = NewExprNode (EXPR_SYMBOL);
1222 SymAddExprRef (Sym, Expr);
1228 static ExprNode* GenSectionExpr (unsigned SegNum)
1229 /* Return an expression node for the given section */
1231 ExprNode* Expr = NewExprNode (EXPR_SECTION);
1232 Expr->V.SegNum = SegNum;
1238 ExprNode* GenAddExpr (ExprNode* Left, ExprNode* Right)
1239 /* Generate an addition from the two operands */
1242 if (IsEasyConst (Left, &Val) && Val == 0) {
1245 } else if (IsEasyConst (Right, &Val) && Val == 0) {
1249 ExprNode* Root = NewExprNode (EXPR_PLUS);
1251 Root->Right = Right;
1258 ExprNode* GenCurrentPC (void)
1259 /* Return the current program counter as expression */
1264 /* Create SegmentBase + Offset */
1265 Root = GenAddExpr (GenSectionExpr (GetCurrentSegNum ()),
1266 GenLiteralExpr (GetPC ()));
1268 /* Absolute mode, just return PC value */
1269 Root = GenLiteralExpr (GetPC ());
1277 ExprNode* GenSwapExpr (ExprNode* Expr)
1278 /* Return an extended expression with lo and hi bytes swapped */
1280 ExprNode* N = NewExprNode (EXPR_SWAP);
1287 ExprNode* GenBranchExpr (unsigned Offs)
1288 /* Return an expression that encodes the difference between current PC plus
1289 * offset and the target expression (that is, Expression() - (*+Offs) ).
1296 /* Read Expression() */
1299 /* If the expression is a cheap constant, generate a simpler tree */
1300 if (IsEasyConst (N, &Val)) {
1302 /* Free the constant expression tree */
1305 /* Generate the final expression:
1307 * Val - ((Seg + PC) + Offs)
1308 * Val - Seg - PC - Offs
1309 * (Val - PC - Offs) - Seg
1311 Root = GenLiteralExpr (Val - GetPC () - Offs);
1314 Root = NewExprNode (EXPR_MINUS);
1316 Root->Right = GenSectionExpr (GetCurrentSegNum ());
1321 /* Generate the expression:
1323 * N - ((Seg + PC) + Offs)
1324 * N - Seg - PC - Offs
1325 * N - (PC + Offs) - Seg
1327 Root = NewExprNode (EXPR_MINUS);
1329 Root->Right = GenLiteralExpr (GetPC () + Offs);
1332 Root = NewExprNode (EXPR_MINUS);
1334 Root->Right = GenSectionExpr (GetCurrentSegNum ());
1338 /* Return the result */
1344 ExprNode* GenULabelExpr (unsigned Num)
1345 /* Return an expression for an unnamed label with the given index */
1347 ExprNode* Node = NewExprNode (EXPR_ULABEL);
1350 /* Return the new node */
1356 ExprNode* GenByteExpr (ExprNode* Expr)
1357 /* Force the given expression into a byte and return the result */
1359 /* Use the low byte operator to force the expression into byte size */
1360 ExprNode* Root = NewExprNode (EXPR_BYTE0);
1363 /* Return the result */
1369 ExprNode* GenWordExpr (ExprNode* Expr)
1370 /* Force the given expression into a word and return the result. */
1372 /* AND the expression by $FFFF to force it into word size */
1373 ExprNode* Root = NewExprNode (EXPR_AND);
1375 Root->Right = GenLiteralExpr (0xFFFF);
1377 /* Return the result */
1383 ExprNode* GenNE (ExprNode* Expr, long Val)
1384 /* Generate an expression that compares Expr and Val for inequality */
1386 /* Generate a compare node */
1387 ExprNode* Root = NewExprNode (EXPR_NE);
1389 Root->Right = GenLiteralExpr (Val);
1391 /* Return the result */
1397 int IsConstExpr (ExprNode* Expr, long* Val)
1398 /* Return true if the given expression is a constant expression, that is, one
1399 * with no references to external symbols. If Val is not NULL and the
1400 * expression is constant, the constant value is stored here.
1405 /* Study the expression */
1408 StudyExpr (Expr, &D);
1410 /* Check if the expression is constant */
1411 IsConst = ED_IsConst (&D);
1412 if (IsConst && Val != 0) {
1416 /* Delete allocated memory and return the result */
1423 static void CheckAddrSize (const ExprNode* N, unsigned char* AddrSize)
1424 /* Internal routine that is recursively called to check for the address size
1425 * of the expression tree.
1429 unsigned char Left, Right;
1432 switch (N->Op & EXPR_TYPEMASK) {
1438 if (SymIsZP (N->V.Sym)) {
1439 if (*AddrSize < ADDR_SIZE_ZP) {
1440 *AddrSize = ADDR_SIZE_ZP;
1442 } else if (SymHasExpr (N->V.Sym)) {
1443 /* Check if this expression is a byte expression */
1444 CheckAddrSize (GetSymExpr (N->V.Sym), AddrSize);
1446 /* Undefined symbol, use absolute */
1447 if (*AddrSize < ADDR_SIZE_ABS) {
1448 *AddrSize = ADDR_SIZE_ABS;
1454 A = GetSegAddrSize (N->V.SegNum);
1455 if (A > *AddrSize) {
1463 case EXPR_UNARYNODE:
1470 /* No need to look at the expression */
1471 *AddrSize = ADDR_SIZE_ZP;
1476 /* No need to look at the expression */
1477 *AddrSize = ADDR_SIZE_ABS;
1481 CheckAddrSize (N->Left, AddrSize);
1486 case EXPR_BINARYNODE:
1487 Left = Right = ADDR_SIZE_DEFAULT;
1488 CheckAddrSize (N->Left, &Left);
1489 CheckAddrSize (N->Right, &Right);
1490 A = (Left > Right)? Left : Right;
1491 if (A > *AddrSize) {
1497 Internal ("Unknown expression op: %02X", N->Op);
1504 int IsByteExpr (ExprNode* Root)
1505 /* Return true if this is a byte expression */
1509 if (IsConstExpr (Root, &Val)) {
1510 return IsByteRange (Val);
1512 unsigned char AddrSize = ADDR_SIZE_DEFAULT;
1513 CheckAddrSize (Root, &AddrSize);
1514 return (AddrSize == ADDR_SIZE_ZP);
1520 ExprNode* CloneExpr (ExprNode* Expr)
1521 /* Clone the given expression tree. The function will simply clone symbol
1522 * nodes, it will not resolve them.
1527 /* Accept NULL pointers */
1532 /* Clone the node */
1536 Clone = GenLiteralExpr (Expr->V.Val);
1540 Clone = GenULabelExpr (Expr->V.Val);
1544 Clone = GenSymExpr (Expr->V.Sym);
1548 Clone = GenSectionExpr (Expr->V.SegNum);
1552 /* Generate a new node */
1553 Clone = NewExprNode (Expr->Op);
1554 /* Clone the tree nodes */
1555 Clone->Left = CloneExpr (Expr->Left);
1556 Clone->Right = CloneExpr (Expr->Right);
1566 void WriteExpr (ExprNode* Expr)
1567 /* Write the given expression to the object file */
1569 /* Null expressions are encoded by a type byte of zero */
1571 ObjWrite8 (EXPR_NULL);
1575 /* If the is a leafnode, write the expression attribute, otherwise
1576 * write the expression operands.
1581 ObjWrite8 (EXPR_LITERAL);
1582 ObjWrite32 (Expr->V.Val);
1586 if (SymIsImport (Expr->V.Sym)) {
1587 ObjWrite8 (EXPR_SYMBOL);
1588 ObjWriteVar (GetSymIndex (Expr->V.Sym));
1590 WriteExpr (GetSymExpr (Expr->V.Sym));
1595 ObjWrite8 (EXPR_SECTION);
1596 ObjWrite8 (Expr->V.SegNum);
1600 WriteExpr (ULabResolve (Expr->V.Val));
1604 /* Not a leaf node */
1605 ObjWrite8 (Expr->Op);
1606 WriteExpr (Expr->Left);
1607 WriteExpr (Expr->Right);