OpenCores
URL https://opencores.org/ocsvn/thor/thor/trunk

Subversion Repositories thor

[/] [thor/] [trunk/] [FT64v5/] [software/] [CC64/] [source/] [ParseExpressions.cpp] - Blame information for rev 48

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2012-2018  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@finitron.ca
6
//       ||
7
//
8
// CC64 - 'C' derived language compiler
9
//  - 64 bit CPU
10
//
11
// This source file is free software: you can redistribute it and/or modify 
12
// it under the terms of the GNU Lesser General Public License as published 
13
// by the Free Software Foundation, either version 3 of the License, or     
14
// (at your option) any later version.                                      
15
//                                                                          
16
// This source file is distributed in the hope that it will be useful,      
17
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
18
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
19
// GNU General Public License for more details.                             
20
//                                                                          
21
// You should have received a copy of the GNU General Public License        
22
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
23
//                                                                          
24
// ============================================================================
25
//
26
#include "stdafx.h"
27
 
28
#define EXPR_DEBUG
29
#define NUM_DIMEN       20
30
extern SYM *currentClass;
31
static unsigned char sizeof_flag = 0;
32
static TYP *ParseCastExpression(ENODE **node);
33
static TYP *NonAssignExpression(ENODE **node);
34
TYP *forcefit(ENODE **node1,TYP *tp1,ENODE **node2,TYP *tp2,bool);
35
extern void backup();
36
extern char *inpline;
37
extern int parsingParameterList;
38
extern SYM *gsearch2(std::string , __int16, TypeArray *,bool);
39
extern SYM *search2(std::string na,TABLE *tbl,TypeArray *typearray);
40
extern int round8(int n);
41
 
42
ENODE *pep1;
43
TYP *ptp1;      // the type just previous to the last dot
44
int pop;   // the just previous operator.
45
 
46
// Tells subsequent levels that ParseCastExpression already fetched a token.
47
//static unsigned char expr_flag = 0;
48
 
49
TYP                             stdvoid;
50
TYP             stdint;
51
TYP             stduint;
52
TYP             stdlong;
53
TYP             stdulong;
54
TYP             stdshort;
55
TYP             stdushort;
56
TYP             stdchar;
57
TYP             stduchar;
58
TYP             stdbyte;
59
TYP             stdubyte;
60
TYP             stdstring;
61
TYP                             stddbl;
62
TYP                             stdtriple;
63
TYP                             stdflt;
64
TYP                             stddouble;
65
TYP                             stdquad;
66
TYP             stdfunc;
67
TYP             stdexception;
68
extern TYP      *head;          /* shared with ParseSpecifier */
69
extern TYP      *tail;
70
 
71
/*
72
 *      expression evaluation
73
 *
74
 *      this set of routines builds a parse tree for an expression.
75
 *      no code is generated for the expressions during the build,
76
 *      this is the job of the codegen module. for most purposes
77
 *      expression() is the routine to call. it will allow all of
78
 *      the C operators. for the case where the comma operator is
79
 *      not valid (function parameters for instance) call NonCommaExpression().
80
 *
81
 *      each of the routines returns a pointer to a describing type
82
 *      structure. each routine also takes one parameter which is a
83
 *      pointer to an expression node by reference (address of pointer).
84
 *      the completed expression is returned in this pointer. all
85
 *      routines return either a pointer to a valid type or NULL if
86
 *      the hierarchy of the next operator is too low or the next
87
 *      symbol is not part of an expression.
88
 */
89
 
90
TYP     *expression();  /* forward ParseSpecifieraration */
91
TYP     *NonCommaExpression();      /* forward ParseSpecifieraration */
92
TYP     *ParseUnaryExpression();       /* forward ParseSpecifieraration */
93
 
94
int nest_level = 0;
95
bool isMember;
96
 
97
void Enter(char *p)
98
{
99
/*
100
  int nn;
101
 
102
  for (nn = 0; nn < nest_level && nn < 60; nn++)
103
    printf("  ");
104
  printf("%s: %d\r\n", p, lineno);
105
  nest_level++;
106
*/
107
}
108
void Leave(char *p, int n)
109
{
110
/*
111
     int nn;
112
 
113
     nest_level--;
114
     for (nn = 0; nn < nest_level; nn++)
115
         printf("   ");
116
     printf("%s (%d)\r\n", p, n);
117
*/
118
}
119
 
120
int GetPtrSize()
121
{
122
        return sizeOfPtr;
123
}
124
 
125
 
126
/*
127
 *      build an expression node with a node type of nt and values
128
 *      v1 and v2.
129
 */
130
ENODE *makenode(int nt, ENODE *v1, ENODE *v2)
131
{
132
        ENODE *ep;
133
        ep = (ENODE *)xalloc(sizeof(ENODE));
134
        ep->nodetype = (enum e_node)nt;
135
 
136
        if (v1!=nullptr && v2 != nullptr) {
137
                ep->constflag = v1->constflag && v2->constflag;
138
                ep->isUnsigned = v1->isUnsigned && v2->isUnsigned;
139
        }
140
        else if (v1 != nullptr) {
141
                ep->constflag = v1->constflag;
142
                ep->isUnsigned = v1->isUnsigned;
143
        }
144
        else if (v2 != nullptr) {
145
                ep->constflag = v2->constflag;
146
                ep->isUnsigned = v2->isUnsigned;
147
        }
148
        ep->etype = bt_void;
149
        ep->esize = -1;
150
        ep->p[0] = v1;
151
        ep->p[1] = v2;
152
        ep->p[2] = 0;
153
    return (ep);
154
}
155
 
156
ENODE *makefcnode(int nt, ENODE *v1, ENODE *v2, SYM *sp)
157
{
158
        ENODE *ep;
159
  ep = (ENODE *)xalloc(sizeof(ENODE));
160
  ep->nodetype = (enum e_node)nt;
161
  ep->sym = sp;
162
  ep->constflag = FALSE;
163
        ep->isUnsigned = FALSE;
164
        ep->etype = bt_void;
165
        ep->esize = -1;
166
        ep->p[0] = v1;
167
        ep->p[1] = v2;
168
        ep->p[2] = 0;
169
  return ep;
170
}
171
 
172
ENODE *makesnode(int nt, std::string *v1, std::string *v2, int64_t i)
173
{
174
        ENODE *ep;
175
  ep = allocEnode();
176
  ep->nodetype = (enum e_node)nt;
177
  ep->constflag = FALSE;
178
        ep->isUnsigned = FALSE;
179
        ep->etype = bt_void;
180
        ep->esize = -1;
181
        ep->sp = v1;
182
  ep->msp = v2;
183
        ep->i = i;
184
        ep->p[0] = 0;
185
        ep->p[1] = 0;
186
        ep->p[2] = 0;
187
  return ep;
188
}
189
 
190
ENODE *makenodei(int nt, ENODE *v1, int i)
191
{
192
        ENODE *ep;
193
  ep = allocEnode();
194
  ep->nodetype = (enum e_node)nt;
195
  ep->constflag = FALSE;
196
        ep->isUnsigned = FALSE;
197
        ep->etype = bt_void;
198
        ep->esize = -1;
199
        ep->i = i;
200
        ep->p[0] = v1;
201
        ep->p[1] = (ENODE *)NULL;
202
        ep->p[2] = 0;
203
    return ep;
204
}
205
 
206
ENODE *makeinode(int nt, int64_t v1)
207
{
208
        ENODE *ep;
209
        ep = allocEnode();
210
        ep->nodetype = (enum e_node)nt;
211
        ep->constflag = TRUE;
212
        ep->isUnsigned = FALSE;
213
        ep->etype = bt_void;
214
        ep->esize = -1;
215
        ep->i = v1;
216
        ep->p[1] = 0;
217
        ep->p[0] = 0;
218
        ep->p[2] = 0;
219
    return ep;
220
}
221
 
222
ENODE *makefnode(int nt, double v1)
223
{
224
        ENODE *ep;
225
  ep = allocEnode();
226
  ep->nodetype = (enum e_node)nt;
227
  ep->constflag = TRUE;
228
        ep->isUnsigned = FALSE;
229
        ep->etype = bt_void;
230
        ep->esize = -1;
231
        ep->f = v1;
232
  ep->f1 = v1;
233
//    ep->f2 = v2;
234
        ep->p[0] = 0;
235
        ep->p[1] = 0;
236
        ep->p[2] = 0;
237
  return ep;
238
}
239
 
240
ENODE *makefqnode(int nt, Float128 *f128)
241
{
242
        ENODE *ep;
243
  ep = allocEnode();
244
  ep->nodetype = (enum e_node)nt;
245
  ep->constflag = TRUE;
246
        ep->isUnsigned = FALSE;
247
        ep->etype = bt_void;
248
        ep->esize = -1;
249
  Float128::Assign(&ep->f128,f128);
250
//    ep->f2 = v2;
251
        ep->p[0] = 0;
252
        ep->p[1] = 0;
253
        ep->p[2] = 0;
254
  return ep;
255
}
256
 
257
void AddToList(ENODE *list, ENODE *ele)
258
{
259
        ENODE *p, *pp;
260
 
261
        p = list;
262
        pp = nullptr;
263
        while (p) {
264
                pp = p;
265
                p = p->p[2];
266
        }
267
        if (pp) {
268
                pp->p[2] = ele;
269
        }
270
        else
271
                list->p[2] = ele;
272
}
273
 
274
bool IsMemberOperator(int op)
275
{
276
  return op==dot || op==pointsto || op==double_colon;
277
}
278
 
279
bool IsClassExpr()
280
{
281
  TYP *tp;
282
 
283
        if (pep1) {// && IsMemberOperator(pop)) {
284
                if (pep1->tp) {
285
                        if (pep1->tp->type==bt_class){
286
                                return true;
287
                        }
288
                        else if (pep1->tp->type==bt_pointer) {
289
                          tp = pep1->tp->GetBtp();
290
                          if (tp==nullptr)
291
                             throw new C64PException(ERR_NULLPOINTER,4);
292
                                if (tp->type==bt_class) {
293
                                        return true;
294
                                }
295
                        }
296
                }
297
        }
298
        return false;
299
}
300
 
301
void PromoteConstFlag(ENODE *ep)
302
{
303
        if (ep->p[0]==nullptr || ep->p[1]==nullptr) {
304
                ep->constflag = false;
305
                return;
306
        }
307
        ep->constflag = ep->p[0]->constflag && ep->p[1]->constflag;
308
}
309
 
310
/*
311
 *      build the proper dereference operation for a node using the
312
 *      type pointer tp.
313
 */
