3 # perl replacement of genmdesc.c for use when cross-compiling
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;}
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__ __wasm__);
25 my %template_table =();
32 my ($define, $file) = @_;
35 open (OPS, $file) || die "Cannot open $file: $!";
37 if (/^\s*#\s*if\s+(.*)/) {
39 die "fix the genmdesc.pl cpp parser to handle all operators" if /(&&)|([!<>=])/;
40 unshift @enabled, scalar ($defines =~ /defined\s*\(\s*$define\s*\)/);
43 if (/^\s*#\s*ifdef\s+(\S+)/) {
45 unshift @enabled, $defines eq $define;
48 if (/^\s*#\s*endif/) {
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};
64 my ($srcdir, $arch) = @_;
65 my $opcodes_def = "$srcdir/../cil/opcode.def";
69 my $cpp = $ENV{"CPP"};
70 $cpp = "cpp" unless defined $cpp;
74 $arch_found = 1 if $arch eq $_;
76 die "$arch arch is not supported.\n" unless $arch_found;
78 my $arch_define = $arch;
79 if ($arch =~ "__i386__") {
80 $arch_define = "TARGET_X86";
82 if ($arch =~ "__x86_64__") {
83 $arch_define = "TARGET_AMD64";
85 if ($arch =~ "__arm__") {
86 $arch_define = "TARGET_ARM";
88 if ($arch =~ "__aarch64__") {
89 $arch_define = "TARGET_ARM64";
91 if ($arch =~ "__wasm__") {
92 $arch_define = "TARGET_WASM";
94 parse_file ($arch_define, "$srcdir/mini-ops.h");
96 $cpp .= " -D$arch_define $srcdir/mini-ops.h|";
97 #print "Running: $cpp\n";
98 open (OPS, $cpp) || die "Cannot execute cpp: $!";
100 next unless /MINI_OP3?\s*\(\s*(\S+?)\s*,\s*"(.*?)"/;
101 my ($sym, $name) = ($1, $2);
102 push @opcodes, [$sym, $name];
103 $table{$name} = {num => $i, name => $name};
114 open (DESC, $name) || die "Cannot open $name: $!";
123 my @values = split (/\s+/);
124 next unless ($values [0] =~ /(\S+?):/);
127 if ($name eq "template") {
131 $desc = $table {$name};
132 die "Invalid opcode $name at line $line\n" unless defined $desc;
133 die "Duplicated opcode $name at line $line\n" if $desc->{"desc"};
136 $desc->{"desc"} = $_;
137 $desc->{"comment"} = $comment;
138 $desc->{"spec"} = {};
140 #print "values for $name: " . join (' ', @values) . " num: " . int(@values), "\n";
141 for my $val (@values) {
142 if ($val =~ /(\S+):(.*)/) {
144 die "name tag only valid in templates at line $line\n" unless $is_template;
145 die "Duplicated name tag in template $desc->{'name'} at line $line\n" if defined $desc->{'name'};
146 die "Duplicated template $2 at line $line\n" if defined $template_table {$2};
147 $desc->{'name'} = $2;
148 $template_table {$2} = $desc;
149 } elsif ($1 eq "template") {
150 my $tdesc = $template_table {$2};
151 die "Invalid template name $2 at line $line\n" unless defined $tdesc;
152 $desc->{"spec"} = {%{$tdesc->{"spec"}}};
154 $desc->{"spec"}->{$1} = $2;
158 die "Template without name at line $1" if ($is_template && !defined ($desc->{'name'}));
167 foreach (@spec_names) {
168 my $val = $spec->{$_};
175 #print "vals: " . join (' ', @vals) . "\n";
178 for (my $i = 0; $i < @vals; ++$i) {
179 next if $i == INST_NACL;
180 if (defined $vals [$i]) {
181 if ($i == INST_LEN) {
183 if ($n =~ /[^0-9]/) {
184 die "Invalid instruction length $n\n";
186 if ((defined $vals [INST_NACL]) and $nacl == 1){
187 $n = $vals [INST_NACL];
189 $res .= sprintf ("\\x%x\" \"", + $n);
191 if ($vals [$i] =~ /^[a-zA-Z0-9]$/) {
194 $res .= sprintf ("\\x%x\" \"", $vals [$i]);
205 my ($fname, $name) = @_;
208 my $idx_array = "const guint16 mono_${name}_idx [] = {\n";
210 open (OUT, ">$fname") || die "Cannot open file $fname: $!";
211 print OUT "/* File automatically generated by genmdesc, don't change */\n\n";
212 print OUT "const char mono_$name [] = {\n";
213 print OUT "\t\"" . ("\\x0" x INST_MAX) . "\"\t/* null entry */\n";
216 for ($i = 0; $i < @opcodes; ++$i) {
217 my $name = $opcodes [$i]->[1];
218 my $desc = $table {$name};
219 my $spec = $desc->{"spec"};
222 print OUT build_spec ($spec);
223 print OUT "\"\t/* $name */\n";
224 my $pos = $idx * INST_MAX;
225 $idx_array .= "\t$pos,\t/* $name */\n";
228 $idx_array .= "\t0,\t/* $name */\n";
232 print OUT "$idx_array};\n\n";
237 die "genmdesc.pl arch srcdir [--nacl] output name desc [desc2 ...]\n";
240 my $arch = shift || usage ();
241 my $srcdir = shift || usage ();
242 my $output = shift || usage ();
243 if ($output eq "--nacl")
246 $output = shift || usage();
248 my $name = shift || usage ();
249 usage () unless @ARGV;
252 load_opcodes ($srcdir, $arch);
253 foreach my $file (@files) {
256 build_table ($output, $name);