blob: ad84f41364b1efcbff765092bbac6d000b7d8bcb [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;
Chris Lattner83b484b2007-06-06 04:39:08 +000046
47 // Handle conversions to bool first, they are special: comparisons against 0.
48 if (const BuiltinType *DestBT = dyn_cast<BuiltinType>(DstTy))
49 if (DestBT->getKind() == BuiltinType::Bool)
50 return RValue::get(ConvertScalarValueToBool(Val, ValTy));
Chris Lattner8394d792007-06-05 20:53:16 +000051
Chris Lattner83b484b2007-06-06 04:39:08 +000052 // Handle pointer conversions next: pointers can only be converted to/from
53 // other pointers and integers.
Chris Lattnercf106ab2007-06-06 04:05:39 +000054 if (isa<PointerType>(DstTy)) {
55 const llvm::Type *DestTy = ConvertType(DstTy, Loc);
56
57 // The source value may be an integer, or a pointer.
58 assert(Val.isScalar() && "Can only convert from integer or pointer");
59 if (isa<llvm::PointerType>(Val.getVal()->getType()))
60 return RValue::get(Builder.CreateBitCast(Val.getVal(), DestTy, "conv"));
61 assert(ValTy->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
62 return RValue::get(Builder.CreatePtrToInt(Val.getVal(), DestTy, "conv"));
Chris Lattner83b484b2007-06-06 04:39:08 +000063 }
64
65 if (isa<PointerType>(ValTy)) {
Chris Lattnercf106ab2007-06-06 04:05:39 +000066 // Must be an ptr to int cast.
67 const llvm::Type *DestTy = ConvertType(DstTy, Loc);
68 assert(isa<llvm::IntegerType>(DestTy) && "not ptr->int?");
69 return RValue::get(Builder.CreateIntToPtr(Val.getVal(), DestTy, "conv"));
Chris Lattner8394d792007-06-05 20:53:16 +000070 }
Chris Lattner83b484b2007-06-06 04:39:08 +000071
72 // Finally, we have the arithmetic types: real int/float and complex
73 // int/float. Handle real->real conversions first, they are the most
74 // common.
75 if (Val.isScalar() && DstTy->isRealType()) {
76 // We know that these are representable as scalars in LLVM, convert to LLVM
77 // types since they are easier to reason about.
78 Value *SrcVal = Val.getVal();
79 const llvm::Type *DestTy = ConvertType(DstTy, Loc);
80 if (SrcVal->getType() == DestTy) return Val;
81
82 Value *Result;
83 if (isa<llvm::IntegerType>(SrcVal->getType())) {
84 bool InputSigned = ValTy->isSignedIntegerType();
85 if (isa<llvm::IntegerType>(DestTy))
86 Result = Builder.CreateIntCast(SrcVal, DestTy, InputSigned, "conv");
87 else if (InputSigned)
88 Result = Builder.CreateSIToFP(SrcVal, DestTy, "conv");
89 else
90 Result = Builder.CreateUIToFP(SrcVal, DestTy, "conv");
91 } else {
92 assert(SrcVal->getType()->isFloatingPoint() && "Unknown real conversion");
93 if (isa<llvm::IntegerType>(DestTy)) {
94 if (DstTy->isSignedIntegerType())
95 Result = Builder.CreateFPToSI(SrcVal, DestTy, "conv");
96 else
97 Result = Builder.CreateFPToUI(SrcVal, DestTy, "conv");
98 } else {
99 assert(DestTy->isFloatingPoint() && "Unknown real conversion");
100 if (DestTy->getTypeID() < SrcVal->getType()->getTypeID())
101 Result = Builder.CreateFPTrunc(SrcVal, DestTy, "conv");
102 else
103 Result = Builder.CreateFPExt(SrcVal, DestTy, "conv");
104 }
105 }
106 return RValue::get(Result);
107 }
108
109 assert(0 && "FIXME: We don't support complex conversions yet!");
Chris Lattner8394d792007-06-05 20:53:16 +0000110}
111
112
113/// ConvertScalarValueToBool - Convert the specified expression value to a
Chris Lattnerf0106d22007-06-02 19:33:17 +0000114/// boolean (i1) truth value. This is equivalent to "Val == 0".
Chris Lattner8394d792007-06-05 20:53:16 +0000115Value *CodeGenFunction::ConvertScalarValueToBool(RValue Val, QualType Ty) {
Chris Lattnerf0106d22007-06-02 19:33:17 +0000116 Ty = Ty.getCanonicalType();
117 Value *Result;
118 if (const BuiltinType *BT = dyn_cast<BuiltinType>(Ty)) {
119 switch (BT->getKind()) {
120 default: assert(0 && "Unknown scalar value");
121 case BuiltinType::Bool:
122 Result = Val.getVal();
123 // Bool is already evaluated right.
124 assert(Result->getType() == llvm::Type::Int1Ty &&
125 "Unexpected bool value type!");
126 return Result;
Chris Lattnerb16f4552007-06-03 07:25:34 +0000127 case BuiltinType::Char_S:
128 case BuiltinType::Char_U:
Chris Lattnerf0106d22007-06-02 19:33:17 +0000129 case BuiltinType::SChar:
130 case BuiltinType::UChar:
131 case BuiltinType::Short:
132 case BuiltinType::UShort:
133 case BuiltinType::Int:
134 case BuiltinType::UInt:
135 case BuiltinType::Long:
136 case BuiltinType::ULong:
137 case BuiltinType::LongLong:
138 case BuiltinType::ULongLong:
139 // Code below handles simple integers.
140 break;
141 case BuiltinType::Float:
142 case BuiltinType::Double:
143 case BuiltinType::LongDouble: {
144 // Compare against 0.0 for fp scalars.
145 Result = Val.getVal();
146 llvm::Value *Zero = Constant::getNullValue(Result->getType());
147 // FIXME: llvm-gcc produces a une comparison: validate this is right.
148 Result = Builder.CreateFCmpUNE(Result, Zero, "tobool");
149 return Result;
150 }
151
152 case BuiltinType::FloatComplex:
153 case BuiltinType::DoubleComplex:
154 case BuiltinType::LongDoubleComplex:
155 assert(0 && "comparisons against complex not implemented yet");
156 }
157 } else {
158 assert((isa<PointerType>(Ty) ||
159 cast<TagType>(Ty)->getDecl()->getKind() == Decl::Enum) &&
160 "Unknown scalar type");
161 // Code below handles this fine.
162 }
163
164 // Usual case for integers, pointers, and enums: compare against zero.
165 Result = Val.getVal();
Chris Lattnera45c5af2007-06-02 19:47:04 +0000166
167 // Because of the type rules of C, we often end up computing a logical value,
168 // then zero extending it to int, then wanting it as a logical value again.
169 // Optimize this common case.
170 if (llvm::ZExtInst *ZI = dyn_cast<ZExtInst>(Result)) {
171 if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
172 Result = ZI->getOperand(0);
173 ZI->eraseFromParent();
174 return Result;
175 }
176 }
177
Chris Lattnerf0106d22007-06-02 19:33:17 +0000178 llvm::Value *Zero = Constant::getNullValue(Result->getType());
179 return Builder.CreateICmpNE(Result, Zero, "tobool");
180}
181
Chris Lattnera45c5af2007-06-02 19:47:04 +0000182//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000183// LValue Expression Emission
Chris Lattnera45c5af2007-06-02 19:47:04 +0000184//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000185
Chris Lattner8394d792007-06-05 20:53:16 +0000186/// EmitLValue - Emit code to compute a designator that specifies the location
187/// of the expression.
188///
189/// This can return one of two things: a simple address or a bitfield
190/// reference. In either case, the LLVM Value* in the LValue structure is
191/// guaranteed to be an LLVM pointer type.
192///
193/// If this returns a bitfield reference, nothing about the pointee type of
194/// the LLVM value is known: For example, it may not be a pointer to an
195/// integer.
196///
197/// If this returns a normal address, and if the lvalue's C type is fixed
198/// size, this method guarantees that the returned pointer type will point to
199/// an LLVM type of the same size of the lvalue's type. If the lvalue has a
200/// variable length type, this is not possible.
201///
Chris Lattnerd7f58862007-06-02 05:24:33 +0000202LValue CodeGenFunction::EmitLValue(const Expr *E) {
203 switch (E->getStmtClass()) {
204 default:
Chris Lattner8394d792007-06-05 20:53:16 +0000205 fprintf(stderr, "Unimplemented lvalue expr!\n");
Chris Lattnerd7f58862007-06-02 05:24:33 +0000206 E->dump();
207 return LValue::getAddr(UndefValue::get(
208 llvm::PointerType::get(llvm::Type::Int32Ty)));
209
210 case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Chris Lattner946aa312007-06-05 03:59:43 +0000211 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattner8394d792007-06-05 20:53:16 +0000212
213
214 case Expr::UnaryOperatorClass:
215 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000216 }
217}
218
Chris Lattner8394d792007-06-05 20:53:16 +0000219/// EmitLoadOfLValue - Given an expression that represents a value lvalue,
220/// this method emits the address of the lvalue, then loads the result as an
221/// rvalue, returning the rvalue.
222RValue CodeGenFunction::EmitLoadOfLValue(const Expr *E) {
223 LValue LV = EmitLValue(E);
224
225 QualType ExprTy = E->getType().getCanonicalType();
226
227 // FIXME: this is silly and obviously wrong for non-scalars.
228 assert(!LV.isBitfield());
229 return RValue::get(Builder.CreateLoad(LV.getAddress(), "tmp"));
230}
231
232/// EmitStoreThroughLValue - Store the specified rvalue into the specified
233/// lvalue, where both are guaranteed to the have the same type, and that type
234/// is 'Ty'.
235void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
236 QualType Ty) {
237 // FIXME: This is obviously bogus.
238 assert(!Dst.isBitfield() && "FIXME: Don't support store to bitfield yet");
239 assert(Src.isScalar() && "FIXME: Don't support store of aggregate yet");
240
241 // TODO: Handle volatility etc.
242 Value *Addr = Dst.getAddress();
243 const llvm::Type *SrcTy = Src.getVal()->getType();
244 const llvm::Type *AddrTy =
245 cast<llvm::PointerType>(Addr->getType())->getElementType();
246
247 if (AddrTy != SrcTy)
248 Addr = Builder.CreateBitCast(Addr, llvm::PointerType::get(SrcTy),
249 "storetmp");
250 Builder.CreateStore(Src.getVal(), Addr);
251}
252
Chris Lattnerd7f58862007-06-02 05:24:33 +0000253
254LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
255 const Decl *D = E->getDecl();
256 if (isa<BlockVarDecl>(D)) {
257 Value *V = LocalDeclMap[D];
258 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
259 return LValue::getAddr(V);
260 }
261 assert(0 && "Unimp declref");
262}
Chris Lattnere47e4402007-06-01 18:02:12 +0000263
Chris Lattner8394d792007-06-05 20:53:16 +0000264LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
265 // __extension__ doesn't affect lvalue-ness.
266 if (E->getOpcode() == UnaryOperator::Extension)
267 return EmitLValue(E->getSubExpr());
268
269 assert(E->getOpcode() == UnaryOperator::Deref &&
270 "'*' is the only unary operator that produces an lvalue");
271 return LValue::getAddr(EmitExpr(E->getSubExpr()).getVal());
272}
273
Chris Lattnere47e4402007-06-01 18:02:12 +0000274//===--------------------------------------------------------------------===//
275// Expression Emission
276//===--------------------------------------------------------------------===//
277
Chris Lattner8394d792007-06-05 20:53:16 +0000278RValue CodeGenFunction::EmitExpr(const Expr *E) {
Chris Lattnere47e4402007-06-01 18:02:12 +0000279 assert(E && "Null expression?");
280
281 switch (E->getStmtClass()) {
282 default:
283 printf("Unimplemented expr!\n");
284 E->dump();
Chris Lattner8394d792007-06-05 20:53:16 +0000285 return RValue::get(UndefValue::get(llvm::Type::Int32Ty));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000286
287 // l-values.
Chris Lattner8394d792007-06-05 20:53:16 +0000288 case Expr::DeclRefExprClass:
289 // FIXME: EnumConstantDecl's are not lvalues. This is wrong for them.
290 return EmitLoadOfLValue(E);
Chris Lattnerd7f58862007-06-02 05:24:33 +0000291
292 // Leaf expressions.
293 case Expr::IntegerLiteralClass:
Chris Lattnere47e4402007-06-01 18:02:12 +0000294 return EmitIntegerLiteral(cast<IntegerLiteral>(E));
Chris Lattnerdb91b162007-06-02 00:16:28 +0000295
Chris Lattnerd7f58862007-06-02 05:24:33 +0000296 // Operators.
297 case Expr::ParenExprClass:
298 return EmitExpr(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000299 case Expr::UnaryOperatorClass:
300 return EmitUnaryOperator(cast<UnaryOperator>(E));
Chris Lattner8394d792007-06-05 20:53:16 +0000301 case Expr::CastExprClass:
302 return EmitCastExpr(cast<CastExpr>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000303 case Expr::BinaryOperatorClass:
Chris Lattnerdb91b162007-06-02 00:16:28 +0000304 return EmitBinaryOperator(cast<BinaryOperator>(E));
Chris Lattnere47e4402007-06-01 18:02:12 +0000305 }
306
307}
308
Chris Lattner8394d792007-06-05 20:53:16 +0000309RValue CodeGenFunction::EmitIntegerLiteral(const IntegerLiteral *E) {
310 return RValue::get(ConstantInt::get(E->getValue()));
Chris Lattnere47e4402007-06-01 18:02:12 +0000311}
312
Chris Lattner8394d792007-06-05 20:53:16 +0000313RValue CodeGenFunction::EmitCastExpr(const CastExpr *E) {
314 QualType SrcTy;
315 RValue Src = EmitExprWithUsualUnaryConversions(E->getSubExpr(), SrcTy);
316
317 // If the destination is void, just evaluate the source.
318 if (E->getType()->isVoidType())
319 return RValue::getAggregate(0);
320
Chris Lattnercf106ab2007-06-06 04:05:39 +0000321 return EmitConversion(Src, SrcTy, E->getType(), E->getLParenLoc());
Chris Lattner8394d792007-06-05 20:53:16 +0000322}
Chris Lattnerf0106d22007-06-02 19:33:17 +0000323
Chris Lattner8394d792007-06-05 20:53:16 +0000324//===----------------------------------------------------------------------===//
325// Unary Operator Emission
326//===----------------------------------------------------------------------===//
327
328RValue CodeGenFunction::EmitExprWithUsualUnaryConversions(const Expr *E,
329 QualType &ResTy) {
Chris Lattner6db1fb82007-06-02 22:49:07 +0000330 ResTy = E->getType().getCanonicalType();
331
332 if (isa<FunctionType>(ResTy)) { // C99 6.3.2.1p4
333 // Functions are promoted to their address.
334 ResTy = getContext().getPointerType(ResTy);
Chris Lattner8394d792007-06-05 20:53:16 +0000335 return RValue::get(EmitLValue(E).getAddress());
Chris Lattner6db1fb82007-06-02 22:49:07 +0000336 } else if (const ArrayType *ary = dyn_cast<ArrayType>(ResTy)) {
337 // C99 6.3.2.1p3
338 ResTy = getContext().getPointerType(ary->getElementType());
339
340 // FIXME: For now we assume that all source arrays map to LLVM arrays. This
341 // will not true when we add support for VLAs.
342 llvm::Value *V = EmitLValue(E).getAddress(); // Bitfields can't be arrays.
343
344 assert(isa<llvm::PointerType>(V->getType()) &&
345 isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
346 ->getElementType()) &&
347 "Doesn't support VLAs yet!");
348 llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
Chris Lattner8394d792007-06-05 20:53:16 +0000349 return RValue::get(Builder.CreateGEP(V, Idx0, Idx0, "arraydecay"));
Chris Lattner6db1fb82007-06-02 22:49:07 +0000350 } else if (ResTy->isPromotableIntegerType()) { // C99 6.3.1.1p2
351 // FIXME: this probably isn't right, pending clarification from Steve.
352 llvm::Value *Val = EmitExpr(E).getVal();
353
Chris Lattner6db1fb82007-06-02 22:49:07 +0000354 // If the input is a signed integer, sign extend to the destination.
355 if (ResTy->isSignedIntegerType()) {
356 Val = Builder.CreateSExt(Val, LLVMIntTy, "promote");
357 } else {
358 // This handles unsigned types, including bool.
359 Val = Builder.CreateZExt(Val, LLVMIntTy, "promote");
360 }
361 ResTy = getContext().IntTy;
362
Chris Lattner8394d792007-06-05 20:53:16 +0000363 return RValue::get(Val);
Chris Lattner6db1fb82007-06-02 22:49:07 +0000364 }
365
366 // Otherwise, this is a float, double, int, struct, etc.
367 return EmitExpr(E);
368}
369
370
Chris Lattner8394d792007-06-05 20:53:16 +0000371RValue CodeGenFunction::EmitUnaryOperator(const UnaryOperator *E) {
Chris Lattnerf0106d22007-06-02 19:33:17 +0000372 switch (E->getOpcode()) {
373 default:
374 printf("Unimplemented unary expr!\n");
375 E->dump();
Chris Lattner8394d792007-06-05 20:53:16 +0000376 return RValue::get(UndefValue::get(llvm::Type::Int32Ty));
377 // FIXME: pre/post inc/dec
378 case UnaryOperator::AddrOf: return EmitUnaryAddrOf(E);
379 case UnaryOperator::Deref : return EmitLoadOfLValue(E);
380 case UnaryOperator::Plus : return EmitUnaryPlus(E);
381 case UnaryOperator::Minus : return EmitUnaryMinus(E);
382 case UnaryOperator::Not : return EmitUnaryNot(E);
383 case UnaryOperator::LNot : return EmitUnaryLNot(E);
384 // FIXME: SIZEOF/ALIGNOF(expr).
385 // FIXME: real/imag
386 case UnaryOperator::Extension: return EmitExpr(E->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000387 }
388}
389
Chris Lattner8394d792007-06-05 20:53:16 +0000390/// C99 6.5.3.2
391RValue CodeGenFunction::EmitUnaryAddrOf(const UnaryOperator *E) {
392 // The address of the operand is just its lvalue. It cannot be a bitfield.
393 return RValue::get(EmitLValue(E->getSubExpr()).getAddress());
394}
395
396RValue CodeGenFunction::EmitUnaryPlus(const UnaryOperator *E) {
397 // Unary plus just performs promotions on its arithmetic operand.
398 QualType Ty;
399 return EmitExprWithUsualUnaryConversions(E, Ty);
400}
401
402RValue CodeGenFunction::EmitUnaryMinus(const UnaryOperator *E) {
403 // Unary minus performs promotions, then negates its arithmetic operand.
404 QualType Ty;
405 RValue V = EmitExprWithUsualUnaryConversions(E, Ty);
Chris Lattnerf0106d22007-06-02 19:33:17 +0000406
Chris Lattner8394d792007-06-05 20:53:16 +0000407 if (V.isScalar())
408 return RValue::get(Builder.CreateNeg(V.getVal(), "neg"));
409
410 assert(0 && "FIXME: This doesn't handle complex operands yet");
411}
412
413RValue CodeGenFunction::EmitUnaryNot(const UnaryOperator *E) {
414 // Unary not performs promotions, then complements its integer operand.
415 QualType Ty;
416 RValue V = EmitExprWithUsualUnaryConversions(E, Ty);
417
418 if (V.isScalar())
419 return RValue::get(Builder.CreateNot(V.getVal(), "neg"));
420
421 assert(0 && "FIXME: This doesn't handle integer complex operands yet (GNU)");
422}
423
424
425/// C99 6.5.3.3
426RValue CodeGenFunction::EmitUnaryLNot(const UnaryOperator *E) {
427 // Compare operand to zero.
428 Value *BoolVal = EvaluateExprAsBool(E->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000429
430 // Invert value.
Chris Lattnera45c5af2007-06-02 19:47:04 +0000431 // TODO: Could dynamically modify easy computations here. For example, if
432 // the operand is an icmp ne, turn into icmp eq.
Chris Lattnerf0106d22007-06-02 19:33:17 +0000433 BoolVal = Builder.CreateNot(BoolVal, "lnot");
434
435 // ZExt result to int.
Chris Lattner8394d792007-06-05 20:53:16 +0000436 return RValue::get(Builder.CreateZExt(BoolVal, LLVMIntTy, "lnot.ext"));
Chris Lattnerf0106d22007-06-02 19:33:17 +0000437}
438
Chris Lattnere47e4402007-06-01 18:02:12 +0000439
Chris Lattnerdb91b162007-06-02 00:16:28 +0000440//===--------------------------------------------------------------------===//
441// Binary Operator Emission
442//===--------------------------------------------------------------------===//
443
444// FIXME describe.
Chris Lattnercf250242007-06-03 02:02:44 +0000445QualType CodeGenFunction::
Chris Lattner8394d792007-06-05 20:53:16 +0000446EmitUsualArithmeticConversions(const BinaryOperator *E, RValue &LHS,
447 RValue &RHS) {
Chris Lattnerc18f9d12007-06-02 22:51:30 +0000448 QualType LHSType, RHSType;
449 LHS = EmitExprWithUsualUnaryConversions(E->getLHS(), LHSType);
450 RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), RHSType);
451
Chris Lattnercf250242007-06-03 02:02:44 +0000452 // If both operands have the same source type, we're done already.
453 if (LHSType == RHSType) return LHSType;
454
455 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
456 // The caller can deal with this (e.g. pointer + int).
457 if (!LHSType->isArithmeticType() || !RHSType->isArithmeticType())
458 return LHSType;
459
460 // At this point, we have two different arithmetic types.
461
462 // Handle complex types first (C99 6.3.1.8p1).
463 if (LHSType->isComplexType() || RHSType->isComplexType()) {
464 assert(0 && "FIXME: complex types unimp");
465#if 0
466 // if we have an integer operand, the result is the complex type.
467 if (rhs->isIntegerType())
468 return lhs;
469 if (lhs->isIntegerType())
470 return rhs;
471 return Context.maxComplexType(lhs, rhs);
472#endif
473 }
474
475 // If neither operand is complex, they must be scalars.
476 llvm::Value *LHSV = LHS.getVal();
477 llvm::Value *RHSV = RHS.getVal();
478
479 // If the LLVM types are already equal, then they only differed in sign, or it
480 // was something like char/signed char or double/long double.
481 if (LHSV->getType() == RHSV->getType())
482 return LHSType;
483
484 // Now handle "real" floating types (i.e. float, double, long double).
485 if (LHSType->isRealFloatingType() || RHSType->isRealFloatingType()) {
486 // if we have an integer operand, the result is the real floating type, and
487 // the integer converts to FP.
488 if (RHSType->isIntegerType()) {
489 // Promote the RHS to an FP type of the LHS, with the sign following the
490 // RHS.
491 if (RHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000492 RHS = RValue::get(Builder.CreateSIToFP(RHSV,LHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000493 else
Chris Lattner8394d792007-06-05 20:53:16 +0000494 RHS = RValue::get(Builder.CreateUIToFP(RHSV,LHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000495 return LHSType;
496 }
497
498 if (LHSType->isIntegerType()) {
499 // Promote the LHS to an FP type of the RHS, with the sign following the
500 // LHS.
501 if (LHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000502 LHS = RValue::get(Builder.CreateSIToFP(LHSV,RHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000503 else
Chris Lattner8394d792007-06-05 20:53:16 +0000504 LHS = RValue::get(Builder.CreateUIToFP(LHSV,RHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000505 return RHSType;
506 }
507
508 // Otherwise, they are two FP types. Promote the smaller operand to the
509 // bigger result.
510 QualType BiggerType = ASTContext::maxFloatingType(LHSType, RHSType);
511
512 if (BiggerType == LHSType)
Chris Lattner8394d792007-06-05 20:53:16 +0000513 RHS = RValue::get(Builder.CreateFPExt(RHSV, LHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000514 else
Chris Lattner8394d792007-06-05 20:53:16 +0000515 LHS = RValue::get(Builder.CreateFPExt(LHSV, RHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000516 return BiggerType;
517 }
518
519 // Finally, we have two integer types that are different according to C. Do
520 // a sign or zero extension if needed.
521
522 // Otherwise, one type is smaller than the other.
523 QualType ResTy = ASTContext::maxIntegerType(LHSType, RHSType);
524
525 if (LHSType == ResTy) {
526 if (RHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000527 RHS = RValue::get(Builder.CreateSExt(RHSV, LHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000528 else
Chris Lattner8394d792007-06-05 20:53:16 +0000529 RHS = RValue::get(Builder.CreateZExt(RHSV, LHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000530 } else {
531 assert(RHSType == ResTy && "Unknown conversion");
532 if (LHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000533 LHS = RValue::get(Builder.CreateSExt(LHSV, RHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000534 else
Chris Lattner8394d792007-06-05 20:53:16 +0000535 LHS = RValue::get(Builder.CreateZExt(LHSV, RHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000536 }
537 return ResTy;
Chris Lattnerdb91b162007-06-02 00:16:28 +0000538}
539
540
Chris Lattner8394d792007-06-05 20:53:16 +0000541RValue CodeGenFunction::EmitBinaryOperator(const BinaryOperator *E) {
Chris Lattnerdb91b162007-06-02 00:16:28 +0000542 switch (E->getOpcode()) {
543 default:
Chris Lattner8394d792007-06-05 20:53:16 +0000544 fprintf(stderr, "Unimplemented expr!\n");
Chris Lattnerdb91b162007-06-02 00:16:28 +0000545 E->dump();
Chris Lattner8394d792007-06-05 20:53:16 +0000546 return RValue::get(UndefValue::get(llvm::Type::Int32Ty));
547 case BinaryOperator::Mul: return EmitBinaryMul(E);
548 case BinaryOperator::Div: return EmitBinaryDiv(E);
549 case BinaryOperator::Rem: return EmitBinaryRem(E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000550 case BinaryOperator::Add: return EmitBinaryAdd(E);
Chris Lattner8394d792007-06-05 20:53:16 +0000551 case BinaryOperator::Sub: return EmitBinarySub(E);
552 case BinaryOperator::Shl: return EmitBinaryShl(E);
553 case BinaryOperator::Shr: return EmitBinaryShr(E);
554
555 // FIXME: relational
556
557 case BinaryOperator::And: return EmitBinaryAnd(E);
558 case BinaryOperator::Xor: return EmitBinaryXor(E);
559 case BinaryOperator::Or : return EmitBinaryOr(E);
560 case BinaryOperator::LAnd: return EmitBinaryLAnd(E);
561 case BinaryOperator::LOr: return EmitBinaryLOr(E);
562
563 case BinaryOperator::Assign: return EmitBinaryAssign(E);
564 // FIXME: Assignment.
565 case BinaryOperator::Comma: return EmitBinaryComma(E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000566 }
567}
568
Chris Lattner8394d792007-06-05 20:53:16 +0000569RValue CodeGenFunction::EmitBinaryMul(const BinaryOperator *E) {
570 RValue LHS, RHS;
571 EmitUsualArithmeticConversions(E, LHS, RHS);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000572
Chris Lattner8394d792007-06-05 20:53:16 +0000573 if (LHS.isScalar())
574 return RValue::get(Builder.CreateMul(LHS.getVal(), RHS.getVal(), "mul"));
575
576 assert(0 && "FIXME: This doesn't handle complex operands yet");
577}
578
579RValue CodeGenFunction::EmitBinaryDiv(const BinaryOperator *E) {
580 RValue LHS, RHS;
581 EmitUsualArithmeticConversions(E, LHS, RHS);
582
583 if (LHS.isScalar()) {
584 Value *RV;
585 if (LHS.getVal()->getType()->isFloatingPoint())
586 RV = Builder.CreateFDiv(LHS.getVal(), RHS.getVal(), "div");
587 else if (E->getType()->isUnsignedIntegerType())
588 RV = Builder.CreateUDiv(LHS.getVal(), RHS.getVal(), "div");
589 else
590 RV = Builder.CreateSDiv(LHS.getVal(), RHS.getVal(), "div");
591 return RValue::get(RV);
592 }
593 assert(0 && "FIXME: This doesn't handle complex operands yet");
594}
595
596RValue CodeGenFunction::EmitBinaryRem(const BinaryOperator *E) {
597 RValue LHS, RHS;
598 EmitUsualArithmeticConversions(E, LHS, RHS);
599
600 if (LHS.isScalar()) {
601 Value *RV;
602 // Rem in C can't be a floating point type: C99 6.5.5p2.
603 if (E->getType()->isUnsignedIntegerType())
604 RV = Builder.CreateURem(LHS.getVal(), RHS.getVal(), "rem");
605 else
606 RV = Builder.CreateSRem(LHS.getVal(), RHS.getVal(), "rem");
607 return RValue::get(RV);
608 }
609
610 assert(0 && "FIXME: This doesn't handle complex operands yet");
611}
612
613RValue CodeGenFunction::EmitBinaryAdd(const BinaryOperator *E) {
614 RValue LHS, RHS;
Chris Lattnerdb91b162007-06-02 00:16:28 +0000615 EmitUsualArithmeticConversions(E, LHS, RHS);
616
Chris Lattner8394d792007-06-05 20:53:16 +0000617 // FIXME: This doesn't handle ptr+int etc yet.
618
619 if (LHS.isScalar())
620 return RValue::get(Builder.CreateAdd(LHS.getVal(), RHS.getVal(), "add"));
621
622 assert(0 && "FIXME: This doesn't handle complex operands yet");
623
624}
625
626RValue CodeGenFunction::EmitBinarySub(const BinaryOperator *E) {
627 RValue LHS, RHS;
628 EmitUsualArithmeticConversions(E, LHS, RHS);
629
630 // FIXME: This doesn't handle ptr-int or ptr-ptr, etc yet.
631
632 if (LHS.isScalar())
633 return RValue::get(Builder.CreateSub(LHS.getVal(), RHS.getVal(), "sub"));
634
635 assert(0 && "FIXME: This doesn't handle complex operands yet");
636
637}
638
639RValue CodeGenFunction::EmitBinaryShl(const BinaryOperator *E) {
640 // For shifts, integer promotions are performed, but the usual arithmetic
641 // conversions are not. The LHS and RHS need not have the same type.
642
643 QualType ResTy;
644 Value *LHS = EmitExprWithUsualUnaryConversions(E->getLHS(), ResTy).getVal();
645 Value *RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), ResTy).getVal();
646
647 // LLVM requires the LHS and RHS to be the same type, promote or truncate the
648 // RHS to the same size as the LHS.
649 if (LHS->getType() != RHS->getType())
650 RHS = Builder.CreateIntCast(RHS, LHS->getType(), false, "sh_prom");
651
652 return RValue::get(Builder.CreateShl(LHS, RHS, "shl"));
653}
654
655RValue CodeGenFunction::EmitBinaryShr(const BinaryOperator *E) {
656 // For shifts, integer promotions are performed, but the usual arithmetic
657 // conversions are not. The LHS and RHS need not have the same type.
658
659 QualType ResTy;
660 Value *LHS = EmitExprWithUsualUnaryConversions(E->getLHS(), ResTy).getVal();
661 Value *RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), ResTy).getVal();
662
663 // LLVM requires the LHS and RHS to be the same type, promote or truncate the
664 // RHS to the same size as the LHS.
665 if (LHS->getType() != RHS->getType())
666 RHS = Builder.CreateIntCast(RHS, LHS->getType(), false, "sh_prom");
667
668 if (E->getType()->isUnsignedIntegerType())
669 return RValue::get(Builder.CreateLShr(LHS, RHS, "shr"));
670 else
671 return RValue::get(Builder.CreateAShr(LHS, RHS, "shr"));
672}
673
674RValue CodeGenFunction::EmitBinaryAnd(const BinaryOperator *E) {
675 RValue LHS, RHS;
676 EmitUsualArithmeticConversions(E, LHS, RHS);
677
678 if (LHS.isScalar())
679 return RValue::get(Builder.CreateAnd(LHS.getVal(), RHS.getVal(), "and"));
680
681 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
682}
683
684RValue CodeGenFunction::EmitBinaryXor(const BinaryOperator *E) {
685 RValue LHS, RHS;
686 EmitUsualArithmeticConversions(E, LHS, RHS);
687
688 if (LHS.isScalar())
689 return RValue::get(Builder.CreateXor(LHS.getVal(), RHS.getVal(), "xor"));
690
691 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
692}
693
694RValue CodeGenFunction::EmitBinaryOr(const BinaryOperator *E) {
695 RValue LHS, RHS;
696 EmitUsualArithmeticConversions(E, LHS, RHS);
697
698 if (LHS.isScalar())
699 return RValue::get(Builder.CreateOr(LHS.getVal(), RHS.getVal(), "or"));
700
701 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
702}
703
704RValue CodeGenFunction::EmitBinaryLAnd(const BinaryOperator *E) {
705 Value *LHSCond = EvaluateExprAsBool(E->getLHS());
706
707 BasicBlock *ContBlock = new BasicBlock("land_cont");
708 BasicBlock *RHSBlock = new BasicBlock("land_rhs");
709
710 BasicBlock *OrigBlock = Builder.GetInsertBlock();
711 Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock);
712
713 EmitBlock(RHSBlock);
714 Value *RHSCond = EvaluateExprAsBool(E->getRHS());
715
716 // Reaquire the RHS block, as there may be subblocks inserted.
717 RHSBlock = Builder.GetInsertBlock();
718 EmitBlock(ContBlock);
719
720 // Create a PHI node. If we just evaluted the LHS condition, the result is
721 // false. If we evaluated both, the result is the RHS condition.
722 PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land");
723 PN->reserveOperandSpace(2);
724 PN->addIncoming(ConstantInt::getFalse(), OrigBlock);
725 PN->addIncoming(RHSCond, RHSBlock);
726
727 // ZExt result to int.
728 return RValue::get(Builder.CreateZExt(PN, LLVMIntTy, "land.ext"));
729}
730
731RValue CodeGenFunction::EmitBinaryLOr(const BinaryOperator *E) {
732 Value *LHSCond = EvaluateExprAsBool(E->getLHS());
733
734 BasicBlock *ContBlock = new BasicBlock("lor_cont");
735 BasicBlock *RHSBlock = new BasicBlock("lor_rhs");
736
737 BasicBlock *OrigBlock = Builder.GetInsertBlock();
738 Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock);
739
740 EmitBlock(RHSBlock);
741 Value *RHSCond = EvaluateExprAsBool(E->getRHS());
742
743 // Reaquire the RHS block, as there may be subblocks inserted.
744 RHSBlock = Builder.GetInsertBlock();
745 EmitBlock(ContBlock);
746
747 // Create a PHI node. If we just evaluted the LHS condition, the result is
748 // true. If we evaluated both, the result is the RHS condition.
749 PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor");
750 PN->reserveOperandSpace(2);
751 PN->addIncoming(ConstantInt::getTrue(), OrigBlock);
752 PN->addIncoming(RHSCond, RHSBlock);
753
754 // ZExt result to int.
755 return RValue::get(Builder.CreateZExt(PN, LLVMIntTy, "lor.ext"));
756}
757
758RValue CodeGenFunction::EmitBinaryAssign(const BinaryOperator *E) {
759 LValue LHS = EmitLValue(E->getLHS());
760
761 QualType RHSTy;
762 RValue RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), RHSTy);
763
764 // Convert the RHS to the type of the LHS.
Chris Lattnercf106ab2007-06-06 04:05:39 +0000765 // FIXME: I'm not thrilled about having to call getLocStart() here... :(
766 RHS = EmitConversion(RHS, RHSTy, E->getType(), E->getLocStart());
Chris Lattner8394d792007-06-05 20:53:16 +0000767
768 // Store the value into the LHS.
769 EmitStoreThroughLValue(RHS, LHS, E->getType());
770
771 // Return the converted RHS.
772 return RHS;
773}
774
775
776RValue CodeGenFunction::EmitBinaryComma(const BinaryOperator *E) {
777 EmitExpr(E->getLHS());
778 return EmitExpr(E->getRHS());
779}