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

Subversion Repositories thor

[/] [thor/] [trunk/] [FT64v5/] [software/] [CC64/] [source/] [Initializers.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
extern int catchdecl;
29
extern void genstorageskip(int nbytes);
30
 
31
void endinit();
32
extern int curseg;
33
static char glbl1[500];
34
static char glbl2[500];
35
bool hasPointer;
36
bool firstPrim;
37
std::streampos patchpoint;
38
 
39
static void pad(char *p, int n)
40
{
41
        int nn;
42
 
43
        nn = strlen(p);
44
        while (nn < n) {
45
                p[nn] = ' ';
46
                nn++;
47
        }
48
        p[nn] = '\n';
49
        p[nn + 1] = '\0';
50
}
51
 
52
void doinit(SYM *sp)
53
{
54
        static bool first = true;
55
        char lbl[200];
56
  int algn;
57
  enum e_sg oseg;
58
  char buf[500];
59
  std::streampos endpoint;
60
 
61
  hasPointer = false;
62
  if (first) {
63
          firstPrim = true;
64
          first = false;
65
  }
66
 
67
  oseg = noseg;
68
        lbl[0] = 0;
69
        // Initialize constants into read-only data segment. Constants may be placed
70
        // in ROM along with code.
71
        if (sp->isConst) {
72
    oseg = rodataseg;
73
  }
74
        if (sp->storage_class == sc_thread) {
75
        if (sp->tp->type==bt_struct || sp->tp->type==bt_union)
76
           algn = imax(sp->tp->alignment,8);
77
        else if (sp->tp->type==bt_pointer && sp->tp->val_flag)
78
           algn = imax(sp->tp->GetBtp()->alignment,8);
79
        else
80
            algn = 2;
81
                seg(oseg==noseg ? tlsseg : oseg,algn);
82
                nl();
83
        }
84
        else if (sp->storage_class == sc_static || lastst==assign) {
85
        if (sp->tp->type==bt_struct || sp->tp->type==bt_union)
86
           algn = imax(sp->tp->alignment,8);
87
        else if (sp->tp->type==bt_pointer && sp->tp->val_flag)
88
           algn = imax(sp->tp->GetBtp()->alignment,8);
89
        else
90
            algn = 2;
91
                seg(oseg==noseg ? dataseg : oseg,algn);          /* initialize into data segment */
92
                nl();                   /* start a new line in object */
93
        }
94
        else {
95
        if (sp->tp->type==bt_struct || sp->tp->type==bt_union)
96
           algn = imax(sp->tp->alignment,8);
97
        else if (sp->tp->type==bt_pointer && sp->tp->val_flag)
98
           algn = imax(sp->tp->GetBtp()->alignment,8);
99
        else
100
            algn = 2;
101
                seg(oseg==noseg ? bssseg : oseg,algn);            /* initialize into data segment */
102
                nl();                   /* start a new line in object */
103
        }
104
 
105
        if (sp->storage_class == sc_static || sp->storage_class == sc_thread) {
106
                //strcpy_s(glbl, sizeof(glbl), gen_label((int)sp->value.i, (char *)sp->name->c_str(), GetNamespace(), 'D'));
107
                if (sp->tp->IsSkippable()) {
108
                        patchpoint = ofs.tellp();
109
                        sprintf_s(buf, sizeof(buf), "\talign\t8\n\tdw\t$FFF0200000000001\n");
110
                        ofs.printf(buf);
111
                }
112
                sp->realname = my_strdup(put_label((int)sp->value.i, (char *)sp->name->c_str(), GetNamespace(), 'D'));
113
                strcpy_s(glbl2, sizeof(glbl2), gen_label((int)sp->value.i, (char *)sp->name->c_str(), GetNamespace(), 'D'));
114
        }
115
        else {
116
                if (sp->storage_class == sc_global) {
117
                        strcpy_s(lbl, sizeof(lbl), "public ");
118
                        if (curseg==dataseg)
119
                                strcat_s(lbl, sizeof(lbl), "data ");
120
                        else if (curseg==bssseg)
121
                                strcat_s(lbl, sizeof(lbl), "bss ");
122
                        else if (curseg==tlsseg)
123
                                strcat_s(lbl, sizeof(lbl), "tls ");
124
                }
125
                strcat_s(lbl, sizeof(lbl), sp->name->c_str());
126
                if (sp->tp->IsSkippable()) {
127
                        patchpoint = ofs.tellp();
128
                        sprintf_s(buf, sizeof(buf), "\talign\t8\n\tdw\t$FFF0200000000001\n");
129
                        ofs.printf(buf);
130
                }
131
                strcpy_s(glbl2, sizeof(glbl2), sp->name->c_str());
132
                gen_strlab(lbl);
133
        }
134
        if (lastst == kw_firstcall) {
135
        GenerateByte(1);
136
        return;
137
    }
138
        else if( lastst != assign) {
139
                hasPointer = sp->tp->FindPointer();
140
                genstorage(sp->tp->size);
141
        }
142
        else {
143
                NextToken();
144
                hasPointer = sp->tp->FindPointer();
145
                sp->tp->Initialize();
146
        }
147
        if (!hasPointer && sp->tp->IsSkippable()) {
148
                endpoint = ofs.tellp();
149
                ofs.seekp(patchpoint);
150
                sprintf_s(buf, sizeof(buf), "\talign\t8\n\tdw\t$%I64X\n", ((genst_cumulative + 7LL) >> 3LL) | 0xFFF0200000000000LL);
151
                ofs.printf(buf);
152
                ofs.seekp(endpoint);
153
                genst_cumulative = 0;
154
        }
155
        else if (sp->tp->IsSkippable()) {
156
                endpoint = ofs.tellp();
157
                ofs.seekp(patchpoint);
158
                sprintf_s(buf, sizeof(buf), "\talign\t8\n\t  \t                 \n");
159
                ofs.printf(buf);
160
                ofs.seekp(endpoint);
161
                genst_cumulative = 0;
162
        }
163
    endinit();
164
        if (sp->storage_class == sc_global)
165
                ofs.printf("\nendpublic\n");
166
}
167
 
168
 
169
// Patch the last gc_skip
170
 
171
void doInitCleanup()
172
{
173
        std::streampos endpoint;
174
        char buf[500];
175
 
176
        if (genst_cumulative && !hasPointer) {
177
                endpoint = ofs.tellp();
178
                if (patchpoint > 0) {
179
                        ofs.seekp(patchpoint);
180
                        sprintf_s(buf, sizeof(buf), "\talign\t8\n\tdw\t$%I64X\n", ((genst_cumulative + 7LL) >> 3LL) | 0xFFF0200000000000LL);
181
                        ofs.printf(buf);
182
                        ofs.seekp(endpoint);
183
                }
184
                genst_cumulative = 0;
185
        }
186
}
187
 
188
int64_t initbyte()
189
{
190
        GenerateByte((int)GetIntegerExpression((ENODE **)NULL));
191
    return (1LL);
192
}
193
 
194
int64_t initchar()
195
{
196
        GenerateChar((int)GetIntegerExpression((ENODE **)NULL));
197
    return (2LL);
198
}
199
 
200
int64_t initshort()
201
{
202
        GenerateWord((int)GetIntegerExpression((ENODE **)NULL));
203
    return (4LL);
204
}
205
 
206
int64_t initlong()
207
{
208
        GenerateLong(GetIntegerExpression((ENODE **)NULL));
209
    return (8LL);
210
}
211
 
212
int64_t initquad()
213
{
214
        GenerateQuad(GetFloatExpression((ENODE **)NULL));
215
        return (16LL);
216
}
217
 
218
int64_t initfloat()
219
{
220
        GenerateFloat(GetFloatExpression((ENODE **)NULL));
221
        return (8LL);
222
}
223
 
224
int64_t inittriple()
225
{
226
        GenerateQuad(GetFloatExpression((ENODE **)NULL));
227
        return (12LL);
228
}
229
 
230
int64_t InitializePointer()
231
{
232
        SYM *sp;
233
        ENODE *n = nullptr;
234
        int64_t lng;
235
 
236
    if(lastst == bitandd) {     /* address of a variable */
237
        NextToken();
238
        if( lastst != id)
239
            error(ERR_IDEXPECT);
240
                else if( (sp = gsearch(lastid)) == NULL)
241
            error(ERR_UNDEFINED);
242
        else {
243
            NextToken();
244
            if( lastst == plus || lastst == minus)
245
                GenerateReference(sp,(int)GetIntegerExpression((ENODE **)NULL));
246
            else
247
                GenerateReference(sp,0);
248
            if( sp->storage_class == sc_auto)
249
                    error(ERR_NOINIT);
250
        }
251
    }
252
    else if(lastst == sconst) {
253
        GenerateLabelReference(stringlit(laststr));
254
        NextToken();
255
    }
256
        else if (lastst == rconst) {
257
        GenerateLabelReference(quadlit(&rval128));
258
        NextToken();
259
        }
260
        //else if (lastst == id) {
261
        //      sp = gsearch(lastid);
262
        //      if (sp->tp->type == bt_func || sp->tp->type == bt_ifunc) {
263
        //              NextToken();
264
        //              GenerateReference(sp,0);
265
        //      }
266
        //      else
267
        //              GenerateLong(GetIntegerExpression(NULL));
268
        //}
269
        else {
270
                lng = GetIntegerExpression(&n);
271
                if (n && n->nodetype == en_cnacon) {
272
                        if (n->sp->length()) {
273
                                sp = gsearch(*n->sp);
274
                                GenerateReference(sp,0);
275
                        }
276
                        else
277
                                GenerateLong(lng);
278
                }
279
                else {
280
                        GenerateLong((lng & 0xFFFFFFFFFFFLL)|0xFFF0100000000000LL);
281
        }
282
        }
283
    endinit();
284
    return 8;       /* pointers are 8 bytes long */
285
}
286
 
287
void endinit()
288
{
289
    if (catchdecl) {
290
        if (lastst!=closepa)
291
                        error(ERR_PUNCT);
292
    }
293
    else if( lastst != comma && lastst != semicolon && lastst != end) {
294
                error(ERR_PUNCT);
295
                while( lastst != comma && lastst != semicolon && lastst != end)
296
            NextToken();
297
    }
298
}

powered by: WebSVN 2.1.0

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