-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathparse_ins.pl
executable file
·138 lines (110 loc) · 2.96 KB
/
parse_ins.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env perl
use warnings;
use strict;
my $file = 'instructions.txt';
open my $info, $file or die "Could not open $file: $!\n";
my $ins = {};
my $cur_ins;
while(my $line = <$info>) {
$line =~ s/(^\s+|\s+$)//;
if ($line =~ m/^Instruction/) {
if ($cur_ins && $cur_ins->{name}) {
$ins->{delete $cur_ins->{name}} = $cur_ins;
}
$cur_ins = {};
} elsif ($line =~ m/^mask: ([0-9abcdefx]+),/) {
$cur_ins->{mask} = $1;
} elsif ($line =~ m/^data: ([0-9abcdefx]+),/) {
$cur_ins->{data} = $1;
} elsif ($line =~ m/^name:\s*"(.+)",/) {
my $n = lc $1;
$n =~ s/\./_/g;
$cur_ins->{name} = $n;
} elsif ($line =~ m/^disassemble: dump_(.+),?$/) {
my $f = $1;
$f =~ s/_mem$//;
$cur_ins->{format} = $f;
}
}
$ins->{delete $cur_ins->{name}} = $cur_ins;
# add layers to the onion
my $ins_masks = {};
foreach my $name (keys %$ins) {
my $mask = $ins->{$name}->{mask};
$ins_masks->{$mask}->{$name} = $ins->{$name};
}
# parse opcode directory
sub parse_opcode_file {
my ($file) = @_;
}
my $opcode_dir = {};
for my $fname (<riscv-opcodes/opcodes-*>) {
$fname =~ m/\/opcodes-(.*)$/;
my $dir = $1;
open my $dirf, $fname or die "Could not open $fname: $!\n";
while(my $line = <$dirf>) {
next if $line =~ m/^\s*$/;
next if $line =~ m/^@/;
next if $line =~ m/^#/;
$line =~ m/^(.+?)\s/;
my $op = $1;
$op =~ s/\./_/g;
if (exists($opcode_dir->{$op})) {
die "instruction registered multiple times: $op\n";
}
$opcode_dir->{$op} = $dir;
}
close $dirf;
}
my $fmt = {
format_r => "FormatR",
format_r2 => "FormatR",
format_i => "FormatI",
format_s => "FormatS",
format_u => "FormatU",
format_j => "FormatJ",
format_b => "FormatB",
format_csr => "FormatCSR",
empty => "FormatEmpty",
unknown => "FormatEmpty /* UNKNOWN */",
};
my $l1 = 0;
my $l2 = 0;
my $supported = {
rv32i => 1,
rv32m => 1,
rv32a => 1,
system => 1,
};
my @accepted = ();
foreach my $name (sort keys %$ins) {
# DEF(addi, FormatI, {
$l2++;
my $format = $fmt->{$ins->{$name}->{format} // "unknown"};
die "unknown format: '$ins->{$name}->{format}'\n" if !$format;
my $dir = $opcode_dir->{$name} or die "unknown instruction origin: $name\n";
next if !exists($supported->{$dir});
$l1++;
print "DEF($name, $format, { // $dir\n NOT_IMPL\n})\n";
}
print "\n";
foreach my $mask (sort keys %$ins_masks) {
my $out = "";
$out .= "ins_masked = ins_word & $mask;\n";
$out .= "switch (ins_masked) {\n";
my $found = 0;
foreach my $name (sort keys %{$ins_masks->{$mask}}) {
# RUN(addi, 0x00000013)
my $dir = $opcode_dir->{$name} or die "unknown instruction origin: $name\n";
next if !exists($supported->{$dir});
my $format = $fmt->{$ins->{$name}->{format} // "unknown"};
$out .= " RUN($name, $ins->{$name}->{data}, ins_$format)\n";
$found++;
}
next if !$found;
$out .= "}\n";
print $out;
}
print "\n";
print "Found $l2 instructions, accepted $l1\n";
close $info;