314
TYP *deref(ENODE **node, TYP *tp)
315
{
316
  dfs.printf("<Deref>");
317
  if (tp==nullptr || node==nullptr || *node==nullptr)
318
    throw new C64PException(ERR_NULLPOINTER,8);
319
        switch( tp->type ) {
320
                case bt_byte:
321
                        if (tp->isUnsigned) {
322
                                *node = makenode(en_ub_ref,*node,(ENODE *)NULL);
323
                                (*node)->isUnsigned = TRUE;
324
                        }
325
                        else {
326
                                *node = makenode(en_b_ref,*node,(ENODE *)NULL);
327
                        }
328
                        (*node)->esize = tp->size;
329
                        (*node)->etype = (enum e_bt)tp->type;
330
                        if (tp->isUnsigned)
331
                    tp = &stdubyte;//&stduint;
332
                        else
333
                    tp = &stdbyte;//&stdint;
334
            break;
335
                case bt_ubyte:
336
                        *node = makenode(en_ub_ref,*node,(ENODE *)NULL);
337
                        (*node)->isUnsigned = TRUE;
338
                        (*node)->esize = tp->size;
339
                        (*node)->etype = (enum e_bt)tp->type;
340
            tp = &stdubyte;//&stduint;
341
            break;
342
                case bt_uchar:
343
                case bt_char:
344
        case bt_enum:
345
                        if (tp->isUnsigned) {
346
                                *node = makenode(en_uc_ref,*node,(ENODE *)NULL);
347
                                (*node)->isUnsigned = TRUE;
348
                        }
349
                        else
350
                                *node = makenode(en_c_ref,*node,(ENODE *)NULL);
351
                        (*node)->esize = tp->size;
352
                        (*node)->etype = (enum e_bt)tp->type;
353
                        if (tp->isUnsigned)
354
                    tp = &stduchar;
355
                        else
356
                    tp = &stdchar;
357
            break;
358
                case bt_ushort:
359
                case bt_short:
360
                        if (tp->isUnsigned) {
361
                                *node = makenode(en_uh_ref,*node,(ENODE *)NULL);
362
                                (*node)->esize = tp->size;
363
                                (*node)->etype = (enum e_bt)tp->type;
364
                                (*node)->isUnsigned = TRUE;
365
                                tp = &stduint;
366
                        }
367
                        else {
368
                                *node = makenode(en_h_ref,*node,(ENODE *)NULL);
369
                                (*node)->esize = tp->size;
370
                                (*node)->etype = (enum e_bt)tp->type;
371
                                tp = &stdint;
372
                        }
373
      break;
374
                case bt_exception:
375
                        (*node)->esize = tp->size;
376
                        (*node)->etype = (enum e_bt)tp->type;
377
                        (*node)->isUnsigned = TRUE;
378
                        *node = makenode(en_uw_ref,*node,(ENODE *)NULL);
379
            break;
380
 
381
                case bt_ulong:
382
                case bt_long:
383
                        (*node)->esize = tp->size;
384
                        (*node)->etype = (enum e_bt)tp->type;
385
                        if (tp->isUnsigned) {
386
                                (*node)->isUnsigned = TRUE;
387
                                *node = makenode(en_uw_ref,*node,(ENODE *)NULL);
388
                        }
389
                        else {
390
                                *node = makenode(en_w_ref,*node,(ENODE *)NULL);
391
                        }
392
                        break;
393
 
394
                case bt_vector:
395
                        (*node)->esize = tp->size;
396
                        (*node)->etype = (enum e_bt)tp->type;
397
            *node = makenode(en_vector_ref,*node,(ENODE *)NULL);
398
                        (*node)->isUnsigned = TRUE;
399
            break;
400
                case bt_vector_mask:
401
                        (*node)->esize = tp->size;
402
                        (*node)->etype = (enum e_bt)tp->type;
403
            *node = makenode(en_uw_ref,*node,(ENODE *)NULL);
404
                        (*node)->isUnsigned = TRUE;
405
                        (*node)->vmask = (*node)->p[0]->vmask;
406
            break;
407
 
408
                // Pointers (addresses) are always unsigned
409
                case bt_pointer:
410
                        (*node)->esize = tp->size;
411
                        (*node)->etype = (enum e_bt)tp->type;
412
                        *node = makenode(sizeOfPtr==4 ? en_hp_ref : en_wp_ref,*node,(ENODE *)NULL);
413
                        (*node)->isUnsigned = TRUE;
414
            break;
415
 
416
                case bt_unsigned:
417
                        (*node)->esize = tp->size;
418
                        (*node)->etype = (enum e_bt)tp->type;
419
            *node = makenode(en_uw_ref,*node,(ENODE *)NULL);
420
                        (*node)->isUnsigned = TRUE;
421
            break;
422
 
423
    case bt_triple:
424
            *node = makenode(en_triple_ref,*node,(ENODE *)NULL);
425
                        (*node)->esize = tp->size;
426
                        (*node)->etype = (enum e_bt)tp->type;
427
            tp = &stdtriple;
428
            break;
429
        case bt_quad:
430
            *node = makenode(en_quad_ref,*node,(ENODE *)NULL);
431
                        (*node)->esize = tp->size;
432
                        (*node)->etype = (enum e_bt)tp->type;
433
            tp = &stdquad;
434
            break;
435
        case bt_double:
436
            *node = makenode(en_dbl_ref,*node,(ENODE *)NULL);
437
                        (*node)->esize = tp->size;
438
                        (*node)->etype = (enum e_bt)tp->type;
439
            tp = &stddbl;
440
            break;
441
        case bt_float:
442
            *node = makenode(en_flt_ref,*node,(ENODE *)NULL);
443
                        (*node)->esize = tp->size;
444
                        (*node)->etype = (enum e_bt)tp->type;
445
            tp = &stdflt;
446
            break;
447
                case bt_bitfield:
448
                        if (tp->isUnsigned){
449
                                switch (tp->size) {
450
                                case 1: *node = makenode(en_ubfieldref, *node, (ENODE *)NULL); break;
451
                                case 2: *node = makenode(en_ucfieldref, *node, (ENODE *)NULL); break;
452
                                case 4: *node = makenode(en_uhfieldref, *node, (ENODE *)NULL); break;
453
                                case 8: *node = makenode(en_uwfieldref, *node, (ENODE *)NULL); break;
454
                                }
455
                                (*node)->isUnsigned = TRUE;
456
                        }
457
                        else {
458
                                switch (tp->size) {
459
                                case 1: *node = makenode(en_bfieldref, *node, (ENODE *)NULL); break;
460
                                case 2: *node = makenode(en_cfieldref, *node, (ENODE *)NULL); break;
461
                                case 4: *node = makenode(en_hfieldref, *node, (ENODE *)NULL); break;
462
                                case 8: *node = makenode(en_wfieldref, *node, (ENODE *)NULL); break;
463
                                }
464
                        }
465
                        (*node)->bit_width = tp->bit_width;
466
                        (*node)->bit_offset = tp->bit_offset;
467
                        /*
468
                        * maybe it should be 'unsigned'
469
                        */
470
                        (*node)->etype = tp->type;//(enum e_bt)stdint.type;
471
                        (*node)->esize = tp->size;
472
                        tp = &stdint;
473
                        break;
474
                case bt_ubitfield:
475
                        switch (tp->size) {
476
                        case 1: *node = makenode(en_ubfieldref, *node, (ENODE *)NULL); break;
477
                        case 2: *node = makenode(en_ucfieldref, *node, (ENODE *)NULL); break;
478
                        case 4: *node = makenode(en_uhfieldref, *node, (ENODE *)NULL); break;
479
                        case 8: *node = makenode(en_uwfieldref, *node, (ENODE *)NULL); break;
480
                        }
481
                        (*node)->bit_width = tp->bit_width;
482
                        (*node)->bit_offset = tp->bit_offset;
483
                        /*
484
                        * maybe it should be 'unsigned'
485
                        */
486
                        (*node)->etype = tp->type;//(enum e_bt)stdint.type;
487
                        (*node)->esize = tp->size;
488
                        tp = &stdint;
489
                        break;
490
                //case bt_func:
491
                //case bt_ifunc:
492
                //      (*node)->esize = tp->size;
493
                //      (*node)->etype = tp->type;
494
                //      (*node)->isUnsigned = TRUE;
495
                //      *node = makenode(en_uw_ref,*node,NULL);
496
  //          break;
497
                //case bt_class:
498
                //case bt_struct:
499
                //case bt_union:
500
                //  dfs.printf("F");
501
                //      (*node)->esize = tp->size;
502
                //      (*node)->etype = (e_bt)tp->type;
503
  //    *node = makenode(en_struct_ref,*node,NULL);
504
                //      (*node)->isUnsigned = TRUE;
505
  //    break;
506
                default:
507
                  dfs.printf("Deref :%d\n", tp->type);
508
                  if ((*node)->msp)
509
                     dfs.printf("%s\n",(char *)(*node)->msp->c_str());
510
                        error(ERR_DEREF);
511
                        break;
512
    }
513
        (*node)->isVolatile = tp->isVolatile;
514
        (*node)->constflag = tp->isConst;
515
  dfs.printf("</Deref>");
516
    return tp;
517
}
518
 
519
/*
520
* dereference the node if val_flag is zero. If val_flag is non_zero and
521
* tp->type is bt_pointer (array reference) set the size field to the
522
* pointer size if this code is not executed on behalf of a sizeof
523
* operator
524
*/
525
TYP *CondDeref(ENODE **node, TYP *tp)
526
{
527
        TYP *tp1;
528
        int64_t sz;
529
        int dimen;
530
        int numele;
531
 
532
        if (tp->isArray == false)
533
                if (tp->type != bt_struct
534
                        && tp->type != bt_union
535
                        && tp->type != bt_class
536
                        && tp->type != bt_ifunc
537
                        && tp->type != bt_func)
538
                        return deref(node, tp);
539
        if (tp->type == bt_pointer && sizeof_flag == 0) {
540
                sz = tp->size;
541
                dimen = tp->dimen;
542
                numele = tp->numele;
543
                tp1 = tp->GetBtp();
544
                if (tp1==NULL)
545
                        printf("DIAG: CondDeref: tp1 is NULL\r\n");
546
                tp =(TYP *) TYP::Make(bt_pointer, sizeOfPtr);
547
                tp->isArray = true;
548
                tp->dimen = dimen;
549
                tp->numele = numele;
550
                tp->btp = tp1->GetIndex();
551
                tp->isUnsigned = TRUE;
552
        }
553
        else if (tp->type==bt_pointer)
554
                return tp;
555
        //    else if (tp->type==bt_struct || tp->type==bt_union)
556
        //       return deref(node, tp);
557
        return tp;
558
}
559
/*
560
TYP *CondDeref(ENODE **node, TYP *tp)
561
{
562
  if (tp->val_flag == 0)
563
    return deref(node, tp);
564
  if (tp->type == bt_pointer && sizeof_flag == 0)
565
        tp->size = 2;
566
  return tp;
567
}
568
*/
569
 
570
/*
571
 *      nameref will build an expression tree that references an
572
 *      identifier. if the identifier is not in the global or
573
 *      local symbol table then a look-ahead to the next character
574
 *      is done and if it indicates a function call the identifier
575
 *      is coerced to an external function name. non-value references
576
 *      generate an additional level of indirection.
577
 */
578
TYP *nameref2(std::string name, ENODE **node,int nt,bool alloc,TypeArray *typearray, TABLE *tbl)
579
{
580
        SYM *sp = nullptr;
581
        Function *fn;
582
        TYP *tp;
583
        std::string stnm;
584
 
585
        dfs.puts("<nameref2>\n");
586
        if (tbl) {
587
                dfs.printf("searching table for:%d:%s|",TABLE::matchno,(char *)name.c_str());
588
                tbl->Find(name,bt_long,typearray,true);
589
                //              gsearch2(name,bt_long,typearray,true);
590
                sp = Function::FindExactMatch(TABLE::matchno, name, bt_long, typearray)->sym;
591
                //              if (sp==nullptr) {
592
                //                      printf("notfound\r\n");
593
                //                      sp = gsearch2(name,bt_long,typearray,false);
594
                //              }
595
        }
596
        else {
597
        dfs.printf("A:%d:%s",TABLE::matchno,(char *)name.c_str());
598
        fn = Function::FindExactMatch(TABLE::matchno, name, bt_long, typearray);
599
        // If we didn't have an exact match and no (parameter) types are known
600
        // return the match if there is only a single one.
601
        if (fn==nullptr) {
602
                if (TABLE::matchno==1 && typearray==nullptr)
603
                        sp = TABLE::match[0];
604
        }
605
        else
606
                sp = fn->sym;
607
        //              memset(typearray,0,sizeof(typearray));
608
        //              sp = gsearch2(name,typearray);
609
        }
610
        if (sp==nullptr && !alloc) {
611
                dfs.printf("returning nullptr");
612
                *node = makeinode(en_labcon,9999);
613
                tp = nullptr;
614
                goto xit;
615
        }
616
        if( sp == NULL ) {
617
                while( my_isspace(lastch) )
618
                        getch();
619
                if( lastch == '(') {
620
                        sp = allocSYM();
621
                        sp->fi = allocFunction(sp->id);
622
                        sp->fi->sym = sp;
623
                        sp->tp = &stdfunc;
624
                        sp->SetName(*(new std::string(lastid)));
625
                        sp->storage_class = sc_external;
626
                        sp->IsUndefined = TRUE;
627
                        dfs.printf("Insert at nameref\r\n");
628
                        typearray->Print();
629
                        //    gsyms[0].insert(sp);
630
                        tp = &stdfunc;
631
                        *node = makesnode(en_cnacon,sp->name, sp->BuildSignature(1),sp->value.i);
632
                        (*node)->constflag = TRUE;
633
                        (*node)->sym = sp;
634
                        if (sp->tp->isUnsigned)
635
                                (*node)->isUnsigned = TRUE;
636
                        (*node)->esize = 8;
637
                        (*node)->isPascal = sp->fi->IsPascal;
638
                }
639
                else {
640
                        dfs.printf("Undefined symbol2 in nameref\r\n");
641
                        tp = (TYP *)NULL;
642
                        *node = makeinode(en_labcon,9999);
643
                        error(ERR_UNDEFINED);
644
                }
645
        }
646
        else {
647
                dfs.printf("sp is not null\n");
648
                typearray->Print();
649
                if( (tp = sp->tp) == NULL ) {
650
                        error(ERR_UNDEFINED);
651
                        goto xit;            // guard against untyped entries
652
                }
653
                switch( sp->storage_class ) {
654
                case sc_static:
655
                        if (sp->tp->type==bt_func || sp->tp->type==bt_ifunc) {
656
                                //strcpy(stnm,GetNamespace());
657
                                //strcat(stnm,"_");
658
                                stnm = "";
659
                                stnm += *sp->name;
660
                                *node = makesnode(en_cnacon,&stnm, sp->fi->BuildSignature(),sp->value.i);
661
                                (*node)->isPascal = sp->fi->IsPascal;
662
                                (*node)->constflag = TRUE;
663
                                (*node)->esize = 8;
664
                                //*node = makesnode(en_nacon,sp->name);
665
                                //(*node)->constflag = TRUE;
666
                        }
667
                        else {
668
                                *node = makeinode(en_labcon,sp->value.i);
669
                                (*node)->constflag = TRUE;
670
                                (*node)->esize = sp->tp->size;//8;
671
                                (*node)->segment = dataseg;
672
                        }
673
                        if (sp->tp->isUnsigned) {
674
                                (*node)->isUnsigned = TRUE;
675
                                (*node)->esize = sp->tp->size;
676
                        }
677
                        (*node)->etype = bt_pointer;//sp->tp->type;
678
                        break;
679
 
680
                case sc_thread:
681
                        *node = makeinode(en_labcon,sp->value.i);
682
                        (*node)->segment = tlsseg;
683
                        (*node)->constflag = TRUE;
684
                        (*node)->esize = sp->tp->size;
685
                        (*node)->etype = bt_pointer;//sp->tp->type;
686
                        if (sp->tp->isUnsigned)
687
                                (*node)->isUnsigned = TRUE;
688
                        break;
689
 
690
                case sc_global:
691
                case sc_external:
692
                        if (sp->tp->type == bt_func || sp->tp->type == bt_ifunc) {
693
                                *node = makesnode(en_cnacon, sp->name, sp->mangledName, sp->value.i);
694
                                (*node)->isPascal = sp->fi->IsPascal;
695
                        }
696
                        else
697
                                *node = makesnode(en_nacon,sp->name,sp->mangledName,sp->value.i);
698
                        (*node)->constflag = TRUE;
699
                        (*node)->esize = sp->tp->size;
700
                        (*node)->etype = bt_pointer;//sp->tp->type;
701
                        (*node)->isUnsigned = TRUE;// sp->tp->isUnsigned;
702
                        break;
703
 
704
                case sc_const:
705
                        if (sp->tp->type==bt_quad)
706
                                *node = makefqnode(en_fqcon,&sp->f128);
707
                        else if (sp->tp->type==bt_float || sp->tp->type==bt_double || sp->tp->type==bt_triple)
708
                                *node = makefnode(en_fcon,sp->value.f);
709
                        else {
710
                                *node = makeinode(en_icon,sp->value.i);
711
                                if (sp->tp->isUnsigned)
712
                                (*node)->isUnsigned = TRUE;
713
                        }
714
                        (*node)->constflag = TRUE;
715
                        (*node)->esize = sp->tp->size;
716
                        break;
717
 
718
                default:        /* auto and any errors */
719
                        if (sp->storage_class == sc_member) {   // will get this for a class member
720
                                // If it's a member we need to pass r25 the class pointer on
721
                                // the stack.
722
                                isMember = true;
723
                                if ((sp->tp->type==bt_func || sp->tp->type==bt_ifunc)
724
                                ||(sp->tp->type==bt_pointer && (sp->tp->GetBtp()->type == bt_func ||sp->tp->GetBtp()->type == bt_ifunc)))
725
                                {
726
                                        *node = makesnode(en_cnacon,sp->name, sp->fi->BuildSignature(),25);
727
                                        (*node)->isPascal = sp->fi->IsPascal;
728
                                }
729
                                else {
730
                                        *node = makeinode(en_classcon,sp->value.i);
731
                                }
732
                                if (sp->tp->isUnsigned || sp->tp->type==bt_pointer)
733
                                        (*node)->isUnsigned = TRUE;
734
                        }
735
                        else {
736
                                if( sp->storage_class != sc_auto) {
737
                                        error(ERR_ILLCLASS);
738
                                }
739
                                //sc_member
740
                                if (sp->tp->IsVectorType())
741
                                        *node = makeinode(en_autovcon,sp->value.i);
742
                                else if (sp->tp->type==bt_vector_mask)
743
                                        *node = makeinode(en_autovmcon,sp->value.i);
744
                                else if (sp->tp->IsFloatType())
745
                                        *node = makeinode(en_autofcon,sp->value.i);
746
                                else {
747
                                        *node = makeinode(en_autocon,sp->value.i);
748
                                        if (sp->tp->isUnsigned)
749
                                                (*node)->isUnsigned = TRUE;
750
                                }
751
                                if (sp->IsRegister) {
752
                                        if (sp->tp->IsFloatType())
753
                                                (*node)->nodetype = en_fpregvar;
754
                                        else
755
                                                (*node)->nodetype = en_regvar;
756
                                        (*node)->i = sp->reg;
757
                                        (*node)->tp = sp->tp;
758
                                        //(*node)->tp->val_flag = TRUE;
759
                                }
760
                        }
761
                        (*node)->esize = sp->tp->size;
762
                        switch((*node)->nodetype) {
763
                        case en_regvar:         (*node)->etype = bt_long;       break;//sp->tp->type;
764
                        case en_fpregvar:       (*node)->etype = sp->tp->type;  break;//sp->tp->type;
765
                        default:                        (*node)->etype = bt_pointer;break;//sp->tp->type;
766
                        }
767
                        //(*node)->etype = ((*node)->nodetype == en_regvar) ? bt_long : bt_pointer;//sp->tp->type;
768
                        break;
769
                }
770
                (*node)->SetType(sp->tp);
771
                (*node)->sym = sp;
772
                dfs.printf("tp:%p ",(char *)tp);
773
                // Not sure about this if - wasn't here in the past.
774
//              if (sp->tp->type!=bt_func && sp->tp->type!=bt_ifunc)
775
                tp = CondDeref(node,tp);
776
                dfs.printf("deref tp:%p ",(char *)tp);
777
        }
778
        if (nt)
779
                NextToken();
780
xit:
781
        if (!tp)
782
                dfs.printf("returning nullptr2");
783
        dfs.puts("</nameref2>\n");
784
        return tp;
785
}
786
 
