blob: cacf7bc367f4b50e28845718b257bbd8ebff20c3 [file] [log] [blame]
Chris Lattnere47e4402007-06-01 18:02:12 +00001//===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Expr nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "clang/AST/AST.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18using namespace llvm;
19using namespace clang;
20using namespace CodeGen;
21
Chris Lattnerd7f58862007-06-02 05:24:33 +000022//===--------------------------------------------------------------------===//
Chris Lattnerf0106d22007-06-02 19:33:17 +000023// Miscellaneous Helper Methods
24//===--------------------------------------------------------------------===//
25
Chris Lattner8394d792007-06-05 20:53:16 +000026
27/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
28/// expression and compare the result against zero, returning an Int1Ty value.
29Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
30 QualType Ty;
31 RValue Val = EmitExprWithUsualUnaryConversions(E, Ty);
32 return ConvertScalarValueToBool(Val, Ty);
33}
34
35//===--------------------------------------------------------------------===//
36// Conversions
37//===--------------------------------------------------------------------===//
38
39/// EmitConversion - Convert the value specied by Val, whose type is ValTy, to
40/// the type specified by DstTy, following the rules of C99 6.3.
41RValue CodeGenFunction::EmitConversion(RValue Val, QualType ValTy,
Chris Lattnercf106ab2007-06-06 04:05:39 +000042 QualType DstTy, SourceLocation Loc) {
Chris Lattner8394d792007-06-05 20:53:16 +000043 ValTy = ValTy.getCanonicalType();
44 DstTy = DstTy.getCanonicalType();
45 if (ValTy == DstTy) return Val;
46
Chris Lattnercf106ab2007-06-06 04:05:39 +000047 if (isa<PointerType>(DstTy)) {
48 const llvm::Type *DestTy = ConvertType(DstTy, Loc);
49
50 // The source value may be an integer, or a pointer.
51 assert(Val.isScalar() && "Can only convert from integer or pointer");
52 if (isa<llvm::PointerType>(Val.getVal()->getType()))
53 return RValue::get(Builder.CreateBitCast(Val.getVal(), DestTy, "conv"));
54 assert(ValTy->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
55 return RValue::get(Builder.CreatePtrToInt(Val.getVal(), DestTy, "conv"));
56 } else if (isa<PointerType>(ValTy)) {
57 // Must be an ptr to int cast.
58 const llvm::Type *DestTy = ConvertType(DstTy, Loc);
59 assert(isa<llvm::IntegerType>(DestTy) && "not ptr->int?");
60 return RValue::get(Builder.CreateIntToPtr(Val.getVal(), DestTy, "conv"));
61 } else if (const BuiltinType *DestBT = dyn_cast<BuiltinType>(DstTy)) {
Chris Lattner8394d792007-06-05 20:53:16 +000062 if (DestBT->getKind() == BuiltinType::Bool)
63 return RValue::get(ConvertScalarValueToBool(Val, ValTy));
64 }
Chris Lattner8394d792007-06-05 20:53:16 +000065 assert(0 && "FIXME: Unsupported conversion!");
66}
67
68
69/// ConvertScalarValueToBool - Convert the specified expression value to a
Chris Lattnerf0106d22007-06-02 19:33:17 +000070/// boolean (i1) truth value. This is equivalent to "Val == 0".
Chris Lattner8394d792007-06-05 20:53:16 +000071Value *CodeGenFunction::ConvertScalarValueToBool(RValue Val, QualType Ty) {
Chris Lattnerf0106d22007-06-02 19:33:17 +000072 Ty = Ty.getCanonicalType();
73 Value *Result;
74 if (const BuiltinType *BT = dyn_cast<BuiltinType>(Ty)) {
75 switch (BT->getKind()) {
76 default: assert(0 && "Unknown scalar value");
77 case BuiltinType::Bool:
78 Result = Val.getVal();
79 // Bool is already evaluated right.
80 assert(Result->getType() == llvm::Type::Int1Ty &&
81 "Unexpected bool value type!");
82 return Result;
Chris Lattnerb16f4552007-06-03 07:25:34 +000083 case BuiltinType::Char_S:
84 case BuiltinType::Char_U:
Chris Lattnerf0106d22007-06-02 19:33:17 +000085 case BuiltinType::SChar:
86 case BuiltinType::UChar:
87 case BuiltinType::Short:
88 case BuiltinType::UShort:
89 case BuiltinType::Int:
90 case BuiltinType::UInt:
91 case BuiltinType::Long:
92 case BuiltinType::ULong:
93 case BuiltinType::LongLong:
94 case BuiltinType::ULongLong:
95 // Code below handles simple integers.
96 break;
97 case BuiltinType::Float:
98 case BuiltinType::Double:
99 case BuiltinType::LongDouble: {
100 // Compare against 0.0 for fp scalars.
101 Result = Val.getVal();
102 llvm::Value *Zero = Constant::getNullValue(Result->getType());
103 // FIXME: llvm-gcc produces a une comparison: validate this is right.
104 Result = Builder.CreateFCmpUNE(Result, Zero, "tobool");
105 return Result;
106 }
107
108 case BuiltinType::FloatComplex:
109 case BuiltinType::DoubleComplex:
110 case BuiltinType::LongDoubleComplex:
111 assert(0 && "comparisons against complex not implemented yet");
112 }
113 } else {
114 assert((isa<PointerType>(Ty) ||
115 cast<TagType>(Ty)->getDecl()->getKind() == Decl::Enum) &&
116 "Unknown scalar type");
117 // Code below handles this fine.
118 }
119
120 // Usual case for integers, pointers, and enums: compare against zero.
121 Result = Val.getVal();
Chris Lattnera45c5af2007-06-02 19:47:04 +0000122
123 // Because of the type rules of C, we often end up computing a logical value,
124 // then zero extending it to int, then wanting it as a logical value again.
125 // Optimize this common case.
126 if (llvm::ZExtInst *ZI = dyn_cast<ZExtInst>(Result)) {
127 if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
128 Result = ZI->getOperand(0);
129 ZI->eraseFromParent();
130 return Result;
131 }
132 }
133
Chris Lattnerf0106d22007-06-02 19:33:17 +0000134 llvm::Value *Zero = Constant::getNullValue(Result->getType());
135 return Builder.CreateICmpNE(Result, Zero, "tobool");
136}
137
Chris Lattnera45c5af2007-06-02 19:47:04 +0000138//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000139// LValue Expression Emission
Chris Lattnera45c5af2007-06-02 19:47:04 +0000140//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000141
Chris Lattner8394d792007-06-05 20:53:16 +0000142/// EmitLValue - Emit code to compute a designator that specifies the location
143/// of the expression.
144///
145/// This can return one of two things: a simple address or a bitfield
146/// reference. In either case, the LLVM Value* in the LValue structure is
147/// guaranteed to be an LLVM pointer type.
148///
149/// If this returns a bitfield reference, nothing about the pointee type of
150/// the LLVM value is known: For example, it may not be a pointer to an
151/// integer.
152///
153/// If this returns a normal address, and if the lvalue's C type is fixed
154/// size, this method guarantees that the returned pointer type will point to
155/// an LLVM type of the same size of the lvalue's type. If the lvalue has a
156/// variable length type, this is not possible.
157///
Chris Lattnerd7f58862007-06-02 05:24:33 +0000158LValue CodeGenFunction::EmitLValue(const Expr *E) {
159 switch (E->getStmtClass()) {
160 default:
Chris Lattner8394d792007-06-05 20:53:16 +0000161 fprintf(stderr, "Unimplemented lvalue expr!\n");
Chris Lattnerd7f58862007-06-02 05:24:33 +0000162 E->dump();
163 return LValue::getAddr(UndefValue::get(
164 llvm::PointerType::get(llvm::Type::Int32Ty)));
165
166 case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Chris Lattner946aa312007-06-05 03:59:43 +0000167 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattner8394d792007-06-05 20:53:16 +0000168
169
170 case Expr::UnaryOperatorClass:
171 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000172 }
173}
174
Chris Lattner8394d792007-06-05 20:53:16 +0000175/// EmitLoadOfLValue - Given an expression that represents a value lvalue,
176/// this method emits the address of the lvalue, then loads the result as an
177/// rvalue, returning the rvalue.
178RValue CodeGenFunction::EmitLoadOfLValue(const Expr *E) {
179 LValue LV = EmitLValue(E);
180
181 QualType ExprTy = E->getType().getCanonicalType();
182
183 // FIXME: this is silly and obviously wrong for non-scalars.
184 assert(!LV.isBitfield());
185 return RValue::get(Builder.CreateLoad(LV.getAddress(), "tmp"));
186}
187
188/// EmitStoreThroughLValue - Store the specified rvalue into the specified
189/// lvalue, where both are guaranteed to the have the same type, and that type
190/// is 'Ty'.
191void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
192 QualType Ty) {
193 // FIXME: This is obviously bogus.
194 assert(!Dst.isBitfield() && "FIXME: Don't support store to bitfield yet");
195 assert(Src.isScalar() && "FIXME: Don't support store of aggregate yet");
196
197 // TODO: Handle volatility etc.
198 Value *Addr = Dst.getAddress();
199 const llvm::Type *SrcTy = Src.getVal()->getType();
200 const llvm::Type *AddrTy =
201 cast<llvm::PointerType>(Addr->getType())->getElementType();
202
203 if (AddrTy != SrcTy)
204 Addr = Builder.CreateBitCast(Addr, llvm::PointerType::get(SrcTy),
205 "storetmp");
206 Builder.CreateStore(Src.getVal(), Addr);
207}
208
Chris Lattnerd7f58862007-06-02 05:24:33 +0000209
210LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
211 const Decl *D = E->getDecl();
212 if (isa<BlockVarDecl>(D)) {
213 Value *V = LocalDeclMap[D];
214 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
215 return LValue::getAddr(V);
216 }
217 assert(0 && "Unimp declref");
218}
Chris Lattnere47e4402007-06-01 18:02:12 +0000219
Chris Lattner8394d792007-06-05 20:53:16 +0000220LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
221 // __extension__ doesn't affect lvalue-ness.
222 if (E->getOpcode() == UnaryOperator::Extension)
223 return EmitLValue(E->getSubExpr());
224
225 assert(E->getOpcode() == UnaryOperator::Deref &&
226 "'*' is the only unary operator that produces an lvalue");
227 return LValue::getAddr(EmitExpr(E->getSubExpr()).getVal());
228}
229
Chris Lattnere47e4402007-06-01 18:02:12 +0000230//===--------------------------------------------------------------------===//
231// Expression Emission
232//===--------------------------------------------------------------------===//
233
Chris Lattner8394d792007-06-05 20:53:16 +0000234RValue CodeGenFunction::EmitExpr(const Expr *E) {
Chris Lattnere47e4402007-06-01 18:02:12 +0000235 assert(E && "Null expression?");
236
237 switch (E->getStmtClass()) {
238 default:
239 printf("Unimplemented expr!\n");
240 E->dump();
Chris Lattner8394d792007-06-05 20:53:16 +0000241 return RValue::get(UndefValue::get(llvm::Type::Int32Ty));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000242
243 // l-values.
Chris Lattner8394d792007-06-05 20:53:16 +0000244 case Expr::DeclRefExprClass:
245 // FIXME: EnumConstantDecl's are not lvalues. This is wrong for them.
246 return EmitLoadOfLValue(E);
Chris Lattnerd7f58862007-06-02 05:24:33 +0000247
248 // Leaf expressions.
249 case Expr::IntegerLiteralClass:
Chris Lattnere47e4402007-06-01 18:02:12 +0000250 return EmitIntegerLiteral(cast<IntegerLiteral>(E));
Chris Lattnerdb91b162007-06-02 00:16:28 +0000251
Chris Lattnerd7f58862007-06-02 05:24:33 +0000252 // Operators.
253 case Expr::ParenExprClass:
254 return EmitExpr(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000255 case Expr::UnaryOperatorClass:
256 return EmitUnaryOperator(cast<UnaryOperator>(E));
Chris Lattner8394d792007-06-05 20:53:16 +0000257 case Expr::CastExprClass:
258 return EmitCastExpr(cast<CastExpr>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000259 case Expr::BinaryOperatorClass:
Chris Lattnerdb91b162007-06-02 00:16:28 +0000260 return EmitBinaryOperator(cast<BinaryOperator>(E));
Chris Lattnere47e4402007-06-01 18:02:12 +0000261 }
262
263}
264
Chris Lattner8394d792007-06-05 20:53:16 +0000265RValue CodeGenFunction::EmitIntegerLiteral(const IntegerLiteral *E) {
266 return RValue::get(ConstantInt::get(E->getValue()));
Chris Lattnere47e4402007-06-01 18:02:12 +0000267}
268
Chris Lattner8394d792007-06-05 20:53:16 +0000269RValue CodeGenFunction::EmitCastExpr(const CastExpr *E) {
270 QualType SrcTy;
271 RValue Src = EmitExprWithUsualUnaryConversions(E->getSubExpr(), SrcTy);
272
273 // If the destination is void, just evaluate the source.
274 if (E->getType()->isVoidType())
275 return RValue::getAggregate(0);
276
Chris Lattnercf106ab2007-06-06 04:05:39 +0000277 return EmitConversion(Src, SrcTy, E->getType(), E->getLParenLoc());
Chris Lattner8394d792007-06-05 20:53:16 +0000278}
Chris Lattnerf0106d22007-06-02 19:33:17 +0000279
Chris Lattner8394d792007-06-05 20:53:16 +0000280//===----------------------------------------------------------------------===//
281// Unary Operator Emission
282//===----------------------------------------------------------------------===//
283
284RValue CodeGenFunction::EmitExprWithUsualUnaryConversions(const Expr *E,
285 QualType &ResTy) {
Chris Lattner6db1fb82007-06-02 22:49:07 +0000286 ResTy = E->getType().getCanonicalType();
287
288 if (isa<FunctionType>(ResTy)) { // C99 6.3.2.1p4
289 // Functions are promoted to their address.
290 ResTy = getContext().getPointerType(ResTy);
Chris Lattner8394d792007-06-05 20:53:16 +0000291 return RValue::get(EmitLValue(E).getAddress());
Chris Lattner6db1fb82007-06-02 22:49:07 +0000292 } else if (const ArrayType *ary = dyn_cast<ArrayType>(ResTy)) {
293 // C99 6.3.2.1p3
294 ResTy = getContext().getPointerType(ary->getElementType());
295
296 // FIXME: For now we assume that all source arrays map to LLVM arrays. This
297 // will not true when we add support for VLAs.
298 llvm::Value *V = EmitLValue(E).getAddress(); // Bitfields can't be arrays.
299
300 assert(isa<llvm::PointerType>(V->getType()) &&
301 isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
302 ->getElementType()) &&
303 "Doesn't support VLAs yet!");
304 llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
Chris Lattner8394d792007-06-05 20:53:16 +0000305 return RValue::get(Builder.CreateGEP(V, Idx0, Idx0, "arraydecay"));
Chris Lattner6db1fb82007-06-02 22:49:07 +0000306 } else if (ResTy->isPromotableIntegerType()) { // C99 6.3.1.1p2
307 // FIXME: this probably isn't right, pending clarification from Steve.
308 llvm::Value *Val = EmitExpr(E).getVal();
309
Chris Lattner6db1fb82007-06-02 22:49:07 +0000310 // If the input is a signed integer, sign extend to the destination.
311 if (ResTy->isSignedIntegerType()) {
312 Val = Builder.CreateSExt(Val, LLVMIntTy, "promote");
313 } else {
314 // This handles unsigned types, including bool.
315 Val = Builder.CreateZExt(Val, LLVMIntTy, "promote");
316 }
317 ResTy = getContext().IntTy;
318
Chris Lattner8394d792007-06-05 20:53:16 +0000319 return RValue::get(Val);
Chris Lattner6db1fb82007-06-02 22:49:07 +0000320 }
321
322 // Otherwise, this is a float, double, int, struct, etc.
323 return EmitExpr(E);
324}
325
326
Chris Lattner8394d792007-06-05 20:53:16 +0000327RValue CodeGenFunction::EmitUnaryOperator(const UnaryOperator *E) {
Chris Lattnerf0106d22007-06-02 19:33:17 +0000328 switch (E->getOpcode()) {
329 default:
330 printf("Unimplemented unary expr!\n");
331 E->dump();
Chris Lattner8394d792007-06-05 20:53:16 +0000332 return RValue::get(UndefValue::get(llvm::Type::Int32Ty));
333 // FIXME: pre/post inc/dec
334 case UnaryOperator::AddrOf: return EmitUnaryAddrOf(E);
335 case UnaryOperator::Deref : return EmitLoadOfLValue(E);
336 case UnaryOperator::Plus : return EmitUnaryPlus(E);
337 case UnaryOperator::Minus : return EmitUnaryMinus(E);
338 case UnaryOperator::Not : return EmitUnaryNot(E);
339 case UnaryOperator::LNot : return EmitUnaryLNot(E);
340 // FIXME: SIZEOF/ALIGNOF(expr).
341 // FIXME: real/imag
342 case UnaryOperator::Extension: return EmitExpr(E->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000343 }
344}
345
Chris Lattner8394d792007-06-05 20:53:16 +0000346/// C99 6.5.3.2
347RValue CodeGenFunction::EmitUnaryAddrOf(const UnaryOperator *E) {
348 // The address of the operand is just its lvalue. It cannot be a bitfield.
349 return RValue::get(EmitLValue(E->getSubExpr()).getAddress());
350}
351
352RValue CodeGenFunction::EmitUnaryPlus(const UnaryOperator *E) {
353 // Unary plus just performs promotions on its arithmetic operand.
354 QualType Ty;
355 return EmitExprWithUsualUnaryConversions(E, Ty);
356}
357
358RValue CodeGenFunction::EmitUnaryMinus(const UnaryOperator *E) {
359 // Unary minus performs promotions, then negates its arithmetic operand.
360 QualType Ty;
361 RValue V = EmitExprWithUsualUnaryConversions(E, Ty);
Chris Lattnerf0106d22007-06-02 19:33:17 +0000362
Chris Lattner8394d792007-06-05 20:53:16 +0000363 if (V.isScalar())
364 return RValue::get(Builder.CreateNeg(V.getVal(), "neg"));
365
366 assert(0 && "FIXME: This doesn't handle complex operands yet");
367}
368
369RValue CodeGenFunction::EmitUnaryNot(const UnaryOperator *E) {
370 // Unary not performs promotions, then complements its integer operand.
371 QualType Ty;
372 RValue V = EmitExprWithUsualUnaryConversions(E, Ty);
373
374 if (V.isScalar())
375 return RValue::get(Builder.CreateNot(V.getVal(), "neg"));
376
377 assert(0 && "FIXME: This doesn't handle integer complex operands yet (GNU)");
378}
379
380
381/// C99 6.5.3.3
382RValue CodeGenFunction::EmitUnaryLNot(const UnaryOperator *E) {
383 // Compare operand to zero.
384 Value *BoolVal = EvaluateExprAsBool(E->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000385
386 // Invert value.
Chris Lattnera45c5af2007-06-02 19:47:04 +0000387 // TODO: Could dynamically modify easy computations here. For example, if
388 // the operand is an icmp ne, turn into icmp eq.
Chris Lattnerf0106d22007-06-02 19:33:17 +0000389 BoolVal = Builder.CreateNot(BoolVal, "lnot");
390
391 // ZExt result to int.
Chris Lattner8394d792007-06-05 20:53:16 +0000392 return RValue::get(Builder.CreateZExt(BoolVal, LLVMIntTy, "lnot.ext"));
Chris Lattnerf0106d22007-06-02 19:33:17 +0000393}
394
Chris Lattnere47e4402007-06-01 18:02:12 +0000395
Chris Lattnerdb91b162007-06-02 00:16:28 +0000396//===--------------------------------------------------------------------===//
397// Binary Operator Emission
398//===--------------------------------------------------------------------===//
399
400// FIXME describe.
Chris Lattnercf250242007-06-03 02:02:44 +0000401QualType CodeGenFunction::
Chris Lattner8394d792007-06-05 20:53:16 +0000402EmitUsualArithmeticConversions(const BinaryOperator *E, RValue &LHS,
403 RValue &RHS) {
Chris Lattnerc18f9d12007-06-02 22:51:30 +0000404 QualType LHSType, RHSType;
405 LHS = EmitExprWithUsualUnaryConversions(E->getLHS(), LHSType);
406 RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), RHSType);
407
Chris Lattnercf250242007-06-03 02:02:44 +0000408 // If both operands have the same source type, we're done already.
409 if (LHSType == RHSType) return LHSType;
410
411 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
412 // The caller can deal with this (e.g. pointer + int).
413 if (!LHSType->isArithmeticType() || !RHSType->isArithmeticType())
414 return LHSType;
415
416 // At this point, we have two different arithmetic types.
417
418 // Handle complex types first (C99 6.3.1.8p1).
419 if (LHSType->isComplexType() || RHSType->isComplexType()) {
420 assert(0 && "FIXME: complex types unimp");
421#if 0
422 // if we have an integer operand, the result is the complex type.
423 if (rhs->isIntegerType())
424 return lhs;
425 if (lhs->isIntegerType())
426 return rhs;
427 return Context.maxComplexType(lhs, rhs);
428#endif
429 }
430
431 // If neither operand is complex, they must be scalars.
432 llvm::Value *LHSV = LHS.getVal();
433 llvm::Value *RHSV = RHS.getVal();
434
435 // If the LLVM types are already equal, then they only differed in sign, or it
436 // was something like char/signed char or double/long double.
437 if (LHSV->getType() == RHSV->getType())
438 return LHSType;
439
440 // Now handle "real" floating types (i.e. float, double, long double).
441 if (LHSType->isRealFloatingType() || RHSType->isRealFloatingType()) {
442 // if we have an integer operand, the result is the real floating type, and
443 // the integer converts to FP.
444 if (RHSType->isIntegerType()) {
445 // Promote the RHS to an FP type of the LHS, with the sign following the
446 // RHS.
447 if (RHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000448 RHS = RValue::get(Builder.CreateSIToFP(RHSV,LHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000449 else
Chris Lattner8394d792007-06-05 20:53:16 +0000450 RHS = RValue::get(Builder.CreateUIToFP(RHSV,LHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000451 return LHSType;
452 }
453
454 if (LHSType->isIntegerType()) {
455 // Promote the LHS to an FP type of the RHS, with the sign following the
456 // LHS.
457 if (LHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000458 LHS = RValue::get(Builder.CreateSIToFP(LHSV,RHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000459 else
Chris Lattner8394d792007-06-05 20:53:16 +0000460 LHS = RValue::get(Builder.CreateUIToFP(LHSV,RHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000461 return RHSType;
462 }
463
464 // Otherwise, they are two FP types. Promote the smaller operand to the
465 // bigger result.
466 QualType BiggerType = ASTContext::maxFloatingType(LHSType, RHSType);
467
468 if (BiggerType == LHSType)
Chris Lattner8394d792007-06-05 20:53:16 +0000469 RHS = RValue::get(Builder.CreateFPExt(RHSV, LHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000470 else
Chris Lattner8394d792007-06-05 20:53:16 +0000471 LHS = RValue::get(Builder.CreateFPExt(LHSV, RHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000472 return BiggerType;
473 }
474
475 // Finally, we have two integer types that are different according to C. Do
476 // a sign or zero extension if needed.
477
478 // Otherwise, one type is smaller than the other.
479 QualType ResTy = ASTContext::maxIntegerType(LHSType, RHSType);
480
481 if (LHSType == ResTy) {
482 if (RHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000483 RHS = RValue::get(Builder.CreateSExt(RHSV, LHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000484 else
Chris Lattner8394d792007-06-05 20:53:16 +0000485 RHS = RValue::get(Builder.CreateZExt(RHSV, LHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000486 } else {
487 assert(RHSType == ResTy && "Unknown conversion");
488 if (LHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000489 LHS = RValue::get(Builder.CreateSExt(LHSV, RHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000490 else
Chris Lattner8394d792007-06-05 20:53:16 +0000491 LHS = RValue::get(Builder.CreateZExt(LHSV, RHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000492 }
493 return ResTy;
Chris Lattnerdb91b162007-06-02 00:16:28 +0000494}
495
496
Chris Lattner8394d792007-06-05 20:53:16 +0000497RValue CodeGenFunction::EmitBinaryOperator(const BinaryOperator *E) {
Chris Lattnerdb91b162007-06-02 00:16:28 +0000498 switch (E->getOpcode()) {
499 default:
Chris Lattner8394d792007-06-05 20:53:16 +0000500 fprintf(stderr, "Unimplemented expr!\n");
Chris Lattnerdb91b162007-06-02 00:16:28 +0000501 E->dump();
Chris Lattner8394d792007-06-05 20:53:16 +0000502 return RValue::get(UndefValue::get(llvm::Type::Int32Ty));
503 case BinaryOperator::Mul: return EmitBinaryMul(E);
504 case BinaryOperator::Div: return EmitBinaryDiv(E);
505 case BinaryOperator::Rem: return EmitBinaryRem(E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000506 case BinaryOperator::Add: return EmitBinaryAdd(E);
Chris Lattner8394d792007-06-05 20:53:16 +0000507 case BinaryOperator::Sub: return EmitBinarySub(E);
508 case BinaryOperator::Shl: return EmitBinaryShl(E);
509 case BinaryOperator::Shr: return EmitBinaryShr(E);
510
511 // FIXME: relational
512
513 case BinaryOperator::And: return EmitBinaryAnd(E);
514 case BinaryOperator::Xor: return EmitBinaryXor(E);
515 case BinaryOperator::Or : return EmitBinaryOr(E);
516 case BinaryOperator::LAnd: return EmitBinaryLAnd(E);
517 case BinaryOperator::LOr: return EmitBinaryLOr(E);
518
519 case BinaryOperator::Assign: return EmitBinaryAssign(E);
520 // FIXME: Assignment.
521 case BinaryOperator::Comma: return EmitBinaryComma(E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000522 }
523}
524
Chris Lattner8394d792007-06-05 20:53:16 +0000525RValue CodeGenFunction::EmitBinaryMul(const BinaryOperator *E) {
526 RValue LHS, RHS;
527 EmitUsualArithmeticConversions(E, LHS, RHS);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000528
Chris Lattner8394d792007-06-05 20:53:16 +0000529 if (LHS.isScalar())
530 return RValue::get(Builder.CreateMul(LHS.getVal(), RHS.getVal(), "mul"));
531
532 assert(0 && "FIXME: This doesn't handle complex operands yet");
533}
534
535RValue CodeGenFunction::EmitBinaryDiv(const BinaryOperator *E) {
536 RValue LHS, RHS;
537 EmitUsualArithmeticConversions(E, LHS, RHS);
538
539 if (LHS.isScalar()) {
540 Value *RV;
541 if (LHS.getVal()->getType()->isFloatingPoint())
542 RV = Builder.CreateFDiv(LHS.getVal(), RHS.getVal(), "div");
543 else if (E->getType()->isUnsignedIntegerType())
544 RV = Builder.CreateUDiv(LHS.getVal(), RHS.getVal(), "div");
545 else
546 RV = Builder.CreateSDiv(LHS.getVal(), RHS.getVal(), "div");
547 return RValue::get(RV);
548 }
549 assert(0 && "FIXME: This doesn't handle complex operands yet");
550}
551
552RValue CodeGenFunction::EmitBinaryRem(const BinaryOperator *E) {
553 RValue LHS, RHS;
554 EmitUsualArithmeticConversions(E, LHS, RHS);
555
556 if (LHS.isScalar()) {
557 Value *RV;
558 // Rem in C can't be a floating point type: C99 6.5.5p2.
559 if (E->getType()->isUnsignedIntegerType())
560 RV = Builder.CreateURem(LHS.getVal(), RHS.getVal(), "rem");
561 else
562 RV = Builder.CreateSRem(LHS.getVal(), RHS.getVal(), "rem");
563 return RValue::get(RV);
564 }
565
566 assert(0 && "FIXME: This doesn't handle complex operands yet");
567}
568
569RValue CodeGenFunction::EmitBinaryAdd(const BinaryOperator *E) {
570 RValue LHS, RHS;
Chris Lattnerdb91b162007-06-02 00:16:28 +0000571 EmitUsualArithmeticConversions(E, LHS, RHS);
572
Chris Lattner8394d792007-06-05 20:53:16 +0000573 // FIXME: This doesn't handle ptr+int etc yet.
574
575 if (LHS.isScalar())
576 return RValue::get(Builder.CreateAdd(LHS.getVal(), RHS.getVal(), "add"));
577
578 assert(0 && "FIXME: This doesn't handle complex operands yet");
579
580}
581
582RValue CodeGenFunction::EmitBinarySub(const BinaryOperator *E) {
583 RValue LHS, RHS;
584 EmitUsualArithmeticConversions(E, LHS, RHS);
585
586 // FIXME: This doesn't handle ptr-int or ptr-ptr, etc yet.
587
588 if (LHS.isScalar())
589 return RValue::get(Builder.CreateSub(LHS.getVal(), RHS.getVal(), "sub"));
590
591 assert(0 && "FIXME: This doesn't handle complex operands yet");
592
593}
594
595RValue CodeGenFunction::EmitBinaryShl(const BinaryOperator *E) {
596 // For shifts, integer promotions are performed, but the usual arithmetic
597 // conversions are not. The LHS and RHS need not have the same type.
598
599 QualType ResTy;
600 Value *LHS = EmitExprWithUsualUnaryConversions(E->getLHS(), ResTy).getVal();
601 Value *RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), ResTy).getVal();
602
603 // LLVM requires the LHS and RHS to be the same type, promote or truncate the
604 // RHS to the same size as the LHS.
605 if (LHS->getType() != RHS->getType())
606 RHS = Builder.CreateIntCast(RHS, LHS->getType(), false, "sh_prom");
607
608 return RValue::get(Builder.CreateShl(LHS, RHS, "shl"));
609}
610
611RValue CodeGenFunction::EmitBinaryShr(const BinaryOperator *E) {
612 // For shifts, integer promotions are performed, but the usual arithmetic
613 // conversions are not. The LHS and RHS need not have the same type.
614
615 QualType ResTy;
616 Value *LHS = EmitExprWithUsualUnaryConversions(E->getLHS(), ResTy).getVal();
617 Value *RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), ResTy).getVal();
618
619 // LLVM requires the LHS and RHS to be the same type, promote or truncate the
620 // RHS to the same size as the LHS.
621 if (LHS->getType() != RHS->getType())
622 RHS = Builder.CreateIntCast(RHS, LHS->getType(), false, "sh_prom");
623
624 if (E->getType()->isUnsignedIntegerType())
625 return RValue::get(Builder.CreateLShr(LHS, RHS, "shr"));
626 else
627 return RValue::get(Builder.CreateAShr(LHS, RHS, "shr"));
628}
629
630RValue CodeGenFunction::EmitBinaryAnd(const BinaryOperator *E) {
631 RValue LHS, RHS;
632 EmitUsualArithmeticConversions(E, LHS, RHS);
633
634 if (LHS.isScalar())
635 return RValue::get(Builder.CreateAnd(LHS.getVal(), RHS.getVal(), "and"));
636
637 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
638}
639
640RValue CodeGenFunction::EmitBinaryXor(const BinaryOperator *E) {
641 RValue LHS, RHS;
642 EmitUsualArithmeticConversions(E, LHS, RHS);
643
644 if (LHS.isScalar())
645 return RValue::get(Builder.CreateXor(LHS.getVal(), RHS.getVal(), "xor"));
646
647 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
648}
649
650RValue CodeGenFunction::EmitBinaryOr(const BinaryOperator *E) {
651 RValue LHS, RHS;
652 EmitUsualArithmeticConversions(E, LHS, RHS);
653
654 if (LHS.isScalar())
655 return RValue::get(Builder.CreateOr(LHS.getVal(), RHS.getVal(), "or"));
656
657 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
658}
659
660RValue CodeGenFunction::EmitBinaryLAnd(const BinaryOperator *E) {
661 Value *LHSCond = EvaluateExprAsBool(E->getLHS());
662
663 BasicBlock *ContBlock = new BasicBlock("land_cont");
664 BasicBlock *RHSBlock = new BasicBlock("land_rhs");
665
666 BasicBlock *OrigBlock = Builder.GetInsertBlock();
667 Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock);
668
669 EmitBlock(RHSBlock);
670 Value *RHSCond = EvaluateExprAsBool(E->getRHS());
671
672 // Reaquire the RHS block, as there may be subblocks inserted.
673 RHSBlock = Builder.GetInsertBlock();
674 EmitBlock(ContBlock);
675
676 // Create a PHI node. If we just evaluted the LHS condition, the result is
677 // false. If we evaluated both, the result is the RHS condition.
678 PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land");
679 PN->reserveOperandSpace(2);
680 PN->addIncoming(ConstantInt::getFalse(), OrigBlock);
681 PN->addIncoming(RHSCond, RHSBlock);
682
683 // ZExt result to int.
684 return RValue::get(Builder.CreateZExt(PN, LLVMIntTy, "land.ext"));
685}
686
687RValue CodeGenFunction::EmitBinaryLOr(const BinaryOperator *E) {
688 Value *LHSCond = EvaluateExprAsBool(E->getLHS());
689
690 BasicBlock *ContBlock = new BasicBlock("lor_cont");
691 BasicBlock *RHSBlock = new BasicBlock("lor_rhs");
692
693 BasicBlock *OrigBlock = Builder.GetInsertBlock();
694 Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock);
695
696 EmitBlock(RHSBlock);
697 Value *RHSCond = EvaluateExprAsBool(E->getRHS());
698
699 // Reaquire the RHS block, as there may be subblocks inserted.
700 RHSBlock = Builder.GetInsertBlock();
701 EmitBlock(ContBlock);
702
703 // Create a PHI node. If we just evaluted the LHS condition, the result is
704 // true. If we evaluated both, the result is the RHS condition.
705 PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor");
706 PN->reserveOperandSpace(2);
707 PN->addIncoming(ConstantInt::getTrue(), OrigBlock);
708 PN->addIncoming(RHSCond, RHSBlock);
709
710 // ZExt result to int.
711 return RValue::get(Builder.CreateZExt(PN, LLVMIntTy, "lor.ext"));
712}
713
714RValue CodeGenFunction::EmitBinaryAssign(const BinaryOperator *E) {
715 LValue LHS = EmitLValue(E->getLHS());
716
717 QualType RHSTy;
718 RValue RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), RHSTy);
719
720 // Convert the RHS to the type of the LHS.
Chris Lattnercf106ab2007-06-06 04:05:39 +0000721 // FIXME: I'm not thrilled about having to call getLocStart() here... :(
722 RHS = EmitConversion(RHS, RHSTy, E->getType(), E->getLocStart());
Chris Lattner8394d792007-06-05 20:53:16 +0000723
724 // Store the value into the LHS.
725 EmitStoreThroughLValue(RHS, LHS, E->getType());
726
727 // Return the converted RHS.
728 return RHS;
729}
730
731
732RValue CodeGenFunction::EmitBinaryComma(const BinaryOperator *E) {
733 EmitExpr(E->getLHS());
734 return EmitExpr(E->getRHS());
735}