]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/code.h
cppbind: convert maps from nasal.
[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 // The idea is that contexts can "cache" allocations to prevent thread
14 // contention on the global pools.  But in practice this interacts
15 // very badly with small subcontext calls, which grab huge numbers of
16 // cached objects and don't use them, causing far more collections
17 // than necessary.  Just leave it at 1 pending a rework of the
18 // collector synchronization.
19 #define OBJ_CACHE_SZ 1
20
21 enum {    
22     OP_NOT, OP_MUL, OP_PLUS, OP_MINUS, OP_DIV, OP_NEG, OP_CAT, OP_LT, OP_LTE,
23     OP_GT, OP_GTE, OP_EQ, OP_NEQ, OP_EACH, OP_JMP, OP_JMPLOOP, OP_JIFNOTPOP,
24     OP_JIFEND, OP_FCALL, OP_MCALL, OP_RETURN, OP_PUSHCONST, OP_PUSHONE,
25     OP_PUSHZERO, OP_PUSHNIL, OP_POP, OP_DUP, OP_XCHG, OP_INSERT, OP_EXTRACT,
26     OP_MEMBER, OP_SETMEMBER, OP_LOCAL, OP_SETLOCAL, OP_NEWVEC, OP_VAPPEND,
27     OP_NEWHASH, OP_HAPPEND, OP_MARK, OP_UNMARK, OP_BREAK, OP_SETSYM, OP_DUP2,
28     OP_INDEX, OP_BREAK2, OP_PUSHEND, OP_JIFTRUE, OP_JIFNOT, OP_FCALLH,
29     OP_MCALLH, OP_XCHG2, OP_UNPACK, OP_SLICE, OP_SLICE2
30 };
31
32 struct Frame {
33     naRef func; // naFunc object
34     naRef locals; // local per-call namespace
35     int ip; // instruction pointer into code
36     int bp; // opStack pointer to start of frame
37 };
38
39 struct Globals {
40     // Garbage collecting allocators:
41     struct naPool pools[NUM_NASAL_TYPES];
42     int allocCount;
43
44     // Dead blocks waiting to be freed when it is safe
45     void** deadBlocks;
46     int deadsz;
47     int ndead;
48     
49     // Threading stuff
50     int nThreads;
51     int waitCount;
52     int needGC;
53     int bottleneck;
54     void* sem;
55     void* lock;
56
57     // Constants
58     naRef meRef;
59     naRef argRef;
60     naRef parentsRef;
61
62     // A hash of symbol names
63     naRef symbols;
64
65     // Vector/hash containing objects which should not be freed by the gc
66     // TODO do we need a separate vector and hash?
67     naRef save;
68     naRef save_hash;
69     int next_gc_key;
70
71     struct Context* freeContexts;
72     struct Context* allContexts;
73 };
74
75 struct Context {
76     // Stack(s)
77     struct Frame fStack[MAX_RECURSION];
78     int fTop;
79     naRef opStack[MAX_STACK_DEPTH];
80     int opFrame; // like Frame::bp, but for C functions
81     int opTop;
82     int markStack[MAX_MARK_DEPTH];
83     int markTop;
84
85     // Free object lists, cached from the global GC
86     struct naObj** free[NUM_NASAL_TYPES];
87     int nfree[NUM_NASAL_TYPES];
88
89     // GC-findable reference point for objects that may live on the
90     // processor ("real") stack during execution.  naNew() places them
91     // here, and clears the array each instruction
92     struct naObj** temps;
93     int ntemps;
94     int tempsz;
95
96     // Error handling
97     jmp_buf jumpHandle;
98     char error[128];
99     naRef dieArg;
100
101     // Sub-call lists
102     struct Context* callParent;
103     struct Context* callChild;
104
105     // Linked list pointers in globals
106     struct Context* nextFree;
107     struct Context* nextAll;
108
109     void* userData;
110 };
111
112 #define globals nasal_globals
113 extern struct Globals* globals;
114
115 // Threading low-level functions
116 void* naNewLock();
117 void naFreeLock(void* lock);
118 void naLock(void* lock);
119 void naUnlock(void* lock);
120 void* naNewSem();
121 void naFreeSem(void* sem);
122 void naSemDown(void* sem);
123 void naSemUp(void* sem, int count);
124
125 void naCheckBottleneck();
126
127 #define LOCK() naLock(globals->lock)
128 #define UNLOCK() naUnlock(globals->lock)
129
130 #endif // _CODE_H