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

📄 scanner.c

📁 cg编译器
💻 C
📖 第 1 页 / 共 3 页
字号:
/****************************************************************************\
Copyright (c) 2002, NVIDIA Corporation.

NVIDIA Corporation("NVIDIA") supplies this software to you in
consideration of your agreement to the following terms, and your use,
installation, modification or redistribution of this NVIDIA software
constitutes acceptance of these terms.  If you do not agree with these
terms, please do not use, install, modify or redistribute this NVIDIA
software.

In consideration of your agreement to abide by the following terms, and
subject to these terms, NVIDIA grants you a personal, non-exclusive
license, under NVIDIA's copyrights in this original NVIDIA software (the
"NVIDIA Software"), to use, reproduce, modify and redistribute the
NVIDIA Software, with or without modifications, in source and/or binary
forms; provided that if you redistribute the NVIDIA Software, you must
retain the copyright notice of NVIDIA, this notice and the following
text and disclaimers in all such redistributions of the NVIDIA Software.
Neither the name, trademarks, service marks nor logos of NVIDIA
Corporation may be used to endorse or promote products derived from the
NVIDIA Software without specific prior written permission from NVIDIA.
Except as expressly stated in this notice, no other rights or licenses
express or implied, are granted by NVIDIA herein, including but not
limited to any patent rights that may be infringed by your derivative
works or by other works in which the NVIDIA Software may be
incorporated. No hardware is licensed hereunder. 

THE NVIDIA SOFTWARE IS BEING PROVIDED ON AN "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING WITHOUT LIMITATION, WARRANTIES OR CONDITIONS OF TITLE,
NON-INFRINGEMENT, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
ITS USE AND OPERATION EITHER ALONE OR IN COMBINATION WITH OTHER
PRODUCTS.

IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT,
INCIDENTAL, EXEMPLARY, CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, LOST PROFITS; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) OR ARISING IN ANY WAY
OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE
NVIDIA SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT,
TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\****************************************************************************/
// scanner.c
//

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if 0
#include <ieeefp.h>
#else
#define isinff(x) (((*(long *)&(x) & 0x7f800000L)==0x7f800000L) && \
                   ((*(long *)&(x) & 0x007fffffL)==0000000000L))
#endif

#include "slglobals.h"

const char *Build_Date = __DATE__;
const char *Build_Time = __TIME__;

typedef struct FileInputSrc {
    InputSrc            base;
    FILE                *fd;
    char                save_cnt;
    char                save[3];
} FileInputSrc;

typedef struct StringInputSrc {
    InputSrc base;
    char *p;
} StringInputSrc;

static int eof_scan(InputSrc *is)
{
    return EOF;
} // eof_scan

static void noop(InputSrc *in, int ch) {}

static InputSrc eof_inputsrc = { 0, &eof_scan, &eof_scan, &noop };

static int byte_scan(InputSrc *);
static int nextchar(FileInputSrc *);
static void ungetchar(FileInputSrc *, int);

#define EOL_SY '\n'

#if defined(_WIN32)
#define DBG_BREAKPOINT() __asm int 3
#else
#define DBG_BREAKPOINT()
#endif

#if defined(_WIN32)
__int64 RDTSC ( void ) {

    __int64 v;

    __asm __emit 0x0f
    __asm __emit 0x31
    __asm mov dword ptr v, eax
    __asm mov dword ptr v+4, edx

    return v;
}
#endif

int InitScanner(CgStruct *Cg)
{
    // Add various atoms needed by the CPP line scanner:
    if (!InitCPP())
        return 0;

    Cg->mostRecentToken = 0;
    Cg->tokenLoc = &Cg->ltokenLoc;

    Cg->ltokenLoc.file = 0;
    Cg->ltokenLoc.line = 0;
    Cg->errorCount = 0;
    Cg->warningCount = 0;
    Cg->lineCount = 0;
    Cg->AllowSemanticParseErrors = 0;

    Cg->currentInput = &eof_inputsrc;

    return 1;
} // InitScanner

int SetInputFile(const char *fname)
{
    FileInputSrc *in = malloc(sizeof(FileInputSrc));
    memset(in, 0, sizeof(FileInputSrc));
    if (fname) {
        if (!Cg->options.Quiet)
            printf("%s\n", fname);
        in->base.name = LookUpAddString(atable, fname);
        in->fd = fopen(fname, "r");
        if (!in->fd) {
            printf(OPENSL_TAG ": cannot open input file \"%s\"\n", fname);
            free(in);
            return 0;
        }
    } else {
        in->fd = stdin;
        in->base.name = LookUpAddString(atable, "<stdin>");
    }
    in->base.line = 1;
    in->base.scan = byte_scan;
    in->base.getch = (int (*)(InputSrc *)) nextchar;
    in->base.ungetch = (void (*)(InputSrc *, int)) ungetchar;
    in->base.prev = Cg->currentInput;
    Cg->currentInput = &in->base;
#if 0 && !defined(_DEBUG)
    if (Cg->options.TraceScanner) {
        __int64 s,e;
        s = RDTSC();
        while (Cg->currentInput->scan(Cg->currentInput) > 0)
            /* empty statement */ ;
        e = RDTSC();
        printf("%d cycles\n", (int)(e-s));
        return 0;
    }
#endif
    return 1;
} // SetInputFile

