summaryrefslogtreecommitdiffstats
path: root/private/windbg/eecan/debtree.c
blob: 2daa67902acd72151f27294d18d1917266e06987 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/***    DEBTREE.C - Routines for building expression tree
 *
 *      Routines to take tokens lexed from expression string
 *      and add to expression tree (tree building routines).
 */




LOCAL   ushort  NEAR    PASCAL  AddLeaf (ptoken_t);
LOCAL   ushort  NEAR    PASCAL  AddNode (ptoken_t);





/***    PushToken - Push a token onto expression stack
 *
 *
 *      fSuccess = PushToken (ptok)
 *
 *      Entry   ptok = pointer to token_t structure to add
 *
 *
 *      Returns TRUE if token pushed ont expression stack
 *              FALSE if error
 */


ushort PASCAL PushToken (ptoken_t ptok)
{
    ushort      retVal;

    if (OP_IS_IDENT (ptok->opTok)) {
        // if the node is an identifier, constant or end of function arguments
        // just add it to the tree
        retVal = AddLeaf (ptok);
    }
    else {
        retVal = AddNode (ptok);
    }
    return (retVal);
}




/***    AddLeaf - Add a leaf node to the expression tree
 *
 *      fSuccess = AddLeaf (ptok)
 *
 *      Entry   ptok = pointer to token to add to tree
 *              pExState->hStree = handle of syntax tree buffer
 *
 *      Returns TRUE if successful
 *              FALSE if not and sets DebErr
 *
 *      Notes   The start_node index in the stree_t structure is not changed
 *              by AddLeaf.  If there is only a single node, (OP_ident or
 *              OP_const), then start_node points to the correct node that
 *              was set by the routine that initialized the tree buffer.
 *              AddNode will set the correct node if there is a
 *              more complex parse tree.
 */


LOCAL ushort NEAR PASCAL AddLeaf (ptoken_t ptok)
{
    pnode_t     pn;
    peval_t     pv;
    bnode_t FAR *ia;
    uint        len;

    //  check for space

    len = sizeof (node_t) + sizeof (eval_t);
    if ((uint)(pTree->stack_base - pTree->node_next) < len) {
        // syntax tree is full, grow it
        if (!GrowTree (NODE_DEFAULT)) {
            // grow failed, clean up and return error
            pExState->err_num = ERR_TOOCOMPLEX;
            return (EEGENERAL);
        }
    }
    if (pTree->stack_next == NSTACK_MAX) {
        pExState->err_num = ERR_TOOCOMPLEX;
        return EEGENERAL;
    }
        
    pn = pnodeOfbnode((bnode_t)(pTree->node_next));

    // Initialize the Node.

    _fmemset ((char FAR *)pn, 0, len);
    NODE_OP (pn) = ptok->opTok;
    pv = &pn->v[0];
    EVAL_ITOK (pv) = ptok->iTokStart;
    EVAL_CBTOK (pv) = ptok->cbTok;

    // If the node is a constant, retrieve its value from the token.

    if (NODE_OP (pn) == OP_const) {
        EVAL_TYP (pv) = ptok->typ;
        EVAL_VAL (pv) = VAL_VAL (ptok);
    }
    ia = (bnode_t FAR *)((char FAR *)pTree + pTree->stack_base);
    ia[pTree->stack_next++] = (bnode_t)(pTree->node_next);
    pTree->node_next += len;
    return (0);
}




/**     AddNode - Add an operator node to the expression tree
 *
 *      fSuccess = AddNode (ptok)
 *
 *      Entry   ptok = pointer to token to add
 *
 *      Returns TRUE if node was added to tree
 *              FALSE if error
 *
 *      Notes   AddNode will set the start_node index in the stree_t structure
 *              to point to the node just added.  If the statement is
 *              correctly parsed, start_node will contain the index of the
 *              last operator and there will be only one node on the stack
 *
 */


