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

Subversion Repositories raptor64

[/] [raptor64/] [trunk/] [software/] [c64/] [source/] [Initializers.c] - Blame information for rev 51

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 37 robfinch
#include <stdio.h>
2
#include <string.h>
3
#include "c.h"
4
#include "expr.h"
5
#include "Statement.h"
6
#include "gen.h"
7
#include "cglbdec.h"
8
/*
9
 *      68000 C compiler
10
 *
11
 *      Copyright 1984, 1985, 1986 Matthew Brandt.
12
 *  all commercial rights reserved.
13
 *
14
 *      This compiler is intended as an instructive tool for personal use. Any
15
 *      use for profit without the written consent of the author is prohibited.
16
 *
17
 *      This compiler may be distributed freely for non-commercial use as long
18
 *      as this notice stays intact. Please forward any enhancements or questions
19
 *      to:
20
 *
21
 *              Matthew Brandt
22
 *              Box 920337
23
 *              Norcross, Ga 30092
24
 */
25
/*******************************************************
26
        Modified to support Raptor64 'C64' language
27
        by Robert Finch
28
        robfinch@opencores.org
29
*******************************************************/
30
 
31
int InitializeType(TYP *tp);
32
int InitializeStructure(TYP *tp);
33
int initbyte();
34
int initchar();
35
int initshort();
36
int initlong();
37
int InitializePointer();
38
void endinit();
39
int InitializeArray(TYP *tp);
40
 
41
void doinit(SYM *sp)
42 51 robfinch
{
43
        if (sp->storage_class == sc_static || lastst==assign) {
44
                seg(dataseg);          /* initialize into data segment */
45
                nl();                   /* start a new line in object */
46
        }
47
        else {
48
                seg(bssseg);            /* initialize into data segment */
49
                nl();                   /* start a new line in object */
50
        }
51
        if(sp->storage_class == sc_static) {
52 37 robfinch
                put_label(sp->value.i);
53 51 robfinch
        }
54
        else {
55 37 robfinch
                gen_strlab(sp->name);
56 51 robfinch
        }
57 37 robfinch
        if( lastst != assign) {
58
                genstorage(sp->tp->size);
59
        }
60
        else {
61
                NextToken();
62
                InitializeType(sp->tp);
63
        }
64
    endinit();
65
}
66
 
67
int InitializeType(TYP *tp)
68
{
69
        int nbytes;
70
 
71
    switch(tp->type) {
72
        case bt_byte:
73
                        nbytes = initbyte();
74
                        break;
75
    case bt_char:
76
    case bt_enum:
77
            nbytes = initchar();
78
            break;
79
    case bt_short:
80
            nbytes = initshort();
81
            break;
82
    case bt_pointer:
83
            if( tp->val_flag)
84
                nbytes = InitializeArray(tp);
85
            else
86
                nbytes = InitializePointer();
87
            break;
88
    case bt_long:
89
            nbytes = initlong();
90
            break;
91
    case bt_struct:
92
            nbytes = InitializeStructure(tp);
93
            break;
94
    default:
95
        error(ERR_NOINIT);
96
        nbytes = 0;
97
    }
98
    return nbytes;
99
}
100
 
101
int InitializeArray(TYP *tp)
102
{
103
        int nbytes;
104
    char *p;
105
 
106
    nbytes = 0;
107
    if( lastst == begin) {
108
        NextToken();               /* skip past the brace */
109
        while(lastst != end) {
110
            nbytes += InitializeType(tp->btp);
111
            if( lastst == comma)
112
                NextToken();
113
            else if( lastst != end)
114
                error(ERR_PUNCT);
115
        }
116
        NextToken();               /* skip closing brace */
117
    }
118
    else if( lastst == sconst && tp->btp->type == bt_char) {
119
        nbytes = strlen(laststr) * 2 + 2;
120
        p = laststr;
121
        while( *p )
122
            GenerateChar(*p++);
123
        GenerateChar(0);
124
        NextToken();
125
    }
126
    else if( lastst != semicolon)
127
        error(ERR_ILLINIT);
128
    if( nbytes < tp->size) {
129
        genstorage( tp->size - nbytes);
130
        nbytes = tp->size;
131
    }
132
    else if( tp->size != 0 && nbytes > tp->size)
133
        error(ERR_INITSIZE);    /* too many initializers */
134
    return nbytes;
135
}
136
 
137
int InitializeStructure(TYP *tp)
138
{
139
        SYM *sp;
140
    int nbytes;
141
 
142
    needpunc(begin);
143
    nbytes = 0;
144
    sp = tp->lst.head;      /* start at top of symbol table */
145
    while(sp != 0) {
146
                while(nbytes < sp->value.i) {     /* align properly */
147
//                    nbytes += GenerateByte(0);
148
            GenerateByte(0);
149
//                    nbytes++;
150
                }
151
        nbytes += InitializeType(sp->tp);
152
        if( lastst == comma)
153
            NextToken();
154
        else if(lastst == end)
155
            break;
156
        else
157
            error(ERR_PUNCT);
158
        sp = sp->next;
159
    }
160
    if( nbytes < tp->size)
161
        genstorage( tp->size - nbytes);
162
    needpunc(end);
163
    return tp->size;
164
}
165
 
166
int initbyte()
167
{
168
        GenerateByte(GetIntegerExpression());
169
    return 1;
170
}
171
 
172
int initchar()
173
{
174
        GenerateChar(GetIntegerExpression());
175
    return 2;
176
}
177
 
178
int initshort()
179
{
180
        GenerateWord(GetIntegerExpression());
181
    return 4;
182
}
183
 
184
int initlong()
185
{
186
        GenerateLong(GetIntegerExpression());
187
    return 8;
188
}
189
 
190
int InitializePointer()
191
{
192
        SYM *sp;
193
 
194
    if(lastst == and) {     /* address of a variable */
195
        NextToken();
196
        if( lastst != id)
197
            error(ERR_IDEXPECT);
198
                else if( (sp = gsearch(lastid)) == NULL)
199
            error(ERR_UNDEFINED);
200
        else {
201
            NextToken();
202
            if( lastst == plus || lastst == minus)
203
                GenerateReference(sp,GetIntegerExpression());
204
            else
205
                GenerateReference(sp,0);
206
            if( sp->storage_class == sc_auto)
207
                    error(ERR_NOINIT);
208
        }
209
    }
210
    else if(lastst == sconst) {
211
        GenerateLabelReference(stringlit(laststr));
212
        NextToken();
213
    }
214
    else
215
        GenerateLong(GetIntegerExpression());
216
    endinit();
217
    return 8;       /* pointers are 4 bytes long */
218
}
219
 
220
void endinit()
221
{
222
        if( lastst != comma && lastst != semicolon && lastst != end) {
223
                error(ERR_PUNCT);
224
                while( lastst != comma && lastst != semicolon && lastst != end)
225
            NextToken();
226
    }
227
}

powered by: WebSVN 2.1.0

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