blob: 96550d6d5323a807c90fec320f9ffbf726e0b216 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- CGStmt.cpp - Emit LLVM Code from Statements ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Stmt nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000014#include "CGDebugInfo.h"
15#include "CodeGenModule.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "CodeGenFunction.h"
Daniel Dunbareee5cd12008-08-11 05:00:27 +000017#include "clang/AST/StmtVisitor.h"
Anders Carlssonaf6a6c22008-02-05 16:35:33 +000018#include "clang/Basic/TargetInfo.h"
Anders Carlssonaf6a6c22008-02-05 16:35:33 +000019#include "llvm/InlineAsm.h"
20#include "llvm/ADT/StringExtras.h"
Chris Lattner4b009652007-07-25 00:24:17 +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 Gupta40e56a12008-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
Chris Lattner4b009652007-07-25 00:24:17 +000043 switch (S->getStmtClass()) {
44 default:
Chris Lattner35055b82007-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.
Chris Lattner4b009652007-07-25 00:24:17 +000047 if (const Expr *E = dyn_cast<Expr>(S)) {
Chris Lattner35055b82007-08-26 22:58:05 +000048 if (!hasAggregateLLVMType(E->getType()))
49 EmitScalarExpr(E);
Chris Lattnerde0908b2008-04-04 16:54:41 +000050 else if (E->getType()->isAnyComplexType())
Chris Lattner35055b82007-08-26 22:58:05 +000051 EmitComplexExpr(E);
52 else
53 EmitAggExpr(E, 0, false);
Chris Lattner4b009652007-07-25 00:24:17 +000054 } else {
Daniel Dunbar9503b782008-08-16 00:56:44 +000055 ErrorUnsupported(S, "statement");
Chris Lattner4b009652007-07-25 00:24:17 +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 Dunbar879788d2008-08-04 16:51:22 +000062 case Stmt::IndirectGotoStmtClass:
63 EmitIndirectGotoStmt(cast<IndirectGotoStmt>(*S)); break;
Chris Lattner4b009652007-07-25 00:24:17 +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;
72
73 case Stmt::BreakStmtClass: EmitBreakStmt(); break;
74 case Stmt::ContinueStmtClass: EmitContinueStmt(); break;
Devang Patele58e0802007-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 Carlssonaf6a6c22008-02-05 16:35:33 +000078 case Stmt::AsmStmtClass: EmitAsmStmt(cast<AsmStmt>(*S)); break;
Daniel Dunbar5e105892008-08-23 10:51:21 +000079
80 case Stmt::ObjCAtTryStmtClass:
Anders Carlssonb01a2112008-09-09 10:04:29 +000081 EmitObjCAtTryStmt(cast<ObjCAtTryStmt>(*S));
82 break;
Daniel Dunbar5e105892008-08-23 10:51:21 +000083 case Stmt::ObjCAtCatchStmtClass:
Anders Carlsson75d86732008-09-11 09:15:33 +000084 assert(0 && "@catch statements should be handled by EmitObjCAtTryStmt");
85 break;
Daniel Dunbar5e105892008-08-23 10:51:21 +000086 case Stmt::ObjCAtFinallyStmtClass:
Anders Carlssonb01a2112008-09-09 10:04:29 +000087 assert(0 && "@finally statements should be handled by EmitObjCAtTryStmt");
Daniel Dunbar5e105892008-08-23 10:51:21 +000088 break;
89 case Stmt::ObjCAtThrowStmtClass:
Anders Carlssonb01a2112008-09-09 10:04:29 +000090 EmitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(*S));
Daniel Dunbar5e105892008-08-23 10:51:21 +000091 break;
92 case Stmt::ObjCAtSynchronizedStmtClass:
93 ErrorUnsupported(S, "@synchronized statement");
94 break;
Anders Carlsson82b0d0c2008-08-30 19:51:14 +000095 case Stmt::ObjCForCollectionStmtClass:
96 EmitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(*S));
Daniel Dunbar5e105892008-08-23 10:51:21 +000097 break;
Chris Lattner4b009652007-07-25 00:24:17 +000098 }
99}
100
Chris Lattnerea6cdd72007-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 Lattnere24c4cf2007-08-31 22:49:20 +0000104RValue CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast,
105 llvm::Value *AggLoc, bool isAggVol) {
Chris Lattner4b009652007-07-25 00:24:17 +0000106 // FIXME: handle vla's etc.
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000107 CGDebugInfo *DI = CGM.getDebugInfo();
108 if (DI) {
Chris Lattnera23eb7b2008-07-26 20:15:14 +0000109 if (S.getLBracLoc().isValid())
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000110 DI->setLocation(S.getLBracLoc());
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000111 DI->EmitRegionStart(CurFn, Builder);
112 }
113
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000114 for (CompoundStmt::const_body_iterator I = S.body_begin(),
115 E = S.body_end()-GetLast; I != E; ++I)
Chris Lattner4b009652007-07-25 00:24:17 +0000116 EmitStmt(*I);
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000117
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000118 if (DI) {
Chris Lattnera23eb7b2008-07-26 20:15:14 +0000119 if (S.getRBracLoc().isValid())
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000120 DI->setLocation(S.getRBracLoc());
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000121 DI->EmitRegionEnd(CurFn, Builder);
122 }
123
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000124 if (!GetLast)
125 return RValue::get(0);
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000126
Chris Lattner09cee852008-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);
Chris Lattner4b009652007-07-25 00:24:17 +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 Dunbar7a7458c2008-07-25 01:11:38 +0000147 } else if (LastBB->empty() && isDummyBlock(LastBB)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000148 // If the last block was an empty placeholder, remove it now.
149 // TODO: cache and reuse these.
Daniel Dunbar7a7458c2008-07-25 01:11:38 +0000150 LastBB->eraseFromParent();
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner09cee852008-07-26 20:23:23 +0000159void CodeGenFunction::EmitLabel(const LabelStmt &S) {
Chris Lattner4b009652007-07-25 00:24:17 +0000160 llvm::BasicBlock *NextBB = getBasicBlockForLabel(&S);
Chris Lattner4b009652007-07-25 00:24:17 +0000161 EmitBlock(NextBB);
Chris Lattner09cee852008-07-26 20:23:23 +0000162}
163
164
165void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) {
166 EmitLabel(S);
Chris Lattner4b009652007-07-25 00:24:17 +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 Greif815e2c12008-04-06 20:42:52 +0000175 Builder.SetInsertPoint(llvm::BasicBlock::Create("", CurFn));
Chris Lattner4b009652007-07-25 00:24:17 +0000176}
177
Daniel Dunbar879788d2008-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
Chris Lattner4b009652007-07-25 00:24:17 +0000193void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
Daniel Dunbar879788d2008-08-04 16:51:22 +0000194 // FIXME: It would probably be nice for us to skip emission of if
195 // (0) code here.
196
Chris Lattner4b009652007-07-25 00:24:17 +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 Greif815e2c12008-04-06 20:42:52 +0000201 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("ifend");
202 llvm::BasicBlock *ThenBlock = llvm::BasicBlock::Create("ifthen");
Chris Lattner4b009652007-07-25 00:24:17 +0000203 llvm::BasicBlock *ElseBlock = ContBlock;
204
205 if (S.getElse())
Gabor Greif815e2c12008-04-06 20:42:52 +0000206 ElseBlock = llvm::BasicBlock::Create("ifelse");
Chris Lattner4b009652007-07-25 00:24:17 +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 Patel97299362007-09-28 21:49:18 +0000214 llvm::BasicBlock *BB = Builder.GetInsertBlock();
215 if (isDummyBlock(BB)) {
216 BB->eraseFromParent();
217 Builder.SetInsertPoint(ThenBlock);
Chris Lattnera23eb7b2008-07-26 20:15:14 +0000218 } else {
Devang Patel97299362007-09-28 21:49:18 +0000219 Builder.CreateBr(ContBlock);
Chris Lattnera23eb7b2008-07-26 20:15:14 +0000220 }
Chris Lattner4b009652007-07-25 00:24:17 +0000221
222 // Emit the 'else' code if present.
223 if (const Stmt *Else = S.getElse()) {
224 EmitBlock(ElseBlock);
225 EmitStmt(Else);
Devang Patel97299362007-09-28 21:49:18 +0000226 llvm::BasicBlock *BB = Builder.GetInsertBlock();
227 if (isDummyBlock(BB)) {
228 BB->eraseFromParent();
229 Builder.SetInsertPoint(ElseBlock);
Chris Lattnera23eb7b2008-07-26 20:15:14 +0000230 } else {
Devang Patel97299362007-09-28 21:49:18 +0000231 Builder.CreateBr(ContBlock);
Chris Lattnera23eb7b2008-07-26 20:15:14 +0000232 }
Chris Lattner4b009652007-07-25 00:24:17 +0000233 }
234
235 // Emit the continuation block for code after the if.
236 EmitBlock(ContBlock);
237}
238
239void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) {
240 // Emit the header for the loop, insert it, which will create an uncond br to
241 // it.
Gabor Greif815e2c12008-04-06 20:42:52 +0000242 llvm::BasicBlock *LoopHeader = llvm::BasicBlock::Create("whilecond");
Chris Lattner4b009652007-07-25 00:24:17 +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 Patel706f8442007-10-09 20:51:27 +0000249
250 // while(1) is common, avoid extra exit blocks. Be sure
Chris Lattner4b009652007-07-25 00:24:17 +0000251 // to correctly handle break/continue though.
Devang Patel706f8442007-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;
Chris Lattner4b009652007-07-25 00:24:17 +0000256
257 // Create an exit block for when the condition fails, create a block for the
258 // body of the loop.
Gabor Greif815e2c12008-04-06 20:42:52 +0000259 llvm::BasicBlock *ExitBlock = llvm::BasicBlock::Create("whileexit");
260 llvm::BasicBlock *LoopBody = llvm::BasicBlock::Create("whilebody");
Chris Lattner4b009652007-07-25 00:24:17 +0000261
262 // As long as the condition is true, go to the loop body.
Devang Patel706f8442007-10-09 20:51:27 +0000263 if (EmitBoolCondBranch)
264 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
Chris Lattner4b009652007-07-25 00:24:17 +0000265
266 // Store the blocks to use for break and continue.
267 BreakContinueStack.push_back(BreakContinue(ExitBlock, LoopHeader));
268
269 // Emit the loop body.
270 EmitBlock(LoopBody);
271 EmitStmt(S.getBody());
272
273 BreakContinueStack.pop_back();
274
275 // Cycle to the condition.
276 Builder.CreateBr(LoopHeader);
277
278 // Emit the exit block.
279 EmitBlock(ExitBlock);
Devang Patel706f8442007-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 }
Chris Lattner4b009652007-07-25 00:24:17 +0000288}
289
290void CodeGenFunction::EmitDoStmt(const DoStmt &S) {
Chris Lattner4b009652007-07-25 00:24:17 +0000291 // Emit the body for the loop, insert it, which will create an uncond br to
292 // it.
Gabor Greif815e2c12008-04-06 20:42:52 +0000293 llvm::BasicBlock *LoopBody = llvm::BasicBlock::Create("dobody");
294 llvm::BasicBlock *AfterDo = llvm::BasicBlock::Create("afterdo");
Chris Lattner4b009652007-07-25 00:24:17 +0000295 EmitBlock(LoopBody);
296
Gabor Greif815e2c12008-04-06 20:42:52 +0000297 llvm::BasicBlock *DoCond = llvm::BasicBlock::Create("docond");
Chris Lattner4b009652007-07-25 00:24:17 +0000298
299 // Store the blocks to use for break and continue.
300 BreakContinueStack.push_back(BreakContinue(AfterDo, DoCond));
301
302 // Emit the body of the loop into the block.
303 EmitStmt(S.getBody());
304
305 BreakContinueStack.pop_back();
306
307 EmitBlock(DoCond);
308
309 // 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 Patel716d02c2007-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
Chris Lattner4b009652007-07-25 00:24:17 +0000324 // As long as the condition is true, iterate the loop.
Devang Patel716d02c2007-10-09 20:33:39 +0000325 if (EmitBoolCondBranch)
326 Builder.CreateCondBr(BoolCondVal, LoopBody, AfterDo);
Chris Lattner4b009652007-07-25 00:24:17 +0000327
328 // Emit the exit block.
329 EmitBlock(AfterDo);
Devang Patel716d02c2007-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 }
Chris Lattner4b009652007-07-25 00:24:17 +0000337}
338
339void CodeGenFunction::EmitForStmt(const ForStmt &S) {
340 // FIXME: What do we do if the increment (f.e.) contains a stmt expression,
341 // which contains a continue/break?
342 // 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
346 // 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 Greif815e2c12008-04-06 20:42:52 +0000351 llvm::BasicBlock *CondBlock = llvm::BasicBlock::Create("forcond");
352 llvm::BasicBlock *AfterFor = llvm::BasicBlock::Create("afterfor");
Chris Lattner4b009652007-07-25 00:24:17 +0000353
354 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 Greif815e2c12008-04-06 20:42:52 +0000364 llvm::BasicBlock *ForBody = llvm::BasicBlock::Create("forbody");
Chris Lattner4b009652007-07-25 00:24:17 +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
372 // 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 Greif815e2c12008-04-06 20:42:52 +0000376 ContinueBlock = llvm::BasicBlock::Create("forinc");
Chris Lattner4b009652007-07-25 00:24:17 +0000377 else
378 ContinueBlock = CondBlock;
379
380 // Store the blocks to use for break and continue.
381 BreakContinueStack.push_back(BreakContinue(AfterFor, ContinueBlock));
382
383 // If the condition is true, execute the body of the for stmt.
384 EmitStmt(S.getBody());
385
386 BreakContinueStack.pop_back();
387
Chris Lattner4b009652007-07-25 00:24:17 +0000388 // If there is an increment, emit it next.
Daniel Dunbar8ca61b22008-09-28 00:19:22 +0000389 if (S.getInc()) {
390 EmitBlock(ContinueBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000391 EmitStmt(S.getInc());
Daniel Dunbar8ca61b22008-09-28 00:19:22 +0000392 }
Chris Lattner4b009652007-07-25 00:24:17 +0000393
394 // Finally, branch back up to the condition for the next iteration.
395 Builder.CreateBr(CondBlock);
396
397 // Emit the fall-through block.
398 EmitBlock(AfterFor);
399}
400
Daniel Dunbare856ac22008-09-24 04:00:38 +0000401void CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) {
402 if (RV.isScalar()) {
403 Builder.CreateStore(RV.getScalarVal(), ReturnValue);
404 } else if (RV.isAggregate()) {
405 EmitAggregateCopy(ReturnValue, RV.getAggregateAddr(), Ty);
406 } else {
407 StoreComplexToAddr(RV.getComplexVal(), ReturnValue, false);
408 }
409 Builder.CreateBr(ReturnBlock);
410
411 // Emit a block after the branch so that dead code after a return has some
412 // place to go.
413 EmitBlock(llvm::BasicBlock::Create());
414}
415
Chris Lattner4b009652007-07-25 00:24:17 +0000416/// EmitReturnStmt - Note that due to GCC extensions, this can have an operand
417/// if the function returns void, or may be missing one if the function returns
418/// non-void. Fun stuff :).
419void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) {
Chris Lattner4b009652007-07-25 00:24:17 +0000420 // Emit the result value, even if unused, to evalute the side effects.
421 const Expr *RV = S.getRetValue();
Daniel Dunbar9fb751f2008-09-09 21:00:17 +0000422
423 // FIXME: Clean this up by using an LValue for ReturnTemp,
424 // EmitStoreThroughLValue, and EmitAnyExpr.
425 if (!ReturnValue) {
426 // Make sure not to return anything, but evaluate the expression
427 // for side effects.
428 if (RV)
Eli Friedman56977312008-05-22 01:22:33 +0000429 EmitAnyExpr(RV);
Chris Lattner4b009652007-07-25 00:24:17 +0000430 } else if (RV == 0) {
Daniel Dunbar9fb751f2008-09-09 21:00:17 +0000431 // Do nothing (return value is left uninitialized)
Chris Lattner74b93032007-08-26 07:14:44 +0000432 } else if (!hasAggregateLLVMType(RV->getType())) {
Daniel Dunbar9fb751f2008-09-09 21:00:17 +0000433 Builder.CreateStore(EmitScalarExpr(RV), ReturnValue);
Chris Lattnerde0908b2008-04-04 16:54:41 +0000434 } else if (RV->getType()->isAnyComplexType()) {
Daniel Dunbar9fb751f2008-09-09 21:00:17 +0000435 EmitComplexExprIntoAddr(RV, ReturnValue, false);
Chris Lattner4b009652007-07-25 00:24:17 +0000436 } else {
Daniel Dunbar9fb751f2008-09-09 21:00:17 +0000437 EmitAggExpr(RV, ReturnValue, false);
Chris Lattner4b009652007-07-25 00:24:17 +0000438 }
Eli Friedman56977312008-05-22 01:22:33 +0000439
Daniel Dunbare9900eb2008-09-30 01:06:03 +0000440 if (!ObjCEHStack.empty()) {
441 for (ObjCEHStackType::reverse_iterator i = ObjCEHStack.rbegin(),
442 e = ObjCEHStack.rend(); i != e; ++i) {
443 llvm::BasicBlock *ReturnPad = llvm::BasicBlock::Create("return.pad");
444 EmitJumpThroughFinally(*i, ReturnPad);
445 EmitBlock(ReturnPad);
446 }
447 }
448
Daniel Dunbar9fb751f2008-09-09 21:00:17 +0000449 Builder.CreateBr(ReturnBlock);
Chris Lattner4b009652007-07-25 00:24:17 +0000450
451 // Emit a block after the branch so that dead code after a return has some
452 // place to go.
Gabor Greif815e2c12008-04-06 20:42:52 +0000453 EmitBlock(llvm::BasicBlock::Create());
Chris Lattner4b009652007-07-25 00:24:17 +0000454}
455
456void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) {
Steve Naroff2591e1b2007-09-13 23:52:58 +0000457 for (const ScopedDecl *Decl = S.getDecl(); Decl;
458 Decl = Decl->getNextDeclarator())
Chris Lattner4b009652007-07-25 00:24:17 +0000459 EmitDecl(*Decl);
460}
461
462void CodeGenFunction::EmitBreakStmt() {
463 assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!");
464
465 llvm::BasicBlock *Block = BreakContinueStack.back().BreakBlock;
466 Builder.CreateBr(Block);
Gabor Greif815e2c12008-04-06 20:42:52 +0000467 EmitBlock(llvm::BasicBlock::Create());
Chris Lattner4b009652007-07-25 00:24:17 +0000468}
469
470void CodeGenFunction::EmitContinueStmt() {
471 assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
472
473 llvm::BasicBlock *Block = BreakContinueStack.back().ContinueBlock;
474 Builder.CreateBr(Block);
Gabor Greif815e2c12008-04-06 20:42:52 +0000475 EmitBlock(llvm::BasicBlock::Create());
Chris Lattner4b009652007-07-25 00:24:17 +0000476}
Devang Patele58e0802007-10-04 23:45:31 +0000477
Devang Patel347ca322007-10-08 20:57:48 +0000478/// EmitCaseStmtRange - If case statement range is not too big then
479/// add multiple cases to switch instruction, one for each value within
480/// the range. If range is too big then emit "if" condition check.
481void CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S) {
Daniel Dunbar5c9ee142008-07-24 01:18:41 +0000482 assert(S.getRHS() && "Expected RHS value in CaseStmt");
Devang Patel347ca322007-10-08 20:57:48 +0000483
Daniel Dunbar5c9ee142008-07-24 01:18:41 +0000484 llvm::APSInt LHS = S.getLHS()->getIntegerConstantExprValue(getContext());
485 llvm::APSInt RHS = S.getRHS()->getIntegerConstantExprValue(getContext());
486
Daniel Dunbar7a7458c2008-07-25 01:11:38 +0000487 // Emit the code for this case. We do this first to make sure it is
488 // properly chained from our predecessor before generating the
489 // switch machinery to enter this block.
490 StartBlock("sw.bb");
491 llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
492 EmitStmt(S.getSubStmt());
493
Daniel Dunbar5c9ee142008-07-24 01:18:41 +0000494 // If range is empty, do nothing.
495 if (LHS.isSigned() ? RHS.slt(LHS) : RHS.ult(LHS))
496 return;
Devang Patel347ca322007-10-08 20:57:48 +0000497
498 llvm::APInt Range = RHS - LHS;
Daniel Dunbar7a7458c2008-07-25 01:11:38 +0000499 // FIXME: parameters such as this should not be hardcoded.
Devang Patel347ca322007-10-08 20:57:48 +0000500 if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) {
501 // Range is small enough to add multiple switch instruction cases.
Daniel Dunbar5c9ee142008-07-24 01:18:41 +0000502 for (unsigned i = 0, e = Range.getZExtValue() + 1; i != e; ++i) {
Devang Patelcf9dbf22007-10-05 20:54:07 +0000503 SwitchInsn->addCase(llvm::ConstantInt::get(LHS), CaseDest);
504 LHS++;
505 }
Devang Patel347ca322007-10-08 20:57:48 +0000506 return;
507 }
508
Daniel Dunbar7a7458c2008-07-25 01:11:38 +0000509 // The range is too big. Emit "if" condition into a new block,
510 // making sure to save and restore the current insertion point.
511 llvm::BasicBlock *RestoreBB = Builder.GetInsertBlock();
Devang Patelcf9dbf22007-10-05 20:54:07 +0000512
Daniel Dunbar7a7458c2008-07-25 01:11:38 +0000513 // Push this test onto the chain of range checks (which terminates
514 // in the default basic block). The switch's default will be changed
515 // to the top of this chain after switch emission is complete.
516 llvm::BasicBlock *FalseDest = CaseRangeBlock;
517 CaseRangeBlock = llvm::BasicBlock::Create("sw.caserange");
Devang Patel347ca322007-10-08 20:57:48 +0000518
Daniel Dunbar7a7458c2008-07-25 01:11:38 +0000519 CurFn->getBasicBlockList().push_back(CaseRangeBlock);
520 Builder.SetInsertPoint(CaseRangeBlock);
Devang Patel347ca322007-10-08 20:57:48 +0000521
522 // Emit range check.
523 llvm::Value *Diff =
Daniel Dunbar5c9ee142008-07-24 01:18:41 +0000524 Builder.CreateSub(SwitchInsn->getCondition(), llvm::ConstantInt::get(LHS),
525 "tmp");
Devang Patel347ca322007-10-08 20:57:48 +0000526 llvm::Value *Cond =
527 Builder.CreateICmpULE(Diff, llvm::ConstantInt::get(Range), "tmp");
528 Builder.CreateCondBr(Cond, CaseDest, FalseDest);
529
Daniel Dunbar7a7458c2008-07-25 01:11:38 +0000530 // Restore the appropriate insertion point.
531 Builder.SetInsertPoint(RestoreBB);
Devang Patel347ca322007-10-08 20:57:48 +0000532}
533
534void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) {
535 if (S.getRHS()) {
536 EmitCaseStmtRange(S);
537 return;
538 }
539
540 StartBlock("sw.bb");
541 llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
Daniel Dunbar5c9ee142008-07-24 01:18:41 +0000542 llvm::APSInt CaseVal = S.getLHS()->getIntegerConstantExprValue(getContext());
543 SwitchInsn->addCase(llvm::ConstantInt::get(CaseVal),
544 CaseDest);
Devang Patele58e0802007-10-04 23:45:31 +0000545 EmitStmt(S.getSubStmt());
546}
547
548void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) {
Daniel Dunbar7a7458c2008-07-25 01:11:38 +0000549 llvm::BasicBlock *DefaultBlock = SwitchInsn->getDefaultDest();
550 assert(DefaultBlock->empty() && "EmitDefaultStmt: Default block already defined?");
551 EmitBlock(DefaultBlock);
Devang Patele58e0802007-10-04 23:45:31 +0000552 EmitStmt(S.getSubStmt());
553}
554
555void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
556 llvm::Value *CondV = EmitScalarExpr(S.getCond());
557
558 // Handle nested switch statements.
559 llvm::SwitchInst *SavedSwitchInsn = SwitchInsn;
Devang Patel347ca322007-10-08 20:57:48 +0000560 llvm::BasicBlock *SavedCRBlock = CaseRangeBlock;
Devang Patele58e0802007-10-04 23:45:31 +0000561
Daniel Dunbar7a7458c2008-07-25 01:11:38 +0000562 // Create basic block to hold stuff that comes after switch
563 // statement. We also need to create a default block now so that
564 // explicit case ranges tests can have a place to jump to on
565 // failure.
566 llvm::BasicBlock *NextBlock = llvm::BasicBlock::Create("sw.epilog");
567 llvm::BasicBlock *DefaultBlock = llvm::BasicBlock::Create("sw.default");
568 SwitchInsn = Builder.CreateSwitch(CondV, DefaultBlock);
569 CaseRangeBlock = DefaultBlock;
Devang Patele58e0802007-10-04 23:45:31 +0000570
Eli Friedman51eaf1b2008-05-12 16:08:04 +0000571 // Create basic block for body of switch
Daniel Dunbar7a7458c2008-07-25 01:11:38 +0000572 StartBlock("sw.body");
Eli Friedman51eaf1b2008-05-12 16:08:04 +0000573
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000574 // All break statements jump to NextBlock. If BreakContinueStack is non empty
575 // then reuse last ContinueBlock.
Devang Patele58e0802007-10-04 23:45:31 +0000576 llvm::BasicBlock *ContinueBlock = NULL;
577 if (!BreakContinueStack.empty())
578 ContinueBlock = BreakContinueStack.back().ContinueBlock;
579 BreakContinueStack.push_back(BreakContinue(NextBlock, ContinueBlock));
580
581 // Emit switch body.
582 EmitStmt(S.getBody());
583 BreakContinueStack.pop_back();
584
Daniel Dunbar7a7458c2008-07-25 01:11:38 +0000585 // Update the default block in case explicit case range tests have
586 // been chained on top.
587 SwitchInsn->setSuccessor(0, CaseRangeBlock);
Devang Patel347ca322007-10-08 20:57:48 +0000588
Daniel Dunbar7a7458c2008-07-25 01:11:38 +0000589 // If a default was never emitted then reroute any jumps to it and
590 // discard.
591 if (!DefaultBlock->getParent()) {
592 DefaultBlock->replaceAllUsesWith(NextBlock);
593 delete DefaultBlock;
594 }
Devang Patele58e0802007-10-04 23:45:31 +0000595
Daniel Dunbar7a7458c2008-07-25 01:11:38 +0000596 // Emit continuation.
597 EmitBlock(NextBlock);
598
Devang Patele58e0802007-10-04 23:45:31 +0000599 SwitchInsn = SavedSwitchInsn;
Devang Patel347ca322007-10-08 20:57:48 +0000600 CaseRangeBlock = SavedCRBlock;
Devang Patele58e0802007-10-04 23:45:31 +0000601}
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000602
Chris Lattnera23eb7b2008-07-26 20:15:14 +0000603static std::string ConvertAsmString(const char *Start, unsigned NumOperands,
604 bool IsSimple) {
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000605 static unsigned AsmCounter = 0;
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000606 AsmCounter++;
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000607 std::string Result;
Anders Carlsson52234522008-02-05 23:18:57 +0000608 if (IsSimple) {
609 while (*Start) {
610 switch (*Start) {
611 default:
612 Result += *Start;
613 break;
614 case '$':
615 Result += "$$";
616 break;
617 }
Anders Carlsson52234522008-02-05 23:18:57 +0000618 Start++;
619 }
620
621 return Result;
622 }
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000623
624 while (*Start) {
625 switch (*Start) {
626 default:
627 Result += *Start;
628 break;
629 case '$':
630 Result += "$$";
631 break;
632 case '%':
633 // Escaped character
634 Start++;
635 if (!*Start) {
636 // FIXME: This should be caught during Sema.
637 assert(0 && "Trailing '%' in asm string.");
638 }
639
640 char EscapedChar = *Start;
641 if (EscapedChar == '%') {
642 // Escaped percentage sign.
643 Result += '%';
Chris Lattnera23eb7b2008-07-26 20:15:14 +0000644 } else if (EscapedChar == '=') {
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000645 // Generate an unique ID.
646 Result += llvm::utostr(AsmCounter);
647 } else if (isdigit(EscapedChar)) {
648 // %n - Assembler operand n
649 char *End;
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000650 unsigned long n = strtoul(Start, &End, 10);
651 if (Start == End) {
652 // FIXME: This should be caught during Sema.
653 assert(0 && "Missing operand!");
654 } else if (n >= NumOperands) {
655 // FIXME: This should be caught during Sema.
656 assert(0 && "Operand number out of range!");
657 }
658
659 Result += '$' + llvm::utostr(n);
Lauro Ramos Venancioee48f3f2008-02-26 19:19:58 +0000660 Start = End - 1;
Anders Carlsson52234522008-02-05 23:18:57 +0000661 } else if (isalpha(EscapedChar)) {
662 char *End;
663
664 unsigned long n = strtoul(Start + 1, &End, 10);
665 if (Start == End) {
666 // FIXME: This should be caught during Sema.
667 assert(0 && "Missing operand!");
668 } else if (n >= NumOperands) {
669 // FIXME: This should be caught during Sema.
670 assert(0 && "Operand number out of range!");
671 }
672
673 Result += "${" + llvm::utostr(n) + ':' + EscapedChar + '}';
Lauro Ramos Venancioee48f3f2008-02-26 19:19:58 +0000674 Start = End - 1;
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000675 } else {
676 assert(0 && "Unhandled asm escaped character!");
677 }
678 }
679 Start++;
680 }
681
682 return Result;
683}
684
Lauro Ramos Venanciobb37a042008-02-26 18:33:46 +0000685static std::string SimplifyConstraint(const char* Constraint,
686 TargetInfo &Target) {
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000687 std::string Result;
688
689 while (*Constraint) {
690 switch (*Constraint) {
691 default:
Lauro Ramos Venanciobb37a042008-02-26 18:33:46 +0000692 Result += Target.convertConstraint(*Constraint);
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000693 break;
694 // Ignore these
695 case '*':
696 case '?':
697 case '!':
698 break;
699 case 'g':
700 Result += "imr";
701 break;
702 }
703
704 Constraint++;
705 }
706
707 return Result;
708}
709
710void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) {
711 std::string AsmString =
712 ConvertAsmString(std::string(S.getAsmString()->getStrData(),
713 S.getAsmString()->getByteLength()).c_str(),
Anders Carlsson52234522008-02-05 23:18:57 +0000714 S.getNumOutputs() + S.getNumInputs(), S.isSimple());
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000715
716 std::string Constraints;
717
718 llvm::Value *ResultAddr = 0;
719 const llvm::Type *ResultType = llvm::Type::VoidTy;
720
721 std::vector<const llvm::Type*> ArgTypes;
722 std::vector<llvm::Value*> Args;
Anders Carlsson0d3019e2008-02-05 20:01:53 +0000723
724 // Keep track of inout constraints.
725 std::string InOutConstraints;
726 std::vector<llvm::Value*> InOutArgs;
727 std::vector<const llvm::Type*> InOutArgTypes;
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000728
729 for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) {
730 std::string OutputConstraint(S.getOutputConstraint(i)->getStrData(),
731 S.getOutputConstraint(i)->getByteLength());
732
733 TargetInfo::ConstraintInfo Info;
734 bool result = Target.validateOutputConstraint(OutputConstraint.c_str(),
735 Info);
736 assert(result && "Failed to parse output constraint");
737
738 // Simplify the output constraint.
Lauro Ramos Venanciobb37a042008-02-26 18:33:46 +0000739 OutputConstraint = SimplifyConstraint(OutputConstraint.c_str() + 1, Target);
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000740
741 LValue Dest = EmitLValue(S.getOutputExpr(i));
742 const llvm::Type *DestValueType =
743 cast<llvm::PointerType>(Dest.getAddress()->getType())->getElementType();
744
745 // If the first output operand is not a memory dest, we'll
746 // make it the return value.
747 if (i == 0 && !(Info & TargetInfo::CI_AllowsMemory) &&
Dan Gohman377ba9f2008-05-22 22:12:56 +0000748 DestValueType->isSingleValueType()) {
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000749 ResultAddr = Dest.getAddress();
750 ResultType = DestValueType;
751 Constraints += "=" + OutputConstraint;
752 } else {
753 ArgTypes.push_back(Dest.getAddress()->getType());
Anders Carlsson78725072008-02-05 16:57:38 +0000754 Args.push_back(Dest.getAddress());
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000755 if (i != 0)
756 Constraints += ',';
Anders Carlsson0d3019e2008-02-05 20:01:53 +0000757 Constraints += "=*";
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000758 Constraints += OutputConstraint;
Anders Carlsson0d3019e2008-02-05 20:01:53 +0000759 }
760
761 if (Info & TargetInfo::CI_ReadWrite) {
762 // FIXME: This code should be shared with the code that handles inputs.
763 InOutConstraints += ',';
764
765 const Expr *InputExpr = S.getOutputExpr(i);
766 llvm::Value *Arg;
767 if ((Info & TargetInfo::CI_AllowsRegister) ||
768 !(Info & TargetInfo::CI_AllowsMemory)) {
Dan Gohman377ba9f2008-05-22 22:12:56 +0000769 if (ConvertType(InputExpr->getType())->isSingleValueType()) {
Anders Carlsson0d3019e2008-02-05 20:01:53 +0000770 Arg = EmitScalarExpr(InputExpr);
771 } else {
Daniel Dunbar952f4732008-08-29 17:28:43 +0000772 ErrorUnsupported(&S, "asm statement passing multiple-value types as inputs");
Anders Carlsson0d3019e2008-02-05 20:01:53 +0000773 }
774 } else {
775 LValue Dest = EmitLValue(InputExpr);
776 Arg = Dest.getAddress();
777 InOutConstraints += '*';
778 }
779
780 InOutArgTypes.push_back(Arg->getType());
781 InOutArgs.push_back(Arg);
782 InOutConstraints += OutputConstraint;
783 }
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000784 }
785
786 unsigned NumConstraints = S.getNumOutputs() + S.getNumInputs();
787
788 for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) {
789 const Expr *InputExpr = S.getInputExpr(i);
790
791 std::string InputConstraint(S.getInputConstraint(i)->getStrData(),
792 S.getInputConstraint(i)->getByteLength());
793
794 TargetInfo::ConstraintInfo Info;
795 bool result = Target.validateInputConstraint(InputConstraint.c_str(),
796 NumConstraints,
797 Info);
798 assert(result && "Failed to parse input constraint");
799
800 if (i != 0 || S.getNumOutputs() > 0)
801 Constraints += ',';
802
803 // Simplify the input constraint.
Lauro Ramos Venanciobb37a042008-02-26 18:33:46 +0000804 InputConstraint = SimplifyConstraint(InputConstraint.c_str(), Target);
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000805
806 llvm::Value *Arg;
807
808 if ((Info & TargetInfo::CI_AllowsRegister) ||
809 !(Info & TargetInfo::CI_AllowsMemory)) {
Dan Gohman377ba9f2008-05-22 22:12:56 +0000810 if (ConvertType(InputExpr->getType())->isSingleValueType()) {
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000811 Arg = EmitScalarExpr(InputExpr);
812 } else {
Daniel Dunbar952f4732008-08-29 17:28:43 +0000813 ErrorUnsupported(&S, "asm statement passing multiple-value types as inputs");
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000814 }
815 } else {
816 LValue Dest = EmitLValue(InputExpr);
817 Arg = Dest.getAddress();
818 Constraints += '*';
819 }
820
821 ArgTypes.push_back(Arg->getType());
822 Args.push_back(Arg);
823 Constraints += InputConstraint;
824 }
825
Anders Carlsson0d3019e2008-02-05 20:01:53 +0000826 // Append the "input" part of inout constraints last.
827 for (unsigned i = 0, e = InOutArgs.size(); i != e; i++) {
828 ArgTypes.push_back(InOutArgTypes[i]);
829 Args.push_back(InOutArgs[i]);
830 }
831 Constraints += InOutConstraints;
832
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000833 // Clobbers
834 for (unsigned i = 0, e = S.getNumClobbers(); i != e; i++) {
835 std::string Clobber(S.getClobber(i)->getStrData(),
836 S.getClobber(i)->getByteLength());
837
838 Clobber = Target.getNormalizedGCCRegisterName(Clobber.c_str());
839
Anders Carlsson74385982008-02-06 00:11:32 +0000840 if (i != 0 || NumConstraints != 0)
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000841 Constraints += ',';
Anders Carlsson74385982008-02-06 00:11:32 +0000842
843 Constraints += "~{";
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000844 Constraints += Clobber;
Anders Carlsson74385982008-02-06 00:11:32 +0000845 Constraints += '}';
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000846 }
847
848 // Add machine specific clobbers
849 if (const char *C = Target.getClobbers()) {
850 if (!Constraints.empty())
851 Constraints += ',';
852 Constraints += C;
853 }
Anders Carlsson0d3019e2008-02-05 20:01:53 +0000854
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000855 const llvm::FunctionType *FTy =
856 llvm::FunctionType::get(ResultType, ArgTypes, false);
857
858 llvm::InlineAsm *IA =
859 llvm::InlineAsm::get(FTy, AsmString, Constraints,
860 S.isVolatile() || S.getNumOutputs() == 0);
861 llvm::Value *Result = Builder.CreateCall(IA, Args.begin(), Args.end(), "");
Eli Friedman2e630542008-06-13 23:01:12 +0000862 if (ResultAddr) // FIXME: volatility
Anders Carlssonaf6a6c22008-02-05 16:35:33 +0000863 Builder.CreateStore(Result, ResultAddr);
864}