]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/data.h
fix #314: Nasal not working on i386/32bit systems with gcc>=4.5.x
[simgear.git] / simgear / nasal / data.h
1 #ifndef _DATA_H
2 #define _DATA_H
3
4 #include "nasal.h"
5
6 #if defined(NASAL_NAN64)
7
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:
16 //
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
21 //
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.
29
30 #define REFMAGIC ((1UL<<48) - 1)
31
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)
35
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)))
44
45 #define SETPTR(r, p) ((r).ptr = (void*)((unsigned long long)p | ~REFMAGIC))
46 #define SETNUM(r, n) ((r).num = n)
47
48 #else
49
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
55
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)
59
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)
62
63 #endif /* platform stuff */
64
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!
67
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)
81
82 #define MUTABLE(r) (IS_STR(r) && PTR(r).str->hashcode == 0)
83
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".
88 #define GC_HEADER \
89     unsigned char mark; \
90     unsigned char type
91
92 struct naObj {
93     GC_HEADER;
94 };
95
96 #define MAX_STR_EMBLEN 15
97 struct naStr {
98     GC_HEADER;
99     char emblen; /* [0-15], or -1 to indicate "not embedded" */
100     unsigned int hashcode;
101     union {
102         unsigned char buf[16];
103         struct {
104             int len;
105             unsigned char* ptr;
106         } ref;
107     } data;
108 };
109
110 struct VecRec {
111     int size;
112     int alloced;
113     naRef array[];
114 };
115
116 struct naVec {
117     GC_HEADER;
118     struct VecRec* rec;
119 };
120
121 struct HashNode {
122     naRef key;
123     naRef val;
124     struct HashNode* next;
125 };
126
127 struct naHash {
128     GC_HEADER;
129     struct HashRec* rec;
130 };
131
132 struct naCode {
133     GC_HEADER;
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;
141     naRef srcFile;
142     naRef* constants;
143 };
144
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)
153
154 struct naFunc {
155     GC_HEADER;
156     naRef code;
157     naRef namespace;
158     naRef next; // parent closure
159 };
160
161 struct naCCode {
162     GC_HEADER;
163     naCFunction fptr;
164 };
165
166 struct naGhost {
167     GC_HEADER;
168     naGhostType* gtype;
169     void* ptr;
170 };
171
172 struct naPool {
173     int           type;
174     int           elemsz;
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
181 };
182
183 void naFree(void* m);
184 void* naAlloc(int n);
185 void* naRealloc(void* buf, int sz);
186 void naBZero(void* m, int n);
187
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);
192
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);
199
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);
203
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);
210
211 void naStr_gcclean(struct naStr* s);
212 void naVec_gcclean(struct naVec* s);
213 void naiGCHashClean(struct naHash* h);
214
215 #endif // _DATA_H