stubutil.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 879 行 · 第 1/2 页

C
879
字号
/****************************************************************************
*
*                            Open Watcom Project
*
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE
*               DESCRIBE IT HERE!
*
****************************************************************************/


#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "standard.h"
#include "cg.h"
#include "bckdef.h"
#include "cgdefs.h"
#include "typclass.h"
#include "typedef.h"
#include "cfloat.h"
#include "cgaux.h"
#include "model.h"
#include "hostsys.h"
#include "cgstub.h"
#include "feprotos.h"

#include "stubdata.h"

extern  pointer         CGAlloc(unsigned );

extern  type_def        *TypeAddress(cg_type);
extern  void            DumpTree(n *);
extern  type_def        *TypeDef(cg_type,type_length);
extern  uint            Length(char*);
extern  void            TypeFini();
extern  char            *CopyStr(char*,char*);
extern  void            TypeInit();
extern  type_def        *TypeAlias(cg_type,cg_type);
extern  byte            *Copy(void*,void*,uint);
extern  void            CGFree(pointer);
extern  void            BECloseFiles();
extern  void            exit(int);
extern  void            Action(char * str, ... );
extern  pointer         SafeRecurse(pointer(*)(),pointer);

extern  char    *EnvVar( char *env ) {
//====================================

    return( getenv( env ) );
}


extern  int     FStdOut() {
//=========================

    return( 1 );
}


extern  int     FCreate( char *file ) {
//=====================================

    return( creat( file, 0 ) );
}

extern  void    FShut( int io ) {
//================================

    close( io );
}


extern  void    FPut( int io, char *str, int len ) {
//==================================================

    write( io, str, len );
}

extern  char    *DoIToHS( char * buff, int buff_len, int i ) {
//=============================================================

    char        *hexdigits;
    int         length;

    buff_len = buff_len;
    length = 2*sizeof( int );
    buff += length + 3;
    *buff = NULLCHAR;
    hexdigits = "0123456789abcdef";
    while( --length >= 0 ) {
        *--buff = hexdigits[ i & 0x0f ];
        i = (unsigned)i >> 4;
    }
    *--buff = 'x';
    *--buff = '0';
    return( buff );
}

extern  char    *DoIToS( char * buff, int buff_len, signed_32 i ) {
//=================================================================

    char        *p;
    bool        neg;

    p = buff + buff_len;
    *p = NULLCHAR;
    if( i < 0 ) {
        neg = TRUE;
        i = -i;
    } else {
        neg = FALSE;
    }
    while( i != 0 ) {
        *--p = i % 10 + '0';
        i /= 10;
    }
    if( neg ) {
        *--p = '-';
    } else {
        *--p = '0';
    }
    return( p );
}

extern  void    PutFmt( int out, char * str, va_list list ) {

    char        *str2;
    char        buff[80];
    char        c;

    while( *str ) {
        if( *str == '%' ) {
            ++str;
            switch( *str ) {
            case '%':
                FPut( out, str, 1 );
                break;
            case 'd':
                str2 = DoIToS( buff, 79, va_arg( list, int ) );
                while( *str2 ) {
                    FPut( out, str2++, 1 );
                }
                break;
            case 'l':
                str2 = DoIToS( buff, 79, va_arg( list, signed_32 ) );
                while( *str2 ) {
                    FPut( out, str2++, 1 );
                }
                break;
#ifndef __386__
            case 'p':
                {
                    unsigned            offset, segment;

                    offset = va_arg( list, unsigned );
                    segment = va_arg( list, unsigned );
                    str2 = DoIToHS( buff, 79, segment );
                    while( *str2 ) FPut( out, str2++, 1 );
                    FPut( out, ":", 1 );
                    str2 = DoIToHS( buff, 79, offset );
                    while( *str2 ) FPut( out, str2++, 1 );
                    break;
                }
#else
            case 'p':
#endif
            case 'h':
                str2 = DoIToHS( buff, 79, va_arg( list, int ) );
                while( *str2 ) {
                    FPut( out, str2++, 1 );
                }
                break;
            case 'c':
                c = va_arg( list, char );
                FPut( out, &c, 1 );
                break;
            case 's':
                str2 = va_arg( list, char * );
                while( *str2 ) {
                    FPut( out, str2++, 1 );
                }
                break;
            case 'n':
                FPut( out, "\n", 1 );
                break;
            case 't':
                {
                    n   *nd;

                    nd = va_arg( list, n * );
                    if( nd != NULL ) {
                        VerNode( nd );
                    }
                    str2 = nd != NULL ? DoIToS( buff, 79, nd->id ) : "NULL";
                    FPut( out, "c", 1 );
                    FPut( out, "g", 1 );
                    FPut( out, "(", 1 );
                    while( *str2 ) {
                        FPut( out, str2++, 1 );
                    }
                    FPut( out, ")", 1 );
                    break;
                }
            default:
                FPut( out, "ZOIKS", 5 );
            }
            ++str;
        } else {
            FPut( out, str++, 1 );
        }
    }
}


extern  void    Code(char * str, ... ) {

    va_list     list;
    int         old;

    va_start( list, str );
    old = SetFile( CodeSeg );
    PutFmt( Out, str, list );
    SetFile( old );
    va_end( list );
}

