translat.c

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

C
838
字号

    if( cmdOpts->Fo && !status->disable_Fo ) {
        newpath = PathConvert( cmdOpts->Fo_value->data, '"' );
        AppendFmtCmdLine( compCmdLine, CL_C_OPTS_SECTION, "-fo=%s", newpath );
    }

    if( cmdOpts->Gh ) {
        AppendCmdLine( compCmdLine, CL_C_OPTS_SECTION, "-ep" );
    }

    if( cmdOpts->Gy ) {
        AppendCmdLine( compCmdLine, CL_C_OPTS_SECTION, "-zm" );
    }

    if( cmdOpts->LD ) {
        AppendCmdLine( compCmdLine, CL_C_OPTS_SECTION, "-bd" );
        if( cmdOpts->threads_linking==OPT_threads_linking_default ) {
            cmdOpts->threads_linking = OPT_threads_linking_MT;
        }
    }

    switch( cmdOpts->threads_linking ) {
      case OPT_threads_linking_MD:
      case OPT_threads_linking_MDd:
        AppendCmdLine( compCmdLine, CL_C_OPTS_SECTION, "-bm" );
        if( !cmdOpts->_10x ) {
            AppendCmdLine( compCmdLine, CL_C_OPTS_SECTION, "-br" );
        }
        break;
      case OPT_threads_linking_ML:
      case OPT_threads_linking_MLd:
        break;
      case OPT_threads_linking_MT:
      case OPT_threads_linking_MTd:
        AppendCmdLine( compCmdLine, CL_C_OPTS_SECTION, "-bm" );
        break;
      case OPT_threads_linking_default:
        /* let the compiler use its default */
        break;
      default:
        Zoinks();
    }

    switch( cmdOpts->debug_info ) {
      case OPT_debug_info_Z7:
        AppendCmdLine( compCmdLine, CL_C_OPTS_SECTION, "-hd" );
        status->debugLevel = 2;
        break;
      case OPT_debug_info_Zd:
        AppendCmdLine( compCmdLine, CL_C_OPTS_SECTION, "-hd" );
        status->debugLevel = 1;
        break;
      case OPT_debug_info_Zi:
        /* unsupported */
      case OPT_debug_info_default:
        /* do nothing */
        break;
      default:
        Zoinks();
    }

    if( cmdOpts->Zl ) {
        AppendCmdLine( compCmdLine, CL_C_OPTS_SECTION, "-zl" );
    }

    if( cmdOpts->Zp ) {
        AppendFmtCmdLine( compCmdLine, CL_C_OPTS_SECTION, "-zp%d",
                          cmdOpts->Zp_value );
    }
}


/*
 * Translate options related to optimization.
 */
static void optimization_opts( struct XlatStatus *status,
                               OPT_STORAGE *cmdOpts, CmdLine *compCmdLine )
/*************************************************************************/
{
    if( cmdOpts->opt_level != OPT_opt_level_Od ) {
        status->opt_od = 0;

        switch( cmdOpts->opt_level ) {
          case OPT_opt_level_O1:                /* minimize size */
            cmdOpts->Og = 1;
            cmdOpts->opt_size_time = OPT_opt_size_time_Os;
            cmdOpts->Oy = 1;
            cmdOpts->Ob_value = 1;
            cmdOpts->stack_probes = OPT_stack_probes_Gs;
            cmdOpts->Gf = 1;
            cmdOpts->GF = 1;
            cmdOpts->Gy = 1;
            break;
          case OPT_opt_level_O2:                /* maximize speed */
            cmdOpts->Og = 1;
            cmdOpts->Oi = 1;
            cmdOpts->opt_size_time = OPT_opt_size_time_Ot;
            cmdOpts->Oy = 1;
            cmdOpts->Ob_value = 1;
            cmdOpts->stack_probes = OPT_stack_probes_Gs;
            cmdOpts->Gf = 1;
            cmdOpts->GF = 1;
            cmdOpts->Gy = 1;
            break;
          case OPT_opt_level_Ox:
            cmdOpts->Ob_value = 1;
            cmdOpts->Og = 1;
            cmdOpts->Oi = 1;
            cmdOpts->opt_size_time = OPT_opt_size_time_Ot;
            cmdOpts->Oy = 1;
            cmdOpts->stack_probes = OPT_stack_probes_Gs;
            status->opt_ol_plus = 1;
            status->opt_om = 1;
            status->opt_on = 1;
            status->opt_ox = 1;
          case OPT_opt_level_default:
            /* let the compiler use its default */
            break;
          default:
            Zoinks();
        }


        if( cmdOpts->Oa ) {
            status->opt_oa = 1;
        }

        if( cmdOpts->Ob_value == 0 ) {
            AppendCmdLine( compCmdLine, CL_C_OPTS_SECTION, "-oe=0" );
        }

        if( cmdOpts->Og ) {
            status->opt_ol = 1;
            status->opt_ot = 1;
        }

        if( cmdOpts->Oi ) {
            status->opt_oi = 1;
        }

        if( cmdOpts->Op ) {
            status->opt_op = 1;
        }

        #ifdef __TARGET_386__
            if( cmdOpts->Oy ) {
                status->opt_of = 0;
            }
        #endif
    }

    switch( cmdOpts->opt_size_time ) {
      case OPT_opt_size_time_Os:
        status->opt_os = 1;
        break;
      case OPT_opt_size_time_Ot:
        status->opt_ot = 1;
        break;
      case OPT_opt_size_time_default:
        break;
      default:
        Zoinks();
    }
}


