]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/code.h
Csaba HALASZ:
[simgear.git] / simgear / nasal / code.h
1 #ifndef _CODE_H
2 #define _CODE_H
3
4 #include <setjmp.h>
5 #include "nasal.h"
6 #include "data.h"
7
8 #define MAX_STACK_DEPTH 512
9 #define MAX_RECURSION 128
10 #define MAX_MARK_DEPTH 128
11
12 // Number of objects (per pool per thread) asked for using naGC_get().
13 // Testing with fib.nas shows that this gives the best performance,
14 // without too much per-thread overhead.
15 #define OBJ_CACHE_SZ 128
16
17 enum {    
18     OP_NOT, OP_MUL, OP_PLUS, OP_MINUS, OP_DIV, OP_NEG,
19     OP_CAT, OP_LT, OP_LTE, OP_GT, OP_GTE, OP_EQ, OP_NEQ, OP_EACH,
20     OP_JMP, OP_JMPLOOP, OP_JIFNOTPOP, OP_JIFEND, OP_FCALL, OP_MCALL,
21     OP_RETURN, OP_PUSHCONST, OP_PUSHONE, OP_PUSHZERO, OP_PUSHNIL, OP_POP,
22     OP_DUP, OP_XCHG, OP_INSERT, OP_EXTRACT, OP_MEMBER, OP_SETMEMBER,
23     OP_LOCAL, OP_SETLOCAL, OP_NEWVEC, OP_VAPPEND, OP_NEWHASH, OP_HAPPEND,
24     OP_MARK, OP_UNMARK, OP_BREAK, OP_SETSYM, OP_DUP2, OP_INDEX, OP_BREAK2,
25     OP_PUSHEND, OP_JIFTRUE, OP_JIFNOT
26 };
27
28 struct Frame {
29     naRef func; // naFunc object
30     naRef locals; // local per-call namespace
31     int ip; // instruction pointer into code
32     int bp; // opStack pointer to start of frame
33 };
34
35 struct Globals {
36     // Garbage collecting allocators:
37     struct naPool pools[NUM_NASAL_TYPES];
38     int allocCount;
39
40     // Dead blocks waiting to be freed when it is safe
41     void** deadBlocks;
42     int deadsz;
43     int ndead;
44     
45     // Threading stuff
46     int nThreads;
47     int waitCount;
48     int needGC;
49     int bottleneck;
50     void* sem;
51     void* lock;
52
53     // Constants
54     naRef meRef;
55     naRef argRef;
56     naRef parentsRef;
57
58     // A hash of symbol names
59     naRef symbols;
60
61     naRef save;
62
63     struct Context* freeContexts;
64     struct Context* allContexts;
65 };
66
67 struct Context {
68     // Stack(s)
69     struct Frame fStack[MAX_RECURSION];
70     int fTop;
71     naRef opStack[MAX_STACK_DEPTH];
72     int opFrame; // like Frame::bp, but for C functions
73     int opTop;
74     int markStack[MAX_MARK_DEPTH];
75     int markTop;
76
77     // Free object lists, cached from the global GC
78     struct naObj** free[NUM_NASAL_TYPES];
79     int nfree[NUM_NASAL_TYPES];
80
81     // GC-findable reference point for objects that may live on the
82     // processor ("real") stack during execution.  naNew() places them
83     // here, and clears the array each instruction
84     struct naObj** temps;
85     int ntemps;
86     int tempsz;
87
88     // Error handling
89     jmp_buf jumpHandle;
90     char error[128];
91     naRef dieArg;
92
93     // Sub-call lists
94     struct Context* callParent;
95     struct Context* callChild;
96
97     // Linked list pointers in globals
98     struct Context* nextFree;
99     struct Context* nextAll;
100
101     void* userData;
102 };
103
104 #define globals nasal_globals
105 extern struct Globals* globals;
106
107 // Threading low-level functions
108 void* naNewLock();
109 void naFreeLock(void* lock);
110 void naLock(void* lock);
111 void naUnlock(void* lock);
112 void* naNewSem();
113 void naFreeSem(void* sem);
114 void naSemDown(void* sem);
115 void naSemUp(void* sem, int count);
116
117 void naCheckBottleneck();
118
119 #define LOCK() naLock(globals->lock)
120 #define UNLOCK() naUnlock(globals->lock)
121
122 #endif // _CODE_H