00001 #ifndef _DATA_H
00002 #define _DATA_H
00003
00004 #include "nasal.h"
00005
00006 #if defined(NASAL_NAN64)
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #define REFMAGIC ((1UL<<48) - 1)
00031
00032 #define _ULP(r) ((unsigned long long)((r).ptr))
00033 #define REFPTR(r) (_ULP(r) & REFMAGIC)
00034 #define IS_REF(r) ((_ULP(r) & ~REFMAGIC) == ~REFMAGIC)
00035
00036
00037
00038
00039
00040
00041
00042
00043 #define PTR(r) ((naPtr)((struct naObj*)(_ULP(r) & REFMAGIC)))
00044
00045 #define SETPTR(r, p) ((r).ptr = (void*)((unsigned long long)p | ~REFMAGIC))
00046 #define SETNUM(r, n) ((r).num = n)
00047
00048 #else
00049
00050
00051
00052
00053
00054
00055 #define NASAL_REFTAG 0x7ff56789 // == 2,146,789,257 decimal
00056 #define IS_REF(r) ((r).ref.reftag == NASAL_REFTAG)
00057 #define PTR(r) ((r).ref.ptr)
00058
00059 #define SETPTR(r, p) ((r).ref.ptr.obj = (void*)p, (r).ref.reftag = NASAL_REFTAG)
00060 #define SETNUM(r, n) ((r).ref.reftag = ~NASAL_REFTAG, (r).num = n)
00061
00062 #endif
00063
00064 enum { T_STR, T_VEC, T_HASH, T_CODE, T_FUNC, T_CCODE, T_GHOST,
00065 NUM_NASAL_TYPES };
00066
00067 #define IS_NUM(r) (!IS_REF(r))
00068 #define IS_OBJ(r) (IS_REF(r) && PTR(r).obj != 0)
00069 #define IS_NIL(r) (IS_REF(r) && PTR(r).obj == 0)
00070 #define IS_STR(r) (IS_OBJ(r) && PTR(r).obj->type == T_STR)
00071 #define IS_VEC(r) (IS_OBJ(r) && PTR(r).obj->type == T_VEC)
00072 #define IS_HASH(r) (IS_OBJ(r) && PTR(r).obj->type == T_HASH)
00073 #define IS_CODE(r) (IS_OBJ(r) && PTR(r).obj->type == T_CODE)
00074 #define IS_FUNC(r) (IS_OBJ(r) && PTR(r).obj->type == T_FUNC)
00075 #define IS_CCODE(r) (IS_OBJ(r) && PTR(r).obj->type == T_CCODE)
00076 #define IS_GHOST(r) (IS_OBJ(r) && PTR(r).obj->type == T_GHOST)
00077 #define IS_CONTAINER(r) (IS_VEC(r)||IS_HASH(r))
00078 #define IS_SCALAR(r) (IS_NUM(r) || IS_STR(r))
00079 #define IDENTICAL(a, b) (IS_REF(a) && IS_REF(b) && PTR(a).obj == PTR(b).obj)
00080
00081 #define MUTABLE(r) (IS_STR(r) && PTR(r).str->hashcode == 0)
00082
00083
00084
00085
00086
00087 #define GC_HEADER \
00088 unsigned char mark; \
00089 unsigned char type
00090
00091 struct naObj {
00092 GC_HEADER;
00093 };
00094
00095 #define MAX_STR_EMBLEN 15
00096 struct naStr {
00097 GC_HEADER;
00098 char emblen;
00099 unsigned int hashcode;
00100 union {
00101 unsigned char buf[16];
00102 struct {
00103 int len;
00104 unsigned char* ptr;
00105 } ref;
00106 } data;
00107 };
00108
00109 struct VecRec {
00110 int size;
00111 int alloced;
00112 naRef array[];
00113 };
00114
00115 struct naVec {
00116 GC_HEADER;
00117 struct VecRec* rec;
00118 };
00119
00120 struct HashNode {
00121 naRef key;
00122 naRef val;
00123 struct HashNode* next;
00124 };
00125
00126 struct naHash {
00127 GC_HEADER;
00128 struct HashRec* rec;
00129 };
00130
00131 struct naCode {
00132 GC_HEADER;
00133 unsigned int nArgs : 5;
00134 unsigned int nOptArgs : 5;
00135 unsigned int needArgVector : 1;
00136 unsigned short nConstants;
00137 unsigned short codesz;
00138 unsigned short restArgSym;
00139 unsigned short nLines;
00140 naRef srcFile;
00141 naRef* constants;
00142 };
00143
00144
00145
00146
00147 #define BYTECODE(c) ((unsigned short*)((c)->constants+(c)->nConstants))
00148 #define ARGSYMS(c) (BYTECODE(c)+(c)->codesz)
00149 #define OPTARGSYMS(c) (ARGSYMS(c)+(c)->nArgs)
00150 #define OPTARGVALS(c) (OPTARGSYMS(c)+(c)->nOptArgs)
00151 #define LINEIPS(c) (OPTARGVALS(c)+(c)->nOptArgs)
00152
00153 struct naFunc {
00154 GC_HEADER;
00155 naRef code;
00156 naRef namespace;
00157 naRef next;
00158 };
00159
00160 struct naCCode {
00161 GC_HEADER;
00162 naCFunction fptr;
00163 };
00164
00165 struct naGhost {
00166 GC_HEADER;
00167 naGhostType* gtype;
00168 void* ptr;
00169 };
00170
00171 struct naPool {
00172 int type;
00173 int elemsz;
00174 struct Block* blocks;
00175 void** free0;
00176 int freesz;
00177 void** free;
00178 int nfree;
00179 int freetop;
00180 };
00181
00182 void naFree(void* m);
00183 void* naAlloc(int n);
00184 void* naRealloc(void* buf, int sz);
00185 void naBZero(void* m, int n);
00186
00187 int naTypeSize(int type);
00188 naRef naObj(int type, struct naObj* o);
00189 naRef naNew(naContext c, int type);
00190 naRef naNewCode(naContext c);
00191
00192 int naStr_equal(naRef s1, naRef s2);
00193 naRef naStr_fromnum(naRef dest, double num);
00194 int naStr_numeric(naRef str);
00195 int naStr_parsenum(char* str, int len, double* result);
00196 int naStr_tonum(naRef str, double* out);
00197 naRef naStr_buf(naRef str, int len);
00198
00199 int naiHash_tryset(naRef hash, naRef key, naRef val);
00200 int naiHash_sym(struct naHash* h, struct naStr* sym, naRef* out);
00201 void naiHash_newsym(struct naHash* h, naRef* sym, naRef* val);
00202
00203 void naGC_init(struct naPool* p, int type);
00204 struct naObj** naGC_get(struct naPool* p, int n, int* nout);
00205 void naGC_swapfree(void** target, void* val);
00206 void naGC_freedead();
00207 void naiGCMark(naRef r);
00208 void naiGCMarkHash(naRef h);
00209
00210 void naStr_gcclean(struct naStr* s);
00211 void naVec_gcclean(struct naVec* s);
00212 void naiGCHashClean(struct naHash* h);
00213
00214 #endif // _DATA_H