Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make make: replace bloated bit-set-to-string code wi...



details:   https://anonhg.NetBSD.org/src/rev/4d2f26dc4192
branches:  trunk
changeset: 1026557:4d2f26dc4192
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Nov 28 18:58:58 2021 +0000

description:
make: replace bloated bit-set-to-string code with simple code

It was a nice idea to implement a bit-set using an enum type and have a
generic ToString function for them.  In the end, the implementation
involved really heavy preprocessor magic and was probably difficult to
understand.  Replace all the code with a few bits of straight-forward
preprocessor magic that can be readily understood by just looking 5
lines around, instead of digging through 130 lines of lengthy macro
definitions.

Curiously, this reduces the binary size even though the 3 ToString
functions now have a few lines of duplicate code and there are more
explicit function calls.

The ToString functions are only seldom used, so the additional memory
allocation is acceptable.

No functional change.

diffstat:

 usr.bin/make/Makefile |    3 +-
 usr.bin/make/enum.c   |   80 ----------------------
 usr.bin/make/enum.h   |  179 --------------------------------------------------
 usr.bin/make/make.c   |   98 +++++++++++++++++++++-----
 usr.bin/make/make.h   |    3 +-
 usr.bin/make/suff.c   |   37 ++++++++--
 6 files changed, 109 insertions(+), 291 deletions(-)

diffs (truncated from 502 to 300 lines):

diff -r 10649da14276 -r 4d2f26dc4192 usr.bin/make/Makefile
--- a/usr.bin/make/Makefile     Sun Nov 28 18:08:51 2021 +0000
+++ b/usr.bin/make/Makefile     Sun Nov 28 18:58:58 2021 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.116 2021/07/31 09:30:17 rillig Exp $
+#      $NetBSD: Makefile,v 1.117 2021/11/28 18:58:58 rillig Exp $
 #      @(#)Makefile    5.2 (Berkeley) 12/28/90
 
 PROG=  make
@@ -7,7 +7,6 @@
 SRCS+=  compat.c
 SRCS+=  cond.c
 SRCS+=  dir.c
-SRCS+=  enum.c
 SRCS+=  for.c
 SRCS+=  hash.c
 SRCS+=  job.c
