blob: 8c3c2d4898c705ed26e80bbfc8a6f8a10e616cd2 [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 Lattnere9a64532007-06-22 21:44:33 +000028/// CreateTempAlloca - This creates a alloca and inserts it into the entry
29/// block.
30llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty,
31 const char *Name) {
32 return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
33}
Chris Lattner8394d792007-06-05 20:53:16 +000034
35/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
36/// expression and compare the result against zero, returning an Int1Ty value.
Chris Lattner23b7eb62007-06-15 23:05:46 +000037llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
Chris Lattner8394d792007-06-05 20:53:16 +000038 QualType Ty;
39 RValue Val = EmitExprWithUsualUnaryConversions(E, Ty);
40 return ConvertScalarValueToBool(Val, Ty);
41}
42
Chris Lattnere9a64532007-06-22 21:44:33 +000043/// EmitLoadOfComplex - Given an RValue reference for a complex, emit code to
44/// load the real and imaginary pieces, returning them as Real/Imag.
45void CodeGenFunction::EmitLoadOfComplex(RValue V,
46 llvm::Value *&Real, llvm::Value *&Imag){
47 llvm::Value *Ptr = V.getAggregateAddr();
48
49 llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
50 llvm::Constant *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1);
51 llvm::Value *RealPtr = Builder.CreateGEP(Ptr, Zero, Zero, "realp");
52 llvm::Value *ImagPtr = Builder.CreateGEP(Ptr, Zero, One, "imagp");
53
54 // FIXME: Handle volatility.
55 Real = Builder.CreateLoad(RealPtr, "real");
56 Imag = Builder.CreateLoad(ImagPtr, "imag");
57}
58
59/// EmitStoreOfComplex - Store the specified real/imag parts into the
60/// specified value pointer.
61void CodeGenFunction::EmitStoreOfComplex(llvm::Value *Real, llvm::Value *Imag,
62 llvm::Value *ResPtr) {
63 llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
64 llvm::Constant *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1);
65 llvm::Value *RealPtr = Builder.CreateGEP(ResPtr, Zero, Zero, "real");
66 llvm::Value *ImagPtr = Builder.CreateGEP(ResPtr, Zero, One, "imag");
67
68 // FIXME: Handle volatility.
69 Builder.CreateStore(Real, RealPtr);
70 Builder.CreateStore(Imag, ImagPtr);
71}
72
Chris Lattner8394d792007-06-05 20:53:16 +000073//===--------------------------------------------------------------------===//
74// Conversions
75//===--------------------------------------------------------------------===//
76
77/// EmitConversion - Convert the value specied by Val, whose type is ValTy, to
78/// the type specified by DstTy, following the rules of C99 6.3.
79RValue CodeGenFunction::EmitConversion(RValue Val, QualType ValTy,
Chris Lattnerf033c142007-06-22 19:05:19 +000080 QualType DstTy) {
Chris Lattner8394d792007-06-05 20:53:16 +000081 ValTy = ValTy.getCanonicalType();
82 DstTy = DstTy.getCanonicalType();
83 if (ValTy == DstTy) return Val;
Chris Lattner83b484b2007-06-06 04:39:08 +000084
85 // Handle conversions to bool first, they are special: comparisons against 0.
86 if (const BuiltinType *DestBT = dyn_cast<BuiltinType>(DstTy))
87 if (DestBT->getKind() == BuiltinType::Bool)
88 return RValue::get(ConvertScalarValueToBool(Val, ValTy));
Chris Lattner8394d792007-06-05 20:53:16 +000089
Chris Lattner83b484b2007-06-06 04:39:08 +000090 // Handle pointer conversions next: pointers can only be converted to/from
91 // other pointers and integers.
Chris Lattnercf106ab2007-06-06 04:05:39 +000092 if (isa<PointerType>(DstTy)) {
Chris Lattnerf033c142007-06-22 19:05:19 +000093 const llvm::Type *DestTy = ConvertType(DstTy);
Chris Lattnercf106ab2007-06-06 04:05:39 +000094
95 // The source value may be an integer, or a pointer.
96 assert(Val.isScalar() && "Can only convert from integer or pointer");
97 if (isa<llvm::PointerType>(Val.getVal()->getType()))
98 return RValue::get(Builder.CreateBitCast(Val.getVal(), DestTy, "conv"));
99 assert(ValTy->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
100 return RValue::get(Builder.CreatePtrToInt(Val.getVal(), DestTy, "conv"));
Chris Lattner83b484b2007-06-06 04:39:08 +0000101 }
102
103 if (isa<PointerType>(ValTy)) {
Chris Lattnercf106ab2007-06-06 04:05:39 +0000104 // Must be an ptr to int cast.
Chris Lattnerf033c142007-06-22 19:05:19 +0000105 const llvm::Type *DestTy = ConvertType(DstTy);
Chris Lattnercf106ab2007-06-06 04:05:39 +0000106 assert(isa<llvm::IntegerType>(DestTy) && "not ptr->int?");
107 return RValue::get(Builder.CreateIntToPtr(Val.getVal(), DestTy, "conv"));
Chris Lattner8394d792007-06-05 20:53:16 +0000108 }
Chris Lattner83b484b2007-06-06 04:39:08 +0000109
110 // Finally, we have the arithmetic types: real int/float and complex
111 // int/float. Handle real->real conversions first, they are the most
112 // common.
113 if (Val.isScalar() && DstTy->isRealType()) {
114 // We know that these are representable as scalars in LLVM, convert to LLVM
115 // types since they are easier to reason about.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000116 llvm::Value *SrcVal = Val.getVal();
Chris Lattnerf033c142007-06-22 19:05:19 +0000117 const llvm::Type *DestTy = ConvertType(DstTy);
Chris Lattner83b484b2007-06-06 04:39:08 +0000118 if (SrcVal->getType() == DestTy) return Val;
119
Chris Lattner23b7eb62007-06-15 23:05:46 +0000120 llvm::Value *Result;
Chris Lattner83b484b2007-06-06 04:39:08 +0000121 if (isa<llvm::IntegerType>(SrcVal->getType())) {
122 bool InputSigned = ValTy->isSignedIntegerType();
123 if (isa<llvm::IntegerType>(DestTy))
124 Result = Builder.CreateIntCast(SrcVal, DestTy, InputSigned, "conv");
125 else if (InputSigned)
126 Result = Builder.CreateSIToFP(SrcVal, DestTy, "conv");
127 else
128 Result = Builder.CreateUIToFP(SrcVal, DestTy, "conv");
129 } else {
130 assert(SrcVal->getType()->isFloatingPoint() && "Unknown real conversion");
131 if (isa<llvm::IntegerType>(DestTy)) {
132 if (DstTy->isSignedIntegerType())
133 Result = Builder.CreateFPToSI(SrcVal, DestTy, "conv");
134 else
135 Result = Builder.CreateFPToUI(SrcVal, DestTy, "conv");
136 } else {
137 assert(DestTy->isFloatingPoint() && "Unknown real conversion");
138 if (DestTy->getTypeID() < SrcVal->getType()->getTypeID())
139 Result = Builder.CreateFPTrunc(SrcVal, DestTy, "conv");
140 else
141 Result = Builder.CreateFPExt(SrcVal, DestTy, "conv");
142 }
143 }
144 return RValue::get(Result);
145 }
146
147 assert(0 && "FIXME: We don't support complex conversions yet!");
Chris Lattner8394d792007-06-05 20:53:16 +0000148}
149
150
151/// ConvertScalarValueToBool - Convert the specified expression value to a
Chris Lattnerf0106d22007-06-02 19:33:17 +0000152/// boolean (i1) truth value. This is equivalent to "Val == 0".
Chris Lattner23b7eb62007-06-15 23:05:46 +0000153llvm::Value *CodeGenFunction::ConvertScalarValueToBool(RValue Val, QualType Ty){
Chris Lattnerf0106d22007-06-02 19:33:17 +0000154 Ty = Ty.getCanonicalType();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000155 llvm::Value *Result;
Chris Lattnerf0106d22007-06-02 19:33:17 +0000156 if (const BuiltinType *BT = dyn_cast<BuiltinType>(Ty)) {
157 switch (BT->getKind()) {
158 default: assert(0 && "Unknown scalar value");
159 case BuiltinType::Bool:
160 Result = Val.getVal();
161 // Bool is already evaluated right.
162 assert(Result->getType() == llvm::Type::Int1Ty &&
163 "Unexpected bool value type!");
164 return Result;
Chris Lattnerb16f4552007-06-03 07:25:34 +0000165 case BuiltinType::Char_S:
166 case BuiltinType::Char_U:
Chris Lattnerf0106d22007-06-02 19:33:17 +0000167 case BuiltinType::SChar:
168 case BuiltinType::UChar:
169 case BuiltinType::Short:
170 case BuiltinType::UShort:
171 case BuiltinType::Int:
172 case BuiltinType::UInt:
173 case BuiltinType::Long:
174 case BuiltinType::ULong:
175 case BuiltinType::LongLong:
176 case BuiltinType::ULongLong:
177 // Code below handles simple integers.
178 break;
179 case BuiltinType::Float:
180 case BuiltinType::Double:
181 case BuiltinType::LongDouble: {
182 // Compare against 0.0 for fp scalars.
183 Result = Val.getVal();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000184 llvm::Value *Zero = llvm::Constant::getNullValue(Result->getType());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000185 // FIXME: llvm-gcc produces a une comparison: validate this is right.
186 Result = Builder.CreateFCmpUNE(Result, Zero, "tobool");
187 return Result;
188 }
Chris Lattnerf0106d22007-06-02 19:33:17 +0000189 }
Chris Lattnerc6395932007-06-22 20:56:16 +0000190 } else if (isa<PointerType>(Ty) ||
191 cast<TagType>(Ty)->getDecl()->getKind() == Decl::Enum) {
Chris Lattnerf0106d22007-06-02 19:33:17 +0000192 // Code below handles this fine.
Chris Lattnerc6395932007-06-22 20:56:16 +0000193 } else {
194 assert(isa<ComplexType>(Ty) && "Unknwon type!");
195 assert(0 && "FIXME: comparisons against complex not implemented yet");
Chris Lattnerf0106d22007-06-02 19:33:17 +0000196 }
197
198 // Usual case for integers, pointers, and enums: compare against zero.
199 Result = Val.getVal();
Chris Lattnera45c5af2007-06-02 19:47:04 +0000200
201 // Because of the type rules of C, we often end up computing a logical value,
202 // then zero extending it to int, then wanting it as a logical value again.
203 // Optimize this common case.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000204 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Result)) {
Chris Lattnera45c5af2007-06-02 19:47:04 +0000205 if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
206 Result = ZI->getOperand(0);
207 ZI->eraseFromParent();
208 return Result;
209 }
210 }
211
Chris Lattner23b7eb62007-06-15 23:05:46 +0000212 llvm::Value *Zero = llvm::Constant::getNullValue(Result->getType());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000213 return Builder.CreateICmpNE(Result, Zero, "tobool");
214}
215
Chris Lattnera45c5af2007-06-02 19:47:04 +0000216//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000217// LValue Expression Emission
Chris Lattnera45c5af2007-06-02 19:47:04 +0000218//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000219
Chris Lattner8394d792007-06-05 20:53:16 +0000220/// EmitLValue - Emit code to compute a designator that specifies the location
221/// of the expression.
222///
223/// This can return one of two things: a simple address or a bitfield
224/// reference. In either case, the LLVM Value* in the LValue structure is
225/// guaranteed to be an LLVM pointer type.
226///
227/// If this returns a bitfield reference, nothing about the pointee type of
228/// the LLVM value is known: For example, it may not be a pointer to an
229/// integer.
230///
231/// If this returns a normal address, and if the lvalue's C type is fixed
232/// size, this method guarantees that the returned pointer type will point to
233/// an LLVM type of the same size of the lvalue's type. If the lvalue has a
234/// variable length type, this is not possible.
235///
Chris Lattnerd7f58862007-06-02 05:24:33 +0000236LValue CodeGenFunction::EmitLValue(const Expr *E) {
237 switch (E->getStmtClass()) {
238 default:
Chris Lattner8394d792007-06-05 20:53:16 +0000239 fprintf(stderr, "Unimplemented lvalue expr!\n");
Chris Lattnerd7f58862007-06-02 05:24:33 +0000240 E->dump();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000241 return LValue::getAddr(llvm::UndefValue::get(
Chris Lattnerd7f58862007-06-02 05:24:33 +0000242 llvm::PointerType::get(llvm::Type::Int32Ty)));
243
244 case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Chris Lattner946aa312007-06-05 03:59:43 +0000245 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattner4347e3692007-06-06 04:54:52 +0000246 case Expr::StringLiteralClass:
247 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattner8394d792007-06-05 20:53:16 +0000248
249 case Expr::UnaryOperatorClass:
250 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000251 case Expr::ArraySubscriptExprClass:
252 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000253 }
254}
255
Chris Lattner8394d792007-06-05 20:53:16 +0000256/// EmitLoadOfLValue - Given an expression that represents a value lvalue,
257/// this method emits the address of the lvalue, then loads the result as an
258/// rvalue, returning the rvalue.
259RValue CodeGenFunction::EmitLoadOfLValue(const Expr *E) {
260 LValue LV = EmitLValue(E);
261
262 QualType ExprTy = E->getType().getCanonicalType();
263
264 // FIXME: this is silly and obviously wrong for non-scalars.
265 assert(!LV.isBitfield());
Chris Lattner09153c02007-06-22 18:48:09 +0000266 llvm::Value *Ptr = LV.getAddress();
267 const llvm::Type *EltTy =
268 cast<llvm::PointerType>(Ptr->getType())->getElementType();
269
270 // Simple scalar l-value.
271 if (EltTy->isFirstClassType())
272 return RValue::get(Builder.CreateLoad(Ptr, "tmp"));
273
274 // Otherwise, we have an aggregate lvalue.
275 return RValue::getAggregate(Ptr);
Chris Lattner8394d792007-06-05 20:53:16 +0000276}
277
278/// EmitStoreThroughLValue - Store the specified rvalue into the specified
279/// lvalue, where both are guaranteed to the have the same type, and that type
280/// is 'Ty'.
281void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
282 QualType Ty) {
Chris Lattner8394d792007-06-05 20:53:16 +0000283 assert(!Dst.isBitfield() && "FIXME: Don't support store to bitfield yet");
Chris Lattner8394d792007-06-05 20:53:16 +0000284
Chris Lattner09153c02007-06-22 18:48:09 +0000285 llvm::Value *DstAddr = Dst.getAddress();
286 if (Src.isScalar()) {
287 // FIXME: Handle volatility etc.
288 const llvm::Type *SrcTy = Src.getVal()->getType();
289 const llvm::Type *AddrTy =
290 cast<llvm::PointerType>(DstAddr->getType())->getElementType();
291
292 if (AddrTy != SrcTy)
293 DstAddr = Builder.CreateBitCast(DstAddr, llvm::PointerType::get(SrcTy),
294 "storetmp");
295 Builder.CreateStore(Src.getVal(), DstAddr);
296 return;
297 }
Chris Lattner8394d792007-06-05 20:53:16 +0000298
Chris Lattnere9a64532007-06-22 21:44:33 +0000299 // Don't use memcpy for complex numbers.
300 if (Ty->isComplexType()) {
301 llvm::Value *Real, *Imag;
302 EmitLoadOfComplex(Src, Real, Imag);
303 EmitStoreOfComplex(Real, Imag, Dst.getAddress());
304 return;
305 }
306
Chris Lattner09153c02007-06-22 18:48:09 +0000307 // Aggregate assignment turns into llvm.memcpy.
308 const llvm::Type *SBP = llvm::PointerType::get(llvm::Type::Int8Ty);
309 llvm::Value *SrcAddr = Src.getAggregateAddr();
310
311 if (DstAddr->getType() != SBP)
312 DstAddr = Builder.CreateBitCast(DstAddr, SBP, "tmp");
313 if (SrcAddr->getType() != SBP)
314 SrcAddr = Builder.CreateBitCast(SrcAddr, SBP, "tmp");
315
316 unsigned Align = 1; // FIXME: Compute type alignments.
317 unsigned Size = 1234; // FIXME: Compute type sizes.
318
319 // FIXME: Handle variable sized types.
320 const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
321 llvm::Value *SizeVal = llvm::ConstantInt::get(IntPtr, Size);
322
323 llvm::Value *MemCpyOps[4] = {
324 DstAddr, SrcAddr, SizeVal,llvm::ConstantInt::get(llvm::Type::Int32Ty, Align)
325 };
326
327 Builder.CreateCall(CGM.getMemCpyFn(), MemCpyOps, 4);
Chris Lattner8394d792007-06-05 20:53:16 +0000328}
329
Chris Lattnerd7f58862007-06-02 05:24:33 +0000330
331LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
332 const Decl *D = E->getDecl();
Chris Lattner53621a52007-06-13 20:44:40 +0000333 if (isa<BlockVarDecl>(D) || isa<ParmVarDecl>(D)) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000334 llvm::Value *V = LocalDeclMap[D];
Chris Lattnerd7f58862007-06-02 05:24:33 +0000335 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
336 return LValue::getAddr(V);
Chris Lattnerb6984c42007-06-20 04:44:43 +0000337 } else if (isa<FunctionDecl>(D) || isa<FileVarDecl>(D)) {
338 return LValue::getAddr(CGM.GetAddrOfGlobalDecl(D));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000339 }
340 assert(0 && "Unimp declref");
341}
Chris Lattnere47e4402007-06-01 18:02:12 +0000342
Chris Lattner8394d792007-06-05 20:53:16 +0000343LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
344 // __extension__ doesn't affect lvalue-ness.
345 if (E->getOpcode() == UnaryOperator::Extension)
346 return EmitLValue(E->getSubExpr());
347
348 assert(E->getOpcode() == UnaryOperator::Deref &&
349 "'*' is the only unary operator that produces an lvalue");
350 return LValue::getAddr(EmitExpr(E->getSubExpr()).getVal());
351}
352
Chris Lattner4347e3692007-06-06 04:54:52 +0000353LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
354 assert(!E->isWide() && "FIXME: Wide strings not supported yet!");
355 const char *StrData = E->getStrData();
356 unsigned Len = E->getByteLength();
357
358 // FIXME: Can cache/reuse these within the module.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000359 llvm::Constant *C=llvm::ConstantArray::get(std::string(StrData, StrData+Len));
Chris Lattner4347e3692007-06-06 04:54:52 +0000360
361 // Create a global variable for this.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000362 C = new llvm::GlobalVariable(C->getType(), true,
363 llvm::GlobalValue::InternalLinkage,
Chris Lattner4347e3692007-06-06 04:54:52 +0000364 C, ".str", CurFn->getParent());
Chris Lattner23b7eb62007-06-15 23:05:46 +0000365 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
366 llvm::Constant *Zeros[] = { Zero, Zero };
367 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
Chris Lattner4347e3692007-06-06 04:54:52 +0000368 return LValue::getAddr(C);
369}
370
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000371LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
372 // The base and index must be pointers or integers, neither of which are
373 // aggregates. Emit them.
374 QualType BaseTy;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000375 llvm::Value *Base =
376 EmitExprWithUsualUnaryConversions(E->getBase(), BaseTy).getVal();
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000377 QualType IdxTy;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000378 llvm::Value *Idx =
379 EmitExprWithUsualUnaryConversions(E->getIdx(), IdxTy).getVal();
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000380
381 // Usually the base is the pointer type, but sometimes it is the index.
382 // Canonicalize to have the pointer as the base.
383 if (isa<llvm::PointerType>(Idx->getType())) {
384 std::swap(Base, Idx);
385 std::swap(BaseTy, IdxTy);
386 }
387
388 // The pointer is now the base. Extend or truncate the index type to 32 or
389 // 64-bits.
390 bool IdxSigned = IdxTy->isSignedIntegerType();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000391 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000392 if (IdxBitwidth != LLVMPointerWidth)
Chris Lattner23b7eb62007-06-15 23:05:46 +0000393 Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth),
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000394 IdxSigned, "idxprom");
395
396 // We know that the pointer points to a type of the correct size, unless the
397 // size is a VLA.
398 if (!E->getType()->isConstantSizeType())
399 assert(0 && "VLA idx not implemented");
400 return LValue::getAddr(Builder.CreateGEP(Base, Idx, "arrayidx"));
401}
402
Chris Lattnere47e4402007-06-01 18:02:12 +0000403//===--------------------------------------------------------------------===//
404// Expression Emission
405//===--------------------------------------------------------------------===//
406
Chris Lattner8394d792007-06-05 20:53:16 +0000407RValue CodeGenFunction::EmitExpr(const Expr *E) {
Chris Lattnere47e4402007-06-01 18:02:12 +0000408 assert(E && "Null expression?");
409
410 switch (E->getStmtClass()) {
411 default:
Chris Lattner1fde0b32007-06-20 18:30:55 +0000412 fprintf(stderr, "Unimplemented expr!\n");
Chris Lattnere47e4402007-06-01 18:02:12 +0000413 E->dump();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000414 return RValue::get(llvm::UndefValue::get(llvm::Type::Int32Ty));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000415
416 // l-values.
Chris Lattner8394d792007-06-05 20:53:16 +0000417 case Expr::DeclRefExprClass:
Chris Lattnerf99b3f52007-06-11 03:52:52 +0000418 // DeclRef's of EnumConstantDecl's are simple rvalues.
419 if (const EnumConstantDecl *EC =
420 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
Chris Lattner23b7eb62007-06-15 23:05:46 +0000421 return RValue::get(llvm::ConstantInt::get(EC->getInitVal()));
Chris Lattnerf99b3f52007-06-11 03:52:52 +0000422
423 // FALLTHROUGH
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000424 case Expr::ArraySubscriptExprClass:
Chris Lattner8394d792007-06-05 20:53:16 +0000425 return EmitLoadOfLValue(E);
Chris Lattner4347e3692007-06-06 04:54:52 +0000426 case Expr::StringLiteralClass:
427 return RValue::get(EmitLValue(E).getAddress());
Chris Lattnerd7f58862007-06-02 05:24:33 +0000428
429 // Leaf expressions.
430 case Expr::IntegerLiteralClass:
Chris Lattnere47e4402007-06-01 18:02:12 +0000431 return EmitIntegerLiteral(cast<IntegerLiteral>(E));
Chris Lattnerdb91b162007-06-02 00:16:28 +0000432
Chris Lattnerd7f58862007-06-02 05:24:33 +0000433 // Operators.
434 case Expr::ParenExprClass:
435 return EmitExpr(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000436 case Expr::UnaryOperatorClass:
437 return EmitUnaryOperator(cast<UnaryOperator>(E));
Chris Lattner8394d792007-06-05 20:53:16 +0000438 case Expr::CastExprClass:
439 return EmitCastExpr(cast<CastExpr>(E));
Chris Lattner2b228c92007-06-15 21:34:29 +0000440 case Expr::CallExprClass:
441 return EmitCallExpr(cast<CallExpr>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000442 case Expr::BinaryOperatorClass:
Chris Lattnerdb91b162007-06-02 00:16:28 +0000443 return EmitBinaryOperator(cast<BinaryOperator>(E));
Chris Lattnere47e4402007-06-01 18:02:12 +0000444 }
445
446}
447
Chris Lattner8394d792007-06-05 20:53:16 +0000448RValue CodeGenFunction::EmitIntegerLiteral(const IntegerLiteral *E) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000449 return RValue::get(llvm::ConstantInt::get(E->getValue()));
Chris Lattnere47e4402007-06-01 18:02:12 +0000450}
451
Chris Lattner8394d792007-06-05 20:53:16 +0000452RValue CodeGenFunction::EmitCastExpr(const CastExpr *E) {
453 QualType SrcTy;
454 RValue Src = EmitExprWithUsualUnaryConversions(E->getSubExpr(), SrcTy);
455
456 // If the destination is void, just evaluate the source.
457 if (E->getType()->isVoidType())
458 return RValue::getAggregate(0);
459
Chris Lattnerf033c142007-06-22 19:05:19 +0000460 return EmitConversion(Src, SrcTy, E->getType());
Chris Lattner8394d792007-06-05 20:53:16 +0000461}
Chris Lattnerf0106d22007-06-02 19:33:17 +0000462
Chris Lattner2b228c92007-06-15 21:34:29 +0000463RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
464 QualType Ty;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000465 llvm::Value *Callee =
466 EmitExprWithUsualUnaryConversions(E->getCallee(), Ty).getVal();
Chris Lattner2b228c92007-06-15 21:34:29 +0000467
Chris Lattner23b7eb62007-06-15 23:05:46 +0000468 llvm::SmallVector<llvm::Value*, 16> Args;
Chris Lattner2b228c92007-06-15 21:34:29 +0000469
470 // FIXME: Handle struct return.
471 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
472 RValue ArgVal = EmitExprWithUsualUnaryConversions(E->getArg(i), Ty);
473
474 if (ArgVal.isScalar())
475 Args.push_back(ArgVal.getVal());
476 else // Pass by-address. FIXME: Set attribute bit on call.
Chris Lattner09153c02007-06-22 18:48:09 +0000477 Args.push_back(ArgVal.getAggregateAddr());
Chris Lattner2b228c92007-06-15 21:34:29 +0000478 }
479
Chris Lattner23b7eb62007-06-15 23:05:46 +0000480 llvm::Value *V = Builder.CreateCall(Callee, &Args[0], Args.size());
Chris Lattner2b228c92007-06-15 21:34:29 +0000481 if (V->getType() != llvm::Type::VoidTy)
482 V->setName("call");
483
484 // FIXME: Struct return;
485 return RValue::get(V);
486}
487
488
Chris Lattner8394d792007-06-05 20:53:16 +0000489//===----------------------------------------------------------------------===//
490// Unary Operator Emission
491//===----------------------------------------------------------------------===//
492
493RValue CodeGenFunction::EmitExprWithUsualUnaryConversions(const Expr *E,
494 QualType &ResTy) {
Chris Lattner6db1fb82007-06-02 22:49:07 +0000495 ResTy = E->getType().getCanonicalType();
496
497 if (isa<FunctionType>(ResTy)) { // C99 6.3.2.1p4
498 // Functions are promoted to their address.
499 ResTy = getContext().getPointerType(ResTy);
Chris Lattner8394d792007-06-05 20:53:16 +0000500 return RValue::get(EmitLValue(E).getAddress());
Chris Lattner6db1fb82007-06-02 22:49:07 +0000501 } else if (const ArrayType *ary = dyn_cast<ArrayType>(ResTy)) {
502 // C99 6.3.2.1p3
503 ResTy = getContext().getPointerType(ary->getElementType());
504
505 // FIXME: For now we assume that all source arrays map to LLVM arrays. This
506 // will not true when we add support for VLAs.
507 llvm::Value *V = EmitLValue(E).getAddress(); // Bitfields can't be arrays.
508
509 assert(isa<llvm::PointerType>(V->getType()) &&
510 isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
511 ->getElementType()) &&
512 "Doesn't support VLAs yet!");
513 llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
Chris Lattner8394d792007-06-05 20:53:16 +0000514 return RValue::get(Builder.CreateGEP(V, Idx0, Idx0, "arraydecay"));
Chris Lattner6db1fb82007-06-02 22:49:07 +0000515 } else if (ResTy->isPromotableIntegerType()) { // C99 6.3.1.1p2
516 // FIXME: this probably isn't right, pending clarification from Steve.
517 llvm::Value *Val = EmitExpr(E).getVal();
518
Chris Lattner6db1fb82007-06-02 22:49:07 +0000519 // If the input is a signed integer, sign extend to the destination.
520 if (ResTy->isSignedIntegerType()) {
521 Val = Builder.CreateSExt(Val, LLVMIntTy, "promote");
522 } else {
523 // This handles unsigned types, including bool.
524 Val = Builder.CreateZExt(Val, LLVMIntTy, "promote");
525 }
526 ResTy = getContext().IntTy;
527
Chris Lattner8394d792007-06-05 20:53:16 +0000528 return RValue::get(Val);
Chris Lattner6db1fb82007-06-02 22:49:07 +0000529 }
530
531 // Otherwise, this is a float, double, int, struct, etc.
532 return EmitExpr(E);
533}
534
535
Chris Lattner8394d792007-06-05 20:53:16 +0000536RValue CodeGenFunction::EmitUnaryOperator(const UnaryOperator *E) {
Chris Lattnerf0106d22007-06-02 19:33:17 +0000537 switch (E->getOpcode()) {
538 default:
539 printf("Unimplemented unary expr!\n");
540 E->dump();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000541 return RValue::get(llvm::UndefValue::get(llvm::Type::Int32Ty));
Chris Lattner8394d792007-06-05 20:53:16 +0000542 // FIXME: pre/post inc/dec
543 case UnaryOperator::AddrOf: return EmitUnaryAddrOf(E);
544 case UnaryOperator::Deref : return EmitLoadOfLValue(E);
545 case UnaryOperator::Plus : return EmitUnaryPlus(E);
546 case UnaryOperator::Minus : return EmitUnaryMinus(E);
547 case UnaryOperator::Not : return EmitUnaryNot(E);
548 case UnaryOperator::LNot : return EmitUnaryLNot(E);
549 // FIXME: SIZEOF/ALIGNOF(expr).
550 // FIXME: real/imag
551 case UnaryOperator::Extension: return EmitExpr(E->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000552 }
553}
554
Chris Lattner8394d792007-06-05 20:53:16 +0000555/// C99 6.5.3.2
556RValue CodeGenFunction::EmitUnaryAddrOf(const UnaryOperator *E) {
557 // The address of the operand is just its lvalue. It cannot be a bitfield.
558 return RValue::get(EmitLValue(E->getSubExpr()).getAddress());
559}
560
561RValue CodeGenFunction::EmitUnaryPlus(const UnaryOperator *E) {
562 // Unary plus just performs promotions on its arithmetic operand.
563 QualType Ty;
Chris Lattnerb48238182007-06-15 21:04:38 +0000564 return EmitExprWithUsualUnaryConversions(E->getSubExpr(), Ty);
Chris Lattner8394d792007-06-05 20:53:16 +0000565}
566
567RValue CodeGenFunction::EmitUnaryMinus(const UnaryOperator *E) {
568 // Unary minus performs promotions, then negates its arithmetic operand.
569 QualType Ty;
Chris Lattnerb48238182007-06-15 21:04:38 +0000570 RValue V = EmitExprWithUsualUnaryConversions(E->getSubExpr(), Ty);
Chris Lattnerf0106d22007-06-02 19:33:17 +0000571
Chris Lattner8394d792007-06-05 20:53:16 +0000572 if (V.isScalar())
573 return RValue::get(Builder.CreateNeg(V.getVal(), "neg"));
574
575 assert(0 && "FIXME: This doesn't handle complex operands yet");
576}
577
578RValue CodeGenFunction::EmitUnaryNot(const UnaryOperator *E) {
579 // Unary not performs promotions, then complements its integer operand.
580 QualType Ty;
Chris Lattnerb48238182007-06-15 21:04:38 +0000581 RValue V = EmitExprWithUsualUnaryConversions(E->getSubExpr(), Ty);
Chris Lattner8394d792007-06-05 20:53:16 +0000582
583 if (V.isScalar())
584 return RValue::get(Builder.CreateNot(V.getVal(), "neg"));
585
586 assert(0 && "FIXME: This doesn't handle integer complex operands yet (GNU)");
587}
588
589
590/// C99 6.5.3.3
591RValue CodeGenFunction::EmitUnaryLNot(const UnaryOperator *E) {
592 // Compare operand to zero.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000593 llvm::Value *BoolVal = EvaluateExprAsBool(E->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000594
595 // Invert value.
Chris Lattnera45c5af2007-06-02 19:47:04 +0000596 // TODO: Could dynamically modify easy computations here. For example, if
597 // the operand is an icmp ne, turn into icmp eq.
Chris Lattnerf0106d22007-06-02 19:33:17 +0000598 BoolVal = Builder.CreateNot(BoolVal, "lnot");
599
600 // ZExt result to int.
Chris Lattner8394d792007-06-05 20:53:16 +0000601 return RValue::get(Builder.CreateZExt(BoolVal, LLVMIntTy, "lnot.ext"));
Chris Lattnerf0106d22007-06-02 19:33:17 +0000602}
603
Chris Lattnere47e4402007-06-01 18:02:12 +0000604
Chris Lattnerdb91b162007-06-02 00:16:28 +0000605//===--------------------------------------------------------------------===//
606// Binary Operator Emission
607//===--------------------------------------------------------------------===//
608
609// FIXME describe.
Chris Lattnercf250242007-06-03 02:02:44 +0000610QualType CodeGenFunction::
Chris Lattner8394d792007-06-05 20:53:16 +0000611EmitUsualArithmeticConversions(const BinaryOperator *E, RValue &LHS,
612 RValue &RHS) {
Chris Lattnerc18f9d12007-06-02 22:51:30 +0000613 QualType LHSType, RHSType;
614 LHS = EmitExprWithUsualUnaryConversions(E->getLHS(), LHSType);
615 RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), RHSType);
616
Chris Lattnercf250242007-06-03 02:02:44 +0000617 // If both operands have the same source type, we're done already.
618 if (LHSType == RHSType) return LHSType;
619
620 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
621 // The caller can deal with this (e.g. pointer + int).
622 if (!LHSType->isArithmeticType() || !RHSType->isArithmeticType())
623 return LHSType;
624
625 // At this point, we have two different arithmetic types.
626
627 // Handle complex types first (C99 6.3.1.8p1).
628 if (LHSType->isComplexType() || RHSType->isComplexType()) {
629 assert(0 && "FIXME: complex types unimp");
630#if 0
631 // if we have an integer operand, the result is the complex type.
632 if (rhs->isIntegerType())
633 return lhs;
634 if (lhs->isIntegerType())
635 return rhs;
636 return Context.maxComplexType(lhs, rhs);
637#endif
638 }
639
640 // If neither operand is complex, they must be scalars.
641 llvm::Value *LHSV = LHS.getVal();
642 llvm::Value *RHSV = RHS.getVal();
643
644 // If the LLVM types are already equal, then they only differed in sign, or it
645 // was something like char/signed char or double/long double.
646 if (LHSV->getType() == RHSV->getType())
647 return LHSType;
648
649 // Now handle "real" floating types (i.e. float, double, long double).
650 if (LHSType->isRealFloatingType() || RHSType->isRealFloatingType()) {
651 // if we have an integer operand, the result is the real floating type, and
652 // the integer converts to FP.
653 if (RHSType->isIntegerType()) {
654 // Promote the RHS to an FP type of the LHS, with the sign following the
655 // RHS.
656 if (RHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000657 RHS = RValue::get(Builder.CreateSIToFP(RHSV,LHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000658 else
Chris Lattner8394d792007-06-05 20:53:16 +0000659 RHS = RValue::get(Builder.CreateUIToFP(RHSV,LHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000660 return LHSType;
661 }
662
663 if (LHSType->isIntegerType()) {
664 // Promote the LHS to an FP type of the RHS, with the sign following the
665 // LHS.
666 if (LHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000667 LHS = RValue::get(Builder.CreateSIToFP(LHSV,RHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000668 else
Chris Lattner8394d792007-06-05 20:53:16 +0000669 LHS = RValue::get(Builder.CreateUIToFP(LHSV,RHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000670 return RHSType;
671 }
672
673 // Otherwise, they are two FP types. Promote the smaller operand to the
674 // bigger result.
675 QualType BiggerType = ASTContext::maxFloatingType(LHSType, RHSType);
676
677 if (BiggerType == LHSType)
Chris Lattner8394d792007-06-05 20:53:16 +0000678 RHS = RValue::get(Builder.CreateFPExt(RHSV, LHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000679 else
Chris Lattner8394d792007-06-05 20:53:16 +0000680 LHS = RValue::get(Builder.CreateFPExt(LHSV, RHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000681 return BiggerType;
682 }
683
684 // Finally, we have two integer types that are different according to C. Do
685 // a sign or zero extension if needed.
686
687 // Otherwise, one type is smaller than the other.
688 QualType ResTy = ASTContext::maxIntegerType(LHSType, RHSType);
689
690 if (LHSType == ResTy) {
691 if (RHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000692 RHS = RValue::get(Builder.CreateSExt(RHSV, LHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000693 else
Chris Lattner8394d792007-06-05 20:53:16 +0000694 RHS = RValue::get(Builder.CreateZExt(RHSV, LHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000695 } else {
696 assert(RHSType == ResTy && "Unknown conversion");
697 if (LHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000698 LHS = RValue::get(Builder.CreateSExt(LHSV, RHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000699 else
Chris Lattner8394d792007-06-05 20:53:16 +0000700 LHS = RValue::get(Builder.CreateZExt(LHSV, RHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000701 }
702 return ResTy;
Chris Lattnerdb91b162007-06-02 00:16:28 +0000703}
704
705
Chris Lattner8394d792007-06-05 20:53:16 +0000706RValue CodeGenFunction::EmitBinaryOperator(const BinaryOperator *E) {
Chris Lattnerdb91b162007-06-02 00:16:28 +0000707 switch (E->getOpcode()) {
708 default:
Chris Lattner8394d792007-06-05 20:53:16 +0000709 fprintf(stderr, "Unimplemented expr!\n");
Chris Lattnerdb91b162007-06-02 00:16:28 +0000710 E->dump();
Chris Lattner23b7eb62007-06-15 23:05:46 +0000711 return RValue::get(llvm::UndefValue::get(llvm::Type::Int32Ty));
Chris Lattner8394d792007-06-05 20:53:16 +0000712 case BinaryOperator::Mul: return EmitBinaryMul(E);
713 case BinaryOperator::Div: return EmitBinaryDiv(E);
714 case BinaryOperator::Rem: return EmitBinaryRem(E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000715 case BinaryOperator::Add: return EmitBinaryAdd(E);
Chris Lattner8394d792007-06-05 20:53:16 +0000716 case BinaryOperator::Sub: return EmitBinarySub(E);
717 case BinaryOperator::Shl: return EmitBinaryShl(E);
718 case BinaryOperator::Shr: return EmitBinaryShr(E);
Chris Lattner8394d792007-06-05 20:53:16 +0000719 case BinaryOperator::And: return EmitBinaryAnd(E);
720 case BinaryOperator::Xor: return EmitBinaryXor(E);
721 case BinaryOperator::Or : return EmitBinaryOr(E);
722 case BinaryOperator::LAnd: return EmitBinaryLAnd(E);
723 case BinaryOperator::LOr: return EmitBinaryLOr(E);
Chris Lattner1fde0b32007-06-20 18:30:55 +0000724 case BinaryOperator::LT:
725 return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_ULT,
726 llvm::ICmpInst::ICMP_SLT,
727 llvm::FCmpInst::FCMP_OLT);
728 case BinaryOperator::GT:
729 return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_UGT,
730 llvm::ICmpInst::ICMP_SGT,
731 llvm::FCmpInst::FCMP_OGT);
732 case BinaryOperator::LE:
733 return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_ULE,
734 llvm::ICmpInst::ICMP_SLE,
735 llvm::FCmpInst::FCMP_OLE);
736 case BinaryOperator::GE:
737 return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_UGE,
738 llvm::ICmpInst::ICMP_SGE,
739 llvm::FCmpInst::FCMP_OGE);
740 case BinaryOperator::EQ:
741 return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_EQ,
742 llvm::ICmpInst::ICMP_EQ,
743 llvm::FCmpInst::FCMP_OEQ);
744 case BinaryOperator::NE:
745 return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_NE,
746 llvm::ICmpInst::ICMP_NE,
747 llvm::FCmpInst::FCMP_UNE);
Chris Lattner8394d792007-06-05 20:53:16 +0000748 case BinaryOperator::Assign: return EmitBinaryAssign(E);
749 // FIXME: Assignment.
750 case BinaryOperator::Comma: return EmitBinaryComma(E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000751 }
752}
753
Chris Lattner8394d792007-06-05 20:53:16 +0000754RValue CodeGenFunction::EmitBinaryMul(const BinaryOperator *E) {
755 RValue LHS, RHS;
756 EmitUsualArithmeticConversions(E, LHS, RHS);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000757
Chris Lattner8394d792007-06-05 20:53:16 +0000758 if (LHS.isScalar())
759 return RValue::get(Builder.CreateMul(LHS.getVal(), RHS.getVal(), "mul"));
760
761 assert(0 && "FIXME: This doesn't handle complex operands yet");
762}
763
764RValue CodeGenFunction::EmitBinaryDiv(const BinaryOperator *E) {
765 RValue LHS, RHS;
766 EmitUsualArithmeticConversions(E, LHS, RHS);
767
768 if (LHS.isScalar()) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000769 llvm::Value *RV;
Chris Lattner8394d792007-06-05 20:53:16 +0000770 if (LHS.getVal()->getType()->isFloatingPoint())
771 RV = Builder.CreateFDiv(LHS.getVal(), RHS.getVal(), "div");
772 else if (E->getType()->isUnsignedIntegerType())
773 RV = Builder.CreateUDiv(LHS.getVal(), RHS.getVal(), "div");
774 else
775 RV = Builder.CreateSDiv(LHS.getVal(), RHS.getVal(), "div");
776 return RValue::get(RV);
777 }
778 assert(0 && "FIXME: This doesn't handle complex operands yet");
779}
780
781RValue CodeGenFunction::EmitBinaryRem(const BinaryOperator *E) {
782 RValue LHS, RHS;
783 EmitUsualArithmeticConversions(E, LHS, RHS);
784
785 if (LHS.isScalar()) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000786 llvm::Value *RV;
Chris Lattner8394d792007-06-05 20:53:16 +0000787 // Rem in C can't be a floating point type: C99 6.5.5p2.
788 if (E->getType()->isUnsignedIntegerType())
789 RV = Builder.CreateURem(LHS.getVal(), RHS.getVal(), "rem");
790 else
791 RV = Builder.CreateSRem(LHS.getVal(), RHS.getVal(), "rem");
792 return RValue::get(RV);
793 }
794
795 assert(0 && "FIXME: This doesn't handle complex operands yet");
796}
797
798RValue CodeGenFunction::EmitBinaryAdd(const BinaryOperator *E) {
799 RValue LHS, RHS;
Chris Lattnerdb91b162007-06-02 00:16:28 +0000800 EmitUsualArithmeticConversions(E, LHS, RHS);
801
Chris Lattner8394d792007-06-05 20:53:16 +0000802 // FIXME: This doesn't handle ptr+int etc yet.
803
804 if (LHS.isScalar())
805 return RValue::get(Builder.CreateAdd(LHS.getVal(), RHS.getVal(), "add"));
Chris Lattner8394d792007-06-05 20:53:16 +0000806
Chris Lattnere9a64532007-06-22 21:44:33 +0000807 // Otherwise, this must be a complex number.
808 llvm::Value *LHSR, *LHSI, *RHSR, *RHSI;
809
810 EmitLoadOfComplex(LHS, LHSR, LHSI);
811 EmitLoadOfComplex(RHS, RHSR, RHSI);
812
813 llvm::Value *ResR = Builder.CreateAdd(LHSR, RHSR, "add.r");
814 llvm::Value *ResI = Builder.CreateAdd(LHSI, RHSI, "add.i");
815
816 llvm::Value *Res = CreateTempAlloca(ConvertType(E->getType()));
817 EmitStoreOfComplex(ResR, ResI, Res);
818 return RValue::getAggregate(Res);
Chris Lattner8394d792007-06-05 20:53:16 +0000819}
820
821RValue CodeGenFunction::EmitBinarySub(const BinaryOperator *E) {
822 RValue LHS, RHS;
823 EmitUsualArithmeticConversions(E, LHS, RHS);
824
825 // FIXME: This doesn't handle ptr-int or ptr-ptr, etc yet.
826
827 if (LHS.isScalar())
828 return RValue::get(Builder.CreateSub(LHS.getVal(), RHS.getVal(), "sub"));
829
830 assert(0 && "FIXME: This doesn't handle complex operands yet");
831
832}
833
834RValue CodeGenFunction::EmitBinaryShl(const BinaryOperator *E) {
835 // For shifts, integer promotions are performed, but the usual arithmetic
836 // conversions are not. The LHS and RHS need not have the same type.
837
838 QualType ResTy;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000839 llvm::Value *LHS =
840 EmitExprWithUsualUnaryConversions(E->getLHS(), ResTy).getVal();
841 llvm::Value *RHS =
842 EmitExprWithUsualUnaryConversions(E->getRHS(), ResTy).getVal();
Chris Lattner8394d792007-06-05 20:53:16 +0000843
844 // LLVM requires the LHS and RHS to be the same type, promote or truncate the
845 // RHS to the same size as the LHS.
846 if (LHS->getType() != RHS->getType())
847 RHS = Builder.CreateIntCast(RHS, LHS->getType(), false, "sh_prom");
848
849 return RValue::get(Builder.CreateShl(LHS, RHS, "shl"));
850}
851
852RValue CodeGenFunction::EmitBinaryShr(const BinaryOperator *E) {
853 // For shifts, integer promotions are performed, but the usual arithmetic
854 // conversions are not. The LHS and RHS need not have the same type.
855
856 QualType ResTy;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000857 llvm::Value *LHS =
858 EmitExprWithUsualUnaryConversions(E->getLHS(), ResTy).getVal();
859 llvm::Value *RHS =
860 EmitExprWithUsualUnaryConversions(E->getRHS(), ResTy).getVal();
Chris Lattner8394d792007-06-05 20:53:16 +0000861
862 // LLVM requires the LHS and RHS to be the same type, promote or truncate the
863 // RHS to the same size as the LHS.
864 if (LHS->getType() != RHS->getType())
865 RHS = Builder.CreateIntCast(RHS, LHS->getType(), false, "sh_prom");
866
867 if (E->getType()->isUnsignedIntegerType())
868 return RValue::get(Builder.CreateLShr(LHS, RHS, "shr"));
869 else
870 return RValue::get(Builder.CreateAShr(LHS, RHS, "shr"));
871}
872
Chris Lattner1fde0b32007-06-20 18:30:55 +0000873RValue CodeGenFunction::EmitBinaryCompare(const BinaryOperator *E,
874 unsigned UICmpOpc, unsigned SICmpOpc,
875 unsigned FCmpOpc) {
Chris Lattner273c63d2007-06-20 18:02:30 +0000876 RValue LHS, RHS;
877 EmitUsualArithmeticConversions(E, LHS, RHS);
878
879 llvm::Value *Result;
880 if (LHS.isScalar()) {
881 if (LHS.getVal()->getType()->isFloatingPoint()) {
Chris Lattner1fde0b32007-06-20 18:30:55 +0000882 Result = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
883 LHS.getVal(), RHS.getVal(), "cmp");
884 } else if (E->getLHS()->getType()->isUnsignedIntegerType()) {
885 // FIXME: This check isn't right for "unsigned short < int" where ushort
886 // promotes to int and does a signed compare.
887 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
888 LHS.getVal(), RHS.getVal(), "cmp");
Chris Lattner273c63d2007-06-20 18:02:30 +0000889 } else {
Chris Lattner1fde0b32007-06-20 18:30:55 +0000890 // Signed integers and pointers.
891 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
892 LHS.getVal(), RHS.getVal(), "cmp");
Chris Lattner273c63d2007-06-20 18:02:30 +0000893 }
894 } else {
895 // Struct/union/complex
896 assert(0 && "Aggregate comparisons not implemented yet!");
897 }
898
899 // ZExt result to int.
900 return RValue::get(Builder.CreateZExt(Result, LLVMIntTy, "cmp.ext"));
901}
902
Chris Lattner8394d792007-06-05 20:53:16 +0000903RValue CodeGenFunction::EmitBinaryAnd(const BinaryOperator *E) {
904 RValue LHS, RHS;
905 EmitUsualArithmeticConversions(E, LHS, RHS);
906
907 if (LHS.isScalar())
908 return RValue::get(Builder.CreateAnd(LHS.getVal(), RHS.getVal(), "and"));
909
910 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
911}
912
913RValue CodeGenFunction::EmitBinaryXor(const BinaryOperator *E) {
914 RValue LHS, RHS;
915 EmitUsualArithmeticConversions(E, LHS, RHS);
916
917 if (LHS.isScalar())
918 return RValue::get(Builder.CreateXor(LHS.getVal(), RHS.getVal(), "xor"));
919
920 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
921}
922
923RValue CodeGenFunction::EmitBinaryOr(const BinaryOperator *E) {
924 RValue LHS, RHS;
925 EmitUsualArithmeticConversions(E, LHS, RHS);
926
927 if (LHS.isScalar())
928 return RValue::get(Builder.CreateOr(LHS.getVal(), RHS.getVal(), "or"));
929
930 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
931}
932
933RValue CodeGenFunction::EmitBinaryLAnd(const BinaryOperator *E) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000934 llvm::Value *LHSCond = EvaluateExprAsBool(E->getLHS());
Chris Lattner8394d792007-06-05 20:53:16 +0000935
Chris Lattner23b7eb62007-06-15 23:05:46 +0000936 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("land_cont");
937 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("land_rhs");
Chris Lattner8394d792007-06-05 20:53:16 +0000938
Chris Lattner23b7eb62007-06-15 23:05:46 +0000939 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
Chris Lattner8394d792007-06-05 20:53:16 +0000940 Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock);
941
942 EmitBlock(RHSBlock);
Chris Lattner23b7eb62007-06-15 23:05:46 +0000943 llvm::Value *RHSCond = EvaluateExprAsBool(E->getRHS());
Chris Lattner8394d792007-06-05 20:53:16 +0000944
945 // Reaquire the RHS block, as there may be subblocks inserted.
946 RHSBlock = Builder.GetInsertBlock();
947 EmitBlock(ContBlock);
948
949 // Create a PHI node. If we just evaluted the LHS condition, the result is
950 // false. If we evaluated both, the result is the RHS condition.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000951 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land");
Chris Lattner8394d792007-06-05 20:53:16 +0000952 PN->reserveOperandSpace(2);
Chris Lattner23b7eb62007-06-15 23:05:46 +0000953 PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock);
Chris Lattner8394d792007-06-05 20:53:16 +0000954 PN->addIncoming(RHSCond, RHSBlock);
955
956 // ZExt result to int.
957 return RValue::get(Builder.CreateZExt(PN, LLVMIntTy, "land.ext"));
958}
959
960RValue CodeGenFunction::EmitBinaryLOr(const BinaryOperator *E) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000961 llvm::Value *LHSCond = EvaluateExprAsBool(E->getLHS());
Chris Lattner8394d792007-06-05 20:53:16 +0000962
Chris Lattner23b7eb62007-06-15 23:05:46 +0000963 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("lor_cont");
964 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("lor_rhs");
Chris Lattner8394d792007-06-05 20:53:16 +0000965
Chris Lattner23b7eb62007-06-15 23:05:46 +0000966 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
Chris Lattner8394d792007-06-05 20:53:16 +0000967 Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock);
968
969 EmitBlock(RHSBlock);
Chris Lattner23b7eb62007-06-15 23:05:46 +0000970 llvm::Value *RHSCond = EvaluateExprAsBool(E->getRHS());
Chris Lattner8394d792007-06-05 20:53:16 +0000971
972 // Reaquire the RHS block, as there may be subblocks inserted.
973 RHSBlock = Builder.GetInsertBlock();
974 EmitBlock(ContBlock);
975
976 // Create a PHI node. If we just evaluted the LHS condition, the result is
977 // true. If we evaluated both, the result is the RHS condition.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000978 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor");
Chris Lattner8394d792007-06-05 20:53:16 +0000979 PN->reserveOperandSpace(2);
Chris Lattner23b7eb62007-06-15 23:05:46 +0000980 PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock);
Chris Lattner8394d792007-06-05 20:53:16 +0000981 PN->addIncoming(RHSCond, RHSBlock);
982
983 // ZExt result to int.
984 return RValue::get(Builder.CreateZExt(PN, LLVMIntTy, "lor.ext"));
985}
986
987RValue CodeGenFunction::EmitBinaryAssign(const BinaryOperator *E) {
988 LValue LHS = EmitLValue(E->getLHS());
989
990 QualType RHSTy;
991 RValue RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), RHSTy);
992
993 // Convert the RHS to the type of the LHS.
Chris Lattnerf033c142007-06-22 19:05:19 +0000994 RHS = EmitConversion(RHS, RHSTy, E->getType());
Chris Lattner8394d792007-06-05 20:53:16 +0000995
996 // Store the value into the LHS.
997 EmitStoreThroughLValue(RHS, LHS, E->getType());
998
999 // Return the converted RHS.
1000 return RHS;
1001}
1002
Chris Lattner8394d792007-06-05 20:53:16 +00001003RValue CodeGenFunction::EmitBinaryComma(const BinaryOperator *E) {
1004 EmitExpr(E->getLHS());
1005 return EmitExpr(E->getRHS());
1006}