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 __ia64__ __alpha__ __mips__ __aarch64__);
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";
92 parse_file ($arch_define, "$srcdir/mini-ops.h");
94 $cpp .= " -D$arch_define $srcdir/mini-ops.h|";
95 #print "Running: $cpp\n";
96 open (OPS, $cpp) || die "Cannot execute cpp: $!";
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};
112 open (DESC, $name) || die "Cannot open $name: $!";
121 my @values = split (/\s+/);
122 next unless ($values [0] =~ /(\S+?):/);
125 if ($name eq "template") {
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"};
134 $desc->{"desc"} = $_;
135 $desc->{"comment"} = $comment;
136 $desc->{"spec"} = {};
138 #print "values for $name: " . join (' ', @values) . " num: " . int(@values), "\n";
139 for my $val (@values) {
140 if ($val =~ /(\S+):(.*)/) {
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"}}};
152 $desc->{"spec"}->{$1} = $2;
156 die "Template without name at line $1" if ($is_template && !defined ($desc->{'name'}));
165 foreach (@spec_names) {
166 my $val = $spec->{$_};
173 #print "vals: " . join (' ', @vals) . "\n";
176 for (my $i = 0; $i < @vals; ++$i) {
177 next if $i == INST_NACL;
178 if (defined $vals [$i]) {
179 if ($i == INST_LEN) {
181 if ((defined $vals [INST_NACL]) and $nacl == 1){
182 $n = $vals [INST_NACL];
184 $res .= sprintf ("\\x%x\" \"", + $n);
186 if ($vals [$i] =~ /^[a-zA-Z0-9]$/) {
189 $res .= sprintf ("\\x%x\" \"", $vals [$i]);
200 my ($fname, $name) = @_;
203 my $idx_array = "const guint16 ${name}_idx [] = {\n";
205 open (OUT, ">$fname") || die "Cannot open file $fname: $!";
206 print OUT "/* File automatically generated by genmdesc, don't change */\n\n";
207 print OUT "const char $name [] = {\n";
208 print OUT "\t\"" . ("\\x0" x INST_MAX) . "\"\t/* null entry */\n";
211 for ($i = 0; $i < @opcodes; ++$i) {
212 my $name = $opcodes [$i]->[1];
213 my $desc = $table {$name};
214 my $spec = $desc->{"spec"};
217 print OUT build_spec ($spec);
218 print OUT "\"\t/* $name */\n";
219 my $pos = $idx * INST_MAX;
220 $idx_array .= "\t$pos,\t/* $name */\n";
223 $idx_array .= "\t0,\t/* $name */\n";
227 print OUT "$idx_array};\n\n";
232 die "genmdesc.pl arch srcdir [--nacl] output name desc [desc2 ...]\n";
235 my $arch = shift || usage ();
236 my $srcdir = shift || usage ();
237 my $output = shift || usage ();
238 if ($output eq "--nacl")
241 $output = shift || usage();
243 my $name = shift || usage ();
244 usage () unless @ARGV;
247 load_opcodes ($srcdir, $arch);
248 foreach my $file (@files) {
251 build_table ($output, $name);