blob: 12eff9b666ed756b442b06e493f9f477ca5a4a7e [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;
36
Chris Lattnerbed31442007-05-28 01:07:47 +000037namespace CodeGen {
38 class CodeGenModule;
39
Chris Lattner5269c032007-05-30 21:03:58 +000040class ExprResult {
Chris Lattner208ae962007-05-30 17:57:17 +000041 Value *V;
Chris Lattner5269c032007-05-30 21:03:58 +000042 bool IsAggregate;
43public:
44
45 bool isAggregate() const { return IsAggregate; }
46 bool isScalar() const { return !IsAggregate; }
47
48 /// getVal() - Return the Value* of this scalar value.
49 Value *getVal() const {
50 assert(!isAggregate() && "Not a scalar!");
51 return V;
52 }
53
54 /// getAggregateVal() - Return the Value* of the address of the aggregate.
55 Value *getAggregateVal() const {
56 assert(isAggregate() && "Not an aggregate!");
57 return V;
58 }
Chris Lattner208ae962007-05-30 17:57:17 +000059
60 static ExprResult get(Value *V) {
61 ExprResult ER;
62 ER.V = V;
Chris Lattner5269c032007-05-30 21:03:58 +000063 ER.IsAggregate = false;
Chris Lattner208ae962007-05-30 17:57:17 +000064 return ER;
65 }
66 static ExprResult getAggregate(Value *V) {
67 ExprResult ER;
68 ER.V = V;
Chris Lattner5269c032007-05-30 21:03:58 +000069 ER.IsAggregate = true;
Chris Lattner208ae962007-05-30 17:57:17 +000070 return ER;
71 }
72};
73
Chris Lattnerbed31442007-05-28 01:07:47 +000074/// CodeGenFunction - This class organizes the per-function state that is used
75/// while generating LLVM code.
76class CodeGenFunction {
77 CodeGenModule &CGM; // Per-module state.
Chris Lattnerd1af2d22007-05-29 23:17:50 +000078 TargetInfo &Target;
Chris Lattner308f4312007-05-29 23:50:05 +000079 LLVMBuilder Builder;
Chris Lattnerac248202007-05-30 00:13:02 +000080 llvm::Function *CurFn;
81
82 /// LabelMap - This keeps track of the LLVM basic block for each C label.
83 DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
Chris Lattnerbed31442007-05-28 01:07:47 +000084public:
Chris Lattnerd1af2d22007-05-29 23:17:50 +000085 CodeGenFunction(CodeGenModule &cgm);
Chris Lattnerbed31442007-05-28 01:07:47 +000086
Chris Lattnerd1af2d22007-05-29 23:17:50 +000087 const llvm::Type *ConvertType(QualType T, SourceLocation Loc);
88
Chris Lattner308f4312007-05-29 23:50:05 +000089 void GenerateCode(const FunctionDecl *FD);
90
Chris Lattnerac248202007-05-30 00:13:02 +000091
92 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
93 /// label maps to.
94 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
95
96
97 void EmitBlock(BasicBlock *BB);
98
Chris Lattner308f4312007-05-29 23:50:05 +000099 //===--------------------------------------------------------------------===//
100 // Statement Emission
101 //===--------------------------------------------------------------------===//
102
103 void EmitStmt(const Stmt *S);
104 void EmitCompoundStmt(const CompoundStmt &S);
Chris Lattnerac248202007-05-30 00:13:02 +0000105 void EmitLabelStmt(const LabelStmt &S);
106 void EmitGotoStmt(const GotoStmt &S);
Chris Lattner5269c032007-05-30 21:03:58 +0000107 void EmitIfStmt(const IfStmt &S);
Chris Lattner208ae962007-05-30 17:57:17 +0000108
109 //===--------------------------------------------------------------------===//
110 // Expression Emission
111 //===--------------------------------------------------------------------===//
112
113 ExprResult EmitExpr(const Expr *E);
114 ExprResult EmitIntegerLiteral(const IntegerLiteral *E);
Chris Lattnerbed31442007-05-28 01:07:47 +0000115};
116} // end namespace CodeGen
117} // end namespace clang
118} // end namespace llvm
119
120#endif