[scc-dev] Re: Sorry for the delay

From: Roberto E. Vargas Caballero <k0ga_at_shike2.com>
Date: Thu, 2 Mar 2023 18:41:26 +0100

> > Then, we can try to find a way to have all these strings that you added only
> > when NDEBUG is not defined, because I am a bit worried about the increment
> > of the binary due to them.
>
> Yes, it is reasonable to have less messaging if NDEBUG is defined, and the
> default is that it is not.

This diff is basically the same that you sent but with the proposed solution
and minor changes:


diff --git a/include/scc/scc/scc.h b/include/scc/scc/scc.h
index e18b38ea..eee2179c 100644
--- a/include/scc/scc/scc.h
+++ b/include/scc/scc/scc.h
_at_@ -5,9 +5,11 @@ extern int enadebug;
 #ifndef NDEBUG
 #define DBG(...) dbg(__VA_ARGS__)
 #define DBGON() (enadebug = 1)
+#define FATAL(...) fatal(__FILE__, __LINE__, __VA_ARGS__)
 #else
 #define DBG(...)
 #define DBGON()
+#define FATAL(...) abort()
 #endif
 
 #define TINT long long
_at_@ -17,6 +19,8 @@ extern int enadebug;
 #define TFLOAT double
 #define SIZET size_t
 
+#define NELEM(x) (sizeof(x)/sizeof((x)[0]))
+
 struct items {
         char **s;
         unsigned n;
_at_@ -26,6 +30,7 @@ typedef struct alloc Alloc;
 
 extern void die(const char *fmt, ...);
 extern void dbg(const char *fmt, ...);
+extern void fatal(char *, int, char *, ...);
 extern void newitem(struct items *items, char *item);
 extern void *xmalloc(size_t size);
 extern void *xcalloc(size_t nmemb, size_t size);
diff --git a/src/cmd/cc/cc1/cc1.h b/src/cmd/cc/cc1/cc1.h
index e64cbf3f..33792454 100644
--- a/src/cmd/cc/cc1/cc1.h
+++ b/src/cmd/cc/cc1/cc1.h
_at_@ -129,7 +129,7 @@ enum {
         CPPMODE
 };
 
-/* input tokens */
+/* input tokens (see error.c for strings) */
 enum tokens {
         CONST = 1 << 0, /* type qualifier tokens are used as flags */
         RESTRICT = 1 << 1,
_at_@ -216,7 +216,7 @@ enum tokens {
         EOFTOK
 };
 
-/* operations */
+/* operations (see error.c for strings) */
 enum op {
         OADD,
         OMUL,
_at_@ -433,6 +433,7 @@ extern void unexpected(void);
 extern void errorp(char *fmt, ...);
 extern void cpperror(char *fmt, ...);
 extern Type *deftype(Type *tp);
+extern char *opstr(int), *tokstr(int);
 
 /* types.c */
 extern int eqtype(Type *tp1, Type *tp2, int eqflag);
diff --git a/src/cmd/cc/cc1/code.c b/src/cmd/cc/cc1/code.c
index 89e3033e..5c8144b0 100644
--- a/src/cmd/cc/cc1/code.c
+++ b/src/cmd/cc/cc1/code.c
_at_@ -230,7 +230,7 @@ emitconst(Node *np)
                         (long long) u & ones(tp->size));
                 break;
         default:
- abort();
+ FATAL("default abort: tp->op = %s\n", tokstr(tp->op));
         }
 }
 
_at_@ -306,7 +306,7 @@ emittype(int op, void *arg)
         case ENUM:
                 return;
         default:
- abort();
+ FATAL("default abort: tp->op = %s\n", tokstr(tp->op));
         }
 }
 
_at_@ -404,7 +404,7 @@ emitdesig(Node *np, Type *tp, SIZET *addr)
                 emitpadding(tp, addr);
                 break;
         default:
