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 FuncXMatch (void)
471 /* Handle the .XMATCH function */
473 return DoMatch (tcIdentical);
478 static ExprNode* Function (int (*F) (void))
479 /* Handle builtin functions */
483 /* Skip the keyword */
486 /* Expression must be enclosed in braces */
487 if (Tok != TOK_LPAREN) {
488 Error (ERR_LPAREN_EXPECTED);
490 return LiteralExpr (0);
494 /* Call the function itself */
495 Result = (F () != 0);
497 /* Closing brace must follow */
500 /* Return an expression node with the boolean code */
501 return LiteralExpr (Result);
506 static ExprNode* Factor (void)
515 N = LiteralExpr (IVal);
521 if (Tok != TOK_IDENT) {
522 Error (ERR_IDENT_EXPECTED);
523 N = LiteralExpr (0); /* Dummy */
525 S = SymRefGlobal (SVal);
526 if (SymIsConst (S)) {
527 /* Use the literal value instead */
528 N = LiteralExpr (GetSymVal (S));
530 /* Create symbol node */
541 if (SymIsConst (S)) {
542 /* Use the literal value instead */
543 N = LiteralExpr (GetSymVal (S));
545 /* Create symbol node */
562 N->Op = EXPR_UNARY_MINUS;
599 N = Function (FuncBlank);
603 N = Function (FuncConst);
607 N = LiteralExpr (GetCPU());
612 N = Function (FuncDefined);
616 N = Function (FuncMatch);
620 N = Function (FuncReferenced);
624 N = Function (FuncXMatch);
628 N = LiteralExpr (0); /* Dummy */
638 static ExprNode* Term (void)
642 /* Read left hand side */
645 /* Handle multiplicative operations */
646 while (Tok == TOK_MUL || Tok == TOK_DIV || Tok == TOK_MOD ||
647 Tok == TOK_AND || Tok == TOK_XOR || Tok == TOK_SHL ||
650 /* Create a new node and insert the left expression */
651 ExprNode* Left = Root;
652 Root = NewExprNode ();
655 /* Determine the operator token */
657 case TOK_MUL: Root->Op = EXPR_MUL; break;
658 case TOK_DIV: Root->Op = EXPR_DIV; break;
659 case TOK_MOD: Root->Op = EXPR_MOD; break;
660 case TOK_AND: Root->Op = EXPR_AND; break;
661 case TOK_XOR: Root->Op = EXPR_XOR; break;
662 case TOK_SHL: Root->Op = EXPR_SHL; break;
663 case TOK_SHR: Root->Op = EXPR_SHR; break;
664 default: Internal ("Invalid token");
668 /* Parse the right hand side */
669 Root->Right = Factor ();
673 /* Return the expression tree we've created */
679 static ExprNode* SimpleExpr (void)
683 /* Read left hand side */
686 /* Handle additive operations */
687 while (Tok == TOK_PLUS || Tok == TOK_MINUS || Tok == TOK_OR) {
689 /* Create a new node and insert the left expression */
690 ExprNode* Left = Root;
691 Root = NewExprNode ();
694 /* Determine the operator token */
696 case TOK_PLUS: Root->Op = EXPR_PLUS; break;
697 case TOK_MINUS: Root->Op = EXPR_MINUS; break;
698 case TOK_OR: Root->Op = EXPR_OR; break;
699 default: Internal ("Invalid token");
703 /* Parse the right hand side */
704 Root->Right = Term ();
708 /* Return the expression tree we've created */
714 static ExprNode* BoolExpr (void)
715 /* Evaluate a boolean expression */
717 /* Read left hand side */
718 ExprNode* Root = SimpleExpr ();
720 /* Handle booleans */
721 while (Tok == TOK_EQ || Tok == TOK_NE || Tok == TOK_LT ||
722 Tok == TOK_GT || Tok == TOK_LE || Tok == TOK_GE) {
724 /* Create a new node and insert the left expression */
725 ExprNode* Left = Root;
726 Root = NewExprNode ();
729 /* Determine the operator token */
731 case TOK_EQ: Root->Op = EXPR_EQ; break;
732 case TOK_NE: Root->Op = EXPR_NE; break;
733 case TOK_LT: Root->Op = EXPR_LT; break;
734 case TOK_GT: Root->Op = EXPR_GT; break;
735 case TOK_LE: Root->Op = EXPR_LE; break;
736 case TOK_GE: Root->Op = EXPR_GE; break;
737 default: Internal ("Invalid token");
741 /* Parse the right hand side */
742 Root->Right = SimpleExpr ();
746 /* Return the expression tree we've created */
752 static ExprNode* Expr2 (void)
753 /* Boolean operators: AND and XOR */
755 /* Read left hand side */
756 ExprNode* Root = BoolExpr ();
758 /* Handle booleans */
759 while (Tok == TOK_BAND || Tok == TOK_BXOR) {
761 /* Create a new node and insert the left expression */
762 ExprNode* Left = Root;
763 Root = NewExprNode ();
766 /* Determine the operator token */
768 case TOK_BAND: Root->Op = EXPR_BAND; break;
769 case TOK_BXOR: Root->Op = EXPR_BXOR; break;
770 default: Internal ("Invalid token");
774 /* Parse the right hand side */
775 Root->Right = BoolExpr ();
779 /* Return the expression tree we've created */
785 static ExprNode* Expr1 (void)
786 /* Boolean operators: OR */
788 /* Read left hand side */
789 ExprNode* Root = Expr2 ();
791 /* Handle booleans */
792 while (Tok == TOK_BOR) {
794 /* Create a new node and insert the left expression */
795 ExprNode* Left = Root;
796 Root = NewExprNode ();
799 /* Determine the operator token */
801 case TOK_BOR: Root->Op = EXPR_BOR; break;
802 default: Internal ("Invalid token");
806 /* Parse the right hand side */
807 Root->Right = Expr2 ();
811 /* Return the expression tree we've created */
817 static ExprNode* Expr0 (void)
818 /* Boolean operators: NOT */
822 /* Handle booleans */
823 if (Tok == TOK_BNOT) {
825 /* Create a new node */
826 Root = NewExprNode ();
828 /* Determine the operator token */
830 case TOK_BNOT: Root->Op = EXPR_BNOT; break;
831 default: Internal ("Invalid token");
835 /* Parse the left hand side, allow more BNOTs */
836 Root->Left = Expr0 ();
840 /* Read left hand side */
845 /* Return the expression tree we've created */
851 static ExprNode* SimplifyExpr (ExprNode* Root)
852 /* Try to simplify the given expression tree */
855 SimplifyExpr (Root->Left);
856 SimplifyExpr (Root->Right);
857 if (IsConstExpr (Root)) {
858 /* The complete expression is constant */
859 Root->V.Val = GetExprVal (Root);
860 Root->Op = EXPR_LITERAL;
861 FreeExpr (Root->Left);
862 FreeExpr (Root->Right);
863 Root->Left = Root->Right = 0;
871 ExprNode* Expression (void)
872 /* Evaluate an expression, build the expression tree on the heap and return
873 * a pointer to the root of the tree.
876 return SimplifyExpr (Expr0 ());
881 long ConstExpression (void)
882 /* Parse an expression. Check if the expression is const, and print an error
883 * message if not. Return the value of the expression, or a dummy, if it is
887 /* Read the expression, and call finalize (exception here, since we
890 ExprNode* Expr = FinalizeExpr (Expression ());
892 /* Return the value */
893 if (IsConstExpr (Expr)) {
894 return GetExprVal (Expr);
896 Error (ERR_CONSTEXPR_EXPECTED);
903 ExprNode* LiteralExpr (long Val)
904 /* Return an expression tree that encodes the given literal value */
906 ExprNode* Expr = NewExprNode ();
907 Expr->Op = EXPR_LITERAL;
914 ExprNode* CurrentPC (void)
915 /* Return the current program counter as expression */
921 /* Create SegmentBase + Offset */
922 Left = NewExprNode ();
923 Left->Op = EXPR_SEGMENT;
924 Left->V.SegNum = GetSegNum ();
926 Root = NewExprNode ();
928 Root->Right = LiteralExpr (GetPC ());
929 Root->Op = EXPR_PLUS;
931 /* Absolute mode, just return PC value */
932 Root = LiteralExpr (GetPC ());
940 ExprNode* SwapExpr (ExprNode* Expr)
941 /* Return an extended expression with lo and hi bytes swapped */
943 ExprNode* N = NewExprNode ();
951 ExprNode* BranchExpr (unsigned Offs)
952 /* Return an expression that encodes the difference between current PC plus
953 * offset and the target expression (that is, Expression() - (*+Offs) ).
962 Left = NewExprNode ();
963 Left->Op = EXPR_SEGMENT;
964 Left->V.SegNum = GetSegNum ();
968 N->Right = LiteralExpr (GetPC () + Offs);
971 N = LiteralExpr (GetPC () + Offs);
974 /* Create the root node */
975 Root = NewExprNode ();
976 Root->Left = Expression ();
978 Root->Op = EXPR_MINUS;
980 /* Return the result */
981 return SimplifyExpr (Root);
986 ExprNode* ULabelExpr (unsigned Num)
987 /* Return an expression for an unnamed label with the given index */
989 /* Get an expression node */
990 ExprNode* Node = NewExprNode ();
993 Node->Op = EXPR_ULABEL;
996 /* Return the new node */
1002 void FreeExpr (ExprNode* Root)
1003 /* Free the expression, Root is pointing to. */
1006 FreeExpr (Root->Left);
1007 FreeExpr (Root->Right);
1008 FreeExprNode (Root);
1014 ExprNode* ForceWordExpr (ExprNode* Expr)
1015 /* Force the given expression into a word and return the result. */
1017 /* And the expression by $FFFF to force it into word size */
1018 ExprNode* Root = NewExprNode ();
1020 Root->Op = EXPR_AND;
1021 Root->Right = LiteralExpr (0xFFFF);
1023 /* Return the result */
1029 int IsConstExpr (ExprNode* Root)
1030 /* Return true if the given expression is a constant expression, that is, one
1031 * with no references to external symbols.
1037 if (EXPR_IS_LEAF (Root->Op)) {
1045 if (SymHasUserMark (Sym)) {
1049 PError (GetSymPos (Sym), ERR_CIRCULAR_REFERENCE);
1053 Const = SymIsConst (Sym);
1054 SymUnmarkUser (Sym);
1062 } else if (EXPR_IS_UNARY (Root->Op)) {
1064 return IsConstExpr (Root->Left);
1068 /* We must handle shortcut boolean expressions here */
1072 if (IsConstExpr (Root->Left)) {
1073 /* lhs is const, if it is zero, don't eval right */
1074 if (GetExprVal (Root->Left) == 0) {
1077 return IsConstExpr (Root->Right);
1080 /* lhs not const --> tree not const */
1086 if (IsConstExpr (Root->Left)) {
1087 /* lhs is const, if it is not zero, don't eval right */
1088 if (GetExprVal (Root->Left) != 0) {
1091 return IsConstExpr (Root->Right);
1094 /* lhs not const --> tree not const */
1100 /* All others are handled normal */
1101 return IsConstExpr (Root->Left) && IsConstExpr (Root->Right);
1108 static void CheckByteExpr (const ExprNode* N, int* IsByte)
1109 /* Internal routine that is recursively called to check if there is a zeropage
1110 * symbol in the expression tree.
1114 switch (N->Op & EXPR_TYPEMASK) {
1120 if (SymIsZP (N->V.Sym)) {
1126 if (GetSegType (N->V.SegNum) == SEGTYPE_ZP) {
1134 case EXPR_UNARYNODE:
1135 CheckByteExpr (N->Left, IsByte);
1138 case EXPR_BINARYNODE:
1139 CheckByteExpr (N->Left, IsByte);
1140 CheckByteExpr (N->Right, IsByte);
1144 Internal ("Unknown expression op: %02X", N->Op);
1151 int IsByteExpr (ExprNode* Root)
1152 /* Return true if this is a byte expression */
1156 if (IsConstExpr (Root)) {
1157 if (Root->Op != EXPR_LITERAL) {
1158 SimplifyExpr (Root);
1160 return IsByteRange (GetExprVal (Root));
1161 } else if (Root->Op == EXPR_LOBYTE || Root->Op == EXPR_HIBYTE) {
1162 /* Symbol forced to have byte range */
1165 /* We have undefined symbols in the expression. Assume that the
1166 * expression is a byte expression if there is at least one symbol
1167 * declared as zeropage in it. Being wrong here is not a very big
1168 * problem since the linker knows about all symbols and detects
1169 * error like mixing absolute and zeropage labels.
1172 CheckByteExpr (Root, &IsByte);
1179 long GetExprVal (ExprNode* Expr)
1180 /* Get the value of a constant expression */
1190 return GetSymVal (Expr->V.Sym);
1193 return GetExprVal (Expr->Left) + GetExprVal (Expr->Right);
1196 return GetExprVal (Expr->Left) - GetExprVal (Expr->Right);
1199 return GetExprVal (Expr->Left) * GetExprVal (Expr->Right);
1202 Left = GetExprVal (Expr->Left);
1203 Right = GetExprVal (Expr->Right);
1205 Error (ERR_DIV_BY_ZERO);
1208 return Left / Right;
1211 Left = GetExprVal (Expr->Left);
1212 Right = GetExprVal (Expr->Right);
1214 Error (ERR_MOD_BY_ZERO);
1217 return Left % 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));
1238 return (GetExprVal (Expr->Left) != GetExprVal (Expr->Right));
1241 return (GetExprVal (Expr->Left) < GetExprVal (Expr->Right));
1244 return (GetExprVal (Expr->Left) > GetExprVal (Expr->Right));
1247 return (GetExprVal (Expr->Left) <= GetExprVal (Expr->Right));
1250 return (GetExprVal (Expr->Left) >= GetExprVal (Expr->Right));
1252 case EXPR_UNARY_MINUS:
1253 return -GetExprVal (Expr->Left);
1256 return ~GetExprVal (Expr->Left);
1259 return GetExprVal (Expr->Left) & 0xFF;
1262 return (GetExprVal (Expr->Left) >> 8) & 0xFF;
1265 Left = GetExprVal (Expr->Left);
1266 return ((Left >> 8) & 0x00FF) | ((Left << 8) & 0xFF00);
1269 return GetExprVal (Expr->Left) && GetExprVal (Expr->Right);
1272 return GetExprVal (Expr->Left) || GetExprVal (Expr->Right);
1275 return (GetExprVal (Expr->Left) != 0) ^ (GetExprVal (Expr->Right) != 0);
1278 return !GetExprVal (Expr->Left);
1281 Internal ("GetExprVal called for EXPR_ULABEL");
1286 Internal ("Unknown Op type: %u", Expr->Op);
1294 static ExprNode* RemoveSyms (ExprNode* Expr, int MustClone)
1295 /* Remove resolved symbols from the tree by cloning symbol expressions */
1297 /* Accept NULL pointers */
1302 /* Special node handling */
1306 if (SymHasExpr (Expr->V.Sym)) {
1307 /* The symbol has an expression tree */
1308 SymEntry* Sym = Expr->V.Sym;
1309 if (SymHasUserMark (Sym)) {
1310 /* Circular definition */
1314 PError (GetSymPos (Sym), ERR_CIRCULAR_REFERENCE);
1315 return LiteralExpr (0); /* Return a dummy value */
1318 Expr = RemoveSyms (GetSymExpr (Sym), 1);
1319 SymUnmarkUser (Sym);
1321 } else if (SymIsConst (Expr->V.Sym)) {
1322 /* The symbol is a constant */
1323 return LiteralExpr (GetSymVal (Expr->V.Sym));
1328 if (ULabCanResolve ()) {
1329 ExprNode* NewExpr = ULabResolve (Expr->V.Val);
1337 /* Clone the current node if needed */
1340 /* Create a new node */
1341 ExprNode* Clone = NewExprNode ();
1343 /* Clone the operation */
1344 Clone->Op = Expr->Op;
1346 /* Clone the attribute if needed */
1351 Clone->V.Val = Expr->V.Val;
1355 Clone->V.Sym = Expr->V.Sym;
1359 Clone->V.SegNum = Expr->V.SegNum;
1364 /* Clone the tree nodes */
1365 Clone->Left = RemoveSyms (Expr->Left, MustClone);
1366 Clone->Right = RemoveSyms (Expr->Right, MustClone);
1373 /* Nothing to clone */
1374 Expr->Left = RemoveSyms (Expr->Left, MustClone);
1375 Expr->Right = RemoveSyms (Expr->Right, MustClone);
1385 static ExprNode* ConstExtract (ExprNode* Expr, long* Val, int Sign)
1386 /* Extract and evaluate all constant factors in an subtree that has only
1387 * additions and subtractions.
1390 if (Expr->Op == EXPR_LITERAL) {
1392 *Val -= Expr->V.Val;
1394 *Val += Expr->V.Val;
1396 FreeExprNode (Expr);
1400 if (Expr->Op == EXPR_PLUS || Expr->Op == EXPR_MINUS) {
1403 Left = ConstExtract (Expr->Left, Val, Sign);
1404 if (Expr->Op == EXPR_MINUS) {
1407 Right = ConstExtract (Expr->Right, Val, Sign);
1408 if (Left == 0 && Right == 0) {
1409 FreeExprNode (Expr);
1411 } else if (Left == 0) {
1412 FreeExprNode (Expr);
1414 } else if (Right == 0) {
1415 FreeExprNode (Expr);
1418 /* Check for SEG - SEG which is now possible */
1419 if (Left->Op == EXPR_SEGMENT && Right->Op == EXPR_SEGMENT &&
1420 Left->V.SegNum == Right->V.SegNum) {
1421 /* SEG - SEG, remove it completely */
1422 FreeExprNode (Left);
1423 FreeExprNode (Right);
1424 FreeExprNode (Expr);
1428 Expr->Right = Right;
1434 /* Some other sort of node, finalize the terms */
1436 Expr->Left = FinalizeExpr (Expr->Left);
1439 Expr->Right = FinalizeExpr (Expr->Right);
1447 ExprNode* FinalizeExpr (ExprNode* Expr)
1448 /* Resolve any symbols by cloning the symbol expression tree instead of the
1449 * symbol reference, then try to simplify the expression as much as possible.
1450 * This function must only be called if all symbols are resolved (no undefined
1457 Expr = RemoveSyms (Expr, 0);
1458 Expr = ConstExtract (Expr, &Val, 1);
1460 /* Reduced to a literal value */
1461 Expr = LiteralExpr (Val);
1463 /* Extracted a value */
1467 N->Right = LiteralExpr (Val);
1475 ExprNode* CloneExpr (ExprNode* Expr)
1476 /* Clone the given expression tree. The function will simply clone symbol
1477 * nodes, it will not resolve them.
1482 /* Accept NULL pointers */
1487 /* Get a new node */
1488 Clone = NewExprNode ();
1490 /* Clone the operation */
1491 Clone->Op = Expr->Op;
1493 /* Clone the attribute if needed */
1498 Clone->V.Val = Expr->V.Val;
1502 Clone->V.Sym = Expr->V.Sym;
1506 Clone->V.SegNum = Expr->V.SegNum;
1511 /* Clone the tree nodes */
1512 Clone->Left = CloneExpr (Expr->Left);
1513 Clone->Right = CloneExpr (Expr->Right);
1521 void WriteExpr (ExprNode* Expr)
1522 /* Write the given expression to the object file */
1524 /* Null expressions are encoded by a type byte of zero */
1530 /* Write the expression code */
1531 ObjWrite8 (Expr->Op);
1533 /* If the is a leafnode, write the expression attribute, otherwise
1534 * write the expression operands.
1539 ObjWrite32 (Expr->V.Val);
1543 /* Maybe we should use a code here? */
1544 CHECK (SymIsImport (Expr->V.Sym)); /* Safety */
1545 ObjWrite16 (GetSymIndex (Expr->V.Sym));
1549 ObjWrite8 (Expr->V.SegNum);
1553 Internal ("WriteExpr: Cannot write EXPR_ULABEL nodes");
1557 /* Not a leaf node */
1558 WriteExpr (Expr->Left);
1559 WriteExpr (Expr->Right);