2007-06-28 Martin Baulig <martin@ximian.com>
[mono.git] / mono / mini / genmdesc.pl
1 #!/usr/bin/perl -w
2
3 # perl replacement of genmdesc.c for use when cross-compiling
4
5 use strict;
6 no locale;
7
8 # must keep in sync with mini.h
9 my @spec_names = qw(dest src1 src2 len clob);
10 sub INST_DEST  () {return 0;}
11 sub INST_SRC1  () {return 1;}
12 sub INST_SRC2  () {return 2;}
13 sub INST_LEN   () {return 3;}
14 sub INST_CLOB  () {return 4;}
15 sub INST_MAX   () {return 5;}
16
17 # this must include all the #defines used in mini-ops.h
18 my @defines = qw (__i386__ __x86_64__ __ppc__ __powerpc__ __arm__ 
19         __sparc__ sparc __s390__ s390 __ia64__ __alpha__ __mips__);
20 my %table =();
21 my @opcodes = ();
22
23 sub load_opcodes
24 {
25         my ($srcdir, $arch) = @_;
26         my $opcodes_def = "$srcdir/../cil/opcode.def";
27         my $i = 0;
28         my $arch_found = 0;
29
30         open (OPS, "$opcodes_def") || die "Cannot open $opcodes_def: $!";
31         while (<OPS>) {
32                 next unless /OPDEF\s*\(\s*(\S+?)\s*,\s*"(.*?)"/;
33                 my ($sym, $name) = ($1, $2);
34                 push @opcodes, [$sym, $name];
35                 $table{$name} = {num => $i, name => $name};
36                 $i++;
37         }
38         close (OPS);
39         my $cpp = "cpp -undef ";
40         foreach (@defines) {
41                 $cpp .= " -U$_";
42                 $arch_found = 1 if $arch eq $_;
43         }
44         die "$arch arch is not supported.\n" unless $arch_found;
45
46         $cpp .= " -D$arch $srcdir/mini-ops.h|";
47         #print "Running: $cpp\n";
48         open (OPS, $cpp) || die "Cannot execute cpp: $!";
49         while (<OPS>) {
50                 next unless /MINI_OP\s*\(\s*(\S+?)\s*,\s*"(.*?)"/;
51                 my ($sym, $name) = ($1, $2);
52                 push @opcodes, [$sym, $name];
53                 $table{$name} = {num => $i, name => $name};
54                 $i++;
55         }
56         close (OPS);
57 }
58
59 sub load_file {
60         my ($name) = @_;
61         my $line = 0;
62         my $comment = "";
63
64         open (DESC, $name) || die "Cannot open $name: $!";
65         while (<DESC>) {
66                 $line++;
67                 next if /^\s*$/;
68                 if (/^\s*(#.*)?$/) {
69                         $comment .= "$1\n";
70                         next;
71                 }
72                 my @values = split (/\s+/);
73                 next unless ($values [0] =~ /(\S+?):/);
74                 my $name = $1;
75                 my $desc = $table {$name};
76                 shift @values;
77                 die "Invalid opcode $name at line $line\n" unless defined $desc;
78                 die "Duplicated opcode $name at line $line\n" if $desc->{"desc"};
79                 $desc->{"desc"} = $_;
80                 $desc->{"comment"} = $comment;
81                 $desc->{"spec"} = {};
82                 $comment = "";
83                 #print "values for $name: " . join (' ', @values) . " num: " . int(@values), "\n";
84                 for my $val (@values) {
85                         if ($val =~ /(\S+):(.*)/) {
86                                 $desc->{"spec"}->{$1} = $2;
87                         }
88                 }
89         }
90         close (DESC);
91 }
92
93 sub build_spec {
94         my ($spec) = shift;
95         my %spec = %{$spec};
96         my @vals = ();
97         foreach (@spec_names) {
98                 my $val = $spec->{$_};
99                 if (defined $val) {
100                         push @vals, $val;
101                 } else {
102                         push @vals, undef;
103                 }
104         }
105         #print "vals: " . join (' ', @vals) . "\n";
106         my $res = "";
107         for (my $i = 0; $i < @vals; ++$i) {
108                 if (defined $vals [$i]) {
109                         if ($i == INST_LEN) {
110                                 $res .= sprintf ("\\x%x\" \"", +$vals [$i]);
111                         } else {
112                                 if ($vals [$i] =~ /^[a-zA-Z0-9]$/) {
113                                         $res .= $vals [$i];
114                                 } else {
115                                         $res .= sprintf ("\\x%x\" \"", $vals [$i]);
116                                 }
117                         }
118                 } else {
119                         $res .= "\\x0\" \"";
120                 }
121         }
122         return $res;
123 }
124
125 sub build_table {
126         my ($fname, $name) = @_;
127         my $i;
128         my $idx;
129         my $idx_array = "const guint16 ${name}_idx [] = {\n";
130
131         open (OUT, ">$fname") || die "Cannot open file $fname: $!";
132         print OUT "/* File automatically generated by genmdesc, don't change */\n\n";
133         print OUT "const char $name [] = {\n";
134         print OUT "\t\"" . ("\\x0" x INST_MAX) . "\"\t/* null entry */\n";
135         $idx = 1;
136
137         for ($i = 0; $i < @opcodes; ++$i) {
138                 my $name = $opcodes [$i]->[1];
139                 my $desc = $table {$name};
140                 my $spec = $desc->{"spec"};
141                 if (defined $spec) {
142                         print OUT "\t\"";
143                         print OUT build_spec ($spec);
144                         print OUT "\"\t/* $name */\n";
145                         my $pos = $idx * INST_MAX;
146                         $idx_array .= "\t$pos,\t/* $name */\n";
147                         ++$idx;
148                 } else {
149                         $idx_array .= "\t0,\t/* $name */\n";
150                 }
151         }
152         print OUT "};\n\n";
153         print OUT "$idx_array};\n\n";
154         close (OUT);
155 }
156
157 sub usage {
158         die "genmdesc.pl arch srcdir desc output name\n";
159 }
160
161 my $arch = shift || usage ();
162 my $srcdir = shift || usage ();
163 my $file = shift || usage ();
164 my $output = shift || usage ();
165 my $name = shift || usage ();
166
167 load_opcodes ($srcdir, $arch);
168 load_file ($file);
169 build_table ($output, $name);
170