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

Subversion Repositories thor

[/] [thor/] [trunk/] [FT64v5/] [software/] [CC64/] [source/] [Compiler.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 lstackptr;
29
extern int options(char *);
30
extern int openfiles(char *);
31
extern void summary();
32
extern void ParseGlobalDeclarations();
33
extern void makename(char *s, char *e);
34
extern char *errtext(int errnum);
35
extern std::string *classname;
36
extern void doInitCleanup();
37
 
38
Compiler::Compiler()
39
{
40
}
41
 
42
int Compiler::main2(int argc, char **argv)
43
{
44
        uctran_off = 0;
45
        optimize =1;
46
        exceptions=1;
47
        dfs.printf("c64 starting...\r\n");
48
        while(--argc) {
49
        if( **++argv == '-')
50
            options(*argv);
51
                else {
52
                        if (PreprocessFile(*argv) == -1)
53
                                break;
54
                        if( openfiles(*argv)) {
55
                                lineno = 0;
56
                                initsym();
57
                                lstackptr = 0;
58
                                lastst = 0;
59
                                NextToken();
60
                                compile();
61
                                summary();
62
//                              MBlk::ReleaseAll();
63
//                              ReleaseGlobalMemory();
64
                                CloseFiles();
65
                        }
66
        }
67
    }
68
        //getchar();
69
        return 0;
70
}
71
 
72
// Builds the debugging log as an XML document
73
//
74
void Compiler::compile()
75
{
76
        GlobalDeclaration *gd;
77
 
78
        dfs.printf("<compile>\n");
79
        genst_cumulative = 0;
80
        typenum = 1;
81
        symnum = 257;
82
        pass = 1;
83
        classname = nullptr;
84
        //pCSETable = new CSETable;
85
        //pCSETable->Clear();
86
        ZeroMemory(&gsyms[0],sizeof(gsyms));
87
        ZeroMemory(&defsyms,sizeof(defsyms));
88
        ZeroMemory(&tagtable,sizeof(tagtable));
89
        ZeroMemory(&symbolTable,sizeof(symbolTable));
90
        ZeroMemory(&typeTable,sizeof(typeTable));
91
        ZeroMemory(&functionTable, sizeof(functionTable));
92
        AddStandardTypes();
93
 
94
        RTFClasses::Random::srand((RANDOM_TYPE)time(NULL));
95
        decls = GlobalDeclaration::Make();
96
        gd = decls;
97
        lastst = tk_nop;
98
 
99
        funcnum = 0;
100
        AddBuiltinFunctions();
101
 
102
        getch();
103
        lstackptr = 0;
104
        lastst = 0;
105
        NextToken();
106
        try {
107
                while(lastst != my_eof)
108
                {
109
                        if (gd==nullptr)
110
                                break;
111
                        dfs.printf("<Parsing GlobalDecl>\n");
112
                        gd->Parse();
113
                        dfs.printf("</Parsing GlobalDecl>\n");
114
                        if( lastst != my_eof) {
115
                                NextToken();
116
                                gd->next = (Declaration *)GlobalDeclaration::Make();
117
                                gd = (GlobalDeclaration*)gd->next;
118
                        }
119
                }
120
                doInitCleanup();
121
                dfs.printf("</compile>\n");
122
        }
123
        catch (C64PException * ex) {
124
                dfs.printf(errtext(ex->errnum));
125
                dfs.printf("</compile>\n");
126
        }
127
        dumplits();
128
}
129
 
130
int Compiler::PreprocessFile(char *nm)
131
{
132
        static char outname[1000];
133
        static char sysbuf[500];
134
 
135
        strcpy_s(outname, sizeof(outname), nm);
136
        makename(outname,".fpp");
137
        sprintf_s(sysbuf, sizeof(sysbuf), "fpp -b %s %s", nm, outname);
138
        return system(sysbuf);
139
}
140
 
141
void Compiler::CloseFiles()
142
{
143
        lfs.close();
144
        ofs.close();
145
        dfs.close();
146
        ifs->close();
147
}
148
 
149
void Compiler::AddStandardTypes()
150
{
151
        TYP *p, *pchar, *pint;
152
 
153
        p = TYP::Make(bt_long,8);
154
        stdint = *p;
155
        pint = p;
156
        p->precision = 64;
157
 
158
        p = TYP::Make(bt_long,8);
159
        p->isUnsigned = true;
160
        p->precision = 64;
161
        stduint = *p;
162
 
163
        p = TYP::Make(bt_long,8);
164
        p->precision = 64;
165
        stdlong = *p;
166
 
167
        p = TYP::Make(bt_long,8);
168
        p->isUnsigned = true;
169
        p->precision = 64;
170
        stdulong = *p;
171
 
172
        p = TYP::Make(bt_short,4);
173
        p->precision = 32;
174
        stdshort = *p;
175
 
176
        p = TYP::Make(bt_short,4);
177
        p->isUnsigned = true;
178
        p->precision = 32;
179
        stdushort = *p;
180
 
181
        p = TYP::Make(bt_char,2);
182
        stdchar = *p;
183
        p->precision = 16;
184
        pchar = p;
185
 
186
        p = TYP::Make(bt_char,2);
187
        p->isUnsigned = true;
188
        p->precision = 16;
189
        stduchar = *p;
190
 
191
        p = TYP::Make(bt_byte,1);
192
        stdbyte = *p;
193
        p->precision = 8;
194
        pchar = p;
195
 
196
        p = TYP::Make(bt_ubyte,1);
197
        p->isUnsigned = true;
198
        p->precision = 8;
199
        stdubyte = *p;
200
 
201
        p = allocTYP();
202
        p->type = bt_pointer;
203
        p->typeno = bt_pointer;
204
        p->val_flag = 1;
205
        p->size = 8;
206
        p->btp = pchar->GetIndex();
207
        p->bit_width = -1;
208
        p->precision = 64;
209
        p->isUnsigned = true;
210
        stdstring = *p;
211
 
212
        p = allocTYP();
213
        p->type = bt_double;
214
        p->typeno = bt_double;
215
        p->size = 8;
216
        p->bit_width = -1;
217
        p->precision = 64;
218
        stddbl = *p;
219
        stddouble = *p;
220
 
221
        p = allocTYP();
222
        p->type = bt_triple;
223
        p->typeno = bt_triple;
224
        p->size = 12;
225
        p->bit_width = -1;
226
        p->precision = 96;
227
        stdtriple = *p;
228
 
229
        p = allocTYP();
230
        p->type = bt_quad;
231
        p->typeno = bt_quad;
232
        p->size = 8;
233
        p->bit_width = -1;
234
        p->precision = 128;
235
        stdquad = *p;
236
 
237
        p = allocTYP();
238
        p->type = bt_float;
239
        p->typeno = bt_float;
240
        p->size = 8;
241
        p->bit_width = -1;
242
        p->precision = 64;
243
        stdflt = *p;
244
 
245
        p = TYP::Make(bt_func,0);
246
        p->btp = pint->GetIndex();
247
        stdfunc = *p;
248
 
249
        p = allocTYP();
250
        p->type = bt_exception;
251
        p->typeno = bt_exception;
252
        p->size = 2;
253
        p->isUnsigned = true;
254
        p->precision = 16;
255
        p->bit_width = -1;
256
        stdexception = *p;
257
 
258
        p = allocTYP();
259
        p->type = bt_long;
260
        p->typeno = bt_long;
261
        p->val_flag = 1;
262
        p->size = 8;
263
        p->bit_width = -1;
264
        p->precision = 64;
265
        stdconst = *p;
266
 
267
        p = allocTYP();
268
        p->type = bt_vector;
269
        p->typeno = bt_vector;
270
        p->val_flag = 1;
271
        p->size = 512;
272
        p->bit_width = -1;
273
        p->precision = 64;
274
        stdvector = *p;
275
 
276
        p = allocTYP();
277
        p->type = bt_vector_mask;
278
        p->typeno = bt_vector_mask;
279
        p->val_flag = 1;
280
        p->size = 64;
281
        p->bit_width = -1;
282
        p->precision = 64;
283
        stdvectormask = p;
284
 
285
        p = allocTYP();
286
        p->type = bt_void;
287
        p->typeno = bt_void;
288
        p->val_flag = 1;
289
        p->size = 8;
290
        p->bit_width = -1;
291
        p->precision = 64;
292
        stdvoid = *p;
293
}
294
 
295
void Compiler::AddBuiltinFunctions()
296
{
297
        SYM *sp;
298
        TypeArray tanew, tadelete;
299
 
300
        sp = allocSYM();
301
        sp->SetName("__new");
302
        sp->fi = allocFunction(sp->id);
303
        sp->fi->sym = sp;
304
        tanew.Add(bt_long, 18);
305
        tanew.Add(bt_pointer,19);
306
        tanew.Add(bt_long, 20);
307
        sp->fi->AddProto(&tanew);
308
        sp->tp = &stdvoid;
309
        gsyms->insert(sp);
310
 
311
        sp = allocSYM();
312
        sp->SetName("__delete");
313
        sp->fi = allocFunction(sp->id);
314
        sp->fi->sym = sp;
315
        tadelete.Add(bt_pointer, 18);
316
        sp->fi->AddProto(&tadelete);
317
        sp->tp = &stdvoid;
318
        gsyms->insert(sp);
319
}
320
 
321
void Compiler::storeSymbols(std::ostream& ofs)
322
{
323
        int nn;
324
        char buf[20];
325
 
326
        nn = compiler.symnum;
327
        sprintf_s(buf, sizeof(buf), "SYM%05d", nn);
328
        ofs.write(buf,8);
329
        ofs.write((char *)symbolTable, nn * sizeof(SYM));
330
}

powered by: WebSVN 2.1.0

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