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

📄 hal.c

📁 cg编译器
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************\
Copyright (c) 2002, NVIDIA Corporation.

NVIDIA Corporation("NVIDIA") supplies this software to you in
consideration of your agreement to the following terms, and your use,
installation, modification or redistribution of this NVIDIA software
constitutes acceptance of these terms.  If you do not agree with these
terms, please do not use, install, modify or redistribute this NVIDIA
software.

In consideration of your agreement to abide by the following terms, and
subject to these terms, NVIDIA grants you a personal, non-exclusive
license, under NVIDIA's copyrights in this original NVIDIA software (the
"NVIDIA Software"), to use, reproduce, modify and redistribute the
NVIDIA Software, with or without modifications, in source and/or binary
forms; provided that if you redistribute the NVIDIA Software, you must
retain the copyright notice of NVIDIA, this notice and the following
text and disclaimers in all such redistributions of the NVIDIA Software.
Neither the name, trademarks, service marks nor logos of NVIDIA
Corporation may be used to endorse or promote products derived from the
NVIDIA Software without specific prior written permission from NVIDIA.
Except as expressly stated in this notice, no other rights or licenses
express or implied, are granted by NVIDIA herein, including but not
limited to any patent rights that may be infringed by your derivative
works or by other works in which the NVIDIA Software may be
incorporated. No hardware is licensed hereunder. 

THE NVIDIA SOFTWARE IS BEING PROVIDED ON AN "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING WITHOUT LIMITATION, WARRANTIES OR CONDITIONS OF TITLE,
NON-INFRINGEMENT, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
ITS USE AND OPERATION EITHER ALONE OR IN COMBINATION WITH OTHER
PRODUCTS.

IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT,
INCIDENTAL, EXEMPLARY, CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, LOST PROFITS; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) OR ARISING IN ANY WAY
OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE
NVIDIA SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT,
TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\****************************************************************************/

//
// hal.c
//

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "slglobals.h"

static void InitHAL_HAL(slHAL *);
static int GetCapsBit_HAL(int bitNumber);
static int GetConnectorUses_HAL(int cid, int pid);
static int GetConnectorRegister_HAL(int cid, int ByIndex, int ratom, Binding *fBind);
static int GetFloatSuffixBase_HAL(SourceLoc *loc, int suffix);
static int GetSizeof_HAL(Type *fType);
static int GetAlignment_HAL(Type *fType);
static int CheckDeclarators_HAL(SourceLoc *loc, const dtype *fDtype);
static int CheckDefinition_HAL(SourceLoc *loc, int name, const Type *fType);
static int CheckStatement_HAL(SourceLoc *loc, stmt *fstmt);
static int CheckInternalFunction_HAL(Symbol *fSymb, int *group);
static int IsNumericBase_HAL(int fBase);
static int IsIntegralBase_HAL(int fBase);
static int IsTexobjBase_HAL(int fBase);
static int IsValidRuntimeBase_HAL(int fBase);
static int IsValidScalarCast_HAL(int toBase, int fromBase, int Explicit);
static int IsValidOperator_HAL(SourceLoc *loc, int name, int op, int suobp);
static int GetBinOpBase_HAL(int lop, int lbase, int rbase, int llen, int rlen);
static int ConvertConstant_HAL(const scalar_constant *fval, int fbase, int tbase,
                    expr **fexpr);
static int BindUniformUnbound_HAL(SourceLoc *loc, Symbol *fSymb, Binding *lBind);
static int BindUniformPragma_HAL(SourceLoc *loc, Symbol *fSymb, Binding *lBind,
                    const Binding *fBind);
static int BindVaryingSemantic_HAL(SourceLoc *loc, Symbol *fSymb, int semantic,
                    Binding *fBind, int IsOutVal);
static int BindVaryingPragma_HAL(SourceLoc *loc, Symbol *fSymb, Binding *lBind,
                    const Binding *fBind, int IsOutVal);
static int BindVaryingUnbound_HAL(SourceLoc *loc, Symbol *fSymb, int name, int connector,
                    Binding *fBind, int IsOutVal);
static int PrintCodeHeader_HAL(FILE *out);
static int GenerateCode_HAL(SourceLoc *loc, Scope *fScope, Symbol *program);

///////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////// Profile Manager: //////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////

/*
 * RegisterProfile() - Add a profile to the list of available profiles.
 *
 */

slProfile *RegisterProfile(int (*InitHAL)(slHAL *), const char *name, int id)
{
    slProfile *lProfile;

    lProfile = (slProfile *) malloc(sizeof(slProfile));
    lProfile->next = Cg->allProfiles;
    lProfile->InitHAL = InitHAL;
    lProfile->name = AddAtom(atable, name);
    lProfile->id = id;
    Cg->allProfiles = lProfile;
    return lProfile;
} // RegisterProfile

/*
 * EnumerateProfiles()
 *
 */

