-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_customasm_rules.pl
executable file
·73 lines (68 loc) · 2.21 KB
/
gen_customasm_rules.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
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
my %name_map = (
"REGA" => "A",
"REGB" => "B",
"REGC" => "C",
"PCNT" => "PCnt",
"MADR" => "MAdr",
"STCK" => "Stack",
"OUTA" => "OutA",
"OUTB" => "OutB",
"SWAP" => "Swap",
"INPA" => "InA",
"INPB" => "InB",
"PCNT_BSELECT" => "HighB",
"MADR_BSELECT" => "HighB",
);
my %flags_mapping = (
"UNCONDITIONAL" => "",
"ZF|ZF_CF" => "Z",
"CF|ZF_CF" => "C",
"CF|NO_FLAGS" => "NZ",
"ZF|NO_FLAGS" => "NC",
);
sub mappedName {
my $name = shift;
if (!defined $name) {
return "";
}
if ($name =~ /^\d+$/) {
return $name;
}
return $name_map{$name};
}
my $instructions_file = `cat Code/instruction_set.cpp` or die "Couldn't read instructions file.\n";
if ($instructions_file =~ /Instruction instructions\[\] = \{(.+?)\}/s) {
my $instructions_block = $1;
my $index = 0;
my @rules;
while ($instructions_block =~/Type::(\w+?),.*?FlagsMask::(\w+)(?:\s*\|.*FlagsMask::(\w+))?,\s*(\d)(?:, (\w+))?(?:, (\w+))?(?:, (\w+))?/g) {
my $arg_template = "";
my $arg_value = "";
my $flags1 = $2;
my $flags2 = $3 ? "|$3" : "";
my $flags_suffix = $flags_mapping{"$flags1$flags2"};
my $inst = "$1$flags_suffix";
my $data_bytes = $4;
my $var1 = mappedName($5);
my $var2 = defined $6 ? ", ".mappedName($6) : "";
my $var3 = defined $7 ? ", ".mappedName($7) : "";
my $vars = "$var1$var2$var3";
if ($data_bytes != 0) {
$arg_template = $data_bytes == 1 ? "{value}" : "{address}";
$arg_value = $data_bytes == 1 ? "\@ value`8" : "\@ address`16";
if ($data_bytes == 3) {
$arg_template = "{address} {value}";
$arg_value = "\@ address`16 \@ value`8";
}
}
my $rule = sprintf("%-14s %-20s %-20s => 0x%02X %s", $inst, $vars, $arg_template, $index, $arg_value);
push(@rules, ($rule));
$index++;
}
print "#ruledef {\n " . join("\n ", @rules) . "\n}\n";
} else {
print "Couldn't find instruction block.\n";
}