-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.cpp
More file actions
155 lines (144 loc) · 3.6 KB
/
Copy pathUtility.cpp
File metadata and controls
155 lines (144 loc) · 3.6 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
#include "Utility.h"
#include <random>
#include "AST.h"
#include <cstring>
#include <string>
#include "Constants.h"
ofstream output;
default_random_engine e(0);
string ToString(ASTNodeType type)
{
switch (type)
{
case ASTNodeType::None: return "None";
case ASTNodeType::ASSIGN: return "Assign";
case ASTNodeType::BLOCK: return "Blobkc";
case ASTNodeType::IF: return "If";
case ASTNodeType::WHILE: return "While";
case ASTNodeType::CONTINUE: return "Continue";
case ASTNodeType::BREAK: return "Break";
case ASTNodeType::RETURN: return "Return";
case ASTNodeType::FUNCCALL: return "Funcall";
case ASTNodeType::VARDEF: return "Vardef";
case ASTNodeType::FUNCDEF: return "Funcdef";
default: return "Unknown Type";
}
}
void InitOutputStream(const char* path)
{
output.open(path);
if (!output.is_open())
error_sys("In InitOutputStream: output stream open failed");
}
void CloseOutputStream()
{
output.close();
}
string GetLabel(int cnt)
{
return ".mylabel" + to_string(cnt);
}
int GetRandomInt(int l, int r)
{
/*auto t = time(0);
cout << "time is :" << t << endl;*/
//static default_random_engine e(0);
uniform_int_distribution<int> u(l, r);
//int ran = u(e);//为什么会获取到ran=r的情况?
//while (ran > r)
// ran -= (r - l + 1);
return u(e);
}
string RandomFromSet(const set<RegisterInfo>& s)
{
if (s.empty())
error_sys("In RandomFromSet: set is empty");
vector<string>names;
vector<int>freqs;
int max_freq = 1;
for(auto i = s.begin(); i != s.end(); ++i)
{
names.push_back((*i).name);
freqs.push_back((*i).freq);
max_freq = max(max_freq, freqs.back());
}
for (auto& i : freqs)
i = max_freq - i + 1;
discrete_distribution<> d(freqs.begin(), freqs.end());
return names[d(e)];
}
string TypeConvert(ValueType src_type, string src_reg, ValueType res_type)
{
string target_reg;
if (res_type == ValueType::INT && src_type == ValueType::FLOAT)
{
target_reg = CheckAndSetRegister_INT(target_reg);
output << "\tfcvt.w.s " << target_reg << ", " << src_reg << ", rtz" << endl;
RestoreRegister(src_reg);
}
else if (res_type == ValueType::FLOAT && src_type == ValueType::INT)
{
target_reg = CheckAndSetRegister_FLOAT(target_reg);
output << "\tfcvt.s.w " << target_reg << ", " << src_reg << endl;
RestoreRegister(src_reg);
}
else
return src_reg;
return target_reg;
}
string DecimalFloatToHex(float num) {
unsigned int* hvalue;
hvalue = (unsigned int*)#
char ptr[32] = {};
snprintf(ptr, 32, "%X", *hvalue);
return ptr;
}
void PrintStoreVariableToMemory(const string& name, Info& info)
{
if ((info.address_sp == -1 && !info.is_global) || info.reg.empty())
return;
if (!info.dimensions.empty() && info.dimensions[0] == -1)
{
PrintCommandRelativeToSP("sd", info.reg, info.address_sp);
}
else if (info.is_global)
{
if (info.type == ValueType::FLOAT)
{
output << "\tlla s0, " << name << "\n";
output << "\tfsw " << info.reg << ", 0(s0)\n";
}
else
{
output << "\tlla s0, " << name << "\n";
output << "\tsw " << info.reg << ", 0(s0)\n";
}
}
else
{
if (info.type == ValueType::FLOAT)
{
PrintCommandRelativeToSP("fsw", info.reg, info.address_sp);
}
else
{
PrintCommandRelativeToSP("sw", info.reg, info.address_sp);
}
}
info.reg = "";
}
void PrintCommandRelativeToSP(string command, string reg, int offset)
{
if (offset > 2047 || offset < -2048)
{
if (reg == "s0")
error_sys("In PrintCommandRelativeToSP: the target_reg is equal to s0");
output << "\tli s0, " << offset << endl;
output << "\tadd s0, s0, sp\n";
output << '\t' << command << " " << reg << ", 0(s0)\n";
}
else
{
output << '\t' << command << " " << reg << ", " << offset << "(sp)\n";
}
}