blob: 06e253e3601bef1e601136dcd0149f7b725a8539 [file] [log] [blame]
Chris Lattnere47e4402007-06-01 18:02:12 +00001//===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Expr nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
Chris Lattnerb6984c42007-06-20 04:44:43 +000015#include "CodeGenModule.h"
Chris Lattnere47e4402007-06-01 18:02:12 +000016#include "clang/AST/AST.h"
17#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
Chris Lattner4347e3692007-06-06 04:54:52 +000019#include "llvm/Function.h"
20#include "llvm/GlobalVariable.h"
Chris Lattnere47e4402007-06-01 18:02:12 +000021using namespace clang;
22using namespace CodeGen;
23
Chris Lattnerd7f58862007-06-02 05:24:33 +000024//===--------------------------------------------------------------------===//
Chris Lattnerf0106d22007-06-02 19:33:17 +000025// Miscellaneous Helper Methods
26//===--------------------------------------------------------------------===//
27
Chris Lattner8394d792007-06-05 20:53:16 +000028
29/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
30/// expression and compare the result against zero, returning an Int1Ty value.
Chris Lattner23b7eb62007-06-15 23:05:46 +000031llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
Chris Lattner8394d792007-06-05 20:53:16 +000032 QualType Ty;
33 RValue Val = EmitExprWithUsualUnaryConversions(E, Ty);
34 return ConvertScalarValueToBool(Val, Ty);
35}
36
37//===--------------------------------------------------------------------===//
38// Conversions
39//===--------------------------------------------------------------------===//
40
41/// EmitConversion - Convert the value specied by Val, whose type is ValTy, to
42/// the type specified by DstTy, following the rules of C99 6.3.
43RValue CodeGenFunction::EmitConversion(RValue Val, QualType ValTy,
Chris Lattnercf106ab2007-06-06 04:05:39 +000044 QualType DstTy, SourceLocation Loc) {
Chris Lattner8394d792007-06-05 20:53:16 +000045 ValTy = ValTy.getCanonicalType();
46 DstTy = DstTy.getCanonicalType();
47 if (ValTy == DstTy) return Val;
Chris Lattner83b484b2007-06-06 04:39:08 +000048
49 // Handle conversions to bool first, they are special: comparisons against 0.
50 if (const BuiltinType *DestBT = dyn_cast<BuiltinType>(DstTy))
51 if (DestBT->getKind() == BuiltinType::Bool)
52 return RValue::get(ConvertScalarValueToBool(Val, ValTy));
Chris Lattner8394d792007-06-05 20:53:16 +000053
Chris Lattner83b484b2007-06-06 04:39:08 +000054 // Handle pointer conversions next: pointers can only be converted to/from
55 // other pointers and integers.
Chris Lattnercf106ab2007-06-06 04:05:39 +000056 if (isa<PointerType>(DstTy)) {
57 const llvm::Type *DestTy = ConvertType(DstTy, Loc);
58
59 // The source value may be an integer, or a pointer.
60 assert(Val.isScalar() && "Can only convert from integer or pointer");
61 if (isa<llvm::PointerType>(Val.getVal()->getType()))
62 return RValue::get(Builder.CreateBitCast(Val.getVal(), DestTy, "conv"));
63 assert(ValTy->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
64 return RValue::get(Builder.CreatePtrToInt(Val.getVal(), DestTy, "conv"));
Chris Lattner83b484b2007-06-06 04:39:08 +000065 }
66
67 if (isa<PointerType>(ValTy)) {
Chris Lattnercf106ab2007-06-06 04:05:39 +000068 // Must be an ptr to int cast.
69 const llvm::Type *DestTy = ConvertType(DstTy, Loc);
70 assert(isa<llvm::IntegerType>(DestTy) && "not ptr->int?");
71 return RValue::get(Builder.CreateIntToPtr(Val.getVal(), DestTy, "conv"));
Chris Lattner8394d792007-06-05 20:53:16 +000072 }
Chris Lattner83b484b2007-06-06 04:39:08 +000073
74 // Finally, we have the arithmetic types: real int/float and complex
75 // int/float. Handle real->real conversions first, they are the most
76 // common.
77 if (Val.isScalar() && DstTy->isRealType()) {
78 // We know that these are representable as scalars in LLVM, convert to LLVM
79 // types since they are easier to reason about.
Chris Lattner23b7eb62007-06-15 23:05:46 +000080 llvm::Value *SrcVal = Val.getVal();
Chris Lattner83b484b2007-06-06 04:39:08 +000081 const llvm::Type *DestTy = ConvertType(DstTy, Loc);
82 if (SrcVal->getType() == DestTy) return Val;
83
Chris Lattner23b7eb62007-06-15 23:05:46 +000084 llvm::Value *Result;
Chris Lattner83b484b2007-06-06 04:39:08 +000085 if (isa<llvm::IntegerType>(SrcVal->getType())) {
86 bool InputSigned = ValTy->isSignedIntegerType();
87 if (isa<llvm::IntegerType>(DestTy))
88 Result = Builder.CreateIntCast(SrcVal, DestTy, InputSigned, "conv");
89 else if (InputSigned)
90 Result = Builder.CreateSIToFP(SrcVal, DestTy, "conv");
91 else
92 Result = Builder.CreateUIToFP(SrcVal, DestTy, "conv");
93 } else {
94 assert(SrcVal->getType()->isFloatingPoint() && "Unknown real conversion");
95 if (isa<llvm::IntegerType>(DestTy)) {
96 if (DstTy->isSignedIntegerType())
97 Result = Builder.CreateFPToSI(SrcVal, DestTy, "conv");
98 else
99 Result = Builder.CreateFPToUI(SrcVal, DestTy, "conv");
100 } else {
101 assert(DestTy->isFloatingPoint() && "Unknown real conversion");
102 if (DestTy->getTypeID() < SrcVal->getType()->getTypeID())
103 Result = Builder.CreateFPTrunc(SrcVal, DestTy, "conv");
104 else
105 Result = Builder.CreateFPExt(SrcVal, DestTy, "conv");
106 }
107 }
108 return RValue::get(Result);
109 }
110
111 assert(0 && "FIXME: We don't support complex conversions yet!");
Chris Lattner8394d792007-06-05 20:53:16 +0000112}
113
114
115/// ConvertScalarValueToBool - Convert the specified expression value to a
Chris Lattnerf0106d22007-06-02 19:33:17 +0000116/// boolean (i1) truth value. This is equivalent to "Val == 0".
Chris Lattner23b7eb62007-06-15 23:05:46 +0000117llvm::Value *CodeGenFunction::ConvertScalarValueToBool(RValue Val, QualType Ty){
Chris Lattnerf0106d22007-06-02 19:33:17 +0000118 Ty = Ty.getCanonicalType();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000119 llvm::Value *Result;
Chris Lattnerf0106d22007-06-02 19:33:17 +0000120 if (const BuiltinType *BT = dyn_cast<BuiltinType>(Ty)) {
121 switch (BT->getKind()) {
122 default: assert(0 && "Unknown scalar value");
123 case BuiltinType::Bool:
124 Result = Val.getVal();
125 // Bool is already evaluated right.
126 assert(Result->getType() == llvm::Type::Int1Ty &&
127 "Unexpected bool value type!");
128 return Result;
Chris Lattnerb16f4552007-06-03 07:25:34 +0000129 case BuiltinType::Char_S:
130 case BuiltinType::Char_U:
Chris Lattnerf0106d22007-06-02 19:33:17 +0000131 case BuiltinType::SChar:
132 case BuiltinType::UChar:
133 case BuiltinType::Short:
134 case BuiltinType::UShort:
135 case BuiltinType::Int:
136 case BuiltinType::UInt:
137 case BuiltinType::Long:
138 case BuiltinType::ULong:
139 case BuiltinType::LongLong:
140 case BuiltinType::ULongLong:
141 // Code below handles simple integers.
142 break;
143 case BuiltinType::Float:
144 case BuiltinType::Double:
145 case BuiltinType::LongDouble: {
146 // Compare against 0.0 for fp scalars.
147 Result = Val.getVal();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000148 llvm::Value *Zero = llvm::Constant::getNullValue(Result->getType());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000149 // FIXME: llvm-gcc produces a une comparison: validate this is right.
150 Result = Builder.CreateFCmpUNE(Result, Zero, "tobool");
151 return Result;
152 }
153
154 case BuiltinType::FloatComplex:
155 case BuiltinType::DoubleComplex:
156 case BuiltinType::LongDoubleComplex:
157 assert(0 && "comparisons against complex not implemented yet");
158 }
159 } else {
160 assert((isa<PointerType>(Ty) ||
161 cast<TagType>(Ty)->getDecl()->getKind() == Decl::Enum) &&
162 "Unknown scalar type");
163 // Code below handles this fine.
164 }
165
166 // Usual case for integers, pointers, and enums: compare against zero.
167 Result = Val.getVal();
Chris Lattnera45c5af2007-06-02 19:47:04 +0000168
169 // Because of the type rules of C, we often end up computing a logical value,
170 // then zero extending it to int, then wanting it as a logical value again.
171 // Optimize this common case.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000172 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Result)) {
Chris Lattnera45c5af2007-06-02 19:47:04 +0000173 if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
174 Result = ZI->getOperand(0);
175 ZI->eraseFromParent();
176 return Result;
177 }
178 }
179
Chris Lattner23b7eb62007-06-15 23:05:46 +0000180 llvm::Value *Zero = llvm::Constant::getNullValue(Result->getType());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000181 return Builder.CreateICmpNE(Result, Zero, "tobool");
182}
183
Chris Lattnera45c5af2007-06-02 19:47:04 +0000184//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000185// LValue Expression Emission
Chris Lattnera45c5af2007-06-02 19:47:04 +0000186//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000187
Chris Lattner8394d792007-06-05 20:53:16 +0000188/// EmitLValue - Emit code to compute a designator that specifies the location
189/// of the expression.
190///
191/// This can return one of two things: a simple address or a bitfield
192/// reference. In either case, the LLVM Value* in the LValue structure is
193/// guaranteed to be an LLVM pointer type.
194///
195/// If this returns a bitfield reference, nothing about the pointee type of
196/// the LLVM value is known: For example, it may not be a pointer to an
197/// integer.
198///
199/// If this returns a normal address, and if the lvalue's C type is fixed
200/// size, this method guarantees that the returned pointer type will point to
201/// an LLVM type of the same size of the lvalue's type. If the lvalue has a
202/// variable length type, this is not possible.
203///
Chris Lattnerd7f58862007-06-02 05:24:33 +0000204LValue CodeGenFunction::EmitLValue(const Expr *E) {
205 switch (E->getStmtClass()) {
206 default:
Chris Lattner8394d792007-06-05 20:53:16 +0000207 fprintf(stderr, "Unimplemented lvalue expr!\n");
Chris Lattnerd7f58862007-06-02 05:24:33 +0000208 E->dump();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000209 return LValue::getAddr(llvm::UndefValue::get(
Chris Lattnerd7f58862007-06-02 05:24:33 +0000210 llvm::PointerType::get(llvm::Type::Int32Ty)));
211
212 case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Chris Lattner946aa312007-06-05 03:59:43 +0000213 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattner4347e3692007-06-06 04:54:52 +0000214 case Expr::StringLiteralClass:
215 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattner8394d792007-06-05 20:53:16 +0000216
217 case Expr::UnaryOperatorClass:
218 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000219 case Expr::ArraySubscriptExprClass:
220 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000221 }
222}
223
Chris Lattner8394d792007-06-05 20:53:16 +0000224/// EmitLoadOfLValue - Given an expression that represents a value lvalue,
225/// this method emits the address of the lvalue, then loads the result as an
226/// rvalue, returning the rvalue.
227RValue CodeGenFunction::EmitLoadOfLValue(const Expr *E) {
228 LValue LV = EmitLValue(E);
229
230 QualType ExprTy = E->getType().getCanonicalType();
231
232 // FIXME: this is silly and obviously wrong for non-scalars.
233 assert(!LV.isBitfield());
234 return RValue::get(Builder.CreateLoad(LV.getAddress(), "tmp"));
235}
236
237/// EmitStoreThroughLValue - Store the specified rvalue into the specified
238/// lvalue, where both are guaranteed to the have the same type, and that type
239/// is 'Ty'.
240void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
241 QualType Ty) {
242 // FIXME: This is obviously bogus.
243 assert(!Dst.isBitfield() && "FIXME: Don't support store to bitfield yet");
244 assert(Src.isScalar() && "FIXME: Don't support store of aggregate yet");
245
246 // TODO: Handle volatility etc.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000247 llvm::Value *Addr = Dst.getAddress();
Chris Lattner8394d792007-06-05 20:53:16 +0000248 const llvm::Type *SrcTy = Src.getVal()->getType();
249 const llvm::Type *AddrTy =
250 cast<llvm::PointerType>(Addr->getType())->getElementType();
251
252 if (AddrTy != SrcTy)
253 Addr = Builder.CreateBitCast(Addr, llvm::PointerType::get(SrcTy),
254 "storetmp");
255 Builder.CreateStore(Src.getVal(), Addr);
256}
257
Chris Lattnerd7f58862007-06-02 05:24:33 +0000258
259LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
260 const Decl *D = E->getDecl();
Chris Lattner53621a52007-06-13 20:44:40 +0000261 if (isa<BlockVarDecl>(D) || isa<ParmVarDecl>(D)) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000262 llvm::Value *V = LocalDeclMap[D];
Chris Lattnerd7f58862007-06-02 05:24:33 +0000263 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
264 return LValue::getAddr(V);
Chris Lattnerb6984c42007-06-20 04:44:43 +0000265 } else if (isa<FunctionDecl>(D) || isa<FileVarDecl>(D)) {
266 return LValue::getAddr(CGM.GetAddrOfGlobalDecl(D));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000267 }
268 assert(0 && "Unimp declref");
269}
Chris Lattnere47e4402007-06-01 18:02:12 +0000270
Chris Lattner8394d792007-06-05 20:53:16 +0000271LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
272 // __extension__ doesn't affect lvalue-ness.
273 if (E->getOpcode() == UnaryOperator::Extension)
274 return EmitLValue(E->getSubExpr());
275
276 assert(E->getOpcode() == UnaryOperator::Deref &&
277 "'*' is the only unary operator that produces an lvalue");
278 return LValue::getAddr(EmitExpr(E->getSubExpr()).getVal());
279}
280
Chris Lattner4347e3692007-06-06 04:54:52 +0000281LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
282 assert(!E->isWide() && "FIXME: Wide strings not supported yet!");
283 const char *StrData = E->getStrData();
284 unsigned Len = E->getByteLength();
285
286 // FIXME: Can cache/reuse these within the module.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000287 llvm::Constant *C=llvm::ConstantArray::get(std::string(StrData, StrData+Len));
Chris Lattner4347e3692007-06-06 04:54:52 +0000288
289 // Create a global variable for this.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000290 C = new llvm::GlobalVariable(C->getType(), true,
291 llvm::GlobalValue::InternalLinkage,
Chris Lattner4347e3692007-06-06 04:54:52 +0000292 C, ".str", CurFn->getParent());
Chris Lattner23b7eb62007-06-15 23:05:46 +0000293 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
294 llvm::Constant *Zeros[] = { Zero, Zero };
295 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
Chris Lattner4347e3692007-06-06 04:54:52 +0000296 return LValue::getAddr(C);
297}
298
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000299LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
300 // The base and index must be pointers or integers, neither of which are
301 // aggregates. Emit them.
302 QualType BaseTy;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000303 llvm::Value *Base =
304 EmitExprWithUsualUnaryConversions(E->getBase(), BaseTy).getVal();
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000305 QualType IdxTy;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000306 llvm::Value *Idx =
307 EmitExprWithUsualUnaryConversions(E->getIdx(), IdxTy).getVal();
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000308
309 // Usually the base is the pointer type, but sometimes it is the index.
310 // Canonicalize to have the pointer as the base.
311 if (isa<llvm::PointerType>(Idx->getType())) {
312 std::swap(Base, Idx);
313 std::swap(BaseTy, IdxTy);
314 }
315
316 // The pointer is now the base. Extend or truncate the index type to 32 or
317 // 64-bits.
318 bool IdxSigned = IdxTy->isSignedIntegerType();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000319 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000320 if (IdxBitwidth != LLVMPointerWidth)
Chris Lattner23b7eb62007-06-15 23:05:46 +0000321 Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth),
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000322 IdxSigned, "idxprom");
323
324 // We know that the pointer points to a type of the correct size, unless the
325 // size is a VLA.
326 if (!E->getType()->isConstantSizeType())
327 assert(0 && "VLA idx not implemented");
328 return LValue::getAddr(Builder.CreateGEP(Base, Idx, "arrayidx"));
329}
330
Chris Lattnere47e4402007-06-01 18:02:12 +0000331//===--------------------------------------------------------------------===//
332// Expression Emission
333//===--------------------------------------------------------------------===//
334
Chris Lattner8394d792007-06-05 20:53:16 +0000335RValue CodeGenFunction::EmitExpr(const Expr *E) {
Chris Lattnere47e4402007-06-01 18:02:12 +0000336 assert(E && "Null expression?");
337
338 switch (E->getStmtClass()) {
339 default:
Chris Lattner1fde0b32007-06-20 18:30:55 +0000340 fprintf(stderr, "Unimplemented expr!\n");
Chris Lattnere47e4402007-06-01 18:02:12 +0000341 E->dump();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000342 return RValue::get(llvm::UndefValue::get(llvm::Type::Int32Ty));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000343
344 // l-values.
Chris Lattner8394d792007-06-05 20:53:16 +0000345 case Expr::DeclRefExprClass:
Chris Lattnerf99b3f52007-06-11 03:52:52 +0000346 // DeclRef's of EnumConstantDecl's are simple rvalues.
347 if (const EnumConstantDecl *EC =
348 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
Chris Lattner23b7eb62007-06-15 23:05:46 +0000349 return RValue::get(llvm::ConstantInt::get(EC->getInitVal()));
Chris Lattnerf99b3f52007-06-11 03:52:52 +0000350
351 // FALLTHROUGH
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000352 case Expr::ArraySubscriptExprClass:
Chris Lattner8394d792007-06-05 20:53:16 +0000353 return EmitLoadOfLValue(E);
Chris Lattner4347e3692007-06-06 04:54:52 +0000354 case Expr::StringLiteralClass:
355 return RValue::get(EmitLValue(E).getAddress());
Chris Lattnerd7f58862007-06-02 05:24:33 +0000356
357 // Leaf expressions.
358 case Expr::IntegerLiteralClass:
Chris Lattnere47e4402007-06-01 18:02:12 +0000359 return EmitIntegerLiteral(cast<IntegerLiteral>(E));
Chris Lattnerdb91b162007-06-02 00:16:28 +0000360
Chris Lattnerd7f58862007-06-02 05:24:33 +0000361 // Operators.
362 case Expr::ParenExprClass:
363 return EmitExpr(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000364 case Expr::UnaryOperatorClass:
365 return EmitUnaryOperator(cast<UnaryOperator>(E));
Chris Lattner8394d792007-06-05 20:53:16 +0000366 case Expr::CastExprClass:
367 return EmitCastExpr(cast<CastExpr>(E));
Chris Lattner2b228c92007-06-15 21:34:29 +0000368 case Expr::CallExprClass:
369 return EmitCallExpr(cast<CallExpr>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000370 case Expr::BinaryOperatorClass:
Chris Lattnerdb91b162007-06-02 00:16:28 +0000371 return EmitBinaryOperator(cast<BinaryOperator>(E));
Chris Lattnere47e4402007-06-01 18:02:12 +0000372 }
373
374}
375
Chris Lattner8394d792007-06-05 20:53:16 +0000376RValue CodeGenFunction::EmitIntegerLiteral(const IntegerLiteral *E) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000377 return RValue::get(llvm::ConstantInt::get(E->getValue()));
Chris Lattnere47e4402007-06-01 18:02:12 +0000378}
379
Chris Lattner8394d792007-06-05 20:53:16 +0000380RValue CodeGenFunction::EmitCastExpr(const CastExpr *E) {
381 QualType SrcTy;
382 RValue Src = EmitExprWithUsualUnaryConversions(E->getSubExpr(), SrcTy);
383
384 // If the destination is void, just evaluate the source.
385 if (E->getType()->isVoidType())
386 return RValue::getAggregate(0);
387
Chris Lattnercf106ab2007-06-06 04:05:39 +0000388 return EmitConversion(Src, SrcTy, E->getType(), E->getLParenLoc());
Chris Lattner8394d792007-06-05 20:53:16 +0000389}
Chris Lattnerf0106d22007-06-02 19:33:17 +0000390
Chris Lattner2b228c92007-06-15 21:34:29 +0000391RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
392 QualType Ty;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000393 llvm::Value *Callee =
394 EmitExprWithUsualUnaryConversions(E->getCallee(), Ty).getVal();
Chris Lattner2b228c92007-06-15 21:34:29 +0000395
Chris Lattner23b7eb62007-06-15 23:05:46 +0000396 llvm::SmallVector<llvm::Value*, 16> Args;
Chris Lattner2b228c92007-06-15 21:34:29 +0000397
398 // FIXME: Handle struct return.
399 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
400 RValue ArgVal = EmitExprWithUsualUnaryConversions(E->getArg(i), Ty);
401
402 if (ArgVal.isScalar())
403 Args.push_back(ArgVal.getVal());
404 else // Pass by-address. FIXME: Set attribute bit on call.
405 Args.push_back(ArgVal.getAggregateVal());
406 }
407
Chris Lattner23b7eb62007-06-15 23:05:46 +0000408 llvm::Value *V = Builder.CreateCall(Callee, &Args[0], Args.size());
Chris Lattner2b228c92007-06-15 21:34:29 +0000409 if (V->getType() != llvm::Type::VoidTy)
410 V->setName("call");
411
412 // FIXME: Struct return;
413 return RValue::get(V);
414}
415
416
Chris Lattner8394d792007-06-05 20:53:16 +0000417//===----------------------------------------------------------------------===//
418// Unary Operator Emission
419//===----------------------------------------------------------------------===//
420
421RValue CodeGenFunction::EmitExprWithUsualUnaryConversions(const Expr *E,
422 QualType &ResTy) {
Chris Lattner6db1fb82007-06-02 22:49:07 +0000423 ResTy = E->getType().getCanonicalType();
424
425 if (isa<FunctionType>(ResTy)) { // C99 6.3.2.1p4
426 // Functions are promoted to their address.
427 ResTy = getContext().getPointerType(ResTy);
Chris Lattner8394d792007-06-05 20:53:16 +0000428 return RValue::get(EmitLValue(E).getAddress());
Chris Lattner6db1fb82007-06-02 22:49:07 +0000429 } else if (const ArrayType *ary = dyn_cast<ArrayType>(ResTy)) {
430 // C99 6.3.2.1p3
431 ResTy = getContext().getPointerType(ary->getElementType());
432
433 // FIXME: For now we assume that all source arrays map to LLVM arrays. This
434 // will not true when we add support for VLAs.
435 llvm::Value *V = EmitLValue(E).getAddress(); // Bitfields can't be arrays.
436
437 assert(isa<llvm::PointerType>(V->getType()) &&
438 isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
439 ->getElementType()) &&
440 "Doesn't support VLAs yet!");
441 llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
Chris Lattner8394d792007-06-05 20:53:16 +0000442 return RValue::get(Builder.CreateGEP(V, Idx0, Idx0, "arraydecay"));
Chris Lattner6db1fb82007-06-02 22:49:07 +0000443 } else if (ResTy->isPromotableIntegerType()) { // C99 6.3.1.1p2
444 // FIXME: this probably isn't right, pending clarification from Steve.
445 llvm::Value *Val = EmitExpr(E).getVal();
446
Chris Lattner6db1fb82007-06-02 22:49:07 +0000447 // If the input is a signed integer, sign extend to the destination.
448 if (ResTy->isSignedIntegerType()) {
449 Val = Builder.CreateSExt(Val, LLVMIntTy, "promote");
450 } else {
451 // This handles unsigned types, including bool.
452 Val = Builder.CreateZExt(Val, LLVMIntTy, "promote");
453 }
454 ResTy = getContext().IntTy;
455
Chris Lattner8394d792007-06-05 20:53:16 +0000456 return RValue::get(Val);
Chris Lattner6db1fb82007-06-02 22:49:07 +0000457 }
458
459 // Otherwise, this is a float, double, int, struct, etc.
460 return EmitExpr(E);
461}
462
463
Chris Lattner8394d792007-06-05 20:53:16 +0000464RValue CodeGenFunction::EmitUnaryOperator(const UnaryOperator *E) {
Chris Lattnerf0106d22007-06-02 19:33:17 +0000465 switch (E->getOpcode()) {
466 default:
467 printf("Unimplemented unary expr!\n");
468 E->dump();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000469 return RValue::get(llvm::UndefValue::get(llvm::Type::Int32Ty));
Chris Lattner8394d792007-06-05 20:53:16 +0000470 // FIXME: pre/post inc/dec
471 case UnaryOperator::AddrOf: return EmitUnaryAddrOf(E);
472 case UnaryOperator::Deref : return EmitLoadOfLValue(E);
473 case UnaryOperator::Plus : return EmitUnaryPlus(E);
474 case UnaryOperator::Minus : return EmitUnaryMinus(E);
475 case UnaryOperator::Not : return EmitUnaryNot(E);
476 case UnaryOperator::LNot : return EmitUnaryLNot(E);
477 // FIXME: SIZEOF/ALIGNOF(expr).
478 // FIXME: real/imag
479 case UnaryOperator::Extension: return EmitExpr(E->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000480 }
481}
482
Chris Lattner8394d792007-06-05 20:53:16 +0000483/// C99 6.5.3.2
484RValue CodeGenFunction::EmitUnaryAddrOf(const UnaryOperator *E) {
485 // The address of the operand is just its lvalue. It cannot be a bitfield.
486 return RValue::get(EmitLValue(E->getSubExpr()).getAddress());
487}
488
489RValue CodeGenFunction::EmitUnaryPlus(const UnaryOperator *E) {
490 // Unary plus just performs promotions on its arithmetic operand.
491 QualType Ty;
Chris Lattnerb48238182007-06-15 21:04:38 +0000492 return EmitExprWithUsualUnaryConversions(E->getSubExpr(), Ty);
Chris Lattner8394d792007-06-05 20:53:16 +0000493}
494
495RValue CodeGenFunction::EmitUnaryMinus(const UnaryOperator *E) {
496 // Unary minus performs promotions, then negates its arithmetic operand.
497 QualType Ty;
Chris Lattnerb48238182007-06-15 21:04:38 +0000498 RValue V = EmitExprWithUsualUnaryConversions(E->getSubExpr(), Ty);
Chris Lattnerf0106d22007-06-02 19:33:17 +0000499
Chris Lattner8394d792007-06-05 20:53:16 +0000500 if (V.isScalar())
501 return RValue::get(Builder.CreateNeg(V.getVal(), "neg"));
502
503 assert(0 && "FIXME: This doesn't handle complex operands yet");
504}
505
506RValue CodeGenFunction::EmitUnaryNot(const UnaryOperator *E) {
507 // Unary not performs promotions, then complements its integer operand.
508 QualType Ty;
Chris Lattnerb48238182007-06-15 21:04:38 +0000509 RValue V = EmitExprWithUsualUnaryConversions(E->getSubExpr(), Ty);
Chris Lattner8394d792007-06-05 20:53:16 +0000510
511 if (V.isScalar())
512 return RValue::get(Builder.CreateNot(V.getVal(), "neg"));
513
514 assert(0 && "FIXME: This doesn't handle integer complex operands yet (GNU)");
515}
516
517
518/// C99 6.5.3.3
519RValue CodeGenFunction::EmitUnaryLNot(const UnaryOperator *E) {
520 // Compare operand to zero.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000521 llvm::Value *BoolVal = EvaluateExprAsBool(E->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000522
523 // Invert value.
Chris Lattnera45c5af2007-06-02 19:47:04 +0000524 // TODO: Could dynamically modify easy computations here. For example, if
525 // the operand is an icmp ne, turn into icmp eq.
Chris Lattnerf0106d22007-06-02 19:33:17 +0000526 BoolVal = Builder.CreateNot(BoolVal, "lnot");
527
528 // ZExt result to int.
Chris Lattner8394d792007-06-05 20:53:16 +0000529 return RValue::get(Builder.CreateZExt(BoolVal, LLVMIntTy, "lnot.ext"));
Chris Lattnerf0106d22007-06-02 19:33:17 +0000530}
531
Chris Lattnere47e4402007-06-01 18:02:12 +0000532
Chris Lattnerdb91b162007-06-02 00:16:28 +0000533//===--------------------------------------------------------------------===//
534// Binary Operator Emission
535//===--------------------------------------------------------------------===//
536
537// FIXME describe.
Chris Lattnercf250242007-06-03 02:02:44 +0000538QualType CodeGenFunction::
Chris Lattner8394d792007-06-05 20:53:16 +0000539EmitUsualArithmeticConversions(const BinaryOperator *E, RValue &LHS,
540 RValue &RHS) {
Chris Lattnerc18f9d12007-06-02 22:51:30 +0000541 QualType LHSType, RHSType;
542 LHS = EmitExprWithUsualUnaryConversions(E->getLHS(), LHSType);
543 RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), RHSType);
544
Chris Lattnercf250242007-06-03 02:02:44 +0000545 // If both operands have the same source type, we're done already.
546 if (LHSType == RHSType) return LHSType;
547
548 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
549 // The caller can deal with this (e.g. pointer + int).
550 if (!LHSType->isArithmeticType() || !RHSType->isArithmeticType())
551 return LHSType;
552
553 // At this point, we have two different arithmetic types.
554
555 // Handle complex types first (C99 6.3.1.8p1).
556 if (LHSType->isComplexType() || RHSType->isComplexType()) {
557 assert(0 && "FIXME: complex types unimp");
558#if 0
559 // if we have an integer operand, the result is the complex type.
560 if (rhs->isIntegerType())
561 return lhs;
562 if (lhs->isIntegerType())
563 return rhs;
564 return Context.maxComplexType(lhs, rhs);
565#endif
566 }
567
568 // If neither operand is complex, they must be scalars.
569 llvm::Value *LHSV = LHS.getVal();
570 llvm::Value *RHSV = RHS.getVal();
571
572 // If the LLVM types are already equal, then they only differed in sign, or it
573 // was something like char/signed char or double/long double.
574 if (LHSV->getType() == RHSV->getType())
575 return LHSType;
576
577 // Now handle "real" floating types (i.e. float, double, long double).
578 if (LHSType->isRealFloatingType() || RHSType->isRealFloatingType()) {
579 // if we have an integer operand, the result is the real floating type, and
580 // the integer converts to FP.
581 if (RHSType->isIntegerType()) {
582 // Promote the RHS to an FP type of the LHS, with the sign following the
583 // RHS.
584 if (RHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000585 RHS = RValue::get(Builder.CreateSIToFP(RHSV,LHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000586 else
Chris Lattner8394d792007-06-05 20:53:16 +0000587 RHS = RValue::get(Builder.CreateUIToFP(RHSV,LHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000588 return LHSType;
589 }
590
591 if (LHSType->isIntegerType()) {
592 // Promote the LHS to an FP type of the RHS, with the sign following the
593 // LHS.
594 if (LHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000595 LHS = RValue::get(Builder.CreateSIToFP(LHSV,RHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000596 else
Chris Lattner8394d792007-06-05 20:53:16 +0000597 LHS = RValue::get(Builder.CreateUIToFP(LHSV,RHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000598 return RHSType;
599 }
600
601 // Otherwise, they are two FP types. Promote the smaller operand to the
602 // bigger result.
603 QualType BiggerType = ASTContext::maxFloatingType(LHSType, RHSType);
604
605 if (BiggerType == LHSType)
Chris Lattner8394d792007-06-05 20:53:16 +0000606 RHS = RValue::get(Builder.CreateFPExt(RHSV, LHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000607 else
Chris Lattner8394d792007-06-05 20:53:16 +0000608 LHS = RValue::get(Builder.CreateFPExt(LHSV, RHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000609 return BiggerType;
610 }
611
612 // Finally, we have two integer types that are different according to C. Do
613 // a sign or zero extension if needed.
614
615 // Otherwise, one type is smaller than the other.
616 QualType ResTy = ASTContext::maxIntegerType(LHSType, RHSType);
617
618 if (LHSType == ResTy) {
619 if (RHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000620 RHS = RValue::get(Builder.CreateSExt(RHSV, LHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000621 else
Chris Lattner8394d792007-06-05 20:53:16 +0000622 RHS = RValue::get(Builder.CreateZExt(RHSV, LHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000623 } else {
624 assert(RHSType == ResTy && "Unknown conversion");
625 if (LHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000626 LHS = RValue::get(Builder.CreateSExt(LHSV, RHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000627 else
Chris Lattner8394d792007-06-05 20:53:16 +0000628 LHS = RValue::get(Builder.CreateZExt(LHSV, RHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000629 }
630 return ResTy;
Chris Lattnerdb91b162007-06-02 00:16:28 +0000631}
632
633
Chris Lattner8394d792007-06-05 20:53:16 +0000634RValue CodeGenFunction::EmitBinaryOperator(const BinaryOperator *E) {
Chris Lattnerdb91b162007-06-02 00:16:28 +0000635 switch (E->getOpcode()) {
636 default:
Chris Lattner8394d792007-06-05 20:53:16 +0000637 fprintf(stderr, "Unimplemented expr!\n");
Chris Lattnerdb91b162007-06-02 00:16:28 +0000638 E->dump();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000639 return RValue::get(llvm::UndefValue::get(llvm::Type::Int32Ty));
Chris Lattner8394d792007-06-05 20:53:16 +0000640 case BinaryOperator::Mul: return EmitBinaryMul(E);
641 case BinaryOperator::Div: return EmitBinaryDiv(E);
642 case BinaryOperator::Rem: return EmitBinaryRem(E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000643 case BinaryOperator::Add: return EmitBinaryAdd(E);
Chris Lattner8394d792007-06-05 20:53:16 +0000644 case BinaryOperator::Sub: return EmitBinarySub(E);
645 case BinaryOperator::Shl: return EmitBinaryShl(E);
646 case BinaryOperator::Shr: return EmitBinaryShr(E);
Chris Lattner8394d792007-06-05 20:53:16 +0000647 case BinaryOperator::And: return EmitBinaryAnd(E);
648 case BinaryOperator::Xor: return EmitBinaryXor(E);
649 case BinaryOperator::Or : return EmitBinaryOr(E);
650 case BinaryOperator::LAnd: return EmitBinaryLAnd(E);
651 case BinaryOperator::LOr: return EmitBinaryLOr(E);
Chris Lattner1fde0b32007-06-20 18:30:55 +0000652 case BinaryOperator::LT:
653 return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_ULT,
654 llvm::ICmpInst::ICMP_SLT,
655 llvm::FCmpInst::FCMP_OLT);
656 case BinaryOperator::GT:
657 return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_UGT,
658 llvm::ICmpInst::ICMP_SGT,
659 llvm::FCmpInst::FCMP_OGT);
660 case BinaryOperator::LE:
661 return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_ULE,
662 llvm::ICmpInst::ICMP_SLE,
663 llvm::FCmpInst::FCMP_OLE);
664 case BinaryOperator::GE:
665 return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_UGE,
666 llvm::ICmpInst::ICMP_SGE,
667 llvm::FCmpInst::FCMP_OGE);
668 case BinaryOperator::EQ:
669 return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_EQ,
670 llvm::ICmpInst::ICMP_EQ,
671 llvm::FCmpInst::FCMP_OEQ);
672 case BinaryOperator::NE:
673 return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_NE,
674 llvm::ICmpInst::ICMP_NE,
675 llvm::FCmpInst::FCMP_UNE);
Chris Lattner8394d792007-06-05 20:53:16 +0000676 case BinaryOperator::Assign: return EmitBinaryAssign(E);
677 // FIXME: Assignment.
678 case BinaryOperator::Comma: return EmitBinaryComma(E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000679 }
680}
681
Chris Lattner8394d792007-06-05 20:53:16 +0000682RValue CodeGenFunction::EmitBinaryMul(const BinaryOperator *E) {
683 RValue LHS, RHS;
684 EmitUsualArithmeticConversions(E, LHS, RHS);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000685
Chris Lattner8394d792007-06-05 20:53:16 +0000686 if (LHS.isScalar())
687 return RValue::get(Builder.CreateMul(LHS.getVal(), RHS.getVal(), "mul"));
688
689 assert(0 && "FIXME: This doesn't handle complex operands yet");
690}
691
692RValue CodeGenFunction::EmitBinaryDiv(const BinaryOperator *E) {
693 RValue LHS, RHS;
694 EmitUsualArithmeticConversions(E, LHS, RHS);
695
696 if (LHS.isScalar()) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000697 llvm::Value *RV;
Chris Lattner8394d792007-06-05 20:53:16 +0000698 if (LHS.getVal()->getType()->isFloatingPoint())
699 RV = Builder.CreateFDiv(LHS.getVal(), RHS.getVal(), "div");
700 else if (E->getType()->isUnsignedIntegerType())
701 RV = Builder.CreateUDiv(LHS.getVal(), RHS.getVal(), "div");
702 else
703 RV = Builder.CreateSDiv(LHS.getVal(), RHS.getVal(), "div");
704 return RValue::get(RV);
705 }
706 assert(0 && "FIXME: This doesn't handle complex operands yet");
707}
708
709RValue CodeGenFunction::EmitBinaryRem(const BinaryOperator *E) {
710 RValue LHS, RHS;
711 EmitUsualArithmeticConversions(E, LHS, RHS);
712
713 if (LHS.isScalar()) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000714 llvm::Value *RV;
Chris Lattner8394d792007-06-05 20:53:16 +0000715 // Rem in C can't be a floating point type: C99 6.5.5p2.
716 if (E->getType()->isUnsignedIntegerType())
717 RV = Builder.CreateURem(LHS.getVal(), RHS.getVal(), "rem");
718 else
719 RV = Builder.CreateSRem(LHS.getVal(), RHS.getVal(), "rem");
720 return RValue::get(RV);
721 }
722
723 assert(0 && "FIXME: This doesn't handle complex operands yet");
724}
725
726RValue CodeGenFunction::EmitBinaryAdd(const BinaryOperator *E) {
727 RValue LHS, RHS;
Chris Lattnerdb91b162007-06-02 00:16:28 +0000728 EmitUsualArithmeticConversions(E, LHS, RHS);
729
Chris Lattner8394d792007-06-05 20:53:16 +0000730 // FIXME: This doesn't handle ptr+int etc yet.
731
732 if (LHS.isScalar())
733 return RValue::get(Builder.CreateAdd(LHS.getVal(), RHS.getVal(), "add"));
734
735 assert(0 && "FIXME: This doesn't handle complex operands yet");
736
737}
738
739RValue CodeGenFunction::EmitBinarySub(const BinaryOperator *E) {
740 RValue LHS, RHS;
741 EmitUsualArithmeticConversions(E, LHS, RHS);
742
743 // FIXME: This doesn't handle ptr-int or ptr-ptr, etc yet.
744
745 if (LHS.isScalar())
746 return RValue::get(Builder.CreateSub(LHS.getVal(), RHS.getVal(), "sub"));
747
748 assert(0 && "FIXME: This doesn't handle complex operands yet");
749
750}
751
752RValue CodeGenFunction::EmitBinaryShl(const BinaryOperator *E) {
753 // For shifts, integer promotions are performed, but the usual arithmetic
754 // conversions are not. The LHS and RHS need not have the same type.
755
756 QualType ResTy;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000757 llvm::Value *LHS =
758 EmitExprWithUsualUnaryConversions(E->getLHS(), ResTy).getVal();
759 llvm::Value *RHS =
760 EmitExprWithUsualUnaryConversions(E->getRHS(), ResTy).getVal();
Chris Lattner8394d792007-06-05 20:53:16 +0000761
762 // LLVM requires the LHS and RHS to be the same type, promote or truncate the
763 // RHS to the same size as the LHS.
764 if (LHS->getType() != RHS->getType())
765 RHS = Builder.CreateIntCast(RHS, LHS->getType(), false, "sh_prom");
766
767 return RValue::get(Builder.CreateShl(LHS, RHS, "shl"));
768}
769
770RValue CodeGenFunction::EmitBinaryShr(const BinaryOperator *E) {
771 // For shifts, integer promotions are performed, but the usual arithmetic
772 // conversions are not. The LHS and RHS need not have the same type.
773
774 QualType ResTy;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000775 llvm::Value *LHS =
776 EmitExprWithUsualUnaryConversions(E->getLHS(), ResTy).getVal();
777 llvm::Value *RHS =
778 EmitExprWithUsualUnaryConversions(E->getRHS(), ResTy).getVal();
Chris Lattner8394d792007-06-05 20:53:16 +0000779
780 // LLVM requires the LHS and RHS to be the same type, promote or truncate the
781 // RHS to the same size as the LHS.
782 if (LHS->getType() != RHS->getType())
783 RHS = Builder.CreateIntCast(RHS, LHS->getType(), false, "sh_prom");
784
785 if (E->getType()->isUnsignedIntegerType())
786 return RValue::get(Builder.CreateLShr(LHS, RHS, "shr"));
787 else
788 return RValue::get(Builder.CreateAShr(LHS, RHS, "shr"));
789}
790
Chris Lattner1fde0b32007-06-20 18:30:55 +0000791RValue CodeGenFunction::EmitBinaryCompare(const BinaryOperator *E,
792 unsigned UICmpOpc, unsigned SICmpOpc,
793 unsigned FCmpOpc) {
Chris Lattner273c63d2007-06-20 18:02:30 +0000794 RValue LHS, RHS;
795 EmitUsualArithmeticConversions(E, LHS, RHS);
796
797 llvm::Value *Result;
798 if (LHS.isScalar()) {
799 if (LHS.getVal()->getType()->isFloatingPoint()) {
Chris Lattner1fde0b32007-06-20 18:30:55 +0000800 Result = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
801 LHS.getVal(), RHS.getVal(), "cmp");
802 } else if (E->getLHS()->getType()->isUnsignedIntegerType()) {
803 // FIXME: This check isn't right for "unsigned short < int" where ushort
804 // promotes to int and does a signed compare.
805 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
806 LHS.getVal(), RHS.getVal(), "cmp");
Chris Lattner273c63d2007-06-20 18:02:30 +0000807 } else {
Chris Lattner1fde0b32007-06-20 18:30:55 +0000808 // Signed integers and pointers.
809 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
810 LHS.getVal(), RHS.getVal(), "cmp");
Chris Lattner273c63d2007-06-20 18:02:30 +0000811 }
812 } else {
813 // Struct/union/complex
814 assert(0 && "Aggregate comparisons not implemented yet!");
815 }
816
817 // ZExt result to int.
818 return RValue::get(Builder.CreateZExt(Result, LLVMIntTy, "cmp.ext"));
819}
820
Chris Lattner8394d792007-06-05 20:53:16 +0000821RValue CodeGenFunction::EmitBinaryAnd(const BinaryOperator *E) {
822 RValue LHS, RHS;
823 EmitUsualArithmeticConversions(E, LHS, RHS);
824
825 if (LHS.isScalar())
826 return RValue::get(Builder.CreateAnd(LHS.getVal(), RHS.getVal(), "and"));
827
828 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
829}
830
831RValue CodeGenFunction::EmitBinaryXor(const BinaryOperator *E) {
832 RValue LHS, RHS;
833 EmitUsualArithmeticConversions(E, LHS, RHS);
834
835 if (LHS.isScalar())
836 return RValue::get(Builder.CreateXor(LHS.getVal(), RHS.getVal(), "xor"));
837
838 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
839}
840
841RValue CodeGenFunction::EmitBinaryOr(const BinaryOperator *E) {
842 RValue LHS, RHS;
843 EmitUsualArithmeticConversions(E, LHS, RHS);
844
845 if (LHS.isScalar())
846 return RValue::get(Builder.CreateOr(LHS.getVal(), RHS.getVal(), "or"));
847
848 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
849}
850
851RValue CodeGenFunction::EmitBinaryLAnd(const BinaryOperator *E) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000852 llvm::Value *LHSCond = EvaluateExprAsBool(E->getLHS());
Chris Lattner8394d792007-06-05 20:53:16 +0000853
Chris Lattner23b7eb62007-06-15 23:05:46 +0000854 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("land_cont");
855 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("land_rhs");
Chris Lattner8394d792007-06-05 20:53:16 +0000856
Chris Lattner23b7eb62007-06-15 23:05:46 +0000857 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
Chris Lattner8394d792007-06-05 20:53:16 +0000858 Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock);
859
860 EmitBlock(RHSBlock);
Chris Lattner23b7eb62007-06-15 23:05:46 +0000861 llvm::Value *RHSCond = EvaluateExprAsBool(E->getRHS());
Chris Lattner8394d792007-06-05 20:53:16 +0000862
863 // Reaquire the RHS block, as there may be subblocks inserted.
864 RHSBlock = Builder.GetInsertBlock();
865 EmitBlock(ContBlock);
866
867 // Create a PHI node. If we just evaluted the LHS condition, the result is
868 // false. If we evaluated both, the result is the RHS condition.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000869 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land");
Chris Lattner8394d792007-06-05 20:53:16 +0000870 PN->reserveOperandSpace(2);
Chris Lattner23b7eb62007-06-15 23:05:46 +0000871 PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock);
Chris Lattner8394d792007-06-05 20:53:16 +0000872 PN->addIncoming(RHSCond, RHSBlock);
873
874 // ZExt result to int.
875 return RValue::get(Builder.CreateZExt(PN, LLVMIntTy, "land.ext"));
876}
877
878RValue CodeGenFunction::EmitBinaryLOr(const BinaryOperator *E) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000879 llvm::Value *LHSCond = EvaluateExprAsBool(E->getLHS());
Chris Lattner8394d792007-06-05 20:53:16 +0000880
Chris Lattner23b7eb62007-06-15 23:05:46 +0000881 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("lor_cont");
882 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("lor_rhs");
Chris Lattner8394d792007-06-05 20:53:16 +0000883
Chris Lattner23b7eb62007-06-15 23:05:46 +0000884 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
Chris Lattner8394d792007-06-05 20:53:16 +0000885 Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock);
886
887 EmitBlock(RHSBlock);
Chris Lattner23b7eb62007-06-15 23:05:46 +0000888 llvm::Value *RHSCond = EvaluateExprAsBool(E->getRHS());
Chris Lattner8394d792007-06-05 20:53:16 +0000889
890 // Reaquire the RHS block, as there may be subblocks inserted.
891 RHSBlock = Builder.GetInsertBlock();
892 EmitBlock(ContBlock);
893
894 // Create a PHI node. If we just evaluted the LHS condition, the result is
895 // true. If we evaluated both, the result is the RHS condition.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000896 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor");
Chris Lattner8394d792007-06-05 20:53:16 +0000897 PN->reserveOperandSpace(2);
Chris Lattner23b7eb62007-06-15 23:05:46 +0000898 PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock);
Chris Lattner8394d792007-06-05 20:53:16 +0000899 PN->addIncoming(RHSCond, RHSBlock);
900
901 // ZExt result to int.
902 return RValue::get(Builder.CreateZExt(PN, LLVMIntTy, "lor.ext"));
903}
904
905RValue CodeGenFunction::EmitBinaryAssign(const BinaryOperator *E) {
906 LValue LHS = EmitLValue(E->getLHS());
907
908 QualType RHSTy;
909 RValue RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), RHSTy);
910
911 // Convert the RHS to the type of the LHS.
Chris Lattnercf106ab2007-06-06 04:05:39 +0000912 // FIXME: I'm not thrilled about having to call getLocStart() here... :(
913 RHS = EmitConversion(RHS, RHSTy, E->getType(), E->getLocStart());
Chris Lattner8394d792007-06-05 20:53:16 +0000914
915 // Store the value into the LHS.
916 EmitStoreThroughLValue(RHS, LHS, E->getType());
917
918 // Return the converted RHS.
919 return RHS;
920}
921
Chris Lattner8394d792007-06-05 20:53:16 +0000922RValue CodeGenFunction::EmitBinaryComma(const BinaryOperator *E) {
923 EmitExpr(E->getLHS());
924 return EmitExpr(E->getRHS());
925}