1 /*****************************************************************************/
5 /* A span of data within a segment */
9 /* (C) 2003-2011, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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 unsigned HT_GenHash (const void* Key);
56 /* Generate the hash over a key. */
58 static const void* HT_GetKey (const void* Entry);
59 /* Given a pointer to the user entry data, return a pointer to the key */
61 static int HT_Compare (const void* Key1, const void* Key2);
62 /* Compare two keys. The function must return a value less than zero if
63 * Key1 is smaller than Key2, zero if both are equal, and a value greater
64 * than zero if Key1 is greater then Key2.
69 /*****************************************************************************/
71 /*****************************************************************************/
75 /* Hash table functions */
76 static const HashFunctions HashFunc = {
83 static HashTable SpanTab = STATIC_HASHTABLE_INITIALIZER (1051, &HashFunc);
87 /*****************************************************************************/
88 /* Hash table functions */
89 /*****************************************************************************/
93 static unsigned HT_GenHash (const void* Key)
94 /* Generate the hash over a key. */
96 /* Key is a Span pointer */
99 /* Hash over a combination of segment number, start and end */
100 return HashInt ((S->Seg->Num << 28) ^ (S->Start << 14) ^ S->End);
105 static const void* HT_GetKey (const void* Entry)
106 /* Given a pointer to the user entry data, return a pointer to the key */
113 static int HT_Compare (const void* Key1, const void* Key2)
114 /* Compare two keys. The function must return a value less than zero if
115 * Key1 is smaller than Key2, zero if both are equal, and a value greater
116 * than zero if Key1 is greater then Key2.
119 /* Convert both parameters to Span pointers */
120 const Span* S1 = Key1;
121 const Span* S2 = Key2;
123 /* Compare segment number, then start and end */
124 int Res = (int)S2->Seg->Num - (int)S1->Seg->Num;
126 Res = (int)S2->Start - (int)S1->Start;
128 Res = (int)S2->End - (int)S1->End;
138 /*****************************************************************************/
140 /*****************************************************************************/
144 static Span* NewSpan (Segment* Seg, unsigned long Start, unsigned long End)
145 /* Create a new span. The segment is set to Seg, Start and End are set to the
146 * current PC of the segment.
149 /* Allocate memory */
150 Span* S = xmalloc (sizeof (Span));
152 /* Initialize the struct */
153 InitHashNode (&S->Node);
160 /* Return the new struct */
166 static void FreeSpan (Span* S)
174 static Span* MergeSpan (Span* S)
175 /* Check if we have a span with the same data as S already. If so, free S and
176 * return the already existing one. If not, remember S and return it.
179 /* Check if we have such a span already. If so use the existing
180 * one and free the one from the collection. If not, add the one to
181 * the hash table and return it.
183 Span* E = HT_Find (&SpanTab, S);
185 /* If S has a type and E not, move the type */
186 CHECK (E->Type == 0);
189 /* Free S and return E */
193 /* Assign the id, insert S, then return it */
194 S->Id = HT_GetCount (&SpanTab);
195 HT_Insert (&SpanTab, S);
202 Span* OpenSpan (void)
203 /* Open a span for the active segment and return it. */
205 return NewSpan (ActiveSeg, ActiveSeg->PC, ActiveSeg->PC);
210 Span* CloseSpan (Span* S)
211 /* Close the given span. Be sure to replace the passed span by the one
212 * returned, since the span will get deleted if it is empty or may be
213 * replaced if a duplicate exists.
216 /* Set the end offset */
217 if (S->Start == S->Seg->PC) {
222 /* Span is not empty */
225 /* Check if we have such a span already. If so use the existing
226 * one and free the one from the collection. If not, add the one to
227 * the hash table and return it.
229 return MergeSpan (S);
235 void OpenSpanList (Collection* Spans)
236 /* Open a list of spans for all existing segments to the given collection of
237 * spans. The currently active segment will be inserted first with all others
243 /* Grow the Spans collection as necessary */
244 CollGrow (Spans, CollCount (&SegmentList));
246 /* Add the currently active segment */
247 CollAppend (Spans, NewSpan (ActiveSeg, ActiveSeg->PC, ActiveSeg->PC));
249 /* Walk through the segment list and add all other segments */
250 for (I = 0; I < CollCount (&SegmentList); ++I) {
251 Segment* Seg = CollAtUnchecked (&SegmentList, I);
253 /* Be sure to skip the active segment, since it was already added */
254 if (Seg != ActiveSeg) {
255 CollAppend (Spans, NewSpan (Seg, Seg->PC, Seg->PC));
262 void CloseSpanList (Collection* Spans)
263 /* Close a list of spans. This will add new segments to the list, mark the end
264 * of existing ones, and remove empty spans from the list.
269 /* Have new segments been added while the span list was open? */
270 for (I = CollCount (Spans); I < CollCount (&SegmentList); ++I) {
272 /* Add new spans if not empty */
273 Segment* S = CollAtUnchecked (&SegmentList, I);
275 /* Segment is empty */
278 CollAppend (Spans, NewSpan (S, 0, S->PC));
281 /* Walk over the spans, close open, remove empty ones */
282 for (I = 0, J = 0; I < CollCount (Spans); ++I) {
284 /* Get the next span */
285 Span* S = CollAtUnchecked (Spans, I);
287 /* Set the end offset */
288 if (S->Start == S->Seg->PC) {
292 /* Span is not empty */
295 /* Merge duplicate spans, then insert it at the new position */
296 CollReplace (Spans, MergeSpan (S), J++);
300 /* New Count is now in J */
306 void WriteSpanList (const Collection* Spans)
307 /* Write a list of spans to the output file */
311 /* We only write spans if debug info is enabled */
313 /* Number of spans is zero */
316 /* Write the number of spans */
317 ObjWriteVar (CollCount (Spans));
319 /* Write the spans */
320 for (I = 0; I < CollCount (Spans); ++I) {
321 /* Write the id of the next span */
322 ObjWriteVar (((const Span*)CollConstAt (Spans, I))->Id);
329 static int CollectSpans (void* Entry, void* Data)
330 /* Collect all spans in a collection sorted by id */
332 /* Cast the pointers to real objects */
334 Collection* C = Data;
336 /* Place the entry into the collection */
337 CollReplaceExpand (C, S, S->Id);
345 void WriteSpans (void)
346 /* Write all spans to the object file */
348 /* Tell the object file module that we're about to start the spans */
351 /* We will write scopes only if debug symbols are requested */
356 /* We must first collect all items in a collection sorted by id */
357 Collection SpanList = STATIC_COLLECTION_INITIALIZER;
358 CollGrow (&SpanList, HT_GetCount (&SpanTab));
360 /* Walk over the hash table and fill the span list */
361 HT_Walk (&SpanTab, CollectSpans, &SpanList);
363 /* Write the span count to the file */
364 ObjWriteVar (CollCount (&SpanList));
366 /* Write all spans */
367 for (I = 0; I < CollCount (&SpanList); ++I) {
369 /* Get the span and check it */
370 const Span* S = CollAtUnchecked (&SpanList, I);
371 CHECK (S->End > S->Start);
373 /* Write data for the span We will write the size instead of the
374 * end offset to save some bytes, since most spans are expected
375 * to be rather small.
377 ObjWriteVar (S->Seg->Num);
378 ObjWriteVar (S->Start);
379 ObjWriteVar (S->End - S->Start);
382 /* Free the collection with the spans */
383 DoneCollection (&SpanList);
387 /* No debug info requested */
392 /* Done writing the spans */