/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Parse/ParseStmtAsm.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===---- ParseStmtAsm.cpp - Assembly Statement Parser --------------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file implements parsing for GCC and Microsoft inline assembly. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "clang/AST/ASTContext.h" |
14 | | #include "clang/Basic/Diagnostic.h" |
15 | | #include "clang/Basic/TargetInfo.h" |
16 | | #include "clang/Parse/Parser.h" |
17 | | #include "clang/Parse/RAIIObjectsForParser.h" |
18 | | #include "llvm/ADT/SmallString.h" |
19 | | #include "llvm/ADT/StringExtras.h" |
20 | | #include "llvm/MC/MCAsmInfo.h" |
21 | | #include "llvm/MC/MCContext.h" |
22 | | #include "llvm/MC/MCInstPrinter.h" |
23 | | #include "llvm/MC/MCInstrInfo.h" |
24 | | #include "llvm/MC/MCObjectFileInfo.h" |
25 | | #include "llvm/MC/MCParser/MCAsmParser.h" |
26 | | #include "llvm/MC/MCParser/MCTargetAsmParser.h" |
27 | | #include "llvm/MC/MCRegisterInfo.h" |
28 | | #include "llvm/MC/MCStreamer.h" |
29 | | #include "llvm/MC/MCSubtargetInfo.h" |
30 | | #include "llvm/MC/MCTargetOptions.h" |
31 | | #include "llvm/MC/TargetRegistry.h" |
32 | | #include "llvm/Support/SourceMgr.h" |
33 | | #include "llvm/Support/TargetSelect.h" |
34 | | using namespace clang; |
35 | | |
36 | | namespace { |
37 | | class ClangAsmParserCallback : public llvm::MCAsmParserSemaCallback { |
38 | | Parser &TheParser; |
39 | | SourceLocation AsmLoc; |
40 | | StringRef AsmString; |
41 | | |
42 | | /// The tokens we streamed into AsmString and handed off to MC. |
43 | | ArrayRef<Token> AsmToks; |
44 | | |
45 | | /// The offset of each token in AsmToks within AsmString. |
46 | | ArrayRef<unsigned> AsmTokOffsets; |
47 | | |
48 | | public: |
49 | | ClangAsmParserCallback(Parser &P, SourceLocation Loc, StringRef AsmString, |
50 | | ArrayRef<Token> Toks, ArrayRef<unsigned> Offsets) |
51 | | : TheParser(P), AsmLoc(Loc), AsmString(AsmString), AsmToks(Toks), |
52 | 238 | AsmTokOffsets(Offsets) { |
53 | 238 | assert(AsmToks.size() == AsmTokOffsets.size()); |
54 | 238 | } |
55 | | |
56 | | void LookupInlineAsmIdentifier(StringRef &LineBuf, |
57 | | llvm::InlineAsmIdentifierInfo &Info, |
58 | | bool IsUnevaluatedContext) override; |
59 | | |
60 | | StringRef LookupInlineAsmLabel(StringRef Identifier, llvm::SourceMgr &LSM, |
61 | | llvm::SMLoc Location, |
62 | | bool Create) override; |
63 | | |
64 | | bool LookupInlineAsmField(StringRef Base, StringRef Member, |
65 | 25 | unsigned &Offset) override { |
66 | 25 | return TheParser.getActions().LookupInlineAsmField(Base, Member, Offset, |
67 | 25 | AsmLoc); |
68 | 25 | } |
69 | | |
70 | 20 | static void DiagHandlerCallback(const llvm::SMDiagnostic &D, void *Context) { |
71 | 20 | ((ClangAsmParserCallback *)Context)->handleDiagnostic(D); |
72 | 20 | } |
73 | | |
74 | | private: |
75 | | /// Collect the appropriate tokens for the given string. |
76 | | void findTokensForString(StringRef Str, SmallVectorImpl<Token> &TempToks, |
77 | | const Token *&FirstOrigToken) const; |
78 | | |
79 | | SourceLocation translateLocation(const llvm::SourceMgr &LSM, |
80 | | llvm::SMLoc SMLoc); |
81 | | |
82 | | void handleDiagnostic(const llvm::SMDiagnostic &D); |
83 | | }; |
84 | | } |
85 | | |
86 | | void ClangAsmParserCallback::LookupInlineAsmIdentifier( |
87 | | StringRef &LineBuf, llvm::InlineAsmIdentifierInfo &Info, |
88 | 234 | bool IsUnevaluatedContext) { |
89 | | // Collect the desired tokens. |
90 | 234 | SmallVector<Token, 16> LineToks; |
91 | 234 | const Token *FirstOrigToken = nullptr; |
92 | 234 | findTokensForString(LineBuf, LineToks, FirstOrigToken); |
93 | | |
94 | 234 | unsigned NumConsumedToks; |
95 | 234 | ExprResult Result = TheParser.ParseMSAsmIdentifier(LineToks, NumConsumedToks, |
96 | 234 | IsUnevaluatedContext); |
97 | | |
98 | | // If we consumed the entire line, tell MC that. |
99 | | // Also do this if we consumed nothing as a way of reporting failure. |
100 | 234 | if (NumConsumedToks == 0 || NumConsumedToks == LineToks.size()) { |
101 | | // By not modifying LineBuf, we're implicitly consuming it all. |
102 | | |
103 | | // Otherwise, consume up to the original tokens. |
104 | 185 | } else { |
105 | 185 | assert(FirstOrigToken && "not using original tokens?"); |
106 | | |
107 | | // Since we're using original tokens, apply that offset. |
108 | 0 | assert(FirstOrigToken[NumConsumedToks].getLocation() == |
109 | 185 | LineToks[NumConsumedToks].getLocation()); |
110 | 0 | unsigned FirstIndex = FirstOrigToken - AsmToks.begin(); |
111 | 185 | unsigned LastIndex = FirstIndex + NumConsumedToks - 1; |
112 | | |
113 | | // The total length we've consumed is the relative offset |
114 | | // of the last token we consumed plus its length. |
115 | 185 | unsigned TotalOffset = |
116 | 185 | (AsmTokOffsets[LastIndex] + AsmToks[LastIndex].getLength() - |
117 | 185 | AsmTokOffsets[FirstIndex]); |
118 | 185 | LineBuf = LineBuf.substr(0, TotalOffset); |
119 | 185 | } |
120 | | |
121 | | // Initialize Info with the lookup result. |
122 | 234 | if (!Result.isUsable()) |
123 | 24 | return; |
124 | 210 | TheParser.getActions().FillInlineAsmIdentifierInfo(Result.get(), Info); |
125 | 210 | } |
126 | | |
127 | | StringRef ClangAsmParserCallback::LookupInlineAsmLabel(StringRef Identifier, |
128 | | llvm::SourceMgr &LSM, |
129 | | llvm::SMLoc Location, |
130 | 42 | bool Create) { |
131 | 42 | SourceLocation Loc = translateLocation(LSM, Location); |
132 | 42 | LabelDecl *Label = |
133 | 42 | TheParser.getActions().GetOrCreateMSAsmLabel(Identifier, Loc, Create); |
134 | 42 | return Label->getMSAsmLabel(); |
135 | 42 | } |
136 | | |
137 | | void ClangAsmParserCallback::findTokensForString( |
138 | | StringRef Str, SmallVectorImpl<Token> &TempToks, |
139 | 234 | const Token *&FirstOrigToken) const { |
140 | | // For now, assert that the string we're working with is a substring |
141 | | // of what we gave to MC. This lets us use the original tokens. |
142 | 234 | assert(!std::less<const char *>()(Str.begin(), AsmString.begin()) && |
143 | 234 | !std::less<const char *>()(AsmString.end(), Str.end())); |
144 | | |
145 | | // Try to find a token whose offset matches the first token. |
146 | 0 | unsigned FirstCharOffset = Str.begin() - AsmString.begin(); |
147 | 234 | const unsigned *FirstTokOffset = |
148 | 234 | llvm::lower_bound(AsmTokOffsets, FirstCharOffset); |
149 | | |
150 | | // For now, assert that the start of the string exactly |
151 | | // corresponds to the start of a token. |
152 | 234 | assert(*FirstTokOffset == FirstCharOffset); |
153 | | |
154 | | // Use all the original tokens for this line. (We assume the |
155 | | // end of the line corresponds cleanly to a token break.) |
156 | 0 | unsigned FirstTokIndex = FirstTokOffset - AsmTokOffsets.begin(); |
157 | 234 | FirstOrigToken = &AsmToks[FirstTokIndex]; |
158 | 234 | unsigned LastCharOffset = Str.end() - AsmString.begin(); |
159 | 4.12k | for (unsigned i = FirstTokIndex, e = AsmTokOffsets.size(); i != e; ++i3.88k ) { |
160 | 3.88k | if (AsmTokOffsets[i] >= LastCharOffset) |
161 | 0 | break; |
162 | 3.88k | TempToks.push_back(AsmToks[i]); |
163 | 3.88k | } |
164 | 234 | } |
165 | | |
166 | | SourceLocation |
167 | | ClangAsmParserCallback::translateLocation(const llvm::SourceMgr &LSM, |
168 | 62 | llvm::SMLoc SMLoc) { |
169 | | // Compute an offset into the inline asm buffer. |
170 | | // FIXME: This isn't right if .macro is involved (but hopefully, no |
171 | | // real-world code does that). |
172 | 62 | const llvm::MemoryBuffer *LBuf = |
173 | 62 | LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(SMLoc)); |
174 | 62 | unsigned Offset = SMLoc.getPointer() - LBuf->getBufferStart(); |
175 | | |
176 | | // Figure out which token that offset points into. |
177 | 62 | const unsigned *TokOffsetPtr = llvm::lower_bound(AsmTokOffsets, Offset); |
178 | 62 | unsigned TokIndex = TokOffsetPtr - AsmTokOffsets.begin(); |
179 | 62 | unsigned TokOffset = *TokOffsetPtr; |
180 | | |
181 | | // If we come up with an answer which seems sane, use it; otherwise, |
182 | | // just point at the __asm keyword. |
183 | | // FIXME: Assert the answer is sane once we handle .macro correctly. |
184 | 62 | SourceLocation Loc = AsmLoc; |
185 | 62 | if (TokIndex < AsmToks.size()) { |
186 | 60 | const Token &Tok = AsmToks[TokIndex]; |
187 | 60 | Loc = Tok.getLocation(); |
188 | 60 | Loc = Loc.getLocWithOffset(Offset - TokOffset); |
189 | 60 | } |
190 | 62 | return Loc; |
191 | 62 | } |
192 | | |
193 | 20 | void ClangAsmParserCallback::handleDiagnostic(const llvm::SMDiagnostic &D) { |
194 | 20 | const llvm::SourceMgr &LSM = *D.getSourceMgr(); |
195 | 20 | SourceLocation Loc = translateLocation(LSM, D.getLoc()); |
196 | 20 | TheParser.Diag(Loc, diag::err_inline_ms_asm_parsing) << D.getMessage(); |
197 | 20 | } |
198 | | |
199 | | /// Parse an identifier in an MS-style inline assembly block. |
200 | | ExprResult Parser::ParseMSAsmIdentifier(llvm::SmallVectorImpl<Token> &LineToks, |
201 | | unsigned &NumLineToksConsumed, |
202 | 234 | bool IsUnevaluatedContext) { |
203 | | // Push a fake token on the end so that we don't overrun the token |
204 | | // stream. We use ';' because it expression-parsing should never |
205 | | // overrun it. |
206 | 234 | const tok::TokenKind EndOfStream = tok::semi; |
207 | 234 | Token EndOfStreamTok; |
208 | 234 | EndOfStreamTok.startToken(); |
209 | 234 | EndOfStreamTok.setKind(EndOfStream); |
210 | 234 | LineToks.push_back(EndOfStreamTok); |
211 | | |
212 | | // Also copy the current token over. |
213 | 234 | LineToks.push_back(Tok); |
214 | | |
215 | 234 | PP.EnterTokenStream(LineToks, /*DisableMacroExpansions*/ true, |
216 | 234 | /*IsReinject*/ true); |
217 | | |
218 | | // Clear the current token and advance to the first token in LineToks. |
219 | 234 | ConsumeAnyToken(); |
220 | | |
221 | | // Parse an optional scope-specifier if we're in C++. |
222 | 234 | CXXScopeSpec SS; |
223 | 234 | if (getLangOpts().CPlusPlus) |
224 | 64 | ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, |
225 | 64 | /*ObjectHasErrors=*/false, |
226 | 64 | /*EnteringContext=*/false); |
227 | | |
228 | | // Require an identifier here. |
229 | 234 | SourceLocation TemplateKWLoc; |
230 | 234 | UnqualifiedId Id; |
231 | 234 | bool Invalid = true; |
232 | 234 | ExprResult Result; |
233 | 234 | if (Tok.is(tok::kw_this)) { |
234 | 3 | Result = ParseCXXThis(); |
235 | 3 | Invalid = false; |
236 | 231 | } else { |
237 | 231 | Invalid = |
238 | 231 | ParseUnqualifiedId(SS, /*ObjectType=*/nullptr, |
239 | 231 | /*ObjectHadErrors=*/false, |
240 | 231 | /*EnteringContext=*/false, |
241 | 231 | /*AllowDestructorName=*/false, |
242 | 231 | /*AllowConstructorName=*/false, |
243 | 231 | /*AllowDeductionGuide=*/false, &TemplateKWLoc, Id); |
244 | | // Perform the lookup. |
245 | 231 | Result = Actions.LookupInlineAsmIdentifier(SS, TemplateKWLoc, Id, |
246 | 231 | IsUnevaluatedContext); |
247 | 231 | } |
248 | | // While the next two tokens are 'period' 'identifier', repeatedly parse it as |
249 | | // a field access. We have to avoid consuming assembler directives that look |
250 | | // like '.' 'else'. |
251 | 276 | while (Result.isUsable() && Tok.is(tok::period)252 ) { |
252 | 42 | Token IdTok = PP.LookAhead(0); |
253 | 42 | if (IdTok.isNot(tok::identifier)) |
254 | 0 | break; |
255 | 42 | ConsumeToken(); // Consume the period. |
256 | 42 | IdentifierInfo *Id = Tok.getIdentifierInfo(); |
257 | 42 | ConsumeToken(); // Consume the identifier. |
258 | 42 | Result = Actions.LookupInlineAsmVarDeclField(Result.get(), Id->getName(), |
259 | 42 | Tok.getLocation()); |
260 | 42 | } |
261 | | |
262 | | // Figure out how many tokens we are into LineToks. |
263 | 234 | unsigned LineIndex = 0; |
264 | 234 | if (Tok.is(EndOfStream)) { |
265 | 49 | LineIndex = LineToks.size() - 2; |
266 | 185 | } else { |
267 | 480 | while (LineToks[LineIndex].getLocation() != Tok.getLocation()) { |
268 | 295 | LineIndex++; |
269 | 295 | assert(LineIndex < LineToks.size() - 2); // we added two extra tokens |
270 | 295 | } |
271 | 185 | } |
272 | | |
273 | | // If we've run into the poison token we inserted before, or there |
274 | | // was a parsing error, then claim the entire line. |
275 | 234 | if (Invalid || Tok.is(EndOfStream)) { |
276 | 49 | NumLineToksConsumed = LineToks.size() - 2; |
277 | 185 | } else { |
278 | | // Otherwise, claim up to the start of the next token. |
279 | 185 | NumLineToksConsumed = LineIndex; |
280 | 185 | } |
281 | | |
282 | | // Finally, restore the old parsing state by consuming all the tokens we |
283 | | // staged before, implicitly killing off the token-lexer we pushed. |
284 | 3.71k | for (unsigned i = 0, e = LineToks.size() - LineIndex - 2; i != e; ++i3.48k ) { |
285 | 3.48k | ConsumeAnyToken(); |
286 | 3.48k | } |
287 | 234 | assert(Tok.is(EndOfStream)); |
288 | 0 | ConsumeToken(); |
289 | | |
290 | | // Leave LineToks in its original state. |
291 | 234 | LineToks.pop_back(); |
292 | 234 | LineToks.pop_back(); |
293 | | |
294 | 234 | return Result; |
295 | 234 | } |
296 | | |
297 | | /// Turn a sequence of our tokens back into a string that we can hand |
298 | | /// to the MC asm parser. |
299 | | static bool buildMSAsmString(Preprocessor &PP, SourceLocation AsmLoc, |
300 | | ArrayRef<Token> AsmToks, |
301 | | SmallVectorImpl<unsigned> &TokOffsets, |
302 | 239 | SmallString<512> &Asm) { |
303 | 239 | assert(!AsmToks.empty() && "Didn't expect an empty AsmToks!"); |
304 | | |
305 | | // Is this the start of a new assembly statement? |
306 | 0 | bool isNewStatement = true; |
307 | | |
308 | 3.12k | for (unsigned i = 0, e = AsmToks.size(); i < e; ++i2.88k ) { |
309 | 2.88k | const Token &Tok = AsmToks[i]; |
310 | | |
311 | | // Start each new statement with a newline and a tab. |
312 | 2.88k | if (!isNewStatement && (2.48k Tok.is(tok::kw_asm)2.48k || Tok.isAtStartOfLine()2.33k )) { |
313 | 256 | Asm += "\n\t"; |
314 | 256 | isNewStatement = true; |
315 | 256 | } |
316 | | |
317 | | // Preserve the existence of leading whitespace except at the |
318 | | // start of a statement. |
319 | 2.88k | if (!isNewStatement && Tok.hasLeadingSpace()2.23k ) |
320 | 1.14k | Asm += ' '; |
321 | | |
322 | | // Remember the offset of this token. |
323 | 2.88k | TokOffsets.push_back(Asm.size()); |
324 | | |
325 | | // Don't actually write '__asm' into the assembly stream. |
326 | 2.88k | if (Tok.is(tok::kw_asm)) { |
327 | | // Complain about __asm at the end of the stream. |
328 | 156 | if (i + 1 == e) { |
329 | 1 | PP.Diag(AsmLoc, diag::err_asm_empty); |
330 | 1 | return true; |
331 | 1 | } |
332 | | |
333 | 155 | continue; |
334 | 156 | } |
335 | | |
336 | | // Append the spelling of the token. |
337 | 2.72k | SmallString<32> SpellingBuffer; |
338 | 2.72k | bool SpellingInvalid = false; |
339 | 2.72k | Asm += PP.getSpelling(Tok, SpellingBuffer, &SpellingInvalid); |
340 | 2.72k | assert(!SpellingInvalid && "spelling was invalid after correct parse?"); |
341 | | |
342 | | // We are no longer at the start of a statement. |
343 | 0 | isNewStatement = false; |
344 | 2.72k | } |
345 | | |
346 | | // Ensure that the buffer is null-terminated. |
347 | 238 | Asm.push_back('\0'); |
348 | 238 | Asm.pop_back(); |
349 | | |
350 | 238 | assert(TokOffsets.size() == AsmToks.size()); |
351 | 0 | return false; |
352 | 239 | } |
353 | | |
354 | | // Determine if this is a GCC-style asm statement. |
355 | 863 | bool Parser::isGCCAsmStatement(const Token &TokAfterAsm) const { |
356 | 863 | return TokAfterAsm.is(tok::l_paren) || isGNUAsmQualifier(TokAfterAsm)686 ; |
357 | 863 | } |
358 | | |
359 | 58.8k | bool Parser::isGNUAsmQualifier(const Token &TokAfterAsm) const { |
360 | 58.8k | return getGNUAsmQualifier(TokAfterAsm) != GNUAsmQualifiers::AQ_unspecified; |
361 | 58.8k | } |
362 | | |
363 | | /// ParseMicrosoftAsmStatement. When -fms-extensions/-fasm-blocks is enabled, |
364 | | /// this routine is called to collect the tokens for an MS asm statement. |
365 | | /// |
366 | | /// [MS] ms-asm-statement: |
367 | | /// ms-asm-block |
368 | | /// ms-asm-block ms-asm-statement |
369 | | /// |
370 | | /// [MS] ms-asm-block: |
371 | | /// '__asm' ms-asm-line '\n' |
372 | | /// '__asm' '{' ms-asm-instruction-block[opt] '}' ';'[opt] |
373 | | /// |
374 | | /// [MS] ms-asm-instruction-block |
375 | | /// ms-asm-line |
376 | | /// ms-asm-line '\n' ms-asm-instruction-block |
377 | | /// |
378 | 254 | StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) { |
379 | 254 | SourceManager &SrcMgr = PP.getSourceManager(); |
380 | 254 | SourceLocation EndLoc = AsmLoc; |
381 | 254 | SmallVector<Token, 4> AsmToks; |
382 | | |
383 | 254 | bool SingleLineMode = true; |
384 | 254 | unsigned BraceNesting = 0; |
385 | 254 | unsigned short savedBraceCount = BraceCount; |
386 | 254 | bool InAsmComment = false; |
387 | 254 | FileID FID; |
388 | 254 | unsigned LineNo = 0; |
389 | 254 | unsigned NumTokensRead = 0; |
390 | 254 | SmallVector<SourceLocation, 4> LBraceLocs; |
391 | 254 | bool SkippedStartOfLine = false; |
392 | | |
393 | 254 | if (Tok.is(tok::l_brace)) { |
394 | | // Braced inline asm: consume the opening brace. |
395 | 136 | SingleLineMode = false; |
396 | 136 | BraceNesting = 1; |
397 | 136 | EndLoc = ConsumeBrace(); |
398 | 136 | LBraceLocs.push_back(EndLoc); |
399 | 136 | ++NumTokensRead; |
400 | 136 | } else { |
401 | | // Single-line inline asm; compute which line it is on. |
402 | 118 | std::pair<FileID, unsigned> ExpAsmLoc = |
403 | 118 | SrcMgr.getDecomposedExpansionLoc(EndLoc); |
404 | 118 | FID = ExpAsmLoc.first; |
405 | 118 | LineNo = SrcMgr.getLineNumber(FID, ExpAsmLoc.second); |
406 | 118 | LBraceLocs.push_back(SourceLocation()); |
407 | 118 | } |
408 | | |
409 | 254 | SourceLocation TokLoc = Tok.getLocation(); |
410 | 3.29k | do { |
411 | | // If we hit EOF, we're done, period. |
412 | 3.29k | if (isEofOrEom()) |
413 | 3 | break; |
414 | | |
415 | 3.28k | if (!InAsmComment && Tok.is(tok::l_brace)3.13k ) { |
416 | | // Consume the opening brace. |
417 | 17 | SkippedStartOfLine = Tok.isAtStartOfLine(); |
418 | 17 | AsmToks.push_back(Tok); |
419 | 17 | EndLoc = ConsumeBrace(); |
420 | 17 | BraceNesting++; |
421 | 17 | LBraceLocs.push_back(EndLoc); |
422 | 17 | TokLoc = Tok.getLocation(); |
423 | 17 | ++NumTokensRead; |
424 | 17 | continue; |
425 | 3.27k | } else if (!InAsmComment && Tok.is(tok::semi)3.12k ) { |
426 | | // A semicolon in an asm is the start of a comment. |
427 | 48 | InAsmComment = true; |
428 | 48 | if (!SingleLineMode) { |
429 | | // Compute which line the comment is on. |
430 | 9 | std::pair<FileID, unsigned> ExpSemiLoc = |
431 | 9 | SrcMgr.getDecomposedExpansionLoc(TokLoc); |
432 | 9 | FID = ExpSemiLoc.first; |
433 | 9 | LineNo = SrcMgr.getLineNumber(FID, ExpSemiLoc.second); |
434 | 9 | } |
435 | 3.22k | } else if (SingleLineMode || InAsmComment1.46k ) { |
436 | | // If end-of-line is significant, check whether this token is on a |
437 | | // new line. |
438 | 1.79k | std::pair<FileID, unsigned> ExpLoc = |
439 | 1.79k | SrcMgr.getDecomposedExpansionLoc(TokLoc); |
440 | 1.79k | if (ExpLoc.first != FID || |
441 | 1.79k | SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second) != LineNo) { |
442 | | // If this is a single-line __asm, we're done, except if the next |
443 | | // line is MS-style asm too, in which case we finish a comment |
444 | | // if needed and then keep processing the next line as a single |
445 | | // line __asm. |
446 | 250 | bool isAsm = Tok.is(tok::kw_asm); |
447 | 250 | if (SingleLineMode && (240 !isAsm240 || isGCCAsmStatement(NextToken())145 )) |
448 | 101 | break; |
449 | | // We're no longer in a comment. |
450 | 149 | InAsmComment = false; |
451 | 149 | if (isAsm) { |
452 | | // If this is a new __asm {} block we want to process it separately |
453 | | // from the single-line __asm statements |
454 | 139 | if (PP.LookAhead(0).is(tok::l_brace)) |
455 | 7 | break; |
456 | 132 | LineNo = SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second); |
457 | 132 | SkippedStartOfLine = Tok.isAtStartOfLine(); |
458 | 132 | } else if (10 Tok.is(tok::semi)10 ) { |
459 | | // A multi-line asm-statement, where next line is a comment |
460 | 1 | InAsmComment = true; |
461 | 1 | FID = ExpLoc.first; |
462 | 1 | LineNo = SrcMgr.getLineNumber(FID, ExpLoc.second); |
463 | 1 | } |
464 | 1.54k | } else if (!InAsmComment && Tok.is(tok::r_brace)1.44k ) { |
465 | | // In MSVC mode, braces only participate in brace matching and |
466 | | // separating the asm statements. This is an intentional |
467 | | // departure from the Apple gcc behavior. |
468 | 15 | if (!BraceNesting) |
469 | 10 | break; |
470 | 15 | } |
471 | 1.79k | } |
472 | 3.15k | if (!InAsmComment && BraceNesting3.00k && Tok.is(tok::r_brace)1.45k && |
473 | 3.15k | BraceCount == (savedBraceCount + BraceNesting)149 ) { |
474 | | // Consume the closing brace. |
475 | 149 | SkippedStartOfLine = Tok.isAtStartOfLine(); |
476 | | // Don't want to add the closing brace of the whole asm block |
477 | 149 | if (SingleLineMode || BraceNesting > 1144 ) { |
478 | 16 | Tok.clearFlag(Token::LeadingSpace); |
479 | 16 | AsmToks.push_back(Tok); |
480 | 16 | } |
481 | 149 | EndLoc = ConsumeBrace(); |
482 | 149 | BraceNesting--; |
483 | | // Finish if all of the opened braces in the inline asm section were |
484 | | // consumed. |
485 | 149 | if (BraceNesting == 0 && !SingleLineMode138 ) |
486 | 133 | break; |
487 | 16 | else { |
488 | 16 | LBraceLocs.pop_back(); |
489 | 16 | TokLoc = Tok.getLocation(); |
490 | 16 | ++NumTokensRead; |
491 | 16 | continue; |
492 | 16 | } |
493 | 149 | } |
494 | | |
495 | | // Consume the next token; make sure we don't modify the brace count etc. |
496 | | // if we are in a comment. |
497 | 3.00k | EndLoc = TokLoc; |
498 | 3.00k | if (InAsmComment) |
499 | 150 | PP.Lex(Tok); |
500 | 2.85k | else { |
501 | | // Set the token as the start of line if we skipped the original start |
502 | | // of line token in case it was a nested brace. |
503 | 2.85k | if (SkippedStartOfLine) |
504 | 139 | Tok.setFlag(Token::StartOfLine); |
505 | 2.85k | AsmToks.push_back(Tok); |
506 | 2.85k | ConsumeAnyToken(); |
507 | 2.85k | } |
508 | 3.00k | TokLoc = Tok.getLocation(); |
509 | 3.00k | ++NumTokensRead; |
510 | 3.00k | SkippedStartOfLine = false; |
511 | 3.03k | } while (true); |
512 | | |
513 | 254 | if (BraceNesting && BraceCount != savedBraceCount3 ) { |
514 | | // __asm without closing brace (this can happen at EOF). |
515 | 7 | for (unsigned i = 0; i < BraceNesting; ++i4 ) { |
516 | 4 | Diag(Tok, diag::err_expected) << tok::r_brace; |
517 | 4 | Diag(LBraceLocs.back(), diag::note_matching) << tok::l_brace; |
518 | 4 | LBraceLocs.pop_back(); |
519 | 4 | } |
520 | 3 | return StmtError(); |
521 | 251 | } else if (NumTokensRead == 0) { |
522 | | // Empty __asm. |
523 | 2 | Diag(Tok, diag::err_expected) << tok::l_brace; |
524 | 2 | return StmtError(); |
525 | 2 | } |
526 | | |
527 | | // Okay, prepare to use MC to parse the assembly. |
528 | 249 | SmallVector<StringRef, 4> ConstraintRefs; |
529 | 249 | SmallVector<Expr *, 4> Exprs; |
530 | 249 | SmallVector<StringRef, 4> ClobberRefs; |
531 | | |
532 | | // We need an actual supported target. |
533 | 249 | const llvm::Triple &TheTriple = Actions.Context.getTargetInfo().getTriple(); |
534 | 249 | const std::string &TT = TheTriple.getTriple(); |
535 | 249 | const llvm::Target *TheTarget = nullptr; |
536 | 249 | if (!TheTriple.isX86()) { |
537 | 1 | Diag(AsmLoc, diag::err_msasm_unsupported_arch) << TheTriple.getArchName(); |
538 | 248 | } else { |
539 | 248 | std::string Error; |
540 | 248 | TheTarget = llvm::TargetRegistry::lookupTarget(TT, Error); |
541 | 248 | if (!TheTarget) |
542 | 0 | Diag(AsmLoc, diag::err_msasm_unable_to_create_target) << Error; |
543 | 248 | } |
544 | | |
545 | 249 | assert(!LBraceLocs.empty() && "Should have at least one location here"); |
546 | | |
547 | 0 | SmallString<512> AsmString; |
548 | 249 | auto EmptyStmt = [&] { |
549 | 10 | return Actions.ActOnMSAsmStmt(AsmLoc, LBraceLocs[0], AsmToks, AsmString, |
550 | 10 | /*NumOutputs*/ 0, /*NumInputs*/ 0, |
551 | 10 | ConstraintRefs, ClobberRefs, Exprs, EndLoc); |
552 | 10 | }; |
553 | | // If we don't support assembly, or the assembly is empty, we don't |
554 | | // need to instantiate the AsmParser, etc. |
555 | 249 | if (!TheTarget || AsmToks.empty()248 ) { |
556 | 10 | return EmptyStmt(); |
557 | 10 | } |
558 | | |
559 | | // Expand the tokens into a string buffer. |
560 | 239 | SmallVector<unsigned, 8> TokOffsets; |
561 | 239 | if (buildMSAsmString(PP, AsmLoc, AsmToks, TokOffsets, AsmString)) |
562 | 1 | return StmtError(); |
563 | | |
564 | 238 | const TargetOptions &TO = Actions.Context.getTargetInfo().getTargetOpts(); |
565 | 238 | std::string FeaturesStr = |
566 | 238 | llvm::join(TO.Features.begin(), TO.Features.end(), ","); |
567 | | |
568 | 238 | std::unique_ptr<llvm::MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TT)); |
569 | 238 | if (!MRI) { |
570 | 0 | Diag(AsmLoc, diag::err_msasm_unable_to_create_target) |
571 | 0 | << "target MC unavailable"; |
572 | 0 | return EmptyStmt(); |
573 | 0 | } |
574 | | // FIXME: init MCOptions from sanitizer flags here. |
575 | 238 | llvm::MCTargetOptions MCOptions; |
576 | 238 | std::unique_ptr<llvm::MCAsmInfo> MAI( |
577 | 238 | TheTarget->createMCAsmInfo(*MRI, TT, MCOptions)); |
578 | | // Get the instruction descriptor. |
579 | 238 | std::unique_ptr<llvm::MCInstrInfo> MII(TheTarget->createMCInstrInfo()); |
580 | 238 | std::unique_ptr<llvm::MCSubtargetInfo> STI( |
581 | 238 | TheTarget->createMCSubtargetInfo(TT, TO.CPU, FeaturesStr)); |
582 | | // Target MCTargetDesc may not be linked in clang-based tools. |
583 | | |
584 | 238 | if (!MAI || !MII || !STI) { |
585 | 0 | Diag(AsmLoc, diag::err_msasm_unable_to_create_target) |
586 | 0 | << "target MC unavailable"; |
587 | 0 | return EmptyStmt(); |
588 | 0 | } |
589 | | |
590 | 238 | llvm::SourceMgr TempSrcMgr; |
591 | 238 | llvm::MCContext Ctx(TheTriple, MAI.get(), MRI.get(), STI.get(), &TempSrcMgr); |
592 | 238 | std::unique_ptr<llvm::MCObjectFileInfo> MOFI( |
593 | 238 | TheTarget->createMCObjectFileInfo(Ctx, /*PIC=*/false)); |
594 | 238 | Ctx.setObjectFileInfo(MOFI.get()); |
595 | | |
596 | 238 | std::unique_ptr<llvm::MemoryBuffer> Buffer = |
597 | 238 | llvm::MemoryBuffer::getMemBuffer(AsmString, "<MS inline asm>"); |
598 | | |
599 | | // Tell SrcMgr about this buffer, which is what the parser will pick up. |
600 | 238 | TempSrcMgr.AddNewSourceBuffer(std::move(Buffer), llvm::SMLoc()); |
601 | | |
602 | 238 | std::unique_ptr<llvm::MCStreamer> Str(createNullStreamer(Ctx)); |
603 | 238 | std::unique_ptr<llvm::MCAsmParser> Parser( |
604 | 238 | createMCAsmParser(TempSrcMgr, Ctx, *Str.get(), *MAI)); |
605 | | |
606 | 238 | std::unique_ptr<llvm::MCTargetAsmParser> TargetParser( |
607 | 238 | TheTarget->createMCAsmParser(*STI, *Parser, *MII, MCOptions)); |
608 | | // Target AsmParser may not be linked in clang-based tools. |
609 | 238 | if (!TargetParser) { |
610 | 0 | Diag(AsmLoc, diag::err_msasm_unable_to_create_target) |
611 | 0 | << "target ASM parser unavailable"; |
612 | 0 | return EmptyStmt(); |
613 | 0 | } |
614 | | |
615 | 238 | std::unique_ptr<llvm::MCInstPrinter> IP( |
616 | 238 | TheTarget->createMCInstPrinter(llvm::Triple(TT), 1, *MAI, *MII, *MRI)); |
617 | | |
618 | | // Change to the Intel dialect. |
619 | 238 | Parser->setAssemblerDialect(1); |
620 | 238 | Parser->setTargetParser(*TargetParser.get()); |
621 | 238 | Parser->setParsingMSInlineAsm(true); |
622 | 238 | TargetParser->setParsingMSInlineAsm(true); |
623 | | |
624 | 238 | ClangAsmParserCallback Callback(*this, AsmLoc, AsmString, AsmToks, |
625 | 238 | TokOffsets); |
626 | 238 | TargetParser->setSemaCallback(&Callback); |
627 | 238 | TempSrcMgr.setDiagHandler(ClangAsmParserCallback::DiagHandlerCallback, |
628 | 238 | &Callback); |
629 | | |
630 | 238 | unsigned NumOutputs; |
631 | 238 | unsigned NumInputs; |
632 | 238 | std::string AsmStringIR; |
633 | 238 | SmallVector<std::pair<void *, bool>, 4> OpExprs; |
634 | 238 | SmallVector<std::string, 4> Constraints; |
635 | 238 | SmallVector<std::string, 4> Clobbers; |
636 | 238 | if (Parser->parseMSInlineAsm(AsmStringIR, NumOutputs, NumInputs, OpExprs, |
637 | 238 | Constraints, Clobbers, MII.get(), IP.get(), |
638 | 238 | Callback)) |
639 | 20 | return StmtError(); |
640 | | |
641 | | // Filter out "fpsw" and "mxcsr". They aren't valid GCC asm clobber |
642 | | // constraints. Clang always adds fpsr to the clobber list anyway. |
643 | 241 | llvm::erase_if(Clobbers, [](const std::string &C) 218 { |
644 | 241 | return C == "fpsr" || C == "mxcsr"236 ; |
645 | 241 | }); |
646 | | |
647 | | // Build the vector of clobber StringRefs. |
648 | 218 | ClobberRefs.insert(ClobberRefs.end(), Clobbers.begin(), Clobbers.end()); |
649 | | |
650 | | // Recast the void pointers and build the vector of constraint StringRefs. |
651 | 218 | unsigned NumExprs = NumOutputs + NumInputs; |
652 | 218 | ConstraintRefs.resize(NumExprs); |
653 | 218 | Exprs.resize(NumExprs); |
654 | 372 | for (unsigned i = 0, e = NumExprs; i != e; ++i154 ) { |
655 | 154 | Expr *OpExpr = static_cast<Expr *>(OpExprs[i].first); |
656 | 154 | if (!OpExpr) |
657 | 0 | return StmtError(); |
658 | | |
659 | | // Need address of variable. |
660 | 154 | if (OpExprs[i].second) |
661 | 10 | OpExpr = |
662 | 10 | Actions.BuildUnaryOp(getCurScope(), AsmLoc, UO_AddrOf, OpExpr).get(); |
663 | | |
664 | 154 | ConstraintRefs[i] = StringRef(Constraints[i]); |
665 | 154 | Exprs[i] = OpExpr; |
666 | 154 | } |
667 | | |
668 | | // FIXME: We should be passing source locations for better diagnostics. |
669 | 218 | return Actions.ActOnMSAsmStmt(AsmLoc, LBraceLocs[0], AsmToks, AsmStringIR, |
670 | 218 | NumOutputs, NumInputs, ConstraintRefs, |
671 | 218 | ClobberRefs, Exprs, EndLoc); |
672 | 218 | } |
673 | | |
674 | | /// parseGNUAsmQualifierListOpt - Parse a GNU extended asm qualifier list. |
675 | | /// asm-qualifier: |
676 | | /// volatile |
677 | | /// inline |
678 | | /// goto |
679 | | /// |
680 | | /// asm-qualifier-list: |
681 | | /// asm-qualifier |
682 | | /// asm-qualifier-list asm-qualifier |
683 | 5.38k | bool Parser::parseGNUAsmQualifierListOpt(GNUAsmQualifiers &AQ) { |
684 | 7.19k | while (true) { |
685 | 7.19k | const GNUAsmQualifiers::AQ A = getGNUAsmQualifier(Tok); |
686 | 7.19k | if (A == GNUAsmQualifiers::AQ_unspecified) { |
687 | 5.38k | if (Tok.isNot(tok::l_paren)) { |
688 | 3 | Diag(Tok.getLocation(), diag::err_asm_qualifier_ignored); |
689 | 3 | SkipUntil(tok::r_paren, StopAtSemi); |
690 | 3 | return true; |
691 | 3 | } |
692 | 5.38k | return false; |
693 | 5.38k | } |
694 | 1.80k | if (AQ.setAsmQualifier(A)) |
695 | 6 | Diag(Tok.getLocation(), diag::err_asm_duplicate_qual) |
696 | 6 | << GNUAsmQualifiers::getQualifierName(A); |
697 | 1.80k | ConsumeToken(); |
698 | 1.80k | } |
699 | 0 | return false; |
700 | 5.38k | } |
701 | | |
702 | | /// ParseAsmStatement - Parse a GNU extended asm statement. |
703 | | /// asm-statement: |
704 | | /// gnu-asm-statement |
705 | | /// ms-asm-statement |
706 | | /// |
707 | | /// [GNU] gnu-asm-statement: |
708 | | /// 'asm' asm-qualifier-list[opt] '(' asm-argument ')' ';' |
709 | | /// |
710 | | /// [GNU] asm-argument: |
711 | | /// asm-string-literal |
712 | | /// asm-string-literal ':' asm-operands[opt] |
713 | | /// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt] |
714 | | /// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt] |
715 | | /// ':' asm-clobbers |
716 | | /// |
717 | | /// [GNU] asm-clobbers: |
718 | | /// asm-string-literal |
719 | | /// asm-clobbers ',' asm-string-literal |
720 | | /// |
721 | 5.64k | StmtResult Parser::ParseAsmStatement(bool &msAsm) { |
722 | 5.64k | assert(Tok.is(tok::kw_asm) && "Not an asm stmt"); |
723 | 0 | SourceLocation AsmLoc = ConsumeToken(); |
724 | | |
725 | 5.64k | if (getLangOpts().AsmBlocks && !isGCCAsmStatement(Tok)718 ) { |
726 | 254 | msAsm = true; |
727 | 254 | return ParseMicrosoftAsmStatement(AsmLoc); |
728 | 254 | } |
729 | | |
730 | 5.38k | SourceLocation Loc = Tok.getLocation(); |
731 | 5.38k | GNUAsmQualifiers GAQ; |
732 | 5.38k | if (parseGNUAsmQualifierListOpt(GAQ)) |
733 | 3 | return StmtError(); |
734 | | |
735 | 5.38k | if (GAQ.isGoto() && getLangOpts().SpeculativeLoadHardening156 ) |
736 | 2 | Diag(Loc, diag::warn_slh_does_not_support_asm_goto); |
737 | | |
738 | 5.38k | BalancedDelimiterTracker T(*this, tok::l_paren); |
739 | 5.38k | T.consumeOpen(); |
740 | | |
741 | 5.38k | ExprResult AsmString(ParseAsmStringLiteral(/*ForAsmLabel*/ false)); |
742 | | |
743 | | // Check if GNU-style InlineAsm is disabled. |
744 | | // Error on anything other than empty string. |
745 | 5.38k | if (!(getLangOpts().GNUAsm || AsmString.isInvalid()2 )) { |
746 | 2 | const auto *SL = cast<StringLiteral>(AsmString.get()); |
747 | 2 | if (!SL->getString().trim().empty()) |
748 | 1 | Diag(Loc, diag::err_gnu_inline_asm_disabled); |
749 | 2 | } |
750 | | |
751 | 5.38k | if (AsmString.isInvalid()) { |
752 | | // Consume up to and including the closing paren. |
753 | 2 | T.skipToEnd(); |
754 | 2 | return StmtError(); |
755 | 2 | } |
756 | | |
757 | 5.38k | SmallVector<IdentifierInfo *, 4> Names; |
758 | 5.38k | ExprVector Constraints; |
759 | 5.38k | ExprVector Exprs; |
760 | 5.38k | ExprVector Clobbers; |
761 | | |
762 | 5.38k | if (Tok.is(tok::r_paren)) { |
763 | | // We have a simple asm expression like 'asm("foo")'. |
764 | 342 | T.consumeClose(); |
765 | 342 | return Actions.ActOnGCCAsmStmt( |
766 | 342 | AsmLoc, /*isSimple*/ true, GAQ.isVolatile(), |
767 | 342 | /*NumOutputs*/ 0, /*NumInputs*/ 0, nullptr, Constraints, Exprs, |
768 | 342 | AsmString.get(), Clobbers, /*NumLabels*/ 0, T.getCloseLocation()); |
769 | 342 | } |
770 | | |
771 | | // Parse Outputs, if present. |
772 | 5.04k | bool AteExtraColon = false; |
773 | 5.04k | if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)216 ) { |
774 | | // In C++ mode, parse "::" like ": :". |
775 | 5.04k | AteExtraColon = Tok.is(tok::coloncolon); |
776 | 5.04k | ConsumeToken(); |
777 | | |
778 | 5.04k | if (!AteExtraColon && ParseAsmOperandsOpt(Names, Constraints, Exprs)4.82k ) |
779 | 2 | return StmtError(); |
780 | 5.04k | } |
781 | | |
782 | 5.03k | unsigned NumOutputs = Names.size(); |
783 | | |
784 | | // Parse Inputs, if present. |
785 | 5.03k | if (AteExtraColon || Tok.is(tok::colon)4.82k || Tok.is(tok::coloncolon)162 ) { |
786 | | // In C++ mode, parse "::" like ": :". |
787 | 4.89k | if (AteExtraColon) |
788 | 216 | AteExtraColon = false; |
789 | 4.67k | else { |
790 | 4.67k | AteExtraColon = Tok.is(tok::coloncolon); |
791 | 4.67k | ConsumeToken(); |
792 | 4.67k | } |
793 | | |
794 | 4.89k | if (!AteExtraColon && ParseAsmOperandsOpt(Names, Constraints, Exprs)4.87k ) |
795 | 3 | return StmtError(); |
796 | 4.89k | } |
797 | | |
798 | 5.03k | assert(Names.size() == Constraints.size() && |
799 | 5.03k | Constraints.size() == Exprs.size() && "Input operand size mismatch!"); |
800 | | |
801 | 0 | unsigned NumInputs = Names.size() - NumOutputs; |
802 | | |
803 | | // Parse the clobbers, if present. |
804 | 5.03k | if (AteExtraColon || Tok.is(tok::colon)5.01k || Tok.is(tok::coloncolon)2.06k ) { |
805 | 3.00k | if (AteExtraColon) |
806 | 18 | AteExtraColon = false; |
807 | 2.98k | else { |
808 | 2.98k | AteExtraColon = Tok.is(tok::coloncolon); |
809 | 2.98k | ConsumeToken(); |
810 | 2.98k | } |
811 | | // Parse the asm-string list for clobbers if present. |
812 | 3.00k | if (!AteExtraColon && isTokenStringLiteral()2.97k ) { |
813 | 3.42k | while (true) { |
814 | 3.42k | ExprResult Clobber(ParseAsmStringLiteral(/*ForAsmLabel*/ false)); |
815 | | |
816 | 3.42k | if (Clobber.isInvalid()) |
817 | 0 | break; |
818 | | |
819 | 3.42k | Clobbers.push_back(Clobber.get()); |
820 | | |
821 | 3.42k | if (!TryConsumeToken(tok::comma)) |
822 | 2.66k | break; |
823 | 3.42k | } |
824 | 2.66k | } |
825 | 3.00k | } |
826 | 5.03k | if (!GAQ.isGoto() && (4.88k Tok.isNot(tok::r_paren)4.88k || AteExtraColon4.87k )) { |
827 | 8 | Diag(Tok, diag::err_expected) << tok::r_paren; |
828 | 8 | SkipUntil(tok::r_paren, StopAtSemi); |
829 | 8 | return StmtError(); |
830 | 8 | } |
831 | | |
832 | | // Parse the goto label, if present. |
833 | 5.02k | unsigned NumLabels = 0; |
834 | 5.02k | if (AteExtraColon || Tok.is(tok::colon)5.00k ) { |
835 | 140 | if (!AteExtraColon) |
836 | 115 | ConsumeToken(); |
837 | | |
838 | 196 | while (true) { |
839 | 196 | if (Tok.isNot(tok::identifier)) { |
840 | 8 | Diag(Tok, diag::err_expected) << tok::identifier; |
841 | 8 | SkipUntil(tok::r_paren, StopAtSemi); |
842 | 8 | return StmtError(); |
843 | 8 | } |
844 | 188 | LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(), |
845 | 188 | Tok.getLocation()); |
846 | 188 | Names.push_back(Tok.getIdentifierInfo()); |
847 | 188 | if (!LD) { |
848 | 0 | SkipUntil(tok::r_paren, StopAtSemi); |
849 | 0 | return StmtError(); |
850 | 0 | } |
851 | 188 | ExprResult Res = |
852 | 188 | Actions.ActOnAddrLabel(Tok.getLocation(), Tok.getLocation(), LD); |
853 | 188 | Exprs.push_back(Res.get()); |
854 | 188 | NumLabels++; |
855 | 188 | ConsumeToken(); |
856 | 188 | if (!TryConsumeToken(tok::comma)) |
857 | 132 | break; |
858 | 188 | } |
859 | 4.88k | } else if (GAQ.isGoto()) { |
860 | 8 | Diag(Tok, diag::err_expected) << tok::colon; |
861 | 8 | SkipUntil(tok::r_paren, StopAtSemi); |
862 | 8 | return StmtError(); |
863 | 8 | } |
864 | 5.01k | T.consumeClose(); |
865 | 5.01k | return Actions.ActOnGCCAsmStmt(AsmLoc, false, GAQ.isVolatile(), NumOutputs, |
866 | 5.01k | NumInputs, Names.data(), Constraints, Exprs, |
867 | 5.01k | AsmString.get(), Clobbers, NumLabels, |
868 | 5.01k | T.getCloseLocation()); |
869 | 5.02k | } |
870 | | |
871 | | /// ParseAsmOperands - Parse the asm-operands production as used by |
872 | | /// asm-statement, assuming the leading ':' token was eaten. |
873 | | /// |
874 | | /// [GNU] asm-operands: |
875 | | /// asm-operand |
876 | | /// asm-operands ',' asm-operand |
877 | | /// |
878 | | /// [GNU] asm-operand: |
879 | | /// asm-string-literal '(' expression ')' |
880 | | /// '[' identifier ']' asm-string-literal '(' expression ')' |
881 | | /// |
882 | | // |
883 | | // FIXME: Avoid unnecessary std::string trashing. |
884 | | bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names, |
885 | | SmallVectorImpl<Expr *> &Constraints, |
886 | 9.70k | SmallVectorImpl<Expr *> &Exprs) { |
887 | | // 'asm-operands' isn't present? |
888 | 9.70k | if (!isTokenStringLiteral() && Tok.isNot(tok::l_square)3.02k ) |
889 | 2.59k | return false; |
890 | | |
891 | 16.6k | while (7.10k true) { |
892 | | // Read the [id] if present. |
893 | 16.6k | if (Tok.is(tok::l_square)) { |
894 | 721 | BalancedDelimiterTracker T(*this, tok::l_square); |
895 | 721 | T.consumeOpen(); |
896 | | |
897 | 721 | if (Tok.isNot(tok::identifier)) { |
898 | 0 | Diag(Tok, diag::err_expected) << tok::identifier; |
899 | 0 | SkipUntil(tok::r_paren, StopAtSemi); |
900 | 0 | return true; |
901 | 0 | } |
902 | | |
903 | 721 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
904 | 721 | ConsumeToken(); |
905 | | |
906 | 721 | Names.push_back(II); |
907 | 721 | T.consumeClose(); |
908 | 721 | } else |
909 | 15.9k | Names.push_back(nullptr); |
910 | | |
911 | 16.6k | ExprResult Constraint(ParseAsmStringLiteral(/*ForAsmLabel*/ false)); |
912 | 16.6k | if (Constraint.isInvalid()) { |
913 | 1 | SkipUntil(tok::r_paren, StopAtSemi); |
914 | 1 | return true; |
915 | 1 | } |
916 | 16.6k | Constraints.push_back(Constraint.get()); |
917 | | |
918 | 16.6k | if (Tok.isNot(tok::l_paren)) { |
919 | 0 | Diag(Tok, diag::err_expected_lparen_after) << "asm operand"; |
920 | 0 | SkipUntil(tok::r_paren, StopAtSemi); |
921 | 0 | return true; |
922 | 0 | } |
923 | | |
924 | | // Read the parenthesized expression. |
925 | 16.6k | BalancedDelimiterTracker T(*this, tok::l_paren); |
926 | 16.6k | T.consumeOpen(); |
927 | 16.6k | ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression()); |
928 | 16.6k | T.consumeClose(); |
929 | 16.6k | if (Res.isInvalid()) { |
930 | 4 | SkipUntil(tok::r_paren, StopAtSemi); |
931 | 4 | return true; |
932 | 4 | } |
933 | 16.6k | Exprs.push_back(Res.get()); |
934 | | // Eat the comma and continue parsing if it exists. |
935 | 16.6k | if (!TryConsumeToken(tok::comma)) |
936 | 7.10k | return false; |
937 | 16.6k | } |
938 | 7.10k | } |
939 | | |
940 | 9 | const char *Parser::GNUAsmQualifiers::getQualifierName(AQ Qualifier) { |
941 | 9 | switch (Qualifier) { |
942 | 3 | case AQ_volatile: return "volatile"; |
943 | 3 | case AQ_inline: return "inline"; |
944 | 3 | case AQ_goto: return "goto"; |
945 | 0 | case AQ_unspecified: return "unspecified"; |
946 | 9 | } |
947 | 0 | llvm_unreachable("Unknown GNUAsmQualifier"); |
948 | 0 | } |
949 | | |
950 | | Parser::GNUAsmQualifiers::AQ |
951 | 66.0k | Parser::getGNUAsmQualifier(const Token &Tok) const { |
952 | 66.0k | switch (Tok.getKind()) { |
953 | 1.92k | case tok::kw_volatile: return GNUAsmQualifiers::AQ_volatile; |
954 | 17 | case tok::kw_inline: return GNUAsmQualifiers::AQ_inline; |
955 | 165 | case tok::kw_goto: return GNUAsmQualifiers::AQ_goto; |
956 | 63.9k | default: return GNUAsmQualifiers::AQ_unspecified; |
957 | 66.0k | } |
958 | 66.0k | } |
959 | 1.80k | bool Parser::GNUAsmQualifiers::setAsmQualifier(AQ Qualifier) { |
960 | 1.80k | bool IsDuplicate = Qualifiers & Qualifier; |
961 | 1.80k | Qualifiers |= Qualifier; |
962 | 1.80k | return IsDuplicate; |
963 | 1.80k | } |