787
TYP *nameref(ENODE **node,int nt)
788
{
789
        TYP *tp;
790
  bool found;
791
 
792
        dfs.puts("<Nameref>\n");
793
        dfs.printf("GSearchfor:%s|",lastid);
794
        found = false;
795
/*
796
        if (ptp1) {
797
          if (ptp1->type == bt_pointer) {
798
      found = ptp1->GetBtp()->lst.FindRising(lastid) > 0;
799
          }
800
          else {
801
      found = ptp1->lst.FindRising(lastid) > 0;
802
    }
803
  }
804
  if (!found)
805
 */
806
  gsearch2(lastid,(__int16)bt_long,nullptr,false);
807
        tp = nameref2(lastid, node, nt, true, nullptr, nullptr);
808
        dfs.puts("</Nameref>\n");
809
        return tp;
810
}
811
/*
812
      // Look for a function
813
                gsearch2(lastid,(__int16)bt_long,nullptr,false);
814
                        while( my_isspace(lastch) )
815
                                getch();
816
                        if(lastch == '(') {
817
                                NextToken();
818
        tptr = nameref(&pnode,TRUE);
819
                                tptr = ExprFunction(nullptr, &pnode);
820
                        }
821
*/
822
//
823
// ArgumentList will build a list of parameter expressions in
824
// a function call and return a pointer to the last expression
825
// parsed. since parameters are generally pushed from right
826
// to left we get just what we asked for...
827
//
828
ENODE *ArgumentList(ENODE *hidden, TypeArray *typearray)
829
{
830
        ENODE *ep1, *ep2;
831
        TYP *typ;
832
        int nn;
833
 
834
        dfs.printf("<ArgumentList>");
835
        nn = 0;
836
        ep1 = 0;
837
        if (hidden) {
838
                ep1 = makenode(en_void,hidden,ep1);
839
        }
840
        typearray->Clear();
841
        while( lastst != closepa)
842
        {
843
                typ = NonCommaExpression(&ep2);          // evaluate a parameter
844
                if (typ)
845
                        dfs.printf("%03d ", typ->typeno);
846
                else
847
                        dfs.printf("%03d ", 0);
848
                if (ep2==nullptr)
849
                        ep2 = makeinode(en_icon, 0);
850
                if (typ==nullptr) {
851
                        error(ERR_BADARG);
852
                        typearray->Add((int)bt_long,0);
853
                }
854
                else {
855
                        // If a function pointer is passed, we want a pointer type
856
                        if (typ->typeno==bt_func || typ->typeno == bt_ifunc)
857
                                typearray->Add((int)bt_pointer,0);
858
                        else
859
                                typearray->Add(typ,0);
860
                }
861
                ep1 = makenode(en_void,ep2,ep1);
862
                if(lastst != comma) {
863
                        dfs.printf("lastst=%d", lastst);
864
                        break;
865
                }
866
                NextToken();
867
        }
868
        NextToken();
869
        dfs.printf("</ArgumentList>\n");
870
        return ep1;
871
}
872
 
873
/*
874
 *      return 1 if st in set of [ kw_char, kw_short, kw_long, kw_int,
875
 *      kw_float, kw_double, kw_struct, kw_union ]
876
 */
877
static int IsIntrinsicType(int st)
878
{
879
        return  st == kw_byte || st==kw_char || st == kw_short || st == kw_int || st==kw_void ||
880
                                st == kw_int16 || st == kw_int8 || st == kw_int32 || st == kw_int16 ||
881
                st == kw_long || st == kw_float || st == kw_double || st == kw_triple ||
882
                st == kw_enum || st == kw_struct || st == kw_union ||
883
                st== kw_unsigned || st==kw_signed || st==kw_exception ||
884
                                st == kw_const;
885
}
886
 
887
int IsBeginningOfTypecast(int st)
888
{
889
        SYM *sp;
890
        if (st==id) {
891
                sp = tagtable.Find(lastid,false);
892
                if (sp == nullptr)
893
                        sp = gsyms[0].Find(lastid,false);
894
                if (sp)
895
                        return sp->storage_class==sc_typedef;
896
                return FALSE;
897
        }
898
        else
899
                return IsIntrinsicType(st) || st==kw_volatile;
900
}
901
 
902
SYM *makeint2(std::string name)
903
{
904
        SYM *sp;
905
        TYP *tp;
906
        sp = allocSYM();
907
        tp = TYP::Make(bt_long,2);
908
        tp->sname = new std::string("");
909
        sp->SetName(name);
910
        sp->storage_class = sc_auto;
911
        sp->SetType(tp);
912
        return sp;
913
}
914
 
915
 
916
SYM *makeStructPtr(std::string name)
917
{
918
        SYM *sp;
919
        TYP *tp,*tp2;
920
        sp = allocSYM();
921
        tp = TYP::Make(bt_pointer,sizeOfPtr);
922
        tp2 = TYP::Make(bt_struct,sizeOfPtr);
923
        tp->btp = tp2->GetIndex();
924
        tp->sname = new std::string("");
925
        tp->isUnsigned = TRUE;
926
        sp->SetName(name);
927
        sp->storage_class = sc_auto;
928
        sp->SetType(tp);
929
        return sp;
930
}
931
 
932
 
933
// This function is dead code.
934
// Create a list of dummy parameters based on argument types.
935
// This is needed in order to add a function to the tables if
936
// the function hasn't been encountered before.
937
 
938
SYM *CreateDummyParameters(ENODE *ep, SYM *parent, TYP *tp)
939
{
940
        int poffset;
941
        SYM *sp1;
942
        SYM *list;
943
        int nn;
944
        ENODE *p;
945
        static char buf[20];
946
 
947
        list = nullptr;
948
        poffset = Compiler::GetReturnBlockSize();
949
 
950
        // Process hidden parameter
951
        if (tp) {
952
                if (tp->GetBtp()) {
953
                        if (tp->GetBtp()->type==bt_struct || tp->GetBtp()->type==bt_union || tp->GetBtp()->type==bt_class ) {
954
                                sp1 = makeint2(std::string(my_strdup("_pHiddenStructPtr")));
955
                                sp1->parent = parent->GetIndex();
956
                                sp1->value.i = poffset;
957
                                poffset += sizeOfWord;
958
                                sp1->storage_class = sc_auto;
959
                                sp1->next = 0;
960
                                list = sp1;
961
                        }
962
                }
963
        }
964
        nn = 0;
965
        for(p = ep; p; p = p->p[1]) {
966
                sprintf_s(buf,sizeof(buf),"_p%d", nn);
967
                sp1 = makeint2(std::string(my_strdup(buf)));
968
                if (p->p[0]==nullptr)
969
                        sp1->tp =(TYP *) TYP::Make(bt_long,2);
970
                else
971
                        sp1->SetType(p->p[0]->tp);
972
                sp1->parent = parent->GetIndex();
973
                sp1->value.i = poffset;
974
                // Check for aggregate types passed as parameters. Structs
975
                // and unions use the type size. There could also be arrays
976
                // passed.
977
                poffset += round8(sp1->tp->size);
978
//              if (round8(sp1->tp->size) > 8)
979
                sp1->storage_class = sc_auto;
980
                sp1->next = 0;
981
 
982
                // record parameter list
983
                if (list == nullptr) {
984
                        list = sp1;
985
                }
986
                else {
987
                        sp1->SetNext(list->GetIndex());
988
                        list = sp1;
989
                }
990
                nn++;
991
        }
992
        return list;
993
}
994
 
995
 
996
// ----------------------------------------------------------------------------
997
//      primary will parse a primary expression and set the node pointer
998
//      returning the type of the expression parsed. primary expressions
999
//      are any of:
1000
//                      id
1001
//                      constant
1002
//                      string
1003
//                      ( expression )
1004
//                      this
1005
// ----------------------------------------------------------------------------
1006
TYP *ParsePrimaryExpression(ENODE **node, int got_pa)
1007
{
1008
        ENODE *pnode, *qnode1, *qnode2;
1009
    TYP *tptr;
1010
        TypeArray typearray;
1011
 
1012
        qnode1 = (ENODE *)NULL;
1013
        qnode2 = (ENODE *)NULL;
1014
        pnode = (ENODE *)NULL;
1015
    *node = (ENODE *)NULL;
1016
    Enter("ParsePrimary ");
1017
    if (got_pa) {
1018
        tptr = expression(&pnode);
1019
        needpunc(closepa,7);
1020
        *node = pnode;
1021
        if (pnode==NULL)
1022
           dfs.printf("pnode is NULL\r\n");
1023
        else
1024
           (*node)->SetType(tptr);
1025
        if (tptr)
1026
        Leave("ParsePrimary", tptr->type);
1027
        else
1028
        Leave("ParsePrimary", 0);
1029
        return tptr;
1030
    }
1031
    switch( lastst ) {
1032
        case ellipsis:
1033
    case id:
1034
        tptr = nameref(&pnode,TRUE);
1035
/*
1036
                // Try and find the symbol, if not found, assume a function
1037
                // but only if it's followed by a (
1038
                if (TABLE::matchno==0) {
1039
                        while( my_isspace(lastch) )
1040
                                getch();
1041
                        if( lastch == '(') {
1042
                                NextToken();
1043
                                tptr = ExprFunction(nullptr, &pnode);
1044
                        }
1045
                        else {
1046
                                tptr = nameref(&pnode,TRUE);
1047
                        }
1048
                }
1049
                else
1050
*/
1051
                /*
1052
                if (tptr==NULL) {
1053
                        tptr = allocTYP();
1054
                        tptr->type = bt_long;
1055
                        tptr->typeno = bt_long;
1056
                        tptr->alignment = 8;
1057
                        tptr->bit_offset = 0;
1058
                        tptr->GetBtp() = nullptr;
1059
                        tptr->isArray = false;
1060
                        tptr->isConst = false;
1061
                        tptr->isIO = false;
1062
                        tptr->isShort = false;
1063
                        tptr->isUnsigned = false;
1064
                        tptr->size = 8;
1065
                        tptr->sname = my_strdup(lastid);
1066
                }
1067
                */
1068
        break;
1069
    case cconst:
1070
        tptr = &stdchar;
1071
        tptr->isConst = TRUE;
1072
        pnode = makeinode(en_icon,ival);
1073
        pnode->constflag = TRUE;
1074
                pnode->esize = 1;
1075
        pnode->SetType(tptr);
1076
        NextToken();
1077
        break;
1078
    case iconst:
1079
        tptr = &stdint;
1080
        tptr->isConst = TRUE;
1081
        pnode = makeinode(en_icon,ival);
1082
        pnode->constflag = TRUE;
1083
                if (ival >= -128 && ival < 128)
1084
                        pnode->esize = 1;
1085
                else if (ival >= -32768 && ival < 32768)
1086
                        pnode->esize = 2;
1087
                else if (ival >= -2147483648LL && ival < 2147483648LL)
1088
                        pnode->esize = 4;
1089
                else
1090
                        pnode->esize = 2;
1091
        pnode->SetType(tptr);
1092
        NextToken();
1093
        break;
1094
 
1095
        case kw_floatmax:
1096
        tptr = &stdquad;
1097
        tptr->isConst = TRUE;
1098
        pnode = makefnode(en_fcon,rval);
1099
        pnode->constflag = TRUE;
1100
        pnode->SetType(tptr);
1101
                pnode->i = quadlit(Float128::FloatMax());
1102
        NextToken();
1103
                break;
1104
 
1105
    case rconst:
1106
        pnode = makefnode(en_fcon,rval);
1107
        pnode->constflag = TRUE;
1108
                pnode->i = quadlit(&rval128);
1109
                pnode->f128 = rval128;
1110
                switch(float_precision) {
1111
                case 'Q': case 'q':
1112
                        tptr = &stdquad;
1113
                        //getch();
1114
                        break;
1115
                case 'D': case 'd':
1116
                        tptr = &stddouble;
1117
                        //getch();
1118
                        break;
1119
                case 'T': case 't':
1120
                        tptr = &stdtriple;
1121
                        //getch();
1122
                        break;
1123
                case 'S': case 's':
1124
                        tptr = &stdflt;
1125
                        //getch();
1126
                        break;
1127
                default:
1128
                        tptr = &stddouble;
1129
                        break;
1130
                }
1131
        pnode->SetType(tptr);
1132
        tptr->isConst = TRUE;
1133
        NextToken();
1134
        break;
1135
 
1136
        case sconst:
1137
                if (sizeof_flag) {
1138
                        tptr = (TYP *)TYP::Make(bt_pointer, 0);
1139
                        tptr->size = strlen(laststr) + 1;
1140
                        tptr->btp = stdchar.GetIndex();
1141
            tptr->GetBtp()->isConst = TRUE;
1142
                        tptr->val_flag = 1;
1143
                        tptr->isConst = TRUE;
1144
                        tptr->isUnsigned = TRUE;
1145
                }
1146
                else {
1147
            tptr = &stdstring;
1148
                }
1149
        pnode = makenodei(en_labcon,(ENODE *)NULL,0);
1150
                if (sizeof_flag == 0)
1151
                        pnode->i = stringlit(laststr);
1152
                pnode->etype = bt_pointer;
1153
                pnode->esize = 2;
1154
        pnode->constflag = TRUE;
1155
                pnode->segment = rodataseg;
1156
        pnode->SetType(tptr);
1157
        tptr->isConst = TRUE;
1158
        NextToken();
1159
        break;
1160
 
1161
    case openpa:
1162
        NextToken();
1163
 
1164
//        if( !IsBeginningOfTypecast(lastst) ) {
1165
//              expr_flag = 0;
1166
        tptr = expression(&pnode);
1167
        pnode->SetType(tptr);
1168
        needpunc(closepa,8);
1169
//        }
1170
        //else {                        /* cast operator */
1171
        //    ParseSpecifier(0); /* do cast ParseSpecifieraration */
1172
        //    ParseDeclarationPrefix(FALSE);
1173
        //    tptr = head;
1174
        //    needpunc(closepa);
1175
        //    if( ParseUnaryExpression(&pnode) == NULL ) {
1176
        //        error(ERR_IDEXPECT);
1177
        //        tptr = NULL;
1178
        //    }
1179
        //}
1180
        break;
1181
 
1182
    case kw_this:
1183
                dfs.puts("<ExprThis>");
1184
                TYP *tptr2;
1185
 
1186
                tptr2 = TYP::Make(bt_class,0);
1187
                if (currentClass==nullptr) {
1188
                        error(ERR_THIS);
1189
                }
1190
                else {
1191
                        memcpy(tptr2,currentClass->tp,sizeof(TYP));
1192
                }
1193
                NextToken();
1194
                tptr = TYP::Make(bt_pointer,sizeOfPtr);
1195
                tptr->btp = tptr2->GetIndex();
1196
                tptr->isUnsigned = TRUE;
1197
                dfs.puts((char *)tptr->GetBtp()->sname->c_str());
1198
                pnode = makeinode(en_regvar,regCLP);
1199
                dfs.puts("</ExprThis>");
1200
        break;
1201
 
1202
        case begin:
1203
                {
1204
                        int sz = 0;
1205
                        ENODE *list;
1206
 
1207
                        NextToken();
1208
                        head = tail = nullptr;
1209
                        list = makenode(en_list,nullptr,nullptr);
1210
                        while (lastst != end) {
1211
                                tptr = NonCommaExpression(&pnode);
1212
                                pnode->SetType(tptr);
1213
                                sz = sz + tptr->size;
1214
                                AddToList(list, pnode);
1215
                                if (lastst!=comma)
1216
                                        break;
1217
                                NextToken();
1218
                        }
1219
                        needpunc(end,9);
1220
                        pnode = makenode(en_aggregate,list,nullptr);
1221
                        pnode->SetType(tptr = TYP::Make(bt_struct,sz));
1222
                }
1223
                break;
1224
 
1225
    default:
1226
        Leave("ParsePrimary", 0);
1227
        return (TYP *)NULL;
1228
    }
1229
        *node = pnode;
1230
    if (*node)
1231
       (*node)->SetType(tptr);
1232
    if (tptr)
1233
    Leave("ParsePrimary", tptr->type);
1234
    else
1235
    Leave("ParsePrimary", 0);
1236
    return tptr;
1237
}
1238
 
