⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 template.c

📁 CC386 is a general-purpose 32-bit C compiler. It is not an optimizing compiler but given that the co
💻 C
📖 第 1 页 / 共 4 页
字号:
/* 
Copyright 1994-2003 Free Software Foundation, Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.  

This program is derived from the cc68k complier by 
Matthew Brandt (mattb@walkingdog.net) 

You may contact the author of this derivative at:

mailto::camille@bluegrass.net

or by snail mail at:

David Lindauer
850 Washburn Ave Apt 99
Louisville, KY 40222
 */
/*
 * handle ALL declarative statements
 */
#include <stdio.h>
#include <string.h>
#include "lists.h"
#include "expr.h"
#include "c.h"
#include "ccerr.h"

extern LIST *varlisthead,  *varlisttail;
extern int virtualdata;
extern int stdaddrsize;
extern int laststrlen;
extern char laststr[];
extern int intemplateargs;
extern int locsize;
extern BLK *locblk;
extern int lastch, lastst;
extern int lineno, errlineno;
extern char *infile,  *errfile;
extern int prm_cmangle;
extern int global_flag;
extern HASHREC **templateFuncs;
extern TYP *head,  **headptr,  *tail;
extern TABLE *tagtable;
extern int prm_cmangle;
extern char lastid[];
extern SYM *currentfunc;
extern SYM *lastfunc;
extern TABLE lsyms, oldlsym, ltags, oldltag;
extern int goodcode;
extern SYM *asntyp,  *andtyp,  *typequal,  *declclass;
extern SNODE *funcendstmt;
extern SNODE *cbautoinithead,  *cbautoinittail,  *cbautorundownhead, 
    *cbautorundowntail;
extern int block_nesting;
extern ENODE *block_rundown;
extern int defaulttype;
extern int recordingTemplate;
extern short unalteredline[];
extern char *cpp_funcname_tab[];
extern int templateLookup;
extern struct template  *currentTemplate;
extern TABLE *gsyms;
extern int manglenamecount;
extern int conscount;
extern int cppflags;

int statictemplate;

char *templatePrefix;
short *interjectptr;
SYM *templateSP;
int virtualfuncs;

static int matching;

static struct codecontext
{
    struct codecontext *next;
    char id[256];
    int defaulttype;
    int blocknest;
    int blockrundown;
    SNODE *cbautoinithead,  *cbautoinittail,  *cbautorundownhead, 
        *cbautorundowntail;
    SNODE *funcendstmt;
    SYM *asntyp,  *andtyp,  *typequal,  *declclass;
    int goodcode;
    int chhold, sthold;
    int line, errline;
    char *file,  *errfile;
    SYM *cfhold;
    TABLE xlsyms, xoldlsyms, xltags, xoldltags;
    short *interject;
    TYP *head,  *tail,  **headptr;
    int oldvirtual;
    LIST *vlisthead,  *vlisttail;
    int conscount;
    int statictemplate;
}  *contextbase;

void template_init()
{
    interjectptr = 0;
    templateSP = 0;
    virtualfuncs = 0;
    virtualdata = 0;
    matching = FALSE;
    statictemplate = 0;
}

//-------------------------------------------------------------------------

void template_rundown(void){}
static void savecontext(void)
{
    struct codecontext *con = xalloc(sizeof(struct codecontext));
    con->next = contextbase;
    contextbase = con;
    con->head = head;
    con->tail = tail;
    con->headptr = headptr;
    headptr = &head;
    head = tail = 0;
    con->errfile = errfile;
    con->interject = interjectptr;
    con->line = lineno;
    con->errline = errlineno;
    con->file = infile;
    strcpy(con->id, lastid);
    con->cbautoinithead = cbautoinithead;
    con->cbautoinittail = cbautoinittail;
    con->cbautorundownhead = cbautorundownhead;
    con->cbautorundowntail = cbautorundowntail;
    cbautoinithead = 0;
    cbautoinittail = 0;
    cbautorundownhead = 0;
    cbautorundowntail = 0;
    con->blockrundown = block_rundown;
    con->blocknest = block_nesting;
    block_rundown = 0;
    block_nesting = 0;
    con->funcendstmt = funcendstmt;
    con->xlsyms = lsyms;
    con->xoldlsyms = oldlsym;
    con->xltags = ltags;
    con->xoldltags = oldltag;
    lsyms.head = lsyms.tail = 0;
    oldlsym.head = oldlsym.tail = 0;
    oldltag.head = oldltag.tail = 0;
    con->goodcode = goodcode;
    con->asntyp = asntyp;
    asntyp = 0;
    con->andtyp = andtyp;
    andtyp = 0;
    con->typequal = typequal;
    typequal = 0;
    con->declclass = declclass;
    declclass = 0;
    con->defaulttype = defaulttype;
    con->cfhold = currentfunc;
    currentfunc = 0;
    con->chhold = lastch;
    con->sthold = lastst;
    con->oldvirtual = virtualfuncs;
    con->vlisthead = varlisthead;
    varlisthead = 0;
    con->vlisttail = varlisttail;
    varlisttail = 0;
    con->conscount = conscount;
    conscount = 0;
    con->statictemplate = statictemplate;
}

//-------------------------------------------------------------------------

