/
df_live_var.cc
96 lines (75 loc) · 2.18 KB
/
df_live_var.cc
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
#include "df_live_var.h"
#include "tac.h"
#include <string>
#include <list>
#include <iostream>
#include <vector>
#include <sstream>
#include <algorithm>
class Instruction;
void df_live_var::get_live_locations(std::list<Instruction*> stmt, int id)
{
std::string str_tmp ("=");
size_t found;
std::map<std::string, std::pair<int,int> > current_live_vars;
std::vector<std::string> fails;
std::string what_temp = "";
std::list<Instruction*>::iterator it;
for(it=stmt.begin(); it != stmt.end(); it++)
{
found = (*it)->command().find(str_tmp);
if(found!=std::string::npos)
{
std::string str_varName ("tmp");
found = (*it)->command().find(str_varName);
int beg = (*it)->command().find_first_of("tmp");
int end = (*it)->command().find_last_of("tmp");
what_temp = (*it)->command().substr(beg,end+1);
fails.push_back( what_temp );
}
}
// remove duplicates
fails.erase( unique(fails.begin(), fails.end() ), fails.end() );
/*for(int i=0; i < fails.size(); i++)
{
std::cout << fails[i] << std::endl;
}*/
// handle one at a time
while( !fails.empty() )
{
int line = 1;
int start;
int end;
bool got_one = false;
bool got_end = false;
std::string what_temp = "";
std::list<Instruction*>::iterator it;
for(it=stmt.begin(); it != stmt.end(); it++)
{
if(got_one)
{
got_end = true;
}
found = (*it)->command().find( fails.at( fails.size()-1 ) );
if (!got_one && !got_end && found!=std::string::npos)
{
start = line;
//std::cout << "start: " << start << std::endl;
//std::cout << "variable: " << (*it)->command() << std::endl;
got_one = true;
}
found = (*it)->command().find( fails.at( fails.size()-1 ) );
if(got_one && got_end && found!=std::string::npos)
{
end = line;
//std::cout << "end: " << end << std::endl;
//std::cout << "variable: " << (*it)->command() << std::endl;
break;
}
line += 1;
}
current_live_vars.insert( std::pair<std::string, std::pair<int,int> >(fails.at(fails.size()-1), std::pair<int,int>(start,end) ));
fails.pop_back();
}
variable_timeline.insert( std::pair<int, std::map<std::string, std::pair<int,int> > > (id,current_live_vars) );
}