1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
#!/usr/bin/perl
#
# Automatically produce some tables useful for a NASM major mode
#
use integer;
use strict;
use File::Spec;
my($outfile, $srcdir, $objdir) = @ARGV;
if (!defined($outfile)) {
die "Usage: $0 outfile srcdir objdir\n";
}
$srcdir = File::Spec->curdir() unless (defined($srcdir));
$objdir = $srcdir unless (defined($objdir));
my %tokens = ();
sub xpush($@) {
my $ref = shift @_;
$$ref = [] unless (defined($$ref));
return push(@$$ref, @_);
}
# Combine some specific token types
my %override = ( 'id' => 'special',
'float' => 'function',
'floatize' => 'function',
'strfunc' => 'function',
'ifunc' => 'function',
'insn' => 'instruction',
'reg' => 'register',
'seg' => 'special',
'wrt' => 'special' );
sub read_tokhash_c($) {
my($tokhash_c) = @_;
open(my $th, '<', $tokhash_c)
or die "$0:$tokhash_c: $!\n";
my $l;
my $tokendata = 0;
while (defined($l = <$th>)) {
if ($l =~ /\bstruct tokendata tokendata\[/) {
$tokendata = 1;
next;
} elsif (!$tokendata) {
next;
}
last if ($l =~ /\}\;/);
if ($l =~ /^\s*\{\s*\"(.*?)\",.*?,\s*TOKEN_(\w+),.*\}/) {
my $token = $1;
my $type = lc($2);
if ($override{$type}) {
$type = $override{$type};
} elsif ($token !~ /^\w/) {
$type = 'operator';
} elsif ($token =~ /^__\?masm_.*\?__$/) {
next;
}
xpush(\$tokens{$type}, $token);
if ($token =~ /^__\?(.*)\?__$/) {
# Also encode the "user" (macro) form without __?...?__
xpush(\$tokens{$type}, $1);
}
}
}
close($th);
}
sub read_pptok_c($) {
my($pptok_c) = @_;
open(my $pt, '<', $pptok_c)
or die "$0:$pptok_c: $!\n";
my $l;
my $pp_dir = 0;
while (defined($l = <$pt>)) {
if ($l =~ /\bpp_directives\[/) {
$pp_dir = 1;
next;
} elsif (!$pp_dir) {
next;
}
last if ($l =~ /\}\;/);
if ($l =~ /^\s*\"(.*?)\"/) {
xpush(\$tokens{'pp-directive'}, $1);
}
}
close($pt);
}
sub read_directiv_dat($) {
my($directiv_dat) = @_;
open(my $dd, '<', $directiv_dat)
or die "$0:$directiv_dat: $!\n";
my $l;
my $directiv = 0;
while (defined($l = <$dd>)) {
if ($l =~ /^\; ---.*?(pragma)?/) {
$directiv = ($1 ne 'pragma');
next;
} elsif (!$directiv) {
next;
}
if ($l =~ /^\s*(\w+)/) {
xpush(\$tokens{'directive'}, $1);
}
}
close($dd);
}
my $version;
sub read_version($) {
my($vfile) = @_;
open(my $v, '<', $vfile)
or die "$0:$vfile: $!\n";
$version = <$v>;
chomp $version;
close($v);
}
sub make_lines($$@) {
my $maxline = shift @_;
my $indent = shift @_;
# The first line isn't explicitly indented and the last line
# doesn't end in "\n"; assumed the surrounding formatter wants
# do control that
my $linepos = 0;
my $linewidth = $maxline - $indent;
my $line = '';
my @lines = ();
foreach my $w (@_) {
my $l = length($w);
if ($linepos > 0 && $linepos+$l+1 >= $linewidth) {
$line .= "\n" . (' ' x $indent);
push(@lines, $line);
$linepos = 0;
$line = '';
}
if ($linepos > 0) {
$line .= ' ';
$linepos++;
}
$line .= $w;
$linepos += $l;
}
if ($linepos > 0) {
push(@lines, $line);
}
return @lines;
}
sub quote_for_emacs(@) {
return map { s/[\\\"\']/\\$1/g; '"'.$_.'"' } @_;
}
sub write_output($) {
my($outfile) = @_;
open(my $out, '>', $outfile)
or die "$0:$outfile: $!\n";
my($vol,$dir,$file) = File::Spec->splitpath($outfile);
print $out ";;; ${file} --- lists of NASM assembler tokens\n";
print $out ";;;\n";
print $out ";;; This file contains list of tokens from the NASM x86\n";
print $out ";;; assembler, automatically extracted from NASM ${version}.\n";
print $out ";;;\n";
print $out ";;; This file is intended to be (require)d from a `nasm-mode\'\n";
print $out ";;; major mode definition.\n";
foreach my $type (sort keys(%tokens)) {
print $out "\n(defconst nasm-${type}\n";
print $out " \'(";
print $out make_lines(78, 4, quote_for_emacs(sort @{$tokens{$type}}));
print $out ")\n";
print $out " \"NASM ${version} ${type} tokens for `nasm-mode\'.\")\n";
}
close($out);
}
read_tokhash_c(File::Spec->catfile($objdir, 'asm', 'tokhash.c'));
read_pptok_c(File::Spec->catfile($objdir, 'asm', 'pptok.c'));
read_directiv_dat(File::Spec->catfile($srcdir, 'asm', 'directiv.dat'));
read_version(File::Spec->catfile($srcdir, 'version'));
write_output($outfile);
|