1
0
mirror of https://github.com/cc65/cc65.git synced 2024-12-26 08:32:00 +00:00

Change the implementation of the file table. It may now grow to an arbitrary

size. It is also possible to search in the table by name, which will be
needed later to add line debug information.


git-svn-id: svn://svn.cc65.org/cc65/trunk@262 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz 2000-08-02 14:12:36 +00:00
parent 097a01094e
commit bb115c8ae2
6 changed files with 118 additions and 29 deletions

View File

@ -36,7 +36,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
/* ca65 */ /* ca65 */
#include "filetab.h" #include "filetab.h"
#include "nexttok.h" #include "nexttok.h"
@ -186,6 +186,7 @@ void ErrorMsg (const FilePos* Pos, unsigned ErrNum, va_list ap)
"Counter underflow", "Counter underflow",
"Undefined label", "Undefined label",
"Open `%s´", "Open `%s´",
"File name `%s' not found in file table",
}; };
fprintf (stderr, "%s(%lu): Error #%u: ", fprintf (stderr, "%s(%lu): Error #%u: ",

View File

@ -38,7 +38,8 @@
#include "scanner.h" /* common */
#include "filepos.h"
@ -126,6 +127,7 @@ enum Errors {
ERR_COUNTER_UNDERFLOW, ERR_COUNTER_UNDERFLOW,
ERR_UNDEFINED_LABEL, ERR_UNDEFINED_LABEL,
ERR_OPEN_STMT, ERR_OPEN_STMT,
ERR_FILENAME_NOT_FOUND,
ERR_COUNT /* Error count */ ERR_COUNT /* Error count */
}; };

View File

@ -35,6 +35,7 @@
/* common */ /* common */
#include "check.h" #include "check.h"
#include "hashstr.h"
#include "xmalloc.h" #include "xmalloc.h"
/* ca65 */ /* ca65 */
@ -50,13 +51,24 @@
/* List of input files */ /* An entry in the file table */
static struct { typedef struct FileEntry FileEntry;
unsigned long MTime; /* Time of last modification */ struct FileEntry {
unsigned long Size; /* Size of file */ FileEntry* Next; /* Next in hash list */
const char* Name; /* Name of file */ unsigned Index; /* Index of entry */
} Files [MAX_INPUT_FILES]; unsigned long Size; /* Size of file */
static unsigned FileCount = 0; unsigned long MTime; /* Time of last modification */
char Name[1]; /* Name, dynamically allocated */
};
/* Array of all entries, listed by index */
static FileEntry** FileTab = 0;
static unsigned FileCount = 0;
static unsigned FileMax = 0;
/* Hash table, hashed by name */
#define HASHTAB_SIZE 31
static FileEntry* HashTab[HASHTAB_SIZE];
@ -66,6 +78,52 @@ static unsigned FileCount = 0;
static FileEntry* NewFileEntry (const char* Name, unsigned long Size, unsigned long MTime)
/* Create a new FileEntry, insert it into the tables and return it */
{
/* Get the length of the name */
unsigned Len = strlen (Name);
/* Get the hash over the name */
unsigned Hash = HashStr (Name) % HASHTAB_SIZE;
/* Allocate memory for the entry */
FileEntry* F = xmalloc (sizeof (FileEntry) + Len);
/* Initialize the fields */
F->Index = FileCount+1;
F->Size = Size;
F->MTime = MTime;
memcpy (F->Name, Name, Len+1);
/* Count the entries and grow the file table if needed */
if (FileCount >= FileMax) {
/* We need to grow the table. Create a new one. */
unsigned NewFileMax = (FileMax == 0)? 32 : FileMax * 2;
FileEntry** NewFileTab = xmalloc (sizeof (FileEntry*) * NewFileMax);
/* Copy the old entries */
memcpy (NewFileTab, FileTab, sizeof (FileEntry*) * FileCount);
/* Use the new table */
xfree (FileTab);
FileTab = NewFileTab;
FileMax = NewFileMax;
}
/* Insert the file into the file table */
FileTab [FileCount++] = F;
/* Insert the entry into the hash table */
F->Next = HashTab[Hash];
HashTab[Hash] = F;
/* Return the new entry */
return F;
}
const char* GetFileName (unsigned Name) const char* GetFileName (unsigned Name)
/* Get the name of a file where the name index is known */ /* Get the name of a file where the name index is known */
{ {
@ -79,33 +137,50 @@ const char* GetFileName (unsigned Name)
/* No files defined until now */ /* No files defined until now */
return "(outside file scope)"; return "(outside file scope)";
} else { } else {
return Files [0].Name; return FileTab [0]->Name;
} }
} else { } else {
return Files [Name-1].Name; return FileTab [Name-1]->Name;
} }
} }
unsigned GetFileIndex (const char* Name)
/* Return the file index for the given file name. */
{
/* Get the hash over the name */
unsigned Hash = HashStr (Name) % HASHTAB_SIZE;
/* Search the linear hash list */
FileEntry* F = HashTab[Hash];
while (F) {
/* Is it this one? */
if (strcmp (Name, F->Name) == 0) {
/* Found, return the index */
return F->Index;
}
/* No, check next */
F = F->Next;
}
/* Not found, use main file */
Error (ERR_FILENAME_NOT_FOUND, Name);
return 0;
}
unsigned AddFile (const char* Name, unsigned long Size, unsigned long MTime) unsigned AddFile (const char* Name, unsigned long Size, unsigned long MTime)
/* Add a new file to the list of input files. Return the index of the file in /* Add a new file to the list of input files. Return the index of the file in
* the table. * the table.
*/ */
{ {
/* Check for a table overflow */ /* Create a new file entry and insert it into the tables */
if (FileCount >= MAX_INPUT_FILES) { FileEntry* F = NewFileEntry (Name, Size, MTime);
/* Table overflow */
Fatal (FAT_MAX_INPUT_FILES);
}
/* Add the file to the table */ /* Return the index */
Files [FileCount].Name = xstrdup (Name); return F->Index;
Files [FileCount].Size = Size;
Files [FileCount].MTime = MTime;
/* One more file */
return ++FileCount;
} }
@ -123,9 +198,12 @@ void WriteFiles (void)
/* Write the file data */ /* Write the file data */
for (I = 0; I < FileCount; ++I) { for (I = 0; I < FileCount; ++I) {
ObjWrite32 (Files [I].MTime); /* Get a pointer to the entry */
ObjWrite32 (Files [I].Size); FileEntry* F = FileTab[I];
ObjWriteStr (Files [I].Name); /* Write the fields */
ObjWrite32 (F->MTime);
ObjWrite32 (F->Size);
ObjWriteStr (F->Name);
} }
/* Done writing files */ /* Done writing files */

View File

@ -46,6 +46,7 @@
#include "error.h" #include "error.h"
#include "expr.h" #include "expr.h"
#include "objfile.h" #include "objfile.h"
#include "scanner.h"
#include "symtab.h" #include "symtab.h"
@ -978,7 +979,7 @@ void WriteImports (void)
/* Write the imports list to the object file */ /* Write the imports list to the object file */
{ {
SymEntry* S; SymEntry* S;
/* Tell the object file module that we're about to start the imports */ /* Tell the object file module that we're about to start the imports */
ObjStartImports (); ObjStartImports ();

View File

@ -40,8 +40,10 @@
#include <stdio.h> #include <stdio.h>
#include "../common/exprdefs.h" /* common */
#include "exprdefs.h"
/* ca65 */
#include "symentry.h" #include "symentry.h"
@ -143,3 +145,4 @@ void WriteDbgSyms (void);

View File

@ -38,6 +38,10 @@
#include "scanner.h"
/*****************************************************************************/ /*****************************************************************************/
/* Data */ /* Data */
/*****************************************************************************/ /*****************************************************************************/