- abort();
+ FATAL("default abort: tp->op = %s\n", tokstr(tp->op));
         }
 
         if (sym) {
diff --git a/src/cmd/cc/cc1/error.c b/src/cmd/cc/cc1/error.c
index a89b7e7f..716811ed 100644
--- a/src/cmd/cc/cc1/error.c
+++ b/src/cmd/cc/cc1/error.c
_at_@ -10,6 +10,175 @@
 extern int failure;
 static unsigned nerrors;
 
+#ifndef NDEBUG
+char *
+tokstr(int tok)
+{
+ char *strings[] = {
+ [CONST] = "CONST",
+ [RESTRICT] = "RESTRICT",
+ [VOLATILE] = "VOLATILE",
+ [INLINE] = "INLINE",
+ [TQUALIFIER] = "TQUALIFIER",
+ [MACROPAR] = "MACROPAR",
+ [CONCAT] = "CONCAT",
+ [STRINGIZE] = "STRINGIZE",
+ [TYPE] = "TYPE",
+ [IDEN] = "IDEN",
+ [SCLASS] = "SCLASS",
+ [CONSTANT] = "CONSTANT",
+ [STRING] = "STRING",
+ [SIZEOF] = "SIZEOF",
+ [INDIR] = "INDIR",
+ [INC] = "INC",
+ [DEC] = "DEC",
+ [SHL] = "SHL",
+ [SHR] = "SHR",
+ [LE] = "LE",
+ [GE] = "GE",
+ [EQ] = "EQ",
+ [NE] = "NE",
+ [AND] = "AND",
+ [OR] = "OR",
+ [MUL_EQ] = "MUL_EQ",
+ [DIV_EQ] = "DIV_EQ",
+ [MOD_EQ] = "MOD_EQ",
+ [ADD_EQ] = "ADD_EQ",
+ [SUB_EQ] = "SUB_EQ",
+ [AND_EQ] = "AND_EQ",
+ [XOR_EQ] = "XOR_EQ",
+ [OR_EQ] = "OR_EQ",
+ [SHL_EQ] = "SHL_EQ",
+ [SHR_EQ] = "SHR_EQ",
+ [ELLIPSIS] = "ELLIPSIS",
+ [CASE] = "CASE",
+ [DEFAULT] = "DEFAULT",
+ [IF] = "IF",
+ [ELSE] = "ELSE",
+ [SWITCH] = "SWITCH",
+ [WHILE] = "WHILE",
+ [DO] = "DO",
+ [FOR] = "FOR",
+ [GOTO] = "GOTO",
+ [VOID] = "VOID",
+ [FLOAT] = "FLOAT",
+ [INT] = "INT",
+ [BOOL] = "BOOL",
+ [VA_LIST] = "VA_LIST",
+ [STRUCT] = "STRUCT",
+ [UNION] = "UNION",
+ [CHAR] = "CHAR",
+ [DOUBLE] = "DOUBLE",
+ [SHORT] = "SHORT",
+ [LONG] = "LONG",
+ [LLONG] = "LLONG",
+ [COMPLEX] = "COMPLEX",
+ [TYPEDEF] = "TYPEDEF",
+ [EXTERN] = "EXTERN",
+ [STATIC] = "STATIC",
+ [AUTO] = "AUTO",
+ [REGISTER] = "REGISTER",
+ [ENUM] = "ENUM",
+ [TYPEIDEN] = "TYPEIDEN",
+ [UNSIGNED] = "UNSIGNED",
+ [SIGNED] = "SIGNED",
+ [CONTINUE] = "CONTINUE",
+ [BREAK] = "BREAK",
+ [RETURN] = "RETURN",
+ [DEFINE] = "DEFINE",
+ [DEFINED] = "DEFINED",
+ [INCLUDE] = "INCLUDE",
+ [LINE] = "LINE",
+ [PRAGMA] = "PRAGMA",
+ [ERROR] = "ERROR",
+ [IFDEF] = "IFDEF",
+ [ELIF] = "ELIF",
+ [IFNDEF] = "IFNDEF",
+ [UNDEF] = "UNDEF",
+ [ENDIF] = "ENDIF",
+ [BUILTIN] = "BUILTIN",
+ [EOFTOK] = "EOFTOK"
+ };
+
+ if (tok >= NELEM(strings) || !strings[tok])
+ return "unkown token";
+ return strings[tok];
+}
+
+char *
+opstr(int op)
+{
+ char *strings[] = {
+ [OADD] = "OADD",
+ [OMUL] = "OMUL",
+ [OSUB] = "OSUB",
+ [OINC] = "OINC",
+ [ODEC] = "ODEC",
+ [ODIV] = "ODIV",
+ [OMOD] = "OMOD",
+ [OSHL] = "OSHL",
+ [OSHR] = "OSHR",
+ [OBAND] = "OBAND",
+ [OBXOR] = "OBXOR",
+ [OBOR] = "OBOR",
+ [OSNEG] = "OSNEG",
+ [ONEG] = "ONEG",
+ [OCPL] = "OCPL",
+ [OAND] = "OAND",
+ [OOR] = "OOR",
+ [OEQ] = "OEQ",
+ [ONE] = "ONE",
+ [OLT] = "OLT",
+ [OGE] = "OGE",
+ [OLE] = "OLE",
+ [OGT] = "OGT",
+ [OASSIGN] = "OASSIGN",
+ [OA_MUL] = "OA_MUL",
+ [OA_DIV] = "OA_DIV",
+ [OA_MOD] = "OA_MOD",
+ [OA_ADD] = "OA_ADD",
+ [OA_SUB] = "OA_SUB",
+ [OA_SHL] = "OA_SHL",
+ [OA_SHR] = "OA_SHR",
+ [OA_AND] = "OA_AND",
+ [OA_XOR] = "OA_XOR",
+ [OA_OR] = "OA_OR",
+ [OADDR] = "OADDR",
+ [OCOMMA] = "OCOMMA",
+ [OCAST] = "OCAST",
+ [OPTR] = "OPTR",
+ [OSYM] = "OSYM",
+ [OASK] = "OASK",
+ [OCOLON] = "OCOLON",
+ [OFIELD] = "OFIELD",
+ [OLABEL] = "OLABEL",
+ [ODEFAULT] = "ODEFAULT",
+ [OCASE] = "OCASE",
+ [OJUMP] = "OJUMP",
+ [OBRANCH] = "OBRANCH",
+ [OEXPR] = "OEXPR",
+ [OEFUN] = "OEFUN",
+ [OELOOP] = "OELOOP",
+ [OBLOOP] = "OBLOOP",
+ [OFUN] = "OFUN",
+ [OPAR] = "OPAR",
+ [OCALL] = "OCALL",
+ [OCALLE] = "OCALLE",
+ [ORET] = "ORET",
+ [ODECL] = "ODECL",
+ [OBSWITCH] = "OBSWITCH",
+ [OESWITCH] = "OESWITCH",
+ [OINIT] = "OINIT",
+ [OBUILTIN] = "OBUILTIN",
+ [OTYP] = "OTYP",
+ };
+
+ if (op >= NELEM(strings) || !strings[op])
+ return "unkown operation";
+ return strings[op];
+}
+#endif
+
 static void
 warn_error(int flag, char *fmt, va_list va)
 {
diff --git a/src/cmd/cc/cc1/expr.c b/src/cmd/cc/cc1/expr.c
index 75edb4b4..dcba095f 100644
--- a/src/cmd/cc/cc1/expr.c
+++ b/src/cmd/cc/cc1/expr.c
_at_@ -81,7 +81,7 @@ promote(Node *np)
                 tp = doubletype;
                 break;
         default:
- abort();
+ FATAL("default abort: tp->op = %s\n", tokstr(tp->op));
         }
         if ((new = convert(np, tp, 1)) != NULL)
                 return new;
_at_@ -466,7 +466,7 @@ negop(int op)
         case OGE: return OLT;
         case OLE: return OGT;
         case OGT: return OLE;
- default: abort();
+ default: FATAL("default abort: op = %s\n", opstr(op));
         }
         return op;
 }