/*
 * Parse compiler options.
 */
static void compiler_opts( struct XlatStatus *status, OPT_STORAGE *cmdOpts,
                           CmdLine *compCmdLine )
/*************************************************************************/
{
    preprocessor_opts( status, cmdOpts, compCmdLine );  /* should be first */
    precomp_header_opts( status, cmdOpts, compCmdLine );
    if( !status->disable_c ) {
        optimization_opts( status, cmdOpts, compCmdLine );  /* before object_opts */
        object_opts( status, cmdOpts, compCmdLine );
        c_plus_plus_opts( status, cmdOpts, compCmdLine );
    }
    misc_opts( status, cmdOpts, compCmdLine );

    if( status->disable_c ) {
        cmdOpts->c = 1;         /* tell mainline to skip link phase */
    }
}


/*
 * Parse linker options.
 */
static void linker_opts( struct XlatStatus *status, OPT_STORAGE *cmdOpts,
                         CmdLine *linkCmdLine )
/***********************************************************************/
{
    OPT_STRING *        optStr;

    if( cmdOpts->F ) {
        AppendFmtCmdLine( linkCmdLine, CL_L_OPTS_SECTION, "/STACK:%s",
                          cmdOpts->F_value->data );
    }

    if( cmdOpts->Fe ) {
        AppendFmtCmdLine( linkCmdLine, CL_L_OPTS_SECTION, "/OUT:%s",
                          cmdOpts->Fe_value->data );
    }

    if( cmdOpts->Fm ) {
        if( cmdOpts->Fm_value != NULL ) {
            AppendFmtCmdLine( linkCmdLine, CL_L_OPTS_SECTION, "/MAP:%s",
                              cmdOpts->Fm_value->data );
        } else {
            AppendCmdLine( linkCmdLine, CL_L_OPTS_SECTION, "/MAP" );
        }
    }

    if( cmdOpts->LD ) {
        AppendCmdLine( linkCmdLine, CL_L_OPTS_SECTION, "/DLL" );
    }

    if( cmdOpts->link ) {
        optStr = cmdOpts->link_value;
        while( optStr != NULL ) {
            AppendFmtCmdLine( linkCmdLine, CL_L_OPTS_SECTION, "%s",
                              optStr->data );
            optStr = optStr->next;
        }
    }

    switch( cmdOpts->debug_info ) {
      case OPT_debug_info_Zd:
        AppendCmdLine( linkCmdLine, CL_L_OPTS_SECTION, "/DEBUG" );
        status->debugLevel = 1;
        break;
      case OPT_debug_info_Z7:
        /* fall through */
      case OPT_debug_info_Zi:
        AppendCmdLine( linkCmdLine, CL_L_OPTS_SECTION, "/DEBUG" );
        status->debugLevel = 2;
        break;
      case OPT_debug_info_default:
        /* do nothing */
        break;
      default:
        Zoinks();
    }
}


/*
 * Activate default options.
 */
static void default_opts(struct XlatStatus *status, OPT_STORAGE *cmdOpts,
                         CmdLine *compCmdLine, CmdLine *linkCmdLine )
{
    OPT_STRING *curr;

    /*** Emit default options if so desired ***/
    if (!cmdOpts->nowopts)
    {
        AppendCmdLine( compCmdLine, CL_C_OPTS_SECTION, "-bt=nt" );
        AppendCmdLine( compCmdLine, CL_C_OPTS_SECTION, "-zlf" );

        #ifdef __TARGET_386__
            AppendCmdLine( compCmdLine, CL_C_OPTS_SECTION, "-ei" );
        #endif

        AppendCmdLine( compCmdLine, CL_C_OPTS_SECTION, "-zq" );
        AppendCmdLine( linkCmdLine, CL_L_OPTS_SECTION, "/nologo" );
    } /* if */

    /*** Add any options meant for the Watcom tools ***/
    if (cmdOpts->passwopts)
    {
        for (curr = cmdOpts->passwopts_value; curr; curr = curr->next)
        {
            AppendCmdLine(compCmdLine, CL_C_OPTS_SECTION, curr->data);
        }
    }
} /* default_opts() */


/*
 * Activate options which have been parsed but not yet turned on.
 */
static void merge_opts( struct XlatStatus *status, OPT_STORAGE *cmdOpts,
                        CmdLine *compCmdLine, CmdLine *linkCmdLine )
