Rewrote the input file management.

Added preliminary version of the dependency file creation.


git-svn-id: svn://svn.cc65.org/cc65/trunk@309 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz
2000-09-01 19:20:32 +00:00
parent d62e48f14a
commit 5abb3954a6
6 changed files with 322 additions and 145 deletions

View File

@@ -69,73 +69,81 @@ typedef enum {
/* Bits encoding the type of the objects stored in List for this /* Bits encoding the type of the objects stored in List for this
* particular node. * particular node.
*/ */
NT_LIST_NODE = 0x0000, /* Items are expression nodes */ NT_LIST_NONE = 0x0000, /* No items */
NT_LIST_SYM = 0x0100, /* Items are symbol table entries */ NT_LIST_EXPR = 0x0100, /* Items are expression nodes */
NT_LIST_STRING = 0x0200, /* List item are character strings */ NT_LIST_SYM = 0x0200, /* Items are symbol table entries */
NT_LIST_STRING = 0x0300, /* List item are character strings */
NT_MASK_LIST = 0x0300,
NT_NONE, /* None (invalid) op */ /* Two bits telling if this is a leaf or a branch */
NT_BRANCH = 0x4000, /* Branch */
NT_LEAF = 0x8000, /* Leaf */
NT_SYM, /* Symbol */ /* Special node type */
NT_CONST, /* A constant of some sort */ NT_NONE = 0x0000, /* None (invalid) op */
NT_ASM, /* Inline assembler */
NT_REG_A, /* A register */ /* Leaves */
NT_REG_X, /* X register */ NT_SYM = 0x0001 | NT_LEAF | NT_LIST_SYM, /* Symbol */
NT_REG_Y, /* Y register */ NT_CONST = 0x0002 | NT_LEAF | NT_LIST_NONE, /* A constant of some sort */
NT_REG_AX, /* AX register */ NT_ASM = 0x0003 | NT_LEAF | NT_LIST_STRING, /* Inline assembler */
NT_REG_EAX, /* EAX register */
NT_ARRAY_SUBSCRIPT, /* Array subscript */ NT_REG_A = 0x0005 | NT_LEAF | NT_LIST_NONE, /* A register */
NT_STRUCT_ACCESS, /* Access of a struct field */ NT_REG_X = 0x0006 | NT_LEAF | NT_LIST_NONE, /* X register */
NT_STRUCTPTR_ACCESS, /* Access via struct ptr */ NT_REG_Y = 0x0007 | NT_LEAF | NT_LIST_NONE, /* Y register */
NT_FUNCTION_CALL, /* Call a function */ NT_REG_AX = 0x0008 | NT_LEAF | NT_LIST_NONE, /* AX register */
NT_TYPECAST, /* A cast */ NT_REG_EAX = 0x0009 | NT_LEAF | NT_LIST_NONE, /* EAX register */
NT_ADDRESS, /* Address operator (&) */
NT_INDIRECT, /* Indirection operator (*) */
NT_UNARY_MINUS, /* Branches */
NT_COMPLEMENT, /* ~ */ NT_ARRAY_SUBSCRIPT = 0x0010 | NT_BRANCH | NT_LIST_EXPR, /* Array subscript */
NT_BOOL_NOT, /* ! */ NT_STRUCT_ACCESS = 0x0011 | NT_BRANCH | NT_LIST_EXPR, /* Access of a struct field */
NT_STRUCTPTR_ACCESS = 0x0012 | NT_BRANCH | NT_LIST_EXPR, /* Access via struct ptr */
NT_FUNCTION_CALL = 0x0013 | NT_BRANCH | NT_LIST_EXPR, /* Call a function */
NT_TYPECAST = 0x0014 | NT_BRANCH | NT_LIST_EXPR, /* A cast */
NT_ADDRESS = 0x0015 | NT_BRANCH | NT_LIST_EXPR, /* Address operator (&) */
NT_INDIRECT = 0x0016 | NT_BRANCH | NT_LIST_EXPR, /* Indirection operator (*) */
NT_PLUS, /* + */ NT_UNARY_MINUS = 0x0018 | NT_BRANCH | NT_LIST_EXPR,
NT_MINUS, /* - */ NT_COMPLEMENT = 0x0019 | NT_BRANCH | NT_LIST_EXPR, /* ~ */
NT_MUL, /* * */ NT_BOOL_NOT = 0x001A | NT_BRANCH | NT_LIST_EXPR, /* ! */
NT_DIV, /* / */
NT_SHL, /* << */
NT_SHR, /* >> */
NT_AND, /* & */
NT_OR, /* | */
NT_XOR, /* ^ */
NT_TERNARY, /* ?: */ NT_PLUS = 0x001B | NT_BRANCH | NT_LIST_EXPR, /* + */
NT_MINUS = 0x001C | NT_BRANCH | NT_LIST_EXPR, /* - */
NT_MUL = 0x001D | NT_BRANCH | NT_LIST_EXPR, /* * */
NT_DIV = 0x001E | NT_BRANCH | NT_LIST_EXPR, /* / */
NT_SHL = 0x001F | NT_BRANCH | NT_LIST_EXPR, /* << */
NT_SHR = 0x0020 | NT_BRANCH | NT_LIST_EXPR, /* >> */
NT_AND = 0x0021 | NT_BRANCH | NT_LIST_EXPR, /* & */
NT_OR = 0x0022 | NT_BRANCH | NT_LIST_EXPR, /* | */
NT_XOR = 0x0023 | NT_BRANCH | NT_LIST_EXPR, /* ^ */
NT_ASSIGN, /* = */ NT_TERNARY = 0x0024 | NT_BRANCH | NT_LIST_EXPR, /* ?: */
NT_PLUS_ASSIGN, /* += */
NT_MINUS_ASSIGN, /* -= */
NT_MUL_ASSIGN, /* *= */
NT_DIV_ASSIGN, /* /= */
NT_SHL_ASSIGN, /* <<= */
NT_SHR_ASSIGN, /* >>= */
NT_AND_ASSIGN, /* &= */
NT_OR_ASSIGN, /* |= */
NT_XOR_ASSIGN, /* ^= */
NT_PRE_DEC, /* -- */ NT_ASSIGN = 0x0025 | NT_BRANCH | NT_LIST_EXPR, /* = */
NT_POST_DEC, /* -- */ NT_PLUS_ASSIGN = 0x0026 | NT_BRANCH | NT_LIST_EXPR, /* += */
NT_PRE_INC, /* ++ */ NT_MINUS_ASSIGN = 0x0027 | NT_BRANCH | NT_LIST_EXPR, /* -= */
NT_POST_INC, /* ++ */ NT_MUL_ASSIGN = 0x0028 | NT_BRANCH | NT_LIST_EXPR, /* *= */
NT_DIV_ASSIGN = 0x0029 | NT_BRANCH | NT_LIST_EXPR, /* /= */
NT_SHL_ASSIGN = 0x002A | NT_BRANCH | NT_LIST_EXPR, /* <<= */
NT_SHR_ASSIGN = 0x002B | NT_BRANCH | NT_LIST_EXPR, /* >>= */
NT_AND_ASSIGN = 0x002C | NT_BRANCH | NT_LIST_EXPR, /* &= */
NT_OR_ASSIGN = 0x002D | NT_BRANCH | NT_LIST_EXPR, /* |= */
NT_XOR_ASSIGN = 0x002E | NT_BRANCH | NT_LIST_EXPR, /* ^= */
NT_BOOL_OR, /* || */ NT_PRE_DEC = 0x002F | NT_BRANCH | NT_LIST_EXPR, /* -- */
NT_BOOL_AND, /* && */ NT_POST_DEC = 0x0030 | NT_BRANCH | NT_LIST_EXPR, /* -- */
NT_PRE_INC = 0x0031 | NT_BRANCH | NT_LIST_EXPR, /* ++ */
NT_POST_INC = 0x0032 | NT_BRANCH | NT_LIST_EXPR, /* ++ */
NT_EQ, /* == */ NT_BOOL_OR = 0x0033 | NT_BRANCH | NT_LIST_EXPR, /* || */
NT_NE, /* != */ NT_BOOL_AND = 0x0034 | NT_BRANCH | NT_LIST_EXPR, /* && */
NT_LT, /* < */
NT_LE, /* <= */ NT_EQ = 0x0035 | NT_BRANCH | NT_LIST_EXPR, /* == */
NT_GT, /* > */ NT_NE = 0x0036 | NT_BRANCH | NT_LIST_EXPR, /* != */
NT_GE, /* >= */ NT_LT = 0x0037 | NT_BRANCH | NT_LIST_EXPR, /* < */
NT_LE = 0x0038 | NT_BRANCH | NT_LIST_EXPR, /* <= */
NT_GT = 0x0039 | NT_BRANCH | NT_LIST_EXPR, /* > */
NT_GE = 0x003A | NT_BRANCH | NT_LIST_EXPR /* >= */
NT_COUNT /* Operation count */
} nodetype_t; } nodetype_t;