slProfile *EnumerateProfiles(int index)
{
    slProfile *lProfile;
    
    lProfile = Cg->allProfiles;
    while (index-- > 0 && lProfile)
        lProfile = lProfile->next;
    return lProfile;
} // EnumerateProfiles

///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// Default Language HAL ////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////

/*
 * InitHAL()
 *
 */

int InitHAL(const char *profileName, const char *entryName)
{
    slProfile *lProfile;
    int result;

    Cg->theHAL = (slHAL *) malloc(sizeof(slHAL));
    InitHAL_HAL(Cg->theHAL);
    Cg->theHAL->profileName = AddAtom(atable, profileName);
    Cg->theHAL->entryName = AddAtom(atable, entryName);
    lProfile = Cg->allProfiles;
    while (lProfile) {
        if (Cg->theHAL->profileName == lProfile->name) {
            Cg->theHAL->InitHAL = lProfile->InitHAL;
            Cg->theHAL->pid = lProfile->id;
            result = Cg->theHAL->InitHAL(Cg->theHAL);
            return result;
        }
        lProfile = lProfile->next;
    }
    printf(OPENSL_TAG ": unknown profile \"%s\".\n", profileName);
    return 0;
} // InitHAL

/*
 * InitHAL_HAL();
 *
 */

static void InitHAL_HAL(slHAL *fHAL)
{
    // Initialize default function members:

    fHAL->InitHAL = NULL;
    fHAL->FreeHAL = NULL;
    fHAL->RegisterNames = NULL;
    fHAL->GetCapsBit = GetCapsBit_HAL;
    fHAL->GetConnectorID = NULL;
    fHAL->GetConnectorAtom = NULL;
    fHAL->GetConnectorUses = GetConnectorUses_HAL;
    fHAL->GetConnectorRegister = GetConnectorRegister_HAL;
    fHAL->GetFloatSuffixBase = GetFloatSuffixBase_HAL;
    fHAL->GetSizeof = GetSizeof_HAL;
    fHAL->GetAlignment = GetAlignment_HAL;
    fHAL->CheckDeclarators = CheckDeclarators_HAL;
    fHAL->CheckDefinition = CheckDefinition_HAL;
    fHAL->CheckStatement = CheckStatement_HAL;
    fHAL->CheckInternalFunction = CheckInternalFunction_HAL;
    fHAL->IsNumericBase = IsNumericBase_HAL;
    fHAL->IsTexobjBase = IsTexobjBase_HAL;
    fHAL->IsIntegralBase = IsIntegralBase_HAL;
    fHAL->IsValidRuntimeBase = IsValidRuntimeBase_HAL;
    fHAL->IsValidScalarCast = IsValidScalarCast_HAL;
    fHAL->IsValidOperator = IsValidOperator_HAL;
    fHAL->GetBinOpBase = GetBinOpBase_HAL;
    fHAL->ConvertConstant = ConvertConstant_HAL;
    fHAL->BindUniformUnbound = BindUniformUnbound_HAL;
    fHAL->BindUniformPragma = BindUniformPragma_HAL;
    fHAL->BindVaryingSemantic = BindVaryingSemantic_HAL;
    fHAL->BindVaryingPragma = BindVaryingPragma_HAL;
    fHAL->BindVaryingUnbound = BindVaryingUnbound_HAL;
    fHAL->PrintCodeHeader = PrintCodeHeader_HAL;
    fHAL->GenerateCode = GenerateCode_HAL;

    // Initialize default data members:

    // Defined when profile is registered:

    fHAL->vendor = "None";
    fHAL->version = "0.0";

    fHAL->profileName = 0;
    fHAL->pid = 0;
    fHAL->entryName = 0;

    fHAL->semantics = NULL;
    fHAL->numSemantics = 0;

    fHAL->incid = CID_NONE_ID;
    fHAL->inputCRegs = NULL;
    fHAL->numInputCRegs = 0;

    fHAL->nextUnboundVinReg = 0;
    fHAL->lastUnboundVinReg = -1;

    fHAL->outcid = CID_NONE_ID;
    fHAL->outputCRegs = NULL;
    fHAL->numOutputCRegs = 0;

    fHAL->nextUnboundVoutReg = 0;
    fHAL->lastUnboundVoutReg = -1;

    // Defined by compiler front end:

    fHAL->globalScope = NULL;
    fHAL->varyingIn = NULL;
    fHAL->varyingOut = NULL;
    fHAL->uniformParam = NULL;
    fHAL->uniformGlobal = NULL;
    fHAL->uniforms = NULL;

    fHAL->constantBindings = NULL;
    fHAL->defaultBindings = NULL;

    // Define default comment start string:

    fHAL->comment = "#";

    // Pointer to profile specific struct:

    fHAL->localData = NULL;

} // InitHAL_HAL

/*
 * AddConstantBinding() - Add a constant binding to the program.
 *
 */

