isa: excluded ngerman for english table/figure naming
[calu.git] / 2_isa / gentex.pl
1 #!/usr/bin/perl
2 use strict;
3
4 sub ins {
5         my $bits = $_[0];
6         my $bline;
7         #bit value line
8         my $vline;
9         my $nline;
10         #print bit value line
11         my $pvline;
12         my @bitlist;
13         my $tabular;
14         my $count = 0;
15         my @fields;
16         my $i;
17         my $x;
18         my @fieldnames;
19         my @flagnames = ('SF', 'CF', 'OF', 'ZF');
20
21
22
23         @fields = split(/\|/, $_[3]);
24         foreach my $field (@fields) {
25                 #remove heading and trailing spaces
26                 $field =~ s/^\s+//;
27                 $field =~ s/\s+$//;
28
29                 $count += $field;
30                 #$tabular .= $field." \\\\";
31         }
32
33         if($count != $bits) {
34                 return "Num of Fields not equal to Bits!";
35         }
36         $count = 0;
37         @fieldnames = split(/\|/, $_[4]);
38
39         if(scalar(@fields) != scalar(@fieldnames)) {
40                 return "Num of Fields != Num of Names";
41         }
42
43         $pvline = 0;
44         foreach my $fieldn (@fieldnames) {
45                 #remove heading and trailing spaces
46                 $fieldn =~ s/^\s+//;
47                 $fieldn =~ s/\s+$//;
48
49                 if($fieldn =~ m/^[^\(.]*\(\s*([0-1]+)\s*\).*$/) {
50                         if(length($1) == $fields[$count]) {
51                                 push(@bitlist,'S'.$1);
52                                 $fieldn =~  s/\s?\(\s*[0-1]+\s*\)//;
53                                 $pvline = 1;
54                         }
55                         else {
56                                 $x = 'LX';
57                                 push(@bitlist,$x);
58                         }
59                 }
60                 else {
61                         $x = 'LX';
62                         push(@bitlist,$x);
63                 }
64                 #$tabular .= $fieldn.": ". $bits." \\\\";
65                 $count++;
66         }
67
68         #$tabular .= "\\textbf{names:} @fieldnames \n";
69         #$tabular .= "\\textbf{bitlist:} $bitlist \n";
70
71         $count = $bits-1;
72
73         $tabular = "\\subsubsection{".$_[1]."}\n";
74         $tabular .= "\\begin{center} \n \\resizebox{\\textwidth}{!}{\n";
75         $tabular .= "\\begin{tabular}{|l|";
76         for($i = $bits-1; $i >= 0; $i--) {
77                 #build table
78                 $tabular .= "c|";
79
80                 #build bitcount
81                 $bline .= $i;
82                 if($i != 0) {
83                         $bline .= " & ";
84                 }
85                 else {
86                         $bline .= "\\\\ \\hline \n";
87                 }
88
89                 #build nameline and bitvalue
90                 #because of multicolumn we need to limit execution
91                 if($i == $count) {
92                         $nline .= "\\multicolumn{@fields[0]}{>{\\columncolor{names}}c|}{@fieldnames[0]}";
93
94                         if(substr(@bitlist[0],0,1) eq "L") {
95                                 $vline .= "\\multicolumn{@fields[0]}{>{\\columncolor{bitval}}c|}{".substr(@bitlist[0],1)."}";
96                         }
97                         else {
98                                 for(my $j = 1; $j <= @fields[0]; $j++) {
99                                         $vline .= substr(@bitlist[0],$j,1);
100                                         if($j != @fields[0]) {
101                                                 $vline .= " & ";
102                                         }
103                                 }
104                         }
105
106                         #update count
107                         $count -= @fields[0];
108
109                         if($count < 0) {
110                                 $nline .= "\\\\ \\hline \n";
111                                 $vline .= "\\\\ \\hline \n";
112                         }
113                         else {
114                                 $nline .= " & ";
115                                 $vline .= " & ";
116                         }
117
118                         shift(@bitlist);
119                         shift(@fieldnames);
120                         shift(@fields);
121                 }
122         }
123         $tabular .= "} \\hline \n";
124         $tabular .= "\\multicolumn{".($bits+1)."}{|>{\\columncolor{title}}c|}{\\textbf{".$_[1]."} (".$_[2].")} \\\\ \\hline \n";
125         $tabular .= "\\rowcolor{bit} \\cellcolor{title} &". $bline;
126         if($pvline == 1) {
127                 $tabular .= "\\rowcolor{bitval} \\cellcolor{title}Values &". $vline;
128         }
129         $tabular .= "\\cellcolor{title}Field &". $nline;
130         $tabular .= "\\end{tabular}\n}\n \\end{center}\n";
131
132         if($_[5]) {
133                 $tabular .= "\\paragraph{Description:}~\\\\ \n ".$_[5]. "\n";
134         }
135
136         if($_[6]) {
137                 $count = 0;
138                 my @flags = split(/\|/, $_[6]);
139
140                 if(scalar(@flags) == scalar(@flagnames)) {
141                         $tabular .= "\\paragraph{Flags:} \n";
142
143                         foreach my $flag (@flags) {
144                                 #remove heading and trailing spaces
145                                 $flag =~ s/^\s+//;
146                                 $flag =~ s/\s+$//;
147                                 if(length($flag) > 0 ) {
148                                         $tabular .= "\\subparagraph{".$flagnames[$count].":} ".$flag." \n";
149                                 }
150                                 $count++;
151                         }
152                 }
153
154         }
155
156         if($_[7]) {
157                 $tabular .= "\\paragraph{Example:}~\\\\ \n \\begin{lstlisting} \n ".$_[7]. " \\end{lstlisting}\n";
158         }
159
160         if($_[8]) {
161                 $tabular .= "\\paragraph{Aliases to this Instruction:}~\\\\\ \n";
162                 my @aliases = split(/\|/, $_[8]);
163
164                 $tabular .= "\\begin{table}[!h] \n \\centering \n \\begin{tabular}{|c|c|} \\hline \n";
165                 $tabular .= "Alias & ".$_[1]." equivalent \\\\ \\hline \n";
166                 foreach my $alias (@aliases) {
167                         my $equv;
168                         my $from;
169                         ($from, $equv) = split(/-/, $alias);
170                         $tabular .= $from . " & ".$equv." \\\\ \\hline \n";
171                 }
172                 $tabular .= "\\end{tabular} \n \\caption{Aliases to the instruction ".$_[1]."} \n \\end{table}";
173         }
174
175         print $tabular;
176 }
177
178 if($#ARGV != 0) {
179         die "Usage: gentex.pl <file.ptex>";
180 }
181
182 my $output = $ARGV[0];
183 my $input = $ARGV[0];
184
185 $output =~ s/\.[^.]*$//;
186 $output .= ".tex";
187
188 open STDOUT, '>', $output or die "Can't redirect STDOUT: $!";
189
190 do $input;