1 /*****************************************************************************/
5 /* Expression evaluation for the ld65 linker */
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 /*****************************************************************************/
49 /*****************************************************************************/
51 /*****************************************************************************/
55 static ExprNode* NewExprNode (ObjData* O)
56 /* Create a new expression node */
58 /* Allocate fresh memory */
59 ExprNode* N = xmalloc (sizeof (ExprNode));
71 static void FreeExprNode (ExprNode* E)
80 /*****************************************************************************/
81 /* Dump an expression tree on stdout for debugging */
82 /*****************************************************************************/
86 static void InternalDumpExpr (const ExprNode* Expr)
87 /* Dump an expression in UPN */
92 InternalDumpExpr (Expr->Left);
93 InternalDumpExpr (Expr->Right);
98 printf (" $%04lX", Expr->V.Val & 0xFFFF);
165 case EXPR_UNARY_MINUS:
186 printf (" BOOL_AND");
194 printf (" BOOL_XOR");
198 printf (" BOOL_NOT");
202 Internal ("Unknown Op type: %u", Expr->Op);
209 void DumpExpr (const ExprNode* Expr)
210 /* Dump an expression tree to stdout */
212 InternalDumpExpr (Expr);
218 /*****************************************************************************/
220 /*****************************************************************************/
224 void FreeExpr (ExprNode* Root)
225 /* Free the expression, Root is pointing to. */
228 FreeExpr (Root->Left);
229 FreeExpr (Root->Right);
236 int IsConstExpr (ExprNode* Root)
237 /* Return true if the given expression is a constant expression, that is, one
238 * with no references to external symbols.
244 if (EXPR_IS_LEAF (Root->Op)) {
251 /* Get the referenced export */
252 E = GetExprExport (Root);
253 /* If this export has a mark set, we've already encountered it.
254 * This means that the export is used to define it's own value,
255 * which in turn means, that we have a circular reference.
257 if (ExportHasMark (E)) {
258 Error ("Circular reference for symbol `%s', %s(%lu)",
259 E->Name, E->Obj->Files [E->Pos.Name], E->Pos.Line);
263 Const = IsConstExport (E);
272 } else if (EXPR_IS_UNARY (Root->Op)) {
274 return IsConstExpr (Root->Left);
278 /* We must handle shortcut boolean expressions here */
282 if (IsConstExpr (Root->Left)) {
283 /* lhs is const, if it is zero, don't eval right */
284 if (GetExprVal (Root->Left) == 0) {
287 return IsConstExpr (Root->Right);
290 /* lhs not const --> tree not const */
296 if (IsConstExpr (Root->Left)) {
297 /* lhs is const, if it is not zero, don't eval right */
298 if (GetExprVal (Root->Left) != 0) {
301 return IsConstExpr (Root->Right);
304 /* lhs not const --> tree not const */
310 /* All others are handled normal */
311 return IsConstExpr (Root->Left) && IsConstExpr (Root->Right);
318 Import* GetExprImport (ExprNode* Expr)
319 /* Get the import data structure for a symbol expression node */
321 /* Check that this is really a symbol */
322 PRECONDITION (Expr->Op == EXPR_SYMBOL);
324 /* Return the import */
325 return Expr->Obj->Imports [Expr->V.ImpNum];
330 Export* GetExprExport (ExprNode* Expr)
331 /* Get the exported symbol for a symbol expression node */
333 /* Check that this is really a symbol */
334 PRECONDITION (Expr->Op == EXPR_SYMBOL);
336 /* Return the export */
337 return Expr->Obj->Imports [Expr->V.ImpNum]->V.Exp;
342 Section* GetExprSection (ExprNode* Expr)
343 /* Get the segment for a segment expression node */
345 /* Check that this is really a segment node */
346 PRECONDITION (Expr->Op == EXPR_SEGMENT);
348 /* Return the export */
349 return Expr->Obj->Sections [Expr->V.SegNum];
354 long GetExprVal (ExprNode* Expr)
355 /* Get the value of a constant expression */
357 long Right, Left, Val;
367 /* Get the referenced export */
368 E = GetExprExport (Expr);
369 /* If this export has a mark set, we've already encountered it.
370 * This means that the export is used to define it's own value,
371 * which in turn means, that we have a circular reference.
373 if (ExportHasMark (E)) {
374 CircularRefError (E);
378 Val = GetExportVal (E);
384 S = GetExprSection (Expr);
385 return S->Offs + S->Seg->PC;
388 return Expr->V.MemArea->Start;
391 return GetExprVal (Expr->Left) + GetExprVal (Expr->Right);
394 return GetExprVal (Expr->Left) - GetExprVal (Expr->Right);
397 return GetExprVal (Expr->Left) * GetExprVal (Expr->Right);
400 Left = GetExprVal (Expr->Left);
401 Right = GetExprVal (Expr->Right);
403 Error ("Division by zero");
408 Left = GetExprVal (Expr->Left);
409 Right = GetExprVal (Expr->Right);
411 Error ("Modulo operation with zero");
416 return GetExprVal (Expr->Left) | GetExprVal (Expr->Right);
419 return GetExprVal (Expr->Left) ^ GetExprVal (Expr->Right);
422 return GetExprVal (Expr->Left) & GetExprVal (Expr->Right);
425 return GetExprVal (Expr->Left) << GetExprVal (Expr->Right);
428 return GetExprVal (Expr->Left) >> GetExprVal (Expr->Right);
431 return (GetExprVal (Expr->Left) == GetExprVal (Expr->Right));
434 return (GetExprVal (Expr->Left) != GetExprVal (Expr->Right));
437 return (GetExprVal (Expr->Left) < GetExprVal (Expr->Right));
440 return (GetExprVal (Expr->Left) > GetExprVal (Expr->Right));
443 return (GetExprVal (Expr->Left) <= GetExprVal (Expr->Right));
446 return (GetExprVal (Expr->Left) >= GetExprVal (Expr->Right));
448 case EXPR_UNARY_MINUS:
449 return -GetExprVal (Expr->Left);
452 return ~GetExprVal (Expr->Left);
455 return GetExprVal (Expr->Left) & 0xFF;
458 return (GetExprVal (Expr->Left) >> 8) & 0xFF;
461 Left = GetExprVal (Expr->Left);
462 return ((Left >> 8) & 0x00FF) | ((Left << 8) & 0xFF00);
465 return GetExprVal (Expr->Left) && GetExprVal (Expr->Right);
468 return GetExprVal (Expr->Left) || GetExprVal (Expr->Right);
471 return (GetExprVal (Expr->Left) != 0) ^ (GetExprVal (Expr->Right) != 0);
474 return !GetExprVal (Expr->Left);
477 Internal ("Unknown expression Op type: %u", Expr->Op);
485 ExprNode* LiteralExpr (long Val, ObjData* O)
486 /* Return an expression tree that encodes the given literal value */
488 ExprNode* Expr = NewExprNode (O);
489 Expr->Op = EXPR_LITERAL;
496 ExprNode* MemExpr (Memory* Mem, long Offs, ObjData* O)
497 /* Return an expression tree that encodes an offset into the memory area */
501 ExprNode* Expr = NewExprNode (O);
502 Expr->Op = EXPR_MEMAREA;
503 Expr->V.MemArea = Mem;
505 Root = NewExprNode (O);
506 Root->Op = EXPR_PLUS;
508 Root->Right = LiteralExpr (Offs, O);
515 ExprNode* ReadExpr (FILE* F, ObjData* O)
516 /* Read an expression from the given file */
520 /* Read the node tag and handle NULL nodes */
521 unsigned char Op = Read8 (F);
522 if (Op == EXPR_NULL) {
526 /* Create a new node */
527 Expr = NewExprNode (O);
530 /* Check the tag and handle the different expression types */
531 if (EXPR_IS_LEAF (Op)) {
535 Expr->V.Val = Read32Signed (F);
539 /* Read the import number */
540 Expr->V.ImpNum = Read16 (F);
544 /* Read the segment number */
545 Expr->V.SegNum = Read8 (F);
549 Error ("Invalid expression op: %02X", Op);
555 /* Not a leaf node */
556 Expr->Left = ReadExpr (F, O);
557 Expr->Right = ReadExpr (F, O);
561 /* Return the tree */
567 int EqualExpr (ExprNode* E1, ExprNode* E2)
568 /* Check if two expressions are identical. */
570 /* If one pointer is NULL, both must be NULL */
571 if ((E1 == 0) ^ (E2 == 0)) {
578 /* Both pointers not NULL, check OP */
579 if (E1->Op != E2->Op) {
583 /* OPs are identical, check data for leafs, or subtrees */
587 /* Value must be identical */
588 return (E1->V.Val == E2->V.Val);
591 /* Import number must be identical */
592 return (E1->V.ImpNum == E2->V.ImpNum);
595 /* Segment number must be identical */
596 return (E1->V.SegNum == E2->V.SegNum);
599 /* Memory area must be identical */
600 return (E1->V.MemArea == E2->V.MemArea);
603 /* Not a leaf node */
604 return EqualExpr (E1->Left, E2->Left) && EqualExpr (E1->Right, E2->Right);