6 #if defined(NASAL_NAN64)
8 // On 64 bit systems, Nasal non-numeric references are stored with a
9 // bitmask that sets the top 16 bits. As a double, this is a
10 // signalling NaN that cannot itself be produced by normal numerics
11 // code. The pointer value can be reconstructed if (and only if) we
12 // are guaranteed that all memory that can be poitned to by a naRef
13 // (i.e. all memory returned by naAlloc) lives in the bottom 48 bits
14 // of memory. Linux on x86_64, Win64, Solaris and Irix all have such
15 // policies with address spaces:
17 // http://msdn.microsoft.com/library/en-us/win64/win64/virtual_address_space.asp
18 // http://docs.sun.com/app/docs/doc/816-5138/6mba6ua5p?a=view
19 // http://techpubs.sgi.com/library/tpl/cgi-bin/getdoc.cgi/
20 // ... 0650/bks/SGI_Developer/books/T_IRIX_Prog/sgi_html/ch01.html
22 // In the above, MS guarantees 44 bits of process address space, SGI
23 // 40, and Sun 43 (Solaris *does* place the stack in the "negative"
24 // address space at 0xffff..., but we don't care as naRefs will never
25 // point there). Linux doesn't document this rigorously, but testing
26 // shows that it allows 47 bits of address space (and current x86_64
27 // implementations are limited to 48 bits of virtual space anyway). So
28 // we choose 48 as the conservative compromise.
30 #define REFMAGIC ((1UL<<48) - 1)
32 #define _ULP(r) ((unsigned long long)((r).ptr))
33 #define REFPTR(r) (_ULP(r) & REFMAGIC)
34 #define IS_REF(r) ((_ULP(r) & ~REFMAGIC) == ~REFMAGIC)
36 // Portability note: this cast from a pointer type to naPtr (a union)
37 // is not defined in ISO C, it's a GCC extention that doesn't work on
38 // (at least) either the SUNWspro or MSVC compilers. Unfortunately,
39 // fixing this would require abandoning the naPtr union for a set of
40 // PTR_<type>() macros, which is a ton of work and a lot of extra
41 // code. And as all enabled 64 bit platforms are gcc anyway, and the
42 // 32 bit fallback code works in any case, this is acceptable for now.
43 #define PTR(r) ((naPtr)((struct naObj*)(_ULP(r) & REFMAGIC)))
45 #define SETPTR(r, p) ((r).ptr = (void*)((unsigned long long)p | ~REFMAGIC))
46 #define SETNUM(r, n) ((r).num = n)
50 // On 32 bit systems where the pointer is half the width of the
51 // double, we store a special magic number in the structure to make
52 // the double a NaN. This must appear in the top bits of the double,
53 // which is why the structure layout is endianness-dependent.
55 #define NASAL_REFTAG 0x7ff56789 // == 2,146,789,257 decimal
56 #define IS_REF(r) ((r).ref.reftag == NASAL_REFTAG)
57 #define PTR(r) ((r).ref.ptr)
59 #define SETPTR(r, p) ((r).ref.ptr.obj = (void*)p, (r).ref.reftag = NASAL_REFTAG)
60 #define SETNUM(r, n) ((r).ref.reftag = ~NASAL_REFTAG, (r).num = n)
62 #endif /* platform stuff */
64 enum { T_STR, T_VEC, T_HASH, T_CODE, T_FUNC, T_CCODE, T_GHOST,
65 NUM_NASAL_TYPES }; // V. important that this come last!
67 #define IS_NUM(r) (!IS_REF(r))
68 #define IS_OBJ(r) (IS_REF(r) && PTR(r).obj != 0)
69 #define IS_NIL(r) (IS_REF(r) && PTR(r).obj == 0)
70 #define IS_STR(r) (IS_OBJ(r) && PTR(r).obj->type == T_STR)
71 #define IS_VEC(r) (IS_OBJ(r) && PTR(r).obj->type == T_VEC)
72 #define IS_HASH(r) (IS_OBJ(r) && PTR(r).obj->type == T_HASH)
73 #define IS_CODE(r) (IS_OBJ(r) && PTR(r).obj->type == T_CODE)
74 #define IS_FUNC(r) (IS_OBJ(r) && PTR(r).obj->type == T_FUNC)
75 #define IS_CCODE(r) (IS_OBJ(r) && PTR(r).obj->type == T_CCODE)
76 #define IS_GHOST(r) (IS_OBJ(r) && PTR(r).obj->type == T_GHOST)
77 #define IS_CONTAINER(r) (IS_VEC(r)||IS_HASH(r))
78 #define IS_SCALAR(r) (IS_NUM(r) || IS_STR(r))
79 #define IDENTICAL(a, b) (IS_REF(a) && IS_REF(b) \
80 && PTR(a).obj == PTR(b).obj)
82 #define MUTABLE(r) (IS_STR(r) && PTR(r).str->hashcode == 0)
84 // This is a macro instead of a separate struct to allow compilers to
85 // avoid padding. GCC on x86, at least, will always padd the size of
86 // an embedded struct up to 32 bits. Doing it this way allows the
87 // implementing objects to pack in 16 bits worth of data "for free".
100 unsigned int hashcode;
117 struct HashNode* next;
124 struct HashNode* nodes;
125 struct HashNode* table[];
136 unsigned char nOptArgs;
137 unsigned char needArgVector;
138 unsigned short nConstants;
139 unsigned short nLines;
140 unsigned short codesz;
141 unsigned short* byteCode;
143 int* argSyms; // indices into constants
146 unsigned short* lineIps; // pairs of {ip, line}
148 naRef restArgSym; // The "..." vector name, defaults to "arg"
155 naRef next; // parent closure
172 struct Block* blocks;
173 void** free0; // pointer to the alloced buffer
174 int freesz; // size of the alloced buffer
175 void** free; // current "free frame"
176 int nfree; // down-counting index within the free frame
177 int freetop; // curr. top of the free list
180 void naFree(void* m);
181 void* naAlloc(int n);
182 void* naRealloc(void* buf, int sz);
183 void naBZero(void* m, int n);
185 int naTypeSize(int type);
186 naRef naObj(int type, struct naObj* o);
187 naRef naNew(naContext c, int type);
188 naRef naNewCode(naContext c);
190 int naStr_equal(naRef s1, naRef s2);
191 naRef naStr_fromnum(naRef dest, double num);
192 int naStr_numeric(naRef str);
193 int naStr_parsenum(char* str, int len, double* result);
194 int naStr_tonum(naRef str, double* out);
195 naRef naStr_buf(naRef str, int len);
197 int naHash_tryset(naRef hash, naRef key, naRef val); // sets if exists
198 int naHash_sym(struct naHash* h, struct naStr* sym, naRef* out);
199 void naHash_newsym(struct naHash* h, naRef* sym, naRef* val);
201 void naGC_init(struct naPool* p, int type);
202 struct naObj** naGC_get(struct naPool* p, int n, int* nout);
203 void naGC_swapfree(void** target, void* val);
204 void naGC_freedead();
206 void naStr_gcclean(struct naStr* s);
207 void naVec_gcclean(struct naVec* s);
208 void naHash_gcclean(struct naHash* s);