blob: 63879ad66bea755519973b35948951a5de2e5f97 [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"
15#include "clang/AST/AST.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
Chris Lattner4347e3692007-06-06 04:54:52 +000018#include "llvm/Function.h"
19#include "llvm/GlobalVariable.h"
Chris Lattnere47e4402007-06-01 18:02:12 +000020using namespace clang;
21using namespace CodeGen;
22
Chris Lattnerd7f58862007-06-02 05:24:33 +000023//===--------------------------------------------------------------------===//
Chris Lattnerf0106d22007-06-02 19:33:17 +000024// Miscellaneous Helper Methods
25//===--------------------------------------------------------------------===//
26
Chris Lattner8394d792007-06-05 20:53:16 +000027
28/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
29/// expression and compare the result against zero, returning an Int1Ty value.
Chris Lattner23b7eb62007-06-15 23:05:46 +000030llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
Chris Lattner8394d792007-06-05 20:53:16 +000031 QualType Ty;
32 RValue Val = EmitExprWithUsualUnaryConversions(E, Ty);
33 return ConvertScalarValueToBool(Val, Ty);
34}
35
36//===--------------------------------------------------------------------===//
37// Conversions
38//===--------------------------------------------------------------------===//
39
40/// EmitConversion - Convert the value specied by Val, whose type is ValTy, to
41/// the type specified by DstTy, following the rules of C99 6.3.
42RValue CodeGenFunction::EmitConversion(RValue Val, QualType ValTy,
Chris Lattnercf106ab2007-06-06 04:05:39 +000043 QualType DstTy, SourceLocation Loc) {
Chris Lattner8394d792007-06-05 20:53:16 +000044 ValTy = ValTy.getCanonicalType();
45 DstTy = DstTy.getCanonicalType();
46 if (ValTy == DstTy) return Val;
Chris Lattner83b484b2007-06-06 04:39:08 +000047
48 // Handle conversions to bool first, they are special: comparisons against 0.
49 if (const BuiltinType *DestBT = dyn_cast<BuiltinType>(DstTy))
50 if (DestBT->getKind() == BuiltinType::Bool)
51 return RValue::get(ConvertScalarValueToBool(Val, ValTy));
Chris Lattner8394d792007-06-05 20:53:16 +000052
Chris Lattner83b484b2007-06-06 04:39:08 +000053 // Handle pointer conversions next: pointers can only be converted to/from
54 // other pointers and integers.
Chris Lattnercf106ab2007-06-06 04:05:39 +000055 if (isa<PointerType>(DstTy)) {
56 const llvm::Type *DestTy = ConvertType(DstTy, Loc);
57
58 // The source value may be an integer, or a pointer.
59 assert(Val.isScalar() && "Can only convert from integer or pointer");
60 if (isa<llvm::PointerType>(Val.getVal()->getType()))
61 return RValue::get(Builder.CreateBitCast(Val.getVal(), DestTy, "conv"));
62 assert(ValTy->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
63 return RValue::get(Builder.CreatePtrToInt(Val.getVal(), DestTy, "conv"));
Chris Lattner83b484b2007-06-06 04:39:08 +000064 }
65
66 if (isa<PointerType>(ValTy)) {
Chris Lattnercf106ab2007-06-06 04:05:39 +000067 // Must be an ptr to int cast.
68 const llvm::Type *DestTy = ConvertType(DstTy, Loc);
69 assert(isa<llvm::IntegerType>(DestTy) && "not ptr->int?");
70 return RValue::get(Builder.CreateIntToPtr(Val.getVal(), DestTy, "conv"));
Chris Lattner8394d792007-06-05 20:53:16 +000071 }
Chris Lattner83b484b2007-06-06 04:39:08 +000072
73 // Finally, we have the arithmetic types: real int/float and complex
74 // int/float. Handle real->real conversions first, they are the most
75 // common.
76 if (Val.isScalar() && DstTy->isRealType()) {
77 // We know that these are representable as scalars in LLVM, convert to LLVM
78 // types since they are easier to reason about.
Chris Lattner23b7eb62007-06-15 23:05:46 +000079 llvm::Value *SrcVal = Val.getVal();
Chris Lattner83b484b2007-06-06 04:39:08 +000080 const llvm::Type *DestTy = ConvertType(DstTy, Loc);
81 if (SrcVal->getType() == DestTy) return Val;
82
Chris Lattner23b7eb62007-06-15 23:05:46 +000083 llvm::Value *Result;
Chris Lattner83b484b2007-06-06 04:39:08 +000084 if (isa<llvm::IntegerType>(SrcVal->getType())) {
85 bool InputSigned = ValTy->isSignedIntegerType();
86 if (isa<llvm::IntegerType>(DestTy))
87 Result = Builder.CreateIntCast(SrcVal, DestTy, InputSigned, "conv");
88 else if (InputSigned)
89 Result = Builder.CreateSIToFP(SrcVal, DestTy, "conv");
90 else
91 Result = Builder.CreateUIToFP(SrcVal, DestTy, "conv");
92 } else {
93 assert(SrcVal->getType()->isFloatingPoint() && "Unknown real conversion");
94 if (isa<llvm::IntegerType>(DestTy)) {
95 if (DstTy->isSignedIntegerType())
96 Result = Builder.CreateFPToSI(SrcVal, DestTy, "conv");
97 else
98 Result = Builder.CreateFPToUI(SrcVal, DestTy, "conv");
99 } else {
100 assert(DestTy->isFloatingPoint() && "Unknown real conversion");
101 if (DestTy->getTypeID() < SrcVal->getType()->getTypeID())
102 Result = Builder.CreateFPTrunc(SrcVal, DestTy, "conv");
103 else
104 Result = Builder.CreateFPExt(SrcVal, DestTy, "conv");
105 }
106 }
107 return RValue::get(Result);
108 }
109
110 assert(0 && "FIXME: We don't support complex conversions yet!");
Chris Lattner8394d792007-06-05 20:53:16 +0000111}
112
113
114/// ConvertScalarValueToBool - Convert the specified expression value to a
Chris Lattnerf0106d22007-06-02 19:33:17 +0000115/// boolean (i1) truth value. This is equivalent to "Val == 0".
Chris Lattner23b7eb62007-06-15 23:05:46 +0000116llvm::Value *CodeGenFunction::ConvertScalarValueToBool(RValue Val, QualType Ty){
Chris Lattnerf0106d22007-06-02 19:33:17 +0000117 Ty = Ty.getCanonicalType();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000118 llvm::Value *Result;
Chris Lattnerf0106d22007-06-02 19:33:17 +0000119 if (const BuiltinType *BT = dyn_cast<BuiltinType>(Ty)) {
120 switch (BT->getKind()) {
121 default: assert(0 && "Unknown scalar value");
122 case BuiltinType::Bool:
123 Result = Val.getVal();
124 // Bool is already evaluated right.
125 assert(Result->getType() == llvm::Type::Int1Ty &&
126 "Unexpected bool value type!");
127 return Result;
Chris Lattnerb16f4552007-06-03 07:25:34 +0000128 case BuiltinType::Char_S:
129 case BuiltinType::Char_U:
Chris Lattnerf0106d22007-06-02 19:33:17 +0000130 case BuiltinType::SChar:
131 case BuiltinType::UChar:
132 case BuiltinType::Short:
133 case BuiltinType::UShort:
134 case BuiltinType::Int:
135 case BuiltinType::UInt:
136 case BuiltinType::Long:
137 case BuiltinType::ULong:
138 case BuiltinType::LongLong:
139 case BuiltinType::ULongLong:
140 // Code below handles simple integers.
141 break;
142 case BuiltinType::Float:
143 case BuiltinType::Double:
144 case BuiltinType::LongDouble: {
145 // Compare against 0.0 for fp scalars.
146 Result = Val.getVal();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000147 llvm::Value *Zero = llvm::Constant::getNullValue(Result->getType());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000148 // FIXME: llvm-gcc produces a une comparison: validate this is right.
149 Result = Builder.CreateFCmpUNE(Result, Zero, "tobool");
150 return Result;
151 }
152
153 case BuiltinType::FloatComplex:
154 case BuiltinType::DoubleComplex:
155 case BuiltinType::LongDoubleComplex:
156 assert(0 && "comparisons against complex not implemented yet");
157 }
158 } else {
159 assert((isa<PointerType>(Ty) ||
160 cast<TagType>(Ty)->getDecl()->getKind() == Decl::Enum) &&
161 "Unknown scalar type");
162 // Code below handles this fine.
163 }
164
165 // Usual case for integers, pointers, and enums: compare against zero.
166 Result = Val.getVal();
Chris Lattnera45c5af2007-06-02 19:47:04 +0000167
168 // Because of the type rules of C, we often end up computing a logical value,
169 // then zero extending it to int, then wanting it as a logical value again.
170 // Optimize this common case.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000171 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Result)) {
Chris Lattnera45c5af2007-06-02 19:47:04 +0000172 if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
173 Result = ZI->getOperand(0);
174 ZI->eraseFromParent();
175 return Result;
176 }
177 }
178
Chris Lattner23b7eb62007-06-15 23:05:46 +0000179 llvm::Value *Zero = llvm::Constant::getNullValue(Result->getType());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000180 return Builder.CreateICmpNE(Result, Zero, "tobool");
181}
182
Chris Lattnera45c5af2007-06-02 19:47:04 +0000183//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000184// LValue Expression Emission
Chris Lattnera45c5af2007-06-02 19:47:04 +0000185//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000186
Chris Lattner8394d792007-06-05 20:53:16 +0000187/// EmitLValue - Emit code to compute a designator that specifies the location
188/// of the expression.
189///
190/// This can return one of two things: a simple address or a bitfield
191/// reference. In either case, the LLVM Value* in the LValue structure is
192/// guaranteed to be an LLVM pointer type.
193///
194/// If this returns a bitfield reference, nothing about the pointee type of
195/// the LLVM value is known: For example, it may not be a pointer to an
196/// integer.
197///
198/// If this returns a normal address, and if the lvalue's C type is fixed
199/// size, this method guarantees that the returned pointer type will point to
200/// an LLVM type of the same size of the lvalue's type. If the lvalue has a
201/// variable length type, this is not possible.
202///
Chris Lattnerd7f58862007-06-02 05:24:33 +0000203LValue CodeGenFunction::EmitLValue(const Expr *E) {
204 switch (E->getStmtClass()) {
205 default:
Chris Lattner8394d792007-06-05 20:53:16 +0000206 fprintf(stderr, "Unimplemented lvalue expr!\n");
Chris Lattnerd7f58862007-06-02 05:24:33 +0000207 E->dump();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000208 return LValue::getAddr(llvm::UndefValue::get(
Chris Lattnerd7f58862007-06-02 05:24:33 +0000209 llvm::PointerType::get(llvm::Type::Int32Ty)));
210
211 case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Chris Lattner946aa312007-06-05 03:59:43 +0000212 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattner4347e3692007-06-06 04:54:52 +0000213 case Expr::StringLiteralClass:
214 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattner8394d792007-06-05 20:53:16 +0000215
216 case Expr::UnaryOperatorClass:
217 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000218 case Expr::ArraySubscriptExprClass:
219 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000220 }
221}
222
Chris Lattner8394d792007-06-05 20:53:16 +0000223/// EmitLoadOfLValue - Given an expression that represents a value lvalue,
224/// this method emits the address of the lvalue, then loads the result as an
225/// rvalue, returning the rvalue.
226RValue CodeGenFunction::EmitLoadOfLValue(const Expr *E) {
227 LValue LV = EmitLValue(E);
228
229 QualType ExprTy = E->getType().getCanonicalType();
230
231 // FIXME: this is silly and obviously wrong for non-scalars.
232 assert(!LV.isBitfield());
233 return RValue::get(Builder.CreateLoad(LV.getAddress(), "tmp"));
234}
235
236/// EmitStoreThroughLValue - Store the specified rvalue into the specified
237/// lvalue, where both are guaranteed to the have the same type, and that type
238/// is 'Ty'.
239void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
240 QualType Ty) {
241 // FIXME: This is obviously bogus.
242 assert(!Dst.isBitfield() && "FIXME: Don't support store to bitfield yet");
243 assert(Src.isScalar() && "FIXME: Don't support store of aggregate yet");
244
245 // TODO: Handle volatility etc.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000246 llvm::Value *Addr = Dst.getAddress();
Chris Lattner8394d792007-06-05 20:53:16 +0000247 const llvm::Type *SrcTy = Src.getVal()->getType();
248 const llvm::Type *AddrTy =
249 cast<llvm::PointerType>(Addr->getType())->getElementType();
250
251 if (AddrTy != SrcTy)
252 Addr = Builder.CreateBitCast(Addr, llvm::PointerType::get(SrcTy),
253 "storetmp");
254 Builder.CreateStore(Src.getVal(), Addr);
255}
256
Chris Lattnerd7f58862007-06-02 05:24:33 +0000257
258LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
259 const Decl *D = E->getDecl();
Chris Lattner53621a52007-06-13 20:44:40 +0000260 if (isa<BlockVarDecl>(D) || isa<ParmVarDecl>(D)) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000261 llvm::Value *V = LocalDeclMap[D];
Chris Lattnerd7f58862007-06-02 05:24:33 +0000262 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
263 return LValue::getAddr(V);
264 }
265 assert(0 && "Unimp declref");
266}
Chris Lattnere47e4402007-06-01 18:02:12 +0000267
Chris Lattner8394d792007-06-05 20:53:16 +0000268LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
269 // __extension__ doesn't affect lvalue-ness.
270 if (E->getOpcode() == UnaryOperator::Extension)
271 return EmitLValue(E->getSubExpr());
272
273 assert(E->getOpcode() == UnaryOperator::Deref &&
274 "'*' is the only unary operator that produces an lvalue");
275 return LValue::getAddr(EmitExpr(E->getSubExpr()).getVal());
276}
277
Chris Lattner4347e3692007-06-06 04:54:52 +0000278LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
279 assert(!E->isWide() && "FIXME: Wide strings not supported yet!");
280 const char *StrData = E->getStrData();
281 unsigned Len = E->getByteLength();
282
283 // FIXME: Can cache/reuse these within the module.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000284 llvm::Constant *C=llvm::ConstantArray::get(std::string(StrData, StrData+Len));
Chris Lattner4347e3692007-06-06 04:54:52 +0000285
286 // Create a global variable for this.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000287 C = new llvm::GlobalVariable(C->getType(), true,
288 llvm::GlobalValue::InternalLinkage,
Chris Lattner4347e3692007-06-06 04:54:52 +0000289 C, ".str", CurFn->getParent());
Chris Lattner23b7eb62007-06-15 23:05:46 +0000290 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
291 llvm::Constant *Zeros[] = { Zero, Zero };
292 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
Chris Lattner4347e3692007-06-06 04:54:52 +0000293 return LValue::getAddr(C);
294}
295
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000296LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
297 // The base and index must be pointers or integers, neither of which are
298 // aggregates. Emit them.
299 QualType BaseTy;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000300 llvm::Value *Base =
301 EmitExprWithUsualUnaryConversions(E->getBase(), BaseTy).getVal();
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000302 QualType IdxTy;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000303 llvm::Value *Idx =
304 EmitExprWithUsualUnaryConversions(E->getIdx(), IdxTy).getVal();
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000305
306 // Usually the base is the pointer type, but sometimes it is the index.
307 // Canonicalize to have the pointer as the base.
308 if (isa<llvm::PointerType>(Idx->getType())) {
309 std::swap(Base, Idx);
310 std::swap(BaseTy, IdxTy);
311 }
312
313 // The pointer is now the base. Extend or truncate the index type to 32 or
314 // 64-bits.
315 bool IdxSigned = IdxTy->isSignedIntegerType();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000316 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000317 if (IdxBitwidth != LLVMPointerWidth)
Chris Lattner23b7eb62007-06-15 23:05:46 +0000318 Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth),
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000319 IdxSigned, "idxprom");
320
321 // We know that the pointer points to a type of the correct size, unless the
322 // size is a VLA.
323 if (!E->getType()->isConstantSizeType())
324 assert(0 && "VLA idx not implemented");
325 return LValue::getAddr(Builder.CreateGEP(Base, Idx, "arrayidx"));
326}
327
Chris Lattnere47e4402007-06-01 18:02:12 +0000328//===--------------------------------------------------------------------===//
329// Expression Emission
330//===--------------------------------------------------------------------===//
331
Chris Lattner8394d792007-06-05 20:53:16 +0000332RValue CodeGenFunction::EmitExpr(const Expr *E) {
Chris Lattnere47e4402007-06-01 18:02:12 +0000333 assert(E && "Null expression?");
334
335 switch (E->getStmtClass()) {
336 default:
337 printf("Unimplemented expr!\n");
338 E->dump();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000339 return RValue::get(llvm::UndefValue::get(llvm::Type::Int32Ty));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000340
341 // l-values.
Chris Lattner8394d792007-06-05 20:53:16 +0000342 case Expr::DeclRefExprClass:
Chris Lattnerf99b3f52007-06-11 03:52:52 +0000343 // DeclRef's of EnumConstantDecl's are simple rvalues.
344 if (const EnumConstantDecl *EC =
345 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
Chris Lattner23b7eb62007-06-15 23:05:46 +0000346 return RValue::get(llvm::ConstantInt::get(EC->getInitVal()));
Chris Lattnerf99b3f52007-06-11 03:52:52 +0000347
348 // FALLTHROUGH
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000349 case Expr::ArraySubscriptExprClass:
Chris Lattner8394d792007-06-05 20:53:16 +0000350 return EmitLoadOfLValue(E);
Chris Lattner4347e3692007-06-06 04:54:52 +0000351 case Expr::StringLiteralClass:
352 return RValue::get(EmitLValue(E).getAddress());
Chris Lattnerd7f58862007-06-02 05:24:33 +0000353
354 // Leaf expressions.
355 case Expr::IntegerLiteralClass:
Chris Lattnere47e4402007-06-01 18:02:12 +0000356 return EmitIntegerLiteral(cast<IntegerLiteral>(E));
Chris Lattnerdb91b162007-06-02 00:16:28 +0000357
Chris Lattnerd7f58862007-06-02 05:24:33 +0000358 // Operators.
359 case Expr::ParenExprClass:
360 return EmitExpr(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000361 case Expr::UnaryOperatorClass:
362 return EmitUnaryOperator(cast<UnaryOperator>(E));
Chris Lattner8394d792007-06-05 20:53:16 +0000363 case Expr::CastExprClass:
364 return EmitCastExpr(cast<CastExpr>(E));
Chris Lattner2b228c92007-06-15 21:34:29 +0000365 case Expr::CallExprClass:
366 return EmitCallExpr(cast<CallExpr>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000367 case Expr::BinaryOperatorClass:
Chris Lattnerdb91b162007-06-02 00:16:28 +0000368 return EmitBinaryOperator(cast<BinaryOperator>(E));
Chris Lattnere47e4402007-06-01 18:02:12 +0000369 }
370
371}
372
Chris Lattner8394d792007-06-05 20:53:16 +0000373RValue CodeGenFunction::EmitIntegerLiteral(const IntegerLiteral *E) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000374 return RValue::get(llvm::ConstantInt::get(E->getValue()));
Chris Lattnere47e4402007-06-01 18:02:12 +0000375}
376
Chris Lattner8394d792007-06-05 20:53:16 +0000377RValue CodeGenFunction::EmitCastExpr(const CastExpr *E) {
378 QualType SrcTy;
379 RValue Src = EmitExprWithUsualUnaryConversions(E->getSubExpr(), SrcTy);
380
381 // If the destination is void, just evaluate the source.
382 if (E->getType()->isVoidType())
383 return RValue::getAggregate(0);
384
Chris Lattnercf106ab2007-06-06 04:05:39 +0000385 return EmitConversion(Src, SrcTy, E->getType(), E->getLParenLoc());
Chris Lattner8394d792007-06-05 20:53:16 +0000386}
Chris Lattnerf0106d22007-06-02 19:33:17 +0000387
Chris Lattner2b228c92007-06-15 21:34:29 +0000388RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
389 QualType Ty;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000390 llvm::Value *Callee =
391 EmitExprWithUsualUnaryConversions(E->getCallee(), Ty).getVal();
Chris Lattner2b228c92007-06-15 21:34:29 +0000392
Chris Lattner23b7eb62007-06-15 23:05:46 +0000393 llvm::SmallVector<llvm::Value*, 16> Args;
Chris Lattner2b228c92007-06-15 21:34:29 +0000394
395 // FIXME: Handle struct return.
396 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
397 RValue ArgVal = EmitExprWithUsualUnaryConversions(E->getArg(i), Ty);
398
399 if (ArgVal.isScalar())
400 Args.push_back(ArgVal.getVal());
401 else // Pass by-address. FIXME: Set attribute bit on call.
402 Args.push_back(ArgVal.getAggregateVal());
403 }
404
Chris Lattner23b7eb62007-06-15 23:05:46 +0000405 llvm::Value *V = Builder.CreateCall(Callee, &Args[0], Args.size());
Chris Lattner2b228c92007-06-15 21:34:29 +0000406 if (V->getType() != llvm::Type::VoidTy)
407 V->setName("call");
408
409 // FIXME: Struct return;
410 return RValue::get(V);
411}
412
413
Chris Lattner8394d792007-06-05 20:53:16 +0000414//===----------------------------------------------------------------------===//
415// Unary Operator Emission
416//===----------------------------------------------------------------------===//
417
418RValue CodeGenFunction::EmitExprWithUsualUnaryConversions(const Expr *E,
419 QualType &ResTy) {
Chris Lattner6db1fb82007-06-02 22:49:07 +0000420 ResTy = E->getType().getCanonicalType();
421
422 if (isa<FunctionType>(ResTy)) { // C99 6.3.2.1p4
423 // Functions are promoted to their address.
424 ResTy = getContext().getPointerType(ResTy);
Chris Lattner8394d792007-06-05 20:53:16 +0000425 return RValue::get(EmitLValue(E).getAddress());
Chris Lattner6db1fb82007-06-02 22:49:07 +0000426 } else if (const ArrayType *ary = dyn_cast<ArrayType>(ResTy)) {
427 // C99 6.3.2.1p3
428 ResTy = getContext().getPointerType(ary->getElementType());
429
430 // FIXME: For now we assume that all source arrays map to LLVM arrays. This
431 // will not true when we add support for VLAs.
432 llvm::Value *V = EmitLValue(E).getAddress(); // Bitfields can't be arrays.
433
434 assert(isa<llvm::PointerType>(V->getType()) &&
435 isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
436 ->getElementType()) &&
437 "Doesn't support VLAs yet!");
438 llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
Chris Lattner8394d792007-06-05 20:53:16 +0000439 return RValue::get(Builder.CreateGEP(V, Idx0, Idx0, "arraydecay"));
Chris Lattner6db1fb82007-06-02 22:49:07 +0000440 } else if (ResTy->isPromotableIntegerType()) { // C99 6.3.1.1p2
441 // FIXME: this probably isn't right, pending clarification from Steve.
442 llvm::Value *Val = EmitExpr(E).getVal();
443
Chris Lattner6db1fb82007-06-02 22:49:07 +0000444 // If the input is a signed integer, sign extend to the destination.
445 if (ResTy->isSignedIntegerType()) {
446 Val = Builder.CreateSExt(Val, LLVMIntTy, "promote");
447 } else {
448 // This handles unsigned types, including bool.
449 Val = Builder.CreateZExt(Val, LLVMIntTy, "promote");
450 }
451 ResTy = getContext().IntTy;
452
Chris Lattner8394d792007-06-05 20:53:16 +0000453 return RValue::get(Val);
Chris Lattner6db1fb82007-06-02 22:49:07 +0000454 }
455
456 // Otherwise, this is a float, double, int, struct, etc.
457 return EmitExpr(E);
458}
459
460
Chris Lattner8394d792007-06-05 20:53:16 +0000461RValue CodeGenFunction::EmitUnaryOperator(const UnaryOperator *E) {
Chris Lattnerf0106d22007-06-02 19:33:17 +0000462 switch (E->getOpcode()) {
463 default:
464 printf("Unimplemented unary expr!\n");
465 E->dump();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000466 return RValue::get(llvm::UndefValue::get(llvm::Type::Int32Ty));
Chris Lattner8394d792007-06-05 20:53:16 +0000467 // FIXME: pre/post inc/dec
468 case UnaryOperator::AddrOf: return EmitUnaryAddrOf(E);
469 case UnaryOperator::Deref : return EmitLoadOfLValue(E);
470 case UnaryOperator::Plus : return EmitUnaryPlus(E);
471 case UnaryOperator::Minus : return EmitUnaryMinus(E);
472 case UnaryOperator::Not : return EmitUnaryNot(E);
473 case UnaryOperator::LNot : return EmitUnaryLNot(E);
474 // FIXME: SIZEOF/ALIGNOF(expr).
475 // FIXME: real/imag
476 case UnaryOperator::Extension: return EmitExpr(E->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000477 }
478}
479
Chris Lattner8394d792007-06-05 20:53:16 +0000480/// C99 6.5.3.2
481RValue CodeGenFunction::EmitUnaryAddrOf(const UnaryOperator *E) {
482 // The address of the operand is just its lvalue. It cannot be a bitfield.
483 return RValue::get(EmitLValue(E->getSubExpr()).getAddress());
484}
485
486RValue CodeGenFunction::EmitUnaryPlus(const UnaryOperator *E) {
487 // Unary plus just performs promotions on its arithmetic operand.
488 QualType Ty;
Chris Lattnerb48238182007-06-15 21:04:38 +0000489 return EmitExprWithUsualUnaryConversions(E->getSubExpr(), Ty);
Chris Lattner8394d792007-06-05 20:53:16 +0000490}
491
492RValue CodeGenFunction::EmitUnaryMinus(const UnaryOperator *E) {
493 // Unary minus performs promotions, then negates its arithmetic operand.
494 QualType Ty;
Chris Lattnerb48238182007-06-15 21:04:38 +0000495 RValue V = EmitExprWithUsualUnaryConversions(E->getSubExpr(), Ty);
Chris Lattnerf0106d22007-06-02 19:33:17 +0000496
Chris Lattner8394d792007-06-05 20:53:16 +0000497 if (V.isScalar())
498 return RValue::get(Builder.CreateNeg(V.getVal(), "neg"));
499
500 assert(0 && "FIXME: This doesn't handle complex operands yet");
501}
502
503RValue CodeGenFunction::EmitUnaryNot(const UnaryOperator *E) {
504 // Unary not performs promotions, then complements its integer operand.
505 QualType Ty;
Chris Lattnerb48238182007-06-15 21:04:38 +0000506 RValue V = EmitExprWithUsualUnaryConversions(E->getSubExpr(), Ty);
Chris Lattner8394d792007-06-05 20:53:16 +0000507
508 if (V.isScalar())
509 return RValue::get(Builder.CreateNot(V.getVal(), "neg"));
510
511 assert(0 && "FIXME: This doesn't handle integer complex operands yet (GNU)");
512}
513
514
515/// C99 6.5.3.3
516RValue CodeGenFunction::EmitUnaryLNot(const UnaryOperator *E) {
517 // Compare operand to zero.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000518 llvm::Value *BoolVal = EvaluateExprAsBool(E->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000519
520 // Invert value.
Chris Lattnera45c5af2007-06-02 19:47:04 +0000521 // TODO: Could dynamically modify easy computations here. For example, if
522 // the operand is an icmp ne, turn into icmp eq.
Chris Lattnerf0106d22007-06-02 19:33:17 +0000523 BoolVal = Builder.CreateNot(BoolVal, "lnot");
524
525 // ZExt result to int.
Chris Lattner8394d792007-06-05 20:53:16 +0000526 return RValue::get(Builder.CreateZExt(BoolVal, LLVMIntTy, "lnot.ext"));
Chris Lattnerf0106d22007-06-02 19:33:17 +0000527}
528
Chris Lattnere47e4402007-06-01 18:02:12 +0000529
Chris Lattnerdb91b162007-06-02 00:16:28 +0000530//===--------------------------------------------------------------------===//
531// Binary Operator Emission
532//===--------------------------------------------------------------------===//
533
534// FIXME describe.
Chris Lattnercf250242007-06-03 02:02:44 +0000535QualType CodeGenFunction::
Chris Lattner8394d792007-06-05 20:53:16 +0000536EmitUsualArithmeticConversions(const BinaryOperator *E, RValue &LHS,
537 RValue &RHS) {
Chris Lattnerc18f9d12007-06-02 22:51:30 +0000538 QualType LHSType, RHSType;
539 LHS = EmitExprWithUsualUnaryConversions(E->getLHS(), LHSType);
540 RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), RHSType);
541
Chris Lattnercf250242007-06-03 02:02:44 +0000542 // If both operands have the same source type, we're done already.
543 if (LHSType == RHSType) return LHSType;
544
545 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
546 // The caller can deal with this (e.g. pointer + int).
547 if (!LHSType->isArithmeticType() || !RHSType->isArithmeticType())
548 return LHSType;
549
550 // At this point, we have two different arithmetic types.
551
552 // Handle complex types first (C99 6.3.1.8p1).
553 if (LHSType->isComplexType() || RHSType->isComplexType()) {
554 assert(0 && "FIXME: complex types unimp");
555#if 0
556 // if we have an integer operand, the result is the complex type.
557 if (rhs->isIntegerType())
558 return lhs;
559 if (lhs->isIntegerType())
560 return rhs;
561 return Context.maxComplexType(lhs, rhs);
562#endif
563 }
564
565 // If neither operand is complex, they must be scalars.
566 llvm::Value *LHSV = LHS.getVal();
567 llvm::Value *RHSV = RHS.getVal();
568
569 // If the LLVM types are already equal, then they only differed in sign, or it
570 // was something like char/signed char or double/long double.
571 if (LHSV->getType() == RHSV->getType())
572 return LHSType;
573
574 // Now handle "real" floating types (i.e. float, double, long double).
575 if (LHSType->isRealFloatingType() || RHSType->isRealFloatingType()) {
576 // if we have an integer operand, the result is the real floating type, and
577 // the integer converts to FP.
578 if (RHSType->isIntegerType()) {
579 // Promote the RHS to an FP type of the LHS, with the sign following the
580 // RHS.
581 if (RHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000582 RHS = RValue::get(Builder.CreateSIToFP(RHSV,LHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000583 else
Chris Lattner8394d792007-06-05 20:53:16 +0000584 RHS = RValue::get(Builder.CreateUIToFP(RHSV,LHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000585 return LHSType;
586 }
587
588 if (LHSType->isIntegerType()) {
589 // Promote the LHS to an FP type of the RHS, with the sign following the
590 // LHS.
591 if (LHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000592 LHS = RValue::get(Builder.CreateSIToFP(LHSV,RHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000593 else
Chris Lattner8394d792007-06-05 20:53:16 +0000594 LHS = RValue::get(Builder.CreateUIToFP(LHSV,RHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000595 return RHSType;
596 }
597
598 // Otherwise, they are two FP types. Promote the smaller operand to the
599 // bigger result.
600 QualType BiggerType = ASTContext::maxFloatingType(LHSType, RHSType);
601
602 if (BiggerType == LHSType)
Chris Lattner8394d792007-06-05 20:53:16 +0000603 RHS = RValue::get(Builder.CreateFPExt(RHSV, LHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000604 else
Chris Lattner8394d792007-06-05 20:53:16 +0000605 LHS = RValue::get(Builder.CreateFPExt(LHSV, RHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000606 return BiggerType;
607 }
608
609 // Finally, we have two integer types that are different according to C. Do
610 // a sign or zero extension if needed.
611
612 // Otherwise, one type is smaller than the other.
613 QualType ResTy = ASTContext::maxIntegerType(LHSType, RHSType);
614
615 if (LHSType == ResTy) {
616 if (RHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000617 RHS = RValue::get(Builder.CreateSExt(RHSV, LHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000618 else
Chris Lattner8394d792007-06-05 20:53:16 +0000619 RHS = RValue::get(Builder.CreateZExt(RHSV, LHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000620 } else {
621 assert(RHSType == ResTy && "Unknown conversion");
622 if (LHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000623 LHS = RValue::get(Builder.CreateSExt(LHSV, RHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000624 else
Chris Lattner8394d792007-06-05 20:53:16 +0000625 LHS = RValue::get(Builder.CreateZExt(LHSV, RHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000626 }
627 return ResTy;
Chris Lattnerdb91b162007-06-02 00:16:28 +0000628}
629
630
Chris Lattner8394d792007-06-05 20:53:16 +0000631RValue CodeGenFunction::EmitBinaryOperator(const BinaryOperator *E) {
Chris Lattnerdb91b162007-06-02 00:16:28 +0000632 switch (E->getOpcode()) {
633 default:
Chris Lattner8394d792007-06-05 20:53:16 +0000634 fprintf(stderr, "Unimplemented expr!\n");
Chris Lattnerdb91b162007-06-02 00:16:28 +0000635 E->dump();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000636 return RValue::get(llvm::UndefValue::get(llvm::Type::Int32Ty));
Chris Lattner8394d792007-06-05 20:53:16 +0000637 case BinaryOperator::Mul: return EmitBinaryMul(E);
638 case BinaryOperator::Div: return EmitBinaryDiv(E);
639 case BinaryOperator::Rem: return EmitBinaryRem(E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000640 case BinaryOperator::Add: return EmitBinaryAdd(E);
Chris Lattner8394d792007-06-05 20:53:16 +0000641 case BinaryOperator::Sub: return EmitBinarySub(E);
642 case BinaryOperator::Shl: return EmitBinaryShl(E);
643 case BinaryOperator::Shr: return EmitBinaryShr(E);
644
645 // FIXME: relational
646
647 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);
652
653 case BinaryOperator::Assign: return EmitBinaryAssign(E);
654 // FIXME: Assignment.
655 case BinaryOperator::Comma: return EmitBinaryComma(E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000656 }
657}
658
Chris Lattner8394d792007-06-05 20:53:16 +0000659RValue CodeGenFunction::EmitBinaryMul(const BinaryOperator *E) {
660 RValue LHS, RHS;
661 EmitUsualArithmeticConversions(E, LHS, RHS);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000662
Chris Lattner8394d792007-06-05 20:53:16 +0000663 if (LHS.isScalar())
664 return RValue::get(Builder.CreateMul(LHS.getVal(), RHS.getVal(), "mul"));
665
666 assert(0 && "FIXME: This doesn't handle complex operands yet");
667}
668
669RValue CodeGenFunction::EmitBinaryDiv(const BinaryOperator *E) {
670 RValue LHS, RHS;
671 EmitUsualArithmeticConversions(E, LHS, RHS);
672
673 if (LHS.isScalar()) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000674 llvm::Value *RV;
Chris Lattner8394d792007-06-05 20:53:16 +0000675 if (LHS.getVal()->getType()->isFloatingPoint())
676 RV = Builder.CreateFDiv(LHS.getVal(), RHS.getVal(), "div");
677 else if (E->getType()->isUnsignedIntegerType())
678 RV = Builder.CreateUDiv(LHS.getVal(), RHS.getVal(), "div");
679 else
680 RV = Builder.CreateSDiv(LHS.getVal(), RHS.getVal(), "div");
681 return RValue::get(RV);
682 }
683 assert(0 && "FIXME: This doesn't handle complex operands yet");
684}
685
686RValue CodeGenFunction::EmitBinaryRem(const BinaryOperator *E) {
687 RValue LHS, RHS;
688 EmitUsualArithmeticConversions(E, LHS, RHS);
689
690 if (LHS.isScalar()) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000691 llvm::Value *RV;
Chris Lattner8394d792007-06-05 20:53:16 +0000692 // Rem in C can't be a floating point type: C99 6.5.5p2.
693 if (E->getType()->isUnsignedIntegerType())
694 RV = Builder.CreateURem(LHS.getVal(), RHS.getVal(), "rem");
695 else
696 RV = Builder.CreateSRem(LHS.getVal(), RHS.getVal(), "rem");
697 return RValue::get(RV);
698 }
699
700 assert(0 && "FIXME: This doesn't handle complex operands yet");
701}
702
703RValue CodeGenFunction::EmitBinaryAdd(const BinaryOperator *E) {
704 RValue LHS, RHS;
Chris Lattnerdb91b162007-06-02 00:16:28 +0000705 EmitUsualArithmeticConversions(E, LHS, RHS);
706
Chris Lattner8394d792007-06-05 20:53:16 +0000707 // FIXME: This doesn't handle ptr+int etc yet.
708
709 if (LHS.isScalar())
710 return RValue::get(Builder.CreateAdd(LHS.getVal(), RHS.getVal(), "add"));
711
712 assert(0 && "FIXME: This doesn't handle complex operands yet");
713
714}
715
716RValue CodeGenFunction::EmitBinarySub(const BinaryOperator *E) {
717 RValue LHS, RHS;
718 EmitUsualArithmeticConversions(E, LHS, RHS);
719
720 // FIXME: This doesn't handle ptr-int or ptr-ptr, etc yet.
721
722 if (LHS.isScalar())
723 return RValue::get(Builder.CreateSub(LHS.getVal(), RHS.getVal(), "sub"));
724
725 assert(0 && "FIXME: This doesn't handle complex operands yet");
726
727}
728
729RValue CodeGenFunction::EmitBinaryShl(const BinaryOperator *E) {
730 // For shifts, integer promotions are performed, but the usual arithmetic
731 // conversions are not. The LHS and RHS need not have the same type.
732
733 QualType ResTy;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000734 llvm::Value *LHS =
735 EmitExprWithUsualUnaryConversions(E->getLHS(), ResTy).getVal();
736 llvm::Value *RHS =
737 EmitExprWithUsualUnaryConversions(E->getRHS(), ResTy).getVal();
Chris Lattner8394d792007-06-05 20:53:16 +0000738
739 // LLVM requires the LHS and RHS to be the same type, promote or truncate the
740 // RHS to the same size as the LHS.
741 if (LHS->getType() != RHS->getType())
742 RHS = Builder.CreateIntCast(RHS, LHS->getType(), false, "sh_prom");
743
744 return RValue::get(Builder.CreateShl(LHS, RHS, "shl"));
745}
746
747RValue CodeGenFunction::EmitBinaryShr(const BinaryOperator *E) {
748 // For shifts, integer promotions are performed, but the usual arithmetic
749 // conversions are not. The LHS and RHS need not have the same type.
750
751 QualType ResTy;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000752 llvm::Value *LHS =
753 EmitExprWithUsualUnaryConversions(E->getLHS(), ResTy).getVal();
754 llvm::Value *RHS =
755 EmitExprWithUsualUnaryConversions(E->getRHS(), ResTy).getVal();
Chris Lattner8394d792007-06-05 20:53:16 +0000756
757 // LLVM requires the LHS and RHS to be the same type, promote or truncate the
758 // RHS to the same size as the LHS.
759 if (LHS->getType() != RHS->getType())
760 RHS = Builder.CreateIntCast(RHS, LHS->getType(), false, "sh_prom");
761
762 if (E->getType()->isUnsignedIntegerType())
763 return RValue::get(Builder.CreateLShr(LHS, RHS, "shr"));
764 else
765 return RValue::get(Builder.CreateAShr(LHS, RHS, "shr"));
766}
767
768RValue CodeGenFunction::EmitBinaryAnd(const BinaryOperator *E) {
769 RValue LHS, RHS;
770 EmitUsualArithmeticConversions(E, LHS, RHS);
771
772 if (LHS.isScalar())
773 return RValue::get(Builder.CreateAnd(LHS.getVal(), RHS.getVal(), "and"));
774
775 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
776}
777
778RValue CodeGenFunction::EmitBinaryXor(const BinaryOperator *E) {
779 RValue LHS, RHS;
780 EmitUsualArithmeticConversions(E, LHS, RHS);
781
782 if (LHS.isScalar())
783 return RValue::get(Builder.CreateXor(LHS.getVal(), RHS.getVal(), "xor"));
784
785 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
786}
787
788RValue CodeGenFunction::EmitBinaryOr(const BinaryOperator *E) {
789 RValue LHS, RHS;
790 EmitUsualArithmeticConversions(E, LHS, RHS);
791
792 if (LHS.isScalar())
793 return RValue::get(Builder.CreateOr(LHS.getVal(), RHS.getVal(), "or"));
794
795 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
796}
797
798RValue CodeGenFunction::EmitBinaryLAnd(const BinaryOperator *E) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000799 llvm::Value *LHSCond = EvaluateExprAsBool(E->getLHS());
Chris Lattner8394d792007-06-05 20:53:16 +0000800
Chris Lattner23b7eb62007-06-15 23:05:46 +0000801 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("land_cont");
802 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("land_rhs");
Chris Lattner8394d792007-06-05 20:53:16 +0000803
Chris Lattner23b7eb62007-06-15 23:05:46 +0000804 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
Chris Lattner8394d792007-06-05 20:53:16 +0000805 Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock);
806
807 EmitBlock(RHSBlock);
Chris Lattner23b7eb62007-06-15 23:05:46 +0000808 llvm::Value *RHSCond = EvaluateExprAsBool(E->getRHS());
Chris Lattner8394d792007-06-05 20:53:16 +0000809
810 // Reaquire the RHS block, as there may be subblocks inserted.
811 RHSBlock = Builder.GetInsertBlock();
812 EmitBlock(ContBlock);
813
814 // Create a PHI node. If we just evaluted the LHS condition, the result is
815 // false. If we evaluated both, the result is the RHS condition.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000816 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land");
Chris Lattner8394d792007-06-05 20:53:16 +0000817 PN->reserveOperandSpace(2);
Chris Lattner23b7eb62007-06-15 23:05:46 +0000818 PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock);
Chris Lattner8394d792007-06-05 20:53:16 +0000819 PN->addIncoming(RHSCond, RHSBlock);
820
821 // ZExt result to int.
822 return RValue::get(Builder.CreateZExt(PN, LLVMIntTy, "land.ext"));
823}
824
825RValue CodeGenFunction::EmitBinaryLOr(const BinaryOperator *E) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000826 llvm::Value *LHSCond = EvaluateExprAsBool(E->getLHS());
Chris Lattner8394d792007-06-05 20:53:16 +0000827
Chris Lattner23b7eb62007-06-15 23:05:46 +0000828 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("lor_cont");
829 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("lor_rhs");
Chris Lattner8394d792007-06-05 20:53:16 +0000830
Chris Lattner23b7eb62007-06-15 23:05:46 +0000831 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
Chris Lattner8394d792007-06-05 20:53:16 +0000832 Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock);
833
834 EmitBlock(RHSBlock);
Chris Lattner23b7eb62007-06-15 23:05:46 +0000835 llvm::Value *RHSCond = EvaluateExprAsBool(E->getRHS());
Chris Lattner8394d792007-06-05 20:53:16 +0000836
837 // Reaquire the RHS block, as there may be subblocks inserted.
838 RHSBlock = Builder.GetInsertBlock();
839 EmitBlock(ContBlock);
840
841 // Create a PHI node. If we just evaluted the LHS condition, the result is
842 // true. If we evaluated both, the result is the RHS condition.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000843 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor");
Chris Lattner8394d792007-06-05 20:53:16 +0000844 PN->reserveOperandSpace(2);
Chris Lattner23b7eb62007-06-15 23:05:46 +0000845 PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock);
Chris Lattner8394d792007-06-05 20:53:16 +0000846 PN->addIncoming(RHSCond, RHSBlock);
847
848 // ZExt result to int.
849 return RValue::get(Builder.CreateZExt(PN, LLVMIntTy, "lor.ext"));
850}
851
852RValue CodeGenFunction::EmitBinaryAssign(const BinaryOperator *E) {
853 LValue LHS = EmitLValue(E->getLHS());
854
855 QualType RHSTy;
856 RValue RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), RHSTy);
857
858 // Convert the RHS to the type of the LHS.
Chris Lattnercf106ab2007-06-06 04:05:39 +0000859 // FIXME: I'm not thrilled about having to call getLocStart() here... :(
860 RHS = EmitConversion(RHS, RHSTy, E->getType(), E->getLocStart());
Chris Lattner8394d792007-06-05 20:53:16 +0000861
862 // Store the value into the LHS.
863 EmitStoreThroughLValue(RHS, LHS, E->getType());
864
865 // Return the converted RHS.
866 return RHS;
867}
868
869
870RValue CodeGenFunction::EmitBinaryComma(const BinaryOperator *E) {
871 EmitExpr(E->getLHS());
872 return EmitExpr(E->getRHS());
873}