⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 symbols.c

📁 cg编译器
💻 C
📖 第 1 页 / 共 3 页
字号:

/*
 * NewType() - Allocate a new type struct.
 *
 */

Type *NewType(int properties, int size)
{
    Type *lType;

    lType = (Type *) malloc(sizeof(Type));
    InitType(lType);
    lType->properties = properties;
    lType->co.size = size;
    return lType;
} // NewType

/*
 * DupType() - Duplicate a type struct.
 *
 */

Type *DupType(Type *fType)
{
    Type *lType;

    lType = (Type *) malloc(sizeof(Type));
    *lType = *fType;
    return lType;
} // DupType

/*
 * NewPackedArrayType() - Define a new packed (vector) array type.
 *
 */

Type *NewPackedArrayType(Type *elType, int numels, int properties)
{
    Type *lType;

    lType = NewType(TYPE_CATEGORY_ARRAY | TYPE_MISC_PACKED | properties | GetBase(elType), 0);
    lType->arr.eltype = elType;
    lType->arr.numels = numels;
    lType->arr.size = Cg->theHAL->GetSizeof(lType);
    return lType;
} // NewPakedArrayType

/*************************************** Category Functions **********************************/

/*
 * IsCategory() - See if a type is of the given category.
 *
 */

int IsCategory(const Type *fType, int category)
{
    if (fType && (fType->properties & TYPE_CATEGORY_MASK) == category) {
        return 1;
    } else {
        return 0;
    }
} // IsCategory

/*
 * IsTypeBase() - See if a type is of the given base.
 *
 */

int IsTypeBase(const Type *fType, int base)
{
    if (fType && (fType->properties & TYPE_BASE_MASK) == base) {
        return 1;
    } else {
        return 0;
    }
} // IsTypeBase

/*
 * IsVoid() - Returns TRUE if a void type.
 *
 */

int IsVoid(const Type *fType)
{
    if (fType && (fType->properties & TYPE_MISC_VOID)) {
        return 1;
    } else {
        return 0;
    }
} // IsVoid

/*
 * IsBoolean() - Returns TRUE if a Boolean type.
 *
 */

int IsBoolean(const Type *fType)
{
    if (fType && (fType->properties & TYPE_BASE_MASK) == TYPE_BASE_BOOLEAN) {
        return 1;
    } else {
        return 0;
    }
} // IsBoolean

/*
 * IsScalar() - Returns TRUE if a scalar type.
 *
 */

int IsScalar(const Type *fType)
{
    if (fType && (fType->properties & TYPE_CATEGORY_MASK) == TYPE_CATEGORY_SCALAR) {
        return 1;
    } else {
        return 0;
    }
} // IsScalar

/*
 * IsArray() - Returns TRUE if a packed or unpacked array type.
 *
 */

int IsArray(const Type *fType)
{
    if (fType && (fType->properties & TYPE_CATEGORY_MASK) == TYPE_CATEGORY_ARRAY) {
        return 1;
    } else {
        return 0;
    }
} // IsScalar

/*
 * IsVector() - Returns TRUE if a vector type.
 *
 */

int IsVector(const Type *fType, int *len)
{
    if (fType &&
        (fType->properties & TYPE_CATEGORY_MASK) == TYPE_CATEGORY_ARRAY &&
        (fType->properties & TYPE_MISC_PACKED) &&
        !IsArray(fType->arr.eltype))
    {
        if (len)
            *len = fType->arr.numels;
        return 1;
    } else {
        return 0;
    }
} // IsVector

/*
 * IsMatrix() - Returns TRUE if a matrix type.
 *
 */

int IsMatrix(const Type *fType, int *len, int *len2)
{
    if (fType &&
        (fType->properties & TYPE_CATEGORY_MASK) == TYPE_CATEGORY_ARRAY &&
        (fType->properties & TYPE_MISC_PACKED) &&
        IsVector(fType->arr.eltype, len))
    {
        if (len2)
            *len2 = fType->arr.numels;
        return 1;
    } else {
        return 0;
    }
} // IsMatrix

