blob: f358e49a757f8f26317192d3cde680629c1a797e [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,
42 QualType DstTy) {
43 ValTy = ValTy.getCanonicalType();
44 DstTy = DstTy.getCanonicalType();
45 if (ValTy == DstTy) return Val;
46
47 if (const BuiltinType *DestBT = dyn_cast<BuiltinType>(DstTy)) {
48 if (DestBT->getKind() == BuiltinType::Bool)
49 return RValue::get(ConvertScalarValueToBool(Val, ValTy));
50 }
51
52 assert(0 && "FIXME: Unsupported conversion!");
53}
54
55
56/// ConvertScalarValueToBool - Convert the specified expression value to a
Chris Lattnerf0106d22007-06-02 19:33:17 +000057/// boolean (i1) truth value. This is equivalent to "Val == 0".
Chris Lattner8394d792007-06-05 20:53:16 +000058Value *CodeGenFunction::ConvertScalarValueToBool(RValue Val, QualType Ty) {
Chris Lattnerf0106d22007-06-02 19:33:17 +000059 Ty = Ty.getCanonicalType();
60 Value *Result;
61 if (const BuiltinType *BT = dyn_cast<BuiltinType>(Ty)) {
62 switch (BT->getKind()) {
63 default: assert(0 && "Unknown scalar value");
64 case BuiltinType::Bool:
65 Result = Val.getVal();
66 // Bool is already evaluated right.
67 assert(Result->getType() == llvm::Type::Int1Ty &&
68 "Unexpected bool value type!");
69 return Result;
Chris Lattnerb16f4552007-06-03 07:25:34 +000070 case BuiltinType::Char_S:
71 case BuiltinType::Char_U:
Chris Lattnerf0106d22007-06-02 19:33:17 +000072 case BuiltinType::SChar:
73 case BuiltinType::UChar:
74 case BuiltinType::Short:
75 case BuiltinType::UShort:
76 case BuiltinType::Int:
77 case BuiltinType::UInt:
78 case BuiltinType::Long:
79 case BuiltinType::ULong:
80 case BuiltinType::LongLong:
81 case BuiltinType::ULongLong:
82 // Code below handles simple integers.
83 break;
84 case BuiltinType::Float:
85 case BuiltinType::Double:
86 case BuiltinType::LongDouble: {
87 // Compare against 0.0 for fp scalars.
88 Result = Val.getVal();
89 llvm::Value *Zero = Constant::getNullValue(Result->getType());
90 // FIXME: llvm-gcc produces a une comparison: validate this is right.
91 Result = Builder.CreateFCmpUNE(Result, Zero, "tobool");
92 return Result;
93 }
94
95 case BuiltinType::FloatComplex:
96 case BuiltinType::DoubleComplex:
97 case BuiltinType::LongDoubleComplex:
98 assert(0 && "comparisons against complex not implemented yet");
99 }
100 } else {
101 assert((isa<PointerType>(Ty) ||
102 cast<TagType>(Ty)->getDecl()->getKind() == Decl::Enum) &&
103 "Unknown scalar type");
104 // Code below handles this fine.
105 }
106
107 // Usual case for integers, pointers, and enums: compare against zero.
108 Result = Val.getVal();
Chris Lattnera45c5af2007-06-02 19:47:04 +0000109
110 // Because of the type rules of C, we often end up computing a logical value,
111 // then zero extending it to int, then wanting it as a logical value again.
112 // Optimize this common case.
113 if (llvm::ZExtInst *ZI = dyn_cast<ZExtInst>(Result)) {
114 if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
115 Result = ZI->getOperand(0);
116 ZI->eraseFromParent();
117 return Result;
118 }
119 }
120
Chris Lattnerf0106d22007-06-02 19:33:17 +0000121 llvm::Value *Zero = Constant::getNullValue(Result->getType());
122 return Builder.CreateICmpNE(Result, Zero, "tobool");
123}
124
Chris Lattnera45c5af2007-06-02 19:47:04 +0000125//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000126// LValue Expression Emission
Chris Lattnera45c5af2007-06-02 19:47:04 +0000127//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000128
Chris Lattner8394d792007-06-05 20:53:16 +0000129/// EmitLValue - Emit code to compute a designator that specifies the location
130/// of the expression.
131///
132/// This can return one of two things: a simple address or a bitfield
133/// reference. In either case, the LLVM Value* in the LValue structure is
134/// guaranteed to be an LLVM pointer type.
135///
136/// If this returns a bitfield reference, nothing about the pointee type of
137/// the LLVM value is known: For example, it may not be a pointer to an
138/// integer.
139///
140/// If this returns a normal address, and if the lvalue's C type is fixed
141/// size, this method guarantees that the returned pointer type will point to
142/// an LLVM type of the same size of the lvalue's type. If the lvalue has a
143/// variable length type, this is not possible.
144///
Chris Lattnerd7f58862007-06-02 05:24:33 +0000145LValue CodeGenFunction::EmitLValue(const Expr *E) {
146 switch (E->getStmtClass()) {
147 default:
Chris Lattner8394d792007-06-05 20:53:16 +0000148 fprintf(stderr, "Unimplemented lvalue expr!\n");
Chris Lattnerd7f58862007-06-02 05:24:33 +0000149 E->dump();
150 return LValue::getAddr(UndefValue::get(
151 llvm::PointerType::get(llvm::Type::Int32Ty)));
152
153 case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Chris Lattner946aa312007-06-05 03:59:43 +0000154 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattner8394d792007-06-05 20:53:16 +0000155
156
157 case Expr::UnaryOperatorClass:
158 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000159 }
160}
161
Chris Lattner8394d792007-06-05 20:53:16 +0000162/// EmitLoadOfLValue - Given an expression that represents a value lvalue,
163/// this method emits the address of the lvalue, then loads the result as an
164/// rvalue, returning the rvalue.
165RValue CodeGenFunction::EmitLoadOfLValue(const Expr *E) {
166 LValue LV = EmitLValue(E);
167
168 QualType ExprTy = E->getType().getCanonicalType();
169
170 // FIXME: this is silly and obviously wrong for non-scalars.
171 assert(!LV.isBitfield());
172 return RValue::get(Builder.CreateLoad(LV.getAddress(), "tmp"));
173}
174
175/// EmitStoreThroughLValue - Store the specified rvalue into the specified
176/// lvalue, where both are guaranteed to the have the same type, and that type
177/// is 'Ty'.
178void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
179 QualType Ty) {
180 // FIXME: This is obviously bogus.
181 assert(!Dst.isBitfield() && "FIXME: Don't support store to bitfield yet");
182 assert(Src.isScalar() && "FIXME: Don't support store of aggregate yet");
183
184 // TODO: Handle volatility etc.
185 Value *Addr = Dst.getAddress();
186 const llvm::Type *SrcTy = Src.getVal()->getType();
187 const llvm::Type *AddrTy =
188 cast<llvm::PointerType>(Addr->getType())->getElementType();
189
190 if (AddrTy != SrcTy)
191 Addr = Builder.CreateBitCast(Addr, llvm::PointerType::get(SrcTy),
192 "storetmp");
193 Builder.CreateStore(Src.getVal(), Addr);
194}
195
Chris Lattnerd7f58862007-06-02 05:24:33 +0000196
197LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
198 const Decl *D = E->getDecl();
199 if (isa<BlockVarDecl>(D)) {
200 Value *V = LocalDeclMap[D];
201 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
202 return LValue::getAddr(V);
203 }
204 assert(0 && "Unimp declref");
205}
Chris Lattnere47e4402007-06-01 18:02:12 +0000206
Chris Lattner8394d792007-06-05 20:53:16 +0000207LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
208 // __extension__ doesn't affect lvalue-ness.
209 if (E->getOpcode() == UnaryOperator::Extension)
210 return EmitLValue(E->getSubExpr());
211
212 assert(E->getOpcode() == UnaryOperator::Deref &&
213 "'*' is the only unary operator that produces an lvalue");
214 return LValue::getAddr(EmitExpr(E->getSubExpr()).getVal());
215}
216
Chris Lattnere47e4402007-06-01 18:02:12 +0000217//===--------------------------------------------------------------------===//
218// Expression Emission
219//===--------------------------------------------------------------------===//
220
Chris Lattner8394d792007-06-05 20:53:16 +0000221RValue CodeGenFunction::EmitExpr(const Expr *E) {
Chris Lattnere47e4402007-06-01 18:02:12 +0000222 assert(E && "Null expression?");
223
224 switch (E->getStmtClass()) {
225 default:
226 printf("Unimplemented expr!\n");
227 E->dump();
Chris Lattner8394d792007-06-05 20:53:16 +0000228 return RValue::get(UndefValue::get(llvm::Type::Int32Ty));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000229
230 // l-values.
Chris Lattner8394d792007-06-05 20:53:16 +0000231 case Expr::DeclRefExprClass:
232 // FIXME: EnumConstantDecl's are not lvalues. This is wrong for them.
233 return EmitLoadOfLValue(E);
Chris Lattnerd7f58862007-06-02 05:24:33 +0000234
235 // Leaf expressions.
236 case Expr::IntegerLiteralClass:
Chris Lattnere47e4402007-06-01 18:02:12 +0000237 return EmitIntegerLiteral(cast<IntegerLiteral>(E));
Chris Lattnerdb91b162007-06-02 00:16:28 +0000238
Chris Lattnerd7f58862007-06-02 05:24:33 +0000239 // Operators.
240 case Expr::ParenExprClass:
241 return EmitExpr(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000242 case Expr::UnaryOperatorClass:
243 return EmitUnaryOperator(cast<UnaryOperator>(E));
Chris Lattner8394d792007-06-05 20:53:16 +0000244 case Expr::CastExprClass:
245 return EmitCastExpr(cast<CastExpr>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000246 case Expr::BinaryOperatorClass:
Chris Lattnerdb91b162007-06-02 00:16:28 +0000247 return EmitBinaryOperator(cast<BinaryOperator>(E));
Chris Lattnere47e4402007-06-01 18:02:12 +0000248 }
249
250}
251
Chris Lattner8394d792007-06-05 20:53:16 +0000252RValue CodeGenFunction::EmitIntegerLiteral(const IntegerLiteral *E) {
253 return RValue::get(ConstantInt::get(E->getValue()));
Chris Lattnere47e4402007-06-01 18:02:12 +0000254}
255
Chris Lattner8394d792007-06-05 20:53:16 +0000256RValue CodeGenFunction::EmitCastExpr(const CastExpr *E) {
257 QualType SrcTy;
258 RValue Src = EmitExprWithUsualUnaryConversions(E->getSubExpr(), SrcTy);
259
260 // If the destination is void, just evaluate the source.
261 if (E->getType()->isVoidType())
262 return RValue::getAggregate(0);
263
264 return EmitConversion(Src, SrcTy, E->getType());
265}
Chris Lattnerf0106d22007-06-02 19:33:17 +0000266
Chris Lattner8394d792007-06-05 20:53:16 +0000267//===----------------------------------------------------------------------===//
268// Unary Operator Emission
269//===----------------------------------------------------------------------===//
270
271RValue CodeGenFunction::EmitExprWithUsualUnaryConversions(const Expr *E,
272 QualType &ResTy) {
Chris Lattner6db1fb82007-06-02 22:49:07 +0000273 ResTy = E->getType().getCanonicalType();
274
275 if (isa<FunctionType>(ResTy)) { // C99 6.3.2.1p4
276 // Functions are promoted to their address.
277 ResTy = getContext().getPointerType(ResTy);
Chris Lattner8394d792007-06-05 20:53:16 +0000278 return RValue::get(EmitLValue(E).getAddress());
Chris Lattner6db1fb82007-06-02 22:49:07 +0000279 } else if (const ArrayType *ary = dyn_cast<ArrayType>(ResTy)) {
280 // C99 6.3.2.1p3
281 ResTy = getContext().getPointerType(ary->getElementType());
282
283 // FIXME: For now we assume that all source arrays map to LLVM arrays. This
284 // will not true when we add support for VLAs.
285 llvm::Value *V = EmitLValue(E).getAddress(); // Bitfields can't be arrays.
286
287 assert(isa<llvm::PointerType>(V->getType()) &&
288 isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
289 ->getElementType()) &&
290 "Doesn't support VLAs yet!");
291 llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
Chris Lattner8394d792007-06-05 20:53:16 +0000292 return RValue::get(Builder.CreateGEP(V, Idx0, Idx0, "arraydecay"));
Chris Lattner6db1fb82007-06-02 22:49:07 +0000293 } else if (ResTy->isPromotableIntegerType()) { // C99 6.3.1.1p2
294 // FIXME: this probably isn't right, pending clarification from Steve.
295 llvm::Value *Val = EmitExpr(E).getVal();
296
Chris Lattner6db1fb82007-06-02 22:49:07 +0000297 // If the input is a signed integer, sign extend to the destination.
298 if (ResTy->isSignedIntegerType()) {
299 Val = Builder.CreateSExt(Val, LLVMIntTy, "promote");
300 } else {
301 // This handles unsigned types, including bool.
302 Val = Builder.CreateZExt(Val, LLVMIntTy, "promote");
303 }
304 ResTy = getContext().IntTy;
305
Chris Lattner8394d792007-06-05 20:53:16 +0000306 return RValue::get(Val);
Chris Lattner6db1fb82007-06-02 22:49:07 +0000307 }
308
309 // Otherwise, this is a float, double, int, struct, etc.
310 return EmitExpr(E);
311}
312
313
Chris Lattner8394d792007-06-05 20:53:16 +0000314RValue CodeGenFunction::EmitUnaryOperator(const UnaryOperator *E) {
Chris Lattnerf0106d22007-06-02 19:33:17 +0000315 switch (E->getOpcode()) {
316 default:
317 printf("Unimplemented unary expr!\n");
318 E->dump();
Chris Lattner8394d792007-06-05 20:53:16 +0000319 return RValue::get(UndefValue::get(llvm::Type::Int32Ty));
320 // FIXME: pre/post inc/dec
321 case UnaryOperator::AddrOf: return EmitUnaryAddrOf(E);
322 case UnaryOperator::Deref : return EmitLoadOfLValue(E);
323 case UnaryOperator::Plus : return EmitUnaryPlus(E);
324 case UnaryOperator::Minus : return EmitUnaryMinus(E);
325 case UnaryOperator::Not : return EmitUnaryNot(E);
326 case UnaryOperator::LNot : return EmitUnaryLNot(E);
327 // FIXME: SIZEOF/ALIGNOF(expr).
328 // FIXME: real/imag
329 case UnaryOperator::Extension: return EmitExpr(E->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000330 }
331}
332
Chris Lattner8394d792007-06-05 20:53:16 +0000333/// C99 6.5.3.2
334RValue CodeGenFunction::EmitUnaryAddrOf(const UnaryOperator *E) {
335 // The address of the operand is just its lvalue. It cannot be a bitfield.
336 return RValue::get(EmitLValue(E->getSubExpr()).getAddress());
337}
338
339RValue CodeGenFunction::EmitUnaryPlus(const UnaryOperator *E) {
340 // Unary plus just performs promotions on its arithmetic operand.
341 QualType Ty;
342 return EmitExprWithUsualUnaryConversions(E, Ty);
343}
344
345RValue CodeGenFunction::EmitUnaryMinus(const UnaryOperator *E) {
346 // Unary minus performs promotions, then negates its arithmetic operand.
347 QualType Ty;
348 RValue V = EmitExprWithUsualUnaryConversions(E, Ty);
Chris Lattnerf0106d22007-06-02 19:33:17 +0000349
Chris Lattner8394d792007-06-05 20:53:16 +0000350 if (V.isScalar())
351 return RValue::get(Builder.CreateNeg(V.getVal(), "neg"));
352
353 assert(0 && "FIXME: This doesn't handle complex operands yet");
354}
355
356RValue CodeGenFunction::EmitUnaryNot(const UnaryOperator *E) {
357 // Unary not performs promotions, then complements its integer operand.
358 QualType Ty;
359 RValue V = EmitExprWithUsualUnaryConversions(E, Ty);
360
361 if (V.isScalar())
362 return RValue::get(Builder.CreateNot(V.getVal(), "neg"));
363
364 assert(0 && "FIXME: This doesn't handle integer complex operands yet (GNU)");
365}
366
367
368/// C99 6.5.3.3
369RValue CodeGenFunction::EmitUnaryLNot(const UnaryOperator *E) {
370 // Compare operand to zero.
371 Value *BoolVal = EvaluateExprAsBool(E->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000372
373 // Invert value.
Chris Lattnera45c5af2007-06-02 19:47:04 +0000374 // TODO: Could dynamically modify easy computations here. For example, if
375 // the operand is an icmp ne, turn into icmp eq.
Chris Lattnerf0106d22007-06-02 19:33:17 +0000376 BoolVal = Builder.CreateNot(BoolVal, "lnot");
377
378 // ZExt result to int.
Chris Lattner8394d792007-06-05 20:53:16 +0000379 return RValue::get(Builder.CreateZExt(BoolVal, LLVMIntTy, "lnot.ext"));
Chris Lattnerf0106d22007-06-02 19:33:17 +0000380}
381
Chris Lattnere47e4402007-06-01 18:02:12 +0000382
Chris Lattnerdb91b162007-06-02 00:16:28 +0000383//===--------------------------------------------------------------------===//
384// Binary Operator Emission
385//===--------------------------------------------------------------------===//
386
387// FIXME describe.
Chris Lattnercf250242007-06-03 02:02:44 +0000388QualType CodeGenFunction::
Chris Lattner8394d792007-06-05 20:53:16 +0000389EmitUsualArithmeticConversions(const BinaryOperator *E, RValue &LHS,
390 RValue &RHS) {
Chris Lattnerc18f9d12007-06-02 22:51:30 +0000391 QualType LHSType, RHSType;
392 LHS = EmitExprWithUsualUnaryConversions(E->getLHS(), LHSType);
393 RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), RHSType);
394
Chris Lattnercf250242007-06-03 02:02:44 +0000395 // If both operands have the same source type, we're done already.
396 if (LHSType == RHSType) return LHSType;
397
398 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
399 // The caller can deal with this (e.g. pointer + int).
400 if (!LHSType->isArithmeticType() || !RHSType->isArithmeticType())
401 return LHSType;
402
403 // At this point, we have two different arithmetic types.
404
405 // Handle complex types first (C99 6.3.1.8p1).
406 if (LHSType->isComplexType() || RHSType->isComplexType()) {
407 assert(0 && "FIXME: complex types unimp");
408#if 0
409 // if we have an integer operand, the result is the complex type.
410 if (rhs->isIntegerType())
411 return lhs;
412 if (lhs->isIntegerType())
413 return rhs;
414 return Context.maxComplexType(lhs, rhs);
415#endif
416 }
417
418 // If neither operand is complex, they must be scalars.
419 llvm::Value *LHSV = LHS.getVal();
420 llvm::Value *RHSV = RHS.getVal();
421
422 // If the LLVM types are already equal, then they only differed in sign, or it
423 // was something like char/signed char or double/long double.
424 if (LHSV->getType() == RHSV->getType())
425 return LHSType;
426
427 // Now handle "real" floating types (i.e. float, double, long double).
428 if (LHSType->isRealFloatingType() || RHSType->isRealFloatingType()) {
429 // if we have an integer operand, the result is the real floating type, and
430 // the integer converts to FP.
431 if (RHSType->isIntegerType()) {
432 // Promote the RHS to an FP type of the LHS, with the sign following the
433 // RHS.
434 if (RHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000435 RHS = RValue::get(Builder.CreateSIToFP(RHSV,LHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000436 else
Chris Lattner8394d792007-06-05 20:53:16 +0000437 RHS = RValue::get(Builder.CreateUIToFP(RHSV,LHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000438 return LHSType;
439 }
440
441 if (LHSType->isIntegerType()) {
442 // Promote the LHS to an FP type of the RHS, with the sign following the
443 // LHS.
444 if (LHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000445 LHS = RValue::get(Builder.CreateSIToFP(LHSV,RHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000446 else
Chris Lattner8394d792007-06-05 20:53:16 +0000447 LHS = RValue::get(Builder.CreateUIToFP(LHSV,RHSV->getType(),"promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000448 return RHSType;
449 }
450
451 // Otherwise, they are two FP types. Promote the smaller operand to the
452 // bigger result.
453 QualType BiggerType = ASTContext::maxFloatingType(LHSType, RHSType);
454
455 if (BiggerType == LHSType)
Chris Lattner8394d792007-06-05 20:53:16 +0000456 RHS = RValue::get(Builder.CreateFPExt(RHSV, LHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000457 else
Chris Lattner8394d792007-06-05 20:53:16 +0000458 LHS = RValue::get(Builder.CreateFPExt(LHSV, RHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000459 return BiggerType;
460 }
461
462 // Finally, we have two integer types that are different according to C. Do
463 // a sign or zero extension if needed.
464
465 // Otherwise, one type is smaller than the other.
466 QualType ResTy = ASTContext::maxIntegerType(LHSType, RHSType);
467
468 if (LHSType == ResTy) {
469 if (RHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000470 RHS = RValue::get(Builder.CreateSExt(RHSV, LHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000471 else
Chris Lattner8394d792007-06-05 20:53:16 +0000472 RHS = RValue::get(Builder.CreateZExt(RHSV, LHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000473 } else {
474 assert(RHSType == ResTy && "Unknown conversion");
475 if (LHSType->isSignedIntegerType())
Chris Lattner8394d792007-06-05 20:53:16 +0000476 LHS = RValue::get(Builder.CreateSExt(LHSV, RHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000477 else
Chris Lattner8394d792007-06-05 20:53:16 +0000478 LHS = RValue::get(Builder.CreateZExt(LHSV, RHSV->getType(), "promote"));
Chris Lattnercf250242007-06-03 02:02:44 +0000479 }
480 return ResTy;
Chris Lattnerdb91b162007-06-02 00:16:28 +0000481}
482
483
Chris Lattner8394d792007-06-05 20:53:16 +0000484RValue CodeGenFunction::EmitBinaryOperator(const BinaryOperator *E) {
Chris Lattnerdb91b162007-06-02 00:16:28 +0000485 switch (E->getOpcode()) {
486 default:
Chris Lattner8394d792007-06-05 20:53:16 +0000487 fprintf(stderr, "Unimplemented expr!\n");
Chris Lattnerdb91b162007-06-02 00:16:28 +0000488 E->dump();
Chris Lattner8394d792007-06-05 20:53:16 +0000489 return RValue::get(UndefValue::get(llvm::Type::Int32Ty));
490 case BinaryOperator::Mul: return EmitBinaryMul(E);
491 case BinaryOperator::Div: return EmitBinaryDiv(E);
492 case BinaryOperator::Rem: return EmitBinaryRem(E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000493 case BinaryOperator::Add: return EmitBinaryAdd(E);
Chris Lattner8394d792007-06-05 20:53:16 +0000494 case BinaryOperator::Sub: return EmitBinarySub(E);
495 case BinaryOperator::Shl: return EmitBinaryShl(E);
496 case BinaryOperator::Shr: return EmitBinaryShr(E);
497
498 // FIXME: relational
499
500 case BinaryOperator::And: return EmitBinaryAnd(E);
501 case BinaryOperator::Xor: return EmitBinaryXor(E);
502 case BinaryOperator::Or : return EmitBinaryOr(E);
503 case BinaryOperator::LAnd: return EmitBinaryLAnd(E);
504 case BinaryOperator::LOr: return EmitBinaryLOr(E);
505
506 case BinaryOperator::Assign: return EmitBinaryAssign(E);
507 // FIXME: Assignment.
508 case BinaryOperator::Comma: return EmitBinaryComma(E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000509 }
510}
511
Chris Lattner8394d792007-06-05 20:53:16 +0000512RValue CodeGenFunction::EmitBinaryMul(const BinaryOperator *E) {
513 RValue LHS, RHS;
514 EmitUsualArithmeticConversions(E, LHS, RHS);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000515
Chris Lattner8394d792007-06-05 20:53:16 +0000516 if (LHS.isScalar())
517 return RValue::get(Builder.CreateMul(LHS.getVal(), RHS.getVal(), "mul"));
518
519 assert(0 && "FIXME: This doesn't handle complex operands yet");
520}
521
522RValue CodeGenFunction::EmitBinaryDiv(const BinaryOperator *E) {
523 RValue LHS, RHS;
524 EmitUsualArithmeticConversions(E, LHS, RHS);
525
526 if (LHS.isScalar()) {
527 Value *RV;
528 if (LHS.getVal()->getType()->isFloatingPoint())
529 RV = Builder.CreateFDiv(LHS.getVal(), RHS.getVal(), "div");
530 else if (E->getType()->isUnsignedIntegerType())
531 RV = Builder.CreateUDiv(LHS.getVal(), RHS.getVal(), "div");
532 else
533 RV = Builder.CreateSDiv(LHS.getVal(), RHS.getVal(), "div");
534 return RValue::get(RV);
535 }
536 assert(0 && "FIXME: This doesn't handle complex operands yet");
537}
538
539RValue CodeGenFunction::EmitBinaryRem(const BinaryOperator *E) {
540 RValue LHS, RHS;
541 EmitUsualArithmeticConversions(E, LHS, RHS);
542
543 if (LHS.isScalar()) {
544 Value *RV;
545 // Rem in C can't be a floating point type: C99 6.5.5p2.
546 if (E->getType()->isUnsignedIntegerType())
547 RV = Builder.CreateURem(LHS.getVal(), RHS.getVal(), "rem");
548 else
549 RV = Builder.CreateSRem(LHS.getVal(), RHS.getVal(), "rem");
550 return RValue::get(RV);
551 }
552
553 assert(0 && "FIXME: This doesn't handle complex operands yet");
554}
555
556RValue CodeGenFunction::EmitBinaryAdd(const BinaryOperator *E) {
557 RValue LHS, RHS;
Chris Lattnerdb91b162007-06-02 00:16:28 +0000558 EmitUsualArithmeticConversions(E, LHS, RHS);
559
Chris Lattner8394d792007-06-05 20:53:16 +0000560 // FIXME: This doesn't handle ptr+int etc yet.
561
562 if (LHS.isScalar())
563 return RValue::get(Builder.CreateAdd(LHS.getVal(), RHS.getVal(), "add"));
564
565 assert(0 && "FIXME: This doesn't handle complex operands yet");
566
567}
568
569RValue CodeGenFunction::EmitBinarySub(const BinaryOperator *E) {
570 RValue LHS, RHS;
571 EmitUsualArithmeticConversions(E, LHS, RHS);
572
573 // FIXME: This doesn't handle ptr-int or ptr-ptr, etc yet.
574
575 if (LHS.isScalar())
576 return RValue::get(Builder.CreateSub(LHS.getVal(), RHS.getVal(), "sub"));
577
578 assert(0 && "FIXME: This doesn't handle complex operands yet");
579
580}
581
582RValue CodeGenFunction::EmitBinaryShl(const BinaryOperator *E) {
583 // For shifts, integer promotions are performed, but the usual arithmetic
584 // conversions are not. The LHS and RHS need not have the same type.
585
586 QualType ResTy;
587 Value *LHS = EmitExprWithUsualUnaryConversions(E->getLHS(), ResTy).getVal();
588 Value *RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), ResTy).getVal();
589
590 // LLVM requires the LHS and RHS to be the same type, promote or truncate the
591 // RHS to the same size as the LHS.
592 if (LHS->getType() != RHS->getType())
593 RHS = Builder.CreateIntCast(RHS, LHS->getType(), false, "sh_prom");
594
595 return RValue::get(Builder.CreateShl(LHS, RHS, "shl"));
596}
597
598RValue CodeGenFunction::EmitBinaryShr(const BinaryOperator *E) {
599 // For shifts, integer promotions are performed, but the usual arithmetic
600 // conversions are not. The LHS and RHS need not have the same type.
601
602 QualType ResTy;
603 Value *LHS = EmitExprWithUsualUnaryConversions(E->getLHS(), ResTy).getVal();
604 Value *RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), ResTy).getVal();
605
606 // LLVM requires the LHS and RHS to be the same type, promote or truncate the
607 // RHS to the same size as the LHS.
608 if (LHS->getType() != RHS->getType())
609 RHS = Builder.CreateIntCast(RHS, LHS->getType(), false, "sh_prom");
610
611 if (E->getType()->isUnsignedIntegerType())
612 return RValue::get(Builder.CreateLShr(LHS, RHS, "shr"));
613 else
614 return RValue::get(Builder.CreateAShr(LHS, RHS, "shr"));
615}
616
617RValue CodeGenFunction::EmitBinaryAnd(const BinaryOperator *E) {
618 RValue LHS, RHS;
619 EmitUsualArithmeticConversions(E, LHS, RHS);
620
621 if (LHS.isScalar())
622 return RValue::get(Builder.CreateAnd(LHS.getVal(), RHS.getVal(), "and"));
623
624 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
625}
626
627RValue CodeGenFunction::EmitBinaryXor(const BinaryOperator *E) {
628 RValue LHS, RHS;
629 EmitUsualArithmeticConversions(E, LHS, RHS);
630
631 if (LHS.isScalar())
632 return RValue::get(Builder.CreateXor(LHS.getVal(), RHS.getVal(), "xor"));
633
634 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
635}
636
637RValue CodeGenFunction::EmitBinaryOr(const BinaryOperator *E) {
638 RValue LHS, RHS;
639 EmitUsualArithmeticConversions(E, LHS, RHS);
640
641 if (LHS.isScalar())
642 return RValue::get(Builder.CreateOr(LHS.getVal(), RHS.getVal(), "or"));
643
644 assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
645}
646
647RValue CodeGenFunction::EmitBinaryLAnd(const BinaryOperator *E) {
648 Value *LHSCond = EvaluateExprAsBool(E->getLHS());
649
650 BasicBlock *ContBlock = new BasicBlock("land_cont");
651 BasicBlock *RHSBlock = new BasicBlock("land_rhs");
652
653 BasicBlock *OrigBlock = Builder.GetInsertBlock();
654 Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock);
655
656 EmitBlock(RHSBlock);
657 Value *RHSCond = EvaluateExprAsBool(E->getRHS());
658
659 // Reaquire the RHS block, as there may be subblocks inserted.
660 RHSBlock = Builder.GetInsertBlock();
661 EmitBlock(ContBlock);
662
663 // Create a PHI node. If we just evaluted the LHS condition, the result is
664 // false. If we evaluated both, the result is the RHS condition.
665 PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land");
666 PN->reserveOperandSpace(2);
667 PN->addIncoming(ConstantInt::getFalse(), OrigBlock);
668 PN->addIncoming(RHSCond, RHSBlock);
669
670 // ZExt result to int.
671 return RValue::get(Builder.CreateZExt(PN, LLVMIntTy, "land.ext"));
672}
673
674RValue CodeGenFunction::EmitBinaryLOr(const BinaryOperator *E) {
675 Value *LHSCond = EvaluateExprAsBool(E->getLHS());
676
677 BasicBlock *ContBlock = new BasicBlock("lor_cont");
678 BasicBlock *RHSBlock = new BasicBlock("lor_rhs");
679
680 BasicBlock *OrigBlock = Builder.GetInsertBlock();
681 Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock);
682
683 EmitBlock(RHSBlock);
684 Value *RHSCond = EvaluateExprAsBool(E->getRHS());
685
686 // Reaquire the RHS block, as there may be subblocks inserted.
687 RHSBlock = Builder.GetInsertBlock();
688 EmitBlock(ContBlock);
689
690 // Create a PHI node. If we just evaluted the LHS condition, the result is
691 // true. If we evaluated both, the result is the RHS condition.
692 PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor");
693 PN->reserveOperandSpace(2);
694 PN->addIncoming(ConstantInt::getTrue(), OrigBlock);
695 PN->addIncoming(RHSCond, RHSBlock);
696
697 // ZExt result to int.
698 return RValue::get(Builder.CreateZExt(PN, LLVMIntTy, "lor.ext"));
699}
700
701RValue CodeGenFunction::EmitBinaryAssign(const BinaryOperator *E) {
702 LValue LHS = EmitLValue(E->getLHS());
703
704 QualType RHSTy;
705 RValue RHS = EmitExprWithUsualUnaryConversions(E->getRHS(), RHSTy);
706
707 // Convert the RHS to the type of the LHS.
708 RHS = EmitConversion(RHS, RHSTy, E->getType());
709
710 // Store the value into the LHS.
711 EmitStoreThroughLValue(RHS, LHS, E->getType());
712
713 // Return the converted RHS.
714 return RHS;
715}
716
717
718RValue CodeGenFunction::EmitBinaryComma(const BinaryOperator *E) {
719 EmitExpr(E->getLHS());
720 return EmitExpr(E->getRHS());
721}