Debian Codegraph @

texmf-dist/doc/latex/register/Reg_macro.pm

raw
1 ###############################################################################
2 #
3 # File: Reg_macro.pm
4 # Description: Module for parsing register macros from LaTeX documentation
5 # Author: Matthew Lovell
6 # Created: Mon Mar 31 16:50:05 2003
7 # Modified: Sat May 19 09:33:20 2018
8 # Language: CPerl
9 #
10 # (C) Copyright 2003, Matthew Lovell, all rights reserved.
11 #
12 ###############################################################################
13
14 =pod
15
16 =head1 NAME
17
18 Reg_macro.pm
19
20 =head1 SYNOPSIS
21
22 Parses register/CSR information from LaTeX files.
23
24 =head1 DESCRIPTION
25
26 After searching through a files input/include tree, this module will search
27 for all \begin{register} macros. All available information will be parsed
28 out of each encountered macro. Parsed information includes:
29
30 register name
31 function
32 offset
33 field names, positions, and widths
34 register reset value
35 register reset mask
36 register write mask
37
38 The parsed information can either be used either to produce in-order
39 tables for inclusion in documentation or to provide CSR information to
40 Phase 1 tools (metacheckers, CSR checkers, etc).
41
42 =head1 PUBLIC ROUTINES
43
44 get_tex_files() Recursively follow a file's input/include/import tree
45
46 get_regs() Parse CSR information from a single specified file
47
48 build_reg_hash() Build a multi-level hash, keyed by function and
49 offset, from the specified register information
50
51 =head1 SEE ALSO
52
53 Any project-specific LaTeX introductory material.
54
55
56 =head1 AUTHOR
57
58 Matthew Lovell ([email protected])
59
60 =cut
61
62 ###############################################################################
63
64 package Reg_macro;
65 use Exporter;
66 @ISA = qw(Exporter);
67 @EXPORT = qw(get_tex_files
68 get_regs
69 build_reg_hash
70 $REG_DEBUG
71 $REG_STRIDE
72 $REG_ERRORS
73 );
74
75 use FileHandle;
76 use Data::Dumper;
77
78 # Debug flag
79 $REG_DEBUG = 0;
80
81 # How large are registers? Default is 64-bits.
82 $REG_STRIDE = hex(0x08);
83
84 # Error count
85 $REG_ERRORS = 0;
86
87 ###############################################################################
88 # P U B L I C R O U T I N E S #
89 ###############################################################################
90
91
92
93
94 # Read a .tex file line by line, finding all \include or \input
95 # commands. Recurse upon encountering one of those, thus making
96 # a list of the entire TeX file tree.
97 #
98 # This subroutine can also be called with an option indicating that
99 # all \includegraphics targets should be parsed. With that option,
100 # this routine can extract all dependencies for a LaTeX file, which
101 # is useful for GNU make.
102 #
103 # Arguments: filename
104 # flag indicating all dependencies should be tracked
105 #
106 sub get_tex_files {
107 my ($filename, $all_files) = @_;
108
109 # return immediately for some file types
110 if ($filename =~ /\.(eps|pdf|ps|func|error)$/) {
111 print "#INFO Reg_macro Not attempting to read '$filename'\n" if ($REG_DEBUG);
112 return $filename;
113 }
114
115 my $path = dirname($filename);
116
117 # Add .tex if not present
118 if ($filename !~ /\.\w+$/) {
119 $filename .= '.tex';
120 }
121
122 # initialize file list
123 my @file_list = ($filename);
124
125 my $file = new FileHandle "$filename";
126 unless (defined $file) {
127 print STDERR "#ERROR Reg_macro Unable to open '$filename'\n";
128 $REG_ERRORS++;
129 return @file_list;
130 }
131
132 # Allow files to turn off dependency extraction in particular sections,
133 # when desired. This may be useful if a preamble needs to input a file
134 # from the texmf tree, for example.
135 my $dependency_skip = 0;
136
137 while (<$file>) {
138
139 $dependency_skip = 1 if (m/^%% DEPEND OFF/);
140 $dependency_skip = 0 if (m/^%% DEPEND ON/);
141 next if $dependency_skip;
142
143 # Skip over comments.
144 s/^%.*$//; # Remove whole comment lines
145 s/([^\\])%.*$/$1/; # Remove everything after unescaped %'s
146
147
148 # Get all include and input commands
149 my @list = m/\\(?:include|input)\{([\w\/.-]+)\}/g;
150 if ($path ne '' and $path ne '.') {
151 @list = map { $path . "/" . $_ } @list;
152 }
153
154 # Find any import commands
155 if (m|\\import\{([\w\/.-]+)}\{([\w\/.-]+)\}|) {
156 push(@list,$1.$2);
157 }
158
159 # Find ALL files for make dependencies
160 if ($all_files) {
161
162 # Graphics files
163 if (m|\\includegraphics(?:\[.*?\])?\{([\w\/.-]+)\}|) {
164
165 # Add .eps if not present
166 my $gfx_file = $1;
167 if ($gfx_file !~ /\.\w+$/) {
168 $gfx_file .= '.eps';
169 }
170
171 if ($path ne '' and $path ne '.') {
172 $gfx_file = $path . "/" . $gfx_file;
173 }
174
175 push(@list,$gfx_file);
176 }
177
178
179 # project-specific:
180 #
181 # CSR files (just look for the \CSRtable invocation)
182 if (m|\\CSRtable|) {
183 my $func_file;
184 ($func_file = $filename) =~ s/\.tex$/.func/;
185 push(@list, $func_file);
186 }
187
188
189 } # end all_files
190
191 my $subfile;
192 for $subfile (@list) {
193 push(@file_list, get_tex_files($subfile, $all_files));
194 }
195 }
196
197 return @file_list;
198 } # get_tex_files
199
200
201
202
203 # Read a .tex file line by line, looking for register declarations
204 # using the register.sty macro. This subroutine only examines
205 # a single file.
206 #
207 # Arguments: filename
208 # reference to array of register information
209 #
210 sub get_regs {
211 my ($filename, $reg_data) = @_;
212
213 my $file = new FileHandle "$filename";
214
215 # Record the function in which a register resides. Allow this to
216 # be defined for just the first register in a file, then carried
217 # over to all the others.
218 my $function = 0;
219
220 while (<$file>) {
221 # find register definition
222 if ( m/\\begin\{register\}\{[\w\!]+\}\{(.*)\}\{(.*)\}%(.*)$/ ) {
223 my $long_name = $1;
224 my $offset = $2;
225 my $testable = 1;
226 my $short_name = undef;
227
228 my $remainder = $3;
229
230 if ($remainder ne "") {
231 if ($remainder =~ /don\'?t(_| )test/i) { $testable = 0 }
232 if ($remainder =~ /NAME=([\w\\<>\d]+)/i) { $short_name = $1 }
233 if ($remainder =~ /func=(\d)/i) { $function = $1 }
234 if ($remainder =~ /off(?:set)?=([\w-,g]+)/) { $offset = $1 }
235 }
236
237 # get rid of escaped underscores
238 $long_name =~ s/\\_/_/g;
239 $short_name =~ s/\\_/_/g;
240
241 my $width = 0;
242 my $reg = {
243 long_name => $long_name,
244 short_name => $short_name,
245 function => $function,
246 offset => $offset,
247 testable => $testable,
248 fields => []
249 };
250
251
252 # now we need to extract fields. first,
253 # read in rest of the reg macro
254 @wholereg = ();
255 while (<$file>) {
256 push(@wholereg, $_);
257 last if (/\\end\{register\}/);
258 }
259
260
261 # combine lines if nested {}'s do not match
262 for (my $i = 0; $i <= $#wholereg; $i++) {
263 my $open = grep(/\{/, split(//, $wholereg[$i]));
264 my $close = grep(/\}/, split(//, $wholereg[$i]));
265
266 if ($open != $close) {
267 my ($next) = splice(@wholereg, $i+1, 1);
268 $wholereg[$i] .= $next;
269 redo;
270 }
271 }
272
273 foreach (@wholereg) {
274
275 if (m/\\label\{(.*)\}/) {
276 $reg->{label} = $1;
277 }
278
279 # normal register field (reset value specified)
280 if ( m/\\regfield\{(.*)\}\s*\{(.*)\}\s*\{(.*)\}\s*\{(.*)\}\s*%(.*)$/s ) {
281 my $remain = $5;
282 my $field_name = $1;
283 my $field_len = $2;
284 my $field_start = $3;
285 my $reset = $4; $reset =~ s/^\{//; $reset =~ s/\}$//; $reset =~ s/_//g;
286 my $reset_mask = $testable; # indicates field is deterministic after reset
287 my $write_mask = $testable; # indicates field is modified by regbus writes
288
289
290 if ($field_name =~ m/\{([\w ])\}$/) {
291 $field_name = $1;
292 }
293 $field_name =~ s/\\_/_/g;
294
295
296 if ($remain =~ /read(?:_| )only/i) { $write_mask = 0; }
297 if ($remain =~ /status/i) { $reset_mask = 0; $write_mask = 0; }
298 if ($field_name =~ /reserved/i) { $write_mask = 0; }
299
300
301 if ($field_len > 32) {
302 die "#ERROR Reg_macro Field of greater than 32 bits in $short_name!\n";
303 }
304
305 push(@{$reg->{fields}},
306 {
307 name => $field_name,
308 len => $field_len,
309 start => $field_start,
310 reset => $reset,
311 reset_mask => $reset_mask,
312 write_mask => $write_mask
313 });
314 }
315
316
317 # register field without reset
318 if ( m/\\regfieldb\{(.*)\}\s*\{(.*)\}\s*\{(.*)\}\s*%(.*)$/s ) {
319 my $remain = $4;
320 my $field_name = $1;
321 my $field_len = $2;
322 my $field_start = $3;
323 my $reset_mask = $testable; # indicates field is deterministic after reset
324 my $write_mask = $testable; # indicates field is modified by regbus writes
325
326
327 if ($field_name =~ m/\{([\w ])\}$/) {
328 $field_name = $1;
329 }
330 $field_name =~ s/\\_/_/g;
331
332 if ($remain =~ /read(?:_| )only/i) { $write_mask = 0; }
333 if ($remain =~ /status/i) { $reset_mask = 0; $write_mask = 0; }
334 if ($field_name =~ /reserved/i) { $write_mask = 0; }
335
336 if ($field_len > 32) {
337 die "#ERROR Reg_macro Field of greater than 32 bits in $short_name!\n";
338 }
339
340 push(@{$reg->{fields}},
341 {
342 name => $field_name,
343 len => $field_len,
344 start => $field_start,
345 reset_mask => $reset_mask,
346 write_mask => $write_mask
347 });
348 }
349
350
351 } # end of register macro found
352
353 print Dumper $reg if ($REG_DEBUG);
354 push(@$reg_data, $reg);
355
356 }
357 }
358
359 } # get_regs
360
361
362
363 # Build a hash of registers, keyed on function and offset. That is,
364 # given parsed register information, construct a hash with the
365 # following structure
366 #
367 # $reg_hash { function } { offset }
368 #
369 # This will enable loops that iterate through registers in offset order
370 #
371 # Arguments: reference to array of reg information from get_regs
372 # Returns: reference to constructed hash
373 #
374 sub build_reg_hash {
375 my ($reg_array) = @_;
376 my $reg_hash = {};
377
378 my $unknown_count = 0;
379
380 # iterate through all defined registers
381 foreach my $reg (@$reg_array) {
382
383 if ($reg->{offset} =~ m/^(0x[0-9A-F]+)$/i) {
384 # simple offset specified
385 my $offset = $1;
386
387 if (exists $reg_hash->{$reg->{function}}{$offset}) {
388 print STDERR "##ERROR Reg_macro A Collision at offset $offset of function $reg->{function} \n";
389 print STDERR " Existing register: " . $reg_hash->{$reg->{function}}{$offset}{long_name} . "\n";
390 print STDERR " New register: " . $reg->{long_name} . "\n";
391 print STDERR "#END\n";
392 $REG_ERRORS++;
393 }
394
395 $reg_hash->{$reg->{function}}{$offset} = $reg;
396
397 } elsif ($reg->{offset} =~ m/^(0x[0-9A-F]+)\s*-+\s*(0x[0-9A-F]+)/i) {
398
399 # range specified.
400 my $start = hex($1);
401 my $end = hex($2);
402
403 # determine stride
404 my $stride = $REG_STRIDE;
405 if ($reg->{offset} =~ m/,((?:0x)?[0-9A-F]+)/) {
406 $stride = hex($1);
407 }
408
409 my $count = 0;
410 for (my $index = $start; $index <= $end; $index += $stride) {
411
412 my $offset = sprintf("0x%03x",$index);
413
414 if (exists $reg_hash->{$reg->{function}}{$offset}) {
415 print STDERR "##ERROR Reg_macro Collision at offset $offset of function $reg->{function} \n";
416 print STDERR " Existing register: " . $reg_hash->{$reg->{function}}{$offset}{long_name} . "\n";
417 print STDERR " New register: " . $reg->{long_name} . "\n";
418 print STDERR "#END\n";
419 $REG_ERRORS++;
420 }
421
422 # perform any substitutions in long_name
423 my $reg_copy = { %{$reg} };
424 if ($reg_copy->{long_name} =~ m/<n>/) {
425 $reg_copy->{long_name} =~ s/<n>/$count/;
426 } else {
427 $reg_copy->{long_name} .= " $count";
428 }
429
430 # perform any substitutions in short_name
431 if ($reg_copy->{short_name} =~ m/<n>/) {
432 $reg_copy->{short_name} =~ s/<n>/$count/;
433 } else {
434 $reg_copy->{short_name} .= "$count";
435 }
436
437 $reg_hash->{$reg->{function}}{$offset} = $reg_copy;
438 $count++;
439 }
440
441 } else {
442 # unknown offset
443 $reg_hash->{$reg->{function}}{"unk".$unknown_count++} = $reg;
444 print STDERR "#WARNING Reg_macro Register '$reg->{long_name}' has an unknown offset \n";
445 }
446
447 } # end foreach
448
449 return $reg_hash;
450 } # build_reg_hash
451
452
453
454 # Convert an ASCII string representing a binary number to a true decimal number
455 sub bin2dec {
456 return unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
457 }
458
459 # Convert a true number to a binary ASCII string
460 sub dec2bin {
461 my $str = unpack("B32", pack("N", shift));
462 return $str;
463 }
464
465 # Covert an ASCII string representing a hex number to a binary ASCII string;
466 sub hex2bin {
467 return unpack("B32", pack("N", hex(shift)));
468 }
469
470 # Convert an ASCII string representing a binary number to a hex ASCII string;
471 sub bin2hex {
472
473 my $Bin = shift(@_);
474 my $Hex;
475
476 # Clean up string and look for errors
477 chomp $Bin;
478
479 if ($Bin =~ m/[^10]/) {
480 print STDERR "#ERROR Reg_macro $Bin is not binary\n";
481 return ("");
482 }
483 return scalar reverse unpack "h*",(pack "b*", scalar reverse $Bin);
484 }
485
486
487
488 # required by Perl
489 1;
490
491 ## Local Variables:
492 ## mode: perl
493 ## End:
494
495
496
497
498