View File

@@ -6,7 +6,7 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 1998 Ullrich von Bassewitz */ /* (C) 1998-2000 Ullrich von Bassewitz */
/* Wacholderweg 14 */ /* Wacholderweg 14 */
/* D-70597 Stuttgart */ /* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */ /* EMail: uz@musoftware.de */
@@ -58,8 +58,7 @@ unsigned char Verbose = 0; /* Verbose flag */
unsigned char AddSource = 0; /* Add source lines as comments */ unsigned char AddSource = 0; /* Add source lines as comments */
unsigned char DebugInfo = 0; /* Add debug info to the obj */ unsigned char DebugInfo = 0; /* Add debug info to the obj */
unsigned char Debug = 0; /* Debug mode */ unsigned char Debug = 0; /* Debug mode */
unsigned char CreateDep = 0; /* Create a dependency file */

View File

@@ -59,7 +59,7 @@ extern unsigned char Verbose; /* Verbose flag */
extern unsigned char AddSource; /* Add source lines as comments */ extern unsigned char AddSource; /* Add source lines as comments */
extern unsigned char DebugInfo; /* Add debug info to the obj */ extern unsigned char DebugInfo; /* Add debug info to the obj */
extern unsigned char Debug; /* Debug mode */ extern unsigned char Debug; /* Debug mode */
extern unsigned char CreateDep; /* Create a dependency file */
/* End of global.h */ /* End of global.h */

