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 /*****************************************************************************/
82 /*****************************************************************************/
86 void FreeExpr (ExprNode* Root)
87 /* Free the expression, Root is pointing to. */
90 FreeExpr (Root->Left);
91 FreeExpr (Root->Right);
98 int IsConstExpr (ExprNode* Root)
99 /* Return true if the given expression is a constant expression, that is, one
100 * with no references to external symbols.
106 if (EXPR_IS_LEAF (Root->Op)) {
113 /* Get the referenced export */
114 E = GetExprExport (Root);
115 /* If this export has a mark set, we've already encountered it.
116 * This means that the export is used to define it's own value,
117 * which in turn means, that we have a circular reference.
119 if (ExportHasMark (E)) {
120 Error ("Circular reference for symbol `%s', %s(%lu)",
121 E->Name, E->Obj->Files [E->Pos.Name], E->Pos.Line);
125 Const = IsConstExport (E);
134 } else if (EXPR_IS_UNARY (Root->Op)) {
136 return IsConstExpr (Root->Left);
140 /* We must handle shortcut boolean expressions here */
144 if (IsConstExpr (Root->Left)) {
145 /* lhs is const, if it is zero, don't eval right */
146 if (GetExprVal (Root->Left) == 0) {
149 return IsConstExpr (Root->Right);
152 /* lhs not const --> tree not const */
158 if (IsConstExpr (Root->Left)) {
159 /* lhs is const, if it is not zero, don't eval right */
160 if (GetExprVal (Root->Left) != 0) {
163 return IsConstExpr (Root->Right);
166 /* lhs not const --> tree not const */
172 /* All others are handled normal */
173 return IsConstExpr (Root->Left) && IsConstExpr (Root->Right);
180 Import* GetExprImport (ExprNode* Expr)
181 /* Get the import data structure for a symbol expression node */
183 /* Check that this is really a symbol */
184 PRECONDITION (Expr->Op == EXPR_SYMBOL);
186 /* Return the import */
187 return Expr->Obj->Imports [Expr->V.ImpNum];
192 Export* GetExprExport (ExprNode* Expr)
193 /* Get the exported symbol for a symbol expression node */
195 /* Check that this is really a symbol */
196 PRECONDITION (Expr->Op == EXPR_SYMBOL);
198 /* Return the export */
199 return Expr->Obj->Imports [Expr->V.ImpNum]->V.Exp;
204 Section* GetExprSection (ExprNode* Expr)
205 /* Get the segment for a segment expression node */
207 /* Check that this is really a segment node */
208 PRECONDITION (Expr->Op == EXPR_SEGMENT);
210 /* Return the export */
211 return Expr->Obj->Sections [Expr->V.SegNum];
216 long GetExprVal (ExprNode* Expr)
217 /* Get the value of a constant expression */
219 long Right, Left, Val;
229 /* Get the referenced export */
230 E = GetExprExport (Expr);
231 /* If this export has a mark set, we've already encountered it.
232 * This means that the export is used to define it's own value,
233 * which in turn means, that we have a circular reference.
235 if (ExportHasMark (E)) {
236 CircularRefError (E);
240 Val = GetExportVal (E);
246 S = GetExprSection (Expr);
247 return S->Offs + S->Seg->PC;
250 return Expr->V.MemArea->Start;
253 return GetExprVal (Expr->Left) + GetExprVal (Expr->Right);
256 return GetExprVal (Expr->Left) - GetExprVal (Expr->Right);
259 return GetExprVal (Expr->Left) * GetExprVal (Expr->Right);
262 Left = GetExprVal (Expr->Left);
263 Right = GetExprVal (Expr->Right);
265 Error ("Division by zero");
270 Left = GetExprVal (Expr->Left);
271 Right = GetExprVal (Expr->Right);
273 Error ("Modulo operation with zero");
278 return GetExprVal (Expr->Left) | GetExprVal (Expr->Right);
281 return GetExprVal (Expr->Left) ^ GetExprVal (Expr->Right);
284 return GetExprVal (Expr->Left) & GetExprVal (Expr->Right);
287 return GetExprVal (Expr->Left) << GetExprVal (Expr->Right);
290 return GetExprVal (Expr->Left) >> GetExprVal (Expr->Right);
293 return (GetExprVal (Expr->Left) == GetExprVal (Expr->Right));
296 return (GetExprVal (Expr->Left) != GetExprVal (Expr->Right));
299 return (GetExprVal (Expr->Left) < GetExprVal (Expr->Right));
302 return (GetExprVal (Expr->Left) > GetExprVal (Expr->Right));
305 return (GetExprVal (Expr->Left) <= GetExprVal (Expr->Right));
308 return (GetExprVal (Expr->Left) >= GetExprVal (Expr->Right));
310 case EXPR_UNARY_MINUS:
311 return -GetExprVal (Expr->Left);
314 return ~GetExprVal (Expr->Left);
317 return GetExprVal (Expr->Left) & 0xFF;
320 return (GetExprVal (Expr->Left) >> 8) & 0xFF;
323 return (GetExprVal (Expr->Left) >> 16) & 0xFF;
326 return (GetExprVal (Expr->Left) >> 24) & 0xFF;
329 Left = GetExprVal (Expr->Left);
330 return ((Left >> 8) & 0x00FF) | ((Left << 8) & 0xFF00);
333 return GetExprVal (Expr->Left) && GetExprVal (Expr->Right);
336 return GetExprVal (Expr->Left) || GetExprVal (Expr->Right);
339 return (GetExprVal (Expr->Left) != 0) ^ (GetExprVal (Expr->Right) != 0);
342 return !GetExprVal (Expr->Left);
345 Internal ("Unknown expression Op type: %u", Expr->Op);
353 ExprNode* LiteralExpr (long Val, ObjData* O)
354 /* Return an expression tree that encodes the given literal value */
356 ExprNode* Expr = NewExprNode (O);
357 Expr->Op = EXPR_LITERAL;
364 ExprNode* MemExpr (Memory* Mem, long Offs, ObjData* O)
365 /* Return an expression tree that encodes an offset into the memory area */
369 ExprNode* Expr = NewExprNode (O);
370 Expr->Op = EXPR_MEMAREA;
371 Expr->V.MemArea = Mem;
373 Root = NewExprNode (O);
374 Root->Op = EXPR_PLUS;
376 Root->Right = LiteralExpr (Offs, O);
383 ExprNode* ReadExpr (FILE* F, ObjData* O)
384 /* Read an expression from the given file */
388 /* Read the node tag and handle NULL nodes */
389 unsigned char Op = Read8 (F);
390 if (Op == EXPR_NULL) {
394 /* Create a new node */
395 Expr = NewExprNode (O);
398 /* Check the tag and handle the different expression types */
399 if (EXPR_IS_LEAF (Op)) {
403 Expr->V.Val = Read32Signed (F);
407 /* Read the import number */
408 Expr->V.ImpNum = Read16 (F);
412 /* Read the segment number */
413 Expr->V.SegNum = Read8 (F);
417 Error ("Invalid expression op: %02X", Op);
423 /* Not a leaf node */
424 Expr->Left = ReadExpr (F, O);
425 Expr->Right = ReadExpr (F, O);
429 /* Return the tree */
435 int EqualExpr (ExprNode* E1, ExprNode* E2)
436 /* Check if two expressions are identical. */
438 /* If one pointer is NULL, both must be NULL */
439 if ((E1 == 0) ^ (E2 == 0)) {
446 /* Both pointers not NULL, check OP */
447 if (E1->Op != E2->Op) {
451 /* OPs are identical, check data for leafs, or subtrees */
455 /* Value must be identical */
456 return (E1->V.Val == E2->V.Val);
459 /* Import number must be identical */
460 return (E1->V.ImpNum == E2->V.ImpNum);
463 /* Segment number must be identical */
464 return (E1->V.SegNum == E2->V.SegNum);
467 /* Memory area must be identical */
468 return (E1->V.MemArea == E2->V.MemArea);
471 /* Not a leaf node */
472 return EqualExpr (E1->Left, E2->Left) && EqualExpr (E1->Right, E2->Right);