brmdump.cpp

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C++ 代码 · 共 1,289 行 · 第 1/3 页

CPP
1,289
字号
/****************************************************************************
*
*                            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 <stdio.h>
#include <string.h>

extern "C" {
#include "brmtypes.h"
}
#include "brmdump.hpp"

#define _PCH_HEADER_ONLY
#include "pcheader.h"

#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif

#define USAGE           "USAGE:  brmdump [/pch] <filename> [<output filename>]\n"
#define FILE_ERR        "Unable to open %s\n"
#define BAD_CHECKSUM    "This is not a valid .BRM file.\n"
#define EOF_ERR         "Premature EOF at position %d.\n"
#define UNKNOWN_FLAG    "Unknown record flag %02X at position %d.\n"

#define NUM_BUCKETS     251

#define ANONYMOUS       "<ANONYMOUS>"


static FileP            infile;
static FileP            outfile;
static BRI_Header       header;
static OrderedList      record_list(TRUE);
static HashTable<DeclRec>       decl_list;
static HashTable<ScopeRec>      scope_list;
static HashTable<TypeRec>       type_list;
static HashTable<StringRec>     string_table;


void StringRec::DumpData()
/************************/
{
    fprintf( outfile, "String:  %08X\n", _index );
    fprintf( outfile, "    %s\n", _buf );
}


void DeclRec::DumpData()
/**********************/
{
    uint_8 *    buf;

    if( name_ptr != NULL ){
        buf = name_ptr->_buf;
    } else {
        buf = (uint_8 *) ANONYMOUS;
    }

    fprintf( outfile, "Declaration:  %08X\n", _index );
    fprintf( outfile, "    Flags:  %08X\n", attributes );
    fprintf( outfile, "    Name:   %s\n", buf );
    if( type_ptr != NULL ){
        fprintf( outfile, "    Type:   " );
        type_ptr->WriteType();
        fprintf( outfile, "  (%08X)\n", type_id );
    } else {
        fprintf( outfile, "    Type:   %08X\n", type_id );
    }
}


void DeclRec::ReIndex()
/*********************/
{
    name_ptr = string_table.Lookup( name_id );
    type_ptr = type_list.Lookup( type_id );
}


TypeRec::~TypeRec()
/*****************/
{
    if( ops != NULL ){
        delete[] ops;
    }
    if( typecode == BRI_TC_Function ){
        delete[] info.ra.args;
    }
}


void TypeRec::DumpData()
/**********************/
{
    fprintf( outfile, "Type:  %08X\n", _index );
    fprintf( outfile, "    " );
    WriteType();
    fprintf( outfile, "\n" );
}


void TypeRec::ReIndex()
/*********************/
{
    int         i;

    switch( typecode ){
        case BRI_TC_None:
        break;

        case BRI_TC_BaseType:
            info.bt.basetype = (BRI_BaseTypes) ops[0];
        break;

        case BRI_TC_Modifier:
            info.fp.flags = ops[0];
            info.fp.parent = type_list.Lookup( ops[1] );
        break;

        case BRI_TC_Pointer:
        case BRI_TC_Reference:
        case BRI_TC_TypeDef:
            info.p.parent = type_list.Lookup( ops[0] );
        break;

        case BRI_TC_PtrToMember:
            info.cm.host = type_list.Lookup( ops[0] );
            info.cm.member = type_list.Lookup( ops[1] );
        break;

        case BRI_TC_Array:
            info.se.size = ops[0];
            info.se.element = type_list.Lookup( ops[1] );
        break;

        case BRI_TC_Function:
            info.ra.args = new TypeRec *[num_ops];
            for( i=0; i<num_ops; i++ ){
                info.ra.args[i] = type_list.Lookup( ops[i] );
            }
        break;

        case BRI_TC_Class:
        case BRI_TC_Struct:
        case BRI_TC_Union:
        case BRI_TC_Enum:
            info.ns.name_ptr = string_table.Lookup( ops[0] );
            info.ns.symbol_ptr = decl_list.Lookup( ops[1] );
        break;

        case BRI_TC_BitField:
            info.w.width = ops[0];
        break;
    }
}


#define WRITE_TYPE(p,d)  if((p)!=NULL) \
                             (p)->WriteType(); \
                         else \
                             fprintf(outfile,"%08X",(d))

