-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConstantGenerator.hpp
232 lines (172 loc) · 6.96 KB
/
ConstantGenerator.hpp
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
#ifndef VM_Component_ConstantGenerator
#define VM_Component_ConstantGenerator
#include <stdint.h>
#include "HwError.hpp"
#include "Component.hpp"
#include "InstructionType.hpp"
namespace Component
{
class ConstantGenerator : public Component_t
{
private:
uint64_t GenerateConst( Instruction::ParsedIType_Null instruction, bool SignExtend )
{
return instruction.garb;
}
uint64_t GenerateConst( Instruction::ParsedIType_R instruction, bool SignExtend )
{
// No constant to generate
return 0;
}
uint64_t GenerateConst( Instruction::ParsedIType_I instruction, bool SignExtend )
{
uint8_t signBit = instruction.imm0to11 >> 11;
uint64_t result = instruction.imm0to11;
if( SignExtend && (signBit != 0) )
{
result |= ~(uint64_t)0xfff;
}
return result;
}
uint64_t GenerateConst( Instruction::ParsedIType_S instruction, bool SignExtend )
{
typedef struct __attribute__ ((__packed__))
{
uint8_t imm0to4 : 5;
uint8_t imm5to11 : 7;
uint64_t imm12to63 : 52;
} intermS;
intermS interm = { instruction.imm0to4, instruction.imm5to11, 0 };
uint64_t result = *(uint64_t*) &interm;
uint8_t signBit = (uint8_t) (result >> 11);
if( SignExtend && (signBit != 0) )
{
result |= ~(uint64_t)0xfff;
}
return result;
}
uint64_t GenerateConst( Instruction::ParsedIType_SB instruction, bool SignExtend )
{
typedef struct __attribute__ ((__packed__))
{
uint8_t imm11 : 1;
uint8_t imm1to4 : 4;
} intermSBdecomp1;
typedef struct __attribute__ ((__packed__))
{
uint8_t imm5to10 : 6;
uint8_t imm12 : 1;
} intermSBdecomp2;
typedef struct __attribute__ ((__packed__))
{
uint8_t imm0 : 1;
uint8_t imm1to4 : 4;
uint8_t imm5to10 : 6;
uint8_t imm11 : 1;
uint8_t imm12 : 1;
uint64_t imm13to63 : 51;
} intermSBtogether;
uint8_t imm11and1to4 = instruction.imm11and1to4;
uint8_t imm5to10and12 = instruction.imm5to10and12;
intermSBdecomp1 dec1 = *( intermSBdecomp1* ) &imm11and1to4;
intermSBdecomp2 dec2 = *( intermSBdecomp2* ) &imm5to10and12;
intermSBtogether result = { 0, dec1.imm1to4, dec2.imm5to10, dec1.imm11, dec2.imm12, 0 };
uint64_t signExtendedResult = *(uint64_t*) &result;
uint8_t signBit = signExtendedResult >> 12;
if( SignExtend && (signBit != 0) )
{
signExtendedResult |= ~(uint64_t)0xfff;
}
return signExtendedResult;
}
uint64_t GenerateConst( Instruction::ParsedIType_U instruction, bool SignExtend )
{
uint64_t result = ((uint64_t) instruction.imm12to31) << 12;
uint8_t signBit = (uint8_t) (result >> 31);
if( SignExtend && (signBit != 0) )
{
result |= ~(uint64_t)0xffffffff;
}
return result;
}
uint64_t GenerateConst( Instruction::ParsedIType_UJ instruction, bool SignExtend )
{
typedef struct __attribute__ ((__packed__))
{
uint8_t imm12to19 : 8;
uint8_t imm11 : 1;
uint16_t imm1to10 : 10;
uint8_t imm20 : 1;
uint16_t imm21to32 : 12; // 0 padding
} intermUJdecomp;
typedef struct __attribute__ ((__packed__))
{
uint8_t imm0 : 1;
uint16_t imm1to10 : 10;
uint8_t imm11 : 1;
uint8_t imm12to19 : 8;
uint8_t imm20 : 1;
uint64_t imm21to63 : 43; // 0 padding
} intermUJtogether;
uint32_t imm12to19and11and1to10and20 = instruction.imm12to19and11and1to10and20;
intermUJdecomp decomp = *(intermUJdecomp*) &imm12to19and11and1to10and20;
intermUJtogether result = { 0, decomp.imm1to10, decomp.imm11, decomp.imm12to19, decomp.imm20 };
uint64_t signExtendedResult = *(uint64_t*) &result;
uint8_t signBit = signExtendedResult >> 20;
if( SignExtend && (signBit != 0) )
{
signExtendedResult |= ~(uint64_t)0xfffff;
}
return signExtendedResult;
}
public:
Component_ID GetID() override
{
return Component_ID_ConstantGenerator;
}
uint64_t GenerateConst( Instruction::ParsedInstruction instr, bool SignExtend )
{
static void * DispatchTable[] = { &&Case_Null, &&Case_R, &&Case_I, &&Case_S, &&Case_SB, &&Case_U, &&Case_UJ };
SetLastHwError( HwError_NoError );
uint8_t instrTypeOffset = (uint8_t) instr.Type;
if( instrTypeOffset < Instruction::NumInstructionTypes )
goto *DispatchTable[instrTypeOffset];
else
goto Case_Bad;
Case_Null:
{
return GenerateConst( instr.ParsedInst.Null, SignExtend );
};
Case_R:
{
return GenerateConst( instr.ParsedInst.R, SignExtend );
};
Case_I:
{
return GenerateConst( instr.ParsedInst.I, SignExtend );
};
Case_S:
{
return GenerateConst( instr.ParsedInst.S, SignExtend );
};
Case_SB:
{
return GenerateConst( instr.ParsedInst.SB, SignExtend );
};
Case_U:
{
return GenerateConst( instr.ParsedInst.U, SignExtend );
};
Case_UJ:
{
return GenerateConst( instr.ParsedInst.UJ, SignExtend );
};
Case_Bad:
{
SetLastHwError( HwError_UnknownInstructionType );
return 0;
};
}
};
};
#endif