-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory_manage.h
115 lines (96 loc) · 3.27 KB
/
memory_manage.h
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
//Copyright 2019 Hust.
//Author: HYL
//Descriptor:
/* This is a head file of memory manage. It is a super class of
* reg_memory_manage class and data_memory_manage class.
*/
#include <unordered_map>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#ifndef _MEMORY_MANAGE_H_
#define _MEMORY_MANAGE_H_
using namespace::std;
/*DataStoreInfo record the allocating infomation In IR format.
*the addr is like the %x. When allocation a acutal memory, we
*should know the data type that decide how much memory should
*be allocate. And store in ROM(data area) or RAM(reg) and so on.
*/
struct DataStoreInfo {
//virtual reg like %x
string virtual_addr;
//i8, i32, i64
string data_type;
//actual register that allocated
vector<string> actual_addr;
//what fun do the value belong,
//if the variable is global, then
//the belong_fun_name is _global.
string belong_which_fun_name;
//store in reg(1) or data area(2)
int store_where_place;
//allocated how many reg(i8/i16)
int actual_addr_num;
//
int unique_index;
//for 2 dimension array
//int 2DArray_first;
//int 2DArray_second;
};
class MemoryManage {
public:
//caculate how many reg will be needed
int HowBigType(string);
void DataTypeCheck(string);
//Return the fun name that is being run now.
string GetBelongWhatCallName();
//split number that can be stored into 8 bit register.
vector<string> GetSplitSectionOfANum(string, int);
//return the index that dedicated the var shtore which map
int VarStoreWhichMap(string);
//set the map
void SetVarStoreWhichMap(string, int);
//set record variable delear
void SetRecordVarDecalare(string, vector<string>);
//get record from the variable_decalare_mape
const vector<string>& GetRecordVarDecalare(string);
string GetCallLevelStack(){
return call_level_stack.top();
}
int GetSizeOfCallLevelStack(){
return call_level_stack.size();
}
//deal with the wihch fun stack
void SetValueToWhichFunStack(string);
string GetValueFromWhichFunStack();
void PopValueFromWhichFunStack();
protected:
//string stand for the var name;
//DataStoreInfo stand for the concrete info of store addr
//unordered_map<string, DataStoreInfo> correspond_map;
private:
//What is call level? It record the caller layer. When A call
//B during run, then B's fun name will be put into call_level.
//When the program come back from B, B'fun name will pop. It
//used to recycle the reg when a fun have been done.
static stack<string> call_level_stack;
//record var store which map
//1 == reg_addr_map
//2 == global_addr_map
//3 == fun_para_map
//4 == data_area_map
static unordered_map<string, int> var_store_which_map;
//this map record some data about variable when the variable
//just decalare without initial. When we decalare a sturct
//type but not decalare a variable. Thus we should record the
//type of this struct. These info will be used when decalare
//a struct variable
static unordered_map<string, vector<string>> variable_decalare_map;
//Which function is processing now
//in every function, the begining IR actual addr is same
//so we shold add the fun name to indentifier same actual
//addr in different fun.
static stack<string> which_fun;
};
#endif