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

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [sw/] [zasm/] [zpp.l] - Blame information for rev 17

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 13 dgisselq
/*******************************************************************************
2
**
3
** Filename:    zpp.l
4
**
5
** Project:     Zip CPU -- a small, lightweight, RISC CPU core
6
**
7
** Purpose:     The preprocessor for the Zip Assembler.
8
**
9
**      This routine strips comments, handles #define's, #ifdef's, and
10
**      #include statements in a C fashion.
11
**
12
**      #define macro's are also defined in the language and therefore
13
**      supposed to be supported, but the support isn't there yet.
14
**
15
** Creator:     Dan Gisselquist, Ph.D.
16
**              Gisselquist Tecnology, LLC
17
**
18
********************************************************************************
19
**
20
** Copyright (C) 2015, Gisselquist Technology, LLC
21
**
22
** This program is free software (firmware): you can redistribute it and/or
23
** modify it under the terms of  the GNU General Public License as published
24
** by the Free Software Foundation, either version 3 of the License, or (at
25
** your option) any later version.
26
**
27
** This program is distributed in the hope that it will be useful, but WITHOUT
28
** ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
29
** FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
30
** for more details.
31
**
32
** You should have received a copy of the GNU General Public License along
33
** with this program.  (It's in the $(ROOT)/doc directory, run make with no
34
** target there if the PDF file isn't present.)  If not, see
35
**  for a copy.
36
**
37
** License:     GPL, v3, as defined and found on www.gnu.org,
38
**              http://www.gnu.org/licenses/gpl.html
39
**
40
**
41
*******************************************************************************/
42
 
43
%{
44
// #include 
45
#include 
46
#include 
47
#include 
48
#include 
49
 
50
using namespace std;
51
 
52 16 dgisselq
int yylex();
53
void    mark_line(void);
54
int     end_of_file(void);
55
void    pushf(const char *fname);
56
 
57 13 dgisselq
// #include "zprepr.tab.h"
58 16 dgisselq
int     ndef = 0, structno = 0;
59
char    *structid = NULL;
60 13 dgisselq
void    stb_define(const char *str);
61
void    stb_args(const char *str);
62
void    stb_macro(const char *value);
63
void    stb_addmacro(const char *value);
64
bool    stb_isdefined(const char *str);
65
const char      *stb_getdefn(const char *str);
66
%}
67 16 dgisselq
%x DEF DFA DFV DFV_EOL INDEF IFDEFV INNOTDEF NODEF NVRDEF COMMENT INSTRUCT
68
%x INDEF_EOL INNOTDEF_EOL GETSTRUCTID
69 13 dgisselq
%option noyywrap
70
%option stack
71
ID [_:A-Za-z][_:A-Za-z0-9]*
72 16 dgisselq
IDDOT {ID}("."{ID})*
73 13 dgisselq
 
74
%%
75
"*/"            { yy_pop_state(); }
76
[^*]+           { /* Ignore comments */ }
77
"*"+[^/]        { /* Ignore comments */ }
78
^"#include"[ \t]+\"[^\"]+\"     {
79
                char *ptr = &yytext[9], *start, *end, *str;
80
                while(isspace(*ptr))
81
                        ptr++;
82 16 dgisselq
                start = ++ptr;
83 13 dgisselq
                ptr++;
84
                while((*ptr)&&(*ptr != '\"'))
85
                        ptr++;
86
                *ptr = '\0';
87 16 dgisselq
                pushf(start);
88 13 dgisselq
                // push_file_state(yylineno); // and filename ...
89 16 dgisselq
                mark_line();
90 13 dgisselq
        }
91
^"#define"[ \t]+        { yy_push_state(DEF); }
92
 /* <*>^"#line"[ \t]+(0-9)+[ \t]+["][^"]*["][ \t]*\n       { } */
