blob: 83e6a4779ea5e7b974bcd20003645c7630bab55f [file] [log] [blame]
Chris Lattnerbed31442007-05-28 01:07:47 +00001//===--- CodeGenFunction.h - Per-Function state for LLVM CodeGen ----------===//
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 is the internal per-function state used for llvm translation.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef CODEGEN_CODEGENFUNCTION_H
15#define CODEGEN_CODEGENFUNCTION_H
16
Chris Lattnerac248202007-05-30 00:13:02 +000017#include "llvm/ADT/DenseMap.h"
Chris Lattner308f4312007-05-29 23:50:05 +000018#include "llvm/Support/LLVMBuilder.h"
19
Chris Lattnerbed31442007-05-28 01:07:47 +000020namespace llvm {
21 class Module;
22namespace clang {
23 class ASTContext;
Chris Lattner84915fa2007-06-02 04:16:21 +000024 class Decl;
Chris Lattnerbed31442007-05-28 01:07:47 +000025 class FunctionDecl;
Chris Lattnerd1af2d22007-05-29 23:17:50 +000026 class QualType;
27 class SourceLocation;
28 class TargetInfo;
Chris Lattner84915fa2007-06-02 04:16:21 +000029
Chris Lattner308f4312007-05-29 23:50:05 +000030 class Stmt;
31 class CompoundStmt;
Chris Lattnerac248202007-05-30 00:13:02 +000032 class LabelStmt;
33 class GotoStmt;
Chris Lattner5269c032007-05-30 21:03:58 +000034 class IfStmt;
Chris Lattner3f3dbee2007-06-02 03:19:07 +000035 class ReturnStmt;
Chris Lattner84915fa2007-06-02 04:16:21 +000036 class DeclStmt;
Chris Lattnerbed31442007-05-28 01:07:47 +000037
Chris Lattner208ae962007-05-30 17:57:17 +000038 class Expr;
Chris Lattnerd7f58862007-06-02 05:24:33 +000039 class DeclRefExpr;
Chris Lattner208ae962007-05-30 17:57:17 +000040 class IntegerLiteral;
Chris Lattnerdb91b162007-06-02 00:16:28 +000041 class BinaryOperator;
Chris Lattner208ae962007-05-30 17:57:17 +000042
Chris Lattner84915fa2007-06-02 04:16:21 +000043 class BlockVarDecl;
44 class EnumConstantDecl;
Chris Lattnerbed31442007-05-28 01:07:47 +000045namespace CodeGen {
46 class CodeGenModule;
47
Chris Lattnerd7f58862007-06-02 05:24:33 +000048
49/// ExprResult - This trivial value class is used to represent the result of an
50/// expression that is evaluated. It can be one of two things: either a simple
51/// LLVM SSA value, or the address of an aggregate value in memory. These two
52/// possibilities are discriminated by isAggregate/isScalar.
Chris Lattner5269c032007-05-30 21:03:58 +000053class ExprResult {
Chris Lattner208ae962007-05-30 17:57:17 +000054 Value *V;
Chris Lattnerdb91b162007-06-02 00:16:28 +000055 // TODO: Encode this into the low bit of pointer for more efficient
56 // return-by-value.
Chris Lattner5269c032007-05-30 21:03:58 +000057 bool IsAggregate;
58public:
59
60 bool isAggregate() const { return IsAggregate; }
61 bool isScalar() const { return !IsAggregate; }
62
63 /// getVal() - Return the Value* of this scalar value.
64 Value *getVal() const {
65 assert(!isAggregate() && "Not a scalar!");
66 return V;
67 }
68
69 /// getAggregateVal() - Return the Value* of the address of the aggregate.
70 Value *getAggregateVal() const {
71 assert(isAggregate() && "Not an aggregate!");
72 return V;
73 }
Chris Lattner208ae962007-05-30 17:57:17 +000074
75 static ExprResult get(Value *V) {
76 ExprResult ER;
77 ER.V = V;
Chris Lattner5269c032007-05-30 21:03:58 +000078 ER.IsAggregate = false;
Chris Lattner208ae962007-05-30 17:57:17 +000079 return ER;
80 }
81 static ExprResult getAggregate(Value *V) {
82 ExprResult ER;
83 ER.V = V;
Chris Lattner5269c032007-05-30 21:03:58 +000084 ER.IsAggregate = true;
Chris Lattner208ae962007-05-30 17:57:17 +000085 return ER;
86 }
87};
Chris Lattnerd7f58862007-06-02 05:24:33 +000088
89
90/// LValue - This represents an lvalue references. Because C/C++ allow
91/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
92/// bitrange.
93class LValue {
94 // FIXME: Volatility. Restrict?
95 llvm::Value *V;
96public:
97 bool isBitfield() const { return false; }
Chris Lattner208ae962007-05-30 17:57:17 +000098
Chris Lattnerd7f58862007-06-02 05:24:33 +000099 llvm::Value *getAddress() const { assert(!isBitfield()); return V; }
100
101 static LValue getAddr(Value *V) {
102 LValue R;
103 R.V = V;
104 return R;
105 }
106};
107
Chris Lattnerbed31442007-05-28 01:07:47 +0000108/// CodeGenFunction - This class organizes the per-function state that is used
109/// while generating LLVM code.
110class CodeGenFunction {
111 CodeGenModule &CGM; // Per-module state.
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000112 TargetInfo &Target;
Chris Lattner308f4312007-05-29 23:50:05 +0000113 LLVMBuilder Builder;
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000114
115 const FunctionDecl *CurFuncDecl;
Chris Lattnerac248202007-05-30 00:13:02 +0000116 llvm::Function *CurFn;
117
Chris Lattner03df1222007-06-02 04:53:11 +0000118 /// AllocaInsertPoint - This is an instruction in the entry block before which
119 /// we prefer to insert allocas.
120 llvm::Instruction *AllocaInsertPt;
121
Chris Lattner84915fa2007-06-02 04:16:21 +0000122 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
123 /// decls.
124 DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
125
Chris Lattnerac248202007-05-30 00:13:02 +0000126 /// LabelMap - This keeps track of the LLVM basic block for each C label.
127 DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
Chris Lattnerbed31442007-05-28 01:07:47 +0000128public:
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000129 CodeGenFunction(CodeGenModule &cgm);
Chris Lattnerbed31442007-05-28 01:07:47 +0000130
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000131 const llvm::Type *ConvertType(QualType T, SourceLocation Loc);
132
Chris Lattner308f4312007-05-29 23:50:05 +0000133 void GenerateCode(const FunctionDecl *FD);
134
Chris Lattnerac248202007-05-30 00:13:02 +0000135
136 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
137 /// label maps to.
138 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
139
140
141 void EmitBlock(BasicBlock *BB);
Chris Lattner84915fa2007-06-02 04:16:21 +0000142
Chris Lattnerac248202007-05-30 00:13:02 +0000143
Chris Lattner308f4312007-05-29 23:50:05 +0000144 //===--------------------------------------------------------------------===//
Chris Lattner84915fa2007-06-02 04:16:21 +0000145 // Local Declaration Emission
146 //===--------------------------------------------------------------------===//
147
148 void EmitDeclStmt(const DeclStmt &S);
Chris Lattner84915fa2007-06-02 04:16:21 +0000149 void EmitEnumConstantDecl(const EnumConstantDecl &D);
Chris Lattner03df1222007-06-02 04:53:11 +0000150 void EmitBlockVarDecl(const BlockVarDecl &D);
151 void EmitLocalBlockVarDecl(const BlockVarDecl &D);
152
Chris Lattner84915fa2007-06-02 04:16:21 +0000153 //===--------------------------------------------------------------------===//
Chris Lattner308f4312007-05-29 23:50:05 +0000154 // Statement Emission
155 //===--------------------------------------------------------------------===//
156
157 void EmitStmt(const Stmt *S);
158 void EmitCompoundStmt(const CompoundStmt &S);
Chris Lattnerac248202007-05-30 00:13:02 +0000159 void EmitLabelStmt(const LabelStmt &S);
160 void EmitGotoStmt(const GotoStmt &S);
Chris Lattner5269c032007-05-30 21:03:58 +0000161 void EmitIfStmt(const IfStmt &S);
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000162 void EmitReturnStmt(const ReturnStmt &S);
Chris Lattner208ae962007-05-30 17:57:17 +0000163
164 //===--------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000165 // LValue Expression Emission
166 //===--------------------------------------------------------------------===//
167
168 LValue EmitLValue(const Expr *E);
169 LValue EmitDeclRefLValue(const DeclRefExpr *E);
170
171 //===--------------------------------------------------------------------===//
Chris Lattner208ae962007-05-30 17:57:17 +0000172 // Expression Emission
173 //===--------------------------------------------------------------------===//
174
175 ExprResult EmitExpr(const Expr *E);
176 ExprResult EmitIntegerLiteral(const IntegerLiteral *E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000177 ExprResult EmitBinaryOperator(const BinaryOperator *E);
178
179
180 void EmitUsualArithmeticConversions(const BinaryOperator *E,
181 ExprResult &LHS, ExprResult &RHS);
182
183 // Binary Operators.
184 ExprResult EmitBinaryAdd(const BinaryOperator *E);
Chris Lattnerbed31442007-05-28 01:07:47 +0000185};
186} // end namespace CodeGen
187} // end namespace clang
188} // end namespace llvm
189
190#endif