1239
/*
1240
 *      this function returns true if the node passed is an IsLValue.
1241
 *      this can be qualified by the fact that an IsLValue must have
1242
 *      one of the dereference operators as it's top node.
1243
 */
1244
int IsLValue(ENODE *node)
1245
{
1246
        if (node==nullptr)
1247
                return FALSE;
1248
        switch( node->nodetype ) {
1249
    case en_b_ref:
1250
        case en_c_ref:
1251
        case en_h_ref:
1252
    case en_w_ref:
1253
        case en_ub_ref:
1254
        case en_uc_ref:
1255
        case en_uh_ref:
1256
    case en_uw_ref:
1257
        case en_hp_ref:
1258
        case en_wp_ref:
1259
        case en_wfieldref:
1260
        case en_uwfieldref:
1261
        case en_bfieldref:
1262
        case en_ubfieldref:
1263
        case en_cfieldref:
1264
        case en_ucfieldref:
1265
        case en_hfieldref:
1266
        case en_uhfieldref:
1267
    case en_triple_ref:
1268
        case en_dbl_ref:
1269
        case en_quad_ref:
1270
        case en_flt_ref:
1271
        case en_ref32:
1272
        case en_ref32u:
1273
        case en_vector_ref:
1274
            return TRUE;
1275
        case en_cbc:
1276
        case en_cbh:
1277
    case en_cbw:
1278
        case en_cch:
1279
        case en_ccw:
1280
        case en_chw:
1281
        case en_cfd:
1282
        case en_cubw:
1283
        case en_cucw:
1284
        case en_cuhw:
1285
        case en_cbu:
1286
        case en_ccu:
1287
        case en_chu:
1288
        case en_cubu:
1289
        case en_cucu:
1290
        case en_cuhu:
1291
        case en_ccwp:
1292
        case en_cucwp:
1293
            return IsLValue(node->p[0]);
1294
        // For an array reference there will be an add node at the top of the
1295
        // expression tree. This evaluates to an address which is essentially
1296
        // the same as an *_ref node. It's an LValue.
1297
        case en_add:
1298
                if (node->tp)
1299
                        return node->tp->type==bt_pointer && node->tp->isArray;
1300
                else
1301
                        return FALSE;
1302
        // A typecast will connect the types with a void node
1303
        case en_void:
1304
                return (node->etype == bt_pointer);
1305
    }
1306
    return FALSE;
1307
}
1308
 
1309
// ----------------------------------------------------------------------------
1310
// ----------------------------------------------------------------------------
1311
TYP *Autoincdec(TYP *tp, ENODE **node, int flag)
1312
{
1313
        ENODE *ep1, *ep2;
1314
        TYP *typ;
1315
        int su;
1316
 
1317
        ep1 = *node;
1318
        if( IsLValue(ep1) ) {
1319
                if (tp->type == bt_pointer) {
1320
                        typ = tp->GetBtp();
1321
                        ep2 = makeinode(en_icon,typ->size);
1322
                        ep2->esize = typ->size;
1323
                }
1324
                else {
1325
                        ep2 = makeinode(en_icon,1);
1326
                        ep2->esize = 1;
1327
                }
1328
                ep2->constflag = TRUE;
1329
                ep2->isUnsigned = tp->isUnsigned;
1330
                su = ep1->isUnsigned;
1331
                ep1 = makenode(flag ? en_assub : en_asadd,ep1,ep2);
1332
                ep1->isUnsigned = tp->isUnsigned;
1333
                ep1->esize = tp->size;
1334
        }
1335
    else
1336
        error(ERR_LVALUE);
1337
        *node = ep1;
1338
        if (*node)
1339
        (*node)->SetType(tp);
1340
        return tp;
1341
}
1342
 
1343
 
1344
void ApplyVMask(ENODE *node, ENODE *mask)
1345
{
1346
        if (node==nullptr || mask==nullptr)
1347
                return;
1348
        if (node->p[0])
1349
                ApplyVMask(node->p[0],mask);
1350
        if (node->p[1])
1351
                ApplyVMask(node->p[1],mask);
1352
        if (node->p[2])
1353
                ApplyVMask(node->p[2],mask);
1354
        if (node->vmask==nullptr)
1355
                node->vmask = mask;
1356
        return;
1357
}
1358
 
1359
// ----------------------------------------------------------------------------
1360
// A Postfix Expression is:
1361
//              primary
1362
//              postfix_expression[expression]
1363
//              postfix_expression()
1364
//              postfix_expression(argument expression list)
1365
//              postfix_expression.ID
1366
//              postfix_expression->ID
1367
//              postfix_expression++
1368
//              postfix_expression--
1369
// ----------------------------------------------------------------------------
1370
 
