-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.cpp
More file actions
141 lines (126 loc) · 3.21 KB
/
func.cpp
File metadata and controls
141 lines (126 loc) · 3.21 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
//
// Created by zfq on 2021/3/6.
//
#include "func.h"
#include <iostream>
bool func::is_empty() {
return size==0;
}
void func::clear(){
label="";
content=vector<string>();
size=0;
address=0;
}
vector<func> process_text(ifstream &infile){
vector <func> funcs=write_func(infile);
for(func i : funcs){
if (i.label=="main"){
swap(i,funcs[0]);
}
}
return funcs;
}
vector<func> write_func(ifstream & infile){
string line;
vector<func> functions;
func f;
f.label="plain_text";
static int count=0;
while (true) {
if(count==0) {
if( getline(infile,line)) {
line = modify(line);
} else{break;}
}
///status decides if this is a data segment or text segment
static bool status = true;
///read nonempty line
if (!line.empty()) {
if (line.find(".text") < line.length()) {
status = true;
continue;
}
else if (line.find(".data") < line.length()) {
if (count>0){break;}
status = false;
continue;
}
if (status){
if(line.find(':') < line.length()) {
if(!f.is_empty()){
functions.push_back(f);
f.clear();
}
line=modify(line);
if (line.find(':') == line.length() - 1) {
f.label = line.substr(0, line.length() - 1);
} else {
f.label = line.substr(0, line.find(':'));
f.content.push_back(line.substr(line.find(':') + 1, line.length() - 1));
f.size++;
}
tuple<func,string>tp = write(f, infile);
f=get<0>(tp);
// functions.push_back(f);
count+=1;
line=get<1>(tp);
if (line.empty()){
// f.clear();
break;
}
}
else{
f.content.push_back(line);
f.size++;
}
}
}
}
if(!f.is_empty()){functions.push_back(f);}
return functions;
}
tuple<func,string> write(func f,ifstream &infile){
string line;
while (getline(infile,line)){
line=modify(line);
if(line.find(".data")<line.length()){break;}
if(line.find(':')<line.length()){
tuple<func,string>tp(f,line);
return tp;
}
if (line.empty()){continue;}
f.content.push_back(line);
f.size++;
}
tuple<func,string>tp(f,"");
return tp;
}
string modify(string line){
line=line.substr(0,line.find(35));
if(line.empty()){return line;}
stringstream word(line);
string temp;
string k;
while (word>>k){
temp+=k;
if(!k.empty()&&k.at(k.length()-1)!=','){
temp+=',';
}
}
if (!temp.empty()&&temp.at(temp.length()-1)==','){
temp=temp.substr(0,temp.length()-1);
}
return temp;
}
map<string,int> addalloc(vector<func> &funcs){
map<string,int> func_address_set;
for(int i=0;i<funcs.size();i++){
if (i==0){funcs[i].address=0;}
else{
funcs[i].address=funcs[i-1].address+funcs[i-1].size;
}
func_address_set.insert(pair<string,int>(funcs[i].label,funcs[i].address));
}
return func_address_set;
}