-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_manage.cpp
183 lines (153 loc) · 4.07 KB
/
memory_manage.cpp
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//Copyright 2019 Hust.
//Author: HYL
//Descriptor:
/*
*/
#include <cstring>
#include <regex>
#include "memory_manage.h"
#include "tool_fun.h"
stack<string> MemoryManage::call_level_stack;
unordered_map<string, int> MemoryManage::var_store_which_map;
unordered_map<string, vector<string>> MemoryManage::variable_decalare_map;
stack<string> MemoryManage::which_fun;
//caculate how many reg will need
int MemoryManage::HowBigType(string var_type){
//char type
regex i8_regex("i8.*");
regex i16_regex("i16.*");
regex i32_regex("i32.*");
regex i64_regex("i64.*");
regex ptr_regex(".+\\*.*");
if(!var_type.compare("i8")){
return 1;
}
else if(!var_type.compare("i16")){
return 1;
}
else if(!var_type.compare("i32")){
return 2;
}
else if(!var_type.compare("i64")){
return 4;
}
else if(!var_type.compare("float")){
return 4;
}
else if(!var_type.compare("double")){
return 4;
}
else if(regex_match(var_type, ptr_regex)){
return 2;
}
/*
//short int
else if(regex_match(var_type, i16_regex)){
return 2;
}
//int, long int,
else if(regex_match(var_type, i32_regex)){
return 2;
}
//long long int,
else if(regex_match(var_type, i64_regex)){
return 4;
}*/
return 0;
}
//return the first element in call_level_stack
string MemoryManage::GetBelongWhatCallName(){
if(GetSizeOfCallLevelStack() == 0){
return " ";
}
return GetCallLevelStack();
}
//when a constant number cant't store in 8 bit reg, then we should
//split it and store it more than ont reg. Firstly, we change string
//type src_val_str to int type data.
vector<string> \
MemoryManage::GetSplitSectionOfANum(string src_val_str,\
int var_type){
long long int src_val = ChangeStrToDec(src_val_str);
vector<string> res;
regex reg_minus("-.*");
//src_val_str is null
if(src_val_str.empty()){
for(int i = 0; i < var_type; i++){
res.push_back("0");
}
return res;
}
//if the src_val is minus data
if(regex_match(src_val_str, reg_minus)){
//deal with 8 bit char type minus data
if(var_type == 1){
src_val = 256 + src_val;
// src_val = src_val - 128;
}
//deal with 16 bits type minus data
else if(var_type == 2){
src_val = 65536 + src_val;
}
//deal with 32 bit int type minus data
else if(var_type == 4){
long long int limit_num1 = 4294967296;
src_val = 4294967296 + src_val;
}
else if(var_type == 8){
//long long int limit_num = 18446744073709552000;
//src_val = limit_num + src_val;
}
}
long long int temp = src_val;
while(temp >= 256){
long long int mod = temp % 256;
temp /= 256;
res.push_back(to_string(mod));
}
res.push_back(to_string(temp));
//align the bit between res and reg number
for(int i = res.size(); i < var_type; i++){
res.push_back("0");
}
return res;
}
int MemoryManage::VarStoreWhichMap(string var_name){
if(var_store_which_map.find(var_name) == var_store_which_map.end()){
throw "No this Variable!";
}
int res = var_store_which_map.find(var_name)->second;
return res;
}
void MemoryManage::SetVarStoreWhichMap(string var_name, int index_num){
if(var_store_which_map.find(var_name) != var_store_which_map.end()){
return ;
}
var_store_which_map.insert(make_pair(var_name, index_num));
}
void MemoryManage::SetRecordVarDecalare(string var_name,\
vector<string> type_vec){
variable_decalare_map.insert(make_pair(var_name, type_vec));
}
const vector<string>& \
MemoryManage::GetRecordVarDecalare(string var_name){
//vector<string> &res = \
// variable_decalare_map.find(var_name)->second;
//return res;
return variable_decalare_map.find(var_name)->second;
}
void MemoryManage::SetValueToWhichFunStack(string fun_name){
which_fun.push(fun_name);
}
string MemoryManage::GetValueFromWhichFunStack(){
string fun_name;
if(which_fun.empty()){
return fun_name;
}
fun_name = which_fun.top();
return fun_name;
}
void MemoryManage::PopValueFromWhichFunStack(){
if(which_fun.empty()) return;
which_fun.pop();
}