From 04a0dafe258b8406cc2eabf0d6b987836bc30a1c Mon Sep 17 00:00:00 2001 From: uz Date: Thu, 4 Aug 2011 13:14:26 +0000 Subject: [PATCH] Use the Span structure also for scopes. git-svn-id: svn://svn.cc65.org/cc65/trunk@5115 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/ld65/objdata.c | 24 +++++++++++++++++ src/ld65/objdata.h | 10 +++++++ src/ld65/objfile.c | 10 ++++--- src/ld65/scopes.c | 65 +++++----------------------------------------- src/ld65/scopes.h | 4 +-- src/ld65/span.c | 43 +++++++++++++++++++++++++++--- src/ld65/span.h | 9 +++++++ 7 files changed, 97 insertions(+), 68 deletions(-) diff --git a/src/ld65/objdata.c b/src/ld65/objdata.c index 694940bd6..ce22898c6 100644 --- a/src/ld65/objdata.c +++ b/src/ld65/objdata.c @@ -186,3 +186,27 @@ const char* GetObjFileName (const ObjData* O) +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); +} + + + diff --git a/src/ld65/objdata.h b/src/ld65/objdata.h index 2ea3d418f..bd838cf3d 100644 --- a/src/ld65/objdata.h +++ b/src/ld65/objdata.h @@ -51,6 +51,10 @@ +/* Forwards */ +struct Scope; +struct Section; + /* Values for the Flags field */ #define OBJ_REF 0x0001 /* We have a reference to this file */ @@ -129,6 +133,12 @@ INLINE int ObjHasFiles (const ObjData* O) # 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 */ diff --git a/src/ld65/objfile.c b/src/ld65/objfile.c index 8f7fbe9d6..5eb60c5e7 100644 --- a/src/ld65/objfile.c +++ b/src/ld65/objfile.c @@ -317,15 +317,17 @@ void ObjAdd (FILE* Obj, const char* Name) /* 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; diff --git a/src/ld65/scopes.c b/src/ld65/scopes.c index 994011f8b..106bd46a9 100644 --- a/src/ld65/scopes.c +++ b/src/ld65/scopes.c @@ -40,21 +40,7 @@ #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" @@ -71,10 +57,10 @@ static Scope* NewScope (ObjData* Obj, unsigned Id) 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; @@ -82,37 +68,9 @@ static Scope* NewScope (ObjData* Obj, unsigned Id) -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); @@ -127,10 +85,7 @@ Scope* ReadScope (FILE* F, ObjData* Obj, unsigned 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; @@ -154,13 +109,7 @@ void ResolveScopes (ObjData* Obj) /* 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); } } } diff --git a/src/ld65/scopes.h b/src/ld65/scopes.h index 2217a8d1b..82f1da00e 100644 --- a/src/ld65/scopes.h +++ b/src/ld65/scopes.h @@ -69,7 +69,7 @@ struct Scope { 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 */ }; @@ -93,4 +93,4 @@ void ResolveScopes (ObjData* Obj); #endif - + diff --git a/src/ld65/span.c b/src/ld65/span.c index aa6ceda87..f1bf280c3 100644 --- a/src/ld65/span.c +++ b/src/ld65/span.c @@ -35,8 +35,11 @@ /* common */ #include "xmalloc.h" - -/* ld65 */ + +/* ld65 */ +#include "fileio.h" +#include "objdata.h" +#include "segments.h" #include "span.h" @@ -48,7 +51,7 @@ 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)); @@ -64,6 +67,38 @@ Span* NewSpan (struct Segment* Seg, unsigned long Offs, unsigned long Size) +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 */ { @@ -77,7 +112,7 @@ void AddSpan (Collection* Spans, struct Segment* Seg, unsigned long Offs, 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; diff --git a/src/ld65/span.h b/src/ld65/span.h index cae1cfd73..82c7cf81b 100644 --- a/src/ld65/span.h +++ b/src/ld65/span.h @@ -38,6 +38,8 @@ +#include + /* common */ #include "coll.h" @@ -49,6 +51,7 @@ +struct ObjData; struct Segment; @@ -77,6 +80,12 @@ struct Span { 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 */