Clean up the 64-bitification of regs.dat for 64-bit ndisasm support
[platform/upstream/nasm.git] / regs.pl
1 #!/usr/bin/perl
2 # $Id$
3 #
4 # Read regs.dat and output regs.h and regs.c (included in names.c)
5 #
6
7 $nline = 0;
8
9 sub toint($) {
10     my($v) = @_;
11
12     return ($v =~ /^0/) ? oct $v : $v+0;
13 }
14
15 sub process_line($) {
16     my($line) = @_;
17     my @v;
18
19     if ( $line !~ /^\s*(\S+)\s*(\S+)\s*(\S+)\s*([1-9][0-9]+|0[0-7]+|0x[0-9a-f]+)\s*([0-9]+)$/i ) {
20         die "regs.dat:$nline: invalid input\n";
21     }
22     $reg      = $1;
23     $aclass   = $2;
24     $dclasses = $3;
25     $regval   = toint($4);
26     $x86regno = toint($5);
27
28     if ($reg =~ /\*$/) {
29         $nregs = 8;
30         $reg =~ s/\*$//;
31     } else {
32         $nregs = 1;
33     }
34
35     while ($nregs--) {
36         $regs{$reg} = $aclass;
37         $regvals{$reg} = $regval;
38
39         foreach $dclass (split(/,/, $dclasses)) {
40             if ( !defined($disclass{$dclass}) ) {
41                 $disclass{$dclass} = [];
42             }
43             
44             $disclass{$dclass}->[$x86regno] = $reg;
45         }
46
47         # Compute the next register, if any
48         $regval++;
49         $x86regno++;
50         if ($reg =~ /^(|.*[^0-9])([0-9]+)$/) {
51             $reg = sprintf("%s%u", $1, $2+1);
52         }
53     }
54 }
55
56 ($fmt, $file) = @ARGV;
57
58 %regs = ();
59 %regvals = ();
60 %disclass = ();
61 open(REGS, "< ${file}") or die "$0: Cannot open $file\n";
62 while ( defined($line = <REGS>) ) {
63     $nline++;
64
65     chomp $line;
66     $line =~ s/\s*(\#.*|)$//;
67     
68     next if ( $line eq '' );
69
70     process_line($line);
71 }
72 close(REGS);
73
74 if ( $fmt eq 'h' ) {
75     # Output regs.h
76     print "/* automatically generated from $file - do not edit */\n";
77     print "enum reg_enum {\n";
78     $attach = ' = EXPR_REG_START'; # EXPR_REG_START == 1
79     foreach $reg ( sort(keys(%regs)) ) {
80         print "    R_\U${reg}\E${attach},\n";
81         $attach = ''; $ch = ',';
82     }
83     print "    REG_ENUM_LIMIT\n";
84     print "};\n\n";
85     foreach $reg ( sort(keys(%regs)) ) {
86         print "#define REG_NUM_\U${reg}     $regvals{$reg}\n";
87     }
88     print "\n";
89 } elsif ( $fmt eq 'c' ) {
90     # Output regs.c
91     print "/* automatically generated from $file - do not edit */\n";
92     print "static const char *reg_names[] = "; $ch = '{';
93     # This one has no dummy entry for 0
94     foreach $reg ( sort(keys(%regs)) ) {
95         print "$ch\n    \"${reg}\"";
96         $ch = ',';
97     }
98     print "\n};\n";
99 } elsif ( $fmt eq 'fc' ) {
100     # Output regflags.c
101     print "/* automatically generated from $file - do not edit */\n";
102     print "static const int32_t reg_flags[] = {\n";
103     print "    0";              # Dummy entry for 0
104     foreach $reg ( sort(keys(%regs)) ) {
105         print ",\n    ", $regs{$reg}; # Print the class of the register
106     }
107     print "\n};\n";
108 } elsif ( $fmt eq 'vc' ) {
109     # Output regvals.c
110     print "/* automatically generated from $file - do not edit */\n";
111     print "static const int regvals[] = {\n";
112     print "    -1";             # Dummy entry for 0
113     foreach $reg ( sort(keys(%regs)) ) {
114         printf ",\n    0%03o", $regvals{$reg}; # Print the regval of the register
115     }
116     print "\n};\n";
117 } elsif ( $fmt eq 'dc' ) {
118     # Output regdis.c
119     print "/* automatically generated from $file - do not edit */\n";
120     foreach $class ( sort(keys(%disclass)) ) {
121         printf "static const int %-8s[] = {", $class;
122         @foo = @{$disclass{$class}};
123         @bar = ();
124         for ( $i = 0 ; $i < scalar(@foo) ; $i++ ) {
125             if (defined($foo[$i])) {
126                 push(@bar, "R_\U$foo[$i]\E");
127             } else {
128                 die "$0: No register name for class $class, value $i\n";
129             }
130         }
131         print join(',', @bar), "};\n";
132     }
133 } else {
134     die "$0: Unknown output format\n";
135 }