Use a collections for the line info and sort them by file/line.

git-svn-id: svn://svn.cc65.org/cc65/trunk@751 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz
2001-05-23 22:02:19 +00:00
parent f3f42c43e5
commit d96baa7c42
2 changed files with 59 additions and 42 deletions

View File

@@ -59,10 +59,8 @@
/* Linked list of all line infos */ /* Collection containing all line infos */
LineInfo* LineInfoRoot = 0; Collection LineInfoColl = STATIC_COLLECTION_INITIALIZER;
LineInfo* LineInfoLast = 0;
unsigned LineInfoCount = 0;
unsigned LineInfoValid = 0; /* Valid, that is, used entries */ unsigned LineInfoValid = 0; /* Valid, that is, used entries */
/* Static pointer to last line info or NULL if not active */ /* Static pointer to last line info or NULL if not active */
@@ -71,7 +69,7 @@ LineInfo* CurLineInfo = 0;
/*****************************************************************************/ /*****************************************************************************/
/* Code */ /* Code */
/*****************************************************************************/ /*****************************************************************************/
@@ -83,23 +81,14 @@ static LineInfo* NewLineInfo (unsigned FileIndex, unsigned long LineNum)
LineInfo* LI = xmalloc (sizeof (LineInfo)); LineInfo* LI = xmalloc (sizeof (LineInfo));
/* Initialize the fields */ /* Initialize the fields */
LI->Next = 0;
LI->Usage = 0; LI->Usage = 0;
LI->Index = 0; /* Currently invalid */ LI->Index = 0; /* Currently invalid */
LI->Pos.Line = LineNum; LI->Pos.Line = LineNum;
LI->Pos.Col = 0; LI->Pos.Col = 0;
LI->Pos.Name = FileIndex; LI->Pos.Name = FileIndex;
/* Insert this structure into the line info list */ /* Insert this structure into the collection */
if (LineInfoLast == 0) { CollAppend (&LineInfoColl, LI);
LineInfoRoot = LI;
} else {
LineInfoLast->Next = LI;
}
LineInfoLast = LI;
/* Count the line infos */
++LineInfoCount;
/* Return the new struct */ /* Return the new struct */
return LI; return LI;
@@ -113,7 +102,10 @@ LineInfo* UseLineInfo (LineInfo* LI)
*/ */
{ {
if (LI) { if (LI) {
++LI->Usage; if (LI->Usage++ == 0) {
/* One more valid line info */
++LineInfoValid;
}
} }
return LI; return LI;
} }
@@ -137,45 +129,72 @@ void ClearLineInfo (void)
void MakeLineInfoIndex (void) static int CmpLineInfo (void* Data, const void* LI1_, const void* LI2_)
/* Walk over the line info list and make an index of all entries ignoring /* Compare function for the sort */
* those with a usage count of zero.
*/
{ {
LineInfo* LI = LineInfoRoot; /* Cast the pointers */
LineInfoValid = 0; const LineInfo* LI1 = LI1_;
while (LI) { const LineInfo* LI2 = LI2_;
if (LI->Usage) {
LI->Index = LineInfoValid++; /* Unreferenced line infos are always larger, otherwise sort by file,
* then by line.
*/
if ((LI1->Usage == 0) == (LI2->Usage == 0)) {
/* Both are either referenced or unreferenced */
if (LI1->Pos.Name< LI2->Pos.Name) {
return -1;
} else if (LI1->Pos.Name > LI2->Pos.Name) {
return 1;
} else if (LI1->Pos.Line < LI2->Pos.Line) {
return -1;
} else if (LI1->Pos.Line > LI2->Pos.Line) {
return 1;
} else {
return 0;
}
} else {
if (LI1->Usage > 0) {
return -1;
} else {
return 1;
} }
LI = LI->Next;
} }
} }
void MakeLineInfoIndex (void)
/* Sort the line infos and drop all unreferenced ones */
{
/* Sort the collection */
CollSort (&LineInfoColl, CmpLineInfo, 0);
}
void WriteLineInfo (void) void WriteLineInfo (void)
/* Write a list of all line infos to the object file. */ /* Write a list of all line infos to the object file. */
{ {
LineInfo* LI;
/* Tell the object file module that we're about to write line infos */ /* Tell the object file module that we're about to write line infos */
ObjStartLineInfos (); ObjStartLineInfos ();
/* Check if debug info is requested */ /* Check if debug info is requested */
if (DbgSyms) { if (DbgSyms) {
unsigned I;
/* Write the line info count to the list */ /* Write the line info count to the list */
ObjWriteVar (LineInfoValid); ObjWriteVar (LineInfoValid);
/* Walk through list and write all line infos that have references */ /* Walk through list and write all line infos that have references.
LI = LineInfoRoot; * Because of the sort, this are exactly the first LineInfoValid
while (LI) { * ones.
if (LI->Usage) { */
/* Write the source file position */ for (I = 0; I < LineInfoValid; ++I) {
ObjWritePos (&LI->Pos); /* Get a pointer to this line info */
} LineInfo* LI = CollAtUnchecked (&LineInfoColl, I);
LI = LI->Next; /* Write the source file position */
ObjWritePos (&LI->Pos);
} }
} else { } else {

View File

@@ -49,6 +49,7 @@
/* common */ /* common */
#include "coll.h"
#include "filepos.h" #include "filepos.h"
/* ca65 */ /* ca65 */
@@ -67,16 +68,13 @@
*/ */
typedef struct LineInfo LineInfo; typedef struct LineInfo LineInfo;
struct LineInfo { struct LineInfo {
LineInfo* Next; /* Pointer to next info in list */
unsigned Usage; /* Usage counter */ unsigned Usage; /* Usage counter */
unsigned Index; /* Index */ unsigned Index; /* Index */
FilePos Pos; /* File position */ FilePos Pos; /* File position */
}; };
/* Linked list of all line infos */ /* Collection containing all line infos */
extern LineInfo* LineInfoRoot; extern Collection LineInfoColl;
extern LineInfo* LineInfoLast;
extern unsigned LineInfoCount;
extern unsigned LineInfoValid; /* Valid, that is, used entries */ extern unsigned LineInfoValid; /* Valid, that is, used entries */
/* Global pointer to last line info or NULL if not active */ /* Global pointer to last line info or NULL if not active */