+struct Section* GetObjSection (ObjData* O, unsigned Id)
+/* Get a section from an object file checking for a valid index */
+{
+ if (Id >= CollCount (&O->Sections)) {
+ Error ("Invalid section index (%u) in module `%s'",
+ Id, GetObjFileName (O));
+ }
+ return CollAtUnchecked (&O->Sections, Id);
+}
+
+
+
+struct Scope* GetObjScope (ObjData* O, unsigned Id)
+/* Get a scope from an object file checking for a valid index */
+{
+ if (Id >= CollCount (&O->Scopes)) {
+ Error ("Invalid scope index (%u) in module `%s'",
+ Id, GetObjFileName (O));
+ }
+ return CollAtUnchecked (&O->Scopes, Id);
+}
+
+
+
+/* Forwards */
+struct Scope;
+struct Section;
+
/* Values for the Flags field */
#define OBJ_REF 0x0001 /* We have a reference to this file */
# define ObjHasFiles(O) ((O) != 0 && CollCount (&(O)->Files) != 0)
#endif
+struct Section* GetObjSection (ObjData* Obj, unsigned Id);
+/* Get a section from an object file checking for a valid index */
+
+struct Scope* GetObjScope (ObjData* Obj, unsigned Id);
+/* Get a scope from an object file checking for a valid index */
+
/* End of objdata.h */
/* Read the assertions from the object file */
ObjReadAssertions (Obj, O->Header.AssertOffs, O);
- /* Read the scope table from the object file */
- ObjReadScopes (Obj, O->Header.ScopeOffs, O);
-
- /* Read the segment list from the object file. This must be last, since
+ /* Read the segment list from the object file. This must be late, since
* the expressions stored in the code may reference segments or imported
* symbols.
*/
ObjReadSections (Obj, O->Header.SegOffs, O);
+ /* Read the scope table from the object file. Scopes reference segments, so
+ * we must read them after the sections.
+ */
+ ObjReadScopes (Obj, O->Header.ScopeOffs, O);
+
/* Mark this object file as needed */
O->Flags |= OBJ_REF;
#include "error.h"
#include "fileio.h"
#include "scopes.h"
-
-
-
-/*****************************************************************************/
-/* Data */
-/*****************************************************************************/
-
-
-
-typedef struct SegRange SegRange;
-struct SegRange {
- unsigned SegId; /* Id of segment */
- unsigned long Start; /* Start of range */
- unsigned long End; /* End of range */
-};
+#include "span.h"
Scope* S = xmalloc (sizeof (Scope));
/* Initialize the fields where necessary */
- S->Id = Id;
- S->Obj = Obj;
- S->Size = 0;
- S->SegRanges = EmptyCollection;
+ S->Id = Id;
+ S->Obj = Obj;
+ S->Size = 0;
+ S->Spans = EmptyCollection;
/* Return the new entry */
return S;
-static SegRange* NewSegRange (void)
-/* Create a new SegRange and return it */
-{
- /* Allocate memory and return it */
- return xmalloc (sizeof (SegRange));
-}
-
-
-
-static SegRange* ReadSegRange (FILE* F)
-/* Read a SegRange from a file and return it */
-{
- /* Create a new SegRange */
- SegRange* S = NewSegRange ();
-
- /* Read data */
- S->SegId = ReadVar (F);
- S->Start = ReadVar (F);
- S->End = ReadVar (F);
-
- /* Return the SegRange read */
- return S;
-}
-
-
-
Scope* ReadScope (FILE* F, ObjData* Obj, unsigned Id)
/* Read a scope from a file and return it */
{
- unsigned Count;
-
/* Create a new scope */
Scope* S = NewScope (Obj, Id);
}
/* Read the segment ranges for this scope */
- Count = ReadVar (F);
- while (Count--) {
- CollAppend (&S->SegRanges, ReadSegRange (F));
- }
+ ReadSpans (&S->Spans, F, Obj);
/* Return the new Scope */
return S;
/* Root scope */
S->Parent.Scope = 0;
} else {
- /* Check the data */
- unsigned ParentId = S->Parent.Id;
- if (ParentId >= CollCount (&Obj->Scopes)) {
- Error ("Invalid scope index (%u) in module `%s'",
- ParentId, GetObjFileName (Obj));
- }
- S->Parent.Scope = CollAtUnchecked (&Obj->Scopes, S->Parent.Id);
+ S->Parent.Scope = GetObjScope (Obj, S->Parent.Id);
}
}
}
unsigned Type; /* Type of scope */
unsigned Name; /* Name of scope */
unsigned long Size; /* Size of scope */
- Collection SegRanges; /* Segment ranges for this scope */
+ Collection Spans; /* Spans for this scope */
};
#endif
-
+
/* common */
#include "xmalloc.h"
-
-/* ld65 */
+
+/* ld65 */
+#include "fileio.h"
+#include "objdata.h"
+#include "segments.h"
#include "span.h"
Span* NewSpan (struct Segment* Seg, unsigned long Offs, unsigned long Size)
-/* Create and return a new span */
+/* Create and return a new span */
{
/* Allocate memory */
Span* S = xmalloc (sizeof (*S));
+Span* ReadSpan (FILE* F, ObjData* O)
+/* Read a Span from a file and return it */
+{
+ /* Read the section id and translate it to a section pointer */
+ Section* Sec = GetObjSection (O, ReadVar (F));
+
+ /* Read the offset and relocate it */
+ unsigned long Offs = ReadVar (F) + Sec->Offs;
+
+ /* Create and return a new Span */
+ return NewSpan (Sec->Seg, Offs, ReadVar (F));
+}
+
+
+
+void ReadSpans (Collection* Spans, FILE* F, ObjData* O)
+/* Read a list of Spans from a file and return it */
+{
+ /* First is number of Spans */
+ unsigned Count = ReadVar (F);
+
+ /* Preallocate enough entries in the collection */
+ CollGrow (Spans, Count);
+
+ /* Read the spans and add them */
+ while (Count--) {
+ CollAppend (Spans, ReadSpan (F, O));
+ }
+}
+
+
+
void FreeSpan (Span* S)
/* Free a span structure */
{
unsigned long Size)
/* Either add a new span to the ones already in the given collection, or - if
* possible - merge it with adjacent ones that already exist.
- */
+ */
{
unsigned I;
+#include <stdio.h>
+
/* common */
#include "coll.h"
+struct ObjData;
struct Segment;
Span* NewSpan (struct Segment* Seg, unsigned long Offs, unsigned long Size);
/* Create and return a new span */
+Span* ReadSpan (FILE* F, struct ObjData* O);
+/* Read a Span from a file and return it */
+
+void ReadSpans (Collection* Spans, FILE* F, struct ObjData* O);
+/* Read a list of Spans from a file and return it */
+
void FreeSpan (Span* S);
/* Free a span structure */