aboutsummaryrefslogtreecommitdiff
path: root/sc4pd/headers/lang/PyrSlot.h
blob: 78bcd1268fdfdadf7977f0f3c0c466b7dc3a66ab (plain)
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
/*
	SuperCollider real time audio synthesis system
    Copyright (c) 2002 James McCartney. All rights reserved.
	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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
/*

PyrSlot is a value holder for SC variables.
A PyrSlot is an 8-byte value which is either a double precision float or a 
32-bit tag plus a 32-bit value.

*/

#ifndef _PYRSLOT_H_
#define _PYRSLOT_H_

#include "SC_Endian.h"
#include "PyrSymbol.h"

/* 
	Pyrite slots are the size of an 8 byte double. If the upper bits
	indicate that the double is a 'Not-A-Number' then the upper 32
	bits are used as a tag to indicate one of a number of other types
	whose data is in the lower 32 bits.
*/

/* some DSPs like the TIC32 do not support 8 byte doubles */
/* on such CPUs, set DOUBLESLOTS to zero */

#define DOUBLESLOTS 1

/* use the high order bits of an IEEE double NaN as a tag */
enum {
	tagObj     = 0x7FF90001,
	tagHFrame  = 0x7FF90002,
	tagSFrame  = 0x7FF90003,
	tagInt     = 0x7FF90004,
	tagSym     = 0x7FF90005,
	tagChar    = 0x7FF90006,
	tagNil     = 0x7FF90007,	// nil, false, and true are indicated by the tag alone.
	tagFalse   = 0x7FF90008,	// the lower 32 bits are zero.
	tagTrue    = 0x7FF90009,
	tagInf     = 0x7FF9000A,
	tagPtr     = 0x7FF9000B,
	/* anything else is a double */
	tagUnused  = 0x7FF9000E
	
	
#if !DOUBLESLOTS	
	,tagFloat = 0x7FF9000F /* used only to initialized 4 byte float tags, never compared with */
#endif
};

struct RGBColor8 {
	unsigned char c[4];
};

typedef union pyrslot {
	double f;
	struct {
#if BYTE_ORDER == BIG_ENDIAN
		int tag;
#endif // BIG_ENDIAN
		union {
			int c; /* char */
			int i;
			float f;
			void *ptr;
			struct RGBColor8 r;
			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 PyrParseNode *opn;
			struct PyrProcess *op;
			struct PyrThread *ot;
			struct PyrInterpreter *oi;
			struct PyrPlug *plug;
		} u;
#if BYTE_ORDER == LITTLE_ENDIAN
		// need to swap on intel <sk>
		int tag;
#endif // LITTLE_ENDIAN
	} s;
} PyrSlot;

/* 
	these are some defines to make accessing the structure less verbose.
	obviously it polutes the namespace of identifiers beginning with 'u'.
*/
#define utag s.tag
//int
#define ui s.u.i
//PyrObject
#define uo s.u.o
//PyrSymbol
#define us s.u.s
//RGBColor8
#define ur s.u.r
#define uc s.u.c
#define uoc s.u.oc
#define uof s.u.of
#define uol s.u.ol
#define uod s.u.od
#define uob s.u.ob
#define uop s.u.op
#define uoi s.u.oi
#define uod s.u.od
//string
#define uos s.u.os
#define uot s.u.ot
//method
#define uom s.u.om
//symbol array
#define uosym s.u.osym
#define uoblk s.u.oblk
#define uopn s.u.opn
#define uptr s.u.ptr
#define uplug s.u.plug

#if DOUBLESLOTS
#define uf f
#else
#define uf s.u.f
#endif

#define ucopy f

/* 
	Note that on the PowerPC, the fastest way to copy a slot is to
	copy the double field, not the struct.
*/

/* some macros for setting values of slots */
inline void SetInt(PyrSlot* slot, int val)    {  (slot)->utag = tagInt;  (slot)->ui = (val); }
inline void SetObject(PyrSlot* slot, void* val) {  (slot)->utag = tagObj;   (slot)->uo = (PyrObject*)(val); }
inline void SetSymbol(PyrSlot* slot, PyrSymbol *val) {  (slot)->utag = tagSym;   (slot)->us = (val); }
inline void SetChar(PyrSlot* slot, char val)   {  (slot)->utag = tagChar;  (slot)->uc = (val); }
inline void SetPtr(PyrSlot* slot, void* val)    {  (slot)->utag = tagPtr;  (slot)->uptr = (void*)(val); }
inline void SetObjectOrNil(PyrSlot* slot, PyrObject* val)  
{ 
	if (val) { 
		(slot)->utag = tagObj; 
		(slot)->uo = (val); 
	} else {
		(slot)->utag = tagNil;  
		(slot)->ui = 0; 
	}
}
									
