gentex: misc. changes
[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,$1);
52                                 $fieldn =~  s/\s?\(\s*[0-1]+\s*\)//;
53                                 $pvline = 1;
54                         }
55                         else {
56                                 $x = '';
57                                 for($i = 1; $i <= $fields[$count]; $i++) {
58                                         $x .= 'X';
59                                 }
60                         push(@bitlist,$x);
61                         }
62                 }
63                 else {
64                         $x = '';
65                         for($i = 1; $i <= $fields[$count]; $i++) {
66                                 $x .= 'X';
67                         }
68                         push(@bitlist,$x);
69                 }
70                 #$tabular .= $fieldn.": ". $bits." \\\\";
71                 $count++;
72         }
73         my $bitlist = join("",@bitlist);
74
75         #$tabular .= "\\textbf{names:} @fieldnames \n";
76         #$tabular .= "\\textbf{bitlist:} $bitlist \n";
77
78         $count = $bits-1;
79
80         $tabular = "\\subsubsection{".$_[1]."}\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 bitvalue
97                 $vline .= substr($bitlist,($bits-1)-$i,1);
98                 if($i != 0) {
99                         $vline .= " & ";
100                 }
101                 else {
102                         $vline .= "\\\\ \\hline \n";
103                 }
104
105                 #build nameline
106                 #because of multicolumn we need to limit execution
107                 if($i == $count) {
108                         $nline .= "\\multicolumn{@fields[0]}{>{\\columncolor{names}}c|}{@fieldnames[0]}";
109
110                         #update count
111                         $count -= @fields[0];
112
113                         if($count < 0) {
114                                 $nline .= "\\\\ \\hline \n";
115                         }
116                         else {
117                                 $nline .= " & ";
118                         }
119
120                         shift(@fieldnames);
121                         shift(@fields);
122                 }
123         }
124         $tabular .= "} \\hline \n";
125         $tabular .= "\\multicolumn{".($bits+1)."}{|>{\\columncolor{title}}c|}{\\textbf{".$_[1]."} (".$_[2].")} \\\\ \\hline \n";
126         $tabular .= "\\rowcolor{bit} \\cellcolor{title} &". $bline;
127         if($pvline == 1) {
128                 $tabular .= "\\rowcolor{bitval} \\cellcolor{title}Values &". $vline;
129         }
130         $tabular .= "\\cellcolor{title}Field &". $nline;
131         $tabular .= "\\end{tabular}\n}\n \\end{center}\n";
132
133         if($_[5]) {
134                 $tabular .= "\\paragraph{Description:}~\\\\ \n ".$_[5]. "\n";
135         }
136
137         if($_[6]) {
138                 $count = 0;
139                 my @flags = split(/\|/, $_[6]);
140
141                 if(scalar(@flags) == scalar(@flagnames)) {
142                         $tabular .= "\\paragraph{Flags:} \n";
143
144                         foreach my $flag (@flags) {
145                                 #remove heading and trailing spaces
146                                 $flag =~ s/^\s+//;
147                                 $flag =~ s/\s+$//;
148                                 if(length($flag) > 0 ) {
149                                         $tabular .= "\\subparagraph{".$flagnames[$count].":} ".$flag." \n";
150                                 }
151                                 $count++;
152                         }
153                 }
154
155         }
156
157         if($_[7]) {
158                 $tabular .= "\\paragraph{Example:}~\\\\ \n \\begin{lstlisting} \n ".$_[7]. " \\end{lstlisting}\n";
159         }
160
161         print $tabular;
162 }
163
164 if($#ARGV != 0) {
165         die "Usage: gentex.pl <file.ptex>";
166 }
167
168 my $output = $ARGV[0];
169 my $input = $ARGV[0];
170
171 $output =~ s/\.[^.]*$//;
172 $output .= ".tex";
173
174 open STDOUT, '>', $output or die "Can't redirect STDOUT: $!";
175
176 do $input;