extern  void    Put(char * str, ... ) {

    va_list     list;

    va_start( list, str );
    PutFmt( Out, str, list );
    va_end( list );
}

extern  void    Action(char * str, ... ) {

    va_list     list;

    va_start( list, str );
    PutFmt( Actions, str, list );
    va_end( list );
}

extern  void    TypDbg(char * str, ... ) {

    va_list     list;

    va_start( list, str );
    PutFmt( TypDebug, str, list );
    va_end( list );
}

extern  void    SymDbg(char * str, ... ) {

    va_list     list;

    va_start( list, str );
    PutFmt( SymDebug, str, list );
    va_end( list );
}

extern  void    PutError( int out, char * str, va_list list ) {

    FPut( out, "\nError! ", 8 );
    PutFmt( out, str, list );
    FPut( out, "\n", 1 );
}
extern  void    CGError(char * str, ... ) {

    va_list     list;
    int         old;

    old = SetFile( CodeSeg );

    va_start( list, str );
    PutError( 2,       str, list );
    va_end( list );
    va_start( list, str );
    PutError( Actions, str, list );
    va_end( list );
    va_start( list, str );
    PutError( Out,     str, list );
    va_end( list );

    BECloseFiles();
    SetFile( old );
    exit( 2010 );
}

extern  char    *Name( pointer sym ) {
//====================================

    static char buff[256];
    char        hexbuf[20];
    char        *end,*hex;

    end = CopyStr( "[", CopyStr( FEName( sym ), buff ) );
    #ifdef __386__
        hex = DoIToHS( hexbuf, 20, (int)sym );
        end = CopyStr( "]", CopyStr( hex, end ) );
    #else
        hex = DoIToHS( hexbuf, 20, (unsigned long)sym >> 16 );
        end = CopyStr( ":", CopyStr( hex, end ) );
        hex = DoIToHS( hexbuf, 20, (unsigned long)sym & 0xFFFF );
        end = CopyStr( "]", CopyStr( hex, end ) );
    #endif
    return( buff );
}

extern  char    *FtnTipe( dbg_ftn_type tipe ) {
//=============================================

    switch( tipe ) {
    case 0x47: return( "T_DBG_COMPLEX" );
    case 0x4f: return( "T_DBG_DCOMPLEX" );
    case 0x53: return( "T_DBG_NEAR_SCB" );
    case 0x55: return( "T_DBG_FAR_SCB" );
    default:   CGError( "Undefined FORTRAN debug type %d", tipe );
    }
    return( NULL );
}

extern  char    *LToS( signed_32 i ) {
//====================================

    return( DoIToS( UBuff, UBUFF_LEN, i ) );
}

extern  char    *Tipe( cg_type tipe ) {
//=====================================

    char        *res;
    type_def    *t;

    if( tipe >= T_FIRST_FREE ) {
        VerTipe( tipe, NULL );
        t = TypeAddress( tipe );
        res = LToS( t->refno );
        *--res = '_';
        *--res = 'T';
    } else {
        res = Tipes[ tipe ];
    }
    return( res );
}
extern  char    *Label( l *lb ) {
//===============================

    char *res;

    VerLabel( lb );
    res = LToS( lb->i );
    *--res = 'L';
    return( res );
}
extern  void    VDefLabel( l *lb ) {
//==================================

    if( ( lb->cref && !lb->cdef ) || ( lb->dref && !( lb->ddef||lb->idef) ) ) {
        CGError( "Label %s used but not defined\n", Label(lb) );
    }
}
extern  void    CRefLabel( l *lb ) {
//=================================

    if( lb->cdef != NULL && lb->cdef != InProc ) {
        CGError( "Referencing label %s outside its procedure\n", Label(lb) );
    }
    if( lb->cref != NULL && lb->cref != InProc ) {
        CGError( "Referencing label %s outside its procedure\n", Label(lb) );
    }
    if( lb->ddef || lb->dref ) {
        CGError( "Illegal control flow reference to code label %s\n", Label(lb) );
    }
    lb->cref = InProc;
}
extern  void    CDefLabel( l *lb ) {
//=================================

    if( lb->cref != NULL && lb->cref != InProc ) {
        CGError( "Referencing label %s outside its procedure\n", Label(lb) );
    }
    if( lb->cdef != NULL || lb->ddef ) {
        CGError( "Defining label %s twice\n", Label(lb) );
    }
    if( lb->dref ) {
        CGError( "Defining label %s in both code and data\n", Label(lb) );
    }
    lb->cdef = InProc;
}
extern  void    DRefLabel( l *lb ) {
//=================================

    if( lb->cdef || lb->cref ) {
        CGError( "Using label %s in both code and data\n", Label(lb) );
    }
    lb->dref = 1;
}
extern  void    DDefLabel( l *lb ) {
//=================================

    if( lb->cdef || lb->cref ) {
        CGError( "Using label %s in both code and data\n", Label(lb) );
    }
    if( lb->ddef ) {
        CGError( "Defining label %s twice\n", Label(lb) );
    }
    lb->ddef = 1;
}
extern  void    DDefILabel( l *lb ) {
//=================================

    if( lb->cdef || lb->cref ) {
        CGError( "Using label %s in both code and data\n", Label(lb) );
    }
    lb->idef = 1;

⌨️ 快捷键说明

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