Fixed 64-bit offset generation.
[nasm/nasm.git] / regs.pl
blob0910a1fe6c12c893384faede73de592e78ec2931
1 #!/usr/bin/perl
2 # $Id$
4 # Read regs.dat and output regs.h and regs.c (included in names.c)
7 $nline = 0;
9 sub toint($) {
10 my($v) = @_;
12 return ($v =~ /^0/) ? oct $v : $v+0;
15 sub process_line($) {
16 my($line) = @_;
17 my @v;
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";
22 $reg = $1;
23 $aclass = $2;
24 $dclasses = $3;
25 $regval = toint($4);
26 $x86regno = toint($5);
28 if ($reg =~ /[0-9]\+$/) {
29 $nregs = 8;
30 $reg =~ s/\+$//;
31 } else {
32 $nregs = 1;
35 while ($nregs--) {
36 $regs{$reg} = $aclass;
37 $regvals{$reg} = $regval;
39 foreach $dclass (split(/,/, $dclasses)) {
40 if ( !defined($disclass{$dclass}) ) {
41 $disclass{$dclass} = [];
44 $disclass{$dclass}->[$x86regno] = $reg;
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);
56 ($fmt, $file) = @ARGV;
58 %regs = ();
59 %regvals = ();
60 %disclass = ();
61 open(REGS, "< ${file}") or die "$0: Cannot open $file\n";
62 while ( defined($line = <REGS>) ) {
63 $nline++;
65 chomp $line;
66 $line =~ s/\s*(\#.*|)$//;
68 next if ( $line eq '' );
70 process_line($line);
72 close(REGS);
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 = ',';
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";
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 = ',';
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
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
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 rd_%-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";
131 print join(',', @bar), "};\n";
133 } else {
134 die "$0: Unknown output format\n";