Merge pull request #5407 from kumpera/fix_31225
[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 src3 len clob nacl);
10 sub INST_DEST  () {return 0;}
11 sub INST_SRC1  () {return 1;}
12 sub INST_SRC2  () {return 2;}
13 sub INST_SRC3  () {return 3;}
14 sub INST_LEN   () {return 4;}
15 sub INST_CLOB  () {return 5;}
16 # making INST_NACL the same as INST_MAX is not a mistake,
17 # INST_NACL writes over INST_LEN, it's not its own field
18 sub INST_NACL  () {return 6;}
19 sub INST_MAX   () {return 6;}
20
21 # this must include all the #defines used in mini-ops.h
22 my @defines = qw (__i386__ __x86_64__ __ppc__ __powerpc__ __ppc64__ __arm__ 
23         __sparc__ sparc __s390__ s390 __alpha__ __mips__ __aarch64__);
24 my %table =();
25 my %template_table =();
26 my @opcodes = ();
27
28 my $nacl = 0;
29
30 sub parse_file
31 {
32         my ($define, $file) = @_;
33         my @enabled = (1);
34         my $i = 0;
35         open (OPS, $file) || die "Cannot open $file: $!";
36         while (<OPS>) {
37                 if (/^\s*#\s*if\s+(.*)/) {
38                         my $defines = $1;
39                         die "fix the genmdesc.pl cpp parser to handle all operators" if /(&&)|([!<>=])/;
40                         unshift @enabled, scalar ($defines =~ /defined\s*\(\s*$define\s*\)/);
41                         next;
42                 }
43                 if (/^\s*#\s*ifdef\s+(\S+)/) {
44                         my $defines = $1;
45                         unshift @enabled, $defines eq $define;
46                         next;
47                 }
48                 if (/^\s*#\s*endif/) {
49                         shift @enabled;
50                         next;
51                 }
52                 next unless $enabled [0];
53                 next unless /MINI_OP3?\s*\(\s*(\S+?)\s*,\s*"(.*?)"/;
54                 my ($sym, $name) = ($1, $2);
55                 push @opcodes, [$sym, $name];
56                 $table{$name} = {num => $i, name => $name};
57                 $i++;
58         }
59         close (OPS);
60 }
61
62 sub load_opcodes
63 {
64         my ($srcdir, $arch) = @_;
65         my $opcodes_def = "$srcdir/../cil/opcode.def";
66         my $i = 0;
67         my $arch_found = 0;
68
69         my $cpp = $ENV{"CPP"};
70         $cpp = "cpp" unless defined $cpp;
71         $cpp .= " -undef ";
72         foreach (@defines) {
73                 $cpp .= " -U$_";
74                 $arch_found = 1 if $arch eq $_;
75         }
76         die "$arch arch is not supported.\n" unless $arch_found;
77
78         my $arch_define = $arch;
79         if ($arch =~ "__i386__") {
80                 $arch_define = "TARGET_X86";
81         }
82         if ($arch =~ "__x86_64__") {
83                 $arch_define = "TARGET_AMD64";
84         }
85         if ($arch =~ "__arm__") {
86                 $arch_define = "TARGET_ARM";
87         }
88         if ($arch =~ "__aarch64__") {
89                 $arch_define = "TARGET_ARM64";
90         }
91
92         parse_file ($arch_define, "$srcdir/mini-ops.h");
93         return;
94         $cpp .= " -D$arch_define $srcdir/mini-ops.h|";
95         #print "Running: $cpp\n";
96         open (OPS, $cpp) || die "Cannot execute cpp: $!";
97         while (<OPS>) {
98                 next unless /MINI_OP3?\s*\(\s*(\S+?)\s*,\s*"(.*?)"/;
99                 my ($sym, $name) = ($1, $2);
100                 push @opcodes, [$sym, $name];
101                 $table{$name} = {num => $i, name => $name};
102                 $i++;
103         }
104         close (OPS);
105 }
106
107 sub load_file {
108         my ($name) = @_;
109         my $line = 0;
110         my $comment = "";
111
112         open (DESC, $name) || die "Cannot open $name: $!";
113         while (<DESC>) {
114                 my $is_template = 0;
115                 $line++;
116                 next if /^\s*$/;
117                 if (/^\s*(#.*)?$/) {
118                         $comment .= "$1\n";
119                         next;
120                 }
121                 my @values = split (/\s+/);
122                 next unless ($values [0] =~ /(\S+?):/);
123                 my $name = $1;
124                 my $desc;
125                 if ($name eq "template") {
126                         $is_template = 1;
127                         $desc = {};
128                 } else {
129                         $desc = $table {$name};
130                         die "Invalid opcode $name at line $line\n" unless defined $desc;
131                         die "Duplicated opcode $name at line $line\n" if $desc->{"desc"};
132                 }
133                 shift @values;
134                 $desc->{"desc"} = $_;
135                 $desc->{"comment"} = $comment;
136                 $desc->{"spec"} = {};
137                 $comment = "";
138                 #print "values for $name: " . join (' ', @values) . " num: " . int(@values), "\n";
139                 for my $val (@values) {
140                         if ($val =~ /(\S+):(.*)/) {
141                                 if ($1 eq "name") {
142                                         die "name tag only valid in templates at line $line\n" unless $is_template;
143                                         die "Duplicated name tag in template $desc->{'name'} at line $line\n" if defined $desc->{'name'};
144                                         die "Duplicated template $2 at line $line\n" if defined $template_table {$2};
145                                         $desc->{'name'} = $2;
146                                         $template_table {$2} = $desc;
147                                 } elsif ($1 eq "template") {
148                                         my $tdesc = $template_table {$2};
149                                         die "Invalid template name $2 at line $line\n" unless defined $tdesc;
150                                         $desc->{"spec"} = {%{$tdesc->{"spec"}}};
151                                 } else {
152                                         $desc->{"spec"}->{$1} = $2;
153                                 }
154                         }
155                 }
156                 die "Template without name at line $1" if ($is_template && !defined ($desc->{'name'}));
157         }
158         close (DESC);
159 }
160
161 sub build_spec {
162         my ($spec) = shift;
163         my %spec = %{$spec};
164         my @vals = ();
165         foreach (@spec_names) {
166                 my $val = $spec->{$_};
167                 if (defined $val) {
168                         push @vals, $val;
169                 } else {
170                         push @vals, undef;
171                 }
172         }
173         #print "vals: " . join (' ', @vals) . "\n";
174         my $res = "";
175         my $n = 0;
176         for (my $i = 0; $i < @vals; ++$i) {
177                 next if $i == INST_NACL;
178                 if (defined $vals [$i]) {
179                         if ($i == INST_LEN) {
180                                 $n = $vals [$i];
181                                 if ($n =~ /[^0-9]/) {
182                                                 die "Invalid instruction length $n\n";
183                                 }
184                                 if ((defined $vals [INST_NACL]) and $nacl == 1){
185                                     $n = $vals [INST_NACL];
186                                 }
187                                 $res .= sprintf ("\\x%x\" \"", + $n);
188                         } else {
189                                 if ($vals [$i] =~ /^[a-zA-Z0-9]$/) {
190                                         $res .= $vals [$i];
191                                 } else {
192                                         $res .= sprintf ("\\x%x\" \"", $vals [$i]);
193                                 }
194                         }
195                 } else {
196                         $res .= "\\x0\" \"";
197                 }
198         }
199         return $res;
200 }
201
202 sub build_table {
203         my ($fname, $name) = @_;
204         my $i;
205         my $idx;
206         my $idx_array = "const guint16 mono_${name}_idx [] = {\n";
207
208         open (OUT, ">$fname") || die "Cannot open file $fname: $!";
209         print OUT "/* File automatically generated by genmdesc, don't change */\n\n";
210         print OUT "const char mono_$name [] = {\n";
211         print OUT "\t\"" . ("\\x0" x INST_MAX) . "\"\t/* null entry */\n";
212         $idx = 1;
213
214         for ($i = 0; $i < @opcodes; ++$i) {
215                 my $name = $opcodes [$i]->[1];
216                 my $desc = $table {$name};
217                 my $spec = $desc->{"spec"};
218                 if (defined $spec) {
219                         print OUT "\t\"";
220                         print OUT build_spec ($spec);
221                         print OUT "\"\t/* $name */\n";
222                         my $pos = $idx * INST_MAX;
223                         $idx_array .= "\t$pos,\t/* $name */\n";
224                         ++$idx;
225                 } else {
226                         $idx_array .= "\t0,\t/* $name */\n";
227                 }
228         }
229         print OUT "};\n\n";
230         print OUT "$idx_array};\n\n";
231         close (OUT);
232 }
233
234 sub usage {
235         die "genmdesc.pl arch srcdir [--nacl] output name desc [desc2 ...]\n";
236 }
237
238 my $arch = shift || usage ();
239 my $srcdir = shift || usage ();
240 my $output = shift || usage ();
241 if ($output eq "--nacl")
242 {
243   $nacl = 1;  
244   $output = shift || usage();
245 }
246 my $name = shift || usage ();
247 usage () unless @ARGV;
248 my @files = @ARGV;
249
250 load_opcodes ($srcdir, $arch);
251 foreach my $file (@files) {
252         load_file ($file);
253 }
254 build_table ($output, $name);
255