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; |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 24 | class Decl; |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 25 | class FunctionDecl; |
Chris Lattner | d1af2d2 | 2007-05-29 23:17:50 +0000 | [diff] [blame] | 26 | class QualType; |
| 27 | class SourceLocation; |
| 28 | class TargetInfo; |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 308f431 | 2007-05-29 23:50:05 +0000 | [diff] [blame] | 30 | class Stmt; |
| 31 | class CompoundStmt; |
Chris Lattner | ac24820 | 2007-05-30 00:13:02 +0000 | [diff] [blame] | 32 | class LabelStmt; |
| 33 | class GotoStmt; |
Chris Lattner | 5269c03 | 2007-05-30 21:03:58 +0000 | [diff] [blame] | 34 | class IfStmt; |
Chris Lattner | 946aa31 | 2007-06-05 03:59:43 +0000 | [diff] [blame] | 35 | class WhileStmt; |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 36 | class DoStmt; |
| 37 | class ForStmt; |
Chris Lattner | 3f3dbee | 2007-06-02 03:19:07 +0000 | [diff] [blame] | 38 | class ReturnStmt; |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 39 | class DeclStmt; |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 208ae96 | 2007-05-30 17:57:17 +0000 | [diff] [blame] | 41 | class Expr; |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 42 | class DeclRefExpr; |
Chris Lattner | 4347e369 | 2007-06-06 04:54:52 +0000 | [diff] [blame] | 43 | class StringLiteral; |
Chris Lattner | 208ae96 | 2007-05-30 17:57:17 +0000 | [diff] [blame] | 44 | class IntegerLiteral; |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 45 | class CastExpr; |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 46 | class UnaryOperator; |
Chris Lattner | db91b16 | 2007-06-02 00:16:28 +0000 | [diff] [blame] | 47 | class BinaryOperator; |
Chris Lattner | d9d2fb1 | 2007-06-08 23:31:14 +0000 | [diff] [blame] | 48 | class ArraySubscriptExpr; |
Chris Lattner | 208ae96 | 2007-05-30 17:57:17 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 50 | class BlockVarDecl; |
| 51 | class EnumConstantDecl; |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 52 | namespace CodeGen { |
| 53 | class CodeGenModule; |
| 54 | |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 56 | /// RValue - This trivial value class is used to represent the result of an |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 57 | /// expression that is evaluated. It can be one of two things: either a simple |
| 58 | /// LLVM SSA value, or the address of an aggregate value in memory. These two |
| 59 | /// possibilities are discriminated by isAggregate/isScalar. |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 60 | class RValue { |
Chris Lattner | 208ae96 | 2007-05-30 17:57:17 +0000 | [diff] [blame] | 61 | Value *V; |
Chris Lattner | db91b16 | 2007-06-02 00:16:28 +0000 | [diff] [blame] | 62 | // TODO: Encode this into the low bit of pointer for more efficient |
| 63 | // return-by-value. |
Chris Lattner | 5269c03 | 2007-05-30 21:03:58 +0000 | [diff] [blame] | 64 | bool IsAggregate; |
| 65 | public: |
| 66 | |
| 67 | bool isAggregate() const { return IsAggregate; } |
| 68 | bool isScalar() const { return !IsAggregate; } |
| 69 | |
| 70 | /// getVal() - Return the Value* of this scalar value. |
| 71 | Value *getVal() const { |
| 72 | assert(!isAggregate() && "Not a scalar!"); |
| 73 | return V; |
| 74 | } |
| 75 | |
| 76 | /// getAggregateVal() - Return the Value* of the address of the aggregate. |
| 77 | Value *getAggregateVal() const { |
| 78 | assert(isAggregate() && "Not an aggregate!"); |
| 79 | return V; |
| 80 | } |
Chris Lattner | 208ae96 | 2007-05-30 17:57:17 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 82 | static RValue get(Value *V) { |
| 83 | RValue ER; |
Chris Lattner | 208ae96 | 2007-05-30 17:57:17 +0000 | [diff] [blame] | 84 | ER.V = V; |
Chris Lattner | 5269c03 | 2007-05-30 21:03:58 +0000 | [diff] [blame] | 85 | ER.IsAggregate = false; |
Chris Lattner | 208ae96 | 2007-05-30 17:57:17 +0000 | [diff] [blame] | 86 | return ER; |
| 87 | } |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 88 | static RValue getAggregate(Value *V) { |
| 89 | RValue ER; |
Chris Lattner | 208ae96 | 2007-05-30 17:57:17 +0000 | [diff] [blame] | 90 | ER.V = V; |
Chris Lattner | 5269c03 | 2007-05-30 21:03:58 +0000 | [diff] [blame] | 91 | ER.IsAggregate = true; |
Chris Lattner | 208ae96 | 2007-05-30 17:57:17 +0000 | [diff] [blame] | 92 | return ER; |
| 93 | } |
| 94 | }; |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 95 | |
| 96 | |
| 97 | /// LValue - This represents an lvalue references. Because C/C++ allow |
| 98 | /// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a |
| 99 | /// bitrange. |
| 100 | class LValue { |
| 101 | // FIXME: Volatility. Restrict? |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 102 | // alignment? |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 103 | llvm::Value *V; |
| 104 | public: |
| 105 | bool isBitfield() const { return false; } |
Chris Lattner | 208ae96 | 2007-05-30 17:57:17 +0000 | [diff] [blame] | 106 | |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 107 | llvm::Value *getAddress() const { assert(!isBitfield()); return V; } |
| 108 | |
| 109 | static LValue getAddr(Value *V) { |
| 110 | LValue R; |
| 111 | R.V = V; |
| 112 | return R; |
| 113 | } |
| 114 | }; |
| 115 | |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 116 | /// CodeGenFunction - This class organizes the per-function state that is used |
| 117 | /// while generating LLVM code. |
| 118 | class CodeGenFunction { |
| 119 | CodeGenModule &CGM; // Per-module state. |
Chris Lattner | d1af2d2 | 2007-05-29 23:17:50 +0000 | [diff] [blame] | 120 | TargetInfo &Target; |
Chris Lattner | 308f431 | 2007-05-29 23:50:05 +0000 | [diff] [blame] | 121 | LLVMBuilder Builder; |
Chris Lattner | 3f3dbee | 2007-06-02 03:19:07 +0000 | [diff] [blame] | 122 | |
| 123 | const FunctionDecl *CurFuncDecl; |
Chris Lattner | ac24820 | 2007-05-30 00:13:02 +0000 | [diff] [blame] | 124 | llvm::Function *CurFn; |
| 125 | |
Chris Lattner | 03df122 | 2007-06-02 04:53:11 +0000 | [diff] [blame] | 126 | /// AllocaInsertPoint - This is an instruction in the entry block before which |
| 127 | /// we prefer to insert allocas. |
| 128 | llvm::Instruction *AllocaInsertPt; |
| 129 | |
Chris Lattner | 6db1fb8 | 2007-06-02 22:49:07 +0000 | [diff] [blame] | 130 | const llvm::Type *LLVMIntTy; |
Chris Lattner | d9d2fb1 | 2007-06-08 23:31:14 +0000 | [diff] [blame] | 131 | unsigned LLVMPointerWidth; |
Chris Lattner | 6db1fb8 | 2007-06-02 22:49:07 +0000 | [diff] [blame] | 132 | |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 133 | /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C |
| 134 | /// decls. |
| 135 | DenseMap<const Decl*, llvm::Value*> LocalDeclMap; |
| 136 | |
Chris Lattner | ac24820 | 2007-05-30 00:13:02 +0000 | [diff] [blame] | 137 | /// LabelMap - This keeps track of the LLVM basic block for each C label. |
| 138 | DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap; |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 139 | public: |
Chris Lattner | d1af2d2 | 2007-05-29 23:17:50 +0000 | [diff] [blame] | 140 | CodeGenFunction(CodeGenModule &cgm); |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 6db1fb8 | 2007-06-02 22:49:07 +0000 | [diff] [blame] | 142 | ASTContext &getContext() const; |
| 143 | |
Chris Lattner | d1af2d2 | 2007-05-29 23:17:50 +0000 | [diff] [blame] | 144 | const llvm::Type *ConvertType(QualType T, SourceLocation Loc); |
| 145 | |
Chris Lattner | 308f431 | 2007-05-29 23:50:05 +0000 | [diff] [blame] | 146 | void GenerateCode(const FunctionDecl *FD); |
| 147 | |
Chris Lattner | ac24820 | 2007-05-30 00:13:02 +0000 | [diff] [blame] | 148 | |
| 149 | /// getBasicBlockForLabel - Return the LLVM basicblock that the specified |
| 150 | /// label maps to. |
| 151 | llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S); |
| 152 | |
| 153 | |
| 154 | void EmitBlock(BasicBlock *BB); |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 156 | |
| 157 | /// EvaluateExprAsBool - Perform the usual unary conversions on the specified |
| 158 | /// expression and compare the result against zero, returning an Int1Ty value. |
| 159 | Value *EvaluateExprAsBool(const Expr *E); |
Chris Lattner | ac24820 | 2007-05-30 00:13:02 +0000 | [diff] [blame] | 160 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 161 | //===--------------------------------------------------------------------===// |
| 162 | // Conversions |
| 163 | //===--------------------------------------------------------------------===// |
| 164 | |
| 165 | /// EmitConversion - Convert the value specied by Val, whose type is ValTy, to |
| 166 | /// the type specified by DstTy, following the rules of C99 6.3. |
Chris Lattner | cf106ab | 2007-06-06 04:05:39 +0000 | [diff] [blame] | 167 | RValue EmitConversion(RValue Val, QualType ValTy, QualType DstTy, |
| 168 | SourceLocation Loc); |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 169 | |
| 170 | /// ConvertScalarValueToBool - Convert the specified expression value to a |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 171 | /// boolean (i1) truth value. This is equivalent to "Val == 0". |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 172 | Value *ConvertScalarValueToBool(RValue Val, QualType Ty); |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 173 | |
Chris Lattner | 308f431 | 2007-05-29 23:50:05 +0000 | [diff] [blame] | 174 | //===--------------------------------------------------------------------===// |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 175 | // Local Declaration Emission |
| 176 | //===--------------------------------------------------------------------===// |
| 177 | |
| 178 | void EmitDeclStmt(const DeclStmt &S); |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 179 | void EmitEnumConstantDecl(const EnumConstantDecl &D); |
Chris Lattner | 03df122 | 2007-06-02 04:53:11 +0000 | [diff] [blame] | 180 | void EmitBlockVarDecl(const BlockVarDecl &D); |
| 181 | void EmitLocalBlockVarDecl(const BlockVarDecl &D); |
| 182 | |
Chris Lattner | 84915fa | 2007-06-02 04:16:21 +0000 | [diff] [blame] | 183 | //===--------------------------------------------------------------------===// |
Chris Lattner | 308f431 | 2007-05-29 23:50:05 +0000 | [diff] [blame] | 184 | // Statement Emission |
| 185 | //===--------------------------------------------------------------------===// |
| 186 | |
| 187 | void EmitStmt(const Stmt *S); |
| 188 | void EmitCompoundStmt(const CompoundStmt &S); |
Chris Lattner | ac24820 | 2007-05-30 00:13:02 +0000 | [diff] [blame] | 189 | void EmitLabelStmt(const LabelStmt &S); |
| 190 | void EmitGotoStmt(const GotoStmt &S); |
Chris Lattner | 5269c03 | 2007-05-30 21:03:58 +0000 | [diff] [blame] | 191 | void EmitIfStmt(const IfStmt &S); |
Chris Lattner | 946aa31 | 2007-06-05 03:59:43 +0000 | [diff] [blame] | 192 | void EmitWhileStmt(const WhileStmt &S); |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 193 | void EmitDoStmt(const DoStmt &S); |
| 194 | void EmitForStmt(const ForStmt &S); |
Chris Lattner | 3f3dbee | 2007-06-02 03:19:07 +0000 | [diff] [blame] | 195 | void EmitReturnStmt(const ReturnStmt &S); |
Chris Lattner | 208ae96 | 2007-05-30 17:57:17 +0000 | [diff] [blame] | 196 | |
| 197 | //===--------------------------------------------------------------------===// |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 198 | // LValue Expression Emission |
| 199 | //===--------------------------------------------------------------------===// |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 200 | |
| 201 | /// EmitLValue - Emit code to compute a designator that specifies the location |
| 202 | /// of the expression. |
| 203 | /// |
| 204 | /// This can return one of two things: a simple address or a bitfield |
| 205 | /// reference. In either case, the LLVM Value* in the LValue structure is |
| 206 | /// guaranteed to be an LLVM pointer type. |
| 207 | /// |
| 208 | /// If this returns a bitfield reference, nothing about the pointee type of |
| 209 | /// the LLVM value is known: For example, it may not be a pointer to an |
| 210 | /// integer. |
| 211 | /// |
| 212 | /// If this returns a normal address, and if the lvalue's C type is fixed |
| 213 | /// size, this method guarantees that the returned pointer type will point to |
| 214 | /// an LLVM type of the same size of the lvalue's type. If the lvalue has a |
| 215 | /// variable length type, this is not possible. |
| 216 | /// |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 217 | LValue EmitLValue(const Expr *E); |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 218 | |
| 219 | /// EmitLoadOfLValue - Given an expression that represents a value lvalue, |
| 220 | /// this method emits the address of the lvalue, then loads the result as an |
| 221 | /// rvalue, returning the rvalue. |
| 222 | RValue EmitLoadOfLValue(const Expr *E); |
| 223 | |
| 224 | /// EmitStoreThroughLValue - Store the specified rvalue into the specified |
| 225 | /// lvalue, where both are guaranteed to the have the same type, and that type |
| 226 | /// is 'Ty'. |
| 227 | void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty); |
| 228 | |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 229 | LValue EmitDeclRefLValue(const DeclRefExpr *E); |
Chris Lattner | 4347e369 | 2007-06-06 04:54:52 +0000 | [diff] [blame] | 230 | LValue EmitStringLiteralLValue(const StringLiteral *E); |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 231 | LValue EmitUnaryOpLValue(const UnaryOperator *E); |
Chris Lattner | d9d2fb1 | 2007-06-08 23:31:14 +0000 | [diff] [blame] | 232 | LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E); |
Chris Lattner | d7f5886 | 2007-06-02 05:24:33 +0000 | [diff] [blame] | 233 | |
| 234 | //===--------------------------------------------------------------------===// |
Chris Lattner | 208ae96 | 2007-05-30 17:57:17 +0000 | [diff] [blame] | 235 | // Expression Emission |
| 236 | //===--------------------------------------------------------------------===// |
| 237 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 238 | RValue EmitExprWithUsualUnaryConversions(const Expr *E, QualType &ResTy); |
Chris Lattner | cf25024 | 2007-06-03 02:02:44 +0000 | [diff] [blame] | 239 | QualType EmitUsualArithmeticConversions(const BinaryOperator *E, |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 240 | RValue &LHS, RValue &RHS); |
Chris Lattner | db91b16 | 2007-06-02 00:16:28 +0000 | [diff] [blame] | 241 | |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 242 | RValue EmitExpr(const Expr *E); |
| 243 | RValue EmitIntegerLiteral(const IntegerLiteral *E); |
| 244 | |
| 245 | RValue EmitCastExpr(const CastExpr *E); |
| 246 | |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 247 | // Unary Operators. |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 248 | RValue EmitUnaryOperator(const UnaryOperator *E); |
| 249 | // FIXME: pre/post inc/dec |
| 250 | RValue EmitUnaryAddrOf (const UnaryOperator *E); |
| 251 | RValue EmitUnaryPlus (const UnaryOperator *E); |
| 252 | RValue EmitUnaryMinus (const UnaryOperator *E); |
| 253 | RValue EmitUnaryNot (const UnaryOperator *E); |
| 254 | RValue EmitUnaryLNot (const UnaryOperator *E); |
| 255 | // FIXME: SIZEOF/ALIGNOF(expr). |
| 256 | // FIXME: real/imag |
Chris Lattner | f0106d2 | 2007-06-02 19:33:17 +0000 | [diff] [blame] | 257 | |
Chris Lattner | db91b16 | 2007-06-02 00:16:28 +0000 | [diff] [blame] | 258 | // Binary Operators. |
Chris Lattner | 8394d79 | 2007-06-05 20:53:16 +0000 | [diff] [blame] | 259 | RValue EmitBinaryOperator(const BinaryOperator *E); |
| 260 | RValue EmitBinaryMul(const BinaryOperator *E); |
| 261 | RValue EmitBinaryDiv(const BinaryOperator *E); |
| 262 | RValue EmitBinaryRem(const BinaryOperator *E); |
| 263 | RValue EmitBinaryAdd(const BinaryOperator *E); |
| 264 | RValue EmitBinarySub(const BinaryOperator *E); |
| 265 | RValue EmitBinaryShl(const BinaryOperator *E); |
| 266 | RValue EmitBinaryShr(const BinaryOperator *E); |
| 267 | |
| 268 | // FIXME: relational |
| 269 | |
| 270 | RValue EmitBinaryAnd(const BinaryOperator *E); |
| 271 | RValue EmitBinaryXor(const BinaryOperator *E); |
| 272 | RValue EmitBinaryOr (const BinaryOperator *E); |
| 273 | RValue EmitBinaryLAnd(const BinaryOperator *E); |
| 274 | RValue EmitBinaryLOr(const BinaryOperator *E); |
| 275 | |
| 276 | RValue EmitBinaryAssign(const BinaryOperator *E); |
| 277 | // FIXME: Assignment. |
| 278 | |
| 279 | RValue EmitBinaryComma(const BinaryOperator *E); |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 280 | }; |
| 281 | } // end namespace CodeGen |
| 282 | } // end namespace clang |
| 283 | } // end namespace llvm |
| 284 | |
| 285 | #endif |