diff -r 10649da14276 -r 4d2f26dc4192 usr.bin/make/enum.c
--- a/usr.bin/make/enum.c       Sun Nov 28 18:08:51 2021 +0000
+++ /dev/null   Thu Jan 01 00:00:00 1970 +0000
@@ -1,80 +0,0 @@
-/*     $NetBSD: enum.c,v 1.15 2021/02/02 17:56:31 rillig Exp $ */
-
-/*
- Copyright (c) 2020 Roland Illig <rillig%NetBSD.org@localhost>
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
-
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
-
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
- BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "make.h"
-
-MAKE_RCSID("$NetBSD: enum.c,v 1.15 2021/02/02 17:56:31 rillig Exp $");
-
-/*
- * Convert a bitset into a string representation, showing the names of the
- * individual bits.
- *
- * Optionally, shortcuts for groups of bits can be added.  To have an effect,
- * they need to be listed before their individual bits.
- */
-const char *
-Enum_FlagsToString(char *buf, size_t buf_size,
-                  int value, const EnumToStringSpec *spec)
-{
-       const char *buf_start = buf;
-       const char *sep = "";
-       size_t sep_len = 0;
-
-       for (; spec->es_value != 0; spec++) {
-               size_t name_len;
-
-               if ((value & spec->es_value) != spec->es_value)
-                       continue;
-               value &= ~spec->es_value;
-
-               assert(buf_size >= sep_len + 1);
-               memcpy(buf, sep, sep_len);
-               buf += sep_len;
-               buf_size -= sep_len;
-
-               name_len = strlen(spec->es_name);
-               assert(buf_size >= name_len + 1);
-               memcpy(buf, spec->es_name, name_len);
-               buf += name_len;
-               buf_size -= name_len;
-
-               sep = ENUM__SEP;
-               sep_len = sizeof ENUM__SEP - 1;
-       }
-
-       /* If this assertion fails, the listed enum values are incomplete. */
-       assert(value == 0);
-
-       if (buf == buf_start)
-               return "none";
-
-       assert(buf_size >= 1);
-       buf[0] = '\0';
-       return buf_start;
-}
diff -r 10649da14276 -r 4d2f26dc4192 usr.bin/make/enum.h
--- a/usr.bin/make/enum.h       Sun Nov 28 18:08:51 2021 +0000
+++ /dev/null   Thu Jan 01 00:00:00 1970 +0000
@@ -1,179 +0,0 @@
-/*     $NetBSD: enum.h,v 1.19 2021/03/15 16:00:05 rillig Exp $ */
-
-/*
- Copyright (c) 2020 Roland Illig <rillig%NetBSD.org@localhost>
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
-
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
-
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
- BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- POSSIBILITY OF SUCH DAMAGE.
- */
-
-#ifndef MAKE_ENUM_H
-#define MAKE_ENUM_H
-
-/* Generate string representations for bitmasks and simple enums. */
-
-#include <stddef.h>
-
-typedef struct EnumToStringSpec {
-       int es_value;
-       const char *es_name;
-} EnumToStringSpec;
-
-
-const char *Enum_FlagsToString(char *, size_t, int, const EnumToStringSpec *);
-
-
-/* For Enum_FlagsToString, the separator between flags. */
-#define ENUM__SEP "|"
-
-/*
- * Generate the string that joins all possible flags, to see how large the
- * buffer must be.
- */
-#define ENUM__JOIN_STR_1(v1) \
-       #v1
-#define ENUM__JOIN_STR_2(v1, v2) \
-       ENUM__JOIN_STR_1(v1) ENUM__SEP \
-       ENUM__JOIN_STR_1(v2)
-#define ENUM__JOIN_STR_4(v1, v2, v3, v4) \
-       ENUM__JOIN_STR_2(v1, v2) ENUM__SEP \
-       ENUM__JOIN_STR_2(v3, v4)
-#define ENUM__JOIN_STR_8(v1, v2, v3, v4, v5, v6, v7, v8) \
-       ENUM__JOIN_STR_4(v1, v2, v3, v4) ENUM__SEP \
-       ENUM__JOIN_STR_4(v5, v6, v7, v8)
-#define ENUM__JOIN_STR_16(v01, v02, v03, v04, v05, v06, v07, v08, \
-                         v09, v10, v11, v12, v13, v14, v15, v16) \
-       ENUM__JOIN_STR_8(v01, v02, v03, v04, v05, v06, v07, v08) ENUM__SEP \
-       ENUM__JOIN_STR_8(v09, v10, v11, v12, v13, v14, v15, v16)
-
-#define ENUM__JOIN_2(part1, part2) \
-       part1 ENUM__SEP part2
-#define ENUM__JOIN_3(part1, part2, part3) \
-       part1 ENUM__SEP part2 ENUM__SEP part3
-#define ENUM__JOIN_4(part1, part2, part3, part4) \
-       part1 ENUM__SEP part2 ENUM__SEP part3 ENUM__SEP part4
-#define ENUM__JOIN_5(part1, part2, part3, part4, part5) \
-       part1 ENUM__SEP part2 ENUM__SEP part3 ENUM__SEP part4 ENUM__SEP part5
-
-/* List the pairs of enum value and corresponding name. */
-#define ENUM__SPEC_1(v1) \
-       { v1, #v1 }
-#define ENUM__SPEC_2(v1, v2) \
-       ENUM__SPEC_1(v1), \
-       ENUM__SPEC_1(v2)
-#define ENUM__SPEC_4(v1, v2, v3, v4) \
-       ENUM__SPEC_2(v1, v2), \
-       ENUM__SPEC_2(v3, v4)
-#define ENUM__SPEC_8(v1, v2, v3, v4, v5, v6, v7, v8) \
-       ENUM__SPEC_4(v1, v2, v3, v4), \
-       ENUM__SPEC_4(v5, v6, v7, v8)
-#define ENUM__SPEC_16(v01, v02, v03, v04, v05, v06, v07, v08, \
-                     v09, v10, v11, v12, v13, v14, v15, v16) \
-       ENUM__SPEC_8(v01, v02, v03, v04, v05, v06, v07, v08), \
-       ENUM__SPEC_8(v09, v10, v11, v12, v13, v14, v15, v16)
-
-#define ENUM__SPECS_2(part1, part2) \
-       { part1, part2, { 0, "" } }
-#define ENUM__SPECS_3(part1, part2, part3) \
-       { part1, part2, part3, { 0, "" } }
-#define ENUM__SPECS_4(part1, part2, part3, part4) \
-       { part1, part2, part3, part4, { 0, "" } }
-#define ENUM__SPECS_5(part1, part2, part3, part4, part5) \
-       { part1, part2, part3, part4, part5, { 0, "" } }
-
-
-/* Declare the necessary data structures for calling Enum_FlagsToString. */
-#define ENUM__FLAGS_RTTI(typnam, specs, joined) \
-       static const EnumToStringSpec typnam ## _ ## ToStringSpecs[] = specs; \
-       enum { typnam ## _ ## ToStringSize = sizeof (joined) }; \
-       MAKE_INLINE const char *typnam ## _ToString(char *buf, typnam value) \
-       { return Enum_FlagsToString(buf, typnam ## _ ## ToStringSize, \
-           value, typnam ## _ ## ToStringSpecs); \
-       } \
-       extern void enum_flags_rtti_dummy(void)
-
-/*
- * Declare the necessary data structures for calling Enum_FlagsToString
- * for an enum with 3 flags.
- */
-#define ENUM_FLAGS_RTTI_3(typnam, v1, v2, v3) \
-       ENUM__FLAGS_RTTI(typnam, \
-           ENUM__SPECS_2( \
-               ENUM__SPEC_2(v1, v2), \
-               ENUM__SPEC_1(v3)), \
-           ENUM__JOIN_2( \
-               ENUM__JOIN_STR_2(v1, v2), \
-               ENUM__JOIN_STR_1(v3)))
-
-/*
- * Declare the necessary data structures for calling Enum_FlagsToString
- * for an enum with 6 flags.
- */
-#define ENUM_FLAGS_RTTI_6(typnam, v1, v2, v3, v4, v5, v6) \
-       ENUM__FLAGS_RTTI(typnam, \
-           ENUM__SPECS_2( \
-               ENUM__SPEC_4(v1, v2, v3, v4), \
-               ENUM__SPEC_2(v5, v6)), \
-           ENUM__JOIN_2( \
-               ENUM__JOIN_STR_4(v1, v2, v3, v4), \
-               ENUM__JOIN_STR_2(v5, v6)))
-
-/*
- * Declare the necessary data structures for calling Enum_FlagsToString
- * for an enum with 9 flags.
- */
-#define ENUM_FLAGS_RTTI_9(typnam, v1, v2, v3, v4, v5, v6, v7, v8, v9) \
-       ENUM__FLAGS_RTTI(typnam, \
-           ENUM__SPECS_2( \
-               ENUM__SPEC_8(v1, v2, v3, v4, v5, v6, v7, v8), \
-               ENUM__SPEC_1(v9)), \
-           ENUM__JOIN_2( \
-               ENUM__JOIN_STR_8(v1, v2, v3, v4, v5, v6, v7, v8), \
-               ENUM__JOIN_STR_1(v9)))
-
-/*
- * Declare the necessary data structures for calling Enum_FlagsToString
- * for an enum with 31 flags.
- */
-#define ENUM_FLAGS_RTTI_31(typnam, \
-                    v01, v02, v03, v04, v05, v06, v07, v08, \
-                    v09, v10, v11, v12, v13, v14, v15, v16, \
-                    v17, v18, v19, v20, v21, v22, v23, v24, \
-                    v25, v26, v27, v28, v29, v30, v31) \
-    ENUM__FLAGS_RTTI(typnam, \
-       ENUM__SPECS_5( \
-           ENUM__SPEC_16(v01, v02, v03, v04, v05, v06, v07, v08, \
-                         v09, v10, v11, v12, v13, v14, v15, v16), \
-           ENUM__SPEC_8(v17, v18, v19, v20, v21, v22, v23, v24), \
-           ENUM__SPEC_4(v25, v26, v27, v28), \
-           ENUM__SPEC_2(v29, v30), \
-           ENUM__SPEC_1(v31)), \
-       ENUM__JOIN_5( \
-           ENUM__JOIN_STR_16(v01, v02, v03, v04, v05, v06, v07, v08, \
-                             v09, v10, v11, v12, v13, v14, v15, v16), \
-           ENUM__JOIN_STR_8(v17, v18, v19, v20, v21, v22, v23, v24), \
-           ENUM__JOIN_STR_4(v25, v26, v27, v28), \
-           ENUM__JOIN_STR_2(v29, v30), \
-           ENUM__JOIN_STR_1(v31)))
-
-#endif
diff -r 10649da14276 -r 4d2f26dc4192 usr.bin/make/make.c
--- a/usr.bin/make/make.c       Sun Nov 28 18:08:51 2021 +0000
+++ b/usr.bin/make/make.c       Sun Nov 28 18:58:58 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: make.c,v 1.244 2021/04/04 10:05:08 rillig Exp $        */
+/*     $NetBSD: make.c,v 1.245 2021/11/28 18:58:58 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -104,7 +104,7 @@
 #include "job.h"
 
 /*     "@(#)make.c     8.1 (Berkeley) 6/6/93"  */
-MAKE_RCSID("$NetBSD: make.c,v 1.244 2021/04/04 10:05:08 rillig Exp $");
+MAKE_RCSID("$NetBSD: make.c,v 1.245 2021/11/28 18:58:58 rillig Exp $");
 



Home | Main Index | Thread Index | Old Index