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

📄 symbols.c

📁 cg编译器
💻 C
📖 第 1 页 / 共 3 页
字号:
    if (!fScope)
        fScope = CurrentScope;
    lSymb = fScope->tags;
    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;
} // LookUpLocalTag

/*
 * LookUpSymbol()
 *
 */

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

    if (!fScope)
        fScope = CurrentScope;
    while (fScope) {
        lSymb = LookUpLocalSymbol(fScope, atom);
        if (lSymb)
            return lSymb;
        fScope = fScope->parent;
    }
    return NULL;
} // LookUpSymbol

/*
 * LookUpTag()
 *
 */

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

    if (!fScope)
        fScope = CurrentScope;
    while (fScope) {
        lSymb = LookUpLocalTag(fScope, atom);
        if (lSymb)
            return lSymb;
        fScope = fScope->parent;
    }
    return NULL;
} // LookUpTag

/*
 * LookUpTypeSymbol()
 *
 */

Type *LookUpTypeSymbol(Scope *fScope, int atom)
{
    Symbol *lSymb;
    Type *lType;

    lSymb = LookUpSymbol(fScope, atom);
    if (lSymb) {
        if (!IsTypedef(lSymb)) {
            InternalError(Cg->tokenLoc, ERROR_S_NAME_NOT_A_TYPE,
                          GetAtomString(atable, atom));
            return UndefinedType;
        }
        lType = lSymb->type;
        if (lType) {
            return lType;
        } else {
            return UndefinedType;
        }
    } else {
        InternalError(Cg->tokenLoc, ERROR_S_TYPE_NAME_NOT_FOUND,
                      GetAtomString(atable, atom));
        return UndefinedType;
    }
} // LookUpTypeSymbol

/*
 * GetStandardType()
 *
 * Scalar: len = 0.
 * Vector: len >= 1 and len2 = 0
 * Matrix: len >= 1 and len2 >= 1
 *
 * len = 1 means "float f[1]" not "float f"
 * Vector and matrix types are PACKED.
 *
 */

Type *GetStandardType(int tbase, int tlen, int tlen2)
{
    Type *lType, *nType;

    if (tbase >= 0 && tbase <= TYPE_BASE_LAST_USER) {
        lType = baseTypes[tbase];
        if (tlen > 0) {
            // Put these in a table, too!!! XYZZY !!!
            nType = NewType(TYPE_CATEGORY_ARRAY | TYPE_MISC_PACKED | tbase, 0);
            nType->arr.eltype = lType;
            nType->arr.numels = tlen;
            nType->arr.size = Cg->theHAL->GetSizeof(nType);
            lType = nType;
            if (tlen2 > 0) {
                // Put these in a table, too!!! XYZZY !!!
                nType = NewType(TYPE_CATEGORY_ARRAY | TYPE_MISC_PACKED | tbase, 0);
                nType->arr.eltype = lType;
                nType->arr.numels = tlen2;
                nType->arr.size = Cg->theHAL->GetSizeof(nType);
                lType = nType;
            }
        }
    } else {
        lType = UndefinedType;
    }
    return lType;
} // GetStandardType

/*
 * GetElementType() - Return a pointer to the type of elements stored in this array.
 *
 */

Type *GetElementType(const Type *fType)
{
    Type *lType;

    if (GetCategory(fType) == TYPE_CATEGORY_ARRAY) {
        lType = fType->arr.eltype;
#if 0000
        if ((fType->properties & TYPE_QUALIFIER_CONST) &&
            !(lType->properties & TYPE_QUALIFIER_CONST))
        {
            lType = DupType(lType);
            lType->properties |= TYPE_QUALIFIER_CONST;
        }
#endif
    } else {
        InternalError(Cg->tokenLoc, ERROR___TYPE_NOT_ARRAY);
        lType = UndefinedType;
    }
    return lType;
} // GetElementType

/*
 * SetMemberOffsets() - Assign offsets to members for use by code generators.
 *
 */

void SetStructMemberOffsets(Type *fType)
{
    int addr, size, alignment;
    Symbol *lSymb;

    addr = 0;
    lSymb = fType->str.members->symbols;
    while (lSymb) {
        alignment = Cg->theHAL->GetAlignment(lSymb->type);
        size = Cg->theHAL->GetSizeof(lSymb->type);
        addr = ((addr + alignment - 1)/alignment)*alignment;
        lSymb->details.var.addr = addr;
        addr += size;
        lSymb = lSymb->next;
    }
    fType->co.size = ((addr + 3)/4)*4;
} // SetStructMemberOffsets

/*
 * SetStructMembers() - Set the member tree of a structure.
 *
 */

Type *SetStructMembers(SourceLoc *loc, Type *fType, Scope *members)
{
    Symbol *lSymb;
    const char *tagname;

    if (fType) {
        if (fType->str.members) {
            SemanticError(loc, ERROR_SSD_STRUCT_ALREADY_DEFINED,
                            GetAtomString(atable, fType->str.tag),
                            GetAtomString(atable, fType->str.loc.file),
                            fType->str.loc.line);
        } else {
            if (fType->str.tag) {
                tagname = GetAtomString(atable, fType->str.tag);
            } else {
                tagname = "<no-name>";
            }
            fType->str.members = members;
            fType->str.loc = *loc;
            fType->str.HasSemantics = members->HasSemantics;
            SetStructMemberOffsets(fType);
            if (fType->str.tag) {
                lSymb = LookUpLocalSymbol(CurrentScope, fType->str.tag);
                if (!lSymb) {
                    lSymb = DefineTypedef(loc, CurrentScope, fType->str.tag, fType);
                } else {
                    if (IsCategory(fType, TYPE_CATEGORY_STRUCT)) {
                        if (!IsCategory(lSymb->type, TYPE_CATEGORY_STRUCT)) {
                            SemanticError(loc, ERROR_S_NAME_ALREADY_DEFINED, tagname);
                        }
                    }
                }
            }
        }
    }
    return fType;
} // SetStructMembers

/*
 * AddParameter() - Add a parameter to a function's formal parameter list, or a member to a
 *         struct or connector's member list.
 */

void AddParameter(Scope *fScope, Symbol *param)
{
    Symbol *lSymb = fScope->params;

    if (lSymb) {
        while (lSymb->next)
            lSymb = lSymb->next;
        lSymb->next = param;
    } else {
        fScope->params = param;
    }
} // AddParameter

///////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////// Various Support Functions: /////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////

/*
 * GetSwizzleOrWriteMask() - Build a swizzle mask out of the letters in an identifier.
 *
 */
 
int GetSwizzleOrWriteMask(SourceLoc *loc, int atom, int *FIsLValue, int *flen)
{
    const char *s, *t;
    int len, bit, mask, bits;
    int groups, group;
    int LIsLValue;
    char ch;

    s = t = GetAtomString(atable, atom);
    len = mask = bits = groups = 0;
    LIsLValue = 1;
    while (*s) {
        ch = *s++;
        switch (ch) {
        case 'x':
            bit = 0;
            group = 1;
            break;
        case 'y':
            bit = 1;
            group = 1;
            break;
        case 'z':
            bit = 2;
            group = 1;
            break;
        case 'w':
            bit = 3;
            group = 1;
            break;
        case 'r':
            bit = 0;
            group = 2;
            break;
        case 'g':
            bit = 1;
            group = 2;
            break;
        case 'b':
            bit = 2;
            group = 2;
            break;
        case 'a':
            bit = 3;
            group = 2;
            break;
        default:
            SemanticError(loc, ERROR_CS_INVALID_SWIZZLE_CHAR, ch, t);
            return mask;
            break;
        }
        mask |= bit << len*2;
        bit = 1 << bit;
        if (bits & bit)
            LIsLValue = 0;
        bits |= bit;
        if (groups && groups != group) {
            SemanticError(loc, ERROR_CS_INVALID_SWIZZLE_CHAR, ch, t);
            return mask;
        }
        groups |= group;
        len++;
    }
    if (len > 4)
        SemanticError(loc, ERROR_S_SWIZZLE_TOO_LONG, t);
    if (FIsLValue)
        *FIsLValue = LIsLValue;
    if (flen)
        *flen = len;
    return mask;
} // GetSwizzleOrWriteMask

/*
 * GetMatrixSwizzleOrWriteMask() - Build a matrix swizzle mask out of the letters in an identifier.
 *
 */
 
