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

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [sw/] [zasm/] [zpp.l] - Diff between revs 26 and 34

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 26 Rev 34
Line 44... Line 44...
// #include 
// #include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
#include 
 
#include 
 
 
using namespace std;
using namespace std;
 
 
int yylex();
int yylex();
void    mark_line(void);
void    mark_line(void);
Line 56... Line 58...
 
 
// #include "zprepr.tab.h"
// #include "zprepr.tab.h"
int     ndef = 0, structno = 0;
int     ndef = 0, structno = 0;
char    *structid = NULL;
char    *structid = NULL;
void    stb_define(const char *str);
void    stb_define(const char *str);
 
bool    stb_current(const char *str);
 
bool    stb_hasargs(const char *str);
 
void    stb_expand(FILE *fout, const char *str);
 
std::string     stb_expand(const char *str);
void    stb_args(const char *str);
void    stb_args(const char *str);
void    stb_macro(const char *value);
void    stb_macro(const char *value);
void    stb_addmacro(const char *value);
void    stb_addmacro(const char *value);
bool    stb_isdefined(const char *str);
bool    stb_isdefined(const char *str);
const char      *stb_getdefn(const char *str);
const char      *stb_getdefn(const char *str);
Line 89... Line 95...
                mark_line();
                mark_line();
        }
        }
^"#define"[ \t]+        { yy_push_state(DEF); }
^"#define"[ \t]+        { yy_push_state(DEF); }
 /* <*>^"#line"[ \t]+(0-9)+[ \t]+["][^"]*["][ \t]*\n       { } */
 /* <*>^"#line"[ \t]+(0-9)+[ \t]+["][^"]*["][ \t]*\n       { } */
{IDDOT}/[^(]    {
{IDDOT}/[^(]    {
 
                // fprintf(stderr, "Defining \'%s\'\n", yytext);
                stb_define(yytext);
                stb_define(yytext);
                BEGIN DFV;
                BEGIN DFV;
        }
        }
{IDDOT}/[(]     { stb_define(yytext); BEGIN DFA; }
{IDDOT}/[(]     { stb_define(yytext); BEGIN DFA; }
"("[^)]+")"                             {
"("[^)]+")"                             {
                /* Process macro arguments */
                /* Process macro arguments */
                stb_args(yytext);
                stb_args(yytext);
                BEGIN DFV;
                BEGIN DFV;
        }
        }
[ \t]+  { /* Ignore initial spaces */ }
^[ \t]+ { stb_macro(yytext); /* Replicate initial spaces */ }
[^ \t\n]*       {/* Parse to end of line, get value for our define */
[ \t]+  { stb_macro(" "); /* Ignore all  but one internal space */ }
 
{ID}    {/* Parse to end of line, get value for our define */
 
                // fprintf(stderr, "%s may be a macro, line %d\n", yytext, yylineno);
 
                if ((!stb_current(yytext))&&(stb_isdefined(yytext))) {
 
                        // fprintf(stderr, "Recursive MACRO!\n");
 
                        stb_macro(stb_getdefn(yytext));
 
                } else {
 
                        // fprintf(stderr, "But it is not defined\n");
 
                        stb_macro(yytext);
 
                }
 
        }
 
{ID}[ \t]*[(][ \t]*{ID}[ \t]*(,[ \t]*{ID}[ \t]*)*[)]    {
 
                // fprintf(stderr, "%s may be a macro within a macro!\n", yytext);
 
                if ((!stb_current(yytext))&&(stb_isdefined(yytext))) {
 
                        if (stb_hasargs(yytext)) {
 
                                std::string str = stb_expand(yytext);
 
                                stb_macro(str.c_str());
 
                        } else {
 
                                char *dup = strdup(yytext), *ptr;
 
                                ptr = strchr(dup, '(');
 
                                *ptr = '\0';
 
                                stb_macro(stb_getdefn(dup));
 
                                free(dup);
 
                                yyless(strchr(yytext,'(')-yytext);
 
                        }
 
                } else {
 
                        char *dup = strdup(yytext), *ptr;
 
                        ptr = strchr(dup, '(');
 
                        *ptr = '\0';
 
                        stb_macro(stb_getdefn(dup));
 
                        free(dup);
 
                        yyless(strchr(yytext,'(')-yytext);
 
                }
 
        }
 
[^a-zA-Z_0-9 \t\n]+     {/* Parse to end of line, get value for our define */
 
                // fprintf(stderr, "A: Adding to macro %s\n", yytext);
 
                stb_macro(yytext);
 
        }
 
0[xX][0-9A-Fa-f]+       {/* Get any hexadecimal constants */
 
                // fprintf(stderr, "B: Adding to macro %s\n", yytext);
 
                stb_macro(yytext);
 
        }
 
[0-9A-Fa-f]+[hH]        {/* Get any hexadecimal constants */
 
                // fprintf(stderr, "C: Adding to macro %s\n", yytext);
 
                stb_macro(yytext);
 
        }
 
[0-7]+[oO]      {/* Get any hexadecimal constants */
 
                // fprintf(stderr, "D: Adding to macro %s\n", yytext);
 
                stb_macro(yytext);
 
        }
 
[0-9]+  {/* Get any hexadecimal constants */
 
                // fprintf(stderr, "E: Adding to macro %s\n", yytext);
                stb_macro(yytext);
                stb_macro(yytext);
        }
        }
[ \t]*((;|"//").*)?$    {/* Parse to end of line, get value for our define */
[ \t]*((;|"//").*)?$    {/* Parse to end of line, get value for our define */
                yy_pop_state();
                yy_pop_state();
        }
        }
Line 157... Line 215...
                if (stb_isdefined(yytext))
                if (stb_isdefined(yytext))
                        fprintf(yyout, "%s", stb_getdefn(yytext));
                        fprintf(yyout, "%s", stb_getdefn(yytext));
                else
                else
                        fprintf(yyout, "%s", yytext);
                        fprintf(yyout, "%s", yytext);
        }
        }
 