/*
 * IsUnsizedArray() - Returns TRUE if an array with an unspecified number of elements.
 *
 */

int IsUnsizedArray(const Type *fType)
{
    if (GetCategory(fType) == TYPE_CATEGORY_ARRAY &&
        fType->arr.numels == 0)
    {
        return 1;
    } else {
        return 0;
    }
} // IsUnsizedArray

/*
 * IsStruct() - Returns TRUE if a struct.
 *
 */

int IsStruct(const Type *fType)
{
    if (fType &&
        GetCategory(fType) == TYPE_CATEGORY_STRUCT)
    {
        return 1;
    } else {
        return 0;
    }
} // IsStruct

/*
 * IsProgram() - See if a type is a program.
 *
 */

int IsProgram(const Type *fType)
{
    if (fType && (fType->properties & TYPE_MISC_PROGRAM)) {
        return 1;
    } else {
        return 0;
    }
} // IsProgram

/*
 * IsPacked()
 *
 */

int IsPacked(const Type *fType)
{
    if (fType && (fType->properties & TYPE_MISC_PACKED)) {
        return 1;
    } else {
        return 0;
    }
} // IsPacked

/*
 * IsSameUnqualifiedType() - Returns TRUE if the unqualified types aType and bType are the same.
 *
 */

int IsSameUnqualifiedType(const Type *aType, const Type *bType)
{
    const int UnQMask = TYPE_BASE_MASK | TYPE_CATEGORY_MASK ; // 020122 // | TYPE_DOMAIN_MASK;

    if (aType == bType) {
        return 1;
    } else {
        if ((aType->properties & UnQMask) == (bType->properties & UnQMask)) {
            switch (aType->properties & TYPE_CATEGORY_MASK) {
            case TYPE_CATEGORY_SCALAR:
                return 1;
            case TYPE_CATEGORY_ARRAY:
                if (aType->arr.numels == bType->arr.numels) {
                    // Should we check for Packed here??? I think so!
                    return IsSameUnqualifiedType(aType->arr.eltype, bType->arr.eltype);
                }
                break;
            case TYPE_CATEGORY_FUNCTION:
                break;
            case TYPE_CATEGORY_STRUCT:
                if (aType->str.unqualifiedtype == bType->str.unqualifiedtype)
                    return 1;
            default:
                break;
            }
        }
    }
    return 0;
} // IsSameUnqualifiedType

/*
 * IsTypedef() - See if a symbol is a typedef.
 *
 */

int IsTypedef(const Symbol *fSymb)
{
    if (fSymb && fSymb->kind == TYPEDEF_S) {
        return 1;
    } else {
        return 0;
    }
} // IsTypedef

/*
 * IsFunction() - See if a symbol is a function.
 *
 */

int IsFunction(const Symbol *fSymb)
{
    if (fSymb && fSymb->kind == FUNCTION_S) {
        return 1;
    } else {
        return 0;
    }
} // IsFunction

/*
 * IsInline() - See if a symbol is an inline function.
 *
 */

int IsInline(const Symbol *fSymb)
{
    if (fSymb && fSymb->kind == FUNCTION_S && (fSymb->properties & SYMB_IS_INLINE_FUNCTION)) {
        return 1;
    } else {
        return 0;
    }
} // IsInline

/*
 * GetBase() - Return the base attributes of a type.
 *
 */

int GetBase(const Type *fType)
{
    if (fType) {
        return fType->properties & TYPE_BASE_MASK;
    } else {
        return TYPE_BASE_NO_TYPE;
    }
} // GetBase

/*
 * GetCategory() - Return the categpry of a type.
 *
 */

int GetCategory(const Type *fType)
{
    if (fType) {
        return fType->properties & TYPE_CATEGORY_MASK;
    } else {
        return TYPE_CATEGORY_NONE;
    }
} // GetCategory