diff --git a/src/cmd/cc/cc1/fold.c b/src/cmd/cc/cc1/fold.c
index e43fff79..124f9081 100644
--- a/src/cmd/cc/cc1/fold.c
+++ b/src/cmd/cc/cc1/fold.c
_at_@ -305,7 +305,7 @@ foldconst(int type, int op, Type *tp, Symbol *ls, Symbol *rs)
                         return NULL;
                 break;
         default:
- abort();
+ FATAL("default abort: type = %s\n", tokstr(type));
         }
         sym = newsym(NS_IDEN, NULL);
         sym->flags |= SCONSTANT;
diff --git a/src/cmd/cc/cc1/init.c b/src/cmd/cc/cc1/init.c
index b3360cec..65967f3c 100644
--- a/src/cmd/cc/cc1/init.c
+++ b/src/cmd/cc/cc1/init.c
_at_@ -343,8 +343,10 @@ repeat:
                 goto repeat;
         case ARY:
         case STRUCT:
- if (!(np->flags & NCONST))
- abort(); /* TODO */
+ if (!(np->flags & NCONST)) {
+ /* TODO */
+ FATAL("default abort: tp->op = %s\n", tokstr(tp->op));
+ }
                 hidden = newsym(NS_IDEN, NULL);
                 hidden->id = newid();
                 hidden->type = sym->type;