inline void SetTrue(PyrSlot* slot)   { (slot)->utag = tagTrue; (slot)->ui = 0; }
inline void SetFalse(PyrSlot* slot)   { (slot)->utag = tagFalse; (slot)->ui = 0; }
inline void SetBool(PyrSlot* slot, bool test)	{ (slot)->utag = ((test) ? tagTrue : tagFalse); (slot)->ui = 0;  }
inline void SetNil(PyrSlot* slot)    { (slot)->utag = tagNil;  (slot)->ui = 0; }
inline void SetInf(PyrSlot* slot)   { (slot)->utag = tagInf;  (slot)->ui = 0; }

#if DOUBLESLOTS
inline void SetFloat(PyrSlot* slot, double val)    { (slot)->uf = (val); }
#else
inline void SetFloat(PyrSlot* slot, double val)    { (slot)->utag = s_float; (slot)->uf = (val); }
#endif

inline bool IsObj(PyrSlot* slot) { return ((slot)->utag == tagObj); }
inline bool NotObj(PyrSlot* slot) { return ((slot)->utag != tagObj); }

inline bool IsNil(PyrSlot* slot) { return ((slot)->utag == tagNil); }
inline bool NotNil(PyrSlot* slot) { return ((slot)->utag != tagNil); }

inline bool IsFalse(PyrSlot* slot) { return ((slot)->utag == tagFalse); }
inline bool IsTrue(PyrSlot* slot) { return ((slot)->utag == tagTrue); }

inline bool SlotEq(PyrSlot* a, PyrSlot* b) { return ((a)->ui == (b)->ui && (a)->utag == (b)->utag); }

inline bool IsSym(PyrSlot* slot) { return ((slot)->utag == tagSym); }
inline bool NotSym(PyrSlot* slot) { return ((slot)->utag != tagSym); }

inline bool IsInt(PyrSlot* slot) { return ((slot)->utag == tagInt); }
inline bool NotInt(PyrSlot* slot) { return ((slot)->utag != tagInt); }

inline bool IsFloatTag(int tag)  { return ((tag & 0xFFFFFFF0) != 0x7FF90000); }
inline bool IsFloat(PyrSlot* slot) { return (((slot)->utag & 0xFFFFFFF0) != 0x7FF90000); }
inline bool NotFloat(PyrSlot* slot) { return (((slot)->utag & 0xFFFFFFF0) == 0x7FF90000); }

inline bool IsInf(PyrSlot* slot) { return ((slot)->utag == tagInf); }
inline bool IsPtr(PyrSlot* slot) { return ((slot)->utag == tagPtr); }

inline bool IsFrame(PyrSlot* slot) { return ((slot)->utag == tagHFrame || (slot)->utag == tagSFrame); }


void dumpPyrSlot(PyrSlot* slot);
void slotString(PyrSlot *slot, char *str);
void slotOneWord(PyrSlot *slot, char *str);
bool postString(PyrSlot *slot, char *str);
char *slotSymString(PyrSlot* slot);
int asCompileString(PyrSlot *slot, char *str);

int slotIntVal(PyrSlot* slot, int *value);
int slotFloatVal(PyrSlot* slot, float *value);
int slotDoubleVal(PyrSlot *slot, double *value);
int slotStrVal(PyrSlot *slot, char *str, int maxlen);
int slotPStrVal(PyrSlot *slot, unsigned char *str);
int slotSymbolVal(PyrSlot *slot, PyrSymbol **symbol);

extern PyrSlot o_nil, o_true, o_false, o_inf;
extern PyrSlot o_pi, o_twopi;
extern PyrSlot o_fhalf, o_fnegone, o_fzero, o_fone, o_ftwo;
extern PyrSlot o_negtwo, o_negone, o_zero, o_one, o_two;
extern PyrSlot o_emptyarray, o_onenilarray, o_argnamethis;

extern PyrSymbol *s_object; // "Object"
extern PyrSymbol *s_this; // "this"
extern PyrSymbol *s_super; // "super"

inline int slotFloatVal(PyrSlot *slot, float *value)
{
	if (IsFloat(slot)) {
		*value = slot->uf;
		return errNone;
	} else if (IsInt(slot)) {
		 *value = slot->ui;
		return errNone;
	}
	return errWrongType;
}

inline int slotIntVal(PyrSlot *slot, int *value)
{
	if (IsInt(slot)) {
		 *value = slot->ui;
		return errNone;
	} else if (IsFloat(slot)) {
		*value = (int)slot->uf;
		return errNone;
	}  
	return errWrongType;
}

inline int slotDoubleVal(PyrSlot *slot, double *value)
{
	if (IsFloat(slot)) {
		*value = slot->uf;
		return errNone;
	} else if (IsInt(slot)) {
		 *value = slot->ui;
		return errNone;
	}
	return errWrongType;
}

inline int slotSymbolVal(PyrSlot *slot, PyrSymbol **symbol)
{
	if (!IsSym(slot)) return errWrongType;
	*symbol = slot->us;
	return errNone;
}

inline void slotCopy(PyrSlot *dst, PyrSlot *src, int num)
{
	double *dstp = (double*)dst - 1;
	double *srcp = (double*)src - 1;
	for (int i=0;i<num;++i) { *++dstp = *++srcp; } 
}


#endif