CGStmt.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //===--- CGStmt.cpp - Emit LLVM Code from Statements ----------------------===//
  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 contains code to emit Stmt nodes as LLVM code.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "CodeGenFunction.h"
  14. #include "clang/AST/AST.h"
  15. #include "llvm/Constants.h"
  16. #include "llvm/DerivedTypes.h"
  17. #include "llvm/Function.h"
  18. using namespace clang;
  19. using namespace CodeGen;
  20. //===----------------------------------------------------------------------===//
  21. // Statement Emission
  22. //===----------------------------------------------------------------------===//
  23. void CodeGenFunction::EmitStmt(const Stmt *S) {
  24. assert(S && "Null statement?");
  25. switch (S->getStmtClass()) {
  26. default:
  27. // Must be an expression in a stmt context. Emit the value and ignore the
  28. // result.
  29. if (const Expr *E = dyn_cast<Expr>(S)) {
  30. EmitExpr(E);
  31. } else {
  32. printf("Unimplemented stmt!\n");
  33. S->dump();
  34. }
  35. break;
  36. case Stmt::NullStmtClass: break;
  37. case Stmt::CompoundStmtClass: EmitCompoundStmt(cast<CompoundStmt>(*S)); break;
  38. case Stmt::LabelStmtClass: EmitLabelStmt(cast<LabelStmt>(*S)); break;
  39. case Stmt::GotoStmtClass: EmitGotoStmt(cast<GotoStmt>(*S)); break;
  40. case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break;
  41. case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S)); break;
  42. case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S)); break;
  43. case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S)); break;
  44. case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break;
  45. case Stmt::DeclStmtClass: EmitDeclStmt(cast<DeclStmt>(*S)); break;
  46. }
  47. }
  48. void CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S) {
  49. // FIXME: handle vla's etc.
  50. for (CompoundStmt::const_body_iterator I = S.body_begin(), E = S.body_end();
  51. I != E; ++I)
  52. EmitStmt(*I);
  53. }
  54. void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB) {
  55. // Emit a branch from this block to the next one if this was a real block. If
  56. // this was just a fall-through block after a terminator, don't emit it.
  57. llvm::BasicBlock *LastBB = Builder.GetInsertBlock();
  58. if (LastBB->getTerminator()) {
  59. // If the previous block is already terminated, don't touch it.
  60. } else if (LastBB->empty() && LastBB->getValueName() == 0) {
  61. // If the last block was an empty placeholder, remove it now.
  62. // TODO: cache and reuse these.
  63. Builder.GetInsertBlock()->eraseFromParent();
  64. } else {
  65. // Otherwise, create a fall-through branch.
  66. Builder.CreateBr(BB);
  67. }
  68. CurFn->getBasicBlockList().push_back(BB);
  69. Builder.SetInsertPoint(BB);
  70. }
  71. void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) {
  72. llvm::BasicBlock *NextBB = getBasicBlockForLabel(&S);
  73. EmitBlock(NextBB);
  74. EmitStmt(S.getSubStmt());
  75. }
  76. void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) {
  77. Builder.CreateBr(getBasicBlockForLabel(S.getLabel()));
  78. // Emit a block after the branch so that dead code after a goto has some place
  79. // to go.
  80. Builder.SetInsertPoint(new llvm::BasicBlock("", CurFn));
  81. }
  82. void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
  83. // C99 6.8.4.1: The first substatement is executed if the expression compares
  84. // unequal to 0. The condition must be a scalar type.
  85. llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
  86. llvm::BasicBlock *ContBlock = new llvm::BasicBlock("ifend");
  87. llvm::BasicBlock *ThenBlock = new llvm::BasicBlock("ifthen");
  88. llvm::BasicBlock *ElseBlock = ContBlock;
  89. if (S.getElse())
  90. ElseBlock = new llvm::BasicBlock("ifelse");
  91. // Insert the conditional branch.
  92. Builder.CreateCondBr(BoolCondVal, ThenBlock, ElseBlock);
  93. // Emit the 'then' code.
  94. EmitBlock(ThenBlock);
  95. EmitStmt(S.getThen());
  96. Builder.CreateBr(ContBlock);
  97. // Emit the 'else' code if present.
  98. if (const Stmt *Else = S.getElse()) {
  99. EmitBlock(ElseBlock);
  100. EmitStmt(Else);
  101. Builder.CreateBr(ContBlock);
  102. }
  103. // Emit the continuation block for code after the if.
  104. EmitBlock(ContBlock);
  105. }
  106. void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) {
  107. // FIXME: Handle continue/break.
  108. // Emit the header for the loop, insert it, which will create an uncond br to
  109. // it.
  110. llvm::BasicBlock *LoopHeader = new llvm::BasicBlock("whilecond");
  111. EmitBlock(LoopHeader);
  112. // Evaluate the conditional in the while header. C99 6.8.5.1: The evaluation
  113. // of the controlling expression takes place before each execution of the loop
  114. // body.
  115. llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
  116. // TODO: while(1) is common, avoid extra exit blocks, etc. Be sure
  117. // to correctly handle break/continue though.
  118. // Create an exit block for when the condition fails, create a block for the
  119. // body of the loop.
  120. llvm::BasicBlock *ExitBlock = new llvm::BasicBlock("whileexit");
  121. llvm::BasicBlock *LoopBody = new llvm::BasicBlock("whilebody");
  122. // As long as the condition is true, go to the loop body.
  123. Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
  124. // Emit the loop body.
  125. EmitBlock(LoopBody);
  126. EmitStmt(S.getBody());
  127. // Cycle to the condition.
  128. Builder.CreateBr(LoopHeader);
  129. // Emit the exit block.
  130. EmitBlock(ExitBlock);
  131. }
  132. void CodeGenFunction::EmitDoStmt(const DoStmt &S) {
  133. // FIXME: Handle continue/break.
  134. // TODO: "do {} while (0)" is common in macros, avoid extra blocks. Be sure
  135. // to correctly handle break/continue though.
  136. // Emit the body for the loop, insert it, which will create an uncond br to
  137. // it.
  138. llvm::BasicBlock *LoopBody = new llvm::BasicBlock("dobody");
  139. llvm::BasicBlock *AfterDo = new llvm::BasicBlock("afterdo");
  140. EmitBlock(LoopBody);
  141. // Emit the body of the loop into the block.
  142. EmitStmt(S.getBody());
  143. // C99 6.8.5.2: "The evaluation of the controlling expression takes place
  144. // after each execution of the loop body."
  145. // Evaluate the conditional in the while header.
  146. // C99 6.8.5p2/p4: The first substatement is executed if the expression
  147. // compares unequal to 0. The condition must be a scalar type.
  148. llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
  149. // As long as the condition is true, iterate the loop.
  150. Builder.CreateCondBr(BoolCondVal, LoopBody, AfterDo);
  151. // Emit the exit block.
  152. EmitBlock(AfterDo);
  153. }
  154. void CodeGenFunction::EmitForStmt(const ForStmt &S) {
  155. // FIXME: Handle continue/break.
  156. // FIXME: What do we do if the increment (f.e.) contains a stmt expression,
  157. // which contains a continue/break?
  158. // Evaluate the first part before the loop.
  159. if (S.getInit())
  160. EmitStmt(S.getInit());
  161. // Start the loop with a block that tests the condition.
  162. llvm::BasicBlock *CondBlock = new llvm::BasicBlock("forcond");
  163. llvm::BasicBlock *AfterFor = 0;
  164. EmitBlock(CondBlock);
  165. // Evaluate the condition if present. If not, treat it as a non-zero-constant
  166. // according to 6.8.5.3p2, aka, true.
  167. if (S.getCond()) {
  168. // C99 6.8.5p2/p4: The first substatement is executed if the expression
  169. // compares unequal to 0. The condition must be a scalar type.
  170. llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
  171. // As long as the condition is true, iterate the loop.
  172. llvm::BasicBlock *ForBody = new llvm::BasicBlock("forbody");
  173. AfterFor = new llvm::BasicBlock("afterfor");
  174. Builder.CreateCondBr(BoolCondVal, ForBody, AfterFor);
  175. EmitBlock(ForBody);
  176. } else {
  177. // Treat it as a non-zero constant. Don't even create a new block for the
  178. // body, just fall into it.
  179. }
  180. // If the condition is true, execute the body of the for stmt.
  181. EmitStmt(S.getBody());
  182. // If there is an increment, emit it next.
  183. if (S.getInc())
  184. EmitExpr(S.getInc());
  185. // Finally, branch back up to the condition for the next iteration.
  186. Builder.CreateBr(CondBlock);
  187. // Emit the fall-through block if there is any.
  188. if (AfterFor)
  189. EmitBlock(AfterFor);
  190. else
  191. EmitBlock(new llvm::BasicBlock());
  192. }
  193. /// EmitReturnStmt - Note that due to GCC extensions, this can have an operand
  194. /// if the function returns void, or may be missing one if the function returns
  195. /// non-void. Fun stuff :).
  196. void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) {
  197. RValue RetVal;
  198. // Emit the result value, even if unused, to evalute the side effects.
  199. const Expr *RV = S.getRetValue();
  200. if (RV)
  201. RetVal = EmitExpr(RV);
  202. QualType FnRetTy = CurFuncDecl->getType().getCanonicalType();
  203. FnRetTy = cast<FunctionType>(FnRetTy)->getResultType();
  204. if (FnRetTy->isVoidType()) {
  205. // If the function returns void, emit ret void, and ignore the retval.
  206. Builder.CreateRetVoid();
  207. } else if (RV == 0) {
  208. // "return;" in a function that returns a value.
  209. const llvm::Type *RetTy = CurFn->getFunctionType()->getReturnType();
  210. if (RetTy == llvm::Type::VoidTy)
  211. Builder.CreateRetVoid(); // struct return etc.
  212. else
  213. Builder.CreateRet(llvm::UndefValue::get(RetTy));
  214. } else {
  215. // Do implicit conversions to the returned type.
  216. RetVal = EmitConversion(RetVal, RV->getType(), FnRetTy);
  217. if (RetVal.isScalar()) {
  218. Builder.CreateRet(RetVal.getVal());
  219. } else {
  220. llvm::Value *SRetPtr = CurFn->arg_begin();
  221. EmitStoreThroughLValue(RetVal, LValue::MakeAddr(SRetPtr), FnRetTy);
  222. }
  223. }
  224. // Emit a block after the branch so that dead code after a return has some
  225. // place to go.
  226. EmitBlock(new llvm::BasicBlock());
  227. }
  228. void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) {
  229. for (const Decl *Decl = S.getDecl(); Decl; Decl = Decl->getNextDeclarator())
  230. EmitDecl(*Decl);
  231. }