doc f7abe7
[p5sagit/p5-mst-13.2.git] / regcomp.pl
index 2884971..9370487 100644 (file)
@@ -1,3 +1,18 @@
+#!/usr/bin/perl
+# 
+# Regenerate (overwriting only if changed):
+#
+#    regnodes.h
+#
+# from information stored in
+#
+#    regcomp.sym
+#    regexp.h
+#
+# Accepts the standard regen_lib -q and -v args.
+#
+# This script is normally invoked from regen.pl.
+
 BEGIN {
     # Get function prototypes
     require 'regen_lib.pl';
@@ -9,7 +24,7 @@ use warnings;
 open DESC, 'regcomp.sym';
 
 my $ind = 0;
-my (@name,@rest,@type,@code,@args,@longj);
+my (@name,@rest,@type,@code,@args,@flags,@longj);
 my ($desc,$lastregop);
 while (<DESC>) {
     s/#.*$//;
@@ -20,12 +35,12 @@ while (<DESC>) {
         next;
     }
     unless ($lastregop) {
-        $ind++;
-        ($name[$ind], $desc, $rest[$ind]) = split /\t+/, $_, 3;  
-        ($type[$ind], $code[$ind], $args[$ind], $longj[$ind]) 
-          = split /[,\s]\s*/, $desc, 4;
+        ($name[$ind], $desc, $rest[$ind]) = /^(\S+)\s+([^\t]+)\s*;\s*(.*)/;
+        ($type[$ind], $code[$ind], $args[$ind], $flags[$ind], $longj[$ind])
+          = split /[,\s]\s*/, $desc;
+        ++$ind;
     } else {
-        my ($type,@lists)=split /\s*\t+\s*/, $_;
+        my ($type,@lists)=split /\s+/, $_;
         die "No list? $type" if !@lists;
         foreach my $list (@lists) {
             my ($names,$special)=split /:/, $list , 2;
@@ -45,43 +60,89 @@ while (<DESC>) {
                     die "unknown :type ':$special'";
                 }
                 foreach my $suffix (@suffix) {
-                    $ind++;
                     $name[$ind]="$real$suffix";
                     $type[$ind]=$type;
-                    $rest[$ind]="Regmatch state for $type";
+                    $rest[$ind]="state for $type";
+                    ++$ind;
                 }
             }
         }
         
     }
 }
-my ($width,$rwidth,$twidth)=(0,0,0);
-for (1..@name) {
-    $width=length($name[$_]) if $name[$_] and $width<length($name[$_]);
-    $twidth=length($type[$_]) if $type[$_] and $twidth<length($type[$_]);
-    $rwidth=$width if $_ == $lastregop;
-}
+# use fixed width to keep the diffs between regcomp.pl recompiles
+# as small as possible.
+my ($width,$rwidth,$twidth)=(22,12,9);
 $lastregop ||= $ind;
 my $tot = $ind;
 close DESC;
 die "Too many regexp/state opcodes! Maximum is 256, but there are $lastregop in file!"
     if $lastregop>256;
 
-my $tmp_h = 'tmp_reg.h';
+sub process_flags {
+  my ($flag, $varname, $comment) = @_;
+  $comment = '' unless defined $comment;
+
+  $ind = 0;
+  my @selected;
+  my $bitmap = '';
+  do {
+    my $set = $flags[$ind] && $flags[$ind] eq $flag ? 1 : 0;
+    # Whilst I could do this with vec, I'd prefer to do longhand the arithmetic
+    # ops in the C code.
+    my $current = do {
+      no warnings 'uninitialized';
+      ord do {
+       no warnings 'substr';
+       substr $bitmap, ($ind >> 3);
+      }
+    };
+    substr $bitmap, ($ind >> 3), 1, chr($current | ($set << ($ind & 7)));
+
+    push @selected, $name[$ind] if $set;
+  } while (++$ind < $lastregop);
+  my $out_string = join ', ', @selected, 0;
+  $out_string =~ s/(.{1,70},) /$1\n    /g;
+
+  my $out_mask = join ', ', map {sprintf "0x%02X", ord $_} split '', $bitmap;
+
+  return $comment . <<"EOP";
+#define REGNODE_\U$varname\E(node) (PL_${varname}_bitmask[(node) >> 3] & (1 << ((node) & 7)))
+
+#ifndef DOINIT
+EXTCONST U8 PL_${varname}[] __attribute__deprecated__;
+#else
+EXTCONST U8 PL_${varname}[] __attribute__deprecated__ = {
+    $out_string
+};
+#endif /* DOINIT */
+
+#ifndef DOINIT
+EXTCONST U8 PL_${varname}_bitmask[];
+#else
+EXTCONST U8 PL_${varname}_bitmask[] = {
+    $out_mask
+};
+#endif /* DOINIT */
+
+EOP
+}
+
+my $tmp_h = 'regnodes.h-new';
 
 unlink $tmp_h if -f $tmp_h;
 
-open OUT, ">$tmp_h";
-#*OUT=\*STDOUT;
-binmode OUT;
+my $out = safer_open($tmp_h);
 
-printf OUT <<EOP,
+printf $out <<EOP,
 /* -*- buffer-read-only: t -*-
    !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
    This file is built by regcomp.pl from regcomp.sym.
    Any changes made here will be lost!
 */
 
+/* Regops and State definitions */
+
 #define %*s\t%d
 #define %*s\t%d
 
@@ -90,17 +151,20 @@ EOP
     -$width, REGMATCH_STATE_MAX => $tot - 1
 ;
 
-$ind = 0;
-while (++$ind <= $tot) {
-  my $oind = $ind - 1;
-  printf OUT "#define\t%*s\t%d\t/* %#04x %s */\n",
-    -$width, $name[$ind], $ind-1, $ind-1, $rest[$ind];
-  print OUT "\n\t/* ------------ States ------------- */\n\n"
-    if $ind == $lastregop and $lastregop != $tot;
+
+for ($ind=0; $ind < $lastregop ; ++$ind) {
+  printf $out "#define\t%*s\t%d\t/* %#04x %s */\n",
+    -$width, $name[$ind], $ind, $ind, $rest[$ind];
+}
+print $out "\t/* ------------ States ------------- */\n";
+for ( ; $ind < $tot ; $ind++) {
+  printf $out "#define\t%*s\t(REGNODE_MAX + %d)\t/* %s */\n",
+    -$width, $name[$ind], $ind - $lastregop + 1, $rest[$ind];
 }
 
-print OUT <<EOP;
+print $out <<EOP;
 
+/* PL_regkind[] What type of regop or state is this. */
 
 #ifndef DOINIT
 EXTCONST U8 PL_regkind[];
@@ -109,74 +173,131 @@ EXTCONST U8 PL_regkind[] = {
 EOP
 
 $ind = 0;
-while (++$ind <= $tot) {
-  printf OUT "\t%*s\t/* %*s */\n",
+do {
+  printf $out "\t%*s\t/* %*s */\n",
              -1-$twidth, "$type[$ind],", -$width, $name[$ind];
-  print OUT "\t/* ------------ States ------------- */\n"
-    if $ind == $lastregop and $lastregop != $tot;
-}
+  print $out "\t/* ------------ States ------------- */\n"
+    if $ind + 1 == $lastregop and $lastregop != $tot;
+} while (++$ind < $tot);
 
-print OUT <<EOP;
+print $out <<EOP;
 };
 #endif
 
+/* regarglen[] - How large is the argument part of the node (in regnodes) */
 
 #ifdef REG_COMP_C
 static const U8 regarglen[] = {
 EOP
 
 $ind = 0;
-while (++$ind <= $lastregop) {
+do {
   my $size = 0;
   $size = "EXTRA_SIZE(struct regnode_$args[$ind])" if $args[$ind];
   
-  printf OUT "\t%*s\t/* %*s */\n",
+  printf $out "\t%*s\t/* %*s */\n",
        -37, "$size,",-$rwidth,$name[$ind];
-}
+} while (++$ind < $lastregop);
 
-print OUT <<EOP;
+print $out <<EOP;
 };
 
+/* reg_off_by_arg[] - Which argument holds the offset to the next node */
+
 static const char reg_off_by_arg[] = {
 EOP
 
 $ind = 0;
-while (++$ind <= $lastregop) {
+do {
   my $size = $longj[$ind] || 0;
 
-  printf OUT "\t%d,\t/* %*s */\n",
+  printf $out "\t%d,\t/* %*s */\n",
        $size, -$rwidth, $name[$ind]
-}
+} while (++$ind < $lastregop);
 
-print OUT <<EOP;
+print $out <<EOP;
 };
 
-#ifdef DEBUGGING
-const char * const reg_name[] = {
+#endif /* REG_COMP_C */
+
+/* reg_name[] - Opcode/state names in string form, for debugging */
+
+#ifndef DOINIT
+EXTCONST char * PL_reg_name[];
+#else
+EXTCONST char * const PL_reg_name[] = {
 EOP
 
 $ind = 0;
-while (++$ind <= $tot) {
+my $ofs = 0;
+my $sym = "";
+do {
   my $size = $longj[$ind] || 0;
 
-  printf OUT "\t%*s\t/* %#04x */\n",
-       -3-$width,qq("$name[$ind]",),$ind-1;
-  print OUT "\t/* ------------ States ------------- */\n"
-    if $ind == $lastregop and $lastregop != $tot;
-}
+  printf $out "\t%*s\t/* $sym%#04x */\n",
+       -3-$width,qq("$name[$ind]",), $ind - $ofs;
+  if ($ind + 1 == $lastregop and $lastregop != $tot) {
+    print $out "\t/* ------------ States ------------- */\n";
+    $ofs = $lastregop - 1;
+    $sym = 'REGNODE_MAX +';
+  }
+    
+} while (++$ind < $tot);
 
-print OUT <<EOP;
+print $out <<EOP;
 };
-#endif /* DEBUGGING */
+#endif /* DOINIT */
+
+/* PL_reg_extflags_name[] - Opcode/state names in string form, for debugging */
+
+#ifndef DOINIT
+EXTCONST char * PL_reg_extflags_name[];
 #else
-#ifdef DEBUGGING
-extern const char * const reg_name[];
-#endif
-#endif /* REG_COMP_C */
+EXTCONST char * const PL_reg_extflags_name[] = {
+EOP
+
+open my $fh,"<","regexp.h" or die "Can't read regexp.h: $!";
+my %rxfv;
+my $val = 0;
+my %reverse;
+while (<$fh>) {
+    if (/#define\s+(RXf_\w+)\s+(0x[A-F\d]+)/i) {
+       my $newval = eval $2;
+       if($val & $newval) {
+           die sprintf "Both $1 and $reverse{$newval} use %08X", $newval;
+       }
+        $val|=$newval;
+        $rxfv{$1}= $newval;
+       $reverse{$newval} = $1;
+    }
+}    
+my %vrxf=reverse %rxfv;
+printf $out "\t/* Bits in extflags defined: %032b */\n",$val;
+for (0..31) {
+    my $n=$vrxf{2**$_}||"UNUSED_BIT_$_";
+    $n=~s/^RXf_(PMf_)?//;
+    printf $out qq(\t%-20s/* 0x%08x */\n), 
+        qq("$n",),2**$_;
+}  
+print $out <<EOP;
+};
+#endif /* DOINIT */
 
-/* ex: set ro: */
 EOP
 
-close OUT or die "close $tmp_h: $!";
+print $out process_flags('V', 'varies', <<'EOC');
+/* The following have no fixed length. U8 so we can do strchr() on it. */
+EOC
+
+print $out process_flags('S', 'simple', <<'EOC');
+/* The following always have a length of 1. U8 we can do strchr() on it. */
+/* (Note that length 1 means "one character" under UTF8, not "one octet".) */
+EOC
+
+print $out <<EOP;
+/* ex: set ro: */
+EOP
+safer_close($out);
 
-safer_rename $tmp_h, 'regnodes.h';
+rename_if_different $tmp_h, 'regnodes.h';