blob: 94f0f2d02e2ee747589514a1004d37efc9ae6006 [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
26/// EvaluateScalarValueToBool - Evaluate the specified expression value to a
27/// boolean (i1) truth value. This is equivalent to "Val == 0".
28Value *CodeGenFunction::EvaluateScalarValueToBool(ExprResult Val, QualType Ty) {
29 Ty = Ty.getCanonicalType();
30 Value *Result;
31 if (const BuiltinType *BT = dyn_cast<BuiltinType>(Ty)) {
32 switch (BT->getKind()) {
33 default: assert(0 && "Unknown scalar value");
34 case BuiltinType::Bool:
35 Result = Val.getVal();
36 // Bool is already evaluated right.
37 assert(Result->getType() == llvm::Type::Int1Ty &&
38 "Unexpected bool value type!");
39 return Result;
40 case BuiltinType::Char:
41 case BuiltinType::SChar:
42 case BuiltinType::UChar:
43 case BuiltinType::Short:
44 case BuiltinType::UShort:
45 case BuiltinType::Int:
46 case BuiltinType::UInt:
47 case BuiltinType::Long:
48 case BuiltinType::ULong:
49 case BuiltinType::LongLong:
50 case BuiltinType::ULongLong:
51 // Code below handles simple integers.
52 break;
53 case BuiltinType::Float:
54 case BuiltinType::Double:
55 case BuiltinType::LongDouble: {
56 // Compare against 0.0 for fp scalars.
57 Result = Val.getVal();
58 llvm::Value *Zero = Constant::getNullValue(Result->getType());
59 // FIXME: llvm-gcc produces a une comparison: validate this is right.
60 Result = Builder.CreateFCmpUNE(Result, Zero, "tobool");
61 return Result;
62 }
63
64 case BuiltinType::FloatComplex:
65 case BuiltinType::DoubleComplex:
66 case BuiltinType::LongDoubleComplex:
67 assert(0 && "comparisons against complex not implemented yet");
68 }
69 } else {
70 assert((isa<PointerType>(Ty) ||
71 cast<TagType>(Ty)->getDecl()->getKind() == Decl::Enum) &&
72 "Unknown scalar type");
73 // Code below handles this fine.
74 }
75
76 // Usual case for integers, pointers, and enums: compare against zero.
77 Result = Val.getVal();
78 llvm::Value *Zero = Constant::getNullValue(Result->getType());
79 return Builder.CreateICmpNE(Result, Zero, "tobool");
80}
81
82//===--------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +000083// LValue Expression Emission
84//===--------------------------------------------------------------------===//
85
86LValue CodeGenFunction::EmitLValue(const Expr *E) {
87 switch (E->getStmtClass()) {
88 default:
89 printf("Unimplemented lvalue expr!\n");
90 E->dump();
91 return LValue::getAddr(UndefValue::get(
92 llvm::PointerType::get(llvm::Type::Int32Ty)));
93
94 case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E));
95 }
96}
97
98
99LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
100 const Decl *D = E->getDecl();
101 if (isa<BlockVarDecl>(D)) {
102 Value *V = LocalDeclMap[D];
103 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
104 return LValue::getAddr(V);
105 }
106 assert(0 && "Unimp declref");
107}
Chris Lattnere47e4402007-06-01 18:02:12 +0000108
109//===--------------------------------------------------------------------===//
110// Expression Emission
111//===--------------------------------------------------------------------===//
112
113ExprResult CodeGenFunction::EmitExpr(const Expr *E) {
114 assert(E && "Null expression?");
115
116 switch (E->getStmtClass()) {
117 default:
118 printf("Unimplemented expr!\n");
119 E->dump();
120 return ExprResult::get(UndefValue::get(llvm::Type::Int32Ty));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000121
122 // l-values.
123 case Expr::DeclRefExprClass: {
124 // FIXME: EnumConstantDecl's are not lvalues.
125 LValue LV = EmitLValue(E);
126 // FIXME: this is silly.
127 assert(!LV.isBitfield());
128 return ExprResult::get(Builder.CreateLoad(LV.getAddress(), "tmp"));
129 }
130
131 // Leaf expressions.
132 case Expr::IntegerLiteralClass:
Chris Lattnere47e4402007-06-01 18:02:12 +0000133 return EmitIntegerLiteral(cast<IntegerLiteral>(E));
Chris Lattnerdb91b162007-06-02 00:16:28 +0000134
Chris Lattnerd7f58862007-06-02 05:24:33 +0000135 // Operators.
136 case Expr::ParenExprClass:
137 return EmitExpr(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnerf0106d22007-06-02 19:33:17 +0000138 case Expr::UnaryOperatorClass:
139 return EmitUnaryOperator(cast<UnaryOperator>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000140 case Expr::BinaryOperatorClass:
Chris Lattnerdb91b162007-06-02 00:16:28 +0000141 return EmitBinaryOperator(cast<BinaryOperator>(E));
Chris Lattnere47e4402007-06-01 18:02:12 +0000142 }
143
144}
145
146ExprResult CodeGenFunction::EmitIntegerLiteral(const IntegerLiteral *E) {
147 return ExprResult::get(ConstantInt::get(E->getValue()));
148}
149
Chris Lattnerf0106d22007-06-02 19:33:17 +0000150//===--------------------------------------------------------------------===//
151// Unary Operator Emission
152//===--------------------------------------------------------------------===//
153
154ExprResult CodeGenFunction::EmitUnaryOperator(const UnaryOperator *E) {
155 switch (E->getOpcode()) {
156 default:
157 printf("Unimplemented unary expr!\n");
158 E->dump();
159 return ExprResult::get(UndefValue::get(llvm::Type::Int32Ty));
160 case UnaryOperator::LNot: return EmitUnaryLNot(E);
161 }
162}
163
164/// C99 6.5.3.3
165ExprResult CodeGenFunction::EmitUnaryLNot(const UnaryOperator *E) {
166 ExprResult Op = EmitExpr(E->getSubExpr());
167
168 //UsualUnary();
169
170 // Compare to zero.
171 Value *BoolVal = EvaluateScalarValueToBool(Op, E->getSubExpr()->getType());
172
173 // Invert value.
174 BoolVal = Builder.CreateNot(BoolVal, "lnot");
175
176 // ZExt result to int.
177 const llvm::Type *ResTy = ConvertType(E->getType(), E->getOperatorLoc());
178 return ExprResult::get(Builder.CreateZExt(BoolVal, ResTy, "lnot.ext"));
179}
180
Chris Lattnere47e4402007-06-01 18:02:12 +0000181
Chris Lattnerdb91b162007-06-02 00:16:28 +0000182//===--------------------------------------------------------------------===//
183// Binary Operator Emission
184//===--------------------------------------------------------------------===//
185
186// FIXME describe.
187void CodeGenFunction::EmitUsualArithmeticConversions(const BinaryOperator *E,
188 ExprResult &LHS,
189 ExprResult &RHS) {
190 // FIXME: implement right.
191 LHS = EmitExpr(E->getLHS());
192 RHS = EmitExpr(E->getRHS());
193}
194
195
196ExprResult CodeGenFunction::EmitBinaryOperator(const BinaryOperator *E) {
197 switch (E->getOpcode()) {
198 default:
199 printf("Unimplemented expr!\n");
200 E->dump();
201 return ExprResult::get(UndefValue::get(llvm::Type::Int32Ty));
202 case BinaryOperator::Add: return EmitBinaryAdd(E);
203 }
204}
205
206
207ExprResult CodeGenFunction::EmitBinaryAdd(const BinaryOperator *E) {
208 ExprResult LHS, RHS;
209
210 EmitUsualArithmeticConversions(E, LHS, RHS);
211
212
213 return ExprResult::get(Builder.CreateAdd(LHS.getVal(), RHS.getVal(), "tmp"));
214}