Rune - Generation work
[rune.git] / librune / exp.h
1 /*
2  * EXP.H
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 struct EmitVarStor;
9 struct Exp;
10
11 /*
12  * The interpreter uses 'run'
13  */
14 typedef rundata_t (*ex_run_t)(runctx_p ctx, struct Exp *exp);
15 typedef gendata_p (*ex_gen_t)(genctx_p ctx, struct Exp *exp);
16
17 #define RDF_RSOK        0x0001
18 #define RDF_NODIRECT    0x0002
19 #define RDF_SLOW        (RDF_RSOK | RDF_NODIRECT)
20
21 /*
22  * Exp - expression node
23  *
24  *      note: ex_Run is the first element of the structure in order to
25  *      improve code generated for our internal operator interpreter 
26  *      functions.
27  *
28  *      note: ex_Decl nominally contains a related declaration.  For example,
29  *      for TOK_DECL or TOK_ID.  In some cases it may also contain auxillary
30  *      information.  For example, a compound expression representing 
31  *      procedure arguments may have an ex_Decl that points to the procedure
32  *      decl in order to allow the resolver to do necessary procedural
33  *      rewrites.
34  *
35  *      note: binary operations must call the Lhs before calling the
36  *      Rhs to avoid clobbering temporary storage.  Additionally,
37  *      you should be careful not to return temporary storage that
38  *      is declared locally and thus blown away when your procedure
39  *      returns.
40  */
41
42 typedef struct Exp {
43         ex_run_t        ex_Run;         /* interpreter */
44         ex_gen_t        ex_Gen;         /* code generator */
45         struct Exp      *ex_Next;       /* list link used in various contexts */
46         struct Exp      *ex_Lhs;        /* left hand side if binary op */
47         struct Exp      *ex_Rhs;        /* right hand side unary or binary op */
48         struct Type     *ex_Type;       /* result type if cast */
49         struct Declaration *ex_Decl;    /* TOK_OPER, TOK_ID, sizeof, */
50                                         /* typeof, etc.. */
51         char            ex_Prec;        /* Precedence */
52         char            ex_Unused01;
53         char            ex_Unused02;
54         char            ex_Unused03;
55         int             ex_Flags;       /* parser/interpreter/compiler flags */
56         int             ex_Token;       /* TOK_ token */
57         int             ex_Visibility;  /* scope visibility for searches */
58         runesize_t      ex_TmpOffset;   /* interpreter/compiler, temp mem */
59         string_t        ex_Id;          /* string representing token */
60         string_t        ex_ArgId;       /* compound selector */
61         LexRef          ex_LexRef;      /* for error reporting */
62         union {
63                 d_bool_t        bool;
64                 int8_t          int8;
65                 int16_t         int16;
66                 int32_t         int32;
67                 int64_t         int64;
68                 uint8_t         uint8;
69                 uint16_t        uint16;
70                 uint32_t        uint32;
71                 uint64_t        uint64;
72                 float           float32;
73                 double          float64;
74                 long double     float128;
75                 int             flags[2];
76                 struct Exp      *exp;
77                 PointerStor     ptr;
78         } u;
79 } Exp;
80
81 #define ex_Bool         u.bool
82 #define ex_Int8         u.int8
83 #define ex_Int16        u.int16
84 #define ex_Int32        u.int32
85 #define ex_Int64        u.int64
86 #define ex_UInt8        u.uint8
87 #define ex_UInt16       u.uint16
88 #define ex_UInt32       u.uint32
89 #define ex_UInt64       u.uint64
90 #define ex_AuxFlags     u.flags
91 #define ex_AuxExp       u.exp
92 #define ex_Ptr          u.ptr
93 #define ex_Float32      u.float32       /* note: size may not be exactly 4 */
94 #define ex_Float64      u.float64       /* note: size may not be exactly 8 */
95 #define ex_Float128     u.float128      /* note: size may not be exactly 16 */
96
97 /*
98  * Expression flags. 
99  *
100  *      NOTE: EXF_CONST indicates that the result will be the same every
101  *      time, it does NOT indicate that the storage the result is returned
102  *      in is static.
103  *
104  *      NOTE: EXF_SIGNED is only set when the constant has the 'S' suffix.
105  *            Integers with neither 'U' nor 'S' can be signed or unsigned with
106  *            the precedence being signed.
107  */
108 #define EXF_BINARY      0x00000001      /* binary op, else unary if operator */
109 #define EXF_COMPOUND    0x00000002      /* compound expression */
110 #define EXF_UNARY       0x00000004      /* unary op, else binary if operator */
111 #define EXF_CAST_VOIDP  0x00000008      /* casting from void pointer */
112 #define EXF_RESOLVING   0x00000010
113 #define EXF_RESOLVED    0x00000020
114 #define EXF_REQ_TYPE    0x00000040      /* allow exp to return a type */
115 #define EXF_RET_TYPE    0x00000080      /* exp returns a type */
116 #define EXF_REQ_PROC    0x00000100      /* procedural declaration ok */
117 #define EXF_REQ_ARRAY   0x00000200      /* request optimize array op */
118 #define EXF_RET_ARRAY   0x00000400      /* acknowledge optimize array op */
119 #define EXF_REQ_ADDROF  0x00000800      /* request &of optimization */
120 #define EXF_RET_ADDROF  0x00001000      /* acknowledge &of optimization */
121 #define EXF_CONST       0x00002000      /* result is flagged as unchanging */
122 #define EXF_RET_VOID    0x00004000      /* cast to void optimization */
123 #define EXF_NULL        0x00008000      /* NULL special */
124 #define EXF_SUPER       0x00010000      /* the 'super' version of 'this' */
125 #define EXF_PROBCONST   0x00020000      /* probably evaluates to a constant */
126 #define EXF_INDREF      0x00040000      /* special (*ref_type) flag for meth */
127 #define EXF_DUPEXP      0x00080000      /* DupExp() optimization */
128 #define EXF_TMPRESOLVED 0x00100000      /* ex_TmpOffset resolved */
129 #define EXF_PARSE_TYPE  0x00200000      /* parser-supplied type */
130 #define EXF_CALL_CONV   0x00400000      /* converted method call */
131 #define EXF_UNUSED23    0x00800000
132 #define EXF_ALIAS       0x01000000      /* identifier is an alias */
133 #define EXF_REQ_DCOPY   0x02000000      /* request direct copy to target */
134 #define EXF_UNUSED26    0x04000000
135 #define EXF_UNUSED27    0x08000000
136 #define EXF_REQ_DECL    0x10000000
137 #define EXF_RET_DECL    0x20000000
138
139 /*
140  * Precedence calculation
141  */
142 #define EXPREC_CALL     60
143 #define EXPREC_DOT      60
144 #define EXPREC_CLOSURE  60
145 #define EXPREC_USER     40
146 #define EXPREC_PLUS     35
147 #define EXPREC_COMPARE  33
148 #define EXPREC_ANDAND   30
149 #define EXPREC_OROR     30
150 #define EXPREC_ASS      20
151
152 #define EXPREC_UNARY    42
153
154 #define dassert_exp(exp, cond)          if (!(cond)) ExpFatalError((exp), 0)
155
156 #define ExpFatalError(exp, type)                        \
157             do {                                        \
158                     ExpPrintError(exp, type);           \
159                     dassert(0);                         \
160             } while(0)
161
162 #define ALLOCEXP_DEBUG  0
163 #define DUPEXP_DEBUG    0
164
165 #if ALLOCEXP_DEBUG
166 #define AllocExp(tok)   _AllocExpDebug(tok, __FILE__, __LINE__)
167 #else
168 #define AllocExp(tok)   _AllocExp(tok)
169 #endif