void AddConstantBinding(Binding *fBind)
{
    BindingList *lBindList, *nBindList;

    nBindList = (BindingList *) malloc(sizeof(BindingList));
    nBindList->next = NULL;
    nBindList->binding = fBind;
    lBindList = Cg->theHAL->constantBindings;
    if (lBindList) {
        while (lBindList->next)
            lBindList = lBindList->next;
        lBindList->next = nBindList;
    } else {
        Cg->theHAL->constantBindings = nBindList;
    }
} // AddConstantBinding

/*
 * AddDefaultBinding() - Add a default binding to the program.
 *
 */

void AddDefaultBinding(Binding *fBind)
{
    BindingList *lBindList, *nBindList;

    nBindList = (BindingList *) malloc(sizeof(BindingList));
    nBindList->next = NULL;
    nBindList->binding = fBind;
    lBindList = Cg->theHAL->defaultBindings;
    if (lBindList) {
        while (lBindList->next)
            lBindList = lBindList->next;
        lBindList->next = nBindList;
    } else {
        Cg->theHAL->defaultBindings = nBindList;
    }
} // AddDefaultBinding

/*
 * LookupConnectorHAL() - Lookup a connector descriptor by cid.
 *
 */

ConnectorDescriptor *LookupConnectorHAL(ConnectorDescriptor *connectors, int cid, int num)
{
    int ii;

    for (ii = 0; ii < num; ii++) {
        if (cid == connectors[ii].cid)
            return &connectors[ii];
    }
    return NULL;
} // LookupConnectorHAL

/*
 * SetSymbolConnectorBindingHAL()
 *
 */

void SetSymbolConnectorBindingHAL(Binding *fBind, ConnectorRegisters *fConn)
{
    BindingConnector *tBind;

    tBind = &fBind->conn;
    tBind->properties = BIND_IS_BOUND;
    tBind->kind = BK_CONNECTOR;
    if (fConn->properties & REG_WRITE_REQUIRED)
        tBind->properties |= BIND_WRITE_REQUIRED;
    // tBind->gname set elsewhere
    // tBind->lname set elsewhere
    tBind->base = fConn->base;
    tBind->size = fConn->size;
    tBind->rname = fConn->name;
    tBind->regno = fConn->regno;
} // SetSymbolConnectorBindingHAL

/*
 * GetCapsBit_HAL() - Return an integer value representing the capabilities of this profile.
 *
 */

static int GetCapsBit_HAL(int bitNumber)
{
    return 0;
} // GetCapsBit_HAL

/*
 * GetConnectorUses_HAL() - Return a connectors capabilities for this profile.
 *
 */

static int GetConnectorUses_HAL(int cid, int pid)
{
    return CONNECTOR_IS_USELESS;
} // GetConnectorUses_HAL

/*
 * GetConnectorRegister_HAL() - Return the hw register number for "ratom" in connector "cid".
 *
 */

static int GetConnectorRegister_HAL(int cid, int ByIndex, int ratom, Binding *fBind)
{
    return 0;
} // GetConnectorRegister_HAL

/*
 * GetFloatSuffixBase_HAL() - Check for profile-specific limitations of floating point
 *         suffixes and return the base for this suffix.
 *
 */

static int GetFloatSuffixBase_HAL(SourceLoc *loc, int suffix)
{
    switch (suffix) {
    case ' ':
        return TYPE_BASE_CFLOAT;
    case 'f':
        return TYPE_BASE_FLOAT;
    default:
        SemanticError(loc, ERROR_C_UNSUPPORTED_FP_SUFFIX, suffix);
        return TYPE_BASE_UNDEFINED_TYPE;
    }
} // GetFloatSuffixBase_HAL

/*
 * GetSizeof_HAL() - Return a profile specific size for this scalar, vector, or matrix type.
 *         Used for defining struct member offsets for use by code generator.
 */

static int GetSizeof_HAL(Type *fType)
{
    int category, size, alignment, len, len2;

    if (fType) {
        category = GetCategory(fType);
        switch (category) {
        case TYPE_CATEGORY_SCALAR:
        case TYPE_CATEGORY_STRUCT:
        case TYPE_CATEGORY_CONNECTOR:
            size = fType->co.size;
            break;
        case TYPE_CATEGORY_ARRAY:
            if (IsVector(fType, &len)) {
                size = len;
            } else if (IsMatrix(fType, &len, &len2)) {
                if (len2 > len) {
                    size = len*4;
                } else {
                    size = len2*4;
                }
            } else {
                size = Cg->theHAL->GetSizeof(fType->arr.eltype);
                alignment = Cg->theHAL->GetAlignment(fType->arr.eltype);
                size = ((size + alignment - 1)/alignment)*alignment*fType->arr.numels;
            }
            break;
        case TYPE_CATEGORY_FUNCTION:

⌨️ 快捷键说明

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