-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.h
306 lines (267 loc) · 8.83 KB
/
parser.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#ifndef RISIC_V_PARSER_H
#define RISIC_V_PARSER_H
#include <string>
#include <iostream>
unsigned int mem[1000000], PC = 0, reg[32];
unsigned int get_num(const unsigned int &order, const int &l, const int &r) {
return (order & ((1ll << (l + 1)) - 1)) >> r;
}
enum Type {
R = 1, I = 2, S = 3, B = 4, U = 5, J = 6,
};
enum operatorType {
lui = 0b0110111,
auipc = 0b0010111,
jal = 0b1101111,
jalr = 0b1100111,
beq = 0b000,
bne = 0b001,
blt = 0b100,
bge = 0b101,
bltu = 0b110,
bgeu = 0b111,
lb = 0b000,
lh = 0b001,
lw = 0b010,
lbu = 0b100,
lhu = 0b101,
sb = 0b000,
sh = 0b001,
sw = 0b010,
addi = 0b000,
slti = 0b010,
sltiu = 0b011,
xori = 0b100,
ori = 0b110,
andi = 0b111,
slli = 0b001,
srli = 0b101,
srai = 0b101 + (0b100 << 3),
add = 0b000,
sub = 0b000 + (0b100 << 3),
sll = 0b001,
slt = 0b010,
sltu = 0b011,
Xor = 0b100,
srl = 0b101,
sra = 0b101 + (0b100 << 3),
Or = 0b110,
And = 0b111,
load = 0b0000011,
store = 0b0100011
};
int getType(const unsigned int &order) {
int type;
unsigned int opcode = get_num(order, 6, 0);
if (opcode == lui || opcode == auipc) type = U;
else if (opcode == jal) type = J;
else if (opcode == 0b1100011) type = B;
else if (opcode == load || opcode == 0b0010011 || opcode == jalr) type = I;
else if (opcode == 0b0100011) type = S;
else if (opcode == 0b0110011) type = R;
else type = -1;
return type;
}
unsigned int getRd(const unsigned int &order) { return get_num(order, 11, 7); }
unsigned int getFunct3(const unsigned int &order) { return get_num(order, 14, 12); }
unsigned int getFunct7(const unsigned int &order) { return get_num(order, 31, 25); }
unsigned int getRs1(const unsigned int &order) { return get_num(order, 19, 15); }
unsigned int getRs2(const unsigned int &order) { return get_num(order, 24, 20); }
unsigned int getShamt(const unsigned int &order) { return get_num(order, 24, 20); }
unsigned int getImm(const unsigned int &order) {
int type = getType(order);
unsigned int imm;
if (type == I ) {
imm=get_num(order, 31, 20);
if (order>>31) {
for (int i = 31; i >= 12; --i) {
imm += (1 << i);
}
}
return imm;
} else if (type == S) {
return (get_num(order, 30, 25) << 5) + get_num(order, 11, 7);
} else if (type == B) {
imm= (get_num(order, 11, 8) << 1) + (get_num(order, 30, 25) << 5) + (get_num(order, 7, 7) << 11);
if (order>>31) {for (int i = 31; i >= 12; --i) imm += (1 << i);}
return imm;
} else if (type == U) { return get_num(order, 31, 12) << 12; }
else if (type == J) {
imm= (get_num(order, 30, 21) << 1) + (get_num(order, 20, 20) << 11) + (get_num(order, 19, 12) << 12) ;
if (imm >> 19) for (int i = 31; i >= 20; --i) {imm += (1 << i);}
return imm;
}
}
unsigned int getOpcode(const unsigned int &order) { return get_num(order, 6, 0); }
void FormatR(unsigned int order, unsigned int rs1,unsigned int rs2, unsigned int &result) {
unsigned int func = getFunct3(order)+ getFunct7(order);
if (func == add) result = rs1 + rs2;
else if (func == sub) result = rs1 - rs2;
else if (func == sll) result = rs1 << (rs2 & 0b11111);
else if (func == slt) result = (int) rs1 < (int) rs2 ? 1 : 0;
else if (func == sltu) result = rs1 < rs2 ? 1 : 0;
else if (func == Xor) result = rs1 ^ rs2;
else if (func == srl) result = rs1 >> (rs2 & 0b11111);
else if (func == sra) result = (int) rs1 >> (rs2 & 0b11111);
else if (func == Or) result = rs1 | rs2;
else if (func == And) result = rs1 & rs2;
}
void FormatI(unsigned int order,unsigned int rs1,unsigned int &result, unsigned int &pc) {
unsigned int imm= getImm(order); //已经符号位拓展的imm
unsigned int func = getFunct3(order);
unsigned int opcode = getOpcode(order);
if (opcode == jalr) {
unsigned int t = pc + 4;
pc = (rs1 + imm) & ~1;
result = t;
} else {
unsigned int shamt;
if (func == addi) {
result = rs1 + imm;
} else if (func == slti) {
result = (int) rs1 < (int) imm ? 1 : 0;
} else if (func == sltiu) {
result = rs1 < imm ? 1 : 0;
} else if (func == xori) {
result = rs1 ^ imm;
} else if (func == ori) {
result = rs1 | imm;
} else if (func == andi) {
result = rs1 & imm;
} else if (func == slli) {
shamt = getShamt(order);
result = rs1 << shamt;
} else if (func == srli) {
shamt = getShamt(order);
result = rs1 >> shamt;
} else {
if (func + get_num(order,31,25) == srai) {
shamt = getShamt(order);
result = (int) rs1 >> shamt;
}
}
}
}
void FormatB(unsigned int order,unsigned int rs1,unsigned int rs2,unsigned int &pc) {
unsigned int imm= getImm(order);
unsigned int func = getFunct3(order);
if (func == beq) {
if (rs1 == rs2) pc += imm -4;
} else if (func == bne) {
if (rs1 != rs2) pc += imm -4;
} else if (func == blt) {
if ((int) rs1 < (int) rs2) pc += imm -4;
} else if (func == bge) {
if ((int) rs1 >=(int)rs2) pc+=imm -4;
} else if (func == bltu) {
if(rs1<rs2) pc+=imm -4;
} else if (func == bgeu) {
if(rs1>=rs2) pc+=imm -4;
}
pc+=4;
}
void FormatU(unsigned int order, unsigned int &result, unsigned int pc) {
unsigned int imm= getImm(order);
unsigned int opcode = getOpcode(order);
if (opcode == lui) {
result = imm;
} else if (opcode == auipc) {
result = imm + pc;
}
}
void FormatJ(unsigned int order, unsigned int &pc, unsigned int &result = reg[1]) {
unsigned int imm= getImm(order);
unsigned int opcode = getOpcode(order);
if (opcode == jal) {
result = pc + 4;
pc += imm;
}
}
void FormatS(unsigned int order,unsigned int rs1,unsigned int rs2, unsigned int &address,unsigned int &result) {
unsigned int imm= getImm(order);
if (order>>31) {
for (int i = 31; i >= 11; --i) {
imm += (1 << i);
}
}
address=rs1+imm;
result = rs2;
}
void Store(unsigned int order, unsigned int address,unsigned int result){
// std::cout<<"storing "<<address<<" "<<result;
unsigned int func = getFunct3(order);
if (func == sb) {
mem[address] = result & 255u;
} else if (func == sh) {
result= result & ((1<<17)-1);
mem[address + 1] = (result >> 8) & 255u;
mem[address] =result & 255u;
} else if (func == sw) {
mem[address] = result & 255u;
mem[address + 1] = (result >> 8) & 255u;
mem[address + 2] = (result >> 16) & 255u;
mem[address + 3] = (result >> 24) & 255u;
}
}
void getAddress(unsigned int order,unsigned int rs1, unsigned int &address){
address=rs1+getImm(order);
}
void Load(unsigned int order,unsigned int address, unsigned int &result) {
unsigned int func = getFunct3(order);
if (func == lb) {
unsigned int tmp = mem[address];
if (tmp >> 7) {
for (int i = 31; i >= 8; --i) tmp += (1 << i);
}
result = tmp;
} else if (func == lh) {
unsigned int tmp = mem[address] + (mem[address + 1]<<8);
if (tmp >> 15) {
for (int i = 31; i >= 16; --i) tmp += (1 << i);
}
result = tmp;
} else if (func == lw) {
unsigned int tmp = (mem[address] ) | (mem[address + 1] << 8) | (mem[address + 2] << 16) | (mem[address + 3] <<24);
result = tmp;
} else if (func == lbu) {
unsigned int tmp = mem[address]&0b11111111;
result = tmp;
} else if (func == lhu) {
unsigned int tmp = (mem[address+1] << 8) + mem[address ];
result = tmp;
}
}
unsigned int des_to_int(std::string num) { //地址
unsigned int des = 0;
int get[] = {28, 24, 20, 16, 12, 8, 4, 0};
for (int i = 0; i < 8; ++i) {
if ('0' <= num[i] && num[i]<= '9')des += ((num[i]-'0') * (1 << get[i]));
else des += ((num[i] - 'A'+10) * (1 << get[i]));
}
return des;
}
void set_memory(char first, unsigned int des) { //读入内存 改变mem[]
int get[] = {4, 0, 4, 0, 4, 0, 4, 0};
int num;
for (int i = 0; i < 8; ++i) {
if ('0' <= first && first<= '9') num = first - '0';
else num = first - 'A' + 10;
mem[des + i / 2] += (num * (1 << get[i]));
if (i <= 6) std::cin >> first;
}
}
void order_input() { //输入指令
std::string num;
char fir;
unsigned int des;
while (std::cin >> fir) {
if (fir == '@') {
std::cin >> num;
des = des_to_int(num);
} else {
set_memory(fir, des);
des += 4;
}
}
}
#endif //RISIC_V_PARSER_H