CodeGenFunction.h 12 KB

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