CodeGenFunction.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. //===--- CodeGenFunction.h - Per-Function state for LLVM CodeGen ----------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This is the internal per-function state used for llvm translation.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef CLANG_CODEGEN_CODEGENFUNCTION_H
  14. #define CLANG_CODEGEN_CODEGENFUNCTION_H
  15. #include "clang/AST/Type.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. #include "llvm/Support/LLVMBuilder.h"
  19. #include <vector>
  20. namespace llvm {
  21. class Module;
  22. }
  23. namespace clang {
  24. class ASTContext;
  25. class Decl;
  26. class FunctionDecl;
  27. class ObjCMethodDecl;
  28. class TargetInfo;
  29. class FunctionTypeProto;
  30. class Stmt;
  31. class CompoundStmt;
  32. class LabelStmt;
  33. class GotoStmt;
  34. class IfStmt;
  35. class WhileStmt;
  36. class DoStmt;
  37. class ForStmt;
  38. class ReturnStmt;
  39. class DeclStmt;
  40. class CaseStmt;
  41. class DefaultStmt;
  42. class SwitchStmt;
  43. class AsmStmt;
  44. class Expr;
  45. class DeclRefExpr;
  46. class StringLiteral;
  47. class IntegerLiteral;
  48. class FloatingLiteral;
  49. class CharacterLiteral;
  50. class TypesCompatibleExpr;
  51. class ImplicitCastExpr;
  52. class CastExpr;
  53. class CallExpr;
  54. class UnaryOperator;
  55. class BinaryOperator;
  56. class CompoundAssignOperator;
  57. class ArraySubscriptExpr;
  58. class OCUVectorElementExpr;
  59. class ConditionalOperator;
  60. class ChooseExpr;
  61. class PreDefinedExpr;
  62. class ObjCStringLiteral;
  63. class ObjCIvarRefExpr;
  64. class MemberExpr;
  65. class BlockVarDecl;
  66. class EnumConstantDecl;
  67. class ParmVarDecl;
  68. class FieldDecl;
  69. namespace CodeGen {
  70. class CodeGenModule;
  71. class CodeGenTypes;
  72. class CGRecordLayout;
  73. /// RValue - This trivial value class is used to represent the result of an
  74. /// expression that is evaluated. It can be one of three things: either a
  75. /// simple LLVM SSA value, a pair of SSA values for complex numbers, or the
  76. /// address of an aggregate value in memory.
  77. class RValue {
  78. llvm::Value *V1, *V2;
  79. // TODO: Encode this into the low bit of pointer for more efficient
  80. // return-by-value.
  81. enum { Scalar, Complex, Aggregate } Flavor;
  82. // FIXME: Aggregate rvalues need to retain information about whether they are
  83. // volatile or not.
  84. public:
  85. bool isScalar() const { return Flavor == Scalar; }
  86. bool isComplex() const { return Flavor == Complex; }
  87. bool isAggregate() const { return Flavor == Aggregate; }
  88. /// getScalar() - Return the Value* of this scalar value.
  89. llvm::Value *getScalarVal() const {
  90. assert(isScalar() && "Not a scalar!");
  91. return V1;
  92. }
  93. /// getComplexVal - Return the real/imag components of this complex value.
  94. ///
  95. std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
  96. return std::pair<llvm::Value *, llvm::Value *>(V1, V2);
  97. }
  98. /// getAggregateAddr() - Return the Value* of the address of the aggregate.
  99. llvm::Value *getAggregateAddr() const {
  100. assert(isAggregate() && "Not an aggregate!");
  101. return V1;
  102. }
  103. static RValue get(llvm::Value *V) {
  104. RValue ER;
  105. ER.V1 = V;
  106. ER.Flavor = Scalar;
  107. return ER;
  108. }
  109. static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
  110. RValue ER;
  111. ER.V1 = V1;
  112. ER.V2 = V2;
  113. ER.Flavor = Complex;
  114. return ER;
  115. }
  116. static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
  117. RValue ER;
  118. ER.V1 = C.first;
  119. ER.V2 = C.second;
  120. ER.Flavor = Complex;
  121. return ER;
  122. }
  123. static RValue getAggregate(llvm::Value *V) {
  124. RValue ER;
  125. ER.V1 = V;
  126. ER.Flavor = Aggregate;
  127. return ER;
  128. }
  129. };
  130. /// LValue - This represents an lvalue references. Because C/C++ allow
  131. /// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
  132. /// bitrange.
  133. class LValue {
  134. // FIXME: Volatility. Restrict?
  135. // alignment?
  136. enum {
  137. Simple, // This is a normal l-value, use getAddress().
  138. VectorElt, // This is a vector element l-value (V[i]), use getVector*
  139. BitField, // This is a bitfield l-value, use getBitfield*.
  140. OCUVectorElt // This is an ocu vector subset, use getOCUVectorComp
  141. } LVType;
  142. llvm::Value *V;
  143. union {
  144. llvm::Value *VectorIdx; // Index into a vector subscript: V[i]
  145. unsigned VectorElts; // Encoded OCUVector element subset: V.xyx
  146. struct {
  147. unsigned short StartBit;
  148. unsigned short Size;
  149. bool IsSigned;
  150. } BitfieldData; // BitField start bit and size
  151. };
  152. public:
  153. bool isSimple() const { return LVType == Simple; }
  154. bool isVectorElt() const { return LVType == VectorElt; }
  155. bool isBitfield() const { return LVType == BitField; }
  156. bool isOCUVectorElt() const { return LVType == OCUVectorElt; }
  157. // simple lvalue
  158. llvm::Value *getAddress() const { assert(isSimple()); return V; }
  159. // vector elt lvalue
  160. llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
  161. llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
  162. // ocu vector elements.
  163. llvm::Value *getOCUVectorAddr() const { assert(isOCUVectorElt()); return V; }
  164. unsigned getOCUVectorElts() const {
  165. assert(isOCUVectorElt());
  166. return VectorElts;
  167. }
  168. // bitfield lvalue
  169. llvm::Value *getBitfieldAddr() const { assert(isBitfield()); return V; }
  170. unsigned short getBitfieldStartBit() const {
  171. assert(isBitfield());
  172. return BitfieldData.StartBit;
  173. }
  174. unsigned short getBitfieldSize() const {
  175. assert(isBitfield());
  176. return BitfieldData.Size;
  177. }
  178. bool isBitfieldSigned() const {
  179. assert(isBitfield());
  180. return BitfieldData.IsSigned;
  181. }
  182. static LValue MakeAddr(llvm::Value *V) {
  183. LValue R;
  184. R.LVType = Simple;
  185. R.V = V;
  186. return R;
  187. }
  188. static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx) {
  189. LValue R;
  190. R.LVType = VectorElt;
  191. R.V = Vec;
  192. R.VectorIdx = Idx;
  193. return R;
  194. }
  195. static LValue MakeOCUVectorElt(llvm::Value *Vec, unsigned Elements) {
  196. LValue R;
  197. R.LVType = OCUVectorElt;
  198. R.V = Vec;
  199. R.VectorElts = Elements;
  200. return R;
  201. }
  202. static LValue MakeBitfield(llvm::Value *V, unsigned short StartBit,
  203. unsigned short Size, bool IsSigned) {
  204. LValue R;
  205. R.LVType = BitField;
  206. R.V = V;
  207. R.BitfieldData.StartBit = StartBit;
  208. R.BitfieldData.Size = Size;
  209. R.BitfieldData.IsSigned = IsSigned;
  210. return R;
  211. }
  212. };
  213. /// CodeGenFunction - This class organizes the per-function state that is used
  214. /// while generating LLVM code.
  215. class CodeGenFunction {
  216. public:
  217. CodeGenModule &CGM; // Per-module state.
  218. TargetInfo &Target;
  219. typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
  220. llvm::LLVMFoldingBuilder Builder;
  221. const FunctionDecl *CurFuncDecl;
  222. QualType FnRetTy;
  223. llvm::Function *CurFn;
  224. /// AllocaInsertPoint - This is an instruction in the entry block before which
  225. /// we prefer to insert allocas.
  226. llvm::Instruction *AllocaInsertPt;
  227. const llvm::Type *LLVMIntTy;
  228. uint32_t LLVMPointerWidth;
  229. private:
  230. /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
  231. /// decls.
  232. llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
  233. /// LabelMap - This keeps track of the LLVM basic block for each C label.
  234. llvm::DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
  235. // BreakContinueStack - This keeps track of where break and continue
  236. // statements should jump to.
  237. struct BreakContinue {
  238. BreakContinue(llvm::BasicBlock *bb, llvm::BasicBlock *cb)
  239. : BreakBlock(bb), ContinueBlock(cb) {}
  240. llvm::BasicBlock *BreakBlock;
  241. llvm::BasicBlock *ContinueBlock;
  242. };
  243. llvm::SmallVector<BreakContinue, 8> BreakContinueStack;
  244. /// SwitchInsn - This is nearest current switch instruction. It is null if
  245. /// if current context is not in a switch.
  246. llvm::SwitchInst *SwitchInsn;
  247. /// CaseRangeBlock - This block holds if condition check for last case
  248. /// statement range in current switch instruction.
  249. llvm::BasicBlock *CaseRangeBlock;
  250. public:
  251. CodeGenFunction(CodeGenModule &cgm);
  252. ASTContext &getContext() const;
  253. void GenerateObjCMethod(const ObjCMethodDecl *OMD);
  254. void GenerateCode(const FunctionDecl *FD);
  255. const llvm::Type *ConvertType(QualType T);
  256. /// hasAggregateLLVMType - Return true if the specified AST type will map into
  257. /// an aggregate LLVM type or is void.
  258. static bool hasAggregateLLVMType(QualType T);
  259. /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
  260. /// label maps to.
  261. llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
  262. void EmitBlock(llvm::BasicBlock *BB);
  263. /// WarnUnsupported - Print out a warning that codegen doesn't support the
  264. /// specified stmt yet.
  265. void WarnUnsupported(const Stmt *S, const char *Type);
  266. //===--------------------------------------------------------------------===//
  267. // Helpers
  268. //===--------------------------------------------------------------------===//
  269. /// CreateTempAlloca - This creates a alloca and inserts it into the entry
  270. /// block.
  271. llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty,
  272. const char *Name = "tmp");
  273. /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
  274. /// expression and compare the result against zero, returning an Int1Ty value.
  275. llvm::Value *EvaluateExprAsBool(const Expr *E);
  276. /// EmitAnyExpr - Emit code to compute the specified expression which can have
  277. /// any type. The result is returned as an RValue struct. If this is an
  278. /// aggregate expression, the aggloc/agglocvolatile arguments indicate where
  279. /// the result should be returned.
  280. RValue EmitAnyExpr(const Expr *E, llvm::Value *AggLoc = 0,
  281. bool isAggLocVolatile = false);
  282. /// isDummyBlock - Return true if BB is an empty basic block
  283. /// with no predecessors.
  284. static bool isDummyBlock(const llvm::BasicBlock *BB);
  285. /// StartBlock - Start new block named N. If insert block is a dummy block
  286. /// then reuse it.
  287. void StartBlock(const char *N);
  288. /// getCGRecordLayout - Return record layout info.
  289. const CGRecordLayout *getCGRecordLayout(CodeGenTypes &CGT, QualType RTy);
  290. /// GetAddrOfStaticLocalVar - Return the address of a static local variable.
  291. llvm::Constant *GetAddrOfStaticLocalVar(const BlockVarDecl *BVD);
  292. //===--------------------------------------------------------------------===//
  293. // Declaration Emission
  294. //===--------------------------------------------------------------------===//
  295. void EmitDecl(const Decl &D);
  296. void EmitEnumConstantDecl(const EnumConstantDecl &D);
  297. void EmitBlockVarDecl(const BlockVarDecl &D);
  298. void EmitLocalBlockVarDecl(const BlockVarDecl &D);
  299. void EmitStaticBlockVarDecl(const BlockVarDecl &D);
  300. void EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg);
  301. //===--------------------------------------------------------------------===//
  302. // Statement Emission
  303. //===--------------------------------------------------------------------===//
  304. void EmitStmt(const Stmt *S);
  305. RValue EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false,
  306. llvm::Value *AggLoc = 0, bool isAggVol = false);
  307. void EmitLabelStmt(const LabelStmt &S);
  308. void EmitGotoStmt(const GotoStmt &S);
  309. void EmitIfStmt(const IfStmt &S);
  310. void EmitWhileStmt(const WhileStmt &S);
  311. void EmitDoStmt(const DoStmt &S);
  312. void EmitForStmt(const ForStmt &S);
  313. void EmitReturnStmt(const ReturnStmt &S);
  314. void EmitDeclStmt(const DeclStmt &S);
  315. void EmitBreakStmt();
  316. void EmitContinueStmt();
  317. void EmitSwitchStmt(const SwitchStmt &S);
  318. void EmitDefaultStmt(const DefaultStmt &S);
  319. void EmitCaseStmt(const CaseStmt &S);
  320. void EmitCaseStmtRange(const CaseStmt &S);
  321. void EmitAsmStmt(const AsmStmt &S);
  322. //===--------------------------------------------------------------------===//
  323. // LValue Expression Emission
  324. //===--------------------------------------------------------------------===//
  325. /// EmitLValue - Emit code to compute a designator that specifies the location
  326. /// of the expression.
  327. ///
  328. /// This can return one of two things: a simple address or a bitfield
  329. /// reference. In either case, the LLVM Value* in the LValue structure is
  330. /// guaranteed to be an LLVM pointer type.
  331. ///
  332. /// If this returns a bitfield reference, nothing about the pointee type of
  333. /// the LLVM value is known: For example, it may not be a pointer to an
  334. /// integer.
  335. ///
  336. /// If this returns a normal address, and if the lvalue's C type is fixed
  337. /// size, this method guarantees that the returned pointer type will point to
  338. /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
  339. /// variable length type, this is not possible.
  340. ///
  341. LValue EmitLValue(const Expr *E);
  342. /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
  343. /// this method emits the address of the lvalue, then loads the result as an
  344. /// rvalue, returning the rvalue.
  345. RValue EmitLoadOfLValue(LValue V, QualType LVType);
  346. RValue EmitLoadOfOCUElementLValue(LValue V, QualType LVType);
  347. RValue EmitLoadOfBitfieldLValue(LValue LV, QualType ExprType);
  348. /// EmitStoreThroughLValue - Store the specified rvalue into the specified
  349. /// lvalue, where both are guaranteed to the have the same type, and that type
  350. /// is 'Ty'.
  351. void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
  352. void EmitStoreThroughOCUComponentLValue(RValue Src, LValue Dst, QualType Ty);
  353. void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, QualType Ty);
  354. // Note: only availabe for agg return types
  355. LValue EmitCallExprLValue(const CallExpr *E);
  356. LValue EmitDeclRefLValue(const DeclRefExpr *E);
  357. LValue EmitStringLiteralLValue(const StringLiteral *E);
  358. LValue EmitPreDefinedLValue(const PreDefinedExpr *E);
  359. LValue EmitUnaryOpLValue(const UnaryOperator *E);
  360. LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
  361. LValue EmitOCUVectorElementExpr(const OCUVectorElementExpr *E);
  362. LValue EmitMemberExpr(const MemberExpr *E);
  363. LValue EmitLValueForField(llvm::Value* Base, FieldDecl* Field,
  364. bool isUnion);
  365. LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E);
  366. //===--------------------------------------------------------------------===//
  367. // Scalar Expression Emission
  368. //===--------------------------------------------------------------------===//
  369. RValue EmitCallExpr(const CallExpr *E);
  370. RValue EmitCallExpr(Expr *FnExpr, Expr *const *Args, unsigned NumArgs);
  371. RValue EmitCallExpr(llvm::Value *Callee, QualType FnType,
  372. Expr *const *Args, unsigned NumArgs);
  373. RValue EmitBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
  374. llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
  375. llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
  376. llvm::Value *EmitShuffleVector(llvm::Value* V1, llvm::Value *V2, ...);
  377. llvm::Value *EmitVector(llvm::Value * const *Vals, unsigned NumVals,
  378. bool isSplat = false);
  379. llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E);
  380. //===--------------------------------------------------------------------===//
  381. // Expression Emission
  382. //===--------------------------------------------------------------------===//
  383. // Expressions are broken into three classes: scalar, complex, aggregate.
  384. /// EmitScalarExpr - Emit the computation of the specified expression of
  385. /// LLVM scalar type, returning the result.
  386. llvm::Value *EmitScalarExpr(const Expr *E);
  387. /// EmitScalarConversion - Emit a conversion from the specified type to the
  388. /// specified destination type, both of which are LLVM scalar types.
  389. llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy,
  390. QualType DstTy);
  391. /// EmitComplexToScalarConversion - Emit a conversion from the specified
  392. /// complex type to the specified destination type, where the destination
  393. /// type is an LLVM scalar type.
  394. llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy,
  395. QualType DstTy);
  396. /// EmitAggExpr - Emit the computation of the specified expression of
  397. /// aggregate type. The result is computed into DestPtr. Note that if
  398. /// DestPtr is null, the value of the aggregate expression is not needed.
  399. void EmitAggExpr(const Expr *E, llvm::Value *DestPtr, bool VolatileDest);
  400. /// EmitComplexExpr - Emit the computation of the specified expression of
  401. /// complex type, returning the result.
  402. ComplexPairTy EmitComplexExpr(const Expr *E);
  403. /// EmitComplexExprIntoAddr - Emit the computation of the specified expression
  404. /// of complex type, storing into the specified Value*.
  405. void EmitComplexExprIntoAddr(const Expr *E, llvm::Value *DestAddr,
  406. bool DestIsVolatile);
  407. /// LoadComplexFromAddr - Load a complex number from the specified address.
  408. ComplexPairTy LoadComplexFromAddr(llvm::Value *SrcAddr, bool SrcIsVolatile);
  409. };
  410. } // end namespace CodeGen
  411. } // end namespace clang
  412. #endif