blob: c7ee22a2c33b98f086880bd433b8553426092939 [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) {
36 if (S->getLocStart().isValid()) {
37 DI->setLocation(S->getLocStart());
38 }
39
40 DI->EmitStopPoint(CurFn, Builder);
41 }
42
Reid Spencer5f016e22007-07-11 17:01:13 +000043 switch (S->getStmtClass()) {
44 default:
Chris Lattner1e4d21e2007-08-26 22:58:05 +000045 // Must be an expression in a stmt context. Emit the value (to get
46 // side-effects) and ignore the result.
Reid Spencer5f016e22007-07-11 17:01:13 +000047 if (const Expr *E = dyn_cast<Expr>(S)) {
Chris Lattner1e4d21e2007-08-26 22:58:05 +000048 if (!hasAggregateLLVMType(E->getType()))
49 EmitScalarExpr(E);
Chris Lattner9b2dc282008-04-04 16:54:41 +000050 else if (E->getType()->isAnyComplexType())
Chris Lattner1e4d21e2007-08-26 22:58:05 +000051 EmitComplexExpr(E);
52 else
53 EmitAggExpr(E, 0, false);
Reid Spencer5f016e22007-07-11 17:01:13 +000054 } else {
Daniel Dunbar488e9932008-08-16 00:56:44 +000055 ErrorUnsupported(S, "statement");
Reid Spencer5f016e22007-07-11 17:01:13 +000056 }
57 break;
58 case Stmt::NullStmtClass: break;
59 case Stmt::CompoundStmtClass: EmitCompoundStmt(cast<CompoundStmt>(*S)); break;
60 case Stmt::LabelStmtClass: EmitLabelStmt(cast<LabelStmt>(*S)); break;
61 case Stmt::GotoStmtClass: EmitGotoStmt(cast<GotoStmt>(*S)); break;
Daniel Dunbar0ffb1252008-08-04 16:51:22 +000062 case Stmt::IndirectGotoStmtClass:
63 EmitIndirectGotoStmt(cast<IndirectGotoStmt>(*S)); break;
Reid Spencer5f016e22007-07-11 17:01:13 +000064
65 case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break;
66 case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S)); break;
67 case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S)); break;
68 case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S)); break;
69
70 case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break;
71 case Stmt::DeclStmtClass: EmitDeclStmt(cast<DeclStmt>(*S)); break;
Chris Lattnerda138702007-07-16 21:28:45 +000072
73 case Stmt::BreakStmtClass: EmitBreakStmt(); break;
74 case Stmt::ContinueStmtClass: EmitContinueStmt(); break;
Devang Patel51b09f22007-10-04 23:45:31 +000075 case Stmt::SwitchStmtClass: EmitSwitchStmt(cast<SwitchStmt>(*S)); break;
76 case Stmt::DefaultStmtClass: EmitDefaultStmt(cast<DefaultStmt>(*S)); break;
77 case Stmt::CaseStmtClass: EmitCaseStmt(cast<CaseStmt>(*S)); break;
Anders Carlssonfb1aeb82008-02-05 16:35:33 +000078 case Stmt::AsmStmtClass: EmitAsmStmt(cast<AsmStmt>(*S)); break;
Daniel Dunbar0a04d772008-08-23 10:51:21 +000079
80 case Stmt::ObjCAtTryStmtClass:
Anders Carlsson64d5d6c2008-09-09 10:04:29 +000081 EmitObjCAtTryStmt(cast<ObjCAtTryStmt>(*S));
82 break;
Daniel Dunbar0a04d772008-08-23 10:51:21 +000083 case Stmt::ObjCAtCatchStmtClass:
Anders Carlssondde0a942008-09-11 09:15:33 +000084 assert(0 && "@catch statements should be handled by EmitObjCAtTryStmt");
85 break;
Daniel Dunbar0a04d772008-08-23 10:51:21 +000086 case Stmt::ObjCAtFinallyStmtClass:
Anders Carlsson64d5d6c2008-09-09 10:04:29 +000087 assert(0 && "@finally statements should be handled by EmitObjCAtTryStmt");
Daniel Dunbar0a04d772008-08-23 10:51:21 +000088 break;
89 case Stmt::ObjCAtThrowStmtClass:
Anders Carlsson64d5d6c2008-09-09 10:04:29 +000090 EmitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(*S));
Daniel Dunbar0a04d772008-08-23 10:51:21 +000091 break;
92 case Stmt::ObjCAtSynchronizedStmtClass:
93 ErrorUnsupported(S, "@synchronized statement");
94 break;
Anders Carlsson3d8400d2008-08-30 19:51:14 +000095 case Stmt::ObjCForCollectionStmtClass:
96 EmitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(*S));
Daniel Dunbar0a04d772008-08-23 10:51:21 +000097 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000098 }
99}
100
Chris Lattner33793202007-08-31 22:09:40 +0000101/// EmitCompoundStmt - Emit a compound statement {..} node. If GetLast is true,
102/// this captures the expression result of the last sub-statement and returns it
103/// (for use by the statement expression extension).
Chris Lattner9b655512007-08-31 22:49:20 +0000104RValue CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast,
105 llvm::Value *AggLoc, bool isAggVol) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 // FIXME: handle vla's etc.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000107 CGDebugInfo *DI = CGM.getDebugInfo();
108 if (DI) {
Chris Lattner345f7202008-07-26 20:15:14 +0000109 if (S.getLBracLoc().isValid())
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000110 DI->setLocation(S.getLBracLoc());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000111 DI->EmitRegionStart(CurFn, Builder);
112 }
113
Chris Lattner33793202007-08-31 22:09:40 +0000114 for (CompoundStmt::const_body_iterator I = S.body_begin(),
115 E = S.body_end()-GetLast; I != E; ++I)
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 EmitStmt(*I);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000117
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000118 if (DI) {
Chris Lattner345f7202008-07-26 20:15:14 +0000119 if (S.getRBracLoc().isValid())
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000120 DI->setLocation(S.getRBracLoc());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000121 DI->EmitRegionEnd(CurFn, Builder);
122 }
123
Chris Lattner33793202007-08-31 22:09:40 +0000124 if (!GetLast)
125 return RValue::get(0);
Chris Lattner9b655512007-08-31 22:49:20 +0000126
Chris Lattner91d723d2008-07-26 20:23:23 +0000127 // We have to special case labels here. They are statements, but when put at
128 // the end of a statement expression, they yield the value of their
129 // subexpression. Handle this by walking through all labels we encounter,
130 // emitting them before we evaluate the subexpr.
131 const Stmt *LastStmt = S.body_back();
132 while (const LabelStmt *LS = dyn_cast<LabelStmt>(LastStmt)) {
133 EmitLabel(*LS);
134 LastStmt = LS->getSubStmt();
135 }
136
137 return EmitAnyExpr(cast<Expr>(LastStmt), AggLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000138}
139
140void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB) {
141 // Emit a branch from this block to the next one if this was a real block. If
142 // this was just a fall-through block after a terminator, don't emit it.
143 llvm::BasicBlock *LastBB = Builder.GetInsertBlock();
144
145 if (LastBB->getTerminator()) {
146 // If the previous block is already terminated, don't touch it.
Daniel Dunbar16f23572008-07-25 01:11:38 +0000147 } else if (LastBB->empty() && isDummyBlock(LastBB)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 // If the last block was an empty placeholder, remove it now.
149 // TODO: cache and reuse these.
Daniel Dunbar16f23572008-07-25 01:11:38 +0000150 LastBB->eraseFromParent();
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 } else {
152 // Otherwise, create a fall-through branch.
153 Builder.CreateBr(BB);
154 }
155 CurFn->getBasicBlockList().push_back(BB);
156 Builder.SetInsertPoint(BB);
157}
158
Chris Lattner91d723d2008-07-26 20:23:23 +0000159void CodeGenFunction::EmitLabel(const LabelStmt &S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 llvm::BasicBlock *NextBB = getBasicBlockForLabel(&S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 EmitBlock(NextBB);
Chris Lattner91d723d2008-07-26 20:23:23 +0000162}
163
164
165void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) {
166 EmitLabel(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 EmitStmt(S.getSubStmt());
168}
169
170void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) {
171 Builder.CreateBr(getBasicBlockForLabel(S.getLabel()));
172
173 // Emit a block after the branch so that dead code after a goto has some place
174 // to go.
Gabor Greif984d0b42008-04-06 20:42:52 +0000175 Builder.SetInsertPoint(llvm::BasicBlock::Create("", CurFn));
Reid Spencer5f016e22007-07-11 17:01:13 +0000176}
177
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000178void CodeGenFunction::EmitIndirectGotoStmt(const IndirectGotoStmt &S) {
179 // Emit initial switch which will be patched up later by
180 // EmitIndirectSwitches(). We need a default dest, so we use the
181 // current BB, but this is overwritten.
182 llvm::Value *V = Builder.CreatePtrToInt(EmitScalarExpr(S.getTarget()),
183 llvm::Type::Int32Ty,
184 "addr");
185 llvm::SwitchInst *I = Builder.CreateSwitch(V, Builder.GetInsertBlock());
186 IndirectSwitches.push_back(I);
187
188 // Emit a block after the branch so that dead code after a goto has some place
189 // to go.
190 Builder.SetInsertPoint(llvm::BasicBlock::Create("", CurFn));
191}
192
Reid Spencer5f016e22007-07-11 17:01:13 +0000193void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000194 // FIXME: It would probably be nice for us to skip emission of if
195 // (0) code here.
196
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 // C99 6.8.4.1: The first substatement is executed if the expression compares
198 // unequal to 0. The condition must be a scalar type.
199 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
200
Gabor Greif984d0b42008-04-06 20:42:52 +0000201 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("ifend");
202 llvm::BasicBlock *ThenBlock = llvm::BasicBlock::Create("ifthen");
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 llvm::BasicBlock *ElseBlock = ContBlock;
204
205 if (S.getElse())
Gabor Greif984d0b42008-04-06 20:42:52 +0000206 ElseBlock = llvm::BasicBlock::Create("ifelse");
Reid Spencer5f016e22007-07-11 17:01:13 +0000207
208 // Insert the conditional branch.
209 Builder.CreateCondBr(BoolCondVal, ThenBlock, ElseBlock);
210
211 // Emit the 'then' code.
212 EmitBlock(ThenBlock);
213 EmitStmt(S.getThen());
Devang Pateld9363c32007-09-28 21:49:18 +0000214 llvm::BasicBlock *BB = Builder.GetInsertBlock();
215 if (isDummyBlock(BB)) {
216 BB->eraseFromParent();
217 Builder.SetInsertPoint(ThenBlock);
Chris Lattner345f7202008-07-26 20:15:14 +0000218 } else {
Devang Pateld9363c32007-09-28 21:49:18 +0000219 Builder.CreateBr(ContBlock);
Chris Lattner345f7202008-07-26 20:15:14 +0000220 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000221
222 // Emit the 'else' code if present.
223 if (const Stmt *Else = S.getElse()) {
224 EmitBlock(ElseBlock);
225 EmitStmt(Else);
Devang Pateld9363c32007-09-28 21:49:18 +0000226 llvm::BasicBlock *BB = Builder.GetInsertBlock();
227 if (isDummyBlock(BB)) {
228 BB->eraseFromParent();
229 Builder.SetInsertPoint(ElseBlock);
Chris Lattner345f7202008-07-26 20:15:14 +0000230 } else {
Devang Pateld9363c32007-09-28 21:49:18 +0000231 Builder.CreateBr(ContBlock);
Chris Lattner345f7202008-07-26 20:15:14 +0000232 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000233 }
234
235 // Emit the continuation block for code after the if.
236 EmitBlock(ContBlock);
237}
238
239void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 // Emit the header for the loop, insert it, which will create an uncond br to
241 // it.
Gabor Greif984d0b42008-04-06 20:42:52 +0000242 llvm::BasicBlock *LoopHeader = llvm::BasicBlock::Create("whilecond");
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 EmitBlock(LoopHeader);
244
245 // Evaluate the conditional in the while header. C99 6.8.5.1: The evaluation
246 // of the controlling expression takes place before each execution of the loop
247 // body.
248 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
Devang Patel2c30d8f2007-10-09 20:51:27 +0000249
250 // while(1) is common, avoid extra exit blocks. Be sure
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 // to correctly handle break/continue though.
Devang Patel2c30d8f2007-10-09 20:51:27 +0000252 bool EmitBoolCondBranch = true;
253 if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
254 if (C->isOne())
255 EmitBoolCondBranch = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000256
257 // Create an exit block for when the condition fails, create a block for the
258 // body of the loop.
Gabor Greif984d0b42008-04-06 20:42:52 +0000259 llvm::BasicBlock *ExitBlock = llvm::BasicBlock::Create("whileexit");
260 llvm::BasicBlock *LoopBody = llvm::BasicBlock::Create("whilebody");
Reid Spencer5f016e22007-07-11 17:01:13 +0000261
262 // As long as the condition is true, go to the loop body.
Devang Patel2c30d8f2007-10-09 20:51:27 +0000263 if (EmitBoolCondBranch)
264 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
Chris Lattnerda138702007-07-16 21:28:45 +0000265
266 // Store the blocks to use for break and continue.
267 BreakContinueStack.push_back(BreakContinue(ExitBlock, LoopHeader));
Reid Spencer5f016e22007-07-11 17:01:13 +0000268
269 // Emit the loop body.
270 EmitBlock(LoopBody);
271 EmitStmt(S.getBody());
Chris Lattnerda138702007-07-16 21:28:45 +0000272
273 BreakContinueStack.pop_back();
Reid Spencer5f016e22007-07-11 17:01:13 +0000274
275 // Cycle to the condition.
276 Builder.CreateBr(LoopHeader);
277
278 // Emit the exit block.
279 EmitBlock(ExitBlock);
Devang Patel2c30d8f2007-10-09 20:51:27 +0000280
281 // If LoopHeader is a simple forwarding block then eliminate it.
282 if (!EmitBoolCondBranch
283 && &LoopHeader->front() == LoopHeader->getTerminator()) {
284 LoopHeader->replaceAllUsesWith(LoopBody);
285 LoopHeader->getTerminator()->eraseFromParent();
286 LoopHeader->eraseFromParent();
287 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000288}
289
290void CodeGenFunction::EmitDoStmt(const DoStmt &S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 // Emit the body for the loop, insert it, which will create an uncond br to
292 // it.
Gabor Greif984d0b42008-04-06 20:42:52 +0000293 llvm::BasicBlock *LoopBody = llvm::BasicBlock::Create("dobody");
294 llvm::BasicBlock *AfterDo = llvm::BasicBlock::Create("afterdo");
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 EmitBlock(LoopBody);
Chris Lattnerda138702007-07-16 21:28:45 +0000296
Gabor Greif984d0b42008-04-06 20:42:52 +0000297 llvm::BasicBlock *DoCond = llvm::BasicBlock::Create("docond");
Chris Lattnerda138702007-07-16 21:28:45 +0000298
299 // Store the blocks to use for break and continue.
300 BreakContinueStack.push_back(BreakContinue(AfterDo, DoCond));
Reid Spencer5f016e22007-07-11 17:01:13 +0000301
302 // Emit the body of the loop into the block.
303 EmitStmt(S.getBody());
304
Chris Lattnerda138702007-07-16 21:28:45 +0000305 BreakContinueStack.pop_back();
306
307 EmitBlock(DoCond);
308
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 // C99 6.8.5.2: "The evaluation of the controlling expression takes place
310 // after each execution of the loop body."
311
312 // Evaluate the conditional in the while header.
313 // C99 6.8.5p2/p4: The first substatement is executed if the expression
314 // compares unequal to 0. The condition must be a scalar type.
315 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
Devang Patel05f6e6b2007-10-09 20:33:39 +0000316
317 // "do {} while (0)" is common in macros, avoid extra blocks. Be sure
318 // to correctly handle break/continue though.
319 bool EmitBoolCondBranch = true;
320 if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
321 if (C->isZero())
322 EmitBoolCondBranch = false;
323
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 // As long as the condition is true, iterate the loop.
Devang Patel05f6e6b2007-10-09 20:33:39 +0000325 if (EmitBoolCondBranch)
326 Builder.CreateCondBr(BoolCondVal, LoopBody, AfterDo);
Reid Spencer5f016e22007-07-11 17:01:13 +0000327
328 // Emit the exit block.
329 EmitBlock(AfterDo);
Devang Patel05f6e6b2007-10-09 20:33:39 +0000330
331 // If DoCond is a simple forwarding block then eliminate it.
332 if (!EmitBoolCondBranch && &DoCond->front() == DoCond->getTerminator()) {
333 DoCond->replaceAllUsesWith(AfterDo);
334 DoCond->getTerminator()->eraseFromParent();
335 DoCond->eraseFromParent();
336 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000337}
338
339void CodeGenFunction::EmitForStmt(const ForStmt &S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 // FIXME: What do we do if the increment (f.e.) contains a stmt expression,
341 // which contains a continue/break?
Chris Lattnerda138702007-07-16 21:28:45 +0000342 // TODO: We could keep track of whether the loop body contains any
343 // break/continue statements and not create unnecessary blocks (like
344 // "afterfor" for a condless loop) if it doesn't.
345
Reid Spencer5f016e22007-07-11 17:01:13 +0000346 // Evaluate the first part before the loop.
347 if (S.getInit())
348 EmitStmt(S.getInit());
349
350 // Start the loop with a block that tests the condition.
Gabor Greif984d0b42008-04-06 20:42:52 +0000351 llvm::BasicBlock *CondBlock = llvm::BasicBlock::Create("forcond");
352 llvm::BasicBlock *AfterFor = llvm::BasicBlock::Create("afterfor");
Chris Lattnerda138702007-07-16 21:28:45 +0000353
Reid Spencer5f016e22007-07-11 17:01:13 +0000354 EmitBlock(CondBlock);
355
356 // Evaluate the condition if present. If not, treat it as a non-zero-constant
357 // according to 6.8.5.3p2, aka, true.
358 if (S.getCond()) {
359 // C99 6.8.5p2/p4: The first substatement is executed if the expression
360 // compares unequal to 0. The condition must be a scalar type.
361 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
362
363 // As long as the condition is true, iterate the loop.
Gabor Greif984d0b42008-04-06 20:42:52 +0000364 llvm::BasicBlock *ForBody = llvm::BasicBlock::Create("forbody");
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 Builder.CreateCondBr(BoolCondVal, ForBody, AfterFor);
366 EmitBlock(ForBody);
367 } else {
368 // Treat it as a non-zero constant. Don't even create a new block for the
369 // body, just fall into it.
370 }
371
Chris Lattnerda138702007-07-16 21:28:45 +0000372 // If the for loop doesn't have an increment we can just use the
373 // condition as the continue block.
374 llvm::BasicBlock *ContinueBlock;
375 if (S.getInc())
Gabor Greif984d0b42008-04-06 20:42:52 +0000376 ContinueBlock = llvm::BasicBlock::Create("forinc");
Chris Lattnerda138702007-07-16 21:28:45 +0000377 else
378 ContinueBlock = CondBlock;
379
380 // Store the blocks to use for break and continue.
381 BreakContinueStack.push_back(BreakContinue(AfterFor, ContinueBlock));
382
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 // If the condition is true, execute the body of the for stmt.
384 EmitStmt(S.getBody());
Chris Lattnerda138702007-07-16 21:28:45 +0000385
386 BreakContinueStack.pop_back();
387
388 if (S.getInc())
389 EmitBlock(ContinueBlock);
Reid Spencer5f016e22007-07-11 17:01:13 +0000390
391 // If there is an increment, emit it next.
392 if (S.getInc())
Chris Lattner883f6a72007-08-11 00:04:45 +0000393 EmitStmt(S.getInc());
Reid Spencer5f016e22007-07-11 17:01:13 +0000394
395 // Finally, branch back up to the condition for the next iteration.
396 Builder.CreateBr(CondBlock);
397
Chris Lattnerda138702007-07-16 21:28:45 +0000398 // Emit the fall-through block.
399 EmitBlock(AfterFor);
Reid Spencer5f016e22007-07-11 17:01:13 +0000400}
401
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +0000402void CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) {
403 if (RV.isScalar()) {
404 Builder.CreateStore(RV.getScalarVal(), ReturnValue);
405 } else if (RV.isAggregate()) {
406 EmitAggregateCopy(ReturnValue, RV.getAggregateAddr(), Ty);
407 } else {
408 StoreComplexToAddr(RV.getComplexVal(), ReturnValue, false);
409 }
410 Builder.CreateBr(ReturnBlock);
411
412 // Emit a block after the branch so that dead code after a return has some
413 // place to go.
414 EmitBlock(llvm::BasicBlock::Create());
415}
416
Reid Spencer5f016e22007-07-11 17:01:13 +0000417/// EmitReturnStmt - Note that due to GCC extensions, this can have an operand
418/// if the function returns void, or may be missing one if the function returns
419/// non-void. Fun stuff :).
420void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000421 // Emit the result value, even if unused, to evalute the side effects.
422 const Expr *RV = S.getRetValue();
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000423
424 // FIXME: Clean this up by using an LValue for ReturnTemp,
425 // EmitStoreThroughLValue, and EmitAnyExpr.
426 if (!ReturnValue) {
427 // Make sure not to return anything, but evaluate the expression
428 // for side effects.
429 if (RV)
Eli Friedman144ac612008-05-22 01:22:33 +0000430 EmitAnyExpr(RV);
Reid Spencer5f016e22007-07-11 17:01:13 +0000431 } else if (RV == 0) {
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000432 // Do nothing (return value is left uninitialized)
Chris Lattner4b0029d2007-08-26 07:14:44 +0000433 } else if (!hasAggregateLLVMType(RV->getType())) {
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000434 Builder.CreateStore(EmitScalarExpr(RV), ReturnValue);
Chris Lattner9b2dc282008-04-04 16:54:41 +0000435 } else if (RV->getType()->isAnyComplexType()) {
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000436 EmitComplexExprIntoAddr(RV, ReturnValue, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000437 } else {
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000438 EmitAggExpr(RV, ReturnValue, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000439 }
Eli Friedman144ac612008-05-22 01:22:33 +0000440
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000441 Builder.CreateBr(ReturnBlock);
Reid Spencer5f016e22007-07-11 17:01:13 +0000442
443 // Emit a block after the branch so that dead code after a return has some
444 // place to go.
Gabor Greif984d0b42008-04-06 20:42:52 +0000445 EmitBlock(llvm::BasicBlock::Create());
Reid Spencer5f016e22007-07-11 17:01:13 +0000446}
447
448void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) {
Steve Naroff94745042007-09-13 23:52:58 +0000449 for (const ScopedDecl *Decl = S.getDecl(); Decl;
450 Decl = Decl->getNextDeclarator())
Reid Spencer5f016e22007-07-11 17:01:13 +0000451 EmitDecl(*Decl);
Chris Lattner6fa5f092007-07-12 15:43:07 +0000452}
Chris Lattnerda138702007-07-16 21:28:45 +0000453
454void CodeGenFunction::EmitBreakStmt() {
455 assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!");
456
457 llvm::BasicBlock *Block = BreakContinueStack.back().BreakBlock;
458 Builder.CreateBr(Block);
Gabor Greif984d0b42008-04-06 20:42:52 +0000459 EmitBlock(llvm::BasicBlock::Create());
Chris Lattnerda138702007-07-16 21:28:45 +0000460}
461
462void CodeGenFunction::EmitContinueStmt() {
463 assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
464
465 llvm::BasicBlock *Block = BreakContinueStack.back().ContinueBlock;
466 Builder.CreateBr(Block);
Gabor Greif984d0b42008-04-06 20:42:52 +0000467 EmitBlock(llvm::BasicBlock::Create());
Chris Lattnerda138702007-07-16 21:28:45 +0000468}
Devang Patel51b09f22007-10-04 23:45:31 +0000469
Devang Patelc049e4f2007-10-08 20:57:48 +0000470/// EmitCaseStmtRange - If case statement range is not too big then
471/// add multiple cases to switch instruction, one for each value within
472/// the range. If range is too big then emit "if" condition check.
473void CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S) {
Daniel Dunbar662174c82008-08-29 17:28:43 +0000474 // FIXME: kill me with param - ddunbar
Daniel Dunbar4efde8d2008-07-24 01:18:41 +0000475 assert(S.getRHS() && "Expected RHS value in CaseStmt");
Devang Patelc049e4f2007-10-08 20:57:48 +0000476
Daniel Dunbar4efde8d2008-07-24 01:18:41 +0000477 llvm::APSInt LHS = S.getLHS()->getIntegerConstantExprValue(getContext());
478 llvm::APSInt RHS = S.getRHS()->getIntegerConstantExprValue(getContext());
479
Daniel Dunbar16f23572008-07-25 01:11:38 +0000480 // Emit the code for this case. We do this first to make sure it is
481 // properly chained from our predecessor before generating the
482 // switch machinery to enter this block.
483 StartBlock("sw.bb");
484 llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
485 EmitStmt(S.getSubStmt());
486
Daniel Dunbar4efde8d2008-07-24 01:18:41 +0000487 // If range is empty, do nothing.
488 if (LHS.isSigned() ? RHS.slt(LHS) : RHS.ult(LHS))
489 return;
Devang Patelc049e4f2007-10-08 20:57:48 +0000490
491 llvm::APInt Range = RHS - LHS;
Daniel Dunbar16f23572008-07-25 01:11:38 +0000492 // FIXME: parameters such as this should not be hardcoded.
Devang Patelc049e4f2007-10-08 20:57:48 +0000493 if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) {
494 // Range is small enough to add multiple switch instruction cases.
Daniel Dunbar4efde8d2008-07-24 01:18:41 +0000495 for (unsigned i = 0, e = Range.getZExtValue() + 1; i != e; ++i) {
Devang Patel2d79d0f2007-10-05 20:54:07 +0000496 SwitchInsn->addCase(llvm::ConstantInt::get(LHS), CaseDest);
497 LHS++;
498 }
Devang Patelc049e4f2007-10-08 20:57:48 +0000499 return;
500 }
501
Daniel Dunbar16f23572008-07-25 01:11:38 +0000502 // The range is too big. Emit "if" condition into a new block,
503 // making sure to save and restore the current insertion point.
504 llvm::BasicBlock *RestoreBB = Builder.GetInsertBlock();
Devang Patel2d79d0f2007-10-05 20:54:07 +0000505
Daniel Dunbar16f23572008-07-25 01:11:38 +0000506 // Push this test onto the chain of range checks (which terminates
507 // in the default basic block). The switch's default will be changed
508 // to the top of this chain after switch emission is complete.
509 llvm::BasicBlock *FalseDest = CaseRangeBlock;
510 CaseRangeBlock = llvm::BasicBlock::Create("sw.caserange");
Devang Patelc049e4f2007-10-08 20:57:48 +0000511
Daniel Dunbar16f23572008-07-25 01:11:38 +0000512 CurFn->getBasicBlockList().push_back(CaseRangeBlock);
513 Builder.SetInsertPoint(CaseRangeBlock);
Devang Patelc049e4f2007-10-08 20:57:48 +0000514
515 // Emit range check.
516 llvm::Value *Diff =
Daniel Dunbar4efde8d2008-07-24 01:18:41 +0000517 Builder.CreateSub(SwitchInsn->getCondition(), llvm::ConstantInt::get(LHS),
518 "tmp");
Devang Patelc049e4f2007-10-08 20:57:48 +0000519 llvm::Value *Cond =
520 Builder.CreateICmpULE(Diff, llvm::ConstantInt::get(Range), "tmp");
521 Builder.CreateCondBr(Cond, CaseDest, FalseDest);
522
Daniel Dunbar16f23572008-07-25 01:11:38 +0000523 // Restore the appropriate insertion point.
524 Builder.SetInsertPoint(RestoreBB);
Devang Patelc049e4f2007-10-08 20:57:48 +0000525}
526
527void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) {
528 if (S.getRHS()) {
529 EmitCaseStmtRange(S);
530 return;
531 }
532
533 StartBlock("sw.bb");
534 llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
Daniel Dunbar4efde8d2008-07-24 01:18:41 +0000535 llvm::APSInt CaseVal = S.getLHS()->getIntegerConstantExprValue(getContext());
536 SwitchInsn->addCase(llvm::ConstantInt::get(CaseVal),
537 CaseDest);
Devang Patel51b09f22007-10-04 23:45:31 +0000538 EmitStmt(S.getSubStmt());
539}
540
541void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) {
Daniel Dunbar16f23572008-07-25 01:11:38 +0000542 llvm::BasicBlock *DefaultBlock = SwitchInsn->getDefaultDest();
543 assert(DefaultBlock->empty() && "EmitDefaultStmt: Default block already defined?");
544 EmitBlock(DefaultBlock);
Devang Patel51b09f22007-10-04 23:45:31 +0000545 EmitStmt(S.getSubStmt());
546}
547
548void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
549 llvm::Value *CondV = EmitScalarExpr(S.getCond());
550
551 // Handle nested switch statements.
552 llvm::SwitchInst *SavedSwitchInsn = SwitchInsn;
Devang Patelc049e4f2007-10-08 20:57:48 +0000553 llvm::BasicBlock *SavedCRBlock = CaseRangeBlock;
Devang Patel51b09f22007-10-04 23:45:31 +0000554
Daniel Dunbar16f23572008-07-25 01:11:38 +0000555 // Create basic block to hold stuff that comes after switch
556 // statement. We also need to create a default block now so that
557 // explicit case ranges tests can have a place to jump to on
558 // failure.
559 llvm::BasicBlock *NextBlock = llvm::BasicBlock::Create("sw.epilog");
560 llvm::BasicBlock *DefaultBlock = llvm::BasicBlock::Create("sw.default");
561 SwitchInsn = Builder.CreateSwitch(CondV, DefaultBlock);
562 CaseRangeBlock = DefaultBlock;
Devang Patel51b09f22007-10-04 23:45:31 +0000563
Eli Friedmand28a80d2008-05-12 16:08:04 +0000564 // Create basic block for body of switch
Daniel Dunbar16f23572008-07-25 01:11:38 +0000565 StartBlock("sw.body");
Eli Friedmand28a80d2008-05-12 16:08:04 +0000566
Devang Patele9b8c0a2007-10-30 20:59:40 +0000567 // All break statements jump to NextBlock. If BreakContinueStack is non empty
568 // then reuse last ContinueBlock.
Devang Patel51b09f22007-10-04 23:45:31 +0000569 llvm::BasicBlock *ContinueBlock = NULL;
570 if (!BreakContinueStack.empty())
571 ContinueBlock = BreakContinueStack.back().ContinueBlock;
572 BreakContinueStack.push_back(BreakContinue(NextBlock, ContinueBlock));
573
574 // Emit switch body.
575 EmitStmt(S.getBody());
576 BreakContinueStack.pop_back();
577
Daniel Dunbar16f23572008-07-25 01:11:38 +0000578 // Update the default block in case explicit case range tests have
579 // been chained on top.
580 SwitchInsn->setSuccessor(0, CaseRangeBlock);
Devang Patelc049e4f2007-10-08 20:57:48 +0000581
Daniel Dunbar16f23572008-07-25 01:11:38 +0000582 // If a default was never emitted then reroute any jumps to it and
583 // discard.
584 if (!DefaultBlock->getParent()) {
585 DefaultBlock->replaceAllUsesWith(NextBlock);
586 delete DefaultBlock;
587 }
Devang Patel51b09f22007-10-04 23:45:31 +0000588
Daniel Dunbar16f23572008-07-25 01:11:38 +0000589 // Emit continuation.
590 EmitBlock(NextBlock);
591
Devang Patel51b09f22007-10-04 23:45:31 +0000592 SwitchInsn = SavedSwitchInsn;
Devang Patelc049e4f2007-10-08 20:57:48 +0000593 CaseRangeBlock = SavedCRBlock;
Devang Patel51b09f22007-10-04 23:45:31 +0000594}
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000595
Chris Lattner345f7202008-07-26 20:15:14 +0000596static std::string ConvertAsmString(const char *Start, unsigned NumOperands,
597 bool IsSimple) {
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000598 static unsigned AsmCounter = 0;
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000599 AsmCounter++;
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000600 std::string Result;
Anders Carlsson2abd25f2008-02-05 23:18:57 +0000601 if (IsSimple) {
602 while (*Start) {
603 switch (*Start) {
604 default:
605 Result += *Start;
606 break;
607 case '$':
608 Result += "$$";
609 break;
610 }
Anders Carlsson2abd25f2008-02-05 23:18:57 +0000611 Start++;
612 }
613
614 return Result;
615 }
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000616
617 while (*Start) {
618 switch (*Start) {
619 default:
620 Result += *Start;
621 break;
622 case '$':
623 Result += "$$";
624 break;
625 case '%':
626 // Escaped character
627 Start++;
628 if (!*Start) {
629 // FIXME: This should be caught during Sema.
630 assert(0 && "Trailing '%' in asm string.");
631 }
632
633 char EscapedChar = *Start;
634 if (EscapedChar == '%') {
635 // Escaped percentage sign.
636 Result += '%';
Chris Lattner345f7202008-07-26 20:15:14 +0000637 } else if (EscapedChar == '=') {
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000638 // Generate an unique ID.
639 Result += llvm::utostr(AsmCounter);
640 } else if (isdigit(EscapedChar)) {
641 // %n - Assembler operand n
642 char *End;
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000643 unsigned long n = strtoul(Start, &End, 10);
644 if (Start == End) {
645 // FIXME: This should be caught during Sema.
646 assert(0 && "Missing operand!");
647 } else if (n >= NumOperands) {
648 // FIXME: This should be caught during Sema.
649 assert(0 && "Operand number out of range!");
650 }
651
652 Result += '$' + llvm::utostr(n);
Lauro Ramos Venancio7695f702008-02-26 19:19:58 +0000653 Start = End - 1;
Anders Carlsson2abd25f2008-02-05 23:18:57 +0000654 } else if (isalpha(EscapedChar)) {
655 char *End;
656
657 unsigned long n = strtoul(Start + 1, &End, 10);
658 if (Start == End) {
659 // FIXME: This should be caught during Sema.
660 assert(0 && "Missing operand!");
661 } else if (n >= NumOperands) {
662 // FIXME: This should be caught during Sema.
663 assert(0 && "Operand number out of range!");
664 }
665
666 Result += "${" + llvm::utostr(n) + ':' + EscapedChar + '}';
Lauro Ramos Venancio7695f702008-02-26 19:19:58 +0000667 Start = End - 1;
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000668 } else {
669 assert(0 && "Unhandled asm escaped character!");
670 }
671 }
672 Start++;
673 }
674
675 return Result;
676}
677
Lauro Ramos Venancioa5694b82008-02-26 18:33:46 +0000678static std::string SimplifyConstraint(const char* Constraint,
679 TargetInfo &Target) {
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000680 std::string Result;
681
682 while (*Constraint) {
683 switch (*Constraint) {
684 default:
Lauro Ramos Venancioa5694b82008-02-26 18:33:46 +0000685 Result += Target.convertConstraint(*Constraint);
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000686 break;
687 // Ignore these
688 case '*':
689 case '?':
690 case '!':
691 break;
692 case 'g':
693 Result += "imr";
694 break;
695 }
696
697 Constraint++;
698 }
699
700 return Result;
701}
702
703void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) {
704 std::string AsmString =
705 ConvertAsmString(std::string(S.getAsmString()->getStrData(),
706 S.getAsmString()->getByteLength()).c_str(),
Anders Carlsson2abd25f2008-02-05 23:18:57 +0000707 S.getNumOutputs() + S.getNumInputs(), S.isSimple());
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000708
709 std::string Constraints;
710
711 llvm::Value *ResultAddr = 0;
712 const llvm::Type *ResultType = llvm::Type::VoidTy;
713
714 std::vector<const llvm::Type*> ArgTypes;
715 std::vector<llvm::Value*> Args;
Anders Carlssonf39a4212008-02-05 20:01:53 +0000716
717 // Keep track of inout constraints.
718 std::string InOutConstraints;
719 std::vector<llvm::Value*> InOutArgs;
720 std::vector<const llvm::Type*> InOutArgTypes;
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000721
722 for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) {
723 std::string OutputConstraint(S.getOutputConstraint(i)->getStrData(),
724 S.getOutputConstraint(i)->getByteLength());
725
726 TargetInfo::ConstraintInfo Info;
727 bool result = Target.validateOutputConstraint(OutputConstraint.c_str(),
728 Info);
729 assert(result && "Failed to parse output constraint");
730
731 // Simplify the output constraint.
Lauro Ramos Venancioa5694b82008-02-26 18:33:46 +0000732 OutputConstraint = SimplifyConstraint(OutputConstraint.c_str() + 1, Target);
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000733
734 LValue Dest = EmitLValue(S.getOutputExpr(i));
735 const llvm::Type *DestValueType =
736 cast<llvm::PointerType>(Dest.getAddress()->getType())->getElementType();
737
738 // If the first output operand is not a memory dest, we'll
739 // make it the return value.
740 if (i == 0 && !(Info & TargetInfo::CI_AllowsMemory) &&
Dan Gohmand79a7262008-05-22 22:12:56 +0000741 DestValueType->isSingleValueType()) {
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000742 ResultAddr = Dest.getAddress();
743 ResultType = DestValueType;
744 Constraints += "=" + OutputConstraint;
745 } else {
746 ArgTypes.push_back(Dest.getAddress()->getType());
Anders Carlssoncad3ab62008-02-05 16:57:38 +0000747 Args.push_back(Dest.getAddress());
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000748 if (i != 0)
749 Constraints += ',';
Anders Carlssonf39a4212008-02-05 20:01:53 +0000750 Constraints += "=*";
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000751 Constraints += OutputConstraint;
Anders Carlssonf39a4212008-02-05 20:01:53 +0000752 }
753
754 if (Info & TargetInfo::CI_ReadWrite) {
755 // FIXME: This code should be shared with the code that handles inputs.
756 InOutConstraints += ',';
757
758 const Expr *InputExpr = S.getOutputExpr(i);
759 llvm::Value *Arg;
760 if ((Info & TargetInfo::CI_AllowsRegister) ||
761 !(Info & TargetInfo::CI_AllowsMemory)) {
Dan Gohmand79a7262008-05-22 22:12:56 +0000762 if (ConvertType(InputExpr->getType())->isSingleValueType()) {
Anders Carlssonf39a4212008-02-05 20:01:53 +0000763 Arg = EmitScalarExpr(InputExpr);
764 } else {
Daniel Dunbar662174c82008-08-29 17:28:43 +0000765 ErrorUnsupported(&S, "asm statement passing multiple-value types as inputs");
Anders Carlssonf39a4212008-02-05 20:01:53 +0000766 }
767 } else {
768 LValue Dest = EmitLValue(InputExpr);
769 Arg = Dest.getAddress();
770 InOutConstraints += '*';
771 }
772
773 InOutArgTypes.push_back(Arg->getType());
774 InOutArgs.push_back(Arg);
775 InOutConstraints += OutputConstraint;
776 }
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000777 }
778
779 unsigned NumConstraints = S.getNumOutputs() + S.getNumInputs();
780
781 for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) {
782 const Expr *InputExpr = S.getInputExpr(i);
783
784 std::string InputConstraint(S.getInputConstraint(i)->getStrData(),
785 S.getInputConstraint(i)->getByteLength());
786
787 TargetInfo::ConstraintInfo Info;
788 bool result = Target.validateInputConstraint(InputConstraint.c_str(),
789 NumConstraints,
790 Info);
791 assert(result && "Failed to parse input constraint");
792
793 if (i != 0 || S.getNumOutputs() > 0)
794 Constraints += ',';
795
796 // Simplify the input constraint.
Lauro Ramos Venancioa5694b82008-02-26 18:33:46 +0000797 InputConstraint = SimplifyConstraint(InputConstraint.c_str(), Target);
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000798
799 llvm::Value *Arg;
800
801 if ((Info & TargetInfo::CI_AllowsRegister) ||
802 !(Info & TargetInfo::CI_AllowsMemory)) {
Dan Gohmand79a7262008-05-22 22:12:56 +0000803 if (ConvertType(InputExpr->getType())->isSingleValueType()) {
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000804 Arg = EmitScalarExpr(InputExpr);
805 } else {
Daniel Dunbar662174c82008-08-29 17:28:43 +0000806 ErrorUnsupported(&S, "asm statement passing multiple-value types as inputs");
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000807 }
808 } else {
809 LValue Dest = EmitLValue(InputExpr);
810 Arg = Dest.getAddress();
811 Constraints += '*';
812 }
813
814 ArgTypes.push_back(Arg->getType());
815 Args.push_back(Arg);
816 Constraints += InputConstraint;
817 }
818
Anders Carlssonf39a4212008-02-05 20:01:53 +0000819 // Append the "input" part of inout constraints last.
820 for (unsigned i = 0, e = InOutArgs.size(); i != e; i++) {
821 ArgTypes.push_back(InOutArgTypes[i]);
822 Args.push_back(InOutArgs[i]);
823 }
824 Constraints += InOutConstraints;
825
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000826 // Clobbers
827 for (unsigned i = 0, e = S.getNumClobbers(); i != e; i++) {
828 std::string Clobber(S.getClobber(i)->getStrData(),
829 S.getClobber(i)->getByteLength());
830
831 Clobber = Target.getNormalizedGCCRegisterName(Clobber.c_str());
832
Anders Carlssonea041752008-02-06 00:11:32 +0000833 if (i != 0 || NumConstraints != 0)
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000834 Constraints += ',';
Anders Carlssonea041752008-02-06 00:11:32 +0000835
836 Constraints += "~{";
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000837 Constraints += Clobber;
Anders Carlssonea041752008-02-06 00:11:32 +0000838 Constraints += '}';
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000839 }
840
841 // Add machine specific clobbers
842 if (const char *C = Target.getClobbers()) {
843 if (!Constraints.empty())
844 Constraints += ',';
845 Constraints += C;
846 }
Anders Carlssonf39a4212008-02-05 20:01:53 +0000847
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000848 const llvm::FunctionType *FTy =
849 llvm::FunctionType::get(ResultType, ArgTypes, false);
850
851 llvm::InlineAsm *IA =
852 llvm::InlineAsm::get(FTy, AsmString, Constraints,
853 S.isVolatile() || S.getNumOutputs() == 0);
854 llvm::Value *Result = Builder.CreateCall(IA, Args.begin(), Args.end(), "");
Eli Friedman1e692ac2008-06-13 23:01:12 +0000855 if (ResultAddr) // FIXME: volatility
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000856 Builder.CreateStore(Result, ResultAddr);
857}