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 /*****************************************************************************/
56 /*****************************************************************************/
58 /*****************************************************************************/
62 /* Since all expressions are first packed into expression trees, and each
63 * expression tree node is allocated on the heap, we add some type of special
64 * purpose memory allocation here: Instead of freeing the nodes, we save some
65 * number of freed nodes for later and remember them in a single linked list
66 * using the Left link.
68 #define MAX_FREE_NODES 64
69 static ExprNode* FreeExprNodes = 0;
70 static unsigned FreeNodeCount = 0;
74 /*****************************************************************************/
76 /*****************************************************************************/
80 static ExprNode* NewExprNode (void)
81 /* Create a new expression node */
85 /* Do we have some nodes in the list already? */
87 /* Use first node from list */
89 FreeExprNodes = N->Left;
91 /* Allocate fresh memory */
92 N = xmalloc (sizeof (ExprNode));
95 N->Left = N->Right = 0;
103 static void FreeExprNode (ExprNode* E)
107 if (FreeNodeCount < MAX_FREE_NODES) {
108 /* Remember this node for later */
109 E->Left = FreeExprNodes;
112 /* Free the memory */
120 /*****************************************************************************/
122 /*****************************************************************************/
126 static ExprNode* Expr0 (void);
130 int IsByteRange (long Val)
131 /* Return true if this is a byte value */
133 return (Val & ~0xFFL) == 0;
138 int IsWordRange (long Val)
139 /* Return true if this is a word value */
141 return (Val & ~0xFFFF) == 0;
146 static int FuncBlank (void)
147 /* Handle the .BLANK builtin function */
149 /* Assume no tokens if the closing brace follows (this is not correct in
150 * all cases, since the token may be the closing brace, but this will
151 * give a syntax error anyway and may not be handled by .BLANK.
153 if (Tok == TOK_RPAREN) {
157 /* Skip any tokens */
159 while (Tok != TOK_SEP && Tok != TOK_EOF) {
160 if (Tok == TOK_LPAREN) {
162 } else if (Tok == TOK_RPAREN) {
178 static int FuncConst (void)
179 /* Handle the .CONST builtin function */
181 /* Read an expression */
182 ExprNode* Expr = Expression ();
184 /* Check the constness of the expression */
185 int Result = IsConstExpr (Expr);
187 /* Free the expression */
196 static int FuncDefined (void)
197 /* Handle the .DEFINED builtin function */
201 if (Tok != TOK_IDENT) {
202 Error (ERR_IDENT_EXPECTED);
203 if (Tok != TOK_RPAREN) {
207 Result = SymIsDef (SVal);
217 static int DoMatch (enum TC EqualityLevel)
218 /* Handle the .MATCH and .XMATCH builtin functions */
225 /* A list of tokens follows. Read this list and remember it building a
226 * single linked list of tokens including attributes. The list is
227 * terminated by a comma.
229 while (Tok != TOK_COMMA) {
231 /* We may not end-of-line of end-of-file here */
232 if (Tok == TOK_SEP || Tok == TOK_EOF) {
233 Error (ERR_UNEXPECTED_EOL);
237 /* Get a node with this token */
238 Node = NewTokNode ();
240 /* Insert the node into the list */
255 /* Read the second list which is terminated by the right parenthesis and
256 * compare each token against the one in the first list.
260 while (Tok != TOK_RPAREN) {
262 /* We may not end-of-line of end-of-file here */
263 if (Tok == TOK_SEP || Tok == TOK_EOF) {
264 Error (ERR_UNEXPECTED_EOL);
268 /* Compare the tokens if the result is not already known */
271 /* The second list is larger than the first one */
273 } else if (TokCmp (Node) < EqualityLevel) {
274 /* Tokens do not match */
279 /* Next token in first list */
284 /* Next token in current list */
288 /* Check if there are remaining tokens in the first list */
293 /* Free the token list */
300 /* Done, return the result */
306 static int FuncMatch (void)
307 /* Handle the .MATCH function */
309 return DoMatch (tcSameToken);
314 static int FuncReferenced (void)
315 /* Handle the .REFERENCED builtin function */
319 if (Tok != TOK_IDENT) {
320 Error (ERR_IDENT_EXPECTED);
321 if (Tok != TOK_RPAREN) {
325 Result = SymIsRef (SVal);
335 static int FuncStrAt (void)
336 /* Handle the .STRAT function */
338 char Str [sizeof(SVal)];
341 /* String constant expected */
342 if (Tok != TOK_STRCON) {
343 Error (ERR_STRCON_EXPECTED);
349 /* Remember the string and skip it */
353 /* Comma must follow */
356 /* Expression expected */
357 Index = ConstExpression ();
359 /* Must be a valid index */
360 if (Index >= strlen (Str)) {
365 /* Return the char, handle as unsigned */
366 return (unsigned char) Str[(size_t)Index];
371 static int FuncStrLen (void)
372 /* Handle the .STRLEN function */
374 /* String constant expected */
375 if (Tok != TOK_STRCON) {
377 Error (ERR_STRCON_EXPECTED);
378 /* Smart error recovery */
379 if (Tok != TOK_RPAREN) {
386 /* Get the length of the string */
387 int Len = strlen (SVal);
389 /* Skip the string */
392 /* Return the length */
400 static int FuncTCount (void)
401 /* Handle the .TCOUNT function */
403 /* We have a list of tokens that ends with the closing paren. Skip
404 * the tokens, handling nested braces and count them.
408 while (Parens != 0 || Tok != TOK_RPAREN) {
410 /* Check for end of line or end of input. Since the calling function
411 * will check for the closing paren, we don't need to print an error
412 * here, just bail out.
414 if (Tok == TOK_SEP || Tok == TOK_EOF) {
421 /* Keep track of the nesting level */
423 case TOK_LPAREN: ++Parens; break;
424 case TOK_RPAREN: --Parens; break;
432 /* Return the number of tokens */
438 static int FuncXMatch (void)
439 /* Handle the .XMATCH function */
441 return DoMatch (tcIdentical);
446 static ExprNode* Function (int (*F) (void))
447 /* Handle builtin functions */
451 /* Skip the keyword */
454 /* Expression must be enclosed in braces */
455 if (Tok != TOK_LPAREN) {
456 Error (ERR_LPAREN_EXPECTED);
458 return LiteralExpr (0);
462 /* Call the function itself */
465 /* Closing brace must follow */
468 /* Return an expression node with the boolean code */
469 return LiteralExpr (Result);
474 static ExprNode* Factor (void)
482 N = LiteralExpr (IVal);
487 N = LiteralExpr (TgtTranslateChar (IVal));
493 if (Tok != TOK_IDENT) {
494 Error (ERR_IDENT_EXPECTED);
495 N = LiteralExpr (0); /* Dummy */
497 S = SymRefGlobal (SVal);
498 if (SymIsConst (S)) {
499 /* Use the literal value instead */
500 N = LiteralExpr (GetSymVal (S));
502 /* Create symbol node */
513 if (SymIsConst (S)) {
514 /* Use the literal value instead */
515 N = LiteralExpr (GetSymVal (S));
517 /* Create symbol node */
534 N->Op = EXPR_UNARY_MINUS;
571 N = Function (FuncBlank);
575 N = Function (FuncConst);
579 N = LiteralExpr (GetCPU());
584 N = Function (FuncDefined);
588 N = Function (FuncMatch);
592 N = Function (FuncReferenced);
596 N = Function (FuncStrAt);
600 N = Function (FuncStrLen);
604 N = Function (FuncTCount);
608 N = Function (FuncXMatch);
612 N = LiteralExpr (0); /* Dummy */
622 static ExprNode* Term (void)
626 /* Read left hand side */
629 /* Handle multiplicative operations */
630 while (Tok == TOK_MUL || Tok == TOK_DIV || Tok == TOK_MOD ||
631 Tok == TOK_AND || Tok == TOK_XOR || Tok == TOK_SHL ||
634 /* Create a new node and insert the left expression */
635 ExprNode* Left = Root;
636 Root = NewExprNode ();
639 /* Determine the operator token */
641 case TOK_MUL: Root->Op = EXPR_MUL; break;
642 case TOK_DIV: Root->Op = EXPR_DIV; break;
643 case TOK_MOD: Root->Op = EXPR_MOD; break;
644 case TOK_AND: Root->Op = EXPR_AND; break;
645 case TOK_XOR: Root->Op = EXPR_XOR; break;
646 case TOK_SHL: Root->Op = EXPR_SHL; break;
647 case TOK_SHR: Root->Op = EXPR_SHR; break;
648 default: Internal ("Invalid token");
652 /* Parse the right hand side */
653 Root->Right = Factor ();
657 /* Return the expression tree we've created */
663 static ExprNode* SimpleExpr (void)
667 /* Read left hand side */
670 /* Handle additive operations */
671 while (Tok == TOK_PLUS || Tok == TOK_MINUS || Tok == TOK_OR) {
673 /* Create a new node and insert the left expression */
674 ExprNode* Left = Root;
675 Root = NewExprNode ();
678 /* Determine the operator token */
680 case TOK_PLUS: Root->Op = EXPR_PLUS; break;
681 case TOK_MINUS: Root->Op = EXPR_MINUS; break;
682 case TOK_OR: Root->Op = EXPR_OR; break;
683 default: Internal ("Invalid token");
687 /* Parse the right hand side */
688 Root->Right = Term ();
692 /* Return the expression tree we've created */
698 static ExprNode* BoolExpr (void)
699 /* Evaluate a boolean expression */
701 /* Read left hand side */
702 ExprNode* Root = SimpleExpr ();
704 /* Handle booleans */
705 while (Tok == TOK_EQ || Tok == TOK_NE || Tok == TOK_LT ||
706 Tok == TOK_GT || Tok == TOK_LE || Tok == TOK_GE) {
708 /* Create a new node and insert the left expression */
709 ExprNode* Left = Root;
710 Root = NewExprNode ();
713 /* Determine the operator token */
715 case TOK_EQ: Root->Op = EXPR_EQ; break;
716 case TOK_NE: Root->Op = EXPR_NE; break;
717 case TOK_LT: Root->Op = EXPR_LT; break;
718 case TOK_GT: Root->Op = EXPR_GT; break;
719 case TOK_LE: Root->Op = EXPR_LE; break;
720 case TOK_GE: Root->Op = EXPR_GE; break;
721 default: Internal ("Invalid token");
725 /* Parse the right hand side */
726 Root->Right = SimpleExpr ();
730 /* Return the expression tree we've created */
736 static ExprNode* Expr2 (void)
737 /* Boolean operators: AND and XOR */
739 /* Read left hand side */
740 ExprNode* Root = BoolExpr ();
742 /* Handle booleans */
743 while (Tok == TOK_BAND || Tok == TOK_BXOR) {
745 /* Create a new node and insert the left expression */
746 ExprNode* Left = Root;
747 Root = NewExprNode ();
750 /* Determine the operator token */
752 case TOK_BAND: Root->Op = EXPR_BAND; break;
753 case TOK_BXOR: Root->Op = EXPR_BXOR; break;
754 default: Internal ("Invalid token");
758 /* Parse the right hand side */
759 Root->Right = BoolExpr ();
763 /* Return the expression tree we've created */
769 static ExprNode* Expr1 (void)
770 /* Boolean operators: OR */
772 /* Read left hand side */
773 ExprNode* Root = Expr2 ();
775 /* Handle booleans */
776 while (Tok == TOK_BOR) {
778 /* Create a new node and insert the left expression */
779 ExprNode* Left = Root;
780 Root = NewExprNode ();
783 /* Determine the operator token */
785 case TOK_BOR: Root->Op = EXPR_BOR; break;
786 default: Internal ("Invalid token");
790 /* Parse the right hand side */
791 Root->Right = Expr2 ();
795 /* Return the expression tree we've created */
801 static ExprNode* Expr0 (void)
802 /* Boolean operators: NOT */
806 /* Handle booleans */
807 if (Tok == TOK_BNOT) {
809 /* Create a new node */
810 Root = NewExprNode ();
812 /* Determine the operator token */
814 case TOK_BNOT: Root->Op = EXPR_BNOT; break;
815 default: Internal ("Invalid token");
819 /* Parse the left hand side, allow more BNOTs */
820 Root->Left = Expr0 ();
824 /* Read left hand side */
829 /* Return the expression tree we've created */
835 static ExprNode* SimplifyExpr (ExprNode* Root)
836 /* Try to simplify the given expression tree */
839 SimplifyExpr (Root->Left);
840 SimplifyExpr (Root->Right);
841 if (IsConstExpr (Root)) {
842 /* The complete expression is constant */
843 Root->V.Val = GetExprVal (Root);
844 Root->Op = EXPR_LITERAL;
845 FreeExpr (Root->Left);
846 FreeExpr (Root->Right);
847 Root->Left = Root->Right = 0;
855 ExprNode* Expression (void)
856 /* Evaluate an expression, build the expression tree on the heap and return
857 * a pointer to the root of the tree.
860 return SimplifyExpr (Expr0 ());
865 long ConstExpression (void)
866 /* Parse an expression. Check if the expression is const, and print an error
867 * message if not. Return the value of the expression, or a dummy, if it is
871 /* Read the expression, and call finalize (exception here, since we
874 ExprNode* Expr = FinalizeExpr (Expression ());
876 /* Return the value */
877 if (IsConstExpr (Expr)) {
878 return GetExprVal (Expr);
880 Error (ERR_CONSTEXPR_EXPECTED);
887 ExprNode* LiteralExpr (long Val)
888 /* Return an expression tree that encodes the given literal value */
890 ExprNode* Expr = NewExprNode ();
891 Expr->Op = EXPR_LITERAL;
898 ExprNode* CurrentPC (void)
899 /* Return the current program counter as expression */
905 /* Create SegmentBase + Offset */
906 Left = NewExprNode ();
907 Left->Op = EXPR_SEGMENT;
908 Left->V.SegNum = GetSegNum ();
910 Root = NewExprNode ();
912 Root->Right = LiteralExpr (GetPC ());
913 Root->Op = EXPR_PLUS;
915 /* Absolute mode, just return PC value */
916 Root = LiteralExpr (GetPC ());
924 ExprNode* SwapExpr (ExprNode* Expr)
925 /* Return an extended expression with lo and hi bytes swapped */
927 ExprNode* N = NewExprNode ();
935 ExprNode* BranchExpr (unsigned Offs)
936 /* Return an expression that encodes the difference between current PC plus
937 * offset and the target expression (that is, Expression() - (*+Offs) ).
946 Left = NewExprNode ();
947 Left->Op = EXPR_SEGMENT;
948 Left->V.SegNum = GetSegNum ();
952 N->Right = LiteralExpr (GetPC () + Offs);
955 N = LiteralExpr (GetPC () + Offs);
958 /* Create the root node */
959 Root = NewExprNode ();
960 Root->Left = Expression ();
962 Root->Op = EXPR_MINUS;
964 /* Return the result */
965 return SimplifyExpr (Root);
970 ExprNode* ULabelExpr (unsigned Num)
971 /* Return an expression for an unnamed label with the given index */
973 /* Get an expression node */
974 ExprNode* Node = NewExprNode ();
977 Node->Op = EXPR_ULABEL;
980 /* Return the new node */
986 void FreeExpr (ExprNode* Root)
987 /* Free the expression, Root is pointing to. */
990 FreeExpr (Root->Left);
991 FreeExpr (Root->Right);
998 ExprNode* ForceWordExpr (ExprNode* Expr)
999 /* Force the given expression into a word and return the result. */
1001 /* And the expression by $FFFF to force it into word size */
1002 ExprNode* Root = NewExprNode ();
1004 Root->Op = EXPR_AND;
1005 Root->Right = LiteralExpr (0xFFFF);
1007 /* Return the result */
1013 int IsConstExpr (ExprNode* Root)
1014 /* Return true if the given expression is a constant expression, that is, one
1015 * with no references to external symbols.
1021 if (EXPR_IS_LEAF (Root->Op)) {
1029 if (SymHasUserMark (Sym)) {
1033 PError (GetSymPos (Sym), ERR_CIRCULAR_REFERENCE);
1037 Const = SymIsConst (Sym);
1038 SymUnmarkUser (Sym);
1046 } else if (EXPR_IS_UNARY (Root->Op)) {
1048 return IsConstExpr (Root->Left);
1052 /* We must handle shortcut boolean expressions here */
1056 if (IsConstExpr (Root->Left)) {
1057 /* lhs is const, if it is zero, don't eval right */
1058 if (GetExprVal (Root->Left) == 0) {
1061 return IsConstExpr (Root->Right);
1064 /* lhs not const --> tree not const */
1070 if (IsConstExpr (Root->Left)) {
1071 /* lhs is const, if it is not zero, don't eval right */
1072 if (GetExprVal (Root->Left) != 0) {
1075 return IsConstExpr (Root->Right);
1078 /* lhs not const --> tree not const */
1084 /* All others are handled normal */
1085 return IsConstExpr (Root->Left) && IsConstExpr (Root->Right);
1092 static void CheckByteExpr (const ExprNode* N, int* IsByte)
1093 /* Internal routine that is recursively called to check if there is a zeropage
1094 * symbol in the expression tree.
1098 switch (N->Op & EXPR_TYPEMASK) {
1104 if (SymIsZP (N->V.Sym)) {
1110 if (GetSegType (N->V.SegNum) == SEGTYPE_ZP) {
1118 case EXPR_UNARYNODE:
1119 CheckByteExpr (N->Left, IsByte);
1122 case EXPR_BINARYNODE:
1123 CheckByteExpr (N->Left, IsByte);
1124 CheckByteExpr (N->Right, IsByte);
1128 Internal ("Unknown expression op: %02X", N->Op);
1135 int IsByteExpr (ExprNode* Root)
1136 /* Return true if this is a byte expression */
1140 if (IsConstExpr (Root)) {
1141 if (Root->Op != EXPR_LITERAL) {
1142 SimplifyExpr (Root);
1144 return IsByteRange (GetExprVal (Root));
1145 } else if (Root->Op == EXPR_BYTE0 || Root->Op == EXPR_BYTE1 ||
1146 Root->Op == EXPR_BYTE2 || Root->Op == EXPR_BYTE3) {
1147 /* Symbol forced to have byte range */
1150 /* We have undefined symbols in the expression. Assume that the
1151 * expression is a byte expression if there is at least one symbol
1152 * declared as zeropage in it. Being wrong here is not a very big
1153 * problem since the linker knows about all symbols and detects
1154 * error like mixing absolute and zeropage labels.
1157 CheckByteExpr (Root, &IsByte);
1164 long GetExprVal (ExprNode* Expr)
1165 /* Get the value of a constant expression */
1175 return GetSymVal (Expr->V.Sym);
1178 return GetExprVal (Expr->Left) + GetExprVal (Expr->Right);
1181 return GetExprVal (Expr->Left) - GetExprVal (Expr->Right);
1184 return GetExprVal (Expr->Left) * GetExprVal (Expr->Right);
1187 Left = GetExprVal (Expr->Left);
1188 Right = GetExprVal (Expr->Right);
1190 Error (ERR_DIV_BY_ZERO);
1193 return Left / Right;
1196 Left = GetExprVal (Expr->Left);
1197 Right = GetExprVal (Expr->Right);
1199 Error (ERR_MOD_BY_ZERO);
1202 return Left % Right;
1205 return GetExprVal (Expr->Left) | GetExprVal (Expr->Right);
1208 return GetExprVal (Expr->Left) ^ GetExprVal (Expr->Right);
1211 return GetExprVal (Expr->Left) & GetExprVal (Expr->Right);
1214 return GetExprVal (Expr->Left) << GetExprVal (Expr->Right);
1217 return GetExprVal (Expr->Left) >> GetExprVal (Expr->Right);
1220 return (GetExprVal (Expr->Left) == GetExprVal (Expr->Right));
1223 return (GetExprVal (Expr->Left) != GetExprVal (Expr->Right));
1226 return (GetExprVal (Expr->Left) < GetExprVal (Expr->Right));
1229 return (GetExprVal (Expr->Left) > GetExprVal (Expr->Right));
1232 return (GetExprVal (Expr->Left) <= GetExprVal (Expr->Right));
1235 return (GetExprVal (Expr->Left) >= GetExprVal (Expr->Right));
1237 case EXPR_UNARY_MINUS:
1238 return -GetExprVal (Expr->Left);
1241 return ~GetExprVal (Expr->Left);
1244 return GetExprVal (Expr->Left) & 0xFF;
1247 return (GetExprVal (Expr->Left) >> 8) & 0xFF;
1250 return (GetExprVal (Expr->Left) >> 16) & 0xFF;
1253 return (GetExprVal (Expr->Left) >> 24) & 0xFF;
1256 Left = GetExprVal (Expr->Left);
1257 return ((Left >> 8) & 0x00FF) | ((Left << 8) & 0xFF00);
1260 return GetExprVal (Expr->Left) && GetExprVal (Expr->Right);
1263 return GetExprVal (Expr->Left) || GetExprVal (Expr->Right);
1266 return (GetExprVal (Expr->Left) != 0) ^ (GetExprVal (Expr->Right) != 0);
1269 return !GetExprVal (Expr->Left);
1272 Internal ("GetExprVal called for EXPR_ULABEL");
1277 Internal ("Unknown Op type: %u", Expr->Op);
1285 static ExprNode* RemoveSyms (ExprNode* Expr, int MustClone)
1286 /* Remove resolved symbols from the tree by cloning symbol expressions */
1288 /* Accept NULL pointers */
1293 /* Special node handling */
1297 if (SymHasExpr (Expr->V.Sym)) {
1298 /* The symbol has an expression tree */
1299 SymEntry* Sym = Expr->V.Sym;
1300 if (SymHasUserMark (Sym)) {
1301 /* Circular definition */
1305 PError (GetSymPos (Sym), ERR_CIRCULAR_REFERENCE);
1306 return LiteralExpr (0); /* Return a dummy value */
1309 Expr = RemoveSyms (GetSymExpr (Sym), 1);
1310 SymUnmarkUser (Sym);
1312 } else if (SymIsConst (Expr->V.Sym)) {
1313 /* The symbol is a constant */
1314 return LiteralExpr (GetSymVal (Expr->V.Sym));
1319 if (ULabCanResolve ()) {
1320 ExprNode* NewExpr = ULabResolve (Expr->V.Val);
1328 /* Clone the current node if needed */
1331 /* Create a new node */
1332 ExprNode* Clone = NewExprNode ();
1334 /* Clone the operation */
1335 Clone->Op = Expr->Op;
1337 /* Clone the attribute if needed */
1342 Clone->V.Val = Expr->V.Val;
1346 Clone->V.Sym = Expr->V.Sym;
1350 Clone->V.SegNum = Expr->V.SegNum;
1355 /* Clone the tree nodes */
1356 Clone->Left = RemoveSyms (Expr->Left, MustClone);
1357 Clone->Right = RemoveSyms (Expr->Right, MustClone);
1364 /* Nothing to clone */
1365 Expr->Left = RemoveSyms (Expr->Left, MustClone);
1366 Expr->Right = RemoveSyms (Expr->Right, MustClone);
1376 static ExprNode* ConstExtract (ExprNode* Expr, long* Val, int Sign)
1377 /* Extract and evaluate all constant factors in an subtree that has only
1378 * additions and subtractions.
1381 if (Expr->Op == EXPR_LITERAL) {
1383 *Val -= Expr->V.Val;
1385 *Val += Expr->V.Val;
1387 FreeExprNode (Expr);
1391 if (Expr->Op == EXPR_PLUS || Expr->Op == EXPR_MINUS) {
1394 Left = ConstExtract (Expr->Left, Val, Sign);
1395 if (Expr->Op == EXPR_MINUS) {
1398 Right = ConstExtract (Expr->Right, Val, Sign);
1399 if (Left == 0 && Right == 0) {
1400 FreeExprNode (Expr);
1402 } else if (Left == 0) {
1403 FreeExprNode (Expr);
1405 } else if (Right == 0) {
1406 FreeExprNode (Expr);
1409 /* Check for SEG - SEG which is now possible */
1410 if (Left->Op == EXPR_SEGMENT && Right->Op == EXPR_SEGMENT &&
1411 Left->V.SegNum == Right->V.SegNum) {
1412 /* SEG - SEG, remove it completely */
1413 FreeExprNode (Left);
1414 FreeExprNode (Right);
1415 FreeExprNode (Expr);
1419 Expr->Right = Right;
1425 /* Some other sort of node, finalize the terms */
1427 Expr->Left = FinalizeExpr (Expr->Left);
1430 Expr->Right = FinalizeExpr (Expr->Right);
1438 ExprNode* FinalizeExpr (ExprNode* Expr)
1439 /* Resolve any symbols by cloning the symbol expression tree instead of the
1440 * symbol reference, then try to simplify the expression as much as possible.
1441 * This function must only be called if all symbols are resolved (no undefined
1448 Expr = RemoveSyms (Expr, 0);
1449 Expr = ConstExtract (Expr, &Val, 1);
1451 /* Reduced to a literal value */
1452 Expr = LiteralExpr (Val);
1454 /* Extracted a value */
1458 N->Right = LiteralExpr (Val);
1466 ExprNode* CloneExpr (ExprNode* Expr)
1467 /* Clone the given expression tree. The function will simply clone symbol
1468 * nodes, it will not resolve them.
1473 /* Accept NULL pointers */
1478 /* Get a new node */
1479 Clone = NewExprNode ();
1481 /* Clone the operation */
1482 Clone->Op = Expr->Op;
1484 /* Clone the attribute if needed */
1489 Clone->V.Val = Expr->V.Val;
1493 Clone->V.Sym = Expr->V.Sym;
1497 Clone->V.SegNum = Expr->V.SegNum;
1502 /* Clone the tree nodes */
1503 Clone->Left = CloneExpr (Expr->Left);
1504 Clone->Right = CloneExpr (Expr->Right);
1512 void WriteExpr (ExprNode* Expr)
1513 /* Write the given expression to the object file */
1515 /* Null expressions are encoded by a type byte of zero */
1521 /* Write the expression code */
1522 ObjWrite8 (Expr->Op);
1524 /* If the is a leafnode, write the expression attribute, otherwise
1525 * write the expression operands.
1530 ObjWrite32 (Expr->V.Val);
1534 /* Maybe we should use a code here? */
1535 CHECK (SymIsImport (Expr->V.Sym)); /* Safety */
1536 ObjWrite16 (GetSymIndex (Expr->V.Sym));
1540 ObjWrite8 (Expr->V.SegNum);
1544 Internal ("WriteExpr: Cannot write EXPR_ULABEL nodes");
1548 /* Not a leaf node */
1549 WriteExpr (Expr->Left);
1550 WriteExpr (Expr->Right);