1
0
mirror of https://github.com/cc65/cc65.git synced 2024-12-22 12:30:41 +00:00

Added new comment feature

git-svn-id: svn://svn.cc65.org/cc65/trunk@3388 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz 2005-02-16 23:30:05 +00:00
parent 00b644ad18
commit e814c484c3
11 changed files with 200 additions and 53 deletions

View File

@ -81,6 +81,7 @@ void AsmInc (const char* Filename, char CommentStart, int IgnoreUnknown)
{ {
char Buf[1024]; char Buf[1024];
char* L; char* L;
const char* Comment;
unsigned Line; unsigned Line;
unsigned Len; unsigned Len;
long Val; long Val;
@ -147,7 +148,7 @@ void AsmInc (const char* Filename, char CommentStart, int IgnoreUnknown)
} else { } else {
if (!IgnoreUnknown) { if (!IgnoreUnknown) {
Error ("%s(%u): Missing `='", Filename, Line); Error ("%s(%u): Missing `='", Filename, Line);
} }
continue; continue;
} }
@ -198,6 +199,16 @@ void AsmInc (const char* Filename, char CommentStart, int IgnoreUnknown)
/* Skip whitespace again */ /* Skip whitespace again */
L = SkipWhitespace (L); L = SkipWhitespace (L);
/* Check for a comment */
if (*L == CommentStart) {
Comment = SkipWhitespace (L+1);
if (*Comment == '\0') {
Comment = 0;
}
} else {
Comment = 0;
}
/* Check for a comment character or end of line */ /* Check for a comment character or end of line */
if (*L != CommentStart && *L != '\0') { if (*L != CommentStart && *L != '\0') {
if (!IgnoreUnknown) { if (!IgnoreUnknown) {
@ -210,7 +221,7 @@ void AsmInc (const char* Filename, char CommentStart, int IgnoreUnknown)
Val *= Sign; Val *= Sign;
/* Define the symbol */ /* Define the symbol */
AddExtLabelRange (Val, SB_GetConstBuf (&Ident), 1); AddExtLabel (Val, SB_GetConstBuf (&Ident), Comment);
} }

View File

@ -55,11 +55,53 @@
/* Label + comment */
typedef struct {
const char* Name;
const char* Comment;
} Label;
/* A whole lot of Labels, so we don't have to allocate them separately */
static Label* LabelHeap = 0;
static unsigned LabelsLeft = 0;
/* Attribute table */ /* Attribute table */
static unsigned char AttrTab [0x10000]; static unsigned char AttrTab [0x10000];
/* Symbol table */ /* Symbol table */
static const char* SymTab [0x10000]; static const Label* SymTab [0x10000];
/*****************************************************************************/
/* struct Label */
/*****************************************************************************/
Label* NewLabel (const char* Name, const char* Comment)
/* Create a new Label structure, initialize and return it */
{
Label* L;
/* Check if we have some free labels left */
if (LabelsLeft == 0) {
/* Allocate a new block of memory */
LabelsLeft = 200;
LabelHeap = xmalloc (LabelsLeft * sizeof (Label));
}
/* Get a new one from the buffer */
L = LabelHeap++;
--LabelsLeft;
/* Initialize the new label */
L->Name = xstrdup (Name);
L->Comment = Comment? xstrdup (Comment) : 0;
/* Return the new label */
return L;
}
@ -133,7 +175,7 @@ void MarkAddr (unsigned Addr, attr_t Attr)
const char* MakeLabelName (unsigned Addr) static const char* MakeLabelName (unsigned Addr)
/* Make the default label name from the given address and return it in a /* Make the default label name from the given address and return it in a
* static buffer. * static buffer.
*/ */
@ -145,7 +187,7 @@ const char* MakeLabelName (unsigned Addr)
void AddLabel (unsigned Addr, attr_t Attr, const char* Name) void AddLabel (unsigned Addr, attr_t Attr, const char* Name, const char* Comment)
/* Add a label */ /* Add a label */
{ {
/* Get an existing label attribute */ /* Get an existing label attribute */
@ -154,14 +196,14 @@ void AddLabel (unsigned Addr, attr_t Attr, const char* Name)
/* Must not have two symbols for one address */ /* Must not have two symbols for one address */
if (ExistingAttr != atNoLabel) { if (ExistingAttr != atNoLabel) {
/* Allow redefinition if identical */ /* Allow redefinition if identical */
if (ExistingAttr == Attr && strcmp (SymTab[Addr], Name) == 0) { if (ExistingAttr == Attr && strcmp (SymTab[Addr]->Name, Name) == 0) {
return; return;
} }
Error ("Duplicate label for address $%04X: %s/%s", Addr, SymTab[Addr], Name); Error ("Duplicate label for address $%04X: %s/%s", Addr, SymTab[Addr]->Name, Name);
} }
/* Create a new label */ /* Create a new label */
SymTab[Addr] = xstrdup (Name); SymTab[Addr] = NewLabel (Name, Comment);
/* Remember the attribute */ /* Remember the attribute */
AttrTab[Addr] |= Attr; AttrTab[Addr] |= Attr;
@ -169,6 +211,22 @@ void AddLabel (unsigned Addr, attr_t Attr, const char* Name)
void AddIntLabel (unsigned Addr)
/* Add an internal label using the address to generate the name. */
{
AddLabel (Addr, atIntLabel, MakeLabelName (Addr), 0);
}
void AddExtLabel (unsigned Addr, const char* Name, const char* Comment)
/* Add an external label */
{
AddLabel (Addr, atExtLabel, Name, Comment);
}
void AddDepLabel (unsigned Addr, attr_t Attr, const char* BaseName, unsigned Offs) void AddDepLabel (unsigned Addr, attr_t Attr, const char* BaseName, unsigned Offs)
/* Add a dependent label at the given address using "base name+Offs" as the new /* Add a dependent label at the given address using "base name+Offs" as the new
* name. * name.
@ -186,7 +244,7 @@ void AddDepLabel (unsigned Addr, attr_t Attr, const char* BaseName, unsigned Off
} }
/* Define the labels */ /* Define the labels */
AddLabel (Addr, Attr | atDepLabel, DepName); AddLabel (Addr, Attr | atDepLabel, DepName, 0);
/* Free the name buffer */ /* Free the name buffer */
xfree (DepName); xfree (DepName);
@ -194,13 +252,15 @@ void AddDepLabel (unsigned Addr, attr_t Attr, const char* BaseName, unsigned Off
static void AddLabelRange (unsigned Addr, attr_t Attr, const char* Name, unsigned Count) static void AddLabelRange (unsigned Addr, attr_t Attr,
const char* Name, const char* Comment,
unsigned Count)
/* Add a label for a range. The first entry gets the label "Name" while the /* Add a label for a range. The first entry gets the label "Name" while the
* others get "Name+offs". * others get "Name+offs".
*/ */
{ {
/* Define the label */ /* Define the label */
AddLabel (Addr, Attr, Name); AddLabel (Addr, Attr, Name, Comment);
/* Define dependent labels if necessary */ /* Define dependent labels if necessary */
if (Count > 1) { if (Count > 1) {
@ -221,7 +281,7 @@ static void AddLabelRange (unsigned Addr, attr_t Attr, const char* Name, unsigne
/* Define the labels */ /* Define the labels */
for (Offs = 1; Offs < Count; ++Offs) { for (Offs = 1; Offs < Count; ++Offs) {
sprintf (DepOffs, Format, Offs); sprintf (DepOffs, Format, Offs);
AddLabel (Addr + Offs, Attr | atDepLabel, DepName); AddLabel (Addr + Offs, Attr | atDepLabel, DepName, 0);
} }
/* Free the name buffer */ /* Free the name buffer */
@ -237,18 +297,18 @@ void AddIntLabelRange (unsigned Addr, const char* Name, unsigned Count)
*/ */
{ {
/* Define the label range */ /* Define the label range */
AddLabelRange (Addr, atIntLabel, Name, Count); AddLabelRange (Addr, atIntLabel, Name, 0, Count);
} }
void AddExtLabelRange (unsigned Addr, const char* Name, unsigned Count) void AddExtLabelRange (unsigned Addr, const char* Name, const char* Comment, unsigned Count)
/* Add an external label for a range. The first entry gets the label "Name" /* Add an external label for a range. The first entry gets the label "Name"
* while the others get "Name+offs". * while the others get "Name+offs".
*/ */
{ {
/* Define the label range */ /* Define the label range */
AddLabelRange (Addr, atExtLabel, Name, Count); AddLabelRange (Addr, atExtLabel, Name, Comment, Count);
} }
@ -286,7 +346,19 @@ const char* GetLabel (unsigned Addr)
AddrCheck (Addr); AddrCheck (Addr);
/* Return the label if any */ /* Return the label if any */
return SymTab[Addr]; return SymTab[Addr]? SymTab[Addr]->Name : 0;
}
const char* GetComment (unsigned Addr)
/* Return the comment for an address */
{
/* Check the given address */
AddrCheck (Addr);
/* Return the label if any */
return SymTab[Addr]? SymTab[Addr]->Comment : 0;
} }
@ -318,7 +390,7 @@ unsigned char GetLabelAttr (unsigned Addr)
static void DefineConst (unsigned Addr) static void DefineConst (unsigned Addr)
/* Define an address constant */ /* Define an address constant */
{ {
Output ("%s", SymTab [Addr]); Output ("%s", SymTab[Addr]->Name);
Indent (AIndent); Indent (AIndent);
Output ("= $%04X", Addr); Output ("= $%04X", Addr);
LineFeed (); LineFeed ();

View File

@ -86,14 +86,15 @@ void MarkRange (unsigned Start, unsigned End, attr_t Attr);
void MarkAddr (unsigned Addr, attr_t Attr); void MarkAddr (unsigned Addr, attr_t Attr);
/* Mark an address with an attribute */ /* Mark an address with an attribute */
const char* MakeLabelName (unsigned Addr); void AddLabel (unsigned Addr, attr_t Attr, const char* Name, const char* Comment);
/* Make the default label name from the given address and return it in a
* static buffer.
*/
void AddLabel (unsigned Addr, attr_t Attr, const char* Name);
/* Add a label */ /* Add a label */
void AddIntLabel (unsigned Addr);
/* Add an internal label using the address to generate the name. */
void AddExtLabel (unsigned Addr, const char* Name, const char* Comment);
/* Add an external label */
void AddDepLabel (unsigned Addr, attr_t Attr, const char* BaseName, unsigned Offs); void AddDepLabel (unsigned Addr, attr_t Attr, const char* BaseName, unsigned Offs);
/* Add a dependent label at the given address using "base name+Offs" as the new /* Add a dependent label at the given address using "base name+Offs" as the new
* name. * name.
@ -104,7 +105,7 @@ void AddIntLabelRange (unsigned Addr, const char* Name, unsigned Count);
* while the others get "Name+offs". * while the others get "Name+offs".
*/ */
void AddExtLabelRange (unsigned Addr, const char* Name, unsigned Count); void AddExtLabelRange (unsigned Addr, const char* Name, const char* Comment, unsigned Count);
/* Add an external label for a range. The first entry gets the label "Name" /* Add an external label for a range. The first entry gets the label "Name"
* while the others get "Name+offs". * while the others get "Name+offs".
*/ */
@ -120,6 +121,9 @@ int MustDefLabel (unsigned Addr);
const char* GetLabel (unsigned Addr); const char* GetLabel (unsigned Addr);
/* Return the label for an address or NULL if there is none */ /* Return the label for an address or NULL if there is none */
const char* GetComment (unsigned Addr);
/* Return the comment for an address */
unsigned char GetStyleAttr (unsigned Addr); unsigned char GetStyleAttr (unsigned Addr);
/* Return the style attribute for the given address */ /* Return the style attribute for the given address */

View File

@ -183,7 +183,7 @@ unsigned AddrTable (void)
/* In pass 1, define a label, in pass 2 output the line */ /* In pass 1, define a label, in pass 2 output the line */
if (Pass == 1) { if (Pass == 1) {
if (!HaveLabel (Addr)) { if (!HaveLabel (Addr)) {
AddLabel (Addr, atIntLabel, MakeLabelName (Addr)); AddIntLabel (Addr);
} }
} else { } else {
const char* Label = GetLabel (Addr); const char* Label = GetLabel (Addr);
@ -243,7 +243,7 @@ unsigned RtsTable (void)
/* In pass 1, define a label, in pass 2 output the line */ /* In pass 1, define a label, in pass 2 output the line */
if (Pass == 1) { if (Pass == 1) {
if (!HaveLabel (Addr)) { if (!HaveLabel (Addr)) {
AddLabel (Addr, atIntLabel, MakeLabelName (Addr)); AddIntLabel (Addr);
} }
} else { } else {
const char* Label = GetLabel (Addr); const char* Label = GetLabel (Addr);

View File

@ -145,7 +145,7 @@ static void GenerateLabel (unsigned Flags, unsigned Addr)
if (Granularity == 1) { if (Granularity == 1) {
/* Just add the label */ /* Just add the label */
AddLabel (Addr, atIntLabel, MakeLabelName (Addr)); AddIntLabel (Addr);
} else { } else {
/* Search for the start of the range or the last non dependent /* Search for the start of the range or the last non dependent
* label in the range. * label in the range.
@ -170,13 +170,13 @@ static void GenerateLabel (unsigned Flags, unsigned Addr)
/* If the proposed label address doesn't have a label, define one */ /* If the proposed label address doesn't have a label, define one */
if ((GetLabelAttr (LabelAddr) & (atIntLabel|atExtLabel)) == 0) { if ((GetLabelAttr (LabelAddr) & (atIntLabel|atExtLabel)) == 0) {
AddLabel (LabelAddr, atIntLabel, MakeLabelName (LabelAddr)); AddIntLabel (LabelAddr);
} }
/* Create the label */ /* Create the label */
Offs = Addr - LabelAddr; Offs = Addr - LabelAddr;
if (Offs == 0) { if (Offs == 0) {
AddLabel (Addr, atIntLabel, MakeLabelName (Addr)); AddIntLabel (Addr);
} else { } else {
AddDepLabel (Addr, atIntLabel, GetLabel (LabelAddr), Offs); AddDepLabel (Addr, atIntLabel, GetLabel (LabelAddr), Offs);
} }

View File

@ -205,6 +205,7 @@ static void RangeSection (void)
/* Parse a range section */ /* Parse a range section */
{ {
static const IdentTok RangeDefs[] = { static const IdentTok RangeDefs[] = {
{ "COMMENT", INFOTOK_COMMENT },
{ "END", INFOTOK_END }, { "END", INFOTOK_END },
{ "NAME", INFOTOK_NAME }, { "NAME", INFOTOK_NAME },
{ "START", INFOTOK_START }, { "START", INFOTOK_START },
@ -212,11 +213,11 @@ static void RangeSection (void)
}; };
static const IdentTok TypeDefs[] = { static const IdentTok TypeDefs[] = {
{ "ADDRTABLE", INFOTOK_ADDRTAB }, { "ADDRTABLE", INFOTOK_ADDRTAB },
{ "BYTETABLE", INFOTOK_BYTETAB }, { "BYTETABLE", INFOTOK_BYTETAB },
{ "CODE", INFOTOK_CODE }, { "CODE", INFOTOK_CODE },
{ "DBYTETABLE", INFOTOK_DBYTETAB }, { "DBYTETABLE", INFOTOK_DBYTETAB },
{ "DWORDTABLE", INFOTOK_DWORDTAB }, { "DWORDTABLE", INFOTOK_DWORDTAB },
{ "RTSTABLE", INFOTOK_RTSTAB }, { "RTSTABLE", INFOTOK_RTSTAB },
{ "SKIP", INFOTOK_SKIP }, { "SKIP", INFOTOK_SKIP },
{ "TEXTTABLE", INFOTOK_TEXTTAB }, { "TEXTTABLE", INFOTOK_TEXTTAB },
@ -231,6 +232,7 @@ static void RangeSection (void)
tEnd = 0x02, tEnd = 0x02,
tType = 0x04, tType = 0x04,
tName = 0x08, tName = 0x08,
tComment= 0x10,
tNeeded = (tStart | tEnd | tType) tNeeded = (tStart | tEnd | tType)
}; };
unsigned Attributes = tNone; unsigned Attributes = tNone;
@ -240,6 +242,7 @@ static void RangeSection (void)
unsigned End = 0; unsigned End = 0;
unsigned char Type = 0; unsigned char Type = 0;
char* Name = 0; char* Name = 0;
char* Comment = 0;
unsigned MemberSize = 0; unsigned MemberSize = 0;
@ -258,6 +261,18 @@ static void RangeSection (void)
/* Look at the token */ /* Look at the token */
switch (InfoTok) { switch (InfoTok) {
case INFOTOK_COMMENT:
AddAttr ("COMMENT", &Attributes, tComment);
InfoNextTok ();
InfoAssureStr ();
if (InfoSVal[0] == '\0') {
InfoError ("Comment may not be empty");
}
Comment = xstrdup (InfoSVal);
Attributes |= tComment;
InfoNextTok ();
break;
case INFOTOK_END: case INFOTOK_END:
AddAttr ("END", &Attributes, tEnd); AddAttr ("END", &Attributes, tEnd);
InfoNextTok (); InfoNextTok ();
@ -333,9 +348,10 @@ static void RangeSection (void)
/* Do we have a label? */ /* Do we have a label? */
if (Attributes & tName) { if (Attributes & tName) {
/* Define a label for the table */ /* Define a label for the table */
AddLabel (Start, atExtLabel, Name); AddExtLabel (Start, Name, Comment);
/* Delete the name */ /* Delete name and comment */
xfree (Name); xfree (Name);
xfree (Comment);
} }
/* Consume the closing brace */ /* Consume the closing brace */
@ -348,15 +364,17 @@ static void LabelSection (void)
/* Parse a label section */ /* Parse a label section */
{ {
static const IdentTok LabelDefs[] = { static const IdentTok LabelDefs[] = {
{ "NAME", INFOTOK_NAME }, { "COMMENT", INFOTOK_COMMENT },
{ "ADDR", INFOTOK_ADDR }, { "ADDR", INFOTOK_ADDR },
{ "NAME", INFOTOK_NAME },
{ "SIZE", INFOTOK_SIZE }, { "SIZE", INFOTOK_SIZE },
}; };
/* Locals - initialize to avoid gcc warnings */ /* Locals - initialize to avoid gcc warnings */
char* Name = 0; char* Name = 0;
long Value = -1; char* Comment = 0;
long Size = -1; long Value = -1;
long Size = -1;
/* Skip the token */ /* Skip the token */
InfoNextTok (); InfoNextTok ();
@ -373,6 +391,30 @@ static void LabelSection (void)
/* Look at the token */ /* Look at the token */
switch (InfoTok) { switch (InfoTok) {
case INFOTOK_ADDR:
InfoNextTok ();
if (Value >= 0) {
InfoError ("Value already given");
}
InfoAssureInt ();
InfoRangeCheck (0, 0xFFFF);
Value = InfoIVal;
InfoNextTok ();
break;
case INFOTOK_COMMENT:
InfoNextTok ();
if (Comment) {
InfoError ("Comment already given");
}
InfoAssureStr ();
if (InfoSVal[0] == '\0') {
InfoError ("Comment may not be empty");
}
Comment = xstrdup (InfoSVal);
InfoNextTok ();
break;
case INFOTOK_NAME: case INFOTOK_NAME:
InfoNextTok (); InfoNextTok ();
if (Name) { if (Name) {
@ -386,17 +428,6 @@ static void LabelSection (void)
InfoNextTok (); InfoNextTok ();
break; break;
case INFOTOK_ADDR:
InfoNextTok ();
if (Value >= 0) {
InfoError ("Value already given");
}
InfoAssureInt ();
InfoRangeCheck (0, 0xFFFF);
Value = InfoIVal;
InfoNextTok ();
break;
case INFOTOK_SIZE: case INFOTOK_SIZE:
InfoNextTok (); InfoNextTok ();
if (Size >= 0) { if (Size >= 0) {
@ -433,10 +464,11 @@ static void LabelSection (void)
} }
/* Define the label(s) */ /* Define the label(s) */
AddExtLabelRange ((unsigned) Value, Name, Size); AddExtLabelRange ((unsigned) Value, Name, Comment, Size);
/* Delete the dynamically allocated memory for Name */ /* Delete the dynamically allocated memory for Name and Comment */
xfree (Name); xfree (Name);
xfree (Comment);
/* Consume the closing brace */ /* Consume the closing brace */
InfoConsumeRCurly (); InfoConsumeRCurly ();

View File

@ -251,10 +251,14 @@ static void OneOpcode (unsigned RemainingBytes)
/* Get the output style for the current PC */ /* Get the output style for the current PC */
attr_t Style = GetStyleAttr (PC); attr_t Style = GetStyleAttr (PC);
/* If we have a label at this address, output the label, provided that /* If we have a label at this address, output the label and an attached
* we aren't in a skip area. * comment, provided that we aren't in a skip area.
*/ */
if (Style != atSkip && MustDefLabel (PC)) { if (Style != atSkip && MustDefLabel (PC)) {
const char* Comment = GetComment (PC);
if (Comment) {
UserComment (Comment);
}
DefLabel (GetLabel (PC)); DefLabel (GetLabel (PC));
} }

View File

@ -267,6 +267,15 @@ void SeparatorLine (void)
void UserComment (const char* Comment)
/* Output a comment line */
{
Output ("; %s", Comment);
LineFeed ();
}
void LineComment (unsigned PC, unsigned Count) void LineComment (unsigned PC, unsigned Count)
/* Add a line comment with the PC and data bytes */ /* Add a line comment with the PC and data bytes */
{ {

View File

@ -85,6 +85,9 @@ void DataDWordLine (unsigned ByteCount);
void SeparatorLine (void); void SeparatorLine (void);
/* Print a separator line */ /* Print a separator line */
void UserComment (const char* Comment);
/* Output a comment line */
void LineComment (unsigned PC, unsigned Count); void LineComment (unsigned PC, unsigned Count);
/* Add a line comment with the PC and data bytes */ /* Add a line comment with the PC and data bytes */

View File

@ -153,7 +153,7 @@ void InfoNextTok (void)
/* Read the next token from the input stream */ /* Read the next token from the input stream */
{ {
unsigned I; unsigned I;
int Esc;
Again: Again:
/* Skip whitespace */ /* Skip whitespace */
@ -249,9 +249,20 @@ Again:
NextChar (); NextChar ();
I = 0; I = 0;
while (C != '\"') { while (C != '\"') {
Esc = (C == '\\');
if (Esc) {
NextChar ();
}
if (C == EOF || C == '\n') { if (C == EOF || C == '\n') {
InfoError ("Unterminated string"); InfoError ("Unterminated string");
} }
if (Esc) {
switch (C) {
case '\"': C = '\"'; break;
case '\'': C = '\''; break;
default: InfoError ("Invalid escape char: %c", C);
}
}
if (I < CFG_MAX_IDENT_LEN) { if (I < CFG_MAX_IDENT_LEN) {
InfoSVal [I++] = C; InfoSVal [I++] = C;
} }

View File

@ -94,6 +94,7 @@ typedef enum token_t {
/* Label section */ /* Label section */
INFOTOK_NAME, INFOTOK_NAME,
INFOTOK_COMMENT,
INFOTOK_ADDR, INFOTOK_ADDR,
INFOTOK_SIZE, INFOTOK_SIZE,