static void restorecontext(void)
{
    struct codecontext *con = contextbase;
    contextbase = contextbase->next;
    head = con->head;
    tail = con->tail;
    headptr = con->headptr;
    errfile = con->errfile;
    interjectptr = con->interject;
    lastst = con->sthold;
    lastch = con->chhold;
    currentfunc = con->cfhold;
    virtualfuncs = con->oldvirtual;
    lsyms = con->xlsyms;
    oldlsym = con->xoldlsyms;
    ltags = con->xltags;
    oldltag = con->xoldltags;
    goodcode = con->goodcode;
    asntyp = con->asntyp;
    andtyp = con->andtyp;
    typequal = con->typequal;
    declclass = con->declclass;
    funcendstmt = con->funcendstmt;
    cbautoinithead = con->cbautoinithead;
    cbautoinittail = con->cbautoinittail;
    cbautorundownhead = con->cbautorundownhead;
    cbautorundowntail = con->cbautorundowntail;
    block_nesting = con->blocknest;
    block_rundown = con->blockrundown;
    defaulttype = con->defaulttype;
    strcpy(lastid, con->id);
    lineno = con->line;
    errlineno = con->errline;
    infile = con->file;
    varlisthead = con->vlisthead;
    varlisttail = con->vlisttail;
    conscount = con->conscount;
    statictemplate = con->statictemplate ;
}

//-------------------------------------------------------------------------

static int tcmp(short *p, short *str, int *len)
{
    int rv = 0;
    int i = 0;
    do
    {
        while (isspace(*p))p++, rv++;
        while (isspace(*str))
            str++, i++;
        if (i >=  *len)
        {
            *len = rv;
            return 0;
        } if (*p !=  *str)
            return 1;
        p++, str++, i++, rv++;
    }
    while (i <  *len)
        ;
    return 0;
}

//-------------------------------------------------------------------------

static short *matchname(short *s, short *str, int *len, int nolt)
{
    short *p = s;
    if (!p)
        return 0;
    while (*p)
    {
        if (p[0] == str[0])
        if (p == s || !issymchar(p[ - 1]))
        {
            int xlen =  *len;
            if (!tcmp(p, str, &xlen))
                if (!issymchar(p[xlen]))
            if (!nolt)
            {
                *len = xlen;
                return p;
            }
            else
            {
                char *t = p +  *len;
                while (isspace(*t))
                    t++;
                if (*t == '<')
                    return 0;
                *len = xlen;
                return p;
            }
        }
        p++;
    }
    return 0;
}

//-------------------------------------------------------------------------

static short *doreplace(short *buf, int *max, int *len, short *find, short
    *replace, int slen, int flen, int nolt)
{
    short *s = buf;
    int sslen = slen;
    while (s = matchname(s, find, &sslen, nolt))
    {
        if (*max <=  *len + flen - sslen)
        {
            short *buf2 = xalloc(*max + 1000 * sizeof(short));
            *max += 1000;
            memcpy(buf2, buf, (*len + 1) *sizeof(short));
            s = s + (int)(buf2 - buf);
            buf = buf2;
        }
        if (flen > sslen)
            memmove(s + flen - sslen, s, (*len - (s - buf) + 1) *sizeof(short));
        else
            memcpy(s, s + sslen - flen, (*len - (s + sslen - flen - buf) + 1)
                *sizeof(short));
        *len += flen - sslen;
        memcpy(s, replace, flen *sizeof(short));
        s += flen;
        sslen = slen;
    }
    return buf;
}

//-------------------------------------------------------------------------

static short *llreplace2(short *buf, int *max, int *len, char *find, char
    *replace)
{
    short formattedtype[512];
    short searchfor[256];
    char *p = replace;
    int flen = 0, slen = 0;
    while (*p)
        if (*p == '@' ||  *p == '$' ||  *p == '#')
            formattedtype[flen++] = '_', p++;
        else
            formattedtype[flen++] =  *p++;
    p = find;
    while (*p)
        searchfor[slen++] =  *p++;
    if (prm_cmangle && searchfor[0] == '_')
    {
        memcpy(searchfor, searchfor + 1, slen *sizeof(short));
        slen--;
    }
    return doreplace(buf, max, len, searchfor, formattedtype, slen, flen, TRUE);
}

//-------------------------------------------------------------------------

static short *llreplace(short *buf, int *max, int *len, TYP *to, TYP *tn)
{
    char type[512];
    short formattedtype[512];
    short searchfor[256];
    int slen;
    int flen;
    int i;
    //   if (to->type != bt_class)
    //   	return buf ;
    slen = strlen(to->sp->name);
    for (i = 0; i < slen; i++)
        searchfor[i] = to->sp->name[i];
    if (prm_cmangle && searchfor[0] == '_')
    {
        memcpy(searchfor, searchfor + 1, slen *sizeof(short));
        slen--;
    }
    if (to->type != bt_class)
    {
        if (scalarnonfloat(to))
        {
            if (to->type == bt_char || to->type == bt_unsignedchar)
            {
                #ifdef CC68K
                    sprintf(type, "'%c'", tn->sp->value.i + 3);
                #else 
                    sprintf(type, "'%c'", tn->sp->value.i);
                #endif 
            }
            else
                sprintf(type, "%d", tn->sp->value.i);
        }
        else
            sprintf(type, "%f", tn->sp->value.i);
        flen = strlen(type);
        for (i = 0; i < flen; i++)
            formattedtype[i] = type[i];
    }
    else if (tn->type == bt_string)
    {
        flen = tn->size;
        for (i = 0; i < flen; i++)
            formattedtype[i] = ((char*)tn->lst.head)[i];
    }
    else
    {
        typenum(type, tn);
        flen = strlen(type);
        for (i = 1; i < flen - 1; i++)
            formattedtype[i - 1] = type[i];
        flen -= 2;
    }
    return doreplace(buf, max, len, searchfor, formattedtype, slen, flen, FALSE)
        ;
}

//-------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -