forked from kframework/c-semantics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzeProfile.pl
More file actions
171 lines (161 loc) · 4.17 KB
/
analyzeProfile.pl
File metadata and controls
171 lines (161 loc) · 4.17 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env perl
use strict;
use DBI;
my $RULE_LENGTH = 20;
my $numArgs = $#ARGV + 1;
my $filename = $ARGV[0];
my $runName = $ARGV[1];
#print "$filename\n";
# terrible hack :(
my $dbh = DBI->connect("dbi:SQLite:dbname=kccProfileDB.sqlite","","");
# if ($shouldClean) { $dbh->do("DROP TABLE IF EXISTS data;");}
$dbh->do("CREATE TABLE if not exists data (
runName NOT NULL
, runNum INTEGER NOT NULL
, ruleName NOT NULL
, rule NOT NULL
, locationFile NOT NULL
, locationFrom NOT NULL
, locationTo NOT NULL
, type NOT NULL
, kind NOT NULL
, matches BIGINT NOT NULL
, rewrites BIGINT NOT NULL
, PRIMARY KEY (runName, runNum, rule)
)");
# key can't be locationFile, locationFrom, locationTo, kind because sometimes we don't have that info :(
# , fragment NULL DEFAULT NULL
# , initialTries NULL DEFAULT NULL
# , resolveTries NULL DEFAULT NULL
# , successes NULL DEFAULT NULL
# , failures NULL DEFAULT NULL
# $dbh->do("CREATE INDEX if not exists index_runName on data (
# runName
# , runNum
# )");
$dbh->do("CREATE INDEX if not exists index_rule on data (
locationFile
, locationFrom
, locationTo
, kind
)");
# $dbh->do("CREATE INDEX if not exists index_matches on data (
# matches
# )");
$dbh->do("PRAGMA default_synchronous = OFF");
$dbh->do("PRAGMA synchronous = OFF");
my $runNum = getMaxRunNumFor($runName) + 1;
my $sql = "
INSERT OR REPLACE INTO
data (runName, runNum, ruleName, rule, locationFile, locationFrom, locationTo, type, kind, matches, rewrites)
VALUES (
'$runName'
, $runNum
, ?
, ?
, ?
, ?
, ?
, ?
, ?
, ? - COALESCE((SELECT matches FROM data WHERE runName == 'tmpSemanticCalibration' AND runNum == 1 AND rule = ?), 0)
, ? - COALESCE((SELECT rewrites FROM data WHERE runName == 'tmpSemanticCalibration' AND runNum == 1 AND rule = ?), 0)
)
";
my $insertOpHandle = $dbh->prepare($sql);
# $sql = "INSERT INTO data (rule, type, kind, matches, rewrites) VALUES (?, ?, ?, ?, ?)";
my $insertEqHandle = $insertOpHandle;
#print "Using $filename as input file\n";
open(MYINPUTFILE, "<$filename");
while(<MYINPUTFILE>) {
my $line = $_;
chomp($line);
if ($line =~ m/^op /){
handleOp($line, *MYINPUTFILE);
} elsif ($line =~ m/^(eq|ceq|rl|crl) /){
handleEq($line, *MYINPUTFILE, $1);
} else {
# ignore line
}
}
$dbh->disconnect;
#############################################
sub handleOp {
my ($line, $file) = @_;
my $rule = substr($line, 3);
my $ruleName = "";
$line = <$file>;
if ($line =~ m/built-in eq rewrites: (\d+) \(/){
my $rewrites = $1;
my $retval = $insertOpHandle->execute($ruleName, $rule, "", "", "", 'op', 'builtin', $rewrites, $rule, $rewrites, $rule);
return;
}
}
sub handleEq {
my ($line, $file, $type) = @_;
my $rule = "";
#$line;
my $ruleName = "";
my $kind = 'macro';
my $locationFile = "";
my $locationFrom = "";
my $locationTo = "";
my $matches = 0;
my $rewrites = 0;
my $shouldInsert = 0;
$_ = "$line\n";
do {
$line = $_;
chomp($line);
if ($line =~ m/[\[ ]label ([^ ]+)[\] ]/){ $ruleName = $1; }
if ($line =~ m/filename=\((.*?)\)/) {
$locationFile = $1;
}
if ($line =~ m/location=\((\d+),(\d+),(\d+),(\d+)\)/) {
$locationFrom = $1;
$locationTo = $3;
}
if ($line =~ m/((?:supercool|superheat|cooling|heating|superheated)=\([^\)]*\))/){
$kind = $1;
} elsif ($line =~ m/(structural|computational)/) {
$kind = $1;
}
if ($line =~ m/^rewrites: (\d+) \(/){
$rewrites = $1;
$matches = $rewrites;
$shouldInsert = 1;
# print "normal\n";
} elsif ($line =~ m/^lhs matches: (\d+) rewrites: (\d+) \(/){
$matches = $1;
$rewrites = $2;
$shouldInsert = 1;
# print "conditional\n";
}
if ($shouldInsert) {
my $retval = $insertEqHandle->execute(
$ruleName,
$rule,
$locationFile,
$locationFrom,
$locationTo,
$type,
$kind,
$matches, $rule,
$rewrites, $rule
);
return;
}
$rule .= "$line\n";
} while (<$file>);
}
sub getMaxRunNumFor {
my ($runName) = (@_);
my $sth = $dbh->prepare("
SELECT max(runNum) as maxNum
FROM data a
WHERE runName like '$runName'
") or die $dbh->errstr;
$sth->execute();
my $hash_ref = $sth->fetchrow_hashref or die "Problem figuring max run for '$runName'";
return $hash_ref->{maxNum};
}