View File

@@ -39,6 +39,7 @@
/* common */ /* common */
#include "check.h" #include "check.h"
#include "coll.h"
#include "xmalloc.h" #include "xmalloc.h"
/* cc65 */ /* cc65 */
@@ -71,22 +72,24 @@ char NextC = '\0';
/* Struct that describes an input file */ /* Struct that describes an input file */
typedef struct IFile IFile; typedef struct IFile IFile;
struct IFile { struct IFile {
IFile* Next; /* Next file in single linked list */
IFile* Active; /* Next file in list of active includes */
unsigned Index; /* File index */ unsigned Index; /* File index */
unsigned Line; /* Line number for this file */ unsigned Usage; /* Usage counter */
FILE* F; /* Input file stream */
char Name[1]; /* Name of file (dynamically allocated) */ char Name[1]; /* Name of file (dynamically allocated) */
}; };
/* Main file input data */ /* Struct that describes an active input file */
static const IFile* MainFile = 0; typedef struct AFile AFile;
struct AFile {
unsigned Line; /* Line number for this file */
FILE* F; /* Input file stream */
const char* Name; /* Points to corresponding IFile name */
};
/* List of input files */ /* List of all input files */
static unsigned IFileTotal = 0; /* Total number of files */ static Collection IFiles = STATIC_COLLECTION_INITIALIZER;
static IFile* IFileList = 0; /* Single linked list of all files */
static unsigned IFileCount = 0; /* Number of active input files */ /* List of all active files */
static IFile* Input = 0; /* Single linked list of active files */ static Collection AFiles = STATIC_COLLECTION_INITIALIZER;
@@ -96,7 +99,7 @@ static IFile* Input = 0; /* Single linked list of active files */
static IFile* NewIFile (const char* Name, FILE* F) static IFile* NewIFile (const char* Name)
/* Create and return a new IFile */ /* Create and return a new IFile */
{ {
/* Get the length of the name */ /* Get the length of the name */
@@ -106,18 +109,12 @@ static IFile* NewIFile (const char* Name, FILE* F)
IFile* IF = xmalloc (sizeof (IFile) + Len); IFile* IF = xmalloc (sizeof (IFile) + Len);
/* Initialize the fields */ /* Initialize the fields */
IF->Index = ++IFileTotal; IF->Index = CollCount (&IFiles) + 1;
IF->Line = 0; IF->Usage = 0;
IF->F = F;
memcpy (IF->Name, Name, Len+1); memcpy (IF->Name, Name, Len+1);
/* Insert the structure into both lists */ /* Insert the new structure into the IFile collection */
IF->Next = IFileList; CollAppend (&IFiles, IF);
IFileList = IF;
IF->Active = Input;
Input = IF;
++IFileCount;
++IFileTotal;
/* Return the new struct */ /* Return the new struct */
return IF; return IF;
@@ -125,15 +122,78 @@ static IFile* NewIFile (const char* Name, FILE* F)
/*****************************************************************************/
/* struct AFile */
/*****************************************************************************/
static AFile* NewAFile (IFile* IF, FILE* F)
/* Create and return a new AFile */
{
/* Allocate a AFile structure */
AFile* AF = xmalloc (sizeof (AFile));
/* Initialize the fields */
AF->Line = 0;
AF->F = F;
AF->Name = IF->Name;
/* Increment the usage counter of the corresponding IFile */
++IF->Usage;
/* Insert the new structure into the AFile collection */
CollAppend (&AFiles, AF);
/* Return the new struct */
return AF;
}
static void FreeAFile (AFile* AF)
/* Free an AFile structure */
{
xfree (AF);
}
/*****************************************************************************/ /*****************************************************************************/
/* Code */ /* Code */
/*****************************************************************************/ /*****************************************************************************/
static IFile* FindFile (const char* Name)
/* Find the file with the given name in the list of all files. Since the list
* is not large (usually less than 10), I don't care about using hashes or
* similar things and do a linear search.
*/
{
unsigned I;
for (I = 0; I < CollCount (&IFiles); ++I) {
/* Get the file struct */
IFile* IF = CollAt (&IFiles, I);
/* Check the name */
if (strcmp (Name, IF->Name) == 0) {
/* Found, return the struct */
return IF;
}
}
/* Not found */
return 0;
}
void OpenMainFile (const char* Name) void OpenMainFile (const char* Name)
/* Open the main file. Will call Fatal() in case of failures. */ /* Open the main file. Will call Fatal() in case of failures. */
{ {
/* Setup a new IFile structure for the main file */
IFile* IF = NewIFile (Name);
/* Open the file for reading */ /* Open the file for reading */
FILE* F = fopen (Name, "r"); FILE* F = fopen (Name, "r");
if (F == 0) { if (F == 0) {
@@ -141,8 +201,8 @@ void OpenMainFile (const char* Name)
Fatal (FAT_CANNOT_OPEN_INPUT, strerror (errno)); Fatal (FAT_CANNOT_OPEN_INPUT, strerror (errno));
} }
/* Setup a new IFile structure */ /* Allocate a new AFile structure for the file */
MainFile = NewIFile (Name, F); (void) NewAFile (IF, F);
} }
@@ -152,9 +212,10 @@ void OpenIncludeFile (const char* Name, unsigned DirSpec)
{ {
char* N; char* N;
FILE* F; FILE* F;
IFile* IF;
/* Check for the maximum include nesting */ /* Check for the maximum include nesting */
if (IFileCount > MAX_INC_NESTING) { if (CollCount (&AFiles) > MAX_INC_NESTING) {
PPError (ERR_INCLUDE_NESTING); PPError (ERR_INCLUDE_NESTING);
return; return;
} }
@@ -166,20 +227,27 @@ void OpenIncludeFile (const char* Name, unsigned DirSpec)
return; return;
} }
/* Search the list of all input files for this file. If we don't find
* it, create a new IFile object.
*/
IF = FindFile (N);
if (IF == 0) {
IF = NewIFile (N);
}
/* We don't need N any longer, since we may now use IF->Name */
xfree (N);
/* Open the file */ /* Open the file */
F = fopen (N, "r"); F = fopen (IF->Name, "r");
if (F == 0) { if (F == 0) {
/* Error opening the file */ /* Error opening the file */
PPError (ERR_INCLUDE_OPEN_FAILURE, N, strerror (errno)); PPError (ERR_INCLUDE_OPEN_FAILURE, IF->Name, strerror (errno));
xfree (N);
return; return;
} }
/* Allocate a new IFile structure */ /* Allocate a new AFile structure */
NewIFile (N, F); (void) NewAFile (IF, F);
/* We don't need the full name any longer */
xfree (N);
} }
@@ -189,17 +257,25 @@ static void CloseIncludeFile (void)
* NULL if this was the main file. * NULL if this was the main file.
*/ */
{ {
AFile* Input;
/* Get the number of active input files */
unsigned AFileCount = CollCount (&AFiles);
/* Must have an input file when called */ /* Must have an input file when called */
PRECONDITION (Input != 0); PRECONDITION (AFileCount > 0);
/* Get the current active input file */
Input = CollLast (&AFiles);
/* Close the current input file (we're just reading so no error check) */ /* Close the current input file (we're just reading so no error check) */
fclose (Input->F); fclose (Input->F);
/* Make this file inactive and the last one active again */ /* Delete the last active file from the active file collection */
Input = Input->Active; CollDelete (&AFiles, AFileCount-1);
/* Adjust the counter */ /* Delete the active file structure */
--IFileCount; FreeAFile (Input);
} }
@@ -253,6 +329,7 @@ void NextChar (void)
int NextLine (void) int NextLine (void)
/* Get a line from the current input. Returns 0 on end of file. */ /* Get a line from the current input. Returns 0 on end of file. */
{ {
AFile* Input;
unsigned Len; unsigned Len;
unsigned Part; unsigned Part;
unsigned Start; unsigned Start;
@@ -261,10 +338,11 @@ int NextLine (void)
/* Setup the line */ /* Setup the line */
ClearLine (); ClearLine ();
/* If there is no file open, bail out */ /* If there is no file open, bail out, otherwise get the current input file */
if (Input == 0) { if (CollCount (&AFiles) == 0) {
return 0; return 0;
} }
Input = CollLast (&AFiles);
/* Read lines until we get one with real contents */ /* Read lines until we get one with real contents */
Len = 0; Len = 0;
@@ -279,10 +357,14 @@ int NextLine (void)
/* Leave the current file */ /* Leave the current file */
CloseIncludeFile (); CloseIncludeFile ();
/* If this was the last file, bail out */ /* If there is no file open, bail out, otherwise get the
if (Input == 0) { * current input file
*/
if (CollCount (&AFiles) == 0) {
return 0; return 0;
} }
Input = CollLast (&AFiles);
} }
/* We got a new line */ /* We got a new line */
@@ -326,14 +408,19 @@ int NextLine (void)
const char* GetCurrentFile (void) const char* GetCurrentFile (void)
/* Return the name of the current input file */ /* Return the name of the current input file */
{ {
if (Input == 0) { unsigned AFileCount = CollCount (&AFiles);
if (MainFile) { if (AFileCount > 0) {
return MainFile->Name; const AFile* AF = CollAt (&AFiles, AFileCount-1);
return AF->Name;
} else {
/* No open file. Use the main file if we have one. */
unsigned IFileCount = CollCount (&IFiles);
if (IFileCount > 0) {
const IFile* IF = CollAt (&IFiles, 0);
return IF->Name;
} else { } else {
return "(outside file scope)"; return "(outside file scope)";
} }
} else {
return Input->Name;
} }
} }
@@ -342,7 +429,41 @@ const char* GetCurrentFile (void)
unsigned GetCurrentLine (void) unsigned GetCurrentLine (void)
/* Return the line number in the current input file */ /* Return the line number in the current input file */
{ {
return Input? Input->Line : 0; unsigned AFileCount = CollCount (&AFiles);
if (AFileCount > 0) {
const AFile* AF = CollAt (&AFiles, AFileCount-1);
return AF->Line;
} else {
/* No open file */
return 0;
}
}
void WriteDependencies (FILE* F, const char* OutputFile)
/* Write a makefile dependency list to the given file */
{
unsigned I;
/* Get the number of input files */
unsigned IFileCount = CollCount (&IFiles);
/* Print the output file followed by a tab char */
fprintf (F, "%s:\t", OutputFile);
/* Loop over all files */
for (I = 0; I < IFileCount; ++I) {
/* Get the next input file */
const IFile* IF = CollAt (&IFiles, I);
/* If this is not the first file, add a space */
const char* Format = (I == 0)? "%s" : " %s";
/* Print the dependency */
fprintf (F, Format, IF->Name);
}
/* End the line */
fprintf (F, "\n\n");
} }