1371
TYP *ParsePostfixExpression(ENODE **node, int got_pa)
1372
{
1373
        TYP *tp1, *tp2, *tp3, *tp4;
1374
        ENODE *ep1, *ep2, *ep3, *ep4;
1375
        ENODE *rnode, *qnode, *pnode;
1376
        SYM *sp;
1377
        int iu;
1378
        int ii;
1379
        bool classdet = false;
1380
        TypeArray typearray;
1381
        std::string name;
1382
        int cf, uf, numdimen;
1383
        int sz1, cnt, cnt2, totsz;
1384
        int sa[20];
1385
        bool firstBr = true;
1386
 
1387
    ep1 = (ENODE *)NULL;
1388
    Enter("<ParsePostfix>");
1389
    *node = (ENODE *)NULL;
1390
        tp1 = ParsePrimaryExpression(&ep1, got_pa);
1391
        if (ep1==NULL) {
1392
//              ep1 = makeinode(en_icon, 0);
1393
//              goto j1;
1394
//         printf("DIAG: ParsePostFix: ep1 is NULL\r\n");
1395
        }
1396
        if (tp1 == NULL) {
1397
        Leave("</ParsePostfix>",0);
1398
                return ((TYP *)NULL);
1399
    }
1400
        pep1 = nullptr;
1401
        cnt = 0;
1402
        while (1) {
1403
                pop = lastst;
1404
                switch(lastst) {
1405
                case openbr:
1406
                        pnode = ep1;
1407
                        if (tp1==NULL) {
1408
                                error(ERR_UNDEFINED);
1409
                                goto j1;
1410
                        }
1411
                        NextToken();
1412
                        if( tp1->type == bt_pointer ) {
1413
                                tp2 = expression(&rnode);
1414
                                tp3 = tp1;
1415
                                tp4 = tp1;
1416
                                if (rnode==nullptr) {
1417
                                        error(ERR_EXPREXPECT);
1418
                                        throw new C64PException(ERR_EXPREXPECT,9);
1419
                                }
1420
                        }
1421
                        else {
1422
                                tp2 = tp1;
1423
                                rnode = pnode;
1424
                                tp3 = expression(&pnode);
1425
                                if (tp3==NULL) {
1426
                                        error(ERR_UNDEFINED);
1427
                                        throw new C64PException(ERR_UNDEFINED,10);
1428
                                        goto j1;
1429
                                }
1430
                                tp1 = tp3;
1431
                                tp4 = tp1;
1432
                        }
1433
                        if (cnt==0) {
1434
                                numdimen = tp1->dimen;
1435
                                cnt2 = 1;
1436
                                for (; tp4; tp4 = tp4->GetBtp()) {
1437
                                        sa[cnt2] = max(tp4->numele,1);
1438
                                        cnt2++;
1439
                                        if (cnt2 > 19) {
1440
                                                error(ERR_TOOMANYDIMEN);
1441
                                                break;
1442
                                        }
1443
                                }
1444
                                if (tp1->type==bt_pointer)
1445
                                        sa[numdimen+1] = tp1->GetBtp()->size;
1446
                                else
1447
                                        sa[numdimen+1] = tp1->size;
1448
                        }
1449
                        if (cnt==0)
1450
                                totsz = tp1->size;
1451
                        firstBr = false;
1452
                        if (tp1->type != bt_pointer)
1453
                                error(ERR_NOPOINTER);
1454
                        else
1455
                                tp1 = tp1->GetBtp();
1456
                        //if (cnt==0) {
1457
                        //      switch(numdimen) {
1458
                        //      case 1: sz1 = sa[numdimen+1]; break;
1459
                        //      case 2: sz1 = sa[1]*sa[numdimen+1]; break;
1460
                        //      case 3: sz1 = sa[1]*sa[2]*sa[numdimen+1]; break;
1461
                        //      default:
1462
                        //              sz1 = sa[numdimen+1];   // could be a void = 0
1463
                        //              for (cnt2 = 1; cnt2 < numdimen; cnt2++)
1464
                        //                      sz1 = sz1 * sa[cnt2];
1465
                        //      }
1466
                        //}
1467
                        //else if (cnt==1) {
1468
                        //      switch(numdimen) {
1469
                        //      case 2: sz1 = sa[numdimen+1]; break;
1470
                        //      case 3: sz1 = sa[1]*sa[numdimen+1]; break;
1471
                        //      default:
1472
                        //              sz1 = sa[numdimen+1];   // could be a void = 0
1473
                        //              for (cnt2 = 1; cnt2 < numdimen-1; cnt2++)
1474
                        //                      sz1 = sz1 * sa[cnt2];
1475
                        //      }
1476
                        //}
1477
                        //else if (cnt==2) {
1478
                        //      switch(numdimen) {
1479
                        //      case 3: sz1 = sa[numdimen+1]; break;
1480
                        //      default:
1481
                        //              sz1 = sa[numdimen+1];   // could be a void = 0
1482
                        //              for (cnt2 = 1; cnt2 < numdimen-2; cnt2++)
1483
                        //                      sz1 = sz1 * sa[cnt2];
1484
                        //      }
1485
                        //}
1486
                        //else
1487
                        {
1488
                                sz1 = sa[numdimen+1];   // could be a void = 0
1489
                                for (cnt2 = 1; cnt2 < numdimen-cnt; cnt2++)
1490
                                        sz1 = sz1 * sa[cnt2];
1491
                        }
1492
                        qnode = makeinode(en_icon,sz1);
1493
                        qnode->etype = bt_ushort;
1494
                        qnode->esize = 8;
1495
                        qnode->constflag = TRUE;
1496
                        qnode->isUnsigned = TRUE;
1497
                        cf = qnode->constflag;
1498
 
1499
                        qnode = makenode(en_mulu, qnode, rnode);
1500
                        qnode->etype = bt_short;
1501
                        qnode->esize = 8;
1502
                        qnode->constflag = cf & rnode->constflag;
1503
                        qnode->isUnsigned = rnode->isUnsigned;
1504
 
1505
                        //(void) cast_op(&qnode, &tp_int32, tp1);
1506
                        cf = pnode->constflag;
1507
                        uf = pnode->isUnsigned;
1508
                        pnode = makenode(en_add, qnode, pnode);
1509
                        pnode->etype = bt_pointer;
1510
                        pnode->esize = sizeOfPtr;
1511
                        pnode->constflag = cf & qnode->constflag;
1512
                        pnode->isUnsigned = uf & qnode->isUnsigned;
1513
 
1514
                        tp1 = CondDeref(&pnode, tp1);
1515
                        pnode->tp = tp1;
1516
                        ep1 = pnode;
1517
                        needpunc(closebr,9);
1518
                        cnt++;
1519
                        break;
1520
 
1521
                case openpa:
1522
                        cnt = 0;
1523
                        if (tp1==NULL) {
1524
                                error(ERR_UNDEFINED);
1525
                                goto j1;
1526
                        }
1527
                        tp2 = ep1->tp;
1528
                        if (tp2==nullptr) {
1529
                                error(ERR_UNDEFINED);
1530
                                goto j1;
1531
                        }
1532
                        if (tp2->type==bt_vector_mask) {
1533
                                NextToken();
1534
                                tp1 = expression(&ep2);
1535
                                needpunc(closepa,9);
1536
                                ApplyVMask(ep2,ep1);
1537
                                ep1 = ep2;
1538
                                break;
1539
                        }
1540
                        if (tp2->type == bt_pointer) {
1541
                                dfs.printf("Got function pointer.\n");
1542
                        }
1543
                        dfs.printf("tp2->type=%d",tp2->type);
1544
                        name = lastid;
1545
                        NextToken();
1546
                        tp3 = tp1->GetBtp();
1547
                        ep4 = nullptr;
1548
                        if (tp3) {
1549
                                if (tp3->type==bt_struct || tp3->type==bt_union || tp3->type==bt_class)
1550
                                        ep4 = makenode(en_regvar,NULL,NULL);
1551
                        }
1552
                        //ep2 = ArgumentList(ep1->p[2],&typearray);
1553
                        ep2 = ArgumentList(ep4,&typearray);
1554
                        typearray.Print();
1555
                        dfs.printf("Got Type: %d",tp1->type);
1556
                        if (tp1->type==bt_pointer) {
1557
                                dfs.printf("Got function pointer.\n");
1558
                                ep1 = makenode(en_fcall,ep1,ep2);
1559
                                currentFn->IsLeaf = FALSE;
1560
                                break;
1561
                        }
1562
                        dfs.printf("openpa calling gsearch2");
1563
                        sp = nullptr;
1564
                        ii = tp1->lst.FindRising(name);
1565
                        if (ii) {
1566
                                sp = Function::FindExactMatch(TABLE::matchno, name, bt_long, &typearray)->sym;
1567
                        }
1568
                        if (!sp)
1569
                                sp = gsearch2(name,bt_long,&typearray,true);
1570
                        if (sp==nullptr) {
1571
                                sp = allocSYM();
1572
                                sp->fi = allocFunction(sp->id);
1573
                                sp->fi->sym = sp;
1574
                                sp->storage_class = sc_external;
1575
                                sp->SetName(name);
1576
                                sp->tp = TYP::Make(bt_func,0);
1577
                                sp->tp->btp = TYP::Make(bt_long,sizeOfWord)->GetIndex();
1578
                                sp->fi->AddProto(&typearray);
1579
                                sp->mangledName = sp->fi->BuildSignature();
1580
                                gsyms[0].insert(sp);
1581
                        }
1582
                        else if (sp->IsUndefined) {
1583
                                sp->tp = TYP::Make(bt_func,0);
1584
                                sp->tp->btp = TYP::Make(bt_long,sizeOfWord)->GetIndex();
1585
                                if (!sp->fi) {
1586
                                        sp->fi = allocFunction(sp->id);
1587
                                        sp->fi->sym = sp;
1588
                                }
1589
                                sp->fi->AddProto(&typearray);
1590
                                sp->mangledName = sp->fi->BuildSignature();
1591
                                gsyms[0].insert(sp);
1592
                                sp->IsUndefined = false;
1593
                        }
1594
                        if (sp->tp->type==bt_pointer) {
1595
                                dfs.printf("Got function pointer");
1596
                                ep1 = makefcnode(en_fcall,ep1,ep2,sp);
1597
                                currentFn->IsLeaf = FALSE;
1598
                        }
1599
                        else {
1600
                                dfs.printf("Got direct function %s ", (char *)sp->name->c_str());
1601
                                ep3 = makesnode(en_cnacon,sp->name,sp->mangledName,sp->value.i);
1602
                                ep1 = makefcnode(en_fcall,ep3,ep2,sp);
1603
                                if (!sp->fi->IsInline)
1604
                                        currentFn->IsLeaf = FALSE;
1605
                        }
1606
                        tp1 = sp->tp->GetBtp();
1607
//                      tp1 = ExprFunction(tp1, &ep1);
1608
                        break;
1609
 
1610
                case pointsto:
1611
                        {
1612
                                //int reftype = sizeOfPtr==4 ? en_h_ref : en_w_ref;
1613
                                int reftype = sizeOfPtr==4 ? en_hp_ref : en_wp_ref;
1614
 
1615
                                cnt = 0;
1616
                                if (tp1==NULL) {
1617
                                        error(ERR_UNDEFINED);
1618
                                        goto j1;
1619
                                }
1620
                                if( tp1->type != bt_pointer ) {
1621
                                        error(ERR_NOPOINTER);
1622
                                }
1623
                                else {
1624
                                        tp1 = tp1->GetBtp();
1625
                                }
1626
                                if( tp1->val_flag == FALSE ) {
1627
                                        ep1 = makenode(reftype,ep1,(ENODE *)NULL);
1628
                                }
1629
                        }
1630
 
1631
                 // fall through to dot operation
1632
                case dot:
1633
                        cnt = 0;
1634
                        if (tp1==NULL) {
1635
                                error(ERR_UNDEFINED);
1636
                                goto j1;
1637
                        }
1638
                        NextToken();       /* past -> or . */
1639
                        if (tp1->IsVectorType()) {
1640
                                NonAssignExpression(&qnode);
1641
                                ep2 = makenode(en_shl,qnode,makeinode(en_icon,3));
1642
                                // The dot operation will deference the result below so the
1643
                                // old dereference operation isn't needed. It is stripped 
1644
                                // off by using ep->p[0] rather than ep.
1645
                                ep1 = makenode(en_add,ep1->p[0],ep2);
1646
                                tp1 = tp1->GetBtp();
1647
                                tp1 = CondDeref(&ep1,tp1);
1648
                                break;
1649
                        }
1650
                        if(lastst != id) {
1651
                                error(ERR_IDEXPECT);
1652
                                break;
1653
                        }
1654
                        dfs.printf("dot search: %p\r\n", (char *)&tp1->lst);
1655
                        ptp1 = tp1;
1656
                        pep1 = ep1;
1657
                        name = lastid;
1658
                        ii = tp1->lst.FindRising(name);
1659
                        if (ii==0) {
1660
                                dfs.printf("Nomember1");
1661
                                error(ERR_NOMEMBER);
1662
                                break;
1663
                        }
1664
                        sp = TABLE::match[ii-1];
1665
                        sp = sp->FindRisingMatch();
1666
                        if( sp == NULL ) {
1667
                                dfs.printf("Nomember2");
1668
                                error(ERR_NOMEMBER);
1669
                                break;
1670
                        }
1671
                        if (sp->IsPrivate && sp->parent != currentFn->sym->parent) {
1672
                                error(ERR_PRIVATE);
1673
                                break;
1674
                        }
1675
                        tp1 = sp->tp;
1676
                        dfs.printf("tp1->type:%d",tp1->type);
1677
                        if (tp1==nullptr)
1678
                                throw new C64PException(ERR_NULLPOINTER,5);
1679
                        if (tp1->type==bt_ifunc || tp1->type==bt_func) {
1680
                                // build the name vector and create a nacon node.
1681
                                dfs.printf("%s is a func\n",(char *)sp->name->c_str());
1682
                                NextToken();
1683
                                if (lastst==openpa) {
1684
                                        NextToken();
1685
                                        ep2 = ArgumentList(pep1,&typearray);
1686
                                        typearray.Print();
1687
                                        sp = Function::FindExactMatch(ii,name,bt_long,&typearray)->sym;
1688
                                        if (sp) {
1689
//                                              sp = TABLE::match[TABLE::matchno-1];
1690
                                                ep3 = makesnode(en_cnacon,sp->name,sp->mangledName,sp->value.i);
1691
                                                ep1 = makenode(en_fcall,ep3,ep2);
1692
                                                tp1 = sp->tp->GetBtp();
1693
                                                currentFn->IsLeaf = FALSE;
1694
                                        }
1695
                                        else {
1696
                                                error(ERR_METHOD_NOTFOUND);
1697
                                                goto j1;
1698
                                        }
1699
                                        ep1->SetType(tp1);
1700
                                        break;
1701
                                }
1702
                                // Else: we likely wanted the addres of the function since the
1703
        // function is referenced without o parameter list indicator. Goto
1704
        // the regular processing code.
1705
                                goto j2;
1706
                        }
1707
                        else {
1708
j2:
1709
                                dfs.printf("tp1->type:%d",tp1->type);
1710
                                qnode = makeinode(en_icon,sp->value.i);
1711
                                qnode->constflag = TRUE;
1712
                                iu = ep1->isUnsigned;
1713
                                ep1 = makenode(en_add,ep1,qnode);
1714
                                ep1->constflag = ep1->p[0]->constflag;
1715
                                ep1->isUnsigned = iu;
1716
                                ep1->esize = 2;
1717
                                ep1->p[2] = pep1;
1718
                                if (tp1->type==bt_pointer && (tp1->GetBtp()->type==bt_func || tp1->GetBtp()->type==bt_ifunc))
1719
                                        dfs.printf("Pointer to func");
1720
                                else
1721
                                        tp1 = CondDeref(&ep1,tp1);
1722
                                ep1->SetType(tp1);
1723
                                dfs.printf("tp1->type:%d",tp1->type);
1724
                        }
1725
                        if (tp1==nullptr)
1726
                                getchar();
1727
                        NextToken();       /* past id */
1728
                        dfs.printf("B");
1729
                        break;
1730
 
1731
                case autodec:
1732
                        cnt = 0;
1733
                        NextToken();
1734
                        Autoincdec(tp1,&ep1,1);
1735
                        break;
1736
                case autoinc:
1737
                        cnt = 0;
1738
                        NextToken();
1739
                        Autoincdec(tp1,&ep1,0);
1740
                        break;
1741
                default:        goto j1;
1742
                }
1743
        }
1744
j1:
1745
        *node = ep1;
1746
        if (ep1)
1747
        (*node)->SetType(tp1);
1748
        if (tp1)
1749
        Leave("</ParsePostfix>", tp1->type);
1750
        else
1751
        Leave("</ParsePostfix>", 0);
1752
        return tp1;
1753
}
1754
 
1755
/*
1756
 *      ParseUnaryExpression evaluates unary expressions and returns the type of the
1757
 *      expression evaluated. unary expressions are any of:
1758
 *
1759
 *                      postfix expression
1760
 *                      ++unary
1761
 *                      --unary
1762
 *                      !cast_expression
1763
 //                     not cast_expression
1764
 *                      ~cast_expression
1765
 *                      -cast_expression
1766
 *                      +cast_expression
1767
 *                      *cast_expression
1768
 *                      &cast_expression
1769
 *                      sizeof(typecast)
1770
 *                      sizeof unary
1771
 *                      typenum(typecast)
1772
 //                     new
1773
 *
1774
 */