static int str_getch(StringInputSrc *in)
{
    if (*in->p)
        return *in->p++;
    Cg->currentInput = in->base.prev;
    free(in);
    return ' ';
} // str_getch

static void str_ungetch(StringInputSrc *in, int ch) {
    if (in->p[-1] == ch) in->p--;
} // str_ungetch

int ScanFromString(char *s)
{
    StringInputSrc *in = malloc(sizeof(StringInputSrc));
    memset(in, 0, sizeof(StringInputSrc));
    in->p = s;
    in->base.line = 1;
    in->base.scan = byte_scan;
    in->base.getch = (int (*)(InputSrc *))str_getch;
    in->base.ungetch = (void (*)(InputSrc *, int))str_ungetch;
    in->base.prev = Cg->currentInput;
    Cg->currentInput = &in->base;
    return 1;
} // ScanFromString;

int FreeScanner(CgStruct *Cg)
{
    FinalCPP();
    if (Cg->warningCount || Cg->errorCount || !Cg->options.Quiet) {
        fprintf(Cg->options.listfd, "%d lines", Cg->lineCount);
        if (Cg->warningCount)
            fprintf(Cg->options.listfd, ", %d warnings", Cg->warningCount);
        fprintf(Cg->options.listfd, ", %d errors.\n", Cg->errorCount);
    }
    return Cg->errorCount;
} // FreeScanner

int GetErrorCount(void)
{
    return Cg->errorCount;
} // GetErrorCount

/*
 * bumpErrorCount() - Useful for setting breakpoints when debugging.
 *
 */

void bumpErrorCount(void)
{
    Cg->errorCount++;
    if (Cg->options.TrapOnError) {
        DBG_BREAKPOINT();
    }
} // bumbErrorCount

/*
 * bumpWarningCount() - Useful for setting breakpoints when debugging.
 *
 */

void bumpWarningCount(void)
{
    Cg->warningCount++;
    if (Cg->options.TrapOnError) {
        DBG_BREAKPOINT();
    }
} // bumpWarningCount

// Called by yyparse on an error:

void yyerror(const char *s)
{
    if (!Cg->options.ErrorMode) {
        if (Cg->ltokenLoc.file) {
            fprintf(Cg->options.listfd, "%s(%d) : error C0000: ",
                    GetAtomString(atable, Cg->ltokenLoc.file), Cg->ltokenLoc.line);
        } else {
            fprintf(Cg->options.listfd, "(%d) : error C0000: ", Cg->currentInput->line);
        }
        fprintf(Cg->options.listfd, "%s at token \"%s\"\n", s,
                GetAtomString(atable, Cg->mostRecentToken));
        Cg->AllowSemanticParseErrors = 1;
    }
    bumpErrorCount();
} // yyerror

/*
 * SemanticParseError() - Compiler generated semantic error inside an error rule.
 *
 */

void SemanticParseError(SourceLoc *loc, int num, const char *mess, ...)
{
    va_list args;

    if (Cg->AllowSemanticParseErrors) {
        if (!Cg->options.ErrorMode) {
            if (loc->file) {
                fprintf(Cg->options.listfd, "%s(%d) : error C%04d: ",
                        GetAtomString(atable, loc->file), loc->line, num);
            } else {
                fprintf(Cg->options.listfd, "(%d) : error C%04d: ", loc->line, num);
            }
            va_start(args, mess);
            vfprintf(Cg->options.listfd, mess, args);
            va_end(args);
            fprintf(Cg->options.listfd, "\n");
            bumpErrorCount();
        } else {
            MarkErrorPosHit(loc);
        }

        Cg->AllowSemanticParseErrors = 0;
    }
} // SemanticParseError

/*
 * SemanticError() - Compiler generated semantic error.
 *
 */

void SemanticError(SourceLoc *loc, int num, const char *mess, ...)
{
    va_list args;

    if (!Cg->options.ErrorMode) {
        if (loc->file) {
            fprintf(Cg->options.listfd, "%s(%d) : error C%04d: ",
                    GetAtomString(atable, loc->file), loc->line, num);
        } else {
            fprintf(Cg->options.listfd, "(%d) : error C%04d: ", loc->line, num);
        }
        va_start(args, mess);
        vfprintf(Cg->options.listfd, mess, args);
        va_end(args);
        fprintf(Cg->options.listfd, "\n");
        bumpErrorCount();
    } else {
        MarkErrorPosHit(loc);
    }
} // SemanticError

/*
 * InternalError() - Internal compiler error.
 *
 */

void InternalError(SourceLoc *loc, int num, const char *mess, ...)
{
    va_list args;

    if (loc->file) {
        fprintf(Cg->options.listfd, "%s(%d) : error C%04d: ",
                GetAtomString(atable, loc->file), loc->line, num);
    } else {

⌨️ 快捷键说明

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