/**********************************************************************/
{
    char                buf[128];
    char *              macro;

    /*** Handle /D and /U ***/
    for( ;; ) {                                 /* defines */
        macro = GetNextDefineMacro();
        if( macro == NULL )  break;
        AppendFmtCmdLine( compCmdLine, CL_C_MACROS_SECTION, "-d%s", macro );
    }
    for( ;; ) {                                 /* undefines */
        macro = GetNextUndefineMacro();
        if( macro == NULL )  break;
        AppendFmtCmdLine( compCmdLine, CL_C_MACROS_SECTION, "-u%s", macro );
    }

    /*** Merge optimization options ***/
    if( status->opt_od ) {
        AppendCmdLine( compCmdLine, CL_C_OPTS_SECTION, "-od" );
    } else {
        strcpy( buf, "-o" );
        #ifdef __TARGET_386__
            if( status->opt_of )   strcat( buf, "f" );
            if( status->opt_or )   strcat( buf, "r" );
        #endif
        if( status->opt_oa )       strcat( buf, "a" );
        if( status->opt_oi )       strcat( buf, "i" );
        if( status->opt_ol )       strcat( buf, "l" );
        if( status->opt_ol_plus )  strcat( buf, "l+" );
        if( status->opt_om )       strcat( buf, "m" );
        if( status->opt_on )       strcat( buf, "n" );
        if( status->opt_op )       strcat( buf, "p" );
        if( status->opt_os )       strcat( buf, "s" );
        if( status->opt_ot )       strcat( buf, "t" );
        if( status->opt_ox )       strcat( buf, "x" );
        if( strcmp( buf, "-o" ) != 0 ) {
            AppendCmdLine( compCmdLine, CL_C_OPTS_SECTION, buf );
        }
    }

    /*** Handle debugging options ***/
    switch( status->debugLevel ) {
      case 0:
        /* no debugging info */
        break;
      case 1:
        AppendCmdLine( compCmdLine, CL_C_OPTS_SECTION, "-d1" );
        break;
      case 2:
        if (!cmdOpts->lesswd) {
            AppendCmdLine( compCmdLine, CL_C_OPTS_SECTION, "-d2" );
        } else {
            AppendCmdLine( compCmdLine, CL_C_OPTS_SECTION, "-d1" );
        }
        break;
      default:
        Zoinks();
    }

    if( !status->charTypeUnsigned ) {
        AppendCmdLine( compCmdLine, CL_C_OPTS_SECTION, "-j" );
    }
    AppendFmtCmdLine( compCmdLine, CL_C_OPTS_SECTION, "-w%d", status->warnLevel );

    if( cmdOpts->showwopts ) {
        AppendCmdLine( linkCmdLine, CL_L_OPTS_SECTION, "/showwopts" );
    }

    if( cmdOpts->noinvoke ) {
        AppendCmdLine( linkCmdLine, CL_L_OPTS_SECTION, "/noinvoke" );
    }

    if( cmdOpts->nowopts ) {
        AppendCmdLine( linkCmdLine, CL_L_OPTS_SECTION, "/nowopts" );
    }
}


/*
 * This function is for options which affect all source files differently.
 * Given the filename, it updates the given command line appropriately.  Any
 * CmdLine pointer which is NULL is ignored.
 */
void HandleFileTranslate( const char *filename, CmdLine *compCmdLine,
                          CmdLine *linkCmdLine )
/*******************************************************************/
{
    char                drive[_MAX_DRIVE];
    char                dir[_MAX_DIR];
    char                fname[_MAX_FNAME];
    char                fullPath[_MAX_PATH];
    char *              newpath;

    linkCmdLine = linkCmdLine;

    /*** Handle the /P switch ***/
    if( status.preprocessToFile ) {
        if( compCmdLine != NULL ) {
            _splitpath( filename, drive, dir, fname, NULL );
            _makepath( fullPath, drive, dir, fname, ".i" );
            newpath = PathConvert( fullPath, '"' );
            AppendFmtCmdLine( compCmdLine, CL_C_OPTS_SECTION, "-fo=%s",
                              newpath );
        }
    }
}


/*
 * Translate scanned MS options to Watcom options.
 */
void OptionsTranslate( OPT_STORAGE *cmdOpts, CmdLine *compCmdLine,
                       CmdLine *linkCmdLine )
/****************************************************************/
{
    /*** Parse the /nologo switch now so we can print the banner ***/
    init_status( &status );
    if( cmdOpts->nologo ) {
        QuietModeMessage();
    } else {
        BannerMessage();
    }

    /*** Parse everything ***/
    unsupported_opts( cmdOpts );
    default_opts( &status, cmdOpts, compCmdLine, linkCmdLine );
    compiler_opts( &status, cmdOpts, compCmdLine );
    linker_opts( &status, cmdOpts, linkCmdLine );
    merge_opts( &status, cmdOpts, compCmdLine, linkCmdLine );
}

⌨️ 快捷键说明

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