blob: a501145a495836811e924ecff68625c80b8b0b95 [file] [log] [blame]
Chris Lattner7f02f722007-08-24 05:35:26 +00001//===--- CGExprScalar.cpp - Emit LLVM Code for Scalar Exprs ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner7f02f722007-08-24 05:35:26 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Expr nodes with scalar LLVM types as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
16#include "clang/AST/AST.h"
Chris Lattner25ddea72008-04-20 00:50:39 +000017#include "clang/Basic/TargetInfo.h"
Chris Lattner7f02f722007-08-24 05:35:26 +000018#include "llvm/Constants.h"
19#include "llvm/Function.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000020#include "llvm/GlobalVariable.h"
Anders Carlsson7c50aca2007-10-15 20:28:48 +000021#include "llvm/Intrinsics.h"
Chris Lattner7f02f722007-08-24 05:35:26 +000022#include "llvm/Support/Compiler.h"
Chris Lattnerc89bf692008-01-03 07:05:49 +000023#include <cstdarg>
Ted Kremenek6aad91a2007-12-10 23:44:32 +000024
Chris Lattner7f02f722007-08-24 05:35:26 +000025using namespace clang;
26using namespace CodeGen;
27using llvm::Value;
28
29//===----------------------------------------------------------------------===//
30// Scalar Expression Emitter
31//===----------------------------------------------------------------------===//
32
33struct BinOpInfo {
34 Value *LHS;
35 Value *RHS;
Chris Lattner1f1ded92007-08-24 21:00:35 +000036 QualType Ty; // Computation Type.
Chris Lattner7f02f722007-08-24 05:35:26 +000037 const BinaryOperator *E;
38};
39
40namespace {
41class VISIBILITY_HIDDEN ScalarExprEmitter
42 : public StmtVisitor<ScalarExprEmitter, Value*> {
43 CodeGenFunction &CGF;
Chris Lattner50b36742008-04-13 07:32:11 +000044 llvm::IRBuilder &Builder;
Chris Lattner2b94fe32008-03-01 08:45:05 +000045 CGObjCRuntime *Runtime;
46
Chris Lattner7f02f722007-08-24 05:35:26 +000047public:
48
Chris Lattner2b94fe32008-03-01 08:45:05 +000049 ScalarExprEmitter(CodeGenFunction &cgf) : CGF(cgf),
50 Builder(CGF.Builder),
51 Runtime(CGF.CGM.getObjCRuntime()) {
Chris Lattner7f02f722007-08-24 05:35:26 +000052 }
Chris Lattner7f02f722007-08-24 05:35:26 +000053
54 //===--------------------------------------------------------------------===//
55 // Utilities
56 //===--------------------------------------------------------------------===//
57
58 const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); }
59 LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); }
60
61 Value *EmitLoadOfLValue(LValue LV, QualType T) {
Chris Lattner9b655512007-08-31 22:49:20 +000062 return CGF.EmitLoadOfLValue(LV, T).getScalarVal();
Chris Lattner7f02f722007-08-24 05:35:26 +000063 }
64
65 /// EmitLoadOfLValue - Given an expression with complex type that represents a
66 /// value l-value, this method emits the address of the l-value, then loads
67 /// and returns the result.
68 Value *EmitLoadOfLValue(const Expr *E) {
69 // FIXME: Volatile
70 return EmitLoadOfLValue(EmitLValue(E), E->getType());
71 }
72
Chris Lattner9abc84e2007-08-26 16:42:57 +000073 /// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner3420d0d2007-08-26 17:25:57 +000074 /// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattner9abc84e2007-08-26 16:42:57 +000075 Value *EmitConversionToBool(Value *Src, QualType DstTy);
76
Chris Lattner3707b252007-08-26 06:48:56 +000077 /// EmitScalarConversion - Emit a conversion from the specified type to the
78 /// specified destination type, both of which are LLVM scalar types.
Chris Lattner4f1a7b32007-08-26 16:34:22 +000079 Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy);
80
81 /// EmitComplexToScalarConversion - Emit a conversion from the specified
82 /// complex type to the specified destination type, where the destination
83 /// type is an LLVM scalar type.
84 Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
85 QualType SrcTy, QualType DstTy);
Chris Lattner3707b252007-08-26 06:48:56 +000086
Chris Lattner7f02f722007-08-24 05:35:26 +000087 //===--------------------------------------------------------------------===//
88 // Visitor Methods
89 //===--------------------------------------------------------------------===//
90
91 Value *VisitStmt(Stmt *S) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +000092 S->dump(CGF.getContext().getSourceManager());
Chris Lattner7f02f722007-08-24 05:35:26 +000093 assert(0 && "Stmt can't have complex result type!");
94 return 0;
95 }
96 Value *VisitExpr(Expr *S);
97 Value *VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr()); }
98
99 // Leaves.
100 Value *VisitIntegerLiteral(const IntegerLiteral *E) {
101 return llvm::ConstantInt::get(E->getValue());
102 }
103 Value *VisitFloatingLiteral(const FloatingLiteral *E) {
Chris Lattner59138ba2008-04-20 00:45:53 +0000104 return llvm::ConstantFP::get(E->getValue());
Chris Lattner7f02f722007-08-24 05:35:26 +0000105 }
106 Value *VisitCharacterLiteral(const CharacterLiteral *E) {
107 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
108 }
Nate Begemane7579b52007-11-15 05:40:03 +0000109 Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
110 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
111 }
Chris Lattner7f02f722007-08-24 05:35:26 +0000112 Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
113 return llvm::ConstantInt::get(ConvertType(E->getType()),
Steve Naroffec0550f2007-10-15 20:41:53 +0000114 CGF.getContext().typesAreCompatible(
115 E->getArgType1(), E->getArgType2()));
Chris Lattner7f02f722007-08-24 05:35:26 +0000116 }
117 Value *VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) {
118 return EmitSizeAlignOf(E->getArgumentType(), E->getType(), E->isSizeOf());
119 }
120
121 // l-values.
122 Value *VisitDeclRefExpr(DeclRefExpr *E) {
123 if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(E->getDecl()))
124 return llvm::ConstantInt::get(EC->getInitVal());
125 return EmitLoadOfLValue(E);
126 }
Chris Lattner2b94fe32008-03-01 08:45:05 +0000127 Value *VisitObjCMessageExpr(ObjCMessageExpr *E);
Chris Lattnerce5605e2008-03-30 23:25:33 +0000128 Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { return EmitLoadOfLValue(E);}
Chris Lattner7f02f722007-08-24 05:35:26 +0000129 Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
130 Value *VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); }
Nate Begeman213541a2008-04-18 23:10:10 +0000131 Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); }
Chris Lattner7f02f722007-08-24 05:35:26 +0000132 Value *VisitStringLiteral(Expr *E) { return EmitLValue(E).getAddress(); }
133 Value *VisitPreDefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); }
Devang Patel35634f52007-10-24 17:18:43 +0000134
135 Value *VisitInitListExpr(InitListExpr *E) {
Anders Carlsson7019a9e2007-12-05 07:36:10 +0000136 unsigned NumInitElements = E->getNumInits();
137
Anders Carlsson7019a9e2007-12-05 07:36:10 +0000138 const llvm::VectorType *VType =
Anders Carlssonf6884ac2008-01-29 01:15:48 +0000139 dyn_cast<llvm::VectorType>(ConvertType(E->getType()));
140
141 // We have a scalar in braces. Just use the first element.
142 if (!VType)
143 return Visit(E->getInit(0));
Anders Carlsson7019a9e2007-12-05 07:36:10 +0000144
Anders Carlsson7019a9e2007-12-05 07:36:10 +0000145 unsigned NumVectorElements = VType->getNumElements();
146 const llvm::Type *ElementType = VType->getElementType();
Anders Carlsson7019a9e2007-12-05 07:36:10 +0000147
148 // Emit individual vector element stores.
149 llvm::Value *V = llvm::UndefValue::get(VType);
150
Anders Carlsson222d2c82007-12-18 02:45:33 +0000151 // Emit initializers
152 unsigned i;
153 for (i = 0; i < NumInitElements; ++i) {
Devang Patela83cc332007-10-24 18:05:48 +0000154 Value *NewV = Visit(E->getInit(i));
155 Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
156 V = Builder.CreateInsertElement(V, NewV, Idx);
Devang Patel35634f52007-10-24 17:18:43 +0000157 }
Anders Carlsson7019a9e2007-12-05 07:36:10 +0000158
159 // Emit remaining default initializers
160 for (/* Do not initialize i*/; i < NumVectorElements; ++i) {
161 Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
162 llvm::Value *NewV = llvm::Constant::getNullValue(ElementType);
163 V = Builder.CreateInsertElement(V, NewV, Idx);
164 }
165
Devang Patela83cc332007-10-24 18:05:48 +0000166 return V;
Devang Patel35634f52007-10-24 17:18:43 +0000167 }
168
169 Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
170 return Visit(E->getInitializer());
171 }
Chris Lattner04421082008-04-08 04:40:51 +0000172
Chris Lattner7f02f722007-08-24 05:35:26 +0000173 Value *VisitImplicitCastExpr(const ImplicitCastExpr *E);
174 Value *VisitCastExpr(const CastExpr *E) {
175 return EmitCastExpr(E->getSubExpr(), E->getType());
176 }
177 Value *EmitCastExpr(const Expr *E, QualType T);
178
179 Value *VisitCallExpr(const CallExpr *E) {
Chris Lattner9b655512007-08-31 22:49:20 +0000180 return CGF.EmitCallExpr(E).getScalarVal();
Chris Lattner7f02f722007-08-24 05:35:26 +0000181 }
182
Chris Lattner33793202007-08-31 22:09:40 +0000183 Value *VisitStmtExpr(const StmtExpr *E);
184
Chris Lattner7f02f722007-08-24 05:35:26 +0000185 // Unary Operators.
186 Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre);
187 Value *VisitUnaryPostDec(const UnaryOperator *E) {
188 return VisitPrePostIncDec(E, false, false);
189 }
190 Value *VisitUnaryPostInc(const UnaryOperator *E) {
191 return VisitPrePostIncDec(E, true, false);
192 }
193 Value *VisitUnaryPreDec(const UnaryOperator *E) {
194 return VisitPrePostIncDec(E, false, true);
195 }
196 Value *VisitUnaryPreInc(const UnaryOperator *E) {
197 return VisitPrePostIncDec(E, true, true);
198 }
199 Value *VisitUnaryAddrOf(const UnaryOperator *E) {
200 return EmitLValue(E->getSubExpr()).getAddress();
201 }
202 Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
203 Value *VisitUnaryPlus(const UnaryOperator *E) {
204 return Visit(E->getSubExpr());
205 }
206 Value *VisitUnaryMinus (const UnaryOperator *E);
207 Value *VisitUnaryNot (const UnaryOperator *E);
208 Value *VisitUnaryLNot (const UnaryOperator *E);
209 Value *VisitUnarySizeOf (const UnaryOperator *E) {
210 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), true);
211 }
212 Value *VisitUnaryAlignOf (const UnaryOperator *E) {
213 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), false);
214 }
215 Value *EmitSizeAlignOf(QualType TypeToSize, QualType RetType,
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000216 bool isSizeOf);
Chris Lattner46f93d02007-08-24 21:20:17 +0000217 Value *VisitUnaryReal (const UnaryOperator *E);
218 Value *VisitUnaryImag (const UnaryOperator *E);
Chris Lattner7f02f722007-08-24 05:35:26 +0000219 Value *VisitUnaryExtension(const UnaryOperator *E) {
220 return Visit(E->getSubExpr());
221 }
Anders Carlsson5a1deb82008-01-29 15:56:48 +0000222 Value *VisitUnaryOffsetOf(const UnaryOperator *E);
Chris Lattner04421082008-04-08 04:40:51 +0000223 Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
224 return Visit(DAE->getExpr());
225 }
Anders Carlsson5a1deb82008-01-29 15:56:48 +0000226
Chris Lattner7f02f722007-08-24 05:35:26 +0000227 // Binary Operators.
Chris Lattner7f02f722007-08-24 05:35:26 +0000228 Value *EmitMul(const BinOpInfo &Ops) {
229 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
230 }
231 Value *EmitDiv(const BinOpInfo &Ops);
232 Value *EmitRem(const BinOpInfo &Ops);
233 Value *EmitAdd(const BinOpInfo &Ops);
234 Value *EmitSub(const BinOpInfo &Ops);
235 Value *EmitShl(const BinOpInfo &Ops);
236 Value *EmitShr(const BinOpInfo &Ops);
237 Value *EmitAnd(const BinOpInfo &Ops) {
238 return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and");
239 }
240 Value *EmitXor(const BinOpInfo &Ops) {
241 return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor");
242 }
243 Value *EmitOr (const BinOpInfo &Ops) {
244 return Builder.CreateOr(Ops.LHS, Ops.RHS, "or");
245 }
246
Chris Lattner1f1ded92007-08-24 21:00:35 +0000247 BinOpInfo EmitBinOps(const BinaryOperator *E);
Chris Lattner3ccf7742007-08-26 21:41:21 +0000248 Value *EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner1f1ded92007-08-24 21:00:35 +0000249 Value *(ScalarExprEmitter::*F)(const BinOpInfo &));
250
251 // Binary operators and binary compound assignment operators.
252#define HANDLEBINOP(OP) \
Chris Lattner3ccf7742007-08-26 21:41:21 +0000253 Value *VisitBin ## OP(const BinaryOperator *E) { \
254 return Emit ## OP(EmitBinOps(E)); \
255 } \
256 Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \
257 return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \
Chris Lattner1f1ded92007-08-24 21:00:35 +0000258 }
259 HANDLEBINOP(Mul);
260 HANDLEBINOP(Div);
261 HANDLEBINOP(Rem);
262 HANDLEBINOP(Add);
263 // (Sub) - Sub is handled specially below for ptr-ptr subtract.
264 HANDLEBINOP(Shl);
265 HANDLEBINOP(Shr);
266 HANDLEBINOP(And);
267 HANDLEBINOP(Xor);
268 HANDLEBINOP(Or);
269#undef HANDLEBINOP
270 Value *VisitBinSub(const BinaryOperator *E);
Chris Lattner3ccf7742007-08-26 21:41:21 +0000271 Value *VisitBinSubAssign(const CompoundAssignOperator *E) {
Chris Lattner1f1ded92007-08-24 21:00:35 +0000272 return EmitCompoundAssign(E, &ScalarExprEmitter::EmitSub);
273 }
274
Chris Lattner7f02f722007-08-24 05:35:26 +0000275 // Comparisons.
276 Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc,
277 unsigned SICmpOpc, unsigned FCmpOpc);
278#define VISITCOMP(CODE, UI, SI, FP) \
279 Value *VisitBin##CODE(const BinaryOperator *E) { \
280 return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
281 llvm::FCmpInst::FP); }
282 VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT);
283 VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT);
284 VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE);
285 VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE);
286 VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ);
287 VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE);
288#undef VISITCOMP
289
290 Value *VisitBinAssign (const BinaryOperator *E);
291
292 Value *VisitBinLAnd (const BinaryOperator *E);
293 Value *VisitBinLOr (const BinaryOperator *E);
Chris Lattner7f02f722007-08-24 05:35:26 +0000294 Value *VisitBinComma (const BinaryOperator *E);
295
296 // Other Operators.
297 Value *VisitConditionalOperator(const ConditionalOperator *CO);
298 Value *VisitChooseExpr(ChooseExpr *CE);
Nate Begemane2ce1d92008-01-17 17:46:27 +0000299 Value *VisitOverloadExpr(OverloadExpr *OE);
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000300 Value *VisitVAArgExpr(VAArgExpr *VE);
Chris Lattner7f02f722007-08-24 05:35:26 +0000301 Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
302 return CGF.EmitObjCStringLiteral(E);
303 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000304 Value *VisitObjCEncodeExpr(const ObjCEncodeExpr *E);
Chris Lattner7f02f722007-08-24 05:35:26 +0000305};
306} // end anonymous namespace.
307
308//===----------------------------------------------------------------------===//
309// Utilities
310//===----------------------------------------------------------------------===//
311
Chris Lattner9abc84e2007-08-26 16:42:57 +0000312/// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner3420d0d2007-08-26 17:25:57 +0000313/// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattner9abc84e2007-08-26 16:42:57 +0000314Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
315 assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs");
316
317 if (SrcType->isRealFloatingType()) {
318 // Compare against 0.0 for fp scalars.
319 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
Chris Lattner9abc84e2007-08-26 16:42:57 +0000320 return Builder.CreateFCmpUNE(Src, Zero, "tobool");
321 }
322
323 assert((SrcType->isIntegerType() || SrcType->isPointerType()) &&
324 "Unknown scalar type to convert");
325
326 // Because of the type rules of C, we often end up computing a logical value,
327 // then zero extending it to int, then wanting it as a logical value again.
328 // Optimize this common case.
329 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) {
330 if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
331 Value *Result = ZI->getOperand(0);
Eli Friedman356916e2008-01-29 18:13:51 +0000332 // If there aren't any more uses, zap the instruction to save space.
333 // Note that there can be more uses, for example if this
334 // is the result of an assignment.
335 if (ZI->use_empty())
336 ZI->eraseFromParent();
Chris Lattner9abc84e2007-08-26 16:42:57 +0000337 return Result;
338 }
339 }
340
341 // Compare against an integer or pointer null.
342 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
343 return Builder.CreateICmpNE(Src, Zero, "tobool");
344}
345
Chris Lattner3707b252007-08-26 06:48:56 +0000346/// EmitScalarConversion - Emit a conversion from the specified type to the
347/// specified destination type, both of which are LLVM scalar types.
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000348Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
349 QualType DstType) {
Chris Lattner3707b252007-08-26 06:48:56 +0000350 SrcType = SrcType.getCanonicalType();
351 DstType = DstType.getCanonicalType();
352 if (SrcType == DstType) return Src;
Chris Lattnercf289082007-08-26 07:21:11 +0000353
354 if (DstType->isVoidType()) return 0;
Chris Lattner3707b252007-08-26 06:48:56 +0000355
356 // Handle conversions to bool first, they are special: comparisons against 0.
Chris Lattnered70f0a2007-08-26 16:52:28 +0000357 if (DstType->isBooleanType())
358 return EmitConversionToBool(Src, SrcType);
Chris Lattner3707b252007-08-26 06:48:56 +0000359
360 const llvm::Type *DstTy = ConvertType(DstType);
361
362 // Ignore conversions like int -> uint.
363 if (Src->getType() == DstTy)
364 return Src;
365
366 // Handle pointer conversions next: pointers can only be converted to/from
367 // other pointers and integers.
368 if (isa<PointerType>(DstType)) {
369 // The source value may be an integer, or a pointer.
370 if (isa<llvm::PointerType>(Src->getType()))
371 return Builder.CreateBitCast(Src, DstTy, "conv");
372 assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
373 return Builder.CreateIntToPtr(Src, DstTy, "conv");
374 }
375
376 if (isa<PointerType>(SrcType)) {
377 // Must be an ptr to int cast.
378 assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
Anders Carlsson50b5a302007-10-31 23:18:02 +0000379 return Builder.CreatePtrToInt(Src, DstTy, "conv");
Chris Lattner3707b252007-08-26 06:48:56 +0000380 }
381
Nate Begeman213541a2008-04-18 23:10:10 +0000382 // A scalar can be splatted to an extended vector of the same element type
383 if (DstType->isExtVectorType() && !isa<VectorType>(SrcType) &&
Chris Lattner3b1ae002008-02-02 04:51:41 +0000384 cast<llvm::VectorType>(DstTy)->getElementType() == Src->getType())
Nate Begeman4119d1a2007-12-30 02:59:45 +0000385 return CGF.EmitVector(&Src, DstType->getAsVectorType()->getNumElements(),
386 true);
Nate Begeman4119d1a2007-12-30 02:59:45 +0000387
Chris Lattner3b1ae002008-02-02 04:51:41 +0000388 // Allow bitcast from vector to integer/fp of the same size.
Anders Carlsson7019a9e2007-12-05 07:36:10 +0000389 if (isa<llvm::VectorType>(Src->getType()) ||
Chris Lattner3b1ae002008-02-02 04:51:41 +0000390 isa<llvm::VectorType>(DstTy))
Anders Carlsson7019a9e2007-12-05 07:36:10 +0000391 return Builder.CreateBitCast(Src, DstTy, "conv");
Anders Carlsson7019a9e2007-12-05 07:36:10 +0000392
Chris Lattner3707b252007-08-26 06:48:56 +0000393 // Finally, we have the arithmetic types: real int/float.
394 if (isa<llvm::IntegerType>(Src->getType())) {
395 bool InputSigned = SrcType->isSignedIntegerType();
Anders Carlssonb5ce0972007-12-26 18:20:19 +0000396 if (isa<llvm::IntegerType>(DstTy))
397 return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
398 else if (InputSigned)
399 return Builder.CreateSIToFP(Src, DstTy, "conv");
400 else
401 return Builder.CreateUIToFP(Src, DstTy, "conv");
Chris Lattner3707b252007-08-26 06:48:56 +0000402 }
403
404 assert(Src->getType()->isFloatingPoint() && "Unknown real conversion");
405 if (isa<llvm::IntegerType>(DstTy)) {
Anders Carlssonb5ce0972007-12-26 18:20:19 +0000406 if (DstType->isSignedIntegerType())
407 return Builder.CreateFPToSI(Src, DstTy, "conv");
408 else
409 return Builder.CreateFPToUI(Src, DstTy, "conv");
Chris Lattner3707b252007-08-26 06:48:56 +0000410 }
411
412 assert(DstTy->isFloatingPoint() && "Unknown real conversion");
Anders Carlssonb5ce0972007-12-26 18:20:19 +0000413 if (DstTy->getTypeID() < Src->getType()->getTypeID())
414 return Builder.CreateFPTrunc(Src, DstTy, "conv");
415 else
416 return Builder.CreateFPExt(Src, DstTy, "conv");
Chris Lattner3707b252007-08-26 06:48:56 +0000417}
418
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000419/// EmitComplexToScalarConversion - Emit a conversion from the specified
420/// complex type to the specified destination type, where the destination
421/// type is an LLVM scalar type.
422Value *ScalarExprEmitter::
423EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
424 QualType SrcTy, QualType DstTy) {
Chris Lattnered70f0a2007-08-26 16:52:28 +0000425 // Get the source element type.
426 SrcTy = cast<ComplexType>(SrcTy.getCanonicalType())->getElementType();
427
428 // Handle conversions to bool first, they are special: comparisons against 0.
429 if (DstTy->isBooleanType()) {
430 // Complex != 0 -> (Real != 0) | (Imag != 0)
431 Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy);
432 Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy);
433 return Builder.CreateOr(Src.first, Src.second, "tobool");
434 }
435
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000436 // C99 6.3.1.7p2: "When a value of complex type is converted to a real type,
437 // the imaginary part of the complex value is discarded and the value of the
438 // real part is converted according to the conversion rules for the
439 // corresponding real type.
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000440 return EmitScalarConversion(Src.first, SrcTy, DstTy);
441}
442
443
Chris Lattner7f02f722007-08-24 05:35:26 +0000444//===----------------------------------------------------------------------===//
445// Visitor Methods
446//===----------------------------------------------------------------------===//
447
448Value *ScalarExprEmitter::VisitExpr(Expr *E) {
Chris Lattnerdc4d2802007-12-02 01:49:16 +0000449 CGF.WarnUnsupported(E, "scalar expression");
Chris Lattner7f02f722007-08-24 05:35:26 +0000450 if (E->getType()->isVoidType())
451 return 0;
452 return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
453}
454
Chris Lattner2b94fe32008-03-01 08:45:05 +0000455Value *ScalarExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
456 // Only the lookup mechanism and first two arguments of the method
457 // implementation vary between runtimes. We can get the receiver and
458 // arguments in generic code.
459
460 // Find the receiver
Chris Lattnerce5605e2008-03-30 23:25:33 +0000461 llvm::Value *Receiver = CGF.EmitScalarExpr(E->getReceiver());
Chris Lattner2b94fe32008-03-01 08:45:05 +0000462
463 // Process the arguments
Chris Lattnerce5605e2008-03-30 23:25:33 +0000464 unsigned ArgC = E->getNumArgs();
Chris Lattner2b94fe32008-03-01 08:45:05 +0000465 llvm::SmallVector<llvm::Value*, 16> Args;
Chris Lattnerce5605e2008-03-30 23:25:33 +0000466 for (unsigned i = 0; i != ArgC; ++i) {
Chris Lattner2b94fe32008-03-01 08:45:05 +0000467 Expr *ArgExpr = E->getArg(i);
468 QualType ArgTy = ArgExpr->getType();
469 if (!CGF.hasAggregateLLVMType(ArgTy)) {
470 // Scalar argument is passed by-value.
471 Args.push_back(CGF.EmitScalarExpr(ArgExpr));
Chris Lattner9b2dc282008-04-04 16:54:41 +0000472 } else if (ArgTy->isAnyComplexType()) {
Chris Lattner2b94fe32008-03-01 08:45:05 +0000473 // Make a temporary alloca to pass the argument.
474 llvm::Value *DestMem = CGF.CreateTempAlloca(ConvertType(ArgTy));
475 CGF.EmitComplexExprIntoAddr(ArgExpr, DestMem, false);
476 Args.push_back(DestMem);
477 } else {
478 llvm::Value *DestMem = CGF.CreateTempAlloca(ConvertType(ArgTy));
479 CGF.EmitAggExpr(ArgExpr, DestMem, false);
480 Args.push_back(DestMem);
481 }
482 }
483
484 // Get the selector string
485 std::string SelStr = E->getSelector().getName();
486 llvm::Constant *Selector = CGF.CGM.GetAddrOfConstantString(SelStr);
Chris Lattner391d77a2008-03-30 23:03:07 +0000487
488 llvm::Value *SelPtr = Builder.CreateStructGEP(Selector, 0);
Chris Lattnerce5605e2008-03-30 23:25:33 +0000489 return Runtime->generateMessageSend(Builder, ConvertType(E->getType()),
Chris Lattnerc8aa5f12008-04-04 04:07:35 +0000490 CGF.LoadObjCSelf(),
Chris Lattnerce5605e2008-03-30 23:25:33 +0000491 Receiver, SelPtr,
492 &Args[0], Args.size());
Chris Lattner2b94fe32008-03-01 08:45:05 +0000493}
494
Chris Lattner7f02f722007-08-24 05:35:26 +0000495Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
496 // Emit subscript expressions in rvalue context's. For most cases, this just
497 // loads the lvalue formed by the subscript expr. However, we have to be
498 // careful, because the base of a vector subscript is occasionally an rvalue,
499 // so we can't get it as an lvalue.
500 if (!E->getBase()->getType()->isVectorType())
501 return EmitLoadOfLValue(E);
502
503 // Handle the vector case. The base must be a vector, the index must be an
504 // integer value.
505 Value *Base = Visit(E->getBase());
506 Value *Idx = Visit(E->getIdx());
507
508 // FIXME: Convert Idx to i32 type.
509 return Builder.CreateExtractElement(Base, Idx, "vecext");
510}
511
512/// VisitImplicitCastExpr - Implicit casts are the same as normal casts, but
513/// also handle things like function to pointer-to-function decay, and array to
514/// pointer decay.
515Value *ScalarExprEmitter::VisitImplicitCastExpr(const ImplicitCastExpr *E) {
516 const Expr *Op = E->getSubExpr();
517
518 // If this is due to array->pointer conversion, emit the array expression as
519 // an l-value.
520 if (Op->getType()->isArrayType()) {
521 // FIXME: For now we assume that all source arrays map to LLVM arrays. This
522 // will not true when we add support for VLAs.
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000523 Value *V = EmitLValue(Op).getAddress(); // Bitfields can't be arrays.
Chris Lattner7f02f722007-08-24 05:35:26 +0000524
525 assert(isa<llvm::PointerType>(V->getType()) &&
526 isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
527 ->getElementType()) &&
528 "Doesn't support VLAs yet!");
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000529 V = Builder.CreateStructGEP(V, 0, "arraydecay");
Chris Lattnera9e63722007-12-12 04:13:20 +0000530
531 // The resultant pointer type can be implicitly casted to other pointer
532 // types as well, for example void*.
533 const llvm::Type *DestPTy = ConvertType(E->getType());
534 assert(isa<llvm::PointerType>(DestPTy) &&
535 "Only expect implicit cast to pointer");
536 if (V->getType() != DestPTy)
537 V = Builder.CreateBitCast(V, DestPTy, "ptrconv");
538 return V;
539
Anders Carlsson793680e2007-10-12 23:56:29 +0000540 } else if (E->getType()->isReferenceType()) {
Anders Carlsson23af9f22007-10-13 05:52:34 +0000541 assert(cast<ReferenceType>(E->getType().getCanonicalType())->
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000542 getPointeeType() ==
Anders Carlsson23af9f22007-10-13 05:52:34 +0000543 Op->getType().getCanonicalType() && "Incompatible types!");
Anders Carlsson793680e2007-10-12 23:56:29 +0000544
545 return EmitLValue(Op).getAddress();
Chris Lattner7f02f722007-08-24 05:35:26 +0000546 }
547
548 return EmitCastExpr(Op, E->getType());
549}
550
551
552// VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts
553// have to handle a more broad range of conversions than explicit casts, as they
554// handle things like function to ptr-to-function decay etc.
555Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy) {
Chris Lattner58a2e942007-08-26 07:26:12 +0000556 // Handle cases where the source is an non-complex type.
Chris Lattner19a1d7c2008-02-16 23:55:16 +0000557
558 if (!CGF.hasAggregateLLVMType(E->getType())) {
Chris Lattner3707b252007-08-26 06:48:56 +0000559 Value *Src = Visit(const_cast<Expr*>(E));
560
Chris Lattner3707b252007-08-26 06:48:56 +0000561 // Use EmitScalarConversion to perform the conversion.
562 return EmitScalarConversion(Src, E->getType(), DestTy);
563 }
Chris Lattner19a1d7c2008-02-16 23:55:16 +0000564
Chris Lattner9b2dc282008-04-04 16:54:41 +0000565 if (E->getType()->isAnyComplexType()) {
Chris Lattner19a1d7c2008-02-16 23:55:16 +0000566 // Handle cases where the source is a complex type.
567 return EmitComplexToScalarConversion(CGF.EmitComplexExpr(E), E->getType(),
568 DestTy);
569 }
Chris Lattner10b00cf2007-08-26 07:16:41 +0000570
Chris Lattner19a1d7c2008-02-16 23:55:16 +0000571 // Okay, this is a cast from an aggregate. It must be a cast to void. Just
572 // evaluate the result and return.
573 CGF.EmitAggExpr(E, 0, false);
574 return 0;
Chris Lattner7f02f722007-08-24 05:35:26 +0000575}
576
Chris Lattner33793202007-08-31 22:09:40 +0000577Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
Chris Lattner9b655512007-08-31 22:49:20 +0000578 return CGF.EmitCompoundStmt(*E->getSubStmt(), true).getScalarVal();
Chris Lattner33793202007-08-31 22:09:40 +0000579}
580
581
Chris Lattner7f02f722007-08-24 05:35:26 +0000582//===----------------------------------------------------------------------===//
583// Unary Operators
584//===----------------------------------------------------------------------===//
585
586Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
Chris Lattnerdfce2a52007-08-24 16:24:49 +0000587 bool isInc, bool isPre) {
Chris Lattner7f02f722007-08-24 05:35:26 +0000588 LValue LV = EmitLValue(E->getSubExpr());
589 // FIXME: Handle volatile!
Chris Lattnere936cc82007-08-26 05:10:16 +0000590 Value *InVal = CGF.EmitLoadOfLValue(LV, // false
Chris Lattner9b655512007-08-31 22:49:20 +0000591 E->getSubExpr()->getType()).getScalarVal();
Chris Lattner7f02f722007-08-24 05:35:26 +0000592
593 int AmountVal = isInc ? 1 : -1;
594
595 Value *NextVal;
Chris Lattnere936cc82007-08-26 05:10:16 +0000596 if (isa<llvm::PointerType>(InVal->getType())) {
597 // FIXME: This isn't right for VLAs.
598 NextVal = llvm::ConstantInt::get(llvm::Type::Int32Ty, AmountVal);
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000599 NextVal = Builder.CreateGEP(InVal, NextVal, "ptrincdec");
Chris Lattnere936cc82007-08-26 05:10:16 +0000600 } else {
601 // Add the inc/dec to the real part.
602 if (isa<llvm::IntegerType>(InVal->getType()))
603 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
Chris Lattnerca2617c2007-09-13 06:19:18 +0000604 else if (InVal->getType() == llvm::Type::FloatTy)
Devang Patele9b8c0a2007-10-30 20:59:40 +0000605 NextVal =
Chris Lattner59138ba2008-04-20 00:45:53 +0000606 llvm::ConstantFP::get(llvm::APFloat(static_cast<float>(AmountVal)));
Chris Lattner25ddea72008-04-20 00:50:39 +0000607 else if (InVal->getType() == llvm::Type::DoubleTy)
Devang Patele9b8c0a2007-10-30 20:59:40 +0000608 NextVal =
Chris Lattner59138ba2008-04-20 00:45:53 +0000609 llvm::ConstantFP::get(llvm::APFloat(static_cast<double>(AmountVal)));
Chris Lattner25ddea72008-04-20 00:50:39 +0000610 else {
611 llvm::APFloat F(static_cast<float>(AmountVal));
612 F.convert(*CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero);
613 NextVal = llvm::ConstantFP::get(F);
Chris Lattnerca2617c2007-09-13 06:19:18 +0000614 }
Chris Lattnere936cc82007-08-26 05:10:16 +0000615 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
616 }
Chris Lattner7f02f722007-08-24 05:35:26 +0000617
618 // Store the updated result through the lvalue.
619 CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV,
620 E->getSubExpr()->getType());
621
622 // If this is a postinc, return the value read from memory, otherwise use the
623 // updated value.
624 return isPre ? NextVal : InVal;
625}
626
627
628Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
629 Value *Op = Visit(E->getSubExpr());
630 return Builder.CreateNeg(Op, "neg");
631}
632
633Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
634 Value *Op = Visit(E->getSubExpr());
635 return Builder.CreateNot(Op, "neg");
636}
637
638Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
639 // Compare operand to zero.
640 Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
641
642 // Invert value.
643 // TODO: Could dynamically modify easy computations here. For example, if
644 // the operand is an icmp ne, turn into icmp eq.
645 BoolVal = Builder.CreateNot(BoolVal, "lnot");
646
647 // ZExt result to int.
648 return Builder.CreateZExt(BoolVal, CGF.LLVMIntTy, "lnot.ext");
649}
650
651/// EmitSizeAlignOf - Return the size or alignment of the 'TypeToSize' type as
652/// an integer (RetType).
653Value *ScalarExprEmitter::EmitSizeAlignOf(QualType TypeToSize,
Chris Lattner46f93d02007-08-24 21:20:17 +0000654 QualType RetType,bool isSizeOf){
Chris Lattnera269ebf2008-02-21 05:45:29 +0000655 assert(RetType->isIntegerType() && "Result type must be an integer!");
656 uint32_t ResultWidth =
Chris Lattner98be4942008-03-05 18:54:05 +0000657 static_cast<uint32_t>(CGF.getContext().getTypeSize(RetType));
Chris Lattnera269ebf2008-02-21 05:45:29 +0000658
659 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
660 if (TypeToSize->isVoidType())
661 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, 1));
662
Chris Lattner7f02f722007-08-24 05:35:26 +0000663 /// FIXME: This doesn't handle VLAs yet!
Chris Lattner98be4942008-03-05 18:54:05 +0000664 std::pair<uint64_t, unsigned> Info = CGF.getContext().getTypeInfo(TypeToSize);
Chris Lattner7f02f722007-08-24 05:35:26 +0000665
666 uint64_t Val = isSizeOf ? Info.first : Info.second;
667 Val /= 8; // Return size in bytes, not bits.
668
Chris Lattner7f02f722007-08-24 05:35:26 +0000669 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
670}
671
Chris Lattner46f93d02007-08-24 21:20:17 +0000672Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) {
673 Expr *Op = E->getSubExpr();
Chris Lattner9b2dc282008-04-04 16:54:41 +0000674 if (Op->getType()->isAnyComplexType())
Chris Lattner46f93d02007-08-24 21:20:17 +0000675 return CGF.EmitComplexExpr(Op).first;
676 return Visit(Op);
677}
678Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
679 Expr *Op = E->getSubExpr();
Chris Lattner9b2dc282008-04-04 16:54:41 +0000680 if (Op->getType()->isAnyComplexType())
Chris Lattner46f93d02007-08-24 21:20:17 +0000681 return CGF.EmitComplexExpr(Op).second;
Chris Lattner36f84062007-08-26 05:29:21 +0000682
683 // __imag on a scalar returns zero. Emit it the subexpr to ensure side
684 // effects are evaluated.
685 CGF.EmitScalarExpr(Op);
686 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Chris Lattner46f93d02007-08-24 21:20:17 +0000687}
688
Anders Carlsson5a1deb82008-01-29 15:56:48 +0000689Value *ScalarExprEmitter::VisitUnaryOffsetOf(const UnaryOperator *E)
690{
691 int64_t Val = E->evaluateOffsetOf(CGF.getContext());
692
693 assert(E->getType()->isIntegerType() && "Result type must be an integer!");
694
Chris Lattner98be4942008-03-05 18:54:05 +0000695 uint32_t ResultWidth =
696 static_cast<uint32_t>(CGF.getContext().getTypeSize(E->getType()));
Anders Carlsson5a1deb82008-01-29 15:56:48 +0000697 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
698}
Chris Lattner46f93d02007-08-24 21:20:17 +0000699
Chris Lattner7f02f722007-08-24 05:35:26 +0000700//===----------------------------------------------------------------------===//
701// Binary Operators
702//===----------------------------------------------------------------------===//
703
704BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) {
705 BinOpInfo Result;
706 Result.LHS = Visit(E->getLHS());
707 Result.RHS = Visit(E->getRHS());
Chris Lattner1f1ded92007-08-24 21:00:35 +0000708 Result.Ty = E->getType();
Chris Lattner7f02f722007-08-24 05:35:26 +0000709 Result.E = E;
710 return Result;
711}
712
Chris Lattner3ccf7742007-08-26 21:41:21 +0000713Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner1f1ded92007-08-24 21:00:35 +0000714 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
715 QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType();
716
717 BinOpInfo OpInfo;
718
719 // Load the LHS and RHS operands.
720 LValue LHSLV = EmitLValue(E->getLHS());
721 OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy);
Chris Lattner04dc7642007-08-26 22:37:40 +0000722
723 // Determine the computation type. If the RHS is complex, then this is one of
724 // the add/sub/mul/div operators. All of these operators can be computed in
725 // with just their real component even though the computation domain really is
726 // complex.
Chris Lattner3ccf7742007-08-26 21:41:21 +0000727 QualType ComputeType = E->getComputationType();
Chris Lattner1f1ded92007-08-24 21:00:35 +0000728
Chris Lattner04dc7642007-08-26 22:37:40 +0000729 // If the computation type is complex, then the RHS is complex. Emit the RHS.
730 if (const ComplexType *CT = ComputeType->getAsComplexType()) {
731 ComputeType = CT->getElementType();
732
733 // Emit the RHS, only keeping the real component.
734 OpInfo.RHS = CGF.EmitComplexExpr(E->getRHS()).first;
735 RHSTy = RHSTy->getAsComplexType()->getElementType();
736 } else {
737 // Otherwise the RHS is a simple scalar value.
738 OpInfo.RHS = Visit(E->getRHS());
739 }
740
741 // Convert the LHS/RHS values to the computation type.
Chris Lattnere9377122007-08-26 07:08:39 +0000742 OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, ComputeType);
Chris Lattner1f1ded92007-08-24 21:00:35 +0000743
Devang Patelf86206f2007-10-25 22:19:13 +0000744 // Do not merge types for -= or += where the LHS is a pointer.
745 if (!(E->getOpcode() == BinaryOperator::SubAssign ||
Devang Patel03f7c032007-10-30 18:31:12 +0000746 E->getOpcode() == BinaryOperator::AddAssign) ||
Chris Lattner3b44b572007-08-25 21:56:20 +0000747 !E->getLHS()->getType()->isPointerType()) {
Chris Lattnere9377122007-08-26 07:08:39 +0000748 OpInfo.RHS = EmitScalarConversion(OpInfo.RHS, RHSTy, ComputeType);
Chris Lattner1f1ded92007-08-24 21:00:35 +0000749 }
750 OpInfo.Ty = ComputeType;
751 OpInfo.E = E;
752
753 // Expand the binary operator.
754 Value *Result = (this->*Func)(OpInfo);
755
756 // Truncate the result back to the LHS type.
Chris Lattnere9377122007-08-26 07:08:39 +0000757 Result = EmitScalarConversion(Result, ComputeType, LHSTy);
Chris Lattner1f1ded92007-08-24 21:00:35 +0000758
759 // Store the result value into the LHS lvalue.
760 CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, E->getType());
761
762 return Result;
763}
764
765
Chris Lattner7f02f722007-08-24 05:35:26 +0000766Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
Nate Begemanb3ab8dc2007-12-30 01:28:16 +0000767 if (Ops.LHS->getType()->isFPOrFPVector())
Chris Lattner7f02f722007-08-24 05:35:26 +0000768 return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
Chris Lattner1f1ded92007-08-24 21:00:35 +0000769 else if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner7f02f722007-08-24 05:35:26 +0000770 return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div");
771 else
772 return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div");
773}
774
775Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
776 // Rem in C can't be a floating point type: C99 6.5.5p2.
Chris Lattner1f1ded92007-08-24 21:00:35 +0000777 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner7f02f722007-08-24 05:35:26 +0000778 return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
779 else
780 return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
781}
782
783
784Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
Chris Lattner1f1ded92007-08-24 21:00:35 +0000785 if (!Ops.Ty->isPointerType())
Chris Lattner7f02f722007-08-24 05:35:26 +0000786 return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
Chris Lattner1f1ded92007-08-24 21:00:35 +0000787
788 // FIXME: What about a pointer to a VLA?
Chris Lattner8f925282008-01-03 06:36:51 +0000789 Value *Ptr, *Idx;
790 Expr *IdxExp;
791 if (isa<llvm::PointerType>(Ops.LHS->getType())) { // pointer + int
792 Ptr = Ops.LHS;
793 Idx = Ops.RHS;
794 IdxExp = Ops.E->getRHS();
795 } else { // int + pointer
796 Ptr = Ops.RHS;
797 Idx = Ops.LHS;
798 IdxExp = Ops.E->getLHS();
799 }
800
801 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
802 if (Width < CGF.LLVMPointerWidth) {
803 // Zero or sign extend the pointer value based on whether the index is
804 // signed or not.
805 const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth);
806 if (IdxExp->getType().getCanonicalType()->isSignedIntegerType())
807 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
808 else
809 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
810 }
811
812 return Builder.CreateGEP(Ptr, Idx, "add.ptr");
Chris Lattner7f02f722007-08-24 05:35:26 +0000813}
814
815Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
816 if (!isa<llvm::PointerType>(Ops.LHS->getType()))
817 return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
818
Chris Lattner1f1ded92007-08-24 21:00:35 +0000819 // pointer - int
820 assert(!isa<llvm::PointerType>(Ops.RHS->getType()) &&
821 "ptr-ptr shouldn't get here");
822 // FIXME: The pointer could point to a VLA.
Chris Lattner6860f3c2008-01-31 04:12:50 +0000823 Value *Idx = Builder.CreateNeg(Ops.RHS, "sub.ptr.neg");
824
825 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
826 if (Width < CGF.LLVMPointerWidth) {
827 // Zero or sign extend the pointer value based on whether the index is
828 // signed or not.
829 const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth);
830 if (Ops.E->getRHS()->getType().getCanonicalType()->isSignedIntegerType())
831 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
832 else
833 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
834 }
835
836 return Builder.CreateGEP(Ops.LHS, Idx, "sub.ptr");
Chris Lattner1f1ded92007-08-24 21:00:35 +0000837}
838
839Value *ScalarExprEmitter::VisitBinSub(const BinaryOperator *E) {
840 // "X - Y" is different from "X -= Y" in one case: when Y is a pointer. In
841 // the compound assignment case it is invalid, so just handle it here.
842 if (!E->getRHS()->getType()->isPointerType())
843 return EmitSub(EmitBinOps(E));
Chris Lattner7f02f722007-08-24 05:35:26 +0000844
845 // pointer - pointer
Chris Lattner1f1ded92007-08-24 21:00:35 +0000846 Value *LHS = Visit(E->getLHS());
847 Value *RHS = Visit(E->getRHS());
848
Seo Sanghyeonec86b972007-12-03 06:23:43 +0000849 const QualType LHSType = E->getLHS()->getType().getCanonicalType();
Seo Sanghyeon9bb947a2007-12-26 05:21:37 +0000850 const QualType LHSElementType = cast<PointerType>(LHSType)->getPointeeType();
Chris Lattner98be4942008-03-05 18:54:05 +0000851 uint64_t ElementSize = CGF.getContext().getTypeSize(LHSElementType) / 8;
Chris Lattner1f1ded92007-08-24 21:00:35 +0000852
853 const llvm::Type *ResultType = ConvertType(E->getType());
854 LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast");
855 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
856 Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
Chris Lattner7f02f722007-08-24 05:35:26 +0000857
858 // HACK: LLVM doesn't have an divide instruction that 'knows' there is no
859 // remainder. As such, we handle common power-of-two cases here to generate
860 // better code.
861 if (llvm::isPowerOf2_64(ElementSize)) {
862 Value *ShAmt =
863 llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize));
864 return Builder.CreateAShr(BytesBetween, ShAmt, "sub.ptr.shr");
865 }
Chris Lattner1f1ded92007-08-24 21:00:35 +0000866
Chris Lattner7f02f722007-08-24 05:35:26 +0000867 // Otherwise, do a full sdiv.
868 Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize);
869 return Builder.CreateSDiv(BytesBetween, BytesPerElt, "sub.ptr.div");
870}
871
Chris Lattner1f1ded92007-08-24 21:00:35 +0000872
Chris Lattner7f02f722007-08-24 05:35:26 +0000873Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
874 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
875 // RHS to the same size as the LHS.
876 Value *RHS = Ops.RHS;
877 if (Ops.LHS->getType() != RHS->getType())
878 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
879
880 return Builder.CreateShl(Ops.LHS, RHS, "shl");
881}
882
883Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
884 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
885 // RHS to the same size as the LHS.
886 Value *RHS = Ops.RHS;
887 if (Ops.LHS->getType() != RHS->getType())
888 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
889
Chris Lattner1f1ded92007-08-24 21:00:35 +0000890 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner7f02f722007-08-24 05:35:26 +0000891 return Builder.CreateLShr(Ops.LHS, RHS, "shr");
892 return Builder.CreateAShr(Ops.LHS, RHS, "shr");
893}
894
895Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
896 unsigned SICmpOpc, unsigned FCmpOpc) {
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000897 Value *Result;
Chris Lattner7f02f722007-08-24 05:35:26 +0000898 QualType LHSTy = E->getLHS()->getType();
Chris Lattner9b2dc282008-04-04 16:54:41 +0000899 if (!LHSTy->isAnyComplexType()) {
Chris Lattner7f02f722007-08-24 05:35:26 +0000900 Value *LHS = Visit(E->getLHS());
901 Value *RHS = Visit(E->getRHS());
902
903 if (LHS->getType()->isFloatingPoint()) {
904 Result = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
905 LHS, RHS, "cmp");
906 } else if (LHSTy->isUnsignedIntegerType()) {
907 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
908 LHS, RHS, "cmp");
909 } else {
910 // Signed integers and pointers.
911 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
912 LHS, RHS, "cmp");
913 }
914 } else {
915 // Complex Comparison: can only be an equality comparison.
916 CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS());
917 CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS());
918
919 QualType CETy =
920 cast<ComplexType>(LHSTy.getCanonicalType())->getElementType();
921
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000922 Value *ResultR, *ResultI;
Chris Lattner7f02f722007-08-24 05:35:26 +0000923 if (CETy->isRealFloatingType()) {
924 ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
925 LHS.first, RHS.first, "cmp.r");
926 ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
927 LHS.second, RHS.second, "cmp.i");
928 } else {
929 // Complex comparisons can only be equality comparisons. As such, signed
930 // and unsigned opcodes are the same.
931 ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
932 LHS.first, RHS.first, "cmp.r");
933 ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
934 LHS.second, RHS.second, "cmp.i");
935 }
936
937 if (E->getOpcode() == BinaryOperator::EQ) {
938 Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
939 } else {
940 assert(E->getOpcode() == BinaryOperator::NE &&
941 "Complex comparison other than == or != ?");
942 Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
943 }
944 }
945
946 // ZExt result to int.
947 return Builder.CreateZExt(Result, CGF.LLVMIntTy, "cmp.ext");
948}
949
950Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
951 LValue LHS = EmitLValue(E->getLHS());
952 Value *RHS = Visit(E->getRHS());
953
954 // Store the value into the LHS.
955 // FIXME: Volatility!
956 CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType());
957
958 // Return the RHS.
959 return RHS;
960}
961
962Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
963 Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
964
Gabor Greif984d0b42008-04-06 20:42:52 +0000965 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("land_cont");
966 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("land_rhs");
Chris Lattner7f02f722007-08-24 05:35:26 +0000967
968 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
969 Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock);
970
971 CGF.EmitBlock(RHSBlock);
972 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
973
974 // Reaquire the RHS block, as there may be subblocks inserted.
975 RHSBlock = Builder.GetInsertBlock();
976 CGF.EmitBlock(ContBlock);
977
978 // Create a PHI node. If we just evaluted the LHS condition, the result is
979 // false. If we evaluated both, the result is the RHS condition.
980 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land");
981 PN->reserveOperandSpace(2);
982 PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock);
983 PN->addIncoming(RHSCond, RHSBlock);
984
985 // ZExt result to int.
986 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext");
987}
988
989Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
990 Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
991
Gabor Greif984d0b42008-04-06 20:42:52 +0000992 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("lor_cont");
993 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("lor_rhs");
Chris Lattner7f02f722007-08-24 05:35:26 +0000994
995 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
996 Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock);
997
998 CGF.EmitBlock(RHSBlock);
999 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
1000
1001 // Reaquire the RHS block, as there may be subblocks inserted.
1002 RHSBlock = Builder.GetInsertBlock();
1003 CGF.EmitBlock(ContBlock);
1004
1005 // Create a PHI node. If we just evaluted the LHS condition, the result is
1006 // true. If we evaluated both, the result is the RHS condition.
1007 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor");
1008 PN->reserveOperandSpace(2);
1009 PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock);
1010 PN->addIncoming(RHSCond, RHSBlock);
1011
1012 // ZExt result to int.
1013 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext");
1014}
1015
1016Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
1017 CGF.EmitStmt(E->getLHS());
1018 return Visit(E->getRHS());
1019}
1020
1021//===----------------------------------------------------------------------===//
1022// Other Operators
1023//===----------------------------------------------------------------------===//
1024
1025Value *ScalarExprEmitter::
1026VisitConditionalOperator(const ConditionalOperator *E) {
Gabor Greif984d0b42008-04-06 20:42:52 +00001027 llvm::BasicBlock *LHSBlock = llvm::BasicBlock::Create("cond.?");
1028 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("cond.:");
1029 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("cond.cont");
Chris Lattner7f02f722007-08-24 05:35:26 +00001030
Chris Lattnera21ddb32007-11-26 01:40:58 +00001031 // Evaluate the conditional, then convert it to bool. We do this explicitly
1032 // because we need the unconverted value if this is a GNU ?: expression with
1033 // missing middle value.
1034 Value *CondVal = CGF.EmitScalarExpr(E->getCond());
Chris Lattnerc89bf692008-01-03 07:05:49 +00001035 Value *CondBoolVal =CGF.EmitScalarConversion(CondVal, E->getCond()->getType(),
1036 CGF.getContext().BoolTy);
Chris Lattnera21ddb32007-11-26 01:40:58 +00001037 Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock);
Chris Lattner7f02f722007-08-24 05:35:26 +00001038
1039 CGF.EmitBlock(LHSBlock);
1040
1041 // Handle the GNU extension for missing LHS.
Chris Lattnera21ddb32007-11-26 01:40:58 +00001042 Value *LHS;
1043 if (E->getLHS())
1044 LHS = Visit(E->getLHS());
1045 else // Perform promotions, to handle cases like "short ?: int"
1046 LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType());
1047
Chris Lattner7f02f722007-08-24 05:35:26 +00001048 Builder.CreateBr(ContBlock);
1049 LHSBlock = Builder.GetInsertBlock();
1050
1051 CGF.EmitBlock(RHSBlock);
1052
1053 Value *RHS = Visit(E->getRHS());
1054 Builder.CreateBr(ContBlock);
1055 RHSBlock = Builder.GetInsertBlock();
1056
1057 CGF.EmitBlock(ContBlock);
1058
Chris Lattner2202bce2007-11-30 17:56:23 +00001059 if (!LHS) {
1060 assert(E->getType()->isVoidType() && "Non-void value should have a value");
1061 return 0;
1062 }
1063
Chris Lattner7f02f722007-08-24 05:35:26 +00001064 // Create a PHI node for the real part.
1065 llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond");
1066 PN->reserveOperandSpace(2);
1067 PN->addIncoming(LHS, LHSBlock);
1068 PN->addIncoming(RHS, RHSBlock);
1069 return PN;
1070}
1071
1072Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
Chris Lattner7f02f722007-08-24 05:35:26 +00001073 // Emit the LHS or RHS as appropriate.
Devang Patele9b8c0a2007-10-30 20:59:40 +00001074 return
1075 Visit(E->isConditionTrue(CGF.getContext()) ? E->getLHS() : E->getRHS());
Chris Lattner7f02f722007-08-24 05:35:26 +00001076}
1077
Nate Begemane2ce1d92008-01-17 17:46:27 +00001078Value *ScalarExprEmitter::VisitOverloadExpr(OverloadExpr *E) {
Nate Begeman67295d02008-01-30 20:50:20 +00001079 return CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
1080 E->getNumArgs(CGF.getContext())).getScalarVal();
Nate Begemane2ce1d92008-01-17 17:46:27 +00001081}
1082
Chris Lattner2202bce2007-11-30 17:56:23 +00001083Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001084 llvm::Value *ArgValue = EmitLValue(VE->getSubExpr()).getAddress();
1085
1086 llvm::Value *V = Builder.CreateVAArg(ArgValue, ConvertType(VE->getType()));
1087 return V;
1088}
1089
Chris Lattner2202bce2007-11-30 17:56:23 +00001090Value *ScalarExprEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001091 std::string str;
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001092 llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes;
1093 CGF.getContext().getObjCEncodingForType(E->getEncodedType(), str,
1094 EncodingRecordTypes);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001095
1096 llvm::Constant *C = llvm::ConstantArray::get(str);
1097 C = new llvm::GlobalVariable(C->getType(), true,
1098 llvm::GlobalValue::InternalLinkage,
1099 C, ".str", &CGF.CGM.getModule());
1100 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
1101 llvm::Constant *Zeros[] = { Zero, Zero };
1102 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
1103
1104 return C;
1105}
1106
Chris Lattner7f02f722007-08-24 05:35:26 +00001107//===----------------------------------------------------------------------===//
1108// Entry Point into this File
1109//===----------------------------------------------------------------------===//
1110
1111/// EmitComplexExpr - Emit the computation of the specified expression of
1112/// complex type, ignoring the result.
1113Value *CodeGenFunction::EmitScalarExpr(const Expr *E) {
1114 assert(E && !hasAggregateLLVMType(E->getType()) &&
1115 "Invalid scalar expression to emit");
1116
1117 return ScalarExprEmitter(*this).Visit(const_cast<Expr*>(E));
1118}
Chris Lattner3707b252007-08-26 06:48:56 +00001119
1120/// EmitScalarConversion - Emit a conversion from the specified type to the
1121/// specified destination type, both of which are LLVM scalar types.
Chris Lattner4f1a7b32007-08-26 16:34:22 +00001122Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy,
1123 QualType DstTy) {
Chris Lattner3707b252007-08-26 06:48:56 +00001124 assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) &&
1125 "Invalid scalar expression to emit");
1126 return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy);
1127}
Chris Lattner4f1a7b32007-08-26 16:34:22 +00001128
1129/// EmitComplexToScalarConversion - Emit a conversion from the specified
1130/// complex type to the specified destination type, where the destination
1131/// type is an LLVM scalar type.
1132Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src,
1133 QualType SrcTy,
1134 QualType DstTy) {
Chris Lattner9b2dc282008-04-04 16:54:41 +00001135 assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) &&
Chris Lattner4f1a7b32007-08-26 16:34:22 +00001136 "Invalid complex -> scalar conversion");
1137 return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy,
1138 DstTy);
1139}
Anders Carlssoncc23aca2007-12-10 19:35:18 +00001140
1141Value *CodeGenFunction::EmitShuffleVector(Value* V1, Value *V2, ...) {
1142 assert(V1->getType() == V2->getType() &&
1143 "Vector operands must be of the same type");
1144
1145 unsigned NumElements =
1146 cast<llvm::VectorType>(V1->getType())->getNumElements();
1147
1148 va_list va;
1149 va_start(va, V2);
1150
1151 llvm::SmallVector<llvm::Constant*, 16> Args;
1152
1153 for (unsigned i = 0; i < NumElements; i++) {
1154 int n = va_arg(va, int);
1155
1156 assert(n >= 0 && n < (int)NumElements * 2 &&
1157 "Vector shuffle index out of bounds!");
1158
1159 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, n));
1160 }
1161
1162 const char *Name = va_arg(va, const char *);
1163 va_end(va);
1164
1165 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
1166
1167 return Builder.CreateShuffleVector(V1, V2, Mask, Name);
1168}
1169
Anders Carlsson6086bbd2007-12-15 21:23:30 +00001170llvm::Value *CodeGenFunction::EmitVector(llvm::Value * const *Vals,
Nate Begeman4119d1a2007-12-30 02:59:45 +00001171 unsigned NumVals, bool isSplat)
Anders Carlsson6086bbd2007-12-15 21:23:30 +00001172{
Anders Carlsson6086bbd2007-12-15 21:23:30 +00001173 llvm::Value *Vec
1174 = llvm::UndefValue::get(llvm::VectorType::get(Vals[0]->getType(), NumVals));
1175
1176 for (unsigned i = 0, e = NumVals ; i != e; ++i) {
Nate Begeman4119d1a2007-12-30 02:59:45 +00001177 llvm::Value *Val = isSplat ? Vals[0] : Vals[i];
Anders Carlsson6086bbd2007-12-15 21:23:30 +00001178 llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
Nate Begeman4119d1a2007-12-30 02:59:45 +00001179 Vec = Builder.CreateInsertElement(Vec, Val, Idx, "tmp");
Anders Carlsson6086bbd2007-12-15 21:23:30 +00001180 }
1181
1182 return Vec;
1183}