From 0e392b79bd5a7b04131b3d869de8c1adca6fd322 Mon Sep 17 00:00:00 2001 From: cuz Date: Fri, 14 Nov 2003 09:03:32 +0000 Subject: [PATCH] Added enums git-svn-id: svn://svn.cc65.org/cc65/trunk@2665 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/ca65/enum.c | 135 +++++++++++++++++++++++++++++++++++++++ src/ca65/enum.h | 57 +++++++++++++++++ src/ca65/expr.c | 48 +++++++------- src/ca65/expr.h | 7 +- src/ca65/make/gcc.mak | 1 + src/ca65/make/watcom.mak | 1 + src/ca65/pseudo.c | 3 + src/ca65/scanner.c | 2 + src/ca65/scanner.h | 4 +- src/ca65/struct.c | 2 +- src/ca65/struct.h | 2 +- src/ca65/symtab.h | 4 +- 12 files changed, 235 insertions(+), 31 deletions(-) create mode 100644 src/ca65/enum.c create mode 100644 src/ca65/enum.h diff --git a/src/ca65/enum.c b/src/ca65/enum.c new file mode 100644 index 000000000..8d9da86f6 --- /dev/null +++ b/src/ca65/enum.c @@ -0,0 +1,135 @@ +/*****************************************************************************/ +/* */ +/* enum.c */ +/* */ +/* .ENUM command */ +/* */ +/* */ +/* */ +/* (C) 2003 Ullrich von Bassewitz */ +/* Römerstraße 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ + + + +/* common */ +#include "addrsize.h" + +/* ca65 */ +#include "enum.h" +#include "error.h" +#include "expr.h" +#include "nexttok.h" +#include "scanner.h" +#include "symbol.h" +#include "symtab.h" + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +void DoEnum (void) +/* Handle the .ENUM command */ +{ + /* Start at zero */ + ExprNode* NextExpr = GenLiteralExpr (0); + + /* Check for a name */ + int Anon = (Tok != TOK_IDENT); + if (!Anon) { + /* Enter a new scope, then skip the name */ + SymEnterLevel (SVal, ST_ENUM, ADDR_SIZE_ABS); + NextTok (); + } + + /* Test for end of line */ + ConsumeSep (); + + /* Read until end of struct */ + while (Tok != TOK_ENDENUM && Tok != TOK_EOF) { + + SymEntry* Sym; + ExprNode* EnumExpr; + + + /* The format is "identifier [ = value ]" */ + if (Tok != TOK_IDENT) { + ErrorSkip ("Identifier expected"); + continue; + } + + /* We have an identifier, generate a symbol */ + Sym = SymFind (CurrentScope, SVal, SYM_ALLOC_NEW); + + /* Skip the member name */ + NextTok (); + + /* Check for an assignment */ + if (Tok == TOK_EQ) { + + /* Skip the equal sign */ + NextTok (); + + /* Delete the old next expression */ + FreeExpr (NextExpr); + + /* Read the new one */ + EnumExpr = Expression (); + + } else { + + EnumExpr = NextExpr; + + } + + /* Generate the next expression from the current one */ + NextExpr = GenAddExpr (CloneExpr (EnumExpr), GenLiteralExpr (1)); + NextExpr = SimplifyExpr (NextExpr); + + /* Assign the value to the enum member */ + SymDef (Sym, EnumExpr, ADDR_SIZE_DEFAULT, SF_NONE); + + /* Expect end of line */ + ConsumeSep (); + } + + /* If this is not an anon enum, leave its scope */ + if (!Anon) { + /* Close the enum scope */ + SymLeaveLevel (); + } + + /* End of enum definition */ + Consume (TOK_ENDENUM, "`.ENDENUM' expected"); + + /* Free the last (unused) enum expression */ + FreeExpr (NextExpr); +} + + + diff --git a/src/ca65/enum.h b/src/ca65/enum.h new file mode 100644 index 000000000..eecadf0cd --- /dev/null +++ b/src/ca65/enum.h @@ -0,0 +1,57 @@ +/*****************************************************************************/ +/* */ +/* enum.h */ +/* */ +/* .ENUM command */ +/* */ +/* */ +/* */ +/* (C) 2003 Ullrich von Bassewitz */ +/* Römerstraße 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ + + + +#ifndef ENUM_H +#define ENUM_H + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +void DoEnum (void); +/* Handle the .ENUM command */ + + + +/* End of enum.h */ + +#endif + + + diff --git a/src/ca65/expr.c b/src/ca65/expr.c index b0eb0503c..b7ccc9798 100644 --- a/src/ca65/expr.c +++ b/src/ca65/expr.c @@ -1523,30 +1523,6 @@ static void StudyExpr (ExprNode* Expr, ExprDesc* D, int Sign) -static ExprNode* SimplifyExpr (ExprNode* Expr) -/* Try to simplify the given expression tree */ -{ - if (Expr && Expr->Op != EXPR_LITERAL) { - - /* Create an expression description and initialize it */ - ExprDesc D; - InitExprDesc (&D); - - /* Study the expression */ - StudyExpr (Expr, &D, 1); - - /* Now check if we can generate a literal value */ - if (ExprDescIsConst (&D)) { - /* No external references */ - FreeExpr (Expr); - Expr = GenLiteralExpr (D.Val); - } - } - return Expr; -} - - - ExprNode* Expression (void) /* Evaluate an expression, build the expression tree on the heap and return * a pointer to the root of the tree. @@ -1610,6 +1586,30 @@ void FreeExpr (ExprNode* Root) +ExprNode* SimplifyExpr (ExprNode* Expr) +/* Try to simplify the given expression tree */ +{ + if (Expr && Expr->Op != EXPR_LITERAL) { + + /* Create an expression description and initialize it */ + ExprDesc D; + InitExprDesc (&D); + + /* Study the expression */ + StudyExpr (Expr, &D, 1); + + /* Now check if we can generate a literal value */ + if (ExprDescIsConst (&D)) { + /* No external references */ + FreeExpr (Expr); + Expr = GenLiteralExpr (D.Val); + } + } + return Expr; +} + + + ExprNode* GenLiteralExpr (long Val) /* Return an expression tree that encodes the given literal value */ { diff --git a/src/ca65/expr.h b/src/ca65/expr.h index 91ccbadb5..d7b046f62 100644 --- a/src/ca65/expr.h +++ b/src/ca65/expr.h @@ -44,7 +44,7 @@ /*****************************************************************************/ -/* Code */ +/* Code */ /*****************************************************************************/ @@ -63,7 +63,10 @@ long ConstExpression (void); void FreeExpr (ExprNode* Root); /* Free the expression tree, Root is pointing to. */ -ExprNode* GenLiteralExpr (long Val); +ExprNode* SimplifyExpr (ExprNode* Expr); +/* Try to simplify the given expression tree */ + +ExprNode* GenLiteralExpr (long Val); /* Return an expression tree that encodes the given literal value */ ExprNode* GenSymExpr (struct SymEntry* Sym); diff --git a/src/ca65/make/gcc.mak b/src/ca65/make/gcc.mak index 8fbfdbbba..baaf3257c 100644 --- a/src/ca65/make/gcc.mak +++ b/src/ca65/make/gcc.mak @@ -15,6 +15,7 @@ OBJS = anonname.o \ condasm.o \ dbginfo.o \ ea.o \ + enum.o \ error.o \ expr.o \ feature.o \ diff --git a/src/ca65/make/watcom.mak b/src/ca65/make/watcom.mak index bcc4ab774..dd04e44df 100644 --- a/src/ca65/make/watcom.mak +++ b/src/ca65/make/watcom.mak @@ -64,6 +64,7 @@ OBJS = anonname.obj \ condasm.obj \ dbginfo.obj \ ea.obj \ + enum.obj \ error.obj \ expr.obj \ feature.obj \ diff --git a/src/ca65/pseudo.c b/src/ca65/pseudo.c index c3752168a..419f226b8 100644 --- a/src/ca65/pseudo.c +++ b/src/ca65/pseudo.c @@ -53,6 +53,7 @@ #include "asserts.h" #include "condasm.h" #include "dbginfo.h" +#include "enum.h" #include "error.h" #include "expr.h" #include "feature.h" @@ -1639,6 +1640,7 @@ static CtrlDesc CtrlCmdTab [] = { { ccKeepToken, DoConditionals }, /* .ELSE */ { ccKeepToken, DoConditionals }, /* .ELSEIF */ { ccKeepToken, DoEnd }, + { ccNone, DoUnexpected }, /* .ENDENUM */ { ccKeepToken, DoConditionals }, /* .ENDIF */ { ccNone, DoUnexpected }, /* .ENDMACRO */ { ccNone, DoEndProc }, @@ -1646,6 +1648,7 @@ static CtrlDesc CtrlCmdTab [] = { { ccNone, DoEndScope }, { ccNone, DoUnexpected }, /* .ENDSTRUCT */ { ccNone, DoUnexpected }, /* .ENDUNION */ + { ccNone, DoEnum }, { ccNone, DoError }, { ccNone, DoExitMacro }, { ccNone, DoExport }, diff --git a/src/ca65/scanner.c b/src/ca65/scanner.c index e75b73e02..469d997a3 100644 --- a/src/ca65/scanner.c +++ b/src/ca65/scanner.c @@ -154,6 +154,7 @@ struct DotKeyword { { ".ELSE", TOK_ELSE }, { ".ELSEIF", TOK_ELSEIF }, { ".END", TOK_END }, + { ".ENDENUM", TOK_ENDENUM }, { ".ENDIF", TOK_ENDIF }, { ".ENDMAC", TOK_ENDMACRO }, { ".ENDMACRO", TOK_ENDMACRO }, @@ -163,6 +164,7 @@ struct DotKeyword { { ".ENDSCOPE", TOK_ENDSCOPE }, { ".ENDSTRUCT", TOK_ENDSTRUCT }, { ".ENDUNION", TOK_ENDUNION }, + { ".ENUM", TOK_ENUM }, { ".ERROR", TOK_ERROR }, { ".EXITMAC", TOK_EXITMACRO }, { ".EXITMACRO", TOK_EXITMACRO }, diff --git a/src/ca65/scanner.h b/src/ca65/scanner.h index 373fdb57b..005441721 100644 --- a/src/ca65/scanner.h +++ b/src/ca65/scanner.h @@ -144,7 +144,8 @@ enum Token { TOK_DWORD, TOK_ELSE, TOK_ELSEIF, - TOK_END, + TOK_END, + TOK_ENDENUM, TOK_ENDIF, TOK_ENDMACRO, TOK_ENDPROC, @@ -152,6 +153,7 @@ enum Token { TOK_ENDSCOPE, TOK_ENDSTRUCT, TOK_ENDUNION, + TOK_ENUM, TOK_ERROR, TOK_EXITMACRO, TOK_EXPORT, diff --git a/src/ca65/struct.c b/src/ca65/struct.c index 69b8864e3..a737e3614 100644 --- a/src/ca65/struct.c +++ b/src/ca65/struct.c @@ -2,7 +2,7 @@ /* */ /* struct.c */ /* */ -/* .STRUCT command */ +/* .STRUCT/.UNION commands */ /* */ /* */ /* */ diff --git a/src/ca65/struct.h b/src/ca65/struct.h index f90edee81..871fa6a42 100644 --- a/src/ca65/struct.h +++ b/src/ca65/struct.h @@ -2,7 +2,7 @@ /* */ /* struct.h */ /* */ -/* .STRUCT command */ +/* .STRUCT/.UNION commands */ /* */ /* */ /* */ diff --git a/src/ca65/symtab.h b/src/ca65/symtab.h index 587384267..20aa2a354 100644 --- a/src/ca65/symtab.h +++ b/src/ca65/symtab.h @@ -63,8 +63,8 @@ #define ST_GLOBAL 0x00 /* Root level */ #define ST_PROC 0x01 /* .PROC */ #define ST_SCOPE 0x02 /* .SCOPE */ -#define ST_STRUCT 0x03 /* .STRUCT */ -#define ST_UNION 0x04 /* .UNION */ +#define ST_STRUCT 0x03 /* .STRUCT/.UNION */ +#define ST_ENUM 0x04 /* .ENUM */ #define ST_UNDEF 0xFF /* A symbol table */ -- 2.39.5