/*
 * GetDomain() - Return the domain of a type.
 *
 */

int GetDomain(const Type *fType)
{
    if (fType) {
        return fType->properties & TYPE_DOMAIN_MASK;
    } else {
        return TYPE_DOMAIN_UNKNOWN;
    }
} // GetDomain

/*
 * GetQualifiers() - Return a type's qualifiers.
 *
 */

int GetQualifiers(const Type *fType)
{
    if (fType) {
        return fType->properties & TYPE_QUALIFIER_MASK;
    } else {
        return TYPE_QUALIFIER_NONE;
    }
} // GetQualifiers

/*
 * GetQuadRegSize() - Return the number of quad registers required to hold an object
 *         of this type.  Minimum size is 1.
 */

int GetQuadRegSize(const Type *fType)
{
    if (fType) {
        return (fType->co.size + 3) >> 2;
    } else {
        return 1;
    }
} // GetQuadRegSize

/*
 * ClearTypeMisc() - Clear bits in the properties field a type.
 *
 */

void ClearTypeMisc(Type *fType, int misc)
{
    if (fType)
        fType->properties &= ~misc;
} // ClearTypeMisc

/*********************************************************************************************/
/************************************ Symbol Semantic Functions ******************************/
/*********************************************************************************************/

/*
 * LookUpLocalSymbol()
 *
 */

Symbol *LookUpLocalSymbol(Scope *fScope, int atom)
{
    Symbol *lSymb;
    int rname, ratom;

    ratom = GetReversedAtom(atable, atom);
    if (!fScope)
        fScope = CurrentScope;
    lSymb = fScope->symbols;
    while (lSymb) {
        rname = GetReversedAtom(atable, lSymb->name);
        if (rname == ratom) {
            return lSymb;
        } else {
            if (rname > ratom) {
                lSymb = lSymb->left;
            } else {
                lSymb = lSymb->right;
            }
        }
    }
    return NULL;
} // LookUpLocalSymbol

/*
 * LookUpLocalSymbolBySemanticName() - Lookup a symbol in a local tree by the : semantic name.
 *
 * Note:  The tree is not ordered for this lookup so the next field is used.  This only works
 * for structs and other scopes that maintain this list.
 *
 * Note: There can be multiple matches.  Returns the first.
 *
 */

Symbol *LookUpLocalSymbolBySemanticName(Scope *fScope, int atom)
{
    Symbol *lSymb;

    if (!fScope)
        return NULL;
    lSymb = fScope->symbols;
    while (lSymb) {
        if (lSymb->kind == VARIABLE_S) {
            if (lSymb->details.var.semantics == atom)
                return lSymb;
        }
        lSymb = lSymb->next;
    }
    return NULL;
} // LookUpLocalSymbolBySemanticName

/*
 * LookUpLocalSymbolByBindingName() - Lookup a symbol in a local tree by the lname in the
 *         semantic binding structure.
 *
 * Note:  The tree is not ordered for this lookup so the next field is used.  This only works
 * for structs and other scopes that maintain this list.
 *
 * Note: There can be multiple matches.  Returns the first.
 *
 */

Symbol *LookUpLocalSymbolByBindingName(Scope *fScope, int atom)
{
    Symbol *lSymb;

    if (!fScope)
        return NULL;
    lSymb = fScope->symbols;
    while (lSymb) {
        if (lSymb->kind == VARIABLE_S && lSymb->details.var.bind) {
            if (lSymb->details.var.bind->none.lname == atom)
                return lSymb;
        }
        lSymb = lSymb->next;
    }
    return NULL;
} // LookUpLocalSymbolByBindingName

/*
 * LookUpLocalTag()
 *
 */

Symbol *LookUpLocalTag(Scope *fScope, int atom)
{
    Symbol *lSymb;
    int rname, ratom;

    ratom = GetReversedAtom(atable, atom);

⌨️ 快捷键说明

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