blob: f0a0f6ee189cf2c9d75f3d24f516a3b1975d0a8e [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 }
Chris Lattnerf0106d22007-06-02 19:33:17 +0000153 }
Chris Lattnerc6395932007-06-22 20:56:16 +0000154 } else if (isa<PointerType>(Ty) ||
155 cast<TagType>(Ty)->getDecl()->getKind() == Decl::Enum) {
Chris Lattnerf0106d22007-06-02 19:33:17 +0000156 // Code below handles this fine.
Chris Lattnerc6395932007-06-22 20:56:16 +0000157 } else {
158 assert(isa<ComplexType>(Ty) && "Unknwon type!");
159 assert(0 && "FIXME: comparisons against complex not implemented yet");
Chris Lattnerf0106d22007-06-02 19:33:17 +0000160 }
161
162 // Usual case for integers, pointers, and enums: compare against zero.
163 Result = Val.getVal();
Chris Lattnera45c5af2007-06-02 19:47:04 +0000164
165 // Because of the type rules of C, we often end up computing a logical value,
166 // then zero extending it to int, then wanting it as a logical value again.
167 // Optimize this common case.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000168 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Result)) {
Chris Lattnera45c5af2007-06-02 19:47:04 +0000169 if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
170 Result = ZI->getOperand(0);
171 ZI->eraseFromParent();
172 return Result;
173 }
174 }
175
Chris Lattner23b7eb62007-06-15 23:05:46 +0000176 llvm::Value *Zero = llvm::Constant::getNullValue(Result->getType());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000177 return Builder.CreateICmpNE(Result, Zero, "tobool");
178}
179
Chris Lattnera45c5af2007-06-02 19:47:04 +0000180//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000181// LValue Expression Emission
Chris Lattnera45c5af2007-06-02 19:47:04 +0000182//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000183
Chris Lattner8394d792007-06-05 20:53:16 +0000184/// EmitLValue - Emit code to compute a designator that specifies the location
185/// of the expression.
186///
187/// This can return one of two things: a simple address or a bitfield
188/// reference. In either case, the LLVM Value* in the LValue structure is
189/// guaranteed to be an LLVM pointer type.
190///
191/// If this returns a bitfield reference, nothing about the pointee type of
192/// the LLVM value is known: For example, it may not be a pointer to an
193/// integer.
194///
195/// If this returns a normal address, and if the lvalue's C type is fixed
196/// size, this method guarantees that the returned pointer type will point to
197/// an LLVM type of the same size of the lvalue's type. If the lvalue has a
198/// variable length type, this is not possible.
199///
Chris Lattnerd7f58862007-06-02 05:24:33 +0000200LValue CodeGenFunction::EmitLValue(const Expr *E) {
201 switch (E->getStmtClass()) {
202 default:
Chris Lattner8394d792007-06-05 20:53:16 +0000203 fprintf(stderr, "Unimplemented lvalue expr!\n");
Chris Lattnerd7f58862007-06-02 05:24:33 +0000204 E->dump();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000205 return LValue::getAddr(llvm::UndefValue::get(
Chris Lattnerd7f58862007-06-02 05:24:33 +0000206 llvm::PointerType::get(llvm::Type::Int32Ty)));
207
208 case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Chris Lattner946aa312007-06-05 03:59:43 +0000209 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattner4347e3692007-06-06 04:54:52 +0000210 case Expr::StringLiteralClass:
211 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattner8394d792007-06-05 20:53:16 +0000212
213 case Expr::UnaryOperatorClass:
214 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000215 case Expr::ArraySubscriptExprClass:
216 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000217 }
218}
219
Chris Lattner8394d792007-06-05 20:53:16 +0000220/// EmitLoadOfLValue - Given an expression that represents a value lvalue,
221/// this method emits the address of the lvalue, then loads the result as an
222/// rvalue, returning the rvalue.
223RValue CodeGenFunction::EmitLoadOfLValue(const Expr *E) {
224 LValue LV = EmitLValue(E);
225
226 QualType ExprTy = E->getType().getCanonicalType();
227
228 // FIXME: this is silly and obviously wrong for non-scalars.
229 assert(!LV.isBitfield());
Chris Lattner09153c02007-06-22 18:48:09 +0000230 llvm::Value *Ptr = LV.getAddress();
231 const llvm::Type *EltTy =
232 cast<llvm::PointerType>(Ptr->getType())->getElementType();
233
234 // Simple scalar l-value.
235 if (EltTy->isFirstClassType())
236 return RValue::get(Builder.CreateLoad(Ptr, "tmp"));
237
238 // Otherwise, we have an aggregate lvalue.
239 return RValue::getAggregate(Ptr);
Chris Lattner8394d792007-06-05 20:53:16 +0000240}
241
242/// EmitStoreThroughLValue - Store the specified rvalue into the specified
243/// lvalue, where both are guaranteed to the have the same type, and that type
244/// is 'Ty'.
245void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
246 QualType Ty) {
Chris Lattner8394d792007-06-05 20:53:16 +0000247 assert(!Dst.isBitfield() && "FIXME: Don't support store to bitfield yet");
Chris Lattner8394d792007-06-05 20:53:16 +0000248
Chris Lattner09153c02007-06-22 18:48:09 +0000249 llvm::Value *DstAddr = Dst.getAddress();
250 if (Src.isScalar()) {
251 // FIXME: Handle volatility etc.
252 const llvm::Type *SrcTy = Src.getVal()->getType();
253 const llvm::Type *AddrTy =
254 cast<llvm::PointerType>(DstAddr->getType())->getElementType();
255
256 if (AddrTy != SrcTy)
257 DstAddr = Builder.CreateBitCast(DstAddr, llvm::PointerType::get(SrcTy),
258 "storetmp");
259 Builder.CreateStore(Src.getVal(), DstAddr);
260 return;
261 }
Chris Lattner8394d792007-06-05 20:53:16 +0000262
Chris Lattner09153c02007-06-22 18:48:09 +0000263 // Aggregate assignment turns into llvm.memcpy.
264 const llvm::Type *SBP = llvm::PointerType::get(llvm::Type::Int8Ty);
265 llvm::Value *SrcAddr = Src.getAggregateAddr();
266
267 if (DstAddr->getType() != SBP)
268 DstAddr = Builder.CreateBitCast(DstAddr, SBP, "tmp");
269 if (SrcAddr->getType() != SBP)
270 SrcAddr = Builder.CreateBitCast(SrcAddr, SBP, "tmp");
271
272 unsigned Align = 1; // FIXME: Compute type alignments.
273 unsigned Size = 1234; // FIXME: Compute type sizes.
274
275 // FIXME: Handle variable sized types.
276 const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
277 llvm::Value *SizeVal = llvm::ConstantInt::get(IntPtr, Size);
278
279 llvm::Value *MemCpyOps[4] = {
280 DstAddr, SrcAddr, SizeVal,llvm::ConstantInt::get(llvm::Type::Int32Ty, Align)
281 };
282
283 Builder.CreateCall(CGM.getMemCpyFn(), MemCpyOps, 4);
Chris Lattner8394d792007-06-05 20:53:16 +0000284}
285
Chris Lattnerd7f58862007-06-02 05:24:33 +0000286
287LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
288 const Decl *D = E->getDecl();
Chris Lattner53621a52007-06-13 20:44:40 +0000289 if (isa<BlockVarDecl>(D) || isa<ParmVarDecl>(D)) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000290 llvm::Value *V = LocalDeclMap[D];
Chris Lattnerd7f58862007-06-02 05:24:33 +0000291 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
292 return LValue::getAddr(V);
Chris Lattnerb6984c42007-06-20 04:44:43 +0000293 } else if (isa<FunctionDecl>(D) || isa<FileVarDecl>(D)) {
294 return LValue::getAddr(CGM.GetAddrOfGlobalDecl(D));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000295 }
296 assert(0 && "Unimp declref");
297}
Chris Lattnere47e4402007-06-01 18:02:12 +0000298
Chris Lattner8394d792007-06-05 20:53:16 +0000299LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
300 // __extension__ doesn't affect lvalue-ness.
301 if (E->getOpcode() == UnaryOperator::Extension)
302 return EmitLValue(E->getSubExpr());
303
304 assert(E->getOpcode() == UnaryOperator::Deref &&
305 "'*' is the only unary operator that produces an lvalue");
306 return LValue::getAddr(EmitExpr(E->getSubExpr()).getVal());
307}
308
Chris Lattner4347e3692007-06-06 04:54:52 +0000309LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
310 assert(!E->isWide() && "FIXME: Wide strings not supported yet!");
311 const char *StrData = E->getStrData();
312 unsigned Len = E->getByteLength();
313
314 // FIXME: Can cache/reuse these within the module.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000315 llvm::Constant *C=llvm::ConstantArray::get(std::string(StrData, StrData+Len));
Chris Lattner4347e3692007-06-06 04:54:52 +0000316
317 // Create a global variable for this.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000318 C = new llvm::GlobalVariable(C->getType(), true,
319 llvm::GlobalValue::InternalLinkage,
Chris Lattner4347e3692007-06-06 04:54:52 +0000320 C, ".str", CurFn->getParent());
Chris Lattner23b7eb62007-06-15 23:05:46 +0000321 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
322 llvm::Constant *Zeros[] = { Zero, Zero };
323 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
Chris Lattner4347e3692007-06-06 04:54:52 +0000324 return LValue::getAddr(C);
325}
326
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000327LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
328 // The base and index must be pointers or integers, neither of which are
329 // aggregates. Emit them.
330 QualType BaseTy;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000331 llvm::Value *Base =
332 EmitExprWithUsualUnaryConversions(E->getBase(), BaseTy).getVal();
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000333 QualType IdxTy;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000334 llvm::Value *Idx =
335 EmitExprWithUsualUnaryConversions(E->getIdx(), IdxTy).getVal();
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000336
337 // Usually the base is the pointer type, but sometimes it is the index.
338 // Canonicalize to have the pointer as the base.
339 if (isa<llvm::PointerType>(Idx->getType())) {
340 std::swap(Base, Idx);
341 std::swap(BaseTy, IdxTy);
342 }
343
344 // The pointer is now the base. Extend or truncate the index type to 32 or
345 // 64-bits.
346 bool IdxSigned = IdxTy->isSignedIntegerType();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000347 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000348 if (IdxBitwidth != LLVMPointerWidth)
Chris Lattner23b7eb62007-06-15 23:05:46 +0000349 Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth),
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000350 IdxSigned, "idxprom");
351
352 // We know that the pointer points to a type of the correct size, unless the
353 // size is a VLA.
354 if (!E->getType()->isConstantSizeType())
355 assert(0 && "VLA idx not implemented");
356 return LValue::getAddr(Builder.CreateGEP(Base, Idx, "arrayidx"));
357}
358
Chris Lattnere47e4402007-06-01 18:02:12 +0000359//===--------------------------------------------------------------------===//
360// Expression Emission
361//===--------------------------------------------------------------------===//
362
Chris Lattner8394d792007-06-05 20:53:16 +0000363RValue CodeGenFunction::EmitExpr(const Expr *E) {
Chris Lattnere47e4402007-06-01 18:02:12 +0000364 assert(E && "Null expression?");
365
366 switch (E->getStmtClass()) {
367 default:
Chris Lattner1fde0b32007-06-20 18:30:55 +0000368 fprintf(stderr, "Unimplemented expr!\n");
Chris Lattnere47e4402007-06-01 18:02:12 +0000369 E->dump();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000370 return RValue::get(llvm::UndefValue::get(llvm::Type::Int32Ty));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000371
372 // l-values.
Chris Lattner8394d792007-06-05 20:53:16 +0000373 case Expr::DeclRefExprClass:
Chris Lattnerf99b3f52007-06-11 03:52:52 +0000374 // DeclRef's of EnumConstantDecl's are simple rvalues.
375 if (const EnumConstantDecl *EC =
376 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
Chris Lattner23b7eb62007-06-15 23:05:46 +0000377 return RValue::get(llvm::ConstantInt::get(EC->getInitVal()));
Chris Lattnerf99b3f52007-06-11 03:52:52 +0000378
379 // FALLTHROUGH
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000380 case Expr::ArraySubscriptExprClass:
Chris Lattner8394d792007-06-05 20:53:16 +0000381 return EmitLoadOfLValue(E);
Chris Lattner4347e3692007-06-06 04:54:52 +0000382 case Expr::StringLiteralClass:
383 return RValue::get(EmitLValue(E).getAddress());
Chris Lattnerd7f58862007-06-02 05:24:33 +0000384
385 // Leaf expressions.
386 case Expr::IntegerLiteralClass:
Chris Lattnere47e4402007-06-01 18:02:12 +0000387 return EmitIntegerLiteral(cast<IntegerLiteral>(E));
Chris Lattnerdb91b162007-06-02 00:16:28 +0000388
Chris Lattnerd7f58862007-06-02 05:24:33 +0000389 // Operators.
390 case Expr::ParenExprClass:
391 return EmitExpr(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000392 case Expr::UnaryOperatorClass:
393 return EmitUnaryOperator(cast<UnaryOperator>(E));
Chris Lattner8394d792007-06-05 20:53:16 +0000394 case Expr::CastExprClass:
395 return EmitCastExpr(cast<CastExpr>(E));
Chris Lattner2b228c92007-06-15 21:34:29 +0000396 case Expr::CallExprClass:
397 return EmitCallExpr(cast<CallExpr>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000398 case Expr::BinaryOperatorClass:
Chris Lattnerdb91b162007-06-02 00:16:28 +0000399 return EmitBinaryOperator(cast<BinaryOperator>(E));
Chris Lattnere47e4402007-06-01 18:02:12 +0000400 }
401
402}
403
Chris Lattner8394d792007-06-05 20:53:16 +0000404RValue CodeGenFunction::EmitIntegerLiteral(const IntegerLiteral *E) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000405 return RValue::get(llvm::ConstantInt::get(E->getValue()));
Chris Lattnere47e4402007-06-01 18:02:12 +0000406}
407
Chris Lattner8394d792007-06-05 20:53:16 +0000408RValue CodeGenFunction::EmitCastExpr(const CastExpr *E) {
409 QualType SrcTy;
410 RValue Src = EmitExprWithUsualUnaryConversions(E->getSubExpr(), SrcTy);
411
412 // If the destination is void, just evaluate the source.
413 if (E->getType()->isVoidType())
414 return RValue::getAggregate(0);
415
Chris Lattnerf033c142007-06-22 19:05:19 +0000416 return EmitConversion(Src, SrcTy, E->getType());
Chris Lattner8394d792007-06-05 20:53:16 +0000417}
Chris Lattnerf0106d22007-06-02 19:33:17 +0000418
Chris Lattner2b228c92007-06-15 21:34:29 +0000419RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
420 QualType Ty;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000421 llvm::Value *Callee =
422 EmitExprWithUsualUnaryConversions(E->getCallee(), Ty).getVal();
Chris Lattner2b228c92007-06-15 21:34:29 +0000423
Chris Lattner23b7eb62007-06-15 23:05:46 +0000424 llvm::SmallVector<llvm::Value*, 16> Args;
Chris Lattner2b228c92007-06-15 21:34:29 +0000425
426 // FIXME: Handle struct return.
427 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
428 RValue ArgVal = EmitExprWithUsualUnaryConversions(E->getArg(i), Ty);
429
430 if (ArgVal.isScalar())
431 Args.push_back(ArgVal.getVal());
432 else // Pass by-address. FIXME: Set attribute bit on call.
Chris Lattner09153c02007-06-22 18:48:09 +0000433 Args.push_back(ArgVal.getAggregateAddr());
Chris Lattner2b228c92007-06-15 21:34:29 +0000434 }
435
Chris Lattner23b7eb62007-06-15 23:05:46 +0000436 llvm::Value *V = Builder.CreateCall(Callee, &Args[0], Args.size());
Chris Lattner2b228c92007-06-15 21:34:29 +0000437 if (V->getType() != llvm::Type::VoidTy)
438 V->setName("call");
439
440 // FIXME: Struct return;
441 return RValue::get(V);
442}
443
444
Chris Lattner8394d792007-06-05 20:53:16 +0000445//===----------------------------------------------------------------------===//
446// Unary Operator Emission
447//===----------------------------------------------------------------------===//
448
449RValue CodeGenFunction::EmitExprWithUsualUnaryConversions(const Expr *E,
450 QualType &ResTy) {
Chris Lattner6db1fb82007-06-02 22:49:07 +0000451 ResTy = E->getType().getCanonicalType();
452
453 if (isa<FunctionType>(ResTy)) { // C99 6.3.2.1p4
454 // Functions are promoted to their address.
455 ResTy = getContext().getPointerType(ResTy);
Chris Lattner8394d792007-06-05 20:53:16 +0000456 return RValue::get(EmitLValue(E).getAddress());
Chris Lattner6db1fb82007-06-02 22:49:07 +0000457 } else if (const ArrayType *ary = dyn_cast<ArrayType>(ResTy)) {
458 // C99 6.3.2.1p3
459 ResTy = getContext().getPointerType(ary->getElementType());
460
461 // FIXME: For now we assume that all source arrays map to LLVM arrays. This
462 // will not true when we add support for VLAs.
463 llvm::Value *V = EmitLValue(E).getAddress(); // Bitfields can't be arrays.
464
465 assert(isa<llvm::PointerType>(V->getType()) &&
466 isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
467 ->getElementType()) &&
468 "Doesn't support VLAs yet!");
469 llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
Chris Lattner8394d792007-06-05 20:53:16 +0000470 return RValue::get(Builder.CreateGEP(V, Idx0, Idx0, "arraydecay"));
Chris Lattner6db1fb82007-06-02 22:49:07 +0000471 } else if (ResTy->isPromotableIntegerType()) { // C99 6.3.1.1p2
472 // FIXME: this probably isn't right, pending clarification from Steve.
473 llvm::Value *Val = EmitExpr(E).getVal();
474
Chris Lattner6db1fb82007-06-02 22:49:07 +0000475 // If the input is a signed integer, sign extend to the destination.
476 if (ResTy->isSignedIntegerType()) {
477 Val = Builder.CreateSExt(Val, LLVMIntTy, "promote");
478 } else {
479 // This handles unsigned types, including bool.
480 Val = Builder.CreateZExt(Val, LLVMIntTy, "promote");
481 }
482 ResTy = getContext().IntTy;
483
Chris Lattner8394d792007-06-05 20:53:16 +0000484 return RValue::get(Val);
Chris Lattner6db1fb82007-06-02 22:49:07 +0000485 }
486
487 // Otherwise, this is a float, double, int, struct, etc.
488 return EmitExpr(E);
489}
490
491
Chris Lattner8394d792007-06-05 20:53:16 +0000492RValue CodeGenFunction::EmitUnaryOperator(const UnaryOperator *E) {
Chris Lattnerf0106d22007-06-02 19:33:17 +0000493 switch (E->getOpcode()) {
494 default:
495 printf("Unimplemented unary expr!\n");
496 E->dump();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000497 return RValue::get(llvm::UndefValue::get(llvm::Type::Int32Ty));
Chris Lattner8394d792007-06-05 20:53:16 +0000498 // FIXME: pre/post inc/dec
499 case UnaryOperator::AddrOf: return EmitUnaryAddrOf(E);
500 case UnaryOperator::Deref : return EmitLoadOfLValue(E);
501 case UnaryOperator::Plus : return EmitUnaryPlus(E);
502 case UnaryOperator::Minus : return EmitUnaryMinus(E);
503 case UnaryOperator::Not : return EmitUnaryNot(E);
504 case UnaryOperator::LNot : return EmitUnaryLNot(E);
505 // FIXME: SIZEOF/ALIGNOF(expr).
506 // FIXME: real/imag
507 case UnaryOperator::Extension: return EmitExpr(E->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000508 }
509}
510
Chris Lattner8394d792007-06-05 20:53:16 +0000511/// C99 6.5.3.2
512RValue CodeGenFunction::EmitUnaryAddrOf(const UnaryOperator *E) {
513 // The address of the operand is just its lvalue. It cannot be a bitfield.
514 return RValue::get(EmitLValue(E->getSubExpr()).getAddress());
515}
516
517RValue CodeGenFunction::EmitUnaryPlus(const UnaryOperator *E) {
518 // Unary plus just performs promotions on its arithmetic operand.
519 QualType Ty;
Chris Lattnerb48238182007-06-15 21:04:38 +0000520 return EmitExprWithUsualUnaryConversions(E->getSubExpr(), Ty);
Chris Lattner8394d792007-06-05 20:53:16 +0000521}
522
523RValue CodeGenFunction::EmitUnaryMinus(const UnaryOperator *E) {
524 // Unary minus performs promotions, then negates its arithmetic operand.
525 QualType Ty;
Chris Lattnerb48238182007-06-15 21:04:38 +0000526 RValue V = EmitExprWithUsualUnaryConversions(E->getSubExpr(), Ty);
Chris Lattnerf0106d22007-06-02 19:33:17 +0000527
Chris Lattner8394d792007-06-05 20:53:16 +0000528 if (V.isScalar())
529 return RValue::get(Builder.CreateNeg(V.getVal(), "neg"));
530
531 assert(0 && "FIXME: This doesn't handle complex operands yet");
532}
533
534RValue CodeGenFunction::EmitUnaryNot(const UnaryOperator *E) {
535 // Unary not performs promotions, then complements its integer operand.
536 QualType Ty;
Chris Lattnerb48238182007-06-15 21:04:38 +0000537 RValue V = EmitExprWithUsualUnaryConversions(E->getSubExpr(), Ty);
Chris Lattner8394d792007-06-05 20:53:16 +0000538
539 if (V.isScalar())
540 return RValue::get(Builder.CreateNot(V.getVal(), "neg"));
541
542 assert(0 && "FIXME: This doesn't handle integer complex operands yet (GNU)");
543}
544
545
546/// C99 6.5.3.3
547RValue CodeGenFunction::EmitUnaryLNot(const UnaryOperator *E) {
548 // Compare operand to zero.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000549 llvm::Value *BoolVal = EvaluateExprAsBool(E->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000550
551 // Invert value.
Chris Lattnera45c5af2007-06-02 19:47:04 +0000552 // TODO: Could dynamically modify easy computations here. For example, if
553 // the operand is an icmp ne, turn into icmp eq.
Chris Lattnerf0106d22007-06-02 19:33:17 +0000554 BoolVal = Builder.CreateNot(BoolVal, "lnot");
555
556 // ZExt result to int.
Chris Lattner8394d792007-06-05 20:53:16 +0000557 return RValue::get(Builder.CreateZExt(BoolVal, LLVMIntTy, "lnot.ext"));
Chris Lattnerf0106d22007-06-02 19:33:17 +0000558}
559
Chris Lattnere47e4402007-06-01 18:02:12 +0000560
Chris Lattnerdb91b162007-06-02 00:16:28 +0000561//===--------------------------------------------------------------------===//
562// Binary Operator Emission
563//===--------------------------------------------------------------------===//
564
565// FIXME describe.
Chris Lattnercf250242007-06-03 02:02:44 +0000566QualType CodeGenFunction::
Chris Lattner8394d792007-06-05 20:53:16 +0000567EmitUsualArithmeticConversions(const BinaryOperator *E, RValue &LHS,
568 RValue &RHS) {
Chris Lattnerc18f9d12007-06-02 22:51:30 +0000569 QualType LHSType, RHSType;
570 LHS = EmitExprWithUsualUnaryConversions(E->getLHS(), LHSType);
571 RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), RHSType);
572
Chris Lattnercf250242007-06-03 02:02:44 +0000573 // If both operands have the same source type, we're done already.
574 if (LHSType == RHSType) return LHSType;
575
576 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
577 // The caller can deal with this (e.g. pointer + int).
578 if (!LHSType->isArithmeticType() || !RHSType->isArithmeticType())
579 return LHSType;
580
581 // At this point, we have two different arithmetic types.
582
583 // Handle complex types first (C99 6.3.1.8p1).
584 if (LHSType->isComplexType() || RHSType->isComplexType()) {
585 assert(0 && "FIXME: complex types unimp");
586#if 0
587 // if we have an integer operand, the result is the complex type.
588 if (rhs->isIntegerType())
589 return lhs;
590 if (lhs->isIntegerType())
591 return rhs;
592 return Context.maxComplexType(lhs, rhs);
593#endif
594 }
595
596 // If neither operand is complex, they must be scalars.
597 llvm::Value *LHSV = LHS.getVal();
598 llvm::Value *RHSV = RHS.getVal();
599
600 // If the LLVM types are already equal, then they only differed in sign, or it
601 // was something like char/signed char or double/long double.
602 if (LHSV->getType() == RHSV->getType())
603 return LHSType;
604
605 // Now handle "real" floating types (i.e. float, double, long double).
606 if (LHSType->isRealFloatingType() || RHSType->isRealFloatingType()) {
607 // if we have an integer operand, the result is the real floating type, and
608 // the integer converts to FP.
609 if (RHSType->isIntegerType()) {
610 // Promote the RHS to an FP type of the LHS, with the sign following the
611 // RHS.
612 if (RHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000613 RHS = RValue::get(Builder.CreateSIToFP(RHSV,LHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000614 else
Chris Lattner8394d792007-06-05 20:53:16 +0000615 RHS = RValue::get(Builder.CreateUIToFP(RHSV,LHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000616 return LHSType;
617 }
618
619 if (LHSType->isIntegerType()) {
620 // Promote the LHS to an FP type of the RHS, with the sign following the
621 // LHS.
622 if (LHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000623 LHS = RValue::get(Builder.CreateSIToFP(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.CreateUIToFP(LHSV,RHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000626 return RHSType;
627 }
628
629 // Otherwise, they are two FP types. Promote the smaller operand to the
630 // bigger result.
631 QualType BiggerType = ASTContext::maxFloatingType(LHSType, RHSType);
632
633 if (BiggerType == LHSType)
Chris Lattner8394d792007-06-05 20:53:16 +0000634 RHS = RValue::get(Builder.CreateFPExt(RHSV, LHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000635 else
Chris Lattner8394d792007-06-05 20:53:16 +0000636 LHS = RValue::get(Builder.CreateFPExt(LHSV, RHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000637 return BiggerType;
638 }
639
640 // Finally, we have two integer types that are different according to C. Do
641 // a sign or zero extension if needed.
642
643 // Otherwise, one type is smaller than the other.
644 QualType ResTy = ASTContext::maxIntegerType(LHSType, RHSType);
645
646 if (LHSType == ResTy) {
647 if (RHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000648 RHS = RValue::get(Builder.CreateSExt(RHSV, LHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000649 else
Chris Lattner8394d792007-06-05 20:53:16 +0000650 RHS = RValue::get(Builder.CreateZExt(RHSV, LHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000651 } else {
652 assert(RHSType == ResTy && "Unknown conversion");
653 if (LHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000654 LHS = RValue::get(Builder.CreateSExt(LHSV, RHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000655 else
Chris Lattner8394d792007-06-05 20:53:16 +0000656 LHS = RValue::get(Builder.CreateZExt(LHSV, RHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000657 }
658 return ResTy;
Chris Lattnerdb91b162007-06-02 00:16:28 +0000659}
660
661
Chris Lattner8394d792007-06-05 20:53:16 +0000662RValue CodeGenFunction::EmitBinaryOperator(const BinaryOperator *E) {
Chris Lattnerdb91b162007-06-02 00:16:28 +0000663 switch (E->getOpcode()) {
664 default:
Chris Lattner8394d792007-06-05 20:53:16 +0000665 fprintf(stderr, "Unimplemented expr!\n");
Chris Lattnerdb91b162007-06-02 00:16:28 +0000666 E->dump();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000667 return RValue::get(llvm::UndefValue::get(llvm::Type::Int32Ty));
Chris Lattner8394d792007-06-05 20:53:16 +0000668 case BinaryOperator::Mul: return EmitBinaryMul(E);
669 case BinaryOperator::Div: return EmitBinaryDiv(E);
670 case BinaryOperator::Rem: return EmitBinaryRem(E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000671 case BinaryOperator::Add: return EmitBinaryAdd(E);
Chris Lattner8394d792007-06-05 20:53:16 +0000672 case BinaryOperator::Sub: return EmitBinarySub(E);
673 case BinaryOperator::Shl: return EmitBinaryShl(E);
674 case BinaryOperator::Shr: return EmitBinaryShr(E);
Chris Lattner8394d792007-06-05 20:53:16 +0000675 case BinaryOperator::And: return EmitBinaryAnd(E);
676 case BinaryOperator::Xor: return EmitBinaryXor(E);
677 case BinaryOperator::Or : return EmitBinaryOr(E);
678 case BinaryOperator::LAnd: return EmitBinaryLAnd(E);
679 case BinaryOperator::LOr: return EmitBinaryLOr(E);
Chris Lattner1fde0b32007-06-20 18:30:55 +0000680 case BinaryOperator::LT:
681 return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_ULT,
682 llvm::ICmpInst::ICMP_SLT,
683 llvm::FCmpInst::FCMP_OLT);
684 case BinaryOperator::GT:
685 return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_UGT,
686 llvm::ICmpInst::ICMP_SGT,
687 llvm::FCmpInst::FCMP_OGT);
688 case BinaryOperator::LE:
689 return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_ULE,
690 llvm::ICmpInst::ICMP_SLE,
691 llvm::FCmpInst::FCMP_OLE);
692 case BinaryOperator::GE:
693 return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_UGE,
694 llvm::ICmpInst::ICMP_SGE,
695 llvm::FCmpInst::FCMP_OGE);
696 case BinaryOperator::EQ:
697 return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_EQ,
698 llvm::ICmpInst::ICMP_EQ,
699 llvm::FCmpInst::FCMP_OEQ);
700 case BinaryOperator::NE:
701 return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_NE,
702 llvm::ICmpInst::ICMP_NE,
703 llvm::FCmpInst::FCMP_UNE);
Chris Lattner8394d792007-06-05 20:53:16 +0000704 case BinaryOperator::Assign: return EmitBinaryAssign(E);
705 // FIXME: Assignment.
706 case BinaryOperator::Comma: return EmitBinaryComma(E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000707 }
708}
709
Chris Lattner8394d792007-06-05 20:53:16 +0000710RValue CodeGenFunction::EmitBinaryMul(const BinaryOperator *E) {
711 RValue LHS, RHS;
712 EmitUsualArithmeticConversions(E, LHS, RHS);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000713
Chris Lattner8394d792007-06-05 20:53:16 +0000714 if (LHS.isScalar())
715 return RValue::get(Builder.CreateMul(LHS.getVal(), RHS.getVal(), "mul"));
716
717 assert(0 && "FIXME: This doesn't handle complex operands yet");
718}
719
720RValue CodeGenFunction::EmitBinaryDiv(const BinaryOperator *E) {
721 RValue LHS, RHS;
722 EmitUsualArithmeticConversions(E, LHS, RHS);
723
724 if (LHS.isScalar()) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000725 llvm::Value *RV;
Chris Lattner8394d792007-06-05 20:53:16 +0000726 if (LHS.getVal()->getType()->isFloatingPoint())
727 RV = Builder.CreateFDiv(LHS.getVal(), RHS.getVal(), "div");
728 else if (E->getType()->isUnsignedIntegerType())
729 RV = Builder.CreateUDiv(LHS.getVal(), RHS.getVal(), "div");
730 else
731 RV = Builder.CreateSDiv(LHS.getVal(), RHS.getVal(), "div");
732 return RValue::get(RV);
733 }
734 assert(0 && "FIXME: This doesn't handle complex operands yet");
735}
736
737RValue CodeGenFunction::EmitBinaryRem(const BinaryOperator *E) {
738 RValue LHS, RHS;
739 EmitUsualArithmeticConversions(E, LHS, RHS);
740
741 if (LHS.isScalar()) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000742 llvm::Value *RV;
Chris Lattner8394d792007-06-05 20:53:16 +0000743 // Rem in C can't be a floating point type: C99 6.5.5p2.
744 if (E->getType()->isUnsignedIntegerType())
745 RV = Builder.CreateURem(LHS.getVal(), RHS.getVal(), "rem");
746 else
747 RV = Builder.CreateSRem(LHS.getVal(), RHS.getVal(), "rem");
748 return RValue::get(RV);
749 }
750
751 assert(0 && "FIXME: This doesn't handle complex operands yet");
752}
753
754RValue CodeGenFunction::EmitBinaryAdd(const BinaryOperator *E) {
755 RValue LHS, RHS;
Chris Lattnerdb91b162007-06-02 00:16:28 +0000756 EmitUsualArithmeticConversions(E, LHS, RHS);
757
Chris Lattner8394d792007-06-05 20:53:16 +0000758 // FIXME: This doesn't handle ptr+int etc yet.
759
760 if (LHS.isScalar())
761 return RValue::get(Builder.CreateAdd(LHS.getVal(), RHS.getVal(), "add"));
762
763 assert(0 && "FIXME: This doesn't handle complex operands yet");
764
765}
766
767RValue CodeGenFunction::EmitBinarySub(const BinaryOperator *E) {
768 RValue LHS, RHS;
769 EmitUsualArithmeticConversions(E, LHS, RHS);
770
771 // FIXME: This doesn't handle ptr-int or ptr-ptr, etc yet.
772
773 if (LHS.isScalar())
774 return RValue::get(Builder.CreateSub(LHS.getVal(), RHS.getVal(), "sub"));
775
776 assert(0 && "FIXME: This doesn't handle complex operands yet");
777
778}
779
780RValue CodeGenFunction::EmitBinaryShl(const BinaryOperator *E) {
781 // For shifts, integer promotions are performed, but the usual arithmetic
782 // conversions are not. The LHS and RHS need not have the same type.
783
784 QualType ResTy;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000785 llvm::Value *LHS =
786 EmitExprWithUsualUnaryConversions(E->getLHS(), ResTy).getVal();
787 llvm::Value *RHS =
788 EmitExprWithUsualUnaryConversions(E->getRHS(), ResTy).getVal();
Chris Lattner8394d792007-06-05 20:53:16 +0000789
790 // LLVM requires the LHS and RHS to be the same type, promote or truncate the
791 // RHS to the same size as the LHS.
792 if (LHS->getType() != RHS->getType())
793 RHS = Builder.CreateIntCast(RHS, LHS->getType(), false, "sh_prom");
794
795 return RValue::get(Builder.CreateShl(LHS, RHS, "shl"));
796}
797
798RValue CodeGenFunction::EmitBinaryShr(const BinaryOperator *E) {
799 // For shifts, integer promotions are performed, but the usual arithmetic
800 // conversions are not. The LHS and RHS need not have the same type.
801
802 QualType ResTy;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000803 llvm::Value *LHS =
804 EmitExprWithUsualUnaryConversions(E->getLHS(), ResTy).getVal();
805 llvm::Value *RHS =
806 EmitExprWithUsualUnaryConversions(E->getRHS(), ResTy).getVal();
Chris Lattner8394d792007-06-05 20:53:16 +0000807
808 // LLVM requires the LHS and RHS to be the same type, promote or truncate the
809 // RHS to the same size as the LHS.
810 if (LHS->getType() != RHS->getType())
811 RHS = Builder.CreateIntCast(RHS, LHS->getType(), false, "sh_prom");
812
813 if (E->getType()->isUnsignedIntegerType())
814 return RValue::get(Builder.CreateLShr(LHS, RHS, "shr"));
815 else
816 return RValue::get(Builder.CreateAShr(LHS, RHS, "shr"));
817}
818
Chris Lattner1fde0b32007-06-20 18:30:55 +0000819RValue CodeGenFunction::EmitBinaryCompare(const BinaryOperator *E,
820 unsigned UICmpOpc, unsigned SICmpOpc,
821 unsigned FCmpOpc) {
Chris Lattner273c63d2007-06-20 18:02:30 +0000822 RValue LHS, RHS;
823 EmitUsualArithmeticConversions(E, LHS, RHS);
824
825 llvm::Value *Result;
826 if (LHS.isScalar()) {
827 if (LHS.getVal()->getType()->isFloatingPoint()) {
Chris Lattner1fde0b32007-06-20 18:30:55 +0000828 Result = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
829 LHS.getVal(), RHS.getVal(), "cmp");
830 } else if (E->getLHS()->getType()->isUnsignedIntegerType()) {
831 // FIXME: This check isn't right for "unsigned short < int" where ushort
832 // promotes to int and does a signed compare.
833 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
834 LHS.getVal(), RHS.getVal(), "cmp");
Chris Lattner273c63d2007-06-20 18:02:30 +0000835 } else {
Chris Lattner1fde0b32007-06-20 18:30:55 +0000836 // Signed integers and pointers.
837 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
838 LHS.getVal(), RHS.getVal(), "cmp");
Chris Lattner273c63d2007-06-20 18:02:30 +0000839 }
840 } else {
841 // Struct/union/complex
842 assert(0 && "Aggregate comparisons not implemented yet!");
843 }
844
845 // ZExt result to int.
846 return RValue::get(Builder.CreateZExt(Result, LLVMIntTy, "cmp.ext"));
847}
848
Chris Lattner8394d792007-06-05 20:53:16 +0000849RValue CodeGenFunction::EmitBinaryAnd(const BinaryOperator *E) {
850 RValue LHS, RHS;
851 EmitUsualArithmeticConversions(E, LHS, RHS);
852
853 if (LHS.isScalar())
854 return RValue::get(Builder.CreateAnd(LHS.getVal(), RHS.getVal(), "and"));
855
856 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
857}
858
859RValue CodeGenFunction::EmitBinaryXor(const BinaryOperator *E) {
860 RValue LHS, RHS;
861 EmitUsualArithmeticConversions(E, LHS, RHS);
862
863 if (LHS.isScalar())
864 return RValue::get(Builder.CreateXor(LHS.getVal(), RHS.getVal(), "xor"));
865
866 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
867}
868
869RValue CodeGenFunction::EmitBinaryOr(const BinaryOperator *E) {
870 RValue LHS, RHS;
871 EmitUsualArithmeticConversions(E, LHS, RHS);
872
873 if (LHS.isScalar())
874 return RValue::get(Builder.CreateOr(LHS.getVal(), RHS.getVal(), "or"));
875
876 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
877}
878
879RValue CodeGenFunction::EmitBinaryLAnd(const BinaryOperator *E) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000880 llvm::Value *LHSCond = EvaluateExprAsBool(E->getLHS());
Chris Lattner8394d792007-06-05 20:53:16 +0000881
Chris Lattner23b7eb62007-06-15 23:05:46 +0000882 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("land_cont");
883 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("land_rhs");
Chris Lattner8394d792007-06-05 20:53:16 +0000884
Chris Lattner23b7eb62007-06-15 23:05:46 +0000885 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
Chris Lattner8394d792007-06-05 20:53:16 +0000886 Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock);
887
888 EmitBlock(RHSBlock);
Chris Lattner23b7eb62007-06-15 23:05:46 +0000889 llvm::Value *RHSCond = EvaluateExprAsBool(E->getRHS());
Chris Lattner8394d792007-06-05 20:53:16 +0000890
891 // Reaquire the RHS block, as there may be subblocks inserted.
892 RHSBlock = Builder.GetInsertBlock();
893 EmitBlock(ContBlock);
894
895 // Create a PHI node. If we just evaluted the LHS condition, the result is
896 // false. If we evaluated both, the result is the RHS condition.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000897 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land");
Chris Lattner8394d792007-06-05 20:53:16 +0000898 PN->reserveOperandSpace(2);
Chris Lattner23b7eb62007-06-15 23:05:46 +0000899 PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock);
Chris Lattner8394d792007-06-05 20:53:16 +0000900 PN->addIncoming(RHSCond, RHSBlock);
901
902 // ZExt result to int.
903 return RValue::get(Builder.CreateZExt(PN, LLVMIntTy, "land.ext"));
904}
905
906RValue CodeGenFunction::EmitBinaryLOr(const BinaryOperator *E) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000907 llvm::Value *LHSCond = EvaluateExprAsBool(E->getLHS());
Chris Lattner8394d792007-06-05 20:53:16 +0000908
Chris Lattner23b7eb62007-06-15 23:05:46 +0000909 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("lor_cont");
910 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("lor_rhs");
Chris Lattner8394d792007-06-05 20:53:16 +0000911
Chris Lattner23b7eb62007-06-15 23:05:46 +0000912 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
Chris Lattner8394d792007-06-05 20:53:16 +0000913 Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock);
914
915 EmitBlock(RHSBlock);
Chris Lattner23b7eb62007-06-15 23:05:46 +0000916 llvm::Value *RHSCond = EvaluateExprAsBool(E->getRHS());
Chris Lattner8394d792007-06-05 20:53:16 +0000917
918 // Reaquire the RHS block, as there may be subblocks inserted.
919 RHSBlock = Builder.GetInsertBlock();
920 EmitBlock(ContBlock);
921
922 // Create a PHI node. If we just evaluted the LHS condition, the result is
923 // true. If we evaluated both, the result is the RHS condition.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000924 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor");
Chris Lattner8394d792007-06-05 20:53:16 +0000925 PN->reserveOperandSpace(2);
Chris Lattner23b7eb62007-06-15 23:05:46 +0000926 PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock);
Chris Lattner8394d792007-06-05 20:53:16 +0000927 PN->addIncoming(RHSCond, RHSBlock);
928
929 // ZExt result to int.
930 return RValue::get(Builder.CreateZExt(PN, LLVMIntTy, "lor.ext"));
931}
932
933RValue CodeGenFunction::EmitBinaryAssign(const BinaryOperator *E) {
934 LValue LHS = EmitLValue(E->getLHS());
935
936 QualType RHSTy;
937 RValue RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), RHSTy);
938
939 // Convert the RHS to the type of the LHS.
Chris Lattnerf033c142007-06-22 19:05:19 +0000940 RHS = EmitConversion(RHS, RHSTy, E->getType());
Chris Lattner8394d792007-06-05 20:53:16 +0000941
942 // Store the value into the LHS.
943 EmitStoreThroughLValue(RHS, LHS, E->getType());
944
945 // Return the converted RHS.
946 return RHS;
947}
948
Chris Lattner8394d792007-06-05 20:53:16 +0000949RValue CodeGenFunction::EmitBinaryComma(const BinaryOperator *E) {
950 EmitExpr(E->getLHS());
951 return EmitExpr(E->getRHS());
952}