asmline.c

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

C
738
字号
        AsmErr( CANNOT_OPEN_INCLUDE_FILE, fullpath );
        return( ERROR );
    } else {
        new = push_flist( tmp, TRUE );
        new->file = file;
        return( NOT_ERROR );
    }
}

static char *input_get( char *string )
/************************************/
{
    line_list   *inputline;
    file_list   *inputfile;

    /* Check the line_queue first; then the file_stack */

    Globals.expand_count = 0;
    while( line_queue != NULL ) {
        if( line_queue->head != NULL ) {
            inputline = line_queue->head;
            strcpy( string, inputline->line );
            line_queue->head = inputline->next;
            if( line_queue->head == NULL )
                line_queue->tail = NULL;
            AsmFree( inputline->line );
            AsmFree( inputline );
            return( string );
        }
        PopLineQueue();
    }

    while( file_stack != NULL ) {
        inputfile = file_stack;
        if( inputfile->is_a_file ) {
            if( get_asmline( string, MAX_LINE_LEN, inputfile->file ) ) {
                LineNumber++;
                return( string );
            }
            /* EOF is reached */
            file_stack = inputfile->next;
            fclose( inputfile->file );
            LineNumber = inputfile->line_num;
            AsmFree( inputfile );
        } else {
            /* this "file" is just a line queue for a macro */
            inputline = inputfile->lines->head;
            LineNumber++;
            if( inputline != NULL ) {
                strcpy( string, inputline->line );
                inputfile->lines->head = inputline->next;
                AsmFree( inputline->line );
                AsmFree( inputline );
                return( string );
            }
            MacroEnd( FALSE );

            file_stack = inputfile->next;
            AsmFree( inputfile->lines );
            LineNumber = inputfile->line_num;
            AsmFree( inputfile );
        }
    }
    return( NULL );
}

char *ReadTextLine( char *string )
/********************************/
{
    /* get a new line, first checking the line queue & the file stack,
     * then looking in the assembly file itself
     * put it into string and return a pointer to the front
     */

    char *line;

    string[0] = '\0';
    line = input_get( string );
    if( line != NULL )
        return( line );
    if( !get_asmline( string, MAX_LINE_LEN, AsmFiles.file[ASM] ) ) {
        return( NULL );
    }
    LineNumber++;
    return( string );
}

static char *join_multiline_cmds( char *line, int len )
/*****************************************************/
{
    char        *ptr;
    int         linelen;

    linelen = strlen( line );

    if( linelen == 0 )
        return( line );

    /* if the last non-whitespace character is a comma, join the next line on */

    for( ptr = line + linelen - 1 ; isspace( *ptr ); ptr-- )
        ;

    /* ptr now points at the last non-whitespace char */

    if( *ptr != ',' ) {
        return( line );
    }
    ptr++;

    ptr = ScanLine( line + linelen, len - linelen );
    return( line );
}

char *ScanLine( char *string, int len )
/*************************************/
{
    char        *line;
    char        buffer[MAX_LINE_LEN];

    line = ( len < MAX_LINE_LEN ? buffer : string );
    line = ReadTextLine( line );
    if( line != NULL ) {

        prep_line_for_conditional_assembly( line );
        if( line != string ) {          /* comparing the pointers */
            if( strlen( line ) <= len ) {
                strcpy( string, line );
            } else {
                AsmError( ASM_CODE_TOO_LONG );
                return( NULL );
            }
        }
        join_multiline_cmds( string, len );
    }
    return( line );
}

void AsmCodeByte( unsigned char byte )
/************************************/
{
    if( CurrSeg != NULL ) {
        if( CurrSeg->seg->e.seginfo->iscode == SEGTYPE_UNDEF ) {
            CurrSeg->seg->e.seginfo->iscode = SEGTYPE_ISCODE;
        }
    }
    OutSelect( FALSE );
    AsmByte( byte );
}

void AsmDataByte( unsigned char byte )
/************************************/
{
    OutSelect( TRUE );
    AsmByte( byte );
}

static bool CheckHaveSeg( void )
/******************************/
{
    if( CurrSeg != NULL )
        return( TRUE );

    if( CheckSeg ) {
        AsmError( DATA_EMITTED_WITH_NO_SEGMENT );
        write_to_file = FALSE;
        CheckSeg = FALSE;
    }
    return( FALSE );
}

void AddStringToIncludePath( char *string )
/*****************************************/
{
    char *tmp;

    while( isspace( *string ) )
        string++;
    if( IncludePath == NULL ) {
        IncludePath = AsmAlloc( strlen( string ) + 1 );
        strcpy( IncludePath, string );
    } else {
        tmp = IncludePath;
        IncludePath = AsmAlloc( strlen( tmp ) + strlen( INCLUDE_PATH_DELIM ) +
                                strlen( string ) + 1 );
        strcpy( IncludePath, tmp );
        strcat( IncludePath, INCLUDE_PATH_DELIM );
        strcat( IncludePath, string );
        AsmFree( tmp );
    }
}

void FreeIncludePath( void )
/**************************/
{
    AsmFree( IncludePath );
}

