BR 3392597: the system malloc() can return NULL
[nasm.git] / x86 / regs.pl
blob31a3fb323b0b0272da202088db5f1590239722c1
1 #!/usr/bin/perl
2 ## --------------------------------------------------------------------------
3 ##
4 ## Copyright 1996-2009 The NASM Authors - All Rights Reserved
5 ## See the file AUTHORS included with the NASM distribution for
6 ## the specific copyright holders.
7 ##
8 ## Redistribution and use in source and binary forms, with or without
9 ## modification, are permitted provided that the following
10 ## conditions are met:
12 ## * Redistributions of source code must retain the above copyright
13 ## notice, this list of conditions and the following disclaimer.
14 ## * Redistributions in binary form must reproduce the above
15 ## copyright notice, this list of conditions and the following
16 ## disclaimer in the documentation and/or other materials provided
17 ## with the distribution.
18 ##
19 ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
20 ## CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
21 ## INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 ## DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 ## NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 ## LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 ## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 ## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 ## OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31 ## EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ## --------------------------------------------------------------------------
36 # Read regs.dat and output regs.h and regs.c (included in names.c)
39 $nline = 0;
41 sub toint($) {
42 my($v) = @_;
44 return ($v =~ /^0/) ? oct $v : $v+0;
47 sub process_line($) {
48 my($line) = @_;
49 my @v;
51 if ( $line !~ /^\s*(\S+)\s*(\S+)\s*(\S+)\s*([0-9]+)\s*(\S*)/i ) {
52 die "regs.dat:$nline: invalid input\n";
54 $reg = $1;
55 $aclass = $2;
56 $dclasses = $3;
57 $x86regno = toint($4);
59 if ($reg =~ /^(.*[^0-9])([0-9]+)\-([0-9]+)(|[^0-9].*)$/) {
60 $nregs = $3-$2+1;
61 $reg = $1.$2.$4;
62 $reg_nr = $2;
63 $reg_prefix = $1;
64 $reg_suffix = $4;
65 } else {
66 $nregs = 1;
67 undef $reg_prefix;
68 undef $reg_suffix;
71 while ($nregs--) {
72 $regs{$reg} = $aclass;
73 $regvals{$reg} = $x86regno;
75 foreach $dclass (split(/,/, $dclasses)) {
76 if ( !defined($disclass{$dclass}) ) {
77 $disclass{$dclass} = [];
80 $disclass{$dclass}->[$x86regno] = $reg;
83 # Compute the next register, if any
84 if (defined($reg_prefix)) {
85 $x86regno++;
86 $reg_nr++;
87 $reg = sprintf("%s%u%s", $reg_prefix, $reg_nr, $reg_suffix);
88 } else {
89 # Not a dashed sequence
90 die if ($nregs);
95 ($fmt, $file) = @ARGV;
97 %regs = ();
98 %regvals = ();
99 %disclass = ();
100 open(REGS, '<', $file) or die "$0: Cannot open $file\n";
101 while ( defined($line = <REGS>) ) {
102 $nline++;
104 chomp $line;
105 $line =~ s/\s*(\#.*|)$//;
107 next if ( $line eq '' );
109 process_line($line);
111 close(REGS);
113 if ( $fmt eq 'h' ) {
114 # Output regs.h
115 print "/* automatically generated from $file - do not edit */\n\n";
116 print "#ifndef NASM_REGS_H\n";
117 print "#define NASM_REGS_H\n\n";
119 $expr_regs = 1;
120 printf "#define EXPR_REG_START %d\n\n", $expr_regs;
121 print "enum reg_enum {\n";
122 # Unfortunately the code uses both 0 and -1 as "no register" in
123 # different places...
124 print " R_zero = 0,\n";
125 print " R_none = -1,\n";
126 $attach = ' = EXPR_REG_START'; # EXPR_REG_START == 1
127 foreach $reg ( sort(keys(%regs)) ) {
128 print " R_\U${reg}\E${attach},\n";
129 $attach = '';
130 $expr_regs++;
132 print " REG_ENUM_LIMIT\n";
133 print "};\n\n";
134 printf "#define EXPR_REG_END %d\n\n", $expr_regs-1;
135 foreach $reg ( sort(keys(%regs)) ) {
136 printf "#define %-15s %2d\n", "REG_NUM_\U${reg}", $regvals{$reg};
138 print "\n\n#endif /* NASM_REGS_H */\n";
139 } elsif ( $fmt eq 'c' ) {
140 # Output regs.c
141 print "/* automatically generated from $file - do not edit */\n\n";
142 print "#include \"tables.h\"\n\n";
143 print "const char * const nasm_reg_names[] = "; $ch = '{';
144 # This one has no dummy entry for 0
145 foreach $reg ( sort(keys(%regs)) ) {
146 print "$ch\n \"${reg}\"";
147 $ch = ',';
149 print "\n};\n";
150 } elsif ( $fmt eq 'fc' ) {
151 # Output regflags.c
152 print "/* automatically generated from $file - do not edit */\n\n";
153 print "#include \"tables.h\"\n";
154 print "#include \"nasm.h\"\n\n";
155 print "const opflags_t nasm_reg_flags[] = {\n";
156 printf " 0,\n"; # Dummy entry for 0
157 foreach $reg ( sort(keys(%regs)) ) {
158 # Print the class of the register
159 printf " %-15s /* %-5s */\n",
160 $regs{$reg}.',', $reg;
162 print "};\n";
163 } elsif ( $fmt eq 'vc' ) {
164 # Output regvals.c
165 print "/* automatically generated from $file - do not edit */\n\n";
166 print "#include \"tables.h\"\n\n";
167 print "const int nasm_regvals[] = {\n";
168 print " -1,\n"; # Dummy entry for 0
169 foreach $reg ( sort(keys(%regs)) ) {
170 # Print the x86 value of the register
171 printf " %2d, /* %-5s */\n", $regvals{$reg}, $reg;
173 print "};\n";
174 } elsif ( $fmt eq 'dc' ) {
175 # Output regdis.c
176 print "/* automatically generated from $file - do not edit */\n\n";
177 print "#include \"regdis.h\"\n\n";
178 foreach $class ( sort(keys(%disclass)) ) {
179 printf "const enum reg_enum nasm_rd_%-8s[%2d] = {",
180 $class, scalar @{$disclass{$class}};
181 @foo = @{$disclass{$class}};
182 @bar = ();
183 for ( $i = 0 ; $i < scalar(@foo) ; $i++ ) {
184 if (defined($foo[$i])) {
185 push(@bar, "R_\U$foo[$i]\E");
186 } else {
187 die "$0: No register name for class $class, value $i\n";
190 print join(',', @bar), "};\n";
192 } elsif ( $fmt eq 'dh' ) {
193 # Output regdis.h
194 print "/* automatically generated from $file - do not edit */\n\n";
195 print "#ifndef NASM_REGDIS_H\n";
196 print "#define NASM_REGDIS_H\n\n";
197 print "#include \"regs.h\"\n\n";
198 foreach $class ( sort(keys(%disclass)) ) {
199 printf "extern const enum reg_enum nasm_rd_%-8s[%2d];\n",
200 $class, scalar @{$disclass{$class}};
202 print "\n#endif /* NASM_REGDIS_H */\n";
203 } else {
204 die "$0: Unknown output format\n";