blob: e6aaff15d558ad322f470730434759d99ac709c8 [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:
340 printf("Unimplemented expr!\n");
341 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);
647
648 // FIXME: relational
649
650 case BinaryOperator::And: return EmitBinaryAnd(E);
651 case BinaryOperator::Xor: return EmitBinaryXor(E);
652 case BinaryOperator::Or : return EmitBinaryOr(E);
653 case BinaryOperator::LAnd: return EmitBinaryLAnd(E);
654 case BinaryOperator::LOr: return EmitBinaryLOr(E);
655
656 case BinaryOperator::Assign: return EmitBinaryAssign(E);
657 // FIXME: Assignment.
658 case BinaryOperator::Comma: return EmitBinaryComma(E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000659 }
660}
661
Chris Lattner8394d792007-06-05 20:53:16 +0000662RValue CodeGenFunction::EmitBinaryMul(const BinaryOperator *E) {
663 RValue LHS, RHS;
664 EmitUsualArithmeticConversions(E, LHS, RHS);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000665
Chris Lattner8394d792007-06-05 20:53:16 +0000666 if (LHS.isScalar())
667 return RValue::get(Builder.CreateMul(LHS.getVal(), RHS.getVal(), "mul"));
668
669 assert(0 && "FIXME: This doesn't handle complex operands yet");
670}
671
672RValue CodeGenFunction::EmitBinaryDiv(const BinaryOperator *E) {
673 RValue LHS, RHS;
674 EmitUsualArithmeticConversions(E, LHS, RHS);
675
676 if (LHS.isScalar()) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000677 llvm::Value *RV;
Chris Lattner8394d792007-06-05 20:53:16 +0000678 if (LHS.getVal()->getType()->isFloatingPoint())
679 RV = Builder.CreateFDiv(LHS.getVal(), RHS.getVal(), "div");
680 else if (E->getType()->isUnsignedIntegerType())
681 RV = Builder.CreateUDiv(LHS.getVal(), RHS.getVal(), "div");
682 else
683 RV = Builder.CreateSDiv(LHS.getVal(), RHS.getVal(), "div");
684 return RValue::get(RV);
685 }
686 assert(0 && "FIXME: This doesn't handle complex operands yet");
687}
688
689RValue CodeGenFunction::EmitBinaryRem(const BinaryOperator *E) {
690 RValue LHS, RHS;
691 EmitUsualArithmeticConversions(E, LHS, RHS);
692
693 if (LHS.isScalar()) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000694 llvm::Value *RV;
Chris Lattner8394d792007-06-05 20:53:16 +0000695 // Rem in C can't be a floating point type: C99 6.5.5p2.
696 if (E->getType()->isUnsignedIntegerType())
697 RV = Builder.CreateURem(LHS.getVal(), RHS.getVal(), "rem");
698 else
699 RV = Builder.CreateSRem(LHS.getVal(), RHS.getVal(), "rem");
700 return RValue::get(RV);
701 }
702
703 assert(0 && "FIXME: This doesn't handle complex operands yet");
704}
705
706RValue CodeGenFunction::EmitBinaryAdd(const BinaryOperator *E) {
707 RValue LHS, RHS;
Chris Lattnerdb91b162007-06-02 00:16:28 +0000708 EmitUsualArithmeticConversions(E, LHS, RHS);
709
Chris Lattner8394d792007-06-05 20:53:16 +0000710 // FIXME: This doesn't handle ptr+int etc yet.
711
712 if (LHS.isScalar())
713 return RValue::get(Builder.CreateAdd(LHS.getVal(), RHS.getVal(), "add"));
714
715 assert(0 && "FIXME: This doesn't handle complex operands yet");
716
717}
718
719RValue CodeGenFunction::EmitBinarySub(const BinaryOperator *E) {
720 RValue LHS, RHS;
721 EmitUsualArithmeticConversions(E, LHS, RHS);
722
723 // FIXME: This doesn't handle ptr-int or ptr-ptr, etc yet.
724
725 if (LHS.isScalar())
726 return RValue::get(Builder.CreateSub(LHS.getVal(), RHS.getVal(), "sub"));
727
728 assert(0 && "FIXME: This doesn't handle complex operands yet");
729
730}
731
732RValue CodeGenFunction::EmitBinaryShl(const BinaryOperator *E) {
733 // For shifts, integer promotions are performed, but the usual arithmetic
734 // conversions are not. The LHS and RHS need not have the same type.
735
736 QualType ResTy;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000737 llvm::Value *LHS =
738 EmitExprWithUsualUnaryConversions(E->getLHS(), ResTy).getVal();
739 llvm::Value *RHS =
740 EmitExprWithUsualUnaryConversions(E->getRHS(), ResTy).getVal();
Chris Lattner8394d792007-06-05 20:53:16 +0000741
742 // LLVM requires the LHS and RHS to be the same type, promote or truncate the
743 // RHS to the same size as the LHS.
744 if (LHS->getType() != RHS->getType())
745 RHS = Builder.CreateIntCast(RHS, LHS->getType(), false, "sh_prom");
746
747 return RValue::get(Builder.CreateShl(LHS, RHS, "shl"));
748}
749
750RValue CodeGenFunction::EmitBinaryShr(const BinaryOperator *E) {
751 // For shifts, integer promotions are performed, but the usual arithmetic
752 // conversions are not. The LHS and RHS need not have the same type.
753
754 QualType ResTy;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000755 llvm::Value *LHS =
756 EmitExprWithUsualUnaryConversions(E->getLHS(), ResTy).getVal();
757 llvm::Value *RHS =
758 EmitExprWithUsualUnaryConversions(E->getRHS(), ResTy).getVal();
Chris Lattner8394d792007-06-05 20:53:16 +0000759
760 // LLVM requires the LHS and RHS to be the same type, promote or truncate the
761 // RHS to the same size as the LHS.
762 if (LHS->getType() != RHS->getType())
763 RHS = Builder.CreateIntCast(RHS, LHS->getType(), false, "sh_prom");
764
765 if (E->getType()->isUnsignedIntegerType())
766 return RValue::get(Builder.CreateLShr(LHS, RHS, "shr"));
767 else
768 return RValue::get(Builder.CreateAShr(LHS, RHS, "shr"));
769}
770
771RValue CodeGenFunction::EmitBinaryAnd(const BinaryOperator *E) {
772 RValue LHS, RHS;
773 EmitUsualArithmeticConversions(E, LHS, RHS);
774
775 if (LHS.isScalar())
776 return RValue::get(Builder.CreateAnd(LHS.getVal(), RHS.getVal(), "and"));
777
778 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
779}
780
781RValue CodeGenFunction::EmitBinaryXor(const BinaryOperator *E) {
782 RValue LHS, RHS;
783 EmitUsualArithmeticConversions(E, LHS, RHS);
784
785 if (LHS.isScalar())
786 return RValue::get(Builder.CreateXor(LHS.getVal(), RHS.getVal(), "xor"));
787
788 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
789}
790
791RValue CodeGenFunction::EmitBinaryOr(const BinaryOperator *E) {
792 RValue LHS, RHS;
793 EmitUsualArithmeticConversions(E, LHS, RHS);
794
795 if (LHS.isScalar())
796 return RValue::get(Builder.CreateOr(LHS.getVal(), RHS.getVal(), "or"));
797
798 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
799}
800
801RValue CodeGenFunction::EmitBinaryLAnd(const BinaryOperator *E) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000802 llvm::Value *LHSCond = EvaluateExprAsBool(E->getLHS());
Chris Lattner8394d792007-06-05 20:53:16 +0000803
Chris Lattner23b7eb62007-06-15 23:05:46 +0000804 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("land_cont");
805 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("land_rhs");
Chris Lattner8394d792007-06-05 20:53:16 +0000806
Chris Lattner23b7eb62007-06-15 23:05:46 +0000807 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
Chris Lattner8394d792007-06-05 20:53:16 +0000808 Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock);
809
810 EmitBlock(RHSBlock);
Chris Lattner23b7eb62007-06-15 23:05:46 +0000811 llvm::Value *RHSCond = EvaluateExprAsBool(E->getRHS());
Chris Lattner8394d792007-06-05 20:53:16 +0000812
813 // Reaquire the RHS block, as there may be subblocks inserted.
814 RHSBlock = Builder.GetInsertBlock();
815 EmitBlock(ContBlock);
816
817 // Create a PHI node. If we just evaluted the LHS condition, the result is
818 // false. If we evaluated both, the result is the RHS condition.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000819 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land");
Chris Lattner8394d792007-06-05 20:53:16 +0000820 PN->reserveOperandSpace(2);
Chris Lattner23b7eb62007-06-15 23:05:46 +0000821 PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock);
Chris Lattner8394d792007-06-05 20:53:16 +0000822 PN->addIncoming(RHSCond, RHSBlock);
823
824 // ZExt result to int.
825 return RValue::get(Builder.CreateZExt(PN, LLVMIntTy, "land.ext"));
826}
827
828RValue CodeGenFunction::EmitBinaryLOr(const BinaryOperator *E) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000829 llvm::Value *LHSCond = EvaluateExprAsBool(E->getLHS());
Chris Lattner8394d792007-06-05 20:53:16 +0000830
Chris Lattner23b7eb62007-06-15 23:05:46 +0000831 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("lor_cont");
832 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("lor_rhs");
Chris Lattner8394d792007-06-05 20:53:16 +0000833
Chris Lattner23b7eb62007-06-15 23:05:46 +0000834 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
Chris Lattner8394d792007-06-05 20:53:16 +0000835 Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock);
836
837 EmitBlock(RHSBlock);
Chris Lattner23b7eb62007-06-15 23:05:46 +0000838 llvm::Value *RHSCond = EvaluateExprAsBool(E->getRHS());
Chris Lattner8394d792007-06-05 20:53:16 +0000839
840 // Reaquire the RHS block, as there may be subblocks inserted.
841 RHSBlock = Builder.GetInsertBlock();
842 EmitBlock(ContBlock);
843
844 // Create a PHI node. If we just evaluted the LHS condition, the result is
845 // true. If we evaluated both, the result is the RHS condition.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000846 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor");
Chris Lattner8394d792007-06-05 20:53:16 +0000847 PN->reserveOperandSpace(2);
Chris Lattner23b7eb62007-06-15 23:05:46 +0000848 PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock);
Chris Lattner8394d792007-06-05 20:53:16 +0000849 PN->addIncoming(RHSCond, RHSBlock);
850
851 // ZExt result to int.
852 return RValue::get(Builder.CreateZExt(PN, LLVMIntTy, "lor.ext"));
853}
854
855RValue CodeGenFunction::EmitBinaryAssign(const BinaryOperator *E) {
856 LValue LHS = EmitLValue(E->getLHS());
857
858 QualType RHSTy;
859 RValue RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), RHSTy);
860
861 // Convert the RHS to the type of the LHS.
Chris Lattnercf106ab2007-06-06 04:05:39 +0000862 // FIXME: I'm not thrilled about having to call getLocStart() here... :(
863 RHS = EmitConversion(RHS, RHSTy, E->getType(), E->getLocStart());
Chris Lattner8394d792007-06-05 20:53:16 +0000864
865 // Store the value into the LHS.
866 EmitStoreThroughLValue(RHS, LHS, E->getType());
867
868 // Return the converted RHS.
869 return RHS;
870}
871
872
873RValue CodeGenFunction::EmitBinaryComma(const BinaryOperator *E) {
874 EmitExpr(E->getLHS());
875 return EmitExpr(E->getRHS());
876}