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