blob: 20312de5bab09e5484612891d58d1f4c738b4989 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CGStmt.cpp - Emit LLVM Code from Statements ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Stmt nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000014#include "CGDebugInfo.h"
15#include "CodeGenModule.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "CodeGenFunction.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000017#include "clang/AST/StmtVisitor.h"
Anders Carlssonfb1aeb82008-02-05 16:35:33 +000018#include "clang/Basic/TargetInfo.h"
Anders Carlssonfb1aeb82008-02-05 16:35:33 +000019#include "llvm/InlineAsm.h"
20#include "llvm/ADT/StringExtras.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22using namespace CodeGen;
23
24//===----------------------------------------------------------------------===//
25// Statement Emission
26//===----------------------------------------------------------------------===//
27
28void CodeGenFunction::EmitStmt(const Stmt *S) {
29 assert(S && "Null statement?");
30
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000031 // Generate stoppoints if we are emitting debug info.
32 // Beginning of a Compound Statement (e.g. an opening '{') does not produce
33 // executable code. So do not generate a stoppoint for that.
34 CGDebugInfo *DI = CGM.getDebugInfo();
35 if (DI && S->getStmtClass() != Stmt::CompoundStmtClass) {
Daniel Dunbar66031a52008-10-17 16:15:48 +000036 DI->setLocation(S->getLocStart());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000037 DI->EmitStopPoint(CurFn, Builder);
38 }
39
Reid Spencer5f016e22007-07-11 17:01:13 +000040 switch (S->getStmtClass()) {
41 default:
Chris Lattner1e4d21e2007-08-26 22:58:05 +000042 // Must be an expression in a stmt context. Emit the value (to get
43 // side-effects) and ignore the result.
Reid Spencer5f016e22007-07-11 17:01:13 +000044 if (const Expr *E = dyn_cast<Expr>(S)) {
Chris Lattner1e4d21e2007-08-26 22:58:05 +000045 if (!hasAggregateLLVMType(E->getType()))
46 EmitScalarExpr(E);
Chris Lattner9b2dc282008-04-04 16:54:41 +000047 else if (E->getType()->isAnyComplexType())
Chris Lattner1e4d21e2007-08-26 22:58:05 +000048 EmitComplexExpr(E);
49 else
50 EmitAggExpr(E, 0, false);
Reid Spencer5f016e22007-07-11 17:01:13 +000051 } else {
Daniel Dunbar488e9932008-08-16 00:56:44 +000052 ErrorUnsupported(S, "statement");
Reid Spencer5f016e22007-07-11 17:01:13 +000053 }
54 break;
55 case Stmt::NullStmtClass: break;
56 case Stmt::CompoundStmtClass: EmitCompoundStmt(cast<CompoundStmt>(*S)); break;
57 case Stmt::LabelStmtClass: EmitLabelStmt(cast<LabelStmt>(*S)); break;
58 case Stmt::GotoStmtClass: EmitGotoStmt(cast<GotoStmt>(*S)); break;
Daniel Dunbar0ffb1252008-08-04 16:51:22 +000059 case Stmt::IndirectGotoStmtClass:
60 EmitIndirectGotoStmt(cast<IndirectGotoStmt>(*S)); break;
Reid Spencer5f016e22007-07-11 17:01:13 +000061
62 case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break;
63 case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S)); break;
64 case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S)); break;
65 case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S)); break;
66
67 case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break;
68 case Stmt::DeclStmtClass: EmitDeclStmt(cast<DeclStmt>(*S)); break;
Chris Lattnerda138702007-07-16 21:28:45 +000069
Daniel Dunbara4275d12008-10-02 18:02:06 +000070 case Stmt::BreakStmtClass:
71 // FIXME: Implement break in @try or @catch blocks.
72 if (!ObjCEHStack.empty()) {
73 CGM.ErrorUnsupported(S, "continue inside an Obj-C exception block");
74 return;
75 }
76 EmitBreakStmt();
77 break;
78
79 case Stmt::ContinueStmtClass:
80 // FIXME: Implement continue in @try or @catch blocks.
81 if (!ObjCEHStack.empty()) {
82 CGM.ErrorUnsupported(S, "continue inside an Obj-C exception block");
83 return;
84 }
85 EmitContinueStmt();
86 break;
87
Devang Patel51b09f22007-10-04 23:45:31 +000088 case Stmt::SwitchStmtClass: EmitSwitchStmt(cast<SwitchStmt>(*S)); break;
89 case Stmt::DefaultStmtClass: EmitDefaultStmt(cast<DefaultStmt>(*S)); break;
90 case Stmt::CaseStmtClass: EmitCaseStmt(cast<CaseStmt>(*S)); break;
Anders Carlssonfb1aeb82008-02-05 16:35:33 +000091 case Stmt::AsmStmtClass: EmitAsmStmt(cast<AsmStmt>(*S)); break;
Daniel Dunbar0a04d772008-08-23 10:51:21 +000092
93 case Stmt::ObjCAtTryStmtClass:
Anders Carlsson64d5d6c2008-09-09 10:04:29 +000094 EmitObjCAtTryStmt(cast<ObjCAtTryStmt>(*S));
95 break;
Daniel Dunbar0a04d772008-08-23 10:51:21 +000096 case Stmt::ObjCAtCatchStmtClass:
Anders Carlssondde0a942008-09-11 09:15:33 +000097 assert(0 && "@catch statements should be handled by EmitObjCAtTryStmt");
98 break;
Daniel Dunbar0a04d772008-08-23 10:51:21 +000099 case Stmt::ObjCAtFinallyStmtClass:
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000100 assert(0 && "@finally statements should be handled by EmitObjCAtTryStmt");
Daniel Dunbar0a04d772008-08-23 10:51:21 +0000101 break;
102 case Stmt::ObjCAtThrowStmtClass:
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000103 EmitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(*S));
Daniel Dunbar0a04d772008-08-23 10:51:21 +0000104 break;
105 case Stmt::ObjCAtSynchronizedStmtClass:
106 ErrorUnsupported(S, "@synchronized statement");
107 break;
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000108 case Stmt::ObjCForCollectionStmtClass:
109 EmitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(*S));
Daniel Dunbar0a04d772008-08-23 10:51:21 +0000110 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 }
112}
113
Chris Lattner33793202007-08-31 22:09:40 +0000114/// EmitCompoundStmt - Emit a compound statement {..} node. If GetLast is true,
115/// this captures the expression result of the last sub-statement and returns it
116/// (for use by the statement expression extension).
Chris Lattner9b655512007-08-31 22:49:20 +0000117RValue CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast,
118 llvm::Value *AggLoc, bool isAggVol) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 // FIXME: handle vla's etc.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000120 CGDebugInfo *DI = CGM.getDebugInfo();
121 if (DI) {
Daniel Dunbar66031a52008-10-17 16:15:48 +0000122 DI->setLocation(S.getLBracLoc());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000123 DI->EmitRegionStart(CurFn, Builder);
124 }
125
Chris Lattner33793202007-08-31 22:09:40 +0000126 for (CompoundStmt::const_body_iterator I = S.body_begin(),
127 E = S.body_end()-GetLast; I != E; ++I)
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 EmitStmt(*I);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000129
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000130 if (DI) {
Daniel Dunbar66031a52008-10-17 16:15:48 +0000131 DI->setLocation(S.getRBracLoc());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000132 DI->EmitRegionEnd(CurFn, Builder);
133 }
134
Chris Lattner33793202007-08-31 22:09:40 +0000135 if (!GetLast)
136 return RValue::get(0);
Chris Lattner9b655512007-08-31 22:49:20 +0000137
Chris Lattner91d723d2008-07-26 20:23:23 +0000138 // We have to special case labels here. They are statements, but when put at
139 // the end of a statement expression, they yield the value of their
140 // subexpression. Handle this by walking through all labels we encounter,
141 // emitting them before we evaluate the subexpr.
142 const Stmt *LastStmt = S.body_back();
143 while (const LabelStmt *LS = dyn_cast<LabelStmt>(LastStmt)) {
144 EmitLabel(*LS);
145 LastStmt = LS->getSubStmt();
146 }
147
148 return EmitAnyExpr(cast<Expr>(LastStmt), AggLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000149}
150
151void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB) {
152 // Emit a branch from this block to the next one if this was a real block. If
153 // this was just a fall-through block after a terminator, don't emit it.
154 llvm::BasicBlock *LastBB = Builder.GetInsertBlock();
155
156 if (LastBB->getTerminator()) {
157 // If the previous block is already terminated, don't touch it.
Daniel Dunbar16f23572008-07-25 01:11:38 +0000158 } else if (LastBB->empty() && isDummyBlock(LastBB)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 // If the last block was an empty placeholder, remove it now.
160 // TODO: cache and reuse these.
Daniel Dunbar16f23572008-07-25 01:11:38 +0000161 LastBB->eraseFromParent();
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 } else {
163 // Otherwise, create a fall-through branch.
164 Builder.CreateBr(BB);
165 }
166 CurFn->getBasicBlockList().push_back(BB);
167 Builder.SetInsertPoint(BB);
168}
169
Chris Lattner91d723d2008-07-26 20:23:23 +0000170void CodeGenFunction::EmitLabel(const LabelStmt &S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 llvm::BasicBlock *NextBB = getBasicBlockForLabel(&S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 EmitBlock(NextBB);
Chris Lattner91d723d2008-07-26 20:23:23 +0000173}
174
175
176void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) {
177 EmitLabel(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 EmitStmt(S.getSubStmt());
179}
180
181void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) {
Daniel Dunbara4275d12008-10-02 18:02:06 +0000182 // FIXME: Implement goto out in @try or @catch blocks.
183 if (!ObjCEHStack.empty()) {
184 CGM.ErrorUnsupported(&S, "goto inside an Obj-C exception block");
185 return;
186 }
187
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 Builder.CreateBr(getBasicBlockForLabel(S.getLabel()));
189
190 // Emit a block after the branch so that dead code after a goto has some place
191 // to go.
Gabor Greif984d0b42008-04-06 20:42:52 +0000192 Builder.SetInsertPoint(llvm::BasicBlock::Create("", CurFn));
Reid Spencer5f016e22007-07-11 17:01:13 +0000193}
194
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000195void CodeGenFunction::EmitIndirectGotoStmt(const IndirectGotoStmt &S) {
Daniel Dunbara4275d12008-10-02 18:02:06 +0000196 // FIXME: Implement indirect goto in @try or @catch blocks.
197 if (!ObjCEHStack.empty()) {
198 CGM.ErrorUnsupported(&S, "goto inside an Obj-C exception block");
199 return;
200 }
201
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000202 // Emit initial switch which will be patched up later by
203 // EmitIndirectSwitches(). We need a default dest, so we use the
204 // current BB, but this is overwritten.
205 llvm::Value *V = Builder.CreatePtrToInt(EmitScalarExpr(S.getTarget()),
206 llvm::Type::Int32Ty,
207 "addr");
208 llvm::SwitchInst *I = Builder.CreateSwitch(V, Builder.GetInsertBlock());
209 IndirectSwitches.push_back(I);
210
211 // Emit a block after the branch so that dead code after a goto has some place
212 // to go.
213 Builder.SetInsertPoint(llvm::BasicBlock::Create("", CurFn));
214}
215
Reid Spencer5f016e22007-07-11 17:01:13 +0000216void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000217 // FIXME: It would probably be nice for us to skip emission of if
218 // (0) code here.
219
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 // C99 6.8.4.1: The first substatement is executed if the expression compares
221 // unequal to 0. The condition must be a scalar type.
222 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
223
Gabor Greif984d0b42008-04-06 20:42:52 +0000224 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("ifend");
225 llvm::BasicBlock *ThenBlock = llvm::BasicBlock::Create("ifthen");
Reid Spencer5f016e22007-07-11 17:01:13 +0000226 llvm::BasicBlock *ElseBlock = ContBlock;
227
228 if (S.getElse())
Gabor Greif984d0b42008-04-06 20:42:52 +0000229 ElseBlock = llvm::BasicBlock::Create("ifelse");
Reid Spencer5f016e22007-07-11 17:01:13 +0000230
231 // Insert the conditional branch.
232 Builder.CreateCondBr(BoolCondVal, ThenBlock, ElseBlock);
233
234 // Emit the 'then' code.
235 EmitBlock(ThenBlock);
236 EmitStmt(S.getThen());
Devang Pateld9363c32007-09-28 21:49:18 +0000237 llvm::BasicBlock *BB = Builder.GetInsertBlock();
238 if (isDummyBlock(BB)) {
239 BB->eraseFromParent();
240 Builder.SetInsertPoint(ThenBlock);
Chris Lattner345f7202008-07-26 20:15:14 +0000241 } else {
Devang Pateld9363c32007-09-28 21:49:18 +0000242 Builder.CreateBr(ContBlock);
Chris Lattner345f7202008-07-26 20:15:14 +0000243 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000244
245 // Emit the 'else' code if present.
246 if (const Stmt *Else = S.getElse()) {
247 EmitBlock(ElseBlock);
248 EmitStmt(Else);
Devang Pateld9363c32007-09-28 21:49:18 +0000249 llvm::BasicBlock *BB = Builder.GetInsertBlock();
250 if (isDummyBlock(BB)) {
251 BB->eraseFromParent();
252 Builder.SetInsertPoint(ElseBlock);
Chris Lattner345f7202008-07-26 20:15:14 +0000253 } else {
Devang Pateld9363c32007-09-28 21:49:18 +0000254 Builder.CreateBr(ContBlock);
Chris Lattner345f7202008-07-26 20:15:14 +0000255 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 }
257
258 // Emit the continuation block for code after the if.
259 EmitBlock(ContBlock);
260}
261
262void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 // Emit the header for the loop, insert it, which will create an uncond br to
264 // it.
Gabor Greif984d0b42008-04-06 20:42:52 +0000265 llvm::BasicBlock *LoopHeader = llvm::BasicBlock::Create("whilecond");
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 EmitBlock(LoopHeader);
267
268 // Evaluate the conditional in the while header. C99 6.8.5.1: The evaluation
269 // of the controlling expression takes place before each execution of the loop
270 // body.
271 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
Devang Patel2c30d8f2007-10-09 20:51:27 +0000272
273 // while(1) is common, avoid extra exit blocks. Be sure
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 // to correctly handle break/continue though.
Devang Patel2c30d8f2007-10-09 20:51:27 +0000275 bool EmitBoolCondBranch = true;
276 if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
277 if (C->isOne())
278 EmitBoolCondBranch = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000279
280 // Create an exit block for when the condition fails, create a block for the
281 // body of the loop.
Gabor Greif984d0b42008-04-06 20:42:52 +0000282 llvm::BasicBlock *ExitBlock = llvm::BasicBlock::Create("whileexit");
283 llvm::BasicBlock *LoopBody = llvm::BasicBlock::Create("whilebody");
Reid Spencer5f016e22007-07-11 17:01:13 +0000284
285 // As long as the condition is true, go to the loop body.
Devang Patel2c30d8f2007-10-09 20:51:27 +0000286 if (EmitBoolCondBranch)
287 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
Chris Lattnerda138702007-07-16 21:28:45 +0000288
289 // Store the blocks to use for break and continue.
290 BreakContinueStack.push_back(BreakContinue(ExitBlock, LoopHeader));
Reid Spencer5f016e22007-07-11 17:01:13 +0000291
292 // Emit the loop body.
293 EmitBlock(LoopBody);
294 EmitStmt(S.getBody());
Chris Lattnerda138702007-07-16 21:28:45 +0000295
296 BreakContinueStack.pop_back();
Reid Spencer5f016e22007-07-11 17:01:13 +0000297
298 // Cycle to the condition.
299 Builder.CreateBr(LoopHeader);
300
301 // Emit the exit block.
302 EmitBlock(ExitBlock);
Devang Patel2c30d8f2007-10-09 20:51:27 +0000303
304 // If LoopHeader is a simple forwarding block then eliminate it.
305 if (!EmitBoolCondBranch
306 && &LoopHeader->front() == LoopHeader->getTerminator()) {
307 LoopHeader->replaceAllUsesWith(LoopBody);
308 LoopHeader->getTerminator()->eraseFromParent();
309 LoopHeader->eraseFromParent();
310 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000311}
312
313void CodeGenFunction::EmitDoStmt(const DoStmt &S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 // Emit the body for the loop, insert it, which will create an uncond br to
315 // it.
Gabor Greif984d0b42008-04-06 20:42:52 +0000316 llvm::BasicBlock *LoopBody = llvm::BasicBlock::Create("dobody");
317 llvm::BasicBlock *AfterDo = llvm::BasicBlock::Create("afterdo");
Reid Spencer5f016e22007-07-11 17:01:13 +0000318 EmitBlock(LoopBody);
Chris Lattnerda138702007-07-16 21:28:45 +0000319
Gabor Greif984d0b42008-04-06 20:42:52 +0000320 llvm::BasicBlock *DoCond = llvm::BasicBlock::Create("docond");
Chris Lattnerda138702007-07-16 21:28:45 +0000321
322 // Store the blocks to use for break and continue.
323 BreakContinueStack.push_back(BreakContinue(AfterDo, DoCond));
Reid Spencer5f016e22007-07-11 17:01:13 +0000324
325 // Emit the body of the loop into the block.
326 EmitStmt(S.getBody());
327
Chris Lattnerda138702007-07-16 21:28:45 +0000328 BreakContinueStack.pop_back();
329
330 EmitBlock(DoCond);
331
Reid Spencer5f016e22007-07-11 17:01:13 +0000332 // C99 6.8.5.2: "The evaluation of the controlling expression takes place
333 // after each execution of the loop body."
334
335 // Evaluate the conditional in the while header.
336 // C99 6.8.5p2/p4: The first substatement is executed if the expression
337 // compares unequal to 0. The condition must be a scalar type.
338 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
Devang Patel05f6e6b2007-10-09 20:33:39 +0000339
340 // "do {} while (0)" is common in macros, avoid extra blocks. Be sure
341 // to correctly handle break/continue though.
342 bool EmitBoolCondBranch = true;
343 if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
344 if (C->isZero())
345 EmitBoolCondBranch = false;
346
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 // As long as the condition is true, iterate the loop.
Devang Patel05f6e6b2007-10-09 20:33:39 +0000348 if (EmitBoolCondBranch)
349 Builder.CreateCondBr(BoolCondVal, LoopBody, AfterDo);
Reid Spencer5f016e22007-07-11 17:01:13 +0000350
351 // Emit the exit block.
352 EmitBlock(AfterDo);
Devang Patel05f6e6b2007-10-09 20:33:39 +0000353
354 // If DoCond is a simple forwarding block then eliminate it.
355 if (!EmitBoolCondBranch && &DoCond->front() == DoCond->getTerminator()) {
356 DoCond->replaceAllUsesWith(AfterDo);
357 DoCond->getTerminator()->eraseFromParent();
358 DoCond->eraseFromParent();
359 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000360}
361
362void CodeGenFunction::EmitForStmt(const ForStmt &S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 // FIXME: What do we do if the increment (f.e.) contains a stmt expression,
364 // which contains a continue/break?
Chris Lattnerda138702007-07-16 21:28:45 +0000365 // TODO: We could keep track of whether the loop body contains any
366 // break/continue statements and not create unnecessary blocks (like
367 // "afterfor" for a condless loop) if it doesn't.
368
Reid Spencer5f016e22007-07-11 17:01:13 +0000369 // Evaluate the first part before the loop.
370 if (S.getInit())
371 EmitStmt(S.getInit());
372
373 // Start the loop with a block that tests the condition.
Gabor Greif984d0b42008-04-06 20:42:52 +0000374 llvm::BasicBlock *CondBlock = llvm::BasicBlock::Create("forcond");
375 llvm::BasicBlock *AfterFor = llvm::BasicBlock::Create("afterfor");
Chris Lattnerda138702007-07-16 21:28:45 +0000376
Reid Spencer5f016e22007-07-11 17:01:13 +0000377 EmitBlock(CondBlock);
378
379 // Evaluate the condition if present. If not, treat it as a non-zero-constant
380 // according to 6.8.5.3p2, aka, true.
381 if (S.getCond()) {
382 // C99 6.8.5p2/p4: The first substatement is executed if the expression
383 // compares unequal to 0. The condition must be a scalar type.
384 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
385
386 // As long as the condition is true, iterate the loop.
Gabor Greif984d0b42008-04-06 20:42:52 +0000387 llvm::BasicBlock *ForBody = llvm::BasicBlock::Create("forbody");
Reid Spencer5f016e22007-07-11 17:01:13 +0000388 Builder.CreateCondBr(BoolCondVal, ForBody, AfterFor);
389 EmitBlock(ForBody);
390 } else {
391 // Treat it as a non-zero constant. Don't even create a new block for the
392 // body, just fall into it.
393 }
394
Chris Lattnerda138702007-07-16 21:28:45 +0000395 // If the for loop doesn't have an increment we can just use the
396 // condition as the continue block.
397 llvm::BasicBlock *ContinueBlock;
398 if (S.getInc())
Gabor Greif984d0b42008-04-06 20:42:52 +0000399 ContinueBlock = llvm::BasicBlock::Create("forinc");
Chris Lattnerda138702007-07-16 21:28:45 +0000400 else
401 ContinueBlock = CondBlock;
402
403 // Store the blocks to use for break and continue.
404 BreakContinueStack.push_back(BreakContinue(AfterFor, ContinueBlock));
405
Reid Spencer5f016e22007-07-11 17:01:13 +0000406 // If the condition is true, execute the body of the for stmt.
407 EmitStmt(S.getBody());
Chris Lattnerda138702007-07-16 21:28:45 +0000408
409 BreakContinueStack.pop_back();
410
Reid Spencer5f016e22007-07-11 17:01:13 +0000411 // If there is an increment, emit it next.
Daniel Dunbarad12b6d2008-09-28 00:19:22 +0000412 if (S.getInc()) {
413 EmitBlock(ContinueBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000414 EmitStmt(S.getInc());
Daniel Dunbarad12b6d2008-09-28 00:19:22 +0000415 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000416
417 // Finally, branch back up to the condition for the next iteration.
418 Builder.CreateBr(CondBlock);
419
Chris Lattnerda138702007-07-16 21:28:45 +0000420 // Emit the fall-through block.
421 EmitBlock(AfterFor);
Reid Spencer5f016e22007-07-11 17:01:13 +0000422}
423
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +0000424void CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) {
425 if (RV.isScalar()) {
426 Builder.CreateStore(RV.getScalarVal(), ReturnValue);
427 } else if (RV.isAggregate()) {
428 EmitAggregateCopy(ReturnValue, RV.getAggregateAddr(), Ty);
429 } else {
430 StoreComplexToAddr(RV.getComplexVal(), ReturnValue, false);
431 }
432 Builder.CreateBr(ReturnBlock);
433
434 // Emit a block after the branch so that dead code after a return has some
435 // place to go.
436 EmitBlock(llvm::BasicBlock::Create());
437}
438
Reid Spencer5f016e22007-07-11 17:01:13 +0000439/// EmitReturnStmt - Note that due to GCC extensions, this can have an operand
440/// if the function returns void, or may be missing one if the function returns
441/// non-void. Fun stuff :).
442void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 // Emit the result value, even if unused, to evalute the side effects.
444 const Expr *RV = S.getRetValue();
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000445
446 // FIXME: Clean this up by using an LValue for ReturnTemp,
447 // EmitStoreThroughLValue, and EmitAnyExpr.
448 if (!ReturnValue) {
449 // Make sure not to return anything, but evaluate the expression
450 // for side effects.
451 if (RV)
Eli Friedman144ac612008-05-22 01:22:33 +0000452 EmitAnyExpr(RV);
Reid Spencer5f016e22007-07-11 17:01:13 +0000453 } else if (RV == 0) {
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000454 // Do nothing (return value is left uninitialized)
Chris Lattner4b0029d2007-08-26 07:14:44 +0000455 } else if (!hasAggregateLLVMType(RV->getType())) {
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000456 Builder.CreateStore(EmitScalarExpr(RV), ReturnValue);
Chris Lattner9b2dc282008-04-04 16:54:41 +0000457 } else if (RV->getType()->isAnyComplexType()) {
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000458 EmitComplexExprIntoAddr(RV, ReturnValue, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 } else {
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000460 EmitAggExpr(RV, ReturnValue, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000461 }
Eli Friedman144ac612008-05-22 01:22:33 +0000462
Daniel Dunbar898d5082008-09-30 01:06:03 +0000463 if (!ObjCEHStack.empty()) {
464 for (ObjCEHStackType::reverse_iterator i = ObjCEHStack.rbegin(),
465 e = ObjCEHStack.rend(); i != e; ++i) {
466 llvm::BasicBlock *ReturnPad = llvm::BasicBlock::Create("return.pad");
467 EmitJumpThroughFinally(*i, ReturnPad);
468 EmitBlock(ReturnPad);
469 }
470 }
471
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000472 Builder.CreateBr(ReturnBlock);
Reid Spencer5f016e22007-07-11 17:01:13 +0000473
474 // Emit a block after the branch so that dead code after a return has some
475 // place to go.
Gabor Greif984d0b42008-04-06 20:42:52 +0000476 EmitBlock(llvm::BasicBlock::Create());
Reid Spencer5f016e22007-07-11 17:01:13 +0000477}
478
479void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) {
Ted Kremeneke4ea1f42008-10-06 18:42:27 +0000480 for (DeclStmt::const_decl_iterator I = S.decl_begin(), E = S.decl_end();
481 I != E; ++I)
482 EmitDecl(**I);
Chris Lattner6fa5f092007-07-12 15:43:07 +0000483}
Chris Lattnerda138702007-07-16 21:28:45 +0000484
485void CodeGenFunction::EmitBreakStmt() {
486 assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!");
487
488 llvm::BasicBlock *Block = BreakContinueStack.back().BreakBlock;
489 Builder.CreateBr(Block);
Gabor Greif984d0b42008-04-06 20:42:52 +0000490 EmitBlock(llvm::BasicBlock::Create());
Chris Lattnerda138702007-07-16 21:28:45 +0000491}
492
493void CodeGenFunction::EmitContinueStmt() {
494 assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
495
496 llvm::BasicBlock *Block = BreakContinueStack.back().ContinueBlock;
497 Builder.CreateBr(Block);
Gabor Greif984d0b42008-04-06 20:42:52 +0000498 EmitBlock(llvm::BasicBlock::Create());
Chris Lattnerda138702007-07-16 21:28:45 +0000499}
Devang Patel51b09f22007-10-04 23:45:31 +0000500
Devang Patelc049e4f2007-10-08 20:57:48 +0000501/// EmitCaseStmtRange - If case statement range is not too big then
502/// add multiple cases to switch instruction, one for each value within
503/// the range. If range is too big then emit "if" condition check.
504void CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S) {
Daniel Dunbar4efde8d2008-07-24 01:18:41 +0000505 assert(S.getRHS() && "Expected RHS value in CaseStmt");
Devang Patelc049e4f2007-10-08 20:57:48 +0000506
Daniel Dunbar4efde8d2008-07-24 01:18:41 +0000507 llvm::APSInt LHS = S.getLHS()->getIntegerConstantExprValue(getContext());
508 llvm::APSInt RHS = S.getRHS()->getIntegerConstantExprValue(getContext());
509
Daniel Dunbar16f23572008-07-25 01:11:38 +0000510 // Emit the code for this case. We do this first to make sure it is
511 // properly chained from our predecessor before generating the
512 // switch machinery to enter this block.
513 StartBlock("sw.bb");
514 llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
515 EmitStmt(S.getSubStmt());
516
Daniel Dunbar4efde8d2008-07-24 01:18:41 +0000517 // If range is empty, do nothing.
518 if (LHS.isSigned() ? RHS.slt(LHS) : RHS.ult(LHS))
519 return;
Devang Patelc049e4f2007-10-08 20:57:48 +0000520
521 llvm::APInt Range = RHS - LHS;
Daniel Dunbar16f23572008-07-25 01:11:38 +0000522 // FIXME: parameters such as this should not be hardcoded.
Devang Patelc049e4f2007-10-08 20:57:48 +0000523 if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) {
524 // Range is small enough to add multiple switch instruction cases.
Daniel Dunbar4efde8d2008-07-24 01:18:41 +0000525 for (unsigned i = 0, e = Range.getZExtValue() + 1; i != e; ++i) {
Devang Patel2d79d0f2007-10-05 20:54:07 +0000526 SwitchInsn->addCase(llvm::ConstantInt::get(LHS), CaseDest);
527 LHS++;
528 }
Devang Patelc049e4f2007-10-08 20:57:48 +0000529 return;
530 }
531
Daniel Dunbar16f23572008-07-25 01:11:38 +0000532 // The range is too big. Emit "if" condition into a new block,
533 // making sure to save and restore the current insertion point.
534 llvm::BasicBlock *RestoreBB = Builder.GetInsertBlock();
Devang Patel2d79d0f2007-10-05 20:54:07 +0000535
Daniel Dunbar16f23572008-07-25 01:11:38 +0000536 // Push this test onto the chain of range checks (which terminates
537 // in the default basic block). The switch's default will be changed
538 // to the top of this chain after switch emission is complete.
539 llvm::BasicBlock *FalseDest = CaseRangeBlock;
540 CaseRangeBlock = llvm::BasicBlock::Create("sw.caserange");
Devang Patelc049e4f2007-10-08 20:57:48 +0000541
Daniel Dunbar16f23572008-07-25 01:11:38 +0000542 CurFn->getBasicBlockList().push_back(CaseRangeBlock);
543 Builder.SetInsertPoint(CaseRangeBlock);
Devang Patelc049e4f2007-10-08 20:57:48 +0000544
545 // Emit range check.
546 llvm::Value *Diff =
Daniel Dunbar4efde8d2008-07-24 01:18:41 +0000547 Builder.CreateSub(SwitchInsn->getCondition(), llvm::ConstantInt::get(LHS),
548 "tmp");
Devang Patelc049e4f2007-10-08 20:57:48 +0000549 llvm::Value *Cond =
550 Builder.CreateICmpULE(Diff, llvm::ConstantInt::get(Range), "tmp");
551 Builder.CreateCondBr(Cond, CaseDest, FalseDest);
552
Daniel Dunbar16f23572008-07-25 01:11:38 +0000553 // Restore the appropriate insertion point.
554 Builder.SetInsertPoint(RestoreBB);
Devang Patelc049e4f2007-10-08 20:57:48 +0000555}
556
557void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) {
558 if (S.getRHS()) {
559 EmitCaseStmtRange(S);
560 return;
561 }
562
563 StartBlock("sw.bb");
564 llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
Daniel Dunbar4efde8d2008-07-24 01:18:41 +0000565 llvm::APSInt CaseVal = S.getLHS()->getIntegerConstantExprValue(getContext());
566 SwitchInsn->addCase(llvm::ConstantInt::get(CaseVal),
567 CaseDest);
Devang Patel51b09f22007-10-04 23:45:31 +0000568 EmitStmt(S.getSubStmt());
569}
570
571void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) {
Daniel Dunbar16f23572008-07-25 01:11:38 +0000572 llvm::BasicBlock *DefaultBlock = SwitchInsn->getDefaultDest();
573 assert(DefaultBlock->empty() && "EmitDefaultStmt: Default block already defined?");
574 EmitBlock(DefaultBlock);
Devang Patel51b09f22007-10-04 23:45:31 +0000575 EmitStmt(S.getSubStmt());
576}
577
578void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
579 llvm::Value *CondV = EmitScalarExpr(S.getCond());
580
581 // Handle nested switch statements.
582 llvm::SwitchInst *SavedSwitchInsn = SwitchInsn;
Devang Patelc049e4f2007-10-08 20:57:48 +0000583 llvm::BasicBlock *SavedCRBlock = CaseRangeBlock;
Devang Patel51b09f22007-10-04 23:45:31 +0000584
Daniel Dunbar16f23572008-07-25 01:11:38 +0000585 // Create basic block to hold stuff that comes after switch
586 // statement. We also need to create a default block now so that
587 // explicit case ranges tests can have a place to jump to on
588 // failure.
589 llvm::BasicBlock *NextBlock = llvm::BasicBlock::Create("sw.epilog");
590 llvm::BasicBlock *DefaultBlock = llvm::BasicBlock::Create("sw.default");
591 SwitchInsn = Builder.CreateSwitch(CondV, DefaultBlock);
592 CaseRangeBlock = DefaultBlock;
Devang Patel51b09f22007-10-04 23:45:31 +0000593
Eli Friedmand28a80d2008-05-12 16:08:04 +0000594 // Create basic block for body of switch
Daniel Dunbar16f23572008-07-25 01:11:38 +0000595 StartBlock("sw.body");
Eli Friedmand28a80d2008-05-12 16:08:04 +0000596
Devang Patele9b8c0a2007-10-30 20:59:40 +0000597 // All break statements jump to NextBlock. If BreakContinueStack is non empty
598 // then reuse last ContinueBlock.
Devang Patel51b09f22007-10-04 23:45:31 +0000599 llvm::BasicBlock *ContinueBlock = NULL;
600 if (!BreakContinueStack.empty())
601 ContinueBlock = BreakContinueStack.back().ContinueBlock;
602 BreakContinueStack.push_back(BreakContinue(NextBlock, ContinueBlock));
603
604 // Emit switch body.
605 EmitStmt(S.getBody());
606 BreakContinueStack.pop_back();
607
Daniel Dunbar16f23572008-07-25 01:11:38 +0000608 // Update the default block in case explicit case range tests have
609 // been chained on top.
610 SwitchInsn->setSuccessor(0, CaseRangeBlock);
Devang Patelc049e4f2007-10-08 20:57:48 +0000611
Daniel Dunbar16f23572008-07-25 01:11:38 +0000612 // If a default was never emitted then reroute any jumps to it and
613 // discard.
614 if (!DefaultBlock->getParent()) {
615 DefaultBlock->replaceAllUsesWith(NextBlock);
616 delete DefaultBlock;
617 }
Devang Patel51b09f22007-10-04 23:45:31 +0000618
Daniel Dunbar16f23572008-07-25 01:11:38 +0000619 // Emit continuation.
620 EmitBlock(NextBlock);
621
Devang Patel51b09f22007-10-04 23:45:31 +0000622 SwitchInsn = SavedSwitchInsn;
Devang Patelc049e4f2007-10-08 20:57:48 +0000623 CaseRangeBlock = SavedCRBlock;
Devang Patel51b09f22007-10-04 23:45:31 +0000624}
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000625
Chris Lattner345f7202008-07-26 20:15:14 +0000626static std::string ConvertAsmString(const char *Start, unsigned NumOperands,
627 bool IsSimple) {
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000628 static unsigned AsmCounter = 0;
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000629 AsmCounter++;
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000630 std::string Result;
Anders Carlsson2abd25f2008-02-05 23:18:57 +0000631 if (IsSimple) {
632 while (*Start) {
633 switch (*Start) {
634 default:
635 Result += *Start;
636 break;
637 case '$':
638 Result += "$$";
639 break;
640 }
Anders Carlsson2abd25f2008-02-05 23:18:57 +0000641 Start++;
642 }
643
644 return Result;
645 }
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000646
647 while (*Start) {
648 switch (*Start) {
649 default:
650 Result += *Start;
651 break;
652 case '$':
653 Result += "$$";
654 break;
655 case '%':
656 // Escaped character
657 Start++;
658 if (!*Start) {
659 // FIXME: This should be caught during Sema.
660 assert(0 && "Trailing '%' in asm string.");
661 }
662
663 char EscapedChar = *Start;
664 if (EscapedChar == '%') {
665 // Escaped percentage sign.
666 Result += '%';
Chris Lattner345f7202008-07-26 20:15:14 +0000667 } else if (EscapedChar == '=') {
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000668 // Generate an unique ID.
669 Result += llvm::utostr(AsmCounter);
670 } else if (isdigit(EscapedChar)) {
671 // %n - Assembler operand n
672 char *End;
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000673 unsigned long n = strtoul(Start, &End, 10);
674 if (Start == End) {
675 // FIXME: This should be caught during Sema.
676 assert(0 && "Missing operand!");
677 } else if (n >= NumOperands) {
678 // FIXME: This should be caught during Sema.
679 assert(0 && "Operand number out of range!");
680 }
681
682 Result += '$' + llvm::utostr(n);
Lauro Ramos Venancio7695f702008-02-26 19:19:58 +0000683 Start = End - 1;
Anders Carlsson2abd25f2008-02-05 23:18:57 +0000684 } else if (isalpha(EscapedChar)) {
685 char *End;
686
687 unsigned long n = strtoul(Start + 1, &End, 10);
688 if (Start == End) {
689 // FIXME: This should be caught during Sema.
690 assert(0 && "Missing operand!");
691 } else if (n >= NumOperands) {
692 // FIXME: This should be caught during Sema.
693 assert(0 && "Operand number out of range!");
694 }
695
696 Result += "${" + llvm::utostr(n) + ':' + EscapedChar + '}';
Lauro Ramos Venancio7695f702008-02-26 19:19:58 +0000697 Start = End - 1;
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000698 } else {
699 assert(0 && "Unhandled asm escaped character!");
700 }
701 }
702 Start++;
703 }
704
705 return Result;
706}
707
Lauro Ramos Venancioa5694b82008-02-26 18:33:46 +0000708static std::string SimplifyConstraint(const char* Constraint,
709 TargetInfo &Target) {
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000710 std::string Result;
711
712 while (*Constraint) {
713 switch (*Constraint) {
714 default:
Lauro Ramos Venancioa5694b82008-02-26 18:33:46 +0000715 Result += Target.convertConstraint(*Constraint);
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000716 break;
717 // Ignore these
718 case '*':
719 case '?':
720 case '!':
721 break;
722 case 'g':
723 Result += "imr";
724 break;
725 }
726
727 Constraint++;
728 }
729
730 return Result;
731}
732
733void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) {
734 std::string AsmString =
735 ConvertAsmString(std::string(S.getAsmString()->getStrData(),
736 S.getAsmString()->getByteLength()).c_str(),
Anders Carlsson2abd25f2008-02-05 23:18:57 +0000737 S.getNumOutputs() + S.getNumInputs(), S.isSimple());
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000738
739 std::string Constraints;
740
741 llvm::Value *ResultAddr = 0;
742 const llvm::Type *ResultType = llvm::Type::VoidTy;
743
744 std::vector<const llvm::Type*> ArgTypes;
745 std::vector<llvm::Value*> Args;
Anders Carlssonf39a4212008-02-05 20:01:53 +0000746
747 // Keep track of inout constraints.
748 std::string InOutConstraints;
749 std::vector<llvm::Value*> InOutArgs;
750 std::vector<const llvm::Type*> InOutArgTypes;
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000751
752 for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) {
753 std::string OutputConstraint(S.getOutputConstraint(i)->getStrData(),
754 S.getOutputConstraint(i)->getByteLength());
755
756 TargetInfo::ConstraintInfo Info;
757 bool result = Target.validateOutputConstraint(OutputConstraint.c_str(),
758 Info);
Chris Lattner3304e552008-10-12 00:31:50 +0000759 assert(result && "Failed to parse output constraint"); result=result;
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000760
761 // Simplify the output constraint.
Lauro Ramos Venancioa5694b82008-02-26 18:33:46 +0000762 OutputConstraint = SimplifyConstraint(OutputConstraint.c_str() + 1, Target);
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000763
764 LValue Dest = EmitLValue(S.getOutputExpr(i));
765 const llvm::Type *DestValueType =
766 cast<llvm::PointerType>(Dest.getAddress()->getType())->getElementType();
767
768 // If the first output operand is not a memory dest, we'll
769 // make it the return value.
770 if (i == 0 && !(Info & TargetInfo::CI_AllowsMemory) &&
Dan Gohmand79a7262008-05-22 22:12:56 +0000771 DestValueType->isSingleValueType()) {
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000772 ResultAddr = Dest.getAddress();
773 ResultType = DestValueType;
774 Constraints += "=" + OutputConstraint;
775 } else {
776 ArgTypes.push_back(Dest.getAddress()->getType());
Anders Carlssoncad3ab62008-02-05 16:57:38 +0000777 Args.push_back(Dest.getAddress());
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000778 if (i != 0)
779 Constraints += ',';
Anders Carlssonf39a4212008-02-05 20:01:53 +0000780 Constraints += "=*";
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000781 Constraints += OutputConstraint;
Anders Carlssonf39a4212008-02-05 20:01:53 +0000782 }
783
784 if (Info & TargetInfo::CI_ReadWrite) {
785 // FIXME: This code should be shared with the code that handles inputs.
786 InOutConstraints += ',';
787
788 const Expr *InputExpr = S.getOutputExpr(i);
789 llvm::Value *Arg;
790 if ((Info & TargetInfo::CI_AllowsRegister) ||
791 !(Info & TargetInfo::CI_AllowsMemory)) {
Dan Gohmand79a7262008-05-22 22:12:56 +0000792 if (ConvertType(InputExpr->getType())->isSingleValueType()) {
Anders Carlssonf39a4212008-02-05 20:01:53 +0000793 Arg = EmitScalarExpr(InputExpr);
794 } else {
Daniel Dunbar662174c82008-08-29 17:28:43 +0000795 ErrorUnsupported(&S, "asm statement passing multiple-value types as inputs");
Anders Carlssonf39a4212008-02-05 20:01:53 +0000796 }
797 } else {
798 LValue Dest = EmitLValue(InputExpr);
799 Arg = Dest.getAddress();
800 InOutConstraints += '*';
801 }
802
803 InOutArgTypes.push_back(Arg->getType());
804 InOutArgs.push_back(Arg);
805 InOutConstraints += OutputConstraint;
806 }
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000807 }
808
809 unsigned NumConstraints = S.getNumOutputs() + S.getNumInputs();
810
811 for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) {
812 const Expr *InputExpr = S.getInputExpr(i);
813
814 std::string InputConstraint(S.getInputConstraint(i)->getStrData(),
815 S.getInputConstraint(i)->getByteLength());
816
817 TargetInfo::ConstraintInfo Info;
818 bool result = Target.validateInputConstraint(InputConstraint.c_str(),
Chris Lattner3304e552008-10-12 00:31:50 +0000819 NumConstraints, Info);
820 assert(result && "Failed to parse input constraint"); result=result;
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000821
822 if (i != 0 || S.getNumOutputs() > 0)
823 Constraints += ',';
824
825 // Simplify the input constraint.
Lauro Ramos Venancioa5694b82008-02-26 18:33:46 +0000826 InputConstraint = SimplifyConstraint(InputConstraint.c_str(), Target);
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000827
828 llvm::Value *Arg;
829
830 if ((Info & TargetInfo::CI_AllowsRegister) ||
831 !(Info & TargetInfo::CI_AllowsMemory)) {
Dan Gohmand79a7262008-05-22 22:12:56 +0000832 if (ConvertType(InputExpr->getType())->isSingleValueType()) {
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000833 Arg = EmitScalarExpr(InputExpr);
834 } else {
Daniel Dunbar662174c82008-08-29 17:28:43 +0000835 ErrorUnsupported(&S, "asm statement passing multiple-value types as inputs");
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000836 }
837 } else {
838 LValue Dest = EmitLValue(InputExpr);
839 Arg = Dest.getAddress();
840 Constraints += '*';
841 }
842
843 ArgTypes.push_back(Arg->getType());
844 Args.push_back(Arg);
845 Constraints += InputConstraint;
846 }
847
Anders Carlssonf39a4212008-02-05 20:01:53 +0000848 // Append the "input" part of inout constraints last.
849 for (unsigned i = 0, e = InOutArgs.size(); i != e; i++) {
850 ArgTypes.push_back(InOutArgTypes[i]);
851 Args.push_back(InOutArgs[i]);
852 }
853 Constraints += InOutConstraints;
854
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000855 // Clobbers
856 for (unsigned i = 0, e = S.getNumClobbers(); i != e; i++) {
857 std::string Clobber(S.getClobber(i)->getStrData(),
858 S.getClobber(i)->getByteLength());
859
860 Clobber = Target.getNormalizedGCCRegisterName(Clobber.c_str());
861
Anders Carlssonea041752008-02-06 00:11:32 +0000862 if (i != 0 || NumConstraints != 0)
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000863 Constraints += ',';
Anders Carlssonea041752008-02-06 00:11:32 +0000864
865 Constraints += "~{";
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000866 Constraints += Clobber;
Anders Carlssonea041752008-02-06 00:11:32 +0000867 Constraints += '}';
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000868 }
869
870 // Add machine specific clobbers
871 if (const char *C = Target.getClobbers()) {
872 if (!Constraints.empty())
873 Constraints += ',';
874 Constraints += C;
875 }
Anders Carlssonf39a4212008-02-05 20:01:53 +0000876
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000877 const llvm::FunctionType *FTy =
878 llvm::FunctionType::get(ResultType, ArgTypes, false);
879
880 llvm::InlineAsm *IA =
881 llvm::InlineAsm::get(FTy, AsmString, Constraints,
882 S.isVolatile() || S.getNumOutputs() == 0);
883 llvm::Value *Result = Builder.CreateCall(IA, Args.begin(), Args.end(), "");
Eli Friedman1e692ac2008-06-13 23:01:12 +0000884 if (ResultAddr) // FIXME: volatility
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000885 Builder.CreateStore(Result, ResultAddr);
886}