Avoid magic values; we have more than 124 registers now
[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*([0-9]+)$/i ) {
20         die "regs.dat:$nline: invalid input\n";
21     }
22     $reg      = $1;
23     $aclass   = $2;
24     $dclasses = $3;
25     $x86regno = toint($4);
26
27     if ($reg =~ /^(.*[^0-9])([0-9]+)\-([0-9]+)$/) {
28         $nregs = $3-$2+1;
29         $reg = $1.$2;
30     } else {
31         $nregs = 1;
32     }
33
34     while ($nregs--) {
35         $regs{$reg} = $aclass;
36         $regvals{$reg} = $x86regno;
37
38         foreach $dclass (split(/,/, $dclasses)) {
39             if ( !defined($disclass{$dclass}) ) {
40                 $disclass{$dclass} = [];
41             }
42             
43             $disclass{$dclass}->[$x86regno] = $reg;
44         }
45
46         # Compute the next register, if any
47         $x86regno++;
48         if ($reg =~ /^(.*[^0-9])([0-9]+)$/) {
49             $reg = sprintf("%s%u", $1, $2+1);
50         }
51     }
52 }
53
54 ($fmt, $file) = @ARGV;
55
56 %regs = ();
57 %regvals = ();
58 %disclass = ();
59 open(REGS, "< ${file}") or die "$0: Cannot open $file\n";
60 while ( defined($line = <REGS>) ) {
61     $nline++;
62
63     chomp $line;
64     $line =~ s/\s*(\#.*|)$//;
65     
66     next if ( $line eq '' );
67
68     process_line($line);
69 }
70 close(REGS);
71
72 if ( $fmt eq 'h' ) {
73     # Output regs.h
74     print "/* automatically generated from $file - do not edit */\n";
75     $expr_regs = 1;
76     printf "#define EXPR_REG_START %d\n", $expr_regs;
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         $expr_regs++;
83     }
84     print "    REG_ENUM_LIMIT\n";
85     print "};\n\n";
86     printf "#define EXPR_REG_END %d\n", $expr_regs-1;
87     foreach $reg ( sort(keys(%regs)) ) {
88         printf "#define %-15s %2d\n", "REG_NUM_\U${reg}", $regvals{$reg};
89     }
90     print "\n";
91 } elsif ( $fmt eq 'c' ) {
92     # Output regs.c
93     print "/* automatically generated from $file - do not edit */\n";
94     print "static const char *reg_names[] = "; $ch = '{';
95     # This one has no dummy entry for 0
96     foreach $reg ( sort(keys(%regs)) ) {
97         print "$ch\n    \"${reg}\"";
98         $ch = ',';
99     }
100     print "\n};\n";
101 } elsif ( $fmt eq 'fc' ) {
102     # Output regflags.c
103     print "/* automatically generated from $file - do not edit */\n";
104     print "static const int32_t reg_flags[] = {\n";
105     print "    0";              # Dummy entry for 0
106     foreach $reg ( sort(keys(%regs)) ) {
107         print ",\n    ", $regs{$reg}; # Print the class of the register
108     }
109     print "\n};\n";
110 } elsif ( $fmt eq 'vc' ) {
111     # Output regvals.c
112     print "/* automatically generated from $file - do not edit */\n";
113     print "static const int regvals[] = {\n";
114     print "    -1";             # Dummy entry for 0
115     foreach $reg ( sort(keys(%regs)) ) {
116         printf ",\n    %2d", $regvals{$reg}; # Print the regval of the register
117     }
118     print "\n};\n";
119 } elsif ( $fmt eq 'dc' ) {
120     # Output regdis.c
121     print "/* automatically generated from $file - do not edit */\n";
122     foreach $class ( sort(keys(%disclass)) ) {
123         printf "static const int rd_%-8s[] = {", $class;
124         @foo = @{$disclass{$class}};
125         @bar = ();
126         for ( $i = 0 ; $i < scalar(@foo) ; $i++ ) {
127             if (defined($foo[$i])) {
128                 push(@bar, "R_\U$foo[$i]\E");
129             } else {
130                 die "$0: No register name for class $class, value $i\n";
131             }
132         }
133         print join(',', @bar), "};\n";
134     }
135 } else {
136     die "$0: Unknown output format\n";
137 }