-
Notifications
You must be signed in to change notification settings - Fork 2
/
Value.h
285 lines (229 loc) · 5.71 KB
/
Value.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
#ifndef INTERPRETER_VALUE_H
#define INTERPRETER_VALUE_H
#include "build.h"
// -------------------------------------------------------------------------------------
// Library Homework
// File Value.h
// Author Ivan Shapovalov <intelfx100@gmail.com>
// Description Interpreter's uniform value class implementation.
// -------------------------------------------------------------------------------------
namespace Processor
{
typedef struct Value
{
union
{
fp_t fp;
int_t integer;
};
enum Type
{
V_INTEGER = 0,
V_FLOAT,
V_MAX
} type;
Value() :
type( V_MAX )
{
}
Value( fp_t src ) :
type( V_FLOAT )
{
fp = src;
}
Value( int_t src ) :
type( V_INTEGER )
{
integer = src;
}
inline Type Expect( Type required_type, bool allow_uninitialised = false ) const;
// Verify type equality and assign contents of another value object to this.
// Should not be used with registers, since they are untyped in JIT mode.
inline void Assign( const Value& that ) {
s_cassert( that.type != V_MAX, "Attempt to assign an uninitialised value" );
switch( Expect( that.type ) ) {
case V_FLOAT:
fp = that.fp;
break;
case V_INTEGER:
integer = that.integer;
break;
case V_MAX:
break;
default:
s_casshole( "Switch error" );
break;
}
}
// Verify type equality and assign the correct value to given reference.
// Pass "V_MAX" as type to disable type checking.
// API should use "allow_uninitialised" switch to read data from registers (since they are untyped in JIT mode).
template <typename T>
void Get( Type required_type, T& dest, bool allow_uninitialised = false ) const
{
switch( Expect( required_type, allow_uninitialised ) ) {
case V_FLOAT:
dest = fp;
break;
case V_INTEGER:
dest = integer;
break;
case V_MAX:
default:
s_casshole( "Switch error" );
break;
}
}
abiret_t GetABI() const
{
switch( type ) {
case V_INTEGER: {
int_abi_t tmp = integer;
return reinterpret_cast<abiret_t&>( tmp );
}
case V_FLOAT: {
fp_abi_t tmp = fp;
fp_abi_t* ptmp = &tmp;
return **reinterpret_cast<abiret_t**>( ptmp );
}
case V_MAX:
s_casshole( "Uninitialised value is being read" );
break;
default:
s_casshole( "Switch error" );
break;
}
return 0;
}
void SetFromABI( abiret_t value )
{
abiret_t* pvalue = &value;
switch( type ) {
case V_INTEGER:
integer = **reinterpret_cast<int_abi_t**>( &pvalue );
break;
case V_FLOAT:
fp = **reinterpret_cast<fp_abi_t**>( &pvalue );
break;
case V_MAX:
s_casshole( "Uninitialised value is being set from ABI data" );
break;
default:
s_casshole( "Switch error" );
break;
}
}
// Verify type equality and set correct value from given object.
// Pass "V_MAX" as type to disable type checking.
// API should use "allow_uninitialised" switch to write data to registers (since they are untyped in JIT mode).
template <typename T>
void Set( Type required_type, const T& src, bool allow_uninitialised = false )
{
switch( Expect( required_type, allow_uninitialised ) ) {
case V_FLOAT:
fp = src;
break;
case V_INTEGER:
integer = src;
break;
case V_MAX:
default:
s_casshole( "Switch error" );
break;
}
}
// Write type and data to the value.
template <typename T>
void Write( Type required_type, const T& src )
{
switch( type = required_type ) {
case V_FLOAT:
fp = src;
break;
case V_INTEGER:
integer = src;
break;
case V_MAX:
default:
s_casshole( "Switch error" );
break;
}
}
static int_t IntParse( const char* string )
{
char* endptr;
unsigned char char_representation;
long result;
if( ( string[0] == '\'' ) &&
( char_representation = string[1] ) &&
( string[2] == '\'' ) &&
!string[3] ) {
result = static_cast<long>( char_representation );
}
else {
errno = 0;
result = strtol( string, &endptr, 0 ); /* base autodetermine */
s_cverify( !errno && ( endptr != string ) && ( *endptr == '\0' ),
"Malformed integer: \"%s\": non-ASCII and strtol() says \"%s\"",
string, strerror( errno ) );
}
return result;
}
static fp_t FPParse( const char* string )
{
char* endptr;
fp_t result;
int classification;
errno = 0;
result = strtof( string, &endptr );
s_cverify( !errno && ( endptr != string ) && ( *endptr == '\0' ),
"Malformed floating-point: \"%s\": strtof() says \"%s\"",
string, strerror( errno ) );
classification = fpclassify( result );
s_cverify( classification == FP_NORMAL || classification == FP_ZERO,
"Invalid floating-point value: \"%s\" -> %lg", string, result );
return result;
}
void Parse( const char* string )
{
switch( type ) {
case V_INTEGER:
integer = IntParse( string );
break;
case V_FLOAT:
fp = FPParse( string );
break;
case V_MAX:
s_casshole( "Uninitialised value" );
break;
default:
s_casshole( "Switch error" );
break;
}
}
} calc_t;
namespace ProcDebug
{
INTERPRETER_API std::string Print( Value::Type arg );
} // namespace ProcDebug
Value::Type Value::Expect( Value::Type required_type, bool allow_uninitialised ) const
{
if( !allow_uninitialised )
s_cverify( type != V_MAX, "Cannot access uninitialised value" );
else if( type == V_MAX ) {
s_cassert( required_type != V_MAX, "Cannot autodetermine type: value is uninitialised" );
return required_type;
}
if( type == V_MAX ) {
return required_type;
}
if( required_type == V_MAX ) {
return type;
}
s_cverify( type == required_type,
"Cannot operate on non-matching types (expected \"%s\" instead of \"%s\")",
ProcDebug::Print( required_type ).c_str(), ProcDebug::Print( type ).c_str() );
return type;
}
} // namespace Processor
#endif // INTERPRETER_VALUE_H