-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cc
102 lines (98 loc) · 2.55 KB
/
util.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
97
98
99
100
101
102
#include <cparse.hh>
std::unordered_map<CMPCode, std::string> CMPtoString {
{CMPCode::LT, "lt"},
{CMPCode::MT, "mt"},
{CMPCode::LTEQ, "lteq"},
{CMPCode::MTEQ, "mteq"},
{CMPCode::EQ, "eq"},
{CMPCode::NEQ, "neq"}
};
std::unordered_map<AVMOpcode, std::string> OpcodeToString {
{AVMOpcode::ADD, "add"},
{AVMOpcode::SUB, "sub"},
{AVMOpcode::MUL, "mul"},
{AVMOpcode::DIV, "div"},
{AVMOpcode::MOD, "mod"},
{AVMOpcode::SLL, "sll"},
{AVMOpcode::ASR, "asr"},
{AVMOpcode::SLR, "slr"},
{AVMOpcode::AND, "and"},
{AVMOpcode::XOR, "xor"},
{AVMOpcode::ORR, "orr"}
};
std::unordered_map<ASTop, AVMOpcode> ASTOperationToAVM {
{A_ADD, AVMOpcode::ADD},
{A_SUB, AVMOpcode::SUB},
{A_MULT, AVMOpcode::MUL},
{A_DIV, AVMOpcode::DIV},
{A_MODULO, AVMOpcode::MOD},
{A_SLL, AVMOpcode::SLL},
{A_ASR, AVMOpcode::ASR},
{A_SLR, AVMOpcode::SLR},
{A_AND, AVMOpcode::AND},
{A_XOR, AVMOpcode::XOR},
{A_OR, AVMOpcode::ORR},
{A_LAND, AVMOpcode::AND},
{A_LOR, AVMOpcode::ORR},
{A_LNOT, AVMOpcode::NOP}
};
std::string mapOptoString(AVMOpcode op) {
auto value = OpcodeToString.find(op);
if (value == OpcodeToString.end())
return {};
return value->second;
}
std::string mapConditionCodetoString(CMPCode code) {
auto value = CMPtoString.find(code);
if (value == CMPtoString.end())
return {};
return value->second;
}
AVMOpcode toAVM(ASTop op)
{
auto value = ASTOperationToAVM.find(op);
if (value == ASTOperationToAVM.end())
return AVMOpcode::NOP;
return value->second;
}
CMPCode toCMPCode(ASTop op)
{
switch (op) {
case A_MT: {
return CMPCode::MT;
}
case A_LT: {
return CMPCode::LT;
}
case A_EQ: {
return CMPCode::EQ;
}
case A_NEQ: {
return CMPCode::NEQ;
}
case A_MTEQ: {
return CMPCode::MTEQ;
}
case A_LTEQ: {
return CMPCode::LTEQ;
}
}
return CMPCode::NC;
}
bool ASTopIsBinOpAVM(ASTop op) {
return (
op == A_MULT
|| op == A_AND
|| op == A_LAND
|| op == A_LOR
|| op == A_DIV
|| op == A_MODULO
|| op == A_ADD
|| op == A_SUB
|| op == A_SLL
|| op == A_ASR
|| op == A_SLR
|| op == A_OR
|| op == A_XOR
);
}