void TypeRec::WriteType()
/***********************/
{
    int         i;

    switch( typecode ){
        case BRI_TC_None:
        break;

        case BRI_TC_BaseType:
            switch( ops[0] ){
                case BRI_TYP_BOOL:
                    fprintf( outfile, "bool" );
                break;

                case BRI_TYP_CHAR:
                    fprintf( outfile, "char" );
                break;

                case BRI_TYP_SCHAR:
                    fprintf( outfile, "signed char" );
                break;

                case BRI_TYP_UCHAR:
                    fprintf( outfile, "unsigned char" );
                break;

                case BRI_TYP_WCHAR:
                    fprintf( outfile, "WCHAR" );
                break;

                case BRI_TYP_SSHORT:
                    fprintf( outfile, "short" );
                break;

                case BRI_TYP_USHORT:
                    fprintf( outfile, "unsigned short" );
                break;

                case BRI_TYP_SINT:
                    fprintf( outfile, "int" );
                break;

                case BRI_TYP_UINT:
                    fprintf( outfile, "unsigned int" );
                break;

                case BRI_TYP_SLONG:
                    fprintf( outfile, "long" );
                break;

                case BRI_TYP_ULONG:
                    fprintf( outfile, "unsigned long" );
                break;

                case BRI_TYP_SLONG64:
                    fprintf( outfile, "SLONG64" );
                break;

                case BRI_TYP_ULONG64:
                    fprintf( outfile, "ULONG64" );
                break;

                case BRI_TYP_FLOAT:
                    fprintf( outfile, "float" );
                break;

                case BRI_TYP_DOUBLE:
                    fprintf( outfile, "double" );
                break;

                case BRI_TYP_LONG_DOUBLE:
                    fprintf( outfile, "long double" );
                break;

                case BRI_TYP_VOID:
                    fprintf( outfile, "void" );
                break;

                case BRI_TYP_DOT_DOT_DOT:
                    fprintf( outfile, "..." );
                break;

                case BRI_TYP_GENERIC:
                    fprintf( outfile, "<GENERIC>" );
                break;
            }
        break;

        case BRI_TC_Modifier:
            if( info.fp.flags & 0x00000001 ){   // TF1_CONST
                fprintf( outfile, "const " );
            }
            WRITE_TYPE( info.fp.parent, ops[1] );
        break;

        case BRI_TC_Pointer:
            fputc('(', outfile);
            WRITE_TYPE( info.p.parent, ops[0] );
            fprintf( outfile, ") *" );
        break;

        case BRI_TC_PtrToMember:
            fputc('(', outfile);
            WRITE_TYPE( info.cm.host, ops[0] );
            fprintf( outfile, ")::(" );
            WRITE_TYPE( info.cm.member, ops[1] );
            fputc(')', outfile);
        break;

        case BRI_TC_Reference:
            fputc('(', outfile);
            WRITE_TYPE( info.p.parent, ops[0] );
            fprintf( outfile, ") &" );
        break;

        case BRI_TC_Array:
            fputc('(', outfile);
            WRITE_TYPE( info.se.element, ops[1] );
            fprintf( outfile, ")[%d]", info.se.size );
        break;

        case BRI_TC_Function:
            fputc('(', outfile);
            WRITE_TYPE( info.ra.args[0], ops[0] );
            fprintf( outfile, ")(" );
            for( i=1; i<num_ops; i++ ){
                WRITE_TYPE( info.ra.args[i], ops[i] );
                if( i < num_ops-1 ){
                    fputc(',', outfile);
                }
            }
            fputc(')', outfile);
        break;

        case BRI_TC_Class:
        case BRI_TC_Struct:
        case BRI_TC_Union:
        case BRI_TC_Enum:
            {
                uint_8 * buf;

                if( info.ns.name_ptr != NULL ){
                    buf = info.ns.name_ptr->_buf;
                } else {
                    buf = (uint_8 *)ANONYMOUS;
                }

                fprintf( outfile, "class %s", buf );
            }
        break;

        case BRI_TC_TypeDef:
            fprintf( outfile, "typedef " );
            WRITE_TYPE( info.p.parent, ops[0] );
        break;

        case BRI_TC_BitField:
            fprintf( outfile, "int : %d", info.w.width );
        break;
    }
}


void FileRec::DumpData()
/**********************/
{
    if( filename_ptr != NULL ){
        fprintf( outfile, "File name:  %s\n", filename_ptr->_buf );
    } else {
        fprintf( outfile, "File name:  %08X\n", filename_id );
    }
}


void FileRec::ReIndex()
/*********************/
{
    filename_ptr = string_table.Lookup( filename_id );
}


void PCHRec::DumpData()
/**********************/
{
    if( filename_ptr != NULL ){
        fprintf( outfile, "PCH name:  %s\n", filename_ptr->_buf );
    } else {
        fprintf( outfile, "PCH name:  %08X\n", filename_id );
    }
}


void PCHRec::ReIndex()
/*********************/
{
    filename_ptr = string_table.Lookup( filename_id );
}


void FileEndRec::DumpData()
/*************************/
{
    fprintf( outfile, "End file.\n" );
}


void TemplateRec::DumpData()
/**************************/
{
    if( filename_ptr != NULL ){
        fprintf( outfile, "Template source file:  %s\n", filename_ptr->_buf );
    } else {
        fprintf( outfile, "Template source file:  %08X\n", filename_id );
    }
}


void TemplateRec::ReIndex()
/*************************/
{
    filename_ptr = string_table.Lookup( filename_id );
}


void TemplateEndRec::DumpData()
/*****************************/
{
    fprintf( outfile, "End template.\n" );
}


void ScopeRec::DumpData()
/***********************/
{
    uint_8 *    buf;

⌨️ 快捷键说明

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