2011-08-04 12:31:37 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* span.c */
|
|
|
|
/* */
|
|
|
|
/* A span of data within a segment */
|
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* (C) 2011, Ullrich von Bassewitz */
|
|
|
|
/* Roemerstrasse 52 */
|
|
|
|
/* D-70794 Filderstadt */
|
|
|
|
/* EMail: uz@cc65.org */
|
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* This software is provided 'as-is', without any expressed or implied */
|
|
|
|
/* warranty. In no event will the authors be held liable for any damages */
|
|
|
|
/* arising from the use of this software. */
|
|
|
|
/* */
|
|
|
|
/* Permission is granted to anyone to use this software for any purpose, */
|
|
|
|
/* including commercial applications, and to alter it and redistribute it */
|
|
|
|
/* freely, subject to the following restrictions: */
|
|
|
|
/* */
|
|
|
|
/* 1. The origin of this software must not be misrepresented; you must not */
|
|
|
|
/* claim that you wrote the original software. If you use this software */
|
|
|
|
/* in a product, an acknowledgment in the product documentation would be */
|
|
|
|
/* appreciated but is not required. */
|
|
|
|
/* 2. Altered source versions must be plainly marked as such, and must not */
|
|
|
|
/* be misrepresented as being the original software. */
|
|
|
|
/* 3. This notice may not be removed or altered from any source */
|
|
|
|
/* distribution. */
|
|
|
|
/* */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* common */
|
2011-08-21 20:18:09 +00:00
|
|
|
#include "gentype.h"
|
2011-08-04 12:31:37 +00:00
|
|
|
#include "xmalloc.h"
|
2011-08-04 13:14:26 +00:00
|
|
|
|
2011-08-11 17:11:45 +00:00
|
|
|
/* ld65 */
|
2011-08-04 13:14:26 +00:00
|
|
|
#include "fileio.h"
|
|
|
|
#include "objdata.h"
|
|
|
|
#include "segments.h"
|
2011-08-04 12:31:37 +00:00
|
|
|
#include "span.h"
|
2011-08-22 18:22:45 +00:00
|
|
|
#include "tpool.h"
|
2011-08-04 12:31:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2011-08-11 17:11:45 +00:00
|
|
|
/* Data */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-14 19:24:03 +00:00
|
|
|
/* Definition of a span */
|
|
|
|
struct Span {
|
2013-05-09 11:56:54 +00:00
|
|
|
unsigned Id; /* Id of the span */
|
2011-08-14 19:24:03 +00:00
|
|
|
unsigned Sec; /* Section id of this span */
|
|
|
|
unsigned long Offs; /* Offset of span within segment */
|
|
|
|
unsigned long Size; /* Size of span */
|
2011-08-21 19:44:02 +00:00
|
|
|
unsigned Type; /* Generic type of the data */
|
2011-08-14 19:24:03 +00:00
|
|
|
};
|
2011-08-11 17:11:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Code */
|
2011-08-04 12:31:37 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-21 19:44:02 +00:00
|
|
|
static Span* NewSpan (unsigned Id)
|
2011-08-04 13:14:26 +00:00
|
|
|
/* Create and return a new span */
|
2011-08-04 12:31:37 +00:00
|
|
|
{
|
|
|
|
/* Allocate memory */
|
2011-08-21 19:44:02 +00:00
|
|
|
Span* S = xmalloc (sizeof (Span));
|
2011-08-04 12:31:37 +00:00
|
|
|
|
2011-08-21 19:44:02 +00:00
|
|
|
/* Initialize the fields as necessary */
|
2011-08-21 19:08:23 +00:00
|
|
|
S->Id = Id;
|
2011-08-04 12:31:37 +00:00
|
|
|
|
|
|
|
/* Return the result */
|
2011-08-11 17:11:45 +00:00
|
|
|
return S;
|
2011-08-04 12:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-21 19:44:02 +00:00
|
|
|
Span* ReadSpan (FILE* F, ObjData* O, unsigned Id)
|
2011-08-04 13:14:26 +00:00
|
|
|
/* Read a Span from a file and return it */
|
|
|
|
{
|
2011-08-22 18:22:45 +00:00
|
|
|
unsigned Type;
|
|
|
|
|
2011-08-21 19:44:02 +00:00
|
|
|
/* Create a new Span and initialize it */
|
|
|
|
Span* S = NewSpan (Id);
|
|
|
|
S->Sec = ReadVar (F);
|
|
|
|
S->Offs = ReadVar (F);
|
|
|
|
S->Size = ReadVar (F);
|
2011-08-22 18:22:45 +00:00
|
|
|
|
|
|
|
/* Read the type. An id of zero means an empty string (no need to check) */
|
|
|
|
Type = ReadVar (F);
|
|
|
|
if (Type == 0) {
|
|
|
|
S->Type = INVALID_TYPE_ID;
|
|
|
|
} else {
|
|
|
|
S->Type = GetTypeId (GetObjString (O, Type));
|
|
|
|
}
|
2011-08-21 19:44:02 +00:00
|
|
|
|
|
|
|
/* Return the new span */
|
|
|
|
return S;
|
2011-08-04 13:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-22 17:36:19 +00:00
|
|
|
unsigned* ReadSpanList (FILE* F)
|
|
|
|
/* Read a list of span ids from a file. The list is returned as an array of
|
2014-06-30 09:10:35 +00:00
|
|
|
** unsigneds, the first being the number of spans (never zero) followed by
|
|
|
|
** the span ids. If the number of spans is zero, NULL is returned.
|
|
|
|
*/
|
2011-08-04 13:14:26 +00:00
|
|
|
{
|
2011-08-22 17:36:19 +00:00
|
|
|
unsigned* Spans;
|
|
|
|
|
2011-08-04 13:14:26 +00:00
|
|
|
/* First is number of Spans */
|
|
|
|
unsigned Count = ReadVar (F);
|
2011-08-22 17:36:19 +00:00
|
|
|
if (Count == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2011-08-04 13:14:26 +00:00
|
|
|
|
2011-08-22 17:36:19 +00:00
|
|
|
/* Allocate memory for the list and set the count */
|
|
|
|
Spans = xmalloc ((Count + 1) * sizeof (*Spans));
|
|
|
|
*Spans = Count;
|
2011-08-04 13:14:26 +00:00
|
|
|
|
|
|
|
/* Read the spans and add them */
|
2011-08-22 17:36:19 +00:00
|
|
|
while (Count) {
|
|
|
|
Spans[Count] = ReadVar (F);
|
|
|
|
--Count;
|
2011-08-04 13:14:26 +00:00
|
|
|
}
|
2011-08-22 17:36:19 +00:00
|
|
|
|
|
|
|
/* Return the list */
|
|
|
|
return Spans;
|
2011-08-04 13:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-11-03 21:37:19 +00:00
|
|
|
unsigned* DupSpanList (const unsigned* S)
|
|
|
|
/* Duplicate a span list */
|
|
|
|
{
|
|
|
|
unsigned Size;
|
|
|
|
|
|
|
|
/* The list may be empty */
|
|
|
|
if (S == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate memory, copy and return the new list */
|
|
|
|
Size = (*S + 1) * sizeof (*S);
|
|
|
|
return memcpy (xmalloc (Size), S, Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-04 12:31:37 +00:00
|
|
|
void FreeSpan (Span* S)
|
|
|
|
/* Free a span structure */
|
|
|
|
{
|
|
|
|
/* Just free the structure */
|
|
|
|
xfree (S);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-14 19:24:03 +00:00
|
|
|
unsigned SpanCount (void)
|
|
|
|
/* Return the total number of spans */
|
|
|
|
{
|
|
|
|
/* Walk over all object files */
|
|
|
|
unsigned I;
|
|
|
|
unsigned Count = 0;
|
|
|
|
for (I = 0; I < CollCount (&ObjDataList); ++I) {
|
2011-08-04 12:31:37 +00:00
|
|
|
|
2011-08-14 19:24:03 +00:00
|
|
|
/* Get this object file */
|
|
|
|
const ObjData* O = CollAtUnchecked (&ObjDataList, I);
|
2011-08-04 12:31:37 +00:00
|
|
|
|
2011-08-14 19:24:03 +00:00
|
|
|
/* Count spans */
|
|
|
|
Count += CollCount (&O->Spans);
|
2011-08-04 12:31:37 +00:00
|
|
|
}
|
|
|
|
|
2011-08-14 19:24:03 +00:00
|
|
|
return Count;
|
2011-08-04 12:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-22 17:36:19 +00:00
|
|
|
void PrintDbgSpanList (FILE* F, const ObjData* O, const unsigned* List)
|
|
|
|
/* Output a string ",span=x[+y...]" for the given list. If the list is empty
|
2014-06-30 09:10:35 +00:00
|
|
|
** or NULL, output nothing. This is a helper function for other modules to
|
|
|
|
** print a list of spans read by ReadSpanList to the debug info file.
|
|
|
|
*/
|
2011-08-22 17:36:19 +00:00
|
|
|
{
|
|
|
|
if (List && *List) {
|
|
|
|
unsigned I;
|
|
|
|
const char* Format = ",span=%u";
|
|
|
|
for (I = 0; I < *List; ++I) {
|
|
|
|
fprintf (F, Format, O->SpanBaseId + List[I+1]);
|
|
|
|
Format = "+%u";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-14 19:24:03 +00:00
|
|
|
void PrintDbgSpans (FILE* F)
|
|
|
|
/* Output the spans to a debug info file */
|
2011-08-11 17:11:45 +00:00
|
|
|
{
|
2011-08-14 19:24:03 +00:00
|
|
|
unsigned I, J;
|
2011-08-21 20:18:09 +00:00
|
|
|
StrBuf SpanType = STATIC_STRBUF_INITIALIZER;
|
|
|
|
|
2011-08-11 17:11:45 +00:00
|
|
|
|
2011-08-14 19:24:03 +00:00
|
|
|
/* Walk over all object files */
|
|
|
|
for (I = 0; I < CollCount (&ObjDataList); ++I) {
|
2011-08-11 17:11:45 +00:00
|
|
|
|
2011-08-14 19:24:03 +00:00
|
|
|
/* Get this object file */
|
|
|
|
ObjData* O = CollAtUnchecked (&ObjDataList, I);
|
2011-08-11 17:11:45 +00:00
|
|
|
|
2011-08-14 19:24:03 +00:00
|
|
|
/* Walk over all spans in this object file */
|
|
|
|
for (J = 0; J < CollCount (&O->Spans); ++J) {
|
|
|
|
|
|
|
|
/* Get this span */
|
2011-08-21 20:18:09 +00:00
|
|
|
const Span* S = CollAtUnchecked (&O->Spans, J);
|
2011-08-11 17:11:45 +00:00
|
|
|
|
2011-08-14 19:24:03 +00:00
|
|
|
/* Get the section for this span */
|
|
|
|
const Section* Sec = GetObjSection (O, S->Sec);
|
2011-08-11 17:11:45 +00:00
|
|
|
|
2011-08-14 19:24:03 +00:00
|
|
|
/* Output the data */
|
2011-08-21 20:18:09 +00:00
|
|
|
fprintf (F, "span\tid=%u,seg=%u,start=%lu,size=%lu",
|
2011-08-18 11:57:17 +00:00
|
|
|
O->SpanBaseId + S->Id,
|
|
|
|
Sec->Seg->Id,
|
|
|
|
Sec->Offs + S->Offs,
|
2011-08-14 19:24:03 +00:00
|
|
|
S->Size);
|
2011-08-21 20:18:09 +00:00
|
|
|
|
|
|
|
/* If we have a type, add it */
|
2011-08-22 18:22:45 +00:00
|
|
|
if (S->Type != INVALID_TYPE_ID) {
|
|
|
|
fprintf (F, ",type=%u", S->Type);
|
2011-08-21 20:18:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Terminate the output line */
|
|
|
|
fputc ('\n', F);
|
2011-08-14 19:24:03 +00:00
|
|
|
}
|
2011-08-11 17:11:45 +00:00
|
|
|
}
|
2011-08-21 20:18:09 +00:00
|
|
|
|
|
|
|
/* Free the string buffer */
|
|
|
|
SB_Done (&SpanType);
|
2011-08-11 17:11:45 +00:00
|
|
|
}
|