makeblk.c

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

C
605
字号


extern  type_class_def  InitCallState( type_def *tipe )
/*****************************************************/
{
    name        *name;
    sym_handle  sym;
    pointer     aux;

    sym = AskForLblSym( CurrProc->label );
    name = AllocMemory( sym, 0, CG_FE, TypeClass( tipe ) );
    name->v.usage |= USE_MEMORY;        /* so not put in conflict graph*/
    aux = FEAuxInfo( sym, AUX_LOOKUP );
    return( CallState( aux, tipe, &CurrProc->state ) );
}


extern  void    AddTarget( label_handle dest, bool dest_label_dies )
/******************************************************************/
/*   Don't handle expression jumps yet*/
{
    block_edge  *edge;

    edge = &CurrBlock->edge[  CurrBlock->targets++  ];
    edge->source = CurrBlock;
    edge->destination = dest;
    edge->next_source = NULL;
    if( dest_label_dies ) {
        edge->flags |= DEST_LABEL_DIES;
    }
}


extern  block   *FindBlockWithLbl( label_handle label )
/*****************************************************/
{
    block       *blk;

    blk = HeadBlock;
    for( ;; ) {
        if( blk == NULL ) return( NULL );
        if( blk->label == label ) return( blk );
        blk = blk->next_block;
    }
}


extern  void    FixEdges( void )
/******************************/
{
    block       *blk;
    block       *dest;
    int         targets;
    block_edge  *edge;

    blk = HeadBlock;
    while( blk != NULL ) {
        targets = blk->targets;
        if( ( blk->class & BIG_JUMP ) == 0 ) {
            while( --targets >= 0 ) {
              edge = &blk->edge[  targets  ];
              dest = FindBlockWithLbl( edge->destination );
              if( dest != NULL ) {
                  edge->flags |= DEST_IS_BLOCK;
                  edge->destination = dest;
                  edge->next_source = edge->destination->input_edges;
                  edge->destination->input_edges = edge;
                  edge->destination->inputs++;
              }
            }
        }
        blk = blk->next_block;
    }
}


static label_handle LinkReturnsParms[ 2 ];

static  pointer  LinkReturns( void )
/**********************************/
{
    block               *blk;
    int                 i;
    bool                found;
    label_handle        link_to;
    label_handle        to_search;

    link_to = LinkReturnsParms[ 0 ];
    to_search = LinkReturnsParms[ 1 ];
    blk = FindBlockWithLbl( to_search );
    found = FALSE;
    if( blk == NULL ) return( (pointer)FALSE );
    if( blk->class & BLOCK_VISITED ) return( (pointer)TRUE );
    if( blk->class & LABEL_RETURN ) {
        i = blk->targets;
        for( ;; ) {
            if( --i < 0 ) {
                blk = ReGenBlock( blk, link_to );
                break;
            }
            if( blk->edge[ i ].destination == link_to ) {
                break; /* kick out ... already linked */
            }
        }
        found = TRUE;
    } else {
        blk->class |= BLOCK_VISITED;
        if( blk->class & CALL_LABEL ) {
            if( blk->next_block == NULL ) return( (pointer)FALSE );
            LinkReturnsParms[ 0 ] = link_to;
            LinkReturnsParms[ 1 ] = blk->next_block->label;
            if( SafeRecurse( LinkReturns, NULL ) == (pointer)FALSE ) {
                return( (pointer)FALSE );
            }
        } else {
            i = blk->targets;
            while( --i >= 0 ) {
                LinkReturnsParms[ 0 ] = link_to;
                LinkReturnsParms[ 1 ] = blk->edge[ i ].destination;
                if( SafeRecurse( LinkReturns, NULL ) == (pointer)FALSE ) {
                    return( (pointer)FALSE );
                }
            }
        }
    }
    return( (pointer)TRUE );
}

extern  bool        FixReturns( void )
/************************************/
/* link all LABEL_RETURN blocks to any CALL_LABEL block they could*/
/* have been invoked from*/
{
    block       *blk;
    block       *other_blk;

    blk = HeadBlock;
    while( blk != NULL ) {
        if( blk->class & CALL_LABEL ) {
            blk->class |= BLOCK_VISITED;
            if( blk->next_block == NULL ) return( FALSE );
            blk->next_block->class |= RETURNED_TO;
            LinkReturnsParms[ 0 ] = blk->next_block->label;
            LinkReturnsParms[ 1 ] = blk->edge[ 0 ].destination;
            if( !LinkReturns() ) {
                return( FALSE );
            }
            other_blk = HeadBlock;
            while( other_blk != NULL ) {
                other_blk->class &= ~BLOCK_VISITED;
                other_blk = other_blk->next_block;
            }
        }
        blk = blk->next_block;
    }
    return( TRUE );
}


