CodeGenFunction.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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/IRBuilder.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 VarDecl;
  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::IRBuilder Builder;
  221. // Holds the Decl for the current function or method
  222. const Decl *CurFuncDecl;
  223. QualType FnRetTy;
  224. llvm::Function *CurFn;
  225. /// AllocaInsertPoint - This is an instruction in the entry block before which
  226. /// we prefer to insert allocas.
  227. llvm::Instruction *AllocaInsertPt;
  228. const llvm::Type *LLVMIntTy;
  229. uint32_t LLVMPointerWidth;
  230. private:
  231. /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
  232. /// decls.
  233. llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
  234. /// LabelMap - This keeps track of the LLVM basic block for each C label.
  235. llvm::DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
  236. // BreakContinueStack - This keeps track of where break and continue
  237. // statements should jump to.
  238. struct BreakContinue {
  239. BreakContinue(llvm::BasicBlock *bb, llvm::BasicBlock *cb)
  240. : BreakBlock(bb), ContinueBlock(cb) {}
  241. llvm::BasicBlock *BreakBlock;
  242. llvm::BasicBlock *ContinueBlock;
  243. };
  244. llvm::SmallVector<BreakContinue, 8> BreakContinueStack;
  245. /// SwitchInsn - This is nearest current switch instruction. It is null if
  246. /// if current context is not in a switch.
  247. llvm::SwitchInst *SwitchInsn;
  248. /// CaseRangeBlock - This block holds if condition check for last case
  249. /// statement range in current switch instruction.
  250. llvm::BasicBlock *CaseRangeBlock;
  251. public:
  252. CodeGenFunction(CodeGenModule &cgm);
  253. ASTContext &getContext() const;
  254. void GenerateObjCMethod(const ObjCMethodDecl *OMD);
  255. void GenerateCode(const FunctionDecl *FD);
  256. const llvm::Type *ConvertType(QualType T);
  257. llvm::Value *LoadObjCSelf();
  258. /// hasAggregateLLVMType - Return true if the specified AST type will map into
  259. /// an aggregate LLVM type or is void.
  260. static bool hasAggregateLLVMType(QualType T);
  261. /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
  262. /// label maps to.
  263. llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
  264. void EmitBlock(llvm::BasicBlock *BB);
  265. /// WarnUnsupported - Print out a warning that codegen doesn't support the
  266. /// specified stmt yet.
  267. void WarnUnsupported(const Stmt *S, const char *Type);
  268. //===--------------------------------------------------------------------===//
  269. // Helpers
  270. //===--------------------------------------------------------------------===//
  271. /// CreateTempAlloca - This creates a alloca and inserts it into the entry
  272. /// block.
  273. llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty,
  274. const char *Name = "tmp");
  275. /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
  276. /// expression and compare the result against zero, returning an Int1Ty value.
  277. llvm::Value *EvaluateExprAsBool(const Expr *E);
  278. /// EmitAnyExpr - Emit code to compute the specified expression which can have
  279. /// any type. The result is returned as an RValue struct. If this is an
  280. /// aggregate expression, the aggloc/agglocvolatile arguments indicate where
  281. /// the result should be returned.
  282. RValue EmitAnyExpr(const Expr *E, llvm::Value *AggLoc = 0,
  283. bool isAggLocVolatile = false);
  284. /// isDummyBlock - Return true if BB is an empty basic block
  285. /// with no predecessors.
  286. static bool isDummyBlock(const llvm::BasicBlock *BB);
  287. /// StartBlock - Start new block named N. If insert block is a dummy block
  288. /// then reuse it.
  289. void StartBlock(const char *N);
  290. /// getCGRecordLayout - Return record layout info.
  291. const CGRecordLayout *getCGRecordLayout(CodeGenTypes &CGT, QualType RTy);
  292. /// GetAddrOfStaticLocalVar - Return the address of a static local variable.
  293. llvm::Constant *GetAddrOfStaticLocalVar(const VarDecl *BVD);
  294. //===--------------------------------------------------------------------===//
  295. // Declaration Emission
  296. //===--------------------------------------------------------------------===//
  297. void EmitDecl(const Decl &D);
  298. void EmitEnumConstantDecl(const EnumConstantDecl &D);
  299. void EmitBlockVarDecl(const VarDecl &D);
  300. void EmitLocalBlockVarDecl(const VarDecl &D);
  301. void EmitStaticBlockVarDecl(const VarDecl &D);
  302. void EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg);
  303. //===--------------------------------------------------------------------===//
  304. // Statement Emission
  305. //===--------------------------------------------------------------------===//
  306. void EmitStmt(const Stmt *S);
  307. RValue EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false,
  308. llvm::Value *AggLoc = 0, bool isAggVol = false);
  309. void EmitLabelStmt(const LabelStmt &S);
  310. void EmitGotoStmt(const GotoStmt &S);
  311. void EmitIfStmt(const IfStmt &S);
  312. void EmitWhileStmt(const WhileStmt &S);
  313. void EmitDoStmt(const DoStmt &S);
  314. void EmitForStmt(const ForStmt &S);
  315. void EmitReturnStmt(const ReturnStmt &S);
  316. void EmitDeclStmt(const DeclStmt &S);
  317. void EmitBreakStmt();
  318. void EmitContinueStmt();
  319. void EmitSwitchStmt(const SwitchStmt &S);
  320. void EmitDefaultStmt(const DefaultStmt &S);
  321. void EmitCaseStmt(const CaseStmt &S);
  322. void EmitCaseStmtRange(const CaseStmt &S);
  323. void EmitAsmStmt(const AsmStmt &S);
  324. //===--------------------------------------------------------------------===//
  325. // LValue Expression Emission
  326. //===--------------------------------------------------------------------===//
  327. /// EmitLValue - Emit code to compute a designator that specifies the location
  328. /// of the expression.
  329. ///
  330. /// This can return one of two things: a simple address or a bitfield
  331. /// reference. In either case, the LLVM Value* in the LValue structure is
  332. /// guaranteed to be an LLVM pointer type.
  333. ///
  334. /// If this returns a bitfield reference, nothing about the pointee type of
  335. /// the LLVM value is known: For example, it may not be a pointer to an
  336. /// integer.
  337. ///
  338. /// If this returns a normal address, and if the lvalue's C type is fixed
  339. /// size, this method guarantees that the returned pointer type will point to
  340. /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
  341. /// variable length type, this is not possible.
  342. ///
  343. LValue EmitLValue(const Expr *E);
  344. /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
  345. /// this method emits the address of the lvalue, then loads the result as an
  346. /// rvalue, returning the rvalue.
  347. RValue EmitLoadOfLValue(LValue V, QualType LVType);
  348. RValue EmitLoadOfOCUElementLValue(LValue V, QualType LVType);
  349. RValue EmitLoadOfBitfieldLValue(LValue LV, QualType ExprType);
  350. /// EmitStoreThroughLValue - Store the specified rvalue into the specified
  351. /// lvalue, where both are guaranteed to the have the same type, and that type
  352. /// is 'Ty'.
  353. void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
  354. void EmitStoreThroughOCUComponentLValue(RValue Src, LValue Dst, QualType Ty);
  355. void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, QualType Ty);
  356. // Note: only availabe for agg return types
  357. LValue EmitCallExprLValue(const CallExpr *E);
  358. LValue EmitDeclRefLValue(const DeclRefExpr *E);
  359. LValue EmitStringLiteralLValue(const StringLiteral *E);
  360. LValue EmitPreDefinedLValue(const PreDefinedExpr *E);
  361. LValue EmitUnaryOpLValue(const UnaryOperator *E);
  362. LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
  363. LValue EmitOCUVectorElementExpr(const OCUVectorElementExpr *E);
  364. LValue EmitMemberExpr(const MemberExpr *E);
  365. LValue EmitLValueForField(llvm::Value* Base, FieldDecl* Field,
  366. bool isUnion);
  367. LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E);
  368. //===--------------------------------------------------------------------===//
  369. // Scalar Expression Emission
  370. //===--------------------------------------------------------------------===//
  371. RValue EmitCallExpr(const CallExpr *E);
  372. RValue EmitCallExpr(Expr *FnExpr, Expr *const *Args, unsigned NumArgs);
  373. RValue EmitCallExpr(llvm::Value *Callee, QualType FnType,
  374. Expr *const *Args, unsigned NumArgs);
  375. RValue EmitBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
  376. llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
  377. llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
  378. llvm::Value *EmitShuffleVector(llvm::Value* V1, llvm::Value *V2, ...);
  379. llvm::Value *EmitVector(llvm::Value * const *Vals, unsigned NumVals,
  380. bool isSplat = false);
  381. llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E);
  382. //===--------------------------------------------------------------------===//
  383. // Expression Emission
  384. //===--------------------------------------------------------------------===//
  385. // Expressions are broken into three classes: scalar, complex, aggregate.
  386. /// EmitScalarExpr - Emit the computation of the specified expression of
  387. /// LLVM scalar type, returning the result.
  388. llvm::Value *EmitScalarExpr(const Expr *E);
  389. /// EmitScalarConversion - Emit a conversion from the specified type to the
  390. /// specified destination type, both of which are LLVM scalar types.
  391. llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy,
  392. QualType DstTy);
  393. /// EmitComplexToScalarConversion - Emit a conversion from the specified
  394. /// complex type to the specified destination type, where the destination
  395. /// type is an LLVM scalar type.
  396. llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy,
  397. QualType DstTy);
  398. /// EmitAggExpr - Emit the computation of the specified expression of
  399. /// aggregate type. The result is computed into DestPtr. Note that if
  400. /// DestPtr is null, the value of the aggregate expression is not needed.
  401. void EmitAggExpr(const Expr *E, llvm::Value *DestPtr, bool VolatileDest);
  402. /// EmitComplexExpr - Emit the computation of the specified expression of
  403. /// complex type, returning the result.
  404. ComplexPairTy EmitComplexExpr(const Expr *E);
  405. /// EmitComplexExprIntoAddr - Emit the computation of the specified expression
  406. /// of complex type, storing into the specified Value*.
  407. void EmitComplexExprIntoAddr(const Expr *E, llvm::Value *DestAddr,
  408. bool DestIsVolatile);
  409. /// LoadComplexFromAddr - Load a complex number from the specified address.
  410. ComplexPairTy LoadComplexFromAddr(llvm::Value *SrcAddr, bool SrcIsVolatile);
  411. };
  412. } // end namespace CodeGen
  413. } // end namespace clang
  414. #endif