Interpreter - Incremental API changes
[rune.git] / libi / stor.h
1 /*
2  * STOR.H       - Physical storage information for interpreter
3  *
4  * (c)Copyright 1993-2014, Matthew Dillon, All Rights Reserved.  See the  
5  *    COPYRIGHT file at the base of the distribution.
6  */
7
8 extern int      ICount;
9
10 typedef struct ThreadCall {
11         Context *tc_ArgsCtx;
12         Stmt    *tc_Stmt;
13         List    tc_List;
14         Thread  *tc_CallingThread;
15 } ThreadCall;
16
17 /*
18  * XXX XXX bad bad... assumes variable is on process local stack.  We
19  * need to figure out:
20  *
21  *      - process arguments
22  *      - module globals
23  *      - class globals  
24  *
25  * XXX import/class through object, import/class through type name..
26  *      determines whether we must require GLOBAL or not.
27  */
28
29 Context *BuildSemGroupContext(SemGroup *sg, int isglobal);
30 void InterpSemGroupDefaultInit(runctx_p ct, RefStor *rs, char *data,
31                         runesize_t bytes, SemGroup *sg, int isglobal);
32 void freeRefStor(RefStor *rs);
33
34 #define REFSTOR_DEBUG   0
35 #define USE_CONTEXT_REFSTOR     1
36
37 #if REFSTOR_DEBUG
38 #define REFSTOR_DEBUG_ARGS      , const char *file, int line
39 #define REFSTOR_DEBUG_CALL      , file, line
40 #define allocRefStor(op)        allocRefStor_debug(op, __FILE__, __LINE__)
41 #define refsRefStor(rs)         _refsRefStor(rs, __FILE__, __LINE__)
42 #define relsRefStor(rs)         _relsRefStor(rs, __FILE__, __LINE__)
43 #define updateRefStor(prs, rs)  \
44                 _updateRefStor(prs, rs, __FILE__, __LINE__)
45 #define replaceRefStor(prs, rs) \
46                 _replaceRefStor(prs, rs, __FILE__, __LINE__)
47 #else
48 #define REFSTOR_DEBUG_ARGS
49 #define REFSTOR_DEBUG_CALL
50 #define allocRefStor(op)        allocRefStor_norm(op)
51 #define refsRefStor(rs)         _refsRefStor(rs)
52 #define relsRefStor(rs)         _relsRefStor(rs)
53 #define updateRefStor(prs, rs)  _updateRefStor(prs, rs)
54 #define replaceRefStor(prs, rs) _replaceRefStor(prs, rs)
55 #endif
56
57 static __inline
58 void
59 _relsRefStor(RefStor *rs REFSTOR_DEBUG_ARGS)
60 {
61         if (rs) {
62 #if REFSTOR_DEBUG
63                 printf("RELSREFSTOR %s/%d %p %d %s (%04x)\n",
64                        file, line, rs, rs->rs_Refs - 1,
65                        TypeToStr(rs->rs_Type, NULL), rs->rs_Op);
66 #endif
67                 if (--rs->rs_Refs <= 0)
68                         freeRefStor(rs);
69         }
70 }
71
72 static __inline
73 void
74 _refsRefStor(RefStor *rs REFSTOR_DEBUG_ARGS)
75 {
76         if (rs) {
77                 dassert(rs->rs_Refs > 0);
78                 ++rs->rs_Refs;
79 #if REFSTOR_DEBUG
80                 printf("REFSREFSTOR %s/%d %p %d (%04x)\n",
81                        file, line, rs, rs->rs_Refs, rs->rs_Op);
82 #endif
83         }
84 }
85
86 static __inline
87 void
88 _updateRefStor(RefStor **prs, RefStor *rs REFSTOR_DEBUG_ARGS)
89 {
90         _refsRefStor(rs REFSTOR_DEBUG_CALL);
91         _relsRefStor(*prs REFSTOR_DEBUG_CALL);
92         *prs = rs;
93 }
94
95 static __inline
96 void
97 _replaceRefStor(RefStor **prs, RefStor *rs REFSTOR_DEBUG_ARGS)
98 {
99         _relsRefStor(*prs REFSTOR_DEBUG_CALL);
100         *prs = rs;
101 }
102
103 static __inline
104 void *
105 getExpTmpData(runctx_p ct, Exp *exp)
106 {
107         runesize_t offset = exp->ex_TmpOffset;
108 #if 1
109         dassert_exp(exp, (exp->ex_Flags & EXF_TMP_OK) &&
110                          offset + exp->ex_Type->ty_Bytes <= ct->ct_TmpBytes);
111 #endif
112         return(ct->ct_TmpData + offset);
113 }