| 1 |
#!/usr/bin/perl |
| 2 |
# SPDX-License-Identifier: BSD-2-Clause |
| 3 |
# Copyright 1996-2025 The NASM Authors - All Rights Reserved |
| 4 |
|
| 5 |
# |
| 6 |
# Read regs.dat and output regs.h and regs.c (included in names.c) |
| 7 |
# |
| 8 |
|
| 9 |
use integer; |
| 10 |
|
| 11 |
# For simplicity make all the disassembly tables the same size |
| 12 |
# This must be power of 2! |
| 13 |
my $distablesz = 32; |
| 14 |
|
| 15 |
die if ($distablesz & ($distablesz-1)); # Not a power of 2? |
| 16 |
|
| 17 |
$nline = 0; |
| 18 |
|
| 19 |
sub toint($) { |
| 20 |
my($v) = @_; |
| 21 |
|
| 22 |
return ($v =~ /^0/) ? oct $v : $v+0; |
| 23 |
} |
| 24 |
|
| 25 |
sub process_line($) { |
| 26 |
my($line) = @_; |
| 27 |
|
| 28 |
if ( $line !~ /^\s*(\S+)\s*(\S+)\s*(\S+)\s*([0-9]+)\s*(?:\&\s*(0x[0-9a-f]+|[0-9]+))?/i ) { |
| 29 |
die "regs.dat:$nline: invalid input\n"; |
| 30 |
} |
| 31 |
$reg = $1; |
| 32 |
$aclass = $2; |
| 33 |
$dclasses = $3; |
| 34 |
$x86regno = toint($4); |
| 35 |
$regmask = toint($5); |
| 36 |
|
| 37 |
if ($reg =~ /^(.*[^0-9])([0-9]+)\-([0-9]+)(|[^0-9].*)$/) { |
| 38 |
$nregs = $3-$2+1; |
| 39 |
$reg = $1.$2.$4; |
| 40 |
$reg_nr = $2; |
| 41 |
$reg_prefix = $1; |
| 42 |
$reg_suffix = $4; |
| 43 |
} else { |
| 44 |
$nregs = 1; |
| 45 |
undef $reg_prefix; |
| 46 |
undef $reg_suffix; |
| 47 |
} |
| 48 |
|
| 49 |
foreach $dclass (split(/,/, $dclasses)) { |
| 50 |
if ( !defined($disclass{$dclass}) ) { |
| 51 |
$disclass{$dclass} = []; |
| 52 |
} |
| 53 |
if ($regmask) { |
| 54 |
if ($regmasks{$dclass} && $regmasks{$dclass} != $regmask) { |
| 55 |
die "regs.dat:$nline: inconsistent regmasks for disassembly class $dclass\n"; |
| 56 |
} |
| 57 |
$regmasks{$dclass} = $regmask; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
while ($nregs--) { |
| 62 |
$regs{$reg} = $aclass; |
| 63 |
$regvals{$reg} = $x86regno; |
| 64 |
|
| 65 |
if ($x86regno >= $distablesz) { |
| 66 |
die "regs.dat:$nline: register number $x86regno too large, increase distablesz in regs.pl\n"; |
| 67 |
} |
| 68 |
|
| 69 |
foreach $dclass (split(/,/, $dclasses)) { |
| 70 |
$disclass{$dclass}->[$x86regno] = $reg; |
| 71 |
} |
| 72 |
|
| 73 |
# Compute the next register, if any |
| 74 |
if (defined($reg_prefix)) { |
| 75 |
$x86regno++; |
| 76 |
$reg_nr++; |
| 77 |
$reg = sprintf("%s%u%s", $reg_prefix, $reg_nr, $reg_suffix); |
| 78 |
} else { |
| 79 |
# Not a dashed sequence |
| 80 |
die if ($nregs); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
($fmt, $file) = @ARGV; |
| 86 |
|
| 87 |
%regs = (); |
| 88 |
%regvals = (); |
| 89 |
%disclass = (); |
| 90 |
%regmasks = (); |
| 91 |
open(REGS, '<', $file) or die "$0: Cannot open $file\n"; |
| 92 |
while ( defined($line = <REGS>) ) { |
| 93 |
$nline++; |
| 94 |
|
| 95 |
chomp $line; |
| 96 |
$line =~ s/\s*(\#.*|)$//; |
| 97 |
|
| 98 |
next if ( $line eq '' ); |
| 99 |
|
| 100 |
process_line($line); |
| 101 |
} |
| 102 |
close(REGS); |
| 103 |
|
| 104 |
if ( $fmt eq 'h' ) { |
| 105 |
# Output regs.h |
| 106 |
print "/* automatically generated from $file - do not edit */\n\n"; |
| 107 |
print "#ifndef NASM_REGS_H\n"; |
| 108 |
print "#define NASM_REGS_H\n\n"; |
| 109 |
|
| 110 |
$expr_regs = 1; |
| 111 |
printf "#define EXPR_REG_START %d\n\n", $expr_regs; |
| 112 |
print "enum reg_enum {\n"; |
| 113 |
# Unfortunately the code uses both 0 and -1 as "no register" in |
| 114 |
# different places... |
| 115 |
print " R_zero = 0,\n"; |
| 116 |
print " R_none = -1,\n"; |
| 117 |
$attach = ' = EXPR_REG_START'; # EXPR_REG_START == 1 |
| 118 |
foreach $reg ( sort(keys(%regs)) ) { |
| 119 |
print " R_\U${reg}\E${attach},\n"; |
| 120 |
$attach = ''; |
| 121 |
$expr_regs++; |
| 122 |
} |
| 123 |
print " REG_ENUM_LIMIT\n"; |
| 124 |
print "};\n\n"; |
| 125 |
printf "#define EXPR_REG_END %d\n\n", $expr_regs-1; |
| 126 |
foreach $reg ( sort(keys(%regs)) ) { |
| 127 |
printf "#define %-15s %2d\n", "REG_NUM_\U${reg}", $regvals{$reg}; |
| 128 |
} |
| 129 |
print "\n\n#endif /* NASM_REGS_H */\n"; |
| 130 |
} elsif ( $fmt eq 'c' ) { |
| 131 |
# Output regs.c |
| 132 |
print "/* automatically generated from $file - do not edit */\n\n"; |
| 133 |
print "#include \"tables.h\"\n\n"; |
| 134 |
print "const char * const nasm_reg_names[] = "; $ch = '{'; |
| 135 |
# This one has no dummy entry for 0 |
| 136 |
foreach $reg ( sort(keys(%regs)) ) { |
| 137 |
print "$ch\n \"${reg}\""; |
| 138 |
$ch = ','; |
| 139 |
} |
| 140 |
print "\n};\n"; |
| 141 |
} elsif ( $fmt eq 'fc' ) { |
| 142 |
# Output regflags.c |
| 143 |
print "/* automatically generated from $file - do not edit */\n\n"; |
| 144 |
print "#include \"tables.h\"\n"; |
| 145 |
print "#include \"nasm.h\"\n\n"; |
| 146 |
print "const opflags_t nasm_reg_flags[] = {\n"; |
| 147 |
printf " 0,\n"; # Dummy entry for 0 |
| 148 |
foreach $reg ( sort(keys(%regs)) ) { |
| 149 |
# Print the class of the register |
| 150 |
printf " /* %-5s */ RN_FLAGS(%2d) | %s,\n", |
| 151 |
$reg, $regvals{$reg}, $regs{$reg}; |
| 152 |
} |
| 153 |
print "};\n"; |
| 154 |
} elsif ( $fmt eq 'vc' ) { |
| 155 |
# Output regvals.c |
| 156 |
print "/* automatically generated from $file - do not edit */\n\n"; |
| 157 |
print "#include \"tables.h\"\n\n"; |
| 158 |
print "const int nasm_regvals[] = {\n"; |
| 159 |
print " -1,\n"; # Dummy entry for 0 |
| 160 |
foreach $reg ( sort(keys(%regs)) ) { |
| 161 |
# Print the x86 value of the register |
| 162 |
printf " %2d, /* %-5s */\n", $regvals{$reg}, $reg; |
| 163 |
} |
| 164 |
print "};\n"; |
| 165 |
} elsif ( $fmt eq 'dc' ) { |
| 166 |
# Output regdis.c |
| 167 |
print "/* automatically generated from $file - do not edit */\n\n"; |
| 168 |
print "#include \"regdis.h\"\n\n"; |
| 169 |
foreach $class ( sort(keys(%disclass)) ) { |
| 170 |
die if (scalar @{$disclass{$class}} > $distablesz); |
| 171 |
$regmask = $regmasks{$class} || ($distablesz - 1); |
| 172 |
printf "const enum reg_enum nasm_rd_%s[DISREGTBLSZ] = {", |
| 173 |
$class; |
| 174 |
for ($i = 0; $i < $distablesz; $i++) { |
| 175 |
my $r = $disclass{$class}->[$i & $regmask]; |
| 176 |
print "\n " if (!($i & 7)); |
| 177 |
print ' ', defined($r) ? "R_\U$r" : '0'; |
| 178 |
print ',' unless ($i == $distablesz - 1); |
| 179 |
} |
| 180 |
print "\n};\n"; |
| 181 |
} |
| 182 |
} elsif ( $fmt eq 'dh' ) { |
| 183 |
# Output regdis.h |
| 184 |
print "/* automatically generated from $file - do not edit */\n\n"; |
| 185 |
print "#ifndef NASM_REGDIS_H\n"; |
| 186 |
print "#define NASM_REGDIS_H\n\n"; |
| 187 |
print "#include \"regs.h\"\n\n"; |
| 188 |
printf "#define DISREGTBLSZ %d\n\n", $distablesz; |
| 189 |
foreach $class ( sort(keys(%disclass)) ) { |
| 190 |
printf "extern const enum reg_enum nasm_rd_%s[DISREGTBLSZ];\n", |
| 191 |
$class; |
| 192 |
} |
| 193 |
print "\n#endif /* NASM_REGDIS_H */\n"; |
| 194 |
} else { |
| 195 |
die "$0: Unknown output format\n"; |
| 196 |
} |