/
PyrSlot64.h
284 lines (229 loc) · 8.06 KB
/
PyrSlot64.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
/*
SuperCollider real time audio synthesis system
Copyright (c) 2002 James McCartney. All rights reserved.
Copyright (c) 2009 Tim Blechmann
http://www.audiosynth.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#pragma once
// generic pyrslot implementation
#define PYR_SLOTS_GENERIC
#include "SC_Endian.h"
#include "SC_Types.h"
#include "PyrErrors.h"
#include <cstddef>
#include <cassert>
#include <vector>
struct PyrSymbol;
enum {
tagNotInitialized, // uninitialized slots have a tag of 0
tagObj,
tagInt,
tagSym,
tagChar,
tagNil, // nil, false, and true are indicated by the tag alone.
tagFalse,
tagTrue,
tagPtr,
/* anything else is a double */
tagFloat,
tagUnused,
};
typedef struct pyrslot {
long tag;
union {
int64 c; /* char */
int64 i;
double f;
void* ptr;
struct PyrObject* o;
PyrSymbol* s;
struct PyrMethod* om;
struct PyrBlock* oblk;
struct PyrClass* oc;
struct PyrFrame* of;
struct PyrList* ol;
struct PyrString* os;
struct PyrInt8Array* ob;
struct PyrDoubleArray* od;
struct PyrSymbolArray* osym;
struct PyrProcess* op;
struct PyrThread* ot;
struct PyrInterpreter* oi;
} u;
} PyrSlot;
/* tag setter function */
inline int GetTag(const PyrSlot* slot) { return slot->tag; }
/* tag checking functions */
inline bool IsObj(const PyrSlot* slot) { return slot->tag == tagObj; }
inline bool NotObj(const PyrSlot* slot) { return slot->tag != tagObj; }
inline bool IsNil(const PyrSlot* slot) { return slot->tag == tagNil; }
inline bool NotNil(const PyrSlot* slot) { return slot->tag != tagNil; }
inline bool IsFalse(const PyrSlot* slot) { return slot->tag == tagFalse; }
inline bool IsTrue(const PyrSlot* slot) { return slot->tag == tagTrue; }
inline bool IsSym(const PyrSlot* slot) { return slot->tag == tagSym; }
inline bool NotSym(const PyrSlot* slot) { return slot->tag != tagSym; }
inline bool IsChar(const PyrSlot* slot) { return slot->tag == tagChar; }
inline bool NotChar(const PyrSlot* slot) { return slot->tag != tagChar; }
inline bool IsInt(const PyrSlot* slot) { return slot->tag == tagInt; }
inline bool NotInt(const PyrSlot* slot) { return slot->tag != tagInt; }
inline bool IsFloat(const PyrSlot* slot) { return slot->tag == tagFloat; }
inline bool NotFloat(const PyrSlot* slot) { return slot->tag != tagFloat; }
inline bool IsPtr(const PyrSlot* slot) { return slot->tag == tagPtr; }
inline bool NotPtr(const PyrSlot* slot) { return slot->tag != tagPtr; }
/* setter functions */
inline void SetInt(PyrSlot* slot, int val) {
slot->tag = tagInt;
slot->u.i = val;
}
inline void SetObject(PyrSlot* slot, struct PyrObjectHdr* val) {
slot->tag = tagObj;
slot->u.o = (struct PyrObject*)(val);
}
inline void SetSymbol(PyrSlot* slot, PyrSymbol* val) {
slot->tag = tagSym;
slot->u.s = val;
}
inline void SetChar(PyrSlot* slot, char val) {
slot->tag = tagChar;
slot->u.c = val;
}
inline void SetPtr(PyrSlot* slot, void* val) {
slot->tag = tagPtr;
slot->u.ptr = (void*)val;
}
inline void SetObjectOrNil(PyrSlot* slot, struct PyrObject* val) {
if (val) {
slot->tag = tagObj;
slot->u.o = val;
} else {
slot->tag = tagNil;
slot->u.i = 0;
}
}
inline void SetTrue(PyrSlot* slot) {
slot->tag = tagTrue;
slot->u.i = 0;
}
inline void SetFalse(PyrSlot* slot) {
slot->tag = tagFalse;
slot->u.i = 0;
}
inline void SetBool(PyrSlot* slot, bool test) {
slot->tag = (test ? tagTrue : tagFalse);
slot->u.i = 0;
}
inline void SetNil(PyrSlot* slot) {
slot->tag = tagNil;
slot->u.i = 0;
}
inline void SetFloat(PyrSlot* slot, double val) {
slot->tag = tagFloat;
slot->u.f = val;
}
/* raw setter functions, no typecheck */
inline void SetRawChar(PyrSlot* slot, int val) {
assert(IsChar(slot));
slot->u.c = val;
}
inline void SetRaw(PyrSlot* slot, int val) {
assert(IsInt(slot));
slot->u.i = val;
}
inline void SetRaw(PyrSlot* slot, long val) {
assert(IsInt(slot));
slot->u.i = val;
}
inline void SetRaw(PyrSlot* slot, PyrObject* val) {
assert(IsObj(slot));
slot->u.o = val;
}
inline void SetRaw(PyrSlot* slot, PyrSymbol* val) {
assert(IsSym(slot));
slot->u.s = val;
}
inline void SetRaw(PyrSlot* slot, void* val) {
assert(IsPtr(slot));
slot->u.ptr = val;
}
inline void SetRaw(PyrSlot* slot, double val) {
assert(IsFloat(slot));
slot->u.f = val;
}
inline void SetTagRaw(PyrSlot* slot, int tag) { slot->tag = tag; }
/* slot comparison */
inline bool SlotEq(const PyrSlot* a, const PyrSlot* b) { return (a->tag == b->tag) && (a->u.i == b->u.i); }
/* extract numeric value */
template <typename numeric_type> inline int slotVal(PyrSlot* slot, numeric_type* value) {
if (IsFloat(slot)) {
*value = static_cast<numeric_type>(slot->u.f);
return errNone;
} else if (IsInt(slot)) {
*value = static_cast<numeric_type>(slot->u.i);
return errNone;
}
return errWrongType;
}
inline int slotFloatVal(PyrSlot* slot, float* value) { return slotVal<float>(slot, value); }
inline int slotIntVal(PyrSlot* slot, int* value) { return slotVal<int>(slot, value); }
inline int slotDoubleVal(PyrSlot* slot, double* value) { return slotVal<double>(slot, value); }
/* get symbol */
inline int slotSymbolVal(PyrSlot* slot, PyrSymbol** symbol) {
if (!IsSym(slot))
return errWrongType;
*symbol = slot->u.s;
return errNone;
}
/* raw access functions */
inline void* slotRawPtr(PyrSlot* slot) {
assert(IsPtr(slot) || (slot->u.ptr == NULL && IsNil(slot)));
return slot->u.ptr;
}
inline PyrBlock* slotRawBlock(PyrSlot* slot) { return slot->u.oblk; }
inline PyrSymbolArray* slotRawSymbolArray(PyrSlot* slot) { return slot->u.osym; }
inline PyrDoubleArray* slotRawDoubleArray(PyrSlot* slot) { return slot->u.od; }
inline PyrInt8Array* slotRawInt8Array(PyrSlot* slot) { return slot->u.ob; }
inline PyrMethod* slotRawMethod(PyrSlot* slot) { return slot->u.om; }
inline const PyrMethod* slotRawMethod(const PyrSlot* slot) { return slot->u.om; }
inline PyrThread* slotRawThread(PyrSlot* slot) { return slot->u.ot; }
inline PyrString* slotRawString(PyrSlot* slot) { return slot->u.os; }
inline PyrList* slotRawList(PyrSlot* slot) { return slot->u.ol; }
inline PyrFrame* slotRawFrame(PyrSlot* slot) { return slot->u.of; }
inline PyrClass* slotRawClass(PyrSlot* slot) { return slot->u.oc; }
inline const PyrClass* slotRawClass(const PyrSlot* slot) { return slot->u.oc; }
inline PyrInterpreter* slotRawInterpreter(PyrSlot* slot) { return slot->u.oi; }
inline PyrSymbol* slotRawSymbol(PyrSlot* slot) { return slot->u.s; }
inline const PyrSymbol* slotRawSymbol(const PyrSlot* slot) { return slot->u.s; }
inline int slotRawChar(const PyrSlot* slot) { return slot->u.c; }
inline int slotRawInt(const PyrSlot* slot) {
assert(IsInt(slot));
return slot->u.i;
}
inline double slotRawFloat(const PyrSlot* slot) {
assert(IsFloat(slot));
return slot->u.f;
}
inline PyrObject* slotRawObject(PyrSlot* slot) {
assert(IsObj(slot));
return slot->u.o;
}
inline const PyrObject* slotRawObject(const PyrSlot* slot) {
assert(IsObj(slot));
return slot->u.o;
}
/* slot copy functions */
inline void slotCopy(PyrSlot* dst, const PyrSlot* src) { *dst = *src; }
inline void slotCopy(PyrSlot* dst, const PyrSlot* src, int num) {
for (int i = 0; i < num; ++i)
slotCopy(dst + i, src + i);
}