LOCAL ushort NEAR PASCAL AddNode (ptoken_t ptok)
{
    pnode_t     pn;
    ushort      retval = 0;
    uint        iStack;
    bnode_t FAR *ia;
    uint        len;

    //  check for space

    switch (ptok->opTok) {
        case OP_cast:
        case OP_function:
        case OP_sizeof:
        case OP_dot:
        case OP_pointsto:
        case OP_bscope:
        case OP_pmember:
        case OP_dotmember:
    case OP_mult:
    case OP_and:
            // these operators use the eval_t portion of the node to store
            // casting information.  In the case of the OP_dot, etc., the
            // casting information is the implicit cast of the left member
            // to a base class if one is required.

            len = sizeof (node_t) + sizeof (eval_t);
            break;

        case OP_arg:
            len = sizeof (node_t) + sizeof (argd_t);
            break;

        case OP_context:
        case OP_execontext:
            len = sizeof (node_t) + sizeof (CXF);
            break;

        default:
            len = sizeof (node_t);
            break;
    }
    len = (len + 1) & ~1;
    if ((uint)(pTree->stack_base - pTree->node_next) < len) {
        // syntax tree is full, grow it
        if (!GrowTree (NODE_DEFAULT)) {
            // grow failed, clean up and return error
            pExState->err_num = ERR_TOOCOMPLEX;
            return (EEGENERAL);
        }
    }
    pn = pnodeOfbnode((bnode_t)(pTree->node_next));

    // Initialize the Node.

    _fmemset (pn, 0, len);
    NODE_OP (pn) = ptok->opTok;
    if (ptok->opTok == OP_context || ptok->opTok == OP_execontext) {
        EVAL_ITOK (&pn->v[0]) = ptok->iTokStart;
        EVAL_CBTOK (&pn->v[0]) = ptok->cbTok;
    }

    // Set up the left and right children (left only if unary).

    ia = (bnode_t FAR *)((char FAR *)pTree + pTree->stack_base);
    iStack = pTree->stack_next;
    if (OP_IS_BINARY (ptok->opTok)) {
        if (iStack < 2) {
            pExState->err_num = ERR_NOOPERAND;
            retval = EEGENERAL;
        }
        else {
            pn->pnRight = ia[--iStack];
            pn->pnLeft  = ia[--iStack];
        }
    }
    else {
        if (iStack < 1) {
            pExState->err_num = ERR_NOOPERAND;
            retval = EEGENERAL;
        }
        else {
            pn->pnLeft  = ia[--iStack];
            pn->pnRight = 0;
        }
    }

    // set this node into the start_node

    if (retval == 0) {
        ia[iStack++] = (bnode_t)(pTree->node_next);
        pTree->start_node = pTree->node_next;
        pTree->node_next += len;
        pTree->stack_next = iStack;
    }
    return (retval);
}





/***    GrowTree - Grow space for the expression tree
 *
 *      error = GrowTree (incr)
 *
 *      Entry   incr = amount to increase tree size
 *              pExState = address of expression state structure
 *              pExState->hSTree locked
 *
 *      Exit    pExState->hSTree locked
 *              pTree = address of locked syntax tree
 *
 *      Returns TRUE if successful
 *              FALSE if unsuccessful
 *
 */


bool_t FASTCALL GrowTree (uint incr)
{
    register HDEP    NewTree;
    register uint    len;

    len = pTree->size + incr;
    MHMemUnLock (pExState->hSTree);
    if ((NewTree = MHMemReAlloc (pExState->hSTree, len)) == 0) {
        pTree = MHMemLock (pExState->hSTree);
        return (FALSE);
    }
    else {
        // reallocation succeeded, move data within buffer and update header

        pExState->hSTree = NewTree;
        pTree = MHMemLock (pExState->hSTree);
        pTree->size = len;

        // move parse stack down by allocated amount if the stack is in use
        // by the parser.  Stack in use is indicated by non-zero stack_base.
        // If we are in the bind phase, stack_base is zero,

        if (pTree->stack_base != 0) {
            _fmemcpy ((char FAR *)pTree + pTree->stack_base + NODE_DEFAULT,
              (char FAR *)pTree + pTree->stack_base,
              NSTACK_MAX * sizeof (bnode_t));
            pTree->stack_base += NODE_DEFAULT;
        }
        return (TRUE);
    }
}






/***    GrowETree - Grow space for the expression tree
 *
 *      error = GrowETree (incr)
 *
 *      Entry   incr = amount to increase tree size
 *              pExState = address of expression state structure
 *              pExState->hETree locked
 *
 *      Exit    pExState->hETree locked
 *              pTree = address of locked syntax tree
 *
 *      Returns TRUE if successful
 *              FALSE if unsuccessful
 *
 */


bool_t FASTCALL GrowETree (uint incr)
{
    register HDEP       NewTree;
    register uint       len;

    len = pTree->size + incr;
    MHMemUnLock (pExState->hETree);
    if ((NewTree = MHMemReAlloc (pExState->hETree, len)) == 0) {
        pTree = MHMemLock (pExState->hETree);
        return (FALSE);
    }
    else {
        // reallocation succeeded, move data within buffer and update header

        pExState->hETree = NewTree;
        pTree = MHMemLock (pExState->hETree);
        pTree->size = len;

        // move parse stack down by allocated amount if the stack is in use
        // by the parser.  Stack in use is indicated by non-zero stack_base.
        // If we are in the bind phase, stack_base is zero,

        if (pTree->stack_base != 0) {
            _fmemcpy ((char FAR *)pTree + pTree->stack_base + NODE_DEFAULT,
              (char FAR *)pTree + pTree->stack_base,
              NSTACK_MAX * sizeof (bnode_t));
            pTree->stack_base += NODE_DEFAULT;
        }
        return (TRUE);
    }
}