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

Subversion Repositories raptor64

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

Go to most recent revision | 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
{
43
        dseg();                 /* initialize into data segment */
44
    nl();                   /* start a new line in object */
45
        if(sp->storage_class == sc_static)
46
                put_label(sp->value.i);
47
        else
48
                gen_strlab(sp->name);
49
        if( lastst != assign) {
50
                genstorage(sp->tp->size);
51
        }
52
        else {
53
                NextToken();
54
                InitializeType(sp->tp);
55
        }
56
    endinit();
57
}
58
 
59
int InitializeType(TYP *tp)
60
{
61
        int nbytes;
62
 
63
    switch(tp->type) {
64
        case bt_byte:
65
                        nbytes = initbyte();
66
                        break;
67
    case bt_char:
68
    case bt_enum:
69
            nbytes = initchar();
70
            break;
71
    case bt_short:
72
            nbytes = initshort();
73
            break;
74
    case bt_pointer:
75
            if( tp->val_flag)
76
                nbytes = InitializeArray(tp);
77
            else
78
                nbytes = InitializePointer();
79
            break;
80
    case bt_long:
81
            nbytes = initlong();
82
            break;
83
    case bt_struct:
84
            nbytes = InitializeStructure(tp);
85
            break;
86
    default:
87
        error(ERR_NOINIT);
88
        nbytes = 0;
89
    }
90
    return nbytes;
91
}
92
 
93
int InitializeArray(TYP *tp)
94
{
95
        int nbytes;
96
    char *p;
97
 
98
    nbytes = 0;
99
    if( lastst == begin) {
100
        NextToken();               /* skip past the brace */
101
        while(lastst != end) {
102
            nbytes += InitializeType(tp->btp);
103
            if( lastst == comma)
104
                NextToken();
105
            else if( lastst != end)
106
                error(ERR_PUNCT);
107
        }
108
        NextToken();               /* skip closing brace */
109
    }
110
    else if( lastst == sconst && tp->btp->type == bt_char) {
111
        nbytes = strlen(laststr) * 2 + 2;
112
        p = laststr;
113
        while( *p )
114
            GenerateChar(*p++);
115
        GenerateChar(0);
116
        NextToken();
117
    }
118
    else if( lastst != semicolon)
119
        error(ERR_ILLINIT);
120
    if( nbytes < tp->size) {
121
        genstorage( tp->size - nbytes);
122
        nbytes = tp->size;
123
    }
124
    else if( tp->size != 0 && nbytes > tp->size)
125
        error(ERR_INITSIZE);    /* too many initializers */
126
    return nbytes;
127
}
128
 
129
int InitializeStructure(TYP *tp)
130
{
131
        SYM *sp;
132
    int nbytes;
133
 
134
    needpunc(begin);
135
    nbytes = 0;
136
    sp = tp->lst.head;      /* start at top of symbol table */
137
    while(sp != 0) {
138
                while(nbytes < sp->value.i) {     /* align properly */
139
//                    nbytes += GenerateByte(0);
140
            GenerateByte(0);
141
//                    nbytes++;
142
                }
143
        nbytes += InitializeType(sp->tp);
144
        if( lastst == comma)
145
            NextToken();
146
        else if(lastst == end)
147
            break;
148
        else
149
            error(ERR_PUNCT);
150
        sp = sp->next;
151
    }
152
    if( nbytes < tp->size)
153
        genstorage( tp->size - nbytes);
154
    needpunc(end);
155
    return tp->size;
156
}
157
 
158
int initbyte()
159
{
160
        GenerateByte(GetIntegerExpression());
161
    return 1;
162
}
163
 
164
int initchar()
165
{
166
        GenerateChar(GetIntegerExpression());
167
    return 2;
168
}
169
 
170
int initshort()
171
{
172
        GenerateWord(GetIntegerExpression());
173
    return 4;
174
}
175
 
176
int initlong()
177
{
178
        GenerateLong(GetIntegerExpression());
179
    return 8;
180
}
181
 
182
int InitializePointer()
183
{
184
        SYM *sp;
185
 
186
    if(lastst == and) {     /* address of a variable */
187
        NextToken();
188
        if( lastst != id)
189
            error(ERR_IDEXPECT);
190
                else if( (sp = gsearch(lastid)) == NULL)
191
            error(ERR_UNDEFINED);
192
        else {
193
            NextToken();
194
            if( lastst == plus || lastst == minus)
195
                GenerateReference(sp,GetIntegerExpression());
196
            else
197
                GenerateReference(sp,0);
198
            if( sp->storage_class == sc_auto)
199
                    error(ERR_NOINIT);
200
        }
201
    }
202
    else if(lastst == sconst) {
203
        GenerateLabelReference(stringlit(laststr));
204
        NextToken();
205
    }
206
    else
207
        GenerateLong(GetIntegerExpression());
208
    endinit();
209
    return 8;       /* pointers are 4 bytes long */
210
}
211
 
212
void endinit()
213
{
214
        if( lastst != comma && lastst != semicolon && lastst != end) {
215
                error(ERR_PUNCT);
216
                while( lastst != comma && lastst != semicolon && lastst != end)
217
            NextToken();
218
    }
219
}

powered by: WebSVN 2.1.0

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