Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | |
| 17 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | #include "llvm/Support/LLVMBuilder.h" |
| 20 | #include <vector> |
| 21 | |
| 22 | namespace llvm { |
| 23 | class Module; |
| 24 | } |
| 25 | |
| 26 | namespace clang { |
| 27 | class ASTContext; |
| 28 | class Decl; |
| 29 | class FunctionDecl; |
| 30 | class TargetInfo; |
| 31 | class QualType; |
| 32 | class FunctionTypeProto; |
| 33 | |
| 34 | class Stmt; |
| 35 | class CompoundStmt; |
| 36 | class LabelStmt; |
| 37 | class GotoStmt; |
| 38 | class IfStmt; |
| 39 | class WhileStmt; |
| 40 | class DoStmt; |
| 41 | class ForStmt; |
| 42 | class ReturnStmt; |
| 43 | class DeclStmt; |
| 44 | |
| 45 | class Expr; |
| 46 | class DeclRefExpr; |
| 47 | class StringLiteral; |
| 48 | class IntegerLiteral; |
| 49 | class FloatingLiteral; |
Chris Lattner | b0a721a | 2007-07-13 05:18:11 +0000 | [diff] [blame] | 50 | class CharacterLiteral; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 51 | class CastExpr; |
| 52 | class CallExpr; |
| 53 | class UnaryOperator; |
| 54 | class BinaryOperator; |
| 55 | class CompoundAssignOperator; |
| 56 | class ArraySubscriptExpr; |
Chris Lattner | b0a721a | 2007-07-13 05:18:11 +0000 | [diff] [blame] | 57 | class ConditionalOperator; |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 58 | class PreDefinedExpr; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 59 | |
| 60 | class BlockVarDecl; |
| 61 | class EnumConstantDecl; |
| 62 | class ParmVarDecl; |
| 63 | namespace CodeGen { |
| 64 | class CodeGenModule; |
| 65 | |
| 66 | |
| 67 | /// RValue - This trivial value class is used to represent the result of an |
| 68 | /// expression that is evaluated. It can be one of two things: either a simple |
| 69 | /// LLVM SSA value, or the address of an aggregate value in memory. These two |
| 70 | /// possibilities are discriminated by isAggregate/isScalar. |
| 71 | class RValue { |
| 72 | llvm::Value *V; |
| 73 | // TODO: Encode this into the low bit of pointer for more efficient |
| 74 | // return-by-value. |
| 75 | bool IsAggregate; |
| 76 | |
| 77 | // FIXME: Aggregate rvalues need to retain information about whether they are |
| 78 | // volatile or not. |
| 79 | public: |
| 80 | |
| 81 | bool isAggregate() const { return IsAggregate; } |
| 82 | bool isScalar() const { return !IsAggregate; } |
| 83 | |
| 84 | /// getVal() - Return the Value* of this scalar value. |
| 85 | llvm::Value *getVal() const { |
| 86 | assert(!isAggregate() && "Not a scalar!"); |
| 87 | return V; |
| 88 | } |
| 89 | |
| 90 | /// getAggregateAddr() - Return the Value* of the address of the aggregate. |
| 91 | llvm::Value *getAggregateAddr() const { |
| 92 | assert(isAggregate() && "Not an aggregate!"); |
| 93 | return V; |
| 94 | } |
| 95 | |
| 96 | static RValue get(llvm::Value *V) { |
| 97 | RValue ER; |
| 98 | ER.V = V; |
| 99 | ER.IsAggregate = false; |
| 100 | return ER; |
| 101 | } |
| 102 | static RValue getAggregate(llvm::Value *V) { |
| 103 | RValue ER; |
| 104 | ER.V = V; |
| 105 | ER.IsAggregate = true; |
| 106 | return ER; |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | |
| 111 | /// LValue - This represents an lvalue references. Because C/C++ allow |
| 112 | /// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a |
| 113 | /// bitrange. |
| 114 | class LValue { |
| 115 | // FIXME: Volatility. Restrict? |
| 116 | // alignment? |
| 117 | |
| 118 | enum { |
| 119 | Simple, // This is a normal l-value, use getAddress(). |
| 120 | VectorElt, // This is a vector element l-value (V[i]), use getVector* |
| 121 | BitField // This is a bitfield l-value, use getBitfield*. |
| 122 | } LVType; |
| 123 | |
| 124 | llvm::Value *V; |
| 125 | |
| 126 | union { |
| 127 | llvm::Value *VectorIdx; |
| 128 | }; |
| 129 | public: |
| 130 | bool isSimple() const { return LVType == Simple; } |
| 131 | bool isVectorElt() const { return LVType == VectorElt; } |
| 132 | bool isBitfield() const { return LVType == BitField; } |
| 133 | |
| 134 | // simple lvalue |
| 135 | llvm::Value *getAddress() const { assert(isSimple()); return V; } |
| 136 | // vector elt lvalue |
| 137 | llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; } |
| 138 | llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; } |
| 139 | |
| 140 | static LValue MakeAddr(llvm::Value *V) { |
| 141 | LValue R; |
| 142 | R.LVType = Simple; |
| 143 | R.V = V; |
| 144 | return R; |
| 145 | } |
| 146 | |
| 147 | static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx) { |
| 148 | LValue R; |
| 149 | R.LVType = VectorElt; |
| 150 | R.V = Vec; |
| 151 | R.VectorIdx = Idx; |
| 152 | return R; |
| 153 | } |
| 154 | |
| 155 | }; |
| 156 | |
| 157 | /// CodeGenFunction - This class organizes the per-function state that is used |
| 158 | /// while generating LLVM code. |
| 159 | class CodeGenFunction { |
| 160 | CodeGenModule &CGM; // Per-module state. |
| 161 | TargetInfo &Target; |
| 162 | llvm::LLVMBuilder Builder; |
| 163 | |
| 164 | const FunctionDecl *CurFuncDecl; |
| 165 | llvm::Function *CurFn; |
| 166 | |
| 167 | /// AllocaInsertPoint - This is an instruction in the entry block before which |
| 168 | /// we prefer to insert allocas. |
| 169 | llvm::Instruction *AllocaInsertPt; |
| 170 | |
| 171 | const llvm::Type *LLVMIntTy; |
| 172 | unsigned LLVMPointerWidth; |
| 173 | |
| 174 | /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C |
| 175 | /// decls. |
| 176 | llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap; |
| 177 | |
| 178 | /// LabelMap - This keeps track of the LLVM basic block for each C label. |
| 179 | llvm::DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap; |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 180 | |
| 181 | // BreakContinueStack - This keeps track of where break and continue |
| 182 | // statements should jump to. |
| 183 | struct BreakContinue { |
| 184 | BreakContinue(llvm::BasicBlock *bb, llvm::BasicBlock *cb) |
| 185 | : BreakBlock(bb), ContinueBlock(cb) {} |
| 186 | |
| 187 | llvm::BasicBlock *BreakBlock; |
| 188 | llvm::BasicBlock *ContinueBlock; |
| 189 | }; |
| 190 | llvm::SmallVector<BreakContinue, 8> BreakContinueStack; |
| 191 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 192 | public: |
| 193 | CodeGenFunction(CodeGenModule &cgm); |
| 194 | |
| 195 | ASTContext &getContext() const; |
| 196 | |
| 197 | void GenerateCode(const FunctionDecl *FD); |
| 198 | |
| 199 | const llvm::Type *ConvertType(QualType T); |
| 200 | |
| 201 | /// hasAggregateLLVMType - Return true if the specified AST type will map into |
| 202 | /// an aggregate LLVM type or is void. |
| 203 | static bool hasAggregateLLVMType(QualType T); |
| 204 | |
| 205 | /// getBasicBlockForLabel - Return the LLVM basicblock that the specified |
| 206 | /// label maps to. |
| 207 | llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S); |
| 208 | |
| 209 | |
| 210 | void EmitBlock(llvm::BasicBlock *BB); |
| 211 | |
| 212 | //===--------------------------------------------------------------------===// |
| 213 | // Helpers |
| 214 | //===--------------------------------------------------------------------===// |
| 215 | |
| 216 | /// CreateTempAlloca - This creates a alloca and inserts it into the entry |
| 217 | /// block. |
| 218 | llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty, |
| 219 | const char *Name = "tmp"); |
| 220 | |
| 221 | /// EvaluateExprAsBool - Perform the usual unary conversions on the specified |
| 222 | /// expression and compare the result against zero, returning an Int1Ty value. |
| 223 | llvm::Value *EvaluateExprAsBool(const Expr *E); |
| 224 | |
| 225 | |
| 226 | /// EmitLoadOfComplex - Given an RValue reference for a complex, emit code to |
| 227 | /// load the real and imaginary pieces, returning them as Real/Imag. |
| 228 | void EmitLoadOfComplex(RValue V, llvm::Value *&Real, llvm::Value *&Imag); |
| 229 | |
| 230 | /// EmitStoreOfComplex - Store the specified real/imag parts into the |
| 231 | /// specified value pointer. |
| 232 | void EmitStoreOfComplex(llvm::Value *Real, llvm::Value *Imag, |
| 233 | llvm::Value *ResPtr); |
| 234 | |
| 235 | //===--------------------------------------------------------------------===// |
| 236 | // Conversions |
| 237 | //===--------------------------------------------------------------------===// |
| 238 | |
| 239 | /// EmitConversion - Convert the value specied by Val, whose type is ValTy, to |
| 240 | /// the type specified by DstTy, following the rules of C99 6.3. |
| 241 | RValue EmitConversion(RValue Val, QualType ValTy, QualType DstTy); |
| 242 | |
| 243 | /// ConvertScalarValueToBool - Convert the specified expression value to a |
| 244 | /// boolean (i1) truth value. This is equivalent to "Val == 0". |
| 245 | llvm::Value *ConvertScalarValueToBool(RValue Val, QualType Ty); |
| 246 | |
| 247 | //===--------------------------------------------------------------------===// |
| 248 | // Declaration Emission |
| 249 | //===--------------------------------------------------------------------===// |
| 250 | |
| 251 | void EmitDecl(const Decl &D); |
| 252 | void EmitEnumConstantDecl(const EnumConstantDecl &D); |
| 253 | void EmitBlockVarDecl(const BlockVarDecl &D); |
| 254 | void EmitLocalBlockVarDecl(const BlockVarDecl &D); |
| 255 | void EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg); |
| 256 | |
| 257 | //===--------------------------------------------------------------------===// |
| 258 | // Statement Emission |
| 259 | //===--------------------------------------------------------------------===// |
| 260 | |
| 261 | void EmitStmt(const Stmt *S); |
| 262 | void EmitCompoundStmt(const CompoundStmt &S); |
| 263 | void EmitLabelStmt(const LabelStmt &S); |
| 264 | void EmitGotoStmt(const GotoStmt &S); |
| 265 | void EmitIfStmt(const IfStmt &S); |
| 266 | void EmitWhileStmt(const WhileStmt &S); |
| 267 | void EmitDoStmt(const DoStmt &S); |
| 268 | void EmitForStmt(const ForStmt &S); |
| 269 | void EmitReturnStmt(const ReturnStmt &S); |
| 270 | void EmitDeclStmt(const DeclStmt &S); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 271 | void EmitBreakStmt(); |
| 272 | void EmitContinueStmt(); |
| 273 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 274 | //===--------------------------------------------------------------------===// |
| 275 | // LValue Expression Emission |
| 276 | //===--------------------------------------------------------------------===// |
| 277 | |
| 278 | /// EmitLValue - Emit code to compute a designator that specifies the location |
| 279 | /// of the expression. |
| 280 | /// |
| 281 | /// This can return one of two things: a simple address or a bitfield |
| 282 | /// reference. In either case, the LLVM Value* in the LValue structure is |
| 283 | /// guaranteed to be an LLVM pointer type. |
| 284 | /// |
| 285 | /// If this returns a bitfield reference, nothing about the pointee type of |
| 286 | /// the LLVM value is known: For example, it may not be a pointer to an |
| 287 | /// integer. |
| 288 | /// |
| 289 | /// If this returns a normal address, and if the lvalue's C type is fixed |
| 290 | /// size, this method guarantees that the returned pointer type will point to |
| 291 | /// an LLVM type of the same size of the lvalue's type. If the lvalue has a |
| 292 | /// variable length type, this is not possible. |
| 293 | /// |
| 294 | LValue EmitLValue(const Expr *E); |
| 295 | |
| 296 | /// EmitLoadOfLValue - Given an expression that represents a value lvalue, |
| 297 | /// this method emits the address of the lvalue, then loads the result as an |
| 298 | /// rvalue, returning the rvalue. |
| 299 | RValue EmitLoadOfLValue(const Expr *E); |
| 300 | RValue EmitLoadOfLValue(LValue V, QualType LVType); |
| 301 | |
| 302 | /// EmitStoreThroughLValue - Store the specified rvalue into the specified |
| 303 | /// lvalue, where both are guaranteed to the have the same type, and that type |
| 304 | /// is 'Ty'. |
| 305 | void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty); |
| 306 | |
| 307 | LValue EmitDeclRefLValue(const DeclRefExpr *E); |
| 308 | LValue EmitStringLiteralLValue(const StringLiteral *E); |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 309 | LValue EmitPreDefinedLValue(const PreDefinedExpr *E); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 310 | LValue EmitUnaryOpLValue(const UnaryOperator *E); |
| 311 | LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E); |
| 312 | |
| 313 | //===--------------------------------------------------------------------===// |
| 314 | // Expression Emission |
| 315 | //===--------------------------------------------------------------------===// |
| 316 | |
| 317 | RValue EmitExprWithUsualUnaryConversions(const Expr *E, QualType &ResTy); |
| 318 | QualType EmitUsualArithmeticConversions(const BinaryOperator *E, |
| 319 | RValue &LHS, RValue &RHS); |
| 320 | void EmitShiftOperands(const BinaryOperator *E, RValue &LHS, RValue &RHS); |
| 321 | |
| 322 | void EmitCompoundAssignmentOperands(const CompoundAssignOperator *CAO, |
| 323 | LValue &LHSLV, RValue &LHS, RValue &RHS); |
| 324 | RValue EmitCompoundAssignmentResult(const CompoundAssignOperator *E, |
| 325 | LValue LHSLV, RValue ResV); |
| 326 | |
| 327 | |
| 328 | RValue EmitExpr(const Expr *E); |
| 329 | RValue EmitIntegerLiteral(const IntegerLiteral *E); |
| 330 | RValue EmitFloatingLiteral(const FloatingLiteral *E); |
Chris Lattner | b0a721a | 2007-07-13 05:18:11 +0000 | [diff] [blame] | 331 | RValue EmitCharacterLiteral(const CharacterLiteral *E); |
| 332 | |
Chris Lattner | d07eb3b | 2007-07-13 20:25:53 +0000 | [diff] [blame] | 333 | RValue EmitCastExpr(const Expr *Op, QualType DestTy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 334 | RValue EmitCallExpr(const CallExpr *E); |
| 335 | RValue EmitArraySubscriptExprRV(const ArraySubscriptExpr *E); |
| 336 | |
| 337 | // Unary Operators. |
| 338 | RValue EmitUnaryOperator(const UnaryOperator *E); |
Chris Lattner | 5727479 | 2007-07-11 23:43:46 +0000 | [diff] [blame] | 339 | RValue EmitUnaryIncDec (const UnaryOperator *E); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 340 | RValue EmitUnaryAddrOf (const UnaryOperator *E); |
| 341 | RValue EmitUnaryPlus (const UnaryOperator *E); |
| 342 | RValue EmitUnaryMinus (const UnaryOperator *E); |
| 343 | RValue EmitUnaryNot (const UnaryOperator *E); |
| 344 | RValue EmitUnaryLNot (const UnaryOperator *E); |
Chris Lattner | 5e3fbe5 | 2007-07-18 18:12:07 +0000 | [diff] [blame] | 345 | RValue EmitSizeAlignOf (QualType TypeToSize, QualType RetType,bool isSizeOf); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 346 | // FIXME: real/imag |
| 347 | |
| 348 | // Binary Operators. |
| 349 | RValue EmitBinaryOperator(const BinaryOperator *E); |
| 350 | RValue EmitBinaryMul(const BinaryOperator *E); |
| 351 | RValue EmitBinaryDiv(const BinaryOperator *E); |
| 352 | RValue EmitBinaryRem(const BinaryOperator *E); |
| 353 | RValue EmitMul(RValue LHS, RValue RHS, QualType EltTy); |
| 354 | RValue EmitDiv(RValue LHS, RValue RHS, QualType EltTy); |
| 355 | RValue EmitRem(RValue LHS, RValue RHS, QualType EltTy); |
| 356 | RValue EmitAdd(RValue LHS, RValue RHS, QualType EltTy); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 357 | RValue EmitPointerAdd(RValue LHS, QualType LHSTy, |
| 358 | RValue RHS, QualType RHSTy, QualType EltTy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 359 | RValue EmitSub(RValue LHS, RValue RHS, QualType EltTy); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 360 | RValue EmitPointerSub(RValue LHS, QualType LHSTy, |
| 361 | RValue RHS, QualType RHSTy, QualType EltTy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 362 | RValue EmitShl(RValue LHS, RValue RHS, QualType ResTy); |
| 363 | RValue EmitShr(RValue LHS, RValue RHS, QualType ResTy); |
| 364 | RValue EmitBinaryCompare(const BinaryOperator *E, unsigned UICmpOpc, |
| 365 | unsigned SICmpOpc, unsigned FCmpOpc); |
| 366 | RValue EmitAnd(RValue LHS, RValue RHS, QualType EltTy); |
| 367 | RValue EmitOr (RValue LHS, RValue RHS, QualType EltTy); |
| 368 | RValue EmitXor(RValue LHS, RValue RHS, QualType EltTy); |
| 369 | RValue EmitBinaryLAnd(const BinaryOperator *E); |
| 370 | RValue EmitBinaryLOr(const BinaryOperator *E); |
| 371 | |
| 372 | RValue EmitBinaryAssign(const BinaryOperator *E); |
| 373 | RValue EmitBinaryComma(const BinaryOperator *E); |
Chris Lattner | b0a721a | 2007-07-13 05:18:11 +0000 | [diff] [blame] | 374 | |
| 375 | // Conditional Operator. |
| 376 | RValue EmitConditionalOperator(const ConditionalOperator *E); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 377 | }; |
| 378 | } // end namespace CodeGen |
| 379 | } // end namespace clang |
| 380 | |
| 381 | #endif |