~z88dk-team/z88dk-pkg/trunk

« back to all changes in this revision

Viewing changes to src/sccz80/primary.c

  • Committer: Bazaar Package Importer
  • Author(s): Luca Falavigna
  • Date: 2008-02-12 08:23:49 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080212082349-wgijt44scmgje90o
Tags: 1.7.ds1-1ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
  - build z88dk and z88dk-bin binary packages for lpia too
  - update Maintainer field as per spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *      This part contains various routines to deal with constants
6
6
 *      and also finds variable names in the hash tables
7
7
 *
8
 
 *      $Id: primary.c,v 1.17 2002/11/02 20:17:15 dom Exp $
 
8
 *      $Id: primary.c,v 1.19 2007/07/05 18:39:00 dom Exp $
9
9
 */
10
10
 
11
11
 
14
14
 
15
15
int primary(LVALUE *lval)
16
16
{
17
 
        char sname[NAMESIZE] ;
18
 
        SYMBOL *ptr ;
19
 
        int k,level ;
20
 
        char cid,vtype,cflags,clevel;
21
 
        TAG_SYMBOL *cotag;
 
17
    char sname[NAMESIZE] ;
 
18
    SYMBOL *ptr ;
 
19
    int k,level ;
 
20
    char cid,vtype,cflags,clevel;
 
21
    TAG_SYMBOL *cotag;
22
22
 
23
 
        if ( cmatch('(') ) {
 
23
    if ( cmatch('(') ) {
24
24
                lval->level++;
25
 
                do k=heir1(lval); while (cmatch(',')) ;
26
 
                needchar(')');
 
25
        do k=heir1(lval); while (cmatch(',')) ;
 
26
        needchar(')');
27
27
                /* Not sure about doing this here..but here goes nowt!*/
28
28
                if (lval->c_vtype) {
29
29
                    docast(lval,YES);
32
32
                if (lval->c_vtype) {
33
33
                    docast(lval,YES);
34
34
                }
35
 
                return k;
36
 
        }
37
 
        /* clear lval array - djm second arg was lval.. now cast, clears lval */
38
 
        cid=lval->c_id;
39
 
        vtype=lval->c_vtype;
40
 
        cflags=lval->c_flags;
41
 
        cotag=lval->c_tag;
 
35
        return k;
 
36
    }
 
37
    /* clear lval array - djm second arg was lval.. now cast, clears lval */
 
38
    cid=lval->c_id;
 
39
    vtype=lval->c_vtype;
 
40
    cflags=lval->c_flags;
 
41
    cotag=lval->c_tag;
42
42
        level=lval->level;
43
43
        clevel=lval->castlevel;
44
44
        memset(lval,0,sizeof(LVALUE));
45
 
        lval->c_id=cid;
46
 
        lval->c_vtype=vtype;
47
 
        lval->c_flags=cflags;
48
 
        lval->c_tag=cotag;
 
45
    lval->c_id=cid;
 
46
    lval->c_vtype=vtype;
 
47
    lval->c_flags=cflags;
 
48
    lval->c_tag=cotag;
49
49
        lval->level=level;
50
50
        lval->castlevel = clevel;
51
51
 
52
 
        if ( symname(sname) ) {
53
 
                if ( strcmp(sname, "sizeof") == 0 ) {
54
 
                        size_of(lval) ;
55
 
                        return(0) ;
56
 
                }
57
 
                else if ( (ptr=findloc(sname)) && ptr->ident !=GOTOLABEL ) {
 
52
    if ( symname(sname) ) {
 
53
        if ( strcmp(sname, "sizeof") == 0 ) {
 
54
            size_of(lval) ;
 
55
            return(0) ;
 
56
        }
 
57
        else if ( (ptr=findloc(sname)) && ptr->ident !=GOTOLABEL ) {
58
58
 
59
 
                        lval->offset=getloc(ptr, 0);
60
 
                        lval->symbol = ptr;
61
 
                        lval->val_type = lval->indirect = ptr->type;
62
 
                        lval->flags = ptr->flags;
63
 
                        lval->ident = ptr->ident;
 
59
            lval->offset=getloc(ptr, 0);
 
60
            lval->symbol = ptr;
 
61
            lval->val_type = lval->indirect = ptr->type;
 
62
            lval->flags = ptr->flags;
 
63
            lval->ident = ptr->ident;
64
64
                        lval->storage = ptr->storage;
65
 
                        lval->ptr_type=0;
66
 
                        ltype=ptr->type;
67
 
                        if ( ptr->type == STRUCT )
68
 
                                lval->tagsym = tagtab + ptr->tag_idx ;
69
 
                        if ( ptr->ident == POINTER ) {
70
 
                                lval->ptr_type = ptr->type;
 
65
            lval->ptr_type=0;
 
66
            ltype=ptr->type;
 
67
            if ( ptr->type == STRUCT )
 
68
                lval->tagsym = tagtab + ptr->tag_idx ;
 
69
            if ( ptr->ident == POINTER ) {
 
70
                lval->ptr_type = ptr->type;
71
71
/* djm long pointers */
72
 
                                lval->indirect=lval->val_type = (ptr->flags&FARPTR ? CPTR : CINT);
73
 
                                ltype=lval->indirect;
74
 
                        }
75
 
                        if ( ptr->ident == ARRAY ||
76
 
                                                (ptr->ident == VARIABLE && ptr->type == STRUCT) ) {
77
 
                                /* djm pointer? */
78
 
                                lval->ptr_type = ptr->type ;
79
 
                                lval->val_type = (ptr->flags&FARPTR ? CPTR : CINT );
80
 
                                return(0) ;
81
 
                        }
82
 
                        else return(1);
83
 
                }
 
72
                lval->indirect=lval->val_type = (ptr->flags&FARPTR ? CPTR : CINT);
 
73
                ltype=lval->indirect;
 
74
            }
 
75
            if ( ptr->ident == ARRAY ||
 
76
                 (ptr->ident == VARIABLE && ptr->type == STRUCT) ) {
 
77
                /* djm pointer? */
 
78
                lval->ptr_type = ptr->type ;
 
79
                lval->val_type = (ptr->flags&FARPTR ? CPTR : CINT );
 
80
                return(0) ;
 
81
            }
 
82
            else return(1);
 
83
        }
84
84
/* djm search for local statics */
85
 
                ptr=findstc(sname);
86
 
                if (!ptr) ptr=findglb(sname);
87
 
                if ( ptr ) {
88
 
                        if ( ptr->ident != FUNCTION && ptr->ident !=FUNCTIONP ) {
89
 
                                if (ptr->ident==ENUM )
90
 
                                        error(E_UNSYMB,sname);
91
 
                                if (ptr->type==ENUM) {
92
 
                                        lval->symbol = NULL_SYM ;
93
 
                                        lval->indirect = 0 ;
94
 
                                        lval->is_const = 1;
95
 
                                        lval->const_val=ptr->size;
96
 
                                        lval->flags=0;
97
 
                                        lval->ident = VARIABLE;
98
 
                                        return(0) ;
99
 
                                }
100
 
                                lval->symbol = ptr ;
101
 
                                lval->indirect = 0 ;
102
 
                                lval->val_type = ptr->type ;
103
 
                                lval->flags = ptr->flags ;
104
 
                                lval->ident = ptr->ident;
105
 
                                lval->ptr_type = 0;
 
85
        ptr=findstc(sname);
 
86
        if (!ptr) ptr=findglb(sname);
 
87
        if ( ptr ) {
 
88
            if ( ptr->ident != FUNCTION && ptr->ident !=FUNCTIONP ) {
 
89
                if (ptr->ident==ENUM )
 
90
                    error(E_UNSYMB,sname);
 
91
                if (ptr->type==ENUM) {
 
92
                    lval->symbol = NULL_SYM ;
 
93
                    lval->indirect = 0 ;
 
94
                    lval->is_const = 1;
 
95
                    lval->const_val=ptr->size;
 
96
                    lval->flags=0;
 
97
                    lval->ident = VARIABLE;
 
98
                    return(0) ;
 
99
                }
 
100
                lval->symbol = ptr ;
 
101
                lval->indirect = 0 ;
 
102
                lval->val_type = ptr->type ;
 
103
                lval->flags = ptr->flags ;
 
104
                lval->ident = ptr->ident;
 
105
                lval->ptr_type = 0;
106
106
                                lval->storage = ptr->storage;
107
 
                                ltype=ptr->type;
108
 
                                if ( ptr->type == STRUCT )
109
 
                                        lval->tagsym = tagtab + ptr->tag_idx ;
110
 
                                if ( ptr->ident != ARRAY &&
111
 
                                                        (ptr->ident != VARIABLE || ptr->type != STRUCT) ) {
112
 
                                        if ( ptr->ident == POINTER ) {
113
 
                                          lval->ptr_type = ptr->type;
114
 
                                          lval->val_type = (ptr->flags&FARPTR ? CPTR : CINT);
115
 
                                          ltype=lval->val_type;
116
 
                                        }
117
 
                                        return(1);
118
 
                                }
 
107
                ltype=ptr->type;
 
108
                if ( ptr->type == STRUCT )
 
109
                    lval->tagsym = tagtab + ptr->tag_idx ;
 
110
                if ( ptr->ident != ARRAY &&
 
111
                     (ptr->ident != VARIABLE || ptr->type != STRUCT) ) {
 
112
                    if ( ptr->ident == POINTER ) {
 
113
                        lval->ptr_type = ptr->type;
 
114
                        lval->val_type = (ptr->flags&FARPTR ? CPTR : CINT);
 
115
                        ltype=lval->val_type;
 
116
                    }
 
117
                    return(1);
 
118
                }
119
119
/* Handle arrays... */
120
 
                                address(ptr);
 
120
                address(ptr);
121
121
/* djm sommat here about pointer types? */
122
 
                                lval->indirect = lval->ptr_type = ptr->type ;
123
 
                                lval->val_type = (ptr->flags&FARPTR ? CPTR : CINT);
124
 
                                return(0) ;
125
 
                        } else  {
126
 
                                lval->ident=FUNCTION;
 
122
                lval->indirect = lval->ptr_type = ptr->type ;
 
123
                lval->val_type = (ptr->flags&FARPTR ? CPTR : CINT);
 
124
                return(0) ;
 
125
            } else  {
 
126
                lval->ident=FUNCTION;
127
127
                        }
128
 
                }
129
 
                else {
 
128
        }
 
129
        else {
130
130
/* Check to see if we have a right bracket, if we don't assume
131
131
 * it's a function then we can break an awful lot of code, do it
132
132
 * this way and it's safer... we're not GNU after all!
133
133
 */
134
 
                        if ( rcmatch('(') ) 
135
 
                                warning(W_FUNC_NO_PROTO);
136
 
                        else {  
137
 
                                error(E_UNSYMB,sname);
 
134
            if ( rcmatch('(') ) 
 
135
                warning(W_FUNC_NO_PROTO);
 
136
            else {      
 
137
                error(E_UNSYMB,sname);
138
138
                        }
139
 
                        /* assume it's a function we haven't seen yet */
140
 
                        /* NB value set to 0 */
141
 
                        ptr = addglb(sname,FUNCTION,CINT,0,STATIK,0,0);
142
 
                        ptr->size=0;
143
 
                        ptr->prototyped=0; /* No parameters known */
144
 
                        ptr->args[0]=CalcArgValue(CINT, FUNCTION, 0);
145
 
                }
146
 
                lval->symbol = ptr ;
147
 
                lval->indirect = 0 ;
148
 
                lval->val_type = CINT ;  /* Null function, always int */
149
 
                lval->flags = 0 ;        /* Assume signed, no far */
150
 
                lval->ident=FUNCTION;
151
 
                return(0) ;
152
 
        }
153
 
        if ( constant(lval) ) {
154
 
                lval->symbol = NULL_SYM ;
155
 
                lval->indirect = 0 ;
156
 
                lval->ident = VARIABLE;
157
 
                return(0) ;
158
 
        }
159
 
        else {
160
 
                error(E_EXPRESSION);
161
 
                vconst(0);
162
 
                junk();
163
 
                return(0);
164
 
        }
 
139
            /* assume it's a function we haven't seen yet */
 
140
            /* NB value set to 0 */
 
141
            ptr = addglb(sname,FUNCTION,CINT,0,STATIK,0,0);
 
142
            ptr->size=0;
 
143
            ptr->prototyped=0; /* No parameters known */
 
144
            ptr->args[0]=CalcArgValue(CINT, FUNCTION, 0);
 
145
        }
 
146
        lval->symbol = ptr ;
 
147
        lval->indirect = 0 ;
 
148
        lval->val_type = CINT ;  /* Null function, always int */
 
149
        lval->flags = 0 ;        /* Assume signed, no far */
 
150
        lval->ident=FUNCTION;
 
151
        return(0) ;
 
152
    }
 
153
    if ( constant(lval) ) {
 
154
        lval->symbol = NULL_SYM ;
 
155
        lval->indirect = 0 ;
 
156
        lval->ident = VARIABLE;
 
157
        return(0) ;
 
158
    }
 
159
    else {
 
160
        error(E_EXPRESSION);
 
161
        vconst(0);
 
162
        junk();
 
163
        return(0);
 
164
    }
165
165
}
166
166
 
167
167
/*
240
240
void force(int t1, int t2,char sign1,char sign2,int lconst)
241
241
{
242
242
    if ( t2 == CARRY ) {
243
 
        zcarryconv();   
244
 
    }
245
 
 
246
 
 
247
 
        if(t1==DOUBLE) {
248
 
                if(t2!=DOUBLE) {
249
 
                        DoDoubConv(t2,sign2);
250
 
                }
251
 
        }
252
 
        else {
253
 
           if (t2==DOUBLE ) {
254
 
                convdoub2int();
255
 
                return;
256
 
           }
257
 
 
258
 
        }
 
243
        zcarryconv();   
 
244
    }
 
245
 
 
246
 
 
247
    if(t1==DOUBLE) {
 
248
        if(t2!=DOUBLE) {
 
249
            DoDoubConv(t2,sign2);
 
250
        }
 
251
    }
 
252
    else {
 
253
        if (t2==DOUBLE ) {
 
254
            convdoub2int();
 
255
            return;
 
256
        }
 
257
 
 
258
    }
259
259
/* t2 =source, t1=dest */
260
260
/* int to long, if signed, do sign, if not ld de,0 */
261
261
/* Check to see if constant or not... */
262
 
        if(t1==LONG) {
263
 
                if (t2!=LONG && (!lconst)) {
264
 
                      if (sign2==NO && sign1==NO && t2 != CARRY) convSint2long();
265
 
                      else convUint2long();
266
 
                }
267
 
                return;
 
262
    if(t1==LONG) {
 
263
        if (t2!=LONG && (!lconst)) {
 
264
            if (sign2==NO && sign1==NO && t2 != CARRY) convSint2long();
 
265
            else convUint2long();
268
266
        }
 
267
        return;
 
268
    }
269
269
/* Converting long to int, not needed, if it don't fit tough!! */
270
270
 
271
271
/* Converting between pointer types..far and near */
272
 
        if (t1==CPTR && t2==CINT) convUint2long();
273
 
        else if (t2==CPTR && t1==CINT) warning(W_FARNR);
 
272
    if (t1==CPTR && t2==CINT) convUint2long();
 
273
    else if (t2==CPTR && t1==CINT) warning(W_FARNR);
274
274
 
275
275
        /* Char conversion */
276
276
        if ( t1 == CCHAR && sign2 == NO && !lconst) {
702
702
 */
703
703
void addconst(int val,int opr, char zfar)
704
704
{
705
 
        LVALUE  lval;
 
705
    LVALUE  lval;
706
706
/*        if (!opr) lval->val_type=ltype; */
707
 
        if ( (ltype == LONG && (!opr) ) || (ltype==CPTR && (!opr)) ) {
708
 
                lval.val_type=LONG;
709
 
                lpush();
710
 
                vlongconst(val);
711
 
                zadd(&lval);
712
 
        }
713
 
        else 
 
707
    if ( (ltype == LONG && (!opr) ) || (ltype==CPTR && (!opr)) ) {
 
708
        lval.val_type=LONG;
 
709
        lpush();
 
710
        vlongconst(val);
 
711
        zadd(&lval);
 
712
    } else {
 
713
        lval.val_type = CINT;
714
714
        switch(val) {
715
715
 
716
 
                case -3 :       dec(&lval) ;
717
 
                case -2 :       dec(&lval) ;
718
 
                case -1 :       dec(&lval) ;
719
 
                case  0 :       break ;
720
 
 
721
 
                case  3 :       inc(&lval) ;
722
 
                case  2 :       inc(&lval) ;
723
 
                case  1 :       inc(&lval) ;
724
 
                                        break ;
725
 
 
726
 
                default :       
727
 
                                addbchl(val);
 
716
        case -3 :       dec(&lval) ;
 
717
        case -2 :       dec(&lval) ;
 
718
        case -1 :       dec(&lval) ;
 
719
        case  0 :       break ;
 
720
 
 
721
        case  3 :       inc(&lval) ;
 
722
        case  2 :       inc(&lval) ;
 
723
        case  1 :       inc(&lval) ;
 
724
            break ;
 
725
 
 
726
        default :       
 
727
            addbchl(val);
728
728
        }
 
729
    }
729
730
}
730
731
 
731
732
/*
736
737
int docast(LVALUE *lval,char df)
737
738
{
738
739
        SYMBOL  *ptr;
739
 
        char    temp_type;
740
 
        int     itag;
741
 
        char    nam[20];
 
740
    char    temp_type;
 
741
    int     itag;
 
742
    char    nam[20];
742
743
 
743
744
 
744
745
 
746
747
        if      (lval->level != lval->castlevel  ) return 0;
747
748
 
748
749
 
749
 
        if ( lval->c_id == VARIABLE ) {
 
750
    if ( lval->c_id == VARIABLE ) {
750
751
/*
751
752
 * Straight forward variable conversion now..
752
753
 */
753
 
                if (df) force(lval->c_vtype,lval->val_type,lval->c_flags&UNSIGNED,lval->flags&UNSIGNED,0);
754
 
                lval->val_type=lval->c_vtype;
755
 
                lval->ptr_type=0;
756
 
                lval->ident=VARIABLE;
757
 
                lval->flags= ( (lval->flags&FARACC) | (lval->c_flags&UNSIGNED) );
 
754
        if (df) force(lval->c_vtype,lval->val_type,lval->c_flags&UNSIGNED,lval->flags&UNSIGNED,0);
 
755
        lval->val_type=lval->c_vtype;
 
756
        lval->ptr_type=0;
 
757
        lval->ident=VARIABLE;
 
758
        lval->flags= ( (lval->flags&FARACC) | (lval->c_flags&UNSIGNED) );
758
759
                lval->c_id=0; lval->c_vtype=0; lval->c_flags=0;
759
 
                return(0);
760
 
        } 
 
760
        lval->is_const = 0;
 
761
        return(0);
 
762
    } 
761
763
 
762
 
        if  ( lval->c_id == POINTER || lval->c_id==PTR_TO_FN ) {
763
 
                switch(lval->c_vtype) {
764
 
                   case STRUCT:
 
764
    if  ( lval->c_id == POINTER || lval->c_id==PTR_TO_FN ) {
 
765
        switch(lval->c_vtype) {
 
766
        case STRUCT:
765
767
/*
766
768
 * Casting a structure - has to be a pointer...
767
769
 */
768
 
                        lval->tagsym=lval->c_tag;       /* Copy tag symbol over */
769
 
                        lval->ptr_type=STRUCT;
770
 
                        temp_type=( ( lval->c_flags&FARPTR ) ? CPTR : CINT );
771
 
                        if (df) force(temp_type,lval->val_type,0,0,0);
772
 
                        lval->val_type=temp_type;
773
 
                        lval->flags=( (lval->flags&FARACC) | lval->c_flags );
 
770
            lval->tagsym=lval->c_tag;       /* Copy tag symbol over */
 
771
            lval->ptr_type=STRUCT;
 
772
            temp_type=( ( lval->c_flags&FARPTR ) ? CPTR : CINT );
 
773
            if (df) force(temp_type,lval->val_type,0,0,0);
 
774
            lval->val_type=temp_type;
 
775
            lval->flags=( (lval->flags&FARACC) | lval->c_flags );
774
776
                        if (df) {lval->c_id=0; lval->c_vtype=0; lval->c_flags=0;}
775
 
                        return(1);
 
777
            return(1);
776
778
 
777
779
/* All other simple pointers.. */
778
 
                  default:
779
 
                  debug(DBG_CAST2,"Converting %d to %d",lval->ptr_type,lval->c_vtype);
780
 
                        lval->ptr_type=lval->c_vtype;
781
 
                        lval->symbol=dummy_sym[(int)lval->c_vtype];
782
 
                        temp_type=( ( lval->c_flags&FARPTR ) ? CPTR : CINT );
783
 
                        if (df) force(temp_type,lval->val_type,0,0,0);
784
 
                        lval->val_type=temp_type;
785
 
                        lval->flags=( (lval->flags&FARACC) | lval->c_flags );
786
 
                        lval->ident=POINTER;
 
780
        default:
 
781
            debug(DBG_CAST2,"Converting %d to %d",lval->ptr_type,lval->c_vtype);
 
782
            lval->ptr_type=lval->c_vtype;
 
783
            lval->symbol=dummy_sym[(int)lval->c_vtype];
 
784
            temp_type=( ( lval->c_flags&FARPTR ) ? CPTR : CINT );
 
785
            if (df) force(temp_type,lval->val_type,0,0,0);
 
786
            lval->val_type=temp_type;
 
787
            lval->flags=( (lval->flags&FARACC) | lval->c_flags );
 
788
            lval->ident=POINTER;
787
789
                        if (df) {lval->c_id=0; lval->c_vtype=0; lval->c_flags=0; }
788
 
                        return(1);
789
 
                }
 
790
            return(1);
790
791
        }
 
792
    }
791
793
/* Now we deal with pointers to pointers and pointers to functions 
792
794
 * returning pointers - to do this, we will define dummy symbols in
793
795
 * the local symbol table so that they do what we want them to do!
794
796
 */
795
 
        sprintf(nam,"0dptr%d",(int)locptr);
796
 
        temp_type = ( (lval->c_flags&FARPTR) ? CPTR : CINT );
797
 
        itag=0;
798
 
        if ( lval->c_tag) 
 
797
    sprintf(nam,"0dptr%d",(int)locptr);
 
798
    temp_type = ( (lval->c_flags&FARPTR) ? CPTR : CINT );
 
799
    itag=0;
 
800
    if ( lval->c_tag) 
799
801
            itag=lval->c_tag-tagtab;
800
802
        ptr=lval->symbol;
801
 
        lval->symbol = addloc(nam,POINTER,temp_type,dummy_idx(lval->c_vtype,lval->c_tag),itag);
 
803
    lval->symbol = addloc(nam,POINTER,temp_type,dummy_idx(lval->c_vtype,lval->c_tag),itag);
802
804
        lval->symbol->offset.p=ptr;
803
 
        if (df) 
 
805
    if (df) 
804
806
            force(temp_type,lval->val_type,0,0,0);
805
 
        lval->val_type=temp_type;
806
 
        lval->flags=( (lval->flags&FARACC) | lval->c_flags );
 
807
    lval->val_type=temp_type;
 
808
    lval->flags=( (lval->flags&FARACC) | lval->c_flags );
807
809
        if (df) {
808
810
            lval->c_id=0; lval->c_vtype=0; lval->c_flags=0;
809
811
        }
810
 
        return(1);
 
812
    return(1);
811
813
 
812
814
}
813
815