View File

@@ -91,6 +91,9 @@ const char* GetCurrentFile (void);
unsigned GetCurrentLine (void); unsigned GetCurrentLine (void);
/* Return the line number in the current input file */ /* Return the line number in the current input file */
void WriteDependencies (FILE* F, const char* OutputFile);
/* Write a makefile dependency list to the given file */
/* End of input.h */ /* End of input.h */

View File

@@ -178,6 +178,33 @@ static void SetSys (const char* Sys)
static void DoCreateDep (const char* OutputName)
/* Create the dependency file */
{
/* Make the dependency file name from the output file name */
char* DepName = MakeFilename (OutputName, ".u");
/* Open the file */
FILE* F = fopen (DepName, "w");
if (F == 0) {
Fatal (FAT_CANNOT_OPEN_OUTPUT, strerror (errno));
}
/* Write the dependencies to the file */
WriteDependencies (F, OutputName);
/* Close the file, check for errors */
if (fclose (F) != 0) {
remove (DepName);
Fatal (FAT_CANNOT_WRITE_OUTPUT);
}
/* Free the name */
xfree (DepName);
}
static void DefineSym (const char* Def) static void DefineSym (const char* Def)
/* Define a symbol on the command line */ /* Define a symbol on the command line */
{ {
@@ -273,6 +300,14 @@ static void OptCodeName (const char* Opt, const char* Arg)
static void OptCreateDep (const char* Opt, const char* Arg)
/* Handle the --create-dep option */
{
CreateDep = 1;
}
static void OptCPU (const char* Opt, const char* Arg) static void OptCPU (const char* Opt, const char* Arg)
/* Handle the --cpu option */ /* Handle the --cpu option */
{ {
@@ -394,6 +429,7 @@ int main (int argc, char* argv[])
{ "--ansi", 0, OptAnsi }, { "--ansi", 0, OptAnsi },
{ "--bss-name", 1, OptBssName }, { "--bss-name", 1, OptBssName },
{ "--code-name", 1, OptCodeName }, { "--code-name", 1, OptCodeName },
{ "--create-dep", 0, OptCreateDep },
{ "--cpu", 1, OptCPU }, { "--cpu", 1, OptCPU },
{ "--data-name", 1, OptDataName }, { "--data-name", 1, OptDataName },
{ "--debug", 0, OptDebug }, { "--debug", 0, OptDebug },
@@ -463,6 +499,10 @@ int main (int argc, char* argv[])
OptTarget (Arg, GetArg (&I, 2)); OptTarget (Arg, GetArg (&I, 2));
break; break;
case 'u':
OptCreateDep (Arg, 0);
break;
case 'v': case 'v':
OptVerbose (Arg, 0); OptVerbose (Arg, 0);
break; break;
@@ -582,6 +622,12 @@ int main (int argc, char* argv[])
remove (OutputFile); remove (OutputFile);
Fatal (FAT_CANNOT_WRITE_OUTPUT); Fatal (FAT_CANNOT_WRITE_OUTPUT);
} }
/* Create dependencies if requested */
if (CreateDep) {
DoCreateDep (OutputFile);
}
} }
/* Return an apropriate exit code */ /* Return an apropriate exit code */