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; |
Chris Lattner | db91b16 | 2007-06-02 00:16:28 +0000 | [diff] [blame^] | 36 | class BinaryOperator; |
Chris Lattner | 208ae96 | 2007-05-30 17:57:17 +0000 | [diff] [blame] | 37 | |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 38 | namespace CodeGen { |
| 39 | class CodeGenModule; |
| 40 | |
Chris Lattner | 5269c03 | 2007-05-30 21:03:58 +0000 | [diff] [blame] | 41 | class ExprResult { |
Chris Lattner | 208ae96 | 2007-05-30 17:57:17 +0000 | [diff] [blame] | 42 | Value *V; |
Chris Lattner | db91b16 | 2007-06-02 00:16:28 +0000 | [diff] [blame^] | 43 | // TODO: Encode this into the low bit of pointer for more efficient |
| 44 | // return-by-value. |
Chris Lattner | 5269c03 | 2007-05-30 21:03:58 +0000 | [diff] [blame] | 45 | bool IsAggregate; |
| 46 | public: |
| 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 Lattner | 208ae96 | 2007-05-30 17:57:17 +0000 | [diff] [blame] | 62 | |
| 63 | static ExprResult get(Value *V) { |
| 64 | ExprResult ER; |
| 65 | ER.V = V; |
Chris Lattner | 5269c03 | 2007-05-30 21:03:58 +0000 | [diff] [blame] | 66 | ER.IsAggregate = false; |
Chris Lattner | 208ae96 | 2007-05-30 17:57:17 +0000 | [diff] [blame] | 67 | return ER; |
| 68 | } |
| 69 | static ExprResult getAggregate(Value *V) { |
| 70 | ExprResult ER; |
| 71 | ER.V = V; |
Chris Lattner | 5269c03 | 2007-05-30 21:03:58 +0000 | [diff] [blame] | 72 | ER.IsAggregate = true; |
Chris Lattner | 208ae96 | 2007-05-30 17:57:17 +0000 | [diff] [blame] | 73 | return ER; |
| 74 | } |
| 75 | }; |
| 76 | |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 77 | /// CodeGenFunction - This class organizes the per-function state that is used |
| 78 | /// while generating LLVM code. |
| 79 | class CodeGenFunction { |
| 80 | CodeGenModule &CGM; // Per-module state. |
Chris Lattner | d1af2d2 | 2007-05-29 23:17:50 +0000 | [diff] [blame] | 81 | TargetInfo &Target; |
Chris Lattner | 308f431 | 2007-05-29 23:50:05 +0000 | [diff] [blame] | 82 | LLVMBuilder Builder; |
Chris Lattner | ac24820 | 2007-05-30 00:13:02 +0000 | [diff] [blame] | 83 | 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 Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 87 | public: |
Chris Lattner | d1af2d2 | 2007-05-29 23:17:50 +0000 | [diff] [blame] | 88 | CodeGenFunction(CodeGenModule &cgm); |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 89 | |
Chris Lattner | d1af2d2 | 2007-05-29 23:17:50 +0000 | [diff] [blame] | 90 | const llvm::Type *ConvertType(QualType T, SourceLocation Loc); |
| 91 | |
Chris Lattner | 308f431 | 2007-05-29 23:50:05 +0000 | [diff] [blame] | 92 | void GenerateCode(const FunctionDecl *FD); |
| 93 | |
Chris Lattner | ac24820 | 2007-05-30 00:13:02 +0000 | [diff] [blame] | 94 | |
| 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 Lattner | 308f431 | 2007-05-29 23:50:05 +0000 | [diff] [blame] | 102 | //===--------------------------------------------------------------------===// |
| 103 | // Statement Emission |
| 104 | //===--------------------------------------------------------------------===// |
| 105 | |
| 106 | void EmitStmt(const Stmt *S); |
| 107 | void EmitCompoundStmt(const CompoundStmt &S); |
Chris Lattner | ac24820 | 2007-05-30 00:13:02 +0000 | [diff] [blame] | 108 | void EmitLabelStmt(const LabelStmt &S); |
| 109 | void EmitGotoStmt(const GotoStmt &S); |
Chris Lattner | 5269c03 | 2007-05-30 21:03:58 +0000 | [diff] [blame] | 110 | void EmitIfStmt(const IfStmt &S); |
Chris Lattner | 208ae96 | 2007-05-30 17:57:17 +0000 | [diff] [blame] | 111 | |
| 112 | //===--------------------------------------------------------------------===// |
| 113 | // Expression Emission |
| 114 | //===--------------------------------------------------------------------===// |
| 115 | |
| 116 | ExprResult EmitExpr(const Expr *E); |
| 117 | ExprResult EmitIntegerLiteral(const IntegerLiteral *E); |
Chris Lattner | db91b16 | 2007-06-02 00:16:28 +0000 | [diff] [blame^] | 118 | 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 Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 126 | }; |
| 127 | } // end namespace CodeGen |
| 128 | } // end namespace clang |
| 129 | } // end namespace llvm |
| 130 | |
| 131 | #endif |