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 /*****************************************************************************/
36 #include "../common/exprdefs.h"
37 #include "../common/xmalloc.h"
52 /*****************************************************************************/
54 /*****************************************************************************/
58 /* Since all expressions are first packed into expression trees, and each
59 * expression tree node is allocated on the heap, we add some type of special
60 * purpose memory allocation here: Instead of freeing the nodes, we save some
61 * number of freed nodes for later and remember them in a single linked list
62 * using the Left link.
64 #define MAX_FREE_NODES 64
65 static ExprNode* FreeExprNodes = 0;
66 static unsigned FreeNodeCount = 0;
70 /*****************************************************************************/
72 /*****************************************************************************/
76 static ExprNode* NewExprNode (void)
77 /* Create a new expression node */
81 /* Do we have some nodes in the list already? */
83 /* Use first node from list */
85 FreeExprNodes = N->Left;
87 /* Allocate fresh memory */
88 N = xmalloc (sizeof (ExprNode));
91 N->Left = N->Right = 0;
99 static void FreeExprNode (ExprNode* E)
103 if (FreeNodeCount < MAX_FREE_NODES) {
104 /* Remember this node for later */
105 E->Left = FreeExprNodes;
108 /* Free the memory */
116 /*****************************************************************************/
117 /* Dump an expression tree on stdout for debugging */
118 /*****************************************************************************/
122 static void InternalDumpExpr (ExprNode* Expr)
123 /* Dump an expression in UPN */
128 InternalDumpExpr (Expr->Left);
129 InternalDumpExpr (Expr->Right);
135 printf (" $%04lX", Expr->V.Val & 0xFFFF);
139 printf (" %s", GetSymName (Expr->V.Sym));
202 case EXPR_UNARY_MINUS:
223 printf (" BOOL_AND");
231 printf (" BOOL_XOR");
235 printf (" BOOL_NOT");
239 Internal ("Unknown Op type: %u", Expr->Op);
246 void DumpExpr (ExprNode* Expr)
247 /* Dump an expression tree to stdout */
249 InternalDumpExpr (Expr);
255 /*****************************************************************************/
257 /*****************************************************************************/
261 static ExprNode* Expr0 (void);
265 int IsByteRange (long Val)
266 /* Return true if this is a byte value */
268 return (Val & ~0xFFL) == 0;
273 int IsWordRange (long Val)
274 /* Return true if this is a word value */
276 return (Val & ~0xFFFF) == 0;
281 static int FuncBlank (void)
282 /* Handle the .BLANK builtin function */
284 /* Assume no tokens if the closing brace follows (this is not correct in
285 * all cases, since the token may be the closing brace, but this will
286 * give a syntax error anyway and may not be handled by .BLANK.
288 if (Tok == TOK_RPAREN) {
292 /* Skip any tokens */
294 while (Tok != TOK_SEP && Tok != TOK_EOF) {
295 if (Tok == TOK_LPAREN) {
297 } else if (Tok == TOK_RPAREN) {
313 static int FuncConst (void)
314 /* Handle the .CONST builtin function */
316 /* Read an expression */
317 ExprNode* Expr = Expression ();
319 /* Check the constness of the expression */
320 int Result = IsConstExpr (Expr);
322 /* Free the expression */
331 static int FuncDefined (void)
332 /* Handle the .DEFINED builtin function */
336 if (Tok != TOK_IDENT) {
337 Error (ERR_IDENT_EXPECTED);
338 if (Tok != TOK_RPAREN) {
342 Result = SymIsDef (SVal);
352 static int DoMatch (enum TC EqualityLevel)
353 /* Handle the .MATCH and .XMATCH builtin functions */
360 /* A list of tokens follows. Read this list and remember it building a
361 * single linked list of tokens including attributes. The list is
362 * terminated by a comma.
364 while (Tok != TOK_COMMA) {
366 /* We may not end-of-line of end-of-file here */
367 if (Tok == TOK_SEP || Tok == TOK_EOF) {
368 Error (ERR_UNEXPECTED_EOL);
372 /* Get a node with this token */
373 Node = NewTokNode ();
375 /* Insert the node into the list */
390 /* Read the second list which is terminated by the right parenthesis and
391 * compare each token against the one in the first list.
395 while (Tok != TOK_RPAREN) {
397 /* We may not end-of-line of end-of-file here */
398 if (Tok == TOK_SEP || Tok == TOK_EOF) {
399 Error (ERR_UNEXPECTED_EOL);
403 /* Compare the tokens if the result is not already known */
406 /* The second list is larger than the first one */
408 } else if (TokCmp (Node) < EqualityLevel) {
409 /* Tokens do not match */
414 /* Next token in first list */
419 /* Next token in current list */
423 /* Check if there are remaining tokens in the first list */
428 /* Free the token list */
435 /* Done, return the result */
441 static int FuncMatch (void)
442 /* Handle the .MATCH function */
444 return DoMatch (tcSameToken);
449 static int FuncReferenced (void)
450 /* Handle the .REFERENCED builtin function */
454 if (Tok != TOK_IDENT) {
455 Error (ERR_IDENT_EXPECTED);
456 if (Tok != TOK_RPAREN) {
460 Result = SymIsRef (SVal);
470 static int FuncTCount (void)
471 /* Handle the .TCOUNT function */
473 /* We have a list of tokens that ends with the closing paren. Skip
474 * the tokens, handling nested braces and count them.
478 while (Parens != 0 || Tok != TOK_RPAREN) {
480 /* Check for end of line or end of input. Since the calling function
481 * will check for the closing paren, we don't need to print an error
482 * here, just bail out.
484 if (Tok == TOK_SEP || Tok == TOK_EOF) {
491 /* Keep track of the nesting level */
493 case TOK_LPAREN: ++Parens; break;
494 case TOK_RPAREN: --Parens; break;
502 /* Return the number of tokens */
508 static int FuncXMatch (void)
509 /* Handle the .XMATCH function */
511 return DoMatch (tcIdentical);
516 static ExprNode* Function (int (*F) (void))
517 /* Handle builtin functions */
521 /* Skip the keyword */
524 /* Expression must be enclosed in braces */
525 if (Tok != TOK_LPAREN) {
526 Error (ERR_LPAREN_EXPECTED);
528 return LiteralExpr (0);
532 /* Call the function itself */
535 /* Closing brace must follow */
538 /* Return an expression node with the boolean code */
539 return LiteralExpr (Result);
544 static ExprNode* Factor (void)
553 N = LiteralExpr (IVal);
559 if (Tok != TOK_IDENT) {
560 Error (ERR_IDENT_EXPECTED);
561 N = LiteralExpr (0); /* Dummy */
563 S = SymRefGlobal (SVal);
564 if (SymIsConst (S)) {
565 /* Use the literal value instead */
566 N = LiteralExpr (GetSymVal (S));
568 /* Create symbol node */
579 if (SymIsConst (S)) {
580 /* Use the literal value instead */
581 N = LiteralExpr (GetSymVal (S));
583 /* Create symbol node */
600 N->Op = EXPR_UNARY_MINUS;
637 N = Function (FuncBlank);
641 N = Function (FuncConst);
645 N = LiteralExpr (GetCPU());
650 N = Function (FuncDefined);
654 N = Function (FuncMatch);
658 N = Function (FuncReferenced);
662 N = Function (FuncTCount);
666 N = Function (FuncXMatch);
670 N = LiteralExpr (0); /* Dummy */
680 static ExprNode* Term (void)
684 /* Read left hand side */
687 /* Handle multiplicative operations */
688 while (Tok == TOK_MUL || Tok == TOK_DIV || Tok == TOK_MOD ||
689 Tok == TOK_AND || Tok == TOK_XOR || Tok == TOK_SHL ||
692 /* Create a new node and insert the left expression */
693 ExprNode* Left = Root;
694 Root = NewExprNode ();
697 /* Determine the operator token */
699 case TOK_MUL: Root->Op = EXPR_MUL; break;
700 case TOK_DIV: Root->Op = EXPR_DIV; break;
701 case TOK_MOD: Root->Op = EXPR_MOD; break;
702 case TOK_AND: Root->Op = EXPR_AND; break;
703 case TOK_XOR: Root->Op = EXPR_XOR; break;
704 case TOK_SHL: Root->Op = EXPR_SHL; break;
705 case TOK_SHR: Root->Op = EXPR_SHR; break;
706 default: Internal ("Invalid token");
710 /* Parse the right hand side */
711 Root->Right = Factor ();
715 /* Return the expression tree we've created */
721 static ExprNode* SimpleExpr (void)
725 /* Read left hand side */
728 /* Handle additive operations */
729 while (Tok == TOK_PLUS || Tok == TOK_MINUS || Tok == TOK_OR) {
731 /* Create a new node and insert the left expression */
732 ExprNode* Left = Root;
733 Root = NewExprNode ();
736 /* Determine the operator token */
738 case TOK_PLUS: Root->Op = EXPR_PLUS; break;
739 case TOK_MINUS: Root->Op = EXPR_MINUS; break;
740 case TOK_OR: Root->Op = EXPR_OR; break;
741 default: Internal ("Invalid token");
745 /* Parse the right hand side */
746 Root->Right = Term ();
750 /* Return the expression tree we've created */
756 static ExprNode* BoolExpr (void)
757 /* Evaluate a boolean expression */
759 /* Read left hand side */
760 ExprNode* Root = SimpleExpr ();
762 /* Handle booleans */
763 while (Tok == TOK_EQ || Tok == TOK_NE || Tok == TOK_LT ||
764 Tok == TOK_GT || Tok == TOK_LE || Tok == TOK_GE) {
766 /* Create a new node and insert the left expression */
767 ExprNode* Left = Root;
768 Root = NewExprNode ();
771 /* Determine the operator token */
773 case TOK_EQ: Root->Op = EXPR_EQ; break;
774 case TOK_NE: Root->Op = EXPR_NE; break;
775 case TOK_LT: Root->Op = EXPR_LT; break;
776 case TOK_GT: Root->Op = EXPR_GT; break;
777 case TOK_LE: Root->Op = EXPR_LE; break;
778 case TOK_GE: Root->Op = EXPR_GE; break;
779 default: Internal ("Invalid token");
783 /* Parse the right hand side */
784 Root->Right = SimpleExpr ();
788 /* Return the expression tree we've created */
794 static ExprNode* Expr2 (void)
795 /* Boolean operators: AND and XOR */
797 /* Read left hand side */
798 ExprNode* Root = BoolExpr ();
800 /* Handle booleans */
801 while (Tok == TOK_BAND || Tok == TOK_BXOR) {
803 /* Create a new node and insert the left expression */
804 ExprNode* Left = Root;
805 Root = NewExprNode ();
808 /* Determine the operator token */
810 case TOK_BAND: Root->Op = EXPR_BAND; break;
811 case TOK_BXOR: Root->Op = EXPR_BXOR; break;
812 default: Internal ("Invalid token");
816 /* Parse the right hand side */
817 Root->Right = BoolExpr ();
821 /* Return the expression tree we've created */
827 static ExprNode* Expr1 (void)
828 /* Boolean operators: OR */
830 /* Read left hand side */
831 ExprNode* Root = Expr2 ();
833 /* Handle booleans */
834 while (Tok == TOK_BOR) {
836 /* Create a new node and insert the left expression */
837 ExprNode* Left = Root;
838 Root = NewExprNode ();
841 /* Determine the operator token */
843 case TOK_BOR: Root->Op = EXPR_BOR; break;
844 default: Internal ("Invalid token");
848 /* Parse the right hand side */
849 Root->Right = Expr2 ();
853 /* Return the expression tree we've created */
859 static ExprNode* Expr0 (void)
860 /* Boolean operators: NOT */
864 /* Handle booleans */
865 if (Tok == TOK_BNOT) {
867 /* Create a new node */
868 Root = NewExprNode ();
870 /* Determine the operator token */
872 case TOK_BNOT: Root->Op = EXPR_BNOT; break;
873 default: Internal ("Invalid token");
877 /* Parse the left hand side, allow more BNOTs */
878 Root->Left = Expr0 ();
882 /* Read left hand side */
887 /* Return the expression tree we've created */
893 static ExprNode* SimplifyExpr (ExprNode* Root)
894 /* Try to simplify the given expression tree */
897 SimplifyExpr (Root->Left);
898 SimplifyExpr (Root->Right);
899 if (IsConstExpr (Root)) {
900 /* The complete expression is constant */
901 Root->V.Val = GetExprVal (Root);
902 Root->Op = EXPR_LITERAL;
903 FreeExpr (Root->Left);
904 FreeExpr (Root->Right);
905 Root->Left = Root->Right = 0;
913 ExprNode* Expression (void)
914 /* Evaluate an expression, build the expression tree on the heap and return
915 * a pointer to the root of the tree.
918 return SimplifyExpr (Expr0 ());
923 long ConstExpression (void)
924 /* Parse an expression. Check if the expression is const, and print an error
925 * message if not. Return the value of the expression, or a dummy, if it is
929 /* Read the expression, and call finalize (exception here, since we
932 ExprNode* Expr = FinalizeExpr (Expression ());
934 /* Return the value */
935 if (IsConstExpr (Expr)) {
936 return GetExprVal (Expr);
938 Error (ERR_CONSTEXPR_EXPECTED);
945 ExprNode* LiteralExpr (long Val)
946 /* Return an expression tree that encodes the given literal value */
948 ExprNode* Expr = NewExprNode ();
949 Expr->Op = EXPR_LITERAL;
956 ExprNode* CurrentPC (void)
957 /* Return the current program counter as expression */
963 /* Create SegmentBase + Offset */
964 Left = NewExprNode ();
965 Left->Op = EXPR_SEGMENT;
966 Left->V.SegNum = GetSegNum ();
968 Root = NewExprNode ();
970 Root->Right = LiteralExpr (GetPC ());
971 Root->Op = EXPR_PLUS;
973 /* Absolute mode, just return PC value */
974 Root = LiteralExpr (GetPC ());
982 ExprNode* SwapExpr (ExprNode* Expr)
983 /* Return an extended expression with lo and hi bytes swapped */
985 ExprNode* N = NewExprNode ();
993 ExprNode* BranchExpr (unsigned Offs)
994 /* Return an expression that encodes the difference between current PC plus
995 * offset and the target expression (that is, Expression() - (*+Offs) ).
1004 Left = NewExprNode ();
1005 Left->Op = EXPR_SEGMENT;
1006 Left->V.SegNum = GetSegNum ();
1010 N->Right = LiteralExpr (GetPC () + Offs);
1013 N = LiteralExpr (GetPC () + Offs);
1016 /* Create the root node */
1017 Root = NewExprNode ();
1018 Root->Left = Expression ();
1020 Root->Op = EXPR_MINUS;
1022 /* Return the result */
1023 return SimplifyExpr (Root);
1028 ExprNode* ULabelExpr (unsigned Num)
1029 /* Return an expression for an unnamed label with the given index */
1031 /* Get an expression node */
1032 ExprNode* Node = NewExprNode ();
1034 /* Set the values */
1035 Node->Op = EXPR_ULABEL;
1038 /* Return the new node */
1044 void FreeExpr (ExprNode* Root)
1045 /* Free the expression, Root is pointing to. */
1048 FreeExpr (Root->Left);
1049 FreeExpr (Root->Right);
1050 FreeExprNode (Root);
1056 ExprNode* ForceWordExpr (ExprNode* Expr)
1057 /* Force the given expression into a word and return the result. */
1059 /* And the expression by $FFFF to force it into word size */
1060 ExprNode* Root = NewExprNode ();
1062 Root->Op = EXPR_AND;
1063 Root->Right = LiteralExpr (0xFFFF);
1065 /* Return the result */
1071 int IsConstExpr (ExprNode* Root)
1072 /* Return true if the given expression is a constant expression, that is, one
1073 * with no references to external symbols.
1079 if (EXPR_IS_LEAF (Root->Op)) {
1087 if (SymHasUserMark (Sym)) {
1091 PError (GetSymPos (Sym), ERR_CIRCULAR_REFERENCE);
1095 Const = SymIsConst (Sym);
1096 SymUnmarkUser (Sym);
1104 } else if (EXPR_IS_UNARY (Root->Op)) {
1106 return IsConstExpr (Root->Left);
1110 /* We must handle shortcut boolean expressions here */
1114 if (IsConstExpr (Root->Left)) {
1115 /* lhs is const, if it is zero, don't eval right */
1116 if (GetExprVal (Root->Left) == 0) {
1119 return IsConstExpr (Root->Right);
1122 /* lhs not const --> tree not const */
1128 if (IsConstExpr (Root->Left)) {
1129 /* lhs is const, if it is not zero, don't eval right */
1130 if (GetExprVal (Root->Left) != 0) {
1133 return IsConstExpr (Root->Right);
1136 /* lhs not const --> tree not const */
1142 /* All others are handled normal */
1143 return IsConstExpr (Root->Left) && IsConstExpr (Root->Right);
1150 static void CheckByteExpr (const ExprNode* N, int* IsByte)
1151 /* Internal routine that is recursively called to check if there is a zeropage
1152 * symbol in the expression tree.
1156 switch (N->Op & EXPR_TYPEMASK) {
1162 if (SymIsZP (N->V.Sym)) {
1168 if (GetSegType (N->V.SegNum) == SEGTYPE_ZP) {
1176 case EXPR_UNARYNODE:
1177 CheckByteExpr (N->Left, IsByte);
1180 case EXPR_BINARYNODE:
1181 CheckByteExpr (N->Left, IsByte);
1182 CheckByteExpr (N->Right, IsByte);
1186 Internal ("Unknown expression op: %02X", N->Op);
1193 int IsByteExpr (ExprNode* Root)
1194 /* Return true if this is a byte expression */
1198 if (IsConstExpr (Root)) {
1199 if (Root->Op != EXPR_LITERAL) {
1200 SimplifyExpr (Root);
1202 return IsByteRange (GetExprVal (Root));
1203 } else if (Root->Op == EXPR_LOBYTE || Root->Op == EXPR_HIBYTE) {
1204 /* Symbol forced to have byte range */
1207 /* We have undefined symbols in the expression. Assume that the
1208 * expression is a byte expression if there is at least one symbol
1209 * declared as zeropage in it. Being wrong here is not a very big
1210 * problem since the linker knows about all symbols and detects
1211 * error like mixing absolute and zeropage labels.
1214 CheckByteExpr (Root, &IsByte);
1221 long GetExprVal (ExprNode* Expr)
1222 /* Get the value of a constant expression */
1232 return GetSymVal (Expr->V.Sym);
1235 return GetExprVal (Expr->Left) + GetExprVal (Expr->Right);
1238 return GetExprVal (Expr->Left) - GetExprVal (Expr->Right);
1241 return GetExprVal (Expr->Left) * GetExprVal (Expr->Right);
1244 Left = GetExprVal (Expr->Left);
1245 Right = GetExprVal (Expr->Right);
1247 Error (ERR_DIV_BY_ZERO);
1250 return Left / Right;
1253 Left = GetExprVal (Expr->Left);
1254 Right = GetExprVal (Expr->Right);
1256 Error (ERR_MOD_BY_ZERO);
1259 return Left % Right;
1262 return GetExprVal (Expr->Left) | GetExprVal (Expr->Right);
1265 return GetExprVal (Expr->Left) ^ GetExprVal (Expr->Right);
1268 return GetExprVal (Expr->Left) & GetExprVal (Expr->Right);
1271 return GetExprVal (Expr->Left) << GetExprVal (Expr->Right);
1274 return GetExprVal (Expr->Left) >> GetExprVal (Expr->Right);
1277 return (GetExprVal (Expr->Left) == GetExprVal (Expr->Right));
1280 return (GetExprVal (Expr->Left) != GetExprVal (Expr->Right));
1283 return (GetExprVal (Expr->Left) < GetExprVal (Expr->Right));
1286 return (GetExprVal (Expr->Left) > GetExprVal (Expr->Right));
1289 return (GetExprVal (Expr->Left) <= GetExprVal (Expr->Right));
1292 return (GetExprVal (Expr->Left) >= GetExprVal (Expr->Right));
1294 case EXPR_UNARY_MINUS:
1295 return -GetExprVal (Expr->Left);
1298 return ~GetExprVal (Expr->Left);
1301 return GetExprVal (Expr->Left) & 0xFF;
1304 return (GetExprVal (Expr->Left) >> 8) & 0xFF;
1307 Left = GetExprVal (Expr->Left);
1308 return ((Left >> 8) & 0x00FF) | ((Left << 8) & 0xFF00);
1311 return GetExprVal (Expr->Left) && GetExprVal (Expr->Right);
1314 return GetExprVal (Expr->Left) || GetExprVal (Expr->Right);
1317 return (GetExprVal (Expr->Left) != 0) ^ (GetExprVal (Expr->Right) != 0);
1320 return !GetExprVal (Expr->Left);
1323 Internal ("GetExprVal called for EXPR_ULABEL");
1328 Internal ("Unknown Op type: %u", Expr->Op);
1336 static ExprNode* RemoveSyms (ExprNode* Expr, int MustClone)
1337 /* Remove resolved symbols from the tree by cloning symbol expressions */
1339 /* Accept NULL pointers */
1344 /* Special node handling */
1348 if (SymHasExpr (Expr->V.Sym)) {
1349 /* The symbol has an expression tree */
1350 SymEntry* Sym = Expr->V.Sym;
1351 if (SymHasUserMark (Sym)) {
1352 /* Circular definition */
1356 PError (GetSymPos (Sym), ERR_CIRCULAR_REFERENCE);
1357 return LiteralExpr (0); /* Return a dummy value */
1360 Expr = RemoveSyms (GetSymExpr (Sym), 1);
1361 SymUnmarkUser (Sym);
1363 } else if (SymIsConst (Expr->V.Sym)) {
1364 /* The symbol is a constant */
1365 return LiteralExpr (GetSymVal (Expr->V.Sym));
1370 if (ULabCanResolve ()) {
1371 ExprNode* NewExpr = ULabResolve (Expr->V.Val);
1379 /* Clone the current node if needed */
1382 /* Create a new node */
1383 ExprNode* Clone = NewExprNode ();
1385 /* Clone the operation */
1386 Clone->Op = Expr->Op;
1388 /* Clone the attribute if needed */
1393 Clone->V.Val = Expr->V.Val;
1397 Clone->V.Sym = Expr->V.Sym;
1401 Clone->V.SegNum = Expr->V.SegNum;
1406 /* Clone the tree nodes */
1407 Clone->Left = RemoveSyms (Expr->Left, MustClone);
1408 Clone->Right = RemoveSyms (Expr->Right, MustClone);
1415 /* Nothing to clone */
1416 Expr->Left = RemoveSyms (Expr->Left, MustClone);
1417 Expr->Right = RemoveSyms (Expr->Right, MustClone);
1427 static ExprNode* ConstExtract (ExprNode* Expr, long* Val, int Sign)
1428 /* Extract and evaluate all constant factors in an subtree that has only
1429 * additions and subtractions.
1432 if (Expr->Op == EXPR_LITERAL) {
1434 *Val -= Expr->V.Val;
1436 *Val += Expr->V.Val;
1438 FreeExprNode (Expr);
1442 if (Expr->Op == EXPR_PLUS || Expr->Op == EXPR_MINUS) {
1445 Left = ConstExtract (Expr->Left, Val, Sign);
1446 if (Expr->Op == EXPR_MINUS) {
1449 Right = ConstExtract (Expr->Right, Val, Sign);
1450 if (Left == 0 && Right == 0) {
1451 FreeExprNode (Expr);
1453 } else if (Left == 0) {
1454 FreeExprNode (Expr);
1456 } else if (Right == 0) {
1457 FreeExprNode (Expr);
1460 /* Check for SEG - SEG which is now possible */
1461 if (Left->Op == EXPR_SEGMENT && Right->Op == EXPR_SEGMENT &&
1462 Left->V.SegNum == Right->V.SegNum) {
1463 /* SEG - SEG, remove it completely */
1464 FreeExprNode (Left);
1465 FreeExprNode (Right);
1466 FreeExprNode (Expr);
1470 Expr->Right = Right;
1476 /* Some other sort of node, finalize the terms */
1478 Expr->Left = FinalizeExpr (Expr->Left);
1481 Expr->Right = FinalizeExpr (Expr->Right);
1489 ExprNode* FinalizeExpr (ExprNode* Expr)
1490 /* Resolve any symbols by cloning the symbol expression tree instead of the
1491 * symbol reference, then try to simplify the expression as much as possible.
1492 * This function must only be called if all symbols are resolved (no undefined
1499 Expr = RemoveSyms (Expr, 0);
1500 Expr = ConstExtract (Expr, &Val, 1);
1502 /* Reduced to a literal value */
1503 Expr = LiteralExpr (Val);
1505 /* Extracted a value */
1509 N->Right = LiteralExpr (Val);
1517 ExprNode* CloneExpr (ExprNode* Expr)
1518 /* Clone the given expression tree. The function will simply clone symbol
1519 * nodes, it will not resolve them.
1524 /* Accept NULL pointers */
1529 /* Get a new node */
1530 Clone = NewExprNode ();
1532 /* Clone the operation */
1533 Clone->Op = Expr->Op;
1535 /* Clone the attribute if needed */
1540 Clone->V.Val = Expr->V.Val;
1544 Clone->V.Sym = Expr->V.Sym;
1548 Clone->V.SegNum = Expr->V.SegNum;
1553 /* Clone the tree nodes */
1554 Clone->Left = CloneExpr (Expr->Left);
1555 Clone->Right = CloneExpr (Expr->Right);
1563 void WriteExpr (ExprNode* Expr)
1564 /* Write the given expression to the object file */
1566 /* Null expressions are encoded by a type byte of zero */
1572 /* Write the expression code */
1573 ObjWrite8 (Expr->Op);
1575 /* If the is a leafnode, write the expression attribute, otherwise
1576 * write the expression operands.
1581 ObjWrite32 (Expr->V.Val);
1585 /* Maybe we should use a code here? */
1586 CHECK (SymIsImport (Expr->V.Sym)); /* Safety */
1587 ObjWrite16 (GetSymIndex (Expr->V.Sym));
1591 ObjWrite8 (Expr->V.SegNum);
1595 Internal ("WriteExpr: Cannot write EXPR_ULABEL nodes");
1599 /* Not a leaf node */
1600 WriteExpr (Expr->Left);
1601 WriteExpr (Expr->Right);