1775
TYP *ParseUnaryExpression(ENODE **node, int got_pa)
1776
{
1777
        TYP *tp, *tp1;
1778
    ENODE *ep1, *ep2, *ep3;
1779
    int flag2;
1780
 
1781
        Enter("<ParseUnary>");
1782
    ep1 = NULL;
1783
    *node = (ENODE *)NULL;
1784
        flag2 = FALSE;
1785
        if (got_pa) {
1786
        tp = ParsePostfixExpression(&ep1, got_pa);
1787
                *node = ep1;
1788
        if (ep1)
1789
                (*node)->SetType(tp);
1790
        if (tp)
1791
        Leave("</ParseUnary>", tp->type);
1792
        else
1793
        Leave("</ParseUnary>", 0);
1794
                return tp;
1795
        }
1796
    switch( lastst ) {
1797
    case autodec:
1798
                NextToken();
1799
                tp = ParseUnaryExpression(&ep1, got_pa);
1800
                Autoincdec(tp,&ep1,1);
1801
                break;
1802
    case autoinc:
1803
                NextToken();
1804
                tp = ParseUnaryExpression(&ep1, got_pa);
1805
                Autoincdec(tp,&ep1,0);
1806
                break;
1807
        case plus:
1808
        NextToken();
1809
        tp = ParseCastExpression(&ep1);
1810
        if( tp == NULL ) {
1811
            error(ERR_IDEXPECT);
1812
            return (TYP *)NULL;
1813
        }
1814
        break;
1815
 
1816
        // Negative constants are trapped here and converted to proper form.
1817
    case minus:
1818
        NextToken();
1819
        tp = ParseCastExpression(&ep1);
1820
        if( tp == NULL ) {
1821
            error(ERR_IDEXPECT);
1822
            return (TYP *)NULL;
1823
        }
1824
                else if (ep1->constflag && (ep1->nodetype==en_icon)) {
1825
                        ep1->i = -ep1->i;
1826
                }
1827
                else if (ep1->constflag && (ep1->nodetype==en_fcon)) {
1828
                        ep1->f = -ep1->f;
1829
                        ep1->f128.sign = !ep1->f128.sign;
1830
                        // A new literal label is required.
1831
                        ep1->i = quadlit(&ep1->f128);
1832
                }
1833
                else
1834
 
1835
                {
1836
                        ep1 = makenode(en_uminus,ep1,(ENODE *)NULL);
1837
                        ep1->constflag = ep1->p[0]->constflag;
1838
                        ep1->isUnsigned = ep1->p[0]->isUnsigned;
1839
                        ep1->esize = tp->size;
1840
                        ep1->etype = (e_bt)tp->type;
1841
                }
1842
        break;
1843
 
1844
    case nott:
1845
    case kw_not:
1846
                NextToken();
1847
                tp = ParseCastExpression(&ep1);
1848
                if( tp == NULL ) {
1849
                        error(ERR_IDEXPECT);
1850
                        return (TYP *)NULL;
1851
                }
1852
                ep1 = makenode(en_not,ep1,(ENODE *)NULL);
1853
                ep1->constflag = ep1->p[0]->constflag;
1854
                ep1->isUnsigned = ep1->p[0]->isUnsigned;
1855
                ep1->SetType(tp);
1856
                ep1->esize = tp->size;
1857
                break;
1858
 
1859
    case cmpl:
1860
        NextToken();
1861
        tp = ParseCastExpression(&ep1);
1862
        if( tp == NULL ) {
1863
            error(ERR_IDEXPECT);
1864
            return 0;
1865
        }
1866
        ep1 = makenode(en_compl,ep1,(ENODE *)NULL);
1867
        ep1->constflag = ep1->p[0]->constflag;
1868
                ep1->isUnsigned = ep1->p[0]->isUnsigned;
1869
                ep1->SetType(tp);
1870
                ep1->esize = tp->size;
1871
        break;
1872
 
1873
    case star:
1874
        NextToken();
1875
        tp = ParseCastExpression(&ep1);
1876
        if( tp == NULL ) {
1877
            error(ERR_IDEXPECT);
1878
            return (TYP *)NULL;
1879
        }
1880
        if( tp->GetBtp() == NULL )
1881
                            error(ERR_DEREF);
1882
        else {
1883
                        // A star before a function pointer just means that we want to
1884
                        // invoke the function. We want to retain the pointer to the
1885
                        // function as the type.
1886
                        if (tp->GetBtp()->type!=bt_func && tp->GetBtp()->type!=bt_ifunc)
1887
                                tp = tp->GetBtp();
1888
        }
1889
            tp1 = tp;
1890
                //Autoincdec(tp,&ep1);
1891
            tp = CondDeref(&ep1,tp);
1892
        break;
1893
 
1894
    case bitandd:
1895
        NextToken();
1896
        tp = ParseCastExpression(&ep1);
1897
        if( tp == NULL ) {
1898
            error(ERR_IDEXPECT);
1899
            return (TYP *)NULL;
1900
        }
1901
        if( IsLValue(ep1))
1902
            ep1 = ep1->p[0];
1903
        ep1->esize = 8;     // converted to a pointer so size is now 8
1904
        tp1 = TYP::Make(bt_pointer,8);
1905
        tp1->btp = tp->GetIndex();
1906
        tp1->val_flag = FALSE;
1907
                tp1->isUnsigned = TRUE;
1908
        tp = tp1;
1909
//        printf("tp %p: %d\r\n", tp, tp->type);
1910
/*
1911
                sp = search("ta_int",&tp->GetBtp()->lst);
1912
                if (sp) {
1913
                        printf("bitandd: ta_int\r\n");
1914
                }
1915
*/
1916
        break;
1917
/*
1918
        case kw_abs:
1919
                NextToken();
1920
                if (lastst==openpa) {
1921
                        flag2 = TRUE;
1922
                        NextToken();
1923
                }
1924
        tp = ParseCastExpression(&ep1);
1925
        if( tp == NULL ) {
1926
            error(ERR_IDEXPECT);
1927
            return (TYP *)NULL;
1928
        }
1929
        ep1 = makenode(en_abs,ep1,(ENODE *)NULL);
1930
        ep1->constflag = ep1->p[0]->constflag;
1931
                ep1->isUnsigned = ep1->p[0]->isUnsigned;
1932
                ep1->esize = tp->size;
1933
                if (flag2)
1934
                        needpunc(closepa,2);
1935
                break;
1936
 
1937
        case kw_max:
1938
        case kw_min:
1939
                {
1940
                        TYP *tp1, *tp2, *tp3;
1941
 
1942
                        flag2 = lastst==kw_max;
1943
                        NextToken();
1944
                        needpunc(comma,2);
1945
                        tp1 = ParseCastExpression(&ep1);
1946
                        if( tp1 == NULL ) {
1947
                                error(ERR_IDEXPECT);
1948
                                return (TYP *)NULL;
1949
                        }
1950
                        needpunc(comma,2);
1951
                        tp2 = ParseCastExpression(&ep2);
1952
                        if( tp1 == NULL ) {
1953
                                error(ERR_IDEXPECT);
1954
                                return (TYP *)NULL;
1955
                        }
1956
                        if (lastst==comma) {
1957
                                NextToken();
1958
                                tp3 = ParseCastExpression(&ep3);
1959
                                if( tp1 == NULL ) {
1960
                                        error(ERR_IDEXPECT);
1961
                                        return (TYP *)NULL;
1962
                                }
1963
                        }
1964
                        else
1965
                                tp3 = nullptr;
1966
                        tp = forcefit(&ep2,tp2,&ep1,tp1,1);
1967
                        tp = forcefit(&ep3,tp3,&ep2,tp,1);
1968
                        ep1 = makenode(flag2 ? en_max : en_min,ep1,ep2);
1969
                        ep1->p[2] = ep3;
1970
                        ep1->constflag = ep1->p[0]->constflag & ep2->p[0]->constflag & ep3->p[0]->constflag;
1971
                        ep1->isUnsigned = ep1->p[0]->isUnsigned;
1972
                        ep1->esize = tp->size;
1973
                        needpunc(closepa,2);
1974
                }
1975
                break;
1976
*/
1977
    case kw_sizeof:
1978
        NextToken();
1979
                if (lastst==openpa) {
1980
                        flag2 = TRUE;
1981
                        NextToken();
1982
                }
1983
                if (flag2 && IsBeginningOfTypecast(lastst)) {
1984
                        tp = head;
1985
                        tp1 = tail;
1986
                        Declaration::ParseSpecifier(0);
1987
                        Declaration::ParsePrefix(FALSE);
1988
                        if( head != NULL )
1989
                                ep1 = makeinode(en_icon,head->size);
1990
                        else {
1991
                                error(ERR_IDEXPECT);
1992
                                ep1 = makeinode(en_icon,1);
1993
                        }
1994
                        head = tp;
1995
                        tail = tp1;
1996
                }
1997
                else {
1998
                        sizeof_flag++;
1999
                        tp = ParseUnaryExpression(&ep1, got_pa);
2000
                        sizeof_flag--;
2001
                        if (tp == 0) {
2002
                                error(ERR_SYNTAX);
2003
                                ep1 = makeinode(en_icon,1);
2004
                        } else
2005
                                ep1 = makeinode(en_icon, (long) tp->size);
2006
                }
2007
                if (flag2)
2008
                        needpunc(closepa,2);
2009
                ep1->constflag = TRUE;
2010
                ep1->esize = 2;
2011
                tp = &stdint;
2012
        break;
2013
 
2014
    case kw_new:
2015
                {
2016
                        ENODE *ep4, *ep5;
2017
                        std::string *name = new std::string("__new");
2018
 
2019
                        currentFn->UsesNew = TRUE;
2020
                        currentFn->IsLeaf = FALSE;
2021
                        NextToken();
2022
                        if (IsBeginningOfTypecast(lastst)) {
2023
 
2024
                                tp = head;
2025
                                tp1 = tail;
2026
                                Declaration::ParseSpecifier(0);
2027
                                Declaration::ParsePrefix(FALSE);
2028
                                if( head != NULL )
2029
                                        ep1 = makeinode(en_icon,head->size);
2030
                                else {
2031
                                        error(ERR_IDEXPECT);
2032
                                        ep1 = makeinode(en_icon,1);
2033
                                }
2034
                                ep4 = nullptr;
2035
                                ep2 = makeinode(en_icon,head->GetHash());
2036
                                ep3 = makenode(en_object_list,nullptr,nullptr);
2037
                                ep4 = makeinode(en_icon, head->typeno);
2038
                                //ep5 = makenode(en_void,ep1,nullptr);
2039
                                ep5 = nullptr;
2040
                                ep5 = makenode(en_void,ep2,ep5);
2041
                                ep5 = makenode(en_void,ep3,ep5);
2042
                                ep5 = makenode(en_void, ep4, ep5);
2043
                                ep2 = makesnode(en_cnacon, name, name, 0);
2044
                                ep1 = makefcnode(en_fcall, ep2, ep5, nullptr);
2045
                                head = tp;
2046
                                tail = tp1;
2047
                        }
2048
      else {
2049
                        sizeof_flag++;
2050
                        tp = ParseUnaryExpression(&ep1, got_pa);
2051
                        sizeof_flag--;
2052
                        if (tp == 0) {
2053
                                error(ERR_SYNTAX);
2054
                                ep1 = makeinode(en_icon,1);
2055
                        } else
2056
                                ep1 = makeinode(en_icon, (long) tp->size);
2057
                        ep3 = makenode(en_void,ep1,nullptr);
2058
                        ep2 = makesnode(en_cnacon, name, name, 0);
2059
                        ep1 = makefcnode(en_fcall, ep2, ep3, nullptr);
2060
                        }
2061
                }
2062
                break;
2063
 
2064
    case kw_delete:
2065
                currentFn->IsLeaf = FALSE;
2066
                NextToken();
2067
                {
2068
                        std::string *name = new std::string("__delete");
2069
 
2070
                        if (lastst==openbr) {
2071
                                NextToken();
2072
                        needpunc(closebr,50);
2073
                    }
2074
                        tp1 = ParseCastExpression(&ep1);
2075
                        tp1 = deref(&ep1, tp1);
2076
                        ep2 = makesnode(en_cnacon, name, name, 0);
2077
                        ep1 = makefcnode(en_fcall, ep2, ep1, nullptr);
2078
      }
2079
      break;
2080
 
2081
    case kw_typenum:
2082
                NextToken();
2083
                needpunc(openpa,3);
2084
                tp = head;
2085
                tp1 = tail;
2086
                Declaration::ParseSpecifier(0);
2087
                Declaration::ParsePrefix(FALSE);
2088
                if( head != NULL )
2089
                        ep1 = makeinode(en_icon,head->GetHash());
2090
                else {
2091
                        error(ERR_IDEXPECT);
2092
                        ep1 = makeinode(en_icon,1);
2093
                }
2094
                head = tp;
2095
                tail = tp1;
2096
                ep1->constflag = TRUE;
2097
                ep1->esize = 2;
2098
                tp = &stdint;
2099
                needpunc(closepa,4);
2100
                break;
2101
 
2102
    default:
2103
        tp = ParsePostfixExpression(&ep1, got_pa);
2104
        break;
2105
    }
2106
    *node = ep1;
2107
    if (ep1)
2108
            (*node)->SetType(tp);
2109
    if (tp)
2110
    Leave("</ParseUnary>", tp->type);
2111
    else
2112
    Leave("</ParseUnary>", 0);
2113
    return tp;
2114
}
2115
 
2116
// ----------------------------------------------------------------------------
2117
// A cast_expression is:
2118
//              unary_expression
2119
//              (type name)cast_expression
2120
// ----------------------------------------------------------------------------
2121
static TYP *ParseCastExpression(ENODE **node)
2122
{
2123
        TYP *tp, *tp1, *tp2;
2124
        ENODE *ep1, *ep2;
2125
 
2126
    Enter("ParseCast ");
2127
    *node = (ENODE *)NULL;
2128
        switch(lastst) {
2129
 /*
2130
        case openpa:
2131
                NextToken();
2132
        if(IsBeginningOfTypecast(lastst) ) {
2133
            ParseSpecifier(0); // do cast declaration
2134
            ParseDeclarationPrefix(FALSE);
2135
            tp = head;
2136
                        tp1 = tail;
2137
            needpunc(closepa);
2138
            if((tp2 = ParseCastExpression(&ep1)) == NULL ) {
2139
                error(ERR_IDEXPECT);
2140
                tp = (TYP *)NULL;
2141
            }
2142
                        ep2 = makenode(en_void,ep1,(ENODE *)NULL);
2143
                        ep2->constflag = ep1->constflag;
2144
                        ep2->isUnsigned = ep1->isUnsigned;
2145
                        ep2->etype = ep1->etype;
2146
                        ep2->esize = ep1->esize;
2147
                        forcefit(&ep2,tp2,&ep1,tp);
2148
                        head = tp;
2149
                        tail = tp1;
2150
        }
2151
                else {
2152
                        tp = ParseUnaryExpression(&ep1,1);
2153
                }
2154
                break;
2155
*/
2156
        case openpa:
2157
                NextToken();
2158
                if (IsBeginningOfTypecast(lastst)) {
2159
                        Declaration::ParseSpecifier(0); // do cast declaration
2160
                        Declaration::ParsePrefix(FALSE);
2161
                        tp = head;
2162
                        tp1 = tail;
2163
                        needpunc(closepa, 5);
2164
                        if ((tp2 = ParseCastExpression(&ep1)) == NULL) {
2165
                                error(ERR_IDEXPECT);
2166
                                tp = (TYP *)NULL;
2167
                        }
2168
                        /*
2169
                        if (tp2->isConst) {
2170
                                *node = ep1;
2171
                                return tp;
2172
                        }
2173
                        */
2174
                        if (tp == nullptr)
2175
                                error(ERR_NULLPOINTER);
2176
                        if (tp && tp->IsFloatType())
2177
                                ep2 = makenode(en_tempfpref, (ENODE *)NULL, (ENODE *)NULL);
2178
                        else {
2179
                                ep2 = makenode(en_tempref, (ENODE *)NULL, (ENODE *)NULL);
2180
                                ep2->SetType(tp);
2181
                        }
2182
                        ep2 = makenode(en_void,ep2,ep1);
2183
                        if (ep1==nullptr)
2184
                error(ERR_NULLPOINTER);
2185
                        else {
2186
                                ep2->constflag = ep1->constflag;
2187
                                ep2->isUnsigned = ep1->isUnsigned;
2188
                                ep2->etype = ep1->etype;
2189
                                ep2->esize = ep1->esize;
2190
                                forcefit(&ep2,tp,&ep1,tp2,false);
2191
                        }
2192
//                      forcefit(&ep2,tp2,&ep1,tp,false);
2193
                        head = tp;
2194
                        tail = tp1;
2195
                        *node = ep2;
2196
                (*node)->SetType(tp);
2197
                        return tp;
2198
        }
2199
                else {
2200
                        tp = ParseUnaryExpression(&ep1,1);
2201
                }
2202
                break;
2203
 
2204
        default:
2205
                tp = ParseUnaryExpression(&ep1,0);
2206
                break;
2207
        }
2208
        *node = ep1;
2209
        if (ep1)
2210
            (*node)->SetType(tp);
2211
        if (tp)
2212
        Leave("ParseCast", tp->type);
2213
        else
2214
        Leave("ParseCast", 0);
2215
        return tp;
2216
}
2217
 
2218
/*
2219
 *      multops parses the multiply priority operators. the syntax of
2220
 *      this group is:
2221
 *
2222
 *              unary
2223
 *              unary * unary
2224
 *              unary / unary
2225
 *              unary % unary
2226
 */
