Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 1 | //===--- 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 Lattner | ac24820 | 2007-05-30 00:13:02 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | 308f431 | 2007-05-29 23:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/Support/LLVMBuilder.h" |
| 19 | |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 20 | namespace llvm { |
| 21 | class Module; |
| 22 | namespace clang { |
| 23 | class ASTContext; |
| 24 | class FunctionDecl; |
Chris Lattner | d1af2d2 | 2007-05-29 23:17:50 +0000 | [diff] [blame] | 25 | class QualType; |
| 26 | class SourceLocation; |
| 27 | class TargetInfo; |
Chris Lattner | 308f431 | 2007-05-29 23:50:05 +0000 | [diff] [blame] | 28 | class Stmt; |
| 29 | class CompoundStmt; |
Chris Lattner | ac24820 | 2007-05-30 00:13:02 +0000 | [diff] [blame] | 30 | class LabelStmt; |
| 31 | class GotoStmt; |
Chris Lattner | 5269c03 | 2007-05-30 21:03:58 +0000 | [diff] [blame^] | 32 | class IfStmt; |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 208ae96 | 2007-05-30 17:57:17 +0000 | [diff] [blame] | 34 | class Expr; |
| 35 | class IntegerLiteral; |
| 36 | |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 37 | namespace CodeGen { |
| 38 | class CodeGenModule; |
| 39 | |
Chris Lattner | 5269c03 | 2007-05-30 21:03:58 +0000 | [diff] [blame^] | 40 | class ExprResult { |
Chris Lattner | 208ae96 | 2007-05-30 17:57:17 +0000 | [diff] [blame] | 41 | Value *V; |
Chris Lattner | 5269c03 | 2007-05-30 21:03:58 +0000 | [diff] [blame^] | 42 | bool IsAggregate; |
| 43 | public: |
| 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 Lattner | 208ae96 | 2007-05-30 17:57:17 +0000 | [diff] [blame] | 59 | |
| 60 | static ExprResult get(Value *V) { |
| 61 | ExprResult ER; |
| 62 | ER.V = V; |
Chris Lattner | 5269c03 | 2007-05-30 21:03:58 +0000 | [diff] [blame^] | 63 | ER.IsAggregate = false; |
Chris Lattner | 208ae96 | 2007-05-30 17:57:17 +0000 | [diff] [blame] | 64 | return ER; |
| 65 | } |
| 66 | static ExprResult getAggregate(Value *V) { |
| 67 | ExprResult ER; |
| 68 | ER.V = V; |
Chris Lattner | 5269c03 | 2007-05-30 21:03:58 +0000 | [diff] [blame^] | 69 | ER.IsAggregate = true; |
Chris Lattner | 208ae96 | 2007-05-30 17:57:17 +0000 | [diff] [blame] | 70 | return ER; |
| 71 | } |
| 72 | }; |
| 73 | |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 74 | /// CodeGenFunction - This class organizes the per-function state that is used |
| 75 | /// while generating LLVM code. |
| 76 | class CodeGenFunction { |
| 77 | CodeGenModule &CGM; // Per-module state. |
Chris Lattner | d1af2d2 | 2007-05-29 23:17:50 +0000 | [diff] [blame] | 78 | TargetInfo &Target; |
Chris Lattner | 308f431 | 2007-05-29 23:50:05 +0000 | [diff] [blame] | 79 | LLVMBuilder Builder; |
Chris Lattner | ac24820 | 2007-05-30 00:13:02 +0000 | [diff] [blame] | 80 | 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 Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 84 | public: |
Chris Lattner | d1af2d2 | 2007-05-29 23:17:50 +0000 | [diff] [blame] | 85 | CodeGenFunction(CodeGenModule &cgm); |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 86 | |
Chris Lattner | d1af2d2 | 2007-05-29 23:17:50 +0000 | [diff] [blame] | 87 | const llvm::Type *ConvertType(QualType T, SourceLocation Loc); |
| 88 | |
Chris Lattner | 308f431 | 2007-05-29 23:50:05 +0000 | [diff] [blame] | 89 | void GenerateCode(const FunctionDecl *FD); |
| 90 | |
Chris Lattner | ac24820 | 2007-05-30 00:13:02 +0000 | [diff] [blame] | 91 | |
| 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 Lattner | 308f431 | 2007-05-29 23:50:05 +0000 | [diff] [blame] | 99 | //===--------------------------------------------------------------------===// |
| 100 | // Statement Emission |
| 101 | //===--------------------------------------------------------------------===// |
| 102 | |
| 103 | void EmitStmt(const Stmt *S); |
| 104 | void EmitCompoundStmt(const CompoundStmt &S); |
Chris Lattner | ac24820 | 2007-05-30 00:13:02 +0000 | [diff] [blame] | 105 | void EmitLabelStmt(const LabelStmt &S); |
| 106 | void EmitGotoStmt(const GotoStmt &S); |
Chris Lattner | 5269c03 | 2007-05-30 21:03:58 +0000 | [diff] [blame^] | 107 | void EmitIfStmt(const IfStmt &S); |
Chris Lattner | 208ae96 | 2007-05-30 17:57:17 +0000 | [diff] [blame] | 108 | |
| 109 | //===--------------------------------------------------------------------===// |
| 110 | // Expression Emission |
| 111 | //===--------------------------------------------------------------------===// |
| 112 | |
| 113 | ExprResult EmitExpr(const Expr *E); |
| 114 | ExprResult EmitIntegerLiteral(const IntegerLiteral *E); |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 115 | }; |
| 116 | } // end namespace CodeGen |
| 117 | } // end namespace clang |
| 118 | } // end namespace llvm |
| 119 | |
| 120 | #endif |