blob: ea60cbb1b048679deede926905e32a8aad7d9286 [file] [log] [blame]
Anders Carlsson3b1d57b2008-01-26 01:36:00 +00001//===--- CGExprConstant.cpp - Emit LLVM Code from Constant Expressions ----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Constant Expr nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000016#include "CGObjCRuntime.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/StmtVisitor.h"
Anders Carlsson3b1d57b2008-01-26 01:36:00 +000019#include "llvm/Constants.h"
20#include "llvm/Function.h"
21#include "llvm/GlobalVariable.h"
22#include "llvm/Support/Compiler.h"
Eli Friedmana8234002008-05-30 10:24:46 +000023#include "llvm/Target/TargetData.h"
Anders Carlsson3b1d57b2008-01-26 01:36:00 +000024using namespace clang;
25using namespace CodeGen;
26
27namespace {
Anders Carlsson84005b42008-01-26 04:30:23 +000028class VISIBILITY_HIDDEN ConstExprEmitter :
29 public StmtVisitor<ConstExprEmitter, llvm::Constant*> {
Anders Carlsson3b1d57b2008-01-26 01:36:00 +000030 CodeGenModule &CGM;
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000031 CodeGenFunction *CGF;
Anders Carlsson3b1d57b2008-01-26 01:36:00 +000032public:
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000033 ConstExprEmitter(CodeGenModule &cgm, CodeGenFunction *cgf)
34 : CGM(cgm), CGF(cgf) {
Anders Carlsson3b1d57b2008-01-26 01:36:00 +000035 }
36
37 //===--------------------------------------------------------------------===//
38 // Visitor Methods
39 //===--------------------------------------------------------------------===//
40
41 llvm::Constant *VisitStmt(Stmt *S) {
Daniel Dunbar488e9932008-08-16 00:56:44 +000042 CGM.ErrorUnsupported(S, "constant expression");
Chris Lattner98be4942008-03-05 18:54:05 +000043 QualType T = cast<Expr>(S)->getType();
44 return llvm::UndefValue::get(CGM.getTypes().ConvertType(T));
Anders Carlsson3b1d57b2008-01-26 01:36:00 +000045 }
46
47 llvm::Constant *VisitParenExpr(ParenExpr *PE) {
48 return Visit(PE->getSubExpr());
49 }
50
51 // Leaves
52 llvm::Constant *VisitIntegerLiteral(const IntegerLiteral *E) {
53 return llvm::ConstantInt::get(E->getValue());
54 }
55 llvm::Constant *VisitFloatingLiteral(const FloatingLiteral *E) {
Chris Lattner59138ba2008-04-20 00:45:53 +000056 return llvm::ConstantFP::get(E->getValue());
Anders Carlsson3b1d57b2008-01-26 01:36:00 +000057 }
58 llvm::Constant *VisitCharacterLiteral(const CharacterLiteral *E) {
59 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
60 }
61 llvm::Constant *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
62 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
63 }
Chris Lattner13fd7e52008-06-21 21:44:18 +000064 llvm::Constant *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000065 std::string S(E->getString()->getStrData(),
66 E->getString()->getByteLength());
67 llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(S);
68 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
Chris Lattner13fd7e52008-06-21 21:44:18 +000069 }
Anders Carlsson3b1d57b2008-01-26 01:36:00 +000070
71 llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
72 return Visit(E->getInitializer());
73 }
74
75 llvm::Constant *VisitCastExpr(const CastExpr* E) {
76 llvm::Constant *C = Visit(E->getSubExpr());
77
78 return EmitConversion(C, E->getSubExpr()->getType(), E->getType());
79 }
Devang Pateleae15602008-02-05 02:39:50 +000080
Chris Lattner04421082008-04-08 04:40:51 +000081 llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
82 return Visit(DAE->getExpr());
83 }
84
Eli Friedmana8234002008-05-30 10:24:46 +000085 llvm::Constant *EmitArrayInitialization(InitListExpr *ILE) {
Eli Friedman4d1c0182008-02-21 17:45:41 +000086 std::vector<llvm::Constant*> Elts;
Eli Friedmana8234002008-05-30 10:24:46 +000087 const llvm::ArrayType *AType =
88 cast<llvm::ArrayType>(ConvertType(ILE->getType()));
Eli Friedman4d1c0182008-02-21 17:45:41 +000089 unsigned NumInitElements = ILE->getNumInits();
90 // FIXME: Check for wide strings
91 if (NumInitElements > 0 && isa<StringLiteral>(ILE->getInit(0)) &&
Chris Lattnerc63a1f22008-08-04 07:31:14 +000092 ILE->getType()->getArrayElementTypeNoTypeQual()->isCharType())
Eli Friedman4d1c0182008-02-21 17:45:41 +000093 return Visit(ILE->getInit(0));
Devang Pateleae15602008-02-05 02:39:50 +000094 const llvm::Type *ElemTy = AType->getElementType();
95 unsigned NumElements = AType->getNumElements();
96
97 // Initialising an array requires us to automatically
98 // initialise any elements that have not been initialised explicitly
99 unsigned NumInitableElts = std::min(NumInitElements, NumElements);
100
101 // Copy initializer elements.
102 unsigned i = 0;
Eli Friedmana04a1532008-05-30 19:58:50 +0000103 bool RewriteType = false;
Devang Pateleae15602008-02-05 02:39:50 +0000104 for (; i < NumInitableElts; ++i) {
Devang Pateleae15602008-02-05 02:39:50 +0000105 llvm::Constant *C = Visit(ILE->getInit(i));
Eli Friedmana04a1532008-05-30 19:58:50 +0000106 RewriteType |= (C->getType() != ElemTy);
Devang Pateleae15602008-02-05 02:39:50 +0000107 Elts.push_back(C);
108 }
Eli Friedmana04a1532008-05-30 19:58:50 +0000109
Devang Pateleae15602008-02-05 02:39:50 +0000110 // Initialize remaining array elements.
111 for (; i < NumElements; ++i)
112 Elts.push_back(llvm::Constant::getNullValue(ElemTy));
113
Eli Friedmana04a1532008-05-30 19:58:50 +0000114 if (RewriteType) {
115 // FIXME: Try to avoid packing the array
116 std::vector<const llvm::Type*> Types;
117 for (unsigned i = 0; i < Elts.size(); ++i)
118 Types.push_back(Elts[i]->getType());
119 const llvm::StructType *SType = llvm::StructType::get(Types, true);
120 return llvm::ConstantStruct::get(SType, Elts);
121 }
122
Devang Pateleae15602008-02-05 02:39:50 +0000123 return llvm::ConstantArray::get(AType, Elts);
124 }
125
Eli Friedmana8234002008-05-30 10:24:46 +0000126 void InsertBitfieldIntoStruct(std::vector<llvm::Constant*>& Elts,
127 FieldDecl* Field, Expr* E) {
128 // Calculate the value to insert
129 llvm::Constant *C = Visit(E);
130 llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(C);
131 if (!CI) {
Daniel Dunbar488e9932008-08-16 00:56:44 +0000132 CGM.ErrorUnsupported(E, "bitfield initialization");
Eli Friedmana8234002008-05-30 10:24:46 +0000133 return;
134 }
135 llvm::APInt V = CI->getValue();
Devang Pateleae15602008-02-05 02:39:50 +0000136
Eli Friedmana8234002008-05-30 10:24:46 +0000137 // Calculate information about the relevant field
138 const llvm::Type* Ty = CI->getType();
Daniel Dunbar9ca8bb02008-08-10 07:00:24 +0000139 const llvm::TargetData &TD = CGM.getTypes().getTargetData();
140 unsigned size = TD.getTypeStoreSizeInBits(Ty);
Eli Friedmana8234002008-05-30 10:24:46 +0000141 unsigned fieldOffset = CGM.getTypes().getLLVMFieldNo(Field) * size;
142 CodeGenTypes::BitFieldInfo bitFieldInfo =
143 CGM.getTypes().getBitFieldInfo(Field);
144 fieldOffset += bitFieldInfo.Begin;
145
146 // Find where to start the insertion
147 // FIXME: This is O(n^2) in the number of bit-fields!
148 // FIXME: This won't work if the struct isn't completely packed!
149 unsigned offset = 0, i = 0;
150 while (offset < (fieldOffset & -8))
Daniel Dunbar9ca8bb02008-08-10 07:00:24 +0000151 offset += TD.getTypeStoreSizeInBits(Elts[i++]->getType());
152
153 // Advance over 0 sized elements (must terminate in bounds since
154 // the bitfield must have a size).
155 while (TD.getTypeStoreSizeInBits(Elts[i]->getType()) == 0)
156 ++i;
Eli Friedmana8234002008-05-30 10:24:46 +0000157
Daniel Dunbar1f3846e2008-07-17 21:07:48 +0000158 // Promote the size of V if necessary
159 // FIXME: This should never occur, but currently it can because
160 // initializer constants are cast to bool, and because clang is
161 // not enforcing bitfield width limits.
162 if (bitFieldInfo.Size > V.getBitWidth())
163 V.zext(bitFieldInfo.Size);
164
Eli Friedmana8234002008-05-30 10:24:46 +0000165 // Insert the bits into the struct
166 // FIXME: This algorthm is only correct on X86!
167 // FIXME: THis algorthm assumes bit-fields only have byte-size elements!
168 unsigned bitsToInsert = bitFieldInfo.Size;
169 unsigned curBits = std::min(8 - (fieldOffset & 7), bitsToInsert);
170 unsigned byte = V.getLoBits(curBits).getZExtValue() << (fieldOffset & 7);
171 do {
172 llvm::Constant* byteC = llvm::ConstantInt::get(llvm::Type::Int8Ty, byte);
173 Elts[i] = llvm::ConstantExpr::getOr(Elts[i], byteC);
174 ++i;
175 V = V.lshr(curBits);
176 bitsToInsert -= curBits;
177
178 if (!bitsToInsert)
179 break;
180
181 curBits = bitsToInsert > 8 ? 8 : bitsToInsert;
182 byte = V.getLoBits(curBits).getZExtValue();
183 } while (true);
184 }
185
186 llvm::Constant *EmitStructInitialization(InitListExpr *ILE) {
187 const llvm::StructType *SType =
188 cast<llvm::StructType>(ConvertType(ILE->getType()));
Eli Friedman017cbdf2008-05-20 00:11:07 +0000189 RecordDecl *RD = ILE->getType()->getAsRecordType()->getDecl();
Devang Pateleae15602008-02-05 02:39:50 +0000190 std::vector<llvm::Constant*> Elts;
Devang Pateleae15602008-02-05 02:39:50 +0000191
Eli Friedman017cbdf2008-05-20 00:11:07 +0000192 // Initialize the whole structure to zero.
193 for (unsigned i = 0; i < SType->getNumElements(); ++i) {
Devang Pateleae15602008-02-05 02:39:50 +0000194 const llvm::Type *FieldTy = SType->getElementType(i);
195 Elts.push_back(llvm::Constant::getNullValue(FieldTy));
196 }
Eli Friedman017cbdf2008-05-20 00:11:07 +0000197
198 // Copy initializer elements. Skip padding fields.
199 unsigned EltNo = 0; // Element no in ILE
200 int FieldNo = 0; // Field no in RecordDecl
Eli Friedmana04a1532008-05-30 19:58:50 +0000201 bool RewriteType = false;
Eli Friedman017cbdf2008-05-20 00:11:07 +0000202 while (EltNo < ILE->getNumInits() && FieldNo < RD->getNumMembers()) {
203 FieldDecl* curField = RD->getMember(FieldNo);
204 FieldNo++;
205 if (!curField->getIdentifier())
206 continue;
207
Eli Friedman017cbdf2008-05-20 00:11:07 +0000208 if (curField->isBitField()) {
Eli Friedmana8234002008-05-30 10:24:46 +0000209 InsertBitfieldIntoStruct(Elts, curField, ILE->getInit(EltNo));
Eli Friedman017cbdf2008-05-20 00:11:07 +0000210 } else {
Eli Friedmana04a1532008-05-30 19:58:50 +0000211 unsigned FieldNo = CGM.getTypes().getLLVMFieldNo(curField);
212 llvm::Constant* C = Visit(ILE->getInit(EltNo));
213 RewriteType |= (C->getType() != Elts[FieldNo]->getType());
214 Elts[FieldNo] = C;
Eli Friedman017cbdf2008-05-20 00:11:07 +0000215 }
216 EltNo++;
217 }
218
Eli Friedmana04a1532008-05-30 19:58:50 +0000219 if (RewriteType) {
220 // FIXME: Make this work for non-packed structs
221 assert(SType->isPacked() && "Cannot recreate unpacked structs");
222 std::vector<const llvm::Type*> Types;
223 for (unsigned i = 0; i < Elts.size(); ++i)
224 Types.push_back(Elts[i]->getType());
225 SType = llvm::StructType::get(Types, true);
226 }
227
Devang Pateleae15602008-02-05 02:39:50 +0000228 return llvm::ConstantStruct::get(SType, Elts);
229 }
230
Eli Friedmana8234002008-05-30 10:24:46 +0000231 llvm::Constant *EmitUnionInitialization(InitListExpr *ILE) {
Eli Friedmana8234002008-05-30 10:24:46 +0000232 RecordDecl *RD = ILE->getType()->getAsRecordType()->getDecl();
233 const llvm::Type *Ty = ConvertType(ILE->getType());
234
235 // Find the field decl we're initializing, if any
236 int FieldNo = 0; // Field no in RecordDecl
Eli Friedmanc55f98d2008-06-06 20:12:37 +0000237 FieldDecl* curField = 0;
238 while (FieldNo < RD->getNumMembers()) {
Eli Friedmana8234002008-05-30 10:24:46 +0000239 curField = RD->getMember(FieldNo);
240 FieldNo++;
Eli Friedmanc55f98d2008-06-06 20:12:37 +0000241 if (curField->getIdentifier())
242 break;
243 }
Eli Friedmana8234002008-05-30 10:24:46 +0000244
Eli Friedmanc55f98d2008-06-06 20:12:37 +0000245 if (!curField || !curField->getIdentifier() || ILE->getNumInits() == 0)
Eli Friedmana8234002008-05-30 10:24:46 +0000246 return llvm::Constant::getNullValue(Ty);
247
248 if (curField->isBitField()) {
249 // Create a dummy struct for bit-field insertion
250 unsigned NumElts = CGM.getTargetData().getABITypeSize(Ty) / 8;
251 llvm::Constant* NV = llvm::Constant::getNullValue(llvm::Type::Int8Ty);
252 std::vector<llvm::Constant*> Elts(NumElts, NV);
253
254 InsertBitfieldIntoStruct(Elts, curField, ILE->getInit(0));
255 const llvm::ArrayType *RetTy =
256 llvm::ArrayType::get(NV->getType(), NumElts);
257 return llvm::ConstantArray::get(RetTy, Elts);
258 }
259
260 llvm::Constant *C = Visit(ILE->getInit(0));
261
262 // Build a struct with the union sub-element as the first member,
263 // and padded to the appropriate size
264 std::vector<llvm::Constant*> Elts;
265 std::vector<const llvm::Type*> Types;
266 Elts.push_back(C);
267 Types.push_back(C->getType());
268 unsigned CurSize = CGM.getTargetData().getTypeStoreSize(C->getType());
269 unsigned TotalSize = CGM.getTargetData().getTypeStoreSize(Ty);
270 while (CurSize < TotalSize) {
271 Elts.push_back(llvm::Constant::getNullValue(llvm::Type::Int8Ty));
272 Types.push_back(llvm::Type::Int8Ty);
273 CurSize++;
274 }
275
276 // This always generates a packed struct
277 // FIXME: Try to generate an unpacked struct when we can
278 llvm::StructType* STy = llvm::StructType::get(Types, true);
279 return llvm::ConstantStruct::get(STy, Elts);
280 }
281
282 llvm::Constant *EmitVectorInitialization(InitListExpr *ILE) {
283 const llvm::VectorType *VType =
284 cast<llvm::VectorType>(ConvertType(ILE->getType()));
Eli Friedmancfb313b2008-05-30 12:02:14 +0000285 const llvm::Type *ElemTy = VType->getElementType();
286 std::vector<llvm::Constant*> Elts;
Devang Pateleae15602008-02-05 02:39:50 +0000287 unsigned NumElements = VType->getNumElements();
Eli Friedmancfb313b2008-05-30 12:02:14 +0000288 unsigned NumInitElements = ILE->getNumInits();
Devang Pateleae15602008-02-05 02:39:50 +0000289
Eli Friedmancfb313b2008-05-30 12:02:14 +0000290 unsigned NumInitableElts = std::min(NumInitElements, NumElements);
291
Devang Pateleae15602008-02-05 02:39:50 +0000292 // Copy initializer elements.
293 unsigned i = 0;
Eli Friedmancfb313b2008-05-30 12:02:14 +0000294 for (; i < NumInitableElts; ++i) {
Devang Pateleae15602008-02-05 02:39:50 +0000295 llvm::Constant *C = Visit(ILE->getInit(i));
Devang Pateleae15602008-02-05 02:39:50 +0000296 Elts.push_back(C);
297 }
298
Eli Friedmancfb313b2008-05-30 12:02:14 +0000299 for (; i < NumElements; ++i)
300 Elts.push_back(llvm::Constant::getNullValue(ElemTy));
301
Devang Pateleae15602008-02-05 02:39:50 +0000302 return llvm::ConstantVector::get(VType, Elts);
303 }
304
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000305 llvm::Constant *VisitInitListExpr(InitListExpr *ILE) {
Eli Friedmana8234002008-05-30 10:24:46 +0000306 if (ILE->getType()->isScalarType()) {
307 // We have a scalar in braces. Just use the first element.
308 if (ILE->getNumInits() > 0)
Anders Carlssonf6884ac2008-01-29 01:15:48 +0000309 return Visit(ILE->getInit(0));
Eli Friedmana8234002008-05-30 10:24:46 +0000310
311 const llvm::Type* RetTy = CGM.getTypes().ConvertType(ILE->getType());
312 return llvm::Constant::getNullValue(RetTy);
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000313 }
Devang Patel47fb6972008-01-29 23:23:18 +0000314
Eli Friedmana8234002008-05-30 10:24:46 +0000315 if (ILE->getType()->isArrayType())
316 return EmitArrayInitialization(ILE);
Devang Patel47fb6972008-01-29 23:23:18 +0000317
Eli Friedmana8234002008-05-30 10:24:46 +0000318 if (ILE->getType()->isStructureType())
319 return EmitStructInitialization(ILE);
320
321 if (ILE->getType()->isUnionType())
322 return EmitUnionInitialization(ILE);
323
324 if (ILE->getType()->isVectorType())
325 return EmitVectorInitialization(ILE);
326
Devang Pateleae15602008-02-05 02:39:50 +0000327 assert(0 && "Unable to handle InitListExpr");
Chris Lattner2b94fe32008-03-01 08:45:05 +0000328 // Get rid of control reaches end of void function warning.
329 // Not reached.
330 return 0;
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000331 }
Eli Friedman3b707e72008-02-21 17:57:49 +0000332
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000333 llvm::Constant *VisitImplicitCastExpr(ImplicitCastExpr *ICExpr) {
Eli Friedman3b707e72008-02-21 17:57:49 +0000334 Expr* SExpr = ICExpr->getSubExpr();
335 QualType SType = SExpr->getType();
336 llvm::Constant *C; // the intermediate expression
337 QualType T; // the type of the intermediate expression
338 if (SType->isArrayType()) {
339 // Arrays decay to a pointer to the first element
340 // VLAs would require special handling, but they can't occur here
341 C = EmitLValue(SExpr);
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000342 llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000343 llvm::Constant *Ops[] = {Idx0, Idx0};
344 C = llvm::ConstantExpr::getGetElementPtr(C, Ops, 2);
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000345 T = CGM.getContext().getArrayDecayedType(SType);
Eli Friedman3b707e72008-02-21 17:57:49 +0000346 } else if (SType->isFunctionType()) {
347 // Function types decay to a pointer to the function
348 C = EmitLValue(SExpr);
349 T = CGM.getContext().getPointerType(SType);
350 } else {
351 C = Visit(SExpr);
352 T = SType;
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000353 }
Eli Friedman3b707e72008-02-21 17:57:49 +0000354
355 // Perform the conversion; note that an implicit cast can both promote
356 // and convert an array/function
357 return EmitConversion(C, T, ICExpr->getType());
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000358 }
Eli Friedman3b707e72008-02-21 17:57:49 +0000359
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000360 llvm::Constant *VisitStringLiteral(StringLiteral *E) {
Chris Lattnera7ad98f2008-02-11 00:02:17 +0000361 assert(!E->getType()->isPointerType() && "Strings are always arrays");
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000362
363 // Otherwise this must be a string initializing an array in a static
364 // initializer. Don't emit it as the address of the string, emit the string
365 // data itself as an inline array.
Daniel Dunbar61432932008-08-13 23:20:05 +0000366 return llvm::ConstantArray::get(CGM.GetStringForStringLiteral(E), false);
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000367 }
368
369 llvm::Constant *VisitDeclRefExpr(DeclRefExpr *E) {
370 const ValueDecl *Decl = E->getDecl();
Anders Carlssona3881fc2008-01-29 01:28:48 +0000371 if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(Decl))
372 return llvm::ConstantInt::get(EC->getInitVal());
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000373 assert(0 && "Unsupported decl ref type!");
374 return 0;
375 }
376
377 llvm::Constant *VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) {
378 return EmitSizeAlignOf(E->getArgumentType(), E->getType(), E->isSizeOf());
379 }
380
Daniel Dunbar54d19092008-08-16 01:41:47 +0000381 llvm::Constant *VisitAddrLabelExpr(const AddrLabelExpr *E) {
382 assert(CGF && "Invalid address of label expression outside function.");
383 llvm::Constant *C =
384 llvm::ConstantInt::get(llvm::Type::Int32Ty,
385 CGF->GetIDForAddrOfLabel(E->getLabel()));
386 return llvm::ConstantExpr::getIntToPtr(C, ConvertType(E->getType()));
387 }
388
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000389 // Unary operators
390 llvm::Constant *VisitUnaryPlus(const UnaryOperator *E) {
391 return Visit(E->getSubExpr());
392 }
393 llvm::Constant *VisitUnaryMinus(const UnaryOperator *E) {
394 return llvm::ConstantExpr::getNeg(Visit(E->getSubExpr()));
395 }
396 llvm::Constant *VisitUnaryNot(const UnaryOperator *E) {
397 return llvm::ConstantExpr::getNot(Visit(E->getSubExpr()));
398 }
399 llvm::Constant *VisitUnaryLNot(const UnaryOperator *E) {
400 llvm::Constant *SubExpr = Visit(E->getSubExpr());
401
402 if (E->getSubExpr()->getType()->isRealFloatingType()) {
403 // Compare against 0.0 for fp scalars.
404 llvm::Constant *Zero = llvm::Constant::getNullValue(SubExpr->getType());
405 SubExpr = llvm::ConstantExpr::getFCmp(llvm::FCmpInst::FCMP_UEQ, SubExpr,
406 Zero);
407 } else {
408 assert((E->getSubExpr()->getType()->isIntegerType() ||
409 E->getSubExpr()->getType()->isPointerType()) &&
410 "Unknown scalar type to convert");
411 // Compare against an integer or pointer null.
412 llvm::Constant *Zero = llvm::Constant::getNullValue(SubExpr->getType());
413 SubExpr = llvm::ConstantExpr::getICmp(llvm::ICmpInst::ICMP_EQ, SubExpr,
414 Zero);
415 }
416
417 return llvm::ConstantExpr::getZExt(SubExpr, ConvertType(E->getType()));
418 }
419 llvm::Constant *VisitUnarySizeOf(const UnaryOperator *E) {
420 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), true);
421 }
422 llvm::Constant *VisitUnaryAlignOf(const UnaryOperator *E) {
423 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), false);
424 }
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000425 llvm::Constant *VisitUnaryAddrOf(const UnaryOperator *E) {
426 return EmitLValue(E->getSubExpr());
427 }
Anders Carlsson5a1deb82008-01-29 15:56:48 +0000428 llvm::Constant *VisitUnaryOffsetOf(const UnaryOperator *E) {
429 int64_t Val = E->evaluateOffsetOf(CGM.getContext());
430
431 assert(E->getType()->isIntegerType() && "Result type must be an integer!");
432
Chris Lattner98be4942008-03-05 18:54:05 +0000433 uint32_t ResultWidth =
434 static_cast<uint32_t>(CGM.getContext().getTypeSize(E->getType()));
Anders Carlsson5a1deb82008-01-29 15:56:48 +0000435 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
436 }
Eli Friedmanbc5ed6e2008-05-29 11:22:45 +0000437
438 llvm::Constant *VisitUnaryExtension(const UnaryOperator *E) {
439 return Visit(E->getSubExpr());
440 }
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000441
Anders Carlssonce6237e2008-01-29 01:33:32 +0000442 // Binary operators
443 llvm::Constant *VisitBinOr(const BinaryOperator *E) {
444 llvm::Constant *LHS = Visit(E->getLHS());
445 llvm::Constant *RHS = Visit(E->getRHS());
446
447 return llvm::ConstantExpr::getOr(LHS, RHS);
448 }
Anders Carlsson7be3d012008-02-05 20:54:21 +0000449 llvm::Constant *VisitBinSub(const BinaryOperator *E) {
450 llvm::Constant *LHS = Visit(E->getLHS());
451 llvm::Constant *RHS = Visit(E->getRHS());
452
453 if (!isa<llvm::PointerType>(RHS->getType())) {
454 // pointer - int
455 if (isa<llvm::PointerType>(LHS->getType())) {
456 llvm::Constant *Idx = llvm::ConstantExpr::getNeg(RHS);
457
458 return llvm::ConstantExpr::getGetElementPtr(LHS, &Idx, 1);
459 }
460
461 // int - int
462 return llvm::ConstantExpr::getSub(LHS, RHS);
463 }
Nuno Lopes77e1dfc2008-06-06 16:41:08 +0000464
465 assert(isa<llvm::PointerType>(LHS->getType()));
466
467 const llvm::Type *ResultType = ConvertType(E->getType());
468 const QualType Type = E->getLHS()->getType();
469 const QualType ElementType = Type->getAsPointerType()->getPointeeType();
470
471 LHS = llvm::ConstantExpr::getPtrToInt(LHS, ResultType);
472 RHS = llvm::ConstantExpr::getPtrToInt(RHS, ResultType);
473
474 llvm::Constant *sub = llvm::ConstantExpr::getSub(LHS, RHS);
475 llvm::Constant *size = EmitSizeAlignOf(ElementType, E->getType(), true);
476 return llvm::ConstantExpr::getSDiv(sub, size);
Anders Carlsson7be3d012008-02-05 20:54:21 +0000477 }
478
479 llvm::Constant *VisitBinShl(const BinaryOperator *E) {
480 llvm::Constant *LHS = Visit(E->getLHS());
481 llvm::Constant *RHS = Visit(E->getRHS());
Anders Carlssonce6237e2008-01-29 01:33:32 +0000482
Anders Carlsson7be3d012008-02-05 20:54:21 +0000483 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
484 // RHS to the same size as the LHS.
485 if (LHS->getType() != RHS->getType())
486 RHS = llvm::ConstantExpr::getIntegerCast(RHS, LHS->getType(), false);
487
488 return llvm::ConstantExpr::getShl(LHS, RHS);
489 }
490
491 llvm::Constant *VisitBinMul(const BinaryOperator *E) {
492 llvm::Constant *LHS = Visit(E->getLHS());
493 llvm::Constant *RHS = Visit(E->getRHS());
494
495 return llvm::ConstantExpr::getMul(LHS, RHS);
496 }
497
498 llvm::Constant *VisitBinDiv(const BinaryOperator *E) {
499 llvm::Constant *LHS = Visit(E->getLHS());
500 llvm::Constant *RHS = Visit(E->getRHS());
501
502 if (LHS->getType()->isFPOrFPVector())
503 return llvm::ConstantExpr::getFDiv(LHS, RHS);
504 else if (E->getType()->isUnsignedIntegerType())
505 return llvm::ConstantExpr::getUDiv(LHS, RHS);
506 else
507 return llvm::ConstantExpr::getSDiv(LHS, RHS);
508 }
509
510 llvm::Constant *VisitBinAdd(const BinaryOperator *E) {
511 llvm::Constant *LHS = Visit(E->getLHS());
512 llvm::Constant *RHS = Visit(E->getRHS());
513
514 if (!E->getType()->isPointerType())
515 return llvm::ConstantExpr::getAdd(LHS, RHS);
516
Anders Carlsson87a98752008-02-06 07:23:14 +0000517 llvm::Constant *Ptr, *Idx;
518 if (isa<llvm::PointerType>(LHS->getType())) { // pointer + int
519 Ptr = LHS;
520 Idx = RHS;
521 } else { // int + pointer
522 Ptr = RHS;
523 Idx = LHS;
524 }
525
526 return llvm::ConstantExpr::getGetElementPtr(Ptr, &Idx, 1);
Anders Carlsson7be3d012008-02-05 20:54:21 +0000527 }
528
529 llvm::Constant *VisitBinAnd(const BinaryOperator *E) {
530 llvm::Constant *LHS = Visit(E->getLHS());
531 llvm::Constant *RHS = Visit(E->getRHS());
532
533 return llvm::ConstantExpr::getAnd(LHS, RHS);
534 }
Eli Friedmanbc5ed6e2008-05-29 11:22:45 +0000535
Eli Friedmanb8965962008-05-29 15:17:49 +0000536 llvm::Constant *EmitCmp(const BinaryOperator *E,
537 llvm::CmpInst::Predicate SignedPred,
538 llvm::CmpInst::Predicate UnsignedPred,
539 llvm::CmpInst::Predicate FloatPred) {
Eli Friedmanbc5ed6e2008-05-29 11:22:45 +0000540 llvm::Constant *LHS = Visit(E->getLHS());
541 llvm::Constant *RHS = Visit(E->getRHS());
Eli Friedmanb8965962008-05-29 15:17:49 +0000542 llvm::Constant *Result;
543 if (LHS->getType()->isInteger() ||
544 isa<llvm::PointerType>(LHS->getType())) {
545 if (E->getLHS()->getType()->isSignedIntegerType())
546 Result = llvm::ConstantExpr::getICmp(SignedPred, LHS, RHS);
547 else
548 Result = llvm::ConstantExpr::getICmp(UnsignedPred, LHS, RHS);
549 } else if (LHS->getType()->isFloatingPoint()) {
550 Result = llvm::ConstantExpr::getFCmp(FloatPred, LHS, RHS);
551 } else {
Daniel Dunbar488e9932008-08-16 00:56:44 +0000552 CGM.ErrorUnsupported(E, "constant expression");
Eli Friedmanb8965962008-05-29 15:17:49 +0000553 Result = llvm::ConstantInt::getFalse();
554 }
Eli Friedmanbc5ed6e2008-05-29 11:22:45 +0000555
556 const llvm::Type* ResultType = ConvertType(E->getType());
Eli Friedmanbc5ed6e2008-05-29 11:22:45 +0000557 return llvm::ConstantExpr::getZExtOrBitCast(Result, ResultType);
558 }
559
Eli Friedmanb8965962008-05-29 15:17:49 +0000560 llvm::Constant *VisitBinNE(const BinaryOperator *E) {
561 return EmitCmp(E, llvm::CmpInst::ICMP_NE, llvm::CmpInst::ICMP_NE,
562 llvm::CmpInst::FCMP_ONE);
563 }
564
565 llvm::Constant *VisitBinEQ(const BinaryOperator *E) {
566 return EmitCmp(E, llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_EQ,
567 llvm::CmpInst::FCMP_OEQ);
568 }
569
570 llvm::Constant *VisitBinLT(const BinaryOperator *E) {
571 return EmitCmp(E, llvm::CmpInst::ICMP_SLT, llvm::CmpInst::ICMP_ULT,
572 llvm::CmpInst::FCMP_OLT);
573 }
574
575 llvm::Constant *VisitBinLE(const BinaryOperator *E) {
576 return EmitCmp(E, llvm::CmpInst::ICMP_SLE, llvm::CmpInst::ICMP_ULE,
577 llvm::CmpInst::FCMP_OLE);
578 }
579
580 llvm::Constant *VisitBinGT(const BinaryOperator *E) {
581 return EmitCmp(E, llvm::CmpInst::ICMP_SGT, llvm::CmpInst::ICMP_UGT,
582 llvm::CmpInst::FCMP_OGT);
583 }
584
585 llvm::Constant *VisitBinGE(const BinaryOperator *E) {
586 return EmitCmp(E, llvm::CmpInst::ICMP_SGE, llvm::CmpInst::ICMP_SGE,
587 llvm::CmpInst::FCMP_OGE);
588 }
589
Eli Friedmanbc5ed6e2008-05-29 11:22:45 +0000590 llvm::Constant *VisitConditionalOperator(const ConditionalOperator *E) {
591 llvm::Constant *Cond = Visit(E->getCond());
592 llvm::Constant *CondVal = EmitConversionToBool(Cond, E->getType());
593 llvm::ConstantInt *CondValInt = dyn_cast<llvm::ConstantInt>(CondVal);
594 if (!CondValInt) {
Daniel Dunbar488e9932008-08-16 00:56:44 +0000595 CGM.ErrorUnsupported(E, "constant expression");
Eli Friedmanbc5ed6e2008-05-29 11:22:45 +0000596 return llvm::Constant::getNullValue(ConvertType(E->getType()));
597 }
598 if (CondValInt->isOne()) {
599 if (E->getLHS())
600 return Visit(E->getLHS());
601 return Cond;
602 }
603
604 return Visit(E->getRHS());
605 }
Anders Carlsson7be3d012008-02-05 20:54:21 +0000606
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000607 // Utility methods
608 const llvm::Type *ConvertType(QualType T) {
609 return CGM.getTypes().ConvertType(T);
610 }
611
612 llvm::Constant *EmitConversionToBool(llvm::Constant *Src, QualType SrcType) {
613 assert(SrcType->isCanonical() && "EmitConversion strips typedefs");
614
615 if (SrcType->isRealFloatingType()) {
616 // Compare against 0.0 for fp scalars.
617 llvm::Constant *Zero = llvm::Constant::getNullValue(Src->getType());
618 return llvm::ConstantExpr::getFCmp(llvm::FCmpInst::FCMP_UNE, Src, Zero);
619 }
620
621 assert((SrcType->isIntegerType() || SrcType->isPointerType()) &&
622 "Unknown scalar type to convert");
623
624 // Compare against an integer or pointer null.
625 llvm::Constant *Zero = llvm::Constant::getNullValue(Src->getType());
626 return llvm::ConstantExpr::getICmp(llvm::ICmpInst::ICMP_NE, Src, Zero);
627 }
628
629 llvm::Constant *EmitConversion(llvm::Constant *Src, QualType SrcType,
630 QualType DstType) {
Chris Lattnerd394cc62008-07-26 22:39:33 +0000631 SrcType = CGM.getContext().getCanonicalType(SrcType);
632 DstType = CGM.getContext().getCanonicalType(DstType);
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000633 if (SrcType == DstType) return Src;
634
635 // Handle conversions to bool first, they are special: comparisons against 0.
636 if (DstType->isBooleanType())
637 return EmitConversionToBool(Src, SrcType);
638
639 const llvm::Type *DstTy = ConvertType(DstType);
640
641 // Ignore conversions like int -> uint.
642 if (Src->getType() == DstTy)
643 return Src;
644
645 // Handle pointer conversions next: pointers can only be converted to/from
646 // other pointers and integers.
Eli Friedmanc53296e2008-06-07 17:27:51 +0000647 if (isa<llvm::PointerType>(DstTy)) {
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000648 // The source value may be an integer, or a pointer.
649 if (isa<llvm::PointerType>(Src->getType()))
650 return llvm::ConstantExpr::getBitCast(Src, DstTy);
Anders Carlsson84005b42008-01-26 04:30:23 +0000651 assert(SrcType->isIntegerType() &&"Not ptr->ptr or int->ptr conversion?");
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000652 return llvm::ConstantExpr::getIntToPtr(Src, DstTy);
653 }
654
Eli Friedmanc53296e2008-06-07 17:27:51 +0000655 if (isa<llvm::PointerType>(Src->getType())) {
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000656 // Must be an ptr to int cast.
657 assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
658 return llvm::ConstantExpr::getPtrToInt(Src, DstTy);
659 }
660
661 // A scalar source can be splatted to a vector of the same element type
662 if (isa<llvm::VectorType>(DstTy) && !isa<VectorType>(SrcType)) {
663 const llvm::VectorType *VT = cast<llvm::VectorType>(DstTy);
664 assert((VT->getElementType() == Src->getType()) &&
665 "Vector element type must match scalar type to splat.");
666 unsigned NumElements = DstType->getAsVectorType()->getNumElements();
667 llvm::SmallVector<llvm::Constant*, 16> Elements;
668 for (unsigned i = 0; i < NumElements; i++)
669 Elements.push_back(Src);
670
671 return llvm::ConstantVector::get(&Elements[0], NumElements);
672 }
673
674 if (isa<llvm::VectorType>(Src->getType()) ||
675 isa<llvm::VectorType>(DstTy)) {
676 return llvm::ConstantExpr::getBitCast(Src, DstTy);
677 }
678
679 // Finally, we have the arithmetic types: real int/float.
680 if (isa<llvm::IntegerType>(Src->getType())) {
681 bool InputSigned = SrcType->isSignedIntegerType();
682 if (isa<llvm::IntegerType>(DstTy))
683 return llvm::ConstantExpr::getIntegerCast(Src, DstTy, InputSigned);
684 else if (InputSigned)
685 return llvm::ConstantExpr::getSIToFP(Src, DstTy);
686 else
687 return llvm::ConstantExpr::getUIToFP(Src, DstTy);
688 }
689
690 assert(Src->getType()->isFloatingPoint() && "Unknown real conversion");
691 if (isa<llvm::IntegerType>(DstTy)) {
692 if (DstType->isSignedIntegerType())
693 return llvm::ConstantExpr::getFPToSI(Src, DstTy);
694 else
695 return llvm::ConstantExpr::getFPToUI(Src, DstTy);
696 }
697
698 assert(DstTy->isFloatingPoint() && "Unknown real conversion");
699 if (DstTy->getTypeID() < Src->getType()->getTypeID())
700 return llvm::ConstantExpr::getFPTrunc(Src, DstTy);
701 else
702 return llvm::ConstantExpr::getFPExtend(Src, DstTy);
703 }
704
705 llvm::Constant *EmitSizeAlignOf(QualType TypeToSize,
706 QualType RetType, bool isSizeOf) {
707 std::pair<uint64_t, unsigned> Info =
Chris Lattner98be4942008-03-05 18:54:05 +0000708 CGM.getContext().getTypeInfo(TypeToSize);
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000709
710 uint64_t Val = isSizeOf ? Info.first : Info.second;
711 Val /= 8; // Return size in bytes, not bits.
712
713 assert(RetType->isIntegerType() && "Result type must be an integer!");
714
Chris Lattner98be4942008-03-05 18:54:05 +0000715 uint32_t ResultWidth =
716 static_cast<uint32_t>(CGM.getContext().getTypeSize(RetType));
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000717 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
718 }
Anders Carlsson84005b42008-01-26 04:30:23 +0000719
720 llvm::Constant *EmitLValue(Expr *E) {
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000721 switch (E->getStmtClass()) {
Eli Friedman546d94c2008-02-11 00:23:10 +0000722 default: break;
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000723 case Expr::ParenExprClass:
724 // Elide parenthesis
725 return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
726 case Expr::CompoundLiteralExprClass: {
727 // Note that due to the nature of compound literals, this is guaranteed
728 // to be the only use of the variable, so we just generate it here.
Anders Carlsson84005b42008-01-26 04:30:23 +0000729 CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E);
730 llvm::Constant* C = Visit(CLE->getInitializer());
Chris Lattnera7ad98f2008-02-11 00:02:17 +0000731 C = new llvm::GlobalVariable(C->getType(),E->getType().isConstQualified(),
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000732 llvm::GlobalValue::InternalLinkage,
733 C, ".compoundliteral", &CGM.getModule());
734 return C;
Anders Carlsson84005b42008-01-26 04:30:23 +0000735 }
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000736 case Expr::DeclRefExprClass: {
Anders Carlsson84005b42008-01-26 04:30:23 +0000737 ValueDecl *Decl = cast<DeclRefExpr>(E)->getDecl();
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000738 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl))
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000739 return CGM.GetAddrOfFunction(FD);
Steve Naroff248a7532008-04-15 22:42:06 +0000740 if (const VarDecl* VD = dyn_cast<VarDecl>(Decl)) {
741 if (VD->isFileVarDecl())
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000742 return CGM.GetAddrOfGlobalVar(VD);
Steve Naroff248a7532008-04-15 22:42:06 +0000743 else if (VD->isBlockVarDecl()) {
744 assert(CGF && "Can't access static local vars without CGF");
745 return CGF->GetAddrOfStaticLocalVar(VD);
746 }
Lauro Ramos Venancio81373352008-02-26 21:41:45 +0000747 }
Eli Friedman546d94c2008-02-11 00:23:10 +0000748 break;
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000749 }
750 case Expr::MemberExprClass: {
Anders Carlsson84005b42008-01-26 04:30:23 +0000751 MemberExpr* ME = cast<MemberExpr>(E);
Anders Carlsson84005b42008-01-26 04:30:23 +0000752 llvm::Constant *Base;
753 if (ME->isArrow())
754 Base = Visit(ME->getBase());
755 else
756 Base = EmitLValue(ME->getBase());
Lauro Ramos Venancio5bfe18c2008-01-30 21:23:20 +0000757
758 unsigned FieldNumber = CGM.getTypes().getLLVMFieldNo(ME->getMemberDecl());
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000759 llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
760 llvm::Constant *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty,
761 FieldNumber);
762 llvm::Value *Ops[] = {Zero, Idx};
763 return llvm::ConstantExpr::getGetElementPtr(Base, Ops, 2);
764 }
765 case Expr::ArraySubscriptExprClass: {
Anders Carlsson84005b42008-01-26 04:30:23 +0000766 ArraySubscriptExpr* ASExpr = cast<ArraySubscriptExpr>(E);
767 llvm::Constant *Base = Visit(ASExpr->getBase());
768 llvm::Constant *Index = Visit(ASExpr->getIdx());
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000769 assert(!ASExpr->getBase()->getType()->isVectorType() &&
770 "Taking the address of a vector component is illegal!");
771 return llvm::ConstantExpr::getGetElementPtr(Base, &Index, 1);
772 }
Daniel Dunbar61432932008-08-13 23:20:05 +0000773 case Expr::StringLiteralClass:
774 return CGM.GetAddrOfConstantStringFromLiteral(cast<StringLiteral>(E));
Anders Carlsson84005b42008-01-26 04:30:23 +0000775 case Expr::UnaryOperatorClass: {
776 UnaryOperator *Exp = cast<UnaryOperator>(E);
777 switch (Exp->getOpcode()) {
Eli Friedman546d94c2008-02-11 00:23:10 +0000778 default: break;
779 case UnaryOperator::Extension:
Anders Carlsson84005b42008-01-26 04:30:23 +0000780 // Extension is just a wrapper for expressions
781 return EmitLValue(Exp->getSubExpr());
782 case UnaryOperator::Real:
783 case UnaryOperator::Imag: {
784 // The address of __real or __imag is just a GEP off the address
785 // of the internal expression
786 llvm::Constant* C = EmitLValue(Exp->getSubExpr());
787 llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
788 llvm::Constant *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty,
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000789 Exp->getOpcode() == UnaryOperator::Imag);
Anders Carlsson84005b42008-01-26 04:30:23 +0000790 llvm::Value *Ops[] = {Zero, Idx};
791 return llvm::ConstantExpr::getGetElementPtr(C, Ops, 2);
792 }
793 case UnaryOperator::Deref:
794 // The address of a deref is just the value of the expression
795 return Visit(Exp->getSubExpr());
796 }
Eli Friedman546d94c2008-02-11 00:23:10 +0000797 break;
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000798 }
Eli Friedman546d94c2008-02-11 00:23:10 +0000799 }
Daniel Dunbar488e9932008-08-16 00:56:44 +0000800 CGM.ErrorUnsupported(E, "constant l-value expression");
Eli Friedman546d94c2008-02-11 00:23:10 +0000801 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
802 return llvm::UndefValue::get(Ty);
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000803 }
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000804
805};
806
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000807} // end anonymous namespace.
808
809
Lauro Ramos Venancio81373352008-02-26 21:41:45 +0000810llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E,
Chris Lattner96196622008-07-26 22:37:01 +0000811 CodeGenFunction *CGF) {
812 QualType type = Context.getCanonicalType(E->getType());
Eli Friedman7dfa6392008-06-01 15:31:44 +0000813
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000814 if (type->isIntegerType()) {
Chris Lattner98be4942008-03-05 18:54:05 +0000815 llvm::APSInt Value(static_cast<uint32_t>(Context.getTypeSize(type)));
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000816 if (E->isIntegerConstantExpr(Value, Context)) {
817 return llvm::ConstantInt::get(Value);
818 }
819 }
Eli Friedman7dfa6392008-06-01 15:31:44 +0000820
821 llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E));
822 if (C->getType() == llvm::Type::Int1Ty) {
823 const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
824 C = llvm::ConstantExpr::getZExt(C, BoolTy);
825 }
826 return C;
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000827}