2008-01-26 09:36:00 +08:00
|
|
|
//===--- CGExprConstant.cpp - Emit LLVM Code from Constant Expressions ----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code to emit Constant Expr nodes as LLVM code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
#include "CodeGenModule.h"
|
2008-08-13 08:59:25 +08:00
|
|
|
#include "CGObjCRuntime.h"
|
2008-10-06 13:59:01 +08:00
|
|
|
#include "clang/AST/APValue.h"
|
2008-08-11 13:00:27 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2009-06-14 09:54:56 +08:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2008-01-26 09:36:00 +08:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/GlobalVariable.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
2008-05-30 18:24:46 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2008-01-26 09:36:00 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
|
|
|
namespace {
|
2008-01-26 12:30:23 +08:00
|
|
|
class VISIBILITY_HIDDEN ConstExprEmitter :
|
|
|
|
public StmtVisitor<ConstExprEmitter, llvm::Constant*> {
|
2008-01-26 09:36:00 +08:00
|
|
|
CodeGenModule &CGM;
|
2008-02-27 05:41:45 +08:00
|
|
|
CodeGenFunction *CGF;
|
2009-07-15 07:10:40 +08:00
|
|
|
llvm::LLVMContext &VMContext;
|
2008-01-26 09:36:00 +08:00
|
|
|
public:
|
2008-02-27 05:41:45 +08:00
|
|
|
ConstExprEmitter(CodeGenModule &cgm, CodeGenFunction *cgf)
|
2009-07-15 07:10:40 +08:00
|
|
|
: CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()) {
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Visitor Methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
llvm::Constant *VisitStmt(Stmt *S) {
|
2009-03-04 00:43:34 +08:00
|
|
|
return 0;
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *VisitParenExpr(ParenExpr *PE) {
|
|
|
|
return Visit(PE->getSubExpr());
|
|
|
|
}
|
2008-11-22 10:34:39 +08:00
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
|
|
|
|
return Visit(E->getInitializer());
|
|
|
|
}
|
|
|
|
|
2008-08-19 07:01:59 +08:00
|
|
|
llvm::Constant *VisitCastExpr(CastExpr* E) {
|
2009-01-17 08:48:48 +08:00
|
|
|
// GCC cast to union extension
|
|
|
|
if (E->getType()->isUnionType()) {
|
|
|
|
const llvm::Type *Ty = ConvertType(E->getType());
|
2009-04-08 12:48:15 +08:00
|
|
|
Expr *SubExpr = E->getSubExpr();
|
|
|
|
return EmitUnion(CGM.EmitConstantExpr(SubExpr, SubExpr->getType(), CGF),
|
|
|
|
Ty);
|
2009-01-17 08:48:48 +08:00
|
|
|
}
|
2009-04-30 15:03:22 +08:00
|
|
|
// Explicit and implicit no-op casts
|
|
|
|
QualType Ty = E->getType(), SubTy = E->getSubExpr()->getType();
|
|
|
|
if (CGM.getContext().hasSameUnqualifiedType(Ty, SubTy)) {
|
2009-02-22 15:29:04 +08:00
|
|
|
return Visit(E->getSubExpr());
|
|
|
|
}
|
|
|
|
return 0;
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2008-01-29 09:15:48 +08:00
|
|
|
|
2008-04-08 12:40:51 +08:00
|
|
|
llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
|
|
|
|
return Visit(DAE->getExpr());
|
|
|
|
}
|
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
llvm::Constant *EmitArrayInitialization(InitListExpr *ILE) {
|
2008-02-22 01:45:41 +08:00
|
|
|
std::vector<llvm::Constant*> Elts;
|
2008-05-30 18:24:46 +08:00
|
|
|
const llvm::ArrayType *AType =
|
|
|
|
cast<llvm::ArrayType>(ConvertType(ILE->getType()));
|
2008-02-22 01:45:41 +08:00
|
|
|
unsigned NumInitElements = ILE->getNumInits();
|
|
|
|
// FIXME: Check for wide strings
|
2009-02-25 06:18:39 +08:00
|
|
|
// FIXME: Check for NumInitElements exactly equal to 1??
|
|
|
|
if (NumInitElements > 0 &&
|
|
|
|
(isa<StringLiteral>(ILE->getInit(0)) ||
|
|
|
|
isa<ObjCEncodeExpr>(ILE->getInit(0))) &&
|
2008-08-04 15:31:14 +08:00
|
|
|
ILE->getType()->getArrayElementTypeNoTypeQual()->isCharType())
|
2008-02-22 01:45:41 +08:00
|
|
|
return Visit(ILE->getInit(0));
|
2008-02-05 10:39:50 +08:00
|
|
|
const llvm::Type *ElemTy = AType->getElementType();
|
|
|
|
unsigned NumElements = AType->getNumElements();
|
|
|
|
|
|
|
|
// Initialising an array requires us to automatically
|
|
|
|
// initialise any elements that have not been initialised explicitly
|
|
|
|
unsigned NumInitableElts = std::min(NumInitElements, NumElements);
|
|
|
|
|
|
|
|
// Copy initializer elements.
|
|
|
|
unsigned i = 0;
|
2008-05-31 03:58:50 +08:00
|
|
|
bool RewriteType = false;
|
2008-02-05 10:39:50 +08:00
|
|
|
for (; i < NumInitableElts; ++i) {
|
2009-04-08 12:48:15 +08:00
|
|
|
Expr *Init = ILE->getInit(i);
|
|
|
|
llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF);
|
2009-02-18 02:43:32 +08:00
|
|
|
if (!C)
|
|
|
|
return 0;
|
2008-05-31 03:58:50 +08:00
|
|
|
RewriteType |= (C->getType() != ElemTy);
|
2008-02-05 10:39:50 +08:00
|
|
|
Elts.push_back(C);
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2008-05-31 03:58:50 +08:00
|
|
|
|
2008-02-05 10:39:50 +08:00
|
|
|
// Initialize remaining array elements.
|
2009-04-14 05:47:26 +08:00
|
|
|
// FIXME: This doesn't handle member pointers correctly!
|
2008-02-05 10:39:50 +08:00
|
|
|
for (; i < NumElements; ++i)
|
2009-07-15 07:10:40 +08:00
|
|
|
Elts.push_back(VMContext.getNullValue(ElemTy));
|
2008-02-05 10:39:50 +08:00
|
|
|
|
2008-05-31 03:58:50 +08:00
|
|
|
if (RewriteType) {
|
|
|
|
// FIXME: Try to avoid packing the array
|
|
|
|
std::vector<const llvm::Type*> Types;
|
|
|
|
for (unsigned i = 0; i < Elts.size(); ++i)
|
|
|
|
Types.push_back(Elts[i]->getType());
|
2009-07-15 07:10:40 +08:00
|
|
|
const llvm::StructType *SType = VMContext.getStructType(Types, true);
|
|
|
|
return VMContext.getConstantStruct(SType, Elts);
|
2008-05-31 03:58:50 +08:00
|
|
|
}
|
|
|
|
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantArray(AType, Elts);
|
2008-02-05 10:39:50 +08:00
|
|
|
}
|
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
void InsertBitfieldIntoStruct(std::vector<llvm::Constant*>& Elts,
|
|
|
|
FieldDecl* Field, Expr* E) {
|
|
|
|
// Calculate the value to insert
|
2009-04-08 12:48:15 +08:00
|
|
|
llvm::Constant *C = CGM.EmitConstantExpr(E, Field->getType(), CGF);
|
2009-02-18 02:43:32 +08:00
|
|
|
if (!C)
|
|
|
|
return;
|
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(C);
|
|
|
|
if (!CI) {
|
2008-08-16 08:56:44 +08:00
|
|
|
CGM.ErrorUnsupported(E, "bitfield initialization");
|
2008-05-30 18:24:46 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
llvm::APInt V = CI->getValue();
|
|
|
|
|
|
|
|
// Calculate information about the relevant field
|
|
|
|
const llvm::Type* Ty = CI->getType();
|
2008-08-10 15:00:24 +08:00
|
|
|
const llvm::TargetData &TD = CGM.getTypes().getTargetData();
|
2009-05-09 15:08:47 +08:00
|
|
|
unsigned size = TD.getTypeAllocSizeInBits(Ty);
|
2009-07-24 01:01:21 +08:00
|
|
|
CodeGenTypes::BitFieldInfo Info = CGM.getTypes().getBitFieldInfo(Field);
|
|
|
|
unsigned FieldOffset = Info.FieldNo * size;
|
|
|
|
|
|
|
|
FieldOffset += Info.Start;
|
2008-05-30 18:24:46 +08:00
|
|
|
|
|
|
|
// Find where to start the insertion
|
|
|
|
// FIXME: This is O(n^2) in the number of bit-fields!
|
|
|
|
// FIXME: This won't work if the struct isn't completely packed!
|
|
|
|
unsigned offset = 0, i = 0;
|
2009-07-24 01:01:21 +08:00
|
|
|
while (offset < (FieldOffset & -8))
|
2009-05-09 15:08:47 +08:00
|
|
|
offset += TD.getTypeAllocSizeInBits(Elts[i++]->getType());
|
2008-08-10 15:00:24 +08:00
|
|
|
|
|
|
|
// Advance over 0 sized elements (must terminate in bounds since
|
|
|
|
// the bitfield must have a size).
|
2009-05-09 15:08:47 +08:00
|
|
|
while (TD.getTypeAllocSizeInBits(Elts[i]->getType()) == 0)
|
2008-08-10 15:00:24 +08:00
|
|
|
++i;
|
2008-05-30 18:24:46 +08:00
|
|
|
|
2008-07-18 05:07:48 +08:00
|
|
|
// Promote the size of V if necessary
|
2009-05-16 15:57:57 +08:00
|
|
|
// FIXME: This should never occur, but currently it can because initializer
|
|
|
|
// constants are cast to bool, and because clang is not enforcing bitfield
|
|
|
|
// width limits.
|
2009-07-24 01:01:21 +08:00
|
|
|
if (Info.Size > V.getBitWidth())
|
|
|
|
V.zext(Info.Size);
|
2008-07-18 05:07:48 +08:00
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
// Insert the bits into the struct
|
|
|
|
// FIXME: This algorthm is only correct on X86!
|
|
|
|
// FIXME: THis algorthm assumes bit-fields only have byte-size elements!
|
2009-07-24 01:01:21 +08:00
|
|
|
unsigned bitsToInsert = Info.Size;
|
|
|
|
unsigned curBits = std::min(8 - (FieldOffset & 7), bitsToInsert);
|
|
|
|
unsigned byte = V.getLoBits(curBits).getZExtValue() << (FieldOffset & 7);
|
2008-05-30 18:24:46 +08:00
|
|
|
do {
|
2009-07-15 07:10:40 +08:00
|
|
|
llvm::Constant* byteC =
|
|
|
|
VMContext.getConstantInt(llvm::Type::Int8Ty, byte);
|
|
|
|
Elts[i] = VMContext.getConstantExprOr(Elts[i], byteC);
|
2008-05-30 18:24:46 +08:00
|
|
|
++i;
|
|
|
|
V = V.lshr(curBits);
|
|
|
|
bitsToInsert -= curBits;
|
2008-02-05 10:39:50 +08:00
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
if (!bitsToInsert)
|
|
|
|
break;
|
|
|
|
|
|
|
|
curBits = bitsToInsert > 8 ? 8 : bitsToInsert;
|
|
|
|
byte = V.getLoBits(curBits).getZExtValue();
|
|
|
|
} while (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *EmitStructInitialization(InitListExpr *ILE) {
|
|
|
|
const llvm::StructType *SType =
|
|
|
|
cast<llvm::StructType>(ConvertType(ILE->getType()));
|
2009-07-18 01:50:17 +08:00
|
|
|
RecordDecl *RD = ILE->getType()->getAsRecordType()->getDecl();
|
2008-02-05 10:39:50 +08:00
|
|
|
std::vector<llvm::Constant*> Elts;
|
2008-05-20 08:11:07 +08:00
|
|
|
|
|
|
|
// Initialize the whole structure to zero.
|
2009-04-14 05:47:26 +08:00
|
|
|
// FIXME: This doesn't handle member pointers correctly!
|
2008-05-20 08:11:07 +08:00
|
|
|
for (unsigned i = 0; i < SType->getNumElements(); ++i) {
|
|
|
|
const llvm::Type *FieldTy = SType->getElementType(i);
|
2009-07-15 07:10:40 +08:00
|
|
|
Elts.push_back(VMContext.getNullValue(FieldTy));
|
2008-05-20 08:11:07 +08:00
|
|
|
}
|
2008-02-05 10:39:50 +08:00
|
|
|
|
|
|
|
// Copy initializer elements. Skip padding fields.
|
|
|
|
unsigned EltNo = 0; // Element no in ILE
|
2008-05-31 03:58:50 +08:00
|
|
|
bool RewriteType = false;
|
2009-06-30 10:36:12 +08:00
|
|
|
for (RecordDecl::field_iterator Field = RD->field_begin(),
|
|
|
|
FieldEnd = RD->field_end();
|
2008-12-12 00:49:14 +08:00
|
|
|
EltNo < ILE->getNumInits() && Field != FieldEnd; ++Field) {
|
|
|
|
if (Field->isBitField()) {
|
2009-06-15 05:41:37 +08:00
|
|
|
if (!Field->getIdentifier())
|
|
|
|
continue;
|
2008-12-12 00:49:14 +08:00
|
|
|
InsertBitfieldIntoStruct(Elts, *Field, ILE->getInit(EltNo));
|
2008-05-20 08:11:07 +08:00
|
|
|
} else {
|
2008-12-12 00:49:14 +08:00
|
|
|
unsigned FieldNo = CGM.getTypes().getLLVMFieldNo(*Field);
|
2009-04-08 12:48:15 +08:00
|
|
|
llvm::Constant *C = CGM.EmitConstantExpr(ILE->getInit(EltNo),
|
|
|
|
Field->getType(), CGF);
|
2009-02-18 02:43:32 +08:00
|
|
|
if (!C) return 0;
|
2008-05-31 03:58:50 +08:00
|
|
|
RewriteType |= (C->getType() != Elts[FieldNo]->getType());
|
|
|
|
Elts[FieldNo] = C;
|
2008-05-20 08:11:07 +08:00
|
|
|
}
|
2008-02-05 10:39:50 +08:00
|
|
|
EltNo++;
|
|
|
|
}
|
2008-05-20 08:11:07 +08:00
|
|
|
|
2008-05-31 03:58:50 +08:00
|
|
|
if (RewriteType) {
|
|
|
|
// FIXME: Make this work for non-packed structs
|
|
|
|
assert(SType->isPacked() && "Cannot recreate unpacked structs");
|
|
|
|
std::vector<const llvm::Type*> Types;
|
|
|
|
for (unsigned i = 0; i < Elts.size(); ++i)
|
|
|
|
Types.push_back(Elts[i]->getType());
|
2009-07-15 07:10:40 +08:00
|
|
|
SType = VMContext.getStructType(Types, true);
|
2008-05-31 03:58:50 +08:00
|
|
|
}
|
|
|
|
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantStruct(SType, Elts);
|
2008-02-05 10:39:50 +08:00
|
|
|
}
|
2008-01-30 07:23:18 +08:00
|
|
|
|
2009-01-17 08:48:48 +08:00
|
|
|
llvm::Constant *EmitUnion(llvm::Constant *C, const llvm::Type *Ty) {
|
2009-02-18 02:43:32 +08:00
|
|
|
if (!C)
|
|
|
|
return 0;
|
|
|
|
|
2009-01-17 08:48:48 +08:00
|
|
|
// Build a struct with the union sub-element as the first member,
|
|
|
|
// and padded to the appropriate size
|
|
|
|
std::vector<llvm::Constant*> Elts;
|
|
|
|
std::vector<const llvm::Type*> Types;
|
|
|
|
Elts.push_back(C);
|
|
|
|
Types.push_back(C->getType());
|
2009-05-09 15:08:47 +08:00
|
|
|
unsigned CurSize = CGM.getTargetData().getTypeAllocSize(C->getType());
|
|
|
|
unsigned TotalSize = CGM.getTargetData().getTypeAllocSize(Ty);
|
2009-07-23 12:50:01 +08:00
|
|
|
|
|
|
|
assert(CurSize <= TotalSize && "Union size mismatch!");
|
|
|
|
if (unsigned NumPadBytes = TotalSize - CurSize) {
|
|
|
|
const llvm::Type *Ty = llvm::Type::Int8Ty;
|
|
|
|
if (NumPadBytes > 1)
|
|
|
|
Ty = VMContext.getArrayType(Ty, NumPadBytes);
|
|
|
|
|
|
|
|
Elts.push_back(VMContext.getNullValue(Ty));
|
|
|
|
Types.push_back(Ty);
|
2009-01-17 08:48:48 +08:00
|
|
|
}
|
|
|
|
|
2009-07-23 12:50:01 +08:00
|
|
|
llvm::StructType* STy = VMContext.getStructType(Types, false);
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantStruct(STy, Elts);
|
2009-01-17 08:48:48 +08:00
|
|
|
}
|
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
llvm::Constant *EmitUnionInitialization(InitListExpr *ILE) {
|
|
|
|
const llvm::Type *Ty = ConvertType(ILE->getType());
|
|
|
|
|
2009-01-30 00:53:55 +08:00
|
|
|
FieldDecl* curField = ILE->getInitializedFieldInUnion();
|
|
|
|
if (!curField) {
|
|
|
|
// There's no field to initialize, so value-initialize the union.
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Make sure that it's really an empty and not a failure of
|
|
|
|
// semantic analysis.
|
2009-07-18 01:50:17 +08:00
|
|
|
RecordDecl *RD = ILE->getType()->getAsRecordType()->getDecl();
|
2009-06-30 10:36:12 +08:00
|
|
|
for (RecordDecl::field_iterator Field = RD->field_begin(),
|
|
|
|
FieldEnd = RD->field_end();
|
2009-01-30 00:53:55 +08:00
|
|
|
Field != FieldEnd; ++Field)
|
|
|
|
assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
|
|
|
|
#endif
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getNullValue(Ty);
|
2009-01-30 00:53:55 +08:00
|
|
|
}
|
2008-05-30 18:24:46 +08:00
|
|
|
|
|
|
|
if (curField->isBitField()) {
|
|
|
|
// Create a dummy struct for bit-field insertion
|
2009-05-09 15:08:47 +08:00
|
|
|
unsigned NumElts = CGM.getTargetData().getTypeAllocSize(Ty);
|
2009-07-13 12:10:07 +08:00
|
|
|
llvm::Constant* NV =
|
2009-07-15 07:10:40 +08:00
|
|
|
VMContext.getNullValue(llvm::Type::Int8Ty);
|
2008-05-30 18:24:46 +08:00
|
|
|
std::vector<llvm::Constant*> Elts(NumElts, NV);
|
2008-02-05 10:39:50 +08:00
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
InsertBitfieldIntoStruct(Elts, curField, ILE->getInit(0));
|
|
|
|
const llvm::ArrayType *RetTy =
|
2009-07-15 07:10:40 +08:00
|
|
|
VMContext.getArrayType(NV->getType(), NumElts);
|
|
|
|
return VMContext.getConstantArray(RetTy, Elts);
|
2008-05-30 18:24:46 +08:00
|
|
|
}
|
|
|
|
|
2009-04-14 05:47:26 +08:00
|
|
|
llvm::Constant *InitElem;
|
|
|
|
if (ILE->getNumInits() > 0) {
|
|
|
|
Expr *Init = ILE->getInit(0);
|
|
|
|
InitElem = CGM.EmitConstantExpr(Init, Init->getType(), CGF);
|
|
|
|
} else {
|
|
|
|
InitElem = CGM.EmitNullConstant(curField->getType());
|
|
|
|
}
|
|
|
|
return EmitUnion(InitElem, Ty);
|
2008-05-30 18:24:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *EmitVectorInitialization(InitListExpr *ILE) {
|
|
|
|
const llvm::VectorType *VType =
|
|
|
|
cast<llvm::VectorType>(ConvertType(ILE->getType()));
|
2008-05-30 20:02:14 +08:00
|
|
|
const llvm::Type *ElemTy = VType->getElementType();
|
|
|
|
std::vector<llvm::Constant*> Elts;
|
2008-02-05 10:39:50 +08:00
|
|
|
unsigned NumElements = VType->getNumElements();
|
2008-05-30 20:02:14 +08:00
|
|
|
unsigned NumInitElements = ILE->getNumInits();
|
|
|
|
|
|
|
|
unsigned NumInitableElts = std::min(NumInitElements, NumElements);
|
2008-01-30 07:23:18 +08:00
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
// Copy initializer elements.
|
|
|
|
unsigned i = 0;
|
2008-05-30 20:02:14 +08:00
|
|
|
for (; i < NumInitableElts; ++i) {
|
2009-04-08 12:48:15 +08:00
|
|
|
Expr *Init = ILE->getInit(i);
|
|
|
|
llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF);
|
2009-02-18 02:43:32 +08:00
|
|
|
if (!C)
|
|
|
|
return 0;
|
2008-01-26 09:36:00 +08:00
|
|
|
Elts.push_back(C);
|
|
|
|
}
|
2008-02-05 10:39:50 +08:00
|
|
|
|
2008-05-30 20:02:14 +08:00
|
|
|
for (; i < NumElements; ++i)
|
2009-07-15 07:10:40 +08:00
|
|
|
Elts.push_back(VMContext.getNullValue(ElemTy));
|
2008-05-30 20:02:14 +08:00
|
|
|
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantVector(VType, Elts);
|
2008-02-05 10:39:50 +08:00
|
|
|
}
|
2009-01-30 14:13:25 +08:00
|
|
|
|
|
|
|
llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E) {
|
2009-04-14 05:47:26 +08:00
|
|
|
return CGM.EmitNullConstant(E->getType());
|
2009-01-30 14:13:25 +08:00
|
|
|
}
|
|
|
|
|
2008-02-05 10:39:50 +08:00
|
|
|
llvm::Constant *VisitInitListExpr(InitListExpr *ILE) {
|
2008-05-30 18:24:46 +08:00
|
|
|
if (ILE->getType()->isScalarType()) {
|
|
|
|
// We have a scalar in braces. Just use the first element.
|
2009-04-08 12:48:15 +08:00
|
|
|
if (ILE->getNumInits() > 0) {
|
|
|
|
Expr *Init = ILE->getInit(0);
|
|
|
|
return CGM.EmitConstantExpr(Init, Init->getType(), CGF);
|
|
|
|
}
|
2009-04-14 05:47:26 +08:00
|
|
|
return CGM.EmitNullConstant(ILE->getType());
|
2008-01-30 07:23:18 +08:00
|
|
|
}
|
2008-10-27 07:53:12 +08:00
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
if (ILE->getType()->isArrayType())
|
|
|
|
return EmitArrayInitialization(ILE);
|
|
|
|
|
|
|
|
if (ILE->getType()->isStructureType())
|
|
|
|
return EmitStructInitialization(ILE);
|
|
|
|
|
|
|
|
if (ILE->getType()->isUnionType())
|
|
|
|
return EmitUnionInitialization(ILE);
|
|
|
|
|
|
|
|
if (ILE->getType()->isVectorType())
|
|
|
|
return EmitVectorInitialization(ILE);
|
2008-01-30 07:23:18 +08:00
|
|
|
|
2008-02-05 10:39:50 +08:00
|
|
|
assert(0 && "Unable to handle InitListExpr");
|
2008-03-01 16:45:05 +08:00
|
|
|
// Get rid of control reaches end of void function warning.
|
|
|
|
// Not reached.
|
|
|
|
return 0;
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2008-02-22 01:57:49 +08:00
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
llvm::Constant *VisitStringLiteral(StringLiteral *E) {
|
2008-02-11 08:02:17 +08:00
|
|
|
assert(!E->getType()->isPointerType() && "Strings are always arrays");
|
2008-01-26 09:36:00 +08:00
|
|
|
|
2009-02-25 06:18:39 +08:00
|
|
|
// This must be a string initializing an array in a static initializer.
|
|
|
|
// Don't emit it as the address of the string, emit the string data itself
|
|
|
|
// as an inline array.
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantArray(CGM.GetStringForStringLiteral(E), false);
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
|
|
|
|
2009-02-25 06:18:39 +08:00
|
|
|
llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
|
|
|
|
// This must be an @encode initializing an array in a static initializer.
|
|
|
|
// Don't emit it as the address of the string, emit the string data itself
|
|
|
|
// as an inline array.
|
|
|
|
std::string Str;
|
|
|
|
CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str);
|
|
|
|
const ConstantArrayType *CAT = cast<ConstantArrayType>(E->getType());
|
|
|
|
|
|
|
|
// Resize the string to the right size, adding zeros at the end, or
|
|
|
|
// truncating as needed.
|
|
|
|
Str.resize(CAT->getSize().getZExtValue(), '\0');
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantArray(Str, false);
|
2009-02-25 06:18:39 +08:00
|
|
|
}
|
|
|
|
|
2008-05-29 19:22:45 +08:00
|
|
|
llvm::Constant *VisitUnaryExtension(const UnaryOperator *E) {
|
|
|
|
return Visit(E->getSubExpr());
|
|
|
|
}
|
2009-02-20 06:01:56 +08:00
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
// Utility methods
|
|
|
|
const llvm::Type *ConvertType(QualType T) {
|
|
|
|
return CGM.getTypes().ConvertType(T);
|
|
|
|
}
|
2008-01-26 12:30:23 +08:00
|
|
|
|
2008-11-16 14:23:45 +08:00
|
|
|
public:
|
2008-01-26 12:30:23 +08:00
|
|
|
llvm::Constant *EmitLValue(Expr *E) {
|
2008-01-26 10:08:50 +08:00
|
|
|
switch (E->getStmtClass()) {
|
2008-02-11 08:23:10 +08:00
|
|
|
default: break;
|
2008-01-26 10:08:50 +08:00
|
|
|
case Expr::CompoundLiteralExprClass: {
|
|
|
|
// Note that due to the nature of compound literals, this is guaranteed
|
|
|
|
// to be the only use of the variable, so we just generate it here.
|
2008-01-26 12:30:23 +08:00
|
|
|
CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E);
|
|
|
|
llvm::Constant* C = Visit(CLE->getInitializer());
|
2009-02-18 02:43:32 +08:00
|
|
|
// FIXME: "Leaked" on failure.
|
|
|
|
if (C)
|
2009-07-09 03:05:04 +08:00
|
|
|
C = new llvm::GlobalVariable(CGM.getModule(), C->getType(),
|
2009-02-18 02:43:32 +08:00
|
|
|
E->getType().isConstQualified(),
|
|
|
|
llvm::GlobalValue::InternalLinkage,
|
2009-07-09 03:05:04 +08:00
|
|
|
C, ".compoundliteral");
|
2008-01-26 10:08:50 +08:00
|
|
|
return C;
|
2008-01-26 12:30:23 +08:00
|
|
|
}
|
2009-01-06 13:10:23 +08:00
|
|
|
case Expr::DeclRefExprClass:
|
|
|
|
case Expr::QualifiedDeclRefExprClass: {
|
2008-10-22 00:13:35 +08:00
|
|
|
NamedDecl *Decl = cast<DeclRefExpr>(E)->getDecl();
|
2008-01-26 10:08:50 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl))
|
2009-05-13 05:21:08 +08:00
|
|
|
return CGM.GetAddrOfFunction(GlobalDecl(FD));
|
2008-04-16 06:42:06 +08:00
|
|
|
if (const VarDecl* VD = dyn_cast<VarDecl>(Decl)) {
|
2009-02-25 02:41:57 +08:00
|
|
|
// We can never refer to a variable with local storage.
|
|
|
|
if (!VD->hasLocalStorage()) {
|
|
|
|
if (VD->isFileVarDecl() || VD->hasExternalStorage())
|
|
|
|
return CGM.GetAddrOfGlobalVar(VD);
|
|
|
|
else if (VD->isBlockVarDecl()) {
|
|
|
|
assert(CGF && "Can't access static local vars without CGF");
|
|
|
|
return CGF->GetAddrOfStaticLocalVar(VD);
|
|
|
|
}
|
2008-04-16 06:42:06 +08:00
|
|
|
}
|
2008-02-27 05:41:45 +08:00
|
|
|
}
|
2008-02-11 08:23:10 +08:00
|
|
|
break;
|
2008-01-26 10:08:50 +08:00
|
|
|
}
|
2008-08-14 07:20:05 +08:00
|
|
|
case Expr::StringLiteralClass:
|
|
|
|
return CGM.GetAddrOfConstantStringFromLiteral(cast<StringLiteral>(E));
|
2009-02-25 06:18:39 +08:00
|
|
|
case Expr::ObjCEncodeExprClass:
|
|
|
|
return CGM.GetAddrOfConstantStringFromObjCEncode(cast<ObjCEncodeExpr>(E));
|
2008-11-16 14:23:45 +08:00
|
|
|
case Expr::ObjCStringLiteralClass: {
|
|
|
|
ObjCStringLiteral* SL = cast<ObjCStringLiteral>(E);
|
2009-04-01 00:53:37 +08:00
|
|
|
llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(SL);
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantExprBitCast(C, ConvertType(E->getType()));
|
2008-11-16 14:23:45 +08:00
|
|
|
}
|
2008-12-12 13:18:02 +08:00
|
|
|
case Expr::PredefinedExprClass: {
|
|
|
|
// __func__/__FUNCTION__ -> "". __PRETTY_FUNCTION__ -> "top level".
|
|
|
|
std::string Str;
|
|
|
|
if (cast<PredefinedExpr>(E)->getIdentType() ==
|
|
|
|
PredefinedExpr::PrettyFunction)
|
|
|
|
Str = "top level";
|
|
|
|
|
|
|
|
return CGM.GetAddrOfConstantCString(Str, ".tmp");
|
|
|
|
}
|
2009-01-25 09:21:06 +08:00
|
|
|
case Expr::AddrLabelExprClass: {
|
|
|
|
assert(CGF && "Invalid address of label expression outside function.");
|
|
|
|
unsigned id = CGF->GetIDForAddrOfLabel(cast<AddrLabelExpr>(E)->getLabel());
|
2009-07-15 07:10:40 +08:00
|
|
|
llvm::Constant *C = VMContext.getConstantInt(llvm::Type::Int32Ty, id);
|
|
|
|
return VMContext.getConstantExprIntToPtr(C, ConvertType(E->getType()));
|
2009-01-25 09:21:06 +08:00
|
|
|
}
|
2009-01-25 09:54:01 +08:00
|
|
|
case Expr::CallExprClass: {
|
|
|
|
CallExpr* CE = cast<CallExpr>(E);
|
2009-02-15 02:57:46 +08:00
|
|
|
if (CE->isBuiltinCall(CGM.getContext()) !=
|
|
|
|
Builtin::BI__builtin___CFStringMakeConstantString)
|
2009-01-25 09:54:01 +08:00
|
|
|
break;
|
|
|
|
const Expr *Arg = CE->getArg(0)->IgnoreParenCasts();
|
|
|
|
const StringLiteral *Literal = cast<StringLiteral>(Arg);
|
2009-04-01 00:53:37 +08:00
|
|
|
// FIXME: need to deal with UCN conversion issues.
|
2009-04-01 21:55:36 +08:00
|
|
|
return CGM.GetAddrOfConstantCFString(Literal);
|
2009-01-25 09:54:01 +08:00
|
|
|
}
|
2009-02-19 09:01:04 +08:00
|
|
|
case Expr::BlockExprClass: {
|
2009-03-01 09:09:12 +08:00
|
|
|
std::string FunctionName;
|
|
|
|
if (CGF)
|
|
|
|
FunctionName = CGF->CurFn->getName();
|
|
|
|
else
|
|
|
|
FunctionName = "global";
|
|
|
|
|
|
|
|
return CGM.GetAddrOfGlobalBlock(cast<BlockExpr>(E), FunctionName.c_str());
|
2009-02-19 09:01:04 +08:00
|
|
|
}
|
2008-02-11 08:23:10 +08:00
|
|
|
}
|
2009-02-18 02:43:32 +08:00
|
|
|
|
|
|
|
return 0;
|
2008-01-26 10:08:50 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
} // end anonymous namespace.
|
|
|
|
|
2008-02-27 05:41:45 +08:00
|
|
|
llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E,
|
2009-04-08 12:48:15 +08:00
|
|
|
QualType DestType,
|
2008-07-27 06:37:01 +08:00
|
|
|
CodeGenFunction *CGF) {
|
2008-12-01 10:42:14 +08:00
|
|
|
Expr::EvalResult Result;
|
|
|
|
|
2009-04-11 09:08:03 +08:00
|
|
|
bool Success = false;
|
|
|
|
|
2009-05-27 14:04:58 +08:00
|
|
|
if (DestType->isReferenceType())
|
|
|
|
Success = E->EvaluateAsLValue(Result, Context);
|
|
|
|
else
|
2009-04-11 09:08:03 +08:00
|
|
|
Success = E->Evaluate(Result, Context);
|
|
|
|
|
|
|
|
if (Success) {
|
2008-12-01 10:42:14 +08:00
|
|
|
assert(!Result.HasSideEffects &&
|
|
|
|
"Constant expr should not have any side effects!");
|
|
|
|
switch (Result.Val.getKind()) {
|
2009-01-29 06:24:07 +08:00
|
|
|
case APValue::Uninitialized:
|
2009-02-18 02:43:32 +08:00
|
|
|
assert(0 && "Constant expressions should be initialized.");
|
|
|
|
return 0;
|
2008-11-16 04:45:50 +08:00
|
|
|
case APValue::LValue: {
|
2009-04-11 09:08:03 +08:00
|
|
|
const llvm::Type *DestTy = getTypes().ConvertTypeForMem(DestType);
|
2008-12-01 10:42:14 +08:00
|
|
|
llvm::Constant *Offset =
|
2009-07-15 07:10:40 +08:00
|
|
|
VMContext.getConstantInt(llvm::Type::Int64Ty,
|
2008-12-01 10:42:14 +08:00
|
|
|
Result.Val.getLValueOffset());
|
2008-11-16 04:45:50 +08:00
|
|
|
|
2009-02-20 05:44:24 +08:00
|
|
|
llvm::Constant *C;
|
2008-12-01 10:42:14 +08:00
|
|
|
if (const Expr *LVBase = Result.Val.getLValueBase()) {
|
2009-02-20 05:44:24 +08:00
|
|
|
C = ConstExprEmitter(*this, CGF).EmitLValue(const_cast<Expr*>(LVBase));
|
|
|
|
|
|
|
|
// Apply offset if necessary.
|
|
|
|
if (!Offset->isNullValue()) {
|
|
|
|
const llvm::Type *Type =
|
2009-07-15 07:10:40 +08:00
|
|
|
VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
|
|
|
|
llvm::Constant *Casted = VMContext.getConstantExprBitCast(C, Type);
|
|
|
|
Casted = VMContext.getConstantExprGetElementPtr(Casted, &Offset, 1);
|
|
|
|
C = VMContext.getConstantExprBitCast(Casted, C->getType());
|
2009-02-20 05:44:24 +08:00
|
|
|
}
|
2008-11-16 14:23:45 +08:00
|
|
|
|
2009-02-20 05:44:24 +08:00
|
|
|
// Convert to the appropriate type; this could be an lvalue for
|
|
|
|
// an integer.
|
2009-04-10 12:59:59 +08:00
|
|
|
if (isa<llvm::PointerType>(DestTy))
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantExprBitCast(C, DestTy);
|
2009-02-20 05:44:24 +08:00
|
|
|
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantExprPtrToInt(C, DestTy);
|
2009-02-20 05:44:24 +08:00
|
|
|
} else {
|
|
|
|
C = Offset;
|
|
|
|
|
|
|
|
// Convert to the appropriate type; this could be an lvalue for
|
|
|
|
// an integer.
|
2009-04-10 12:59:59 +08:00
|
|
|
if (isa<llvm::PointerType>(DestTy))
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantExprIntToPtr(C, DestTy);
|
2009-02-20 05:44:24 +08:00
|
|
|
|
|
|
|
// If the types don't match this should only be a truncate.
|
2009-04-10 12:59:59 +08:00
|
|
|
if (C->getType() != DestTy)
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantExprTrunc(C, DestTy);
|
2009-02-20 05:44:24 +08:00
|
|
|
|
|
|
|
return C;
|
2008-11-16 14:23:45 +08:00
|
|
|
}
|
2008-11-16 04:45:50 +08:00
|
|
|
}
|
2008-11-17 11:57:28 +08:00
|
|
|
case APValue::Int: {
|
2009-07-15 07:10:40 +08:00
|
|
|
llvm::Constant *C = VMContext.getConstantInt(Result.Val.getInt());
|
2008-11-16 04:45:50 +08:00
|
|
|
|
|
|
|
if (C->getType() == llvm::Type::Int1Ty) {
|
|
|
|
const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
|
2009-07-15 07:10:40 +08:00
|
|
|
C = VMContext.getConstantExprZExt(C, BoolTy);
|
2008-11-16 04:45:50 +08:00
|
|
|
}
|
|
|
|
return C;
|
2008-11-17 11:57:28 +08:00
|
|
|
}
|
2009-01-29 06:24:07 +08:00
|
|
|
case APValue::ComplexInt: {
|
|
|
|
llvm::Constant *Complex[2];
|
|
|
|
|
2009-07-15 07:10:40 +08:00
|
|
|
Complex[0] = VMContext.getConstantInt(Result.Val.getComplexIntReal());
|
|
|
|
Complex[1] = VMContext.getConstantInt(Result.Val.getComplexIntImag());
|
2009-01-29 06:24:07 +08:00
|
|
|
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantStruct(Complex, 2);
|
2009-01-29 06:24:07 +08:00
|
|
|
}
|
2008-11-16 04:45:50 +08:00
|
|
|
case APValue::Float:
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantFP(Result.Val.getFloat());
|
2008-11-17 09:58:55 +08:00
|
|
|
case APValue::ComplexFloat: {
|
|
|
|
llvm::Constant *Complex[2];
|
|
|
|
|
2009-07-15 07:10:40 +08:00
|
|
|
Complex[0] = VMContext.getConstantFP(Result.Val.getComplexFloatReal());
|
|
|
|
Complex[1] = VMContext.getConstantFP(Result.Val.getComplexFloatImag());
|
2008-11-17 09:58:55 +08:00
|
|
|
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantStruct(Complex, 2);
|
2008-11-17 09:58:55 +08:00
|
|
|
}
|
2009-01-18 09:01:34 +08:00
|
|
|
case APValue::Vector: {
|
|
|
|
llvm::SmallVector<llvm::Constant *, 4> Inits;
|
|
|
|
unsigned NumElts = Result.Val.getVectorLength();
|
|
|
|
|
|
|
|
for (unsigned i = 0; i != NumElts; ++i) {
|
|
|
|
APValue &Elt = Result.Val.getVectorElt(i);
|
|
|
|
if (Elt.isInt())
|
2009-07-15 07:10:40 +08:00
|
|
|
Inits.push_back(VMContext.getConstantInt(Elt.getInt()));
|
2009-01-18 09:01:34 +08:00
|
|
|
else
|
2009-07-15 07:10:40 +08:00
|
|
|
Inits.push_back(VMContext.getConstantFP(Elt.getFloat()));
|
2009-01-18 09:01:34 +08:00
|
|
|
}
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantVector(&Inits[0], Inits.size());
|
2009-01-18 09:01:34 +08:00
|
|
|
}
|
2008-11-16 04:45:50 +08:00
|
|
|
}
|
|
|
|
}
|
2008-06-01 23:31:44 +08:00
|
|
|
|
|
|
|
llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E));
|
2009-02-18 02:43:32 +08:00
|
|
|
if (C && C->getType() == llvm::Type::Int1Ty) {
|
2008-06-01 23:31:44 +08:00
|
|
|
const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
|
2009-07-15 07:10:40 +08:00
|
|
|
C = VMContext.getConstantExprZExt(C, BoolTy);
|
2008-06-01 23:31:44 +08:00
|
|
|
}
|
|
|
|
return C;
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2009-04-14 05:47:26 +08:00
|
|
|
|
|
|
|
llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) {
|
|
|
|
// Always return an LLVM null constant for now; this will change when we
|
|
|
|
// get support for IRGen of member pointers.
|
2009-07-13 12:10:07 +08:00
|
|
|
return getLLVMContext().getNullValue(getTypes().ConvertType(T));
|
2009-04-14 05:47:26 +08:00
|
|
|
}
|