{ID}([ \t]*)    {
 
                if (stb_isdefined(yytext))
 
                        fprintf(yyout, "%s", stb_getdefn(yytext));
 
                else
 
                        fprintf(yyout, "%s", yytext);
 
        }
 
{ID}[ \t]*[(][ \t]*{ID}[ \t]*(,[ \t]*{ID}[ \t]*)*[)]    {
 
                // fprintf(stderr, "%s may be a macro!\n", yytext);
 
                if (stb_isdefined(yytext)) {
 
                        if (stb_hasargs(yytext)) {
 
                                stb_expand(yyout, yytext);
 
                        } else {
 
                                fprintf(yyout, "%s", stb_getdefn(yytext));
 
                                yyless(strchr(yytext,'(')-yytext);
 
                        }
 
                } else {
 
                        // fprintf(stderr, "But it is not defined\n");
 
                        fprintf(yyout, "%s", yytext);
 
                }
 
        }
<*>[ \t]*"//".*$   { /* Ignore (trailing) comment only lines */ }
<*>[ \t]*"//".*$   { /* Ignore (trailing) comment only lines */ }
<*>[ \t]*";".*$            { /* Ignore (trailing) comment only lines */ }
<*>[ \t]*";".*$            { /* Ignore (trailing) comment only lines */ }
<*>"#warning".*$   { fprintf(stderr, "WARNING: %s\n", &yytext[8]); }
<*>"#warning".*$   { fprintf(stderr, "WARNING: %s\n", &yytext[8]); }
<*>"#error".*$             { fprintf(stderr, "ERROR: %s\n", &yytext[8]); exit(-1); }
<*>"#error".*$             { fprintf(stderr, "ERROR: %s\n", &yytext[8]); exit(-1); }
<*>"/*"                    { yy_push_state(COMMENT); }
<*>"/*"                    { yy_push_state(COMMENT); }
Line 176... Line 254...
 
 
<>       { if (end_of_file()) yyterminate(); }
<>       { if (end_of_file()) yyterminate(); }
 
 
%%
%%
 
 
 
class   SYMTABLE_ACTION {
 
public:
 
        // Types: 0 (end of actions), 1-X, argument number, <0 raw string
 
        int             m_type;
 
        std::string     m_str;          // if m_type < 0, have m_str
 
};
 
 
class   SYMTABLE_ENTRY {
class   SYMTABLE_ENTRY {
private:
private:
 
        bool    m_reduced;
        std::string     &trim(std::string &s) {
        std::string     &trim(std::string &s) {
                std::string::iterator   ptr = s.end();
                std::string::iterator   ptr = s.end();
 
 
                while((ptr >= s.begin())&&(isspace(*ptr)))
                while((ptr >= s.begin())&&(isspace(*ptr)))
                        *ptr-- = '\0';
                        *ptr-- = '\0';
Line 189... Line 275...
                return s;
                return s;
        }
        }
 
 
public:
public:
        std::string     m_name, m_value, m_args;
        std::string     m_name, m_value, m_args;
 
