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 pointed 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 qNaN. This must appear in the top bits of the double,
53 // which is why the structure layout is endianness-dependent.
54 // qNaN (quiet NaNs) use range 0x7ff80000-0x7fffffff
56 #define NASAL_REFTAG 0x7fff6789 // == 2,147,444,617 decimal
57 #define IS_REF(r) ((r).ref.reftag == NASAL_REFTAG)
58 #define PTR(r) ((r).ref.ptr)
60 #define SETPTR(r, p) ((r).ref.ptr.obj = (void*)p, (r).ref.reftag = NASAL_REFTAG)
61 #define SETNUM(r, n) ((r).ref.reftag = ~NASAL_REFTAG, (r).num = n)
63 #endif /* platform stuff */
65 enum { T_STR, T_VEC, T_HASH, T_CODE, T_FUNC, T_CCODE, T_GHOST,
66 NUM_NASAL_TYPES }; // V. important that this come last!
68 #define IS_NUM(r) (!IS_REF(r))
69 #define IS_OBJ(r) (IS_REF(r) && PTR(r).obj != 0)
70 #define IS_NIL(r) (IS_REF(r) && PTR(r).obj == 0)
71 #define IS_STR(r) (IS_OBJ(r) && PTR(r).obj->type == T_STR)
72 #define IS_VEC(r) (IS_OBJ(r) && PTR(r).obj->type == T_VEC)
73 #define IS_HASH(r) (IS_OBJ(r) && PTR(r).obj->type == T_HASH)
74 #define IS_CODE(r) (IS_OBJ(r) && PTR(r).obj->type == T_CODE)
75 #define IS_FUNC(r) (IS_OBJ(r) && PTR(r).obj->type == T_FUNC)
76 #define IS_CCODE(r) (IS_OBJ(r) && PTR(r).obj->type == T_CCODE)
77 #define IS_GHOST(r) (IS_OBJ(r) && PTR(r).obj->type == T_GHOST)
78 #define IS_CONTAINER(r) (IS_VEC(r)||IS_HASH(r))
79 #define IS_SCALAR(r) (IS_NUM(r) || IS_STR(r))
80 #define IDENTICAL(a, b) (IS_REF(a) && IS_REF(b) && 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 pad 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".
96 #define MAX_STR_EMBLEN 15
99 char emblen; /* [0-15], or -1 to indicate "not embedded" */
100 unsigned int hashcode;
102 unsigned char buf[16];
124 struct HashNode* next;
134 unsigned int nArgs : 5;
135 unsigned int nOptArgs : 5;
136 unsigned int needArgVector : 1;
137 unsigned short nConstants;
138 unsigned short codesz;
139 unsigned short restArgSym; // The "..." vector name, defaults to "arg"
140 unsigned short nLines;
145 /* naCode objects store their variable length arrays in a single block
146 * starting with their constants table. Compute indexes at runtime
147 * for space efficiency: */
148 #define BYTECODE(c) ((unsigned short*)((c)->constants+(c)->nConstants))
149 #define ARGSYMS(c) (BYTECODE(c)+(c)->codesz)
150 #define OPTARGSYMS(c) (ARGSYMS(c)+(c)->nArgs)
151 #define OPTARGVALS(c) (OPTARGSYMS(c)+(c)->nOptArgs)
152 #define LINEIPS(c) (OPTARGVALS(c)+(c)->nOptArgs)
158 naRef next; // parent closure
175 struct Block* blocks;
176 void** free0; // pointer to the alloced buffer
177 int freesz; // size of the alloced buffer
178 void** free; // current "free frame"
179 int nfree; // down-counting index within the free frame
180 int freetop; // curr. top of the free list
183 void naFree(void* m);
184 void* naAlloc(int n);
185 void* naRealloc(void* buf, int sz);
186 void naBZero(void* m, int n);
188 int naTypeSize(int type);
189 naRef naObj(int type, struct naObj* o);
190 naRef naNew(naContext c, int type);
191 naRef naNewCode(naContext c);
193 int naStr_equal(naRef s1, naRef s2);
194 naRef naStr_fromnum(naRef dest, double num);
195 int naStr_numeric(naRef str);
196 int naStr_parsenum(char* str, int len, double* result);
197 int naStr_tonum(naRef str, double* out);
198 naRef naStr_buf(naRef str, int len);
200 int naiHash_tryset(naRef hash, naRef key, naRef val); // sets if exists
201 int naiHash_sym(struct naHash* h, struct naStr* sym, naRef* out);
202 void naiHash_newsym(struct naHash* h, naRef* sym, naRef* val);
204 void naGC_init(struct naPool* p, int type);
205 struct naObj** naGC_get(struct naPool* p, int n, int* nout);
206 void naGC_swapfree(void** target, void* val);
207 void naGC_freedead();
208 void naiGCMark(naRef r);
209 void naiGCMarkHash(naRef h);
211 void naStr_gcclean(struct naStr* s);
212 void naVec_gcclean(struct naVec* s);
213 void naiGCHashClean(struct naHash* h);