extern  void    UnFixEdges( void )
/********************************/
{
    block       *blk;
    int         targets;
    block_edge  *edge;

    blk = HeadBlock;
    while( blk != NULL ) {
        if( ( blk->class & BIG_JUMP ) == EMPTY ) {
            targets = blk->targets;
            while( --targets >= 0 ) {
                edge = &blk->edge[  targets  ];
                if( edge->flags & DEST_IS_BLOCK ) {
                    RemoveInputEdge( edge );
                    edge->destination = edge->destination->label;
                    edge->flags &= ~DEST_IS_BLOCK;
                }
            }
        }
        blk = blk->next_block;
    }
}


extern  void    AddAnIns( block *blk, instruction *ins )
/******************************************************/
{
    block       *curr_block;

    curr_block = CurrBlock;
    CurrBlock = blk;
    AddIns( ins );
    CurrBlock = curr_block;
}


extern  bool    BlkTooBig( void )
/*******************************/
{
    label_handle        blk;

    if( !HaveCurrBlock ) return( FALSE );
    if( CurrBlock == NULL ) return( FALSE );
    if( CurrBlock->ins.hd.next == (instruction *)&CurrBlock->ins ) return( FALSE );
    _INS_NOT_BLOCK( CurrBlock->ins.hd.next );
    if( (InsId - CurrBlock->ins.hd.next->id) < INS_PER_BLOCK ) return( FALSE );
    if( CurrBlock->targets != 0 ) return( FALSE );
    blk = AskForNewLabel();
    GenBlock( JUMP, 1 );
    AddTarget( blk, FALSE );
    EnLink( blk, TRUE );
    return( TRUE );
}


extern  void    NewProc( int level )
/**********************************/
{
    proc_def    *new;

    if( CurrProc != NULL ) {
        SaveToTargProc();
        CurrProc->head_block = HeadBlock;
        CurrProc->tail_block = BlockList;
        CurrProc->curr_block = CurrBlock;
        CurrProc->lasttemp = LastTemp;
        CurrProc->dummy_index = DummyIndex;
        CurrProc->names[ N_CONSTANT ] = Names[ N_CONSTANT ];
        CurrProc->names[ N_MEMORY   ] = Names[ N_MEMORY   ];
        CurrProc->names[ N_TEMP     ] = Names[ N_TEMP     ];
        CurrProc->names[ N_REGISTER ] = Names[ N_REGISTER ];
        CurrProc->names[ N_INDEXED  ] = Names[ N_INDEXED  ];
        CurrProc->block_by_block = BlockByBlock;
        CurrProc->ins_id = InsId;
        CurrProc->untrimmed = BlocksUnTrimmed;
    }
    HaveCurrBlock = TRUE;
    BlocksUnTrimmed = TRUE;
    MaxStack = 0;
    InsId = 0;
    HeadBlock = NULL;
    BlockList = NULL;
    ReInitNames();
    _Alloc( new, sizeof( proc_def ) );
    memset( new, 0, sizeof( proc_def ) );
    new->next_proc = CurrProc;
    CurrProc = new;
    new->frame_index = NULL;
    new->lex_level = level;
    new->parms.size = 0;
    new->parms.base = 0;
    new->locals.size = 0;
    new->locals.base = AskDisplaySize( level );
    new->prolog_state = 0;
    new->parms_list = NULL;
    InitTargProc();
}


extern  void    FreeProc( void )
/******************************/
{
    proc_def    *oldproc;

    for( ;; ) {
        CurrBlock = HeadBlock;
        if( CurrBlock == NULL ) break;
        HeadBlock = CurrBlock->next_block;
        FreeBlock();
    }
    BlockList = NULL;
    FreeNames();
    TellBeginExecutions();
    oldproc = CurrProc;
    if( oldproc != NULL ) {
        CurrProc = CurrProc->next_proc;
        if( oldproc->state.parm.table ) {
            CGFree( oldproc->state.parm.table );
        }
        _Free( oldproc, sizeof( proc_def ) );
        if( CurrProc != NULL ) {
            RestoreFromTargProc();
            InsId = CurrProc->ins_id;
            HeadBlock = CurrProc->head_block;
            BlockList = CurrProc->tail_block;
            CurrBlock = CurrProc->curr_block;
            Names[ N_CONSTANT ] = CurrProc->names[ N_CONSTANT ];
            Names[ N_MEMORY   ] = CurrProc->names[ N_MEMORY   ];
            Names[ N_TEMP     ] = CurrProc->names[ N_TEMP     ];
            Names[ N_REGISTER ] = CurrProc->names[ N_REGISTER ];
            Names[ N_INDEXED  ] = CurrProc->names[ N_INDEXED  ];
            LastTemp = CurrProc->lasttemp;
            DummyIndex = CurrProc->dummy_index;
            BlockByBlock = CurrProc->block_by_block;
            BlocksUnTrimmed = CurrProc->untrimmed;
        } else {
            InsId = 0;
            BlockByBlock = FALSE;
        }
    }
}

⌨️ 快捷键说明

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