blob: 6e042d986583cb8a1dabd8098cc0f677553604be [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;
24 class FunctionDecl;
Chris Lattnerd1af2d22007-05-29 23:17:50 +000025 class QualType;
26 class SourceLocation;
27 class TargetInfo;
Chris Lattner308f4312007-05-29 23:50:05 +000028 class Stmt;
29 class CompoundStmt;
Chris Lattnerac248202007-05-30 00:13:02 +000030 class LabelStmt;
31 class GotoStmt;
Chris Lattner5269c032007-05-30 21:03:58 +000032 class IfStmt;
Chris Lattnerbed31442007-05-28 01:07:47 +000033
Chris Lattner208ae962007-05-30 17:57:17 +000034 class Expr;
35 class IntegerLiteral;
Chris Lattnerdb91b162007-06-02 00:16:28 +000036 class BinaryOperator;
Chris Lattner208ae962007-05-30 17:57:17 +000037
Chris Lattnerbed31442007-05-28 01:07:47 +000038namespace CodeGen {
39 class CodeGenModule;
40
Chris Lattner5269c032007-05-30 21:03:58 +000041class ExprResult {
Chris Lattner208ae962007-05-30 17:57:17 +000042 Value *V;
Chris Lattnerdb91b162007-06-02 00:16:28 +000043 // TODO: Encode this into the low bit of pointer for more efficient
44 // return-by-value.
Chris Lattner5269c032007-05-30 21:03:58 +000045 bool IsAggregate;
46public:
47
48 bool isAggregate() const { return IsAggregate; }
49 bool isScalar() const { return !IsAggregate; }
50
51 /// getVal() - Return the Value* of this scalar value.
52 Value *getVal() const {
53 assert(!isAggregate() && "Not a scalar!");
54 return V;
55 }
56
57 /// getAggregateVal() - Return the Value* of the address of the aggregate.
58 Value *getAggregateVal() const {
59 assert(isAggregate() && "Not an aggregate!");
60 return V;
61 }
Chris Lattner208ae962007-05-30 17:57:17 +000062
63 static ExprResult get(Value *V) {
64 ExprResult ER;
65 ER.V = V;
Chris Lattner5269c032007-05-30 21:03:58 +000066 ER.IsAggregate = false;
Chris Lattner208ae962007-05-30 17:57:17 +000067 return ER;
68 }
69 static ExprResult getAggregate(Value *V) {
70 ExprResult ER;
71 ER.V = V;
Chris Lattner5269c032007-05-30 21:03:58 +000072 ER.IsAggregate = true;
Chris Lattner208ae962007-05-30 17:57:17 +000073 return ER;
74 }
75};
76
Chris Lattnerbed31442007-05-28 01:07:47 +000077/// CodeGenFunction - This class organizes the per-function state that is used
78/// while generating LLVM code.
79class CodeGenFunction {
80 CodeGenModule &CGM; // Per-module state.
Chris Lattnerd1af2d22007-05-29 23:17:50 +000081 TargetInfo &Target;
Chris Lattner308f4312007-05-29 23:50:05 +000082 LLVMBuilder Builder;
Chris Lattnerac248202007-05-30 00:13:02 +000083 llvm::Function *CurFn;
84
85 /// LabelMap - This keeps track of the LLVM basic block for each C label.
86 DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
Chris Lattnerbed31442007-05-28 01:07:47 +000087public:
Chris Lattnerd1af2d22007-05-29 23:17:50 +000088 CodeGenFunction(CodeGenModule &cgm);
Chris Lattnerbed31442007-05-28 01:07:47 +000089
Chris Lattnerd1af2d22007-05-29 23:17:50 +000090 const llvm::Type *ConvertType(QualType T, SourceLocation Loc);
91
Chris Lattner308f4312007-05-29 23:50:05 +000092 void GenerateCode(const FunctionDecl *FD);
93
Chris Lattnerac248202007-05-30 00:13:02 +000094
95 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
96 /// label maps to.
97 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
98
99
100 void EmitBlock(BasicBlock *BB);
101
Chris Lattner308f4312007-05-29 23:50:05 +0000102 //===--------------------------------------------------------------------===//
103 // Statement Emission
104 //===--------------------------------------------------------------------===//
105
106 void EmitStmt(const Stmt *S);
107 void EmitCompoundStmt(const CompoundStmt &S);
Chris Lattnerac248202007-05-30 00:13:02 +0000108 void EmitLabelStmt(const LabelStmt &S);
109 void EmitGotoStmt(const GotoStmt &S);
Chris Lattner5269c032007-05-30 21:03:58 +0000110 void EmitIfStmt(const IfStmt &S);
Chris Lattner208ae962007-05-30 17:57:17 +0000111
112 //===--------------------------------------------------------------------===//
113 // Expression Emission
114 //===--------------------------------------------------------------------===//
115
116 ExprResult EmitExpr(const Expr *E);
117 ExprResult EmitIntegerLiteral(const IntegerLiteral *E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000118 ExprResult EmitBinaryOperator(const BinaryOperator *E);
119
120
121 void EmitUsualArithmeticConversions(const BinaryOperator *E,
122 ExprResult &LHS, ExprResult &RHS);
123
124 // Binary Operators.
125 ExprResult EmitBinaryAdd(const BinaryOperator *E);
Chris Lattnerbed31442007-05-28 01:07:47 +0000126};
127} // end namespace CodeGen
128} // end namespace clang
129} // end namespace llvm
130
131#endif