stubutil.c

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

C
879
字号
}
extern  char    *Op( cg_op op ) {
//===============================

    char        *res;
    if( op >= STUB_MAX_OP ) {
        CGError( "Illegal operator" );
    } else {
        res = Ops[ op ];
    }
    return( res );
}
extern  n       *NewNode( nclass c, cg_type t ) {
//===============================================

    n   *nd;
    nd = CGAlloc( sizeof( n ) );
    nd->c = c;
    nd->o = O_NOP;
    nd->l = NULL;
    nd->r = NULL;
    nd->n = NodeList;
    nd->i = 0;
    nd->t = t;
    nd->id = ++NodeId;
    nd->src = NULL;
    nd->burnt = FALSE;
    NodeList = nd;
    return( nd );
}

#define FE_TYPE( x )    ( ( (x) > T_FIRST_FREE ) && ( (x) < T_LAST_FREE ) )
extern  n       *Binary( cg_op op, n *l, n *r, cg_type t ) {
//==========================================================

    n   *nd;

    Use( l );
    Use( r );
    if( op == O_CONVERT ) {
        if( FE_TYPE( l->t ) || FE_TYPE( r->t ) ) {
            Action( "***ILLEGAL CONVERSION***" );
        }
    }
    nd = NewNode( OP, t );
    nd->l = l;
    nd->r = r;
    nd->o = op;
    return( nd );
}
extern  n       *Unary( cg_op op, n *r, cg_type t ) {
//===================================================

    n   *nd;

    Use( r );
    nd = NewNode( OP, t );
    nd->r = r;
    nd->o = op;
    return( nd );
}
extern  void    Use( n *nd ) {
//============================

    if( nd != NULL ) {
        VerNode( nd );
        nd->i++;
        if( nd->i != 1 ) {
            Code( "Used twice ====================================%n" );
            DumpSubTree( nd );
            CGError( "A cg-name has been reused%n" );
        }
    }
}
extern  void    VerNode( n *nd ) {
//================================

    Find( "node", (pointer *)NodeList, nd );
}
extern  void    VerAuto( a *au ) {
//================================

    Find( "auto variable", (pointer *)AutoList, au );
}
extern  void    VerLabel( l *lb ) {
//=================================

    Find( "label", (pointer *)LblList, lb );
    if( lb->i == -1 ) {
        CGError( "Unknown label%n" );
    }
}
extern  void    VerBack( b *bk ) {
//================================

    Find( "back handle",(pointer *)BackList, bk );
}
extern  bool    LkUpOp( cg_op o, cg_op *l ) {
//==========================================

    while( *l != O_NOP ) {
        if( o == *l++ ) return( TRUE );
    }
    return( FALSE );
}
extern  void    VerOp( cg_op o, cg_op *l ) {
//==========================================

    Op(o);
    if( l != NULL ) {
        if( !LkUpOp( o, l ) ) {
            CGError( "Illegal operator for given CG routine %s", Op(o) );
        }
    }
}
extern  void    VerTipe( cg_type t, cg_type *l ) {
//================================================

    type_def    *a;

    a = TypeAddress( t );
    if( a == NULL ) {
        CGError( "Undefined type %d", t );
    }
    t = a->refno;
    if( l != NULL ) {
        while( *l != T_DEFAULT ) {
            if( t == *l++ ) return;
        }
        CGError( "Illegal type for given routine %s", Tipe(t) );
    }
}
extern  void    Find( char *msg, pointer *list, pointer nd ) {
//============================================================

    while( list != NULL ) {
        if( list == nd ) return;
        list = *list;
    }
    CGError( "Unknown %s %p", msg, nd );
}
extern  char    *ACopyOf( char *s ) {
//===================================

    char        *c;
    c = CGAlloc( Length( s ) + 1 );
    CopyStr( s, c );
    return( c );
}
extern  void    DumpTree( n *t ) {
//================================

    NodeId = 0;
    DumpT( t );
}
extern  void    NoDanglers() {
//============================

    if( NodeList != NULL ) {
        Code( "Dangling trees ========================%n" );
        while( NodeList != NULL ) {
            Code( "%t %p\n", NodeList, NodeList );
            NodeFree( NodeList );
        }
        CGError( "Hanging on to cgnames too long!" );
    }
}
extern  void    DumpT( n *t ) {
//=============================

    Use( t );
    DumpSubTree( t );
    Code( "%n" );
}
extern n *FindParm( n *p, n *r ) {
//================================

    for( ;; ) {
        if( p->r == r ) return( p );
        p = p->r;
    }
//  return( NULL );
}
extern void DumpCallTree( n *t ) {
//================================

    n   *parm;
    n   *junk;

    DumpSubTree( t->l );
    Code( "(" );
    if( t->r == NULL ) return;
    parm = t->r;
    for( ;; ) {
        DumpSubTree( parm->l );
        junk = parm;
        parm = parm->r;
        NodeFree( junk );
        if( parm == NULL ) break;
        Code( "," );
    }
    Code( ")" );
}
extern  bool    CheckInLine( n * t ) {
//====================================

    n   *nlist;
    ic  *icall;
    ip  *iparm;
    ip  **padd;
    n   *parm;
    b   *bk;

    if( !( *(call_class*)FEAuxInfo( t->h, CALL_CLASS ) & MAKE_CALL_INLINE ) ) {
        return( FALSE );
    }
    icall = CGAlloc( sizeof( ic ) );
    icall->t = t->t;
    icall->p = NULL;
    icall->n = Inlines;
    icall->d = 0;
    icall->c = 0;
    icall->h = t->h;
    bk = FEBack( t->h );
    VerBack( bk );
    DDefILabel( bk->lp );
    Inlines = icall;
    padd = &icall->p;
    parm = t->r;
    while( parm != NULL ) {
        iparm = CGAlloc( sizeof( ip ) );
        iparm->t = parm->t;
        iparm->n = NULL;
        *padd = iparm;
        padd = &iparm->n;
        icall->c++;
        parm = parm->r;
    }
    DumpCallTree( t );
    nlist = NodeList;
    NodeList = NULL;
    Code( "%n======== Inline code for %s starts%n", FEName( t->h ) );
    Action( "%nFEGenProc( sym=%s, handle=%p )%n", FEName( t->h ), t );
    Action( "======== Inline code for %s starts%n", FEName( t->h ) );
    FEGenProc( t->h, t );
    Code( "%n======== Inline code for %s ends%n", FEName( t->h ) );
    Action( "%n======== Inline code for %s ends%n", FEName( t->h ) );
    NodeList = nlist;
    return TRUE;
}
extern  void    DumpSubTree( n *t ) {
//===================================

    if( t == NULL ) {
        ;
    } else {
        VerNode( t );
        if( t->c == LEAF ) {
            Code( "%s", t->l );
            if( NULL != t->src ) {
                Code( "[%p]", t->src );
            }
        } else if( t->c == CALLBACK ) {
            call_back* cb = (call_back*)t->l;
            Code( "%s", "<CallBack>" );
            t->l = NULL;
            Action( "CALLBACK( %p, %p )%n"
                  , cb->function
                  , cb->data );
            (cb->function)( cb->data );
            Action( "== completed CALLBACK( %p, %p )%n"
                  , cb->function
                  , cb->data );
            CGFree( cb );
        } else if( t->o == OP_BIT_FIELD ) {
            Code( "{%d:%d}", t->st, t->ln );
            DumpSubTree( t->r );
        } else if( t->o == OP_CHOOSE ) {
            DumpSubTree( t->l );
            Code( "?" );
            DumpSubTree( t->r->l );
            Code( ":" );
            DumpSubTree( t->r->r );
            NodeFree( t->r );
        } else if( t->o == OP_WARP ) {
            Code( "__Warp[ (" );
            if( t->l->r != NULL ) {
                DumpSubTree( t->l->r );
            } else {
                Code( "\\" );
            }
            Code( ") ,%s, (", Label((struct l*)t->l->l) );
            DumpSubTree( t->r );
            Code( ") ]" );
            NodeFree( t->l );
        } else if( t->o == O_GETS || t->o == O_LV_GETS ) {
            if( t->o == O_GETS ) Code( "@" );
            Code( "(" );
            DumpSubTree( t->l );
            Code( "=" );
            DumpSubTree( t->r );
            Code( ")" );
        } else if( t->o == O_POST_GETS ) {
            Code( "(" );
            DumpSubTree( t->r->l );
            Code( "," );
            DumpSubTree( t->r->r );
            Code( ")" );
            Code( "%s%s", Op( t->r->o ), Op( t->r->o ) );
            NodeFree( t->r );
        } else if( t->o == O_PRE_GETS || t->o == O_LV_PRE_GETS ) {
            if( t->o == O_PRE_GETS ) Code( "@" );
            Code( "(" );
            DumpSubTree( t->r->l );
            Code( "%s", Op( t->r->o ) );
            Code( "=" );
            DumpSubTree( t->r->r );
            Code( ")" );
            NodeFree( t->r );
        } else if( t->o == O_CALL ) {
            if( !CheckInLine( t ) ) DumpCallTree( t );
        } else {
            if( LkUpOp( t->o, FunkyOps ) ) {
                Code( "%s(", Op( t->o ) );
                if( t->l != NULL ) {
                    DumpSubTree( t->l );
                    Code( "," );
                }
                DumpSubTree( t->r );
                Code( ")" );
            } else {
                if( t->l != NULL && t->r != NULL ) {
                    Code( "(" );
                }
                DumpSubTree( t->l );
                Code( "%s", Op( t->o ) );
                DumpSubTree( t->r );
                if( t->l != NULL && t->r != NULL ) {
                    Code( ")" );
                }
            }
        }
        if( t->i == 1 ) {
            NodeFree( t );
        }
    }
}
extern  void    NodeFree( n *nd ) {
//=================================

    n   **o;
    o = &NodeList;
    while( *o != nd ) {
        o = (n**)*o;
    }
    *o = nd->n;
    CGFree( nd );
}
extern  segment_id      SetFile( segment_id seg ) {
//=================================================

    segment_id  old;
    int         i;

    old = CurSeg;
    CurSeg = seg;
    if( seg > MAX_SEG || seg < MIN_SEG || SegOk[ seg ] == FALSE ) {
        CGError( "BESetSeg - bad segment (%d)", seg );
    } else {
        if( Files[ seg ].hdl == 0 ) {
            if( FilesOpen > 10 ) {
                for( i = 0; Files[ i ].hdl == 0; ++i );
                FShut( Files[ i ].hdl );
                --FilesOpen;
            }
            if( Files[ seg ].exists ) {
                Files[ seg ].hdl = open( Files[ seg ].name, O_RDWR );
                lseek( Files[ seg ].hdl, 0, SEEK_END );
                ++FilesOpen;
            } else {
                Files[ seg ].hdl = FCreate( Files[ seg ].name );
                if( Files[ seg ].hdl != -1 ) {
                    Files[ seg ].exists = TRUE;
                    ++FilesOpen;
                } else {
                    Files[ seg ].hdl = 0;
                }
            }
        }
        Out = Files[ seg ].hdl;
    }
    return( old );
}
extern  void    NotDefault( cg_type  t ) {
//========================================

    if( t == T_DEFAULT ) {
        CGError( "T_DEFAULT not allowed as type to routine" );
    }
}

extern  char   *CFCnvFS( cfloat *f, char *buffer, int maxlen ) {
//====================================

    int         len;

    len = f->len - 1;
    if( len + 10 > maxlen ) {
        len = maxlen - 10;
    }
    if( f->sign == -1 ) {
        *buffer++ = '-';
    }
    *buffer++ = f->mant[0];
    *buffer++ = '.';
    Copy( &f->mant[1], buffer, len );
    buffer += len;
    *buffer++ = 'E';
    len = f->exp - 1;
    if( len < 0 ) {
        *buffer++ = '-';
        len = -len;
    }
    buffer[ 2 ] = len % 10 + '0';
    len /= 10;
    buffer[ 1 ] = len % 10 + '0';
    len /= 10;
    buffer[ 0 ] = len + '0';
    buffer += 3;
    return( buffer );
}

pointer SafeRecurse( pointer (* rtn)(), pointer arg ) {
/*****************************************************/

    return( rtn( arg ) );
}

⌨️ 快捷键说明

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