Debian Codegraph @

editors/nasmtok.pl

raw
1 #!/usr/bin/perl
2 #
3 # Automatically produce some tables useful for a NASM major mode
4 #
5
6 use integer;
7 use strict;
8 use File::Spec;
9 use File::Find;
10
11 my $format = 'el';
12
13 if ($ARGV[0] =~ /^-(\S+)$/) {
14 $format = $1;
15 shift @ARGV;
16 }
17
18 my($outfile, $srcdir, $objdir) = @ARGV;
19
20 if (!defined($outfile)) {
21 die "Usage: $0 [-format] outfile srcdir objdir\n";
22 }
23
24 my @vpath;
25
26 $srcdir = $srcdir || File::Spec->curdir();
27 $objdir = $objdir || $srcdir;
28 push(@vpath, $objdir) if ($objdir ne $srcdir);
29 push(@vpath, $srcdir);
30
31 my %tokens = (); # Token lists per category
32 my %token_category = (); # Tokens to category map
33
34 sub xpush($@) {
35 my $ref = shift @_;
36
37 $$ref = [] unless (defined($$ref));
38 return push(@$$ref, @_);
39 }
40
41 # Search for a file, and return a file handle if successfully opened
42 sub open_vpath($$) {
43 my($mode, $file) = @_;
44 my %tried;
45
46 # For simplicity, allow filenames to be specified
47 # with Unix / syntax internally
48 $file = File::Spec->catfile(split(/\//, $file));
49
50 foreach my $d (@vpath) {
51 my $fn = File::Spec->catfile($d, $file);
52 next if ($tried{$fn});
53 $tried{$fn}++;
54 my $fh;
55 return $fh if (open($fh, $mode, $fn));
56 }
57 return undef;
58 }
59
60 sub must_open($) {
61 my($file) = @_;
62 my $fh = open_vpath('<', $file);
63 return $fh if (defined($fh));
64 die "$0:$file: $!\n";
65 }
66
67 # Combine some specific token types
68 my %override = (
69 'brcconst' => 'special-constant',
70 'id' => 'special',
71 'float' => 'function',
72 'floatize' => 'function',
73 'strfunc' => 'function',
74 'ifunc' => 'function',
75 'insn' => 'instruction',
76 'reg' => 'register',
77 'seg' => 'special',
78 'wrt' => 'special',
79 'times' => 'special');
80
81 sub addtoken($@) {
82 my $type = shift @_;
83
84 foreach my $token (@_) {
85 unless (defined($token_category{$token})) {
86 $type = $override{$type} if (defined($override{$type}));
87 xpush(\$tokens{$type}, $token);
88 $token_category{$token} = $type;
89 }
90 }
91 }
92
93 sub read_tokhash_c($) {
94 my($tokhash_c) = @_;
95
96 my $th = must_open($tokhash_c);
97
98 my $l;
99 my $tokendata = 0;
100 while (defined($l = <$th>)) {
101 if ($l =~ /\bstruct tokendata tokendata\[/) {
102 $tokendata = 1;
103 next;
104 } elsif (!$tokendata) {
105 next;
106 }
107
108 last if ($l =~ /\}\;/);
109
110 if ($l =~ /^\s*\{\s*\"(.*?)\",.*?,\s*TOKEN_(\w+),(.*)\}/) {
111 my $token = $1;
112 my $type = lc($2);
113 my $flags = $3;
114
115 $token = "{${token}}" if ($flags =~ /\bTFLAG_BRC\b/);
116
117 # Parametric token: omit the actual parameter(s)
118 $token =~ s/^(\{[\w-]+=).+(\})$/$1$2/;
119
120 if ($token !~ /^(\{[\w-]+=?\}|\w+)$/) {
121 $type = 'operator';
122 } elsif ($token =~ /^__\?masm_.*\?__$/) {
123 next;
124 }
125 addtoken($type, $token);
126 if ($token =~ /^__\?(.*)\?__$/) {
127 # Also encode the "user" (macro) form without __?...?__
128 addtoken($type, $1);
129 }
130 }
131 }
132 close($th);
133 }
134
135 sub read_pptok_c($) {
136 my($pptok_c) = @_;
137
138 my $pt = must_open($pptok_c);
139
140 my $l;
141 my $pp_dir = 0;
142
143 while (defined($l = <$pt>)) {
144 if ($l =~ /\bpp_directives\[/) {
145 $pp_dir = 1;
146 next;
147 } elsif (!$pp_dir) {
148 next;
149 }
150
151 last if ($l =~ /\}\;/);
152
153 if ($l =~ /^\s*\"(.*?)\"/) {
154 addtoken('pp-directive', $1);
155 }
156 }
157 close($pt);
158 }
159
160 sub read_directiv_dat($) {
161 my($directiv_dat) = @_;
162
163 my $dd = must_open($directiv_dat);
164
165 my $l;
166 my $directiv = 0;
167
168 while (defined($l = <$dd>)) {
169 if ($l =~ /^\; ---.*?(pragma)?/) {
170 $directiv = ($1 ne 'pragma');
171 next;
172 } elsif (!$directiv) {
173 next;
174 }
175
176 if ($l =~ /^\s*(\w+)/) {
177 addtoken('directive', $1);
178 }
179 }
180
181 close($dd);
182 }
183
184 my %version;
185 sub read_version($) {
186 my($vfile) = @_;
187 my $v = must_open($vfile);
188
189 while (defined(my $vl = <$v>)) {
190 if ($vl =~ /^NASM_(\w+)=(\S+)\s*$/) {
191 $version{lc($1)} = $2;
192 }
193 }
194 close($v);
195 }
196
197 # This is called from the directory search in read_macros(), so
198 # don't use must_open() here.
199 sub read_macro_file($) {
200 my($file) = @_;
201
202 open(my $fh, '<', $file) or die "$0:$file: $!\n";
203 while (defined(my $l = <$fh>)) {
204 next unless ($l =~ /^\s*\%/);
205 my @f = split(/\s+/, $l);
206 next unless (scalar(@f) >= 2);
207 next if ($f[1] =~ /^[\%\$][^\(]+$/); # Internal use only
208 $f[1] =~ s/\(.*$//; # Strip argument list if any
209 $f[1] = lc($f[1]) if ($f[0] =~ /^\%i/);
210 if ($f[0] =~ /^\%(i)?(assign|defalias|define|defstr|substr|xdefine)\b/) {
211 addtoken('smacro', $f[1]);
212 } elsif ($f[0] =~ /^\%i?macro$/) {
213 addtoken('mmacro', $f[1]);
214 }
215 }
216 close($fh);
217 }
218
219 sub read_macros(@) {
220 my %visited;
221 my @dirs = (File::Spec->curdir(), qw(macros output editors));
222 @dirs = map { my $od = $_; map { File::Spec->catdir($od, $_) } @dirs } @_;
223 foreach my $dir (@dirs) {
224 next if ($visited{$dir});
225 $visited{$dir}++;
226 next unless opendir(my $dh, $dir);
227 while (defined(my $fn = readdir($dh))) {
228 next unless ($fn =~ /\.mac$/i);
229 read_macro_file(File::Spec->catfile($dir, $fn));
230 }
231 closedir($dh);
232 }
233 }
234
235 # Handle special tokens which may not have been picked up by the automatic
236 # process, because they depend on the build parameters, or are buried
237 # deep in C code...
238 sub add_special_cases() {
239 # Not defined in non-snapshot builds
240 addtoken('smacro', '__NASM_SNAPSHOT__', '__?NASM_SNAPSHOT?__');
241 }
242
243 sub make_lines($$@) {
244 my $maxline = shift @_;
245 my $indent = shift @_;
246
247 # The first line isn't explicitly indented and the last line
248 # doesn't end in "\n"; assumed the surrounding formatter wants
249 # do control that
250 my $linepos = 0;
251 my $linewidth = $maxline - $indent;
252
253 my $line = '';
254 my @lines = ();
255
256 foreach my $w (@_) {
257 my $l = length($w);
258
259 if ($linepos > 0 && $linepos+$l+1 >= $linewidth) {
260 $line .= "\n" . (' ' x $indent);
261 push(@lines, $line);
262 $linepos = 0;
263 $line = '';
264 }
265 if ($linepos > 0) {
266 $line .= ' ';
267 $linepos++;
268 }
269 $line .= $w;
270 $linepos += $l;
271 }
272
273 if ($linepos > 0) {
274 push(@lines, $line);
275 }
276
277 return @lines;
278 }
279
280 sub quote_for_emacs(@) {
281 return map { s/[\\\"\']/\\$1/g; '"'.$_.'"' } @_;
282 }
283
284 # Emacs LISP
285 sub write_output_el {
286 my($out, $outfile, $file) = @_;
287 my $whoami = 'NASM '.$version{'ver'};
288
289 print $out ";;; ${file} --- lists of NASM assembler tokens\n\n";
290 print $out ";;; Commentary:\n\n";
291 print $out ";; This file contains list of tokens from the NASM x86\n";
292 print $out ";; assembler, automatically extracted from ${whoami}.\n";
293 print $out ";;\n";
294 print $out ";; This file is intended to be (require)d from a `nasm-mode\'\n";
295 print $out ";; major mode definition.\n";
296 print $out ";;\n";
297 print $out ";; Tokens that are only recognized inside curly braces are\n";
298 print $out ";; noted as such. Tokens of the form {xxx=} are parametric\n";
299 print $out ";; tokens, where the token may contain additional text on\n";
300 print $out ";; the right side of the = sign. For example,\n";
301 print $out ";; {dfv=} should be matched by {dfv=cf,zf}.\n";
302 print $out "\n";
303 print $out ";;; Code:\n";
304
305 my @types = sort keys(%tokens);
306
307 # Write the individual token type lists
308 foreach my $type (sort keys(%tokens)) {
309 print $out "\n(defconst nasm-${type}\n";
310 print $out " \'(";
311
312 print $out make_lines(78, 4, quote_for_emacs(sort @{$tokens{$type}}));
313 print $out ")\n";
314 print $out " \"${whoami} ${type} tokens for `nasm-mode\'.\")\n";
315 }
316
317 # Generate a list of all the token type lists.
318 print $out "\n(defconst nasm-token-lists\n";
319 print $out " \'(";
320 print $out make_lines(78, 4, map { "'nasm-$_" } sort keys(%tokens));
321 print $out ")\n";
322 print $out " \"List of all ${whoami} token type lists.\")\n";
323
324 # The NASM token extracted version
325 printf $out "\n(defconst nasm-token-version %s\n",
326 quote_for_emacs($version{'ver'});
327 print $out " \"Version of NASM from which tokens were extracted,\n";
328 print $out "as a human-readable string.\")\n";
329
330 printf $out "\n(defconst nasm-token-version-id #x%08x\n",
331 $version{'version_id'};
332 print $out " \"Version of NASM from which tokens were extracted,\n";
333 print $out "as numeric identifier, for comparisons. Equivalent to the\n";
334 print $out "__?NASM_VERSION_ID?__ NASM macro value.\")\n";
335
336 printf $out "\n(defconst nasm-token-version-snapshot %s\n",
337 $version{'snapshot'} || 'nil';
338 print $out " \"Daily NASM snapshot build from which tokens were extracted,\n";
339 print $out "as a decimal number in YYYYMMDD format, or nil if not a\n";
340 print $out "daily snapshot build.\")\n";
341
342 # Footer
343 print $out "\n(provide 'nasmtok)\n";
344 print $out ";;; ${file} ends here\n";
345
346 return 0;
347 }
348
349 # JSON
350 sub write_output_json {
351 use JSON;
352
353 my($out, $outfile, $file) = @_;
354 my $whoami = 'NASM '.$version{'ver'};
355
356
357 my $json = JSON->new;
358 $json = $json->ascii(1)->canonical(1);
359
360 my %ver;
361 foreach my $vn (keys(%version)) {
362 my $vv = $version{$vn};
363 next if ($vn eq 'version_xid');
364 $vn =~ s/_ver$//;
365 $vn =~ s/^version_//;
366 $vv = $vv + 0 if ($vn ne 'ver');
367 $ver{$vn} = $vv;
368 }
369
370 print $out $json->encode({
371 '$comment' => "NASM syntax information extracted from ${whoami}",
372 'tokens' => \%tokens, 'version' => \%ver});
373 print $out "\n";
374 return 0;
375 }
376
377 sub write_output($$) {
378 my($format, $outfile) = @_;
379 my %formats = (
380 'el' => \&write_output_el,
381 'json' => \&write_output_json
382 );
383
384 my $outfunc = $formats{$format};
385 if (!defined($outfunc)) {
386 die "$0: unknown output format: $format\n";
387 }
388
389 open(my $out, '>', $outfile)
390 or die "$0:$outfile: $!\n";
391
392 my($vol,$dir,$file) = File::Spec->splitpath($outfile);
393
394 my $err = $outfunc->($out, $outfile, $file);
395 close($out);
396
397 if ($err) {
398 unlink($outfile);
399 die "$0:$outfile: error writing output\n";
400 }
401 }
402
403 add_special_cases();
404 read_tokhash_c('asm/tokhash.c');
405 read_pptok_c('asm/pptok.c');
406 read_directiv_dat('asm/directiv.dat');
407 read_version('version.mak');
408 read_macros(@vpath);
409 write_output($format, $outfile);