blob: a8ad1f47e93013d6704ac3be321ccddf1e6c9c4f [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 Lattnerf033c142007-06-22 19:05:19 +000044 QualType DstTy) {
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)) {
Chris Lattnerf033c142007-06-22 19:05:19 +000057 const llvm::Type *DestTy = ConvertType(DstTy);
Chris Lattnercf106ab2007-06-06 04:05:39 +000058
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.
Chris Lattnerf033c142007-06-22 19:05:19 +000069 const llvm::Type *DestTy = ConvertType(DstTy);
Chris Lattnercf106ab2007-06-06 04:05:39 +000070 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 Lattnerf033c142007-06-22 19:05:19 +000081 const llvm::Type *DestTy = ConvertType(DstTy);
Chris Lattner83b484b2007-06-06 04:39:08 +000082 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());
Chris Lattner09153c02007-06-22 18:48:09 +0000234 llvm::Value *Ptr = LV.getAddress();
235 const llvm::Type *EltTy =
236 cast<llvm::PointerType>(Ptr->getType())->getElementType();
237
238 // Simple scalar l-value.
239 if (EltTy->isFirstClassType())
240 return RValue::get(Builder.CreateLoad(Ptr, "tmp"));
241
242 // Otherwise, we have an aggregate lvalue.
243 return RValue::getAggregate(Ptr);
Chris Lattner8394d792007-06-05 20:53:16 +0000244}
245
246/// EmitStoreThroughLValue - Store the specified rvalue into the specified
247/// lvalue, where both are guaranteed to the have the same type, and that type
248/// is 'Ty'.
249void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
250 QualType Ty) {
Chris Lattner8394d792007-06-05 20:53:16 +0000251 assert(!Dst.isBitfield() && "FIXME: Don't support store to bitfield yet");
Chris Lattner8394d792007-06-05 20:53:16 +0000252
Chris Lattner09153c02007-06-22 18:48:09 +0000253 llvm::Value *DstAddr = Dst.getAddress();
254 if (Src.isScalar()) {
255 // FIXME: Handle volatility etc.
256 const llvm::Type *SrcTy = Src.getVal()->getType();
257 const llvm::Type *AddrTy =
258 cast<llvm::PointerType>(DstAddr->getType())->getElementType();
259
260 if (AddrTy != SrcTy)
261 DstAddr = Builder.CreateBitCast(DstAddr, llvm::PointerType::get(SrcTy),
262 "storetmp");
263 Builder.CreateStore(Src.getVal(), DstAddr);
264 return;
265 }
Chris Lattner8394d792007-06-05 20:53:16 +0000266
Chris Lattner09153c02007-06-22 18:48:09 +0000267 // Aggregate assignment turns into llvm.memcpy.
268 const llvm::Type *SBP = llvm::PointerType::get(llvm::Type::Int8Ty);
269 llvm::Value *SrcAddr = Src.getAggregateAddr();
270
271 if (DstAddr->getType() != SBP)
272 DstAddr = Builder.CreateBitCast(DstAddr, SBP, "tmp");
273 if (SrcAddr->getType() != SBP)
274 SrcAddr = Builder.CreateBitCast(SrcAddr, SBP, "tmp");
275
276 unsigned Align = 1; // FIXME: Compute type alignments.
277 unsigned Size = 1234; // FIXME: Compute type sizes.
278
279 // FIXME: Handle variable sized types.
280 const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
281 llvm::Value *SizeVal = llvm::ConstantInt::get(IntPtr, Size);
282
283 llvm::Value *MemCpyOps[4] = {
284 DstAddr, SrcAddr, SizeVal,llvm::ConstantInt::get(llvm::Type::Int32Ty, Align)
285 };
286
287 Builder.CreateCall(CGM.getMemCpyFn(), MemCpyOps, 4);
Chris Lattner8394d792007-06-05 20:53:16 +0000288}
289
Chris Lattnerd7f58862007-06-02 05:24:33 +0000290
291LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
292 const Decl *D = E->getDecl();
Chris Lattner53621a52007-06-13 20:44:40 +0000293 if (isa<BlockVarDecl>(D) || isa<ParmVarDecl>(D)) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000294 llvm::Value *V = LocalDeclMap[D];
Chris Lattnerd7f58862007-06-02 05:24:33 +0000295 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
296 return LValue::getAddr(V);
Chris Lattnerb6984c42007-06-20 04:44:43 +0000297 } else if (isa<FunctionDecl>(D) || isa<FileVarDecl>(D)) {
298 return LValue::getAddr(CGM.GetAddrOfGlobalDecl(D));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000299 }
300 assert(0 && "Unimp declref");
301}
Chris Lattnere47e4402007-06-01 18:02:12 +0000302
Chris Lattner8394d792007-06-05 20:53:16 +0000303LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
304 // __extension__ doesn't affect lvalue-ness.
305 if (E->getOpcode() == UnaryOperator::Extension)
306 return EmitLValue(E->getSubExpr());
307
308 assert(E->getOpcode() == UnaryOperator::Deref &&
309 "'*' is the only unary operator that produces an lvalue");
310 return LValue::getAddr(EmitExpr(E->getSubExpr()).getVal());
311}
312
Chris Lattner4347e3692007-06-06 04:54:52 +0000313LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
314 assert(!E->isWide() && "FIXME: Wide strings not supported yet!");
315 const char *StrData = E->getStrData();
316 unsigned Len = E->getByteLength();
317
318 // FIXME: Can cache/reuse these within the module.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000319 llvm::Constant *C=llvm::ConstantArray::get(std::string(StrData, StrData+Len));
Chris Lattner4347e3692007-06-06 04:54:52 +0000320
321 // Create a global variable for this.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000322 C = new llvm::GlobalVariable(C->getType(), true,
323 llvm::GlobalValue::InternalLinkage,
Chris Lattner4347e3692007-06-06 04:54:52 +0000324 C, ".str", CurFn->getParent());
Chris Lattner23b7eb62007-06-15 23:05:46 +0000325 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
326 llvm::Constant *Zeros[] = { Zero, Zero };
327 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
Chris Lattner4347e3692007-06-06 04:54:52 +0000328 return LValue::getAddr(C);
329}
330
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000331LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
332 // The base and index must be pointers or integers, neither of which are
333 // aggregates. Emit them.
334 QualType BaseTy;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000335 llvm::Value *Base =
336 EmitExprWithUsualUnaryConversions(E->getBase(), BaseTy).getVal();
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000337 QualType IdxTy;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000338 llvm::Value *Idx =
339 EmitExprWithUsualUnaryConversions(E->getIdx(), IdxTy).getVal();
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000340
341 // Usually the base is the pointer type, but sometimes it is the index.
342 // Canonicalize to have the pointer as the base.
343 if (isa<llvm::PointerType>(Idx->getType())) {
344 std::swap(Base, Idx);
345 std::swap(BaseTy, IdxTy);
346 }
347
348 // The pointer is now the base. Extend or truncate the index type to 32 or
349 // 64-bits.
350 bool IdxSigned = IdxTy->isSignedIntegerType();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000351 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000352 if (IdxBitwidth != LLVMPointerWidth)
Chris Lattner23b7eb62007-06-15 23:05:46 +0000353 Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth),
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000354 IdxSigned, "idxprom");
355
356 // We know that the pointer points to a type of the correct size, unless the
357 // size is a VLA.
358 if (!E->getType()->isConstantSizeType())
359 assert(0 && "VLA idx not implemented");
360 return LValue::getAddr(Builder.CreateGEP(Base, Idx, "arrayidx"));
361}
362
Chris Lattnere47e4402007-06-01 18:02:12 +0000363//===--------------------------------------------------------------------===//
364// Expression Emission
365//===--------------------------------------------------------------------===//
366
Chris Lattner8394d792007-06-05 20:53:16 +0000367RValue CodeGenFunction::EmitExpr(const Expr *E) {
Chris Lattnere47e4402007-06-01 18:02:12 +0000368 assert(E && "Null expression?");
369
370 switch (E->getStmtClass()) {
371 default:
Chris Lattner1fde0b32007-06-20 18:30:55 +0000372 fprintf(stderr, "Unimplemented expr!\n");
Chris Lattnere47e4402007-06-01 18:02:12 +0000373 E->dump();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000374 return RValue::get(llvm::UndefValue::get(llvm::Type::Int32Ty));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000375
376 // l-values.
Chris Lattner8394d792007-06-05 20:53:16 +0000377 case Expr::DeclRefExprClass:
Chris Lattnerf99b3f52007-06-11 03:52:52 +0000378 // DeclRef's of EnumConstantDecl's are simple rvalues.
379 if (const EnumConstantDecl *EC =
380 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
Chris Lattner23b7eb62007-06-15 23:05:46 +0000381 return RValue::get(llvm::ConstantInt::get(EC->getInitVal()));
Chris Lattnerf99b3f52007-06-11 03:52:52 +0000382
383 // FALLTHROUGH
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000384 case Expr::ArraySubscriptExprClass:
Chris Lattner8394d792007-06-05 20:53:16 +0000385 return EmitLoadOfLValue(E);
Chris Lattner4347e3692007-06-06 04:54:52 +0000386 case Expr::StringLiteralClass:
387 return RValue::get(EmitLValue(E).getAddress());
Chris Lattnerd7f58862007-06-02 05:24:33 +0000388
389 // Leaf expressions.
390 case Expr::IntegerLiteralClass:
Chris Lattnere47e4402007-06-01 18:02:12 +0000391 return EmitIntegerLiteral(cast<IntegerLiteral>(E));
Chris Lattnerdb91b162007-06-02 00:16:28 +0000392
Chris Lattnerd7f58862007-06-02 05:24:33 +0000393 // Operators.
394 case Expr::ParenExprClass:
395 return EmitExpr(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000396 case Expr::UnaryOperatorClass:
397 return EmitUnaryOperator(cast<UnaryOperator>(E));
Chris Lattner8394d792007-06-05 20:53:16 +0000398 case Expr::CastExprClass:
399 return EmitCastExpr(cast<CastExpr>(E));
Chris Lattner2b228c92007-06-15 21:34:29 +0000400 case Expr::CallExprClass:
401 return EmitCallExpr(cast<CallExpr>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000402 case Expr::BinaryOperatorClass:
Chris Lattnerdb91b162007-06-02 00:16:28 +0000403 return EmitBinaryOperator(cast<BinaryOperator>(E));
Chris Lattnere47e4402007-06-01 18:02:12 +0000404 }
405
406}
407
Chris Lattner8394d792007-06-05 20:53:16 +0000408RValue CodeGenFunction::EmitIntegerLiteral(const IntegerLiteral *E) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000409 return RValue::get(llvm::ConstantInt::get(E->getValue()));
Chris Lattnere47e4402007-06-01 18:02:12 +0000410}
411
Chris Lattner8394d792007-06-05 20:53:16 +0000412RValue CodeGenFunction::EmitCastExpr(const CastExpr *E) {
413 QualType SrcTy;
414 RValue Src = EmitExprWithUsualUnaryConversions(E->getSubExpr(), SrcTy);
415
416 // If the destination is void, just evaluate the source.
417 if (E->getType()->isVoidType())
418 return RValue::getAggregate(0);
419
Chris Lattnerf033c142007-06-22 19:05:19 +0000420 return EmitConversion(Src, SrcTy, E->getType());
Chris Lattner8394d792007-06-05 20:53:16 +0000421}
Chris Lattnerf0106d22007-06-02 19:33:17 +0000422
Chris Lattner2b228c92007-06-15 21:34:29 +0000423RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
424 QualType Ty;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000425 llvm::Value *Callee =
426 EmitExprWithUsualUnaryConversions(E->getCallee(), Ty).getVal();
Chris Lattner2b228c92007-06-15 21:34:29 +0000427
Chris Lattner23b7eb62007-06-15 23:05:46 +0000428 llvm::SmallVector<llvm::Value*, 16> Args;
Chris Lattner2b228c92007-06-15 21:34:29 +0000429
430 // FIXME: Handle struct return.
431 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
432 RValue ArgVal = EmitExprWithUsualUnaryConversions(E->getArg(i), Ty);
433
434 if (ArgVal.isScalar())
435 Args.push_back(ArgVal.getVal());
436 else // Pass by-address. FIXME: Set attribute bit on call.
Chris Lattner09153c02007-06-22 18:48:09 +0000437 Args.push_back(ArgVal.getAggregateAddr());
Chris Lattner2b228c92007-06-15 21:34:29 +0000438 }
439
Chris Lattner23b7eb62007-06-15 23:05:46 +0000440 llvm::Value *V = Builder.CreateCall(Callee, &Args[0], Args.size());
Chris Lattner2b228c92007-06-15 21:34:29 +0000441 if (V->getType() != llvm::Type::VoidTy)
442 V->setName("call");
443
444 // FIXME: Struct return;
445 return RValue::get(V);
446}
447
448
Chris Lattner8394d792007-06-05 20:53:16 +0000449//===----------------------------------------------------------------------===//
450// Unary Operator Emission
451//===----------------------------------------------------------------------===//
452
453RValue CodeGenFunction::EmitExprWithUsualUnaryConversions(const Expr *E,
454 QualType &ResTy) {
Chris Lattner6db1fb82007-06-02 22:49:07 +0000455 ResTy = E->getType().getCanonicalType();
456
457 if (isa<FunctionType>(ResTy)) { // C99 6.3.2.1p4
458 // Functions are promoted to their address.
459 ResTy = getContext().getPointerType(ResTy);
Chris Lattner8394d792007-06-05 20:53:16 +0000460 return RValue::get(EmitLValue(E).getAddress());
Chris Lattner6db1fb82007-06-02 22:49:07 +0000461 } else if (const ArrayType *ary = dyn_cast<ArrayType>(ResTy)) {
462 // C99 6.3.2.1p3
463 ResTy = getContext().getPointerType(ary->getElementType());
464
465 // FIXME: For now we assume that all source arrays map to LLVM arrays. This
466 // will not true when we add support for VLAs.
467 llvm::Value *V = EmitLValue(E).getAddress(); // Bitfields can't be arrays.
468
469 assert(isa<llvm::PointerType>(V->getType()) &&
470 isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
471 ->getElementType()) &&
472 "Doesn't support VLAs yet!");
473 llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
Chris Lattner8394d792007-06-05 20:53:16 +0000474 return RValue::get(Builder.CreateGEP(V, Idx0, Idx0, "arraydecay"));
Chris Lattner6db1fb82007-06-02 22:49:07 +0000475 } else if (ResTy->isPromotableIntegerType()) { // C99 6.3.1.1p2
476 // FIXME: this probably isn't right, pending clarification from Steve.
477 llvm::Value *Val = EmitExpr(E).getVal();
478
Chris Lattner6db1fb82007-06-02 22:49:07 +0000479 // If the input is a signed integer, sign extend to the destination.
480 if (ResTy->isSignedIntegerType()) {
481 Val = Builder.CreateSExt(Val, LLVMIntTy, "promote");
482 } else {
483 // This handles unsigned types, including bool.
484 Val = Builder.CreateZExt(Val, LLVMIntTy, "promote");
485 }
486 ResTy = getContext().IntTy;
487
Chris Lattner8394d792007-06-05 20:53:16 +0000488 return RValue::get(Val);
Chris Lattner6db1fb82007-06-02 22:49:07 +0000489 }
490
491 // Otherwise, this is a float, double, int, struct, etc.
492 return EmitExpr(E);
493}
494
495
Chris Lattner8394d792007-06-05 20:53:16 +0000496RValue CodeGenFunction::EmitUnaryOperator(const UnaryOperator *E) {
Chris Lattnerf0106d22007-06-02 19:33:17 +0000497 switch (E->getOpcode()) {
498 default:
499 printf("Unimplemented unary expr!\n");
500 E->dump();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000501 return RValue::get(llvm::UndefValue::get(llvm::Type::Int32Ty));
Chris Lattner8394d792007-06-05 20:53:16 +0000502 // FIXME: pre/post inc/dec
503 case UnaryOperator::AddrOf: return EmitUnaryAddrOf(E);
504 case UnaryOperator::Deref : return EmitLoadOfLValue(E);
505 case UnaryOperator::Plus : return EmitUnaryPlus(E);
506 case UnaryOperator::Minus : return EmitUnaryMinus(E);
507 case UnaryOperator::Not : return EmitUnaryNot(E);
508 case UnaryOperator::LNot : return EmitUnaryLNot(E);
509 // FIXME: SIZEOF/ALIGNOF(expr).
510 // FIXME: real/imag
511 case UnaryOperator::Extension: return EmitExpr(E->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000512 }
513}
514
Chris Lattner8394d792007-06-05 20:53:16 +0000515/// C99 6.5.3.2
516RValue CodeGenFunction::EmitUnaryAddrOf(const UnaryOperator *E) {
517 // The address of the operand is just its lvalue. It cannot be a bitfield.
518 return RValue::get(EmitLValue(E->getSubExpr()).getAddress());
519}
520
521RValue CodeGenFunction::EmitUnaryPlus(const UnaryOperator *E) {
522 // Unary plus just performs promotions on its arithmetic operand.
523 QualType Ty;
Chris Lattnerb48238182007-06-15 21:04:38 +0000524 return EmitExprWithUsualUnaryConversions(E->getSubExpr(), Ty);
Chris Lattner8394d792007-06-05 20:53:16 +0000525}
526
527RValue CodeGenFunction::EmitUnaryMinus(const UnaryOperator *E) {
528 // Unary minus performs promotions, then negates its arithmetic operand.
529 QualType Ty;
Chris Lattnerb48238182007-06-15 21:04:38 +0000530 RValue V = EmitExprWithUsualUnaryConversions(E->getSubExpr(), Ty);
Chris Lattnerf0106d22007-06-02 19:33:17 +0000531
Chris Lattner8394d792007-06-05 20:53:16 +0000532 if (V.isScalar())
533 return RValue::get(Builder.CreateNeg(V.getVal(), "neg"));
534
535 assert(0 && "FIXME: This doesn't handle complex operands yet");
536}
537
538RValue CodeGenFunction::EmitUnaryNot(const UnaryOperator *E) {
539 // Unary not performs promotions, then complements its integer operand.
540 QualType Ty;
Chris Lattnerb48238182007-06-15 21:04:38 +0000541 RValue V = EmitExprWithUsualUnaryConversions(E->getSubExpr(), Ty);
Chris Lattner8394d792007-06-05 20:53:16 +0000542
543 if (V.isScalar())
544 return RValue::get(Builder.CreateNot(V.getVal(), "neg"));
545
546 assert(0 && "FIXME: This doesn't handle integer complex operands yet (GNU)");
547}
548
549
550/// C99 6.5.3.3
551RValue CodeGenFunction::EmitUnaryLNot(const UnaryOperator *E) {
552 // Compare operand to zero.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000553 llvm::Value *BoolVal = EvaluateExprAsBool(E->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000554
555 // Invert value.
Chris Lattnera45c5af2007-06-02 19:47:04 +0000556 // TODO: Could dynamically modify easy computations here. For example, if
557 // the operand is an icmp ne, turn into icmp eq.
Chris Lattnerf0106d22007-06-02 19:33:17 +0000558 BoolVal = Builder.CreateNot(BoolVal, "lnot");
559
560 // ZExt result to int.
Chris Lattner8394d792007-06-05 20:53:16 +0000561 return RValue::get(Builder.CreateZExt(BoolVal, LLVMIntTy, "lnot.ext"));
Chris Lattnerf0106d22007-06-02 19:33:17 +0000562}
563
Chris Lattnere47e4402007-06-01 18:02:12 +0000564
Chris Lattnerdb91b162007-06-02 00:16:28 +0000565//===--------------------------------------------------------------------===//
566// Binary Operator Emission
567//===--------------------------------------------------------------------===//
568
569// FIXME describe.
Chris Lattnercf250242007-06-03 02:02:44 +0000570QualType CodeGenFunction::
Chris Lattner8394d792007-06-05 20:53:16 +0000571EmitUsualArithmeticConversions(const BinaryOperator *E, RValue &LHS,
572 RValue &RHS) {
Chris Lattnerc18f9d12007-06-02 22:51:30 +0000573 QualType LHSType, RHSType;
574 LHS = EmitExprWithUsualUnaryConversions(E->getLHS(), LHSType);
575 RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), RHSType);
576
Chris Lattnercf250242007-06-03 02:02:44 +0000577 // If both operands have the same source type, we're done already.
578 if (LHSType == RHSType) return LHSType;
579
580 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
581 // The caller can deal with this (e.g. pointer + int).
582 if (!LHSType->isArithmeticType() || !RHSType->isArithmeticType())
583 return LHSType;
584
585 // At this point, we have two different arithmetic types.
586
587 // Handle complex types first (C99 6.3.1.8p1).
588 if (LHSType->isComplexType() || RHSType->isComplexType()) {
589 assert(0 && "FIXME: complex types unimp");
590#if 0
591 // if we have an integer operand, the result is the complex type.
592 if (rhs->isIntegerType())
593 return lhs;
594 if (lhs->isIntegerType())
595 return rhs;
596 return Context.maxComplexType(lhs, rhs);
597#endif
598 }
599
600 // If neither operand is complex, they must be scalars.
601 llvm::Value *LHSV = LHS.getVal();
602 llvm::Value *RHSV = RHS.getVal();
603
604 // If the LLVM types are already equal, then they only differed in sign, or it
605 // was something like char/signed char or double/long double.
606 if (LHSV->getType() == RHSV->getType())
607 return LHSType;
608
609 // Now handle "real" floating types (i.e. float, double, long double).
610 if (LHSType->isRealFloatingType() || RHSType->isRealFloatingType()) {
611 // if we have an integer operand, the result is the real floating type, and
612 // the integer converts to FP.
613 if (RHSType->isIntegerType()) {
614 // Promote the RHS to an FP type of the LHS, with the sign following the
615 // RHS.
616 if (RHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000617 RHS = RValue::get(Builder.CreateSIToFP(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.CreateUIToFP(RHSV,LHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000620 return LHSType;
621 }
622
623 if (LHSType->isIntegerType()) {
624 // Promote the LHS to an FP type of the RHS, with the sign following the
625 // LHS.
626 if (LHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000627 LHS = RValue::get(Builder.CreateSIToFP(LHSV,RHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000628 else
Chris Lattner8394d792007-06-05 20:53:16 +0000629 LHS = RValue::get(Builder.CreateUIToFP(LHSV,RHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000630 return RHSType;
631 }
632
633 // Otherwise, they are two FP types. Promote the smaller operand to the
634 // bigger result.
635 QualType BiggerType = ASTContext::maxFloatingType(LHSType, RHSType);
636
637 if (BiggerType == LHSType)
Chris Lattner8394d792007-06-05 20:53:16 +0000638 RHS = RValue::get(Builder.CreateFPExt(RHSV, LHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000639 else
Chris Lattner8394d792007-06-05 20:53:16 +0000640 LHS = RValue::get(Builder.CreateFPExt(LHSV, RHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000641 return BiggerType;
642 }
643
644 // Finally, we have two integer types that are different according to C. Do
645 // a sign or zero extension if needed.
646
647 // Otherwise, one type is smaller than the other.
648 QualType ResTy = ASTContext::maxIntegerType(LHSType, RHSType);
649
650 if (LHSType == ResTy) {
651 if (RHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000652 RHS = RValue::get(Builder.CreateSExt(RHSV, LHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000653 else
Chris Lattner8394d792007-06-05 20:53:16 +0000654 RHS = RValue::get(Builder.CreateZExt(RHSV, LHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000655 } else {
656 assert(RHSType == ResTy && "Unknown conversion");
657 if (LHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000658 LHS = RValue::get(Builder.CreateSExt(LHSV, RHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000659 else
Chris Lattner8394d792007-06-05 20:53:16 +0000660 LHS = RValue::get(Builder.CreateZExt(LHSV, RHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000661 }
662 return ResTy;
Chris Lattnerdb91b162007-06-02 00:16:28 +0000663}
664
665
Chris Lattner8394d792007-06-05 20:53:16 +0000666RValue CodeGenFunction::EmitBinaryOperator(const BinaryOperator *E) {
Chris Lattnerdb91b162007-06-02 00:16:28 +0000667 switch (E->getOpcode()) {
668 default:
Chris Lattner8394d792007-06-05 20:53:16 +0000669 fprintf(stderr, "Unimplemented expr!\n");
Chris Lattnerdb91b162007-06-02 00:16:28 +0000670 E->dump();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000671 return RValue::get(llvm::UndefValue::get(llvm::Type::Int32Ty));
Chris Lattner8394d792007-06-05 20:53:16 +0000672 case BinaryOperator::Mul: return EmitBinaryMul(E);
673 case BinaryOperator::Div: return EmitBinaryDiv(E);
674 case BinaryOperator::Rem: return EmitBinaryRem(E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000675 case BinaryOperator::Add: return EmitBinaryAdd(E);
Chris Lattner8394d792007-06-05 20:53:16 +0000676 case BinaryOperator::Sub: return EmitBinarySub(E);
677 case BinaryOperator::Shl: return EmitBinaryShl(E);
678 case BinaryOperator::Shr: return EmitBinaryShr(E);
Chris Lattner8394d792007-06-05 20:53:16 +0000679 case BinaryOperator::And: return EmitBinaryAnd(E);
680 case BinaryOperator::Xor: return EmitBinaryXor(E);
681 case BinaryOperator::Or : return EmitBinaryOr(E);
682 case BinaryOperator::LAnd: return EmitBinaryLAnd(E);
683 case BinaryOperator::LOr: return EmitBinaryLOr(E);
Chris Lattner1fde0b32007-06-20 18:30:55 +0000684 case BinaryOperator::LT:
685 return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_ULT,
686 llvm::ICmpInst::ICMP_SLT,
687 llvm::FCmpInst::FCMP_OLT);
688 case BinaryOperator::GT:
689 return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_UGT,
690 llvm::ICmpInst::ICMP_SGT,
691 llvm::FCmpInst::FCMP_OGT);
692 case BinaryOperator::LE:
693 return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_ULE,
694 llvm::ICmpInst::ICMP_SLE,
695 llvm::FCmpInst::FCMP_OLE);
696 case BinaryOperator::GE:
697 return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_UGE,
698 llvm::ICmpInst::ICMP_SGE,
699 llvm::FCmpInst::FCMP_OGE);
700 case BinaryOperator::EQ:
701 return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_EQ,
702 llvm::ICmpInst::ICMP_EQ,
703 llvm::FCmpInst::FCMP_OEQ);
704 case BinaryOperator::NE:
705 return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_NE,
706 llvm::ICmpInst::ICMP_NE,
707 llvm::FCmpInst::FCMP_UNE);
Chris Lattner8394d792007-06-05 20:53:16 +0000708 case BinaryOperator::Assign: return EmitBinaryAssign(E);
709 // FIXME: Assignment.
710 case BinaryOperator::Comma: return EmitBinaryComma(E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000711 }
712}
713
Chris Lattner8394d792007-06-05 20:53:16 +0000714RValue CodeGenFunction::EmitBinaryMul(const BinaryOperator *E) {
715 RValue LHS, RHS;
716 EmitUsualArithmeticConversions(E, LHS, RHS);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000717
Chris Lattner8394d792007-06-05 20:53:16 +0000718 if (LHS.isScalar())
719 return RValue::get(Builder.CreateMul(LHS.getVal(), RHS.getVal(), "mul"));
720
721 assert(0 && "FIXME: This doesn't handle complex operands yet");
722}
723
724RValue CodeGenFunction::EmitBinaryDiv(const BinaryOperator *E) {
725 RValue LHS, RHS;
726 EmitUsualArithmeticConversions(E, LHS, RHS);
727
728 if (LHS.isScalar()) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000729 llvm::Value *RV;
Chris Lattner8394d792007-06-05 20:53:16 +0000730 if (LHS.getVal()->getType()->isFloatingPoint())
731 RV = Builder.CreateFDiv(LHS.getVal(), RHS.getVal(), "div");
732 else if (E->getType()->isUnsignedIntegerType())
733 RV = Builder.CreateUDiv(LHS.getVal(), RHS.getVal(), "div");
734 else
735 RV = Builder.CreateSDiv(LHS.getVal(), RHS.getVal(), "div");
736 return RValue::get(RV);
737 }
738 assert(0 && "FIXME: This doesn't handle complex operands yet");
739}
740
741RValue CodeGenFunction::EmitBinaryRem(const BinaryOperator *E) {
742 RValue LHS, RHS;
743 EmitUsualArithmeticConversions(E, LHS, RHS);
744
745 if (LHS.isScalar()) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000746 llvm::Value *RV;
Chris Lattner8394d792007-06-05 20:53:16 +0000747 // Rem in C can't be a floating point type: C99 6.5.5p2.
748 if (E->getType()->isUnsignedIntegerType())
749 RV = Builder.CreateURem(LHS.getVal(), RHS.getVal(), "rem");
750 else
751 RV = Builder.CreateSRem(LHS.getVal(), RHS.getVal(), "rem");
752 return RValue::get(RV);
753 }
754
755 assert(0 && "FIXME: This doesn't handle complex operands yet");
756}
757
758RValue CodeGenFunction::EmitBinaryAdd(const BinaryOperator *E) {
759 RValue LHS, RHS;
Chris Lattnerdb91b162007-06-02 00:16:28 +0000760 EmitUsualArithmeticConversions(E, LHS, RHS);
761
Chris Lattner8394d792007-06-05 20:53:16 +0000762 // FIXME: This doesn't handle ptr+int etc yet.
763
764 if (LHS.isScalar())
765 return RValue::get(Builder.CreateAdd(LHS.getVal(), RHS.getVal(), "add"));
766
767 assert(0 && "FIXME: This doesn't handle complex operands yet");
768
769}
770
771RValue CodeGenFunction::EmitBinarySub(const BinaryOperator *E) {
772 RValue LHS, RHS;
773 EmitUsualArithmeticConversions(E, LHS, RHS);
774
775 // FIXME: This doesn't handle ptr-int or ptr-ptr, etc yet.
776
777 if (LHS.isScalar())
778 return RValue::get(Builder.CreateSub(LHS.getVal(), RHS.getVal(), "sub"));
779
780 assert(0 && "FIXME: This doesn't handle complex operands yet");
781
782}
783
784RValue CodeGenFunction::EmitBinaryShl(const BinaryOperator *E) {
785 // For shifts, integer promotions are performed, but the usual arithmetic
786 // conversions are not. The LHS and RHS need not have the same type.
787
788 QualType ResTy;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000789 llvm::Value *LHS =
790 EmitExprWithUsualUnaryConversions(E->getLHS(), ResTy).getVal();
791 llvm::Value *RHS =
792 EmitExprWithUsualUnaryConversions(E->getRHS(), ResTy).getVal();
Chris Lattner8394d792007-06-05 20:53:16 +0000793
794 // LLVM requires the LHS and RHS to be the same type, promote or truncate the
795 // RHS to the same size as the LHS.
796 if (LHS->getType() != RHS->getType())
797 RHS = Builder.CreateIntCast(RHS, LHS->getType(), false, "sh_prom");
798
799 return RValue::get(Builder.CreateShl(LHS, RHS, "shl"));
800}
801
802RValue CodeGenFunction::EmitBinaryShr(const BinaryOperator *E) {
803 // For shifts, integer promotions are performed, but the usual arithmetic
804 // conversions are not. The LHS and RHS need not have the same type.
805
806 QualType ResTy;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000807 llvm::Value *LHS =
808 EmitExprWithUsualUnaryConversions(E->getLHS(), ResTy).getVal();
809 llvm::Value *RHS =
810 EmitExprWithUsualUnaryConversions(E->getRHS(), ResTy).getVal();
Chris Lattner8394d792007-06-05 20:53:16 +0000811
812 // LLVM requires the LHS and RHS to be the same type, promote or truncate the
813 // RHS to the same size as the LHS.
814 if (LHS->getType() != RHS->getType())
815 RHS = Builder.CreateIntCast(RHS, LHS->getType(), false, "sh_prom");
816
817 if (E->getType()->isUnsignedIntegerType())
818 return RValue::get(Builder.CreateLShr(LHS, RHS, "shr"));
819 else
820 return RValue::get(Builder.CreateAShr(LHS, RHS, "shr"));
821}
822
Chris Lattner1fde0b32007-06-20 18:30:55 +0000823RValue CodeGenFunction::EmitBinaryCompare(const BinaryOperator *E,
824 unsigned UICmpOpc, unsigned SICmpOpc,
825 unsigned FCmpOpc) {
Chris Lattner273c63d2007-06-20 18:02:30 +0000826 RValue LHS, RHS;
827 EmitUsualArithmeticConversions(E, LHS, RHS);
828
829 llvm::Value *Result;
830 if (LHS.isScalar()) {
831 if (LHS.getVal()->getType()->isFloatingPoint()) {
Chris Lattner1fde0b32007-06-20 18:30:55 +0000832 Result = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
833 LHS.getVal(), RHS.getVal(), "cmp");
834 } else if (E->getLHS()->getType()->isUnsignedIntegerType()) {
835 // FIXME: This check isn't right for "unsigned short < int" where ushort
836 // promotes to int and does a signed compare.
837 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
838 LHS.getVal(), RHS.getVal(), "cmp");
Chris Lattner273c63d2007-06-20 18:02:30 +0000839 } else {
Chris Lattner1fde0b32007-06-20 18:30:55 +0000840 // Signed integers and pointers.
841 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
842 LHS.getVal(), RHS.getVal(), "cmp");
Chris Lattner273c63d2007-06-20 18:02:30 +0000843 }
844 } else {
845 // Struct/union/complex
846 assert(0 && "Aggregate comparisons not implemented yet!");
847 }
848
849 // ZExt result to int.
850 return RValue::get(Builder.CreateZExt(Result, LLVMIntTy, "cmp.ext"));
851}
852
Chris Lattner8394d792007-06-05 20:53:16 +0000853RValue CodeGenFunction::EmitBinaryAnd(const BinaryOperator *E) {
854 RValue LHS, RHS;
855 EmitUsualArithmeticConversions(E, LHS, RHS);
856
857 if (LHS.isScalar())
858 return RValue::get(Builder.CreateAnd(LHS.getVal(), RHS.getVal(), "and"));
859
860 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
861}
862
863RValue CodeGenFunction::EmitBinaryXor(const BinaryOperator *E) {
864 RValue LHS, RHS;
865 EmitUsualArithmeticConversions(E, LHS, RHS);
866
867 if (LHS.isScalar())
868 return RValue::get(Builder.CreateXor(LHS.getVal(), RHS.getVal(), "xor"));
869
870 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
871}
872
873RValue CodeGenFunction::EmitBinaryOr(const BinaryOperator *E) {
874 RValue LHS, RHS;
875 EmitUsualArithmeticConversions(E, LHS, RHS);
876
877 if (LHS.isScalar())
878 return RValue::get(Builder.CreateOr(LHS.getVal(), RHS.getVal(), "or"));
879
880 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
881}
882
883RValue CodeGenFunction::EmitBinaryLAnd(const BinaryOperator *E) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000884 llvm::Value *LHSCond = EvaluateExprAsBool(E->getLHS());
Chris Lattner8394d792007-06-05 20:53:16 +0000885
Chris Lattner23b7eb62007-06-15 23:05:46 +0000886 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("land_cont");
887 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("land_rhs");
Chris Lattner8394d792007-06-05 20:53:16 +0000888
Chris Lattner23b7eb62007-06-15 23:05:46 +0000889 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
Chris Lattner8394d792007-06-05 20:53:16 +0000890 Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock);
891
892 EmitBlock(RHSBlock);
Chris Lattner23b7eb62007-06-15 23:05:46 +0000893 llvm::Value *RHSCond = EvaluateExprAsBool(E->getRHS());
Chris Lattner8394d792007-06-05 20:53:16 +0000894
895 // Reaquire the RHS block, as there may be subblocks inserted.
896 RHSBlock = Builder.GetInsertBlock();
897 EmitBlock(ContBlock);
898
899 // Create a PHI node. If we just evaluted the LHS condition, the result is
900 // false. If we evaluated both, the result is the RHS condition.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000901 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land");
Chris Lattner8394d792007-06-05 20:53:16 +0000902 PN->reserveOperandSpace(2);
Chris Lattner23b7eb62007-06-15 23:05:46 +0000903 PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock);
Chris Lattner8394d792007-06-05 20:53:16 +0000904 PN->addIncoming(RHSCond, RHSBlock);
905
906 // ZExt result to int.
907 return RValue::get(Builder.CreateZExt(PN, LLVMIntTy, "land.ext"));
908}
909
910RValue CodeGenFunction::EmitBinaryLOr(const BinaryOperator *E) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000911 llvm::Value *LHSCond = EvaluateExprAsBool(E->getLHS());
Chris Lattner8394d792007-06-05 20:53:16 +0000912
Chris Lattner23b7eb62007-06-15 23:05:46 +0000913 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("lor_cont");
914 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("lor_rhs");
Chris Lattner8394d792007-06-05 20:53:16 +0000915
Chris Lattner23b7eb62007-06-15 23:05:46 +0000916 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
Chris Lattner8394d792007-06-05 20:53:16 +0000917 Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock);
918
919 EmitBlock(RHSBlock);
Chris Lattner23b7eb62007-06-15 23:05:46 +0000920 llvm::Value *RHSCond = EvaluateExprAsBool(E->getRHS());
Chris Lattner8394d792007-06-05 20:53:16 +0000921
922 // Reaquire the RHS block, as there may be subblocks inserted.
923 RHSBlock = Builder.GetInsertBlock();
924 EmitBlock(ContBlock);
925
926 // Create a PHI node. If we just evaluted the LHS condition, the result is
927 // true. If we evaluated both, the result is the RHS condition.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000928 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor");
Chris Lattner8394d792007-06-05 20:53:16 +0000929 PN->reserveOperandSpace(2);
Chris Lattner23b7eb62007-06-15 23:05:46 +0000930 PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock);
Chris Lattner8394d792007-06-05 20:53:16 +0000931 PN->addIncoming(RHSCond, RHSBlock);
932
933 // ZExt result to int.
934 return RValue::get(Builder.CreateZExt(PN, LLVMIntTy, "lor.ext"));
935}
936
937RValue CodeGenFunction::EmitBinaryAssign(const BinaryOperator *E) {
938 LValue LHS = EmitLValue(E->getLHS());
939
940 QualType RHSTy;
941 RValue RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), RHSTy);
942
943 // Convert the RHS to the type of the LHS.
Chris Lattnerf033c142007-06-22 19:05:19 +0000944 RHS = EmitConversion(RHS, RHSTy, E->getType());
Chris Lattner8394d792007-06-05 20:53:16 +0000945
946 // Store the value into the LHS.
947 EmitStoreThroughLValue(RHS, LHS, E->getType());
948
949 // Return the converted RHS.
950 return RHS;
951}
952
Chris Lattner8394d792007-06-05 20:53:16 +0000953RValue CodeGenFunction::EmitBinaryComma(const BinaryOperator *E) {
954 EmitExpr(E->getLHS());
955 return EmitExpr(E->getRHS());
956}