void PushMacro( const char *name, bool hidden )
/*********************************************/
{
    file_list *new;

    DebugMsg(( "PUSH_MACRO\n" ));
    new = push_flist( name, FALSE );
    new->lines = line_queue;
    new->hidden = hidden;
    line_queue = line_queue->next;
}

#ifdef DEBUG_OUT
static void dbg_output( void )
/****************************/
/* For debugging use only; print out a simplied version of the source line
   after it is parsed and the expression is evaluated */
{
    if( Options.debug ) {
        int             i;

        DebugMsg(("Line: %lu ", LineNumber ));
        DebugMsg(("Output :"));
        for( i = 0; i < Token_Count; i++ ) {
            switch( AsmBuffer[i]->token ) {
            case T_NUM:
                DebugMsg(( " %d ", AsmBuffer[i]->value ));
                break;
            case T_STRING:
                DebugMsg(( " '%s' ", AsmBuffer[i]->string_ptr));
                break;
            case T_OP_SQ_BRACKET:
                DebugMsg(( " %s ", "[" ));
                break;
            case T_CL_SQ_BRACKET:
                DebugMsg(( " %s ", "]" ));
                break;
            case T_COLON:
                DebugMsg(( " %s ", ":" ));
                break;
            case T_RES_ID:
                switch( AsmBuffer[i]->value ) {
                case T_PTR:
                    DebugMsg(( " %s ", "Ptr" ));
                    break;
                case T_NEAR:
                    DebugMsg(( " %s ", "Near" ));
                    break;
                case T_FAR:
                    DebugMsg(( " %s ", "Far" ));
                    break;
                case T_PROC:
                    DebugMsg(( " %s ", "Proc" ));
                    break;
                case T_BYTE:
                case T_SBYTE:
                    DebugMsg(( " %s ", "Byte" ));
                    break;
                case T_WORD:
                case T_SWORD:
                    DebugMsg(( " %s ", "Word" ));
                    break;
                case T_DWORD:
                case T_SDWORD:
                    DebugMsg(( " %s ", "DWord" ));
                    break;
                case T_PWORD:
                case T_FWORD:
                    DebugMsg(( " %s ", "FWord" ));
                    break;
                case T_QWORD:
                    DebugMsg(( " %s ", "QWord" ));
                    break;
                case T_TBYTE:
                    DebugMsg(( " %s ", "TByte" ));
                    break;
                case T_OWORD:
                    DebugMsg(( " %s ", "OWord" ));
                    break;
                case T_ABS:
                    DebugMsg(( " %s ", "Abs" ));
                    break;
                default:
                    DebugMsg((" %s ", AsmBuffer[i]->string_ptr ));
                    break;
                }
                break;
            default:
                DebugMsg((" %s ", AsmBuffer[i]->string_ptr ));
                break;
            }
        }
        DebugMsg(("\n"));
    } /* if Options.debug */
}
#endif
#endif

void AsmByte( unsigned char byte )
/********************************/
/* Write a byte to the object file */
{
#if defined( _STANDALONE_ )
    if( CheckHaveSeg() ) {
        (CurrSeg->seg->e.seginfo->current_loc)++;
        if( CurrSeg->seg->e.seginfo->current_loc >=
            CurrSeg->seg->e.seginfo->segrec->d.segdef.seg_length ) {
            CurrSeg->seg->e.seginfo->segrec->d.segdef.seg_length = CurrSeg->seg->e.seginfo->current_loc;
        }
        if( Parse_Pass != PASS_1 && write_to_file ) {
            AsmCodeBuffer[BufSize] = byte;
            ++BufSize;
            if( BufSize >= MAX_LEDATA_THRESHOLD ) {
                FlushCurrSeg();
            }
        }
    }
    PassTotal++;
#else
    AsmCodeBuffer[AsmCodeAddress] = byte;
    ++AsmCodeAddress;
#endif
}

void AsmLine( char *string )
/**************************/
{
#if defined( _STANDALONE_ )
    int             count;

    // Token_Count is the number of tokens scanned
    Token_Count = AsmScan( string );
    Token_Count = ExpandMacro( Token_Count );

    if( ExpandTheWorld( 1, TRUE, TRUE ) == ERROR ) {
        write_to_file = FALSE;
        return;
    }

    if( CurrProc != NULL && !DefineProc ) {
        for( count = 0; count < Token_Count; count++ ) {
            switch( ExpandProcString( count ) ) {
            case ERROR:
                write_to_file = FALSE;
                /* fall through */
            case STRING_EXPANDED:
                return;
            }
        }
    }

    if( Token_Count > 0 ) {
        if( AsmParse() == ERROR ) {
            write_to_file = FALSE;
        }
    } else if( Token_Count < 0 ) {
        // syntax error
        write_to_file = FALSE;
    }
  #ifdef DEBUG_OUT
    dbg_output();               // for debuggin only
  #endif
#else
    // Token_Count is the number of tokens scanned
    Token_Count = AsmScan( string );
    if( Token_Count > 0 ) {
        AsmParse();
    }
#endif
}

⌨️ 快捷键说明

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