From f04d65a662482ed181e8c3fb9308d71692d0670f Mon Sep 17 00:00:00 2001 From: uz Date: Thu, 4 Aug 2011 12:31:37 +0000 Subject: [PATCH] Generalized the CodeRange structure used for line infos and renamed it to Span. git-svn-id: svn://svn.cc65.org/cc65/trunk@5112 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/ld65/dbginfo.c | 15 +++-- src/ld65/lineinfo.c | 86 ++------------------------ src/ld65/lineinfo.h | 14 +---- src/ld65/make/gcc.mak | 1 + src/ld65/make/watcom.mak | 1 + src/ld65/span.c | 128 +++++++++++++++++++++++++++++++++++++++ src/ld65/span.h | 95 +++++++++++++++++++++++++++++ 7 files changed, 241 insertions(+), 99 deletions(-) create mode 100644 src/ld65/span.c create mode 100644 src/ld65/span.h diff --git a/src/ld65/dbginfo.c b/src/ld65/dbginfo.c index 72363fe78..7dca2fef9 100644 --- a/src/ld65/dbginfo.c +++ b/src/ld65/dbginfo.c @@ -77,20 +77,20 @@ void PrintDbgInfo (ObjData* O, FILE* F) unsigned Type = LI_GET_TYPE (LI->Type); unsigned Count = LI_GET_COUNT (LI->Type); - /* Get a pointer to the code ranges */ - const Collection* CodeRanges = &LI->CodeRanges; + /* Get a pointer to the spans */ + const Collection* Spans = &LI->Spans; - /* Code ranges */ - for (J = 0; J < CollCount (CodeRanges); ++J) { + /* Spans */ + for (J = 0; J < CollCount (Spans); ++J) { /* Get this code range */ - const CodeRange* R = CollConstAt (CodeRanges, J); + const Span* S = CollConstAt (Spans, J); /* Print it */ fprintf (F, "line\tfile=%u,line=%lu,segment=%u,range=0x%lX-0x%lX", - LI->File->Id, GetSourceLine (LI), R->Seg->Id, - R->Offs, R->Offs + R->Size - 1); + LI->File->Id, GetSourceLine (LI), S->Seg->Id, + S->Offs, S->Offs + S->Size - 1); /* Print type if not LI_TYPE_ASM and count if not zero */ if (Type != LI_TYPE_ASM) { @@ -109,4 +109,3 @@ void PrintDbgInfo (ObjData* O, FILE* F) - diff --git a/src/ld65/lineinfo.c b/src/ld65/lineinfo.c index 2ed4f7a33..219dcf1cc 100644 --- a/src/ld65/lineinfo.c +++ b/src/ld65/lineinfo.c @@ -55,32 +55,6 @@ -static CodeRange* NewCodeRange (Segment* Seg, unsigned long Offs, unsigned long Size) -/* Create and return a new CodeRange struct */ -{ - /* Allocate memory */ - CodeRange* R = xmalloc (sizeof (CodeRange)); - - /* Initialize the fields */ - R->Seg = Seg; - R->Offs = Offs; - R->Size = Size; - - /* Return the new struct */ - return R; -} - - - -static void FreeCodeRange (CodeRange* R) -/* Free a CodeRange structure */ -{ - /* Just free the memory */ - xfree (R); -} - - - static LineInfo* NewLineInfo (void) /* Create and return a new LineInfo struct with mostly empty fields */ { @@ -94,7 +68,7 @@ static LineInfo* NewLineInfo (void) LI->Pos.Line = 0; LI->Pos.Col = 0; LI->Fragments = EmptyCollection; - LI->CodeRanges = EmptyCollection; + LI->Spans = EmptyCollection; /* Return the new struct */ return LI; @@ -113,12 +87,12 @@ void FreeLineInfo (LineInfo* LI) PRECONDITION (CollCount (&LI->Fragments) == 0); /* Free all the code ranges */ - for (I = 0; I < CollCount (&LI->CodeRanges); ++I) { - FreeCodeRange (CollAtUnchecked (&LI->CodeRanges, I)); + for (I = 0; I < CollCount (&LI->Spans); ++I) { + FreeSpan (CollAtUnchecked (&LI->Spans, I)); } /* Free the collections */ - DoneCollection (&LI->CodeRanges); + DoneCollection (&LI->Spans); /* Free the structure itself */ xfree (LI); @@ -191,55 +165,6 @@ void ReadLineInfoList (FILE* F, ObjData* O, Collection* LineInfos) -static void AddCodeRange (LineInfo* LI, Segment* Seg, unsigned long Offs, - unsigned long Size) -/* Add a range of code to this line */ -{ - unsigned I; - - /* Get a pointer to the collection */ - Collection* CodeRanges = &LI->CodeRanges; - - /* We will keep the CodeRanges collection sorted by starting offset, - * so we have to search for the correct insert position. Since in most - * cases, the fragments have increasing order, and there is usually not - * more than one or two ranges, we do a linear search. - */ - for (I = 0; I < CollCount (CodeRanges); ++I) { - CodeRange* R = CollAtUnchecked (CodeRanges, I); - /* Must be same segment */ - if (R->Seg == Seg) { - if (Offs < R->Offs) { - - /* Got the insert position */ - if (Offs + Size == R->Offs) { - /* Merge the two */ - R->Offs = Offs; - R->Size += Size; - } else { - /* Insert a new entry */ - CollInsert (CodeRanges, NewCodeRange (Seg, Offs, Size), I); - } - - /* Done */ - return; - - } else if (R->Offs + R->Size == Offs) { - /* This is the regular case. Merge the two. */ - R->Size += Size; - - /* Done */ - return; - } - } - } - - /* We must append an entry */ - CollAppend (CodeRanges, NewCodeRange (Seg, Offs, Size)); -} - - - void RelocLineInfo (Segment* S) /* Relocate the line info for a segment. */ { @@ -261,7 +186,8 @@ void RelocLineInfo (Segment* S) /* Add the range for this fragment to all line infos */ for (I = 0; I < CollCount (&Frag->LineInfos); ++I) { - AddCodeRange (CollAt (&Frag->LineInfos, I), S, Offs, Frag->Size); + LineInfo* LI = CollAtUnchecked (&Frag->LineInfos, I); + AddSpan (&LI->Spans, S, Offs, Frag->Size); } /* Update the offset */ diff --git a/src/ld65/lineinfo.h b/src/ld65/lineinfo.h index 0c92423fc..bc7106eff 100644 --- a/src/ld65/lineinfo.h +++ b/src/ld65/lineinfo.h @@ -45,12 +45,13 @@ #include "filepos.h" /* ld65 */ +#include "span.h" #include "spool.h" /*****************************************************************************/ -/* Forwards */ +/* Forwards */ /*****************************************************************************/ @@ -66,15 +67,6 @@ struct Segment; -typedef struct CodeRange CodeRange; -struct CodeRange { - struct Segment* Seg; /* Segment of this code range */ - unsigned long Offs; /* Offset of code range */ - unsigned long Size; /* Size of code range */ -}; - - - /* Structure holding line information. The Pos.Name field is always the * global string id of the file name. If the line info was read from the * object file, the File pointer is valid, otherwise it is NULL. @@ -85,7 +77,7 @@ struct LineInfo { unsigned Type; /* Type of line info */ FilePos Pos; /* Position in file */ Collection Fragments; /* Fragments for this line */ - Collection CodeRanges; /* Code ranges for this line */ + Collection Spans; /* Spans for this line */ }; diff --git a/src/ld65/make/gcc.mak b/src/ld65/make/gcc.mak index 1ce5654e5..89bce8d4c 100644 --- a/src/ld65/make/gcc.mak +++ b/src/ld65/make/gcc.mak @@ -58,6 +58,7 @@ OBJS = asserts.o \ scanner.o \ scopes.o \ segments.o \ + span.o \ spool.o \ tgtcfg.o diff --git a/src/ld65/make/watcom.mak b/src/ld65/make/watcom.mak index 1020c8142..37a8e6fc6 100644 --- a/src/ld65/make/watcom.mak +++ b/src/ld65/make/watcom.mak @@ -89,6 +89,7 @@ OBJS = asserts.obj \ scanner.obj \ scopes.obj \ segments.obj \ + span.obj \ spool.obj \ tgtcfg.obj diff --git a/src/ld65/span.c b/src/ld65/span.c new file mode 100644 index 000000000..aa6ceda87 --- /dev/null +++ b/src/ld65/span.c @@ -0,0 +1,128 @@ +/*****************************************************************************/ +/* */ +/* 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 */ +#include "xmalloc.h" + +/* ld65 */ +#include "span.h" + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +Span* NewSpan (struct Segment* Seg, unsigned long Offs, unsigned long Size) +/* Create and return a new span */ +{ + /* Allocate memory */ + Span* S = xmalloc (sizeof (*S)); + + /* Initialize the fields */ + S->Seg = Seg; + S->Offs = Offs; + S->Size = Size; + + /* Return the result */ + return S; +} + + + +void FreeSpan (Span* S) +/* Free a span structure */ +{ + /* Just free the structure */ + xfree (S); +} + + + +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; + + /* We don't have many spans in a collection, so we do a linear search here. + * The collection is kept sorted which eases our work here. + */ + for (I = 0; I < CollCount (Spans); ++I) { + + /* Get the next span */ + Span* S = CollAtUnchecked (Spans, I); + + /* Must be same segment, otherwise we cannot merge */ + if (S->Seg != Seg) { + continue; + } + + /* Check if we can merge it */ + if (Offs < S->Offs) { + + /* Got the insert position */ + if (Offs + Size == S->Offs) { + /* Merge the two */ + S->Offs = Offs; + S->Size += Size; + } else { + /* Insert a new entry */ + CollInsert (Spans, NewSpan (Seg, Offs, Size), I); + } + + /* Done */ + return; + + } else if (S->Offs + S->Size == Offs) { + + /* This is the regular case. Merge the two. */ + S->Size += Size; + + /* Done */ + return; + } + } + + /* We must append an entry */ + CollAppend (Spans, NewSpan (Seg, Offs, Size)); +} + + + diff --git a/src/ld65/span.h b/src/ld65/span.h new file mode 100644 index 000000000..cae1cfd73 --- /dev/null +++ b/src/ld65/span.h @@ -0,0 +1,95 @@ +/*****************************************************************************/ +/* */ +/* span.h */ +/* */ +/* 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. */ +/* */ +/*****************************************************************************/ + + + +#ifndef SPAN_H +#define SPAN_H + + + +/* common */ +#include "coll.h" + + + +/*****************************************************************************/ +/* Forwards */ +/*****************************************************************************/ + + + +struct Segment; + + + +/*****************************************************************************/ +/* Data */ +/*****************************************************************************/ + + + +typedef struct Span Span; +struct Span { + struct Segment* Seg; /* Segment of this span */ + unsigned long Offs; /* Offset of span within segment */ + unsigned long Size; /* Size of span */ +}; + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +Span* NewSpan (struct Segment* Seg, unsigned long Offs, unsigned long Size); +/* Create and return a new span */ + +void FreeSpan (Span* S); +/* Free a span structure */ + +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. + */ + + + +/* End of span.h */ +#endif + + + -- 2.39.5