        std::vector     m_actions;
        SYMTABLE_ENTRY(const char *str) : m_name(str) {
        SYMTABLE_ENTRY(const char *str) : m_name(str) {
                trim(m_name);
                trim(m_name);
 
                m_reduced = false;
        }
        }
        SYMTABLE_ENTRY &operator+=(const char *str) {
        SYMTABLE_ENTRY &operator+=(const char *str) {
                const char      *start = str;
                const char      *start = str;
 
 
                while(isspace(*start))
                while(isspace(*start))
                        start++;
                        start++;
                if (m_value.length()!=0)
                if (m_value.length()!=0)
                        m_value += " ";
                        m_value += " ";
 
 
                std::string     trimd(start);
                m_value += str;
                trim(trimd);
 
                m_value += trimd;
 
 
 
                /*
                /*
                printf("ENTRY::SYMBOL \'%s\' NOW = \'%s\'\n",
                printf("ENTRY::SYMBOL \'%s\' NOW = \'%s\'\n",
                        m_name.c_str(), m_value.c_str());
                        m_name.c_str(), m_value.c_str());
                */
                */
Line 218... Line 304...
        }
        }
 
 
        const std::string &getdefn(void) {
        const std::string &getdefn(void) {
                return m_value;
                return m_value;
        }
        }
 
 
 
        bool    hasargs(void) {
 
                return (m_args.size()>0);
 
        }
 
 
 
        void    reduce(void) {
 
                if (m_reduced)
 
                        return;
 
 
 
                // fprintf(stderr, "Reducing %s ( %s ) \n", m_name.c_str(), m_args.c_str());
 
                std::vector     alist;
 
                int i=0, bg, en;
 
                do {
 
                        if ((m_args[i] == ',')||(m_args[i] == '('))
 
                                i++;
 
                        while((m_args[i])&&(isspace(m_args[i])))
 
                                i++;
 
                        bg = i;
 
                        while((m_args[i])&&(
 
                                (isalpha(m_args[i]))
 
                                ||(m_args[i]==':')
 
                                ||(m_args[i]=='_')
 
                                ||(isdigit(m_args[i]))))
 
                                i++;
 
                        en = i;
 
                        while((m_args[i])&&(isspace(m_args[i])))
 
                                i++;
 
 
 
                        alist.push_back(m_args.substr(bg,en-bg));
 
                        // printf("Found argument %2ld of %s: \'%s\'\n",
 
                                // alist.size(),
 
                                // m_name.c_str(),
 
                                // m_args.substr(bg,en-bg).c_str());
 
                } while((m_args[i])&&(m_args[i] == ','));
 
 
 
                assert(m_args[i] == ')');
 
 
 
                // Now that we know our arguments, lets look for these
 
                // arguments in our macro definition
 
                std::string     building;
 
                i = 0;
 
                while(m_value[i]) {
 
                        int     nxti = m_value.size(), nxtv;
 
                        for(int a=0; a
 
                                const char *ptr;
 
                                ptr = strstr(m_value.c_str()+i, alist[a].c_str());
 
                                while((ptr)&&(ptr-m_value.c_str() < nxti)) {
 
                                        int loc = ptr-m_value.c_str();
 
                                        const char *pre = ptr-1;
 
                                        const char *pst = ptr+alist[a].size();
 
                                        if ((loc < nxti)&&(
 
                                                (loc == i)
 
                                                ||((!isalpha(*pre)
 
                                                        &&(*pre != '_')
 
                                                        &&(*pre != ':')
 
                                                        &&(!isdigit(*pre)))))
 
                                                &&((*pst=='\0')
 
                                                ||(     (!isalpha(*pst))
 
                                                        &&(*pst != '_')
 
                                                        &&(*pst != ':')
 
                                                        &&(!isdigit(*pst)))))
 
                                        {
 
                                                        nxti = loc;
 
                                                        nxtv = a;
 
                                                        break;
 
                                        } else {
 
                                                ptr = strstr(m_value.c_str()+loc, alist[a].c_str());
 
                                                loc = ptr-m_value.c_str();
 
                                        }
 
                                }
 
                        }
 
 
 
                        if (nxti < m_value.size()) {
 
                                // Found an argument!!
 
                                SYMTABLE_ACTION act;
 
                                if (nxti > i) {
 
                                        act.m_type = -1;
 
                                        act.m_str = m_value.substr(i,nxti-i);
 
                                        // printf("ACTION: \'%s\'\n", act.m_str.c_str());
 
                                        m_actions.push_back(act);
 
                                }
 
                                act.m_type = nxtv;
 
                                act.m_str = "";
 
                                m_actions.push_back(act);
 
                                // printf("ACTION[%2d]: \'%s\'\n", nxtv, alist[nxtv].c_str());
 
 
 
                                i = nxti+alist[nxtv].size();
 
                        } else break; // No more arguments
 
                } if (i
 
                        SYMTABLE_ACTION act;
 
                        act.m_type = -1;
 
                        act.m_str = m_value.substr(i);
 
                        // printf("ACTION: \'%s\'\n", act.m_str.c_str());
 
                        m_actions.push_back(act);
 
                }
 
                m_reduced = true;
 
        }
 
 
 
