1//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
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 class implements a parser for assembly files similar to gas syntax.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/ADT/APFloat.h"
14#include "llvm/ADT/APInt.h"
15#include "llvm/ADT/ArrayRef.h"
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/SmallSet.h"
18#include "llvm/ADT/SmallString.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/StringExtras.h"
21#include "llvm/ADT/StringMap.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/ADT/Twine.h"
24#include "llvm/BinaryFormat/Dwarf.h"
25#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
26#include "llvm/MC/MCAsmInfo.h"
27#include "llvm/MC/MCCodeView.h"
28#include "llvm/MC/MCContext.h"
29#include "llvm/MC/MCDirectives.h"
30#include "llvm/MC/MCDwarf.h"
31#include "llvm/MC/MCExpr.h"
32#include "llvm/MC/MCInstPrinter.h"
33#include "llvm/MC/MCInstrDesc.h"
34#include "llvm/MC/MCInstrInfo.h"
35#include "llvm/MC/MCParser/AsmCond.h"
36#include "llvm/MC/MCParser/AsmLexer.h"
37#include "llvm/MC/MCParser/MCAsmParser.h"
38#include "llvm/MC/MCParser/MCAsmParserExtension.h"
39#include "llvm/MC/MCParser/MCAsmParserUtils.h"
40#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
41#include "llvm/MC/MCParser/MCTargetAsmParser.h"
42#include "llvm/MC/MCRegisterInfo.h"
43#include "llvm/MC/MCSection.h"
44#include "llvm/MC/MCStreamer.h"
45#include "llvm/MC/MCSymbol.h"
46#include "llvm/MC/MCSymbolMachO.h"
47#include "llvm/MC/MCTargetOptions.h"
48#include "llvm/MC/MCValue.h"
49#include "llvm/Support/Base64.h"
50#include "llvm/Support/Casting.h"
51#include "llvm/Support/CommandLine.h"
52#include "llvm/Support/ErrorHandling.h"
53#include "llvm/Support/MD5.h"
54#include "llvm/Support/MathExtras.h"
55#include "llvm/Support/MemoryBuffer.h"
56#include "llvm/Support/SMLoc.h"
57#include "llvm/Support/SourceMgr.h"
58#include "llvm/Support/raw_ostream.h"
59#include <algorithm>
60#include <cassert>
61#include <cctype>
62#include <climits>
63#include <cstddef>
64#include <cstdint>
65#include <deque>
66#include <memory>
67#include <optional>
68#include <sstream>
69#include <string>
70#include <tuple>
71#include <utility>
72#include <vector>
73
74using namespace llvm;
75
76MCAsmParserSemaCallback::~MCAsmParserSemaCallback() = default;
77
78namespace {
79
80/// Helper types for tracking macro definitions.
81typedef std::vector<AsmToken> MCAsmMacroArgument;
82typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
83
84/// Helper class for storing information about an active macro
85/// instantiation.
86struct MacroInstantiation {
87 /// The location of the instantiation.
88 SMLoc InstantiationLoc;
89
90 /// The buffer where parsing should resume upon instantiation completion.
91 unsigned ExitBuffer;
92
93 /// The location where parsing should resume upon instantiation completion.
94 SMLoc ExitLoc;
95
96 /// The depth of TheCondStack at the start of the instantiation.
97 size_t CondStackDepth;
98};
99
100struct ParseStatementInfo {
101 /// The parsed operands from the last parsed statement.
102 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
103
104 /// The opcode from the last parsed instruction.
105 unsigned Opcode = ~0U;
106
107 /// Was there an error parsing the inline assembly?
108 bool ParseError = false;
109
110 SmallVectorImpl<AsmRewrite> *AsmRewrites = nullptr;
111
112 ParseStatementInfo() = delete;
113 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
114 : AsmRewrites(rewrites) {}
115};
116
117/// The concrete assembly parser instance.
118class AsmParser : public MCAsmParser {
119private:
120 SourceMgr::DiagHandlerTy SavedDiagHandler;
121 void *SavedDiagContext;
122 std::unique_ptr<MCAsmParserExtension> PlatformParser;
123 std::unique_ptr<MCAsmParserExtension> LFIParser;
124 SMLoc StartTokLoc;
125 std::optional<SMLoc> CFIStartProcLoc;
126
127 /// This is the current buffer index we're lexing from as managed by the
128 /// SourceMgr object.
129 unsigned CurBuffer;
130
131 AsmCond TheCondState;
132 std::vector<AsmCond> TheCondStack;
133
134 /// maps directive names to handler methods in parser
135 /// extensions. Extensions register themselves in this map by calling
136 /// addDirectiveHandler.
137 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
138
139 /// Stack of active macro instantiations.
140 std::vector<MacroInstantiation*> ActiveMacros;
141
142 /// List of bodies of anonymous macros.
143 std::deque<MCAsmMacro> MacroLikeBodies;
144
145 /// Boolean tracking whether macro substitution is enabled.
146 unsigned MacrosEnabledFlag : 1;
147
148 /// Keeps track of how many .macro's have been instantiated.
149 unsigned NumOfMacroInstantiations = 0;
150
151 /// The values from the last parsed cpp hash file line comment if any.
152 struct CppHashInfoTy {
153 StringRef Filename;
154 int64_t LineNumber;
155 SMLoc Loc;
156 unsigned Buf;
157 CppHashInfoTy() : LineNumber(0), Buf(0) {}
158 };
159 CppHashInfoTy CppHashInfo;
160
161 /// Have we seen any file line comment.
162 bool HadCppHashFilename = false;
163
164 /// List of forward directional labels for diagnosis at the end.
165 SmallVector<std::tuple<SMLoc, CppHashInfoTy, MCSymbol *>, 4> DirLabels;
166
167 SmallSet<StringRef, 2> LTODiscardSymbols;
168
169 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
170 unsigned AssemblerDialect = ~0U;
171
172 /// is Darwin compatibility enabled?
173 bool IsDarwin = false;
174
175 /// Are we parsing ms-style inline assembly?
176 bool ParsingMSInlineAsm = false;
177
178 /// Did we already inform the user about inconsistent MD5 usage?
179 bool ReportedInconsistentMD5 = false;
180
181 // Is alt macro mode enabled.
182 bool AltMacroMode = false;
183
184protected:
185 virtual bool parseStatement(ParseStatementInfo &Info,
186 MCAsmParserSemaCallback *SI);
187
188 /// This routine uses the target specific ParseInstruction function to
189 /// parse an instruction into Operands, and then call the target specific
190 /// MatchAndEmit function to match and emit the instruction.
191 bool parseAndMatchAndEmitTargetInstruction(ParseStatementInfo &Info,
192 StringRef IDVal, AsmToken ID,
193 SMLoc IDLoc);
194
195 /// Should we emit DWARF describing this assembler source? (Returns false if
196 /// the source has .file directives, which means we don't want to generate
197 /// info describing the assembler source itself.)
198 bool enabledGenDwarfForAssembly();
199
200public:
201 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
202 const MCAsmInfo &MAI, unsigned CB);
203 AsmParser(const AsmParser &) = delete;
204 AsmParser &operator=(const AsmParser &) = delete;
205 ~AsmParser() override;
206
207 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
208
209 void addDirectiveHandler(StringRef Directive,
210 ExtensionDirectiveHandler Handler) override {
211 ExtensionDirectiveMap[Directive] = Handler;
212 }
213
214 void addAliasForDirective(StringRef Directive, StringRef Alias) override {
215 DirectiveKindMap[Directive.lower()] = DirectiveKindMap[Alias.lower()];
216 }
217
218 /// @name MCAsmParser Interface
219 /// {
220
221 CodeViewContext &getCVContext() { return Ctx.getCVContext(); }
222
223 unsigned getAssemblerDialect() override {
224 if (AssemblerDialect == ~0U)
225 return MAI.getAssemblerDialect();
226 else
227 return AssemblerDialect;
228 }
229 void setAssemblerDialect(unsigned i) override {
230 AssemblerDialect = i;
231 }
232
233 void Note(SMLoc L, const Twine &Msg, SMRange Range = {}) override;
234 bool Warning(SMLoc L, const Twine &Msg, SMRange Range = {}) override;
235 bool printError(SMLoc L, const Twine &Msg, SMRange Range = {}) override;
236
237 const AsmToken &Lex() override;
238
239 void setParsingMSInlineAsm(bool V) override {
240 ParsingMSInlineAsm = V;
241 // When parsing MS inline asm, we must lex 0b1101 and 0ABCH as binary and
242 // hex integer literals.
243 Lexer.setLexMasmIntegers(V);
244 }
245 bool isParsingMSInlineAsm() override { return ParsingMSInlineAsm; }
246
247 bool discardLTOSymbol(StringRef Name) const override {
248 return LTODiscardSymbols.contains(V: Name);
249 }
250
251 bool parseMSInlineAsm(std::string &AsmString, unsigned &NumOutputs,
252 unsigned &NumInputs,
253 SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
254 SmallVectorImpl<std::string> &Constraints,
255 SmallVectorImpl<std::string> &Clobbers,
256 const MCInstrInfo *MII, MCInstPrinter *IP,
257 MCAsmParserSemaCallback &SI) override;
258
259 bool parseExpression(const MCExpr *&Res);
260 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
261 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
262 AsmTypeInfo *TypeInfo) override;
263 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
264 bool parseAbsoluteExpression(int64_t &Res) override;
265
266 /// Parse a floating point expression using the float \p Semantics
267 /// and set \p Res to the value.
268 bool parseRealValue(const fltSemantics &Semantics, APInt &Res);
269
270 /// Parse an identifier or string (as a quoted identifier)
271 /// and set \p Res to the identifier contents.
272 bool parseIdentifier(StringRef &Res) override;
273 void eatToEndOfStatement() override;
274
275 bool checkForValidSection() override;
276
277 /// }
278
279private:
280 bool parseCurlyBlockScope(SmallVectorImpl<AsmRewrite>& AsmStrRewrites);
281 bool parseCppHashLineFilenameComment(SMLoc L, bool SaveLocInfo = true);
282
283 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
284 ArrayRef<MCAsmMacroParameter> Parameters);
285 bool expandMacro(raw_svector_ostream &OS, MCAsmMacro &Macro,
286 ArrayRef<MCAsmMacroParameter> Parameters,
287 ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable);
288
289 /// Are macros enabled in the parser?
290 bool areMacrosEnabled() {return MacrosEnabledFlag;}
291
292 /// Control a flag in the parser that enables or disables macros.
293 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
294
295 /// Are we inside a macro instantiation?
296 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
297
298 /// Handle entry to macro instantiation.
299 ///
300 /// \param M The macro.
301 /// \param NameLoc Instantiation location.
302 bool handleMacroEntry(MCAsmMacro *M, SMLoc NameLoc);
303
304 /// Handle exit from macro instantiation.
305 void handleMacroExit();
306
307 /// Extract AsmTokens for a macro argument.
308 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
309
310 /// Parse all macro arguments for a given macro.
311 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
312
313 void printMacroInstantiations();
314 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
315 SMRange Range = {}) const {
316 ArrayRef<SMRange> Ranges(Range);
317 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
318 }
319 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
320
321 /// Enter the specified file. This returns true on failure.
322 bool enterIncludeFile(const std::string &Filename);
323
324 /// Process the specified file for the .incbin directive.
325 /// This returns true on failure.
326 bool processIncbinFile(const std::string &Filename, int64_t Skip = 0,
327 const MCExpr *Count = nullptr, SMLoc Loc = SMLoc());
328
329 /// Reset the current lexer position to that given by \p Loc. The
330 /// current token is not set; clients should ensure Lex() is called
331 /// subsequently.
332 ///
333 /// \param InBuffer If not 0, should be the known buffer id that contains the
334 /// location.
335 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
336
337 /// Parse up to the end of statement and a return the contents from the
338 /// current token until the end of the statement; the current token on exit
339 /// will be either the EndOfStatement or EOF.
340 StringRef parseStringToEndOfStatement() override;
341
342 /// Parse until the end of a statement or a comma is encountered,
343 /// return the contents from the current token up to the end or comma.
344 StringRef parseStringToComma();
345
346 enum class AssignmentKind {
347 Set,
348 Equiv,
349 Equal,
350 LTOSetConditional,
351 };
352
353 bool parseAssignment(StringRef Name, AssignmentKind Kind);
354
355 unsigned getBinOpPrecedence(AsmToken::TokenKind K,
356 MCBinaryExpr::Opcode &Kind);
357
358 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
359 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
360 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
361
362 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
363
364 bool parseCVFunctionId(int64_t &FunctionId, StringRef DirectiveName);
365 bool parseCVFileId(int64_t &FileId, StringRef DirectiveName);
366
367 // Generic (target and platform independent) directive parsing.
368 enum DirectiveKind {
369 DK_NO_DIRECTIVE, // Placeholder
370 DK_SET,
371 DK_EQU,
372 DK_EQUIV,
373 DK_ASCII,
374 DK_ASCIZ,
375 DK_STRING,
376 DK_BYTE,
377 DK_SHORT,
378 DK_RELOC,
379 DK_VALUE,
380 DK_2BYTE,
381 DK_LONG,
382 DK_INT,
383 DK_4BYTE,
384 DK_QUAD,
385 DK_8BYTE,
386 DK_OCTA,
387 DK_DC,
388 DK_DC_A,
389 DK_DC_B,
390 DK_DC_D,
391 DK_DC_L,
392 DK_DC_S,
393 DK_DC_W,
394 DK_DC_X,
395 DK_DCB,
396 DK_DCB_B,
397 DK_DCB_D,
398 DK_DCB_L,
399 DK_DCB_S,
400 DK_DCB_W,
401 DK_DCB_X,
402 DK_DS,
403 DK_DS_B,
404 DK_DS_D,
405 DK_DS_L,
406 DK_DS_P,
407 DK_DS_S,
408 DK_DS_W,
409 DK_DS_X,
410 DK_SINGLE,
411 DK_FLOAT,
412 DK_DOUBLE,
413 DK_ALIGN,
414 DK_ALIGN32,
415 DK_BALIGN,
416 DK_BALIGNW,
417 DK_BALIGNL,
418 DK_P2ALIGN,
419 DK_P2ALIGNW,
420 DK_P2ALIGNL,
421 DK_PREFALIGN,
422 DK_ORG,
423 DK_FILL,
424 DK_ENDR,
425 DK_ZERO,
426 DK_EXTERN,
427 DK_GLOBL,
428 DK_GLOBAL,
429 DK_LAZY_REFERENCE,
430 DK_NO_DEAD_STRIP,
431 DK_SYMBOL_RESOLVER,
432 DK_PRIVATE_EXTERN,
433 DK_REFERENCE,
434 DK_WEAK_DEFINITION,
435 DK_WEAK_REFERENCE,
436 DK_WEAK_DEF_CAN_BE_HIDDEN,
437 DK_COLD,
438 DK_COMM,
439 DK_COMMON,
440 DK_LCOMM,
441 DK_ABORT,
442 DK_INCLUDE,
443 DK_INCBIN,
444 DK_CODE16,
445 DK_CODE16GCC,
446 DK_REPT,
447 DK_IRP,
448 DK_IRPC,
449 DK_IF,
450 DK_IFEQ,
451 DK_IFGE,
452 DK_IFGT,
453 DK_IFLE,
454 DK_IFLT,
455 DK_IFNE,
456 DK_IFB,
457 DK_IFNB,
458 DK_IFC,
459 DK_IFEQS,
460 DK_IFNC,
461 DK_IFNES,
462 DK_IFDEF,
463 DK_IFNDEF,
464 DK_IFNOTDEF,
465 DK_ELSEIF,
466 DK_ELSE,
467 DK_ENDIF,
468 DK_SPACE,
469 DK_SKIP,
470 DK_FILE,
471 DK_LINE,
472 DK_LOC,
473 DK_LOC_LABEL,
474 DK_STABS,
475 DK_CV_FILE,
476 DK_CV_FUNC_ID,
477 DK_CV_INLINE_SITE_ID,
478 DK_CV_LOC,
479 DK_CV_LINETABLE,
480 DK_CV_INLINE_LINETABLE,
481 DK_CV_DEF_RANGE,
482 DK_CV_STRINGTABLE,
483 DK_CV_STRING,
484 DK_CV_FILECHECKSUMS,
485 DK_CV_FILECHECKSUM_OFFSET,
486 DK_CV_FPO_DATA,
487 DK_CFI_SECTIONS,
488 DK_CFI_STARTPROC,
489 DK_CFI_ENDPROC,
490 DK_CFI_DEF_CFA,
491 DK_CFI_DEF_CFA_OFFSET,
492 DK_CFI_ADJUST_CFA_OFFSET,
493 DK_CFI_DEF_CFA_REGISTER,
494 DK_CFI_LLVM_DEF_ASPACE_CFA,
495 DK_CFI_OFFSET,
496 DK_CFI_REL_OFFSET,
497 DK_CFI_PERSONALITY,
498 DK_CFI_LSDA,
499 DK_CFI_REMEMBER_STATE,
500 DK_CFI_RESTORE_STATE,
501 DK_CFI_SAME_VALUE,
502 DK_CFI_RESTORE,
503 DK_CFI_ESCAPE,
504 DK_CFI_RETURN_COLUMN,
505 DK_CFI_SIGNAL_FRAME,
506 DK_CFI_UNDEFINED,
507 DK_CFI_REGISTER,
508 DK_CFI_WINDOW_SAVE,
509 DK_CFI_LABEL,
510 DK_CFI_B_KEY_FRAME,
511 DK_CFI_VAL_OFFSET,
512 DK_MACROS_ON,
513 DK_MACROS_OFF,
514 DK_ALTMACRO,
515 DK_NOALTMACRO,
516 DK_MACRO,
517 DK_EXITM,
518 DK_ENDM,
519 DK_ENDMACRO,
520 DK_PURGEM,
521 DK_SLEB128,
522 DK_ULEB128,
523 DK_ERR,
524 DK_ERROR,
525 DK_WARNING,
526 DK_PRINT,
527 DK_ADDRSIG,
528 DK_ADDRSIG_SYM,
529 DK_PSEUDO_PROBE,
530 DK_LTO_DISCARD,
531 DK_LTO_SET_CONDITIONAL,
532 DK_CFI_MTE_TAGGED_FRAME,
533 DK_MEMTAG,
534 DK_BASE64,
535 DK_END
536 };
537
538 /// Maps directive name --> DirectiveKind enum, for
539 /// directives parsed by this class.
540 StringMap<DirectiveKind> DirectiveKindMap;
541
542 // Codeview def_range type parsing.
543 enum CVDefRangeType {
544 CVDR_DEFRANGE = 0, // Placeholder
545 CVDR_DEFRANGE_REGISTER,
546 CVDR_DEFRANGE_FRAMEPOINTER_REL,
547 CVDR_DEFRANGE_SUBFIELD_REGISTER,
548 CVDR_DEFRANGE_REGISTER_REL
549 };
550
551 /// Maps Codeview def_range types --> CVDefRangeType enum, for
552 /// Codeview def_range types parsed by this class.
553 StringMap<CVDefRangeType> CVDefRangeTypeMap;
554
555 // ".ascii", ".asciz", ".string"
556 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
557 bool parseDirectiveBase64(); // ".base64"
558 bool parseDirectiveReloc(SMLoc DirectiveLoc); // ".reloc"
559 bool parseDirectiveValue(StringRef IDVal,
560 unsigned Size); // ".byte", ".long", ...
561 bool parseDirectiveOctaValue(StringRef IDVal); // ".octa", ...
562 bool parseDirectiveRealValue(StringRef IDVal,
563 const fltSemantics &); // ".single", ...
564 bool parseDirectiveFill(); // ".fill"
565 bool parseDirectiveZero(); // ".zero"
566 // ".set", ".equ", ".equiv", ".lto_set_conditional"
567 bool parseDirectiveSet(StringRef IDVal, AssignmentKind Kind);
568 bool parseDirectiveOrg(); // ".org"
569 // ".align{,32}", ".p2align{,w,l}"
570 bool parseDirectiveAlign(bool IsPow2, uint8_t ValueSize);
571 bool parseDirectivePrefAlign();
572
573 // ".file", ".line", ".loc", ".loc_label", ".stabs"
574 bool parseDirectiveFile(SMLoc DirectiveLoc);
575 bool parseDirectiveLine();
576 bool parseDirectiveLoc();
577 bool parseDirectiveLocLabel(SMLoc DirectiveLoc);
578 bool parseDirectiveStabs();
579
580 // ".cv_file", ".cv_func_id", ".cv_inline_site_id", ".cv_loc", ".cv_linetable",
581 // ".cv_inline_linetable", ".cv_def_range", ".cv_string"
582 bool parseDirectiveCVFile();
583 bool parseDirectiveCVFuncId();
584 bool parseDirectiveCVInlineSiteId();
585 bool parseDirectiveCVLoc();
586 bool parseDirectiveCVLinetable();
587 bool parseDirectiveCVInlineLinetable();
588 bool parseDirectiveCVDefRange();
589 bool parseDirectiveCVString();
590 bool parseDirectiveCVStringTable();
591 bool parseDirectiveCVFileChecksums();
592 bool parseDirectiveCVFileChecksumOffset();
593 bool parseDirectiveCVFPOData();
594
595 // .cfi directives
596 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
597 bool parseDirectiveCFIWindowSave(SMLoc DirectiveLoc);
598 bool parseDirectiveCFISections();
599 bool parseDirectiveCFIStartProc();
600 bool parseDirectiveCFIEndProc();
601 bool parseDirectiveCFIDefCfaOffset(SMLoc DirectiveLoc);
602 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
603 bool parseDirectiveCFIAdjustCfaOffset(SMLoc DirectiveLoc);
604 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
605 bool parseDirectiveCFILLVMDefAspaceCfa(SMLoc DirectiveLoc);
606 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
607 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
608 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
609 bool parseDirectiveCFIRememberState(SMLoc DirectiveLoc);
610 bool parseDirectiveCFIRestoreState(SMLoc DirectiveLoc);
611 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
612 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
613 bool parseDirectiveCFIEscape(SMLoc DirectiveLoc);
614 bool parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc);
615 bool parseDirectiveCFISignalFrame(SMLoc DirectiveLoc);
616 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
617 bool parseDirectiveCFILabel(SMLoc DirectiveLoc);
618 bool parseDirectiveCFIValOffset(SMLoc DirectiveLoc);
619
620 // macro directives
621 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
622 bool parseDirectiveExitMacro(StringRef Directive);
623 bool parseDirectiveEndMacro(StringRef Directive);
624 bool parseDirectiveMacro(SMLoc DirectiveLoc);
625 bool parseDirectiveMacrosOnOff(StringRef Directive);
626 // alternate macro mode directives
627 bool parseDirectiveAltmacro(StringRef Directive);
628
629 // ".space", ".skip"
630 bool parseDirectiveSpace(StringRef IDVal);
631
632 // ".dcb"
633 bool parseDirectiveDCB(StringRef IDVal, unsigned Size);
634 bool parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &);
635 // ".ds"
636 bool parseDirectiveDS(StringRef IDVal, unsigned Size);
637
638 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
639 bool parseDirectiveLEB128(bool Signed);
640
641 /// Parse a directive like ".globl" which
642 /// accepts a single symbol (which should be a label or an external).
643 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
644
645 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
646
647 bool parseDirectiveAbort(SMLoc DirectiveLoc); // ".abort"
648 bool parseDirectiveInclude(); // ".include"
649 bool parseDirectiveIncbin(); // ".incbin"
650
651 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
652 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
653 // ".ifb" or ".ifnb", depending on ExpectBlank.
654 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
655 // ".ifc" or ".ifnc", depending on ExpectEqual.
656 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
657 // ".ifeqs" or ".ifnes", depending on ExpectEqual.
658 bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual);
659 // ".ifdef" or ".ifndef", depending on expect_defined
660 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
661 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
662 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
663 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
664 bool parseEscapedString(std::string &Data) override;
665 bool parseAngleBracketString(std::string &Data) override;
666
667 // Macro-like directives
668 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
669 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
670 raw_svector_ostream &OS);
671 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
672 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
673 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
674 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
675
676 // "_emit" or "__emit"
677 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
678 size_t Len);
679
680 // "align"
681 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
682
683 // "end"
684 bool parseDirectiveEnd(SMLoc DirectiveLoc);
685
686 // ".err" or ".error"
687 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
688
689 // ".warning"
690 bool parseDirectiveWarning(SMLoc DirectiveLoc);
691
692 // .print <double-quotes-string>
693 bool parseDirectivePrint(SMLoc DirectiveLoc);
694
695 // .pseudoprobe
696 bool parseDirectivePseudoProbe();
697
698 // ".lto_discard"
699 bool parseDirectiveLTODiscard();
700
701 // Directives to support address-significance tables.
702 bool parseDirectiveAddrsig();
703 bool parseDirectiveAddrsigSym();
704
705 void initializeDirectiveKindMap();
706 void initializeCVDefRangeTypeMap();
707};
708
709class HLASMAsmParser final : public AsmParser {
710private:
711 AsmLexer &Lexer;
712 MCStreamer &Out;
713
714 void lexLeadingSpaces() {
715 while (Lexer.is(K: AsmToken::Space))
716 Lexer.Lex();
717 }
718
719 bool parseAsHLASMLabel(ParseStatementInfo &Info, MCAsmParserSemaCallback *SI);
720 bool parseAsMachineInstruction(ParseStatementInfo &Info,
721 MCAsmParserSemaCallback *SI);
722
723public:
724 HLASMAsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
725 const MCAsmInfo &MAI, unsigned CB = 0)
726 : AsmParser(SM, Ctx, Out, MAI, CB), Lexer(getLexer()), Out(Out) {
727 Lexer.setSkipSpace(false);
728 Lexer.setAllowHashInIdentifier(true);
729 Lexer.setLexHLASMIntegers(true);
730 Lexer.setLexHLASMStrings(true);
731 }
732
733 ~HLASMAsmParser() override { Lexer.setSkipSpace(true); }
734
735 bool parseStatement(ParseStatementInfo &Info,
736 MCAsmParserSemaCallback *SI) override;
737};
738
739} // end anonymous namespace
740
741namespace llvm {
742
743extern cl::opt<unsigned> AsmMacroMaxNestingDepth;
744
745} // end namespace llvm
746
747AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
748 const MCAsmInfo &MAI, unsigned CB = 0)
749 : MCAsmParser(Ctx, Out, SM, MAI), CurBuffer(CB ? CB : SM.getMainFileID()),
750 MacrosEnabledFlag(true) {
751 HadError = false;
752 // Save the old handler.
753 SavedDiagHandler = SrcMgr.getDiagHandler();
754 SavedDiagContext = SrcMgr.getDiagContext();
755 // Set our own handler which calls the saved handler.
756 SrcMgr.setDiagHandler(DH: DiagHandler, Ctx: this);
757 Lexer.setBuffer(Buf: SrcMgr.getMemoryBuffer(i: CurBuffer)->getBuffer());
758 // Make MCStreamer aware of the StartTokLoc for locations in diagnostics.
759 Out.setStartTokLocPtr(&StartTokLoc);
760
761 // Initialize the platform / file format parser.
762 switch (Ctx.getObjectFileType()) {
763 case MCContext::IsCOFF:
764 PlatformParser.reset(p: createCOFFAsmParser());
765 break;
766 case MCContext::IsMachO:
767 PlatformParser.reset(p: createDarwinAsmParser());
768 IsDarwin = true;
769 break;
770 case MCContext::IsELF:
771 PlatformParser.reset(p: createELFAsmParser());
772 break;
773 case MCContext::IsGOFF:
774 PlatformParser.reset(p: createGOFFAsmParser());
775 break;
776 case MCContext::IsSPIRV:
777 report_fatal_error(
778 reason: "Need to implement createSPIRVAsmParser for SPIRV format.");
779 break;
780 case MCContext::IsWasm:
781 PlatformParser.reset(p: createWasmAsmParser());
782 break;
783 case MCContext::IsXCOFF:
784 PlatformParser.reset(p: createXCOFFAsmParser());
785 break;
786 case MCContext::IsDXContainer:
787 report_fatal_error(reason: "DXContainer is not supported yet");
788 break;
789 }
790
791 PlatformParser->Initialize(Parser&: *this);
792 if (Out.getLFIRewriter()) {
793 LFIParser.reset(p: createLFIAsmParser(Exp: Out.getLFIRewriter()));
794 LFIParser->Initialize(Parser&: *this);
795 }
796 initializeDirectiveKindMap();
797 initializeCVDefRangeTypeMap();
798}
799
800AsmParser::~AsmParser() {
801 assert((HadError || ActiveMacros.empty()) &&
802 "Unexpected active macro instantiation!");
803
804 // Remove MCStreamer's reference to the parser SMLoc.
805 Out.setStartTokLocPtr(nullptr);
806 // Restore the saved diagnostics handler and context for use during
807 // finalization.
808 SrcMgr.setDiagHandler(DH: SavedDiagHandler, Ctx: SavedDiagContext);
809}
810
811void AsmParser::printMacroInstantiations() {
812 // Print the active macro instantiation stack.
813 for (MacroInstantiation *M : reverse(C&: ActiveMacros))
814 printMessage(Loc: M->InstantiationLoc, Kind: SourceMgr::DK_Note,
815 Msg: "while in macro instantiation");
816}
817
818void AsmParser::Note(SMLoc L, const Twine &Msg, SMRange Range) {
819 printPendingErrors();
820 printMessage(Loc: L, Kind: SourceMgr::DK_Note, Msg, Range);
821 printMacroInstantiations();
822}
823
824bool AsmParser::Warning(SMLoc L, const Twine &Msg, SMRange Range) {
825 if(getTargetParser().getTargetOptions().MCNoWarn)
826 return false;
827 if (getTargetParser().getTargetOptions().MCFatalWarnings)
828 return Error(L, Msg, Range);
829 printMessage(Loc: L, Kind: SourceMgr::DK_Warning, Msg, Range);
830 printMacroInstantiations();
831 return false;
832}
833
834bool AsmParser::printError(SMLoc L, const Twine &Msg, SMRange Range) {
835 HadError = true;
836 printMessage(Loc: L, Kind: SourceMgr::DK_Error, Msg, Range);
837 printMacroInstantiations();
838 return true;
839}
840
841bool AsmParser::enterIncludeFile(const std::string &Filename) {
842 std::string IncludedFile;
843 unsigned NewBuf =
844 SrcMgr.AddIncludeFile(Filename, IncludeLoc: Lexer.getLoc(), IncludedFile);
845 if (!NewBuf)
846 return true;
847
848 CurBuffer = NewBuf;
849 Lexer.setBuffer(Buf: SrcMgr.getMemoryBuffer(i: CurBuffer)->getBuffer());
850 return false;
851}
852
853/// Process the specified .incbin file by searching for it in the include paths
854/// then just emitting the byte contents of the file to the streamer. This
855/// returns true on failure.
856bool AsmParser::processIncbinFile(const std::string &Filename, int64_t Skip,
857 const MCExpr *Count, SMLoc Loc) {
858 std::string IncludedFile;
859 unsigned NewBuf =
860 SrcMgr.AddIncludeFile(Filename, IncludeLoc: Lexer.getLoc(), IncludedFile);
861 if (!NewBuf)
862 return true;
863
864 // Pick up the bytes from the file and emit them.
865 StringRef Bytes = SrcMgr.getMemoryBuffer(i: NewBuf)->getBuffer();
866 Bytes = Bytes.drop_front(N: Skip);
867 if (Count) {
868 int64_t Res;
869 if (!Count->evaluateAsAbsolute(Res, Asm: getStreamer().getAssemblerPtr()))
870 return Error(L: Loc, Msg: "expected absolute expression");
871 if (Res < 0)
872 return Warning(L: Loc, Msg: "negative count has no effect");
873 Bytes = Bytes.take_front(N: Res);
874 }
875 getStreamer().emitBytes(Data: Bytes);
876 return false;
877}
878
879void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
880 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
881 Lexer.setBuffer(Buf: SrcMgr.getMemoryBuffer(i: CurBuffer)->getBuffer(),
882 ptr: Loc.getPointer());
883}
884
885const AsmToken &AsmParser::Lex() {
886 if (Lexer.getTok().is(K: AsmToken::Error))
887 Error(L: Lexer.getErrLoc(), Msg: Lexer.getErr());
888
889 // if it's a end of statement with a comment in it
890 if (getTok().is(K: AsmToken::EndOfStatement)) {
891 // if this is a line comment output it.
892 if (!getTok().getString().empty() && getTok().getString().front() != '\n' &&
893 getTok().getString().front() != '\r' && MAI.preserveAsmComments())
894 Out.addExplicitComment(T: Twine(getTok().getString()));
895 }
896
897 const AsmToken *tok = &Lexer.Lex();
898
899 // Parse comments here to be deferred until end of next statement.
900 while (tok->is(K: AsmToken::Comment)) {
901 if (MAI.preserveAsmComments())
902 Out.addExplicitComment(T: Twine(tok->getString()));
903 tok = &Lexer.Lex();
904 }
905
906 if (tok->is(K: AsmToken::Eof)) {
907 // If this is the end of an included file, pop the parent file off the
908 // include stack.
909 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(i: CurBuffer);
910 if (ParentIncludeLoc != SMLoc()) {
911 jumpToLoc(Loc: ParentIncludeLoc);
912 return Lex();
913 }
914 }
915
916 return *tok;
917}
918
919bool AsmParser::enabledGenDwarfForAssembly() {
920 // Check whether the user specified -g.
921 if (!getContext().getGenDwarfForAssembly())
922 return false;
923 // If we haven't encountered any .file directives (which would imply that
924 // the assembler source was produced with debug info already) then emit one
925 // describing the assembler source file itself.
926 if (getContext().getGenDwarfFileNumber() == 0) {
927 const MCDwarfFile &RootFile =
928 getContext().getMCDwarfLineTable(/*CUID=*/0).getRootFile();
929 getContext().setGenDwarfFileNumber(getStreamer().emitDwarfFileDirective(
930 /*CUID=*/FileNo: 0, Directory: getContext().getCompilationDir(), Filename: RootFile.Name,
931 Checksum: RootFile.Checksum, Source: RootFile.Source));
932 }
933 return true;
934}
935
936bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
937 LTODiscardSymbols.clear();
938
939 // Create the initial section, if requested.
940 if (!NoInitialTextSection)
941 Out.initSections(NoExecStack: false, STI: getTargetParser().getSTI());
942
943 // Prime the lexer.
944 Lex();
945
946 HadError = false;
947 AsmCond StartingCondState = TheCondState;
948 SmallVector<AsmRewrite, 4> AsmStrRewrites;
949
950 // If we are generating dwarf for assembly source files save the initial text
951 // section. (Don't use enabledGenDwarfForAssembly() here, as we aren't
952 // emitting any actual debug info yet and haven't had a chance to parse any
953 // embedded .file directives.)
954 if (getContext().getGenDwarfForAssembly()) {
955 MCSection *Sec = getStreamer().getCurrentSectionOnly();
956 if (!Sec->getBeginSymbol()) {
957 MCSymbol *SectionStartSym = getContext().createTempSymbol();
958 getStreamer().emitLabel(Symbol: SectionStartSym);
959 Sec->setBeginSymbol(SectionStartSym);
960 }
961 bool InsertResult = getContext().addGenDwarfSection(Sec);
962 assert(InsertResult && ".text section should not have debug info yet");
963 (void)InsertResult;
964 }
965
966 getTargetParser().onBeginOfFile();
967
968 // While we have input, parse each statement.
969 while (Lexer.isNot(K: AsmToken::Eof)) {
970 ParseStatementInfo Info(&AsmStrRewrites);
971 bool HasError = parseStatement(Info, SI: nullptr);
972
973 // If we have a Lexer Error we are on an Error Token. Load in Lexer Error
974 // for printing ErrMsg via Lex() only if no (presumably better) parser error
975 // exists.
976 if (HasError && !hasPendingError() && Lexer.getTok().is(K: AsmToken::Error))
977 Lex();
978
979 // parseStatement returned true so may need to emit an error.
980 printPendingErrors();
981
982 // Skipping to the next line if needed.
983 if (HasError && !getLexer().justConsumedEOL())
984 eatToEndOfStatement();
985 }
986
987 getTargetParser().onEndOfFile();
988 printPendingErrors();
989
990 // All errors should have been emitted.
991 assert(!hasPendingError() && "unexpected error from parseStatement");
992
993 if (TheCondState.TheCond != StartingCondState.TheCond ||
994 TheCondState.Ignore != StartingCondState.Ignore)
995 printError(L: getTok().getLoc(), Msg: "unmatched .ifs or .elses");
996 // Check to see there are no empty DwarfFile slots.
997 const auto &LineTables = getContext().getMCDwarfLineTables();
998 if (!LineTables.empty()) {
999 unsigned Index = 0;
1000 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
1001 if (File.Name.empty() && Index != 0)
1002 printError(L: getTok().getLoc(), Msg: "unassigned file number: " +
1003 Twine(Index) +
1004 " for .file directives");
1005 ++Index;
1006 }
1007 }
1008
1009 // Check to see that all assembler local symbols were actually defined.
1010 // Targets that don't do subsections via symbols may not want this, though,
1011 // so conservatively exclude them. Only do this if we're finalizing, though,
1012 // as otherwise we won't necessarily have seen everything yet.
1013 if (!NoFinalize) {
1014 if (MAI.hasSubsectionsViaSymbols()) {
1015 for (const auto &TableEntry : getContext().getSymbols()) {
1016 MCSymbol *Sym = TableEntry.getValue().Symbol;
1017 // Variable symbols may not be marked as defined, so check those
1018 // explicitly. If we know it's a variable, we have a definition for
1019 // the purposes of this check.
1020 if (Sym && Sym->isTemporary() && !Sym->isVariable() &&
1021 !Sym->isDefined())
1022 // FIXME: We would really like to refer back to where the symbol was
1023 // first referenced for a source location. We need to add something
1024 // to track that. Currently, we just point to the end of the file.
1025 printError(L: getTok().getLoc(), Msg: "assembler local symbol '" +
1026 Sym->getName() + "' not defined");
1027 }
1028 }
1029
1030 // Temporary symbols like the ones for directional jumps don't go in the
1031 // symbol table. They also need to be diagnosed in all (final) cases.
1032 for (std::tuple<SMLoc, CppHashInfoTy, MCSymbol *> &LocSym : DirLabels) {
1033 if (std::get<2>(t&: LocSym)->isUndefined()) {
1034 // Reset the state of any "# line file" directives we've seen to the
1035 // context as it was at the diagnostic site.
1036 CppHashInfo = std::get<1>(t&: LocSym);
1037 printError(L: std::get<0>(t&: LocSym), Msg: "directional label undefined");
1038 }
1039 }
1040 }
1041 // Finalize the output stream if there are no errors and if the client wants
1042 // us to.
1043 if (!HadError && !NoFinalize) {
1044 if (auto *TS = Out.getTargetStreamer())
1045 TS->emitConstantPools();
1046
1047 Out.finish(EndLoc: Lexer.getLoc());
1048 }
1049
1050 return HadError || getContext().hadError();
1051}
1052
1053bool AsmParser::checkForValidSection() {
1054 if (!ParsingMSInlineAsm && !getStreamer().getCurrentFragment()) {
1055 Out.initSections(NoExecStack: false, STI: getTargetParser().getSTI());
1056 return Error(L: getTok().getLoc(),
1057 Msg: "expected section directive before assembly directive");
1058 }
1059 return false;
1060}
1061
1062/// Throw away the rest of the line for testing purposes.
1063void AsmParser::eatToEndOfStatement() {
1064 while (Lexer.isNot(K: AsmToken::EndOfStatement) && Lexer.isNot(K: AsmToken::Eof))
1065 Lexer.Lex();
1066
1067 // Eat EOL.
1068 if (Lexer.is(K: AsmToken::EndOfStatement))
1069 Lexer.Lex();
1070}
1071
1072StringRef AsmParser::parseStringToEndOfStatement() {
1073 const char *Start = getTok().getLoc().getPointer();
1074
1075 while (Lexer.isNot(K: AsmToken::EndOfStatement) && Lexer.isNot(K: AsmToken::Eof))
1076 Lexer.Lex();
1077
1078 const char *End = getTok().getLoc().getPointer();
1079 return StringRef(Start, End - Start);
1080}
1081
1082StringRef AsmParser::parseStringToComma() {
1083 const char *Start = getTok().getLoc().getPointer();
1084
1085 while (Lexer.isNot(K: AsmToken::EndOfStatement) &&
1086 Lexer.isNot(K: AsmToken::Comma) && Lexer.isNot(K: AsmToken::Eof))
1087 Lexer.Lex();
1088
1089 const char *End = getTok().getLoc().getPointer();
1090 return StringRef(Start, End - Start);
1091}
1092
1093/// Parse a paren expression and return it.
1094/// NOTE: This assumes the leading '(' has already been consumed.
1095///
1096/// parenexpr ::= expr)
1097///
1098bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
1099 if (parseExpression(Res))
1100 return true;
1101 EndLoc = Lexer.getTok().getEndLoc();
1102 return parseRParen();
1103}
1104
1105/// Parse a bracket expression and return it.
1106/// NOTE: This assumes the leading '[' has already been consumed.
1107///
1108/// bracketexpr ::= expr]
1109///
1110bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
1111 if (parseExpression(Res))
1112 return true;
1113 EndLoc = getTok().getEndLoc();
1114 if (parseToken(T: AsmToken::RBrac, Msg: "expected ']' in brackets expression"))
1115 return true;
1116 return false;
1117}
1118
1119/// Parse a primary expression and return it.
1120/// primaryexpr ::= (parenexpr
1121/// primaryexpr ::= symbol
1122/// primaryexpr ::= number
1123/// primaryexpr ::= '.'
1124/// primaryexpr ::= ~,+,- primaryexpr
1125bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
1126 AsmTypeInfo *TypeInfo) {
1127 SMLoc FirstTokenLoc = getLexer().getLoc();
1128 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
1129 switch (FirstTokenKind) {
1130 default:
1131 return TokError(Msg: "unknown token in expression");
1132 // If we have an error assume that we've already handled it.
1133 case AsmToken::Error:
1134 return true;
1135 case AsmToken::Exclaim:
1136 Lex(); // Eat the operator.
1137 if (parsePrimaryExpr(Res, EndLoc, TypeInfo))
1138 return true;
1139 Res = MCUnaryExpr::createLNot(Expr: Res, Ctx&: getContext(), Loc: FirstTokenLoc);
1140 return false;
1141 case AsmToken::Dollar:
1142 case AsmToken::Star:
1143 case AsmToken::At:
1144 case AsmToken::String:
1145 case AsmToken::Identifier: {
1146 StringRef Identifier;
1147 if (parseIdentifier(Res&: Identifier)) {
1148 // We may have failed but '$'|'*' may be a valid token in context of
1149 // the current PC.
1150 if (getTok().is(K: AsmToken::Dollar) || getTok().is(K: AsmToken::Star)) {
1151 bool ShouldGenerateTempSymbol = false;
1152 if ((getTok().is(K: AsmToken::Dollar) && MAI.getDollarIsPC()) ||
1153 (getTok().is(K: AsmToken::Star) && MAI.isHLASM()))
1154 ShouldGenerateTempSymbol = true;
1155
1156 if (!ShouldGenerateTempSymbol)
1157 return Error(L: FirstTokenLoc, Msg: "invalid token in expression");
1158
1159 // Eat the '$'|'*' token.
1160 Lex();
1161 // This is either a '$'|'*' reference, which references the current PC.
1162 // Emit a temporary label to the streamer and refer to it.
1163 MCSymbol *Sym = Ctx.createTempSymbol();
1164 Out.emitLabel(Symbol: Sym);
1165 Res = MCSymbolRefExpr::create(Symbol: Sym, Ctx&: getContext());
1166 EndLoc = FirstTokenLoc;
1167 return false;
1168 }
1169 }
1170 // Parse an optional relocation specifier.
1171 std::pair<StringRef, StringRef> Split;
1172 if (MAI.useAtForSpecifier()) {
1173 if (FirstTokenKind == AsmToken::String) {
1174 if (Lexer.is(K: AsmToken::At)) {
1175 Lex(); // eat @
1176 SMLoc AtLoc = getLexer().getLoc();
1177 StringRef VName;
1178 if (parseIdentifier(Res&: VName))
1179 return Error(L: AtLoc, Msg: "expected symbol variant after '@'");
1180
1181 Split = std::make_pair(x&: Identifier, y&: VName);
1182 }
1183 } else if (Lexer.getAllowAtInIdentifier()) {
1184 Split = Identifier.split(Separator: '@');
1185 }
1186 } else if (MAI.useParensForSpecifier() &&
1187 parseOptionalToken(T: AsmToken::LParen)) {
1188 StringRef VName;
1189 parseIdentifier(Res&: VName);
1190 if (parseRParen())
1191 return true;
1192 Split = std::make_pair(x&: Identifier, y&: VName);
1193 }
1194
1195 EndLoc = SMLoc::getFromPointer(Ptr: Identifier.end());
1196
1197 // This is a symbol reference.
1198 StringRef SymbolName = Identifier;
1199 if (SymbolName.empty())
1200 return Error(L: getLexer().getLoc(), Msg: "expected a symbol reference");
1201
1202 // Lookup the @specifier if used.
1203 uint16_t Spec = 0;
1204 if (!Split.second.empty()) {
1205 auto MaybeSpecifier = MAI.getSpecifierForName(Name: Split.second);
1206 if (MaybeSpecifier) {
1207 SymbolName = Split.first;
1208 Spec = *MaybeSpecifier;
1209 } else if (!MAI.doesAllowAtInName()) {
1210 return Error(L: SMLoc::getFromPointer(Ptr: Split.second.begin()),
1211 Msg: "invalid variant '" + Split.second + "'");
1212 }
1213 }
1214
1215 MCSymbol *Sym = getContext().getInlineAsmLabel(Name: SymbolName);
1216 if (!Sym)
1217 Sym = getContext().parseSymbol(Name: MAI.isHLASM() ? SymbolName.upper()
1218 : SymbolName);
1219
1220 // If this is an absolute variable reference, substitute it now to preserve
1221 // semantics in the face of reassignment.
1222 if (Sym->isVariable()) {
1223 auto V = Sym->getVariableValue();
1224 bool DoInline = isa<MCConstantExpr>(Val: V) && !Spec;
1225 if (auto TV = dyn_cast<MCTargetExpr>(Val: V))
1226 DoInline = TV->inlineAssignedExpr();
1227 if (DoInline) {
1228 if (Spec)
1229 return Error(L: EndLoc, Msg: "unexpected modifier on variable reference");
1230 Res = Sym->getVariableValue();
1231 return false;
1232 }
1233 }
1234
1235 // Otherwise create a symbol ref.
1236 Res = MCSymbolRefExpr::create(Symbol: Sym, specifier: Spec, Ctx&: getContext(), Loc: FirstTokenLoc);
1237 return false;
1238 }
1239 case AsmToken::BigNum:
1240 return TokError(Msg: "literal value out of range for directive");
1241 case AsmToken::Integer: {
1242 SMLoc Loc = getTok().getLoc();
1243 int64_t IntVal = getTok().getIntVal();
1244 Res = MCConstantExpr::create(Value: IntVal, Ctx&: getContext());
1245 EndLoc = Lexer.getTok().getEndLoc();
1246 Lex(); // Eat token.
1247 // Look for 'b' or 'f' following an Integer as a directional label
1248 if (Lexer.getKind() == AsmToken::Identifier) {
1249 StringRef IDVal = getTok().getString();
1250 // Lookup the symbol variant if used.
1251 std::pair<StringRef, StringRef> Split = IDVal.split(Separator: '@');
1252 uint16_t Spec = 0;
1253 if (Split.first.size() != IDVal.size()) {
1254 auto MaybeSpec = MAI.getSpecifierForName(Name: Split.second);
1255 if (!MaybeSpec)
1256 return TokError(Msg: "invalid variant '" + Split.second + "'");
1257 IDVal = Split.first;
1258 Spec = *MaybeSpec;
1259 }
1260 if (IDVal == "f" || IDVal == "b") {
1261 MCSymbol *Sym =
1262 Ctx.getDirectionalLocalSymbol(LocalLabelVal: IntVal, Before: IDVal == "b");
1263 Res = MCSymbolRefExpr::create(Symbol: Sym, specifier: Spec, Ctx&: getContext(), Loc);
1264 if (IDVal == "b" && Sym->isUndefined())
1265 return Error(L: Loc, Msg: "directional label undefined");
1266 DirLabels.push_back(Elt: std::make_tuple(args&: Loc, args&: CppHashInfo, args&: Sym));
1267 EndLoc = Lexer.getTok().getEndLoc();
1268 Lex(); // Eat identifier.
1269 }
1270 }
1271 return false;
1272 }
1273 case AsmToken::Real: {
1274 APFloat RealVal(APFloat::IEEEdouble(), getTok().getString());
1275 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
1276 Res = MCConstantExpr::create(Value: IntVal, Ctx&: getContext());
1277 EndLoc = Lexer.getTok().getEndLoc();
1278 Lex(); // Eat token.
1279 return false;
1280 }
1281 case AsmToken::Dot: {
1282 if (MAI.isHLASM())
1283 return TokError(Msg: "cannot use . as current PC");
1284
1285 // This is a '.' reference, which references the current PC. Emit a
1286 // temporary label to the streamer and refer to it.
1287 MCSymbol *Sym = Ctx.createTempSymbol();
1288 Out.emitLabel(Symbol: Sym);
1289 Res = MCSymbolRefExpr::create(Symbol: Sym, Ctx&: getContext());
1290 EndLoc = Lexer.getTok().getEndLoc();
1291 Lex(); // Eat identifier.
1292 return false;
1293 }
1294 case AsmToken::LParen:
1295 Lex(); // Eat the '('.
1296 return parseParenExpr(Res, EndLoc);
1297 case AsmToken::LBrac:
1298 if (!PlatformParser->HasBracketExpressions())
1299 return TokError(Msg: "brackets expression not supported on this target");
1300 Lex(); // Eat the '['.
1301 return parseBracketExpr(Res, EndLoc);
1302 case AsmToken::Minus:
1303 Lex(); // Eat the operator.
1304 if (parsePrimaryExpr(Res, EndLoc, TypeInfo))
1305 return true;
1306 Res = MCUnaryExpr::createMinus(Expr: Res, Ctx&: getContext(), Loc: FirstTokenLoc);
1307 return false;
1308 case AsmToken::Plus:
1309 Lex(); // Eat the operator.
1310 if (parsePrimaryExpr(Res, EndLoc, TypeInfo))
1311 return true;
1312 Res = MCUnaryExpr::createPlus(Expr: Res, Ctx&: getContext(), Loc: FirstTokenLoc);
1313 return false;
1314 case AsmToken::Tilde:
1315 Lex(); // Eat the operator.
1316 if (parsePrimaryExpr(Res, EndLoc, TypeInfo))
1317 return true;
1318 Res = MCUnaryExpr::createNot(Expr: Res, Ctx&: getContext(), Loc: FirstTokenLoc);
1319 return false;
1320 }
1321}
1322
1323bool AsmParser::parseExpression(const MCExpr *&Res) {
1324 SMLoc EndLoc;
1325 return parseExpression(Res, EndLoc);
1326}
1327
1328const MCExpr *MCAsmParser::applySpecifier(const MCExpr *E, uint32_t Spec) {
1329 // Ask the target implementation about this expression first.
1330 const MCExpr *NewE = getTargetParser().applySpecifier(E, Spec, Ctx);
1331 if (NewE)
1332 return NewE;
1333 // Recurse over the given expression, rebuilding it to apply the given variant
1334 // if there is exactly one symbol.
1335 switch (E->getKind()) {
1336 case MCExpr::Specifier:
1337 llvm_unreachable("cannot apply another specifier to MCSpecifierExpr");
1338 case MCExpr::Target:
1339 case MCExpr::Constant:
1340 return nullptr;
1341
1342 case MCExpr::SymbolRef: {
1343 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(Val: E);
1344
1345 if (SRE->getSpecifier()) {
1346 TokError(Msg: "invalid variant on expression '" + getTok().getIdentifier() +
1347 "' (already modified)");
1348 return E;
1349 }
1350
1351 return MCSymbolRefExpr::create(Symbol: &SRE->getSymbol(), specifier: Spec, Ctx&: getContext(),
1352 Loc: SRE->getLoc());
1353 }
1354
1355 case MCExpr::Unary: {
1356 const MCUnaryExpr *UE = cast<MCUnaryExpr>(Val: E);
1357 const MCExpr *Sub = applySpecifier(E: UE->getSubExpr(), Spec);
1358 if (!Sub)
1359 return nullptr;
1360 return MCUnaryExpr::create(Op: UE->getOpcode(), Expr: Sub, Ctx&: getContext(),
1361 Loc: UE->getLoc());
1362 }
1363
1364 case MCExpr::Binary: {
1365 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Val: E);
1366 const MCExpr *LHS = applySpecifier(E: BE->getLHS(), Spec);
1367 const MCExpr *RHS = applySpecifier(E: BE->getRHS(), Spec);
1368
1369 if (!LHS && !RHS)
1370 return nullptr;
1371
1372 if (!LHS)
1373 LHS = BE->getLHS();
1374 if (!RHS)
1375 RHS = BE->getRHS();
1376
1377 return MCBinaryExpr::create(Op: BE->getOpcode(), LHS, RHS, Ctx&: getContext(),
1378 Loc: BE->getLoc());
1379 }
1380 }
1381
1382 llvm_unreachable("Invalid expression kind!");
1383}
1384
1385/// This function checks if the next token is <string> type or arithmetic.
1386/// string that begin with character '<' must end with character '>'.
1387/// otherwise it is arithmetics.
1388/// If the function returns a 'true' value,
1389/// the End argument will be filled with the last location pointed to the '>'
1390/// character.
1391
1392/// There is a gap between the AltMacro's documentation and the single quote
1393/// implementation. GCC does not fully support this feature and so we will not
1394/// support it.
1395/// TODO: Adding single quote as a string.
1396static bool isAngleBracketString(SMLoc &StrLoc, SMLoc &EndLoc) {
1397 assert((StrLoc.getPointer() != nullptr) &&
1398 "Argument to the function cannot be a NULL value");
1399 const char *CharPtr = StrLoc.getPointer();
1400 while ((*CharPtr != '>') && (*CharPtr != '\n') && (*CharPtr != '\r') &&
1401 (*CharPtr != '\0')) {
1402 if (*CharPtr == '!')
1403 CharPtr++;
1404 CharPtr++;
1405 }
1406 if (*CharPtr == '>') {
1407 EndLoc = StrLoc.getFromPointer(Ptr: CharPtr + 1);
1408 return true;
1409 }
1410 return false;
1411}
1412
1413/// creating a string without the escape characters '!'.
1414static std::string angleBracketString(StringRef AltMacroStr) {
1415 std::string Res;
1416 for (size_t Pos = 0; Pos < AltMacroStr.size(); Pos++) {
1417 if (AltMacroStr[Pos] == '!')
1418 Pos++;
1419 Res += AltMacroStr[Pos];
1420 }
1421 return Res;
1422}
1423
1424bool MCAsmParser::parseAtSpecifier(const MCExpr *&Res, SMLoc &EndLoc) {
1425 if (parseOptionalToken(T: AsmToken::At)) {
1426 if (getLexer().isNot(K: AsmToken::Identifier))
1427 return TokError(Msg: "expected specifier following '@'");
1428
1429 auto Spec = MAI.getSpecifierForName(Name: getTok().getIdentifier());
1430 if (!Spec)
1431 return TokError(Msg: "invalid specifier '@" + getTok().getIdentifier() + "'");
1432
1433 const MCExpr *ModifiedRes = applySpecifier(E: Res, Spec: *Spec);
1434 if (ModifiedRes)
1435 Res = ModifiedRes;
1436 Lex();
1437 }
1438 return false;
1439}
1440
1441/// Parse an expression and return it.
1442///
1443/// expr ::= expr &&,|| expr -> lowest.
1444/// expr ::= expr |,^,&,! expr
1445/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1446/// expr ::= expr <<,>> expr
1447/// expr ::= expr +,- expr
1448/// expr ::= expr *,/,% expr -> highest.
1449/// expr ::= primaryexpr
1450///
1451bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
1452 // Parse the expression.
1453 Res = nullptr;
1454 auto &TS = getTargetParser();
1455 if (TS.parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(Precedence: 1, Res, EndLoc))
1456 return true;
1457
1458 // As a special case, we support 'a op b @ modifier' by rewriting the
1459 // expression to include the modifier. This is inefficient, but in general we
1460 // expect users to use 'a@modifier op b'.
1461 if (Lexer.getAllowAtInIdentifier() && parseOptionalToken(T: AsmToken::At)) {
1462 if (Lexer.isNot(K: AsmToken::Identifier))
1463 return TokError(Msg: "unexpected symbol modifier following '@'");
1464
1465 auto Spec = MAI.getSpecifierForName(Name: getTok().getIdentifier());
1466 if (!Spec)
1467 return TokError(Msg: "invalid variant '" + getTok().getIdentifier() + "'");
1468
1469 const MCExpr *ModifiedRes = applySpecifier(E: Res, Spec: *Spec);
1470 if (!ModifiedRes) {
1471 return TokError(Msg: "invalid modifier '" + getTok().getIdentifier() +
1472 "' (no symbols present)");
1473 }
1474
1475 Res = ModifiedRes;
1476 Lex();
1477 }
1478
1479 // Try to constant fold it up front, if possible. Do not exploit
1480 // assembler here.
1481 int64_t Value;
1482 if (Res->evaluateAsAbsolute(Res&: Value))
1483 Res = MCConstantExpr::create(Value, Ctx&: getContext());
1484
1485 return false;
1486}
1487
1488bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
1489 Res = nullptr;
1490 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(Precedence: 1, Res, EndLoc);
1491}
1492
1493bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
1494 const MCExpr *Expr;
1495
1496 SMLoc StartLoc = Lexer.getLoc();
1497 if (parseExpression(Res&: Expr))
1498 return true;
1499
1500 if (!Expr->evaluateAsAbsolute(Res, Asm: getStreamer().getAssemblerPtr()))
1501 return Error(L: StartLoc, Msg: "expected absolute expression");
1502
1503 return false;
1504}
1505
1506static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K,
1507 MCBinaryExpr::Opcode &Kind,
1508 bool ShouldUseLogicalShr) {
1509 switch (K) {
1510 default:
1511 return 0; // not a binop.
1512
1513 // Lowest Precedence: &&, ||
1514 case AsmToken::AmpAmp:
1515 Kind = MCBinaryExpr::LAnd;
1516 return 1;
1517 case AsmToken::PipePipe:
1518 Kind = MCBinaryExpr::LOr;
1519 return 1;
1520
1521 // Low Precedence: |, &, ^
1522 case AsmToken::Pipe:
1523 Kind = MCBinaryExpr::Or;
1524 return 2;
1525 case AsmToken::Caret:
1526 Kind = MCBinaryExpr::Xor;
1527 return 2;
1528 case AsmToken::Amp:
1529 Kind = MCBinaryExpr::And;
1530 return 2;
1531
1532 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
1533 case AsmToken::EqualEqual:
1534 Kind = MCBinaryExpr::EQ;
1535 return 3;
1536 case AsmToken::ExclaimEqual:
1537 case AsmToken::LessGreater:
1538 Kind = MCBinaryExpr::NE;
1539 return 3;
1540 case AsmToken::Less:
1541 Kind = MCBinaryExpr::LT;
1542 return 3;
1543 case AsmToken::LessEqual:
1544 Kind = MCBinaryExpr::LTE;
1545 return 3;
1546 case AsmToken::Greater:
1547 Kind = MCBinaryExpr::GT;
1548 return 3;
1549 case AsmToken::GreaterEqual:
1550 Kind = MCBinaryExpr::GTE;
1551 return 3;
1552
1553 // Intermediate Precedence: <<, >>
1554 case AsmToken::LessLess:
1555 Kind = MCBinaryExpr::Shl;
1556 return 4;
1557 case AsmToken::GreaterGreater:
1558 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
1559 return 4;
1560
1561 // High Intermediate Precedence: +, -
1562 case AsmToken::Plus:
1563 Kind = MCBinaryExpr::Add;
1564 return 5;
1565 case AsmToken::Minus:
1566 Kind = MCBinaryExpr::Sub;
1567 return 5;
1568
1569 // Highest Precedence: *, /, %
1570 case AsmToken::Star:
1571 Kind = MCBinaryExpr::Mul;
1572 return 6;
1573 case AsmToken::Slash:
1574 Kind = MCBinaryExpr::Div;
1575 return 6;
1576 case AsmToken::Percent:
1577 Kind = MCBinaryExpr::Mod;
1578 return 6;
1579 }
1580}
1581
1582static unsigned getGNUBinOpPrecedence(const MCAsmInfo &MAI,
1583 AsmToken::TokenKind K,
1584 MCBinaryExpr::Opcode &Kind,
1585 bool ShouldUseLogicalShr) {
1586 switch (K) {
1587 default:
1588 return 0; // not a binop.
1589
1590 // Lowest Precedence: &&, ||
1591 case AsmToken::AmpAmp:
1592 Kind = MCBinaryExpr::LAnd;
1593 return 2;
1594 case AsmToken::PipePipe:
1595 Kind = MCBinaryExpr::LOr;
1596 return 1;
1597
1598 // Low Precedence: ==, !=, <>, <, <=, >, >=
1599 case AsmToken::EqualEqual:
1600 Kind = MCBinaryExpr::EQ;
1601 return 3;
1602 case AsmToken::ExclaimEqual:
1603 case AsmToken::LessGreater:
1604 Kind = MCBinaryExpr::NE;
1605 return 3;
1606 case AsmToken::Less:
1607 Kind = MCBinaryExpr::LT;
1608 return 3;
1609 case AsmToken::LessEqual:
1610 Kind = MCBinaryExpr::LTE;
1611 return 3;
1612 case AsmToken::Greater:
1613 Kind = MCBinaryExpr::GT;
1614 return 3;
1615 case AsmToken::GreaterEqual:
1616 Kind = MCBinaryExpr::GTE;
1617 return 3;
1618
1619 // Low Intermediate Precedence: +, -
1620 case AsmToken::Plus:
1621 Kind = MCBinaryExpr::Add;
1622 return 4;
1623 case AsmToken::Minus:
1624 Kind = MCBinaryExpr::Sub;
1625 return 4;
1626
1627 // High Intermediate Precedence: |, !, &, ^
1628 //
1629 case AsmToken::Pipe:
1630 Kind = MCBinaryExpr::Or;
1631 return 5;
1632 case AsmToken::Exclaim:
1633 // Hack to support ARM compatible aliases (implied 'sp' operand in 'srs*'
1634 // instructions like 'srsda #31!') and not parse ! as an infix operator.
1635 if (MAI.getCommentString() == "@")
1636 return 0;
1637 Kind = MCBinaryExpr::OrNot;
1638 return 5;
1639 case AsmToken::Caret:
1640 Kind = MCBinaryExpr::Xor;
1641 return 5;
1642 case AsmToken::Amp:
1643 Kind = MCBinaryExpr::And;
1644 return 5;
1645
1646 // Highest Precedence: *, /, %, <<, >>
1647 case AsmToken::Star:
1648 Kind = MCBinaryExpr::Mul;
1649 return 6;
1650 case AsmToken::Slash:
1651 Kind = MCBinaryExpr::Div;
1652 return 6;
1653 case AsmToken::Percent:
1654 Kind = MCBinaryExpr::Mod;
1655 return 6;
1656 case AsmToken::LessLess:
1657 Kind = MCBinaryExpr::Shl;
1658 return 6;
1659 case AsmToken::GreaterGreater:
1660 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
1661 return 6;
1662 }
1663}
1664
1665unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K,
1666 MCBinaryExpr::Opcode &Kind) {
1667 bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr();
1668 return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr)
1669 : getGNUBinOpPrecedence(MAI, K, Kind, ShouldUseLogicalShr);
1670}
1671
1672/// Parse all binary operators with precedence >= 'Precedence'.
1673/// Res contains the LHS of the expression on input.
1674bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
1675 SMLoc &EndLoc) {
1676 SMLoc StartLoc = Lexer.getLoc();
1677 while (true) {
1678 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
1679 unsigned TokPrec = getBinOpPrecedence(K: Lexer.getKind(), Kind);
1680
1681 // If the next token is lower precedence than we are allowed to eat, return
1682 // successfully with what we ate already.
1683 if (TokPrec < Precedence)
1684 return false;
1685
1686 Lex();
1687
1688 // Eat the next primary expression.
1689 const MCExpr *RHS;
1690 if (getTargetParser().parsePrimaryExpr(Res&: RHS, EndLoc))
1691 return true;
1692
1693 // If BinOp binds less tightly with RHS than the operator after RHS, let
1694 // the pending operator take RHS as its LHS.
1695 MCBinaryExpr::Opcode Dummy;
1696 unsigned NextTokPrec = getBinOpPrecedence(K: Lexer.getKind(), Kind&: Dummy);
1697 if (TokPrec < NextTokPrec && parseBinOpRHS(Precedence: TokPrec + 1, Res&: RHS, EndLoc))
1698 return true;
1699
1700 // Merge LHS and RHS according to operator.
1701 Res = MCBinaryExpr::create(Op: Kind, LHS: Res, RHS, Ctx&: getContext(), Loc: StartLoc);
1702 }
1703}
1704
1705/// ParseStatement:
1706/// ::= EndOfStatement
1707/// ::= Label* Directive ...Operands... EndOfStatement
1708/// ::= Label* Identifier OperandList* EndOfStatement
1709bool AsmParser::parseStatement(ParseStatementInfo &Info,
1710 MCAsmParserSemaCallback *SI) {
1711 assert(!hasPendingError() && "parseStatement started with pending error");
1712 // Eat initial spaces and comments
1713 while (Lexer.is(K: AsmToken::Space))
1714 Lex();
1715 if (Lexer.is(K: AsmToken::EndOfStatement)) {
1716 // if this is a line comment we can drop it safely
1717 if (getTok().getString().empty() || getTok().getString().front() == '\r' ||
1718 getTok().getString().front() == '\n')
1719 Out.addBlankLine();
1720 Lex();
1721 return false;
1722 }
1723 // Statements always start with an identifier.
1724 AsmToken ID = getTok();
1725 SMLoc IDLoc = ID.getLoc();
1726 StringRef IDVal;
1727 int64_t LocalLabelVal = -1;
1728 StartTokLoc = ID.getLoc();
1729 if (Lexer.is(K: AsmToken::HashDirective))
1730 return parseCppHashLineFilenameComment(L: IDLoc,
1731 SaveLocInfo: !isInsideMacroInstantiation());
1732
1733 // Allow an integer followed by a ':' as a directional local label.
1734 if (Lexer.is(K: AsmToken::Integer)) {
1735 LocalLabelVal = getTok().getIntVal();
1736 if (LocalLabelVal < 0) {
1737 if (!TheCondState.Ignore) {
1738 Lex(); // always eat a token
1739 return Error(L: IDLoc, Msg: "unexpected token at start of statement");
1740 }
1741 IDVal = "";
1742 } else {
1743 IDVal = getTok().getString();
1744 Lex(); // Consume the integer token to be used as an identifier token.
1745 if (Lexer.getKind() != AsmToken::Colon) {
1746 if (!TheCondState.Ignore) {
1747 Lex(); // always eat a token
1748 return Error(L: IDLoc, Msg: "unexpected token at start of statement");
1749 }
1750 }
1751 }
1752 } else if (Lexer.is(K: AsmToken::Dot)) {
1753 // Treat '.' as a valid identifier in this context.
1754 Lex();
1755 IDVal = ".";
1756 } else if (getTargetParser().tokenIsStartOfStatement(Token: ID.getKind())) {
1757 Lex();
1758 IDVal = ID.getString();
1759 } else if (parseIdentifier(Res&: IDVal)) {
1760 if (!TheCondState.Ignore) {
1761 Lex(); // always eat a token
1762 return Error(L: IDLoc, Msg: "unexpected token at start of statement");
1763 }
1764 IDVal = "";
1765 }
1766
1767 // Handle conditional assembly here before checking for skipping. We
1768 // have to do this so that .endif isn't skipped in a ".if 0" block for
1769 // example.
1770 StringMap<DirectiveKind>::const_iterator DirKindIt =
1771 DirectiveKindMap.find(Key: IDVal.lower());
1772 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1773 ? DK_NO_DIRECTIVE
1774 : DirKindIt->getValue();
1775 switch (DirKind) {
1776 default:
1777 break;
1778 case DK_IF:
1779 case DK_IFEQ:
1780 case DK_IFGE:
1781 case DK_IFGT:
1782 case DK_IFLE:
1783 case DK_IFLT:
1784 case DK_IFNE:
1785 return parseDirectiveIf(DirectiveLoc: IDLoc, DirKind);
1786 case DK_IFB:
1787 return parseDirectiveIfb(DirectiveLoc: IDLoc, ExpectBlank: true);
1788 case DK_IFNB:
1789 return parseDirectiveIfb(DirectiveLoc: IDLoc, ExpectBlank: false);
1790 case DK_IFC:
1791 return parseDirectiveIfc(DirectiveLoc: IDLoc, ExpectEqual: true);
1792 case DK_IFEQS:
1793 return parseDirectiveIfeqs(DirectiveLoc: IDLoc, ExpectEqual: true);
1794 case DK_IFNC:
1795 return parseDirectiveIfc(DirectiveLoc: IDLoc, ExpectEqual: false);
1796 case DK_IFNES:
1797 return parseDirectiveIfeqs(DirectiveLoc: IDLoc, ExpectEqual: false);
1798 case DK_IFDEF:
1799 return parseDirectiveIfdef(DirectiveLoc: IDLoc, expect_defined: true);
1800 case DK_IFNDEF:
1801 case DK_IFNOTDEF:
1802 return parseDirectiveIfdef(DirectiveLoc: IDLoc, expect_defined: false);
1803 case DK_ELSEIF:
1804 return parseDirectiveElseIf(DirectiveLoc: IDLoc);
1805 case DK_ELSE:
1806 return parseDirectiveElse(DirectiveLoc: IDLoc);
1807 case DK_ENDIF:
1808 return parseDirectiveEndIf(DirectiveLoc: IDLoc);
1809 }
1810
1811 // Ignore the statement if in the middle of inactive conditional
1812 // (e.g. ".if 0").
1813 if (TheCondState.Ignore) {
1814 eatToEndOfStatement();
1815 return false;
1816 }
1817
1818 // FIXME: Recurse on local labels?
1819
1820 // Check for a label.
1821 // ::= identifier ':'
1822 // ::= number ':'
1823 if (Lexer.is(K: AsmToken::Colon) && getTargetParser().isLabel(Token&: ID)) {
1824 if (checkForValidSection())
1825 return true;
1826
1827 Lex(); // Consume the ':'.
1828
1829 // Diagnose attempt to use '.' as a label.
1830 if (IDVal == ".")
1831 return Error(L: IDLoc, Msg: "invalid use of pseudo-symbol '.' as a label");
1832
1833 // Diagnose attempt to use a variable as a label.
1834 //
1835 // FIXME: Diagnostics. Note the location of the definition as a label.
1836 // FIXME: This doesn't diagnose assignment to a symbol which has been
1837 // implicitly marked as external.
1838 MCSymbol *Sym;
1839 if (LocalLabelVal == -1) {
1840 if (ParsingMSInlineAsm && SI) {
1841 StringRef RewrittenLabel =
1842 SI->LookupInlineAsmLabel(Identifier: IDVal, SM&: getSourceManager(), Location: IDLoc, Create: true);
1843 assert(!RewrittenLabel.empty() &&
1844 "We should have an internal name here.");
1845 Info.AsmRewrites->emplace_back(Args: AOK_Label, Args&: IDLoc, Args: IDVal.size(),
1846 Args&: RewrittenLabel);
1847 IDVal = RewrittenLabel;
1848 }
1849 Sym = getContext().parseSymbol(Name: IDVal);
1850 } else
1851 Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal);
1852 // End of Labels should be treated as end of line for lexing
1853 // purposes but that information is not available to the Lexer who
1854 // does not understand Labels. This may cause us to see a Hash
1855 // here instead of a preprocessor line comment.
1856 if (getTok().is(K: AsmToken::Hash)) {
1857 StringRef CommentStr = parseStringToEndOfStatement();
1858 Lexer.Lex();
1859 Lexer.UnLex(Token: AsmToken(AsmToken::EndOfStatement, CommentStr));
1860 }
1861
1862 // Consume any end of statement token, if present, to avoid spurious
1863 // addBlankLine calls().
1864 if (getTok().is(K: AsmToken::EndOfStatement)) {
1865 Lex();
1866 }
1867
1868 if (MAI.isMachO() && CFIStartProcLoc) {
1869 auto *SymM = static_cast<MCSymbolMachO *>(Sym);
1870 if (SymM->isExternal() && !SymM->isAltEntry())
1871 return Error(L: StartTokLoc, Msg: "non-private labels cannot appear between "
1872 ".cfi_startproc / .cfi_endproc pairs") &&
1873 Error(L: *CFIStartProcLoc, Msg: "previous .cfi_startproc was here");
1874 }
1875
1876 if (discardLTOSymbol(Name: IDVal))
1877 return false;
1878
1879 getTargetParser().doBeforeLabelEmit(Symbol: Sym, IDLoc);
1880
1881 // Emit the label.
1882 if (!getTargetParser().isParsingMSInlineAsm())
1883 Out.emitLabel(Symbol: Sym, Loc: IDLoc);
1884
1885 // If we are generating dwarf for assembly source files then gather the
1886 // info to make a dwarf label entry for this label if needed.
1887 if (enabledGenDwarfForAssembly())
1888 MCGenDwarfLabelEntry::Make(Symbol: Sym, MCOS: &getStreamer(), SrcMgr&: getSourceManager(),
1889 Loc&: IDLoc);
1890
1891 getTargetParser().onLabelParsed(Symbol: Sym);
1892
1893 return false;
1894 }
1895
1896 // Check for an assignment statement.
1897 // ::= identifier '='
1898 if (Lexer.is(K: AsmToken::Equal) && getTargetParser().equalIsAsmAssignment()) {
1899 Lex();
1900 return parseAssignment(Name: IDVal, Kind: AssignmentKind::Equal);
1901 }
1902
1903 // If macros are enabled, check to see if this is a macro instantiation.
1904 if (areMacrosEnabled())
1905 if (MCAsmMacro *M = getContext().lookupMacro(Name: IDVal))
1906 return handleMacroEntry(M, NameLoc: IDLoc);
1907
1908 // Otherwise, we have a normal instruction or directive.
1909
1910 // Directives start with "."
1911 if (IDVal.starts_with(Prefix: ".") && IDVal != ".") {
1912 // There are several entities interested in parsing directives:
1913 //
1914 // 1. The target-specific assembly parser. Some directives are target
1915 // specific or may potentially behave differently on certain targets.
1916 // 2. Asm parser extensions. For example, platform-specific parsers
1917 // (like the ELF parser) register themselves as extensions.
1918 // 3. The generic directive parser implemented by this class. These are
1919 // all the directives that behave in a target and platform independent
1920 // manner, or at least have a default behavior that's shared between
1921 // all targets and platforms.
1922
1923 getTargetParser().flushPendingInstructions(Out&: getStreamer());
1924
1925 ParseStatus TPDirectiveReturn = getTargetParser().parseDirective(DirectiveID: ID);
1926 assert(TPDirectiveReturn.isFailure() == hasPendingError() &&
1927 "Should only return Failure iff there was an error");
1928 if (TPDirectiveReturn.isFailure())
1929 return true;
1930 if (TPDirectiveReturn.isSuccess())
1931 return false;
1932
1933 // Next, check the extension directive map to see if any extension has
1934 // registered itself to parse this directive.
1935 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1936 ExtensionDirectiveMap.lookup(Key: IDVal);
1937 if (Handler.first)
1938 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1939
1940 // Finally, if no one else is interested in this directive, it must be
1941 // generic and familiar to this class.
1942 switch (DirKind) {
1943 default:
1944 break;
1945 case DK_SET:
1946 case DK_EQU:
1947 return parseDirectiveSet(IDVal, Kind: AssignmentKind::Set);
1948 case DK_EQUIV:
1949 return parseDirectiveSet(IDVal, Kind: AssignmentKind::Equiv);
1950 case DK_LTO_SET_CONDITIONAL:
1951 return parseDirectiveSet(IDVal, Kind: AssignmentKind::LTOSetConditional);
1952 case DK_ASCII:
1953 return parseDirectiveAscii(IDVal, ZeroTerminated: false);
1954 case DK_ASCIZ:
1955 case DK_STRING:
1956 return parseDirectiveAscii(IDVal, ZeroTerminated: true);
1957 case DK_BASE64:
1958 return parseDirectiveBase64();
1959 case DK_BYTE:
1960 case DK_DC_B:
1961 return parseDirectiveValue(IDVal, Size: 1);
1962 case DK_DC:
1963 case DK_DC_W:
1964 case DK_SHORT:
1965 case DK_VALUE:
1966 case DK_2BYTE:
1967 return parseDirectiveValue(IDVal, Size: 2);
1968 case DK_LONG:
1969 case DK_INT:
1970 case DK_4BYTE:
1971 case DK_DC_L:
1972 return parseDirectiveValue(IDVal, Size: 4);
1973 case DK_QUAD:
1974 case DK_8BYTE:
1975 return parseDirectiveValue(IDVal, Size: 8);
1976 case DK_DC_A:
1977 return parseDirectiveValue(
1978 IDVal, Size: getContext().getAsmInfo()->getCodePointerSize());
1979 case DK_OCTA:
1980 return parseDirectiveOctaValue(IDVal);
1981 case DK_SINGLE:
1982 case DK_FLOAT:
1983 case DK_DC_S:
1984 return parseDirectiveRealValue(IDVal, APFloat::IEEEsingle());
1985 case DK_DOUBLE:
1986 case DK_DC_D:
1987 return parseDirectiveRealValue(IDVal, APFloat::IEEEdouble());
1988 case DK_ALIGN: {
1989 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1990 return parseDirectiveAlign(IsPow2, /*ExprSize=*/ValueSize: 1);
1991 }
1992 case DK_ALIGN32: {
1993 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1994 return parseDirectiveAlign(IsPow2, /*ExprSize=*/ValueSize: 4);
1995 }
1996 case DK_BALIGN:
1997 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/ValueSize: 1);
1998 case DK_BALIGNW:
1999 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/ValueSize: 2);
2000 case DK_BALIGNL:
2001 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/ValueSize: 4);
2002 case DK_P2ALIGN:
2003 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/ValueSize: 1);
2004 case DK_P2ALIGNW:
2005 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/ValueSize: 2);
2006 case DK_P2ALIGNL:
2007 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/ValueSize: 4);
2008 case DK_PREFALIGN:
2009 return parseDirectivePrefAlign();
2010 case DK_ORG:
2011 return parseDirectiveOrg();
2012 case DK_FILL:
2013 return parseDirectiveFill();
2014 case DK_ZERO:
2015 return parseDirectiveZero();
2016 case DK_EXTERN:
2017 eatToEndOfStatement(); // .extern is the default, ignore it.
2018 return false;
2019 case DK_GLOBL:
2020 case DK_GLOBAL:
2021 return parseDirectiveSymbolAttribute(Attr: MCSA_Global);
2022 case DK_LAZY_REFERENCE:
2023 return parseDirectiveSymbolAttribute(Attr: MCSA_LazyReference);
2024 case DK_NO_DEAD_STRIP:
2025 return parseDirectiveSymbolAttribute(Attr: MCSA_NoDeadStrip);
2026 case DK_SYMBOL_RESOLVER:
2027 return parseDirectiveSymbolAttribute(Attr: MCSA_SymbolResolver);
2028 case DK_PRIVATE_EXTERN:
2029 return parseDirectiveSymbolAttribute(Attr: MCSA_PrivateExtern);
2030 case DK_REFERENCE:
2031 return parseDirectiveSymbolAttribute(Attr: MCSA_Reference);
2032 case DK_WEAK_DEFINITION:
2033 return parseDirectiveSymbolAttribute(Attr: MCSA_WeakDefinition);
2034 case DK_WEAK_REFERENCE:
2035 return parseDirectiveSymbolAttribute(Attr: MCSA_WeakReference);
2036 case DK_WEAK_DEF_CAN_BE_HIDDEN:
2037 return parseDirectiveSymbolAttribute(Attr: MCSA_WeakDefAutoPrivate);
2038 case DK_COLD:
2039 return parseDirectiveSymbolAttribute(Attr: MCSA_Cold);
2040 case DK_COMM:
2041 case DK_COMMON:
2042 return parseDirectiveComm(/*IsLocal=*/false);
2043 case DK_LCOMM:
2044 return parseDirectiveComm(/*IsLocal=*/true);
2045 case DK_ABORT:
2046 return parseDirectiveAbort(DirectiveLoc: IDLoc);
2047 case DK_INCLUDE:
2048 return parseDirectiveInclude();
2049 case DK_INCBIN:
2050 return parseDirectiveIncbin();
2051 case DK_CODE16:
2052 case DK_CODE16GCC:
2053 return TokError(Msg: Twine(IDVal) +
2054 " not currently supported for this target");
2055 case DK_REPT:
2056 return parseDirectiveRept(DirectiveLoc: IDLoc, Directive: IDVal);
2057 case DK_IRP:
2058 return parseDirectiveIrp(DirectiveLoc: IDLoc);
2059 case DK_IRPC:
2060 return parseDirectiveIrpc(DirectiveLoc: IDLoc);
2061 case DK_ENDR:
2062 return parseDirectiveEndr(DirectiveLoc: IDLoc);
2063 case DK_SLEB128:
2064 return parseDirectiveLEB128(Signed: true);
2065 case DK_ULEB128:
2066 return parseDirectiveLEB128(Signed: false);
2067 case DK_SPACE:
2068 case DK_SKIP:
2069 return parseDirectiveSpace(IDVal);
2070 case DK_FILE:
2071 return parseDirectiveFile(DirectiveLoc: IDLoc);
2072 case DK_LINE:
2073 return parseDirectiveLine();
2074 case DK_LOC:
2075 return parseDirectiveLoc();
2076 case DK_LOC_LABEL:
2077 return parseDirectiveLocLabel(DirectiveLoc: IDLoc);
2078 case DK_STABS:
2079 return parseDirectiveStabs();
2080 case DK_CV_FILE:
2081 return parseDirectiveCVFile();
2082 case DK_CV_FUNC_ID:
2083 return parseDirectiveCVFuncId();
2084 case DK_CV_INLINE_SITE_ID:
2085 return parseDirectiveCVInlineSiteId();
2086 case DK_CV_LOC:
2087 return parseDirectiveCVLoc();
2088 case DK_CV_LINETABLE:
2089 return parseDirectiveCVLinetable();
2090 case DK_CV_INLINE_LINETABLE:
2091 return parseDirectiveCVInlineLinetable();
2092 case DK_CV_DEF_RANGE:
2093 return parseDirectiveCVDefRange();
2094 case DK_CV_STRING:
2095 return parseDirectiveCVString();
2096 case DK_CV_STRINGTABLE:
2097 return parseDirectiveCVStringTable();
2098 case DK_CV_FILECHECKSUMS:
2099 return parseDirectiveCVFileChecksums();
2100 case DK_CV_FILECHECKSUM_OFFSET:
2101 return parseDirectiveCVFileChecksumOffset();
2102 case DK_CV_FPO_DATA:
2103 return parseDirectiveCVFPOData();
2104 case DK_CFI_SECTIONS:
2105 return parseDirectiveCFISections();
2106 case DK_CFI_STARTPROC:
2107 return parseDirectiveCFIStartProc();
2108 case DK_CFI_ENDPROC:
2109 return parseDirectiveCFIEndProc();
2110 case DK_CFI_DEF_CFA:
2111 return parseDirectiveCFIDefCfa(DirectiveLoc: IDLoc);
2112 case DK_CFI_DEF_CFA_OFFSET:
2113 return parseDirectiveCFIDefCfaOffset(DirectiveLoc: IDLoc);
2114 case DK_CFI_ADJUST_CFA_OFFSET:
2115 return parseDirectiveCFIAdjustCfaOffset(DirectiveLoc: IDLoc);
2116 case DK_CFI_DEF_CFA_REGISTER:
2117 return parseDirectiveCFIDefCfaRegister(DirectiveLoc: IDLoc);
2118 case DK_CFI_LLVM_DEF_ASPACE_CFA:
2119 return parseDirectiveCFILLVMDefAspaceCfa(DirectiveLoc: IDLoc);
2120 case DK_CFI_OFFSET:
2121 return parseDirectiveCFIOffset(DirectiveLoc: IDLoc);
2122 case DK_CFI_REL_OFFSET:
2123 return parseDirectiveCFIRelOffset(DirectiveLoc: IDLoc);
2124 case DK_CFI_PERSONALITY:
2125 return parseDirectiveCFIPersonalityOrLsda(IsPersonality: true);
2126 case DK_CFI_LSDA:
2127 return parseDirectiveCFIPersonalityOrLsda(IsPersonality: false);
2128 case DK_CFI_REMEMBER_STATE:
2129 return parseDirectiveCFIRememberState(DirectiveLoc: IDLoc);
2130 case DK_CFI_RESTORE_STATE:
2131 return parseDirectiveCFIRestoreState(DirectiveLoc: IDLoc);
2132 case DK_CFI_SAME_VALUE:
2133 return parseDirectiveCFISameValue(DirectiveLoc: IDLoc);
2134 case DK_CFI_RESTORE:
2135 return parseDirectiveCFIRestore(DirectiveLoc: IDLoc);
2136 case DK_CFI_ESCAPE:
2137 return parseDirectiveCFIEscape(DirectiveLoc: IDLoc);
2138 case DK_CFI_RETURN_COLUMN:
2139 return parseDirectiveCFIReturnColumn(DirectiveLoc: IDLoc);
2140 case DK_CFI_SIGNAL_FRAME:
2141 return parseDirectiveCFISignalFrame(DirectiveLoc: IDLoc);
2142 case DK_CFI_UNDEFINED:
2143 return parseDirectiveCFIUndefined(DirectiveLoc: IDLoc);
2144 case DK_CFI_REGISTER:
2145 return parseDirectiveCFIRegister(DirectiveLoc: IDLoc);
2146 case DK_CFI_WINDOW_SAVE:
2147 return parseDirectiveCFIWindowSave(DirectiveLoc: IDLoc);
2148 case DK_CFI_LABEL:
2149 return parseDirectiveCFILabel(DirectiveLoc: IDLoc);
2150 case DK_CFI_VAL_OFFSET:
2151 return parseDirectiveCFIValOffset(DirectiveLoc: IDLoc);
2152 case DK_MACROS_ON:
2153 case DK_MACROS_OFF:
2154 return parseDirectiveMacrosOnOff(Directive: IDVal);
2155 case DK_MACRO:
2156 return parseDirectiveMacro(DirectiveLoc: IDLoc);
2157 case DK_ALTMACRO:
2158 case DK_NOALTMACRO:
2159 return parseDirectiveAltmacro(Directive: IDVal);
2160 case DK_EXITM:
2161 return parseDirectiveExitMacro(Directive: IDVal);
2162 case DK_ENDM:
2163 case DK_ENDMACRO:
2164 return parseDirectiveEndMacro(Directive: IDVal);
2165 case DK_PURGEM:
2166 return parseDirectivePurgeMacro(DirectiveLoc: IDLoc);
2167 case DK_END:
2168 return parseDirectiveEnd(DirectiveLoc: IDLoc);
2169 case DK_ERR:
2170 return parseDirectiveError(DirectiveLoc: IDLoc, WithMessage: false);
2171 case DK_ERROR:
2172 return parseDirectiveError(DirectiveLoc: IDLoc, WithMessage: true);
2173 case DK_WARNING:
2174 return parseDirectiveWarning(DirectiveLoc: IDLoc);
2175 case DK_RELOC:
2176 return parseDirectiveReloc(DirectiveLoc: IDLoc);
2177 case DK_DCB:
2178 case DK_DCB_W:
2179 return parseDirectiveDCB(IDVal, Size: 2);
2180 case DK_DCB_B:
2181 return parseDirectiveDCB(IDVal, Size: 1);
2182 case DK_DCB_D:
2183 return parseDirectiveRealDCB(IDVal, APFloat::IEEEdouble());
2184 case DK_DCB_L:
2185 return parseDirectiveDCB(IDVal, Size: 4);
2186 case DK_DCB_S:
2187 return parseDirectiveRealDCB(IDVal, APFloat::IEEEsingle());
2188 case DK_DC_X:
2189 case DK_DCB_X:
2190 return TokError(Msg: Twine(IDVal) +
2191 " not currently supported for this target");
2192 case DK_DS:
2193 case DK_DS_W:
2194 return parseDirectiveDS(IDVal, Size: 2);
2195 case DK_DS_B:
2196 return parseDirectiveDS(IDVal, Size: 1);
2197 case DK_DS_D:
2198 return parseDirectiveDS(IDVal, Size: 8);
2199 case DK_DS_L:
2200 case DK_DS_S:
2201 return parseDirectiveDS(IDVal, Size: 4);
2202 case DK_DS_P:
2203 case DK_DS_X:
2204 return parseDirectiveDS(IDVal, Size: 12);
2205 case DK_PRINT:
2206 return parseDirectivePrint(DirectiveLoc: IDLoc);
2207 case DK_ADDRSIG:
2208 return parseDirectiveAddrsig();
2209 case DK_ADDRSIG_SYM:
2210 return parseDirectiveAddrsigSym();
2211 case DK_PSEUDO_PROBE:
2212 return parseDirectivePseudoProbe();
2213 case DK_LTO_DISCARD:
2214 return parseDirectiveLTODiscard();
2215 case DK_MEMTAG:
2216 return parseDirectiveSymbolAttribute(Attr: MCSA_Memtag);
2217 }
2218
2219 return Error(L: IDLoc, Msg: "unknown directive");
2220 }
2221
2222 // __asm _emit or __asm __emit
2223 if (ParsingMSInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
2224 IDVal == "_EMIT" || IDVal == "__EMIT"))
2225 return parseDirectiveMSEmit(DirectiveLoc: IDLoc, Info, Len: IDVal.size());
2226
2227 // __asm align
2228 if (ParsingMSInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
2229 return parseDirectiveMSAlign(DirectiveLoc: IDLoc, Info);
2230
2231 if (ParsingMSInlineAsm && (IDVal == "even" || IDVal == "EVEN"))
2232 Info.AsmRewrites->emplace_back(Args: AOK_EVEN, Args&: IDLoc, Args: 4);
2233 if (checkForValidSection())
2234 return true;
2235
2236 return parseAndMatchAndEmitTargetInstruction(Info, IDVal, ID, IDLoc);
2237}
2238
2239bool AsmParser::parseAndMatchAndEmitTargetInstruction(ParseStatementInfo &Info,
2240 StringRef IDVal,
2241 AsmToken ID,
2242 SMLoc IDLoc) {
2243 // Canonicalize the opcode to lower case.
2244 std::string OpcodeStr = IDVal.lower();
2245 ParseInstructionInfo IInfo(Info.AsmRewrites);
2246 bool ParseHadError = getTargetParser().parseInstruction(Info&: IInfo, Name: OpcodeStr, Token: ID,
2247 Operands&: Info.ParsedOperands);
2248 Info.ParseError = ParseHadError;
2249
2250 // Dump the parsed representation, if requested.
2251 if (getShowParsedOperands()) {
2252 SmallString<256> Str;
2253 raw_svector_ostream OS(Str);
2254 OS << "parsed instruction: [";
2255 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
2256 if (i != 0)
2257 OS << ", ";
2258 Info.ParsedOperands[i]->print(OS, MAI);
2259 }
2260 OS << "]";
2261
2262 printMessage(Loc: IDLoc, Kind: SourceMgr::DK_Note, Msg: OS.str());
2263 }
2264
2265 // Fail even if ParseInstruction erroneously returns false.
2266 if (hasPendingError() || ParseHadError)
2267 return true;
2268
2269 // If we are generating dwarf for the current section then generate a .loc
2270 // directive for the instruction.
2271 if (!ParseHadError && enabledGenDwarfForAssembly() &&
2272 getContext().getGenDwarfSectionSyms().count(
2273 key: getStreamer().getCurrentSectionOnly())) {
2274 unsigned Line;
2275 if (ActiveMacros.empty())
2276 Line = SrcMgr.FindLineNumber(Loc: IDLoc, BufferID: CurBuffer);
2277 else
2278 Line = SrcMgr.FindLineNumber(Loc: ActiveMacros.front()->InstantiationLoc,
2279 BufferID: ActiveMacros.front()->ExitBuffer);
2280
2281 // If we previously parsed a cpp hash file line comment then make sure the
2282 // current Dwarf File is for the CppHashFilename if not then emit the
2283 // Dwarf File table for it and adjust the line number for the .loc.
2284 if (!CppHashInfo.Filename.empty()) {
2285 unsigned FileNumber = getStreamer().emitDwarfFileDirective(
2286 FileNo: 0, Directory: StringRef(), Filename: CppHashInfo.Filename);
2287 getContext().setGenDwarfFileNumber(FileNumber);
2288
2289 unsigned CppHashLocLineNo =
2290 SrcMgr.FindLineNumber(Loc: CppHashInfo.Loc, BufferID: CppHashInfo.Buf);
2291 Line = CppHashInfo.LineNumber - 1 + (Line - CppHashLocLineNo);
2292 }
2293
2294 getStreamer().emitDwarfLocDirective(
2295 FileNo: getContext().getGenDwarfFileNumber(), Line, Column: 0,
2296 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, Isa: 0, Discriminator: 0,
2297 FileName: StringRef());
2298 }
2299
2300 // If parsing succeeded, match the instruction.
2301 if (!ParseHadError) {
2302 uint64_t ErrorInfo;
2303 if (getTargetParser().matchAndEmitInstruction(
2304 IDLoc, Opcode&: Info.Opcode, Operands&: Info.ParsedOperands, Out, ErrorInfo,
2305 MatchingInlineAsm: getTargetParser().isParsingMSInlineAsm()))
2306 return true;
2307 }
2308 return false;
2309}
2310
2311// Parse and erase curly braces marking block start/end
2312bool
2313AsmParser::parseCurlyBlockScope(SmallVectorImpl<AsmRewrite> &AsmStrRewrites) {
2314 // Identify curly brace marking block start/end
2315 if (Lexer.isNot(K: AsmToken::LCurly) && Lexer.isNot(K: AsmToken::RCurly))
2316 return false;
2317
2318 SMLoc StartLoc = Lexer.getLoc();
2319 Lex(); // Eat the brace
2320 if (Lexer.is(K: AsmToken::EndOfStatement))
2321 Lex(); // Eat EndOfStatement following the brace
2322
2323 // Erase the block start/end brace from the output asm string
2324 AsmStrRewrites.emplace_back(Args: AOK_Skip, Args&: StartLoc, Args: Lexer.getLoc().getPointer() -
2325 StartLoc.getPointer());
2326 return true;
2327}
2328
2329/// parseCppHashLineFilenameComment as this:
2330/// ::= # number "filename"
2331bool AsmParser::parseCppHashLineFilenameComment(SMLoc L, bool SaveLocInfo) {
2332 Lex(); // Eat the hash token.
2333 // Lexer only ever emits HashDirective if it fully formed if it's
2334 // done the checking already so this is an internal error.
2335 assert(getTok().is(AsmToken::Integer) &&
2336 "Lexing Cpp line comment: Expected Integer");
2337 int64_t LineNumber = getTok().getIntVal();
2338 Lex();
2339 assert(getTok().is(AsmToken::String) &&
2340 "Lexing Cpp line comment: Expected String");
2341 StringRef Filename = getTok().getString();
2342 Lex();
2343
2344 if (!SaveLocInfo)
2345 return false;
2346
2347 // Get rid of the enclosing quotes.
2348 Filename = Filename.substr(Start: 1, N: Filename.size() - 2);
2349
2350 // Save the SMLoc, Filename and LineNumber for later use by diagnostics
2351 // and possibly DWARF file info.
2352 CppHashInfo.Loc = L;
2353 CppHashInfo.Filename = Filename;
2354 CppHashInfo.LineNumber = LineNumber;
2355 CppHashInfo.Buf = CurBuffer;
2356 if (!HadCppHashFilename) {
2357 HadCppHashFilename = true;
2358 // If we haven't encountered any .file directives, then the first #line
2359 // directive describes the "root" file and directory of the compilation
2360 // unit.
2361 if (getContext().getGenDwarfForAssembly() &&
2362 getContext().getGenDwarfFileNumber() == 0) {
2363 // It's preprocessed, so there is no checksum, and of course no source
2364 // directive.
2365 getContext().setMCLineTableRootFile(
2366 /*CUID=*/0, CompilationDir: getContext().getCompilationDir(), Filename,
2367 /*Cksum=*/Checksum: std::nullopt, /*Source=*/std::nullopt);
2368 }
2369 }
2370 return false;
2371}
2372
2373/// will use the last parsed cpp hash line filename comment
2374/// for the Filename and LineNo if any in the diagnostic.
2375void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
2376 auto *Parser = static_cast<AsmParser *>(Context);
2377 raw_ostream &OS = errs();
2378
2379 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
2380 SMLoc DiagLoc = Diag.getLoc();
2381 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(Loc: DiagLoc);
2382 unsigned CppHashBuf =
2383 Parser->SrcMgr.FindBufferContainingLoc(Loc: Parser->CppHashInfo.Loc);
2384
2385 // Like SourceMgr::printMessage() we need to print the include stack if any
2386 // before printing the message.
2387 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(Loc: DiagLoc);
2388 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
2389 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
2390 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(i: DiagCurBuffer);
2391 DiagSrcMgr.PrintIncludeStack(IncludeLoc: ParentIncludeLoc, OS);
2392 }
2393
2394 // If we have not parsed a cpp hash line filename comment or the source
2395 // manager changed or buffer changed (like in a nested include) then just
2396 // print the normal diagnostic using its Filename and LineNo.
2397 if (!Parser->CppHashInfo.LineNumber || DiagBuf != CppHashBuf) {
2398 if (Parser->SavedDiagHandler)
2399 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
2400 else
2401 Parser->getContext().diagnose(SMD: Diag);
2402 return;
2403 }
2404
2405 // Use the CppHashFilename and calculate a line number based on the
2406 // CppHashInfo.Loc and CppHashInfo.LineNumber relative to this Diag's SMLoc
2407 // for the diagnostic.
2408 const std::string &Filename = std::string(Parser->CppHashInfo.Filename);
2409
2410 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(Loc: DiagLoc, BufferID: DiagBuf);
2411 int CppHashLocLineNo =
2412 Parser->SrcMgr.FindLineNumber(Loc: Parser->CppHashInfo.Loc, BufferID: CppHashBuf);
2413 int LineNo =
2414 Parser->CppHashInfo.LineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
2415
2416 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
2417 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
2418 Diag.getLineContents(), Diag.getRanges());
2419
2420 if (Parser->SavedDiagHandler)
2421 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
2422 else
2423 Parser->getContext().diagnose(SMD: NewDiag);
2424}
2425
2426// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
2427// difference being that that function accepts '@' as part of identifiers and
2428// we can't do that. AsmLexer.cpp should probably be changed to handle
2429// '@' as a special case when needed.
2430static bool isIdentifierChar(char c) {
2431 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
2432 c == '.';
2433}
2434
2435bool AsmParser::expandMacro(raw_svector_ostream &OS, MCAsmMacro &Macro,
2436 ArrayRef<MCAsmMacroParameter> Parameters,
2437 ArrayRef<MCAsmMacroArgument> A,
2438 bool EnableAtPseudoVariable) {
2439 unsigned NParameters = Parameters.size();
2440 auto expandArg = [&](unsigned Index) {
2441 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
2442 bool VarargParameter = HasVararg && Index == (NParameters - 1);
2443 for (const AsmToken &Token : A[Index])
2444 // For altmacro mode, you can write '%expr'.
2445 // The prefix '%' evaluates the expression 'expr'
2446 // and uses the result as a string (e.g. replace %(1+2) with the
2447 // string "3").
2448 // Here, we identify the integer token which is the result of the
2449 // absolute expression evaluation and replace it with its string
2450 // representation.
2451 if (AltMacroMode && Token.getString().front() == '%' &&
2452 Token.is(K: AsmToken::Integer))
2453 // Emit an integer value to the buffer.
2454 OS << Token.getIntVal();
2455 // Only Token that was validated as a string and begins with '<'
2456 // is considered altMacroString!!!
2457 else if (AltMacroMode && Token.getString().front() == '<' &&
2458 Token.is(K: AsmToken::String)) {
2459 OS << angleBracketString(AltMacroStr: Token.getStringContents());
2460 }
2461 // We expect no quotes around the string's contents when
2462 // parsing for varargs.
2463 else if (Token.isNot(K: AsmToken::String) || VarargParameter)
2464 OS << Token.getString();
2465 else
2466 OS << Token.getStringContents();
2467 };
2468
2469 // A macro without parameters is handled differently on Darwin:
2470 // gas accepts no arguments and does no substitutions
2471 StringRef Body = Macro.Body;
2472 size_t I = 0, End = Body.size();
2473 while (I != End) {
2474 if (Body[I] == '\\' && I + 1 != End) {
2475 // Check for \@ and \+ pseudo variables.
2476 if (EnableAtPseudoVariable && Body[I + 1] == '@') {
2477 OS << NumOfMacroInstantiations;
2478 I += 2;
2479 continue;
2480 }
2481 if (Body[I + 1] == '+') {
2482 OS << Macro.Count;
2483 I += 2;
2484 continue;
2485 }
2486 if (Body[I + 1] == '(' && Body[I + 2] == ')') {
2487 I += 3;
2488 continue;
2489 }
2490
2491 size_t Pos = ++I;
2492 while (I != End && isIdentifierChar(c: Body[I]))
2493 ++I;
2494 StringRef Argument(Body.data() + Pos, I - Pos);
2495 if (AltMacroMode && I != End && Body[I] == '&')
2496 ++I;
2497 unsigned Index = 0;
2498 for (; Index < NParameters; ++Index)
2499 if (Parameters[Index].Name == Argument)
2500 break;
2501 if (Index == NParameters)
2502 OS << '\\' << Argument;
2503 else
2504 expandArg(Index);
2505 continue;
2506 }
2507
2508 // In Darwin mode, $ is used for macro expansion, not considered an
2509 // identifier char.
2510 if (Body[I] == '$' && I + 1 != End && IsDarwin && !NParameters) {
2511 // This macro has no parameters, look for $0, $1, etc.
2512 switch (Body[I + 1]) {
2513 // $$ => $
2514 case '$':
2515 OS << '$';
2516 I += 2;
2517 continue;
2518 // $n => number of arguments
2519 case 'n':
2520 OS << A.size();
2521 I += 2;
2522 continue;
2523 default: {
2524 if (!isDigit(C: Body[I + 1]))
2525 break;
2526 // $[0-9] => argument
2527 // Missing arguments are ignored.
2528 unsigned Index = Body[I + 1] - '0';
2529 if (Index < A.size())
2530 for (const AsmToken &Token : A[Index])
2531 OS << Token.getString();
2532 I += 2;
2533 continue;
2534 }
2535 }
2536 }
2537
2538 if (!isIdentifierChar(c: Body[I]) || IsDarwin) {
2539 OS << Body[I++];
2540 continue;
2541 }
2542
2543 const size_t Start = I;
2544 while (++I && isIdentifierChar(c: Body[I])) {
2545 }
2546 StringRef Token(Body.data() + Start, I - Start);
2547 if (AltMacroMode) {
2548 unsigned Index = 0;
2549 for (; Index != NParameters; ++Index)
2550 if (Parameters[Index].Name == Token)
2551 break;
2552 if (Index != NParameters) {
2553 expandArg(Index);
2554 if (I != End && Body[I] == '&')
2555 ++I;
2556 continue;
2557 }
2558 }
2559 OS << Token;
2560 }
2561
2562 ++Macro.Count;
2563 return false;
2564}
2565
2566static bool isOperator(AsmToken::TokenKind kind) {
2567 switch (kind) {
2568 default:
2569 return false;
2570 case AsmToken::Plus:
2571 case AsmToken::Minus:
2572 case AsmToken::Tilde:
2573 case AsmToken::Slash:
2574 case AsmToken::Star:
2575 case AsmToken::Dot:
2576 case AsmToken::Equal:
2577 case AsmToken::EqualEqual:
2578 case AsmToken::Pipe:
2579 case AsmToken::PipePipe:
2580 case AsmToken::Caret:
2581 case AsmToken::Amp:
2582 case AsmToken::AmpAmp:
2583 case AsmToken::Exclaim:
2584 case AsmToken::ExclaimEqual:
2585 case AsmToken::Less:
2586 case AsmToken::LessEqual:
2587 case AsmToken::LessLess:
2588 case AsmToken::LessGreater:
2589 case AsmToken::Greater:
2590 case AsmToken::GreaterEqual:
2591 case AsmToken::GreaterGreater:
2592 return true;
2593 }
2594}
2595
2596namespace {
2597
2598class AsmLexerSkipSpaceRAII {
2599public:
2600 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
2601 Lexer.setSkipSpace(SkipSpace);
2602 }
2603
2604 ~AsmLexerSkipSpaceRAII() {
2605 Lexer.setSkipSpace(true);
2606 }
2607
2608private:
2609 AsmLexer &Lexer;
2610};
2611
2612} // end anonymous namespace
2613
2614bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
2615
2616 if (Vararg) {
2617 if (Lexer.isNot(K: AsmToken::EndOfStatement)) {
2618 StringRef Str = parseStringToEndOfStatement();
2619 MA.emplace_back(args: AsmToken::String, args&: Str);
2620 }
2621 return false;
2622 }
2623
2624 unsigned ParenLevel = 0;
2625
2626 // Darwin doesn't use spaces to delmit arguments.
2627 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
2628
2629 bool SpaceEaten;
2630
2631 while (true) {
2632 SpaceEaten = false;
2633 if (Lexer.is(K: AsmToken::Eof) || Lexer.is(K: AsmToken::Equal))
2634 return TokError(Msg: "unexpected token in macro instantiation");
2635
2636 if (ParenLevel == 0) {
2637
2638 if (Lexer.is(K: AsmToken::Comma))
2639 break;
2640
2641 if (parseOptionalToken(T: AsmToken::Space))
2642 SpaceEaten = true;
2643
2644 // Spaces can delimit parameters, but could also be part an expression.
2645 // If the token after a space is an operator, add the token and the next
2646 // one into this argument
2647 if (!IsDarwin) {
2648 if (isOperator(kind: Lexer.getKind())) {
2649 MA.push_back(x: getTok());
2650 Lexer.Lex();
2651
2652 // Whitespace after an operator can be ignored.
2653 parseOptionalToken(T: AsmToken::Space);
2654 continue;
2655 }
2656 }
2657 if (SpaceEaten)
2658 break;
2659 }
2660
2661 // handleMacroEntry relies on not advancing the lexer here
2662 // to be able to fill in the remaining default parameter values
2663 if (Lexer.is(K: AsmToken::EndOfStatement))
2664 break;
2665
2666 // Adjust the current parentheses level.
2667 if (Lexer.is(K: AsmToken::LParen))
2668 ++ParenLevel;
2669 else if (Lexer.is(K: AsmToken::RParen) && ParenLevel)
2670 --ParenLevel;
2671
2672 // Append the token to the current argument list.
2673 MA.push_back(x: getTok());
2674 Lexer.Lex();
2675 }
2676
2677 if (ParenLevel != 0)
2678 return TokError(Msg: "unbalanced parentheses in macro argument");
2679 return false;
2680}
2681
2682// Parse the macro instantiation arguments.
2683bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
2684 MCAsmMacroArguments &A) {
2685 const unsigned NParameters = M ? M->Parameters.size() : 0;
2686 bool NamedParametersFound = false;
2687 SmallVector<SMLoc, 4> FALocs;
2688
2689 A.resize(new_size: NParameters);
2690 FALocs.resize(N: NParameters);
2691
2692 // Parse two kinds of macro invocations:
2693 // - macros defined without any parameters accept an arbitrary number of them
2694 // - macros defined with parameters accept at most that many of them
2695 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
2696 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
2697 ++Parameter) {
2698 SMLoc IDLoc = Lexer.getLoc();
2699 MCAsmMacroParameter FA;
2700
2701 if (Lexer.is(K: AsmToken::Identifier) && Lexer.peekTok().is(K: AsmToken::Equal)) {
2702 if (parseIdentifier(Res&: FA.Name))
2703 return Error(L: IDLoc, Msg: "invalid argument identifier for formal argument");
2704
2705 if (Lexer.isNot(K: AsmToken::Equal))
2706 return TokError(Msg: "expected '=' after formal parameter identifier");
2707
2708 Lex();
2709
2710 NamedParametersFound = true;
2711 }
2712 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2713
2714 if (NamedParametersFound && FA.Name.empty())
2715 return Error(L: IDLoc, Msg: "cannot mix positional and keyword arguments");
2716
2717 SMLoc StrLoc = Lexer.getLoc();
2718 SMLoc EndLoc;
2719 if (AltMacroMode && Lexer.is(K: AsmToken::Percent)) {
2720 const MCExpr *AbsoluteExp;
2721 int64_t Value;
2722 /// Eat '%'
2723 Lex();
2724 if (parseExpression(Res&: AbsoluteExp, EndLoc))
2725 return false;
2726 if (!AbsoluteExp->evaluateAsAbsolute(Res&: Value,
2727 Asm: getStreamer().getAssemblerPtr()))
2728 return Error(L: StrLoc, Msg: "expected absolute expression");
2729 const char *StrChar = StrLoc.getPointer();
2730 const char *EndChar = EndLoc.getPointer();
2731 AsmToken newToken(AsmToken::Integer,
2732 StringRef(StrChar, EndChar - StrChar), Value);
2733 FA.Value.push_back(x: newToken);
2734 } else if (AltMacroMode && Lexer.is(K: AsmToken::Less) &&
2735 isAngleBracketString(StrLoc, EndLoc)) {
2736 const char *StrChar = StrLoc.getPointer();
2737 const char *EndChar = EndLoc.getPointer();
2738 jumpToLoc(Loc: EndLoc, InBuffer: CurBuffer);
2739 /// Eat from '<' to '>'
2740 Lex();
2741 AsmToken newToken(AsmToken::String,
2742 StringRef(StrChar, EndChar - StrChar));
2743 FA.Value.push_back(x: newToken);
2744 } else if(parseMacroArgument(MA&: FA.Value, Vararg))
2745 return true;
2746
2747 unsigned PI = Parameter;
2748 if (!FA.Name.empty()) {
2749 unsigned FAI = 0;
2750 for (FAI = 0; FAI < NParameters; ++FAI)
2751 if (M->Parameters[FAI].Name == FA.Name)
2752 break;
2753
2754 if (FAI >= NParameters) {
2755 assert(M && "expected macro to be defined");
2756 return Error(L: IDLoc, Msg: "parameter named '" + FA.Name +
2757 "' does not exist for macro '" + M->Name + "'");
2758 }
2759 PI = FAI;
2760 }
2761
2762 if (!FA.Value.empty()) {
2763 if (A.size() <= PI)
2764 A.resize(new_size: PI + 1);
2765 A[PI] = FA.Value;
2766
2767 if (FALocs.size() <= PI)
2768 FALocs.resize(N: PI + 1);
2769
2770 FALocs[PI] = Lexer.getLoc();
2771 }
2772
2773 // At the end of the statement, fill in remaining arguments that have
2774 // default values. If there aren't any, then the next argument is
2775 // required but missing
2776 if (Lexer.is(K: AsmToken::EndOfStatement)) {
2777 bool Failure = false;
2778 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2779 if (A[FAI].empty()) {
2780 if (M->Parameters[FAI].Required) {
2781 Error(L: FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2782 Msg: "missing value for required parameter "
2783 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2784 Failure = true;
2785 }
2786
2787 if (!M->Parameters[FAI].Value.empty())
2788 A[FAI] = M->Parameters[FAI].Value;
2789 }
2790 }
2791 return Failure;
2792 }
2793
2794 parseOptionalToken(T: AsmToken::Comma);
2795 }
2796
2797 return TokError(Msg: "too many positional arguments");
2798}
2799
2800bool AsmParser::handleMacroEntry(MCAsmMacro *M, SMLoc NameLoc) {
2801 // Arbitrarily limit macro nesting depth (default matches 'as'). We can
2802 // eliminate this, although we should protect against infinite loops.
2803 unsigned MaxNestingDepth = AsmMacroMaxNestingDepth;
2804 if (ActiveMacros.size() == MaxNestingDepth) {
2805 std::ostringstream MaxNestingDepthError;
2806 MaxNestingDepthError << "macros cannot be nested more than "
2807 << MaxNestingDepth << " levels deep."
2808 << " Use -asm-macro-max-nesting-depth to increase "
2809 "this limit.";
2810 return TokError(Msg: MaxNestingDepthError.str());
2811 }
2812
2813 MCAsmMacroArguments A;
2814 if (parseMacroArguments(M, A))
2815 return true;
2816
2817 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2818 // to hold the macro body with substitutions.
2819 SmallString<256> Buf;
2820 raw_svector_ostream OS(Buf);
2821
2822 if ((!IsDarwin || M->Parameters.size()) && M->Parameters.size() != A.size())
2823 return Error(L: getTok().getLoc(), Msg: "Wrong number of arguments");
2824 if (expandMacro(OS, Macro&: *M, Parameters: M->Parameters, A, EnableAtPseudoVariable: true))
2825 return true;
2826
2827 // We include the .endmacro in the buffer as our cue to exit the macro
2828 // instantiation.
2829 OS << ".endmacro\n";
2830
2831 std::unique_ptr<MemoryBuffer> Instantiation =
2832 MemoryBuffer::getMemBufferCopy(InputData: OS.str(), BufferName: "<instantiation>");
2833
2834 // Create the macro instantiation object and add to the current macro
2835 // instantiation stack.
2836 MacroInstantiation *MI = new MacroInstantiation{
2837 .InstantiationLoc: NameLoc, .ExitBuffer: CurBuffer, .ExitLoc: getTok().getLoc(), .CondStackDepth: TheCondStack.size()};
2838 ActiveMacros.push_back(x: MI);
2839
2840 ++NumOfMacroInstantiations;
2841
2842 // Jump to the macro instantiation and prime the lexer.
2843 CurBuffer = SrcMgr.AddNewSourceBuffer(F: std::move(Instantiation), IncludeLoc: SMLoc());
2844 Lexer.setBuffer(Buf: SrcMgr.getMemoryBuffer(i: CurBuffer)->getBuffer());
2845 Lex();
2846
2847 return false;
2848}
2849
2850void AsmParser::handleMacroExit() {
2851 // Jump to the EndOfStatement we should return to, and consume it.
2852 jumpToLoc(Loc: ActiveMacros.back()->ExitLoc, InBuffer: ActiveMacros.back()->ExitBuffer);
2853 Lex();
2854 // If .endm/.endr is followed by \n instead of a comment, consume it so that
2855 // we don't print an excess \n.
2856 if (getTok().is(K: AsmToken::EndOfStatement))
2857 Lex();
2858
2859 // Pop the instantiation entry.
2860 delete ActiveMacros.back();
2861 ActiveMacros.pop_back();
2862}
2863
2864bool AsmParser::parseAssignment(StringRef Name, AssignmentKind Kind) {
2865 MCSymbol *Sym;
2866 const MCExpr *Value;
2867 SMLoc ExprLoc = getTok().getLoc();
2868 bool AllowRedef =
2869 Kind == AssignmentKind::Set || Kind == AssignmentKind::Equal;
2870 if (MCParserUtils::parseAssignmentExpression(Name, allow_redef: AllowRedef, Parser&: *this, Symbol&: Sym,
2871 Value))
2872 return true;
2873
2874 if (!Sym) {
2875 // In the case where we parse an expression starting with a '.', we will
2876 // not generate an error, nor will we create a symbol. In this case we
2877 // should just return out.
2878 return false;
2879 }
2880
2881 if (discardLTOSymbol(Name))
2882 return false;
2883
2884 // Do the assignment.
2885 switch (Kind) {
2886 case AssignmentKind::Equal:
2887 Out.emitAssignment(Symbol: Sym, Value);
2888 break;
2889 case AssignmentKind::Set:
2890 case AssignmentKind::Equiv:
2891 Out.emitAssignment(Symbol: Sym, Value);
2892 Out.emitSymbolAttribute(Symbol: Sym, Attribute: MCSA_NoDeadStrip);
2893 break;
2894 case AssignmentKind::LTOSetConditional:
2895 if (Value->getKind() != MCExpr::SymbolRef)
2896 return Error(L: ExprLoc, Msg: "expected identifier");
2897
2898 Out.emitConditionalAssignment(Symbol: Sym, Value);
2899 break;
2900 }
2901
2902 return false;
2903}
2904
2905/// parseIdentifier:
2906/// ::= identifier
2907/// ::= string
2908bool AsmParser::parseIdentifier(StringRef &Res) {
2909 // The assembler has relaxed rules for accepting identifiers, in particular we
2910 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2911 // separate tokens. At this level, we have already lexed so we cannot (currently)
2912 // handle this as a context dependent token, instead we detect adjacent tokens
2913 // and return the combined identifier.
2914 if (Lexer.is(K: AsmToken::Dollar) || Lexer.is(K: AsmToken::At)) {
2915 SMLoc PrefixLoc = getLexer().getLoc();
2916
2917 // Consume the prefix character, and check for a following identifier.
2918
2919 AsmToken Buf[1];
2920 Lexer.peekTokens(Buf, ShouldSkipSpace: false);
2921
2922 if (Buf[0].isNot(K: AsmToken::Identifier) && Buf[0].isNot(K: AsmToken::Integer))
2923 return true;
2924
2925 // We have a '$' or '@' followed by an identifier or integer token, make
2926 // sure they are adjacent.
2927 if (PrefixLoc.getPointer() + 1 != Buf[0].getLoc().getPointer())
2928 return true;
2929
2930 // eat $ or @
2931 Lexer.Lex(); // Lexer's Lex guarantees consecutive token.
2932 // Construct the joined identifier and consume the token.
2933 Res = StringRef(PrefixLoc.getPointer(), getTok().getString().size() + 1);
2934 Lex(); // Parser Lex to maintain invariants.
2935 return false;
2936 }
2937
2938 if (Lexer.isNot(K: AsmToken::Identifier) && Lexer.isNot(K: AsmToken::String))
2939 return true;
2940
2941 Res = getTok().getIdentifier();
2942
2943 Lex(); // Consume the identifier token.
2944
2945 return false;
2946}
2947
2948/// parseDirectiveSet:
2949/// ::= .equ identifier ',' expression
2950/// ::= .equiv identifier ',' expression
2951/// ::= .set identifier ',' expression
2952/// ::= .lto_set_conditional identifier ',' expression
2953bool AsmParser::parseDirectiveSet(StringRef IDVal, AssignmentKind Kind) {
2954 StringRef Name;
2955 if (check(P: parseIdentifier(Res&: Name), Msg: "expected identifier") || parseComma() ||
2956 parseAssignment(Name, Kind))
2957 return true;
2958 return false;
2959}
2960
2961bool AsmParser::parseEscapedString(std::string &Data) {
2962 if (check(P: getTok().isNot(K: AsmToken::String), Msg: "expected string"))
2963 return true;
2964
2965 Data = "";
2966 StringRef Str = getTok().getStringContents();
2967 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2968 if (Str[i] != '\\') {
2969 if ((Str[i] == '\n') || (Str[i] == '\r')) {
2970 // Don't double-warn for Windows newlines.
2971 if ((Str[i] == '\n') && (i > 0) && (Str[i - 1] == '\r'))
2972 continue;
2973
2974 SMLoc NewlineLoc = SMLoc::getFromPointer(Ptr: Str.data() + i);
2975 if (Warning(L: NewlineLoc, Msg: "unterminated string; newline inserted"))
2976 return true;
2977 }
2978 Data += Str[i];
2979 continue;
2980 }
2981
2982 // Recognize escaped characters. Note that this escape semantics currently
2983 // loosely follows Darwin 'as'.
2984 ++i;
2985 if (i == e)
2986 return TokError(Msg: "unexpected backslash at end of string");
2987
2988 // Recognize hex sequences similarly to GNU 'as'.
2989 if (Str[i] == 'x' || Str[i] == 'X') {
2990 size_t length = Str.size();
2991 if (i + 1 >= length || !isHexDigit(C: Str[i + 1]))
2992 return TokError(Msg: "invalid hexadecimal escape sequence");
2993
2994 // Consume hex characters. GNU 'as' reads all hexadecimal characters and
2995 // then truncates to the lower 16 bits. Seems reasonable.
2996 unsigned Value = 0;
2997 while (i + 1 < length && isHexDigit(C: Str[i + 1]))
2998 Value = Value * 16 + hexDigitValue(C: Str[++i]);
2999
3000 Data += (unsigned char)(Value & 0xFF);
3001 continue;
3002 }
3003
3004 // Recognize octal sequences.
3005 if ((unsigned)(Str[i] - '0') <= 7) {
3006 // Consume up to three octal characters.
3007 unsigned Value = Str[i] - '0';
3008
3009 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
3010 ++i;
3011 Value = Value * 8 + (Str[i] - '0');
3012
3013 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
3014 ++i;
3015 Value = Value * 8 + (Str[i] - '0');
3016 }
3017 }
3018
3019 if (Value > 255)
3020 return TokError(Msg: "invalid octal escape sequence (out of range)");
3021
3022 Data += (unsigned char)Value;
3023 continue;
3024 }
3025
3026 // Otherwise recognize individual escapes.
3027 switch (Str[i]) {
3028 default:
3029 // Just reject invalid escape sequences for now.
3030 return TokError(Msg: "invalid escape sequence (unrecognized character)");
3031
3032 case 'b': Data += '\b'; break;
3033 case 'f': Data += '\f'; break;
3034 case 'n': Data += '\n'; break;
3035 case 'r': Data += '\r'; break;
3036 case 't': Data += '\t'; break;
3037 case '"': Data += '"'; break;
3038 case '\\': Data += '\\'; break;
3039 }
3040 }
3041
3042 Lex();
3043 return false;
3044}
3045
3046bool AsmParser::parseAngleBracketString(std::string &Data) {
3047 SMLoc EndLoc, StartLoc = getTok().getLoc();
3048 if (isAngleBracketString(StrLoc&: StartLoc, EndLoc)) {
3049 const char *StartChar = StartLoc.getPointer() + 1;
3050 const char *EndChar = EndLoc.getPointer() - 1;
3051 jumpToLoc(Loc: EndLoc, InBuffer: CurBuffer);
3052 /// Eat from '<' to '>'
3053 Lex();
3054
3055 Data = angleBracketString(AltMacroStr: StringRef(StartChar, EndChar - StartChar));
3056 return false;
3057 }
3058 return true;
3059}
3060
3061/// parseDirectiveAscii:
3062// ::= .ascii [ "string"+ ( , "string"+ )* ]
3063/// ::= ( .asciz | .string ) [ "string" ( , "string" )* ]
3064bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
3065 auto parseOp = [&]() -> bool {
3066 std::string Data;
3067 if (checkForValidSection())
3068 return true;
3069 // Only support spaces as separators for .ascii directive for now. See the
3070 // discusssion at https://reviews.llvm.org/D91460 for more details.
3071 do {
3072 if (parseEscapedString(Data))
3073 return true;
3074 getStreamer().emitBytes(Data);
3075 } while (!ZeroTerminated && getTok().is(K: AsmToken::String));
3076 if (ZeroTerminated)
3077 getStreamer().emitBytes(Data: StringRef("\0", 1));
3078 return false;
3079 };
3080
3081 return parseMany(parseOne: parseOp);
3082}
3083
3084/// parseDirectiveBase64:
3085// ::= .base64 "string" (, "string" )*
3086bool AsmParser::parseDirectiveBase64() {
3087 auto parseOp = [&]() -> bool {
3088 if (checkForValidSection())
3089 return true;
3090
3091 if (getTok().isNot(K: AsmToken::String)) {
3092 return true;
3093 }
3094
3095 std::vector<char> Decoded;
3096 std::string const str = getTok().getStringContents().str();
3097 if (check(P: str.empty(), Msg: "expected nonempty string")) {
3098 return true;
3099 }
3100
3101 llvm::Error e = decodeBase64(Input: str, Output&: Decoded);
3102 if (e) {
3103 consumeError(Err: std::move(e));
3104 return Error(L: Lexer.getLoc(), Msg: "failed to base64 decode string data");
3105 }
3106
3107 getStreamer().emitBytes(Data: std::string(Decoded.begin(), Decoded.end()));
3108 Lex();
3109 return false;
3110 };
3111
3112 return check(P: parseMany(parseOne: parseOp), Msg: "expected string");
3113}
3114
3115/// parseDirectiveReloc
3116/// ::= .reloc expression , identifier [ , expression ]
3117bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) {
3118 const MCExpr *Offset;
3119 const MCExpr *Expr = nullptr;
3120
3121 if (parseExpression(Res&: Offset))
3122 return true;
3123 if (parseComma() ||
3124 check(P: getTok().isNot(K: AsmToken::Identifier), Msg: "expected relocation name"))
3125 return true;
3126
3127 SMLoc NameLoc = Lexer.getTok().getLoc();
3128 StringRef Name = Lexer.getTok().getIdentifier();
3129 Lex();
3130
3131 if (Lexer.is(K: AsmToken::Comma)) {
3132 Lex();
3133 SMLoc ExprLoc = Lexer.getLoc();
3134 if (parseExpression(Res&: Expr))
3135 return true;
3136
3137 MCValue Value;
3138 if (!Expr->evaluateAsRelocatable(Res&: Value, Asm: nullptr))
3139 return Error(L: ExprLoc, Msg: "expression must be relocatable");
3140 }
3141
3142 if (parseEOL())
3143 return true;
3144
3145 getStreamer().emitRelocDirective(Offset: *Offset, Name, Expr, Loc: NameLoc);
3146 return false;
3147}
3148
3149/// parseDirectiveValue
3150/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
3151bool AsmParser::parseDirectiveValue(StringRef IDVal, unsigned Size) {
3152 auto parseOp = [&]() -> bool {
3153 const MCExpr *Value;
3154 SMLoc ExprLoc = getLexer().getLoc();
3155 if (checkForValidSection() || getTargetParser().parseDataExpr(Res&: Value))
3156 return true;
3157 // Special case constant expressions to match code generator.
3158 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Val: Value)) {
3159 assert(Size <= 8 && "Invalid size");
3160 uint64_t IntValue = MCE->getValue();
3161 if (!isUIntN(N: 8 * Size, x: IntValue) && !isIntN(N: 8 * Size, x: IntValue))
3162 return Error(L: ExprLoc, Msg: "out of range literal value");
3163 getStreamer().emitIntValue(Value: IntValue, Size);
3164 } else
3165 getStreamer().emitValue(Value, Size, Loc: ExprLoc);
3166 return false;
3167 };
3168
3169 return parseMany(parseOne: parseOp);
3170}
3171
3172static bool parseHexOcta(AsmParser &Asm, uint64_t &hi, uint64_t &lo) {
3173 if (Asm.getTok().isNot(K: AsmToken::Integer) &&
3174 Asm.getTok().isNot(K: AsmToken::BigNum))
3175 return Asm.TokError(Msg: "unknown token in expression");
3176 SMLoc ExprLoc = Asm.getTok().getLoc();
3177 APInt IntValue = Asm.getTok().getAPIntVal();
3178 Asm.Lex();
3179 if (!IntValue.isIntN(N: 128))
3180 return Asm.Error(L: ExprLoc, Msg: "out of range literal value");
3181 if (!IntValue.isIntN(N: 64)) {
3182 hi = IntValue.getHiBits(numBits: IntValue.getBitWidth() - 64).getZExtValue();
3183 lo = IntValue.getLoBits(numBits: 64).getZExtValue();
3184 } else {
3185 hi = 0;
3186 lo = IntValue.getZExtValue();
3187 }
3188 return false;
3189}
3190
3191/// ParseDirectiveOctaValue
3192/// ::= .octa [ hexconstant (, hexconstant)* ]
3193
3194bool AsmParser::parseDirectiveOctaValue(StringRef IDVal) {
3195 auto parseOp = [&]() -> bool {
3196 if (checkForValidSection())
3197 return true;
3198 uint64_t hi, lo;
3199 if (parseHexOcta(Asm&: *this, hi, lo))
3200 return true;
3201 if (MAI.isLittleEndian()) {
3202 getStreamer().emitInt64(Value: lo);
3203 getStreamer().emitInt64(Value: hi);
3204 } else {
3205 getStreamer().emitInt64(Value: hi);
3206 getStreamer().emitInt64(Value: lo);
3207 }
3208 return false;
3209 };
3210
3211 return parseMany(parseOne: parseOp);
3212}
3213
3214bool AsmParser::parseRealValue(const fltSemantics &Semantics, APInt &Res) {
3215 // We don't truly support arithmetic on floating point expressions, so we
3216 // have to manually parse unary prefixes.
3217 bool IsNeg = false;
3218 if (getLexer().is(K: AsmToken::Minus)) {
3219 Lexer.Lex();
3220 IsNeg = true;
3221 } else if (getLexer().is(K: AsmToken::Plus))
3222 Lexer.Lex();
3223
3224 if (Lexer.is(K: AsmToken::Error))
3225 return TokError(Msg: Lexer.getErr());
3226 if (Lexer.isNot(K: AsmToken::Integer) && Lexer.isNot(K: AsmToken::Real) &&
3227 Lexer.isNot(K: AsmToken::Identifier))
3228 return TokError(Msg: "unexpected token in directive");
3229
3230 // Convert to an APFloat.
3231 APFloat Value(Semantics);
3232 StringRef IDVal = getTok().getString();
3233 if (getLexer().is(K: AsmToken::Identifier)) {
3234 if (!IDVal.compare_insensitive(RHS: "infinity") ||
3235 !IDVal.compare_insensitive(RHS: "inf"))
3236 Value = APFloat::getInf(Sem: Semantics);
3237 else if (!IDVal.compare_insensitive(RHS: "nan"))
3238 Value = APFloat::getNaN(Sem: Semantics, Negative: false, payload: ~0);
3239 else
3240 return TokError(Msg: "invalid floating point literal");
3241 } else if (errorToBool(
3242 Err: Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven)
3243 .takeError()))
3244 return TokError(Msg: "invalid floating point literal");
3245 if (IsNeg)
3246 Value.changeSign();
3247
3248 // Consume the numeric token.
3249 Lex();
3250
3251 Res = Value.bitcastToAPInt();
3252
3253 return false;
3254}
3255
3256/// parseDirectiveRealValue
3257/// ::= (.single | .double) [ expression (, expression)* ]
3258bool AsmParser::parseDirectiveRealValue(StringRef IDVal,
3259 const fltSemantics &Semantics) {
3260 auto parseOp = [&]() -> bool {
3261 APInt AsInt;
3262 if (checkForValidSection() || parseRealValue(Semantics, Res&: AsInt))
3263 return true;
3264 getStreamer().emitIntValue(Value: AsInt.getLimitedValue(),
3265 Size: AsInt.getBitWidth() / 8);
3266 return false;
3267 };
3268
3269 return parseMany(parseOne: parseOp);
3270}
3271
3272/// parseDirectiveZero
3273/// ::= .zero expression
3274bool AsmParser::parseDirectiveZero() {
3275 SMLoc NumBytesLoc = Lexer.getLoc();
3276 const MCExpr *NumBytes;
3277 if (checkForValidSection() || parseExpression(Res&: NumBytes))
3278 return true;
3279
3280 int64_t Val = 0;
3281 if (getLexer().is(K: AsmToken::Comma)) {
3282 Lex();
3283 if (parseAbsoluteExpression(Res&: Val))
3284 return true;
3285 }
3286
3287 if (parseEOL())
3288 return true;
3289 getStreamer().emitFill(NumBytes: *NumBytes, FillValue: Val, Loc: NumBytesLoc);
3290
3291 return false;
3292}
3293
3294/// parseDirectiveFill
3295/// ::= .fill expression [ , expression [ , expression ] ]
3296bool AsmParser::parseDirectiveFill() {
3297 SMLoc NumValuesLoc = Lexer.getLoc();
3298 const MCExpr *NumValues;
3299 if (checkForValidSection() || parseExpression(Res&: NumValues))
3300 return true;
3301
3302 int64_t FillSize = 1;
3303 int64_t FillExpr = 0;
3304
3305 SMLoc SizeLoc, ExprLoc;
3306
3307 if (parseOptionalToken(T: AsmToken::Comma)) {
3308 SizeLoc = getTok().getLoc();
3309 if (parseAbsoluteExpression(Res&: FillSize))
3310 return true;
3311 if (parseOptionalToken(T: AsmToken::Comma)) {
3312 ExprLoc = getTok().getLoc();
3313 if (parseAbsoluteExpression(Res&: FillExpr))
3314 return true;
3315 }
3316 }
3317 if (parseEOL())
3318 return true;
3319
3320 if (FillSize < 0) {
3321 Warning(L: SizeLoc, Msg: "'.fill' directive with negative size has no effect");
3322 return false;
3323 }
3324 if (FillSize > 8) {
3325 Warning(L: SizeLoc, Msg: "'.fill' directive with size greater than 8 has been truncated to 8");
3326 FillSize = 8;
3327 }
3328
3329 if (!isUInt<32>(x: FillExpr) && FillSize > 4)
3330 Warning(L: ExprLoc, Msg: "'.fill' directive pattern has been truncated to 32-bits");
3331
3332 getStreamer().emitFill(NumValues: *NumValues, Size: FillSize, Expr: FillExpr, Loc: NumValuesLoc);
3333
3334 return false;
3335}
3336
3337/// parseDirectiveOrg
3338/// ::= .org expression [ , expression ]
3339bool AsmParser::parseDirectiveOrg() {
3340 const MCExpr *Offset;
3341 SMLoc OffsetLoc = Lexer.getLoc();
3342 if (checkForValidSection() || parseExpression(Res&: Offset))
3343 return true;
3344
3345 // Parse optional fill expression.
3346 int64_t FillExpr = 0;
3347 if (parseOptionalToken(T: AsmToken::Comma))
3348 if (parseAbsoluteExpression(Res&: FillExpr))
3349 return true;
3350 if (parseEOL())
3351 return true;
3352
3353 getStreamer().emitValueToOffset(Offset, Value: FillExpr, Loc: OffsetLoc);
3354 return false;
3355}
3356
3357/// parseDirectiveAlign
3358/// ::= {.align, ...} expression [ , expression [ , expression ]]
3359bool AsmParser::parseDirectiveAlign(bool IsPow2, uint8_t ValueSize) {
3360 SMLoc AlignmentLoc = getLexer().getLoc();
3361 int64_t Alignment;
3362 SMLoc MaxBytesLoc;
3363 bool HasFillExpr = false;
3364 int64_t FillExpr = 0;
3365 int64_t MaxBytesToFill = 0;
3366 SMLoc FillExprLoc;
3367
3368 auto parseAlign = [&]() -> bool {
3369 if (parseAbsoluteExpression(Res&: Alignment))
3370 return true;
3371 if (parseOptionalToken(T: AsmToken::Comma)) {
3372 // The fill expression can be omitted while specifying a maximum number of
3373 // alignment bytes, e.g:
3374 // .align 3,,4
3375 if (getTok().isNot(K: AsmToken::Comma)) {
3376 HasFillExpr = true;
3377 if (parseTokenLoc(Loc&: FillExprLoc) || parseAbsoluteExpression(Res&: FillExpr))
3378 return true;
3379 }
3380 if (parseOptionalToken(T: AsmToken::Comma))
3381 if (parseTokenLoc(Loc&: MaxBytesLoc) ||
3382 parseAbsoluteExpression(Res&: MaxBytesToFill))
3383 return true;
3384 }
3385 return parseEOL();
3386 };
3387
3388 if (checkForValidSection())
3389 return true;
3390 // Ignore empty '.p2align' directives for GNU-as compatibility
3391 if (IsPow2 && (ValueSize == 1) && getTok().is(K: AsmToken::EndOfStatement)) {
3392 Warning(L: AlignmentLoc, Msg: "p2align directive with no operand(s) is ignored");
3393 return parseEOL();
3394 }
3395 if (parseAlign())
3396 return true;
3397
3398 // Always emit an alignment here even if we thrown an error.
3399 bool ReturnVal = false;
3400
3401 // Compute alignment in bytes.
3402 if (IsPow2) {
3403 // FIXME: Diagnose overflow.
3404 if (Alignment >= 32) {
3405 ReturnVal |= Error(L: AlignmentLoc, Msg: "invalid alignment value");
3406 Alignment = 31;
3407 }
3408
3409 Alignment = 1ULL << Alignment;
3410 } else {
3411 // Reject alignments that aren't either a power of two or zero,
3412 // for gas compatibility. Alignment of zero is silently rounded
3413 // up to one.
3414 if (Alignment == 0)
3415 Alignment = 1;
3416 else if (!isPowerOf2_64(Value: Alignment)) {
3417 ReturnVal |= Error(L: AlignmentLoc, Msg: "alignment must be a power of 2");
3418 Alignment = llvm::bit_floor<uint64_t>(Value: Alignment);
3419 }
3420 if (!isUInt<32>(x: Alignment)) {
3421 ReturnVal |= Error(L: AlignmentLoc, Msg: "alignment must be smaller than 2**32");
3422 Alignment = 1u << 31;
3423 }
3424 }
3425
3426 // Diagnose non-sensical max bytes to align.
3427 if (MaxBytesLoc.isValid()) {
3428 if (MaxBytesToFill < 1) {
3429 ReturnVal |= Error(L: MaxBytesLoc,
3430 Msg: "alignment directive can never be satisfied in this "
3431 "many bytes, ignoring maximum bytes expression");
3432 MaxBytesToFill = 0;
3433 }
3434
3435 if (MaxBytesToFill >= Alignment) {
3436 Warning(L: MaxBytesLoc, Msg: "maximum bytes expression exceeds alignment and "
3437 "has no effect");
3438 MaxBytesToFill = 0;
3439 }
3440 }
3441
3442 const MCSection *Section = getStreamer().getCurrentSectionOnly();
3443 assert(Section && "must have section to emit alignment");
3444
3445 if (HasFillExpr && FillExpr != 0 && Section->isBssSection()) {
3446 ReturnVal |=
3447 Warning(L: FillExprLoc, Msg: "ignoring non-zero fill value in BSS section '" +
3448 Section->getName() + "'");
3449 FillExpr = 0;
3450 }
3451
3452 // Check whether we should use optimal code alignment for this .align
3453 // directive.
3454 if (MAI.useCodeAlign(Sec: *Section) && !HasFillExpr) {
3455 getStreamer().emitCodeAlignment(
3456 Alignment: Align(Alignment), STI: &getTargetParser().getSTI(), MaxBytesToEmit: MaxBytesToFill);
3457 } else {
3458 // FIXME: Target specific behavior about how the "extra" bytes are filled.
3459 getStreamer().emitValueToAlignment(Alignment: Align(Alignment), Fill: FillExpr, FillLen: ValueSize,
3460 MaxBytesToEmit: MaxBytesToFill);
3461 }
3462
3463 return ReturnVal;
3464}
3465
3466bool AsmParser::parseDirectivePrefAlign() {
3467 SMLoc AlignmentLoc = getLexer().getLoc();
3468 int64_t Alignment;
3469 if (checkForValidSection() || parseAbsoluteExpression(Res&: Alignment))
3470 return true;
3471 if (parseEOL())
3472 return true;
3473
3474 if (!isPowerOf2_64(Value: Alignment))
3475 return Error(L: AlignmentLoc, Msg: "alignment must be a power of 2");
3476 getStreamer().emitPrefAlign(A: Align(Alignment));
3477
3478 return false;
3479}
3480
3481/// parseDirectiveFile
3482/// ::= .file filename
3483/// ::= .file number [directory] filename [md5 checksum] [source source-text]
3484bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
3485 // FIXME: I'm not sure what this is.
3486 int64_t FileNumber = -1;
3487 if (getLexer().is(K: AsmToken::Integer)) {
3488 FileNumber = getTok().getIntVal();
3489 Lex();
3490
3491 if (FileNumber < 0)
3492 return TokError(Msg: "negative file number");
3493 }
3494
3495 std::string Path;
3496
3497 // Usually the directory and filename together, otherwise just the directory.
3498 // Allow the strings to have escaped octal character sequence.
3499 if (parseEscapedString(Data&: Path))
3500 return true;
3501
3502 StringRef Directory;
3503 StringRef Filename;
3504 std::string FilenameData;
3505 if (getLexer().is(K: AsmToken::String)) {
3506 if (check(P: FileNumber == -1,
3507 Msg: "explicit path specified, but no file number") ||
3508 parseEscapedString(Data&: FilenameData))
3509 return true;
3510 Filename = FilenameData;
3511 Directory = Path;
3512 } else {
3513 Filename = Path;
3514 }
3515
3516 uint64_t MD5Hi, MD5Lo;
3517 bool HasMD5 = false;
3518
3519 std::optional<StringRef> Source;
3520 bool HasSource = false;
3521 std::string SourceString;
3522
3523 while (!parseOptionalToken(T: AsmToken::EndOfStatement)) {
3524 StringRef Keyword;
3525 if (check(P: getTok().isNot(K: AsmToken::Identifier),
3526 Msg: "unexpected token in '.file' directive") ||
3527 parseIdentifier(Res&: Keyword))
3528 return true;
3529 if (Keyword == "md5") {
3530 HasMD5 = true;
3531 if (check(P: FileNumber == -1,
3532 Msg: "MD5 checksum specified, but no file number") ||
3533 parseHexOcta(Asm&: *this, hi&: MD5Hi, lo&: MD5Lo))
3534 return true;
3535 } else if (Keyword == "source") {
3536 HasSource = true;
3537 if (check(P: FileNumber == -1,
3538 Msg: "source specified, but no file number") ||
3539 check(P: getTok().isNot(K: AsmToken::String),
3540 Msg: "unexpected token in '.file' directive") ||
3541 parseEscapedString(Data&: SourceString))
3542 return true;
3543 } else {
3544 return TokError(Msg: "unexpected token in '.file' directive");
3545 }
3546 }
3547
3548 if (FileNumber == -1) {
3549 // Ignore the directive if there is no number and the target doesn't support
3550 // numberless .file directives. This allows some portability of assembler
3551 // between different object file formats.
3552 if (getContext().getAsmInfo()->hasSingleParameterDotFile())
3553 getStreamer().emitFileDirective(Filename);
3554 } else {
3555 // In case there is a -g option as well as debug info from directive .file,
3556 // we turn off the -g option, directly use the existing debug info instead.
3557 // Throw away any implicit file table for the assembler source.
3558 if (Ctx.getGenDwarfForAssembly()) {
3559 Ctx.getMCDwarfLineTable(CUID: 0).resetFileTable();
3560 Ctx.setGenDwarfForAssembly(false);
3561 }
3562
3563 std::optional<MD5::MD5Result> CKMem;
3564 if (HasMD5) {
3565 MD5::MD5Result Sum;
3566 for (unsigned i = 0; i != 8; ++i) {
3567 Sum[i] = uint8_t(MD5Hi >> ((7 - i) * 8));
3568 Sum[i + 8] = uint8_t(MD5Lo >> ((7 - i) * 8));
3569 }
3570 CKMem = Sum;
3571 }
3572 if (HasSource) {
3573 char *SourceBuf = static_cast<char *>(Ctx.allocate(Size: SourceString.size()));
3574 memcpy(dest: SourceBuf, src: SourceString.data(), n: SourceString.size());
3575 Source = StringRef(SourceBuf, SourceString.size());
3576 }
3577 if (FileNumber == 0) {
3578 // Upgrade to Version 5 for assembly actions like clang -c a.s.
3579 if (Ctx.getDwarfVersion() < 5)
3580 Ctx.setDwarfVersion(5);
3581 getStreamer().emitDwarfFile0Directive(Directory, Filename, Checksum: CKMem, Source);
3582 } else {
3583 Expected<unsigned> FileNumOrErr = getStreamer().tryEmitDwarfFileDirective(
3584 FileNo: FileNumber, Directory, Filename, Checksum: CKMem, Source);
3585 if (!FileNumOrErr)
3586 return Error(L: DirectiveLoc, Msg: toString(E: FileNumOrErr.takeError()));
3587 }
3588 // Alert the user if there are some .file directives with MD5 and some not.
3589 // But only do that once.
3590 if (!ReportedInconsistentMD5 && !Ctx.isDwarfMD5UsageConsistent(CUID: 0)) {
3591 ReportedInconsistentMD5 = true;
3592 return Warning(L: DirectiveLoc, Msg: "inconsistent use of MD5 checksums");
3593 }
3594 }
3595
3596 return false;
3597}
3598
3599/// parseDirectiveLine
3600/// ::= .line [number]
3601bool AsmParser::parseDirectiveLine() {
3602 parseOptionalToken(T: AsmToken::Integer);
3603 return parseEOL();
3604}
3605
3606/// parseDirectiveLoc
3607/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
3608/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
3609/// The first number is a file number, must have been previously assigned with
3610/// a .file directive, the second number is the line number and optionally the
3611/// third number is a column position (zero if not specified). The remaining
3612/// optional items are .loc sub-directives.
3613bool AsmParser::parseDirectiveLoc() {
3614 int64_t FileNumber = 0, LineNumber = 0;
3615 SMLoc Loc = getTok().getLoc();
3616 if (parseIntToken(V&: FileNumber) ||
3617 check(P: FileNumber < 1 && Ctx.getDwarfVersion() < 5, Loc,
3618 Msg: "file number less than one in '.loc' directive") ||
3619 check(P: !getContext().isValidDwarfFileNumber(FileNumber), Loc,
3620 Msg: "unassigned file number in '.loc' directive"))
3621 return true;
3622
3623 // optional
3624 if (getLexer().is(K: AsmToken::Integer)) {
3625 LineNumber = getTok().getIntVal();
3626 if (LineNumber < 0)
3627 return TokError(Msg: "line number less than zero in '.loc' directive");
3628 Lex();
3629 }
3630
3631 int64_t ColumnPos = 0;
3632 if (getLexer().is(K: AsmToken::Integer)) {
3633 ColumnPos = getTok().getIntVal();
3634 if (ColumnPos < 0)
3635 return TokError(Msg: "column position less than zero in '.loc' directive");
3636 Lex();
3637 }
3638
3639 auto PrevFlags = getContext().getCurrentDwarfLoc().getFlags();
3640 unsigned Flags = PrevFlags & DWARF2_FLAG_IS_STMT;
3641 unsigned Isa = 0;
3642 int64_t Discriminator = 0;
3643
3644 auto parseLocOp = [&]() -> bool {
3645 StringRef Name;
3646 SMLoc Loc = getTok().getLoc();
3647 if (parseIdentifier(Res&: Name))
3648 return TokError(Msg: "unexpected token in '.loc' directive");
3649
3650 if (Name == "basic_block")
3651 Flags |= DWARF2_FLAG_BASIC_BLOCK;
3652 else if (Name == "prologue_end")
3653 Flags |= DWARF2_FLAG_PROLOGUE_END;
3654 else if (Name == "epilogue_begin")
3655 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
3656 else if (Name == "is_stmt") {
3657 Loc = getTok().getLoc();
3658 const MCExpr *Value;
3659 if (parseExpression(Res&: Value))
3660 return true;
3661 // The expression must be the constant 0 or 1.
3662 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Val: Value)) {
3663 int Value = MCE->getValue();
3664 if (Value == 0)
3665 Flags &= ~DWARF2_FLAG_IS_STMT;
3666 else if (Value == 1)
3667 Flags |= DWARF2_FLAG_IS_STMT;
3668 else
3669 return Error(L: Loc, Msg: "is_stmt value not 0 or 1");
3670 } else {
3671 return Error(L: Loc, Msg: "is_stmt value not the constant value of 0 or 1");
3672 }
3673 } else if (Name == "isa") {
3674 Loc = getTok().getLoc();
3675 const MCExpr *Value;
3676 if (parseExpression(Res&: Value))
3677 return true;
3678 // The expression must be a constant greater or equal to 0.
3679 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Val: Value)) {
3680 int Value = MCE->getValue();
3681 if (Value < 0)
3682 return Error(L: Loc, Msg: "isa number less than zero");
3683 Isa = Value;
3684 } else {
3685 return Error(L: Loc, Msg: "isa number not a constant value");
3686 }
3687 } else if (Name == "discriminator") {
3688 if (parseAbsoluteExpression(Res&: Discriminator))
3689 return true;
3690 } else {
3691 return Error(L: Loc, Msg: "unknown sub-directive in '.loc' directive");
3692 }
3693 return false;
3694 };
3695
3696 if (parseMany(parseOne: parseLocOp, hasComma: false /*hasComma*/))
3697 return true;
3698
3699 getStreamer().emitDwarfLocDirective(FileNo: FileNumber, Line: LineNumber, Column: ColumnPos, Flags,
3700 Isa, Discriminator, FileName: StringRef());
3701
3702 return false;
3703}
3704
3705/// parseDirectiveLoc
3706/// ::= .loc_label label
3707bool AsmParser::parseDirectiveLocLabel(SMLoc DirectiveLoc) {
3708 StringRef Name;
3709 DirectiveLoc = Lexer.getLoc();
3710 if (parseIdentifier(Res&: Name))
3711 return TokError(Msg: "expected identifier");
3712 if (parseEOL())
3713 return true;
3714 getStreamer().emitDwarfLocLabelDirective(Loc: DirectiveLoc, Name);
3715 return false;
3716}
3717
3718/// parseDirectiveStabs
3719/// ::= .stabs string, number, number, number
3720bool AsmParser::parseDirectiveStabs() {
3721 return TokError(Msg: "unsupported directive '.stabs'");
3722}
3723
3724/// parseDirectiveCVFile
3725/// ::= .cv_file number filename [checksum] [checksumkind]
3726bool AsmParser::parseDirectiveCVFile() {
3727 SMLoc FileNumberLoc = getTok().getLoc();
3728 int64_t FileNumber;
3729 std::string Filename;
3730 std::string Checksum;
3731 int64_t ChecksumKind = 0;
3732
3733 if (parseIntToken(V&: FileNumber, ErrMsg: "expected file number") ||
3734 check(P: FileNumber < 1, Loc: FileNumberLoc, Msg: "file number less than one") ||
3735 check(P: getTok().isNot(K: AsmToken::String),
3736 Msg: "unexpected token in '.cv_file' directive") ||
3737 parseEscapedString(Data&: Filename))
3738 return true;
3739 if (!parseOptionalToken(T: AsmToken::EndOfStatement)) {
3740 if (check(P: getTok().isNot(K: AsmToken::String),
3741 Msg: "unexpected token in '.cv_file' directive") ||
3742 parseEscapedString(Data&: Checksum) ||
3743 parseIntToken(V&: ChecksumKind,
3744 ErrMsg: "expected checksum kind in '.cv_file' directive") ||
3745 parseEOL())
3746 return true;
3747 }
3748
3749 Checksum = fromHex(Input: Checksum);
3750 void *CKMem = Ctx.allocate(Size: Checksum.size(), Align: 1);
3751 memcpy(dest: CKMem, src: Checksum.data(), n: Checksum.size());
3752 ArrayRef<uint8_t> ChecksumAsBytes(reinterpret_cast<const uint8_t *>(CKMem),
3753 Checksum.size());
3754
3755 if (!getStreamer().emitCVFileDirective(FileNo: FileNumber, Filename, Checksum: ChecksumAsBytes,
3756 ChecksumKind: static_cast<uint8_t>(ChecksumKind)))
3757 return Error(L: FileNumberLoc, Msg: "file number already allocated");
3758
3759 return false;
3760}
3761
3762bool AsmParser::parseCVFunctionId(int64_t &FunctionId,
3763 StringRef DirectiveName) {
3764 SMLoc Loc;
3765 return parseTokenLoc(Loc) ||
3766 parseIntToken(V&: FunctionId, ErrMsg: "expected function id") ||
3767 check(P: FunctionId < 0 || FunctionId >= UINT_MAX, Loc,
3768 Msg: "expected function id within range [0, UINT_MAX)");
3769}
3770
3771bool AsmParser::parseCVFileId(int64_t &FileNumber, StringRef DirectiveName) {
3772 SMLoc Loc;
3773 return parseTokenLoc(Loc) ||
3774 parseIntToken(V&: FileNumber, ErrMsg: "expected file number") ||
3775 check(P: FileNumber < 1, Loc,
3776 Msg: "file number less than one in '" + DirectiveName +
3777 "' directive") ||
3778 check(P: !getCVContext().isValidFileNumber(FileNumber), Loc,
3779 Msg: "unassigned file number in '" + DirectiveName + "' directive");
3780}
3781
3782/// parseDirectiveCVFuncId
3783/// ::= .cv_func_id FunctionId
3784///
3785/// Introduces a function ID that can be used with .cv_loc.
3786bool AsmParser::parseDirectiveCVFuncId() {
3787 SMLoc FunctionIdLoc = getTok().getLoc();
3788 int64_t FunctionId;
3789
3790 if (parseCVFunctionId(FunctionId, DirectiveName: ".cv_func_id") || parseEOL())
3791 return true;
3792
3793 if (!getStreamer().emitCVFuncIdDirective(FunctionId))
3794 return Error(L: FunctionIdLoc, Msg: "function id already allocated");
3795
3796 return false;
3797}
3798
3799/// parseDirectiveCVInlineSiteId
3800/// ::= .cv_inline_site_id FunctionId
3801/// "within" IAFunc
3802/// "inlined_at" IAFile IALine [IACol]
3803///
3804/// Introduces a function ID that can be used with .cv_loc. Includes "inlined
3805/// at" source location information for use in the line table of the caller,
3806/// whether the caller is a real function or another inlined call site.
3807bool AsmParser::parseDirectiveCVInlineSiteId() {
3808 SMLoc FunctionIdLoc = getTok().getLoc();
3809 int64_t FunctionId;
3810 int64_t IAFunc;
3811 int64_t IAFile;
3812 int64_t IALine;
3813 int64_t IACol = 0;
3814
3815 // FunctionId
3816 if (parseCVFunctionId(FunctionId, DirectiveName: ".cv_inline_site_id"))
3817 return true;
3818
3819 // "within"
3820 if (check(P: (getLexer().isNot(K: AsmToken::Identifier) ||
3821 getTok().getIdentifier() != "within"),
3822 Msg: "expected 'within' identifier in '.cv_inline_site_id' directive"))
3823 return true;
3824 Lex();
3825
3826 // IAFunc
3827 if (parseCVFunctionId(FunctionId&: IAFunc, DirectiveName: ".cv_inline_site_id"))
3828 return true;
3829
3830 // "inlined_at"
3831 if (check(P: (getLexer().isNot(K: AsmToken::Identifier) ||
3832 getTok().getIdentifier() != "inlined_at"),
3833 Msg: "expected 'inlined_at' identifier in '.cv_inline_site_id' "
3834 "directive") )
3835 return true;
3836 Lex();
3837
3838 // IAFile IALine
3839 if (parseCVFileId(FileNumber&: IAFile, DirectiveName: ".cv_inline_site_id") ||
3840 parseIntToken(V&: IALine, ErrMsg: "expected line number after 'inlined_at'"))
3841 return true;
3842
3843 // [IACol]
3844 if (getLexer().is(K: AsmToken::Integer)) {
3845 IACol = getTok().getIntVal();
3846 Lex();
3847 }
3848
3849 if (parseEOL())
3850 return true;
3851
3852 if (!getStreamer().emitCVInlineSiteIdDirective(FunctionId, IAFunc, IAFile,
3853 IALine, IACol, Loc: FunctionIdLoc))
3854 return Error(L: FunctionIdLoc, Msg: "function id already allocated");
3855
3856 return false;
3857}
3858
3859/// parseDirectiveCVLoc
3860/// ::= .cv_loc FunctionId FileNumber [LineNumber] [ColumnPos] [prologue_end]
3861/// [is_stmt VALUE]
3862/// The first number is a file number, must have been previously assigned with
3863/// a .file directive, the second number is the line number and optionally the
3864/// third number is a column position (zero if not specified). The remaining
3865/// optional items are .loc sub-directives.
3866bool AsmParser::parseDirectiveCVLoc() {
3867 SMLoc DirectiveLoc = getTok().getLoc();
3868 int64_t FunctionId, FileNumber;
3869 if (parseCVFunctionId(FunctionId, DirectiveName: ".cv_loc") ||
3870 parseCVFileId(FileNumber, DirectiveName: ".cv_loc"))
3871 return true;
3872
3873 int64_t LineNumber = 0;
3874 if (getLexer().is(K: AsmToken::Integer)) {
3875 LineNumber = getTok().getIntVal();
3876 if (LineNumber < 0)
3877 return TokError(Msg: "line number less than zero in '.cv_loc' directive");
3878 Lex();
3879 }
3880
3881 int64_t ColumnPos = 0;
3882 if (getLexer().is(K: AsmToken::Integer)) {
3883 ColumnPos = getTok().getIntVal();
3884 if (ColumnPos < 0)
3885 return TokError(Msg: "column position less than zero in '.cv_loc' directive");
3886 Lex();
3887 }
3888
3889 bool PrologueEnd = false;
3890 uint64_t IsStmt = 0;
3891
3892 auto parseOp = [&]() -> bool {
3893 StringRef Name;
3894 SMLoc Loc = getTok().getLoc();
3895 if (parseIdentifier(Res&: Name))
3896 return TokError(Msg: "unexpected token in '.cv_loc' directive");
3897 if (Name == "prologue_end")
3898 PrologueEnd = true;
3899 else if (Name == "is_stmt") {
3900 Loc = getTok().getLoc();
3901 const MCExpr *Value;
3902 if (parseExpression(Res&: Value))
3903 return true;
3904 // The expression must be the constant 0 or 1.
3905 IsStmt = ~0ULL;
3906 if (const auto *MCE = dyn_cast<MCConstantExpr>(Val: Value))
3907 IsStmt = MCE->getValue();
3908
3909 if (IsStmt > 1)
3910 return Error(L: Loc, Msg: "is_stmt value not 0 or 1");
3911 } else {
3912 return Error(L: Loc, Msg: "unknown sub-directive in '.cv_loc' directive");
3913 }
3914 return false;
3915 };
3916
3917 if (parseMany(parseOne: parseOp, hasComma: false /*hasComma*/))
3918 return true;
3919
3920 getStreamer().emitCVLocDirective(FunctionId, FileNo: FileNumber, Line: LineNumber,
3921 Column: ColumnPos, PrologueEnd, IsStmt, FileName: StringRef(),
3922 Loc: DirectiveLoc);
3923 return false;
3924}
3925
3926/// parseDirectiveCVLinetable
3927/// ::= .cv_linetable FunctionId, FnStart, FnEnd
3928bool AsmParser::parseDirectiveCVLinetable() {
3929 int64_t FunctionId;
3930 MCSymbol *FnStartSym, *FnEndSym;
3931 SMLoc Loc = getTok().getLoc();
3932 if (parseCVFunctionId(FunctionId, DirectiveName: ".cv_linetable") || parseComma() ||
3933 parseTokenLoc(Loc) ||
3934 check(P: parseSymbol(Res&: FnStartSym), Loc, Msg: "expected identifier in directive") ||
3935 parseComma() || parseTokenLoc(Loc) ||
3936 check(P: parseSymbol(Res&: FnEndSym), Loc, Msg: "expected identifier in directive"))
3937 return true;
3938
3939 getStreamer().emitCVLinetableDirective(FunctionId, FnStart: FnStartSym, FnEnd: FnEndSym);
3940 return false;
3941}
3942
3943/// parseDirectiveCVInlineLinetable
3944/// ::= .cv_inline_linetable PrimaryFunctionId FileId LineNum FnStart FnEnd
3945bool AsmParser::parseDirectiveCVInlineLinetable() {
3946 int64_t PrimaryFunctionId, SourceFileId, SourceLineNum;
3947 MCSymbol *FnStartSym, *FnEndSym;
3948 SMLoc Loc = getTok().getLoc();
3949 if (parseCVFunctionId(FunctionId&: PrimaryFunctionId, DirectiveName: ".cv_inline_linetable") ||
3950 parseTokenLoc(Loc) ||
3951 parseIntToken(V&: SourceFileId, ErrMsg: "expected SourceField") ||
3952 check(P: SourceFileId <= 0, Loc, Msg: "File id less than zero") ||
3953 parseTokenLoc(Loc) ||
3954 parseIntToken(V&: SourceLineNum, ErrMsg: "expected SourceLineNum") ||
3955 check(P: SourceLineNum < 0, Loc, Msg: "Line number less than zero") ||
3956 parseTokenLoc(Loc) ||
3957 check(P: parseSymbol(Res&: FnStartSym), Loc, Msg: "expected identifier") ||
3958 parseTokenLoc(Loc) ||
3959 check(P: parseSymbol(Res&: FnEndSym), Loc, Msg: "expected identifier"))
3960 return true;
3961
3962 if (parseEOL())
3963 return true;
3964
3965 getStreamer().emitCVInlineLinetableDirective(PrimaryFunctionId, SourceFileId,
3966 SourceLineNum, FnStartSym,
3967 FnEndSym);
3968 return false;
3969}
3970
3971void AsmParser::initializeCVDefRangeTypeMap() {
3972 CVDefRangeTypeMap["reg"] = CVDR_DEFRANGE_REGISTER;
3973 CVDefRangeTypeMap["frame_ptr_rel"] = CVDR_DEFRANGE_FRAMEPOINTER_REL;
3974 CVDefRangeTypeMap["subfield_reg"] = CVDR_DEFRANGE_SUBFIELD_REGISTER;
3975 CVDefRangeTypeMap["reg_rel"] = CVDR_DEFRANGE_REGISTER_REL;
3976}
3977
3978/// parseDirectiveCVDefRange
3979/// ::= .cv_def_range RangeStart RangeEnd (GapStart GapEnd)*, bytes*
3980bool AsmParser::parseDirectiveCVDefRange() {
3981 SMLoc Loc;
3982 std::vector<std::pair<const MCSymbol *, const MCSymbol *>> Ranges;
3983 while (getLexer().is(K: AsmToken::Identifier)) {
3984 Loc = getLexer().getLoc();
3985 MCSymbol *GapStartSym;
3986 if (parseSymbol(Res&: GapStartSym))
3987 return Error(L: Loc, Msg: "expected identifier in directive");
3988
3989 Loc = getLexer().getLoc();
3990 MCSymbol *GapEndSym;
3991 if (parseSymbol(Res&: GapEndSym))
3992 return Error(L: Loc, Msg: "expected identifier in directive");
3993
3994 Ranges.push_back(x: {GapStartSym, GapEndSym});
3995 }
3996
3997 StringRef CVDefRangeTypeStr;
3998 if (parseToken(
3999 T: AsmToken::Comma,
4000 Msg: "expected comma before def_range type in .cv_def_range directive") ||
4001 parseIdentifier(Res&: CVDefRangeTypeStr))
4002 return Error(L: Loc, Msg: "expected def_range type in directive");
4003
4004 StringMap<CVDefRangeType>::const_iterator CVTypeIt =
4005 CVDefRangeTypeMap.find(Key: CVDefRangeTypeStr);
4006 CVDefRangeType CVDRType = (CVTypeIt == CVDefRangeTypeMap.end())
4007 ? CVDR_DEFRANGE
4008 : CVTypeIt->getValue();
4009 switch (CVDRType) {
4010 case CVDR_DEFRANGE_REGISTER: {
4011 int64_t DRRegister;
4012 if (parseToken(T: AsmToken::Comma, Msg: "expected comma before register number in "
4013 ".cv_def_range directive") ||
4014 parseAbsoluteExpression(Res&: DRRegister))
4015 return Error(L: Loc, Msg: "expected register number");
4016
4017 codeview::DefRangeRegisterHeader DRHdr;
4018 DRHdr.Register = DRRegister;
4019 DRHdr.MayHaveNoName = 0;
4020 getStreamer().emitCVDefRangeDirective(Ranges, DRHdr);
4021 break;
4022 }
4023 case CVDR_DEFRANGE_FRAMEPOINTER_REL: {
4024 int64_t DROffset;
4025 if (parseToken(T: AsmToken::Comma,
4026 Msg: "expected comma before offset in .cv_def_range directive") ||
4027 parseAbsoluteExpression(Res&: DROffset))
4028 return Error(L: Loc, Msg: "expected offset value");
4029
4030 codeview::DefRangeFramePointerRelHeader DRHdr;
4031 DRHdr.Offset = DROffset;
4032 getStreamer().emitCVDefRangeDirective(Ranges, DRHdr);
4033 break;
4034 }
4035 case CVDR_DEFRANGE_SUBFIELD_REGISTER: {
4036 int64_t DRRegister;
4037 int64_t DROffsetInParent;
4038 if (parseToken(T: AsmToken::Comma, Msg: "expected comma before register number in "
4039 ".cv_def_range directive") ||
4040 parseAbsoluteExpression(Res&: DRRegister))
4041 return Error(L: Loc, Msg: "expected register number");
4042 if (parseToken(T: AsmToken::Comma,
4043 Msg: "expected comma before offset in .cv_def_range directive") ||
4044 parseAbsoluteExpression(Res&: DROffsetInParent))
4045 return Error(L: Loc, Msg: "expected offset value");
4046
4047 codeview::DefRangeSubfieldRegisterHeader DRHdr;
4048 DRHdr.Register = DRRegister;
4049 DRHdr.MayHaveNoName = 0;
4050 DRHdr.OffsetInParent = DROffsetInParent;
4051 getStreamer().emitCVDefRangeDirective(Ranges, DRHdr);
4052 break;
4053 }
4054 case CVDR_DEFRANGE_REGISTER_REL: {
4055 int64_t DRRegister;
4056 int64_t DRFlags;
4057 int64_t DRBasePointerOffset;
4058 if (parseToken(T: AsmToken::Comma, Msg: "expected comma before register number in "
4059 ".cv_def_range directive") ||
4060 parseAbsoluteExpression(Res&: DRRegister))
4061 return Error(L: Loc, Msg: "expected register value");
4062 if (parseToken(
4063 T: AsmToken::Comma,
4064 Msg: "expected comma before flag value in .cv_def_range directive") ||
4065 parseAbsoluteExpression(Res&: DRFlags))
4066 return Error(L: Loc, Msg: "expected flag value");
4067 if (parseToken(T: AsmToken::Comma, Msg: "expected comma before base pointer offset "
4068 "in .cv_def_range directive") ||
4069 parseAbsoluteExpression(Res&: DRBasePointerOffset))
4070 return Error(L: Loc, Msg: "expected base pointer offset value");
4071
4072 codeview::DefRangeRegisterRelHeader DRHdr;
4073 DRHdr.Register = DRRegister;
4074 DRHdr.Flags = DRFlags;
4075 DRHdr.BasePointerOffset = DRBasePointerOffset;
4076 getStreamer().emitCVDefRangeDirective(Ranges, DRHdr);
4077 break;
4078 }
4079 default:
4080 return Error(L: Loc, Msg: "unexpected def_range type in .cv_def_range directive");
4081 }
4082 return true;
4083}
4084
4085/// parseDirectiveCVString
4086/// ::= .cv_stringtable "string"
4087bool AsmParser::parseDirectiveCVString() {
4088 std::string Data;
4089 if (checkForValidSection() || parseEscapedString(Data))
4090 return true;
4091
4092 // Put the string in the table and emit the offset.
4093 std::pair<StringRef, unsigned> Insertion =
4094 getCVContext().addToStringTable(S: Data);
4095 getStreamer().emitInt32(Value: Insertion.second);
4096 return false;
4097}
4098
4099/// parseDirectiveCVStringTable
4100/// ::= .cv_stringtable
4101bool AsmParser::parseDirectiveCVStringTable() {
4102 getStreamer().emitCVStringTableDirective();
4103 return false;
4104}
4105
4106/// parseDirectiveCVFileChecksums
4107/// ::= .cv_filechecksums
4108bool AsmParser::parseDirectiveCVFileChecksums() {
4109 getStreamer().emitCVFileChecksumsDirective();
4110 return false;
4111}
4112
4113/// parseDirectiveCVFileChecksumOffset
4114/// ::= .cv_filechecksumoffset fileno
4115bool AsmParser::parseDirectiveCVFileChecksumOffset() {
4116 int64_t FileNo;
4117 if (parseIntToken(V&: FileNo))
4118 return true;
4119 if (parseEOL())
4120 return true;
4121 getStreamer().emitCVFileChecksumOffsetDirective(FileNo);
4122 return false;
4123}
4124
4125/// parseDirectiveCVFPOData
4126/// ::= .cv_fpo_data procsym
4127bool AsmParser::parseDirectiveCVFPOData() {
4128 SMLoc DirLoc = getLexer().getLoc();
4129 MCSymbol *ProcSym;
4130 if (parseSymbol(Res&: ProcSym))
4131 return TokError(Msg: "expected symbol name");
4132 if (parseEOL())
4133 return true;
4134 getStreamer().emitCVFPOData(ProcSym, Loc: DirLoc);
4135 return false;
4136}
4137
4138/// parseDirectiveCFISections
4139/// ::= .cfi_sections section [, section][, section]
4140bool AsmParser::parseDirectiveCFISections() {
4141 StringRef Name;
4142 bool EH = false;
4143 bool Debug = false;
4144 bool SFrame = false;
4145
4146 if (!parseOptionalToken(T: AsmToken::EndOfStatement)) {
4147 for (;;) {
4148 if (parseIdentifier(Res&: Name))
4149 return TokError(Msg: "expected .eh_frame, .debug_frame, or .sframe");
4150 if (Name == ".eh_frame")
4151 EH = true;
4152 else if (Name == ".debug_frame")
4153 Debug = true;
4154 else if (Name == ".sframe")
4155 SFrame = true;
4156 if (parseOptionalToken(T: AsmToken::EndOfStatement))
4157 break;
4158 if (parseComma())
4159 return true;
4160 }
4161 }
4162 getStreamer().emitCFISections(EH, Debug, SFrame);
4163 return false;
4164}
4165
4166/// parseDirectiveCFIStartProc
4167/// ::= .cfi_startproc [simple]
4168bool AsmParser::parseDirectiveCFIStartProc() {
4169 CFIStartProcLoc = StartTokLoc;
4170
4171 StringRef Simple;
4172 if (!parseOptionalToken(T: AsmToken::EndOfStatement)) {
4173 if (check(P: parseIdentifier(Res&: Simple) || Simple != "simple",
4174 Msg: "unexpected token") ||
4175 parseEOL())
4176 return true;
4177 }
4178
4179 // TODO(kristina): Deal with a corner case of incorrect diagnostic context
4180 // being produced if this directive is emitted as part of preprocessor macro
4181 // expansion which can *ONLY* happen if Clang's cc1as is the API consumer.
4182 // Tools like llvm-mc on the other hand are not affected by it, and report
4183 // correct context information.
4184 getStreamer().emitCFIStartProc(IsSimple: !Simple.empty(), Loc: Lexer.getLoc());
4185 return false;
4186}
4187
4188/// parseDirectiveCFIEndProc
4189/// ::= .cfi_endproc
4190bool AsmParser::parseDirectiveCFIEndProc() {
4191 CFIStartProcLoc = std::nullopt;
4192
4193 if (parseEOL())
4194 return true;
4195
4196 getStreamer().emitCFIEndProc();
4197 return false;
4198}
4199
4200/// parse register name or number.
4201bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
4202 SMLoc DirectiveLoc) {
4203 MCRegister RegNo;
4204
4205 if (getLexer().isNot(K: AsmToken::Integer)) {
4206 if (getTargetParser().parseRegister(Reg&: RegNo, StartLoc&: DirectiveLoc, EndLoc&: DirectiveLoc))
4207 return true;
4208 Register = getContext().getRegisterInfo()->getDwarfRegNum(Reg: RegNo, isEH: true);
4209 } else
4210 return parseAbsoluteExpression(Res&: Register);
4211
4212 return false;
4213}
4214
4215/// parseDirectiveCFIDefCfa
4216/// ::= .cfi_def_cfa register, offset
4217bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
4218 int64_t Register = 0, Offset = 0;
4219 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseComma() ||
4220 parseAbsoluteExpression(Res&: Offset) || parseEOL())
4221 return true;
4222
4223 getStreamer().emitCFIDefCfa(Register, Offset, Loc: DirectiveLoc);
4224 return false;
4225}
4226
4227/// parseDirectiveCFIDefCfaOffset
4228/// ::= .cfi_def_cfa_offset offset
4229bool AsmParser::parseDirectiveCFIDefCfaOffset(SMLoc DirectiveLoc) {
4230 int64_t Offset = 0;
4231 if (parseAbsoluteExpression(Res&: Offset) || parseEOL())
4232 return true;
4233
4234 getStreamer().emitCFIDefCfaOffset(Offset, Loc: DirectiveLoc);
4235 return false;
4236}
4237
4238/// parseDirectiveCFIRegister
4239/// ::= .cfi_register register, register
4240bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
4241 int64_t Register1 = 0, Register2 = 0;
4242 if (parseRegisterOrRegisterNumber(Register&: Register1, DirectiveLoc) || parseComma() ||
4243 parseRegisterOrRegisterNumber(Register&: Register2, DirectiveLoc) || parseEOL())
4244 return true;
4245
4246 getStreamer().emitCFIRegister(Register1, Register2, Loc: DirectiveLoc);
4247 return false;
4248}
4249
4250/// parseDirectiveCFIWindowSave
4251/// ::= .cfi_window_save
4252bool AsmParser::parseDirectiveCFIWindowSave(SMLoc DirectiveLoc) {
4253 if (parseEOL())
4254 return true;
4255 getStreamer().emitCFIWindowSave(Loc: DirectiveLoc);
4256 return false;
4257}
4258
4259/// parseDirectiveCFIAdjustCfaOffset
4260/// ::= .cfi_adjust_cfa_offset adjustment
4261bool AsmParser::parseDirectiveCFIAdjustCfaOffset(SMLoc DirectiveLoc) {
4262 int64_t Adjustment = 0;
4263 if (parseAbsoluteExpression(Res&: Adjustment) || parseEOL())
4264 return true;
4265
4266 getStreamer().emitCFIAdjustCfaOffset(Adjustment, Loc: DirectiveLoc);
4267 return false;
4268}
4269
4270/// parseDirectiveCFIDefCfaRegister
4271/// ::= .cfi_def_cfa_register register
4272bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
4273 int64_t Register = 0;
4274 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseEOL())
4275 return true;
4276
4277 getStreamer().emitCFIDefCfaRegister(Register, Loc: DirectiveLoc);
4278 return false;
4279}
4280
4281/// parseDirectiveCFILLVMDefAspaceCfa
4282/// ::= .cfi_llvm_def_aspace_cfa register, offset, address_space
4283bool AsmParser::parseDirectiveCFILLVMDefAspaceCfa(SMLoc DirectiveLoc) {
4284 int64_t Register = 0, Offset = 0, AddressSpace = 0;
4285 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseComma() ||
4286 parseAbsoluteExpression(Res&: Offset) || parseComma() ||
4287 parseAbsoluteExpression(Res&: AddressSpace) || parseEOL())
4288 return true;
4289
4290 getStreamer().emitCFILLVMDefAspaceCfa(Register, Offset, AddressSpace,
4291 Loc: DirectiveLoc);
4292 return false;
4293}
4294
4295/// parseDirectiveCFIOffset
4296/// ::= .cfi_offset register, offset
4297bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
4298 int64_t Register = 0;
4299 int64_t Offset = 0;
4300
4301 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseComma() ||
4302 parseAbsoluteExpression(Res&: Offset) || parseEOL())
4303 return true;
4304
4305 getStreamer().emitCFIOffset(Register, Offset, Loc: DirectiveLoc);
4306 return false;
4307}
4308
4309/// parseDirectiveCFIRelOffset
4310/// ::= .cfi_rel_offset register, offset
4311bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
4312 int64_t Register = 0, Offset = 0;
4313
4314 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseComma() ||
4315 parseAbsoluteExpression(Res&: Offset) || parseEOL())
4316 return true;
4317
4318 getStreamer().emitCFIRelOffset(Register, Offset, Loc: DirectiveLoc);
4319 return false;
4320}
4321
4322static bool isValidEncoding(int64_t Encoding) {
4323 if (Encoding & ~0xff)
4324 return false;
4325
4326 if (Encoding == dwarf::DW_EH_PE_omit)
4327 return true;
4328
4329 const unsigned Format = Encoding & 0xf;
4330 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
4331 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
4332 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
4333 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
4334 return false;
4335
4336 const unsigned Application = Encoding & 0x70;
4337 if (Application != dwarf::DW_EH_PE_absptr &&
4338 Application != dwarf::DW_EH_PE_pcrel)
4339 return false;
4340
4341 return true;
4342}
4343
4344/// parseDirectiveCFIPersonalityOrLsda
4345/// IsPersonality true for cfi_personality, false for cfi_lsda
4346/// ::= .cfi_personality encoding, [symbol_name]
4347/// ::= .cfi_lsda encoding, [symbol_name]
4348bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
4349 int64_t Encoding = 0;
4350 if (parseAbsoluteExpression(Res&: Encoding))
4351 return true;
4352 if (Encoding == dwarf::DW_EH_PE_omit)
4353 return false;
4354
4355 MCSymbol *Sym;
4356 if (check(P: !isValidEncoding(Encoding), Msg: "unsupported encoding.") ||
4357 parseComma() ||
4358 check(P: parseSymbol(Res&: Sym), Msg: "expected identifier in directive") || parseEOL())
4359 return true;
4360
4361 if (IsPersonality)
4362 getStreamer().emitCFIPersonality(Sym, Encoding);
4363 else
4364 getStreamer().emitCFILsda(Sym, Encoding);
4365 return false;
4366}
4367
4368/// parseDirectiveCFIRememberState
4369/// ::= .cfi_remember_state
4370bool AsmParser::parseDirectiveCFIRememberState(SMLoc DirectiveLoc) {
4371 if (parseEOL())
4372 return true;
4373 getStreamer().emitCFIRememberState(Loc: DirectiveLoc);
4374 return false;
4375}
4376
4377/// parseDirectiveCFIRestoreState
4378/// ::= .cfi_remember_state
4379bool AsmParser::parseDirectiveCFIRestoreState(SMLoc DirectiveLoc) {
4380 if (parseEOL())
4381 return true;
4382 getStreamer().emitCFIRestoreState(Loc: DirectiveLoc);
4383 return false;
4384}
4385
4386/// parseDirectiveCFISameValue
4387/// ::= .cfi_same_value register
4388bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
4389 int64_t Register = 0;
4390
4391 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseEOL())
4392 return true;
4393
4394 getStreamer().emitCFISameValue(Register, Loc: DirectiveLoc);
4395 return false;
4396}
4397
4398/// parseDirectiveCFIRestore
4399/// ::= .cfi_restore register
4400bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
4401 int64_t Register = 0;
4402 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseEOL())
4403 return true;
4404
4405 getStreamer().emitCFIRestore(Register, Loc: DirectiveLoc);
4406 return false;
4407}
4408
4409/// parseDirectiveCFIEscape
4410/// ::= .cfi_escape expression[,...]
4411bool AsmParser::parseDirectiveCFIEscape(SMLoc DirectiveLoc) {
4412 std::string Values;
4413 int64_t CurrValue;
4414 if (parseAbsoluteExpression(Res&: CurrValue))
4415 return true;
4416
4417 Values.push_back(c: (uint8_t)CurrValue);
4418
4419 while (getLexer().is(K: AsmToken::Comma)) {
4420 Lex();
4421
4422 if (parseAbsoluteExpression(Res&: CurrValue))
4423 return true;
4424
4425 Values.push_back(c: (uint8_t)CurrValue);
4426 }
4427
4428 getStreamer().emitCFIEscape(Values, Loc: DirectiveLoc);
4429 return false;
4430}
4431
4432/// parseDirectiveCFIReturnColumn
4433/// ::= .cfi_return_column register
4434bool AsmParser::parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc) {
4435 int64_t Register = 0;
4436 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseEOL())
4437 return true;
4438 getStreamer().emitCFIReturnColumn(Register);
4439 return false;
4440}
4441
4442/// parseDirectiveCFISignalFrame
4443/// ::= .cfi_signal_frame
4444bool AsmParser::parseDirectiveCFISignalFrame(SMLoc DirectiveLoc) {
4445 if (parseEOL())
4446 return true;
4447
4448 getStreamer().emitCFISignalFrame();
4449 return false;
4450}
4451
4452/// parseDirectiveCFIUndefined
4453/// ::= .cfi_undefined register
4454bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
4455 int64_t Register = 0;
4456
4457 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseEOL())
4458 return true;
4459
4460 getStreamer().emitCFIUndefined(Register, Loc: DirectiveLoc);
4461 return false;
4462}
4463
4464/// parseDirectiveCFILabel
4465/// ::= .cfi_label label
4466bool AsmParser::parseDirectiveCFILabel(SMLoc Loc) {
4467 StringRef Name;
4468 Loc = Lexer.getLoc();
4469 if (parseIdentifier(Res&: Name))
4470 return TokError(Msg: "expected identifier");
4471 if (parseEOL())
4472 return true;
4473 getStreamer().emitCFILabelDirective(Loc, Name);
4474 return false;
4475}
4476
4477/// parseDirectiveCFIValOffset
4478/// ::= .cfi_val_offset register, offset
4479bool AsmParser::parseDirectiveCFIValOffset(SMLoc DirectiveLoc) {
4480 int64_t Register = 0;
4481 int64_t Offset = 0;
4482
4483 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseComma() ||
4484 parseAbsoluteExpression(Res&: Offset) || parseEOL())
4485 return true;
4486
4487 getStreamer().emitCFIValOffset(Register, Offset, Loc: DirectiveLoc);
4488 return false;
4489}
4490
4491/// parseDirectiveAltmacro
4492/// ::= .altmacro
4493/// ::= .noaltmacro
4494bool AsmParser::parseDirectiveAltmacro(StringRef Directive) {
4495 if (parseEOL())
4496 return true;
4497 AltMacroMode = (Directive == ".altmacro");
4498 return false;
4499}
4500
4501/// parseDirectiveMacrosOnOff
4502/// ::= .macros_on
4503/// ::= .macros_off
4504bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
4505 if (parseEOL())
4506 return true;
4507 setMacrosEnabled(Directive == ".macros_on");
4508 return false;
4509}
4510
4511/// parseDirectiveMacro
4512/// ::= .macro name[,] [parameters]
4513bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
4514 StringRef Name;
4515 if (parseIdentifier(Res&: Name))
4516 return TokError(Msg: "expected identifier in '.macro' directive");
4517
4518 if (getLexer().is(K: AsmToken::Comma))
4519 Lex();
4520
4521 MCAsmMacroParameters Parameters;
4522 while (getLexer().isNot(K: AsmToken::EndOfStatement)) {
4523
4524 if (!Parameters.empty() && Parameters.back().Vararg)
4525 return Error(L: Lexer.getLoc(), Msg: "vararg parameter '" +
4526 Parameters.back().Name +
4527 "' should be the last parameter");
4528
4529 MCAsmMacroParameter Parameter;
4530 if (parseIdentifier(Res&: Parameter.Name))
4531 return TokError(Msg: "expected identifier in '.macro' directive");
4532
4533 // Emit an error if two (or more) named parameters share the same name
4534 for (const MCAsmMacroParameter& CurrParam : Parameters)
4535 if (CurrParam.Name == Parameter.Name)
4536 return TokError(Msg: "macro '" + Name + "' has multiple parameters"
4537 " named '" + Parameter.Name + "'");
4538
4539 if (Lexer.is(K: AsmToken::Colon)) {
4540 Lex(); // consume ':'
4541
4542 SMLoc QualLoc;
4543 StringRef Qualifier;
4544
4545 QualLoc = Lexer.getLoc();
4546 if (parseIdentifier(Res&: Qualifier))
4547 return Error(L: QualLoc, Msg: "missing parameter qualifier for "
4548 "'" + Parameter.Name + "' in macro '" + Name + "'");
4549
4550 if (Qualifier == "req")
4551 Parameter.Required = true;
4552 else if (Qualifier == "vararg")
4553 Parameter.Vararg = true;
4554 else
4555 return Error(L: QualLoc, Msg: Qualifier + " is not a valid parameter qualifier "
4556 "for '" + Parameter.Name + "' in macro '" + Name + "'");
4557 }
4558
4559 if (getLexer().is(K: AsmToken::Equal)) {
4560 Lex();
4561
4562 SMLoc ParamLoc;
4563
4564 ParamLoc = Lexer.getLoc();
4565 if (parseMacroArgument(MA&: Parameter.Value, /*Vararg=*/false ))
4566 return true;
4567
4568 if (Parameter.Required)
4569 Warning(L: ParamLoc, Msg: "pointless default value for required parameter "
4570 "'" + Parameter.Name + "' in macro '" + Name + "'");
4571 }
4572
4573 Parameters.push_back(x: std::move(Parameter));
4574
4575 if (getLexer().is(K: AsmToken::Comma))
4576 Lex();
4577 }
4578
4579 // Eat just the end of statement.
4580 Lexer.Lex();
4581
4582 // Consuming deferred text, so use Lexer.Lex to ignore Lexing Errors
4583 AsmToken EndToken, StartToken = getTok();
4584 unsigned MacroDepth = 0;
4585 // Lex the macro definition.
4586 while (true) {
4587 // Ignore Lexing errors in macros.
4588 while (Lexer.is(K: AsmToken::Error)) {
4589 Lexer.Lex();
4590 }
4591
4592 // Check whether we have reached the end of the file.
4593 if (getLexer().is(K: AsmToken::Eof))
4594 return Error(L: DirectiveLoc, Msg: "no matching '.endmacro' in definition");
4595
4596 // Otherwise, check whether we have reach the .endmacro or the start of a
4597 // preprocessor line marker.
4598 if (getLexer().is(K: AsmToken::Identifier)) {
4599 if (getTok().getIdentifier() == ".endm" ||
4600 getTok().getIdentifier() == ".endmacro") {
4601 if (MacroDepth == 0) { // Outermost macro.
4602 EndToken = getTok();
4603 Lexer.Lex();
4604 if (getLexer().isNot(K: AsmToken::EndOfStatement))
4605 return TokError(Msg: "unexpected token in '" + EndToken.getIdentifier() +
4606 "' directive");
4607 break;
4608 } else {
4609 // Otherwise we just found the end of an inner macro.
4610 --MacroDepth;
4611 }
4612 } else if (getTok().getIdentifier() == ".macro") {
4613 // We allow nested macros. Those aren't instantiated until the outermost
4614 // macro is expanded so just ignore them for now.
4615 ++MacroDepth;
4616 }
4617 } else if (Lexer.is(K: AsmToken::HashDirective)) {
4618 (void)parseCppHashLineFilenameComment(L: getLexer().getLoc());
4619 }
4620
4621 // Otherwise, scan til the end of the statement.
4622 eatToEndOfStatement();
4623 }
4624
4625 if (getContext().lookupMacro(Name)) {
4626 return Error(L: DirectiveLoc, Msg: "macro '" + Name + "' is already defined");
4627 }
4628
4629 const char *BodyStart = StartToken.getLoc().getPointer();
4630 const char *BodyEnd = EndToken.getLoc().getPointer();
4631 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4632 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
4633 MCAsmMacro Macro(Name, Body, std::move(Parameters));
4634 DEBUG_WITH_TYPE("asm-macros", dbgs() << "Defining new macro:\n";
4635 Macro.dump());
4636 getContext().defineMacro(Name, Macro: std::move(Macro));
4637 return false;
4638}
4639
4640/// checkForBadMacro
4641///
4642/// With the support added for named parameters there may be code out there that
4643/// is transitioning from positional parameters. In versions of gas that did
4644/// not support named parameters they would be ignored on the macro definition.
4645/// But to support both styles of parameters this is not possible so if a macro
4646/// definition has named parameters but does not use them and has what appears
4647/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
4648/// warning that the positional parameter found in body which have no effect.
4649/// Hoping the developer will either remove the named parameters from the macro
4650/// definition so the positional parameters get used if that was what was
4651/// intended or change the macro to use the named parameters. It is possible
4652/// this warning will trigger when the none of the named parameters are used
4653/// and the strings like $1 are infact to simply to be passed trough unchanged.
4654void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
4655 StringRef Body,
4656 ArrayRef<MCAsmMacroParameter> Parameters) {
4657 // If this macro is not defined with named parameters the warning we are
4658 // checking for here doesn't apply.
4659 unsigned NParameters = Parameters.size();
4660 if (NParameters == 0)
4661 return;
4662
4663 bool NamedParametersFound = false;
4664 bool PositionalParametersFound = false;
4665
4666 // Look at the body of the macro for use of both the named parameters and what
4667 // are likely to be positional parameters. This is what expandMacro() is
4668 // doing when it finds the parameters in the body.
4669 while (!Body.empty()) {
4670 // Scan for the next possible parameter.
4671 std::size_t End = Body.size(), Pos = 0;
4672 for (; Pos != End; ++Pos) {
4673 // Check for a substitution or escape.
4674 // This macro is defined with parameters, look for \foo, \bar, etc.
4675 if (Body[Pos] == '\\' && Pos + 1 != End)
4676 break;
4677
4678 // This macro should have parameters, but look for $0, $1, ..., $n too.
4679 if (Body[Pos] != '$' || Pos + 1 == End)
4680 continue;
4681 char Next = Body[Pos + 1];
4682 if (Next == '$' || Next == 'n' ||
4683 isdigit(static_cast<unsigned char>(Next)))
4684 break;
4685 }
4686
4687 // Check if we reached the end.
4688 if (Pos == End)
4689 break;
4690
4691 if (Body[Pos] == '$') {
4692 switch (Body[Pos + 1]) {
4693 // $$ => $
4694 case '$':
4695 break;
4696
4697 // $n => number of arguments
4698 case 'n':
4699 PositionalParametersFound = true;
4700 break;
4701
4702 // $[0-9] => argument
4703 default: {
4704 PositionalParametersFound = true;
4705 break;
4706 }
4707 }
4708 Pos += 2;
4709 } else {
4710 unsigned I = Pos + 1;
4711 while (isIdentifierChar(c: Body[I]) && I + 1 != End)
4712 ++I;
4713
4714 const char *Begin = Body.data() + Pos + 1;
4715 StringRef Argument(Begin, I - (Pos + 1));
4716 unsigned Index = 0;
4717 for (; Index < NParameters; ++Index)
4718 if (Parameters[Index].Name == Argument)
4719 break;
4720
4721 if (Index == NParameters) {
4722 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
4723 Pos += 3;
4724 else {
4725 Pos = I;
4726 }
4727 } else {
4728 NamedParametersFound = true;
4729 Pos += 1 + Argument.size();
4730 }
4731 }
4732 // Update the scan point.
4733 Body = Body.substr(Start: Pos);
4734 }
4735
4736 if (!NamedParametersFound && PositionalParametersFound)
4737 Warning(L: DirectiveLoc, Msg: "macro defined with named parameters which are not "
4738 "used in macro body, possible positional parameter "
4739 "found in body which will have no effect");
4740}
4741
4742/// parseDirectiveExitMacro
4743/// ::= .exitm
4744bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
4745 if (parseEOL())
4746 return true;
4747
4748 if (!isInsideMacroInstantiation())
4749 return TokError(Msg: "unexpected '" + Directive + "' in file, "
4750 "no current macro definition");
4751
4752 // Exit all conditionals that are active in the current macro.
4753 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
4754 TheCondState = TheCondStack.back();
4755 TheCondStack.pop_back();
4756 }
4757
4758 handleMacroExit();
4759 return false;
4760}
4761
4762/// parseDirectiveEndMacro
4763/// ::= .endm
4764/// ::= .endmacro
4765bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
4766 if (getLexer().isNot(K: AsmToken::EndOfStatement))
4767 return TokError(Msg: "unexpected token in '" + Directive + "' directive");
4768
4769 // If we are inside a macro instantiation, terminate the current
4770 // instantiation.
4771 if (isInsideMacroInstantiation()) {
4772 handleMacroExit();
4773 return false;
4774 }
4775
4776 // Otherwise, this .endmacro is a stray entry in the file; well formed
4777 // .endmacro directives are handled during the macro definition parsing.
4778 return TokError(Msg: "unexpected '" + Directive + "' in file, "
4779 "no current macro definition");
4780}
4781
4782/// parseDirectivePurgeMacro
4783/// ::= .purgem name
4784bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
4785 StringRef Name;
4786 SMLoc Loc;
4787 if (parseTokenLoc(Loc) ||
4788 check(P: parseIdentifier(Res&: Name), Loc,
4789 Msg: "expected identifier in '.purgem' directive") ||
4790 parseEOL())
4791 return true;
4792
4793 if (!getContext().lookupMacro(Name))
4794 return Error(L: DirectiveLoc, Msg: "macro '" + Name + "' is not defined");
4795
4796 getContext().undefineMacro(Name);
4797 DEBUG_WITH_TYPE("asm-macros", dbgs()
4798 << "Un-defining macro: " << Name << "\n");
4799 return false;
4800}
4801
4802/// parseDirectiveSpace
4803/// ::= (.skip | .space) expression [ , expression ]
4804bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
4805 SMLoc NumBytesLoc = Lexer.getLoc();
4806 const MCExpr *NumBytes;
4807 if (checkForValidSection() || parseExpression(Res&: NumBytes))
4808 return true;
4809
4810 int64_t FillExpr = 0;
4811 if (parseOptionalToken(T: AsmToken::Comma))
4812 if (parseAbsoluteExpression(Res&: FillExpr))
4813 return true;
4814 if (parseEOL())
4815 return true;
4816
4817 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
4818 getStreamer().emitFill(NumBytes: *NumBytes, FillValue: FillExpr, Loc: NumBytesLoc);
4819
4820 return false;
4821}
4822
4823/// parseDirectiveDCB
4824/// ::= .dcb.{b, l, w} expression, expression
4825bool AsmParser::parseDirectiveDCB(StringRef IDVal, unsigned Size) {
4826 SMLoc NumValuesLoc = Lexer.getLoc();
4827 int64_t NumValues;
4828 if (checkForValidSection() || parseAbsoluteExpression(Res&: NumValues))
4829 return true;
4830
4831 if (NumValues < 0) {
4832 Warning(L: NumValuesLoc, Msg: "'" + Twine(IDVal) + "' directive with negative repeat count has no effect");
4833 return false;
4834 }
4835
4836 if (parseComma())
4837 return true;
4838
4839 const MCExpr *Value;
4840 SMLoc ExprLoc = getLexer().getLoc();
4841 if (parseExpression(Res&: Value))
4842 return true;
4843
4844 // Special case constant expressions to match code generator.
4845 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Val: Value)) {
4846 assert(Size <= 8 && "Invalid size");
4847 uint64_t IntValue = MCE->getValue();
4848 if (!isUIntN(N: 8 * Size, x: IntValue) && !isIntN(N: 8 * Size, x: IntValue))
4849 return Error(L: ExprLoc, Msg: "literal value out of range for directive");
4850 for (uint64_t i = 0, e = NumValues; i != e; ++i)
4851 getStreamer().emitIntValue(Value: IntValue, Size);
4852 } else {
4853 for (uint64_t i = 0, e = NumValues; i != e; ++i)
4854 getStreamer().emitValue(Value, Size, Loc: ExprLoc);
4855 }
4856
4857 return parseEOL();
4858}
4859
4860/// parseDirectiveRealDCB
4861/// ::= .dcb.{d, s} expression, expression
4862bool AsmParser::parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &Semantics) {
4863 SMLoc NumValuesLoc = Lexer.getLoc();
4864 int64_t NumValues;
4865 if (checkForValidSection() || parseAbsoluteExpression(Res&: NumValues))
4866 return true;
4867
4868 if (NumValues < 0) {
4869 Warning(L: NumValuesLoc, Msg: "'" + Twine(IDVal) + "' directive with negative repeat count has no effect");
4870 return false;
4871 }
4872
4873 if (parseComma())
4874 return true;
4875
4876 APInt AsInt;
4877 if (parseRealValue(Semantics, Res&: AsInt) || parseEOL())
4878 return true;
4879
4880 for (uint64_t i = 0, e = NumValues; i != e; ++i)
4881 getStreamer().emitIntValue(Value: AsInt.getLimitedValue(),
4882 Size: AsInt.getBitWidth() / 8);
4883
4884 return false;
4885}
4886
4887/// parseDirectiveDS
4888/// ::= .ds.{b, d, l, p, s, w, x} expression
4889bool AsmParser::parseDirectiveDS(StringRef IDVal, unsigned Size) {
4890 SMLoc NumValuesLoc = Lexer.getLoc();
4891 int64_t NumValues;
4892 if (checkForValidSection() || parseAbsoluteExpression(Res&: NumValues) ||
4893 parseEOL())
4894 return true;
4895
4896 if (NumValues < 0) {
4897 Warning(L: NumValuesLoc, Msg: "'" + Twine(IDVal) + "' directive with negative repeat count has no effect");
4898 return false;
4899 }
4900
4901 for (uint64_t i = 0, e = NumValues; i != e; ++i)
4902 getStreamer().emitFill(NumBytes: Size, FillValue: 0);
4903
4904 return false;
4905}
4906
4907/// parseDirectiveLEB128
4908/// ::= (.sleb128 | .uleb128) [ expression (, expression)* ]
4909bool AsmParser::parseDirectiveLEB128(bool Signed) {
4910 if (checkForValidSection())
4911 return true;
4912
4913 auto parseOp = [&]() -> bool {
4914 const MCExpr *Value;
4915 if (parseExpression(Res&: Value))
4916 return true;
4917 if (Signed)
4918 getStreamer().emitSLEB128Value(Value);
4919 else
4920 getStreamer().emitULEB128Value(Value);
4921 return false;
4922 };
4923
4924 return parseMany(parseOne: parseOp);
4925}
4926
4927/// parseDirectiveSymbolAttribute
4928/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
4929bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
4930 auto parseOp = [&]() -> bool {
4931 StringRef Name;
4932 SMLoc Loc = getTok().getLoc();
4933 if (parseIdentifier(Res&: Name))
4934 return Error(L: Loc, Msg: "expected identifier");
4935
4936 if (discardLTOSymbol(Name))
4937 return false;
4938
4939 MCSymbol *Sym = getContext().parseSymbol(Name);
4940
4941 // Assembler local symbols don't make any sense here, except for directives
4942 // that the symbol should be tagged.
4943 if (Sym->isTemporary() && Attr != MCSA_Memtag)
4944 return Error(L: Loc, Msg: "non-local symbol required");
4945
4946 if (!getStreamer().emitSymbolAttribute(Symbol: Sym, Attribute: Attr))
4947 return Error(L: Loc, Msg: "unable to emit symbol attribute");
4948 return false;
4949 };
4950
4951 return parseMany(parseOne: parseOp);
4952}
4953
4954/// parseDirectiveComm
4955/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
4956bool AsmParser::parseDirectiveComm(bool IsLocal) {
4957 if (checkForValidSection())
4958 return true;
4959
4960 SMLoc IDLoc = getLexer().getLoc();
4961 MCSymbol *Sym;
4962 if (parseSymbol(Res&: Sym))
4963 return TokError(Msg: "expected identifier in directive");
4964
4965 if (parseComma())
4966 return true;
4967
4968 int64_t Size;
4969 SMLoc SizeLoc = getLexer().getLoc();
4970 if (parseAbsoluteExpression(Res&: Size))
4971 return true;
4972
4973 int64_t Pow2Alignment = 0;
4974 SMLoc Pow2AlignmentLoc;
4975 if (getLexer().is(K: AsmToken::Comma)) {
4976 Lex();
4977 Pow2AlignmentLoc = getLexer().getLoc();
4978 if (parseAbsoluteExpression(Res&: Pow2Alignment))
4979 return true;
4980
4981 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
4982 if (IsLocal && LCOMM == LCOMM::NoAlignment)
4983 return Error(L: Pow2AlignmentLoc, Msg: "alignment not supported on this target");
4984
4985 // If this target takes alignments in bytes (not log) validate and convert.
4986 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
4987 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
4988 if (!isPowerOf2_64(Value: Pow2Alignment))
4989 return Error(L: Pow2AlignmentLoc, Msg: "alignment must be a power of 2");
4990 Pow2Alignment = Log2_64(Value: Pow2Alignment);
4991 }
4992 }
4993
4994 if (parseEOL())
4995 return true;
4996
4997 // NOTE: a size of zero for a .comm should create a undefined symbol
4998 // but a size of .lcomm creates a bss symbol of size zero.
4999 if (Size < 0)
5000 return Error(L: SizeLoc, Msg: "size must be non-negative");
5001
5002 Sym->redefineIfPossible();
5003 if (!Sym->isUndefined())
5004 return Error(L: IDLoc, Msg: "invalid symbol redefinition");
5005
5006 // Create the Symbol as a common or local common with Size and Pow2Alignment
5007 if (IsLocal) {
5008 getStreamer().emitLocalCommonSymbol(Symbol: Sym, Size,
5009 ByteAlignment: Align(1ULL << Pow2Alignment));
5010 return false;
5011 }
5012
5013 getStreamer().emitCommonSymbol(Symbol: Sym, Size, ByteAlignment: Align(1ULL << Pow2Alignment));
5014 return false;
5015}
5016
5017/// parseDirectiveAbort
5018/// ::= .abort [... message ...]
5019bool AsmParser::parseDirectiveAbort(SMLoc DirectiveLoc) {
5020 StringRef Str = parseStringToEndOfStatement();
5021 if (parseEOL())
5022 return true;
5023
5024 if (Str.empty())
5025 return Error(L: DirectiveLoc, Msg: ".abort detected. Assembly stopping");
5026
5027 // FIXME: Actually abort assembly here.
5028 return Error(L: DirectiveLoc,
5029 Msg: ".abort '" + Str + "' detected. Assembly stopping");
5030}
5031
5032/// parseDirectiveInclude
5033/// ::= .include "filename"
5034bool AsmParser::parseDirectiveInclude() {
5035 // Allow the strings to have escaped octal character sequence.
5036 std::string Filename;
5037 SMLoc IncludeLoc = getTok().getLoc();
5038
5039 if (check(P: getTok().isNot(K: AsmToken::String),
5040 Msg: "expected string in '.include' directive") ||
5041 parseEscapedString(Data&: Filename) ||
5042 check(P: getTok().isNot(K: AsmToken::EndOfStatement),
5043 Msg: "unexpected token in '.include' directive") ||
5044 // Attempt to switch the lexer to the included file before consuming the
5045 // end of statement to avoid losing it when we switch.
5046 check(P: enterIncludeFile(Filename), Loc: IncludeLoc,
5047 Msg: "Could not find include file '" + Filename + "'"))
5048 return true;
5049
5050 return false;
5051}
5052
5053/// parseDirectiveIncbin
5054/// ::= .incbin "filename" [ , skip [ , count ] ]
5055bool AsmParser::parseDirectiveIncbin() {
5056 // Allow the strings to have escaped octal character sequence.
5057 std::string Filename;
5058 SMLoc IncbinLoc = getTok().getLoc();
5059 if (check(P: getTok().isNot(K: AsmToken::String),
5060 Msg: "expected string in '.incbin' directive") ||
5061 parseEscapedString(Data&: Filename))
5062 return true;
5063
5064 int64_t Skip = 0;
5065 const MCExpr *Count = nullptr;
5066 SMLoc SkipLoc, CountLoc;
5067 if (parseOptionalToken(T: AsmToken::Comma)) {
5068 // The skip expression can be omitted while specifying the count, e.g:
5069 // .incbin "filename",,4
5070 if (getTok().isNot(K: AsmToken::Comma)) {
5071 if (parseTokenLoc(Loc&: SkipLoc) || parseAbsoluteExpression(Res&: Skip))
5072 return true;
5073 }
5074 if (parseOptionalToken(T: AsmToken::Comma)) {
5075 CountLoc = getTok().getLoc();
5076 if (parseExpression(Res&: Count))
5077 return true;
5078 }
5079 }
5080
5081 if (parseEOL())
5082 return true;
5083
5084 if (check(P: Skip < 0, Loc: SkipLoc, Msg: "skip is negative"))
5085 return true;
5086
5087 // Attempt to process the included file.
5088 if (processIncbinFile(Filename, Skip, Count, Loc: CountLoc))
5089 return Error(L: IncbinLoc, Msg: "Could not find incbin file '" + Filename + "'");
5090 return false;
5091}
5092
5093/// parseDirectiveIf
5094/// ::= .if{,eq,ge,gt,le,lt,ne} expression
5095bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
5096 TheCondStack.push_back(x: TheCondState);
5097 TheCondState.TheCond = AsmCond::IfCond;
5098 if (TheCondState.Ignore) {
5099 eatToEndOfStatement();
5100 } else {
5101 int64_t ExprValue;
5102 if (parseAbsoluteExpression(Res&: ExprValue) || parseEOL())
5103 return true;
5104
5105 switch (DirKind) {
5106 default:
5107 llvm_unreachable("unsupported directive");
5108 case DK_IF:
5109 case DK_IFNE:
5110 break;
5111 case DK_IFEQ:
5112 ExprValue = ExprValue == 0;
5113 break;
5114 case DK_IFGE:
5115 ExprValue = ExprValue >= 0;
5116 break;
5117 case DK_IFGT:
5118 ExprValue = ExprValue > 0;
5119 break;
5120 case DK_IFLE:
5121 ExprValue = ExprValue <= 0;
5122 break;
5123 case DK_IFLT:
5124 ExprValue = ExprValue < 0;
5125 break;
5126 }
5127
5128 TheCondState.CondMet = ExprValue;
5129 TheCondState.Ignore = !TheCondState.CondMet;
5130 }
5131
5132 return false;
5133}
5134
5135/// parseDirectiveIfb
5136/// ::= .ifb string
5137bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
5138 TheCondStack.push_back(x: TheCondState);
5139 TheCondState.TheCond = AsmCond::IfCond;
5140
5141 if (TheCondState.Ignore) {
5142 eatToEndOfStatement();
5143 } else {
5144 StringRef Str = parseStringToEndOfStatement();
5145
5146 if (parseEOL())
5147 return true;
5148
5149 TheCondState.CondMet = ExpectBlank == Str.empty();
5150 TheCondState.Ignore = !TheCondState.CondMet;
5151 }
5152
5153 return false;
5154}
5155
5156/// parseDirectiveIfc
5157/// ::= .ifc string1, string2
5158/// ::= .ifnc string1, string2
5159bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
5160 TheCondStack.push_back(x: TheCondState);
5161 TheCondState.TheCond = AsmCond::IfCond;
5162
5163 if (TheCondState.Ignore) {
5164 eatToEndOfStatement();
5165 } else {
5166 StringRef Str1 = parseStringToComma();
5167
5168 if (parseComma())
5169 return true;
5170
5171 StringRef Str2 = parseStringToEndOfStatement();
5172
5173 if (parseEOL())
5174 return true;
5175
5176 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
5177 TheCondState.Ignore = !TheCondState.CondMet;
5178 }
5179
5180 return false;
5181}
5182
5183/// parseDirectiveIfeqs
5184/// ::= .ifeqs string1, string2
5185bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) {
5186 TheCondStack.push_back(x: TheCondState);
5187 TheCondState.TheCond = AsmCond::IfCond;
5188
5189 if (TheCondState.Ignore) {
5190 eatToEndOfStatement();
5191 } else {
5192 if (Lexer.isNot(K: AsmToken::String)) {
5193 if (ExpectEqual)
5194 return TokError(Msg: "expected string parameter for '.ifeqs' directive");
5195 return TokError(Msg: "expected string parameter for '.ifnes' directive");
5196 }
5197
5198 StringRef String1 = getTok().getStringContents();
5199 Lex();
5200
5201 if (Lexer.isNot(K: AsmToken::Comma)) {
5202 if (ExpectEqual)
5203 return TokError(
5204 Msg: "expected comma after first string for '.ifeqs' directive");
5205 return TokError(
5206 Msg: "expected comma after first string for '.ifnes' directive");
5207 }
5208
5209 Lex();
5210
5211 if (Lexer.isNot(K: AsmToken::String)) {
5212 if (ExpectEqual)
5213 return TokError(Msg: "expected string parameter for '.ifeqs' directive");
5214 return TokError(Msg: "expected string parameter for '.ifnes' directive");
5215 }
5216
5217 StringRef String2 = getTok().getStringContents();
5218 Lex();
5219
5220 TheCondState.CondMet = ExpectEqual == (String1 == String2);
5221 TheCondState.Ignore = !TheCondState.CondMet;
5222 }
5223
5224 return false;
5225}
5226
5227/// parseDirectiveIfdef
5228/// ::= .ifdef symbol
5229bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
5230 StringRef Name;
5231 TheCondStack.push_back(x: TheCondState);
5232 TheCondState.TheCond = AsmCond::IfCond;
5233
5234 if (TheCondState.Ignore) {
5235 eatToEndOfStatement();
5236 } else {
5237 if (check(P: parseIdentifier(Res&: Name), Msg: "expected identifier after '.ifdef'") ||
5238 parseEOL())
5239 return true;
5240
5241 MCSymbol *Sym = getContext().lookupSymbol(Name);
5242
5243 if (expect_defined)
5244 TheCondState.CondMet = (Sym && !Sym->isUndefined());
5245 else
5246 TheCondState.CondMet = (!Sym || Sym->isUndefined());
5247 TheCondState.Ignore = !TheCondState.CondMet;
5248 }
5249
5250 return false;
5251}
5252
5253/// parseDirectiveElseIf
5254/// ::= .elseif expression
5255bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
5256 if (TheCondState.TheCond != AsmCond::IfCond &&
5257 TheCondState.TheCond != AsmCond::ElseIfCond)
5258 return Error(L: DirectiveLoc, Msg: "Encountered a .elseif that doesn't follow an"
5259 " .if or an .elseif");
5260 TheCondState.TheCond = AsmCond::ElseIfCond;
5261
5262 bool LastIgnoreState = false;
5263 if (!TheCondStack.empty())
5264 LastIgnoreState = TheCondStack.back().Ignore;
5265 if (LastIgnoreState || TheCondState.CondMet) {
5266 TheCondState.Ignore = true;
5267 eatToEndOfStatement();
5268 } else {
5269 int64_t ExprValue;
5270 if (parseAbsoluteExpression(Res&: ExprValue))
5271 return true;
5272
5273 if (parseEOL())
5274 return true;
5275
5276 TheCondState.CondMet = ExprValue;
5277 TheCondState.Ignore = !TheCondState.CondMet;
5278 }
5279
5280 return false;
5281}
5282
5283/// parseDirectiveElse
5284/// ::= .else
5285bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
5286 if (parseEOL())
5287 return true;
5288
5289 if (TheCondState.TheCond != AsmCond::IfCond &&
5290 TheCondState.TheCond != AsmCond::ElseIfCond)
5291 return Error(L: DirectiveLoc, Msg: "Encountered a .else that doesn't follow "
5292 " an .if or an .elseif");
5293 TheCondState.TheCond = AsmCond::ElseCond;
5294 bool LastIgnoreState = false;
5295 if (!TheCondStack.empty())
5296 LastIgnoreState = TheCondStack.back().Ignore;
5297 if (LastIgnoreState || TheCondState.CondMet)
5298 TheCondState.Ignore = true;
5299 else
5300 TheCondState.Ignore = false;
5301
5302 return false;
5303}
5304
5305/// parseDirectiveEnd
5306/// ::= .end
5307bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
5308 if (parseEOL())
5309 return true;
5310
5311 while (Lexer.isNot(K: AsmToken::Eof))
5312 Lexer.Lex();
5313
5314 return false;
5315}
5316
5317/// parseDirectiveError
5318/// ::= .err
5319/// ::= .error [string]
5320bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
5321 if (!TheCondStack.empty()) {
5322 if (TheCondStack.back().Ignore) {
5323 eatToEndOfStatement();
5324 return false;
5325 }
5326 }
5327
5328 if (!WithMessage)
5329 return Error(L, Msg: ".err encountered");
5330
5331 StringRef Message = ".error directive invoked in source file";
5332 if (Lexer.isNot(K: AsmToken::EndOfStatement)) {
5333 if (Lexer.isNot(K: AsmToken::String))
5334 return TokError(Msg: ".error argument must be a string");
5335
5336 Message = getTok().getStringContents();
5337 Lex();
5338 }
5339
5340 return Error(L, Msg: Message);
5341}
5342
5343/// parseDirectiveWarning
5344/// ::= .warning [string]
5345bool AsmParser::parseDirectiveWarning(SMLoc L) {
5346 if (!TheCondStack.empty()) {
5347 if (TheCondStack.back().Ignore) {
5348 eatToEndOfStatement();
5349 return false;
5350 }
5351 }
5352
5353 StringRef Message = ".warning directive invoked in source file";
5354
5355 if (!parseOptionalToken(T: AsmToken::EndOfStatement)) {
5356 if (Lexer.isNot(K: AsmToken::String))
5357 return TokError(Msg: ".warning argument must be a string");
5358
5359 Message = getTok().getStringContents();
5360 Lex();
5361 if (parseEOL())
5362 return true;
5363 }
5364
5365 return Warning(L, Msg: Message);
5366}
5367
5368/// parseDirectiveEndIf
5369/// ::= .endif
5370bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
5371 if (parseEOL())
5372 return true;
5373
5374 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
5375 return Error(L: DirectiveLoc, Msg: "Encountered a .endif that doesn't follow "
5376 "an .if or .else");
5377 if (!TheCondStack.empty()) {
5378 TheCondState = TheCondStack.back();
5379 TheCondStack.pop_back();
5380 }
5381
5382 return false;
5383}
5384
5385void AsmParser::initializeDirectiveKindMap() {
5386 /* Lookup will be done with the directive
5387 * converted to lower case, so all these
5388 * keys should be lower case.
5389 * (target specific directives are handled
5390 * elsewhere)
5391 */
5392 DirectiveKindMap[".set"] = DK_SET;
5393 DirectiveKindMap[".equ"] = DK_EQU;
5394 DirectiveKindMap[".equiv"] = DK_EQUIV;
5395 DirectiveKindMap[".ascii"] = DK_ASCII;
5396 DirectiveKindMap[".asciz"] = DK_ASCIZ;
5397 DirectiveKindMap[".string"] = DK_STRING;
5398 DirectiveKindMap[".byte"] = DK_BYTE;
5399 DirectiveKindMap[".base64"] = DK_BASE64;
5400 DirectiveKindMap[".short"] = DK_SHORT;
5401 DirectiveKindMap[".value"] = DK_VALUE;
5402 DirectiveKindMap[".2byte"] = DK_2BYTE;
5403 DirectiveKindMap[".long"] = DK_LONG;
5404 DirectiveKindMap[".int"] = DK_INT;
5405 DirectiveKindMap[".4byte"] = DK_4BYTE;
5406 DirectiveKindMap[".quad"] = DK_QUAD;
5407 DirectiveKindMap[".8byte"] = DK_8BYTE;
5408 DirectiveKindMap[".octa"] = DK_OCTA;
5409 DirectiveKindMap[".single"] = DK_SINGLE;
5410 DirectiveKindMap[".float"] = DK_FLOAT;
5411 DirectiveKindMap[".double"] = DK_DOUBLE;
5412 DirectiveKindMap[".align"] = DK_ALIGN;
5413 DirectiveKindMap[".align32"] = DK_ALIGN32;
5414 DirectiveKindMap[".balign"] = DK_BALIGN;
5415 DirectiveKindMap[".balignw"] = DK_BALIGNW;
5416 DirectiveKindMap[".balignl"] = DK_BALIGNL;
5417 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
5418 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
5419 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
5420 DirectiveKindMap[".prefalign"] = DK_PREFALIGN;
5421 DirectiveKindMap[".org"] = DK_ORG;
5422 DirectiveKindMap[".fill"] = DK_FILL;
5423 DirectiveKindMap[".zero"] = DK_ZERO;
5424 DirectiveKindMap[".extern"] = DK_EXTERN;
5425 DirectiveKindMap[".globl"] = DK_GLOBL;
5426 DirectiveKindMap[".global"] = DK_GLOBAL;
5427 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
5428 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
5429 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
5430 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
5431 DirectiveKindMap[".reference"] = DK_REFERENCE;
5432 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
5433 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
5434 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
5435 DirectiveKindMap[".cold"] = DK_COLD;
5436 DirectiveKindMap[".comm"] = DK_COMM;
5437 DirectiveKindMap[".common"] = DK_COMMON;
5438 DirectiveKindMap[".lcomm"] = DK_LCOMM;
5439 DirectiveKindMap[".abort"] = DK_ABORT;
5440 DirectiveKindMap[".include"] = DK_INCLUDE;
5441 DirectiveKindMap[".incbin"] = DK_INCBIN;
5442 DirectiveKindMap[".code16"] = DK_CODE16;
5443 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
5444 DirectiveKindMap[".rept"] = DK_REPT;
5445 DirectiveKindMap[".rep"] = DK_REPT;
5446 DirectiveKindMap[".irp"] = DK_IRP;
5447 DirectiveKindMap[".irpc"] = DK_IRPC;
5448 DirectiveKindMap[".endr"] = DK_ENDR;
5449 DirectiveKindMap[".if"] = DK_IF;
5450 DirectiveKindMap[".ifeq"] = DK_IFEQ;
5451 DirectiveKindMap[".ifge"] = DK_IFGE;
5452 DirectiveKindMap[".ifgt"] = DK_IFGT;
5453 DirectiveKindMap[".ifle"] = DK_IFLE;
5454 DirectiveKindMap[".iflt"] = DK_IFLT;
5455 DirectiveKindMap[".ifne"] = DK_IFNE;
5456 DirectiveKindMap[".ifb"] = DK_IFB;
5457 DirectiveKindMap[".ifnb"] = DK_IFNB;
5458 DirectiveKindMap[".ifc"] = DK_IFC;
5459 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
5460 DirectiveKindMap[".ifnc"] = DK_IFNC;
5461 DirectiveKindMap[".ifnes"] = DK_IFNES;
5462 DirectiveKindMap[".ifdef"] = DK_IFDEF;
5463 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
5464 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
5465 DirectiveKindMap[".elseif"] = DK_ELSEIF;
5466 DirectiveKindMap[".else"] = DK_ELSE;
5467 DirectiveKindMap[".end"] = DK_END;
5468 DirectiveKindMap[".endif"] = DK_ENDIF;
5469 DirectiveKindMap[".skip"] = DK_SKIP;
5470 DirectiveKindMap[".space"] = DK_SPACE;
5471 DirectiveKindMap[".file"] = DK_FILE;
5472 DirectiveKindMap[".line"] = DK_LINE;
5473 DirectiveKindMap[".loc"] = DK_LOC;
5474 DirectiveKindMap[".loc_label"] = DK_LOC_LABEL;
5475 DirectiveKindMap[".stabs"] = DK_STABS;
5476 DirectiveKindMap[".cv_file"] = DK_CV_FILE;
5477 DirectiveKindMap[".cv_func_id"] = DK_CV_FUNC_ID;
5478 DirectiveKindMap[".cv_loc"] = DK_CV_LOC;
5479 DirectiveKindMap[".cv_linetable"] = DK_CV_LINETABLE;
5480 DirectiveKindMap[".cv_inline_linetable"] = DK_CV_INLINE_LINETABLE;
5481 DirectiveKindMap[".cv_inline_site_id"] = DK_CV_INLINE_SITE_ID;
5482 DirectiveKindMap[".cv_def_range"] = DK_CV_DEF_RANGE;
5483 DirectiveKindMap[".cv_string"] = DK_CV_STRING;
5484 DirectiveKindMap[".cv_stringtable"] = DK_CV_STRINGTABLE;
5485 DirectiveKindMap[".cv_filechecksums"] = DK_CV_FILECHECKSUMS;
5486 DirectiveKindMap[".cv_filechecksumoffset"] = DK_CV_FILECHECKSUM_OFFSET;
5487 DirectiveKindMap[".cv_fpo_data"] = DK_CV_FPO_DATA;
5488 DirectiveKindMap[".sleb128"] = DK_SLEB128;
5489 DirectiveKindMap[".uleb128"] = DK_ULEB128;
5490 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
5491 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
5492 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
5493 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
5494 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
5495 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
5496 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
5497 DirectiveKindMap[".cfi_llvm_def_aspace_cfa"] = DK_CFI_LLVM_DEF_ASPACE_CFA;
5498 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
5499 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
5500 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
5501 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
5502 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
5503 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
5504 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
5505 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
5506 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
5507 DirectiveKindMap[".cfi_return_column"] = DK_CFI_RETURN_COLUMN;
5508 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
5509 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
5510 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
5511 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
5512 DirectiveKindMap[".cfi_label"] = DK_CFI_LABEL;
5513 DirectiveKindMap[".cfi_b_key_frame"] = DK_CFI_B_KEY_FRAME;
5514 DirectiveKindMap[".cfi_mte_tagged_frame"] = DK_CFI_MTE_TAGGED_FRAME;
5515 DirectiveKindMap[".cfi_val_offset"] = DK_CFI_VAL_OFFSET;
5516 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
5517 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
5518 DirectiveKindMap[".macro"] = DK_MACRO;
5519 DirectiveKindMap[".exitm"] = DK_EXITM;
5520 DirectiveKindMap[".endm"] = DK_ENDM;
5521 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
5522 DirectiveKindMap[".purgem"] = DK_PURGEM;
5523 DirectiveKindMap[".err"] = DK_ERR;
5524 DirectiveKindMap[".error"] = DK_ERROR;
5525 DirectiveKindMap[".warning"] = DK_WARNING;
5526 DirectiveKindMap[".altmacro"] = DK_ALTMACRO;
5527 DirectiveKindMap[".noaltmacro"] = DK_NOALTMACRO;
5528 DirectiveKindMap[".reloc"] = DK_RELOC;
5529 DirectiveKindMap[".dc"] = DK_DC;
5530 DirectiveKindMap[".dc.a"] = DK_DC_A;
5531 DirectiveKindMap[".dc.b"] = DK_DC_B;
5532 DirectiveKindMap[".dc.d"] = DK_DC_D;
5533 DirectiveKindMap[".dc.l"] = DK_DC_L;
5534 DirectiveKindMap[".dc.s"] = DK_DC_S;
5535 DirectiveKindMap[".dc.w"] = DK_DC_W;
5536 DirectiveKindMap[".dc.x"] = DK_DC_X;
5537 DirectiveKindMap[".dcb"] = DK_DCB;
5538 DirectiveKindMap[".dcb.b"] = DK_DCB_B;
5539 DirectiveKindMap[".dcb.d"] = DK_DCB_D;
5540 DirectiveKindMap[".dcb.l"] = DK_DCB_L;
5541 DirectiveKindMap[".dcb.s"] = DK_DCB_S;
5542 DirectiveKindMap[".dcb.w"] = DK_DCB_W;
5543 DirectiveKindMap[".dcb.x"] = DK_DCB_X;
5544 DirectiveKindMap[".ds"] = DK_DS;
5545 DirectiveKindMap[".ds.b"] = DK_DS_B;
5546 DirectiveKindMap[".ds.d"] = DK_DS_D;
5547 DirectiveKindMap[".ds.l"] = DK_DS_L;
5548 DirectiveKindMap[".ds.p"] = DK_DS_P;
5549 DirectiveKindMap[".ds.s"] = DK_DS_S;
5550 DirectiveKindMap[".ds.w"] = DK_DS_W;
5551 DirectiveKindMap[".ds.x"] = DK_DS_X;
5552 DirectiveKindMap[".print"] = DK_PRINT;
5553 DirectiveKindMap[".addrsig"] = DK_ADDRSIG;
5554 DirectiveKindMap[".addrsig_sym"] = DK_ADDRSIG_SYM;
5555 DirectiveKindMap[".pseudoprobe"] = DK_PSEUDO_PROBE;
5556 DirectiveKindMap[".lto_discard"] = DK_LTO_DISCARD;
5557 DirectiveKindMap[".lto_set_conditional"] = DK_LTO_SET_CONDITIONAL;
5558 DirectiveKindMap[".memtag"] = DK_MEMTAG;
5559}
5560
5561MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
5562 AsmToken EndToken, StartToken = getTok();
5563
5564 unsigned NestLevel = 0;
5565 while (true) {
5566 // Check whether we have reached the end of the file.
5567 if (getLexer().is(K: AsmToken::Eof)) {
5568 printError(L: DirectiveLoc, Msg: "no matching '.endr' in definition");
5569 return nullptr;
5570 }
5571
5572 if (Lexer.is(K: AsmToken::Identifier)) {
5573 StringRef Ident = getTok().getIdentifier();
5574 if (Ident == ".rep" || Ident == ".rept" || Ident == ".irp" ||
5575 Ident == ".irpc") {
5576 ++NestLevel;
5577 } else if (Ident == ".endr") {
5578 if (NestLevel == 0) {
5579 EndToken = getTok();
5580 Lex();
5581 if (Lexer.is(K: AsmToken::EndOfStatement))
5582 break;
5583 printError(L: getTok().getLoc(), Msg: "expected newline");
5584 return nullptr;
5585 }
5586 --NestLevel;
5587 }
5588 }
5589
5590 // Otherwise, scan till the end of the statement.
5591 eatToEndOfStatement();
5592 }
5593
5594 const char *BodyStart = StartToken.getLoc().getPointer();
5595 const char *BodyEnd = EndToken.getLoc().getPointer();
5596 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
5597
5598 // We Are Anonymous.
5599 MacroLikeBodies.emplace_back(args: StringRef(), args&: Body, args: MCAsmMacroParameters());
5600 return &MacroLikeBodies.back();
5601}
5602
5603void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
5604 raw_svector_ostream &OS) {
5605 OS << ".endr\n";
5606
5607 std::unique_ptr<MemoryBuffer> Instantiation =
5608 MemoryBuffer::getMemBufferCopy(InputData: OS.str(), BufferName: "<instantiation>");
5609
5610 // Create the macro instantiation object and add to the current macro
5611 // instantiation stack.
5612 MacroInstantiation *MI = new MacroInstantiation{
5613 .InstantiationLoc: DirectiveLoc, .ExitBuffer: CurBuffer, .ExitLoc: getTok().getLoc(), .CondStackDepth: TheCondStack.size()};
5614 ActiveMacros.push_back(x: MI);
5615
5616 // Jump to the macro instantiation and prime the lexer.
5617 CurBuffer = SrcMgr.AddNewSourceBuffer(F: std::move(Instantiation), IncludeLoc: SMLoc());
5618 Lexer.setBuffer(Buf: SrcMgr.getMemoryBuffer(i: CurBuffer)->getBuffer());
5619 Lex();
5620}
5621
5622/// parseDirectiveRept
5623/// ::= .rep | .rept count
5624bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
5625 const MCExpr *CountExpr;
5626 SMLoc CountLoc = getTok().getLoc();
5627 if (parseExpression(Res&: CountExpr))
5628 return true;
5629
5630 int64_t Count;
5631 if (!CountExpr->evaluateAsAbsolute(Res&: Count, Asm: getStreamer().getAssemblerPtr())) {
5632 return Error(L: CountLoc, Msg: "unexpected token in '" + Dir + "' directive");
5633 }
5634
5635 if (check(P: Count < 0, Loc: CountLoc, Msg: "Count is negative") || parseEOL())
5636 return true;
5637
5638 // Lex the rept definition.
5639 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
5640 if (!M)
5641 return true;
5642
5643 // Macro instantiation is lexical, unfortunately. We construct a new buffer
5644 // to hold the macro body with substitutions.
5645 SmallString<256> Buf;
5646 raw_svector_ostream OS(Buf);
5647 while (Count--) {
5648 // Note that the AtPseudoVariable is disabled for instantiations of .rep(t).
5649 if (expandMacro(OS, Macro&: *M, Parameters: {}, A: {}, EnableAtPseudoVariable: false))
5650 return true;
5651 }
5652 instantiateMacroLikeBody(M, DirectiveLoc, OS);
5653
5654 return false;
5655}
5656
5657/// parseDirectiveIrp
5658/// ::= .irp symbol,values
5659bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
5660 MCAsmMacroParameter Parameter;
5661 MCAsmMacroArguments A;
5662 if (check(P: parseIdentifier(Res&: Parameter.Name),
5663 Msg: "expected identifier in '.irp' directive") ||
5664 parseComma() || parseMacroArguments(M: nullptr, A) || parseEOL())
5665 return true;
5666
5667 // Lex the irp definition.
5668 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
5669 if (!M)
5670 return true;
5671
5672 // Macro instantiation is lexical, unfortunately. We construct a new buffer
5673 // to hold the macro body with substitutions.
5674 SmallString<256> Buf;
5675 raw_svector_ostream OS(Buf);
5676
5677 for (const MCAsmMacroArgument &Arg : A) {
5678 // Note that the AtPseudoVariable is enabled for instantiations of .irp.
5679 // This is undocumented, but GAS seems to support it.
5680 if (expandMacro(OS, Macro&: *M, Parameters: Parameter, A: Arg, EnableAtPseudoVariable: true))
5681 return true;
5682 }
5683
5684 instantiateMacroLikeBody(M, DirectiveLoc, OS);
5685
5686 return false;
5687}
5688
5689/// parseDirectiveIrpc
5690/// ::= .irpc symbol,values
5691bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
5692 MCAsmMacroParameter Parameter;
5693 MCAsmMacroArguments A;
5694
5695 if (check(P: parseIdentifier(Res&: Parameter.Name),
5696 Msg: "expected identifier in '.irpc' directive") ||
5697 parseComma() || parseMacroArguments(M: nullptr, A))
5698 return true;
5699
5700 if (A.size() != 1 || A.front().size() != 1)
5701 return TokError(Msg: "unexpected token in '.irpc' directive");
5702 if (parseEOL())
5703 return true;
5704
5705 // Lex the irpc definition.
5706 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
5707 if (!M)
5708 return true;
5709
5710 // Macro instantiation is lexical, unfortunately. We construct a new buffer
5711 // to hold the macro body with substitutions.
5712 SmallString<256> Buf;
5713 raw_svector_ostream OS(Buf);
5714
5715 StringRef Values = A[0][0].is(K: AsmToken::String) ? A[0][0].getStringContents()
5716 : A[0][0].getString();
5717 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
5718 MCAsmMacroArgument Arg;
5719 Arg.emplace_back(args: AsmToken::Identifier, args: Values.substr(Start: I, N: 1));
5720
5721 // Note that the AtPseudoVariable is enabled for instantiations of .irpc.
5722 // This is undocumented, but GAS seems to support it.
5723 if (expandMacro(OS, Macro&: *M, Parameters: Parameter, A: Arg, EnableAtPseudoVariable: true))
5724 return true;
5725 }
5726
5727 instantiateMacroLikeBody(M, DirectiveLoc, OS);
5728
5729 return false;
5730}
5731
5732bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
5733 if (ActiveMacros.empty())
5734 return TokError(Msg: "unmatched '.endr' directive");
5735
5736 // The only .repl that should get here are the ones created by
5737 // instantiateMacroLikeBody.
5738 assert(getLexer().is(AsmToken::EndOfStatement));
5739
5740 handleMacroExit();
5741 return false;
5742}
5743
5744bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
5745 size_t Len) {
5746 const MCExpr *Value;
5747 SMLoc ExprLoc = getLexer().getLoc();
5748 if (parseExpression(Res&: Value))
5749 return true;
5750 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Val: Value);
5751 if (!MCE)
5752 return Error(L: ExprLoc, Msg: "unexpected expression in _emit");
5753 uint64_t IntValue = MCE->getValue();
5754 if (!isUInt<8>(x: IntValue) && !isInt<8>(x: IntValue))
5755 return Error(L: ExprLoc, Msg: "literal value out of range for directive");
5756
5757 Info.AsmRewrites->emplace_back(Args: AOK_Emit, Args&: IDLoc, Args&: Len);
5758 return false;
5759}
5760
5761bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
5762 const MCExpr *Value;
5763 SMLoc ExprLoc = getLexer().getLoc();
5764 if (parseExpression(Res&: Value))
5765 return true;
5766 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Val: Value);
5767 if (!MCE)
5768 return Error(L: ExprLoc, Msg: "unexpected expression in align");
5769 uint64_t IntValue = MCE->getValue();
5770 if (!isPowerOf2_64(Value: IntValue))
5771 return Error(L: ExprLoc, Msg: "literal value not a power of two greater then zero");
5772
5773 Info.AsmRewrites->emplace_back(Args: AOK_Align, Args&: IDLoc, Args: 5, Args: Log2_64(Value: IntValue));
5774 return false;
5775}
5776
5777bool AsmParser::parseDirectivePrint(SMLoc DirectiveLoc) {
5778 const AsmToken StrTok = getTok();
5779 Lex();
5780 if (StrTok.isNot(K: AsmToken::String) || StrTok.getString().front() != '"')
5781 return Error(L: DirectiveLoc, Msg: "expected double quoted string after .print");
5782 if (parseEOL())
5783 return true;
5784 llvm::outs() << StrTok.getStringContents() << '\n';
5785 return false;
5786}
5787
5788bool AsmParser::parseDirectiveAddrsig() {
5789 if (parseEOL())
5790 return true;
5791 getStreamer().emitAddrsig();
5792 return false;
5793}
5794
5795bool AsmParser::parseDirectiveAddrsigSym() {
5796 MCSymbol *Sym;
5797 if (check(P: parseSymbol(Res&: Sym), Msg: "expected identifier") || parseEOL())
5798 return true;
5799 getStreamer().emitAddrsigSym(Sym);
5800 return false;
5801}
5802
5803bool AsmParser::parseDirectivePseudoProbe() {
5804 int64_t Guid;
5805 int64_t Index;
5806 int64_t Type;
5807 int64_t Attr;
5808 int64_t Discriminator = 0;
5809 if (parseIntToken(V&: Guid))
5810 return true;
5811 if (parseIntToken(V&: Index))
5812 return true;
5813 if (parseIntToken(V&: Type))
5814 return true;
5815 if (parseIntToken(V&: Attr))
5816 return true;
5817 if (hasDiscriminator(Flags: Attr) && parseIntToken(V&: Discriminator))
5818 return true;
5819
5820 // Parse inline stack like @ GUID:11:12 @ GUID:1:11 @ GUID:3:21
5821 MCPseudoProbeInlineStack InlineStack;
5822
5823 while (getLexer().is(K: AsmToken::At)) {
5824 // eat @
5825 Lex();
5826
5827 int64_t CallerGuid = 0;
5828 if (getLexer().is(K: AsmToken::Integer)) {
5829 CallerGuid = getTok().getIntVal();
5830 Lex();
5831 }
5832
5833 // eat colon
5834 if (getLexer().is(K: AsmToken::Colon))
5835 Lex();
5836
5837 int64_t CallerProbeId = 0;
5838 if (getLexer().is(K: AsmToken::Integer)) {
5839 CallerProbeId = getTok().getIntVal();
5840 Lex();
5841 }
5842
5843 InlineSite Site(CallerGuid, CallerProbeId);
5844 InlineStack.push_back(Elt: Site);
5845 }
5846
5847 // Parse function entry name
5848 StringRef FnName;
5849 if (parseIdentifier(Res&: FnName))
5850 return Error(L: getLexer().getLoc(), Msg: "expected identifier");
5851 MCSymbol *FnSym = getContext().lookupSymbol(Name: FnName);
5852
5853 if (parseEOL())
5854 return true;
5855
5856 getStreamer().emitPseudoProbe(Guid, Index, Type, Attr, Discriminator,
5857 InlineStack, FnSym);
5858 return false;
5859}
5860
5861/// parseDirectiveLTODiscard
5862/// ::= ".lto_discard" [ identifier ( , identifier )* ]
5863/// The LTO library emits this directive to discard non-prevailing symbols.
5864/// We ignore symbol assignments and attribute changes for the specified
5865/// symbols.
5866bool AsmParser::parseDirectiveLTODiscard() {
5867 auto ParseOp = [&]() -> bool {
5868 StringRef Name;
5869 SMLoc Loc = getTok().getLoc();
5870 if (parseIdentifier(Res&: Name))
5871 return Error(L: Loc, Msg: "expected identifier");
5872 LTODiscardSymbols.insert(V: Name);
5873 return false;
5874 };
5875
5876 LTODiscardSymbols.clear();
5877 return parseMany(parseOne: ParseOp);
5878}
5879
5880// We are comparing pointers, but the pointers are relative to a single string.
5881// Thus, this should always be deterministic.
5882static int rewritesSort(const AsmRewrite *AsmRewriteA,
5883 const AsmRewrite *AsmRewriteB) {
5884 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
5885 return -1;
5886 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
5887 return 1;
5888
5889 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
5890 // rewrite to the same location. Make sure the SizeDirective rewrite is
5891 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
5892 // ensures the sort algorithm is stable.
5893 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
5894 AsmRewritePrecedence[AsmRewriteB->Kind])
5895 return -1;
5896
5897 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
5898 AsmRewritePrecedence[AsmRewriteB->Kind])
5899 return 1;
5900 llvm_unreachable("Unstable rewrite sort.");
5901}
5902
5903bool AsmParser::parseMSInlineAsm(
5904 std::string &AsmString, unsigned &NumOutputs, unsigned &NumInputs,
5905 SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
5906 SmallVectorImpl<std::string> &Constraints,
5907 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
5908 MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
5909 SmallVector<void *, 4> InputDecls;
5910 SmallVector<void *, 4> OutputDecls;
5911 SmallVector<bool, 4> InputDeclsAddressOf;
5912 SmallVector<bool, 4> OutputDeclsAddressOf;
5913 SmallVector<std::string, 4> InputConstraints;
5914 SmallVector<std::string, 4> OutputConstraints;
5915 SmallVector<MCRegister, 4> ClobberRegs;
5916
5917 SmallVector<AsmRewrite, 4> AsmStrRewrites;
5918
5919 // Prime the lexer.
5920 Lex();
5921
5922 // While we have input, parse each statement.
5923 unsigned InputIdx = 0;
5924 unsigned OutputIdx = 0;
5925 while (getLexer().isNot(K: AsmToken::Eof)) {
5926 // Parse curly braces marking block start/end
5927 if (parseCurlyBlockScope(AsmStrRewrites))
5928 continue;
5929
5930 ParseStatementInfo Info(&AsmStrRewrites);
5931 bool StatementErr = parseStatement(Info, SI: &SI);
5932
5933 if (StatementErr || Info.ParseError) {
5934 // Emit pending errors if any exist.
5935 printPendingErrors();
5936 return true;
5937 }
5938
5939 // No pending error should exist here.
5940 assert(!hasPendingError() && "unexpected error from parseStatement");
5941
5942 if (Info.Opcode == ~0U)
5943 continue;
5944
5945 const MCInstrDesc &Desc = MII->get(Opcode: Info.Opcode);
5946
5947 // Build the list of clobbers, outputs and inputs.
5948 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
5949 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
5950
5951 // Register operand.
5952 if (Operand.isReg() && !Operand.needAddressOf() &&
5953 !getTargetParser().omitRegisterFromClobberLists(Reg: Operand.getReg())) {
5954 unsigned NumDefs = Desc.getNumDefs();
5955 // Clobber.
5956 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
5957 ClobberRegs.push_back(Elt: Operand.getReg());
5958 continue;
5959 }
5960
5961 // Expr/Input or Output.
5962 StringRef SymName = Operand.getSymName();
5963 if (SymName.empty())
5964 continue;
5965
5966 void *OpDecl = Operand.getOpDecl();
5967 if (!OpDecl)
5968 continue;
5969
5970 StringRef Constraint = Operand.getConstraint();
5971 if (Operand.isImm()) {
5972 // Offset as immediate
5973 if (Operand.isOffsetOfLocal())
5974 Constraint = "r";
5975 else
5976 Constraint = "i";
5977 }
5978
5979 bool isOutput = (i == 1) && Desc.mayStore();
5980 bool Restricted = Operand.isMemUseUpRegs();
5981 SMLoc Start = SMLoc::getFromPointer(Ptr: SymName.data());
5982 if (isOutput) {
5983 ++InputIdx;
5984 OutputDecls.push_back(Elt: OpDecl);
5985 OutputDeclsAddressOf.push_back(Elt: Operand.needAddressOf());
5986 OutputConstraints.push_back(Elt: ("=" + Constraint).str());
5987 AsmStrRewrites.emplace_back(Args: AOK_Output, Args&: Start, Args: SymName.size(), Args: 0,
5988 Args&: Restricted);
5989 } else {
5990 InputDecls.push_back(Elt: OpDecl);
5991 InputDeclsAddressOf.push_back(Elt: Operand.needAddressOf());
5992 InputConstraints.push_back(Elt: Constraint.str());
5993 if (Desc.operands()[i - 1].isBranchTarget())
5994 AsmStrRewrites.emplace_back(Args: AOK_CallInput, Args&: Start, Args: SymName.size(), Args: 0,
5995 Args&: Restricted);
5996 else
5997 AsmStrRewrites.emplace_back(Args: AOK_Input, Args&: Start, Args: SymName.size(), Args: 0,
5998 Args&: Restricted);
5999 }
6000 }
6001
6002 // Consider implicit defs to be clobbers. Think of cpuid and push.
6003 llvm::append_range(C&: ClobberRegs, R: Desc.implicit_defs());
6004 }
6005
6006 // Set the number of Outputs and Inputs.
6007 NumOutputs = OutputDecls.size();
6008 NumInputs = InputDecls.size();
6009
6010 // Set the unique clobbers.
6011 array_pod_sort(Start: ClobberRegs.begin(), End: ClobberRegs.end());
6012 ClobberRegs.erase(CS: llvm::unique(R&: ClobberRegs), CE: ClobberRegs.end());
6013 Clobbers.assign(NumElts: ClobberRegs.size(), Elt: std::string());
6014 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
6015 raw_string_ostream OS(Clobbers[I]);
6016 IP->printRegName(OS, Reg: ClobberRegs[I]);
6017 }
6018
6019 // Merge the various outputs and inputs. Output are expected first.
6020 if (NumOutputs || NumInputs) {
6021 unsigned NumExprs = NumOutputs + NumInputs;
6022 OpDecls.resize(N: NumExprs);
6023 Constraints.resize(N: NumExprs);
6024 for (unsigned i = 0; i < NumOutputs; ++i) {
6025 OpDecls[i] = std::make_pair(x&: OutputDecls[i], y&: OutputDeclsAddressOf[i]);
6026 Constraints[i] = OutputConstraints[i];
6027 }
6028 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
6029 OpDecls[j] = std::make_pair(x&: InputDecls[i], y&: InputDeclsAddressOf[i]);
6030 Constraints[j] = InputConstraints[i];
6031 }
6032 }
6033
6034 // Build the IR assembly string.
6035 std::string AsmStringIR;
6036 raw_string_ostream OS(AsmStringIR);
6037 StringRef ASMString =
6038 SrcMgr.getMemoryBuffer(i: SrcMgr.getMainFileID())->getBuffer();
6039 const char *AsmStart = ASMString.begin();
6040 const char *AsmEnd = ASMString.end();
6041 array_pod_sort(Start: AsmStrRewrites.begin(), End: AsmStrRewrites.end(), Compare: rewritesSort);
6042 for (auto I = AsmStrRewrites.begin(), E = AsmStrRewrites.end(); I != E; ++I) {
6043 const AsmRewrite &AR = *I;
6044 // Check if this has already been covered by another rewrite...
6045 if (AR.Done)
6046 continue;
6047 AsmRewriteKind Kind = AR.Kind;
6048
6049 const char *Loc = AR.Loc.getPointer();
6050 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
6051
6052 // Emit everything up to the immediate/expression.
6053 if (unsigned Len = Loc - AsmStart)
6054 OS << StringRef(AsmStart, Len);
6055
6056 // Skip the original expression.
6057 if (Kind == AOK_Skip) {
6058 AsmStart = Loc + AR.Len;
6059 continue;
6060 }
6061
6062 unsigned AdditionalSkip = 0;
6063 // Rewrite expressions in $N notation.
6064 switch (Kind) {
6065 default:
6066 break;
6067 case AOK_IntelExpr:
6068 assert(AR.IntelExp.isValid() && "cannot write invalid intel expression");
6069 if (AR.IntelExp.NeedBracs)
6070 OS << "[";
6071 if (AR.IntelExp.hasBaseReg())
6072 OS << AR.IntelExp.BaseReg;
6073 if (AR.IntelExp.hasIndexReg())
6074 OS << (AR.IntelExp.hasBaseReg() ? " + " : "")
6075 << AR.IntelExp.IndexReg;
6076 if (AR.IntelExp.Scale > 1)
6077 OS << " * $$" << AR.IntelExp.Scale;
6078 if (AR.IntelExp.hasOffset()) {
6079 if (AR.IntelExp.hasRegs())
6080 OS << " + ";
6081 // Fuse this rewrite with a rewrite of the offset name, if present.
6082 StringRef OffsetName = AR.IntelExp.OffsetName;
6083 SMLoc OffsetLoc = SMLoc::getFromPointer(Ptr: AR.IntelExp.OffsetName.data());
6084 size_t OffsetLen = OffsetName.size();
6085 auto rewrite_it = std::find_if(
6086 first: I, last: AsmStrRewrites.end(), pred: [&](const AsmRewrite &FusingAR) {
6087 return FusingAR.Loc == OffsetLoc && FusingAR.Len == OffsetLen &&
6088 (FusingAR.Kind == AOK_Input ||
6089 FusingAR.Kind == AOK_CallInput);
6090 });
6091 if (rewrite_it == AsmStrRewrites.end()) {
6092 OS << "offset " << OffsetName;
6093 } else if (rewrite_it->Kind == AOK_CallInput) {
6094 OS << "${" << InputIdx++ << ":P}";
6095 rewrite_it->Done = true;
6096 } else {
6097 OS << '$' << InputIdx++;
6098 rewrite_it->Done = true;
6099 }
6100 }
6101 if (AR.IntelExp.Imm || AR.IntelExp.emitImm())
6102 OS << (AR.IntelExp.emitImm() ? "$$" : " + $$") << AR.IntelExp.Imm;
6103 if (AR.IntelExp.NeedBracs)
6104 OS << "]";
6105 break;
6106 case AOK_Label:
6107 OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
6108 break;
6109 case AOK_Input:
6110 if (AR.IntelExpRestricted)
6111 OS << "${" << InputIdx++ << ":P}";
6112 else
6113 OS << '$' << InputIdx++;
6114 break;
6115 case AOK_CallInput:
6116 OS << "${" << InputIdx++ << ":P}";
6117 break;
6118 case AOK_Output:
6119 if (AR.IntelExpRestricted)
6120 OS << "${" << OutputIdx++ << ":P}";
6121 else
6122 OS << '$' << OutputIdx++;
6123 break;
6124 case AOK_SizeDirective:
6125 switch (AR.Val) {
6126 default: break;
6127 case 8: OS << "byte ptr "; break;
6128 case 16: OS << "word ptr "; break;
6129 case 32: OS << "dword ptr "; break;
6130 case 64: OS << "qword ptr "; break;
6131 case 80: OS << "xword ptr "; break;
6132 case 128: OS << "xmmword ptr "; break;
6133 case 256: OS << "ymmword ptr "; break;
6134 }
6135 break;
6136 case AOK_Emit:
6137 OS << ".byte";
6138 break;
6139 case AOK_Align: {
6140 // MS alignment directives are measured in bytes. If the native assembler
6141 // measures alignment in bytes, we can pass it straight through.
6142 OS << ".align";
6143 if (getContext().getAsmInfo()->getAlignmentIsInBytes())
6144 break;
6145
6146 // Alignment is in log2 form, so print that instead and skip the original
6147 // immediate.
6148 unsigned Val = AR.Val;
6149 OS << ' ' << Val;
6150 assert(Val < 10 && "Expected alignment less then 2^10.");
6151 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
6152 break;
6153 }
6154 case AOK_EVEN:
6155 OS << ".even";
6156 break;
6157 case AOK_EndOfStatement:
6158 OS << "\n\t";
6159 break;
6160 }
6161
6162 // Skip the original expression.
6163 AsmStart = Loc + AR.Len + AdditionalSkip;
6164 }
6165
6166 // Emit the remainder of the asm string.
6167 if (AsmStart != AsmEnd)
6168 OS << StringRef(AsmStart, AsmEnd - AsmStart);
6169
6170 AsmString = std::move(AsmStringIR);
6171 return false;
6172}
6173
6174bool HLASMAsmParser::parseAsHLASMLabel(ParseStatementInfo &Info,
6175 MCAsmParserSemaCallback *SI) {
6176 AsmToken LabelTok = getTok();
6177 SMLoc LabelLoc = LabelTok.getLoc();
6178 StringRef LabelVal;
6179
6180 if (parseIdentifier(Res&: LabelVal))
6181 return Error(L: LabelLoc, Msg: "The HLASM Label has to be an Identifier");
6182
6183 // We have validated whether the token is an Identifier.
6184 // Now we have to validate whether the token is a
6185 // valid HLASM Label.
6186 if (!getTargetParser().isLabel(Token&: LabelTok) || checkForValidSection())
6187 return true;
6188
6189 // Lex leading spaces to get to the next operand.
6190 lexLeadingSpaces();
6191
6192 // We shouldn't emit the label if there is nothing else after the label.
6193 // i.e asm("<token>\n")
6194 if (getTok().is(K: AsmToken::EndOfStatement))
6195 return Error(L: LabelLoc,
6196 Msg: "Cannot have just a label for an HLASM inline asm statement");
6197
6198 MCSymbol *Sym = getContext().parseSymbol(
6199 Name: getContext().getAsmInfo()->isHLASM() ? LabelVal.upper() : LabelVal);
6200
6201 // Emit the label.
6202 Out.emitLabel(Symbol: Sym, Loc: LabelLoc);
6203
6204 // If we are generating dwarf for assembly source files then gather the
6205 // info to make a dwarf label entry for this label if needed.
6206 if (enabledGenDwarfForAssembly())
6207 MCGenDwarfLabelEntry::Make(Symbol: Sym, MCOS: &getStreamer(), SrcMgr&: getSourceManager(),
6208 Loc&: LabelLoc);
6209
6210 return false;
6211}
6212
6213bool HLASMAsmParser::parseAsMachineInstruction(ParseStatementInfo &Info,
6214 MCAsmParserSemaCallback *SI) {
6215 AsmToken OperationEntryTok = Lexer.getTok();
6216 SMLoc OperationEntryLoc = OperationEntryTok.getLoc();
6217 StringRef OperationEntryVal;
6218
6219 // Attempt to parse the first token as an Identifier
6220 if (parseIdentifier(Res&: OperationEntryVal))
6221 return Error(L: OperationEntryLoc, Msg: "unexpected token at start of statement");
6222
6223 // Once we've parsed the operation entry successfully, lex
6224 // any spaces to get to the OperandEntries.
6225 lexLeadingSpaces();
6226
6227 return parseAndMatchAndEmitTargetInstruction(
6228 Info, IDVal: OperationEntryVal, ID: OperationEntryTok, IDLoc: OperationEntryLoc);
6229}
6230
6231bool HLASMAsmParser::parseStatement(ParseStatementInfo &Info,
6232 MCAsmParserSemaCallback *SI) {
6233 assert(!hasPendingError() && "parseStatement started with pending error");
6234
6235 // Should the first token be interpreted as a HLASM Label.
6236 bool ShouldParseAsHLASMLabel = false;
6237
6238 // If a Name Entry exists, it should occur at the very
6239 // start of the string. In this case, we should parse the
6240 // first non-space token as a Label.
6241 // If the Name entry is missing (i.e. there's some other
6242 // token), then we attempt to parse the first non-space
6243 // token as a Machine Instruction.
6244 if (getTok().isNot(K: AsmToken::Space))
6245 ShouldParseAsHLASMLabel = true;
6246
6247 // If we have an EndOfStatement (which includes the target's comment
6248 // string) we can appropriately lex it early on)
6249 if (Lexer.is(K: AsmToken::EndOfStatement)) {
6250 // if this is a line comment we can drop it safely
6251 if (getTok().getString().empty() || getTok().getString().front() == '\r' ||
6252 getTok().getString().front() == '\n')
6253 Out.addBlankLine();
6254 Lex();
6255 return false;
6256 }
6257
6258 // We have established how to parse the inline asm statement.
6259 // Now we can safely lex any leading spaces to get to the
6260 // first token.
6261 lexLeadingSpaces();
6262
6263 // If we see a new line or carriage return as the first operand,
6264 // after lexing leading spaces, emit the new line and lex the
6265 // EndOfStatement token.
6266 if (Lexer.is(K: AsmToken::EndOfStatement)) {
6267 if (getTok().getString().front() == '\n' ||
6268 getTok().getString().front() == '\r') {
6269 Out.addBlankLine();
6270 Lex();
6271 return false;
6272 }
6273 }
6274
6275 // Handle the label first if we have to before processing the rest
6276 // of the tokens as a machine instruction.
6277 if (ShouldParseAsHLASMLabel) {
6278 // If there were any errors while handling and emitting the label,
6279 // early return.
6280 if (parseAsHLASMLabel(Info, SI)) {
6281 // If we know we've failed in parsing, simply eat until end of the
6282 // statement. This ensures that we don't process any other statements.
6283 eatToEndOfStatement();
6284 return true;
6285 }
6286 }
6287
6288 return parseAsMachineInstruction(Info, SI);
6289}
6290
6291bool llvm::MCParserUtils::parseAssignmentExpression(StringRef Name,
6292 bool allow_redef,
6293 MCAsmParser &Parser,
6294 MCSymbol *&Sym,
6295 const MCExpr *&Value) {
6296
6297 // FIXME: Use better location, we should use proper tokens.
6298 SMLoc EqualLoc = Parser.getTok().getLoc();
6299 if (Parser.parseExpression(Res&: Value))
6300 return Parser.TokError(Msg: "missing expression");
6301 if (Parser.parseEOL())
6302 return true;
6303 // Relocation specifiers are not permitted. For now, handle just
6304 // MCSymbolRefExpr.
6305 if (auto *S = dyn_cast<MCSymbolRefExpr>(Val: Value); S && S->getSpecifier())
6306 return Parser.Error(
6307 L: EqualLoc, Msg: "relocation specifier not permitted in symbol equating");
6308
6309 // Validate that the LHS is allowed to be a variable (either it has not been
6310 // used as a symbol, or it is an absolute symbol).
6311 Sym = Parser.getContext().lookupSymbol(Name);
6312 if (Sym) {
6313 if ((Sym->isVariable() || Sym->isDefined()) &&
6314 (!allow_redef || !Sym->isRedefinable()))
6315 return Parser.Error(L: EqualLoc, Msg: "redefinition of '" + Name + "'");
6316 // If the symbol is redefinable, clone it and update the symbol table
6317 // to the new symbol. Existing references to the original symbol remain
6318 // unchanged.
6319 if (Sym->isRedefinable())
6320 Sym = Parser.getContext().cloneSymbol(Sym&: *Sym);
6321 } else if (Name == ".") {
6322 Parser.getStreamer().emitValueToOffset(Offset: Value, Value: 0, Loc: EqualLoc);
6323 return false;
6324 } else
6325 Sym = Parser.getContext().parseSymbol(Name);
6326
6327 Sym->setRedefinable(allow_redef);
6328
6329 return false;
6330}
6331
6332/// Create an MCAsmParser instance.
6333MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
6334 MCStreamer &Out, const MCAsmInfo &MAI,
6335 unsigned CB) {
6336 if (C.getTargetTriple().isSystemZ() && C.getTargetTriple().isOSzOS())
6337 return new HLASMAsmParser(SM, C, Out, MAI, CB);
6338
6339 return new AsmParser(SM, C, Out, MAI, CB);
6340}
6341