diff --git a/src/cmd/cc/cc1/lex.c b/src/cmd/cc/cc1/lex.c
index 22f0928c..c356fec9 100644
--- a/src/cmd/cc/cc1/lex.c
+++ b/src/cmd/cc/cc1/lex.c
_at_@ -107,7 +107,7 @@ addinput(int type, void *arg, int fail)
                 DBG("INPUT: file input 'stdin'");
                 break;
         default:
- abort();
+ FATAL("default abort: type = %d\n", type);
         }
 
         if (!buffer) {
_at_@ -153,7 +153,7 @@ delinput(void)
                 DBG("INPUT: stdin finished");
                 break;
         default:
- abort();
+ FATAL("default abort: type = %d\n", ip->flags & ITYPE);
         }
 
         input = ip->next;
diff --git a/src/cmd/cc/cc1/symbol.c b/src/cmd/cc/cc1/symbol.c
index 7055ff03..879df899 100644
--- a/src/cmd/cc/cc1/symbol.c
+++ b/src/cmd/cc/cc1/symbol.c
_at_@ -40,7 +40,7 @@ dumpstab(Symbol **tbl, char *msg)
         else if (tbl == htablbl)
                 size = NR_LBL_HASH;
         else
- abort();
+ FATAL("default abort; tbl = %d\n", tbl);
 
         for (bp = tbl; bp < &tbl[size]; ++bp) {
                 if (*bp == NULL)
diff --git a/src/cmd/cc/cc1/types.c b/src/cmd/cc/cc1/types.c
index fc2b84a9..53cf4e9c 100644
--- a/src/cmd/cc/cc1/types.c
+++ b/src/cmd/cc/cc1/types.c
_at_@ -98,7 +98,7 @@ getlimits(Type *tp)
                 }
                 break;
         default:
- abort();
+ FATAL("default abort: tp->op = %s\n", tokstr(tp->op));
         }
 
         return &limits[ntable][ntype];
_at_@ -240,7 +240,7 @@ typesize(Type *tp)
         case FTN:
                 return;
         default:
- abort();
+ FATAL("default abort: tp->op = %s\n", tokstr(tp->op));
         }
 }
 
_at_@ -337,7 +337,7 @@ mktype(Type *tp, int op, TINT nelem, Type *pars[])
         create_type:
                 return newtype(&type);
         default:
- abort();
+ FATAL("default abort: op = %s\n", tokstr(op));
         }
 
         tbl = &typetab[HASH(&type)];
_at_@ -467,7 +467,7 @@ eqtype(Type *tp1, Type *tp2, int equiv)
         case FLOAT:
                 return tp1->letter == tp2->letter;
         default:
- abort();
+ FATAL("default abort: tp1->op = %s\n", tokstr(tp1->op));
         }
 }
 
diff --git a/src/libscc/Makefile b/src/libscc/Makefile
index a0a0b070..752c8f7d 100644
--- a/src/libscc/Makefile
+++ b/src/libscc/Makefile
_at_@ -6,6 +6,7 @@ include $(PROJECTDIR)/scripts/rules.mk
 OBJS =\
         debug.o\
         die.o\
+ fatal.o\
         newitem.o\
         xcalloc.o\

         xmalloc.o\
--
To unsubscribe send a mail to scc-dev+unsubscribe_at_simple-cc.org
Received on Thu 02 Mar 2023 - 18:41:26 CET

This archive was generated by hypermail 2.3.0 : Fri 21 Apr 2023 - 16:20:37 CEST