Implement NullPointerExpr::GetConstant()
Also reworked TypeCastExpr::GetConstant() to just forward the request along and moved the code that was previously there to handle uniform->varying smears of function pointers to FunctionSymbolExpr::GetConstant(). Fixes issue #168.
This commit is contained in:
72
expr.cpp
72
expr.cpp
@@ -6037,28 +6037,15 @@ llvm::Constant *
|
|||||||
TypeCastExpr::GetConstant(const Type *constType) const {
|
TypeCastExpr::GetConstant(const Type *constType) const {
|
||||||
// We don't need to worry about most the basic cases where the type
|
// We don't need to worry about most the basic cases where the type
|
||||||
// cast can resolve to a constant here, since the
|
// cast can resolve to a constant here, since the
|
||||||
// TypeCastExpr::Optimize() method ends up doing the type conversion
|
// TypeCastExpr::Optimize() method generally ends up doing the type
|
||||||
// and returning a ConstExpr, which in turn will have its GetConstant()
|
// conversion and returning a ConstExpr, which in turn will have its
|
||||||
// method called. Thus, the only case we do need to worry about here
|
// GetConstant() method called. However, because ConstExpr currently
|
||||||
// is converting a uniform function pointer to a varying function
|
// can't represent pointer values, we have to handle two cases here:
|
||||||
// pointer of the same type.
|
// 1. Null pointers (NULL, 0) valued initializers, and
|
||||||
|
// 2. Converting a uniform function pointer to a varying function
|
||||||
|
// pointer of the same type.
|
||||||
Assert(Type::Equal(constType, type));
|
Assert(Type::Equal(constType, type));
|
||||||
const FunctionType *ft = NULL;
|
return expr->GetConstant(constType);
|
||||||
if (dynamic_cast<const PointerType *>(type) == NULL ||
|
|
||||||
(ft = dynamic_cast<const FunctionType *>(type->GetBaseType())) == NULL)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
llvm::Constant *ec = expr->GetConstant(expr->GetType());
|
|
||||||
if (ec == NULL)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
ec = llvm::ConstantExpr::getPtrToInt(ec, LLVMTypes::PointerIntType);
|
|
||||||
|
|
||||||
Assert(type->IsVaryingType());
|
|
||||||
std::vector<llvm::Constant *> smear;
|
|
||||||
for (int i = 0; i < g->target.vectorWidth; ++i)
|
|
||||||
smear.push_back(ec);
|
|
||||||
return llvm::ConstantVector::get(smear);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -6553,13 +6540,30 @@ FunctionSymbolExpr::Print() const {
|
|||||||
|
|
||||||
llvm::Constant *
|
llvm::Constant *
|
||||||
FunctionSymbolExpr::GetConstant(const Type *type) const {
|
FunctionSymbolExpr::GetConstant(const Type *type) const {
|
||||||
Assert(type->IsUniformType());
|
if (matchingFunc == NULL || matchingFunc->function == NULL)
|
||||||
Assert(GetType()->IsUniformType());
|
|
||||||
|
|
||||||
if (Type::EqualIgnoringConst(type, GetType()) == false)
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
return matchingFunc ? matchingFunc->function : NULL;
|
const FunctionType *ft;
|
||||||
|
if (dynamic_cast<const PointerType *>(type) == NULL ||
|
||||||
|
(ft = dynamic_cast<const FunctionType *>(type->GetBaseType())) == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
LLVM_TYPE_CONST llvm::Type *llvmUnifType =
|
||||||
|
type->GetAsUniformType()->LLVMType(g->ctx);
|
||||||
|
if (llvmUnifType != matchingFunc->function->getType())
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (type->IsUniformType())
|
||||||
|
return matchingFunc->function;
|
||||||
|
else {
|
||||||
|
llvm::Constant *intPtr =
|
||||||
|
llvm::ConstantExpr::getPtrToInt(matchingFunc->function,
|
||||||
|
LLVMTypes::PointerIntType);
|
||||||
|
std::vector<llvm::Constant *> smear;
|
||||||
|
for (int i = 0; i < g->target.vectorWidth; ++i)
|
||||||
|
smear.push_back(intPtr);
|
||||||
|
return llvm::ConstantVector::get(smear);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -6985,6 +6989,22 @@ NullPointerExpr::Optimize() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
llvm::Constant *
|
||||||
|
NullPointerExpr::GetConstant(const Type *type) const {
|
||||||
|
const PointerType *pt = dynamic_cast<const PointerType *>(type);
|
||||||
|
if (pt == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
LLVM_TYPE_CONST llvm::Type *llvmType = type->LLVMType(g->ctx);
|
||||||
|
if (llvmType == NULL) {
|
||||||
|
Assert(m->errorCount > 0);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return llvm::Constant::getNullValue(llvmType);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
NullPointerExpr::Print() const {
|
NullPointerExpr::Print() const {
|
||||||
printf("NULL");
|
printf("NULL");
|
||||||
|
|||||||
1
expr.h
1
expr.h
@@ -684,6 +684,7 @@ public:
|
|||||||
const Type *GetType() const;
|
const Type *GetType() const;
|
||||||
Expr *TypeCheck();
|
Expr *TypeCheck();
|
||||||
Expr *Optimize();
|
Expr *Optimize();
|
||||||
|
llvm::Constant *GetConstant(const Type *type) const;
|
||||||
void Print() const;
|
void Print() const;
|
||||||
int EstimateCost() const;
|
int EstimateCost() const;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user