From 2edc4e2865e3407b4171deb49d1bc782e2f1718e Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Mon, 15 Mar 2004 05:18:07 +0000 Subject: [PATCH] Convert re2c cleanup.pl to cleanup.c, removing final Perl dependency. Remove checking for Perl and Perl version in configure. svn path=/trunk/yasm/; revision=1104 --- configure.ac | 20 ---- modules/arch/lc3b/Makefile.inc | 4 +- modules/arch/x86/Makefile.inc | 4 +- modules/parsers/nasm/Makefile.inc | 4 +- tools/re2c/Makefile.inc | 5 +- tools/re2c/cleanup.c | 163 ++++++++++++++++++++++++++++++ tools/re2c/cleanup.pl | 32 ------ 7 files changed, 172 insertions(+), 60 deletions(-) create mode 100644 tools/re2c/cleanup.c delete mode 100755 tools/re2c/cleanup.pl diff --git a/configure.ac b/configure.ac index ad8f4e78..9654fb27 100644 --- a/configure.ac +++ b/configure.ac @@ -1,9 +1,6 @@ # Process this file with autoconf to produce a configure script. # $IdPath$ -# Minimum required perl version for development -PERL_VERSION=5.004 - # # autoconf setup # @@ -83,8 +80,6 @@ if test "$ac_cv_prog_cc_stdc" = no; then AC_MSG_ERROR([A standard (ANSI/ISO C89) C compiler is required.]) fi -# Check for Perl (for gen_instr.pl and the like, needed only for development) -AC_PATH_PROGS(PERL, $PERL perl5 perl) # Check for groff (for rendering manpages, needed only for development) AC_PATH_PROGS(GROFF, $GROFF groff) @@ -175,21 +170,6 @@ if test "$USE_MAINTAINER_MODE" = "yes"; then MORE_CFLAGS="$MORE_CFLAGS -Waggregate-return -Wbad-function-cast -Wcast-align -Wcast-qual -Wconversion -Winline -Wmissing-declarations -Wmissing-prototypes -Wnested-externs -Wpointer-arith -Wshadow -Wsign-compare -Wstrict-prototypes -Wwrite-strings" fi - # Require Perl - if test -z "$PERL" || test "$PERL" = ":"; then - AC_MSG_ERROR([Perl not found in \$PATH]) - fi - - # Require Perl >= PERL_VERSION - AC_MSG_CHECKING([for minimum required perl version >= $PERL_VERSION]) - _perl_version=`PERL_VERSION=$PERL_VERSION $PERL -e 'print "$]"; if ($] >= $ENV{PERL_VERSION}) { exit(0); } else { exit(1); }' 2>&5` - _perl_res=$? - AC_MSG_RESULT([$_perl_version]) - - if test "$_perl_res" != 0; then - AC_MSG_ERROR([Perl $PERL_VERSION or higher is required.]) - fi - # Require groff if test -z "$GROFF" || test "$GROFF" = ":"; then AC_MSG_ERROR([groff not found in \$PATH]) diff --git a/modules/arch/lc3b/Makefile.inc b/modules/arch/lc3b/Makefile.inc index c11b2188..ff14ef9c 100644 --- a/modules/arch/lc3b/Makefile.inc +++ b/modules/arch/lc3b/Makefile.inc @@ -11,9 +11,9 @@ arch_lc3b_la_LDFLAGS = -module -avoid-version -no-undefined arch_lc3b_la_LIBADD = libyasm.la YASM_MODULES += -dlopen arch_lc3b.la -lc3bid.c: $(srcdir)/modules/arch/lc3b/lc3bid.re re2c$(EXEEXT) $(srcdir)/tools/re2c/cleanup.pl +lc3bid.c: $(srcdir)/modules/arch/lc3b/lc3bid.re re2c$(EXEEXT) cleanup$(EXEEXT) $(top_builddir)/re2c$(EXEEXT) -s $(srcdir)/modules/arch/lc3b/lc3bid.re \ - | $(PERL) $(top_srcdir)/tools/re2c/cleanup.pl \ + | $(top_builddir)/cleanup$(EXEEXT) \ | sed "/^#l/ s,re2c-out\.c,$@," > $@ BUILT_SOURCES += lc3bid.c diff --git a/modules/arch/x86/Makefile.inc b/modules/arch/x86/Makefile.inc index 2b89d14f..aa9f3642 100644 --- a/modules/arch/x86/Makefile.inc +++ b/modules/arch/x86/Makefile.inc @@ -12,9 +12,9 @@ arch_x86_la_LDFLAGS = -module -avoid-version -no-undefined arch_x86_la_LIBADD = libyasm.la YASM_MODULES += -dlopen arch_x86.la -x86id.c: $(srcdir)/modules/arch/x86/x86id.re re2c$(EXEEXT) $(srcdir)/tools/re2c/cleanup.pl +x86id.c: $(srcdir)/modules/arch/x86/x86id.re re2c$(EXEEXT) cleanup$(EXEEXT) $(top_builddir)/re2c$(EXEEXT) -s $(srcdir)/modules/arch/x86/x86id.re \ - | $(PERL) $(top_srcdir)/tools/re2c/cleanup.pl \ + | $(top_builddir)/cleanup$(EXEEXT) \ | sed "/^#l/ s,re2c-out\.c,$@," > $@ BUILT_SOURCES += x86id.c diff --git a/modules/parsers/nasm/Makefile.inc b/modules/parsers/nasm/Makefile.inc index 9881629e..5eba5953 100644 --- a/modules/parsers/nasm/Makefile.inc +++ b/modules/parsers/nasm/Makefile.inc @@ -13,9 +13,9 @@ parser_nasm_la_LDFLAGS = -module -avoid-version -no-undefined parser_nasm_la_LIBADD = libyasm.la YASM_MODULES += -dlopen parser_nasm.la -nasm-token.c: $(srcdir)/modules/parsers/nasm/nasm-token.re re2c$(EXEEXT) $(srcdir)/tools/re2c/cleanup.pl +nasm-token.c: $(srcdir)/modules/parsers/nasm/nasm-token.re re2c$(EXEEXT) cleanup$(EXEEXT) $(top_builddir)/re2c$(EXEEXT) -b $(srcdir)/modules/parsers/nasm/nasm-token.re \ - | $(PERL) $(top_srcdir)/tools/re2c/cleanup.pl \ + | $(top_builddir)/cleanup$(EXEEXT) \ | sed "/^#l/ s,re2c-out\.c,$@," > $@ BUILT_SOURCES += nasm-bison.c diff --git a/tools/re2c/Makefile.inc b/tools/re2c/Makefile.inc index 87b00ba1..c1d107a4 100644 --- a/tools/re2c/Makefile.inc +++ b/tools/re2c/Makefile.inc @@ -1,6 +1,6 @@ # $IdPath$ -noinst_PROGRAMS += re2c +noinst_PROGRAMS += re2c cleanup re2c_CFLAGS = re2c_SOURCES = tools/re2c/main.c @@ -21,6 +21,8 @@ re2c_SOURCES += tools/re2c/substr.h re2c_SOURCES += tools/re2c/substr.c re2c_SOURCES += tools/re2c/translate.c +cleanup_SOURCES = tools/re2c/cleanup.c + BUILT_SOURCES += re2c-parser.c BUILT_SOURCES += re2c-parser.h @@ -47,4 +49,3 @@ EXTRA_DIST += tools/re2c/examples/simple.re EXTRA_DIST += tools/re2c/examples/rexx/README EXTRA_DIST += tools/re2c/examples/rexx/rexx.l EXTRA_DIST += tools/re2c/examples/rexx/scanio.c -EXTRA_DIST += tools/re2c/cleanup.pl diff --git a/tools/re2c/cleanup.c b/tools/re2c/cleanup.c new file mode 100644 index 00000000..960f2bc4 --- /dev/null +++ b/tools/re2c/cleanup.c @@ -0,0 +1,163 @@ +/* $IdPath$ + * + * Clean up re2c output to avoid compiler warnings. + * + * Copyright (C) 2004 Peter Johnson + * + * 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 AUTHOR AND OTHER 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 AUTHOR OR OTHER 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 +#include +#include + +#define MAXLINE 1024 + +int +main() +{ + char str[MAXLINE]; + size_t alloclines = 1000; + size_t numlines = 0; + char **inlines; + size_t alloclabel = 100; + unsigned char *usedlabel; + size_t allocvar = 1000; + unsigned char *usedvar; + int lastusedvarline = 0; + int level = 0; + size_t line; + unsigned long value; + char *pos; + size_t span1, span2; + + inlines = malloc(alloclines * sizeof(char *)); + if (!inlines) { + fputs("Out of memory.\n", stderr); + return EXIT_FAILURE; + } + + while (fgets(str, MAXLINE, stdin)) { + /* check array bounds */ + if (numlines >= alloclines) { + alloclines *= 2; + inlines = realloc(inlines, alloclines * sizeof(char *)); + if (!inlines) { + fputs("Out of memory.\n", stderr); + return EXIT_FAILURE; + } + } + inlines[numlines] = strdup(str); + numlines++; + } + + usedlabel = calloc(alloclabel, 1); + usedvar = calloc(allocvar, 1); + if (!usedlabel || !usedvar) { + fputs("Out of memory.\n", stderr); + return EXIT_FAILURE; + } + + for (line = 1; line <= numlines; line++) { + /* look for goto yy[0-9]+ statements */ + if ((pos = strstr(inlines[line-1], "goto")) && + (span1 = strspn(&pos[4], " \t")) > 0 && + strncmp(&pos[4+span1], "yy", 2) == 0 && + (span2 = strspn(&pos[6+span1], "0123456789")) > 0 && + strspn(&pos[6+span1+span2], " \t;") > 0) { + /* convert label to integer */ + value = strtoul(&pos[6+span1], NULL, 10); + /* check array bounds */ + while (value >= alloclabel) { + usedlabel = realloc(usedlabel, alloclabel * 2); + if (!usedlabel) { + fputs("Out of memory.\n", stderr); + return EXIT_FAILURE; + } + memset(usedlabel + alloclabel, 0, alloclabel); + alloclabel *= 2; + } + usedlabel[value] = 1; + } + + /* keep track of the brace level of the code (approximately) */ + pos = inlines[line-1]; + while ((pos = strchr(pos, '{'))) { + level++; + pos++; + } + pos = inlines[line-1]; + while ((pos = strchr(pos, '}'))) { + level--; + pos++; + } + + /* check for leaving the scope of the last used yyaccept variable */ + if (level < usedvar[lastusedvarline]) + lastusedvarline = 0; + + /* check for int yyaccept variable declaration / usage */ + if ((pos = strstr(inlines[line-1], "int")) && + (span1 = strspn(&pos[3], " \t")) > 0 && + strncmp(&pos[3+span1], "yyaccept", 8) == 0 && + strspn(&pos[11+span1], " \t;") > 0) { + /* declaration */ + /* check array bounds */ + while (line >= allocvar) { + usedvar = realloc(usedvar, allocvar * 2); + if (!usedvar) { + fputs("Out of memory.\n", stderr); + return EXIT_FAILURE; + } + memset(usedvar + allocvar, 0, allocvar); + allocvar *= 2; + } + usedvar[line] = level; + lastusedvarline = line; + } else if (strstr(inlines[line-1], "yyaccept")) + usedvar[lastusedvarline] = 255; /* used */ + } + + for (line = 1; line <= numlines; line++) { + pos = inlines[line-1]; + /* look for yy[0-9]+ labels */ + if (strncmp(pos, "yy", 2) == 0 && + (span1 = strspn(&pos[2], "0123456789")) > 0 && + pos[2+span1] == ':') { + value = strtoul(&pos[2], NULL, 10); + /* delete unused yy[0-9]+ labels */ + if (value >= alloclabel || !usedlabel[value]) + pos = &pos[2+span1+1]; + } + if (line < allocvar && usedvar[line] != 0 && usedvar[line] != 255) + putc('\n', stdout); + else + fputs(pos, stdout); + } + + free(usedvar); + free(usedlabel); + for (line = 0; line < numlines; line++) + free(inlines[line]); + free(inlines); + + return EXIT_SUCCESS; +} diff --git a/tools/re2c/cleanup.pl b/tools/re2c/cleanup.pl deleted file mode 100755 index 928783a5..00000000 --- a/tools/re2c/cleanup.pl +++ /dev/null @@ -1,32 +0,0 @@ -#! /usr/bin/env perl -my @sourcelines = <>; -my %usedlabel; -my %usedvar; -my $lastusedvarline = 0; -my $level = 0; -my $line = 1; -for (@sourcelines) { - $usedlabel{"$1:"} = "$1:" if m/goto\s+(yy[0-9]*)\s*;/; - $level = $level + 1 if m/\{/; - $level = $level - 1 if m/\}/; - if ($level < $usedvar{$lastusedvarline}) { - $lastusedvarline = 0; - } - if (m/int\s+yyaccept/) { - $usedvar{$line} = $level; - $lastusedvarline = $line; - } elsif (m/yyaccept/) { - $usedvar{$lastusedvarline} = 1000; - } - $line = $line + 1; -} -$line = 1; -for (@sourcelines) { - s/^(yy[0-9]*:)/$usedlabel{$1}/; - if ($usedvar{$line} != 0 && $usedvar{$line} != 1000) { - print "\n"; - } else { - print; - } - $line = $line + 1; -} -- 2.40.0