93 16 dgisselq
{IDDOT}/[^(]    {
94 13 dgisselq
                stb_define(yytext);
95
                BEGIN DFV;
96
        }
97 16 dgisselq
{IDDOT}/[(]     { stb_define(yytext); BEGIN DFA; }
98
"("[^)]+")"                             {
99
                /* Process macro arguments */
100 13 dgisselq
                stb_args(yytext);
101
                BEGIN DFV;
102
        }
103
[ \t]+  { /* Ignore initial spaces */ }
104 16 dgisselq
[^ \t\n]*       {/* Parse to end of line, get value for our define */
105 13 dgisselq
                stb_macro(yytext);
106 16 dgisselq
        }
107
[ \t]*((;|"//").*)?$    {/* Parse to end of line, get value for our define */
108 13 dgisselq
                yy_pop_state();
109
        }
110 16 dgisselq
[ \t]*"\\"[ \t]*((;|"//").*)?\n {/* Continue onto next line */
111
                fprintf(yyout, "\n"); mark_line(); yylineno++;
112
                stb_macro("\n");
113
        }
114 13 dgisselq
^[ \t]+.[dD][aA][tT][aA]?       { fprintf(yyout, "\tWORD"); }
115
^"#defcont"[ \t]+       { yy_push_state(DFV); }
116
^"#ifdef"[ \t]* { ndef = 0; yy_push_state(IFDEFV); }
117
^"#ifndef"[ \t]*        { ndef = 1; yy_push_state(IFDEFV); }
118 16 dgisselq
{IDDOT} {
119 13 dgisselq
                bool    known = stb_isdefined(yytext);
120
                if ( ((known)&&(ndef==0)) || ((!known)&&(ndef!=0)) ) {
121
                        BEGIN INDEF_EOL;
122
                } else {
123
                        BEGIN INNOTDEF_EOL;
124
                }
125
        }
126
        /* Not yet: ^"#if"[ \t]*                { yy_push_state(IFDEFE); }
127
        /* Not yet: ^"#if"[ \t]*                { yy_push_state(IFDEFE); }
128
        /* Not yet:  { yy_push_state(IFDEFE); } */
129
        /* ^"#elsif"[ \t]*      { yy_pop_state(); yy_push_state(IFDEFE); } */
130
[ \t]*$         { BEGIN INDEF; }
131
(;|"//").*$             { BEGIN INDEF; }
132
[ \t]*$         { BEGIN INNOTDEF; }
133
(;|"//").*$     { BEGIN INNOTDEF; }
134 16 dgisselq
[^ \t\n].*$             { BEGIN INDEF; fprintf(stderr, "WARNING! Unexpected characters on IFDEF line, \'%s\'\n", yytext); mark_line(); }
135
[^ \t\n].*$     { BEGIN INNOTDEF; fprintf(stderr, "WARNING! Unexpected characters on IFNDEF line, %s\n", yytext); mark_line(); }
136 13 dgisselq
^"#else"[ \t]*((;|"//").*)?$    { BEGIN NODEF; }
137
^"#else"[ \t]*((;|"//").*)?$            { BEGIN INDEF; }
138
(.*)                    { }
139
^"#elsif"[ \t]*         { BEGIN NVRDEF; }
140
^"#endif"[ \t]*((;|"//").*)?$   { yy_pop_state(); }
141
^"#endif"[ \t]*"/*"     { BEGIN COMMENT; }
142
<*>^"#endif"[ \t]*         { fprintf(stderr, "ERR: Unknown endif!!\n");}
143 16 dgisselq
^"#struct"[ \t]*        {
144
                yy_push_state(GETSTRUCTID); structno  = 0; }
145
{ID}/[ \t\n;/]  { BEGIN INSTRUCT;
146
                structid = strdup(yytext);
147
                }
148
{ID}("."{ID})*  {
149
                fprintf(yyout, "\t%s.%s\tequ\t%d", structid, yytext, structno++); }
150
^"#endstruct".*$        { yy_pop_state(); }
151 13 dgisselq
        /* Not yet: ^"#struct"[ \t]*    {}      */
152
        /* Not yet: ^"#endstruct"[ \t]* {}      */
153
        /* Not yet: ^"#seg"[ \t]*       {}      */
154
.*              { /* Ignore everything in these states*/ }
155
{ID}/[^(]       {
156
                if (stb_isdefined(yytext))
157
                        fprintf(yyout, "%s", stb_getdefn(yytext));
158
                else
159
                        fprintf(yyout, "%s", yytext);
160
        }
161 16 dgisselq
<*>[ \t]*"//".*$   { /* Ignore (trailing) comment only lines */ }
162
<*>[ \t]*";".*$            { /* Ignore (trailing) comment only lines */ }
163 13 dgisselq
<*>"/*"                    { yy_push_state(COMMENT); }
164
<*>[ \t]+          { ECHO; }
165 16 dgisselq
<*>\n                      { ECHO; yylineno++; mark_line(); }
166 13 dgisselq
        /* <*>.                    { printf("Unmatched \'%c\'\n", yytext[0]); } */
167 16 dgisselq
<>                       { if (end_of_file()) yyterminate(); }
168 13 dgisselq
 
169
%%
170
 
171
class   SYMTABLE_ENTRY {
172
private:
173
        std::string     &trim(std::string &s) {
174
                std::string::iterator   ptr = s.end();
175
 
176
                while((ptr >= s.begin())&&(isspace(*ptr)))
177
                        *ptr-- = '\0';
178
 
179
                return s;
180
        }
181
 
182
public:
183
        std::string     m_name, m_value, m_args;
184
        SYMTABLE_ENTRY(const char *str) : m_name(str) {
185
                trim(m_name);
186
        }
187
        SYMTABLE_ENTRY &operator+=(const char *str) {
188
                const char      *start = str;
189
 
190
                while(isspace(*start))
191
                        start++;
192
                if (m_value.length()!=0)
193
                        m_value += " ";
194
 
195
                std::string     trimd(start);
196
                trim(trimd);
197
                m_value += trimd;
198
 
199
                /*
200
                printf("ENTRY::SYMBOL \'%s\' NOW = \'%s\'\n",
201
                        m_name.c_str(), m_value.c_str());
202
                */
203
                return *this;
204
        }
205
        SYMTABLE_ENTRY &setargs(const char *str) {
206
                m_args += str;
207
                return *this;
208
        }
209
 
210 16 dgisselq
        const std::string &getdefn(void) {
211 13 dgisselq
                return m_value;
212
        }
213
};
214
 
215
class   SYMBOL_TABLE {
216
private:
217
        typedef SYMTABLE_ENTRY  *TBLV;
218
        typedef std::list       TBLT;
219
 
220
        TBLT    m_tbl;
221
        TBLT::iterator  lookup(const char *str) {
222
                TBLT::iterator  i = m_tbl.begin();
223
                for(; (i!= m_tbl.end())&&(strcmp(str, (*i)->m_name.c_str())>0); i++)
224
                        ;
225
                if ((i != m_tbl.end())&&(strcmp(str, (*i)->m_name.c_str())==0))
226
                        return i;
227
                return m_tbl.end();
228
        }
229
 
230
public:
231
        SYMBOL_TABLE(void) {}
232
 
233
        void define(const char *str) {
234
                SYMTABLE_ENTRY  *v = new SYMTABLE_ENTRY(str);
235
                TBLT::iterator  i = m_tbl.begin();
236
                for(; (i!= m_tbl.end())&&(strcmp(str, (*i)->m_name.c_str())>0); i++)
237
                        ;
238
                m_tbl.insert(i, v);
239
 
240
                // printf("SYMS::Defining SYMBOL: \'%s\'\n", str);
241
        }
242
 
243
        bool defined(const char *str) {
244
                TBLT::iterator  i = lookup(str);
245
                if (i==m_tbl.end())
246
                        return false;
247
                else
248
                        return true;
249
        }
250
 
251
 
252
        void    undefine(const char *str) {
253
                TBLT::iterator  i = lookup(str);
254
                if (i == m_tbl.end())
255
                        return;
256
                TBLV    v = (*i);
257
                m_tbl.erase(i);
258
                delete  v;
259
        }
260
 
261
        void    addmacro(const char *name, const char *str) {
262
                TBLT::iterator i = lookup(name);
263
                if (i == m_tbl.end()) {
264
                        fprintf(stderr, "INTERNAL ERR, %s NOT DEFINED!\n", name);
265
                } *(*i) += str;
266
        }
267
 
268
        void    addargs(const char *name, const char *str) {
269
                TBLT::iterator i = lookup(name);
270
                if (i == m_tbl.end()) {
271
                        fprintf(stderr, "INTERNAL ERR, %s NOT DEFINED!\n", name);
272
                } (*i)->setargs(str);
273
        }
274
        const   char *getdefn(const char *name) {
275
                TBLT::iterator i = lookup(name);
276
                if (i == m_tbl.end()) {
277
                        fprintf(stderr, "INTERNAL ERR, %s NOT DEFINED!\n", name);
278
                        return NULL;
279
                } (*i)->getdefn().c_str();
280
        }
281
 
282
};
283
 
284
SYMTABLE_ENTRY  *last = NULL;
285
SYMBOL_TABLE    syms;
286
std::string     last_define;
287
 
288
void    stb_define(const char *str) {
289
        if (syms.defined(str)) {
290
                fprintf(stderr, "WARNING!  Symbol \'%s\' is already defined!\n", str);
291
                syms.undefine(str);
292
        }
293
 
294
        syms.define(str);
295
        last_define = str;
296
}
297
 
298
void    stb_args(const char *args) {
299
        syms.addargs(last_define.c_str(), args);
300
}
301
 
302
void    stb_macro(const char *value) {
303
        syms.addmacro(last_define.c_str(), value);
304
}
305
 
306
void    stb_addmacro(const char *value) {
307
        syms.addmacro(last_define.c_str(),value);
308
}
309
 
310
bool    stb_isdefined(const char *str) {
311
        return syms.defined(str);
312
}
313
 
314
const char *stb_getdefn(const char *str) {
315
        return syms.getdefn(str);
316
}
317
 
318 16 dgisselq
class   BUFSTACK {
319
public:
320
        FILE            *m_fp;
321
        char            *m_fname;
322
        int             m_lineno;
323
        BUFSTACK        *m_prev;
324
        YY_BUFFER_STATE m_bs;
325
 
326
        static  BUFSTACK        *curbs;
327
        static  const char      *curfilename;
328
 
329
        BUFSTACK(void) {
330
                m_fp = stdin;
331
 
332
                if (curbs)
333
                        curbs->m_lineno = yylineno;
334
                m_prev = curbs;
335
                // m_bs = yy_create_buffer(fp, YY_BUF_SIZE);
336
                m_fname = strdup("(stdin)");
337
                // yy_switch_to_buffer(m_bs);
338
                m_bs = NULL;
339
                curbs = this;
340
                m_lineno = 1;
341
                curfilename = m_fname;
342
 
343
                yyrestart(m_fp);
344
                yylineno = 1;
345
        }
346
 
347
        BUFSTACK(const char *fname) {
348
                m_fp = fopen(fname, "r");
349
                if (!m_fp) {
350
                        fprintf(stderr, "Cannot open %s\n", fname);
351
                        perror("O/S Err:");
352
                        exit(-1);
353
                }
354
 
355
                if (curbs)
356
                        curbs->m_lineno = yylineno;
357
                m_prev = curbs;
358
                m_bs = yy_create_buffer(m_fp, YY_BUF_SIZE);
359
                m_fname = strdup(fname);
360
                yy_switch_to_buffer(m_bs);
361
                curbs = this;
362
                m_lineno = 1;
363
                curfilename = m_fname;
364
 
365
                yyrestart(m_fp);
366
                yylineno = 1;
367
        }
368
 
369
        ~BUFSTACK(void) {
370
                // fprintf(stderr, "DELETING(%s)\n", m_fname);
371
                fclose(m_fp);
372
                free(m_fname);
373
                if (m_bs)
374
                        yy_delete_buffer(m_bs);
375
                curbs = m_prev;
376
 
377
                if (curbs) {
378
                        yy_switch_to_buffer(curbs->m_bs);
379
                        yylineno = curbs->m_lineno;
380
                        curfilename = curbs->m_fname;
381
                }
382
        }
383
 
384
        void    mark(void) {
385
                FILE    *fp = yyout;
386
                if (!fp) fp = stdout;
387
                fprintf(fp, "#line %d \"%s\"\n", yylineno, m_fname);
388
        }
389
 
390
        static  void pop(void) {
391
                // fprintf(stderr, "POP! (%s)\n", curbs->m_fname);
392
                if (curbs)
393
                        delete curbs;
394
        }
395
};
396
 
397
BUFSTACK *BUFSTACK::curbs = NULL;
398
const char *BUFSTACK::curfilename = NULL;
399
 
400
int             last_marked_line = -1;
401
const char      *last_marked_file = NULL;
402
void    mark_line(void) {
403
        if ((yylineno != last_marked_line+1)||(BUFSTACK::curfilename != last_marked_file))
404
                BUFSTACK::curbs->mark();
405
        last_marked_line = yylineno;
406
        last_marked_file = BUFSTACK::curfilename;
407
}
408
 
409
int     end_of_file(void) {
410
        BUFSTACK::pop();
411
        return (BUFSTACK::curbs == NULL);
412
}
413
 
414
void    pushf(const char *fname) {
415
        BUFSTACK        *bs = new BUFSTACK(fname);
416
}
417
 
418 13 dgisselq
int main(int argc, char **argv) {
419 16 dgisselq
        yylineno = 1;
420
        if (argc < 2) { // Stdin only
421
                BUFSTACK::curbs = new BUFSTACK();
422
                yylex();
423
        } else {
424
                for(int argn=1; argn
425
                        BUFSTACK        *bs = new BUFSTACK(argv[argn]);
426
                        mark_line();
427
                        yylex();
428
                        // delete       bs;
429
                }
430
        }
431 13 dgisselq
}
432
 

powered by: WebSVN 2.1.0

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