1
0
mirror of https://github.com/cc65/cc65.git synced 2024-06-09 06:29:38 +00:00

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

@ -58,7 +58,7 @@ struct SymEntry;
/*****************************************************************************/
/* Data */
/* Data */
/*****************************************************************************/
@ -69,73 +69,81 @@ typedef enum {
/* Bits encoding the type of the objects stored in List for this
* particular node.
*/
NT_LIST_NODE = 0x0000, /* Items are expression nodes */
NT_LIST_SYM = 0x0100, /* Items are symbol table entries */
NT_LIST_STRING = 0x0200, /* List item are character strings */
NT_LIST_NONE = 0x0000, /* No items */
NT_LIST_EXPR = 0x0100, /* Items are expression nodes */
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 */
NT_CONST, /* A constant of some sort */
NT_ASM, /* Inline assembler */
/* Special node type */
NT_NONE = 0x0000, /* None (invalid) op */
NT_REG_A, /* A register */
NT_REG_X, /* X register */
NT_REG_Y, /* Y register */
NT_REG_AX, /* AX register */
NT_REG_EAX, /* EAX register */
/* Leaves */
NT_SYM = 0x0001 | NT_LEAF | NT_LIST_SYM, /* Symbol */
NT_CONST = 0x0002 | NT_LEAF | NT_LIST_NONE, /* A constant of some sort */
NT_ASM = 0x0003 | NT_LEAF | NT_LIST_STRING, /* Inline assembler */
NT_ARRAY_SUBSCRIPT, /* Array subscript */
NT_STRUCT_ACCESS, /* Access of a struct field */
NT_STRUCTPTR_ACCESS, /* Access via struct ptr */
NT_FUNCTION_CALL, /* Call a function */
NT_TYPECAST, /* A cast */
NT_ADDRESS, /* Address operator (&) */
NT_INDIRECT, /* Indirection operator (*) */
NT_REG_A = 0x0005 | NT_LEAF | NT_LIST_NONE, /* A register */
NT_REG_X = 0x0006 | NT_LEAF | NT_LIST_NONE, /* X register */
NT_REG_Y = 0x0007 | NT_LEAF | NT_LIST_NONE, /* Y register */
NT_REG_AX = 0x0008 | NT_LEAF | NT_LIST_NONE, /* AX register */
NT_REG_EAX = 0x0009 | NT_LEAF | NT_LIST_NONE, /* EAX register */
NT_UNARY_MINUS,
NT_COMPLEMENT, /* ~ */
NT_BOOL_NOT, /* ! */
/* Branches */
NT_ARRAY_SUBSCRIPT = 0x0010 | NT_BRANCH | NT_LIST_EXPR, /* Array subscript */
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_MINUS, /* - */
NT_MUL, /* * */
NT_DIV, /* / */
NT_SHL, /* << */
NT_SHR, /* >> */
NT_AND, /* & */
NT_OR, /* | */
NT_XOR, /* ^ */
NT_UNARY_MINUS = 0x0018 | NT_BRANCH | NT_LIST_EXPR,
NT_COMPLEMENT = 0x0019 | NT_BRANCH | NT_LIST_EXPR, /* ~ */
NT_BOOL_NOT = 0x001A | NT_BRANCH | NT_LIST_EXPR, /* ! */
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_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_TERNARY = 0x0024 | NT_BRANCH | NT_LIST_EXPR, /* ?: */
NT_PRE_DEC, /* -- */
NT_POST_DEC, /* -- */
NT_PRE_INC, /* ++ */
NT_POST_INC, /* ++ */
NT_ASSIGN = 0x0025 | NT_BRANCH | NT_LIST_EXPR, /* = */
NT_PLUS_ASSIGN = 0x0026 | NT_BRANCH | NT_LIST_EXPR, /* += */
NT_MINUS_ASSIGN = 0x0027 | NT_BRANCH | NT_LIST_EXPR, /* -= */
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_BOOL_AND, /* && */
NT_PRE_DEC = 0x002F | NT_BRANCH | NT_LIST_EXPR, /* -- */
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_NE, /* != */
NT_LT, /* < */
NT_LE, /* <= */
NT_GT, /* > */
NT_GE, /* >= */
NT_BOOL_OR = 0x0033 | NT_BRANCH | NT_LIST_EXPR, /* || */
NT_BOOL_AND = 0x0034 | NT_BRANCH | NT_LIST_EXPR, /* && */
NT_EQ = 0x0035 | NT_BRANCH | NT_LIST_EXPR, /* == */
NT_NE = 0x0036 | NT_BRANCH | NT_LIST_EXPR, /* != */
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;

View File

@ -6,10 +6,10 @@
/* */
/* */
/* */
/* (C) 1998 Ullrich von Bassewitz */
/* Wacholderweg 14 */
/* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */
/* (C) 1998-2000 Ullrich von Bassewitz */
/* Wacholderweg 14 */
/* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
@ -43,23 +43,22 @@
unsigned char ANSI = 0; /* Strict ANSI flag */
unsigned char WriteableStrings = 0; /* Literal strings are r/w */
unsigned char NoWarn = 0; /* Suppress warnings */
unsigned char Optimize = 0; /* Optimize flag */
unsigned char FavourSize = 1; /* Favour size over speed */
unsigned char InlineStdFuncs = 0; /* Inline some known functions */
unsigned char EnableRegVars = 0; /* Enable register variables */
unsigned char AllowRegVarAddr = 0; /* Allow taking addresses of register vars */
unsigned char RegVarsToCallStack= 0; /* Save reg variables on call stack */
unsigned char StaticLocals = 0; /* Make local variables static */
unsigned char SignedChars = 0; /* Make characters signed by default */
unsigned char Verbose = 0; /* Verbose flag */
unsigned char AddSource = 0; /* Add source lines as comments */
unsigned char DebugInfo = 0; /* Add debug info to the obj */
unsigned char Debug = 0; /* Debug mode */
unsigned char ANSI = 0; /* Strict ANSI flag */
unsigned char WriteableStrings = 0; /* Literal strings are r/w */
unsigned char NoWarn = 0; /* Suppress warnings */
unsigned char Optimize = 0; /* Optimize flag */
unsigned char FavourSize = 1; /* Favour size over speed */
unsigned char InlineStdFuncs = 0; /* Inline some known functions */
unsigned char EnableRegVars = 0; /* Enable register variables */
unsigned char AllowRegVarAddr = 0; /* Allow taking addresses of register vars */
unsigned char RegVarsToCallStack= 0; /* Save reg variables on call stack */
unsigned char StaticLocals = 0; /* Make local variables static */
unsigned char SignedChars = 0; /* Make characters signed by default */
unsigned char Verbose = 0; /* Verbose flag */
unsigned char AddSource = 0; /* Add source lines as comments */
unsigned char DebugInfo = 0; /* Add debug info to the obj */
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 DebugInfo; /* Add debug info to the obj */
extern unsigned char Debug; /* Debug mode */
extern unsigned char CreateDep; /* Create a dependency file */
/* End of global.h */

View File

@ -39,6 +39,7 @@
/* common */
#include "check.h"
#include "coll.h"
#include "xmalloc.h"
/* cc65 */
@ -71,22 +72,24 @@ char NextC = '\0';
/* Struct that describes an input file */
typedef struct IFile 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 Line; /* Line number for this file */
FILE* F; /* Input file stream */
unsigned Usage; /* Usage counter */
char Name[1]; /* Name of file (dynamically allocated) */
};
/* Main file input data */
static const IFile* MainFile = 0;
/* Struct that describes an active input file */
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 */
static unsigned IFileTotal = 0; /* Total number of files */
static IFile* IFileList = 0; /* Single linked list of all files */
static unsigned IFileCount = 0; /* Number of active input files */
static IFile* Input = 0; /* Single linked list of active files */
/* List of all input files */
static Collection IFiles = STATIC_COLLECTION_INITIALIZER;
/* List of all 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 */
{
/* Get the length of the name */
@ -106,18 +109,12 @@ static IFile* NewIFile (const char* Name, FILE* F)
IFile* IF = xmalloc (sizeof (IFile) + Len);
/* Initialize the fields */
IF->Index = ++IFileTotal;
IF->Line = 0;
IF->F = F;
IF->Index = CollCount (&IFiles) + 1;
IF->Usage = 0;
memcpy (IF->Name, Name, Len+1);
/* Insert the structure into both lists */
IF->Next = IFileList;
IFileList = IF;
IF->Active = Input;
Input = IF;
++IFileCount;
++IFileTotal;
/* Insert the new structure into the IFile collection */
CollAppend (&IFiles, IF);
/* Return the new struct */
return IF;
@ -125,24 +122,87 @@ 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 */
/*****************************************************************************/
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)
/* 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 */
FILE* F = fopen (Name, "r");
if (F == 0) {
/* Cannot open */
Fatal (FAT_CANNOT_OPEN_INPUT, strerror (errno));
/* Cannot open */
Fatal (FAT_CANNOT_OPEN_INPUT, strerror (errno));
}
/* Setup a new IFile structure */
MainFile = NewIFile (Name, F);
/* Allocate a new AFile structure for the file */
(void) NewAFile (IF, F);
}
@ -150,11 +210,12 @@ void OpenMainFile (const char* Name)
void OpenIncludeFile (const char* Name, unsigned DirSpec)
/* Open an include file and insert it into the tables. */
{
char* N;
FILE* F;
char* N;
FILE* F;
IFile* IF;
/* Check for the maximum include nesting */
if (IFileCount > MAX_INC_NESTING) {
if (CollCount (&AFiles) > MAX_INC_NESTING) {
PPError (ERR_INCLUDE_NESTING);
return;
}
@ -166,20 +227,27 @@ void OpenIncludeFile (const char* Name, unsigned DirSpec)
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 */
F = fopen (N, "r");
F = fopen (IF->Name, "r");
if (F == 0) {
/* Error opening the file */
PPError (ERR_INCLUDE_OPEN_FAILURE, N, strerror (errno));
xfree (N);
PPError (ERR_INCLUDE_OPEN_FAILURE, IF->Name, strerror (errno));
return;
}
/* Allocate a new IFile structure */
NewIFile (N, F);
/* We don't need the full name any longer */
xfree (N);
/* Allocate a new AFile structure */
(void) NewAFile (IF, F);
}
@ -189,17 +257,25 @@ static void CloseIncludeFile (void)
* 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 */
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) */
fclose (Input->F);
/* Make this file inactive and the last one active again */
Input = Input->Active;
/* Delete the last active file from the active file collection */
CollDelete (&AFiles, AFileCount-1);
/* Adjust the counter */
--IFileCount;
/* Delete the active file structure */
FreeAFile (Input);
}
@ -253,6 +329,7 @@ void NextChar (void)
int NextLine (void)
/* Get a line from the current input. Returns 0 on end of file. */
{
AFile* Input;
unsigned Len;
unsigned Part;
unsigned Start;
@ -261,10 +338,11 @@ int NextLine (void)
/* Setup the line */
ClearLine ();
/* If there is no file open, bail out */
if (Input == 0) {
/* If there is no file open, bail out, otherwise get the current input file */
if (CollCount (&AFiles) == 0) {
return 0;
}
Input = CollLast (&AFiles);
/* Read lines until we get one with real contents */
Len = 0;
@ -279,10 +357,14 @@ int NextLine (void)
/* Leave the current file */
CloseIncludeFile ();
/* If this was the last file, bail out */
if (Input == 0) {
return 0;
/* If there is no file open, bail out, otherwise get the
* current input file
*/
if (CollCount (&AFiles) == 0) {
return 0;
}
Input = CollLast (&AFiles);
}
/* We got a new line */
@ -326,14 +408,19 @@ int NextLine (void)
const char* GetCurrentFile (void)
/* Return the name of the current input file */
{
if (Input == 0) {
if (MainFile) {
return MainFile->Name;
unsigned AFileCount = CollCount (&AFiles);
if (AFileCount > 0) {
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 {
return "(outside file scope)";
}
} else {
return Input->Name;
}
}
@ -342,7 +429,41 @@ const char* GetCurrentFile (void)
unsigned GetCurrentLine (void)
/* 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);
/* 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 */

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)
/* 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)
/* Handle the --cpu option */
{
@ -393,18 +428,19 @@ int main (int argc, char* argv[])
{ "--add-source", 0, OptAddSource },
{ "--ansi", 0, OptAnsi },
{ "--bss-name", 1, OptBssName },
{ "--code-name", 1, OptCodeName },
{ "--cpu", 1, OptCPU },
{ "--data-name", 1, OptDataName },
{ "--code-name", 1, OptCodeName },
{ "--create-dep", 0, OptCreateDep },
{ "--cpu", 1, OptCPU },
{ "--data-name", 1, OptDataName },
{ "--debug", 0, OptDebug },
{ "--debug-info", 0, OptDebugInfo },
{ "--help", 0, OptHelp },
{ "--debug-info", 0, OptDebugInfo },
{ "--help", 0, OptHelp },
{ "--include-dir", 1, OptIncludeDir },
{ "--rodata-name", 1, OptRodataName },
{ "--signed-chars", 0, OptSignedChars },
{ "--static-locals", 0, OptStaticLocals },
{ "--rodata-name", 1, OptRodataName },
{ "--signed-chars", 0, OptSignedChars },
{ "--static-locals", 0, OptStaticLocals },
{ "--target", 1, OptTarget },
{ "--verbose", 0, OptVerbose },
{ "--verbose", 0, OptVerbose },
{ "--version", 0, OptVersion },
};
@ -463,6 +499,10 @@ int main (int argc, char* argv[])
OptTarget (Arg, GetArg (&I, 2));
break;
case 'u':
OptCreateDep (Arg, 0);
break;
case 'v':
OptVerbose (Arg, 0);
break;
@ -478,9 +518,9 @@ int main (int argc, char* argv[])
case 'l':
OptStaticLocals (Arg, 0);
break;
default:
UnknownOption (Arg);
break;
default:
UnknownOption (Arg);
break;
}
}
break;
@ -495,7 +535,7 @@ int main (int argc, char* argv[])
case 'O':
Optimize = 1;
P = Arg + 2;
P = Arg + 2;
while (*P) {
switch (*P++) {
case 'f':
@ -506,7 +546,7 @@ int main (int argc, char* argv[])
break;
case 'r':
EnableRegVars = 1;
break;
break;
case 's':
InlineStdFuncs = 1;
break;
@ -582,6 +622,12 @@ int main (int argc, char* argv[])
remove (OutputFile);
Fatal (FAT_CANNOT_WRITE_OUTPUT);
}
/* Create dependencies if requested */
if (CreateDep) {
DoCreateDep (OutputFile);
}
}
/* Return an apropriate exit code */