1 /*****************************************************************************/
5 /* Expression evaluation for the ca65 macroassembler */
9 /* (C) 1998-2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 /*****************************************************************************/
58 /*****************************************************************************/
60 /*****************************************************************************/
64 /* Since all expressions are first packed into expression trees, and each
65 * expression tree node is allocated on the heap, we add some type of special
66 * purpose memory allocation here: Instead of freeing the nodes, we save some
67 * number of freed nodes for later and remember them in a single linked list
68 * using the Left link.
70 #define MAX_FREE_NODES 64
71 static ExprNode* FreeExprNodes = 0;
72 static unsigned FreeNodeCount = 0;
76 /*****************************************************************************/
78 /*****************************************************************************/
82 static ExprNode* NewExprNode (void)
83 /* Create a new expression node */
87 /* Do we have some nodes in the list already? */
89 /* Use first node from list */
91 FreeExprNodes = N->Left;
93 /* Allocate fresh memory */
94 N = xmalloc (sizeof (ExprNode));
97 N->Left = N->Right = 0;
105 static void FreeExprNode (ExprNode* E)
109 if (FreeNodeCount < MAX_FREE_NODES) {
110 /* Remember this node for later */
111 E->Left = FreeExprNodes;
114 /* Free the memory */
122 /*****************************************************************************/
124 /*****************************************************************************/
128 static ExprNode* Expr0 (void);
132 int IsByteRange (long Val)
133 /* Return true if this is a byte value */
135 return (Val & ~0xFFL) == 0;
140 int IsWordRange (long Val)
141 /* Return true if this is a word value */
143 return (Val & ~0xFFFF) == 0;
148 static int FuncBlank (void)
149 /* Handle the .BLANK builtin function */
151 /* Assume no tokens if the closing brace follows (this is not correct in
152 * all cases, since the token may be the closing brace, but this will
153 * give a syntax error anyway and may not be handled by .BLANK.
155 if (Tok == TOK_RPAREN) {
159 /* Skip any tokens */
161 while (Tok != TOK_SEP && Tok != TOK_EOF) {
162 if (Tok == TOK_LPAREN) {
164 } else if (Tok == TOK_RPAREN) {
180 static int FuncConst (void)
181 /* Handle the .CONST builtin function */
183 /* Read an expression */
184 ExprNode* Expr = Expression ();
186 /* Check the constness of the expression */
187 int Result = IsConstExpr (Expr);
189 /* Free the expression */
198 static int FuncDefined (void)
199 /* Handle the .DEFINED builtin function */
203 if (Tok != TOK_IDENT) {
204 Error (ERR_IDENT_EXPECTED);
205 if (Tok != TOK_RPAREN) {
209 Result = SymIsDef (SVal);
219 static int DoMatch (enum TC EqualityLevel)
220 /* Handle the .MATCH and .XMATCH builtin functions */
227 /* A list of tokens follows. Read this list and remember it building a
228 * single linked list of tokens including attributes. The list is
229 * terminated by a comma.
231 while (Tok != TOK_COMMA) {
233 /* We may not end-of-line of end-of-file here */
234 if (Tok == TOK_SEP || Tok == TOK_EOF) {
235 Error (ERR_UNEXPECTED_EOL);
239 /* Get a node with this token */
240 Node = NewTokNode ();
242 /* Insert the node into the list */
257 /* Read the second list which is terminated by the right parenthesis and
258 * compare each token against the one in the first list.
262 while (Tok != TOK_RPAREN) {
264 /* We may not end-of-line of end-of-file here */
265 if (Tok == TOK_SEP || Tok == TOK_EOF) {
266 Error (ERR_UNEXPECTED_EOL);
270 /* Compare the tokens if the result is not already known */
273 /* The second list is larger than the first one */
275 } else if (TokCmp (Node) < EqualityLevel) {
276 /* Tokens do not match */
281 /* Next token in first list */
286 /* Next token in current list */
290 /* Check if there are remaining tokens in the first list */
295 /* Free the token list */
302 /* Done, return the result */
308 static int FuncMatch (void)
309 /* Handle the .MATCH function */
311 return DoMatch (tcSameToken);
316 static int FuncReferenced (void)
317 /* Handle the .REFERENCED builtin function */
321 if (Tok != TOK_IDENT) {
322 Error (ERR_IDENT_EXPECTED);
323 if (Tok != TOK_RPAREN) {
327 Result = SymIsRef (SVal);
337 static int FuncStrAt (void)
338 /* Handle the .STRAT function */
340 char Str [sizeof(SVal)];
343 /* String constant expected */
344 if (Tok != TOK_STRCON) {
345 Error (ERR_STRCON_EXPECTED);
351 /* Remember the string and skip it */
355 /* Comma must follow */
358 /* Expression expected */
359 Index = ConstExpression ();
361 /* Must be a valid index */
362 if (Index >= strlen (Str)) {
367 /* Return the char, handle as unsigned */
368 return (unsigned char) Str[(size_t)Index];
373 static int FuncStrLen (void)
374 /* Handle the .STRLEN function */
376 /* String constant expected */
377 if (Tok != TOK_STRCON) {
379 Error (ERR_STRCON_EXPECTED);
380 /* Smart error recovery */
381 if (Tok != TOK_RPAREN) {
388 /* Get the length of the string */
389 int Len = strlen (SVal);
391 /* Skip the string */
394 /* Return the length */
402 static int FuncTCount (void)
403 /* Handle the .TCOUNT function */
405 /* We have a list of tokens that ends with the closing paren. Skip
406 * the tokens, handling nested braces and count them.
410 while (Parens != 0 || Tok != TOK_RPAREN) {
412 /* Check for end of line or end of input. Since the calling function
413 * will check for the closing paren, we don't need to print an error
414 * here, just bail out.
416 if (Tok == TOK_SEP || Tok == TOK_EOF) {
423 /* Keep track of the nesting level */
425 case TOK_LPAREN: ++Parens; break;
426 case TOK_RPAREN: --Parens; break;
434 /* Return the number of tokens */
440 static int FuncXMatch (void)
441 /* Handle the .XMATCH function */
443 return DoMatch (tcIdentical);
448 static ExprNode* Function (int (*F) (void))
449 /* Handle builtin functions */
453 /* Skip the keyword */
456 /* Expression must be enclosed in braces */
457 if (Tok != TOK_LPAREN) {
458 Error (ERR_LPAREN_EXPECTED);
460 return LiteralExpr (0);
464 /* Call the function itself */
467 /* Closing brace must follow */
470 /* Return an expression node with the boolean code */
471 return LiteralExpr (Result);
476 static ExprNode* Factor (void)
484 N = LiteralExpr (IVal);
489 N = LiteralExpr (TgtTranslateChar (IVal));
495 if (Tok != TOK_IDENT) {
496 Error (ERR_IDENT_EXPECTED);
497 N = LiteralExpr (0); /* Dummy */
499 S = SymRefGlobal (SVal);
500 if (SymIsConst (S)) {
501 /* Use the literal value instead */
502 N = LiteralExpr (GetSymVal (S));
504 /* Create symbol node */
515 if (SymIsConst (S)) {
516 /* Use the literal value instead */
517 N = LiteralExpr (GetSymVal (S));
519 /* Create symbol node */
536 N->Op = EXPR_UNARY_MINUS;
573 N = Function (FuncBlank);
577 N = Function (FuncConst);
581 N = LiteralExpr (GetCPU());
586 N = Function (FuncDefined);
590 N = Function (FuncMatch);
594 N = Function (FuncReferenced);
598 N = Function (FuncStrAt);
602 N = Function (FuncStrLen);
606 N = Function (FuncTCount);
610 N = Function (FuncXMatch);
614 if (LooseCharTerm && Tok == TOK_STRCON && strlen(SVal) == 1) {
615 /* A character constant */
616 N = LiteralExpr (TgtTranslateChar (SVal[0]));
618 N = LiteralExpr (0); /* Dummy */
629 static ExprNode* Term (void)
633 /* Read left hand side */
636 /* Handle multiplicative operations */
637 while (Tok == TOK_MUL || Tok == TOK_DIV || Tok == TOK_MOD ||
638 Tok == TOK_AND || Tok == TOK_XOR || Tok == TOK_SHL ||
641 /* Create a new node and insert the left expression */
642 ExprNode* Left = Root;
643 Root = NewExprNode ();
646 /* Determine the operator token */
648 case TOK_MUL: Root->Op = EXPR_MUL; break;
649 case TOK_DIV: Root->Op = EXPR_DIV; break;
650 case TOK_MOD: Root->Op = EXPR_MOD; break;
651 case TOK_AND: Root->Op = EXPR_AND; break;
652 case TOK_XOR: Root->Op = EXPR_XOR; break;
653 case TOK_SHL: Root->Op = EXPR_SHL; break;
654 case TOK_SHR: Root->Op = EXPR_SHR; break;
655 default: Internal ("Invalid token");
659 /* Parse the right hand side */
660 Root->Right = Factor ();
664 /* Return the expression tree we've created */
670 static ExprNode* SimpleExpr (void)
674 /* Read left hand side */
677 /* Handle additive operations */
678 while (Tok == TOK_PLUS || Tok == TOK_MINUS || Tok == TOK_OR) {
680 /* Create a new node and insert the left expression */
681 ExprNode* Left = Root;
682 Root = NewExprNode ();
685 /* Determine the operator token */
687 case TOK_PLUS: Root->Op = EXPR_PLUS; break;
688 case TOK_MINUS: Root->Op = EXPR_MINUS; break;
689 case TOK_OR: Root->Op = EXPR_OR; break;
690 default: Internal ("Invalid token");
694 /* Parse the right hand side */
695 Root->Right = Term ();
699 /* Return the expression tree we've created */
705 static ExprNode* BoolExpr (void)
706 /* Evaluate a boolean expression */
708 /* Read left hand side */
709 ExprNode* Root = SimpleExpr ();
711 /* Handle booleans */
712 while (Tok == TOK_EQ || Tok == TOK_NE || Tok == TOK_LT ||
713 Tok == TOK_GT || Tok == TOK_LE || Tok == TOK_GE) {
715 /* Create a new node and insert the left expression */
716 ExprNode* Left = Root;
717 Root = NewExprNode ();
720 /* Determine the operator token */
722 case TOK_EQ: Root->Op = EXPR_EQ; break;
723 case TOK_NE: Root->Op = EXPR_NE; break;
724 case TOK_LT: Root->Op = EXPR_LT; break;
725 case TOK_GT: Root->Op = EXPR_GT; break;
726 case TOK_LE: Root->Op = EXPR_LE; break;
727 case TOK_GE: Root->Op = EXPR_GE; break;
728 default: Internal ("Invalid token");
732 /* Parse the right hand side */
733 Root->Right = SimpleExpr ();
737 /* Return the expression tree we've created */
743 static ExprNode* Expr2 (void)
744 /* Boolean operators: AND and XOR */
746 /* Read left hand side */
747 ExprNode* Root = BoolExpr ();
749 /* Handle booleans */
750 while (Tok == TOK_BAND || Tok == TOK_BXOR) {
752 /* Create a new node and insert the left expression */
753 ExprNode* Left = Root;
754 Root = NewExprNode ();
757 /* Determine the operator token */
759 case TOK_BAND: Root->Op = EXPR_BAND; break;
760 case TOK_BXOR: Root->Op = EXPR_BXOR; break;
761 default: Internal ("Invalid token");
765 /* Parse the right hand side */
766 Root->Right = BoolExpr ();
770 /* Return the expression tree we've created */
776 static ExprNode* Expr1 (void)
777 /* Boolean operators: OR */
779 /* Read left hand side */
780 ExprNode* Root = Expr2 ();
782 /* Handle booleans */
783 while (Tok == TOK_BOR) {
785 /* Create a new node and insert the left expression */
786 ExprNode* Left = Root;
787 Root = NewExprNode ();
790 /* Determine the operator token */
792 case TOK_BOR: Root->Op = EXPR_BOR; break;
793 default: Internal ("Invalid token");
797 /* Parse the right hand side */
798 Root->Right = Expr2 ();
802 /* Return the expression tree we've created */
808 static ExprNode* Expr0 (void)
809 /* Boolean operators: NOT */
813 /* Handle booleans */
814 if (Tok == TOK_BNOT) {
816 /* Create a new node */
817 Root = NewExprNode ();
819 /* Determine the operator token */
821 case TOK_BNOT: Root->Op = EXPR_BNOT; break;
822 default: Internal ("Invalid token");
826 /* Parse the left hand side, allow more BNOTs */
827 Root->Left = Expr0 ();
831 /* Read left hand side */
836 /* Return the expression tree we've created */
842 static ExprNode* SimplifyExpr (ExprNode* Root)
843 /* Try to simplify the given expression tree */
846 SimplifyExpr (Root->Left);
847 SimplifyExpr (Root->Right);
848 if (IsConstExpr (Root)) {
849 /* The complete expression is constant */
850 Root->V.Val = GetExprVal (Root);
851 Root->Op = EXPR_LITERAL;
852 FreeExpr (Root->Left);
853 FreeExpr (Root->Right);
854 Root->Left = Root->Right = 0;
862 ExprNode* Expression (void)
863 /* Evaluate an expression, build the expression tree on the heap and return
864 * a pointer to the root of the tree.
867 return SimplifyExpr (Expr0 ());
872 long ConstExpression (void)
873 /* Parse an expression. Check if the expression is const, and print an error
874 * message if not. Return the value of the expression, or a dummy, if it is
878 /* Read the expression, and call finalize (exception here, since we
881 ExprNode* Expr = FinalizeExpr (Expression ());
883 /* Return the value */
884 if (IsConstExpr (Expr)) {
885 return GetExprVal (Expr);
887 Error (ERR_CONSTEXPR_EXPECTED);
894 ExprNode* LiteralExpr (long Val)
895 /* Return an expression tree that encodes the given literal value */
897 ExprNode* Expr = NewExprNode ();
898 Expr->Op = EXPR_LITERAL;
905 ExprNode* CurrentPC (void)
906 /* Return the current program counter as expression */
912 /* Create SegmentBase + Offset */
913 Left = NewExprNode ();
914 Left->Op = EXPR_SEGMENT;
915 Left->V.SegNum = GetSegNum ();
917 Root = NewExprNode ();
919 Root->Right = LiteralExpr (GetPC ());
920 Root->Op = EXPR_PLUS;
922 /* Absolute mode, just return PC value */
923 Root = LiteralExpr (GetPC ());
931 ExprNode* SwapExpr (ExprNode* Expr)
932 /* Return an extended expression with lo and hi bytes swapped */
934 ExprNode* N = NewExprNode ();
942 ExprNode* BranchExpr (unsigned Offs)
943 /* Return an expression that encodes the difference between current PC plus
944 * offset and the target expression (that is, Expression() - (*+Offs) ).
953 Left = NewExprNode ();
954 Left->Op = EXPR_SEGMENT;
955 Left->V.SegNum = GetSegNum ();
959 N->Right = LiteralExpr (GetPC () + Offs);
962 N = LiteralExpr (GetPC () + Offs);
965 /* Create the root node */
966 Root = NewExprNode ();
967 Root->Left = Expression ();
969 Root->Op = EXPR_MINUS;
971 /* Return the result */
972 return SimplifyExpr (Root);
977 ExprNode* ULabelExpr (unsigned Num)
978 /* Return an expression for an unnamed label with the given index */
980 /* Get an expression node */
981 ExprNode* Node = NewExprNode ();
984 Node->Op = EXPR_ULABEL;
987 /* Return the new node */
993 void FreeExpr (ExprNode* Root)
994 /* Free the expression, Root is pointing to. */
997 FreeExpr (Root->Left);
998 FreeExpr (Root->Right);
1005 ExprNode* ForceWordExpr (ExprNode* Expr)
1006 /* Force the given expression into a word and return the result. */
1008 /* And the expression by $FFFF to force it into word size */
1009 ExprNode* Root = NewExprNode ();
1011 Root->Op = EXPR_AND;
1012 Root->Right = LiteralExpr (0xFFFF);
1014 /* Return the result */
1020 int IsConstExpr (ExprNode* Root)
1021 /* Return true if the given expression is a constant expression, that is, one
1022 * with no references to external symbols.
1028 if (EXPR_IS_LEAF (Root->Op)) {
1036 if (SymHasUserMark (Sym)) {
1040 PError (GetSymPos (Sym), ERR_CIRCULAR_REFERENCE);
1044 Const = SymIsConst (Sym);
1045 SymUnmarkUser (Sym);
1053 } else if (EXPR_IS_UNARY (Root->Op)) {
1055 return IsConstExpr (Root->Left);
1059 /* We must handle shortcut boolean expressions here */
1063 if (IsConstExpr (Root->Left)) {
1064 /* lhs is const, if it is zero, don't eval right */
1065 if (GetExprVal (Root->Left) == 0) {
1068 return IsConstExpr (Root->Right);
1071 /* lhs not const --> tree not const */
1077 if (IsConstExpr (Root->Left)) {
1078 /* lhs is const, if it is not zero, don't eval right */
1079 if (GetExprVal (Root->Left) != 0) {
1082 return IsConstExpr (Root->Right);
1085 /* lhs not const --> tree not const */
1091 /* All others are handled normal */
1092 return IsConstExpr (Root->Left) && IsConstExpr (Root->Right);
1099 static void CheckByteExpr (const ExprNode* N, int* IsByte)
1100 /* Internal routine that is recursively called to check if there is a zeropage
1101 * symbol in the expression tree.
1105 switch (N->Op & EXPR_TYPEMASK) {
1111 if (SymIsZP (N->V.Sym)) {
1117 if (GetSegType (N->V.SegNum) == SEGTYPE_ZP) {
1125 case EXPR_UNARYNODE:
1126 CheckByteExpr (N->Left, IsByte);
1129 case EXPR_BINARYNODE:
1130 CheckByteExpr (N->Left, IsByte);
1131 CheckByteExpr (N->Right, IsByte);
1135 Internal ("Unknown expression op: %02X", N->Op);
1142 int IsByteExpr (ExprNode* Root)
1143 /* Return true if this is a byte expression */
1147 if (IsConstExpr (Root)) {
1148 if (Root->Op != EXPR_LITERAL) {
1149 SimplifyExpr (Root);
1151 return IsByteRange (GetExprVal (Root));
1152 } else if (Root->Op == EXPR_BYTE0 || Root->Op == EXPR_BYTE1 ||
1153 Root->Op == EXPR_BYTE2 || Root->Op == EXPR_BYTE3) {
1154 /* Symbol forced to have byte range */
1157 /* We have undefined symbols in the expression. Assume that the
1158 * expression is a byte expression if there is at least one symbol
1159 * declared as zeropage in it. Being wrong here is not a very big
1160 * problem since the linker knows about all symbols and detects
1161 * error like mixing absolute and zeropage labels.
1164 CheckByteExpr (Root, &IsByte);
1171 long GetExprVal (ExprNode* Expr)
1172 /* Get the value of a constant expression */
1182 return GetSymVal (Expr->V.Sym);
1185 return GetExprVal (Expr->Left) + GetExprVal (Expr->Right);
1188 return GetExprVal (Expr->Left) - GetExprVal (Expr->Right);
1191 return GetExprVal (Expr->Left) * GetExprVal (Expr->Right);
1194 Left = GetExprVal (Expr->Left);
1195 Right = GetExprVal (Expr->Right);
1197 Error (ERR_DIV_BY_ZERO);
1200 return Left / Right;
1203 Left = GetExprVal (Expr->Left);
1204 Right = GetExprVal (Expr->Right);
1206 Error (ERR_MOD_BY_ZERO);
1209 return Left % Right;
1212 return GetExprVal (Expr->Left) | GetExprVal (Expr->Right);
1215 return GetExprVal (Expr->Left) ^ GetExprVal (Expr->Right);
1218 return GetExprVal (Expr->Left) & GetExprVal (Expr->Right);
1221 return GetExprVal (Expr->Left) << GetExprVal (Expr->Right);
1224 return GetExprVal (Expr->Left) >> GetExprVal (Expr->Right);
1227 return (GetExprVal (Expr->Left) == GetExprVal (Expr->Right));
1230 return (GetExprVal (Expr->Left) != GetExprVal (Expr->Right));
1233 return (GetExprVal (Expr->Left) < GetExprVal (Expr->Right));
1236 return (GetExprVal (Expr->Left) > GetExprVal (Expr->Right));
1239 return (GetExprVal (Expr->Left) <= GetExprVal (Expr->Right));
1242 return (GetExprVal (Expr->Left) >= GetExprVal (Expr->Right));
1244 case EXPR_UNARY_MINUS:
1245 return -GetExprVal (Expr->Left);
1248 return ~GetExprVal (Expr->Left);
1251 return GetExprVal (Expr->Left) & 0xFF;
1254 return (GetExprVal (Expr->Left) >> 8) & 0xFF;
1257 return (GetExprVal (Expr->Left) >> 16) & 0xFF;
1260 return (GetExprVal (Expr->Left) >> 24) & 0xFF;
1263 Left = GetExprVal (Expr->Left);
1264 return ((Left >> 8) & 0x00FF) | ((Left << 8) & 0xFF00);
1267 return GetExprVal (Expr->Left) && GetExprVal (Expr->Right);
1270 return GetExprVal (Expr->Left) || GetExprVal (Expr->Right);
1273 return (GetExprVal (Expr->Left) != 0) ^ (GetExprVal (Expr->Right) != 0);
1276 return !GetExprVal (Expr->Left);
1279 Internal ("GetExprVal called for EXPR_ULABEL");
1284 Internal ("Unknown Op type: %u", Expr->Op);
1292 static ExprNode* RemoveSyms (ExprNode* Expr, int MustClone)
1293 /* Remove resolved symbols from the tree by cloning symbol expressions */
1295 /* Accept NULL pointers */
1300 /* Special node handling */
1304 if (SymHasExpr (Expr->V.Sym)) {
1305 /* The symbol has an expression tree */
1306 SymEntry* Sym = Expr->V.Sym;
1307 if (SymHasUserMark (Sym)) {
1308 /* Circular definition */
1312 PError (GetSymPos (Sym), ERR_CIRCULAR_REFERENCE);
1313 return LiteralExpr (0); /* Return a dummy value */
1316 Expr = RemoveSyms (GetSymExpr (Sym), 1);
1317 SymUnmarkUser (Sym);
1319 } else if (SymIsConst (Expr->V.Sym)) {
1320 /* The symbol is a constant */
1321 return LiteralExpr (GetSymVal (Expr->V.Sym));
1326 if (ULabCanResolve ()) {
1327 ExprNode* NewExpr = ULabResolve (Expr->V.Val);
1335 /* Clone the current node if needed */
1338 /* Create a new node */
1339 ExprNode* Clone = NewExprNode ();
1341 /* Clone the operation */
1342 Clone->Op = Expr->Op;
1344 /* Clone the attribute if needed */
1349 Clone->V.Val = Expr->V.Val;
1353 Clone->V.Sym = Expr->V.Sym;
1357 Clone->V.SegNum = Expr->V.SegNum;
1362 /* Clone the tree nodes */
1363 Clone->Left = RemoveSyms (Expr->Left, MustClone);
1364 Clone->Right = RemoveSyms (Expr->Right, MustClone);
1371 /* Nothing to clone */
1372 Expr->Left = RemoveSyms (Expr->Left, MustClone);
1373 Expr->Right = RemoveSyms (Expr->Right, MustClone);
1383 static ExprNode* ConstExtract (ExprNode* Expr, long* Val, int Sign)
1384 /* Extract and evaluate all constant factors in an subtree that has only
1385 * additions and subtractions.
1388 if (Expr->Op == EXPR_LITERAL) {
1390 *Val -= Expr->V.Val;
1392 *Val += Expr->V.Val;
1394 FreeExprNode (Expr);
1398 if (Expr->Op == EXPR_PLUS || Expr->Op == EXPR_MINUS) {
1401 Left = ConstExtract (Expr->Left, Val, Sign);
1402 if (Expr->Op == EXPR_MINUS) {
1405 Right = ConstExtract (Expr->Right, Val, Sign);
1406 if (Left == 0 && Right == 0) {
1407 FreeExprNode (Expr);
1409 } else if (Left == 0) {
1410 FreeExprNode (Expr);
1412 } else if (Right == 0) {
1413 FreeExprNode (Expr);
1416 /* Check for SEG - SEG which is now possible */
1417 if (Left->Op == EXPR_SEGMENT && Right->Op == EXPR_SEGMENT &&
1418 Left->V.SegNum == Right->V.SegNum) {
1419 /* SEG - SEG, remove it completely */
1420 FreeExprNode (Left);
1421 FreeExprNode (Right);
1422 FreeExprNode (Expr);
1426 Expr->Right = Right;
1432 /* Some other sort of node, finalize the terms */
1434 Expr->Left = FinalizeExpr (Expr->Left);
1437 Expr->Right = FinalizeExpr (Expr->Right);
1445 ExprNode* FinalizeExpr (ExprNode* Expr)
1446 /* Resolve any symbols by cloning the symbol expression tree instead of the
1447 * symbol reference, then try to simplify the expression as much as possible.
1448 * This function must only be called if all symbols are resolved (no undefined
1455 Expr = RemoveSyms (Expr, 0);
1456 Expr = ConstExtract (Expr, &Val, 1);
1458 /* Reduced to a literal value */
1459 Expr = LiteralExpr (Val);
1461 /* Extracted a value */
1465 N->Right = LiteralExpr (Val);
1473 ExprNode* CloneExpr (ExprNode* Expr)
1474 /* Clone the given expression tree. The function will simply clone symbol
1475 * nodes, it will not resolve them.
1480 /* Accept NULL pointers */
1485 /* Get a new node */
1486 Clone = NewExprNode ();
1488 /* Clone the operation */
1489 Clone->Op = Expr->Op;
1491 /* Clone the attribute if needed */
1496 Clone->V.Val = Expr->V.Val;
1500 Clone->V.Sym = Expr->V.Sym;
1504 Clone->V.SegNum = Expr->V.SegNum;
1509 /* Clone the tree nodes */
1510 Clone->Left = CloneExpr (Expr->Left);
1511 Clone->Right = CloneExpr (Expr->Right);
1519 void WriteExpr (ExprNode* Expr)
1520 /* Write the given expression to the object file */
1522 /* Null expressions are encoded by a type byte of zero */
1528 /* Write the expression code */
1529 ObjWrite8 (Expr->Op);
1531 /* If the is a leafnode, write the expression attribute, otherwise
1532 * write the expression operands.
1537 ObjWrite32 (Expr->V.Val);
1541 /* Maybe we should use a code here? */
1542 CHECK (SymIsImport (Expr->V.Sym)); /* Safety */
1543 ObjWrite16 (GetSymIndex (Expr->V.Sym));
1547 ObjWrite8 (Expr->V.SegNum);
1551 Internal ("WriteExpr: Cannot write EXPR_ULABEL nodes");
1555 /* Not a leaf node */
1556 WriteExpr (Expr->Left);
1557 WriteExpr (Expr->Right);