]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/code.h
8b45a75d85ad02da0f45c86a578838e53b707f2f
[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_AND, OP_OR, 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_JIFNOT, OP_JIFNIL, OP_FCALL, OP_MCALL, OP_RETURN,
21     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_FTAIL, OP_MTAIL, OP_SETSYM
25 };
26
27 struct Frame {
28     naRef func; // naFunc object
29     naRef locals; // local per-call namespace
30     int ip; // instruction pointer into code
31     int bp; // opStack pointer to start of frame
32 };
33
34 struct Globals {
35     // Garbage collecting allocators:
36     struct naPool pools[NUM_NASAL_TYPES];
37     int allocCount;
38
39     // Dead blocks waiting to be freed when it is safe
40     void** deadBlocks;
41     int deadsz;
42     int ndead;
43     
44     // Threading stuff
45     int nThreads;
46     int waitCount;
47     int needGC;
48     int bottleneck;
49     void* sem;
50     void* lock;
51
52     // Constants
53     naRef meRef;
54     naRef argRef;
55     naRef parentsRef;
56
57     // A hash of symbol names
58     naRef symbols;
59
60     naRef save;
61
62     struct Context* freeContexts;
63     struct Context* allContexts;
64 };
65
66 struct Context {
67     // Stack(s)
68     struct Frame fStack[MAX_RECURSION];
69     int fTop;
70     naRef opStack[MAX_STACK_DEPTH];
71     int opTop;
72     int markStack[MAX_MARK_DEPTH];
73     int markTop;
74
75     // Free object lists, cached from the global GC
76     struct naObj** free[NUM_NASAL_TYPES];
77     int nfree[NUM_NASAL_TYPES];
78
79     // GC-findable reference point for objects that may live on the
80     // processor ("real") stack during execution.  naNew() places them
81     // here, and clears the array each instruction
82     naRef temps;
83
84     // Error handling
85     jmp_buf jumpHandle;
86     char* error;
87     naRef dieArg;
88
89     // Sub-call lists
90     struct Context* callParent;
91     struct Context* callChild;
92
93     // Linked list pointers in globals
94     struct Context* nextFree;
95     struct Context* nextAll;
96 };
97
98 #define globals nasal_globals
99 extern struct Globals* globals;
100
101 // Threading low-level functions
102 void* naNewLock();
103 void naLock(void* lock);
104 void naUnlock(void* lock);
105 void* naNewSem();
106 void naSemDown(void* sem);
107 void naSemUpAll(void* sem, int count);
108
109 void naCheckBottleneck();
110
111 #define LOCK() naLock(globals->lock)
112 #define UNLOCK() naUnlock(globals->lock)
113
114 #endif // _CODE_H