        std::string expand(std::string args) {
 
                if (!m_reduced)
 
                        reduce();
 
                std::vector     alist;
 
                std::string     result;
 
 
 
                // printf("Expanding %s\n", args.c_str());
 
                int i=0, bg, en, nest=-1;
 
                do {
 
                        if ((args[i] == '(')||(args[i] == ',')) {
 
                                if (args[i] =='(')
 
                                        nest++;
 
                                i++;
 
                        }
 
                        while((args[i])&&(isspace(args[i])))
 
                                i++;
 
                        bg = i;
 
                        while((args[i])&&(args[i] != ',')&&((args[i] != ')')||(nest != 0))) {
 
                                if (args[i] == '(')
 
                                        nest++;
 
                                else if (args[i] == ')')
 
                                        nest--;
 
                                i++;
 
                        } en = i-1;
 
                        while((en>0)&&(isspace(args[en])))
 
                                en--;
 
                        alist.push_back(args.substr(bg,en+1-bg));
 
                        // printf("Argument %2ld of %s maps to \'%s\'\n",
 
                                // alist.size(),
 
                                // m_name.c_str(),
 
                                // args.substr(bg,en+1-bg).c_str());
 
                } while((args[i])&&(args[i] == ','));
 
 
 
                // printf("At end, args[i] = \'%s\'\n", &args[i]);
 
                assert(args[i] == ')');
 
 
 
                // printf("Filling in %ld actions\n", m_actions.size());
 
                for(i=0; i
 
                        if((m_actions[i].m_type >= 0)&&(m_actions[i].m_type < alist.size()))
 
                                result += alist[m_actions[i].m_type].c_str();
 
                        else if (m_actions[i].m_type < 0)
 
                                result += m_actions[i].m_str.c_str();
 
                        // else {
 
                                // fprintf(fout, "m_type = %d, size = %ld\n", m_actions[i].m_type, alist.size());
 
                        // }
 
                }
 
 
 
                return result;
 
        }
};
};
 
 
class   SYMBOL_TABLE {
class   SYMBOL_TABLE {
private:
private:
        typedef SYMTABLE_ENTRY  *TBLV;
        typedef SYMTABLE_ENTRY  *TBLV;
Line 245... Line 478...
                TBLT::iterator  i = m_tbl.begin();
                TBLT::iterator  i = m_tbl.begin();
                for(; (i!= m_tbl.end())&&(strcmp(str, (*i)->m_name.c_str())>0); i++)
                for(; (i!= m_tbl.end())&&(strcmp(str, (*i)->m_name.c_str())>0); i++)
                        ;
                        ;
                m_tbl.insert(i, v);
                m_tbl.insert(i, v);
 
 
                // printf("SYMS::Defining SYMBOL: \'%s\'\n", str);
                // fprintf(stderr, "SYMS::Defining SYMBOL: \'%s\'\n", str);
        }
        }
 
 
        bool defined(const char *str) {
        bool defined(const char *str) {
                TBLT::iterator  i = lookup(str);
                TBLT::iterator  i = lookup(str);
                if (i==m_tbl.end())
                if (i==m_tbl.end())
Line 279... Line 512...
                TBLT::iterator i = lookup(name);
                TBLT::iterator i = lookup(name);
                if (i == m_tbl.end()) {
                if (i == m_tbl.end()) {
                        fprintf(stderr, "INTERNAL ERR, %s NOT DEFINED!\n", name);
                        fprintf(stderr, "INTERNAL ERR, %s NOT DEFINED!\n", name);
                } (*i)->setargs(str);
                } (*i)->setargs(str);
        }
        }
 
        bool    hasargs(const char *name) {
 
                TBLT::iterator i = lookup(name);
 
                if (i == m_tbl.end()) {
 
                        return false;
 
                } return (*i)->hasargs();
 
        }
        const   char *getdefn(const char *name) {
        const   char *getdefn(const char *name) {
                TBLT::iterator i = lookup(name);
                TBLT::iterator i = lookup(name);
                if (i == m_tbl.end()) {
                if (i == m_tbl.end()) {
                        fprintf(stderr, "INTERNAL ERR, %s NOT DEFINED!\n", name);
                        fprintf(stderr, "INTERNAL ERR, %s NOT DEFINED!\n", name);
                        return NULL;
                        return NULL;
                } (*i)->getdefn().c_str();
                } (*i)->getdefn().c_str();
        }
        }
 
 
 
        std::string     expand(const char *name, const char *ptr) {
 
                TBLT::iterator  i = lookup(name);
 
                if (i==m_tbl.end())
 
                        return std::string("");
 
                return (*i)->expand(std::string(ptr));
 
        }
 
 
};
};
 
 
SYMTABLE_ENTRY  *last = NULL;
SYMTABLE_ENTRY  *last = NULL;
SYMBOL_TABLE    syms;
SYMBOL_TABLE    syms;
std::string     last_define;
std::string     last_define;
 
 
void    stb_define(const char *str) {
void    stb_define(const char *str) {
 
        /*
 
        if (last_define.size()>0) {
 
                fprintf(stderr, "LAST-DEFINE(%s): %s\n", last_define.c_str(),
 
                        stb_getdefn(last_define.c_str()));
 
        } */
 
 
        if (syms.defined(str)) {
        if (syms.defined(str)) {
                fprintf(stderr, "WARNING!  Symbol \'%s\' is already defined!\n", str);
                fprintf(stderr, "WARNING!  Symbol \'%s\' is already defined!\n", str);
                syms.undefine(str);
                syms.undefine(str);
        }
        }
 
 
Line 316... Line 568...
void    stb_addmacro(const char *value) {
void    stb_addmacro(const char *value) {
        syms.addmacro(last_define.c_str(),value);
        syms.addmacro(last_define.c_str(),value);
}
}
 
 
bool    stb_isdefined(const char *str) {
bool    stb_isdefined(const char *str) {
 
        const char *ptr;
 
        if ((ptr = strchr(str, '('))!=NULL) {
 
                // fprintf(stderr, "Checking whether %s needs to be expanded\n",str);
 
                char *dup = strdup(str), *chr;
 
                chr = strchr(dup, '(');
 
                *chr = '\0';
 
                // fprintf(stderr, "\tLooking it up by the name \'%s\'\n", dup);
 
                bool r = (syms.defined(dup));
 
                free(dup);
 
                return r;
 
        } else {
        return syms.defined(str);
        return syms.defined(str);
}
}
 
}
 
 
const char *stb_getdefn(const char *str) {
const char *stb_getdefn(const char *str) {
        return syms.getdefn(str);
        return syms.getdefn(str);
}
}
 
 
 
bool    stb_current(const char *str) {
 
        return (strcmp(str, last_define.c_str())==0);
 
}
 
 
 
bool    stb_hasargs(const char *str) {
 
        const char *ptr;
 
        if ((ptr = strchr(str, '('))!=NULL) {
 
                char *dup = strdup(str), *chr;
 
                chr = strchr(dup, '(');
 
                *chr = '\0';
 
                bool r = (syms.hasargs(dup));
 
                // fprintf(stderr, "\t%s has %sarguments\n", dup, (r)?"":"no ");
 
                free(dup);
 
                return r;
 
        } else {
 
                return syms.hasargs(str);
 
        }
 
}
 
 
 
std::string     stb_expand(const char *macro) {
 
        const   char    *ptr;
 
        std::string     str;
 
        ptr = strchr(macro, '(');
 
        assert(ptr);
 
        ptr--;
 
        while((ptr>macro)&&(isspace(*ptr)))
 
                ptr--;
 
        char *nam = strndup(macro, ptr+1-macro);
 
        ptr = strchr(ptr, '(');
 
        // fprintf(stderr, "Requesting an expansion of %s -- %s\n", nam, ptr);
 
        str = syms.expand(nam, ptr);
 
        free(nam);
 
 
 
        return str;
 
}
 
 
 
void    stb_expand(FILE *fout, const char *macro) {
 
        std::string     str = stb_expand(macro);
 
        fprintf(fout, "%s", str.c_str());
 
}
 
 
class   BUFSTACK {
class   BUFSTACK {
public:
public:
        FILE            *m_fp;
        FILE            *m_fp;
        char            *m_fname;
        char            *m_fname;
        int             m_lineno;
        int             m_lineno;

powered by: WebSVN 2.1.0

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