int GetMatrixSwizzleOrWriteMask(SourceLoc *loc, int atom, int *FIsLValue, int *flen)
{
    const char *s, *t;
    int len, bit, mask, bits, base;
    int LIsLValue, Error;
    char lch, ch;

    s = t = GetAtomString(atable, atom);
    len = mask = bits = 0;
    LIsLValue = 1;
    if (s[0] == '_' && s[1] != '\0') {
        Error = 0;
        if (s[1] == 'm') {
            base = 0;
        } else {
            base = 1;
        }
        while (*s) {
            ch = lch = *s++;
            if (ch == '_') {
                if (base == 0) {
                    if (*s++ != 'm') {
                        Error = 1;
                        break;
                    }
                }
                lch = *s++;
                ch = lch - base;
                if (ch >= '0' && ch <= '3') {
                    bit = (ch - '0') << 2;
                    lch = *s++;
                    ch = lch - base;
                    if (ch >= '0' && ch <= '3') {
                        bit = bit | (ch - '0');
                        mask |= bit << len*4;
                        bit = 1 << bit;
                        if (bit & bits)
                            LIsLValue = 0;
                        bits |= bit;
                        len++;
                    } else {
                        Error = 1;
                        break;
                    }
                } else {
                    Error = 1;
                    break;
                }
            } else {
                Error = 1;
                break;
            }
        }
    } else {
        lch = *s;
        Error = 1;
    }
    if (Error) {
        SemanticError(loc, ERROR_CS_INVALID_SWIZZLE_CHAR, lch, t);
    }
    if (len > 4)
        SemanticError(loc, ERROR_S_SWIZZLE_TOO_LONG, t);
    if (FIsLValue)
        *FIsLValue = LIsLValue;
    if (flen)
        *flen = len;
    return mask;
} // GetMatrixSwizzleOrWriteMask

/*
 * GetBaseTypeNameString() - Return a pointer to a string representation of a base type name.
 *
 */

const char *GetBaseTypeNameString(int base)
{
    if (base >= 0 && base <= TYPE_BASE_LAST_USER) {
        return GetAtomString(atable, baseTypeNames[base]);
    } else {
        return "*** bad base value ***";
    }
} // GetBaseTypeNameString

/*
 * ClearSymbolTempptr() - Clear the tempptr for all symbols in this tree.
 *
 */

static void ClearSymbolTempptr(Symbol *fSymb)
{
    if (fSymb) {
        fSymb->tempptr = NULL;
        ClearSymbolTempptr(fSymb->left);
        ClearSymbolTempptr(fSymb->right);
    }
} // ClearSymbolTempptr

/*
 * ClearSymbolTempptrList() - Walk a list of scopes and
 *                            clear tempptr field for all symbols.
 *
 */

static void ClearSymbolTempptrList(Scope *fScope)
{
    while (fScope) {
        ClearSymbolTempptr(fScope->symbols);
        fScope = fScope->next;
    }
} // ClearSymbolTempptrList

/*
 * ClearAllSymbolTempptr
 *
 */

void ClearAllSymbolTempptr(void)
{
    ClearSymbolTempptrList(ScopeList);
} // ClearSymbolTempptr


/*
 * ClearSymbolTempptr2() - Clear the tempptr2 for all symbols in this tree.
 *
 */

static void ClearSymbolTempptr2(Symbol *fSymb)
{
    if (fSymb) {
        fSymb->tempptr2 = NULL;
        ClearSymbolTempptr2(fSymb->left);
        ClearSymbolTempptr2(fSymb->right);
    }
} // ClearSymbolTempptr2

/*
 * ClearSymbolTempptr2List() - Walk a list of scopes and
 *                            clear tempptr field for all symbols.
 *
 */

static void ClearSymbolTempptr2List(Scope *fScope)
{
    while (fScope) {
        ClearSymbolTempptr2(fScope->symbols);
        fScope = fScope->next;
    }
} // ClearSymbolTempptr2List

/*
 * ClearAllSymbolTempptr2
 *
 */

void ClearAllSymbolTempptr2(void)
{
    ClearSymbolTempptr2List(ScopeList);
} // ClearSymbolTempptr2

///////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////// End of symbols.c //////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

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