2227
TYP *multops(ENODE **node)
2228
{
2229
        ENODE *ep1, *ep2;
2230
        TYP *tp1, *tp2;
2231
        int     oper;
2232
        bool isScalar;
2233
 
2234
    Enter("Mulops");
2235
    ep1 = (ENODE *)NULL;
2236
    *node = (ENODE *)NULL;
2237
        tp1 = ParseCastExpression(&ep1);
2238
        if( tp1 == 0 ) {
2239
        Leave("Mulops NULL",0);
2240
                return 0;
2241
    }
2242
        while( lastst == star || lastst == divide || lastst == modop) {
2243
                oper = lastst;
2244
                NextToken();       /* move on to next unary op */
2245
                tp2 = ParseCastExpression(&ep2);
2246
                if( tp2 == 0 ) {
2247
                        error(ERR_IDEXPECT);
2248
                        *node = ep1;
2249
                        if (ep1)
2250
                            (*node)->SetType(tp1);
2251
                        return tp1;
2252
                        }
2253
                                isScalar = !tp2->IsVectorType();
2254
                tp1 = forcefit(&ep2,tp2,&ep1,tp1,true);
2255
                switch( oper ) {
2256
                        case star:
2257
                                                                switch(tp1->type) {
2258
                                                                case bt_triple:
2259
                                                                        ep1 = makenode(en_fmul,ep1,ep2);
2260
                                                                        ep1->esize = sizeOfFPT;
2261
                                                                        break;
2262
                                                                case bt_double:
2263
                                                                        ep1 = makenode(en_fmul,ep1,ep2);
2264
                                                                        ep1->esize = sizeOfFPD;
2265
                                                                        break;
2266
                                                                case bt_quad:
2267
                                                                        ep1 = makenode(en_fmul,ep1,ep2);
2268
                                                                        ep1->esize = sizeOfFPQ;
2269
                                                                        break;
2270
                                                                case bt_float:
2271
                                                                        ep1 = makenode(en_fmul,ep1,ep2);
2272
                                                                        ep1->esize = sizeOfFP;
2273
                                                                        break;
2274
                                                                case bt_vector:
2275
                                                                        if (isScalar)
2276
                                                                                ep1 = makenode(en_vmuls,ep1,ep2);
2277
                                                                        else
2278
                                                                                ep1 = makenode(en_vmul,ep1,ep2);
2279
                                                                        ep1->esize = 512;
2280
                                                                        break;
2281
                                default:
2282
                                                                        // place constant as second operand.
2283
                                                                        if (ep1->nodetype == en_icon) {
2284
                                                                                if (tp1->isUnsigned)
2285
                                                                                        ep1 = makenode(en_mulu, ep2, ep1);
2286
                                                                                else
2287
                                                                                        ep1 = makenode(en_mul, ep2, ep1);
2288
                                                                        }
2289
                                                                        else {
2290
                                                                                if (tp1->isUnsigned)
2291
                                                                                        ep1 = makenode(en_mulu, ep1, ep2);
2292
                                                                                else
2293
                                                                                        ep1 = makenode(en_mul, ep1, ep2);
2294
                                                                        }
2295
                                                                }
2296
                                                                ep1->esize = tp1->size;
2297
                                                                ep1->etype = (e_bt)tp1->type;
2298
                                break;
2299
                        case divide:
2300
                                if (tp1->type==bt_triple) {
2301
                                                                        ep1 = makenode(en_fdiv,ep1,ep2);
2302
                                                                        ep1->esize = sizeOfFPT;
2303
                                                                }
2304
                                                                else if (tp1->type==bt_double) {
2305
                                                                        ep1 = makenode(en_fdiv,ep1,ep2);
2306
                                                                        ep1->esize = sizeOfFPD;
2307
                                                                }
2308
                                                                else if (tp1->type==bt_quad) {
2309
                                                                        ep1 = makenode(en_fdiv,ep1,ep2);
2310
                                                                        ep1->esize = sizeOfFPQ;
2311
                                                                }
2312
                                                                else if (tp1->type==bt_float) {
2313
                                                                        ep1 = makenode(en_fdiv,ep1,ep2);
2314
                                                                        ep1->esize = sizeOfFP;
2315
                                                                }
2316
                                else if( tp1->isUnsigned )
2317
                                    ep1 = makenode(en_udiv,ep1,ep2);
2318
                                else
2319
                                    ep1 = makenode(en_div,ep1,ep2);
2320
                                break;
2321
                                                                ep1->esize = tp1->size;
2322
                                                                ep1->etype = (e_bt)tp1->type;
2323
                        case modop:
2324
                                if( tp1->isUnsigned )
2325
                                        ep1 = makenode(en_umod,ep1,ep2);
2326
                                else
2327
                                        ep1 = makenode(en_mod,ep1,ep2);
2328
                                                                ep1->esize = tp1->size;
2329
                                                                ep1->etype = tp1->type;
2330
                                break;
2331
                        }
2332
                PromoteConstFlag(ep1);
2333
                }
2334
        *node = ep1;
2335
        if (ep1)
2336
                    (*node)->SetType(tp1);
2337
    Leave("Mulops",0);
2338
        return tp1;
2339
}
2340
 
2341
// ----------------------------------------------------------------------------
2342
// Addops handles the addition and subtraction operators.
2343
// ----------------------------------------------------------------------------
2344
 
2345
static TYP *addops(ENODE **node)
2346
{
2347
        ENODE *ep1, *ep2, *ep3, *ep4;
2348
    TYP *tp1, *tp2;
2349
    int oper;
2350
        int sz1, sz2;
2351
        bool isScalar = true;
2352
 
2353
    Enter("Addops");
2354
    ep1 = (ENODE *)NULL;
2355
    *node = (ENODE *)NULL;
2356
        sz1 = sz2 = 0;
2357
        tp1 = multops(&ep1);
2358
    if( tp1 == (TYP *)NULL )
2359
        goto xit;
2360
        if (tp1->type == bt_pointer) {
2361
        if (tp1->GetBtp()==NULL) {
2362
            printf("DIAG: pointer to NULL type.\r\n");
2363
            goto xit;
2364
        }
2365
        else
2366
                    sz1 = tp1->GetBtp()->size;
2367
    }
2368
    while( lastst == plus || lastst == minus ) {
2369
            oper = (lastst == plus);
2370
            NextToken();
2371
            tp2 = multops(&ep2);
2372
                        if (tp2==nullptr)
2373
                                throw new C64PException(ERR_NULLPOINTER,1);
2374
                        isScalar = !tp2->IsVectorType();
2375
            if( tp2 == 0 ) {
2376
                    error(ERR_IDEXPECT);
2377
                    *node = ep1;
2378
                    goto xit;
2379
                    }
2380
                        if (tp2->type == bt_pointer)
2381
                                sz2 = tp2->GetBtp()->size;
2382
                        // Difference of two pointers to the same type of object...
2383
                        // Divide the result by the size of the pointed to object.
2384
                        if (!oper && (tp1->type == bt_pointer) && (tp2->type == bt_pointer) && (sz1==sz2))
2385
                        {
2386
                                ep1 = makenode( en_sub,ep1,ep2);
2387
                                ep4 = makeinode(en_icon, sz1);
2388
                                ep1 = makenode(en_udiv,ep1,ep4);
2389
                        }
2390
                        else {
2391
                if( tp1->type == bt_pointer ) {
2392
                        tp2 = forcefit(&ep2,tp2,0,&stdint,true);
2393
                        ep3 = makeinode(en_icon,tp1->GetBtp()->size);
2394
                        ep3->constflag = TRUE;
2395
                                        ep3->esize = tp2->size;
2396
                        ep2 = makenode(en_mulu,ep3,ep2);
2397
                        ep2->constflag = ep2->p[1]->constflag;
2398
                                        ep2->esize = tp2->size;
2399
                        }
2400
                else if( tp2->type == bt_pointer ) {
2401
                        tp1 = forcefit(&ep1,tp1,0,&stdint,true);
2402
                        ep3 = makeinode(en_icon,tp2->GetBtp()->size);
2403
                        ep3->constflag = TRUE;
2404
                                        ep3->esize = tp2->size;
2405
                        ep1 = makenode(en_mulu,ep3,ep1);
2406
                        ep1->constflag = ep1->p[1]->constflag;
2407
                                        ep2->esize = tp2->size;
2408
                        }
2409
                tp1 = forcefit(&ep2,tp2,&ep1,tp1,true);
2410
                                switch (tp1->type) {
2411
                                case bt_triple:
2412
                                ep1 = makenode( oper ? en_fadd : en_fsub,ep1,ep2);
2413
                                        ep1->esize = sizeOfFPT;
2414
                                        break;
2415
                                case bt_double:
2416
                                ep1 = makenode( oper ? en_fadd : en_fsub,ep1,ep2);
2417
                                        ep1->esize = sizeOfFPD;
2418
                                        break;
2419
                                case bt_quad:
2420
                    //tp1 = forcefit(&ep1,tp1,&ep2,tp2,true);
2421
                                ep1 = makenode( oper ? en_fadd : en_fsub,ep1,ep2);
2422
                                        ep1->esize = sizeOfFPQ;
2423
                                        break;
2424
                                case bt_float:
2425
                                ep1 = makenode( oper ? en_fadd : en_fsub,ep1,ep2);
2426
                                        ep1->esize = sizeOfFPS;
2427
                                        break;
2428
                                case bt_vector:
2429
                                        if (isScalar)
2430
                                        ep1 = makenode( oper ? en_vadds : en_vsubs,ep1,ep2);
2431
                                        else
2432
                                        ep1 = makenode( oper ? en_vadd : en_vsub,ep1,ep2);
2433
                                        ep1->esize = 8;
2434
                                        break;
2435
                                // In the case of a pointer place any constant to be added
2436
                                // as the second operand. This will allow the use of immediate
2437
                                // mode addressing rather than having to load into a register.
2438
                                case bt_pointer:
2439
                                        if (ep1->nodetype==en_icon && oper)
2440
                                                ep1 = makenode(en_add, ep2, ep1);
2441
                                        else
2442
                                                ep1 = makenode(oper ? en_add : en_sub, ep1, ep2);
2443
                                        break;
2444
                                default:
2445
                                ep1 = makenode( oper ? en_add : en_sub,ep1,ep2);
2446
                                }
2447
            }
2448
            PromoteConstFlag(ep1);
2449
                        ep1->esize = tp1->size;
2450
                        ep1->etype = tp1->type;
2451
            }
2452
    *node = ep1;
2453
xit:
2454
    if (*node)
2455
        (*node)->SetType(tp1);
2456
    Leave("Addops",0);
2457
    return tp1;
2458
}
2459
 
2460
// ----------------------------------------------------------------------------
2461
// Shiftop handles the shift operators << and >>.
2462
// ----------------------------------------------------------------------------
2463
TYP *shiftop(ENODE **node)
2464
{
2465
        ENODE *ep1, *ep2;
2466
    TYP *tp1, *tp2;
2467
    int oper;
2468
 
2469
    Enter("Shiftop");
2470
    *node = NULL;
2471
        tp1 = addops(&ep1);
2472
        if( tp1 == 0)
2473
        goto xit;
2474
    while( lastst == lshift || lastst == rshift || lastst==lrot || lastst==rrot) {
2475
            oper = (lastst == lshift);
2476
                        if (lastst==lrot || lastst==rrot)
2477
                                oper=2 + (lastst==lrot);
2478
            NextToken();
2479
            tp2 = addops(&ep2);
2480
            if( tp2 == 0 )
2481
                    error(ERR_IDEXPECT);
2482
            else    {
2483
                    tp1 = forcefit(&ep2,tp2,&ep1,tp1,true);
2484
                                        if (tp1->IsFloatType())
2485
                                                error(ERR_UNDEF_OP);
2486
                                        else {
2487
                                                if (tp1->isUnsigned) {
2488
                                                        switch(oper) {
2489
                                                        case 0:  ep1 = makenode(en_shru,ep1,ep2); break;
2490
                                                        case 1: ep1 = makenode(en_shlu,ep1,ep2); break;
2491
                                                        case 2: ep1 = makenode(en_ror,ep1,ep2); break;
2492
                                                        case 3: ep1 = makenode(en_rol,ep1,ep2); break;
2493
                                                        }
2494
                                                }
2495
                                                else {
2496
                                                        switch(oper) {
2497
                                                        case 0:  ep1 = makenode(en_asr,ep1,ep2); break;
2498
                                                        case 1: ep1 = makenode(en_asl,ep1,ep2); break;
2499
                                                        case 2: ep1 = makenode(en_ror,ep1,ep2); break;
2500
                                                        case 3: ep1 = makenode(en_rol,ep1,ep2); break;
2501
                                                        }
2502
                                                }
2503
                                                ep1->esize = tp1->size;
2504
                                                PromoteConstFlag(ep1);
2505
                                                }
2506
                    }
2507
            }
2508
    *node = ep1;
2509
 xit:
2510
     if (*node)
2511
        (*node)->SetType(tp1);
2512
    Leave("Shiftop",0);
2513
    return tp1;
2514
}
2515
 
2516
/*
2517
 *      relation handles the relational operators < <= > and >=.
2518
 */
2519
TYP     *relation(ENODE **node)
2520
{
2521
        ENODE *ep1, *ep2;
2522
    TYP *tp1, *tp2;
2523
        bool isVector = false;
2524
 
2525
        int             nt;
2526
        Enter("Relation");
2527
        *node = (ENODE *)NULL;
2528
        tp1 = shiftop(&ep1);
2529
        if( tp1 == 0 )
2530
                goto xit;
2531
        for(;;) {
2532
                        if (tp1->IsVectorType())
2533
                                isVector = true;
2534
                switch( lastst ) {
2535
 
2536
                        case lt:
2537
                                                        if (tp1->IsVectorType())
2538
                                                                nt = en_vlt;
2539
                                                        else if (tp1->IsFloatType())
2540
                                nt = en_flt;
2541
                            else if( tp1->isUnsigned )
2542
                                    nt = en_ult;
2543
                            else
2544
                                    nt = en_lt;
2545
                            break;
2546
                        case gt:
2547
                                                        if (tp1->IsVectorType())
2548
                                                                nt = en_vgt;
2549
                                                        else if (tp1->IsFloatType())
2550
                                nt = en_fgt;
2551
                            else if( tp1->isUnsigned )
2552
                                    nt = en_ugt;
2553
                            else
2554
                                    nt = en_gt;
2555
                            break;
2556
                        case leq:
2557
                                                        if (tp1->IsVectorType())
2558
                                                                nt = en_vle;
2559
                                                        else if (tp1->IsFloatType())
2560
                                nt = en_fle;
2561
                            else if( tp1->isUnsigned )
2562
                                    nt = en_ule;
2563
                            else
2564
                                    nt = en_le;
2565
                            break;
2566
                        case geq:
2567
                                                        if (tp1->IsVectorType())
2568
                                                                nt = en_vge;
2569
                                                        else if (tp1->IsFloatType())
2570
                                nt = en_fge;
2571
                            else if( tp1->isUnsigned )
2572
                                    nt = en_uge;
2573
                            else
2574
                                    nt = en_ge;
2575
                            break;
2576
                        default:
2577
                                goto fini;
2578
                        }
2579
                NextToken();
2580
                tp2 = shiftop(&ep2);
2581
                if( tp2 == 0 )
2582
                        error(ERR_IDEXPECT);
2583
                else    {
2584
                                        if (tp2->IsVectorType())
2585
                                                isVector = true;
2586
                        tp1 = forcefit(&ep2,tp2,&ep1,tp1,true);
2587
                        ep1 = makenode(nt,ep1,ep2);
2588
                                                ep1->esize = 1;
2589
                                                if (isVector)
2590
                                                        tp1 = TYP::Make(bt_vector_mask,sizeOfWord);
2591
                                                PromoteConstFlag(ep1);
2592
                        }
2593
                }
2594
fini:   *node = ep1;
2595
xit:
2596
     if (*node)
2597
                (*node)->SetType(tp1);
2598
        Leave("Relation",0);
2599
        return tp1;
2600
}
2601
 
