1*0a6a1f1dSLionel Sambuc //===---- ParseStmtAsm.cpp - Assembly Statement Parser --------------------===//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc // The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*0a6a1f1dSLionel Sambuc // License. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc //
10*0a6a1f1dSLionel Sambuc // This file implements parsing for GCC and Microsoft inline assembly.
11*0a6a1f1dSLionel Sambuc //
12*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
13*0a6a1f1dSLionel Sambuc
14*0a6a1f1dSLionel Sambuc #include "clang/Parse/Parser.h"
15*0a6a1f1dSLionel Sambuc #include "RAIIObjectsForParser.h"
16*0a6a1f1dSLionel Sambuc #include "clang/AST/ASTContext.h"
17*0a6a1f1dSLionel Sambuc #include "clang/Basic/Diagnostic.h"
18*0a6a1f1dSLionel Sambuc #include "clang/Basic/TargetInfo.h"
19*0a6a1f1dSLionel Sambuc #include "llvm/ADT/SmallString.h"
20*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCAsmInfo.h"
21*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCContext.h"
22*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCInstPrinter.h"
23*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCInstrInfo.h"
24*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCObjectFileInfo.h"
25*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCParser/MCAsmParser.h"
26*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCRegisterInfo.h"
27*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCStreamer.h"
28*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCSubtargetInfo.h"
29*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCTargetAsmParser.h"
30*0a6a1f1dSLionel Sambuc #include "llvm/MC/MCTargetOptions.h"
31*0a6a1f1dSLionel Sambuc #include "llvm/Support/SourceMgr.h"
32*0a6a1f1dSLionel Sambuc #include "llvm/Support/TargetRegistry.h"
33*0a6a1f1dSLionel Sambuc #include "llvm/Support/TargetSelect.h"
34*0a6a1f1dSLionel Sambuc using namespace clang;
35*0a6a1f1dSLionel Sambuc
36*0a6a1f1dSLionel Sambuc namespace {
37*0a6a1f1dSLionel Sambuc class ClangAsmParserCallback : public llvm::MCAsmParserSemaCallback {
38*0a6a1f1dSLionel Sambuc Parser &TheParser;
39*0a6a1f1dSLionel Sambuc SourceLocation AsmLoc;
40*0a6a1f1dSLionel Sambuc StringRef AsmString;
41*0a6a1f1dSLionel Sambuc
42*0a6a1f1dSLionel Sambuc /// The tokens we streamed into AsmString and handed off to MC.
43*0a6a1f1dSLionel Sambuc ArrayRef<Token> AsmToks;
44*0a6a1f1dSLionel Sambuc
45*0a6a1f1dSLionel Sambuc /// The offset of each token in AsmToks within AsmString.
46*0a6a1f1dSLionel Sambuc ArrayRef<unsigned> AsmTokOffsets;
47*0a6a1f1dSLionel Sambuc
48*0a6a1f1dSLionel Sambuc public:
ClangAsmParserCallback(Parser & P,SourceLocation Loc,StringRef AsmString,ArrayRef<Token> Toks,ArrayRef<unsigned> Offsets)49*0a6a1f1dSLionel Sambuc ClangAsmParserCallback(Parser &P, SourceLocation Loc, StringRef AsmString,
50*0a6a1f1dSLionel Sambuc ArrayRef<Token> Toks, ArrayRef<unsigned> Offsets)
51*0a6a1f1dSLionel Sambuc : TheParser(P), AsmLoc(Loc), AsmString(AsmString), AsmToks(Toks),
52*0a6a1f1dSLionel Sambuc AsmTokOffsets(Offsets) {
53*0a6a1f1dSLionel Sambuc assert(AsmToks.size() == AsmTokOffsets.size());
54*0a6a1f1dSLionel Sambuc }
55*0a6a1f1dSLionel Sambuc
LookupInlineAsmIdentifier(StringRef & LineBuf,llvm::InlineAsmIdentifierInfo & Info,bool IsUnevaluatedContext)56*0a6a1f1dSLionel Sambuc void *LookupInlineAsmIdentifier(StringRef &LineBuf,
57*0a6a1f1dSLionel Sambuc llvm::InlineAsmIdentifierInfo &Info,
58*0a6a1f1dSLionel Sambuc bool IsUnevaluatedContext) override {
59*0a6a1f1dSLionel Sambuc // Collect the desired tokens.
60*0a6a1f1dSLionel Sambuc SmallVector<Token, 16> LineToks;
61*0a6a1f1dSLionel Sambuc const Token *FirstOrigToken = nullptr;
62*0a6a1f1dSLionel Sambuc findTokensForString(LineBuf, LineToks, FirstOrigToken);
63*0a6a1f1dSLionel Sambuc
64*0a6a1f1dSLionel Sambuc unsigned NumConsumedToks;
65*0a6a1f1dSLionel Sambuc ExprResult Result = TheParser.ParseMSAsmIdentifier(
66*0a6a1f1dSLionel Sambuc LineToks, NumConsumedToks, &Info, IsUnevaluatedContext);
67*0a6a1f1dSLionel Sambuc
68*0a6a1f1dSLionel Sambuc // If we consumed the entire line, tell MC that.
69*0a6a1f1dSLionel Sambuc // Also do this if we consumed nothing as a way of reporting failure.
70*0a6a1f1dSLionel Sambuc if (NumConsumedToks == 0 || NumConsumedToks == LineToks.size()) {
71*0a6a1f1dSLionel Sambuc // By not modifying LineBuf, we're implicitly consuming it all.
72*0a6a1f1dSLionel Sambuc
73*0a6a1f1dSLionel Sambuc // Otherwise, consume up to the original tokens.
74*0a6a1f1dSLionel Sambuc } else {
75*0a6a1f1dSLionel Sambuc assert(FirstOrigToken && "not using original tokens?");
76*0a6a1f1dSLionel Sambuc
77*0a6a1f1dSLionel Sambuc // Since we're using original tokens, apply that offset.
78*0a6a1f1dSLionel Sambuc assert(FirstOrigToken[NumConsumedToks].getLocation() ==
79*0a6a1f1dSLionel Sambuc LineToks[NumConsumedToks].getLocation());
80*0a6a1f1dSLionel Sambuc unsigned FirstIndex = FirstOrigToken - AsmToks.begin();
81*0a6a1f1dSLionel Sambuc unsigned LastIndex = FirstIndex + NumConsumedToks - 1;
82*0a6a1f1dSLionel Sambuc
83*0a6a1f1dSLionel Sambuc // The total length we've consumed is the relative offset
84*0a6a1f1dSLionel Sambuc // of the last token we consumed plus its length.
85*0a6a1f1dSLionel Sambuc unsigned TotalOffset =
86*0a6a1f1dSLionel Sambuc (AsmTokOffsets[LastIndex] + AsmToks[LastIndex].getLength() -
87*0a6a1f1dSLionel Sambuc AsmTokOffsets[FirstIndex]);
88*0a6a1f1dSLionel Sambuc LineBuf = LineBuf.substr(0, TotalOffset);
89*0a6a1f1dSLionel Sambuc }
90*0a6a1f1dSLionel Sambuc
91*0a6a1f1dSLionel Sambuc // Initialize the "decl" with the lookup result.
92*0a6a1f1dSLionel Sambuc Info.OpDecl = static_cast<void *>(Result.get());
93*0a6a1f1dSLionel Sambuc return Info.OpDecl;
94*0a6a1f1dSLionel Sambuc }
95*0a6a1f1dSLionel Sambuc
LookupInlineAsmLabel(StringRef Identifier,llvm::SourceMgr & LSM,llvm::SMLoc Location,bool Create)96*0a6a1f1dSLionel Sambuc StringRef LookupInlineAsmLabel(StringRef Identifier, llvm::SourceMgr &LSM,
97*0a6a1f1dSLionel Sambuc llvm::SMLoc Location,
98*0a6a1f1dSLionel Sambuc bool Create) override {
99*0a6a1f1dSLionel Sambuc SourceLocation Loc = translateLocation(LSM, Location);
100*0a6a1f1dSLionel Sambuc LabelDecl *Label =
101*0a6a1f1dSLionel Sambuc TheParser.getActions().GetOrCreateMSAsmLabel(Identifier, Loc, Create);
102*0a6a1f1dSLionel Sambuc return Label->getMSAsmLabel();
103*0a6a1f1dSLionel Sambuc }
104*0a6a1f1dSLionel Sambuc
LookupInlineAsmField(StringRef Base,StringRef Member,unsigned & Offset)105*0a6a1f1dSLionel Sambuc bool LookupInlineAsmField(StringRef Base, StringRef Member,
106*0a6a1f1dSLionel Sambuc unsigned &Offset) override {
107*0a6a1f1dSLionel Sambuc return TheParser.getActions().LookupInlineAsmField(Base, Member, Offset,
108*0a6a1f1dSLionel Sambuc AsmLoc);
109*0a6a1f1dSLionel Sambuc }
110*0a6a1f1dSLionel Sambuc
DiagHandlerCallback(const llvm::SMDiagnostic & D,void * Context)111*0a6a1f1dSLionel Sambuc static void DiagHandlerCallback(const llvm::SMDiagnostic &D, void *Context) {
112*0a6a1f1dSLionel Sambuc ((ClangAsmParserCallback *)Context)->handleDiagnostic(D);
113*0a6a1f1dSLionel Sambuc }
114*0a6a1f1dSLionel Sambuc
115*0a6a1f1dSLionel Sambuc private:
116*0a6a1f1dSLionel Sambuc /// Collect the appropriate tokens for the given string.
findTokensForString(StringRef Str,SmallVectorImpl<Token> & TempToks,const Token * & FirstOrigToken) const117*0a6a1f1dSLionel Sambuc void findTokensForString(StringRef Str, SmallVectorImpl<Token> &TempToks,
118*0a6a1f1dSLionel Sambuc const Token *&FirstOrigToken) const {
119*0a6a1f1dSLionel Sambuc // For now, assert that the string we're working with is a substring
120*0a6a1f1dSLionel Sambuc // of what we gave to MC. This lets us use the original tokens.
121*0a6a1f1dSLionel Sambuc assert(!std::less<const char *>()(Str.begin(), AsmString.begin()) &&
122*0a6a1f1dSLionel Sambuc !std::less<const char *>()(AsmString.end(), Str.end()));
123*0a6a1f1dSLionel Sambuc
124*0a6a1f1dSLionel Sambuc // Try to find a token whose offset matches the first token.
125*0a6a1f1dSLionel Sambuc unsigned FirstCharOffset = Str.begin() - AsmString.begin();
126*0a6a1f1dSLionel Sambuc const unsigned *FirstTokOffset = std::lower_bound(
127*0a6a1f1dSLionel Sambuc AsmTokOffsets.begin(), AsmTokOffsets.end(), FirstCharOffset);
128*0a6a1f1dSLionel Sambuc
129*0a6a1f1dSLionel Sambuc // For now, assert that the start of the string exactly
130*0a6a1f1dSLionel Sambuc // corresponds to the start of a token.
131*0a6a1f1dSLionel Sambuc assert(*FirstTokOffset == FirstCharOffset);
132*0a6a1f1dSLionel Sambuc
133*0a6a1f1dSLionel Sambuc // Use all the original tokens for this line. (We assume the
134*0a6a1f1dSLionel Sambuc // end of the line corresponds cleanly to a token break.)
135*0a6a1f1dSLionel Sambuc unsigned FirstTokIndex = FirstTokOffset - AsmTokOffsets.begin();
136*0a6a1f1dSLionel Sambuc FirstOrigToken = &AsmToks[FirstTokIndex];
137*0a6a1f1dSLionel Sambuc unsigned LastCharOffset = Str.end() - AsmString.begin();
138*0a6a1f1dSLionel Sambuc for (unsigned i = FirstTokIndex, e = AsmTokOffsets.size(); i != e; ++i) {
139*0a6a1f1dSLionel Sambuc if (AsmTokOffsets[i] >= LastCharOffset)
140*0a6a1f1dSLionel Sambuc break;
141*0a6a1f1dSLionel Sambuc TempToks.push_back(AsmToks[i]);
142*0a6a1f1dSLionel Sambuc }
143*0a6a1f1dSLionel Sambuc }
144*0a6a1f1dSLionel Sambuc
translateLocation(const llvm::SourceMgr & LSM,llvm::SMLoc SMLoc)145*0a6a1f1dSLionel Sambuc SourceLocation translateLocation(const llvm::SourceMgr &LSM, llvm::SMLoc SMLoc) {
146*0a6a1f1dSLionel Sambuc // Compute an offset into the inline asm buffer.
147*0a6a1f1dSLionel Sambuc // FIXME: This isn't right if .macro is involved (but hopefully, no
148*0a6a1f1dSLionel Sambuc // real-world code does that).
149*0a6a1f1dSLionel Sambuc const llvm::MemoryBuffer *LBuf =
150*0a6a1f1dSLionel Sambuc LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(SMLoc));
151*0a6a1f1dSLionel Sambuc unsigned Offset = SMLoc.getPointer() - LBuf->getBufferStart();
152*0a6a1f1dSLionel Sambuc
153*0a6a1f1dSLionel Sambuc // Figure out which token that offset points into.
154*0a6a1f1dSLionel Sambuc const unsigned *TokOffsetPtr =
155*0a6a1f1dSLionel Sambuc std::lower_bound(AsmTokOffsets.begin(), AsmTokOffsets.end(), Offset);
156*0a6a1f1dSLionel Sambuc unsigned TokIndex = TokOffsetPtr - AsmTokOffsets.begin();
157*0a6a1f1dSLionel Sambuc unsigned TokOffset = *TokOffsetPtr;
158*0a6a1f1dSLionel Sambuc
159*0a6a1f1dSLionel Sambuc // If we come up with an answer which seems sane, use it; otherwise,
160*0a6a1f1dSLionel Sambuc // just point at the __asm keyword.
161*0a6a1f1dSLionel Sambuc // FIXME: Assert the answer is sane once we handle .macro correctly.
162*0a6a1f1dSLionel Sambuc SourceLocation Loc = AsmLoc;
163*0a6a1f1dSLionel Sambuc if (TokIndex < AsmToks.size()) {
164*0a6a1f1dSLionel Sambuc const Token &Tok = AsmToks[TokIndex];
165*0a6a1f1dSLionel Sambuc Loc = Tok.getLocation();
166*0a6a1f1dSLionel Sambuc Loc = Loc.getLocWithOffset(Offset - TokOffset);
167*0a6a1f1dSLionel Sambuc }
168*0a6a1f1dSLionel Sambuc return Loc;
169*0a6a1f1dSLionel Sambuc }
170*0a6a1f1dSLionel Sambuc
handleDiagnostic(const llvm::SMDiagnostic & D)171*0a6a1f1dSLionel Sambuc void handleDiagnostic(const llvm::SMDiagnostic &D) {
172*0a6a1f1dSLionel Sambuc const llvm::SourceMgr &LSM = *D.getSourceMgr();
173*0a6a1f1dSLionel Sambuc SourceLocation Loc = translateLocation(LSM, D.getLoc());
174*0a6a1f1dSLionel Sambuc TheParser.Diag(Loc, diag::err_inline_ms_asm_parsing) << D.getMessage();
175*0a6a1f1dSLionel Sambuc }
176*0a6a1f1dSLionel Sambuc };
177*0a6a1f1dSLionel Sambuc }
178*0a6a1f1dSLionel Sambuc
179*0a6a1f1dSLionel Sambuc /// Parse an identifier in an MS-style inline assembly block.
180*0a6a1f1dSLionel Sambuc ///
181*0a6a1f1dSLionel Sambuc /// \param CastInfo - a void* so that we don't have to teach Parser.h
182*0a6a1f1dSLionel Sambuc /// about the actual type.
ParseMSAsmIdentifier(llvm::SmallVectorImpl<Token> & LineToks,unsigned & NumLineToksConsumed,void * CastInfo,bool IsUnevaluatedContext)183*0a6a1f1dSLionel Sambuc ExprResult Parser::ParseMSAsmIdentifier(llvm::SmallVectorImpl<Token> &LineToks,
184*0a6a1f1dSLionel Sambuc unsigned &NumLineToksConsumed,
185*0a6a1f1dSLionel Sambuc void *CastInfo,
186*0a6a1f1dSLionel Sambuc bool IsUnevaluatedContext) {
187*0a6a1f1dSLionel Sambuc llvm::InlineAsmIdentifierInfo &Info =
188*0a6a1f1dSLionel Sambuc *(llvm::InlineAsmIdentifierInfo *)CastInfo;
189*0a6a1f1dSLionel Sambuc
190*0a6a1f1dSLionel Sambuc // Push a fake token on the end so that we don't overrun the token
191*0a6a1f1dSLionel Sambuc // stream. We use ';' because it expression-parsing should never
192*0a6a1f1dSLionel Sambuc // overrun it.
193*0a6a1f1dSLionel Sambuc const tok::TokenKind EndOfStream = tok::semi;
194*0a6a1f1dSLionel Sambuc Token EndOfStreamTok;
195*0a6a1f1dSLionel Sambuc EndOfStreamTok.startToken();
196*0a6a1f1dSLionel Sambuc EndOfStreamTok.setKind(EndOfStream);
197*0a6a1f1dSLionel Sambuc LineToks.push_back(EndOfStreamTok);
198*0a6a1f1dSLionel Sambuc
199*0a6a1f1dSLionel Sambuc // Also copy the current token over.
200*0a6a1f1dSLionel Sambuc LineToks.push_back(Tok);
201*0a6a1f1dSLionel Sambuc
202*0a6a1f1dSLionel Sambuc PP.EnterTokenStream(LineToks.begin(), LineToks.size(),
203*0a6a1f1dSLionel Sambuc /*disable macros*/ true,
204*0a6a1f1dSLionel Sambuc /*owns tokens*/ false);
205*0a6a1f1dSLionel Sambuc
206*0a6a1f1dSLionel Sambuc // Clear the current token and advance to the first token in LineToks.
207*0a6a1f1dSLionel Sambuc ConsumeAnyToken();
208*0a6a1f1dSLionel Sambuc
209*0a6a1f1dSLionel Sambuc // Parse an optional scope-specifier if we're in C++.
210*0a6a1f1dSLionel Sambuc CXXScopeSpec SS;
211*0a6a1f1dSLionel Sambuc if (getLangOpts().CPlusPlus) {
212*0a6a1f1dSLionel Sambuc ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
213*0a6a1f1dSLionel Sambuc }
214*0a6a1f1dSLionel Sambuc
215*0a6a1f1dSLionel Sambuc // Require an identifier here.
216*0a6a1f1dSLionel Sambuc SourceLocation TemplateKWLoc;
217*0a6a1f1dSLionel Sambuc UnqualifiedId Id;
218*0a6a1f1dSLionel Sambuc bool Invalid =
219*0a6a1f1dSLionel Sambuc ParseUnqualifiedId(SS,
220*0a6a1f1dSLionel Sambuc /*EnteringContext=*/false,
221*0a6a1f1dSLionel Sambuc /*AllowDestructorName=*/false,
222*0a6a1f1dSLionel Sambuc /*AllowConstructorName=*/false,
223*0a6a1f1dSLionel Sambuc /*ObjectType=*/ParsedType(), TemplateKWLoc, Id);
224*0a6a1f1dSLionel Sambuc
225*0a6a1f1dSLionel Sambuc // Figure out how many tokens we are into LineToks.
226*0a6a1f1dSLionel Sambuc unsigned LineIndex = 0;
227*0a6a1f1dSLionel Sambuc if (Tok.is(EndOfStream)) {
228*0a6a1f1dSLionel Sambuc LineIndex = LineToks.size() - 2;
229*0a6a1f1dSLionel Sambuc } else {
230*0a6a1f1dSLionel Sambuc while (LineToks[LineIndex].getLocation() != Tok.getLocation()) {
231*0a6a1f1dSLionel Sambuc LineIndex++;
232*0a6a1f1dSLionel Sambuc assert(LineIndex < LineToks.size() - 2); // we added two extra tokens
233*0a6a1f1dSLionel Sambuc }
234*0a6a1f1dSLionel Sambuc }
235*0a6a1f1dSLionel Sambuc
236*0a6a1f1dSLionel Sambuc // If we've run into the poison token we inserted before, or there
237*0a6a1f1dSLionel Sambuc // was a parsing error, then claim the entire line.
238*0a6a1f1dSLionel Sambuc if (Invalid || Tok.is(EndOfStream)) {
239*0a6a1f1dSLionel Sambuc NumLineToksConsumed = LineToks.size() - 2;
240*0a6a1f1dSLionel Sambuc } else {
241*0a6a1f1dSLionel Sambuc // Otherwise, claim up to the start of the next token.
242*0a6a1f1dSLionel Sambuc NumLineToksConsumed = LineIndex;
243*0a6a1f1dSLionel Sambuc }
244*0a6a1f1dSLionel Sambuc
245*0a6a1f1dSLionel Sambuc // Finally, restore the old parsing state by consuming all the tokens we
246*0a6a1f1dSLionel Sambuc // staged before, implicitly killing off the token-lexer we pushed.
247*0a6a1f1dSLionel Sambuc for (unsigned i = 0, e = LineToks.size() - LineIndex - 2; i != e; ++i) {
248*0a6a1f1dSLionel Sambuc ConsumeAnyToken();
249*0a6a1f1dSLionel Sambuc }
250*0a6a1f1dSLionel Sambuc assert(Tok.is(EndOfStream));
251*0a6a1f1dSLionel Sambuc ConsumeToken();
252*0a6a1f1dSLionel Sambuc
253*0a6a1f1dSLionel Sambuc // Leave LineToks in its original state.
254*0a6a1f1dSLionel Sambuc LineToks.pop_back();
255*0a6a1f1dSLionel Sambuc LineToks.pop_back();
256*0a6a1f1dSLionel Sambuc
257*0a6a1f1dSLionel Sambuc // Perform the lookup.
258*0a6a1f1dSLionel Sambuc return Actions.LookupInlineAsmIdentifier(SS, TemplateKWLoc, Id, Info,
259*0a6a1f1dSLionel Sambuc IsUnevaluatedContext);
260*0a6a1f1dSLionel Sambuc }
261*0a6a1f1dSLionel Sambuc
262*0a6a1f1dSLionel Sambuc /// Turn a sequence of our tokens back into a string that we can hand
263*0a6a1f1dSLionel Sambuc /// to the MC asm parser.
buildMSAsmString(Preprocessor & PP,SourceLocation AsmLoc,ArrayRef<Token> AsmToks,SmallVectorImpl<unsigned> & TokOffsets,SmallString<512> & Asm)264*0a6a1f1dSLionel Sambuc static bool buildMSAsmString(Preprocessor &PP, SourceLocation AsmLoc,
265*0a6a1f1dSLionel Sambuc ArrayRef<Token> AsmToks,
266*0a6a1f1dSLionel Sambuc SmallVectorImpl<unsigned> &TokOffsets,
267*0a6a1f1dSLionel Sambuc SmallString<512> &Asm) {
268*0a6a1f1dSLionel Sambuc assert(!AsmToks.empty() && "Didn't expect an empty AsmToks!");
269*0a6a1f1dSLionel Sambuc
270*0a6a1f1dSLionel Sambuc // Is this the start of a new assembly statement?
271*0a6a1f1dSLionel Sambuc bool isNewStatement = true;
272*0a6a1f1dSLionel Sambuc
273*0a6a1f1dSLionel Sambuc for (unsigned i = 0, e = AsmToks.size(); i < e; ++i) {
274*0a6a1f1dSLionel Sambuc const Token &Tok = AsmToks[i];
275*0a6a1f1dSLionel Sambuc
276*0a6a1f1dSLionel Sambuc // Start each new statement with a newline and a tab.
277*0a6a1f1dSLionel Sambuc if (!isNewStatement && (Tok.is(tok::kw_asm) || Tok.isAtStartOfLine())) {
278*0a6a1f1dSLionel Sambuc Asm += "\n\t";
279*0a6a1f1dSLionel Sambuc isNewStatement = true;
280*0a6a1f1dSLionel Sambuc }
281*0a6a1f1dSLionel Sambuc
282*0a6a1f1dSLionel Sambuc // Preserve the existence of leading whitespace except at the
283*0a6a1f1dSLionel Sambuc // start of a statement.
284*0a6a1f1dSLionel Sambuc if (!isNewStatement && Tok.hasLeadingSpace())
285*0a6a1f1dSLionel Sambuc Asm += ' ';
286*0a6a1f1dSLionel Sambuc
287*0a6a1f1dSLionel Sambuc // Remember the offset of this token.
288*0a6a1f1dSLionel Sambuc TokOffsets.push_back(Asm.size());
289*0a6a1f1dSLionel Sambuc
290*0a6a1f1dSLionel Sambuc // Don't actually write '__asm' into the assembly stream.
291*0a6a1f1dSLionel Sambuc if (Tok.is(tok::kw_asm)) {
292*0a6a1f1dSLionel Sambuc // Complain about __asm at the end of the stream.
293*0a6a1f1dSLionel Sambuc if (i + 1 == e) {
294*0a6a1f1dSLionel Sambuc PP.Diag(AsmLoc, diag::err_asm_empty);
295*0a6a1f1dSLionel Sambuc return true;
296*0a6a1f1dSLionel Sambuc }
297*0a6a1f1dSLionel Sambuc
298*0a6a1f1dSLionel Sambuc continue;
299*0a6a1f1dSLionel Sambuc }
300*0a6a1f1dSLionel Sambuc
301*0a6a1f1dSLionel Sambuc // Append the spelling of the token.
302*0a6a1f1dSLionel Sambuc SmallString<32> SpellingBuffer;
303*0a6a1f1dSLionel Sambuc bool SpellingInvalid = false;
304*0a6a1f1dSLionel Sambuc Asm += PP.getSpelling(Tok, SpellingBuffer, &SpellingInvalid);
305*0a6a1f1dSLionel Sambuc assert(!SpellingInvalid && "spelling was invalid after correct parse?");
306*0a6a1f1dSLionel Sambuc
307*0a6a1f1dSLionel Sambuc // We are no longer at the start of a statement.
308*0a6a1f1dSLionel Sambuc isNewStatement = false;
309*0a6a1f1dSLionel Sambuc }
310*0a6a1f1dSLionel Sambuc
311*0a6a1f1dSLionel Sambuc // Ensure that the buffer is null-terminated.
312*0a6a1f1dSLionel Sambuc Asm.push_back('\0');
313*0a6a1f1dSLionel Sambuc Asm.pop_back();
314*0a6a1f1dSLionel Sambuc
315*0a6a1f1dSLionel Sambuc assert(TokOffsets.size() == AsmToks.size());
316*0a6a1f1dSLionel Sambuc return false;
317*0a6a1f1dSLionel Sambuc }
318*0a6a1f1dSLionel Sambuc
319*0a6a1f1dSLionel Sambuc /// ParseMicrosoftAsmStatement. When -fms-extensions/-fasm-blocks is enabled,
320*0a6a1f1dSLionel Sambuc /// this routine is called to collect the tokens for an MS asm statement.
321*0a6a1f1dSLionel Sambuc ///
322*0a6a1f1dSLionel Sambuc /// [MS] ms-asm-statement:
323*0a6a1f1dSLionel Sambuc /// ms-asm-block
324*0a6a1f1dSLionel Sambuc /// ms-asm-block ms-asm-statement
325*0a6a1f1dSLionel Sambuc ///
326*0a6a1f1dSLionel Sambuc /// [MS] ms-asm-block:
327*0a6a1f1dSLionel Sambuc /// '__asm' ms-asm-line '\n'
328*0a6a1f1dSLionel Sambuc /// '__asm' '{' ms-asm-instruction-block[opt] '}' ';'[opt]
329*0a6a1f1dSLionel Sambuc ///
330*0a6a1f1dSLionel Sambuc /// [MS] ms-asm-instruction-block
331*0a6a1f1dSLionel Sambuc /// ms-asm-line
332*0a6a1f1dSLionel Sambuc /// ms-asm-line '\n' ms-asm-instruction-block
333*0a6a1f1dSLionel Sambuc ///
ParseMicrosoftAsmStatement(SourceLocation AsmLoc)334*0a6a1f1dSLionel Sambuc StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
335*0a6a1f1dSLionel Sambuc SourceManager &SrcMgr = PP.getSourceManager();
336*0a6a1f1dSLionel Sambuc SourceLocation EndLoc = AsmLoc;
337*0a6a1f1dSLionel Sambuc SmallVector<Token, 4> AsmToks;
338*0a6a1f1dSLionel Sambuc
339*0a6a1f1dSLionel Sambuc bool SingleLineMode = true;
340*0a6a1f1dSLionel Sambuc unsigned BraceNesting = 0;
341*0a6a1f1dSLionel Sambuc unsigned short savedBraceCount = BraceCount;
342*0a6a1f1dSLionel Sambuc bool InAsmComment = false;
343*0a6a1f1dSLionel Sambuc FileID FID;
344*0a6a1f1dSLionel Sambuc unsigned LineNo = 0;
345*0a6a1f1dSLionel Sambuc unsigned NumTokensRead = 0;
346*0a6a1f1dSLionel Sambuc SmallVector<SourceLocation, 4> LBraceLocs;
347*0a6a1f1dSLionel Sambuc bool SkippedStartOfLine = false;
348*0a6a1f1dSLionel Sambuc
349*0a6a1f1dSLionel Sambuc if (Tok.is(tok::l_brace)) {
350*0a6a1f1dSLionel Sambuc // Braced inline asm: consume the opening brace.
351*0a6a1f1dSLionel Sambuc SingleLineMode = false;
352*0a6a1f1dSLionel Sambuc BraceNesting = 1;
353*0a6a1f1dSLionel Sambuc EndLoc = ConsumeBrace();
354*0a6a1f1dSLionel Sambuc LBraceLocs.push_back(EndLoc);
355*0a6a1f1dSLionel Sambuc ++NumTokensRead;
356*0a6a1f1dSLionel Sambuc } else {
357*0a6a1f1dSLionel Sambuc // Single-line inline asm; compute which line it is on.
358*0a6a1f1dSLionel Sambuc std::pair<FileID, unsigned> ExpAsmLoc =
359*0a6a1f1dSLionel Sambuc SrcMgr.getDecomposedExpansionLoc(EndLoc);
360*0a6a1f1dSLionel Sambuc FID = ExpAsmLoc.first;
361*0a6a1f1dSLionel Sambuc LineNo = SrcMgr.getLineNumber(FID, ExpAsmLoc.second);
362*0a6a1f1dSLionel Sambuc LBraceLocs.push_back(SourceLocation());
363*0a6a1f1dSLionel Sambuc }
364*0a6a1f1dSLionel Sambuc
365*0a6a1f1dSLionel Sambuc SourceLocation TokLoc = Tok.getLocation();
366*0a6a1f1dSLionel Sambuc do {
367*0a6a1f1dSLionel Sambuc // If we hit EOF, we're done, period.
368*0a6a1f1dSLionel Sambuc if (isEofOrEom())
369*0a6a1f1dSLionel Sambuc break;
370*0a6a1f1dSLionel Sambuc
371*0a6a1f1dSLionel Sambuc if (!InAsmComment && Tok.is(tok::l_brace)) {
372*0a6a1f1dSLionel Sambuc // Consume the opening brace.
373*0a6a1f1dSLionel Sambuc SkippedStartOfLine = Tok.isAtStartOfLine();
374*0a6a1f1dSLionel Sambuc EndLoc = ConsumeBrace();
375*0a6a1f1dSLionel Sambuc BraceNesting++;
376*0a6a1f1dSLionel Sambuc LBraceLocs.push_back(EndLoc);
377*0a6a1f1dSLionel Sambuc TokLoc = Tok.getLocation();
378*0a6a1f1dSLionel Sambuc ++NumTokensRead;
379*0a6a1f1dSLionel Sambuc continue;
380*0a6a1f1dSLionel Sambuc } else if (!InAsmComment && Tok.is(tok::semi)) {
381*0a6a1f1dSLionel Sambuc // A semicolon in an asm is the start of a comment.
382*0a6a1f1dSLionel Sambuc InAsmComment = true;
383*0a6a1f1dSLionel Sambuc if (!SingleLineMode) {
384*0a6a1f1dSLionel Sambuc // Compute which line the comment is on.
385*0a6a1f1dSLionel Sambuc std::pair<FileID, unsigned> ExpSemiLoc =
386*0a6a1f1dSLionel Sambuc SrcMgr.getDecomposedExpansionLoc(TokLoc);
387*0a6a1f1dSLionel Sambuc FID = ExpSemiLoc.first;
388*0a6a1f1dSLionel Sambuc LineNo = SrcMgr.getLineNumber(FID, ExpSemiLoc.second);
389*0a6a1f1dSLionel Sambuc }
390*0a6a1f1dSLionel Sambuc } else if (SingleLineMode || InAsmComment) {
391*0a6a1f1dSLionel Sambuc // If end-of-line is significant, check whether this token is on a
392*0a6a1f1dSLionel Sambuc // new line.
393*0a6a1f1dSLionel Sambuc std::pair<FileID, unsigned> ExpLoc =
394*0a6a1f1dSLionel Sambuc SrcMgr.getDecomposedExpansionLoc(TokLoc);
395*0a6a1f1dSLionel Sambuc if (ExpLoc.first != FID ||
396*0a6a1f1dSLionel Sambuc SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second) != LineNo) {
397*0a6a1f1dSLionel Sambuc // If this is a single-line __asm, we're done, except if the next
398*0a6a1f1dSLionel Sambuc // line begins with an __asm too, in which case we finish a comment
399*0a6a1f1dSLionel Sambuc // if needed and then keep processing the next line as a single
400*0a6a1f1dSLionel Sambuc // line __asm.
401*0a6a1f1dSLionel Sambuc bool isAsm = Tok.is(tok::kw_asm);
402*0a6a1f1dSLionel Sambuc if (SingleLineMode && !isAsm)
403*0a6a1f1dSLionel Sambuc break;
404*0a6a1f1dSLionel Sambuc // We're no longer in a comment.
405*0a6a1f1dSLionel Sambuc InAsmComment = false;
406*0a6a1f1dSLionel Sambuc if (isAsm) {
407*0a6a1f1dSLionel Sambuc LineNo = SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second);
408*0a6a1f1dSLionel Sambuc SkippedStartOfLine = Tok.isAtStartOfLine();
409*0a6a1f1dSLionel Sambuc }
410*0a6a1f1dSLionel Sambuc } else if (!InAsmComment && Tok.is(tok::r_brace)) {
411*0a6a1f1dSLionel Sambuc // In MSVC mode, braces only participate in brace matching and
412*0a6a1f1dSLionel Sambuc // separating the asm statements. This is an intentional
413*0a6a1f1dSLionel Sambuc // departure from the Apple gcc behavior.
414*0a6a1f1dSLionel Sambuc if (!BraceNesting)
415*0a6a1f1dSLionel Sambuc break;
416*0a6a1f1dSLionel Sambuc }
417*0a6a1f1dSLionel Sambuc }
418*0a6a1f1dSLionel Sambuc if (!InAsmComment && BraceNesting && Tok.is(tok::r_brace) &&
419*0a6a1f1dSLionel Sambuc BraceCount == (savedBraceCount + BraceNesting)) {
420*0a6a1f1dSLionel Sambuc // Consume the closing brace.
421*0a6a1f1dSLionel Sambuc SkippedStartOfLine = Tok.isAtStartOfLine();
422*0a6a1f1dSLionel Sambuc EndLoc = ConsumeBrace();
423*0a6a1f1dSLionel Sambuc BraceNesting--;
424*0a6a1f1dSLionel Sambuc // Finish if all of the opened braces in the inline asm section were
425*0a6a1f1dSLionel Sambuc // consumed.
426*0a6a1f1dSLionel Sambuc if (BraceNesting == 0 && !SingleLineMode)
427*0a6a1f1dSLionel Sambuc break;
428*0a6a1f1dSLionel Sambuc else {
429*0a6a1f1dSLionel Sambuc LBraceLocs.pop_back();
430*0a6a1f1dSLionel Sambuc TokLoc = Tok.getLocation();
431*0a6a1f1dSLionel Sambuc ++NumTokensRead;
432*0a6a1f1dSLionel Sambuc continue;
433*0a6a1f1dSLionel Sambuc }
434*0a6a1f1dSLionel Sambuc }
435*0a6a1f1dSLionel Sambuc
436*0a6a1f1dSLionel Sambuc // Consume the next token; make sure we don't modify the brace count etc.
437*0a6a1f1dSLionel Sambuc // if we are in a comment.
438*0a6a1f1dSLionel Sambuc EndLoc = TokLoc;
439*0a6a1f1dSLionel Sambuc if (InAsmComment)
440*0a6a1f1dSLionel Sambuc PP.Lex(Tok);
441*0a6a1f1dSLionel Sambuc else {
442*0a6a1f1dSLionel Sambuc // Set the token as the start of line if we skipped the original start
443*0a6a1f1dSLionel Sambuc // of line token in case it was a nested brace.
444*0a6a1f1dSLionel Sambuc if (SkippedStartOfLine)
445*0a6a1f1dSLionel Sambuc Tok.setFlag(Token::StartOfLine);
446*0a6a1f1dSLionel Sambuc AsmToks.push_back(Tok);
447*0a6a1f1dSLionel Sambuc ConsumeAnyToken();
448*0a6a1f1dSLionel Sambuc }
449*0a6a1f1dSLionel Sambuc TokLoc = Tok.getLocation();
450*0a6a1f1dSLionel Sambuc ++NumTokensRead;
451*0a6a1f1dSLionel Sambuc SkippedStartOfLine = false;
452*0a6a1f1dSLionel Sambuc } while (1);
453*0a6a1f1dSLionel Sambuc
454*0a6a1f1dSLionel Sambuc if (BraceNesting && BraceCount != savedBraceCount) {
455*0a6a1f1dSLionel Sambuc // __asm without closing brace (this can happen at EOF).
456*0a6a1f1dSLionel Sambuc for (unsigned i = 0; i < BraceNesting; ++i) {
457*0a6a1f1dSLionel Sambuc Diag(Tok, diag::err_expected) << tok::r_brace;
458*0a6a1f1dSLionel Sambuc Diag(LBraceLocs.back(), diag::note_matching) << tok::l_brace;
459*0a6a1f1dSLionel Sambuc LBraceLocs.pop_back();
460*0a6a1f1dSLionel Sambuc }
461*0a6a1f1dSLionel Sambuc return StmtError();
462*0a6a1f1dSLionel Sambuc } else if (NumTokensRead == 0) {
463*0a6a1f1dSLionel Sambuc // Empty __asm.
464*0a6a1f1dSLionel Sambuc Diag(Tok, diag::err_expected) << tok::l_brace;
465*0a6a1f1dSLionel Sambuc return StmtError();
466*0a6a1f1dSLionel Sambuc }
467*0a6a1f1dSLionel Sambuc
468*0a6a1f1dSLionel Sambuc // Okay, prepare to use MC to parse the assembly.
469*0a6a1f1dSLionel Sambuc SmallVector<StringRef, 4> ConstraintRefs;
470*0a6a1f1dSLionel Sambuc SmallVector<Expr *, 4> Exprs;
471*0a6a1f1dSLionel Sambuc SmallVector<StringRef, 4> ClobberRefs;
472*0a6a1f1dSLionel Sambuc
473*0a6a1f1dSLionel Sambuc // We need an actual supported target.
474*0a6a1f1dSLionel Sambuc const llvm::Triple &TheTriple = Actions.Context.getTargetInfo().getTriple();
475*0a6a1f1dSLionel Sambuc llvm::Triple::ArchType ArchTy = TheTriple.getArch();
476*0a6a1f1dSLionel Sambuc const std::string &TT = TheTriple.getTriple();
477*0a6a1f1dSLionel Sambuc const llvm::Target *TheTarget = nullptr;
478*0a6a1f1dSLionel Sambuc bool UnsupportedArch =
479*0a6a1f1dSLionel Sambuc (ArchTy != llvm::Triple::x86 && ArchTy != llvm::Triple::x86_64);
480*0a6a1f1dSLionel Sambuc if (UnsupportedArch) {
481*0a6a1f1dSLionel Sambuc Diag(AsmLoc, diag::err_msasm_unsupported_arch) << TheTriple.getArchName();
482*0a6a1f1dSLionel Sambuc } else {
483*0a6a1f1dSLionel Sambuc std::string Error;
484*0a6a1f1dSLionel Sambuc TheTarget = llvm::TargetRegistry::lookupTarget(TT, Error);
485*0a6a1f1dSLionel Sambuc if (!TheTarget)
486*0a6a1f1dSLionel Sambuc Diag(AsmLoc, diag::err_msasm_unable_to_create_target) << Error;
487*0a6a1f1dSLionel Sambuc }
488*0a6a1f1dSLionel Sambuc
489*0a6a1f1dSLionel Sambuc assert(!LBraceLocs.empty() && "Should have at least one location here");
490*0a6a1f1dSLionel Sambuc
491*0a6a1f1dSLionel Sambuc // If we don't support assembly, or the assembly is empty, we don't
492*0a6a1f1dSLionel Sambuc // need to instantiate the AsmParser, etc.
493*0a6a1f1dSLionel Sambuc if (!TheTarget || AsmToks.empty()) {
494*0a6a1f1dSLionel Sambuc return Actions.ActOnMSAsmStmt(AsmLoc, LBraceLocs[0], AsmToks, StringRef(),
495*0a6a1f1dSLionel Sambuc /*NumOutputs*/ 0, /*NumInputs*/ 0,
496*0a6a1f1dSLionel Sambuc ConstraintRefs, ClobberRefs, Exprs, EndLoc);
497*0a6a1f1dSLionel Sambuc }
498*0a6a1f1dSLionel Sambuc
499*0a6a1f1dSLionel Sambuc // Expand the tokens into a string buffer.
500*0a6a1f1dSLionel Sambuc SmallString<512> AsmString;
501*0a6a1f1dSLionel Sambuc SmallVector<unsigned, 8> TokOffsets;
502*0a6a1f1dSLionel Sambuc if (buildMSAsmString(PP, AsmLoc, AsmToks, TokOffsets, AsmString))
503*0a6a1f1dSLionel Sambuc return StmtError();
504*0a6a1f1dSLionel Sambuc
505*0a6a1f1dSLionel Sambuc std::unique_ptr<llvm::MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TT));
506*0a6a1f1dSLionel Sambuc std::unique_ptr<llvm::MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TT));
507*0a6a1f1dSLionel Sambuc // Get the instruction descriptor.
508*0a6a1f1dSLionel Sambuc std::unique_ptr<llvm::MCInstrInfo> MII(TheTarget->createMCInstrInfo());
509*0a6a1f1dSLionel Sambuc std::unique_ptr<llvm::MCObjectFileInfo> MOFI(new llvm::MCObjectFileInfo());
510*0a6a1f1dSLionel Sambuc std::unique_ptr<llvm::MCSubtargetInfo> STI(
511*0a6a1f1dSLionel Sambuc TheTarget->createMCSubtargetInfo(TT, "", ""));
512*0a6a1f1dSLionel Sambuc
513*0a6a1f1dSLionel Sambuc llvm::SourceMgr TempSrcMgr;
514*0a6a1f1dSLionel Sambuc llvm::MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &TempSrcMgr);
515*0a6a1f1dSLionel Sambuc MOFI->InitMCObjectFileInfo(TT, llvm::Reloc::Default, llvm::CodeModel::Default,
516*0a6a1f1dSLionel Sambuc Ctx);
517*0a6a1f1dSLionel Sambuc std::unique_ptr<llvm::MemoryBuffer> Buffer =
518*0a6a1f1dSLionel Sambuc llvm::MemoryBuffer::getMemBuffer(AsmString, "<MS inline asm>");
519*0a6a1f1dSLionel Sambuc
520*0a6a1f1dSLionel Sambuc // Tell SrcMgr about this buffer, which is what the parser will pick up.
521*0a6a1f1dSLionel Sambuc TempSrcMgr.AddNewSourceBuffer(std::move(Buffer), llvm::SMLoc());
522*0a6a1f1dSLionel Sambuc
523*0a6a1f1dSLionel Sambuc std::unique_ptr<llvm::MCStreamer> Str(createNullStreamer(Ctx));
524*0a6a1f1dSLionel Sambuc std::unique_ptr<llvm::MCAsmParser> Parser(
525*0a6a1f1dSLionel Sambuc createMCAsmParser(TempSrcMgr, Ctx, *Str.get(), *MAI));
526*0a6a1f1dSLionel Sambuc
527*0a6a1f1dSLionel Sambuc // FIXME: init MCOptions from sanitizer flags here.
528*0a6a1f1dSLionel Sambuc llvm::MCTargetOptions MCOptions;
529*0a6a1f1dSLionel Sambuc std::unique_ptr<llvm::MCTargetAsmParser> TargetParser(
530*0a6a1f1dSLionel Sambuc TheTarget->createMCAsmParser(*STI, *Parser, *MII, MCOptions));
531*0a6a1f1dSLionel Sambuc
532*0a6a1f1dSLionel Sambuc std::unique_ptr<llvm::MCInstPrinter> IP(
533*0a6a1f1dSLionel Sambuc TheTarget->createMCInstPrinter(1, *MAI, *MII, *MRI, *STI));
534*0a6a1f1dSLionel Sambuc
535*0a6a1f1dSLionel Sambuc // Change to the Intel dialect.
536*0a6a1f1dSLionel Sambuc Parser->setAssemblerDialect(1);
537*0a6a1f1dSLionel Sambuc Parser->setTargetParser(*TargetParser.get());
538*0a6a1f1dSLionel Sambuc Parser->setParsingInlineAsm(true);
539*0a6a1f1dSLionel Sambuc TargetParser->setParsingInlineAsm(true);
540*0a6a1f1dSLionel Sambuc
541*0a6a1f1dSLionel Sambuc ClangAsmParserCallback Callback(*this, AsmLoc, AsmString, AsmToks,
542*0a6a1f1dSLionel Sambuc TokOffsets);
543*0a6a1f1dSLionel Sambuc TargetParser->setSemaCallback(&Callback);
544*0a6a1f1dSLionel Sambuc TempSrcMgr.setDiagHandler(ClangAsmParserCallback::DiagHandlerCallback,
545*0a6a1f1dSLionel Sambuc &Callback);
546*0a6a1f1dSLionel Sambuc
547*0a6a1f1dSLionel Sambuc unsigned NumOutputs;
548*0a6a1f1dSLionel Sambuc unsigned NumInputs;
549*0a6a1f1dSLionel Sambuc std::string AsmStringIR;
550*0a6a1f1dSLionel Sambuc SmallVector<std::pair<void *, bool>, 4> OpExprs;
551*0a6a1f1dSLionel Sambuc SmallVector<std::string, 4> Constraints;
552*0a6a1f1dSLionel Sambuc SmallVector<std::string, 4> Clobbers;
553*0a6a1f1dSLionel Sambuc if (Parser->parseMSInlineAsm(AsmLoc.getPtrEncoding(), AsmStringIR, NumOutputs,
554*0a6a1f1dSLionel Sambuc NumInputs, OpExprs, Constraints, Clobbers,
555*0a6a1f1dSLionel Sambuc MII.get(), IP.get(), Callback))
556*0a6a1f1dSLionel Sambuc return StmtError();
557*0a6a1f1dSLionel Sambuc
558*0a6a1f1dSLionel Sambuc // Filter out "fpsw". Clang doesn't accept it, and it always lists flags and
559*0a6a1f1dSLionel Sambuc // fpsr as clobbers.
560*0a6a1f1dSLionel Sambuc auto End = std::remove(Clobbers.begin(), Clobbers.end(), "fpsw");
561*0a6a1f1dSLionel Sambuc Clobbers.erase(End, Clobbers.end());
562*0a6a1f1dSLionel Sambuc
563*0a6a1f1dSLionel Sambuc // Build the vector of clobber StringRefs.
564*0a6a1f1dSLionel Sambuc ClobberRefs.insert(ClobberRefs.end(), Clobbers.begin(), Clobbers.end());
565*0a6a1f1dSLionel Sambuc
566*0a6a1f1dSLionel Sambuc // Recast the void pointers and build the vector of constraint StringRefs.
567*0a6a1f1dSLionel Sambuc unsigned NumExprs = NumOutputs + NumInputs;
568*0a6a1f1dSLionel Sambuc ConstraintRefs.resize(NumExprs);
569*0a6a1f1dSLionel Sambuc Exprs.resize(NumExprs);
570*0a6a1f1dSLionel Sambuc for (unsigned i = 0, e = NumExprs; i != e; ++i) {
571*0a6a1f1dSLionel Sambuc Expr *OpExpr = static_cast<Expr *>(OpExprs[i].first);
572*0a6a1f1dSLionel Sambuc if (!OpExpr)
573*0a6a1f1dSLionel Sambuc return StmtError();
574*0a6a1f1dSLionel Sambuc
575*0a6a1f1dSLionel Sambuc // Need address of variable.
576*0a6a1f1dSLionel Sambuc if (OpExprs[i].second)
577*0a6a1f1dSLionel Sambuc OpExpr =
578*0a6a1f1dSLionel Sambuc Actions.BuildUnaryOp(getCurScope(), AsmLoc, UO_AddrOf, OpExpr).get();
579*0a6a1f1dSLionel Sambuc
580*0a6a1f1dSLionel Sambuc ConstraintRefs[i] = StringRef(Constraints[i]);
581*0a6a1f1dSLionel Sambuc Exprs[i] = OpExpr;
582*0a6a1f1dSLionel Sambuc }
583*0a6a1f1dSLionel Sambuc
584*0a6a1f1dSLionel Sambuc // FIXME: We should be passing source locations for better diagnostics.
585*0a6a1f1dSLionel Sambuc return Actions.ActOnMSAsmStmt(AsmLoc, LBraceLocs[0], AsmToks, AsmStringIR,
586*0a6a1f1dSLionel Sambuc NumOutputs, NumInputs, ConstraintRefs,
587*0a6a1f1dSLionel Sambuc ClobberRefs, Exprs, EndLoc);
588*0a6a1f1dSLionel Sambuc }
589*0a6a1f1dSLionel Sambuc
590*0a6a1f1dSLionel Sambuc /// ParseAsmStatement - Parse a GNU extended asm statement.
591*0a6a1f1dSLionel Sambuc /// asm-statement:
592*0a6a1f1dSLionel Sambuc /// gnu-asm-statement
593*0a6a1f1dSLionel Sambuc /// ms-asm-statement
594*0a6a1f1dSLionel Sambuc ///
595*0a6a1f1dSLionel Sambuc /// [GNU] gnu-asm-statement:
596*0a6a1f1dSLionel Sambuc /// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
597*0a6a1f1dSLionel Sambuc ///
598*0a6a1f1dSLionel Sambuc /// [GNU] asm-argument:
599*0a6a1f1dSLionel Sambuc /// asm-string-literal
600*0a6a1f1dSLionel Sambuc /// asm-string-literal ':' asm-operands[opt]
601*0a6a1f1dSLionel Sambuc /// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
602*0a6a1f1dSLionel Sambuc /// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
603*0a6a1f1dSLionel Sambuc /// ':' asm-clobbers
604*0a6a1f1dSLionel Sambuc ///
605*0a6a1f1dSLionel Sambuc /// [GNU] asm-clobbers:
606*0a6a1f1dSLionel Sambuc /// asm-string-literal
607*0a6a1f1dSLionel Sambuc /// asm-clobbers ',' asm-string-literal
608*0a6a1f1dSLionel Sambuc ///
ParseAsmStatement(bool & msAsm)609*0a6a1f1dSLionel Sambuc StmtResult Parser::ParseAsmStatement(bool &msAsm) {
610*0a6a1f1dSLionel Sambuc assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
611*0a6a1f1dSLionel Sambuc SourceLocation AsmLoc = ConsumeToken();
612*0a6a1f1dSLionel Sambuc
613*0a6a1f1dSLionel Sambuc if (getLangOpts().AsmBlocks && Tok.isNot(tok::l_paren) &&
614*0a6a1f1dSLionel Sambuc !isTypeQualifier()) {
615*0a6a1f1dSLionel Sambuc msAsm = true;
616*0a6a1f1dSLionel Sambuc return ParseMicrosoftAsmStatement(AsmLoc);
617*0a6a1f1dSLionel Sambuc }
618*0a6a1f1dSLionel Sambuc DeclSpec DS(AttrFactory);
619*0a6a1f1dSLionel Sambuc SourceLocation Loc = Tok.getLocation();
620*0a6a1f1dSLionel Sambuc ParseTypeQualifierListOpt(DS, AR_VendorAttributesParsed);
621*0a6a1f1dSLionel Sambuc
622*0a6a1f1dSLionel Sambuc // GNU asms accept, but warn, about type-qualifiers other than volatile.
623*0a6a1f1dSLionel Sambuc if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
624*0a6a1f1dSLionel Sambuc Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
625*0a6a1f1dSLionel Sambuc if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
626*0a6a1f1dSLionel Sambuc Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
627*0a6a1f1dSLionel Sambuc // FIXME: Once GCC supports _Atomic, check whether it permits it here.
628*0a6a1f1dSLionel Sambuc if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
629*0a6a1f1dSLionel Sambuc Diag(Loc, diag::w_asm_qualifier_ignored) << "_Atomic";
630*0a6a1f1dSLionel Sambuc
631*0a6a1f1dSLionel Sambuc // Remember if this was a volatile asm.
632*0a6a1f1dSLionel Sambuc bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
633*0a6a1f1dSLionel Sambuc if (Tok.isNot(tok::l_paren)) {
634*0a6a1f1dSLionel Sambuc Diag(Tok, diag::err_expected_lparen_after) << "asm";
635*0a6a1f1dSLionel Sambuc SkipUntil(tok::r_paren, StopAtSemi);
636*0a6a1f1dSLionel Sambuc return StmtError();
637*0a6a1f1dSLionel Sambuc }
638*0a6a1f1dSLionel Sambuc BalancedDelimiterTracker T(*this, tok::l_paren);
639*0a6a1f1dSLionel Sambuc T.consumeOpen();
640*0a6a1f1dSLionel Sambuc
641*0a6a1f1dSLionel Sambuc ExprResult AsmString(ParseAsmStringLiteral());
642*0a6a1f1dSLionel Sambuc if (AsmString.isInvalid()) {
643*0a6a1f1dSLionel Sambuc // Consume up to and including the closing paren.
644*0a6a1f1dSLionel Sambuc T.skipToEnd();
645*0a6a1f1dSLionel Sambuc return StmtError();
646*0a6a1f1dSLionel Sambuc }
647*0a6a1f1dSLionel Sambuc
648*0a6a1f1dSLionel Sambuc SmallVector<IdentifierInfo *, 4> Names;
649*0a6a1f1dSLionel Sambuc ExprVector Constraints;
650*0a6a1f1dSLionel Sambuc ExprVector Exprs;
651*0a6a1f1dSLionel Sambuc ExprVector Clobbers;
652*0a6a1f1dSLionel Sambuc
653*0a6a1f1dSLionel Sambuc if (Tok.is(tok::r_paren)) {
654*0a6a1f1dSLionel Sambuc // We have a simple asm expression like 'asm("foo")'.
655*0a6a1f1dSLionel Sambuc T.consumeClose();
656*0a6a1f1dSLionel Sambuc return Actions.ActOnGCCAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
657*0a6a1f1dSLionel Sambuc /*NumOutputs*/ 0, /*NumInputs*/ 0, nullptr,
658*0a6a1f1dSLionel Sambuc Constraints, Exprs, AsmString.get(),
659*0a6a1f1dSLionel Sambuc Clobbers, T.getCloseLocation());
660*0a6a1f1dSLionel Sambuc }
661*0a6a1f1dSLionel Sambuc
662*0a6a1f1dSLionel Sambuc // Parse Outputs, if present.
663*0a6a1f1dSLionel Sambuc bool AteExtraColon = false;
664*0a6a1f1dSLionel Sambuc if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
665*0a6a1f1dSLionel Sambuc // In C++ mode, parse "::" like ": :".
666*0a6a1f1dSLionel Sambuc AteExtraColon = Tok.is(tok::coloncolon);
667*0a6a1f1dSLionel Sambuc ConsumeToken();
668*0a6a1f1dSLionel Sambuc
669*0a6a1f1dSLionel Sambuc if (!AteExtraColon && ParseAsmOperandsOpt(Names, Constraints, Exprs))
670*0a6a1f1dSLionel Sambuc return StmtError();
671*0a6a1f1dSLionel Sambuc }
672*0a6a1f1dSLionel Sambuc
673*0a6a1f1dSLionel Sambuc unsigned NumOutputs = Names.size();
674*0a6a1f1dSLionel Sambuc
675*0a6a1f1dSLionel Sambuc // Parse Inputs, if present.
676*0a6a1f1dSLionel Sambuc if (AteExtraColon || Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
677*0a6a1f1dSLionel Sambuc // In C++ mode, parse "::" like ": :".
678*0a6a1f1dSLionel Sambuc if (AteExtraColon)
679*0a6a1f1dSLionel Sambuc AteExtraColon = false;
680*0a6a1f1dSLionel Sambuc else {
681*0a6a1f1dSLionel Sambuc AteExtraColon = Tok.is(tok::coloncolon);
682*0a6a1f1dSLionel Sambuc ConsumeToken();
683*0a6a1f1dSLionel Sambuc }
684*0a6a1f1dSLionel Sambuc
685*0a6a1f1dSLionel Sambuc if (!AteExtraColon && ParseAsmOperandsOpt(Names, Constraints, Exprs))
686*0a6a1f1dSLionel Sambuc return StmtError();
687*0a6a1f1dSLionel Sambuc }
688*0a6a1f1dSLionel Sambuc
689*0a6a1f1dSLionel Sambuc assert(Names.size() == Constraints.size() &&
690*0a6a1f1dSLionel Sambuc Constraints.size() == Exprs.size() && "Input operand size mismatch!");
691*0a6a1f1dSLionel Sambuc
692*0a6a1f1dSLionel Sambuc unsigned NumInputs = Names.size() - NumOutputs;
693*0a6a1f1dSLionel Sambuc
694*0a6a1f1dSLionel Sambuc // Parse the clobbers, if present.
695*0a6a1f1dSLionel Sambuc if (AteExtraColon || Tok.is(tok::colon)) {
696*0a6a1f1dSLionel Sambuc if (!AteExtraColon)
697*0a6a1f1dSLionel Sambuc ConsumeToken();
698*0a6a1f1dSLionel Sambuc
699*0a6a1f1dSLionel Sambuc // Parse the asm-string list for clobbers if present.
700*0a6a1f1dSLionel Sambuc if (Tok.isNot(tok::r_paren)) {
701*0a6a1f1dSLionel Sambuc while (1) {
702*0a6a1f1dSLionel Sambuc ExprResult Clobber(ParseAsmStringLiteral());
703*0a6a1f1dSLionel Sambuc
704*0a6a1f1dSLionel Sambuc if (Clobber.isInvalid())
705*0a6a1f1dSLionel Sambuc break;
706*0a6a1f1dSLionel Sambuc
707*0a6a1f1dSLionel Sambuc Clobbers.push_back(Clobber.get());
708*0a6a1f1dSLionel Sambuc
709*0a6a1f1dSLionel Sambuc if (!TryConsumeToken(tok::comma))
710*0a6a1f1dSLionel Sambuc break;
711*0a6a1f1dSLionel Sambuc }
712*0a6a1f1dSLionel Sambuc }
713*0a6a1f1dSLionel Sambuc }
714*0a6a1f1dSLionel Sambuc
715*0a6a1f1dSLionel Sambuc T.consumeClose();
716*0a6a1f1dSLionel Sambuc return Actions.ActOnGCCAsmStmt(
717*0a6a1f1dSLionel Sambuc AsmLoc, false, isVolatile, NumOutputs, NumInputs, Names.data(),
718*0a6a1f1dSLionel Sambuc Constraints, Exprs, AsmString.get(), Clobbers, T.getCloseLocation());
719*0a6a1f1dSLionel Sambuc }
720*0a6a1f1dSLionel Sambuc
721*0a6a1f1dSLionel Sambuc /// ParseAsmOperands - Parse the asm-operands production as used by
722*0a6a1f1dSLionel Sambuc /// asm-statement, assuming the leading ':' token was eaten.
723*0a6a1f1dSLionel Sambuc ///
724*0a6a1f1dSLionel Sambuc /// [GNU] asm-operands:
725*0a6a1f1dSLionel Sambuc /// asm-operand
726*0a6a1f1dSLionel Sambuc /// asm-operands ',' asm-operand
727*0a6a1f1dSLionel Sambuc ///
728*0a6a1f1dSLionel Sambuc /// [GNU] asm-operand:
729*0a6a1f1dSLionel Sambuc /// asm-string-literal '(' expression ')'
730*0a6a1f1dSLionel Sambuc /// '[' identifier ']' asm-string-literal '(' expression ')'
731*0a6a1f1dSLionel Sambuc ///
732*0a6a1f1dSLionel Sambuc //
733*0a6a1f1dSLionel Sambuc // FIXME: Avoid unnecessary std::string trashing.
ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo * > & Names,SmallVectorImpl<Expr * > & Constraints,SmallVectorImpl<Expr * > & Exprs)734*0a6a1f1dSLionel Sambuc bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names,
735*0a6a1f1dSLionel Sambuc SmallVectorImpl<Expr *> &Constraints,
736*0a6a1f1dSLionel Sambuc SmallVectorImpl<Expr *> &Exprs) {
737*0a6a1f1dSLionel Sambuc // 'asm-operands' isn't present?
738*0a6a1f1dSLionel Sambuc if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
739*0a6a1f1dSLionel Sambuc return false;
740*0a6a1f1dSLionel Sambuc
741*0a6a1f1dSLionel Sambuc while (1) {
742*0a6a1f1dSLionel Sambuc // Read the [id] if present.
743*0a6a1f1dSLionel Sambuc if (Tok.is(tok::l_square)) {
744*0a6a1f1dSLionel Sambuc BalancedDelimiterTracker T(*this, tok::l_square);
745*0a6a1f1dSLionel Sambuc T.consumeOpen();
746*0a6a1f1dSLionel Sambuc
747*0a6a1f1dSLionel Sambuc if (Tok.isNot(tok::identifier)) {
748*0a6a1f1dSLionel Sambuc Diag(Tok, diag::err_expected) << tok::identifier;
749*0a6a1f1dSLionel Sambuc SkipUntil(tok::r_paren, StopAtSemi);
750*0a6a1f1dSLionel Sambuc return true;
751*0a6a1f1dSLionel Sambuc }
752*0a6a1f1dSLionel Sambuc
753*0a6a1f1dSLionel Sambuc IdentifierInfo *II = Tok.getIdentifierInfo();
754*0a6a1f1dSLionel Sambuc ConsumeToken();
755*0a6a1f1dSLionel Sambuc
756*0a6a1f1dSLionel Sambuc Names.push_back(II);
757*0a6a1f1dSLionel Sambuc T.consumeClose();
758*0a6a1f1dSLionel Sambuc } else
759*0a6a1f1dSLionel Sambuc Names.push_back(nullptr);
760*0a6a1f1dSLionel Sambuc
761*0a6a1f1dSLionel Sambuc ExprResult Constraint(ParseAsmStringLiteral());
762*0a6a1f1dSLionel Sambuc if (Constraint.isInvalid()) {
763*0a6a1f1dSLionel Sambuc SkipUntil(tok::r_paren, StopAtSemi);
764*0a6a1f1dSLionel Sambuc return true;
765*0a6a1f1dSLionel Sambuc }
766*0a6a1f1dSLionel Sambuc Constraints.push_back(Constraint.get());
767*0a6a1f1dSLionel Sambuc
768*0a6a1f1dSLionel Sambuc if (Tok.isNot(tok::l_paren)) {
769*0a6a1f1dSLionel Sambuc Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
770*0a6a1f1dSLionel Sambuc SkipUntil(tok::r_paren, StopAtSemi);
771*0a6a1f1dSLionel Sambuc return true;
772*0a6a1f1dSLionel Sambuc }
773*0a6a1f1dSLionel Sambuc
774*0a6a1f1dSLionel Sambuc // Read the parenthesized expression.
775*0a6a1f1dSLionel Sambuc BalancedDelimiterTracker T(*this, tok::l_paren);
776*0a6a1f1dSLionel Sambuc T.consumeOpen();
777*0a6a1f1dSLionel Sambuc ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression());
778*0a6a1f1dSLionel Sambuc T.consumeClose();
779*0a6a1f1dSLionel Sambuc if (Res.isInvalid()) {
780*0a6a1f1dSLionel Sambuc SkipUntil(tok::r_paren, StopAtSemi);
781*0a6a1f1dSLionel Sambuc return true;
782*0a6a1f1dSLionel Sambuc }
783*0a6a1f1dSLionel Sambuc Exprs.push_back(Res.get());
784*0a6a1f1dSLionel Sambuc // Eat the comma and continue parsing if it exists.
785*0a6a1f1dSLionel Sambuc if (!TryConsumeToken(tok::comma))
786*0a6a1f1dSLionel Sambuc return false;
787*0a6a1f1dSLionel Sambuc }
788*0a6a1f1dSLionel Sambuc }
789