]> git.sur5r.net Git - cc65/blob - src/sim65/location.c
New optimization step
[cc65] / src / sim65 / location.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                location.c                                 */
4 /*                                                                           */
5 /*                        Memory location description                        */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2003      Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 52                                             */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 /* common.h */
37 #include "coll.h"
38 #include "xmalloc.h"
39
40 /* sim65 */
41 #include "cfgdata.h"
42 #include "error.h"
43 #include "scanner.h"
44 #include "location.h"
45
46
47
48 /*****************************************************************************/
49 /*                                     Data                                  */
50 /*****************************************************************************/
51
52
53
54 /* List of all memory locations */
55 Collection Locations = STATIC_COLLECTION_INITIALIZER;
56
57
58
59 /*****************************************************************************/
60 /*                                   Code                                    */
61 /*****************************************************************************/
62
63
64
65 Location* NewLocation (unsigned long Start, unsigned long End)
66 /* Create a new location, initialize and return it */
67 {
68     /* Allocate memory */
69     Location* L = xmalloc (sizeof (Location));
70
71     /* Initialize the fields */
72     L->Start      = Start;
73     L->End        = End;
74     L->Attributes = EmptyCollection;
75     L->Line       = CfgErrorLine;
76     L->Col        = CfgErrorCol;
77
78     /* Return the new struct */
79     return L;
80 }
81
82
83
84 static int CmpLocations (void* Data attribute ((unused)),
85                          const void* lhs, const void* rhs)
86 /* Compare function for CollSort */
87 {
88     /* Cast the object pointers */
89     const Location* Left  = (const Location*) rhs;
90     const Location* Right = (const Location*) lhs;
91
92     /* Do the compare */
93     if (Left->Start < Right->Start) {
94         return 1;
95     } else if (Left->Start > Right->Start) {
96         return -1;
97     } else {
98         return 0;
99     }
100 }
101
102
103
104 int LocationGetAttr (const Location* L, const char* AttrName)
105 /* Find the attribute with the given name and return it. Call Error() if the
106  * attribute was not found.
107  */
108 {
109     int I = CfgDataFind (&L->Attributes, AttrName);
110     if (I < 0) {
111         Error ("%s(%u): Attribute `%s' missing", CfgGetName(), L->Line, AttrName);
112     }
113     return I;
114 }
115
116
117
118 int LocationIsMirror (const Location* L)
119 /* Return true if the given location is a mirror of another one. */
120 {
121     /* Find the "mirror" attribute */
122     return (CfgDataFind (&L->Attributes, "mirror") >= 0);
123 }
124
125
126
127 void LocationSort (Collection* Locations)
128 /* Sort all locations by address */
129 {
130     /* Sort all memory locations */
131     CollSort (Locations, CmpLocations, 0);
132 }
133
134
135
136 void LocationCheck (const Collection* Locations)
137 /* Check all locations for problems */
138 {
139     unsigned I;
140
141     /* Check for overlaps and other problems */
142     const Location* Last = 0;
143     for (I = 0; I < CollCount (Locations); ++I) {
144
145         /* Get this location */
146         const Location* L = CollConstAt (Locations, I);
147
148         /* Check for an overlap with the following location */
149         if (Last && Last->End >= L->Start) {
150             Error ("%s(%u): Address range overlap (overlapping entry is in line %u)",
151                    CfgGetName(), L->Line, Last->Line);
152         }
153
154         /* If the location is a mirror, it must not have other attributes,
155          * and the mirror attribute must be an integer.
156          */
157         if (LocationIsMirror (L)) {
158             const CfgData* D;
159             if (CollCount (&L->Attributes) > 1) {
160                 Error ("%s(%u): Location at address $%06X is a mirror "
161                        "but has attributes", CfgGetName(), L->Line, L->Start);
162             }
163             D = CollConstAt (&L->Attributes, 0);
164             CfgDataCheckType (D, CfgDataNumber);
165         }
166
167         /* Remember this entry */
168         Last = L;
169     }
170 }
171
172
173
174