common: prevent buffer overflow
[supercollider.git] / include / lang / PyrKernel.h
blob683fc8801f5872bafab9d12de52c128014104d72
1 /*
2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 This file contains the definitions of the core objects that implement the class system.
26 #ifndef _PYRKERNEL_H_
27 #define _PYRKERNEL_H_
29 #include "PyrObject.h"
30 #include "VMGlobals.h"
32 #define classClassNumInstVars 19
34 enum { classIsIntrinsic = 1, classHasIndexableInstances = 2 };
36 struct PyrClass : public PyrObjectHdr
38 PyrSlot name;
39 PyrSlot nextclass;
40 PyrSlot superclass;
41 PyrSlot subclasses;
42 PyrSlot methods;
44 PyrSlot instVarNames;
45 PyrSlot classVarNames;
46 PyrSlot iprototype; // instance prototype
47 PyrSlot cprototype; // class var prototype
49 PyrSlot constNames;
50 PyrSlot constValues; // const values
52 PyrSlot instanceFormat;
53 PyrSlot instanceFlags;
54 PyrSlot classIndex;
55 PyrSlot classFlags;
56 PyrSlot maxSubclassIndex; // used by isKindOf
57 PyrSlot filenameSym;
58 PyrSlot charPos;
59 PyrSlot classVarIndex;
64 inline bool isKindOf(PyrObjectHdr *obj, struct PyrClass *testclass)
66 int objClassIndex = slotRawInt(&obj->classptr->classIndex);
67 return objClassIndex >= slotRawInt(&testclass->classIndex) && objClassIndex <= slotRawInt(&testclass->maxSubclassIndex);
70 inline bool isKindOfSlot(PyrSlot *slot, struct PyrClass *testclass)
72 return IsObj(slot) && isKindOf(slotRawObject(slot), testclass);
76 operations on class:
77 numInstVars()
78 numClassVars()
82 struct PyrFrame : public PyrObjectHdr
84 PyrSlot method;
85 PyrSlot caller;
86 PyrSlot context;
87 PyrSlot homeContext;
88 PyrSlot ip;
89 PyrSlot vars[1];
92 #define FRAMESIZE 5
94 struct PyrProcess : public PyrObjectHdr
97 PyrSlot classVars;
98 PyrSlot interpreter;
99 PyrSlot curThread, mainThread;
100 PyrSlot sysSchedulerQueue;
101 PyrSlot nowExecutingPath;
105 enum { tInit, tStart, tReady, tRunning, tSleeping, tSuspended, tDone };
107 struct PyrThread : public PyrObjectHdr
109 PyrSlot state, func, stack, method, block, frame, ip, sp;
110 PyrSlot numpop, receiver, numArgsPushed;
111 PyrSlot parent, terminalValue;
112 PyrSlot primitiveError;
113 PyrSlot primitiveIndex;
114 PyrSlot randData;
115 PyrSlot beats, seconds, clock, nextBeat, endBeat, endValue;
116 PyrSlot environment;
117 PyrSlot exceptionHandler;
118 PyrSlot executingPath;
119 PyrSlot oldExecutingPath;
120 PyrSlot stackSize;
123 #define EVALSTACKDEPTH 512
127 struct PyrMethodRaw
129 #ifdef PYR_SLOTS_GENERIC
130 long padding; // used for the tag in the generic pyrslot implementation
131 #endif
132 unsigned short unused1;
133 unsigned short specialIndex;
134 unsigned short methType;
135 unsigned short frameSize;
137 #ifdef PYR_SLOTS_GENERIC
138 long padding2; // used for the tag in generic pyrslot implementation, second slot
139 #endif
141 unsigned char unused2;
142 unsigned char numargs;
143 unsigned char varargs;
144 unsigned char numvars;
145 unsigned char numtemps;
146 unsigned char needsHeapContext;
147 unsigned char popSize;
148 unsigned char posargs;
152 #define METHRAW(obj) ((PyrMethodRaw*)&(((PyrBlock*)obj)->rawData1))
154 struct PyrBlock : public PyrObjectHdr
157 PyrSlot rawData1;
158 PyrSlot rawData2;
159 PyrSlot code; // byte codes, nil if inlined
160 PyrSlot selectors; // method selectors, class names, closures table
161 PyrSlot constants; // floating point constants table (to alleviate the literal table problem)
162 PyrSlot prototypeFrame; // prototype of an activation frame
163 PyrSlot contextDef; // ***defining block context
164 PyrSlot argNames; // ***arguments to block
165 PyrSlot varNames; // ***variables in block
166 PyrSlot sourceCode; // source code if it is a closed function.
169 struct PyrMethod : public PyrBlock
171 PyrSlot ownerclass;
172 PyrSlot name;
173 PyrSlot primitiveName;
174 PyrSlot filenameSym;
175 PyrSlot charPos;
176 //PyrSlot byteMeter;
177 //PyrSlot callMeter;
180 enum {
181 methNormal = 0,
182 methReturnSelf,
183 methReturnLiteral,
184 methReturnArg,
185 methReturnInstVar,
186 methAssignInstVar,
187 methReturnClassVar,
188 methAssignClassVar,
189 methRedirect,
190 methRedirectSuper,
191 methForwardInstVar,
192 methForwardClassVar,
193 methPrimitive,
194 methBlock
197 struct PyrClosure : public PyrObjectHdr
200 PyrSlot block;
201 PyrSlot context;
204 struct PyrInterpreter : public PyrObjectHdr
207 PyrSlot cmdLine, context;
208 PyrSlot a, b, c, d, e, f, g, h, i, j;
209 PyrSlot k, l, m, n, o, p, q, r, s, t;
210 PyrSlot u, v, w, x, y, z;
211 PyrSlot codeDump, preProcessor;
214 /* special values */
215 enum {
216 svNil,
217 svFalse,
218 svTrue,
219 svNegOne,
220 svZero,
221 svOne,
222 svTwo,
223 svFHalf,
224 svFNegOne,
225 svFZero,
226 svFOne,
227 svFTwo,
228 svInf,
230 svNumSpecialValues
233 extern PyrSlot gSpecialValues[svNumSpecialValues];
235 extern PyrMethod *gNullMethod; // used to fill row table
237 PyrObject* instantiateObject(class PyrGC *gc, PyrClass* classobj, int size,
238 bool fill, bool collect);
240 PyrObject* newPyrObject(class PyrGC *gc, size_t inNumBytes, int inFlags, int inFormat, bool inCollect);
241 PyrString* newPyrString(class PyrGC *gc, const char *s, int flags, bool collect);
242 PyrString* newPyrStringN(class PyrGC *gc, int size, int flags, bool collect);
243 PyrObject* newPyrArray(class PyrGC *gc, int size, int flags, bool collect);
244 PyrSymbolArray* newPyrSymbolArray(class PyrGC *gc, int size, int flags, bool collect);
245 PyrInt8Array* newPyrInt8Array(class PyrGC *gc, int size, int flags, bool collect);
246 PyrInt32Array* newPyrInt32Array(class PyrGC *gc, int size, int flags, bool collect);
247 PyrDoubleArray* newPyrDoubleArray(class PyrGC *gc, int size, int flags, bool collect);
249 PyrObject* copyObject(class PyrGC *gc, PyrObject *inobj, bool collect);
250 PyrObject* copyObjectRange(class PyrGC *gc, PyrObject *inobj, int start, int end, bool collect);
252 #endif