2602
/*
2603
 *      equalops handles the equality and inequality operators.
2604
 */
2605
TYP *equalops(ENODE **node)
2606
{
2607
        ENODE *ep1, *ep2;
2608
    TYP *tp1, *tp2;
2609
    int oper;
2610
        bool isVector = false;
2611
 
2612
    Enter("EqualOps");
2613
    *node = (ENODE *)NULL;
2614
    tp1 = relation(&ep1);
2615
    if( tp1 == (TYP *)NULL )
2616
        goto xit;
2617
        if (tp1->IsVectorType())
2618
                isVector = true;
2619
    while( lastst == eq || lastst == neq ) {
2620
        oper = (lastst == eq);
2621
        NextToken();
2622
        tp2 = relation(&ep2);
2623
        if( tp2 == NULL )
2624
                error(ERR_IDEXPECT);
2625
        else {
2626
                        if (tp2->IsVectorType())
2627
                                isVector = true;
2628
            tp1 = forcefit(&ep2,tp2,&ep1,tp1,true);
2629
                        if (tp1->IsVectorType())
2630
                                ep1 = makenode( oper ? en_veq : en_vne,ep1,ep2);
2631
            else if (tp1->IsFloatType())
2632
                ep1 = makenode( oper ? en_feq : en_fne,ep1,ep2);
2633
            else
2634
                ep1 = makenode( oper ? en_eq : en_ne,ep1,ep2);
2635
                        ep1->esize = 2;
2636
                        if (isVector)
2637
                                tp1 = TYP::Make(bt_vector_mask,sizeOfWord);
2638
                        ep1->etype = tp1->type;
2639
            PromoteConstFlag(ep1);
2640
        }
2641
        }
2642
    *node = ep1;
2643
 xit:
2644
     if (*node)
2645
        (*node)->SetType(tp1);
2646
    Leave("EqualOps",0);
2647
    return tp1;
2648
}
2649
 
2650
/*
2651
 *      binop is a common routine to handle all of the legwork and
2652
 *      error checking for bitand, bitor, bitxor, andop, and orop.
2653
 */
2654
TYP *binop(ENODE **node, TYP *(*xfunc)(ENODE **),int nt, int sy)
2655
{
2656
        ENODE    *ep1, *ep2;
2657
        TYP             *tp1, *tp2;
2658
        Enter("Binop");
2659
        *node = (ENODE *)NULL;
2660
        tp1 = (*xfunc)(&ep1);
2661
        if( tp1 == 0 )
2662
            goto xit;
2663
        while( lastst == sy ) {
2664
                NextToken();
2665
                tp2 = (*xfunc)(&ep2);
2666
                if( tp2 == 0 )
2667
                        error(ERR_IDEXPECT);
2668
                else    {
2669
                        tp1 = forcefit(&ep2,tp2,&ep1,tp1,true);
2670
                        ep1 = makenode(nt,ep1,ep2);
2671
                                                ep1->esize = tp1->size;
2672
                                                ep1->etype = tp1->type;
2673
                                PromoteConstFlag(ep1);
2674
                        }
2675
                }
2676
        *node = ep1;
2677
xit:
2678
     if (*node)
2679
                (*node)->SetType(tp1);
2680
        Leave("Binop",0);
2681
        return tp1;
2682
}
2683
 
2684
TYP *bitwiseand(ENODE **node)
2685
/*
2686
 *      the bitwise and operator...
2687
 */
2688
{       return binop(node,equalops,en_and,bitandd);
2689
}
2690
 
2691
TYP     *bitwisexor(ENODE **node)
2692
{       return binop(node,bitwiseand,en_xor,uparrow);
2693
}
2694
 
2695
TYP     *bitwiseor(ENODE **node)
2696
{       return binop(node,bitwisexor,en_or,bitorr);
2697
}
2698
 
2699
TYP     *andop(ENODE **node)
2700
{       return binop(node,bitwiseor,en_land,land);
2701
}
2702
 
2703
TYP *orop(ENODE **node)
2704
{
2705
        return binop(node,andop,en_lor,lor);
2706
}
2707
 
2708
/*
2709
 *      this routine processes the hook operator.
2710
 */
2711
TYP *conditional(ENODE **node)
2712
{
2713
        TYP             *tp1, *tp2, *tp3;
2714
    ENODE    *ep1, *ep2, *ep3;
2715
    Enter("Conditional");
2716
    *node = (ENODE *)NULL;
2717
    tp1 = orop(&ep1);       /* get condition */
2718
    if( tp1 == (TYP *)NULL )
2719
        goto xit;
2720
    if( lastst == hook ) {
2721
                        iflevel++;
2722
                if ((iflevel > maxPn-1) && isThor)
2723
                    error(ERR_OUTOFPREDS);
2724
            NextToken();
2725
            if( (tp2 = conditional(&ep2)) == NULL) {
2726
                    error(ERR_IDEXPECT);
2727
                    goto cexit;
2728
                    }
2729
            needpunc(colon,6);
2730
            if( (tp3 = conditional(&ep3)) == NULL) {
2731
                    error(ERR_IDEXPECT);
2732
                    goto cexit;
2733
                    }
2734
            tp1 = forcefit(&ep3,tp3,&ep2,tp2,true);
2735
            ep2 = makenode(en_void,ep2,ep3);
2736
                        ep2->esize = tp1->size;
2737
            ep1 = makenode(en_cond,ep1,ep2);
2738
            ep1->predreg = iflevel;
2739
                        ep1->esize = tp1->size;
2740
                        iflevel--;
2741
            }
2742
cexit:  *node = ep1;
2743
xit:
2744
     if (*node)
2745
        (*node)->SetType(tp1);
2746
    Leave("Conditional",0);
2747
    return tp1;
2748
}
2749
 
2750
// ----------------------------------------------------------------------------
2751
//      asnop handles the assignment operators. currently only the
2752
//      simple assignment is implemented.
2753
// ----------------------------------------------------------------------------
2754
TYP *asnop(ENODE **node)
2755
{
2756
        ENODE *ep1, *ep2, *ep3;
2757
    TYP *tp1, *tp2;
2758
    int op;
2759
 
2760
    Enter("Assignop");
2761
    *node = (ENODE *)NULL;
2762
    tp1 = conditional(&ep1);
2763
    if( tp1 == 0 )
2764
        goto xit;
2765
    for(;;) {
2766
        switch( lastst ) {
2767
            case assign:
2768
                op = en_assign;
2769
ascomm:         NextToken();
2770
                tp2 = asnop(&ep2);
2771
ascomm2:
2772
                        if( tp2 == 0 || !IsLValue(ep1) )
2773
                    error(ERR_LVALUE);
2774
                                else {
2775
                                        tp1 = forcefit(&ep2,tp2,&ep1,tp1,false);
2776
                                        ep1 = makenode(op,ep1,ep2);
2777
                                        ep1->esize = tp1->size;
2778
                                        ep1->etype = tp1->type;
2779
                                        ep1->isUnsigned = tp1->isUnsigned;
2780
                                        // Struct assign calls memcpy, so function is no
2781
                                        // longer a leaf routine.
2782
                                        if (tp1->size > 8)
2783
                                                currentFn->IsLeaf = FALSE;
2784
                                }
2785
                                break;
2786
                        case asplus:
2787
                                op = en_asadd;
2788
ascomm3:        NextToken();
2789
                                tp2 = asnop(&ep2);
2790
                                if( tp1->type == bt_pointer ) {
2791
                                        ep3 = makeinode(en_icon,tp1->GetBtp()->size);
2792
                                        ep3->esize = sizeOfPtr;
2793
                                        ep2 = makenode(en_mul,ep2,ep3);
2794
                                        ep2->esize = sizeOfPtr;
2795
                                }
2796
                                goto ascomm2;
2797
                        case asminus:
2798
                                op = en_assub;
2799
                                goto ascomm3;
2800
                        case astimes:
2801
                                if (tp1->isUnsigned)
2802
                                        op = en_asmulu;
2803
                                else
2804
                                        op = en_asmul;
2805
                                goto ascomm;
2806
                        case asdivide:
2807
                                if (tp1->isUnsigned)
2808
                                        op = en_asdivu;
2809
                                else
2810
                                        op = en_asdiv;
2811
                                goto ascomm;
2812
                        case asmodop:
2813
                                if (tp1->isUnsigned)
2814
                                        op = en_asmodu;
2815
                                else
2816
                                        op = en_asmod;
2817
                                goto ascomm;
2818
                        case aslshift:
2819
                                op = en_aslsh;
2820
                                goto ascomm;
2821
                        case asrshift:
2822
                                if (tp1->isUnsigned)
2823
                                        op = en_asrshu;
2824
                                else
2825
                                        op = en_asrsh;
2826
                                goto ascomm;
2827
                        case asand:
2828
                                op = en_asand;
2829
                                goto ascomm;
2830
                        case asor:
2831
                                op = en_asor;
2832
                                goto ascomm;
2833
                        case asxor:
2834
                                op = en_asxor;
2835
                                goto ascomm;
2836
                        default:
2837
                                goto asexit;
2838
                        }
2839
        }
2840
asexit: *node = ep1;
2841
xit:
2842
     if (*node)
2843
        (*node)->SetType(tp1);
2844
    Leave("Assignop",0);
2845
        return tp1;
2846
}
2847
 
2848
// ----------------------------------------------------------------------------
2849
// Evaluate an expression where the assignment operator is not legal.
2850
// ----------------------------------------------------------------------------
2851
static TYP *NonAssignExpression(ENODE **node)
2852
{
2853
        TYP *tp;
2854
        pep1 = nullptr;
2855
        Enter("NonAssignExpression");
2856
    *node = (ENODE *)NULL;
2857
    tp = conditional(node);
2858
    if( tp == (TYP *)NULL )
2859
        *node =(ENODE *)NULL;
2860
    Leave("NonAssignExpression",tp ? tp->type : 0);
2861
     if (*node)
2862
        (*node)->SetType(tp);
2863
    return tp;
2864
}
2865
 
2866
// ----------------------------------------------------------------------------
2867
// Evaluate an expression where the comma operator is not legal.
2868
// Externally visible entry point for GetIntegerExpression() and
2869
// ArgumentList().
2870
// ----------------------------------------------------------------------------
2871
TYP *NonCommaExpression(ENODE **node)
2872
{
2873
        TYP *tp;
2874
        pep1 = nullptr;
2875
        Enter("NonCommaExpression");
2876
    *node = (ENODE *)NULL;
2877
    tp = asnop(node);
2878
    if( tp == (TYP *)NULL )
2879
        *node =(ENODE *)NULL;
2880
    Leave("NonCommaExpression",tp ? tp->type : 0);
2881
     if (*node)
2882
        (*node)->SetType(tp);
2883
    return tp;
2884
}
2885
 
2886
/*
2887
 *      evaluate the comma operator. comma operators are kept as
2888
 *      void nodes.
2889
 */
2890
TYP *commaop(ENODE **node)
2891
{
2892
        TYP *tp1,*tp2;
2893
        ENODE *ep1,*ep2;
2894
 
2895
  *node = (ENODE *)NULL;
2896
        tp1 = NonCommaExpression(&ep1);
2897
        if (tp1==(TYP *)NULL)
2898
                return (TYP *)NULL;
2899
        while(1) {
2900
                if (lastst==comma) {
2901
                        NextToken();
2902
                        tp2 = NonCommaExpression(&ep2);
2903
      ep1 = makenode(en_void,ep1,ep2);
2904
                        ep1->esize = tp1->size;
2905
                }
2906
                else
2907
                        break;
2908
        }
2909
        *node = ep1;
2910
     if (*node)
2911
        (*node)->SetType(tp1);
2912
        return tp1;
2913
}
2914
 
2915
//TYP *commaop(ENODE **node)
2916
//{  
2917
//      TYP             *tp1;
2918
//        ENODE    *ep1, *ep2;
2919
//        tp1 = asnop(&ep1);
2920
//        if( tp1 == NULL )
2921
//                return NULL;
2922
//        if( lastst == comma ) {
2923
//                              NextToken();
2924
//                tp1 = commaop(&ep2);
2925
//                if( tp1 == NULL ) {
2926
//                        error(ERR_IDEXPECT);
2927
//                        goto coexit;
2928
//                        }
2929
//                ep1 = makenode(en_void,ep1,ep2);
2930
//                }
2931
//coexit: *node = ep1;
2932
//        return tp1;
2933
//}
2934
 
2935
// ----------------------------------------------------------------------------
2936
// Evaluate an expression where all operators are legal.
2937
// ----------------------------------------------------------------------------
2938
TYP *expression(ENODE **node)
2939
{
2940
        TYP *tp;
2941
        Enter("<expression>");
2942
        pep1 = nullptr;
2943
  *node = (ENODE *)NULL;
2944
  tp = commaop(node);
2945
  if( tp == (TYP *)NULL )
2946
      *node = (ENODE *)NULL;
2947
  TRACE(printf("leave exp\r\n"));
2948
  if (tp) {
2949
     if (*node)
2950
        (*node)->SetType(tp);
2951
      Leave("Expression",tp->type);
2952
  }
2953
  else
2954
  Leave("</Expression>",0);
2955
  return tp;
2956
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.