xref: /freebsd/contrib/llvm-project/llvm/lib/MC/MCParser/AsmParser.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
10b57cec5SDimitry Andric //===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
95ffd83dbSDimitry Andric // This class implements a parser for assembly files similar to gas syntax.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/ADT/APFloat.h"
140b57cec5SDimitry Andric #include "llvm/ADT/APInt.h"
150b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
160b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
17fe6060f1SDimitry Andric #include "llvm/ADT/SmallSet.h"
180b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
190b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
200b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h"
210b57cec5SDimitry Andric #include "llvm/ADT/StringMap.h"
220b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
230b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
240b57cec5SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h"
258bcb0991SDimitry Andric #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
260b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
270b57cec5SDimitry Andric #include "llvm/MC/MCCodeView.h"
280b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
290b57cec5SDimitry Andric #include "llvm/MC/MCDirectives.h"
300b57cec5SDimitry Andric #include "llvm/MC/MCDwarf.h"
310b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
320b57cec5SDimitry Andric #include "llvm/MC/MCInstPrinter.h"
330b57cec5SDimitry Andric #include "llvm/MC/MCInstrDesc.h"
340b57cec5SDimitry Andric #include "llvm/MC/MCInstrInfo.h"
350b57cec5SDimitry Andric #include "llvm/MC/MCParser/AsmCond.h"
360b57cec5SDimitry Andric #include "llvm/MC/MCParser/AsmLexer.h"
370b57cec5SDimitry Andric #include "llvm/MC/MCParser/MCAsmLexer.h"
380b57cec5SDimitry Andric #include "llvm/MC/MCParser/MCAsmParser.h"
390b57cec5SDimitry Andric #include "llvm/MC/MCParser/MCAsmParserExtension.h"
400b57cec5SDimitry Andric #include "llvm/MC/MCParser/MCAsmParserUtils.h"
410b57cec5SDimitry Andric #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
420b57cec5SDimitry Andric #include "llvm/MC/MCParser/MCTargetAsmParser.h"
430b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
440b57cec5SDimitry Andric #include "llvm/MC/MCSection.h"
450b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
460b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h"
470b57cec5SDimitry Andric #include "llvm/MC/MCTargetOptions.h"
480b57cec5SDimitry Andric #include "llvm/MC/MCValue.h"
490b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
500b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
510b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
520b57cec5SDimitry Andric #include "llvm/Support/MD5.h"
530b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
540b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
550b57cec5SDimitry Andric #include "llvm/Support/SMLoc.h"
560b57cec5SDimitry Andric #include "llvm/Support/SourceMgr.h"
570b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
580b57cec5SDimitry Andric #include <algorithm>
590b57cec5SDimitry Andric #include <cassert>
600b57cec5SDimitry Andric #include <cctype>
610b57cec5SDimitry Andric #include <climits>
620b57cec5SDimitry Andric #include <cstddef>
630b57cec5SDimitry Andric #include <cstdint>
640b57cec5SDimitry Andric #include <deque>
650b57cec5SDimitry Andric #include <memory>
66bdd1243dSDimitry Andric #include <optional>
670b57cec5SDimitry Andric #include <sstream>
680b57cec5SDimitry Andric #include <string>
690b57cec5SDimitry Andric #include <tuple>
700b57cec5SDimitry Andric #include <utility>
710b57cec5SDimitry Andric #include <vector>
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric using namespace llvm;
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric MCAsmParserSemaCallback::~MCAsmParserSemaCallback() = default;
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric namespace {
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric /// Helper types for tracking macro definitions.
800b57cec5SDimitry Andric typedef std::vector<AsmToken> MCAsmMacroArgument;
810b57cec5SDimitry Andric typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric /// Helper class for storing information about an active macro
840b57cec5SDimitry Andric /// instantiation.
850b57cec5SDimitry Andric struct MacroInstantiation {
860b57cec5SDimitry Andric   /// The location of the instantiation.
870b57cec5SDimitry Andric   SMLoc InstantiationLoc;
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric   /// The buffer where parsing should resume upon instantiation completion.
90480093f4SDimitry Andric   unsigned ExitBuffer;
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric   /// The location where parsing should resume upon instantiation completion.
930b57cec5SDimitry Andric   SMLoc ExitLoc;
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric   /// The depth of TheCondStack at the start of the instantiation.
960b57cec5SDimitry Andric   size_t CondStackDepth;
970b57cec5SDimitry Andric };
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric struct ParseStatementInfo {
1000b57cec5SDimitry Andric   /// The parsed operands from the last parsed statement.
1010b57cec5SDimitry Andric   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric   /// The opcode from the last parsed instruction.
1040b57cec5SDimitry Andric   unsigned Opcode = ~0U;
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric   /// Was there an error parsing the inline assembly?
1070b57cec5SDimitry Andric   bool ParseError = false;
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric   SmallVectorImpl<AsmRewrite> *AsmRewrites = nullptr;
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric   ParseStatementInfo() = delete;
1120b57cec5SDimitry Andric   ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
1130b57cec5SDimitry Andric     : AsmRewrites(rewrites) {}
1140b57cec5SDimitry Andric };
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric /// The concrete assembly parser instance.
1170b57cec5SDimitry Andric class AsmParser : public MCAsmParser {
1180b57cec5SDimitry Andric private:
1190b57cec5SDimitry Andric   AsmLexer Lexer;
1200b57cec5SDimitry Andric   MCContext &Ctx;
1210b57cec5SDimitry Andric   MCStreamer &Out;
1220b57cec5SDimitry Andric   const MCAsmInfo &MAI;
1230b57cec5SDimitry Andric   SourceMgr &SrcMgr;
1240b57cec5SDimitry Andric   SourceMgr::DiagHandlerTy SavedDiagHandler;
1250b57cec5SDimitry Andric   void *SavedDiagContext;
1260b57cec5SDimitry Andric   std::unique_ptr<MCAsmParserExtension> PlatformParser;
127e8d8bef9SDimitry Andric   SMLoc StartTokLoc;
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric   /// This is the current buffer index we're lexing from as managed by the
1300b57cec5SDimitry Andric   /// SourceMgr object.
1310b57cec5SDimitry Andric   unsigned CurBuffer;
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric   AsmCond TheCondState;
1340b57cec5SDimitry Andric   std::vector<AsmCond> TheCondStack;
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric   /// maps directive names to handler methods in parser
1370b57cec5SDimitry Andric   /// extensions. Extensions register themselves in this map by calling
1380b57cec5SDimitry Andric   /// addDirectiveHandler.
1390b57cec5SDimitry Andric   StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric   /// Stack of active macro instantiations.
1420b57cec5SDimitry Andric   std::vector<MacroInstantiation*> ActiveMacros;
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric   /// List of bodies of anonymous macros.
1450b57cec5SDimitry Andric   std::deque<MCAsmMacro> MacroLikeBodies;
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric   /// Boolean tracking whether macro substitution is enabled.
1480b57cec5SDimitry Andric   unsigned MacrosEnabledFlag : 1;
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric   /// Keeps track of how many .macro's have been instantiated.
1510b57cec5SDimitry Andric   unsigned NumOfMacroInstantiations;
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric   /// The values from the last parsed cpp hash file line comment if any.
1540b57cec5SDimitry Andric   struct CppHashInfoTy {
1550b57cec5SDimitry Andric     StringRef Filename;
1560b57cec5SDimitry Andric     int64_t LineNumber;
1570b57cec5SDimitry Andric     SMLoc Loc;
1580b57cec5SDimitry Andric     unsigned Buf;
15904eeddc0SDimitry Andric     CppHashInfoTy() : LineNumber(0), Buf(0) {}
1600b57cec5SDimitry Andric   };
1610b57cec5SDimitry Andric   CppHashInfoTy CppHashInfo;
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric   /// The filename from the first cpp hash file line comment, if any.
1640b57cec5SDimitry Andric   StringRef FirstCppHashFilename;
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric   /// List of forward directional labels for diagnosis at the end.
1670b57cec5SDimitry Andric   SmallVector<std::tuple<SMLoc, CppHashInfoTy, MCSymbol *>, 4> DirLabels;
1680b57cec5SDimitry Andric 
169fe6060f1SDimitry Andric   SmallSet<StringRef, 2> LTODiscardSymbols;
170fe6060f1SDimitry Andric 
1710b57cec5SDimitry Andric   /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
1720b57cec5SDimitry Andric   unsigned AssemblerDialect = ~0U;
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric   /// is Darwin compatibility enabled?
1750b57cec5SDimitry Andric   bool IsDarwin = false;
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric   /// Are we parsing ms-style inline assembly?
1785ffd83dbSDimitry Andric   bool ParsingMSInlineAsm = false;
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric   /// Did we already inform the user about inconsistent MD5 usage?
1810b57cec5SDimitry Andric   bool ReportedInconsistentMD5 = false;
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric   // Is alt macro mode enabled.
1840b57cec5SDimitry Andric   bool AltMacroMode = false;
1850b57cec5SDimitry Andric 
186fe6060f1SDimitry Andric protected:
187fe6060f1SDimitry Andric   virtual bool parseStatement(ParseStatementInfo &Info,
188fe6060f1SDimitry Andric                               MCAsmParserSemaCallback *SI);
189fe6060f1SDimitry Andric 
190fe6060f1SDimitry Andric   /// This routine uses the target specific ParseInstruction function to
191fe6060f1SDimitry Andric   /// parse an instruction into Operands, and then call the target specific
192fe6060f1SDimitry Andric   /// MatchAndEmit function to match and emit the instruction.
193fe6060f1SDimitry Andric   bool parseAndMatchAndEmitTargetInstruction(ParseStatementInfo &Info,
194fe6060f1SDimitry Andric                                              StringRef IDVal, AsmToken ID,
195fe6060f1SDimitry Andric                                              SMLoc IDLoc);
196fe6060f1SDimitry Andric 
197fe6060f1SDimitry Andric   /// Should we emit DWARF describing this assembler source?  (Returns false if
198fe6060f1SDimitry Andric   /// the source has .file directives, which means we don't want to generate
199fe6060f1SDimitry Andric   /// info describing the assembler source itself.)
200fe6060f1SDimitry Andric   bool enabledGenDwarfForAssembly();
201fe6060f1SDimitry Andric 
2020b57cec5SDimitry Andric public:
2030b57cec5SDimitry Andric   AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
2040b57cec5SDimitry Andric             const MCAsmInfo &MAI, unsigned CB);
2050b57cec5SDimitry Andric   AsmParser(const AsmParser &) = delete;
2060b57cec5SDimitry Andric   AsmParser &operator=(const AsmParser &) = delete;
2070b57cec5SDimitry Andric   ~AsmParser() override;
2080b57cec5SDimitry Andric 
2090b57cec5SDimitry Andric   bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric   void addDirectiveHandler(StringRef Directive,
2120b57cec5SDimitry Andric                            ExtensionDirectiveHandler Handler) override {
2130b57cec5SDimitry Andric     ExtensionDirectiveMap[Directive] = Handler;
2140b57cec5SDimitry Andric   }
2150b57cec5SDimitry Andric 
2160b57cec5SDimitry Andric   void addAliasForDirective(StringRef Directive, StringRef Alias) override {
2175ffd83dbSDimitry Andric     DirectiveKindMap[Directive.lower()] = DirectiveKindMap[Alias.lower()];
2180b57cec5SDimitry Andric   }
2190b57cec5SDimitry Andric 
2200b57cec5SDimitry Andric   /// @name MCAsmParser Interface
2210b57cec5SDimitry Andric   /// {
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric   SourceMgr &getSourceManager() override { return SrcMgr; }
2240b57cec5SDimitry Andric   MCAsmLexer &getLexer() override { return Lexer; }
2250b57cec5SDimitry Andric   MCContext &getContext() override { return Ctx; }
2260b57cec5SDimitry Andric   MCStreamer &getStreamer() override { return Out; }
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric   CodeViewContext &getCVContext() { return Ctx.getCVContext(); }
2290b57cec5SDimitry Andric 
2300b57cec5SDimitry Andric   unsigned getAssemblerDialect() override {
2310b57cec5SDimitry Andric     if (AssemblerDialect == ~0U)
2320b57cec5SDimitry Andric       return MAI.getAssemblerDialect();
2330b57cec5SDimitry Andric     else
2340b57cec5SDimitry Andric       return AssemblerDialect;
2350b57cec5SDimitry Andric   }
2360b57cec5SDimitry Andric   void setAssemblerDialect(unsigned i) override {
2370b57cec5SDimitry Andric     AssemblerDialect = i;
2380b57cec5SDimitry Andric   }
2390b57cec5SDimitry Andric 
240bdd1243dSDimitry Andric   void Note(SMLoc L, const Twine &Msg, SMRange Range = std::nullopt) override;
241bdd1243dSDimitry Andric   bool Warning(SMLoc L, const Twine &Msg,
242bdd1243dSDimitry Andric                SMRange Range = std::nullopt) override;
243bdd1243dSDimitry Andric   bool printError(SMLoc L, const Twine &Msg,
244bdd1243dSDimitry Andric                   SMRange Range = std::nullopt) override;
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric   const AsmToken &Lex() override;
2470b57cec5SDimitry Andric 
2485ffd83dbSDimitry Andric   void setParsingMSInlineAsm(bool V) override {
2495ffd83dbSDimitry Andric     ParsingMSInlineAsm = V;
2500b57cec5SDimitry Andric     // When parsing MS inline asm, we must lex 0b1101 and 0ABCH as binary and
2510b57cec5SDimitry Andric     // hex integer literals.
2520b57cec5SDimitry Andric     Lexer.setLexMasmIntegers(V);
2530b57cec5SDimitry Andric   }
2545ffd83dbSDimitry Andric   bool isParsingMSInlineAsm() override { return ParsingMSInlineAsm; }
2550b57cec5SDimitry Andric 
256fe6060f1SDimitry Andric   bool discardLTOSymbol(StringRef Name) const override {
257fe6060f1SDimitry Andric     return LTODiscardSymbols.contains(Name);
258fe6060f1SDimitry Andric   }
259fe6060f1SDimitry Andric 
260fe6060f1SDimitry Andric   bool parseMSInlineAsm(std::string &AsmString, unsigned &NumOutputs,
261fe6060f1SDimitry Andric                         unsigned &NumInputs,
2620b57cec5SDimitry Andric                         SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
2630b57cec5SDimitry Andric                         SmallVectorImpl<std::string> &Constraints,
2640b57cec5SDimitry Andric                         SmallVectorImpl<std::string> &Clobbers,
2650b57cec5SDimitry Andric                         const MCInstrInfo *MII, const MCInstPrinter *IP,
2660b57cec5SDimitry Andric                         MCAsmParserSemaCallback &SI) override;
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric   bool parseExpression(const MCExpr *&Res);
2690b57cec5SDimitry Andric   bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
270e8d8bef9SDimitry Andric   bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
271e8d8bef9SDimitry Andric                         AsmTypeInfo *TypeInfo) override;
2720b57cec5SDimitry Andric   bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
2730b57cec5SDimitry Andric   bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
2740b57cec5SDimitry Andric                              SMLoc &EndLoc) override;
2750b57cec5SDimitry Andric   bool parseAbsoluteExpression(int64_t &Res) override;
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric   /// Parse a floating point expression using the float \p Semantics
2780b57cec5SDimitry Andric   /// and set \p Res to the value.
2790b57cec5SDimitry Andric   bool parseRealValue(const fltSemantics &Semantics, APInt &Res);
2800b57cec5SDimitry Andric 
2810b57cec5SDimitry Andric   /// Parse an identifier or string (as a quoted identifier)
2820b57cec5SDimitry Andric   /// and set \p Res to the identifier contents.
2830b57cec5SDimitry Andric   bool parseIdentifier(StringRef &Res) override;
2840b57cec5SDimitry Andric   void eatToEndOfStatement() override;
2850b57cec5SDimitry Andric 
2860b57cec5SDimitry Andric   bool checkForValidSection() override;
2870b57cec5SDimitry Andric 
2880b57cec5SDimitry Andric   /// }
2890b57cec5SDimitry Andric 
2900b57cec5SDimitry Andric private:
2910b57cec5SDimitry Andric   bool parseCurlyBlockScope(SmallVectorImpl<AsmRewrite>& AsmStrRewrites);
2925ffd83dbSDimitry Andric   bool parseCppHashLineFilenameComment(SMLoc L, bool SaveLocInfo = true);
2930b57cec5SDimitry Andric 
2940b57cec5SDimitry Andric   void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
2950b57cec5SDimitry Andric                         ArrayRef<MCAsmMacroParameter> Parameters);
2960b57cec5SDimitry Andric   bool expandMacro(raw_svector_ostream &OS, StringRef Body,
2970b57cec5SDimitry Andric                    ArrayRef<MCAsmMacroParameter> Parameters,
2980b57cec5SDimitry Andric                    ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable,
2990b57cec5SDimitry Andric                    SMLoc L);
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric   /// Are macros enabled in the parser?
3020b57cec5SDimitry Andric   bool areMacrosEnabled() {return MacrosEnabledFlag;}
3030b57cec5SDimitry Andric 
3040b57cec5SDimitry Andric   /// Control a flag in the parser that enables or disables macros.
3050b57cec5SDimitry Andric   void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
3060b57cec5SDimitry Andric 
3070b57cec5SDimitry Andric   /// Are we inside a macro instantiation?
3080b57cec5SDimitry Andric   bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
3090b57cec5SDimitry Andric 
3100b57cec5SDimitry Andric   /// Handle entry to macro instantiation.
3110b57cec5SDimitry Andric   ///
3120b57cec5SDimitry Andric   /// \param M The macro.
3130b57cec5SDimitry Andric   /// \param NameLoc Instantiation location.
3140b57cec5SDimitry Andric   bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
3150b57cec5SDimitry Andric 
3160b57cec5SDimitry Andric   /// Handle exit from macro instantiation.
3170b57cec5SDimitry Andric   void handleMacroExit();
3180b57cec5SDimitry Andric 
3190b57cec5SDimitry Andric   /// Extract AsmTokens for a macro argument.
3200b57cec5SDimitry Andric   bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric   /// Parse all macro arguments for a given macro.
3230b57cec5SDimitry Andric   bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
3240b57cec5SDimitry Andric 
3250b57cec5SDimitry Andric   void printMacroInstantiations();
3260b57cec5SDimitry Andric   void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
327bdd1243dSDimitry Andric                     SMRange Range = std::nullopt) const {
3280b57cec5SDimitry Andric     ArrayRef<SMRange> Ranges(Range);
3290b57cec5SDimitry Andric     SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
3300b57cec5SDimitry Andric   }
3310b57cec5SDimitry Andric   static void DiagHandler(const SMDiagnostic &Diag, void *Context);
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric   /// Enter the specified file. This returns true on failure.
3340b57cec5SDimitry Andric   bool enterIncludeFile(const std::string &Filename);
3350b57cec5SDimitry Andric 
3360b57cec5SDimitry Andric   /// Process the specified file for the .incbin directive.
3370b57cec5SDimitry Andric   /// This returns true on failure.
3380b57cec5SDimitry Andric   bool processIncbinFile(const std::string &Filename, int64_t Skip = 0,
3390b57cec5SDimitry Andric                          const MCExpr *Count = nullptr, SMLoc Loc = SMLoc());
3400b57cec5SDimitry Andric 
3410b57cec5SDimitry Andric   /// Reset the current lexer position to that given by \p Loc. The
3420b57cec5SDimitry Andric   /// current token is not set; clients should ensure Lex() is called
3430b57cec5SDimitry Andric   /// subsequently.
3440b57cec5SDimitry Andric   ///
3450b57cec5SDimitry Andric   /// \param InBuffer If not 0, should be the known buffer id that contains the
3460b57cec5SDimitry Andric   /// location.
3470b57cec5SDimitry Andric   void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
3480b57cec5SDimitry Andric 
3490b57cec5SDimitry Andric   /// Parse up to the end of statement and a return the contents from the
3500b57cec5SDimitry Andric   /// current token until the end of the statement; the current token on exit
3510b57cec5SDimitry Andric   /// will be either the EndOfStatement or EOF.
3520b57cec5SDimitry Andric   StringRef parseStringToEndOfStatement() override;
3530b57cec5SDimitry Andric 
3540b57cec5SDimitry Andric   /// Parse until the end of a statement or a comma is encountered,
3550b57cec5SDimitry Andric   /// return the contents from the current token up to the end or comma.
3560b57cec5SDimitry Andric   StringRef parseStringToComma();
3570b57cec5SDimitry Andric 
3580eae32dcSDimitry Andric   enum class AssignmentKind {
3590eae32dcSDimitry Andric     Set,
3600eae32dcSDimitry Andric     Equiv,
3610eae32dcSDimitry Andric     Equal,
3620eae32dcSDimitry Andric     LTOSetConditional,
3630eae32dcSDimitry Andric   };
3640eae32dcSDimitry Andric 
3650eae32dcSDimitry Andric   bool parseAssignment(StringRef Name, AssignmentKind Kind);
3660b57cec5SDimitry Andric 
3670b57cec5SDimitry Andric   unsigned getBinOpPrecedence(AsmToken::TokenKind K,
3680b57cec5SDimitry Andric                               MCBinaryExpr::Opcode &Kind);
3690b57cec5SDimitry Andric 
3700b57cec5SDimitry Andric   bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
3710b57cec5SDimitry Andric   bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
3720b57cec5SDimitry Andric   bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
3730b57cec5SDimitry Andric 
3740b57cec5SDimitry Andric   bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
3750b57cec5SDimitry Andric 
3760b57cec5SDimitry Andric   bool parseCVFunctionId(int64_t &FunctionId, StringRef DirectiveName);
3770b57cec5SDimitry Andric   bool parseCVFileId(int64_t &FileId, StringRef DirectiveName);
3780b57cec5SDimitry Andric 
3790b57cec5SDimitry Andric   // Generic (target and platform independent) directive parsing.
3800b57cec5SDimitry Andric   enum DirectiveKind {
3810b57cec5SDimitry Andric     DK_NO_DIRECTIVE, // Placeholder
3820b57cec5SDimitry Andric     DK_SET,
3830b57cec5SDimitry Andric     DK_EQU,
3840b57cec5SDimitry Andric     DK_EQUIV,
3850b57cec5SDimitry Andric     DK_ASCII,
3860b57cec5SDimitry Andric     DK_ASCIZ,
3870b57cec5SDimitry Andric     DK_STRING,
3880b57cec5SDimitry Andric     DK_BYTE,
3890b57cec5SDimitry Andric     DK_SHORT,
3900b57cec5SDimitry Andric     DK_RELOC,
3910b57cec5SDimitry Andric     DK_VALUE,
3920b57cec5SDimitry Andric     DK_2BYTE,
3930b57cec5SDimitry Andric     DK_LONG,
3940b57cec5SDimitry Andric     DK_INT,
3950b57cec5SDimitry Andric     DK_4BYTE,
3960b57cec5SDimitry Andric     DK_QUAD,
3970b57cec5SDimitry Andric     DK_8BYTE,
3980b57cec5SDimitry Andric     DK_OCTA,
3990b57cec5SDimitry Andric     DK_DC,
4000b57cec5SDimitry Andric     DK_DC_A,
4010b57cec5SDimitry Andric     DK_DC_B,
4020b57cec5SDimitry Andric     DK_DC_D,
4030b57cec5SDimitry Andric     DK_DC_L,
4040b57cec5SDimitry Andric     DK_DC_S,
4050b57cec5SDimitry Andric     DK_DC_W,
4060b57cec5SDimitry Andric     DK_DC_X,
4070b57cec5SDimitry Andric     DK_DCB,
4080b57cec5SDimitry Andric     DK_DCB_B,
4090b57cec5SDimitry Andric     DK_DCB_D,
4100b57cec5SDimitry Andric     DK_DCB_L,
4110b57cec5SDimitry Andric     DK_DCB_S,
4120b57cec5SDimitry Andric     DK_DCB_W,
4130b57cec5SDimitry Andric     DK_DCB_X,
4140b57cec5SDimitry Andric     DK_DS,
4150b57cec5SDimitry Andric     DK_DS_B,
4160b57cec5SDimitry Andric     DK_DS_D,
4170b57cec5SDimitry Andric     DK_DS_L,
4180b57cec5SDimitry Andric     DK_DS_P,
4190b57cec5SDimitry Andric     DK_DS_S,
4200b57cec5SDimitry Andric     DK_DS_W,
4210b57cec5SDimitry Andric     DK_DS_X,
4220b57cec5SDimitry Andric     DK_SINGLE,
4230b57cec5SDimitry Andric     DK_FLOAT,
4240b57cec5SDimitry Andric     DK_DOUBLE,
4250b57cec5SDimitry Andric     DK_ALIGN,
4260b57cec5SDimitry Andric     DK_ALIGN32,
4270b57cec5SDimitry Andric     DK_BALIGN,
4280b57cec5SDimitry Andric     DK_BALIGNW,
4290b57cec5SDimitry Andric     DK_BALIGNL,
4300b57cec5SDimitry Andric     DK_P2ALIGN,
4310b57cec5SDimitry Andric     DK_P2ALIGNW,
4320b57cec5SDimitry Andric     DK_P2ALIGNL,
4330b57cec5SDimitry Andric     DK_ORG,
4340b57cec5SDimitry Andric     DK_FILL,
4350b57cec5SDimitry Andric     DK_ENDR,
4360b57cec5SDimitry Andric     DK_BUNDLE_ALIGN_MODE,
4370b57cec5SDimitry Andric     DK_BUNDLE_LOCK,
4380b57cec5SDimitry Andric     DK_BUNDLE_UNLOCK,
4390b57cec5SDimitry Andric     DK_ZERO,
4400b57cec5SDimitry Andric     DK_EXTERN,
4410b57cec5SDimitry Andric     DK_GLOBL,
4420b57cec5SDimitry Andric     DK_GLOBAL,
4430b57cec5SDimitry Andric     DK_LAZY_REFERENCE,
4440b57cec5SDimitry Andric     DK_NO_DEAD_STRIP,
4450b57cec5SDimitry Andric     DK_SYMBOL_RESOLVER,
4460b57cec5SDimitry Andric     DK_PRIVATE_EXTERN,
4470b57cec5SDimitry Andric     DK_REFERENCE,
4480b57cec5SDimitry Andric     DK_WEAK_DEFINITION,
4490b57cec5SDimitry Andric     DK_WEAK_REFERENCE,
4500b57cec5SDimitry Andric     DK_WEAK_DEF_CAN_BE_HIDDEN,
4510b57cec5SDimitry Andric     DK_COLD,
4520b57cec5SDimitry Andric     DK_COMM,
4530b57cec5SDimitry Andric     DK_COMMON,
4540b57cec5SDimitry Andric     DK_LCOMM,
4550b57cec5SDimitry Andric     DK_ABORT,
4560b57cec5SDimitry Andric     DK_INCLUDE,
4570b57cec5SDimitry Andric     DK_INCBIN,
4580b57cec5SDimitry Andric     DK_CODE16,
4590b57cec5SDimitry Andric     DK_CODE16GCC,
4600b57cec5SDimitry Andric     DK_REPT,
4610b57cec5SDimitry Andric     DK_IRP,
4620b57cec5SDimitry Andric     DK_IRPC,
4630b57cec5SDimitry Andric     DK_IF,
4640b57cec5SDimitry Andric     DK_IFEQ,
4650b57cec5SDimitry Andric     DK_IFGE,
4660b57cec5SDimitry Andric     DK_IFGT,
4670b57cec5SDimitry Andric     DK_IFLE,
4680b57cec5SDimitry Andric     DK_IFLT,
4690b57cec5SDimitry Andric     DK_IFNE,
4700b57cec5SDimitry Andric     DK_IFB,
4710b57cec5SDimitry Andric     DK_IFNB,
4720b57cec5SDimitry Andric     DK_IFC,
4730b57cec5SDimitry Andric     DK_IFEQS,
4740b57cec5SDimitry Andric     DK_IFNC,
4750b57cec5SDimitry Andric     DK_IFNES,
4760b57cec5SDimitry Andric     DK_IFDEF,
4770b57cec5SDimitry Andric     DK_IFNDEF,
4780b57cec5SDimitry Andric     DK_IFNOTDEF,
4790b57cec5SDimitry Andric     DK_ELSEIF,
4800b57cec5SDimitry Andric     DK_ELSE,
4810b57cec5SDimitry Andric     DK_ENDIF,
4820b57cec5SDimitry Andric     DK_SPACE,
4830b57cec5SDimitry Andric     DK_SKIP,
4840b57cec5SDimitry Andric     DK_FILE,
4850b57cec5SDimitry Andric     DK_LINE,
4860b57cec5SDimitry Andric     DK_LOC,
4870b57cec5SDimitry Andric     DK_STABS,
4880b57cec5SDimitry Andric     DK_CV_FILE,
4890b57cec5SDimitry Andric     DK_CV_FUNC_ID,
4900b57cec5SDimitry Andric     DK_CV_INLINE_SITE_ID,
4910b57cec5SDimitry Andric     DK_CV_LOC,
4920b57cec5SDimitry Andric     DK_CV_LINETABLE,
4930b57cec5SDimitry Andric     DK_CV_INLINE_LINETABLE,
4940b57cec5SDimitry Andric     DK_CV_DEF_RANGE,
4950b57cec5SDimitry Andric     DK_CV_STRINGTABLE,
4960b57cec5SDimitry Andric     DK_CV_STRING,
4970b57cec5SDimitry Andric     DK_CV_FILECHECKSUMS,
4980b57cec5SDimitry Andric     DK_CV_FILECHECKSUM_OFFSET,
4990b57cec5SDimitry Andric     DK_CV_FPO_DATA,
5000b57cec5SDimitry Andric     DK_CFI_SECTIONS,
5010b57cec5SDimitry Andric     DK_CFI_STARTPROC,
5020b57cec5SDimitry Andric     DK_CFI_ENDPROC,
5030b57cec5SDimitry Andric     DK_CFI_DEF_CFA,
5040b57cec5SDimitry Andric     DK_CFI_DEF_CFA_OFFSET,
5050b57cec5SDimitry Andric     DK_CFI_ADJUST_CFA_OFFSET,
5060b57cec5SDimitry Andric     DK_CFI_DEF_CFA_REGISTER,
507fe6060f1SDimitry Andric     DK_CFI_LLVM_DEF_ASPACE_CFA,
5080b57cec5SDimitry Andric     DK_CFI_OFFSET,
5090b57cec5SDimitry Andric     DK_CFI_REL_OFFSET,
5100b57cec5SDimitry Andric     DK_CFI_PERSONALITY,
5110b57cec5SDimitry Andric     DK_CFI_LSDA,
5120b57cec5SDimitry Andric     DK_CFI_REMEMBER_STATE,
5130b57cec5SDimitry Andric     DK_CFI_RESTORE_STATE,
5140b57cec5SDimitry Andric     DK_CFI_SAME_VALUE,
5150b57cec5SDimitry Andric     DK_CFI_RESTORE,
5160b57cec5SDimitry Andric     DK_CFI_ESCAPE,
5170b57cec5SDimitry Andric     DK_CFI_RETURN_COLUMN,
5180b57cec5SDimitry Andric     DK_CFI_SIGNAL_FRAME,
5190b57cec5SDimitry Andric     DK_CFI_UNDEFINED,
5200b57cec5SDimitry Andric     DK_CFI_REGISTER,
5210b57cec5SDimitry Andric     DK_CFI_WINDOW_SAVE,
5220b57cec5SDimitry Andric     DK_CFI_B_KEY_FRAME,
5230b57cec5SDimitry Andric     DK_MACROS_ON,
5240b57cec5SDimitry Andric     DK_MACROS_OFF,
5250b57cec5SDimitry Andric     DK_ALTMACRO,
5260b57cec5SDimitry Andric     DK_NOALTMACRO,
5270b57cec5SDimitry Andric     DK_MACRO,
5280b57cec5SDimitry Andric     DK_EXITM,
5290b57cec5SDimitry Andric     DK_ENDM,
5300b57cec5SDimitry Andric     DK_ENDMACRO,
5310b57cec5SDimitry Andric     DK_PURGEM,
5320b57cec5SDimitry Andric     DK_SLEB128,
5330b57cec5SDimitry Andric     DK_ULEB128,
5340b57cec5SDimitry Andric     DK_ERR,
5350b57cec5SDimitry Andric     DK_ERROR,
5360b57cec5SDimitry Andric     DK_WARNING,
5370b57cec5SDimitry Andric     DK_PRINT,
5380b57cec5SDimitry Andric     DK_ADDRSIG,
5390b57cec5SDimitry Andric     DK_ADDRSIG_SYM,
540e8d8bef9SDimitry Andric     DK_PSEUDO_PROBE,
541fe6060f1SDimitry Andric     DK_LTO_DISCARD,
5420eae32dcSDimitry Andric     DK_LTO_SET_CONDITIONAL,
54381ad6265SDimitry Andric     DK_CFI_MTE_TAGGED_FRAME,
544bdd1243dSDimitry Andric     DK_MEMTAG,
5450b57cec5SDimitry Andric     DK_END
5460b57cec5SDimitry Andric   };
5470b57cec5SDimitry Andric 
5480b57cec5SDimitry Andric   /// Maps directive name --> DirectiveKind enum, for
5490b57cec5SDimitry Andric   /// directives parsed by this class.
5500b57cec5SDimitry Andric   StringMap<DirectiveKind> DirectiveKindMap;
5510b57cec5SDimitry Andric 
5528bcb0991SDimitry Andric   // Codeview def_range type parsing.
5538bcb0991SDimitry Andric   enum CVDefRangeType {
5548bcb0991SDimitry Andric     CVDR_DEFRANGE = 0, // Placeholder
5558bcb0991SDimitry Andric     CVDR_DEFRANGE_REGISTER,
5568bcb0991SDimitry Andric     CVDR_DEFRANGE_FRAMEPOINTER_REL,
5578bcb0991SDimitry Andric     CVDR_DEFRANGE_SUBFIELD_REGISTER,
5588bcb0991SDimitry Andric     CVDR_DEFRANGE_REGISTER_REL
5598bcb0991SDimitry Andric   };
5608bcb0991SDimitry Andric 
5618bcb0991SDimitry Andric   /// Maps Codeview def_range types --> CVDefRangeType enum, for
5628bcb0991SDimitry Andric   /// Codeview def_range types parsed by this class.
5638bcb0991SDimitry Andric   StringMap<CVDefRangeType> CVDefRangeTypeMap;
5648bcb0991SDimitry Andric 
5650b57cec5SDimitry Andric   // ".ascii", ".asciz", ".string"
5660b57cec5SDimitry Andric   bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
5670b57cec5SDimitry Andric   bool parseDirectiveReloc(SMLoc DirectiveLoc); // ".reloc"
5680b57cec5SDimitry Andric   bool parseDirectiveValue(StringRef IDVal,
5690b57cec5SDimitry Andric                            unsigned Size);       // ".byte", ".long", ...
5700b57cec5SDimitry Andric   bool parseDirectiveOctaValue(StringRef IDVal); // ".octa", ...
5710b57cec5SDimitry Andric   bool parseDirectiveRealValue(StringRef IDVal,
5720b57cec5SDimitry Andric                                const fltSemantics &); // ".single", ...
5730b57cec5SDimitry Andric   bool parseDirectiveFill(); // ".fill"
5740b57cec5SDimitry Andric   bool parseDirectiveZero(); // ".zero"
5750eae32dcSDimitry Andric   // ".set", ".equ", ".equiv", ".lto_set_conditional"
5760eae32dcSDimitry Andric   bool parseDirectiveSet(StringRef IDVal, AssignmentKind Kind);
5770b57cec5SDimitry Andric   bool parseDirectiveOrg(); // ".org"
5780b57cec5SDimitry Andric   // ".align{,32}", ".p2align{,w,l}"
5790b57cec5SDimitry Andric   bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
5800b57cec5SDimitry Andric 
5810b57cec5SDimitry Andric   // ".file", ".line", ".loc", ".stabs"
5820b57cec5SDimitry Andric   bool parseDirectiveFile(SMLoc DirectiveLoc);
5830b57cec5SDimitry Andric   bool parseDirectiveLine();
5840b57cec5SDimitry Andric   bool parseDirectiveLoc();
5850b57cec5SDimitry Andric   bool parseDirectiveStabs();
5860b57cec5SDimitry Andric 
5870b57cec5SDimitry Andric   // ".cv_file", ".cv_func_id", ".cv_inline_site_id", ".cv_loc", ".cv_linetable",
5880b57cec5SDimitry Andric   // ".cv_inline_linetable", ".cv_def_range", ".cv_string"
5890b57cec5SDimitry Andric   bool parseDirectiveCVFile();
5900b57cec5SDimitry Andric   bool parseDirectiveCVFuncId();
5910b57cec5SDimitry Andric   bool parseDirectiveCVInlineSiteId();
5920b57cec5SDimitry Andric   bool parseDirectiveCVLoc();
5930b57cec5SDimitry Andric   bool parseDirectiveCVLinetable();
5940b57cec5SDimitry Andric   bool parseDirectiveCVInlineLinetable();
5950b57cec5SDimitry Andric   bool parseDirectiveCVDefRange();
5960b57cec5SDimitry Andric   bool parseDirectiveCVString();
5970b57cec5SDimitry Andric   bool parseDirectiveCVStringTable();
5980b57cec5SDimitry Andric   bool parseDirectiveCVFileChecksums();
5990b57cec5SDimitry Andric   bool parseDirectiveCVFileChecksumOffset();
6000b57cec5SDimitry Andric   bool parseDirectiveCVFPOData();
6010b57cec5SDimitry Andric 
6020b57cec5SDimitry Andric   // .cfi directives
6030b57cec5SDimitry Andric   bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
604*06c3fb27SDimitry Andric   bool parseDirectiveCFIWindowSave(SMLoc DirectiveLoc);
6050b57cec5SDimitry Andric   bool parseDirectiveCFISections();
6060b57cec5SDimitry Andric   bool parseDirectiveCFIStartProc();
6070b57cec5SDimitry Andric   bool parseDirectiveCFIEndProc();
608*06c3fb27SDimitry Andric   bool parseDirectiveCFIDefCfaOffset(SMLoc DirectiveLoc);
6090b57cec5SDimitry Andric   bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
610*06c3fb27SDimitry Andric   bool parseDirectiveCFIAdjustCfaOffset(SMLoc DirectiveLoc);
6110b57cec5SDimitry Andric   bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
612fe6060f1SDimitry Andric   bool parseDirectiveCFILLVMDefAspaceCfa(SMLoc DirectiveLoc);
6130b57cec5SDimitry Andric   bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
6140b57cec5SDimitry Andric   bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
6150b57cec5SDimitry Andric   bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
616*06c3fb27SDimitry Andric   bool parseDirectiveCFIRememberState(SMLoc DirectiveLoc);
617*06c3fb27SDimitry Andric   bool parseDirectiveCFIRestoreState(SMLoc DirectiveLoc);
6180b57cec5SDimitry Andric   bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
6190b57cec5SDimitry Andric   bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
620*06c3fb27SDimitry Andric   bool parseDirectiveCFIEscape(SMLoc DirectiveLoc);
6210b57cec5SDimitry Andric   bool parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc);
622*06c3fb27SDimitry Andric   bool parseDirectiveCFISignalFrame(SMLoc DirectiveLoc);
6230b57cec5SDimitry Andric   bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
6240b57cec5SDimitry Andric 
6250b57cec5SDimitry Andric   // macro directives
6260b57cec5SDimitry Andric   bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
6270b57cec5SDimitry Andric   bool parseDirectiveExitMacro(StringRef Directive);
6280b57cec5SDimitry Andric   bool parseDirectiveEndMacro(StringRef Directive);
6290b57cec5SDimitry Andric   bool parseDirectiveMacro(SMLoc DirectiveLoc);
6300b57cec5SDimitry Andric   bool parseDirectiveMacrosOnOff(StringRef Directive);
6310b57cec5SDimitry Andric   // alternate macro mode directives
6320b57cec5SDimitry Andric   bool parseDirectiveAltmacro(StringRef Directive);
6330b57cec5SDimitry Andric   // ".bundle_align_mode"
6340b57cec5SDimitry Andric   bool parseDirectiveBundleAlignMode();
6350b57cec5SDimitry Andric   // ".bundle_lock"
6360b57cec5SDimitry Andric   bool parseDirectiveBundleLock();
6370b57cec5SDimitry Andric   // ".bundle_unlock"
6380b57cec5SDimitry Andric   bool parseDirectiveBundleUnlock();
6390b57cec5SDimitry Andric 
6400b57cec5SDimitry Andric   // ".space", ".skip"
6410b57cec5SDimitry Andric   bool parseDirectiveSpace(StringRef IDVal);
6420b57cec5SDimitry Andric 
6430b57cec5SDimitry Andric   // ".dcb"
6440b57cec5SDimitry Andric   bool parseDirectiveDCB(StringRef IDVal, unsigned Size);
6450b57cec5SDimitry Andric   bool parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &);
6460b57cec5SDimitry Andric   // ".ds"
6470b57cec5SDimitry Andric   bool parseDirectiveDS(StringRef IDVal, unsigned Size);
6480b57cec5SDimitry Andric 
6490b57cec5SDimitry Andric   // .sleb128 (Signed=true) and .uleb128 (Signed=false)
6500b57cec5SDimitry Andric   bool parseDirectiveLEB128(bool Signed);
6510b57cec5SDimitry Andric 
6520b57cec5SDimitry Andric   /// Parse a directive like ".globl" which
6530b57cec5SDimitry Andric   /// accepts a single symbol (which should be a label or an external).
6540b57cec5SDimitry Andric   bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
6550b57cec5SDimitry Andric 
6560b57cec5SDimitry Andric   bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
6570b57cec5SDimitry Andric 
6580b57cec5SDimitry Andric   bool parseDirectiveAbort(); // ".abort"
6590b57cec5SDimitry Andric   bool parseDirectiveInclude(); // ".include"
6600b57cec5SDimitry Andric   bool parseDirectiveIncbin(); // ".incbin"
6610b57cec5SDimitry Andric 
6620b57cec5SDimitry Andric   // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
6630b57cec5SDimitry Andric   bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
6640b57cec5SDimitry Andric   // ".ifb" or ".ifnb", depending on ExpectBlank.
6650b57cec5SDimitry Andric   bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
6660b57cec5SDimitry Andric   // ".ifc" or ".ifnc", depending on ExpectEqual.
6670b57cec5SDimitry Andric   bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
6680b57cec5SDimitry Andric   // ".ifeqs" or ".ifnes", depending on ExpectEqual.
6690b57cec5SDimitry Andric   bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual);
6700b57cec5SDimitry Andric   // ".ifdef" or ".ifndef", depending on expect_defined
6710b57cec5SDimitry Andric   bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
6720b57cec5SDimitry Andric   bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
6730b57cec5SDimitry Andric   bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
6740b57cec5SDimitry Andric   bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
6750b57cec5SDimitry Andric   bool parseEscapedString(std::string &Data) override;
6765ffd83dbSDimitry Andric   bool parseAngleBracketString(std::string &Data) override;
6770b57cec5SDimitry Andric 
6780b57cec5SDimitry Andric   const MCExpr *applyModifierToExpr(const MCExpr *E,
6790b57cec5SDimitry Andric                                     MCSymbolRefExpr::VariantKind Variant);
6800b57cec5SDimitry Andric 
6810b57cec5SDimitry Andric   // Macro-like directives
6820b57cec5SDimitry Andric   MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
6830b57cec5SDimitry Andric   void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
6840b57cec5SDimitry Andric                                 raw_svector_ostream &OS);
6850b57cec5SDimitry Andric   bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
6860b57cec5SDimitry Andric   bool parseDirectiveIrp(SMLoc DirectiveLoc);  // ".irp"
6870b57cec5SDimitry Andric   bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
6880b57cec5SDimitry Andric   bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
6890b57cec5SDimitry Andric 
6900b57cec5SDimitry Andric   // "_emit" or "__emit"
6910b57cec5SDimitry Andric   bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
6920b57cec5SDimitry Andric                             size_t Len);
6930b57cec5SDimitry Andric 
6940b57cec5SDimitry Andric   // "align"
6950b57cec5SDimitry Andric   bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
6960b57cec5SDimitry Andric 
6970b57cec5SDimitry Andric   // "end"
6980b57cec5SDimitry Andric   bool parseDirectiveEnd(SMLoc DirectiveLoc);
6990b57cec5SDimitry Andric 
7000b57cec5SDimitry Andric   // ".err" or ".error"
7010b57cec5SDimitry Andric   bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
7020b57cec5SDimitry Andric 
7030b57cec5SDimitry Andric   // ".warning"
7040b57cec5SDimitry Andric   bool parseDirectiveWarning(SMLoc DirectiveLoc);
7050b57cec5SDimitry Andric 
7060b57cec5SDimitry Andric   // .print <double-quotes-string>
7070b57cec5SDimitry Andric   bool parseDirectivePrint(SMLoc DirectiveLoc);
7080b57cec5SDimitry Andric 
709e8d8bef9SDimitry Andric   // .pseudoprobe
710e8d8bef9SDimitry Andric   bool parseDirectivePseudoProbe();
711e8d8bef9SDimitry Andric 
712fe6060f1SDimitry Andric   // ".lto_discard"
713fe6060f1SDimitry Andric   bool parseDirectiveLTODiscard();
714fe6060f1SDimitry Andric 
7150b57cec5SDimitry Andric   // Directives to support address-significance tables.
7160b57cec5SDimitry Andric   bool parseDirectiveAddrsig();
7170b57cec5SDimitry Andric   bool parseDirectiveAddrsigSym();
7180b57cec5SDimitry Andric 
7190b57cec5SDimitry Andric   void initializeDirectiveKindMap();
7208bcb0991SDimitry Andric   void initializeCVDefRangeTypeMap();
7210b57cec5SDimitry Andric };
7220b57cec5SDimitry Andric 
723fe6060f1SDimitry Andric class HLASMAsmParser final : public AsmParser {
724fe6060f1SDimitry Andric private:
725fe6060f1SDimitry Andric   MCAsmLexer &Lexer;
726fe6060f1SDimitry Andric   MCStreamer &Out;
727fe6060f1SDimitry Andric 
728fe6060f1SDimitry Andric   void lexLeadingSpaces() {
729fe6060f1SDimitry Andric     while (Lexer.is(AsmToken::Space))
730fe6060f1SDimitry Andric       Lexer.Lex();
731fe6060f1SDimitry Andric   }
732fe6060f1SDimitry Andric 
733fe6060f1SDimitry Andric   bool parseAsHLASMLabel(ParseStatementInfo &Info, MCAsmParserSemaCallback *SI);
734fe6060f1SDimitry Andric   bool parseAsMachineInstruction(ParseStatementInfo &Info,
735fe6060f1SDimitry Andric                                  MCAsmParserSemaCallback *SI);
736fe6060f1SDimitry Andric 
737fe6060f1SDimitry Andric public:
738fe6060f1SDimitry Andric   HLASMAsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
739fe6060f1SDimitry Andric                  const MCAsmInfo &MAI, unsigned CB = 0)
740fe6060f1SDimitry Andric       : AsmParser(SM, Ctx, Out, MAI, CB), Lexer(getLexer()), Out(Out) {
741fe6060f1SDimitry Andric     Lexer.setSkipSpace(false);
742fe6060f1SDimitry Andric     Lexer.setAllowHashInIdentifier(true);
743fe6060f1SDimitry Andric     Lexer.setLexHLASMIntegers(true);
744fe6060f1SDimitry Andric     Lexer.setLexHLASMStrings(true);
745fe6060f1SDimitry Andric   }
746fe6060f1SDimitry Andric 
747fe6060f1SDimitry Andric   ~HLASMAsmParser() { Lexer.setSkipSpace(true); }
748fe6060f1SDimitry Andric 
749fe6060f1SDimitry Andric   bool parseStatement(ParseStatementInfo &Info,
750fe6060f1SDimitry Andric                       MCAsmParserSemaCallback *SI) override;
751fe6060f1SDimitry Andric };
752fe6060f1SDimitry Andric 
7530b57cec5SDimitry Andric } // end anonymous namespace
7540b57cec5SDimitry Andric 
7550b57cec5SDimitry Andric namespace llvm {
7560b57cec5SDimitry Andric 
757bdd1243dSDimitry Andric extern cl::opt<unsigned> AsmMacroMaxNestingDepth;
758bdd1243dSDimitry Andric 
7590b57cec5SDimitry Andric extern MCAsmParserExtension *createDarwinAsmParser();
7600b57cec5SDimitry Andric extern MCAsmParserExtension *createELFAsmParser();
7610b57cec5SDimitry Andric extern MCAsmParserExtension *createCOFFAsmParser();
762349cc55cSDimitry Andric extern MCAsmParserExtension *createGOFFAsmParser();
763fe6060f1SDimitry Andric extern MCAsmParserExtension *createXCOFFAsmParser();
7640b57cec5SDimitry Andric extern MCAsmParserExtension *createWasmAsmParser();
7650b57cec5SDimitry Andric 
7660b57cec5SDimitry Andric } // end namespace llvm
7670b57cec5SDimitry Andric 
7680b57cec5SDimitry Andric enum { DEFAULT_ADDRSPACE = 0 };
7690b57cec5SDimitry Andric 
7700b57cec5SDimitry Andric AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
7710b57cec5SDimitry Andric                      const MCAsmInfo &MAI, unsigned CB = 0)
7720b57cec5SDimitry Andric     : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM),
7730b57cec5SDimitry Andric       CurBuffer(CB ? CB : SM.getMainFileID()), MacrosEnabledFlag(true) {
7740b57cec5SDimitry Andric   HadError = false;
7750b57cec5SDimitry Andric   // Save the old handler.
7760b57cec5SDimitry Andric   SavedDiagHandler = SrcMgr.getDiagHandler();
7770b57cec5SDimitry Andric   SavedDiagContext = SrcMgr.getDiagContext();
7780b57cec5SDimitry Andric   // Set our own handler which calls the saved handler.
7790b57cec5SDimitry Andric   SrcMgr.setDiagHandler(DiagHandler, this);
7800b57cec5SDimitry Andric   Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
781e8d8bef9SDimitry Andric   // Make MCStreamer aware of the StartTokLoc for locations in diagnostics.
782e8d8bef9SDimitry Andric   Out.setStartTokLocPtr(&StartTokLoc);
7830b57cec5SDimitry Andric 
7840b57cec5SDimitry Andric   // Initialize the platform / file format parser.
785fe6060f1SDimitry Andric   switch (Ctx.getObjectFileType()) {
786fe6060f1SDimitry Andric   case MCContext::IsCOFF:
7870b57cec5SDimitry Andric     PlatformParser.reset(createCOFFAsmParser());
7880b57cec5SDimitry Andric     break;
789fe6060f1SDimitry Andric   case MCContext::IsMachO:
7900b57cec5SDimitry Andric     PlatformParser.reset(createDarwinAsmParser());
7910b57cec5SDimitry Andric     IsDarwin = true;
7920b57cec5SDimitry Andric     break;
793fe6060f1SDimitry Andric   case MCContext::IsELF:
7940b57cec5SDimitry Andric     PlatformParser.reset(createELFAsmParser());
7950b57cec5SDimitry Andric     break;
796fe6060f1SDimitry Andric   case MCContext::IsGOFF:
797349cc55cSDimitry Andric     PlatformParser.reset(createGOFFAsmParser());
798349cc55cSDimitry Andric     break;
79981ad6265SDimitry Andric   case MCContext::IsSPIRV:
80081ad6265SDimitry Andric     report_fatal_error(
80181ad6265SDimitry Andric         "Need to implement createSPIRVAsmParser for SPIRV format.");
80281ad6265SDimitry Andric     break;
803fe6060f1SDimitry Andric   case MCContext::IsWasm:
8040b57cec5SDimitry Andric     PlatformParser.reset(createWasmAsmParser());
8050b57cec5SDimitry Andric     break;
806fe6060f1SDimitry Andric   case MCContext::IsXCOFF:
807fe6060f1SDimitry Andric     PlatformParser.reset(createXCOFFAsmParser());
8080b57cec5SDimitry Andric     break;
80981ad6265SDimitry Andric   case MCContext::IsDXContainer:
81081ad6265SDimitry Andric     llvm_unreachable("DXContainer is not supported yet");
81181ad6265SDimitry Andric     break;
8120b57cec5SDimitry Andric   }
8130b57cec5SDimitry Andric 
8140b57cec5SDimitry Andric   PlatformParser->Initialize(*this);
8150b57cec5SDimitry Andric   initializeDirectiveKindMap();
8168bcb0991SDimitry Andric   initializeCVDefRangeTypeMap();
8170b57cec5SDimitry Andric 
8180b57cec5SDimitry Andric   NumOfMacroInstantiations = 0;
8190b57cec5SDimitry Andric }
8200b57cec5SDimitry Andric 
8210b57cec5SDimitry Andric AsmParser::~AsmParser() {
8220b57cec5SDimitry Andric   assert((HadError || ActiveMacros.empty()) &&
8230b57cec5SDimitry Andric          "Unexpected active macro instantiation!");
8240b57cec5SDimitry Andric 
825e8d8bef9SDimitry Andric   // Remove MCStreamer's reference to the parser SMLoc.
826e8d8bef9SDimitry Andric   Out.setStartTokLocPtr(nullptr);
8270b57cec5SDimitry Andric   // Restore the saved diagnostics handler and context for use during
8280b57cec5SDimitry Andric   // finalization.
8290b57cec5SDimitry Andric   SrcMgr.setDiagHandler(SavedDiagHandler, SavedDiagContext);
8300b57cec5SDimitry Andric }
8310b57cec5SDimitry Andric 
8320b57cec5SDimitry Andric void AsmParser::printMacroInstantiations() {
8330b57cec5SDimitry Andric   // Print the active macro instantiation stack.
8340b57cec5SDimitry Andric   for (std::vector<MacroInstantiation *>::const_reverse_iterator
8350b57cec5SDimitry Andric            it = ActiveMacros.rbegin(),
8360b57cec5SDimitry Andric            ie = ActiveMacros.rend();
8370b57cec5SDimitry Andric        it != ie; ++it)
8380b57cec5SDimitry Andric     printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
8390b57cec5SDimitry Andric                  "while in macro instantiation");
8400b57cec5SDimitry Andric }
8410b57cec5SDimitry Andric 
8420b57cec5SDimitry Andric void AsmParser::Note(SMLoc L, const Twine &Msg, SMRange Range) {
8430b57cec5SDimitry Andric   printPendingErrors();
8440b57cec5SDimitry Andric   printMessage(L, SourceMgr::DK_Note, Msg, Range);
8450b57cec5SDimitry Andric   printMacroInstantiations();
8460b57cec5SDimitry Andric }
8470b57cec5SDimitry Andric 
8480b57cec5SDimitry Andric bool AsmParser::Warning(SMLoc L, const Twine &Msg, SMRange Range) {
8490b57cec5SDimitry Andric   if(getTargetParser().getTargetOptions().MCNoWarn)
8500b57cec5SDimitry Andric     return false;
8510b57cec5SDimitry Andric   if (getTargetParser().getTargetOptions().MCFatalWarnings)
8520b57cec5SDimitry Andric     return Error(L, Msg, Range);
8530b57cec5SDimitry Andric   printMessage(L, SourceMgr::DK_Warning, Msg, Range);
8540b57cec5SDimitry Andric   printMacroInstantiations();
8550b57cec5SDimitry Andric   return false;
8560b57cec5SDimitry Andric }
8570b57cec5SDimitry Andric 
8580b57cec5SDimitry Andric bool AsmParser::printError(SMLoc L, const Twine &Msg, SMRange Range) {
8590b57cec5SDimitry Andric   HadError = true;
8600b57cec5SDimitry Andric   printMessage(L, SourceMgr::DK_Error, Msg, Range);
8610b57cec5SDimitry Andric   printMacroInstantiations();
8620b57cec5SDimitry Andric   return true;
8630b57cec5SDimitry Andric }
8640b57cec5SDimitry Andric 
8650b57cec5SDimitry Andric bool AsmParser::enterIncludeFile(const std::string &Filename) {
8660b57cec5SDimitry Andric   std::string IncludedFile;
8670b57cec5SDimitry Andric   unsigned NewBuf =
8680b57cec5SDimitry Andric       SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
8690b57cec5SDimitry Andric   if (!NewBuf)
8700b57cec5SDimitry Andric     return true;
8710b57cec5SDimitry Andric 
8720b57cec5SDimitry Andric   CurBuffer = NewBuf;
8730b57cec5SDimitry Andric   Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
8740b57cec5SDimitry Andric   return false;
8750b57cec5SDimitry Andric }
8760b57cec5SDimitry Andric 
8770b57cec5SDimitry Andric /// Process the specified .incbin file by searching for it in the include paths
8780b57cec5SDimitry Andric /// then just emitting the byte contents of the file to the streamer. This
8790b57cec5SDimitry Andric /// returns true on failure.
8800b57cec5SDimitry Andric bool AsmParser::processIncbinFile(const std::string &Filename, int64_t Skip,
8810b57cec5SDimitry Andric                                   const MCExpr *Count, SMLoc Loc) {
8820b57cec5SDimitry Andric   std::string IncludedFile;
8830b57cec5SDimitry Andric   unsigned NewBuf =
8840b57cec5SDimitry Andric       SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
8850b57cec5SDimitry Andric   if (!NewBuf)
8860b57cec5SDimitry Andric     return true;
8870b57cec5SDimitry Andric 
8880b57cec5SDimitry Andric   // Pick up the bytes from the file and emit them.
8890b57cec5SDimitry Andric   StringRef Bytes = SrcMgr.getMemoryBuffer(NewBuf)->getBuffer();
8900b57cec5SDimitry Andric   Bytes = Bytes.drop_front(Skip);
8910b57cec5SDimitry Andric   if (Count) {
8920b57cec5SDimitry Andric     int64_t Res;
8930b57cec5SDimitry Andric     if (!Count->evaluateAsAbsolute(Res, getStreamer().getAssemblerPtr()))
8940b57cec5SDimitry Andric       return Error(Loc, "expected absolute expression");
8950b57cec5SDimitry Andric     if (Res < 0)
8960b57cec5SDimitry Andric       return Warning(Loc, "negative count has no effect");
8970b57cec5SDimitry Andric     Bytes = Bytes.take_front(Res);
8980b57cec5SDimitry Andric   }
8995ffd83dbSDimitry Andric   getStreamer().emitBytes(Bytes);
9000b57cec5SDimitry Andric   return false;
9010b57cec5SDimitry Andric }
9020b57cec5SDimitry Andric 
9030b57cec5SDimitry Andric void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
9040b57cec5SDimitry Andric   CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
9050b57cec5SDimitry Andric   Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
9060b57cec5SDimitry Andric                   Loc.getPointer());
9070b57cec5SDimitry Andric }
9080b57cec5SDimitry Andric 
9090b57cec5SDimitry Andric const AsmToken &AsmParser::Lex() {
9100b57cec5SDimitry Andric   if (Lexer.getTok().is(AsmToken::Error))
9110b57cec5SDimitry Andric     Error(Lexer.getErrLoc(), Lexer.getErr());
9120b57cec5SDimitry Andric 
9130b57cec5SDimitry Andric   // if it's a end of statement with a comment in it
9140b57cec5SDimitry Andric   if (getTok().is(AsmToken::EndOfStatement)) {
9150b57cec5SDimitry Andric     // if this is a line comment output it.
9160b57cec5SDimitry Andric     if (!getTok().getString().empty() && getTok().getString().front() != '\n' &&
9170b57cec5SDimitry Andric         getTok().getString().front() != '\r' && MAI.preserveAsmComments())
9180b57cec5SDimitry Andric       Out.addExplicitComment(Twine(getTok().getString()));
9190b57cec5SDimitry Andric   }
9200b57cec5SDimitry Andric 
9210b57cec5SDimitry Andric   const AsmToken *tok = &Lexer.Lex();
9220b57cec5SDimitry Andric 
9230b57cec5SDimitry Andric   // Parse comments here to be deferred until end of next statement.
9240b57cec5SDimitry Andric   while (tok->is(AsmToken::Comment)) {
9250b57cec5SDimitry Andric     if (MAI.preserveAsmComments())
9260b57cec5SDimitry Andric       Out.addExplicitComment(Twine(tok->getString()));
9270b57cec5SDimitry Andric     tok = &Lexer.Lex();
9280b57cec5SDimitry Andric   }
9290b57cec5SDimitry Andric 
9300b57cec5SDimitry Andric   if (tok->is(AsmToken::Eof)) {
9310b57cec5SDimitry Andric     // If this is the end of an included file, pop the parent file off the
9320b57cec5SDimitry Andric     // include stack.
9330b57cec5SDimitry Andric     SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
9340b57cec5SDimitry Andric     if (ParentIncludeLoc != SMLoc()) {
9350b57cec5SDimitry Andric       jumpToLoc(ParentIncludeLoc);
9360b57cec5SDimitry Andric       return Lex();
9370b57cec5SDimitry Andric     }
9380b57cec5SDimitry Andric   }
9390b57cec5SDimitry Andric 
9400b57cec5SDimitry Andric   return *tok;
9410b57cec5SDimitry Andric }
9420b57cec5SDimitry Andric 
9430b57cec5SDimitry Andric bool AsmParser::enabledGenDwarfForAssembly() {
9440b57cec5SDimitry Andric   // Check whether the user specified -g.
9450b57cec5SDimitry Andric   if (!getContext().getGenDwarfForAssembly())
9460b57cec5SDimitry Andric     return false;
9470b57cec5SDimitry Andric   // If we haven't encountered any .file directives (which would imply that
9480b57cec5SDimitry Andric   // the assembler source was produced with debug info already) then emit one
9490b57cec5SDimitry Andric   // describing the assembler source file itself.
9500b57cec5SDimitry Andric   if (getContext().getGenDwarfFileNumber() == 0) {
9510b57cec5SDimitry Andric     // Use the first #line directive for this, if any. It's preprocessed, so
9520b57cec5SDimitry Andric     // there is no checksum, and of course no source directive.
9530b57cec5SDimitry Andric     if (!FirstCppHashFilename.empty())
954bdd1243dSDimitry Andric       getContext().setMCLineTableRootFile(
955bdd1243dSDimitry Andric           /*CUID=*/0, getContext().getCompilationDir(), FirstCppHashFilename,
956bdd1243dSDimitry Andric           /*Cksum=*/std::nullopt, /*Source=*/std::nullopt);
9570b57cec5SDimitry Andric     const MCDwarfFile &RootFile =
9580b57cec5SDimitry Andric         getContext().getMCDwarfLineTable(/*CUID=*/0).getRootFile();
9595ffd83dbSDimitry Andric     getContext().setGenDwarfFileNumber(getStreamer().emitDwarfFileDirective(
9600b57cec5SDimitry Andric         /*CUID=*/0, getContext().getCompilationDir(), RootFile.Name,
9610b57cec5SDimitry Andric         RootFile.Checksum, RootFile.Source));
9620b57cec5SDimitry Andric   }
9630b57cec5SDimitry Andric   return true;
9640b57cec5SDimitry Andric }
9650b57cec5SDimitry Andric 
9660b57cec5SDimitry Andric bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
967fe6060f1SDimitry Andric   LTODiscardSymbols.clear();
968fe6060f1SDimitry Andric 
9690b57cec5SDimitry Andric   // Create the initial section, if requested.
9700b57cec5SDimitry Andric   if (!NoInitialTextSection)
971349cc55cSDimitry Andric     Out.initSections(false, getTargetParser().getSTI());
9720b57cec5SDimitry Andric 
9730b57cec5SDimitry Andric   // Prime the lexer.
9740b57cec5SDimitry Andric   Lex();
9750b57cec5SDimitry Andric 
9760b57cec5SDimitry Andric   HadError = false;
9770b57cec5SDimitry Andric   AsmCond StartingCondState = TheCondState;
9780b57cec5SDimitry Andric   SmallVector<AsmRewrite, 4> AsmStrRewrites;
9790b57cec5SDimitry Andric 
9800b57cec5SDimitry Andric   // If we are generating dwarf for assembly source files save the initial text
9810b57cec5SDimitry Andric   // section.  (Don't use enabledGenDwarfForAssembly() here, as we aren't
9820b57cec5SDimitry Andric   // emitting any actual debug info yet and haven't had a chance to parse any
9830b57cec5SDimitry Andric   // embedded .file directives.)
9840b57cec5SDimitry Andric   if (getContext().getGenDwarfForAssembly()) {
9850b57cec5SDimitry Andric     MCSection *Sec = getStreamer().getCurrentSectionOnly();
9860b57cec5SDimitry Andric     if (!Sec->getBeginSymbol()) {
9870b57cec5SDimitry Andric       MCSymbol *SectionStartSym = getContext().createTempSymbol();
9885ffd83dbSDimitry Andric       getStreamer().emitLabel(SectionStartSym);
9890b57cec5SDimitry Andric       Sec->setBeginSymbol(SectionStartSym);
9900b57cec5SDimitry Andric     }
9910b57cec5SDimitry Andric     bool InsertResult = getContext().addGenDwarfSection(Sec);
9920b57cec5SDimitry Andric     assert(InsertResult && ".text section should not have debug info yet");
9930b57cec5SDimitry Andric     (void)InsertResult;
9940b57cec5SDimitry Andric   }
9950b57cec5SDimitry Andric 
996fe6060f1SDimitry Andric   getTargetParser().onBeginOfFile();
997fe6060f1SDimitry Andric 
9980b57cec5SDimitry Andric   // While we have input, parse each statement.
9990b57cec5SDimitry Andric   while (Lexer.isNot(AsmToken::Eof)) {
10000b57cec5SDimitry Andric     ParseStatementInfo Info(&AsmStrRewrites);
1001480093f4SDimitry Andric     bool Parsed = parseStatement(Info, nullptr);
10020b57cec5SDimitry Andric 
10030b57cec5SDimitry Andric     // If we have a Lexer Error we are on an Error Token. Load in Lexer Error
10040b57cec5SDimitry Andric     // for printing ErrMsg via Lex() only if no (presumably better) parser error
10050b57cec5SDimitry Andric     // exists.
1006480093f4SDimitry Andric     if (Parsed && !hasPendingError() && Lexer.getTok().is(AsmToken::Error)) {
10070b57cec5SDimitry Andric       Lex();
10080b57cec5SDimitry Andric     }
10090b57cec5SDimitry Andric 
10100b57cec5SDimitry Andric     // parseStatement returned true so may need to emit an error.
10110b57cec5SDimitry Andric     printPendingErrors();
10120b57cec5SDimitry Andric 
10130b57cec5SDimitry Andric     // Skipping to the next line if needed.
1014480093f4SDimitry Andric     if (Parsed && !getLexer().isAtStartOfStatement())
10150b57cec5SDimitry Andric       eatToEndOfStatement();
10160b57cec5SDimitry Andric   }
10170b57cec5SDimitry Andric 
10180b57cec5SDimitry Andric   getTargetParser().onEndOfFile();
10190b57cec5SDimitry Andric   printPendingErrors();
10200b57cec5SDimitry Andric 
10210b57cec5SDimitry Andric   // All errors should have been emitted.
10220b57cec5SDimitry Andric   assert(!hasPendingError() && "unexpected error from parseStatement");
10230b57cec5SDimitry Andric 
10240b57cec5SDimitry Andric   getTargetParser().flushPendingInstructions(getStreamer());
10250b57cec5SDimitry Andric 
10260b57cec5SDimitry Andric   if (TheCondState.TheCond != StartingCondState.TheCond ||
10270b57cec5SDimitry Andric       TheCondState.Ignore != StartingCondState.Ignore)
10280b57cec5SDimitry Andric     printError(getTok().getLoc(), "unmatched .ifs or .elses");
10290b57cec5SDimitry Andric   // Check to see there are no empty DwarfFile slots.
10300b57cec5SDimitry Andric   const auto &LineTables = getContext().getMCDwarfLineTables();
10310b57cec5SDimitry Andric   if (!LineTables.empty()) {
10320b57cec5SDimitry Andric     unsigned Index = 0;
10330b57cec5SDimitry Andric     for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
10340b57cec5SDimitry Andric       if (File.Name.empty() && Index != 0)
10350b57cec5SDimitry Andric         printError(getTok().getLoc(), "unassigned file number: " +
10360b57cec5SDimitry Andric                                           Twine(Index) +
10370b57cec5SDimitry Andric                                           " for .file directives");
10380b57cec5SDimitry Andric       ++Index;
10390b57cec5SDimitry Andric     }
10400b57cec5SDimitry Andric   }
10410b57cec5SDimitry Andric 
10420b57cec5SDimitry Andric   // Check to see that all assembler local symbols were actually defined.
10430b57cec5SDimitry Andric   // Targets that don't do subsections via symbols may not want this, though,
10440b57cec5SDimitry Andric   // so conservatively exclude them. Only do this if we're finalizing, though,
10450b57cec5SDimitry Andric   // as otherwise we won't necessarilly have seen everything yet.
10460b57cec5SDimitry Andric   if (!NoFinalize) {
10470b57cec5SDimitry Andric     if (MAI.hasSubsectionsViaSymbols()) {
10480b57cec5SDimitry Andric       for (const auto &TableEntry : getContext().getSymbols()) {
10490b57cec5SDimitry Andric         MCSymbol *Sym = TableEntry.getValue();
10500b57cec5SDimitry Andric         // Variable symbols may not be marked as defined, so check those
10510b57cec5SDimitry Andric         // explicitly. If we know it's a variable, we have a definition for
10520b57cec5SDimitry Andric         // the purposes of this check.
10530b57cec5SDimitry Andric         if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
10540b57cec5SDimitry Andric           // FIXME: We would really like to refer back to where the symbol was
10550b57cec5SDimitry Andric           // first referenced for a source location. We need to add something
10560b57cec5SDimitry Andric           // to track that. Currently, we just point to the end of the file.
10570b57cec5SDimitry Andric           printError(getTok().getLoc(), "assembler local symbol '" +
10580b57cec5SDimitry Andric                                             Sym->getName() + "' not defined");
10590b57cec5SDimitry Andric       }
10600b57cec5SDimitry Andric     }
10610b57cec5SDimitry Andric 
10620b57cec5SDimitry Andric     // Temporary symbols like the ones for directional jumps don't go in the
10630b57cec5SDimitry Andric     // symbol table. They also need to be diagnosed in all (final) cases.
10640b57cec5SDimitry Andric     for (std::tuple<SMLoc, CppHashInfoTy, MCSymbol *> &LocSym : DirLabels) {
10650b57cec5SDimitry Andric       if (std::get<2>(LocSym)->isUndefined()) {
10660b57cec5SDimitry Andric         // Reset the state of any "# line file" directives we've seen to the
10670b57cec5SDimitry Andric         // context as it was at the diagnostic site.
10680b57cec5SDimitry Andric         CppHashInfo = std::get<1>(LocSym);
10690b57cec5SDimitry Andric         printError(std::get<0>(LocSym), "directional label undefined");
10700b57cec5SDimitry Andric       }
10710b57cec5SDimitry Andric     }
10720b57cec5SDimitry Andric   }
10730b57cec5SDimitry Andric   // Finalize the output stream if there are no errors and if the client wants
10740b57cec5SDimitry Andric   // us to.
1075349cc55cSDimitry Andric   if (!HadError && !NoFinalize) {
1076349cc55cSDimitry Andric     if (auto *TS = Out.getTargetStreamer())
1077349cc55cSDimitry Andric       TS->emitConstantPools();
1078349cc55cSDimitry Andric 
107981ad6265SDimitry Andric     Out.finish(Lexer.getLoc());
1080349cc55cSDimitry Andric   }
10810b57cec5SDimitry Andric 
10820b57cec5SDimitry Andric   return HadError || getContext().hadError();
10830b57cec5SDimitry Andric }
10840b57cec5SDimitry Andric 
10850b57cec5SDimitry Andric bool AsmParser::checkForValidSection() {
10865ffd83dbSDimitry Andric   if (!ParsingMSInlineAsm && !getStreamer().getCurrentSectionOnly()) {
1087349cc55cSDimitry Andric     Out.initSections(false, getTargetParser().getSTI());
10880b57cec5SDimitry Andric     return Error(getTok().getLoc(),
10890b57cec5SDimitry Andric                  "expected section directive before assembly directive");
10900b57cec5SDimitry Andric   }
10910b57cec5SDimitry Andric   return false;
10920b57cec5SDimitry Andric }
10930b57cec5SDimitry Andric 
10940b57cec5SDimitry Andric /// Throw away the rest of the line for testing purposes.
10950b57cec5SDimitry Andric void AsmParser::eatToEndOfStatement() {
10960b57cec5SDimitry Andric   while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
10970b57cec5SDimitry Andric     Lexer.Lex();
10980b57cec5SDimitry Andric 
10990b57cec5SDimitry Andric   // Eat EOL.
11000b57cec5SDimitry Andric   if (Lexer.is(AsmToken::EndOfStatement))
11010b57cec5SDimitry Andric     Lexer.Lex();
11020b57cec5SDimitry Andric }
11030b57cec5SDimitry Andric 
11040b57cec5SDimitry Andric StringRef AsmParser::parseStringToEndOfStatement() {
11050b57cec5SDimitry Andric   const char *Start = getTok().getLoc().getPointer();
11060b57cec5SDimitry Andric 
11070b57cec5SDimitry Andric   while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
11080b57cec5SDimitry Andric     Lexer.Lex();
11090b57cec5SDimitry Andric 
11100b57cec5SDimitry Andric   const char *End = getTok().getLoc().getPointer();
11110b57cec5SDimitry Andric   return StringRef(Start, End - Start);
11120b57cec5SDimitry Andric }
11130b57cec5SDimitry Andric 
11140b57cec5SDimitry Andric StringRef AsmParser::parseStringToComma() {
11150b57cec5SDimitry Andric   const char *Start = getTok().getLoc().getPointer();
11160b57cec5SDimitry Andric 
11170b57cec5SDimitry Andric   while (Lexer.isNot(AsmToken::EndOfStatement) &&
11180b57cec5SDimitry Andric          Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
11190b57cec5SDimitry Andric     Lexer.Lex();
11200b57cec5SDimitry Andric 
11210b57cec5SDimitry Andric   const char *End = getTok().getLoc().getPointer();
11220b57cec5SDimitry Andric   return StringRef(Start, End - Start);
11230b57cec5SDimitry Andric }
11240b57cec5SDimitry Andric 
11250b57cec5SDimitry Andric /// Parse a paren expression and return it.
11260b57cec5SDimitry Andric /// NOTE: This assumes the leading '(' has already been consumed.
11270b57cec5SDimitry Andric ///
11280b57cec5SDimitry Andric /// parenexpr ::= expr)
11290b57cec5SDimitry Andric ///
11300b57cec5SDimitry Andric bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
11310b57cec5SDimitry Andric   if (parseExpression(Res))
11320b57cec5SDimitry Andric     return true;
11330b57cec5SDimitry Andric   EndLoc = Lexer.getTok().getEndLoc();
113404eeddc0SDimitry Andric   return parseRParen();
11350b57cec5SDimitry Andric }
11360b57cec5SDimitry Andric 
11370b57cec5SDimitry Andric /// Parse a bracket expression and return it.
11380b57cec5SDimitry Andric /// NOTE: This assumes the leading '[' has already been consumed.
11390b57cec5SDimitry Andric ///
11400b57cec5SDimitry Andric /// bracketexpr ::= expr]
11410b57cec5SDimitry Andric ///
11420b57cec5SDimitry Andric bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
11430b57cec5SDimitry Andric   if (parseExpression(Res))
11440b57cec5SDimitry Andric     return true;
11450b57cec5SDimitry Andric   EndLoc = getTok().getEndLoc();
11460b57cec5SDimitry Andric   if (parseToken(AsmToken::RBrac, "expected ']' in brackets expression"))
11470b57cec5SDimitry Andric     return true;
11480b57cec5SDimitry Andric   return false;
11490b57cec5SDimitry Andric }
11500b57cec5SDimitry Andric 
11510b57cec5SDimitry Andric /// Parse a primary expression and return it.
11520b57cec5SDimitry Andric ///  primaryexpr ::= (parenexpr
11530b57cec5SDimitry Andric ///  primaryexpr ::= symbol
11540b57cec5SDimitry Andric ///  primaryexpr ::= number
11550b57cec5SDimitry Andric ///  primaryexpr ::= '.'
11560b57cec5SDimitry Andric ///  primaryexpr ::= ~,+,- primaryexpr
1157e8d8bef9SDimitry Andric bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
1158e8d8bef9SDimitry Andric                                  AsmTypeInfo *TypeInfo) {
11590b57cec5SDimitry Andric   SMLoc FirstTokenLoc = getLexer().getLoc();
11600b57cec5SDimitry Andric   AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
11610b57cec5SDimitry Andric   switch (FirstTokenKind) {
11620b57cec5SDimitry Andric   default:
11630b57cec5SDimitry Andric     return TokError("unknown token in expression");
11640b57cec5SDimitry Andric   // If we have an error assume that we've already handled it.
11650b57cec5SDimitry Andric   case AsmToken::Error:
11660b57cec5SDimitry Andric     return true;
11670b57cec5SDimitry Andric   case AsmToken::Exclaim:
11680b57cec5SDimitry Andric     Lex(); // Eat the operator.
1169e8d8bef9SDimitry Andric     if (parsePrimaryExpr(Res, EndLoc, TypeInfo))
11700b57cec5SDimitry Andric       return true;
11710b57cec5SDimitry Andric     Res = MCUnaryExpr::createLNot(Res, getContext(), FirstTokenLoc);
11720b57cec5SDimitry Andric     return false;
11730b57cec5SDimitry Andric   case AsmToken::Dollar:
1174fe6060f1SDimitry Andric   case AsmToken::Star:
11750b57cec5SDimitry Andric   case AsmToken::At:
11760b57cec5SDimitry Andric   case AsmToken::String:
11770b57cec5SDimitry Andric   case AsmToken::Identifier: {
11780b57cec5SDimitry Andric     StringRef Identifier;
11790b57cec5SDimitry Andric     if (parseIdentifier(Identifier)) {
1180fe6060f1SDimitry Andric       // We may have failed but '$'|'*' may be a valid token in context of
1181fe6060f1SDimitry Andric       // the current PC.
1182fe6060f1SDimitry Andric       if (getTok().is(AsmToken::Dollar) || getTok().is(AsmToken::Star)) {
1183fe6060f1SDimitry Andric         bool ShouldGenerateTempSymbol = false;
1184fe6060f1SDimitry Andric         if ((getTok().is(AsmToken::Dollar) && MAI.getDollarIsPC()) ||
1185fe6060f1SDimitry Andric             (getTok().is(AsmToken::Star) && MAI.getStarIsPC()))
1186fe6060f1SDimitry Andric           ShouldGenerateTempSymbol = true;
1187fe6060f1SDimitry Andric 
1188fe6060f1SDimitry Andric         if (!ShouldGenerateTempSymbol)
1189fe6060f1SDimitry Andric           return Error(FirstTokenLoc, "invalid token in expression");
1190fe6060f1SDimitry Andric 
1191fe6060f1SDimitry Andric         // Eat the '$'|'*' token.
11920b57cec5SDimitry Andric         Lex();
1193fe6060f1SDimitry Andric         // This is either a '$'|'*' reference, which references the current PC.
1194fe6060f1SDimitry Andric         // Emit a temporary label to the streamer and refer to it.
11950b57cec5SDimitry Andric         MCSymbol *Sym = Ctx.createTempSymbol();
11965ffd83dbSDimitry Andric         Out.emitLabel(Sym);
11970b57cec5SDimitry Andric         Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None,
11980b57cec5SDimitry Andric                                       getContext());
11990b57cec5SDimitry Andric         EndLoc = FirstTokenLoc;
12000b57cec5SDimitry Andric         return false;
12010b57cec5SDimitry Andric       }
12020b57cec5SDimitry Andric     }
12030b57cec5SDimitry Andric     // Parse symbol variant
12040b57cec5SDimitry Andric     std::pair<StringRef, StringRef> Split;
12050b57cec5SDimitry Andric     if (!MAI.useParensForSymbolVariant()) {
12060b57cec5SDimitry Andric       if (FirstTokenKind == AsmToken::String) {
12070b57cec5SDimitry Andric         if (Lexer.is(AsmToken::At)) {
12080b57cec5SDimitry Andric           Lex(); // eat @
12090b57cec5SDimitry Andric           SMLoc AtLoc = getLexer().getLoc();
12100b57cec5SDimitry Andric           StringRef VName;
12110b57cec5SDimitry Andric           if (parseIdentifier(VName))
12120b57cec5SDimitry Andric             return Error(AtLoc, "expected symbol variant after '@'");
12130b57cec5SDimitry Andric 
12140b57cec5SDimitry Andric           Split = std::make_pair(Identifier, VName);
12150b57cec5SDimitry Andric         }
12160b57cec5SDimitry Andric       } else {
12170b57cec5SDimitry Andric         Split = Identifier.split('@');
12180b57cec5SDimitry Andric       }
12190b57cec5SDimitry Andric     } else if (Lexer.is(AsmToken::LParen)) {
12200b57cec5SDimitry Andric       Lex(); // eat '('.
12210b57cec5SDimitry Andric       StringRef VName;
12220b57cec5SDimitry Andric       parseIdentifier(VName);
122304eeddc0SDimitry Andric       if (parseRParen())
12240b57cec5SDimitry Andric         return true;
12250b57cec5SDimitry Andric       Split = std::make_pair(Identifier, VName);
12260b57cec5SDimitry Andric     }
12270b57cec5SDimitry Andric 
12280b57cec5SDimitry Andric     EndLoc = SMLoc::getFromPointer(Identifier.end());
12290b57cec5SDimitry Andric 
12300b57cec5SDimitry Andric     // This is a symbol reference.
12310b57cec5SDimitry Andric     StringRef SymbolName = Identifier;
12320b57cec5SDimitry Andric     if (SymbolName.empty())
12330b57cec5SDimitry Andric       return Error(getLexer().getLoc(), "expected a symbol reference");
12340b57cec5SDimitry Andric 
12350b57cec5SDimitry Andric     MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
12360b57cec5SDimitry Andric 
12370b57cec5SDimitry Andric     // Lookup the symbol variant if used.
12380b57cec5SDimitry Andric     if (!Split.second.empty()) {
12390b57cec5SDimitry Andric       Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
12400b57cec5SDimitry Andric       if (Variant != MCSymbolRefExpr::VK_Invalid) {
12410b57cec5SDimitry Andric         SymbolName = Split.first;
12420b57cec5SDimitry Andric       } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
12430b57cec5SDimitry Andric         Variant = MCSymbolRefExpr::VK_None;
12440b57cec5SDimitry Andric       } else {
12450b57cec5SDimitry Andric         return Error(SMLoc::getFromPointer(Split.second.begin()),
12460b57cec5SDimitry Andric                      "invalid variant '" + Split.second + "'");
12470b57cec5SDimitry Andric       }
12480b57cec5SDimitry Andric     }
12490b57cec5SDimitry Andric 
12500b57cec5SDimitry Andric     MCSymbol *Sym = getContext().getInlineAsmLabel(SymbolName);
12510b57cec5SDimitry Andric     if (!Sym)
1252fe6060f1SDimitry Andric       Sym = getContext().getOrCreateSymbol(
1253fe6060f1SDimitry Andric           MAI.shouldEmitLabelsInUpperCase() ? SymbolName.upper() : SymbolName);
12540b57cec5SDimitry Andric 
12550b57cec5SDimitry Andric     // If this is an absolute variable reference, substitute it now to preserve
12560b57cec5SDimitry Andric     // semantics in the face of reassignment.
12570b57cec5SDimitry Andric     if (Sym->isVariable()) {
12580b57cec5SDimitry Andric       auto V = Sym->getVariableValue(/*SetUsed*/ false);
12590b57cec5SDimitry Andric       bool DoInline = isa<MCConstantExpr>(V) && !Variant;
12600b57cec5SDimitry Andric       if (auto TV = dyn_cast<MCTargetExpr>(V))
12610b57cec5SDimitry Andric         DoInline = TV->inlineAssignedExpr();
12620b57cec5SDimitry Andric       if (DoInline) {
12630b57cec5SDimitry Andric         if (Variant)
12640b57cec5SDimitry Andric           return Error(EndLoc, "unexpected modifier on variable reference");
12650b57cec5SDimitry Andric         Res = Sym->getVariableValue(/*SetUsed*/ false);
12660b57cec5SDimitry Andric         return false;
12670b57cec5SDimitry Andric       }
12680b57cec5SDimitry Andric     }
12690b57cec5SDimitry Andric 
12700b57cec5SDimitry Andric     // Otherwise create a symbol ref.
12710b57cec5SDimitry Andric     Res = MCSymbolRefExpr::create(Sym, Variant, getContext(), FirstTokenLoc);
12720b57cec5SDimitry Andric     return false;
12730b57cec5SDimitry Andric   }
12740b57cec5SDimitry Andric   case AsmToken::BigNum:
12750b57cec5SDimitry Andric     return TokError("literal value out of range for directive");
12760b57cec5SDimitry Andric   case AsmToken::Integer: {
12770b57cec5SDimitry Andric     SMLoc Loc = getTok().getLoc();
12780b57cec5SDimitry Andric     int64_t IntVal = getTok().getIntVal();
12790b57cec5SDimitry Andric     Res = MCConstantExpr::create(IntVal, getContext());
12800b57cec5SDimitry Andric     EndLoc = Lexer.getTok().getEndLoc();
12810b57cec5SDimitry Andric     Lex(); // Eat token.
12820b57cec5SDimitry Andric     // Look for 'b' or 'f' following an Integer as a directional label
12830b57cec5SDimitry Andric     if (Lexer.getKind() == AsmToken::Identifier) {
12840b57cec5SDimitry Andric       StringRef IDVal = getTok().getString();
12850b57cec5SDimitry Andric       // Lookup the symbol variant if used.
12860b57cec5SDimitry Andric       std::pair<StringRef, StringRef> Split = IDVal.split('@');
12870b57cec5SDimitry Andric       MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
12880b57cec5SDimitry Andric       if (Split.first.size() != IDVal.size()) {
12890b57cec5SDimitry Andric         Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
12900b57cec5SDimitry Andric         if (Variant == MCSymbolRefExpr::VK_Invalid)
12910b57cec5SDimitry Andric           return TokError("invalid variant '" + Split.second + "'");
12920b57cec5SDimitry Andric         IDVal = Split.first;
12930b57cec5SDimitry Andric       }
12940b57cec5SDimitry Andric       if (IDVal == "f" || IDVal == "b") {
12950b57cec5SDimitry Andric         MCSymbol *Sym =
12960b57cec5SDimitry Andric             Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b");
12970b57cec5SDimitry Andric         Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
12980b57cec5SDimitry Andric         if (IDVal == "b" && Sym->isUndefined())
12990b57cec5SDimitry Andric           return Error(Loc, "directional label undefined");
13000b57cec5SDimitry Andric         DirLabels.push_back(std::make_tuple(Loc, CppHashInfo, Sym));
13010b57cec5SDimitry Andric         EndLoc = Lexer.getTok().getEndLoc();
13020b57cec5SDimitry Andric         Lex(); // Eat identifier.
13030b57cec5SDimitry Andric       }
13040b57cec5SDimitry Andric     }
13050b57cec5SDimitry Andric     return false;
13060b57cec5SDimitry Andric   }
13070b57cec5SDimitry Andric   case AsmToken::Real: {
13080b57cec5SDimitry Andric     APFloat RealVal(APFloat::IEEEdouble(), getTok().getString());
13090b57cec5SDimitry Andric     uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
13100b57cec5SDimitry Andric     Res = MCConstantExpr::create(IntVal, getContext());
13110b57cec5SDimitry Andric     EndLoc = Lexer.getTok().getEndLoc();
13120b57cec5SDimitry Andric     Lex(); // Eat token.
13130b57cec5SDimitry Andric     return false;
13140b57cec5SDimitry Andric   }
13150b57cec5SDimitry Andric   case AsmToken::Dot: {
1316fe6060f1SDimitry Andric     if (!MAI.getDotIsPC())
1317fe6060f1SDimitry Andric       return TokError("cannot use . as current PC");
1318fe6060f1SDimitry Andric 
13190b57cec5SDimitry Andric     // This is a '.' reference, which references the current PC.  Emit a
13200b57cec5SDimitry Andric     // temporary label to the streamer and refer to it.
13210b57cec5SDimitry Andric     MCSymbol *Sym = Ctx.createTempSymbol();
13225ffd83dbSDimitry Andric     Out.emitLabel(Sym);
13230b57cec5SDimitry Andric     Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
13240b57cec5SDimitry Andric     EndLoc = Lexer.getTok().getEndLoc();
13250b57cec5SDimitry Andric     Lex(); // Eat identifier.
13260b57cec5SDimitry Andric     return false;
13270b57cec5SDimitry Andric   }
13280b57cec5SDimitry Andric   case AsmToken::LParen:
13290b57cec5SDimitry Andric     Lex(); // Eat the '('.
13300b57cec5SDimitry Andric     return parseParenExpr(Res, EndLoc);
13310b57cec5SDimitry Andric   case AsmToken::LBrac:
13320b57cec5SDimitry Andric     if (!PlatformParser->HasBracketExpressions())
13330b57cec5SDimitry Andric       return TokError("brackets expression not supported on this target");
13340b57cec5SDimitry Andric     Lex(); // Eat the '['.
13350b57cec5SDimitry Andric     return parseBracketExpr(Res, EndLoc);
13360b57cec5SDimitry Andric   case AsmToken::Minus:
13370b57cec5SDimitry Andric     Lex(); // Eat the operator.
1338e8d8bef9SDimitry Andric     if (parsePrimaryExpr(Res, EndLoc, TypeInfo))
13390b57cec5SDimitry Andric       return true;
13400b57cec5SDimitry Andric     Res = MCUnaryExpr::createMinus(Res, getContext(), FirstTokenLoc);
13410b57cec5SDimitry Andric     return false;
13420b57cec5SDimitry Andric   case AsmToken::Plus:
13430b57cec5SDimitry Andric     Lex(); // Eat the operator.
1344e8d8bef9SDimitry Andric     if (parsePrimaryExpr(Res, EndLoc, TypeInfo))
13450b57cec5SDimitry Andric       return true;
13460b57cec5SDimitry Andric     Res = MCUnaryExpr::createPlus(Res, getContext(), FirstTokenLoc);
13470b57cec5SDimitry Andric     return false;
13480b57cec5SDimitry Andric   case AsmToken::Tilde:
13490b57cec5SDimitry Andric     Lex(); // Eat the operator.
1350e8d8bef9SDimitry Andric     if (parsePrimaryExpr(Res, EndLoc, TypeInfo))
13510b57cec5SDimitry Andric       return true;
13520b57cec5SDimitry Andric     Res = MCUnaryExpr::createNot(Res, getContext(), FirstTokenLoc);
13530b57cec5SDimitry Andric     return false;
13540b57cec5SDimitry Andric   // MIPS unary expression operators. The lexer won't generate these tokens if
13550b57cec5SDimitry Andric   // MCAsmInfo::HasMipsExpressions is false for the target.
13560b57cec5SDimitry Andric   case AsmToken::PercentCall16:
13570b57cec5SDimitry Andric   case AsmToken::PercentCall_Hi:
13580b57cec5SDimitry Andric   case AsmToken::PercentCall_Lo:
13590b57cec5SDimitry Andric   case AsmToken::PercentDtprel_Hi:
13600b57cec5SDimitry Andric   case AsmToken::PercentDtprel_Lo:
13610b57cec5SDimitry Andric   case AsmToken::PercentGot:
13620b57cec5SDimitry Andric   case AsmToken::PercentGot_Disp:
13630b57cec5SDimitry Andric   case AsmToken::PercentGot_Hi:
13640b57cec5SDimitry Andric   case AsmToken::PercentGot_Lo:
13650b57cec5SDimitry Andric   case AsmToken::PercentGot_Ofst:
13660b57cec5SDimitry Andric   case AsmToken::PercentGot_Page:
13670b57cec5SDimitry Andric   case AsmToken::PercentGottprel:
13680b57cec5SDimitry Andric   case AsmToken::PercentGp_Rel:
13690b57cec5SDimitry Andric   case AsmToken::PercentHi:
13700b57cec5SDimitry Andric   case AsmToken::PercentHigher:
13710b57cec5SDimitry Andric   case AsmToken::PercentHighest:
13720b57cec5SDimitry Andric   case AsmToken::PercentLo:
13730b57cec5SDimitry Andric   case AsmToken::PercentNeg:
13740b57cec5SDimitry Andric   case AsmToken::PercentPcrel_Hi:
13750b57cec5SDimitry Andric   case AsmToken::PercentPcrel_Lo:
13760b57cec5SDimitry Andric   case AsmToken::PercentTlsgd:
13770b57cec5SDimitry Andric   case AsmToken::PercentTlsldm:
13780b57cec5SDimitry Andric   case AsmToken::PercentTprel_Hi:
13790b57cec5SDimitry Andric   case AsmToken::PercentTprel_Lo:
13800b57cec5SDimitry Andric     Lex(); // Eat the operator.
13810b57cec5SDimitry Andric     if (Lexer.isNot(AsmToken::LParen))
13820b57cec5SDimitry Andric       return TokError("expected '(' after operator");
13830b57cec5SDimitry Andric     Lex(); // Eat the operator.
13840b57cec5SDimitry Andric     if (parseExpression(Res, EndLoc))
13850b57cec5SDimitry Andric       return true;
138604eeddc0SDimitry Andric     if (parseRParen())
138704eeddc0SDimitry Andric       return true;
13880b57cec5SDimitry Andric     Res = getTargetParser().createTargetUnaryExpr(Res, FirstTokenKind, Ctx);
13890b57cec5SDimitry Andric     return !Res;
13900b57cec5SDimitry Andric   }
13910b57cec5SDimitry Andric }
13920b57cec5SDimitry Andric 
13930b57cec5SDimitry Andric bool AsmParser::parseExpression(const MCExpr *&Res) {
13940b57cec5SDimitry Andric   SMLoc EndLoc;
13950b57cec5SDimitry Andric   return parseExpression(Res, EndLoc);
13960b57cec5SDimitry Andric }
13970b57cec5SDimitry Andric 
13980b57cec5SDimitry Andric const MCExpr *
13990b57cec5SDimitry Andric AsmParser::applyModifierToExpr(const MCExpr *E,
14000b57cec5SDimitry Andric                                MCSymbolRefExpr::VariantKind Variant) {
14010b57cec5SDimitry Andric   // Ask the target implementation about this expression first.
14020b57cec5SDimitry Andric   const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
14030b57cec5SDimitry Andric   if (NewE)
14040b57cec5SDimitry Andric     return NewE;
14050b57cec5SDimitry Andric   // Recurse over the given expression, rebuilding it to apply the given variant
14060b57cec5SDimitry Andric   // if there is exactly one symbol.
14070b57cec5SDimitry Andric   switch (E->getKind()) {
14080b57cec5SDimitry Andric   case MCExpr::Target:
14090b57cec5SDimitry Andric   case MCExpr::Constant:
14100b57cec5SDimitry Andric     return nullptr;
14110b57cec5SDimitry Andric 
14120b57cec5SDimitry Andric   case MCExpr::SymbolRef: {
14130b57cec5SDimitry Andric     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
14140b57cec5SDimitry Andric 
14150b57cec5SDimitry Andric     if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
14160b57cec5SDimitry Andric       TokError("invalid variant on expression '" + getTok().getIdentifier() +
14170b57cec5SDimitry Andric                "' (already modified)");
14180b57cec5SDimitry Andric       return E;
14190b57cec5SDimitry Andric     }
14200b57cec5SDimitry Andric 
14210b57cec5SDimitry Andric     return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, getContext());
14220b57cec5SDimitry Andric   }
14230b57cec5SDimitry Andric 
14240b57cec5SDimitry Andric   case MCExpr::Unary: {
14250b57cec5SDimitry Andric     const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
14260b57cec5SDimitry Andric     const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
14270b57cec5SDimitry Andric     if (!Sub)
14280b57cec5SDimitry Andric       return nullptr;
14290b57cec5SDimitry Andric     return MCUnaryExpr::create(UE->getOpcode(), Sub, getContext());
14300b57cec5SDimitry Andric   }
14310b57cec5SDimitry Andric 
14320b57cec5SDimitry Andric   case MCExpr::Binary: {
14330b57cec5SDimitry Andric     const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
14340b57cec5SDimitry Andric     const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
14350b57cec5SDimitry Andric     const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
14360b57cec5SDimitry Andric 
14370b57cec5SDimitry Andric     if (!LHS && !RHS)
14380b57cec5SDimitry Andric       return nullptr;
14390b57cec5SDimitry Andric 
14400b57cec5SDimitry Andric     if (!LHS)
14410b57cec5SDimitry Andric       LHS = BE->getLHS();
14420b57cec5SDimitry Andric     if (!RHS)
14430b57cec5SDimitry Andric       RHS = BE->getRHS();
14440b57cec5SDimitry Andric 
14450b57cec5SDimitry Andric     return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, getContext());
14460b57cec5SDimitry Andric   }
14470b57cec5SDimitry Andric   }
14480b57cec5SDimitry Andric 
14490b57cec5SDimitry Andric   llvm_unreachable("Invalid expression kind!");
14500b57cec5SDimitry Andric }
14510b57cec5SDimitry Andric 
14520b57cec5SDimitry Andric /// This function checks if the next token is <string> type or arithmetic.
14530b57cec5SDimitry Andric /// string that begin with character '<' must end with character '>'.
14540b57cec5SDimitry Andric /// otherwise it is arithmetics.
14550b57cec5SDimitry Andric /// If the function returns a 'true' value,
14560b57cec5SDimitry Andric /// the End argument will be filled with the last location pointed to the '>'
14570b57cec5SDimitry Andric /// character.
14580b57cec5SDimitry Andric 
14590b57cec5SDimitry Andric /// There is a gap between the AltMacro's documentation and the single quote
14600b57cec5SDimitry Andric /// implementation. GCC does not fully support this feature and so we will not
14610b57cec5SDimitry Andric /// support it.
14620b57cec5SDimitry Andric /// TODO: Adding single quote as a string.
14635ffd83dbSDimitry Andric static bool isAngleBracketString(SMLoc &StrLoc, SMLoc &EndLoc) {
14640b57cec5SDimitry Andric   assert((StrLoc.getPointer() != nullptr) &&
14650b57cec5SDimitry Andric          "Argument to the function cannot be a NULL value");
14660b57cec5SDimitry Andric   const char *CharPtr = StrLoc.getPointer();
14670b57cec5SDimitry Andric   while ((*CharPtr != '>') && (*CharPtr != '\n') && (*CharPtr != '\r') &&
14680b57cec5SDimitry Andric          (*CharPtr != '\0')) {
14690b57cec5SDimitry Andric     if (*CharPtr == '!')
14700b57cec5SDimitry Andric       CharPtr++;
14710b57cec5SDimitry Andric     CharPtr++;
14720b57cec5SDimitry Andric   }
14730b57cec5SDimitry Andric   if (*CharPtr == '>') {
14740b57cec5SDimitry Andric     EndLoc = StrLoc.getFromPointer(CharPtr + 1);
14750b57cec5SDimitry Andric     return true;
14760b57cec5SDimitry Andric   }
14770b57cec5SDimitry Andric   return false;
14780b57cec5SDimitry Andric }
14790b57cec5SDimitry Andric 
14800b57cec5SDimitry Andric /// creating a string without the escape characters '!'.
14815ffd83dbSDimitry Andric static std::string angleBracketString(StringRef AltMacroStr) {
14820b57cec5SDimitry Andric   std::string Res;
14830b57cec5SDimitry Andric   for (size_t Pos = 0; Pos < AltMacroStr.size(); Pos++) {
14840b57cec5SDimitry Andric     if (AltMacroStr[Pos] == '!')
14850b57cec5SDimitry Andric       Pos++;
14860b57cec5SDimitry Andric     Res += AltMacroStr[Pos];
14870b57cec5SDimitry Andric   }
14880b57cec5SDimitry Andric   return Res;
14890b57cec5SDimitry Andric }
14900b57cec5SDimitry Andric 
14910b57cec5SDimitry Andric /// Parse an expression and return it.
14920b57cec5SDimitry Andric ///
14930b57cec5SDimitry Andric ///  expr ::= expr &&,|| expr               -> lowest.
14940b57cec5SDimitry Andric ///  expr ::= expr |,^,&,! expr
14950b57cec5SDimitry Andric ///  expr ::= expr ==,!=,<>,<,<=,>,>= expr
14960b57cec5SDimitry Andric ///  expr ::= expr <<,>> expr
14970b57cec5SDimitry Andric ///  expr ::= expr +,- expr
14980b57cec5SDimitry Andric ///  expr ::= expr *,/,% expr               -> highest.
14990b57cec5SDimitry Andric ///  expr ::= primaryexpr
15000b57cec5SDimitry Andric ///
15010b57cec5SDimitry Andric bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
15020b57cec5SDimitry Andric   // Parse the expression.
15030b57cec5SDimitry Andric   Res = nullptr;
15040b57cec5SDimitry Andric   if (getTargetParser().parsePrimaryExpr(Res, EndLoc) ||
15050b57cec5SDimitry Andric       parseBinOpRHS(1, Res, EndLoc))
15060b57cec5SDimitry Andric     return true;
15070b57cec5SDimitry Andric 
15080b57cec5SDimitry Andric   // As a special case, we support 'a op b @ modifier' by rewriting the
15090b57cec5SDimitry Andric   // expression to include the modifier. This is inefficient, but in general we
15100b57cec5SDimitry Andric   // expect users to use 'a@modifier op b'.
15110b57cec5SDimitry Andric   if (Lexer.getKind() == AsmToken::At) {
15120b57cec5SDimitry Andric     Lex();
15130b57cec5SDimitry Andric 
15140b57cec5SDimitry Andric     if (Lexer.isNot(AsmToken::Identifier))
15150b57cec5SDimitry Andric       return TokError("unexpected symbol modifier following '@'");
15160b57cec5SDimitry Andric 
15170b57cec5SDimitry Andric     MCSymbolRefExpr::VariantKind Variant =
15180b57cec5SDimitry Andric         MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
15190b57cec5SDimitry Andric     if (Variant == MCSymbolRefExpr::VK_Invalid)
15200b57cec5SDimitry Andric       return TokError("invalid variant '" + getTok().getIdentifier() + "'");
15210b57cec5SDimitry Andric 
15220b57cec5SDimitry Andric     const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
15230b57cec5SDimitry Andric     if (!ModifiedRes) {
15240b57cec5SDimitry Andric       return TokError("invalid modifier '" + getTok().getIdentifier() +
15250b57cec5SDimitry Andric                       "' (no symbols present)");
15260b57cec5SDimitry Andric     }
15270b57cec5SDimitry Andric 
15280b57cec5SDimitry Andric     Res = ModifiedRes;
15290b57cec5SDimitry Andric     Lex();
15300b57cec5SDimitry Andric   }
15310b57cec5SDimitry Andric 
15320b57cec5SDimitry Andric   // Try to constant fold it up front, if possible. Do not exploit
15330b57cec5SDimitry Andric   // assembler here.
15340b57cec5SDimitry Andric   int64_t Value;
15350b57cec5SDimitry Andric   if (Res->evaluateAsAbsolute(Value))
15360b57cec5SDimitry Andric     Res = MCConstantExpr::create(Value, getContext());
15370b57cec5SDimitry Andric 
15380b57cec5SDimitry Andric   return false;
15390b57cec5SDimitry Andric }
15400b57cec5SDimitry Andric 
15410b57cec5SDimitry Andric bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
15420b57cec5SDimitry Andric   Res = nullptr;
15430b57cec5SDimitry Andric   return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
15440b57cec5SDimitry Andric }
15450b57cec5SDimitry Andric 
15460b57cec5SDimitry Andric bool AsmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
15470b57cec5SDimitry Andric                                       SMLoc &EndLoc) {
15480b57cec5SDimitry Andric   if (parseParenExpr(Res, EndLoc))
15490b57cec5SDimitry Andric     return true;
15500b57cec5SDimitry Andric 
15510b57cec5SDimitry Andric   for (; ParenDepth > 0; --ParenDepth) {
15520b57cec5SDimitry Andric     if (parseBinOpRHS(1, Res, EndLoc))
15530b57cec5SDimitry Andric       return true;
15540b57cec5SDimitry Andric 
15550b57cec5SDimitry Andric     // We don't Lex() the last RParen.
15560b57cec5SDimitry Andric     // This is the same behavior as parseParenExpression().
15570b57cec5SDimitry Andric     if (ParenDepth - 1 > 0) {
15580b57cec5SDimitry Andric       EndLoc = getTok().getEndLoc();
155904eeddc0SDimitry Andric       if (parseRParen())
15600b57cec5SDimitry Andric         return true;
15610b57cec5SDimitry Andric     }
15620b57cec5SDimitry Andric   }
15630b57cec5SDimitry Andric   return false;
15640b57cec5SDimitry Andric }
15650b57cec5SDimitry Andric 
15660b57cec5SDimitry Andric bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
15670b57cec5SDimitry Andric   const MCExpr *Expr;
15680b57cec5SDimitry Andric 
15690b57cec5SDimitry Andric   SMLoc StartLoc = Lexer.getLoc();
15700b57cec5SDimitry Andric   if (parseExpression(Expr))
15710b57cec5SDimitry Andric     return true;
15720b57cec5SDimitry Andric 
15730b57cec5SDimitry Andric   if (!Expr->evaluateAsAbsolute(Res, getStreamer().getAssemblerPtr()))
15740b57cec5SDimitry Andric     return Error(StartLoc, "expected absolute expression");
15750b57cec5SDimitry Andric 
15760b57cec5SDimitry Andric   return false;
15770b57cec5SDimitry Andric }
15780b57cec5SDimitry Andric 
15790b57cec5SDimitry Andric static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K,
15800b57cec5SDimitry Andric                                          MCBinaryExpr::Opcode &Kind,
15810b57cec5SDimitry Andric                                          bool ShouldUseLogicalShr) {
15820b57cec5SDimitry Andric   switch (K) {
15830b57cec5SDimitry Andric   default:
15840b57cec5SDimitry Andric     return 0; // not a binop.
15850b57cec5SDimitry Andric 
15860b57cec5SDimitry Andric   // Lowest Precedence: &&, ||
15870b57cec5SDimitry Andric   case AsmToken::AmpAmp:
15880b57cec5SDimitry Andric     Kind = MCBinaryExpr::LAnd;
15890b57cec5SDimitry Andric     return 1;
15900b57cec5SDimitry Andric   case AsmToken::PipePipe:
15910b57cec5SDimitry Andric     Kind = MCBinaryExpr::LOr;
15920b57cec5SDimitry Andric     return 1;
15930b57cec5SDimitry Andric 
15940b57cec5SDimitry Andric   // Low Precedence: |, &, ^
15950b57cec5SDimitry Andric   case AsmToken::Pipe:
15960b57cec5SDimitry Andric     Kind = MCBinaryExpr::Or;
15970b57cec5SDimitry Andric     return 2;
15980b57cec5SDimitry Andric   case AsmToken::Caret:
15990b57cec5SDimitry Andric     Kind = MCBinaryExpr::Xor;
16000b57cec5SDimitry Andric     return 2;
16010b57cec5SDimitry Andric   case AsmToken::Amp:
16020b57cec5SDimitry Andric     Kind = MCBinaryExpr::And;
16030b57cec5SDimitry Andric     return 2;
16040b57cec5SDimitry Andric 
16050b57cec5SDimitry Andric   // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
16060b57cec5SDimitry Andric   case AsmToken::EqualEqual:
16070b57cec5SDimitry Andric     Kind = MCBinaryExpr::EQ;
16080b57cec5SDimitry Andric     return 3;
16090b57cec5SDimitry Andric   case AsmToken::ExclaimEqual:
16100b57cec5SDimitry Andric   case AsmToken::LessGreater:
16110b57cec5SDimitry Andric     Kind = MCBinaryExpr::NE;
16120b57cec5SDimitry Andric     return 3;
16130b57cec5SDimitry Andric   case AsmToken::Less:
16140b57cec5SDimitry Andric     Kind = MCBinaryExpr::LT;
16150b57cec5SDimitry Andric     return 3;
16160b57cec5SDimitry Andric   case AsmToken::LessEqual:
16170b57cec5SDimitry Andric     Kind = MCBinaryExpr::LTE;
16180b57cec5SDimitry Andric     return 3;
16190b57cec5SDimitry Andric   case AsmToken::Greater:
16200b57cec5SDimitry Andric     Kind = MCBinaryExpr::GT;
16210b57cec5SDimitry Andric     return 3;
16220b57cec5SDimitry Andric   case AsmToken::GreaterEqual:
16230b57cec5SDimitry Andric     Kind = MCBinaryExpr::GTE;
16240b57cec5SDimitry Andric     return 3;
16250b57cec5SDimitry Andric 
16260b57cec5SDimitry Andric   // Intermediate Precedence: <<, >>
16270b57cec5SDimitry Andric   case AsmToken::LessLess:
16280b57cec5SDimitry Andric     Kind = MCBinaryExpr::Shl;
16290b57cec5SDimitry Andric     return 4;
16300b57cec5SDimitry Andric   case AsmToken::GreaterGreater:
16310b57cec5SDimitry Andric     Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
16320b57cec5SDimitry Andric     return 4;
16330b57cec5SDimitry Andric 
16340b57cec5SDimitry Andric   // High Intermediate Precedence: +, -
16350b57cec5SDimitry Andric   case AsmToken::Plus:
16360b57cec5SDimitry Andric     Kind = MCBinaryExpr::Add;
16370b57cec5SDimitry Andric     return 5;
16380b57cec5SDimitry Andric   case AsmToken::Minus:
16390b57cec5SDimitry Andric     Kind = MCBinaryExpr::Sub;
16400b57cec5SDimitry Andric     return 5;
16410b57cec5SDimitry Andric 
16420b57cec5SDimitry Andric   // Highest Precedence: *, /, %
16430b57cec5SDimitry Andric   case AsmToken::Star:
16440b57cec5SDimitry Andric     Kind = MCBinaryExpr::Mul;
16450b57cec5SDimitry Andric     return 6;
16460b57cec5SDimitry Andric   case AsmToken::Slash:
16470b57cec5SDimitry Andric     Kind = MCBinaryExpr::Div;
16480b57cec5SDimitry Andric     return 6;
16490b57cec5SDimitry Andric   case AsmToken::Percent:
16500b57cec5SDimitry Andric     Kind = MCBinaryExpr::Mod;
16510b57cec5SDimitry Andric     return 6;
16520b57cec5SDimitry Andric   }
16530b57cec5SDimitry Andric }
16540b57cec5SDimitry Andric 
1655e8d8bef9SDimitry Andric static unsigned getGNUBinOpPrecedence(const MCAsmInfo &MAI,
1656e8d8bef9SDimitry Andric                                       AsmToken::TokenKind K,
16570b57cec5SDimitry Andric                                       MCBinaryExpr::Opcode &Kind,
16580b57cec5SDimitry Andric                                       bool ShouldUseLogicalShr) {
16590b57cec5SDimitry Andric   switch (K) {
16600b57cec5SDimitry Andric   default:
16610b57cec5SDimitry Andric     return 0; // not a binop.
16620b57cec5SDimitry Andric 
16630b57cec5SDimitry Andric   // Lowest Precedence: &&, ||
16640b57cec5SDimitry Andric   case AsmToken::AmpAmp:
16650b57cec5SDimitry Andric     Kind = MCBinaryExpr::LAnd;
16660b57cec5SDimitry Andric     return 2;
16670b57cec5SDimitry Andric   case AsmToken::PipePipe:
16680b57cec5SDimitry Andric     Kind = MCBinaryExpr::LOr;
16690b57cec5SDimitry Andric     return 1;
16700b57cec5SDimitry Andric 
16710b57cec5SDimitry Andric   // Low Precedence: ==, !=, <>, <, <=, >, >=
16720b57cec5SDimitry Andric   case AsmToken::EqualEqual:
16730b57cec5SDimitry Andric     Kind = MCBinaryExpr::EQ;
16740b57cec5SDimitry Andric     return 3;
16750b57cec5SDimitry Andric   case AsmToken::ExclaimEqual:
16760b57cec5SDimitry Andric   case AsmToken::LessGreater:
16770b57cec5SDimitry Andric     Kind = MCBinaryExpr::NE;
16780b57cec5SDimitry Andric     return 3;
16790b57cec5SDimitry Andric   case AsmToken::Less:
16800b57cec5SDimitry Andric     Kind = MCBinaryExpr::LT;
16810b57cec5SDimitry Andric     return 3;
16820b57cec5SDimitry Andric   case AsmToken::LessEqual:
16830b57cec5SDimitry Andric     Kind = MCBinaryExpr::LTE;
16840b57cec5SDimitry Andric     return 3;
16850b57cec5SDimitry Andric   case AsmToken::Greater:
16860b57cec5SDimitry Andric     Kind = MCBinaryExpr::GT;
16870b57cec5SDimitry Andric     return 3;
16880b57cec5SDimitry Andric   case AsmToken::GreaterEqual:
16890b57cec5SDimitry Andric     Kind = MCBinaryExpr::GTE;
16900b57cec5SDimitry Andric     return 3;
16910b57cec5SDimitry Andric 
16920b57cec5SDimitry Andric   // Low Intermediate Precedence: +, -
16930b57cec5SDimitry Andric   case AsmToken::Plus:
16940b57cec5SDimitry Andric     Kind = MCBinaryExpr::Add;
16950b57cec5SDimitry Andric     return 4;
16960b57cec5SDimitry Andric   case AsmToken::Minus:
16970b57cec5SDimitry Andric     Kind = MCBinaryExpr::Sub;
16980b57cec5SDimitry Andric     return 4;
16990b57cec5SDimitry Andric 
1700e8d8bef9SDimitry Andric   // High Intermediate Precedence: |, !, &, ^
17010b57cec5SDimitry Andric   //
17020b57cec5SDimitry Andric   case AsmToken::Pipe:
17030b57cec5SDimitry Andric     Kind = MCBinaryExpr::Or;
17040b57cec5SDimitry Andric     return 5;
1705e8d8bef9SDimitry Andric   case AsmToken::Exclaim:
1706e8d8bef9SDimitry Andric     // Hack to support ARM compatible aliases (implied 'sp' operand in 'srs*'
1707e8d8bef9SDimitry Andric     // instructions like 'srsda #31!') and not parse ! as an infix operator.
1708e8d8bef9SDimitry Andric     if (MAI.getCommentString() == "@")
1709e8d8bef9SDimitry Andric       return 0;
1710e8d8bef9SDimitry Andric     Kind = MCBinaryExpr::OrNot;
1711e8d8bef9SDimitry Andric     return 5;
17120b57cec5SDimitry Andric   case AsmToken::Caret:
17130b57cec5SDimitry Andric     Kind = MCBinaryExpr::Xor;
17140b57cec5SDimitry Andric     return 5;
17150b57cec5SDimitry Andric   case AsmToken::Amp:
17160b57cec5SDimitry Andric     Kind = MCBinaryExpr::And;
17170b57cec5SDimitry Andric     return 5;
17180b57cec5SDimitry Andric 
17190b57cec5SDimitry Andric   // Highest Precedence: *, /, %, <<, >>
17200b57cec5SDimitry Andric   case AsmToken::Star:
17210b57cec5SDimitry Andric     Kind = MCBinaryExpr::Mul;
17220b57cec5SDimitry Andric     return 6;
17230b57cec5SDimitry Andric   case AsmToken::Slash:
17240b57cec5SDimitry Andric     Kind = MCBinaryExpr::Div;
17250b57cec5SDimitry Andric     return 6;
17260b57cec5SDimitry Andric   case AsmToken::Percent:
17270b57cec5SDimitry Andric     Kind = MCBinaryExpr::Mod;
17280b57cec5SDimitry Andric     return 6;
17290b57cec5SDimitry Andric   case AsmToken::LessLess:
17300b57cec5SDimitry Andric     Kind = MCBinaryExpr::Shl;
17310b57cec5SDimitry Andric     return 6;
17320b57cec5SDimitry Andric   case AsmToken::GreaterGreater:
17330b57cec5SDimitry Andric     Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
17340b57cec5SDimitry Andric     return 6;
17350b57cec5SDimitry Andric   }
17360b57cec5SDimitry Andric }
17370b57cec5SDimitry Andric 
17380b57cec5SDimitry Andric unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K,
17390b57cec5SDimitry Andric                                        MCBinaryExpr::Opcode &Kind) {
17400b57cec5SDimitry Andric   bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr();
17410b57cec5SDimitry Andric   return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr)
1742e8d8bef9SDimitry Andric                   : getGNUBinOpPrecedence(MAI, K, Kind, ShouldUseLogicalShr);
17430b57cec5SDimitry Andric }
17440b57cec5SDimitry Andric 
17450b57cec5SDimitry Andric /// Parse all binary operators with precedence >= 'Precedence'.
17460b57cec5SDimitry Andric /// Res contains the LHS of the expression on input.
17470b57cec5SDimitry Andric bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
17480b57cec5SDimitry Andric                               SMLoc &EndLoc) {
17490b57cec5SDimitry Andric   SMLoc StartLoc = Lexer.getLoc();
17500b57cec5SDimitry Andric   while (true) {
17510b57cec5SDimitry Andric     MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
17520b57cec5SDimitry Andric     unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
17530b57cec5SDimitry Andric 
17540b57cec5SDimitry Andric     // If the next token is lower precedence than we are allowed to eat, return
17550b57cec5SDimitry Andric     // successfully with what we ate already.
17560b57cec5SDimitry Andric     if (TokPrec < Precedence)
17570b57cec5SDimitry Andric       return false;
17580b57cec5SDimitry Andric 
17590b57cec5SDimitry Andric     Lex();
17600b57cec5SDimitry Andric 
17610b57cec5SDimitry Andric     // Eat the next primary expression.
17620b57cec5SDimitry Andric     const MCExpr *RHS;
17630b57cec5SDimitry Andric     if (getTargetParser().parsePrimaryExpr(RHS, EndLoc))
17640b57cec5SDimitry Andric       return true;
17650b57cec5SDimitry Andric 
17660b57cec5SDimitry Andric     // If BinOp binds less tightly with RHS than the operator after RHS, let
17670b57cec5SDimitry Andric     // the pending operator take RHS as its LHS.
17680b57cec5SDimitry Andric     MCBinaryExpr::Opcode Dummy;
17690b57cec5SDimitry Andric     unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
17700b57cec5SDimitry Andric     if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
17710b57cec5SDimitry Andric       return true;
17720b57cec5SDimitry Andric 
17730b57cec5SDimitry Andric     // Merge LHS and RHS according to operator.
17740b57cec5SDimitry Andric     Res = MCBinaryExpr::create(Kind, Res, RHS, getContext(), StartLoc);
17750b57cec5SDimitry Andric   }
17760b57cec5SDimitry Andric }
17770b57cec5SDimitry Andric 
17780b57cec5SDimitry Andric /// ParseStatement:
17790b57cec5SDimitry Andric ///   ::= EndOfStatement
17800b57cec5SDimitry Andric ///   ::= Label* Directive ...Operands... EndOfStatement
17810b57cec5SDimitry Andric ///   ::= Label* Identifier OperandList* EndOfStatement
17820b57cec5SDimitry Andric bool AsmParser::parseStatement(ParseStatementInfo &Info,
17830b57cec5SDimitry Andric                                MCAsmParserSemaCallback *SI) {
17840b57cec5SDimitry Andric   assert(!hasPendingError() && "parseStatement started with pending error");
17850b57cec5SDimitry Andric   // Eat initial spaces and comments
17860b57cec5SDimitry Andric   while (Lexer.is(AsmToken::Space))
17870b57cec5SDimitry Andric     Lex();
17880b57cec5SDimitry Andric   if (Lexer.is(AsmToken::EndOfStatement)) {
17890b57cec5SDimitry Andric     // if this is a line comment we can drop it safely
17900b57cec5SDimitry Andric     if (getTok().getString().empty() || getTok().getString().front() == '\r' ||
17910b57cec5SDimitry Andric         getTok().getString().front() == '\n')
179281ad6265SDimitry Andric       Out.addBlankLine();
17930b57cec5SDimitry Andric     Lex();
17940b57cec5SDimitry Andric     return false;
17950b57cec5SDimitry Andric   }
17960b57cec5SDimitry Andric   // Statements always start with an identifier.
17970b57cec5SDimitry Andric   AsmToken ID = getTok();
17980b57cec5SDimitry Andric   SMLoc IDLoc = ID.getLoc();
17990b57cec5SDimitry Andric   StringRef IDVal;
18000b57cec5SDimitry Andric   int64_t LocalLabelVal = -1;
1801e8d8bef9SDimitry Andric   StartTokLoc = ID.getLoc();
18020b57cec5SDimitry Andric   if (Lexer.is(AsmToken::HashDirective))
18035ffd83dbSDimitry Andric     return parseCppHashLineFilenameComment(IDLoc,
18045ffd83dbSDimitry Andric                                            !isInsideMacroInstantiation());
18055ffd83dbSDimitry Andric 
18060b57cec5SDimitry Andric   // Allow an integer followed by a ':' as a directional local label.
18070b57cec5SDimitry Andric   if (Lexer.is(AsmToken::Integer)) {
18080b57cec5SDimitry Andric     LocalLabelVal = getTok().getIntVal();
18090b57cec5SDimitry Andric     if (LocalLabelVal < 0) {
18100b57cec5SDimitry Andric       if (!TheCondState.Ignore) {
18110b57cec5SDimitry Andric         Lex(); // always eat a token
18120b57cec5SDimitry Andric         return Error(IDLoc, "unexpected token at start of statement");
18130b57cec5SDimitry Andric       }
18140b57cec5SDimitry Andric       IDVal = "";
18150b57cec5SDimitry Andric     } else {
18160b57cec5SDimitry Andric       IDVal = getTok().getString();
18170b57cec5SDimitry Andric       Lex(); // Consume the integer token to be used as an identifier token.
18180b57cec5SDimitry Andric       if (Lexer.getKind() != AsmToken::Colon) {
18190b57cec5SDimitry Andric         if (!TheCondState.Ignore) {
18200b57cec5SDimitry Andric           Lex(); // always eat a token
18210b57cec5SDimitry Andric           return Error(IDLoc, "unexpected token at start of statement");
18220b57cec5SDimitry Andric         }
18230b57cec5SDimitry Andric       }
18240b57cec5SDimitry Andric     }
18250b57cec5SDimitry Andric   } else if (Lexer.is(AsmToken::Dot)) {
18260b57cec5SDimitry Andric     // Treat '.' as a valid identifier in this context.
18270b57cec5SDimitry Andric     Lex();
18280b57cec5SDimitry Andric     IDVal = ".";
18290b57cec5SDimitry Andric   } else if (Lexer.is(AsmToken::LCurly)) {
18300b57cec5SDimitry Andric     // Treat '{' as a valid identifier in this context.
18310b57cec5SDimitry Andric     Lex();
18320b57cec5SDimitry Andric     IDVal = "{";
18330b57cec5SDimitry Andric 
18340b57cec5SDimitry Andric   } else if (Lexer.is(AsmToken::RCurly)) {
18350b57cec5SDimitry Andric     // Treat '}' as a valid identifier in this context.
18360b57cec5SDimitry Andric     Lex();
18370b57cec5SDimitry Andric     IDVal = "}";
18380b57cec5SDimitry Andric   } else if (Lexer.is(AsmToken::Star) &&
18390b57cec5SDimitry Andric              getTargetParser().starIsStartOfStatement()) {
18400b57cec5SDimitry Andric     // Accept '*' as a valid start of statement.
18410b57cec5SDimitry Andric     Lex();
18420b57cec5SDimitry Andric     IDVal = "*";
18430b57cec5SDimitry Andric   } else if (parseIdentifier(IDVal)) {
18440b57cec5SDimitry Andric     if (!TheCondState.Ignore) {
18450b57cec5SDimitry Andric       Lex(); // always eat a token
18460b57cec5SDimitry Andric       return Error(IDLoc, "unexpected token at start of statement");
18470b57cec5SDimitry Andric     }
18480b57cec5SDimitry Andric     IDVal = "";
18490b57cec5SDimitry Andric   }
18500b57cec5SDimitry Andric 
18510b57cec5SDimitry Andric   // Handle conditional assembly here before checking for skipping.  We
18520b57cec5SDimitry Andric   // have to do this so that .endif isn't skipped in a ".if 0" block for
18530b57cec5SDimitry Andric   // example.
18540b57cec5SDimitry Andric   StringMap<DirectiveKind>::const_iterator DirKindIt =
18555ffd83dbSDimitry Andric       DirectiveKindMap.find(IDVal.lower());
18560b57cec5SDimitry Andric   DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
18570b57cec5SDimitry Andric                               ? DK_NO_DIRECTIVE
18580b57cec5SDimitry Andric                               : DirKindIt->getValue();
18590b57cec5SDimitry Andric   switch (DirKind) {
18600b57cec5SDimitry Andric   default:
18610b57cec5SDimitry Andric     break;
18620b57cec5SDimitry Andric   case DK_IF:
18630b57cec5SDimitry Andric   case DK_IFEQ:
18640b57cec5SDimitry Andric   case DK_IFGE:
18650b57cec5SDimitry Andric   case DK_IFGT:
18660b57cec5SDimitry Andric   case DK_IFLE:
18670b57cec5SDimitry Andric   case DK_IFLT:
18680b57cec5SDimitry Andric   case DK_IFNE:
18690b57cec5SDimitry Andric     return parseDirectiveIf(IDLoc, DirKind);
18700b57cec5SDimitry Andric   case DK_IFB:
18710b57cec5SDimitry Andric     return parseDirectiveIfb(IDLoc, true);
18720b57cec5SDimitry Andric   case DK_IFNB:
18730b57cec5SDimitry Andric     return parseDirectiveIfb(IDLoc, false);
18740b57cec5SDimitry Andric   case DK_IFC:
18750b57cec5SDimitry Andric     return parseDirectiveIfc(IDLoc, true);
18760b57cec5SDimitry Andric   case DK_IFEQS:
18770b57cec5SDimitry Andric     return parseDirectiveIfeqs(IDLoc, true);
18780b57cec5SDimitry Andric   case DK_IFNC:
18790b57cec5SDimitry Andric     return parseDirectiveIfc(IDLoc, false);
18800b57cec5SDimitry Andric   case DK_IFNES:
18810b57cec5SDimitry Andric     return parseDirectiveIfeqs(IDLoc, false);
18820b57cec5SDimitry Andric   case DK_IFDEF:
18830b57cec5SDimitry Andric     return parseDirectiveIfdef(IDLoc, true);
18840b57cec5SDimitry Andric   case DK_IFNDEF:
18850b57cec5SDimitry Andric   case DK_IFNOTDEF:
18860b57cec5SDimitry Andric     return parseDirectiveIfdef(IDLoc, false);
18870b57cec5SDimitry Andric   case DK_ELSEIF:
18880b57cec5SDimitry Andric     return parseDirectiveElseIf(IDLoc);
18890b57cec5SDimitry Andric   case DK_ELSE:
18900b57cec5SDimitry Andric     return parseDirectiveElse(IDLoc);
18910b57cec5SDimitry Andric   case DK_ENDIF:
18920b57cec5SDimitry Andric     return parseDirectiveEndIf(IDLoc);
18930b57cec5SDimitry Andric   }
18940b57cec5SDimitry Andric 
18950b57cec5SDimitry Andric   // Ignore the statement if in the middle of inactive conditional
18960b57cec5SDimitry Andric   // (e.g. ".if 0").
18970b57cec5SDimitry Andric   if (TheCondState.Ignore) {
18980b57cec5SDimitry Andric     eatToEndOfStatement();
18990b57cec5SDimitry Andric     return false;
19000b57cec5SDimitry Andric   }
19010b57cec5SDimitry Andric 
19020b57cec5SDimitry Andric   // FIXME: Recurse on local labels?
19030b57cec5SDimitry Andric 
1904bdd1243dSDimitry Andric   // Check for a label.
1905bdd1243dSDimitry Andric   //   ::= identifier ':'
1906bdd1243dSDimitry Andric   //   ::= number ':'
1907bdd1243dSDimitry Andric   if (Lexer.is(AsmToken::Colon) && getTargetParser().isLabel(ID)) {
19080b57cec5SDimitry Andric     if (checkForValidSection())
19090b57cec5SDimitry Andric       return true;
19100b57cec5SDimitry Andric 
1911bdd1243dSDimitry Andric     Lex(); // Consume the ':'.
19120b57cec5SDimitry Andric 
19130b57cec5SDimitry Andric     // Diagnose attempt to use '.' as a label.
19140b57cec5SDimitry Andric     if (IDVal == ".")
19150b57cec5SDimitry Andric       return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
19160b57cec5SDimitry Andric 
19170b57cec5SDimitry Andric     // Diagnose attempt to use a variable as a label.
19180b57cec5SDimitry Andric     //
19190b57cec5SDimitry Andric     // FIXME: Diagnostics. Note the location of the definition as a label.
19200b57cec5SDimitry Andric     // FIXME: This doesn't diagnose assignment to a symbol which has been
19210b57cec5SDimitry Andric     // implicitly marked as external.
19220b57cec5SDimitry Andric     MCSymbol *Sym;
19230b57cec5SDimitry Andric     if (LocalLabelVal == -1) {
19245ffd83dbSDimitry Andric       if (ParsingMSInlineAsm && SI) {
19250b57cec5SDimitry Andric         StringRef RewrittenLabel =
19260b57cec5SDimitry Andric             SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
19270b57cec5SDimitry Andric         assert(!RewrittenLabel.empty() &&
19280b57cec5SDimitry Andric                "We should have an internal name here.");
19290b57cec5SDimitry Andric         Info.AsmRewrites->emplace_back(AOK_Label, IDLoc, IDVal.size(),
19300b57cec5SDimitry Andric                                        RewrittenLabel);
19310b57cec5SDimitry Andric         IDVal = RewrittenLabel;
19320b57cec5SDimitry Andric       }
19330b57cec5SDimitry Andric       Sym = getContext().getOrCreateSymbol(IDVal);
19340b57cec5SDimitry Andric     } else
19350b57cec5SDimitry Andric       Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal);
19360b57cec5SDimitry Andric     // End of Labels should be treated as end of line for lexing
19370b57cec5SDimitry Andric     // purposes but that information is not available to the Lexer who
19380b57cec5SDimitry Andric     // does not understand Labels. This may cause us to see a Hash
19390b57cec5SDimitry Andric     // here instead of a preprocessor line comment.
19400b57cec5SDimitry Andric     if (getTok().is(AsmToken::Hash)) {
19410b57cec5SDimitry Andric       StringRef CommentStr = parseStringToEndOfStatement();
19420b57cec5SDimitry Andric       Lexer.Lex();
19430b57cec5SDimitry Andric       Lexer.UnLex(AsmToken(AsmToken::EndOfStatement, CommentStr));
19440b57cec5SDimitry Andric     }
19450b57cec5SDimitry Andric 
19460b57cec5SDimitry Andric     // Consume any end of statement token, if present, to avoid spurious
194781ad6265SDimitry Andric     // addBlankLine calls().
19480b57cec5SDimitry Andric     if (getTok().is(AsmToken::EndOfStatement)) {
19490b57cec5SDimitry Andric       Lex();
19500b57cec5SDimitry Andric     }
19510b57cec5SDimitry Andric 
1952fe6060f1SDimitry Andric     if (discardLTOSymbol(IDVal))
1953fe6060f1SDimitry Andric       return false;
1954fe6060f1SDimitry Andric 
1955bdd1243dSDimitry Andric     getTargetParser().doBeforeLabelEmit(Sym, IDLoc);
19560b57cec5SDimitry Andric 
19570b57cec5SDimitry Andric     // Emit the label.
19585ffd83dbSDimitry Andric     if (!getTargetParser().isParsingMSInlineAsm())
19595ffd83dbSDimitry Andric       Out.emitLabel(Sym, IDLoc);
19600b57cec5SDimitry Andric 
19610b57cec5SDimitry Andric     // If we are generating dwarf for assembly source files then gather the
19620b57cec5SDimitry Andric     // info to make a dwarf label entry for this label if needed.
19630b57cec5SDimitry Andric     if (enabledGenDwarfForAssembly())
19640b57cec5SDimitry Andric       MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
19650b57cec5SDimitry Andric                                  IDLoc);
19660b57cec5SDimitry Andric 
19670b57cec5SDimitry Andric     getTargetParser().onLabelParsed(Sym);
19680b57cec5SDimitry Andric 
19690b57cec5SDimitry Andric     return false;
19700b57cec5SDimitry Andric   }
19710b57cec5SDimitry Andric 
1972bdd1243dSDimitry Andric   // Check for an assignment statement.
1973bdd1243dSDimitry Andric   //   ::= identifier '='
1974bdd1243dSDimitry Andric   if (Lexer.is(AsmToken::Equal) && getTargetParser().equalIsAsmAssignment()) {
19750b57cec5SDimitry Andric     Lex();
19760eae32dcSDimitry Andric     return parseAssignment(IDVal, AssignmentKind::Equal);
19770b57cec5SDimitry Andric   }
19780b57cec5SDimitry Andric 
19790b57cec5SDimitry Andric   // If macros are enabled, check to see if this is a macro instantiation.
19800b57cec5SDimitry Andric   if (areMacrosEnabled())
19810b57cec5SDimitry Andric     if (const MCAsmMacro *M = getContext().lookupMacro(IDVal)) {
19820b57cec5SDimitry Andric       return handleMacroEntry(M, IDLoc);
19830b57cec5SDimitry Andric     }
19840b57cec5SDimitry Andric 
19850b57cec5SDimitry Andric   // Otherwise, we have a normal instruction or directive.
19860b57cec5SDimitry Andric 
19870b57cec5SDimitry Andric   // Directives start with "."
19880b57cec5SDimitry Andric   if (IDVal.startswith(".") && IDVal != ".") {
19890b57cec5SDimitry Andric     // There are several entities interested in parsing directives:
19900b57cec5SDimitry Andric     //
19910b57cec5SDimitry Andric     // 1. The target-specific assembly parser. Some directives are target
19920b57cec5SDimitry Andric     //    specific or may potentially behave differently on certain targets.
19930b57cec5SDimitry Andric     // 2. Asm parser extensions. For example, platform-specific parsers
19940b57cec5SDimitry Andric     //    (like the ELF parser) register themselves as extensions.
19950b57cec5SDimitry Andric     // 3. The generic directive parser implemented by this class. These are
19960b57cec5SDimitry Andric     //    all the directives that behave in a target and platform independent
19970b57cec5SDimitry Andric     //    manner, or at least have a default behavior that's shared between
19980b57cec5SDimitry Andric     //    all targets and platforms.
19990b57cec5SDimitry Andric 
20000b57cec5SDimitry Andric     getTargetParser().flushPendingInstructions(getStreamer());
20010b57cec5SDimitry Andric 
2002*06c3fb27SDimitry Andric     ParseStatus TPDirectiveReturn = getTargetParser().parseDirective(ID);
2003*06c3fb27SDimitry Andric     assert(TPDirectiveReturn.isFailure() == hasPendingError() &&
2004*06c3fb27SDimitry Andric            "Should only return Failure iff there was an error");
2005*06c3fb27SDimitry Andric     if (TPDirectiveReturn.isFailure())
20060b57cec5SDimitry Andric       return true;
2007*06c3fb27SDimitry Andric     if (TPDirectiveReturn.isSuccess())
20080b57cec5SDimitry Andric       return false;
20090b57cec5SDimitry Andric 
20100b57cec5SDimitry Andric     // Next, check the extension directive map to see if any extension has
20110b57cec5SDimitry Andric     // registered itself to parse this directive.
20120b57cec5SDimitry Andric     std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
20130b57cec5SDimitry Andric         ExtensionDirectiveMap.lookup(IDVal);
20140b57cec5SDimitry Andric     if (Handler.first)
20150b57cec5SDimitry Andric       return (*Handler.second)(Handler.first, IDVal, IDLoc);
20160b57cec5SDimitry Andric 
20170b57cec5SDimitry Andric     // Finally, if no one else is interested in this directive, it must be
20180b57cec5SDimitry Andric     // generic and familiar to this class.
20190b57cec5SDimitry Andric     switch (DirKind) {
20200b57cec5SDimitry Andric     default:
20210b57cec5SDimitry Andric       break;
20220b57cec5SDimitry Andric     case DK_SET:
20230b57cec5SDimitry Andric     case DK_EQU:
20240eae32dcSDimitry Andric       return parseDirectiveSet(IDVal, AssignmentKind::Set);
20250b57cec5SDimitry Andric     case DK_EQUIV:
20260eae32dcSDimitry Andric       return parseDirectiveSet(IDVal, AssignmentKind::Equiv);
20270eae32dcSDimitry Andric     case DK_LTO_SET_CONDITIONAL:
20280eae32dcSDimitry Andric       return parseDirectiveSet(IDVal, AssignmentKind::LTOSetConditional);
20290b57cec5SDimitry Andric     case DK_ASCII:
20300b57cec5SDimitry Andric       return parseDirectiveAscii(IDVal, false);
20310b57cec5SDimitry Andric     case DK_ASCIZ:
20320b57cec5SDimitry Andric     case DK_STRING:
20330b57cec5SDimitry Andric       return parseDirectiveAscii(IDVal, true);
20340b57cec5SDimitry Andric     case DK_BYTE:
20350b57cec5SDimitry Andric     case DK_DC_B:
20360b57cec5SDimitry Andric       return parseDirectiveValue(IDVal, 1);
20370b57cec5SDimitry Andric     case DK_DC:
20380b57cec5SDimitry Andric     case DK_DC_W:
20390b57cec5SDimitry Andric     case DK_SHORT:
20400b57cec5SDimitry Andric     case DK_VALUE:
20410b57cec5SDimitry Andric     case DK_2BYTE:
20420b57cec5SDimitry Andric       return parseDirectiveValue(IDVal, 2);
20430b57cec5SDimitry Andric     case DK_LONG:
20440b57cec5SDimitry Andric     case DK_INT:
20450b57cec5SDimitry Andric     case DK_4BYTE:
20460b57cec5SDimitry Andric     case DK_DC_L:
20470b57cec5SDimitry Andric       return parseDirectiveValue(IDVal, 4);
20480b57cec5SDimitry Andric     case DK_QUAD:
20490b57cec5SDimitry Andric     case DK_8BYTE:
20500b57cec5SDimitry Andric       return parseDirectiveValue(IDVal, 8);
20510b57cec5SDimitry Andric     case DK_DC_A:
20520b57cec5SDimitry Andric       return parseDirectiveValue(
20530b57cec5SDimitry Andric           IDVal, getContext().getAsmInfo()->getCodePointerSize());
20540b57cec5SDimitry Andric     case DK_OCTA:
20550b57cec5SDimitry Andric       return parseDirectiveOctaValue(IDVal);
20560b57cec5SDimitry Andric     case DK_SINGLE:
20570b57cec5SDimitry Andric     case DK_FLOAT:
20580b57cec5SDimitry Andric     case DK_DC_S:
20590b57cec5SDimitry Andric       return parseDirectiveRealValue(IDVal, APFloat::IEEEsingle());
20600b57cec5SDimitry Andric     case DK_DOUBLE:
20610b57cec5SDimitry Andric     case DK_DC_D:
20620b57cec5SDimitry Andric       return parseDirectiveRealValue(IDVal, APFloat::IEEEdouble());
20630b57cec5SDimitry Andric     case DK_ALIGN: {
20640b57cec5SDimitry Andric       bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
20650b57cec5SDimitry Andric       return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
20660b57cec5SDimitry Andric     }
20670b57cec5SDimitry Andric     case DK_ALIGN32: {
20680b57cec5SDimitry Andric       bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
20690b57cec5SDimitry Andric       return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
20700b57cec5SDimitry Andric     }
20710b57cec5SDimitry Andric     case DK_BALIGN:
20720b57cec5SDimitry Andric       return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
20730b57cec5SDimitry Andric     case DK_BALIGNW:
20740b57cec5SDimitry Andric       return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
20750b57cec5SDimitry Andric     case DK_BALIGNL:
20760b57cec5SDimitry Andric       return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
20770b57cec5SDimitry Andric     case DK_P2ALIGN:
20780b57cec5SDimitry Andric       return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
20790b57cec5SDimitry Andric     case DK_P2ALIGNW:
20800b57cec5SDimitry Andric       return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
20810b57cec5SDimitry Andric     case DK_P2ALIGNL:
20820b57cec5SDimitry Andric       return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
20830b57cec5SDimitry Andric     case DK_ORG:
20840b57cec5SDimitry Andric       return parseDirectiveOrg();
20850b57cec5SDimitry Andric     case DK_FILL:
20860b57cec5SDimitry Andric       return parseDirectiveFill();
20870b57cec5SDimitry Andric     case DK_ZERO:
20880b57cec5SDimitry Andric       return parseDirectiveZero();
20890b57cec5SDimitry Andric     case DK_EXTERN:
20900b57cec5SDimitry Andric       eatToEndOfStatement(); // .extern is the default, ignore it.
20910b57cec5SDimitry Andric       return false;
20920b57cec5SDimitry Andric     case DK_GLOBL:
20930b57cec5SDimitry Andric     case DK_GLOBAL:
20940b57cec5SDimitry Andric       return parseDirectiveSymbolAttribute(MCSA_Global);
20950b57cec5SDimitry Andric     case DK_LAZY_REFERENCE:
20960b57cec5SDimitry Andric       return parseDirectiveSymbolAttribute(MCSA_LazyReference);
20970b57cec5SDimitry Andric     case DK_NO_DEAD_STRIP:
20980b57cec5SDimitry Andric       return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
20990b57cec5SDimitry Andric     case DK_SYMBOL_RESOLVER:
21000b57cec5SDimitry Andric       return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
21010b57cec5SDimitry Andric     case DK_PRIVATE_EXTERN:
21020b57cec5SDimitry Andric       return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
21030b57cec5SDimitry Andric     case DK_REFERENCE:
21040b57cec5SDimitry Andric       return parseDirectiveSymbolAttribute(MCSA_Reference);
21050b57cec5SDimitry Andric     case DK_WEAK_DEFINITION:
21060b57cec5SDimitry Andric       return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
21070b57cec5SDimitry Andric     case DK_WEAK_REFERENCE:
21080b57cec5SDimitry Andric       return parseDirectiveSymbolAttribute(MCSA_WeakReference);
21090b57cec5SDimitry Andric     case DK_WEAK_DEF_CAN_BE_HIDDEN:
21100b57cec5SDimitry Andric       return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
21110b57cec5SDimitry Andric     case DK_COLD:
21120b57cec5SDimitry Andric       return parseDirectiveSymbolAttribute(MCSA_Cold);
21130b57cec5SDimitry Andric     case DK_COMM:
21140b57cec5SDimitry Andric     case DK_COMMON:
21150b57cec5SDimitry Andric       return parseDirectiveComm(/*IsLocal=*/false);
21160b57cec5SDimitry Andric     case DK_LCOMM:
21170b57cec5SDimitry Andric       return parseDirectiveComm(/*IsLocal=*/true);
21180b57cec5SDimitry Andric     case DK_ABORT:
21190b57cec5SDimitry Andric       return parseDirectiveAbort();
21200b57cec5SDimitry Andric     case DK_INCLUDE:
21210b57cec5SDimitry Andric       return parseDirectiveInclude();
21220b57cec5SDimitry Andric     case DK_INCBIN:
21230b57cec5SDimitry Andric       return parseDirectiveIncbin();
21240b57cec5SDimitry Andric     case DK_CODE16:
21250b57cec5SDimitry Andric     case DK_CODE16GCC:
21260b57cec5SDimitry Andric       return TokError(Twine(IDVal) +
21270b57cec5SDimitry Andric                       " not currently supported for this target");
21280b57cec5SDimitry Andric     case DK_REPT:
21290b57cec5SDimitry Andric       return parseDirectiveRept(IDLoc, IDVal);
21300b57cec5SDimitry Andric     case DK_IRP:
21310b57cec5SDimitry Andric       return parseDirectiveIrp(IDLoc);
21320b57cec5SDimitry Andric     case DK_IRPC:
21330b57cec5SDimitry Andric       return parseDirectiveIrpc(IDLoc);
21340b57cec5SDimitry Andric     case DK_ENDR:
21350b57cec5SDimitry Andric       return parseDirectiveEndr(IDLoc);
21360b57cec5SDimitry Andric     case DK_BUNDLE_ALIGN_MODE:
21370b57cec5SDimitry Andric       return parseDirectiveBundleAlignMode();
21380b57cec5SDimitry Andric     case DK_BUNDLE_LOCK:
21390b57cec5SDimitry Andric       return parseDirectiveBundleLock();
21400b57cec5SDimitry Andric     case DK_BUNDLE_UNLOCK:
21410b57cec5SDimitry Andric       return parseDirectiveBundleUnlock();
21420b57cec5SDimitry Andric     case DK_SLEB128:
21430b57cec5SDimitry Andric       return parseDirectiveLEB128(true);
21440b57cec5SDimitry Andric     case DK_ULEB128:
21450b57cec5SDimitry Andric       return parseDirectiveLEB128(false);
21460b57cec5SDimitry Andric     case DK_SPACE:
21470b57cec5SDimitry Andric     case DK_SKIP:
21480b57cec5SDimitry Andric       return parseDirectiveSpace(IDVal);
21490b57cec5SDimitry Andric     case DK_FILE:
21500b57cec5SDimitry Andric       return parseDirectiveFile(IDLoc);
21510b57cec5SDimitry Andric     case DK_LINE:
21520b57cec5SDimitry Andric       return parseDirectiveLine();
21530b57cec5SDimitry Andric     case DK_LOC:
21540b57cec5SDimitry Andric       return parseDirectiveLoc();
21550b57cec5SDimitry Andric     case DK_STABS:
21560b57cec5SDimitry Andric       return parseDirectiveStabs();
21570b57cec5SDimitry Andric     case DK_CV_FILE:
21580b57cec5SDimitry Andric       return parseDirectiveCVFile();
21590b57cec5SDimitry Andric     case DK_CV_FUNC_ID:
21600b57cec5SDimitry Andric       return parseDirectiveCVFuncId();
21610b57cec5SDimitry Andric     case DK_CV_INLINE_SITE_ID:
21620b57cec5SDimitry Andric       return parseDirectiveCVInlineSiteId();
21630b57cec5SDimitry Andric     case DK_CV_LOC:
21640b57cec5SDimitry Andric       return parseDirectiveCVLoc();
21650b57cec5SDimitry Andric     case DK_CV_LINETABLE:
21660b57cec5SDimitry Andric       return parseDirectiveCVLinetable();
21670b57cec5SDimitry Andric     case DK_CV_INLINE_LINETABLE:
21680b57cec5SDimitry Andric       return parseDirectiveCVInlineLinetable();
21690b57cec5SDimitry Andric     case DK_CV_DEF_RANGE:
21700b57cec5SDimitry Andric       return parseDirectiveCVDefRange();
21710b57cec5SDimitry Andric     case DK_CV_STRING:
21720b57cec5SDimitry Andric       return parseDirectiveCVString();
21730b57cec5SDimitry Andric     case DK_CV_STRINGTABLE:
21740b57cec5SDimitry Andric       return parseDirectiveCVStringTable();
21750b57cec5SDimitry Andric     case DK_CV_FILECHECKSUMS:
21760b57cec5SDimitry Andric       return parseDirectiveCVFileChecksums();
21770b57cec5SDimitry Andric     case DK_CV_FILECHECKSUM_OFFSET:
21780b57cec5SDimitry Andric       return parseDirectiveCVFileChecksumOffset();
21790b57cec5SDimitry Andric     case DK_CV_FPO_DATA:
21800b57cec5SDimitry Andric       return parseDirectiveCVFPOData();
21810b57cec5SDimitry Andric     case DK_CFI_SECTIONS:
21820b57cec5SDimitry Andric       return parseDirectiveCFISections();
21830b57cec5SDimitry Andric     case DK_CFI_STARTPROC:
21840b57cec5SDimitry Andric       return parseDirectiveCFIStartProc();
21850b57cec5SDimitry Andric     case DK_CFI_ENDPROC:
21860b57cec5SDimitry Andric       return parseDirectiveCFIEndProc();
21870b57cec5SDimitry Andric     case DK_CFI_DEF_CFA:
21880b57cec5SDimitry Andric       return parseDirectiveCFIDefCfa(IDLoc);
21890b57cec5SDimitry Andric     case DK_CFI_DEF_CFA_OFFSET:
2190*06c3fb27SDimitry Andric       return parseDirectiveCFIDefCfaOffset(IDLoc);
21910b57cec5SDimitry Andric     case DK_CFI_ADJUST_CFA_OFFSET:
2192*06c3fb27SDimitry Andric       return parseDirectiveCFIAdjustCfaOffset(IDLoc);
21930b57cec5SDimitry Andric     case DK_CFI_DEF_CFA_REGISTER:
21940b57cec5SDimitry Andric       return parseDirectiveCFIDefCfaRegister(IDLoc);
2195fe6060f1SDimitry Andric     case DK_CFI_LLVM_DEF_ASPACE_CFA:
2196fe6060f1SDimitry Andric       return parseDirectiveCFILLVMDefAspaceCfa(IDLoc);
21970b57cec5SDimitry Andric     case DK_CFI_OFFSET:
21980b57cec5SDimitry Andric       return parseDirectiveCFIOffset(IDLoc);
21990b57cec5SDimitry Andric     case DK_CFI_REL_OFFSET:
22000b57cec5SDimitry Andric       return parseDirectiveCFIRelOffset(IDLoc);
22010b57cec5SDimitry Andric     case DK_CFI_PERSONALITY:
22020b57cec5SDimitry Andric       return parseDirectiveCFIPersonalityOrLsda(true);
22030b57cec5SDimitry Andric     case DK_CFI_LSDA:
22040b57cec5SDimitry Andric       return parseDirectiveCFIPersonalityOrLsda(false);
22050b57cec5SDimitry Andric     case DK_CFI_REMEMBER_STATE:
2206*06c3fb27SDimitry Andric       return parseDirectiveCFIRememberState(IDLoc);
22070b57cec5SDimitry Andric     case DK_CFI_RESTORE_STATE:
2208*06c3fb27SDimitry Andric       return parseDirectiveCFIRestoreState(IDLoc);
22090b57cec5SDimitry Andric     case DK_CFI_SAME_VALUE:
22100b57cec5SDimitry Andric       return parseDirectiveCFISameValue(IDLoc);
22110b57cec5SDimitry Andric     case DK_CFI_RESTORE:
22120b57cec5SDimitry Andric       return parseDirectiveCFIRestore(IDLoc);
22130b57cec5SDimitry Andric     case DK_CFI_ESCAPE:
2214*06c3fb27SDimitry Andric       return parseDirectiveCFIEscape(IDLoc);
22150b57cec5SDimitry Andric     case DK_CFI_RETURN_COLUMN:
22160b57cec5SDimitry Andric       return parseDirectiveCFIReturnColumn(IDLoc);
22170b57cec5SDimitry Andric     case DK_CFI_SIGNAL_FRAME:
2218*06c3fb27SDimitry Andric       return parseDirectiveCFISignalFrame(IDLoc);
22190b57cec5SDimitry Andric     case DK_CFI_UNDEFINED:
22200b57cec5SDimitry Andric       return parseDirectiveCFIUndefined(IDLoc);
22210b57cec5SDimitry Andric     case DK_CFI_REGISTER:
22220b57cec5SDimitry Andric       return parseDirectiveCFIRegister(IDLoc);
22230b57cec5SDimitry Andric     case DK_CFI_WINDOW_SAVE:
2224*06c3fb27SDimitry Andric       return parseDirectiveCFIWindowSave(IDLoc);
22250b57cec5SDimitry Andric     case DK_MACROS_ON:
22260b57cec5SDimitry Andric     case DK_MACROS_OFF:
22270b57cec5SDimitry Andric       return parseDirectiveMacrosOnOff(IDVal);
22280b57cec5SDimitry Andric     case DK_MACRO:
22290b57cec5SDimitry Andric       return parseDirectiveMacro(IDLoc);
22300b57cec5SDimitry Andric     case DK_ALTMACRO:
22310b57cec5SDimitry Andric     case DK_NOALTMACRO:
22320b57cec5SDimitry Andric       return parseDirectiveAltmacro(IDVal);
22330b57cec5SDimitry Andric     case DK_EXITM:
22340b57cec5SDimitry Andric       return parseDirectiveExitMacro(IDVal);
22350b57cec5SDimitry Andric     case DK_ENDM:
22360b57cec5SDimitry Andric     case DK_ENDMACRO:
22370b57cec5SDimitry Andric       return parseDirectiveEndMacro(IDVal);
22380b57cec5SDimitry Andric     case DK_PURGEM:
22390b57cec5SDimitry Andric       return parseDirectivePurgeMacro(IDLoc);
22400b57cec5SDimitry Andric     case DK_END:
22410b57cec5SDimitry Andric       return parseDirectiveEnd(IDLoc);
22420b57cec5SDimitry Andric     case DK_ERR:
22430b57cec5SDimitry Andric       return parseDirectiveError(IDLoc, false);
22440b57cec5SDimitry Andric     case DK_ERROR:
22450b57cec5SDimitry Andric       return parseDirectiveError(IDLoc, true);
22460b57cec5SDimitry Andric     case DK_WARNING:
22470b57cec5SDimitry Andric       return parseDirectiveWarning(IDLoc);
22480b57cec5SDimitry Andric     case DK_RELOC:
22490b57cec5SDimitry Andric       return parseDirectiveReloc(IDLoc);
22500b57cec5SDimitry Andric     case DK_DCB:
22510b57cec5SDimitry Andric     case DK_DCB_W:
22520b57cec5SDimitry Andric       return parseDirectiveDCB(IDVal, 2);
22530b57cec5SDimitry Andric     case DK_DCB_B:
22540b57cec5SDimitry Andric       return parseDirectiveDCB(IDVal, 1);
22550b57cec5SDimitry Andric     case DK_DCB_D:
22560b57cec5SDimitry Andric       return parseDirectiveRealDCB(IDVal, APFloat::IEEEdouble());
22570b57cec5SDimitry Andric     case DK_DCB_L:
22580b57cec5SDimitry Andric       return parseDirectiveDCB(IDVal, 4);
22590b57cec5SDimitry Andric     case DK_DCB_S:
22600b57cec5SDimitry Andric       return parseDirectiveRealDCB(IDVal, APFloat::IEEEsingle());
22610b57cec5SDimitry Andric     case DK_DC_X:
22620b57cec5SDimitry Andric     case DK_DCB_X:
22630b57cec5SDimitry Andric       return TokError(Twine(IDVal) +
22640b57cec5SDimitry Andric                       " not currently supported for this target");
22650b57cec5SDimitry Andric     case DK_DS:
22660b57cec5SDimitry Andric     case DK_DS_W:
22670b57cec5SDimitry Andric       return parseDirectiveDS(IDVal, 2);
22680b57cec5SDimitry Andric     case DK_DS_B:
22690b57cec5SDimitry Andric       return parseDirectiveDS(IDVal, 1);
22700b57cec5SDimitry Andric     case DK_DS_D:
22710b57cec5SDimitry Andric       return parseDirectiveDS(IDVal, 8);
22720b57cec5SDimitry Andric     case DK_DS_L:
22730b57cec5SDimitry Andric     case DK_DS_S:
22740b57cec5SDimitry Andric       return parseDirectiveDS(IDVal, 4);
22750b57cec5SDimitry Andric     case DK_DS_P:
22760b57cec5SDimitry Andric     case DK_DS_X:
22770b57cec5SDimitry Andric       return parseDirectiveDS(IDVal, 12);
22780b57cec5SDimitry Andric     case DK_PRINT:
22790b57cec5SDimitry Andric       return parseDirectivePrint(IDLoc);
22800b57cec5SDimitry Andric     case DK_ADDRSIG:
22810b57cec5SDimitry Andric       return parseDirectiveAddrsig();
22820b57cec5SDimitry Andric     case DK_ADDRSIG_SYM:
22830b57cec5SDimitry Andric       return parseDirectiveAddrsigSym();
2284e8d8bef9SDimitry Andric     case DK_PSEUDO_PROBE:
2285e8d8bef9SDimitry Andric       return parseDirectivePseudoProbe();
2286fe6060f1SDimitry Andric     case DK_LTO_DISCARD:
2287fe6060f1SDimitry Andric       return parseDirectiveLTODiscard();
2288bdd1243dSDimitry Andric     case DK_MEMTAG:
2289bdd1243dSDimitry Andric       return parseDirectiveSymbolAttribute(MCSA_Memtag);
22900b57cec5SDimitry Andric     }
22910b57cec5SDimitry Andric 
22920b57cec5SDimitry Andric     return Error(IDLoc, "unknown directive");
22930b57cec5SDimitry Andric   }
22940b57cec5SDimitry Andric 
22950b57cec5SDimitry Andric   // __asm _emit or __asm __emit
22965ffd83dbSDimitry Andric   if (ParsingMSInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
22970b57cec5SDimitry Andric                              IDVal == "_EMIT" || IDVal == "__EMIT"))
22980b57cec5SDimitry Andric     return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
22990b57cec5SDimitry Andric 
23000b57cec5SDimitry Andric   // __asm align
23015ffd83dbSDimitry Andric   if (ParsingMSInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
23020b57cec5SDimitry Andric     return parseDirectiveMSAlign(IDLoc, Info);
23030b57cec5SDimitry Andric 
23045ffd83dbSDimitry Andric   if (ParsingMSInlineAsm && (IDVal == "even" || IDVal == "EVEN"))
23050b57cec5SDimitry Andric     Info.AsmRewrites->emplace_back(AOK_EVEN, IDLoc, 4);
23060b57cec5SDimitry Andric   if (checkForValidSection())
23070b57cec5SDimitry Andric     return true;
23080b57cec5SDimitry Andric 
2309fe6060f1SDimitry Andric   return parseAndMatchAndEmitTargetInstruction(Info, IDVal, ID, IDLoc);
2310fe6060f1SDimitry Andric }
2311fe6060f1SDimitry Andric 
2312fe6060f1SDimitry Andric bool AsmParser::parseAndMatchAndEmitTargetInstruction(ParseStatementInfo &Info,
2313fe6060f1SDimitry Andric                                                       StringRef IDVal,
2314fe6060f1SDimitry Andric                                                       AsmToken ID,
2315fe6060f1SDimitry Andric                                                       SMLoc IDLoc) {
23160b57cec5SDimitry Andric   // Canonicalize the opcode to lower case.
23170b57cec5SDimitry Andric   std::string OpcodeStr = IDVal.lower();
23180b57cec5SDimitry Andric   ParseInstructionInfo IInfo(Info.AsmRewrites);
23190b57cec5SDimitry Andric   bool ParseHadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, ID,
23200b57cec5SDimitry Andric                                                           Info.ParsedOperands);
23210b57cec5SDimitry Andric   Info.ParseError = ParseHadError;
23220b57cec5SDimitry Andric 
23230b57cec5SDimitry Andric   // Dump the parsed representation, if requested.
23240b57cec5SDimitry Andric   if (getShowParsedOperands()) {
23250b57cec5SDimitry Andric     SmallString<256> Str;
23260b57cec5SDimitry Andric     raw_svector_ostream OS(Str);
23270b57cec5SDimitry Andric     OS << "parsed instruction: [";
23280b57cec5SDimitry Andric     for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
23290b57cec5SDimitry Andric       if (i != 0)
23300b57cec5SDimitry Andric         OS << ", ";
23310b57cec5SDimitry Andric       Info.ParsedOperands[i]->print(OS);
23320b57cec5SDimitry Andric     }
23330b57cec5SDimitry Andric     OS << "]";
23340b57cec5SDimitry Andric 
23350b57cec5SDimitry Andric     printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
23360b57cec5SDimitry Andric   }
23370b57cec5SDimitry Andric 
23380b57cec5SDimitry Andric   // Fail even if ParseInstruction erroneously returns false.
23390b57cec5SDimitry Andric   if (hasPendingError() || ParseHadError)
23400b57cec5SDimitry Andric     return true;
23410b57cec5SDimitry Andric 
23420b57cec5SDimitry Andric   // If we are generating dwarf for the current section then generate a .loc
23430b57cec5SDimitry Andric   // directive for the instruction.
23440b57cec5SDimitry Andric   if (!ParseHadError && enabledGenDwarfForAssembly() &&
23450b57cec5SDimitry Andric       getContext().getGenDwarfSectionSyms().count(
23460b57cec5SDimitry Andric           getStreamer().getCurrentSectionOnly())) {
23470b57cec5SDimitry Andric     unsigned Line;
23480b57cec5SDimitry Andric     if (ActiveMacros.empty())
23490b57cec5SDimitry Andric       Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
23500b57cec5SDimitry Andric     else
23510b57cec5SDimitry Andric       Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc,
23520b57cec5SDimitry Andric                                    ActiveMacros.front()->ExitBuffer);
23530b57cec5SDimitry Andric 
23540b57cec5SDimitry Andric     // If we previously parsed a cpp hash file line comment then make sure the
23550b57cec5SDimitry Andric     // current Dwarf File is for the CppHashFilename if not then emit the
23560b57cec5SDimitry Andric     // Dwarf File table for it and adjust the line number for the .loc.
23570b57cec5SDimitry Andric     if (!CppHashInfo.Filename.empty()) {
23585ffd83dbSDimitry Andric       unsigned FileNumber = getStreamer().emitDwarfFileDirective(
23590b57cec5SDimitry Andric           0, StringRef(), CppHashInfo.Filename);
23600b57cec5SDimitry Andric       getContext().setGenDwarfFileNumber(FileNumber);
23610b57cec5SDimitry Andric 
23620b57cec5SDimitry Andric       unsigned CppHashLocLineNo =
23630b57cec5SDimitry Andric         SrcMgr.FindLineNumber(CppHashInfo.Loc, CppHashInfo.Buf);
23640b57cec5SDimitry Andric       Line = CppHashInfo.LineNumber - 1 + (Line - CppHashLocLineNo);
23650b57cec5SDimitry Andric     }
23660b57cec5SDimitry Andric 
23675ffd83dbSDimitry Andric     getStreamer().emitDwarfLocDirective(
23680b57cec5SDimitry Andric         getContext().getGenDwarfFileNumber(), Line, 0,
23690b57cec5SDimitry Andric         DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
23700b57cec5SDimitry Andric         StringRef());
23710b57cec5SDimitry Andric   }
23720b57cec5SDimitry Andric 
23730b57cec5SDimitry Andric   // If parsing succeeded, match the instruction.
23740b57cec5SDimitry Andric   if (!ParseHadError) {
23750b57cec5SDimitry Andric     uint64_t ErrorInfo;
23760b57cec5SDimitry Andric     if (getTargetParser().MatchAndEmitInstruction(
23770b57cec5SDimitry Andric             IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo,
23785ffd83dbSDimitry Andric             getTargetParser().isParsingMSInlineAsm()))
23790b57cec5SDimitry Andric       return true;
23800b57cec5SDimitry Andric   }
23810b57cec5SDimitry Andric   return false;
23820b57cec5SDimitry Andric }
23830b57cec5SDimitry Andric 
23840b57cec5SDimitry Andric // Parse and erase curly braces marking block start/end
23850b57cec5SDimitry Andric bool
23860b57cec5SDimitry Andric AsmParser::parseCurlyBlockScope(SmallVectorImpl<AsmRewrite> &AsmStrRewrites) {
23870b57cec5SDimitry Andric   // Identify curly brace marking block start/end
23880b57cec5SDimitry Andric   if (Lexer.isNot(AsmToken::LCurly) && Lexer.isNot(AsmToken::RCurly))
23890b57cec5SDimitry Andric     return false;
23900b57cec5SDimitry Andric 
23910b57cec5SDimitry Andric   SMLoc StartLoc = Lexer.getLoc();
23920b57cec5SDimitry Andric   Lex(); // Eat the brace
23930b57cec5SDimitry Andric   if (Lexer.is(AsmToken::EndOfStatement))
23940b57cec5SDimitry Andric     Lex(); // Eat EndOfStatement following the brace
23950b57cec5SDimitry Andric 
23960b57cec5SDimitry Andric   // Erase the block start/end brace from the output asm string
23970b57cec5SDimitry Andric   AsmStrRewrites.emplace_back(AOK_Skip, StartLoc, Lexer.getLoc().getPointer() -
23980b57cec5SDimitry Andric                                                   StartLoc.getPointer());
23990b57cec5SDimitry Andric   return true;
24000b57cec5SDimitry Andric }
24010b57cec5SDimitry Andric 
24020b57cec5SDimitry Andric /// parseCppHashLineFilenameComment as this:
24030b57cec5SDimitry Andric ///   ::= # number "filename"
24045ffd83dbSDimitry Andric bool AsmParser::parseCppHashLineFilenameComment(SMLoc L, bool SaveLocInfo) {
24050b57cec5SDimitry Andric   Lex(); // Eat the hash token.
24060b57cec5SDimitry Andric   // Lexer only ever emits HashDirective if it fully formed if it's
24070b57cec5SDimitry Andric   // done the checking already so this is an internal error.
24080b57cec5SDimitry Andric   assert(getTok().is(AsmToken::Integer) &&
24090b57cec5SDimitry Andric          "Lexing Cpp line comment: Expected Integer");
24100b57cec5SDimitry Andric   int64_t LineNumber = getTok().getIntVal();
24110b57cec5SDimitry Andric   Lex();
24120b57cec5SDimitry Andric   assert(getTok().is(AsmToken::String) &&
24130b57cec5SDimitry Andric          "Lexing Cpp line comment: Expected String");
24140b57cec5SDimitry Andric   StringRef Filename = getTok().getString();
24150b57cec5SDimitry Andric   Lex();
24160b57cec5SDimitry Andric 
24175ffd83dbSDimitry Andric   if (!SaveLocInfo)
24185ffd83dbSDimitry Andric     return false;
24195ffd83dbSDimitry Andric 
24200b57cec5SDimitry Andric   // Get rid of the enclosing quotes.
24210b57cec5SDimitry Andric   Filename = Filename.substr(1, Filename.size() - 2);
24220b57cec5SDimitry Andric 
24230b57cec5SDimitry Andric   // Save the SMLoc, Filename and LineNumber for later use by diagnostics
24240b57cec5SDimitry Andric   // and possibly DWARF file info.
24250b57cec5SDimitry Andric   CppHashInfo.Loc = L;
24260b57cec5SDimitry Andric   CppHashInfo.Filename = Filename;
24270b57cec5SDimitry Andric   CppHashInfo.LineNumber = LineNumber;
24280b57cec5SDimitry Andric   CppHashInfo.Buf = CurBuffer;
24290b57cec5SDimitry Andric   if (FirstCppHashFilename.empty())
24300b57cec5SDimitry Andric     FirstCppHashFilename = Filename;
24310b57cec5SDimitry Andric   return false;
24320b57cec5SDimitry Andric }
24330b57cec5SDimitry Andric 
24340b57cec5SDimitry Andric /// will use the last parsed cpp hash line filename comment
24350b57cec5SDimitry Andric /// for the Filename and LineNo if any in the diagnostic.
24360b57cec5SDimitry Andric void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
2437fe6060f1SDimitry Andric   auto *Parser = static_cast<AsmParser *>(Context);
24380b57cec5SDimitry Andric   raw_ostream &OS = errs();
24390b57cec5SDimitry Andric 
24400b57cec5SDimitry Andric   const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
24410b57cec5SDimitry Andric   SMLoc DiagLoc = Diag.getLoc();
24420b57cec5SDimitry Andric   unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
24430b57cec5SDimitry Andric   unsigned CppHashBuf =
24440b57cec5SDimitry Andric       Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashInfo.Loc);
24450b57cec5SDimitry Andric 
24460b57cec5SDimitry Andric   // Like SourceMgr::printMessage() we need to print the include stack if any
24470b57cec5SDimitry Andric   // before printing the message.
24480b57cec5SDimitry Andric   unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
24490b57cec5SDimitry Andric   if (!Parser->SavedDiagHandler && DiagCurBuffer &&
24500b57cec5SDimitry Andric       DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
24510b57cec5SDimitry Andric     SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
24520b57cec5SDimitry Andric     DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
24530b57cec5SDimitry Andric   }
24540b57cec5SDimitry Andric 
24550b57cec5SDimitry Andric   // If we have not parsed a cpp hash line filename comment or the source
24560b57cec5SDimitry Andric   // manager changed or buffer changed (like in a nested include) then just
24570b57cec5SDimitry Andric   // print the normal diagnostic using its Filename and LineNo.
2458fe6060f1SDimitry Andric   if (!Parser->CppHashInfo.LineNumber || DiagBuf != CppHashBuf) {
24590b57cec5SDimitry Andric     if (Parser->SavedDiagHandler)
24600b57cec5SDimitry Andric       Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
24610b57cec5SDimitry Andric     else
2462fe6060f1SDimitry Andric       Parser->getContext().diagnose(Diag);
24630b57cec5SDimitry Andric     return;
24640b57cec5SDimitry Andric   }
24650b57cec5SDimitry Andric 
24660b57cec5SDimitry Andric   // Use the CppHashFilename and calculate a line number based on the
24670b57cec5SDimitry Andric   // CppHashInfo.Loc and CppHashInfo.LineNumber relative to this Diag's SMLoc
24680b57cec5SDimitry Andric   // for the diagnostic.
24695ffd83dbSDimitry Andric   const std::string &Filename = std::string(Parser->CppHashInfo.Filename);
24700b57cec5SDimitry Andric 
24710b57cec5SDimitry Andric   int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
24720b57cec5SDimitry Andric   int CppHashLocLineNo =
24730b57cec5SDimitry Andric       Parser->SrcMgr.FindLineNumber(Parser->CppHashInfo.Loc, CppHashBuf);
24740b57cec5SDimitry Andric   int LineNo =
24750b57cec5SDimitry Andric       Parser->CppHashInfo.LineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
24760b57cec5SDimitry Andric 
24770b57cec5SDimitry Andric   SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
24780b57cec5SDimitry Andric                        Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
24790b57cec5SDimitry Andric                        Diag.getLineContents(), Diag.getRanges());
24800b57cec5SDimitry Andric 
24810b57cec5SDimitry Andric   if (Parser->SavedDiagHandler)
2482fe6060f1SDimitry Andric     Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
24830b57cec5SDimitry Andric   else
2484fe6060f1SDimitry Andric     Parser->getContext().diagnose(NewDiag);
24850b57cec5SDimitry Andric }
24860b57cec5SDimitry Andric 
24870b57cec5SDimitry Andric // FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
24880b57cec5SDimitry Andric // difference being that that function accepts '@' as part of identifiers and
24890b57cec5SDimitry Andric // we can't do that. AsmLexer.cpp should probably be changed to handle
24900b57cec5SDimitry Andric // '@' as a special case when needed.
24910b57cec5SDimitry Andric static bool isIdentifierChar(char c) {
24920b57cec5SDimitry Andric   return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
24930b57cec5SDimitry Andric          c == '.';
24940b57cec5SDimitry Andric }
24950b57cec5SDimitry Andric 
24960b57cec5SDimitry Andric bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
24970b57cec5SDimitry Andric                             ArrayRef<MCAsmMacroParameter> Parameters,
24980b57cec5SDimitry Andric                             ArrayRef<MCAsmMacroArgument> A,
24990b57cec5SDimitry Andric                             bool EnableAtPseudoVariable, SMLoc L) {
25000b57cec5SDimitry Andric   unsigned NParameters = Parameters.size();
25010b57cec5SDimitry Andric   bool HasVararg = NParameters ? Parameters.back().Vararg : false;
25020b57cec5SDimitry Andric   if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
25030b57cec5SDimitry Andric     return Error(L, "Wrong number of arguments");
25040b57cec5SDimitry Andric 
25050b57cec5SDimitry Andric   // A macro without parameters is handled differently on Darwin:
25060b57cec5SDimitry Andric   // gas accepts no arguments and does no substitutions
25070b57cec5SDimitry Andric   while (!Body.empty()) {
25080b57cec5SDimitry Andric     // Scan for the next substitution.
25090b57cec5SDimitry Andric     std::size_t End = Body.size(), Pos = 0;
25100b57cec5SDimitry Andric     for (; Pos != End; ++Pos) {
25110b57cec5SDimitry Andric       // Check for a substitution or escape.
25120b57cec5SDimitry Andric       if (IsDarwin && !NParameters) {
25130b57cec5SDimitry Andric         // This macro has no parameters, look for $0, $1, etc.
25140b57cec5SDimitry Andric         if (Body[Pos] != '$' || Pos + 1 == End)
25150b57cec5SDimitry Andric           continue;
25160b57cec5SDimitry Andric 
25170b57cec5SDimitry Andric         char Next = Body[Pos + 1];
25180b57cec5SDimitry Andric         if (Next == '$' || Next == 'n' ||
25190b57cec5SDimitry Andric             isdigit(static_cast<unsigned char>(Next)))
25200b57cec5SDimitry Andric           break;
25210b57cec5SDimitry Andric       } else {
25220b57cec5SDimitry Andric         // This macro has parameters, look for \foo, \bar, etc.
25230b57cec5SDimitry Andric         if (Body[Pos] == '\\' && Pos + 1 != End)
25240b57cec5SDimitry Andric           break;
25250b57cec5SDimitry Andric       }
25260b57cec5SDimitry Andric     }
25270b57cec5SDimitry Andric 
25280b57cec5SDimitry Andric     // Add the prefix.
25290b57cec5SDimitry Andric     OS << Body.slice(0, Pos);
25300b57cec5SDimitry Andric 
25310b57cec5SDimitry Andric     // Check if we reached the end.
25320b57cec5SDimitry Andric     if (Pos == End)
25330b57cec5SDimitry Andric       break;
25340b57cec5SDimitry Andric 
25350b57cec5SDimitry Andric     if (IsDarwin && !NParameters) {
25360b57cec5SDimitry Andric       switch (Body[Pos + 1]) {
25370b57cec5SDimitry Andric       // $$ => $
25380b57cec5SDimitry Andric       case '$':
25390b57cec5SDimitry Andric         OS << '$';
25400b57cec5SDimitry Andric         break;
25410b57cec5SDimitry Andric 
25420b57cec5SDimitry Andric       // $n => number of arguments
25430b57cec5SDimitry Andric       case 'n':
25440b57cec5SDimitry Andric         OS << A.size();
25450b57cec5SDimitry Andric         break;
25460b57cec5SDimitry Andric 
25470b57cec5SDimitry Andric       // $[0-9] => argument
25480b57cec5SDimitry Andric       default: {
25490b57cec5SDimitry Andric         // Missing arguments are ignored.
25500b57cec5SDimitry Andric         unsigned Index = Body[Pos + 1] - '0';
25510b57cec5SDimitry Andric         if (Index >= A.size())
25520b57cec5SDimitry Andric           break;
25530b57cec5SDimitry Andric 
25540b57cec5SDimitry Andric         // Otherwise substitute with the token values, with spaces eliminated.
25550b57cec5SDimitry Andric         for (const AsmToken &Token : A[Index])
25560b57cec5SDimitry Andric           OS << Token.getString();
25570b57cec5SDimitry Andric         break;
25580b57cec5SDimitry Andric       }
25590b57cec5SDimitry Andric       }
25600b57cec5SDimitry Andric       Pos += 2;
25610b57cec5SDimitry Andric     } else {
25620b57cec5SDimitry Andric       unsigned I = Pos + 1;
25630b57cec5SDimitry Andric 
25640b57cec5SDimitry Andric       // Check for the \@ pseudo-variable.
25650b57cec5SDimitry Andric       if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End)
25660b57cec5SDimitry Andric         ++I;
25670b57cec5SDimitry Andric       else
25680b57cec5SDimitry Andric         while (isIdentifierChar(Body[I]) && I + 1 != End)
25690b57cec5SDimitry Andric           ++I;
25700b57cec5SDimitry Andric 
25710b57cec5SDimitry Andric       const char *Begin = Body.data() + Pos + 1;
25720b57cec5SDimitry Andric       StringRef Argument(Begin, I - (Pos + 1));
25730b57cec5SDimitry Andric       unsigned Index = 0;
25740b57cec5SDimitry Andric 
25750b57cec5SDimitry Andric       if (Argument == "@") {
25760b57cec5SDimitry Andric         OS << NumOfMacroInstantiations;
25770b57cec5SDimitry Andric         Pos += 2;
25780b57cec5SDimitry Andric       } else {
25790b57cec5SDimitry Andric         for (; Index < NParameters; ++Index)
25800b57cec5SDimitry Andric           if (Parameters[Index].Name == Argument)
25810b57cec5SDimitry Andric             break;
25820b57cec5SDimitry Andric 
25830b57cec5SDimitry Andric         if (Index == NParameters) {
25840b57cec5SDimitry Andric           if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
25850b57cec5SDimitry Andric             Pos += 3;
25860b57cec5SDimitry Andric           else {
25870b57cec5SDimitry Andric             OS << '\\' << Argument;
25880b57cec5SDimitry Andric             Pos = I;
25890b57cec5SDimitry Andric           }
25900b57cec5SDimitry Andric         } else {
25910b57cec5SDimitry Andric           bool VarargParameter = HasVararg && Index == (NParameters - 1);
25920b57cec5SDimitry Andric           for (const AsmToken &Token : A[Index])
25930b57cec5SDimitry Andric             // For altmacro mode, you can write '%expr'.
25940b57cec5SDimitry Andric             // The prefix '%' evaluates the expression 'expr'
25950b57cec5SDimitry Andric             // and uses the result as a string (e.g. replace %(1+2) with the
25960b57cec5SDimitry Andric             // string "3").
25970b57cec5SDimitry Andric             // Here, we identify the integer token which is the result of the
25980b57cec5SDimitry Andric             // absolute expression evaluation and replace it with its string
25990b57cec5SDimitry Andric             // representation.
26000b57cec5SDimitry Andric             if (AltMacroMode && Token.getString().front() == '%' &&
26010b57cec5SDimitry Andric                 Token.is(AsmToken::Integer))
26020b57cec5SDimitry Andric               // Emit an integer value to the buffer.
26030b57cec5SDimitry Andric               OS << Token.getIntVal();
26040b57cec5SDimitry Andric             // Only Token that was validated as a string and begins with '<'
26050b57cec5SDimitry Andric             // is considered altMacroString!!!
26060b57cec5SDimitry Andric             else if (AltMacroMode && Token.getString().front() == '<' &&
26070b57cec5SDimitry Andric                      Token.is(AsmToken::String)) {
26085ffd83dbSDimitry Andric               OS << angleBracketString(Token.getStringContents());
26090b57cec5SDimitry Andric             }
26100b57cec5SDimitry Andric             // We expect no quotes around the string's contents when
26110b57cec5SDimitry Andric             // parsing for varargs.
26120b57cec5SDimitry Andric             else if (Token.isNot(AsmToken::String) || VarargParameter)
26130b57cec5SDimitry Andric               OS << Token.getString();
26140b57cec5SDimitry Andric             else
26150b57cec5SDimitry Andric               OS << Token.getStringContents();
26160b57cec5SDimitry Andric 
26170b57cec5SDimitry Andric           Pos += 1 + Argument.size();
26180b57cec5SDimitry Andric         }
26190b57cec5SDimitry Andric       }
26200b57cec5SDimitry Andric     }
26210b57cec5SDimitry Andric     // Update the scan point.
26220b57cec5SDimitry Andric     Body = Body.substr(Pos);
26230b57cec5SDimitry Andric   }
26240b57cec5SDimitry Andric 
26250b57cec5SDimitry Andric   return false;
26260b57cec5SDimitry Andric }
26270b57cec5SDimitry Andric 
26280b57cec5SDimitry Andric static bool isOperator(AsmToken::TokenKind kind) {
26290b57cec5SDimitry Andric   switch (kind) {
26300b57cec5SDimitry Andric   default:
26310b57cec5SDimitry Andric     return false;
26320b57cec5SDimitry Andric   case AsmToken::Plus:
26330b57cec5SDimitry Andric   case AsmToken::Minus:
26340b57cec5SDimitry Andric   case AsmToken::Tilde:
26350b57cec5SDimitry Andric   case AsmToken::Slash:
26360b57cec5SDimitry Andric   case AsmToken::Star:
26370b57cec5SDimitry Andric   case AsmToken::Dot:
26380b57cec5SDimitry Andric   case AsmToken::Equal:
26390b57cec5SDimitry Andric   case AsmToken::EqualEqual:
26400b57cec5SDimitry Andric   case AsmToken::Pipe:
26410b57cec5SDimitry Andric   case AsmToken::PipePipe:
26420b57cec5SDimitry Andric   case AsmToken::Caret:
26430b57cec5SDimitry Andric   case AsmToken::Amp:
26440b57cec5SDimitry Andric   case AsmToken::AmpAmp:
26450b57cec5SDimitry Andric   case AsmToken::Exclaim:
26460b57cec5SDimitry Andric   case AsmToken::ExclaimEqual:
26470b57cec5SDimitry Andric   case AsmToken::Less:
26480b57cec5SDimitry Andric   case AsmToken::LessEqual:
26490b57cec5SDimitry Andric   case AsmToken::LessLess:
26500b57cec5SDimitry Andric   case AsmToken::LessGreater:
26510b57cec5SDimitry Andric   case AsmToken::Greater:
26520b57cec5SDimitry Andric   case AsmToken::GreaterEqual:
26530b57cec5SDimitry Andric   case AsmToken::GreaterGreater:
26540b57cec5SDimitry Andric     return true;
26550b57cec5SDimitry Andric   }
26560b57cec5SDimitry Andric }
26570b57cec5SDimitry Andric 
26580b57cec5SDimitry Andric namespace {
26590b57cec5SDimitry Andric 
26600b57cec5SDimitry Andric class AsmLexerSkipSpaceRAII {
26610b57cec5SDimitry Andric public:
26620b57cec5SDimitry Andric   AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
26630b57cec5SDimitry Andric     Lexer.setSkipSpace(SkipSpace);
26640b57cec5SDimitry Andric   }
26650b57cec5SDimitry Andric 
26660b57cec5SDimitry Andric   ~AsmLexerSkipSpaceRAII() {
26670b57cec5SDimitry Andric     Lexer.setSkipSpace(true);
26680b57cec5SDimitry Andric   }
26690b57cec5SDimitry Andric 
26700b57cec5SDimitry Andric private:
26710b57cec5SDimitry Andric   AsmLexer &Lexer;
26720b57cec5SDimitry Andric };
26730b57cec5SDimitry Andric 
26740b57cec5SDimitry Andric } // end anonymous namespace
26750b57cec5SDimitry Andric 
26760b57cec5SDimitry Andric bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
26770b57cec5SDimitry Andric 
26780b57cec5SDimitry Andric   if (Vararg) {
26790b57cec5SDimitry Andric     if (Lexer.isNot(AsmToken::EndOfStatement)) {
26800b57cec5SDimitry Andric       StringRef Str = parseStringToEndOfStatement();
26810b57cec5SDimitry Andric       MA.emplace_back(AsmToken::String, Str);
26820b57cec5SDimitry Andric     }
26830b57cec5SDimitry Andric     return false;
26840b57cec5SDimitry Andric   }
26850b57cec5SDimitry Andric 
26860b57cec5SDimitry Andric   unsigned ParenLevel = 0;
26870b57cec5SDimitry Andric 
26880b57cec5SDimitry Andric   // Darwin doesn't use spaces to delmit arguments.
26890b57cec5SDimitry Andric   AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
26900b57cec5SDimitry Andric 
26910b57cec5SDimitry Andric   bool SpaceEaten;
26920b57cec5SDimitry Andric 
26930b57cec5SDimitry Andric   while (true) {
26940b57cec5SDimitry Andric     SpaceEaten = false;
26950b57cec5SDimitry Andric     if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
26960b57cec5SDimitry Andric       return TokError("unexpected token in macro instantiation");
26970b57cec5SDimitry Andric 
26980b57cec5SDimitry Andric     if (ParenLevel == 0) {
26990b57cec5SDimitry Andric 
27000b57cec5SDimitry Andric       if (Lexer.is(AsmToken::Comma))
27010b57cec5SDimitry Andric         break;
27020b57cec5SDimitry Andric 
27030b57cec5SDimitry Andric       if (Lexer.is(AsmToken::Space)) {
27040b57cec5SDimitry Andric         SpaceEaten = true;
27050b57cec5SDimitry Andric         Lexer.Lex(); // Eat spaces
27060b57cec5SDimitry Andric       }
27070b57cec5SDimitry Andric 
27080b57cec5SDimitry Andric       // Spaces can delimit parameters, but could also be part an expression.
27090b57cec5SDimitry Andric       // If the token after a space is an operator, add the token and the next
27100b57cec5SDimitry Andric       // one into this argument
27110b57cec5SDimitry Andric       if (!IsDarwin) {
27120b57cec5SDimitry Andric         if (isOperator(Lexer.getKind())) {
27130b57cec5SDimitry Andric           MA.push_back(getTok());
27140b57cec5SDimitry Andric           Lexer.Lex();
27150b57cec5SDimitry Andric 
27160b57cec5SDimitry Andric           // Whitespace after an operator can be ignored.
27170b57cec5SDimitry Andric           if (Lexer.is(AsmToken::Space))
27180b57cec5SDimitry Andric             Lexer.Lex();
27190b57cec5SDimitry Andric 
27200b57cec5SDimitry Andric           continue;
27210b57cec5SDimitry Andric         }
27220b57cec5SDimitry Andric       }
27230b57cec5SDimitry Andric       if (SpaceEaten)
27240b57cec5SDimitry Andric         break;
27250b57cec5SDimitry Andric     }
27260b57cec5SDimitry Andric 
27270b57cec5SDimitry Andric     // handleMacroEntry relies on not advancing the lexer here
27280b57cec5SDimitry Andric     // to be able to fill in the remaining default parameter values
27290b57cec5SDimitry Andric     if (Lexer.is(AsmToken::EndOfStatement))
27300b57cec5SDimitry Andric       break;
27310b57cec5SDimitry Andric 
27320b57cec5SDimitry Andric     // Adjust the current parentheses level.
27330b57cec5SDimitry Andric     if (Lexer.is(AsmToken::LParen))
27340b57cec5SDimitry Andric       ++ParenLevel;
27350b57cec5SDimitry Andric     else if (Lexer.is(AsmToken::RParen) && ParenLevel)
27360b57cec5SDimitry Andric       --ParenLevel;
27370b57cec5SDimitry Andric 
27380b57cec5SDimitry Andric     // Append the token to the current argument list.
27390b57cec5SDimitry Andric     MA.push_back(getTok());
27400b57cec5SDimitry Andric     Lexer.Lex();
27410b57cec5SDimitry Andric   }
27420b57cec5SDimitry Andric 
27430b57cec5SDimitry Andric   if (ParenLevel != 0)
27440b57cec5SDimitry Andric     return TokError("unbalanced parentheses in macro argument");
27450b57cec5SDimitry Andric   return false;
27460b57cec5SDimitry Andric }
27470b57cec5SDimitry Andric 
27480b57cec5SDimitry Andric // Parse the macro instantiation arguments.
27490b57cec5SDimitry Andric bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
27500b57cec5SDimitry Andric                                     MCAsmMacroArguments &A) {
27510b57cec5SDimitry Andric   const unsigned NParameters = M ? M->Parameters.size() : 0;
27520b57cec5SDimitry Andric   bool NamedParametersFound = false;
27530b57cec5SDimitry Andric   SmallVector<SMLoc, 4> FALocs;
27540b57cec5SDimitry Andric 
27550b57cec5SDimitry Andric   A.resize(NParameters);
27560b57cec5SDimitry Andric   FALocs.resize(NParameters);
27570b57cec5SDimitry Andric 
27580b57cec5SDimitry Andric   // Parse two kinds of macro invocations:
27590b57cec5SDimitry Andric   // - macros defined without any parameters accept an arbitrary number of them
27600b57cec5SDimitry Andric   // - macros defined with parameters accept at most that many of them
27610b57cec5SDimitry Andric   bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
27620b57cec5SDimitry Andric   for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
27630b57cec5SDimitry Andric        ++Parameter) {
27640b57cec5SDimitry Andric     SMLoc IDLoc = Lexer.getLoc();
27650b57cec5SDimitry Andric     MCAsmMacroParameter FA;
27660b57cec5SDimitry Andric 
27670b57cec5SDimitry Andric     if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
27680b57cec5SDimitry Andric       if (parseIdentifier(FA.Name))
27690b57cec5SDimitry Andric         return Error(IDLoc, "invalid argument identifier for formal argument");
27700b57cec5SDimitry Andric 
27710b57cec5SDimitry Andric       if (Lexer.isNot(AsmToken::Equal))
27720b57cec5SDimitry Andric         return TokError("expected '=' after formal parameter identifier");
27730b57cec5SDimitry Andric 
27740b57cec5SDimitry Andric       Lex();
27750b57cec5SDimitry Andric 
27760b57cec5SDimitry Andric       NamedParametersFound = true;
27770b57cec5SDimitry Andric     }
27780b57cec5SDimitry Andric     bool Vararg = HasVararg && Parameter == (NParameters - 1);
27790b57cec5SDimitry Andric 
27800b57cec5SDimitry Andric     if (NamedParametersFound && FA.Name.empty())
27810b57cec5SDimitry Andric       return Error(IDLoc, "cannot mix positional and keyword arguments");
27820b57cec5SDimitry Andric 
27830b57cec5SDimitry Andric     SMLoc StrLoc = Lexer.getLoc();
27840b57cec5SDimitry Andric     SMLoc EndLoc;
27850b57cec5SDimitry Andric     if (AltMacroMode && Lexer.is(AsmToken::Percent)) {
27860b57cec5SDimitry Andric       const MCExpr *AbsoluteExp;
27870b57cec5SDimitry Andric       int64_t Value;
27880b57cec5SDimitry Andric       /// Eat '%'
27890b57cec5SDimitry Andric       Lex();
27900b57cec5SDimitry Andric       if (parseExpression(AbsoluteExp, EndLoc))
27910b57cec5SDimitry Andric         return false;
27920b57cec5SDimitry Andric       if (!AbsoluteExp->evaluateAsAbsolute(Value,
27930b57cec5SDimitry Andric                                            getStreamer().getAssemblerPtr()))
27940b57cec5SDimitry Andric         return Error(StrLoc, "expected absolute expression");
27950b57cec5SDimitry Andric       const char *StrChar = StrLoc.getPointer();
27960b57cec5SDimitry Andric       const char *EndChar = EndLoc.getPointer();
27970b57cec5SDimitry Andric       AsmToken newToken(AsmToken::Integer,
27980b57cec5SDimitry Andric                         StringRef(StrChar, EndChar - StrChar), Value);
27990b57cec5SDimitry Andric       FA.Value.push_back(newToken);
28000b57cec5SDimitry Andric     } else if (AltMacroMode && Lexer.is(AsmToken::Less) &&
28015ffd83dbSDimitry Andric                isAngleBracketString(StrLoc, EndLoc)) {
28020b57cec5SDimitry Andric       const char *StrChar = StrLoc.getPointer();
28030b57cec5SDimitry Andric       const char *EndChar = EndLoc.getPointer();
28040b57cec5SDimitry Andric       jumpToLoc(EndLoc, CurBuffer);
28050b57cec5SDimitry Andric       /// Eat from '<' to '>'
28060b57cec5SDimitry Andric       Lex();
28070b57cec5SDimitry Andric       AsmToken newToken(AsmToken::String,
28080b57cec5SDimitry Andric                         StringRef(StrChar, EndChar - StrChar));
28090b57cec5SDimitry Andric       FA.Value.push_back(newToken);
28100b57cec5SDimitry Andric     } else if(parseMacroArgument(FA.Value, Vararg))
28110b57cec5SDimitry Andric       return true;
28120b57cec5SDimitry Andric 
28130b57cec5SDimitry Andric     unsigned PI = Parameter;
28140b57cec5SDimitry Andric     if (!FA.Name.empty()) {
28150b57cec5SDimitry Andric       unsigned FAI = 0;
28160b57cec5SDimitry Andric       for (FAI = 0; FAI < NParameters; ++FAI)
28170b57cec5SDimitry Andric         if (M->Parameters[FAI].Name == FA.Name)
28180b57cec5SDimitry Andric           break;
28190b57cec5SDimitry Andric 
28200b57cec5SDimitry Andric       if (FAI >= NParameters) {
28210b57cec5SDimitry Andric         assert(M && "expected macro to be defined");
28220b57cec5SDimitry Andric         return Error(IDLoc, "parameter named '" + FA.Name +
28230b57cec5SDimitry Andric                                 "' does not exist for macro '" + M->Name + "'");
28240b57cec5SDimitry Andric       }
28250b57cec5SDimitry Andric       PI = FAI;
28260b57cec5SDimitry Andric     }
28270b57cec5SDimitry Andric 
28280b57cec5SDimitry Andric     if (!FA.Value.empty()) {
28290b57cec5SDimitry Andric       if (A.size() <= PI)
28300b57cec5SDimitry Andric         A.resize(PI + 1);
28310b57cec5SDimitry Andric       A[PI] = FA.Value;
28320b57cec5SDimitry Andric 
28330b57cec5SDimitry Andric       if (FALocs.size() <= PI)
28340b57cec5SDimitry Andric         FALocs.resize(PI + 1);
28350b57cec5SDimitry Andric 
28360b57cec5SDimitry Andric       FALocs[PI] = Lexer.getLoc();
28370b57cec5SDimitry Andric     }
28380b57cec5SDimitry Andric 
28390b57cec5SDimitry Andric     // At the end of the statement, fill in remaining arguments that have
28400b57cec5SDimitry Andric     // default values. If there aren't any, then the next argument is
28410b57cec5SDimitry Andric     // required but missing
28420b57cec5SDimitry Andric     if (Lexer.is(AsmToken::EndOfStatement)) {
28430b57cec5SDimitry Andric       bool Failure = false;
28440b57cec5SDimitry Andric       for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
28450b57cec5SDimitry Andric         if (A[FAI].empty()) {
28460b57cec5SDimitry Andric           if (M->Parameters[FAI].Required) {
28470b57cec5SDimitry Andric             Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
28480b57cec5SDimitry Andric                   "missing value for required parameter "
28490b57cec5SDimitry Andric                   "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
28500b57cec5SDimitry Andric             Failure = true;
28510b57cec5SDimitry Andric           }
28520b57cec5SDimitry Andric 
28530b57cec5SDimitry Andric           if (!M->Parameters[FAI].Value.empty())
28540b57cec5SDimitry Andric             A[FAI] = M->Parameters[FAI].Value;
28550b57cec5SDimitry Andric         }
28560b57cec5SDimitry Andric       }
28570b57cec5SDimitry Andric       return Failure;
28580b57cec5SDimitry Andric     }
28590b57cec5SDimitry Andric 
28600b57cec5SDimitry Andric     if (Lexer.is(AsmToken::Comma))
28610b57cec5SDimitry Andric       Lex();
28620b57cec5SDimitry Andric   }
28630b57cec5SDimitry Andric 
28640b57cec5SDimitry Andric   return TokError("too many positional arguments");
28650b57cec5SDimitry Andric }
28660b57cec5SDimitry Andric 
28670b57cec5SDimitry Andric bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
28680b57cec5SDimitry Andric   // Arbitrarily limit macro nesting depth (default matches 'as'). We can
28690b57cec5SDimitry Andric   // eliminate this, although we should protect against infinite loops.
28700b57cec5SDimitry Andric   unsigned MaxNestingDepth = AsmMacroMaxNestingDepth;
28710b57cec5SDimitry Andric   if (ActiveMacros.size() == MaxNestingDepth) {
28720b57cec5SDimitry Andric     std::ostringstream MaxNestingDepthError;
28730b57cec5SDimitry Andric     MaxNestingDepthError << "macros cannot be nested more than "
28740b57cec5SDimitry Andric                          << MaxNestingDepth << " levels deep."
28750b57cec5SDimitry Andric                          << " Use -asm-macro-max-nesting-depth to increase "
28760b57cec5SDimitry Andric                             "this limit.";
28770b57cec5SDimitry Andric     return TokError(MaxNestingDepthError.str());
28780b57cec5SDimitry Andric   }
28790b57cec5SDimitry Andric 
28800b57cec5SDimitry Andric   MCAsmMacroArguments A;
28810b57cec5SDimitry Andric   if (parseMacroArguments(M, A))
28820b57cec5SDimitry Andric     return true;
28830b57cec5SDimitry Andric 
28840b57cec5SDimitry Andric   // Macro instantiation is lexical, unfortunately. We construct a new buffer
28850b57cec5SDimitry Andric   // to hold the macro body with substitutions.
28860b57cec5SDimitry Andric   SmallString<256> Buf;
28870b57cec5SDimitry Andric   StringRef Body = M->Body;
28880b57cec5SDimitry Andric   raw_svector_ostream OS(Buf);
28890b57cec5SDimitry Andric 
28900b57cec5SDimitry Andric   if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc()))
28910b57cec5SDimitry Andric     return true;
28920b57cec5SDimitry Andric 
28930b57cec5SDimitry Andric   // We include the .endmacro in the buffer as our cue to exit the macro
28940b57cec5SDimitry Andric   // instantiation.
28950b57cec5SDimitry Andric   OS << ".endmacro\n";
28960b57cec5SDimitry Andric 
28970b57cec5SDimitry Andric   std::unique_ptr<MemoryBuffer> Instantiation =
28980b57cec5SDimitry Andric       MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
28990b57cec5SDimitry Andric 
29000b57cec5SDimitry Andric   // Create the macro instantiation object and add to the current macro
29010b57cec5SDimitry Andric   // instantiation stack.
2902480093f4SDimitry Andric   MacroInstantiation *MI = new MacroInstantiation{
2903480093f4SDimitry Andric       NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()};
29040b57cec5SDimitry Andric   ActiveMacros.push_back(MI);
29050b57cec5SDimitry Andric 
29060b57cec5SDimitry Andric   ++NumOfMacroInstantiations;
29070b57cec5SDimitry Andric 
29080b57cec5SDimitry Andric   // Jump to the macro instantiation and prime the lexer.
29090b57cec5SDimitry Andric   CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
29100b57cec5SDimitry Andric   Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
29110b57cec5SDimitry Andric   Lex();
29120b57cec5SDimitry Andric 
29130b57cec5SDimitry Andric   return false;
29140b57cec5SDimitry Andric }
29150b57cec5SDimitry Andric 
29160b57cec5SDimitry Andric void AsmParser::handleMacroExit() {
29170b57cec5SDimitry Andric   // Jump to the EndOfStatement we should return to, and consume it.
29180b57cec5SDimitry Andric   jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
29190b57cec5SDimitry Andric   Lex();
29200b57cec5SDimitry Andric 
29210b57cec5SDimitry Andric   // Pop the instantiation entry.
29220b57cec5SDimitry Andric   delete ActiveMacros.back();
29230b57cec5SDimitry Andric   ActiveMacros.pop_back();
29240b57cec5SDimitry Andric }
29250b57cec5SDimitry Andric 
29260eae32dcSDimitry Andric bool AsmParser::parseAssignment(StringRef Name, AssignmentKind Kind) {
29270b57cec5SDimitry Andric   MCSymbol *Sym;
29280b57cec5SDimitry Andric   const MCExpr *Value;
29290eae32dcSDimitry Andric   SMLoc ExprLoc = getTok().getLoc();
29300eae32dcSDimitry Andric   bool AllowRedef =
29310eae32dcSDimitry Andric       Kind == AssignmentKind::Set || Kind == AssignmentKind::Equal;
29320eae32dcSDimitry Andric   if (MCParserUtils::parseAssignmentExpression(Name, AllowRedef, *this, Sym,
29330b57cec5SDimitry Andric                                                Value))
29340b57cec5SDimitry Andric     return true;
29350b57cec5SDimitry Andric 
29360b57cec5SDimitry Andric   if (!Sym) {
29370b57cec5SDimitry Andric     // In the case where we parse an expression starting with a '.', we will
29380b57cec5SDimitry Andric     // not generate an error, nor will we create a symbol.  In this case we
29390b57cec5SDimitry Andric     // should just return out.
29400b57cec5SDimitry Andric     return false;
29410b57cec5SDimitry Andric   }
29420b57cec5SDimitry Andric 
2943fe6060f1SDimitry Andric   if (discardLTOSymbol(Name))
2944fe6060f1SDimitry Andric     return false;
2945fe6060f1SDimitry Andric 
29460b57cec5SDimitry Andric   // Do the assignment.
29470eae32dcSDimitry Andric   switch (Kind) {
29480eae32dcSDimitry Andric   case AssignmentKind::Equal:
29495ffd83dbSDimitry Andric     Out.emitAssignment(Sym, Value);
29500eae32dcSDimitry Andric     break;
29510eae32dcSDimitry Andric   case AssignmentKind::Set:
29520eae32dcSDimitry Andric   case AssignmentKind::Equiv:
29530eae32dcSDimitry Andric     Out.emitAssignment(Sym, Value);
29545ffd83dbSDimitry Andric     Out.emitSymbolAttribute(Sym, MCSA_NoDeadStrip);
29550eae32dcSDimitry Andric     break;
29560eae32dcSDimitry Andric   case AssignmentKind::LTOSetConditional:
29570eae32dcSDimitry Andric     if (Value->getKind() != MCExpr::SymbolRef)
29580eae32dcSDimitry Andric       return Error(ExprLoc, "expected identifier");
29590eae32dcSDimitry Andric 
29600eae32dcSDimitry Andric     Out.emitConditionalAssignment(Sym, Value);
29610eae32dcSDimitry Andric     break;
29620eae32dcSDimitry Andric   }
29630b57cec5SDimitry Andric 
29640b57cec5SDimitry Andric   return false;
29650b57cec5SDimitry Andric }
29660b57cec5SDimitry Andric 
29670b57cec5SDimitry Andric /// parseIdentifier:
29680b57cec5SDimitry Andric ///   ::= identifier
29690b57cec5SDimitry Andric ///   ::= string
29700b57cec5SDimitry Andric bool AsmParser::parseIdentifier(StringRef &Res) {
29710b57cec5SDimitry Andric   // The assembler has relaxed rules for accepting identifiers, in particular we
29720b57cec5SDimitry Andric   // allow things like '.globl $foo' and '.def @feat.00', which would normally be
29730b57cec5SDimitry Andric   // separate tokens. At this level, we have already lexed so we cannot (currently)
29740b57cec5SDimitry Andric   // handle this as a context dependent token, instead we detect adjacent tokens
29750b57cec5SDimitry Andric   // and return the combined identifier.
29760b57cec5SDimitry Andric   if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
29770b57cec5SDimitry Andric     SMLoc PrefixLoc = getLexer().getLoc();
29780b57cec5SDimitry Andric 
29790b57cec5SDimitry Andric     // Consume the prefix character, and check for a following identifier.
29800b57cec5SDimitry Andric 
29810b57cec5SDimitry Andric     AsmToken Buf[1];
29820b57cec5SDimitry Andric     Lexer.peekTokens(Buf, false);
29830b57cec5SDimitry Andric 
29845ffd83dbSDimitry Andric     if (Buf[0].isNot(AsmToken::Identifier) && Buf[0].isNot(AsmToken::Integer))
29850b57cec5SDimitry Andric       return true;
29860b57cec5SDimitry Andric 
29875ffd83dbSDimitry Andric     // We have a '$' or '@' followed by an identifier or integer token, make
29885ffd83dbSDimitry Andric     // sure they are adjacent.
29890b57cec5SDimitry Andric     if (PrefixLoc.getPointer() + 1 != Buf[0].getLoc().getPointer())
29900b57cec5SDimitry Andric       return true;
29910b57cec5SDimitry Andric 
29920b57cec5SDimitry Andric     // eat $ or @
29930b57cec5SDimitry Andric     Lexer.Lex(); // Lexer's Lex guarantees consecutive token.
29940b57cec5SDimitry Andric     // Construct the joined identifier and consume the token.
29955ffd83dbSDimitry Andric     Res = StringRef(PrefixLoc.getPointer(), getTok().getString().size() + 1);
29960b57cec5SDimitry Andric     Lex(); // Parser Lex to maintain invariants.
29970b57cec5SDimitry Andric     return false;
29980b57cec5SDimitry Andric   }
29990b57cec5SDimitry Andric 
30000b57cec5SDimitry Andric   if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
30010b57cec5SDimitry Andric     return true;
30020b57cec5SDimitry Andric 
30030b57cec5SDimitry Andric   Res = getTok().getIdentifier();
30040b57cec5SDimitry Andric 
30050b57cec5SDimitry Andric   Lex(); // Consume the identifier token.
30060b57cec5SDimitry Andric 
30070b57cec5SDimitry Andric   return false;
30080b57cec5SDimitry Andric }
30090b57cec5SDimitry Andric 
30100b57cec5SDimitry Andric /// parseDirectiveSet:
30110b57cec5SDimitry Andric ///   ::= .equ identifier ',' expression
30120b57cec5SDimitry Andric ///   ::= .equiv identifier ',' expression
30130b57cec5SDimitry Andric ///   ::= .set identifier ',' expression
30140eae32dcSDimitry Andric ///   ::= .lto_set_conditional identifier ',' expression
30150eae32dcSDimitry Andric bool AsmParser::parseDirectiveSet(StringRef IDVal, AssignmentKind Kind) {
30160b57cec5SDimitry Andric   StringRef Name;
3017fe6060f1SDimitry Andric   if (check(parseIdentifier(Name), "expected identifier") || parseComma() ||
30180eae32dcSDimitry Andric       parseAssignment(Name, Kind))
3019fe6060f1SDimitry Andric     return true;
30200b57cec5SDimitry Andric   return false;
30210b57cec5SDimitry Andric }
30220b57cec5SDimitry Andric 
30230b57cec5SDimitry Andric bool AsmParser::parseEscapedString(std::string &Data) {
30240b57cec5SDimitry Andric   if (check(getTok().isNot(AsmToken::String), "expected string"))
30250b57cec5SDimitry Andric     return true;
30260b57cec5SDimitry Andric 
30270b57cec5SDimitry Andric   Data = "";
30280b57cec5SDimitry Andric   StringRef Str = getTok().getStringContents();
30290b57cec5SDimitry Andric   for (unsigned i = 0, e = Str.size(); i != e; ++i) {
30300b57cec5SDimitry Andric     if (Str[i] != '\\') {
30310b57cec5SDimitry Andric       Data += Str[i];
30320b57cec5SDimitry Andric       continue;
30330b57cec5SDimitry Andric     }
30340b57cec5SDimitry Andric 
30350b57cec5SDimitry Andric     // Recognize escaped characters. Note that this escape semantics currently
30368bcb0991SDimitry Andric     // loosely follows Darwin 'as'.
30370b57cec5SDimitry Andric     ++i;
30380b57cec5SDimitry Andric     if (i == e)
30390b57cec5SDimitry Andric       return TokError("unexpected backslash at end of string");
30400b57cec5SDimitry Andric 
30418bcb0991SDimitry Andric     // Recognize hex sequences similarly to GNU 'as'.
30428bcb0991SDimitry Andric     if (Str[i] == 'x' || Str[i] == 'X') {
30438bcb0991SDimitry Andric       size_t length = Str.size();
30448bcb0991SDimitry Andric       if (i + 1 >= length || !isHexDigit(Str[i + 1]))
30458bcb0991SDimitry Andric         return TokError("invalid hexadecimal escape sequence");
30468bcb0991SDimitry Andric 
30478bcb0991SDimitry Andric       // Consume hex characters. GNU 'as' reads all hexadecimal characters and
30488bcb0991SDimitry Andric       // then truncates to the lower 16 bits. Seems reasonable.
30498bcb0991SDimitry Andric       unsigned Value = 0;
30508bcb0991SDimitry Andric       while (i + 1 < length && isHexDigit(Str[i + 1]))
30518bcb0991SDimitry Andric         Value = Value * 16 + hexDigitValue(Str[++i]);
30528bcb0991SDimitry Andric 
30538bcb0991SDimitry Andric       Data += (unsigned char)(Value & 0xFF);
30548bcb0991SDimitry Andric       continue;
30558bcb0991SDimitry Andric     }
30568bcb0991SDimitry Andric 
30570b57cec5SDimitry Andric     // Recognize octal sequences.
30580b57cec5SDimitry Andric     if ((unsigned)(Str[i] - '0') <= 7) {
30590b57cec5SDimitry Andric       // Consume up to three octal characters.
30600b57cec5SDimitry Andric       unsigned Value = Str[i] - '0';
30610b57cec5SDimitry Andric 
30620b57cec5SDimitry Andric       if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
30630b57cec5SDimitry Andric         ++i;
30640b57cec5SDimitry Andric         Value = Value * 8 + (Str[i] - '0');
30650b57cec5SDimitry Andric 
30660b57cec5SDimitry Andric         if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
30670b57cec5SDimitry Andric           ++i;
30680b57cec5SDimitry Andric           Value = Value * 8 + (Str[i] - '0');
30690b57cec5SDimitry Andric         }
30700b57cec5SDimitry Andric       }
30710b57cec5SDimitry Andric 
30720b57cec5SDimitry Andric       if (Value > 255)
30730b57cec5SDimitry Andric         return TokError("invalid octal escape sequence (out of range)");
30740b57cec5SDimitry Andric 
30750b57cec5SDimitry Andric       Data += (unsigned char)Value;
30760b57cec5SDimitry Andric       continue;
30770b57cec5SDimitry Andric     }
30780b57cec5SDimitry Andric 
30790b57cec5SDimitry Andric     // Otherwise recognize individual escapes.
30800b57cec5SDimitry Andric     switch (Str[i]) {
30810b57cec5SDimitry Andric     default:
30820b57cec5SDimitry Andric       // Just reject invalid escape sequences for now.
30830b57cec5SDimitry Andric       return TokError("invalid escape sequence (unrecognized character)");
30840b57cec5SDimitry Andric 
30850b57cec5SDimitry Andric     case 'b': Data += '\b'; break;
30860b57cec5SDimitry Andric     case 'f': Data += '\f'; break;
30870b57cec5SDimitry Andric     case 'n': Data += '\n'; break;
30880b57cec5SDimitry Andric     case 'r': Data += '\r'; break;
30890b57cec5SDimitry Andric     case 't': Data += '\t'; break;
30900b57cec5SDimitry Andric     case '"': Data += '"'; break;
30910b57cec5SDimitry Andric     case '\\': Data += '\\'; break;
30920b57cec5SDimitry Andric     }
30930b57cec5SDimitry Andric   }
30940b57cec5SDimitry Andric 
30950b57cec5SDimitry Andric   Lex();
30960b57cec5SDimitry Andric   return false;
30970b57cec5SDimitry Andric }
30980b57cec5SDimitry Andric 
30995ffd83dbSDimitry Andric bool AsmParser::parseAngleBracketString(std::string &Data) {
31005ffd83dbSDimitry Andric   SMLoc EndLoc, StartLoc = getTok().getLoc();
31015ffd83dbSDimitry Andric   if (isAngleBracketString(StartLoc, EndLoc)) {
31025ffd83dbSDimitry Andric     const char *StartChar = StartLoc.getPointer() + 1;
31035ffd83dbSDimitry Andric     const char *EndChar = EndLoc.getPointer() - 1;
31045ffd83dbSDimitry Andric     jumpToLoc(EndLoc, CurBuffer);
31055ffd83dbSDimitry Andric     /// Eat from '<' to '>'
31065ffd83dbSDimitry Andric     Lex();
31075ffd83dbSDimitry Andric 
31085ffd83dbSDimitry Andric     Data = angleBracketString(StringRef(StartChar, EndChar - StartChar));
31095ffd83dbSDimitry Andric     return false;
31105ffd83dbSDimitry Andric   }
31115ffd83dbSDimitry Andric   return true;
31125ffd83dbSDimitry Andric }
31135ffd83dbSDimitry Andric 
31140b57cec5SDimitry Andric /// parseDirectiveAscii:
3115e8d8bef9SDimitry Andric //    ::= .ascii [ "string"+ ( , "string"+ )* ]
3116e8d8bef9SDimitry Andric ///   ::= ( .asciz | .string ) [ "string" ( , "string" )* ]
31170b57cec5SDimitry Andric bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
31180b57cec5SDimitry Andric   auto parseOp = [&]() -> bool {
31190b57cec5SDimitry Andric     std::string Data;
3120e8d8bef9SDimitry Andric     if (checkForValidSection())
3121e8d8bef9SDimitry Andric       return true;
3122e8d8bef9SDimitry Andric     // Only support spaces as separators for .ascii directive for now. See the
3123e8d8bef9SDimitry Andric     // discusssion at https://reviews.llvm.org/D91460 for more details.
3124e8d8bef9SDimitry Andric     do {
3125e8d8bef9SDimitry Andric       if (parseEscapedString(Data))
31260b57cec5SDimitry Andric         return true;
31275ffd83dbSDimitry Andric       getStreamer().emitBytes(Data);
3128e8d8bef9SDimitry Andric     } while (!ZeroTerminated && getTok().is(AsmToken::String));
31290b57cec5SDimitry Andric     if (ZeroTerminated)
31305ffd83dbSDimitry Andric       getStreamer().emitBytes(StringRef("\0", 1));
31310b57cec5SDimitry Andric     return false;
31320b57cec5SDimitry Andric   };
31330b57cec5SDimitry Andric 
3134fe6060f1SDimitry Andric   return parseMany(parseOp);
31350b57cec5SDimitry Andric }
31360b57cec5SDimitry Andric 
31370b57cec5SDimitry Andric /// parseDirectiveReloc
31380b57cec5SDimitry Andric ///  ::= .reloc expression , identifier [ , expression ]
31390b57cec5SDimitry Andric bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) {
31400b57cec5SDimitry Andric   const MCExpr *Offset;
31410b57cec5SDimitry Andric   const MCExpr *Expr = nullptr;
31420b57cec5SDimitry Andric   SMLoc OffsetLoc = Lexer.getTok().getLoc();
31430b57cec5SDimitry Andric 
31440b57cec5SDimitry Andric   if (parseExpression(Offset))
31450b57cec5SDimitry Andric     return true;
3146fe6060f1SDimitry Andric   if (parseComma() ||
31475ffd83dbSDimitry Andric       check(getTok().isNot(AsmToken::Identifier), "expected relocation name"))
31480b57cec5SDimitry Andric     return true;
31490b57cec5SDimitry Andric 
31500b57cec5SDimitry Andric   SMLoc NameLoc = Lexer.getTok().getLoc();
31510b57cec5SDimitry Andric   StringRef Name = Lexer.getTok().getIdentifier();
31520b57cec5SDimitry Andric   Lex();
31530b57cec5SDimitry Andric 
31540b57cec5SDimitry Andric   if (Lexer.is(AsmToken::Comma)) {
31550b57cec5SDimitry Andric     Lex();
31560b57cec5SDimitry Andric     SMLoc ExprLoc = Lexer.getLoc();
31570b57cec5SDimitry Andric     if (parseExpression(Expr))
31580b57cec5SDimitry Andric       return true;
31590b57cec5SDimitry Andric 
31600b57cec5SDimitry Andric     MCValue Value;
31610b57cec5SDimitry Andric     if (!Expr->evaluateAsRelocatable(Value, nullptr, nullptr))
31620b57cec5SDimitry Andric       return Error(ExprLoc, "expression must be relocatable");
31630b57cec5SDimitry Andric   }
31640b57cec5SDimitry Andric 
3165fe6060f1SDimitry Andric   if (parseEOL())
31660b57cec5SDimitry Andric     return true;
31670b57cec5SDimitry Andric 
31680b57cec5SDimitry Andric   const MCTargetAsmParser &MCT = getTargetParser();
31690b57cec5SDimitry Andric   const MCSubtargetInfo &STI = MCT.getSTI();
3170bdd1243dSDimitry Andric   if (std::optional<std::pair<bool, std::string>> Err =
31715ffd83dbSDimitry Andric           getStreamer().emitRelocDirective(*Offset, Name, Expr, DirectiveLoc,
31725ffd83dbSDimitry Andric                                            STI))
31735ffd83dbSDimitry Andric     return Error(Err->first ? NameLoc : OffsetLoc, Err->second);
31740b57cec5SDimitry Andric 
31750b57cec5SDimitry Andric   return false;
31760b57cec5SDimitry Andric }
31770b57cec5SDimitry Andric 
31780b57cec5SDimitry Andric /// parseDirectiveValue
31790b57cec5SDimitry Andric ///  ::= (.byte | .short | ... ) [ expression (, expression)* ]
31800b57cec5SDimitry Andric bool AsmParser::parseDirectiveValue(StringRef IDVal, unsigned Size) {
31810b57cec5SDimitry Andric   auto parseOp = [&]() -> bool {
31820b57cec5SDimitry Andric     const MCExpr *Value;
31830b57cec5SDimitry Andric     SMLoc ExprLoc = getLexer().getLoc();
31840b57cec5SDimitry Andric     if (checkForValidSection() || parseExpression(Value))
31850b57cec5SDimitry Andric       return true;
31860b57cec5SDimitry Andric     // Special case constant expressions to match code generator.
31870b57cec5SDimitry Andric     if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
31880b57cec5SDimitry Andric       assert(Size <= 8 && "Invalid size");
31890b57cec5SDimitry Andric       uint64_t IntValue = MCE->getValue();
31900b57cec5SDimitry Andric       if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
31910b57cec5SDimitry Andric         return Error(ExprLoc, "out of range literal value");
31925ffd83dbSDimitry Andric       getStreamer().emitIntValue(IntValue, Size);
31930b57cec5SDimitry Andric     } else
31945ffd83dbSDimitry Andric       getStreamer().emitValue(Value, Size, ExprLoc);
31950b57cec5SDimitry Andric     return false;
31960b57cec5SDimitry Andric   };
31970b57cec5SDimitry Andric 
3198fe6060f1SDimitry Andric   return parseMany(parseOp);
31990b57cec5SDimitry Andric }
32000b57cec5SDimitry Andric 
32010b57cec5SDimitry Andric static bool parseHexOcta(AsmParser &Asm, uint64_t &hi, uint64_t &lo) {
32020b57cec5SDimitry Andric   if (Asm.getTok().isNot(AsmToken::Integer) &&
32030b57cec5SDimitry Andric       Asm.getTok().isNot(AsmToken::BigNum))
32040b57cec5SDimitry Andric     return Asm.TokError("unknown token in expression");
32050b57cec5SDimitry Andric   SMLoc ExprLoc = Asm.getTok().getLoc();
32060b57cec5SDimitry Andric   APInt IntValue = Asm.getTok().getAPIntVal();
32070b57cec5SDimitry Andric   Asm.Lex();
32080b57cec5SDimitry Andric   if (!IntValue.isIntN(128))
32090b57cec5SDimitry Andric     return Asm.Error(ExprLoc, "out of range literal value");
32100b57cec5SDimitry Andric   if (!IntValue.isIntN(64)) {
32110b57cec5SDimitry Andric     hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
32120b57cec5SDimitry Andric     lo = IntValue.getLoBits(64).getZExtValue();
32130b57cec5SDimitry Andric   } else {
32140b57cec5SDimitry Andric     hi = 0;
32150b57cec5SDimitry Andric     lo = IntValue.getZExtValue();
32160b57cec5SDimitry Andric   }
32170b57cec5SDimitry Andric   return false;
32180b57cec5SDimitry Andric }
32190b57cec5SDimitry Andric 
32200b57cec5SDimitry Andric /// ParseDirectiveOctaValue
32210b57cec5SDimitry Andric ///  ::= .octa [ hexconstant (, hexconstant)* ]
32220b57cec5SDimitry Andric 
32230b57cec5SDimitry Andric bool AsmParser::parseDirectiveOctaValue(StringRef IDVal) {
32240b57cec5SDimitry Andric   auto parseOp = [&]() -> bool {
32250b57cec5SDimitry Andric     if (checkForValidSection())
32260b57cec5SDimitry Andric       return true;
32270b57cec5SDimitry Andric     uint64_t hi, lo;
32280b57cec5SDimitry Andric     if (parseHexOcta(*this, hi, lo))
32290b57cec5SDimitry Andric       return true;
32300b57cec5SDimitry Andric     if (MAI.isLittleEndian()) {
32315ffd83dbSDimitry Andric       getStreamer().emitInt64(lo);
32325ffd83dbSDimitry Andric       getStreamer().emitInt64(hi);
32330b57cec5SDimitry Andric     } else {
32345ffd83dbSDimitry Andric       getStreamer().emitInt64(hi);
32355ffd83dbSDimitry Andric       getStreamer().emitInt64(lo);
32360b57cec5SDimitry Andric     }
32370b57cec5SDimitry Andric     return false;
32380b57cec5SDimitry Andric   };
32390b57cec5SDimitry Andric 
3240fe6060f1SDimitry Andric   return parseMany(parseOp);
32410b57cec5SDimitry Andric }
32420b57cec5SDimitry Andric 
32430b57cec5SDimitry Andric bool AsmParser::parseRealValue(const fltSemantics &Semantics, APInt &Res) {
32440b57cec5SDimitry Andric   // We don't truly support arithmetic on floating point expressions, so we
32450b57cec5SDimitry Andric   // have to manually parse unary prefixes.
32460b57cec5SDimitry Andric   bool IsNeg = false;
32470b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Minus)) {
32480b57cec5SDimitry Andric     Lexer.Lex();
32490b57cec5SDimitry Andric     IsNeg = true;
32500b57cec5SDimitry Andric   } else if (getLexer().is(AsmToken::Plus))
32510b57cec5SDimitry Andric     Lexer.Lex();
32520b57cec5SDimitry Andric 
32530b57cec5SDimitry Andric   if (Lexer.is(AsmToken::Error))
32540b57cec5SDimitry Andric     return TokError(Lexer.getErr());
32550b57cec5SDimitry Andric   if (Lexer.isNot(AsmToken::Integer) && Lexer.isNot(AsmToken::Real) &&
32560b57cec5SDimitry Andric       Lexer.isNot(AsmToken::Identifier))
32570b57cec5SDimitry Andric     return TokError("unexpected token in directive");
32580b57cec5SDimitry Andric 
32590b57cec5SDimitry Andric   // Convert to an APFloat.
32600b57cec5SDimitry Andric   APFloat Value(Semantics);
32610b57cec5SDimitry Andric   StringRef IDVal = getTok().getString();
32620b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Identifier)) {
3263fe6060f1SDimitry Andric     if (!IDVal.compare_insensitive("infinity") ||
3264fe6060f1SDimitry Andric         !IDVal.compare_insensitive("inf"))
32650b57cec5SDimitry Andric       Value = APFloat::getInf(Semantics);
3266fe6060f1SDimitry Andric     else if (!IDVal.compare_insensitive("nan"))
32670b57cec5SDimitry Andric       Value = APFloat::getNaN(Semantics, false, ~0);
32680b57cec5SDimitry Andric     else
32690b57cec5SDimitry Andric       return TokError("invalid floating point literal");
3270480093f4SDimitry Andric   } else if (errorToBool(
3271480093f4SDimitry Andric                  Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven)
3272480093f4SDimitry Andric                      .takeError()))
32730b57cec5SDimitry Andric     return TokError("invalid floating point literal");
32740b57cec5SDimitry Andric   if (IsNeg)
32750b57cec5SDimitry Andric     Value.changeSign();
32760b57cec5SDimitry Andric 
32770b57cec5SDimitry Andric   // Consume the numeric token.
32780b57cec5SDimitry Andric   Lex();
32790b57cec5SDimitry Andric 
32800b57cec5SDimitry Andric   Res = Value.bitcastToAPInt();
32810b57cec5SDimitry Andric 
32820b57cec5SDimitry Andric   return false;
32830b57cec5SDimitry Andric }
32840b57cec5SDimitry Andric 
32850b57cec5SDimitry Andric /// parseDirectiveRealValue
32860b57cec5SDimitry Andric ///  ::= (.single | .double) [ expression (, expression)* ]
32870b57cec5SDimitry Andric bool AsmParser::parseDirectiveRealValue(StringRef IDVal,
32880b57cec5SDimitry Andric                                         const fltSemantics &Semantics) {
32890b57cec5SDimitry Andric   auto parseOp = [&]() -> bool {
32900b57cec5SDimitry Andric     APInt AsInt;
32910b57cec5SDimitry Andric     if (checkForValidSection() || parseRealValue(Semantics, AsInt))
32920b57cec5SDimitry Andric       return true;
32935ffd83dbSDimitry Andric     getStreamer().emitIntValue(AsInt.getLimitedValue(),
32940b57cec5SDimitry Andric                                AsInt.getBitWidth() / 8);
32950b57cec5SDimitry Andric     return false;
32960b57cec5SDimitry Andric   };
32970b57cec5SDimitry Andric 
3298fe6060f1SDimitry Andric   return parseMany(parseOp);
32990b57cec5SDimitry Andric }
33000b57cec5SDimitry Andric 
33010b57cec5SDimitry Andric /// parseDirectiveZero
33020b57cec5SDimitry Andric ///  ::= .zero expression
33030b57cec5SDimitry Andric bool AsmParser::parseDirectiveZero() {
33040b57cec5SDimitry Andric   SMLoc NumBytesLoc = Lexer.getLoc();
33050b57cec5SDimitry Andric   const MCExpr *NumBytes;
33060b57cec5SDimitry Andric   if (checkForValidSection() || parseExpression(NumBytes))
33070b57cec5SDimitry Andric     return true;
33080b57cec5SDimitry Andric 
33090b57cec5SDimitry Andric   int64_t Val = 0;
33100b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Comma)) {
33110b57cec5SDimitry Andric     Lex();
33120b57cec5SDimitry Andric     if (parseAbsoluteExpression(Val))
33130b57cec5SDimitry Andric       return true;
33140b57cec5SDimitry Andric   }
33150b57cec5SDimitry Andric 
3316fe6060f1SDimitry Andric   if (parseEOL())
33170b57cec5SDimitry Andric     return true;
33180b57cec5SDimitry Andric   getStreamer().emitFill(*NumBytes, Val, NumBytesLoc);
33190b57cec5SDimitry Andric 
33200b57cec5SDimitry Andric   return false;
33210b57cec5SDimitry Andric }
33220b57cec5SDimitry Andric 
33230b57cec5SDimitry Andric /// parseDirectiveFill
33240b57cec5SDimitry Andric ///  ::= .fill expression [ , expression [ , expression ] ]
33250b57cec5SDimitry Andric bool AsmParser::parseDirectiveFill() {
33260b57cec5SDimitry Andric   SMLoc NumValuesLoc = Lexer.getLoc();
33270b57cec5SDimitry Andric   const MCExpr *NumValues;
33280b57cec5SDimitry Andric   if (checkForValidSection() || parseExpression(NumValues))
33290b57cec5SDimitry Andric     return true;
33300b57cec5SDimitry Andric 
33310b57cec5SDimitry Andric   int64_t FillSize = 1;
33320b57cec5SDimitry Andric   int64_t FillExpr = 0;
33330b57cec5SDimitry Andric 
33340b57cec5SDimitry Andric   SMLoc SizeLoc, ExprLoc;
33350b57cec5SDimitry Andric 
33360b57cec5SDimitry Andric   if (parseOptionalToken(AsmToken::Comma)) {
33370b57cec5SDimitry Andric     SizeLoc = getTok().getLoc();
33380b57cec5SDimitry Andric     if (parseAbsoluteExpression(FillSize))
33390b57cec5SDimitry Andric       return true;
33400b57cec5SDimitry Andric     if (parseOptionalToken(AsmToken::Comma)) {
33410b57cec5SDimitry Andric       ExprLoc = getTok().getLoc();
33420b57cec5SDimitry Andric       if (parseAbsoluteExpression(FillExpr))
33430b57cec5SDimitry Andric         return true;
33440b57cec5SDimitry Andric     }
33450b57cec5SDimitry Andric   }
3346fe6060f1SDimitry Andric   if (parseEOL())
33470b57cec5SDimitry Andric     return true;
33480b57cec5SDimitry Andric 
33490b57cec5SDimitry Andric   if (FillSize < 0) {
33500b57cec5SDimitry Andric     Warning(SizeLoc, "'.fill' directive with negative size has no effect");
33510b57cec5SDimitry Andric     return false;
33520b57cec5SDimitry Andric   }
33530b57cec5SDimitry Andric   if (FillSize > 8) {
33540b57cec5SDimitry Andric     Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
33550b57cec5SDimitry Andric     FillSize = 8;
33560b57cec5SDimitry Andric   }
33570b57cec5SDimitry Andric 
33580b57cec5SDimitry Andric   if (!isUInt<32>(FillExpr) && FillSize > 4)
33590b57cec5SDimitry Andric     Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
33600b57cec5SDimitry Andric 
33610b57cec5SDimitry Andric   getStreamer().emitFill(*NumValues, FillSize, FillExpr, NumValuesLoc);
33620b57cec5SDimitry Andric 
33630b57cec5SDimitry Andric   return false;
33640b57cec5SDimitry Andric }
33650b57cec5SDimitry Andric 
33660b57cec5SDimitry Andric /// parseDirectiveOrg
33670b57cec5SDimitry Andric ///  ::= .org expression [ , expression ]
33680b57cec5SDimitry Andric bool AsmParser::parseDirectiveOrg() {
33690b57cec5SDimitry Andric   const MCExpr *Offset;
33700b57cec5SDimitry Andric   SMLoc OffsetLoc = Lexer.getLoc();
33710b57cec5SDimitry Andric   if (checkForValidSection() || parseExpression(Offset))
33720b57cec5SDimitry Andric     return true;
33730b57cec5SDimitry Andric 
33740b57cec5SDimitry Andric   // Parse optional fill expression.
33750b57cec5SDimitry Andric   int64_t FillExpr = 0;
33760b57cec5SDimitry Andric   if (parseOptionalToken(AsmToken::Comma))
33770b57cec5SDimitry Andric     if (parseAbsoluteExpression(FillExpr))
3378fe6060f1SDimitry Andric       return true;
3379fe6060f1SDimitry Andric   if (parseEOL())
3380fe6060f1SDimitry Andric     return true;
33810b57cec5SDimitry Andric 
33820b57cec5SDimitry Andric   getStreamer().emitValueToOffset(Offset, FillExpr, OffsetLoc);
33830b57cec5SDimitry Andric   return false;
33840b57cec5SDimitry Andric }
33850b57cec5SDimitry Andric 
33860b57cec5SDimitry Andric /// parseDirectiveAlign
33870b57cec5SDimitry Andric ///  ::= {.align, ...} expression [ , expression [ , expression ]]
33880b57cec5SDimitry Andric bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
33890b57cec5SDimitry Andric   SMLoc AlignmentLoc = getLexer().getLoc();
33900b57cec5SDimitry Andric   int64_t Alignment;
33910b57cec5SDimitry Andric   SMLoc MaxBytesLoc;
33920b57cec5SDimitry Andric   bool HasFillExpr = false;
33930b57cec5SDimitry Andric   int64_t FillExpr = 0;
33940b57cec5SDimitry Andric   int64_t MaxBytesToFill = 0;
33950b57cec5SDimitry Andric 
33960b57cec5SDimitry Andric   auto parseAlign = [&]() -> bool {
33970b57cec5SDimitry Andric     if (parseAbsoluteExpression(Alignment))
33980b57cec5SDimitry Andric       return true;
33990b57cec5SDimitry Andric     if (parseOptionalToken(AsmToken::Comma)) {
34000b57cec5SDimitry Andric       // The fill expression can be omitted while specifying a maximum number of
34010b57cec5SDimitry Andric       // alignment bytes, e.g:
34020b57cec5SDimitry Andric       //  .align 3,,4
34030b57cec5SDimitry Andric       if (getTok().isNot(AsmToken::Comma)) {
34040b57cec5SDimitry Andric         HasFillExpr = true;
34050b57cec5SDimitry Andric         if (parseAbsoluteExpression(FillExpr))
34060b57cec5SDimitry Andric           return true;
34070b57cec5SDimitry Andric       }
34080b57cec5SDimitry Andric       if (parseOptionalToken(AsmToken::Comma))
34090b57cec5SDimitry Andric         if (parseTokenLoc(MaxBytesLoc) ||
34100b57cec5SDimitry Andric             parseAbsoluteExpression(MaxBytesToFill))
34110b57cec5SDimitry Andric           return true;
34120b57cec5SDimitry Andric     }
3413fe6060f1SDimitry Andric     return parseEOL();
34140b57cec5SDimitry Andric   };
34150b57cec5SDimitry Andric 
34160b57cec5SDimitry Andric   if (checkForValidSection())
3417fe6060f1SDimitry Andric     return true;
34180b57cec5SDimitry Andric   // Ignore empty '.p2align' directives for GNU-as compatibility
34190b57cec5SDimitry Andric   if (IsPow2 && (ValueSize == 1) && getTok().is(AsmToken::EndOfStatement)) {
34200b57cec5SDimitry Andric     Warning(AlignmentLoc, "p2align directive with no operand(s) is ignored");
3421fe6060f1SDimitry Andric     return parseEOL();
34220b57cec5SDimitry Andric   }
34230b57cec5SDimitry Andric   if (parseAlign())
3424fe6060f1SDimitry Andric     return true;
34250b57cec5SDimitry Andric 
34260b57cec5SDimitry Andric   // Always emit an alignment here even if we thrown an error.
34270b57cec5SDimitry Andric   bool ReturnVal = false;
34280b57cec5SDimitry Andric 
34290b57cec5SDimitry Andric   // Compute alignment in bytes.
34300b57cec5SDimitry Andric   if (IsPow2) {
34310b57cec5SDimitry Andric     // FIXME: Diagnose overflow.
34320b57cec5SDimitry Andric     if (Alignment >= 32) {
34330b57cec5SDimitry Andric       ReturnVal |= Error(AlignmentLoc, "invalid alignment value");
34340b57cec5SDimitry Andric       Alignment = 31;
34350b57cec5SDimitry Andric     }
34360b57cec5SDimitry Andric 
34370b57cec5SDimitry Andric     Alignment = 1ULL << Alignment;
34380b57cec5SDimitry Andric   } else {
34390b57cec5SDimitry Andric     // Reject alignments that aren't either a power of two or zero,
34400b57cec5SDimitry Andric     // for gas compatibility. Alignment of zero is silently rounded
34410b57cec5SDimitry Andric     // up to one.
34420b57cec5SDimitry Andric     if (Alignment == 0)
34430b57cec5SDimitry Andric       Alignment = 1;
344481ad6265SDimitry Andric     else if (!isPowerOf2_64(Alignment)) {
34450b57cec5SDimitry Andric       ReturnVal |= Error(AlignmentLoc, "alignment must be a power of 2");
3446*06c3fb27SDimitry Andric       Alignment = llvm::bit_floor<uint64_t>(Alignment);
344781ad6265SDimitry Andric     }
344881ad6265SDimitry Andric     if (!isUInt<32>(Alignment)) {
3449e8d8bef9SDimitry Andric       ReturnVal |= Error(AlignmentLoc, "alignment must be smaller than 2**32");
345081ad6265SDimitry Andric       Alignment = 1u << 31;
345181ad6265SDimitry Andric     }
34520b57cec5SDimitry Andric   }
34530b57cec5SDimitry Andric 
34540b57cec5SDimitry Andric   // Diagnose non-sensical max bytes to align.
34550b57cec5SDimitry Andric   if (MaxBytesLoc.isValid()) {
34560b57cec5SDimitry Andric     if (MaxBytesToFill < 1) {
34570b57cec5SDimitry Andric       ReturnVal |= Error(MaxBytesLoc,
34580b57cec5SDimitry Andric                          "alignment directive can never be satisfied in this "
34590b57cec5SDimitry Andric                          "many bytes, ignoring maximum bytes expression");
34600b57cec5SDimitry Andric       MaxBytesToFill = 0;
34610b57cec5SDimitry Andric     }
34620b57cec5SDimitry Andric 
34630b57cec5SDimitry Andric     if (MaxBytesToFill >= Alignment) {
34640b57cec5SDimitry Andric       Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
34650b57cec5SDimitry Andric                            "has no effect");
34660b57cec5SDimitry Andric       MaxBytesToFill = 0;
34670b57cec5SDimitry Andric     }
34680b57cec5SDimitry Andric   }
34690b57cec5SDimitry Andric 
34700b57cec5SDimitry Andric   // Check whether we should use optimal code alignment for this .align
34710b57cec5SDimitry Andric   // directive.
34720b57cec5SDimitry Andric   const MCSection *Section = getStreamer().getCurrentSectionOnly();
34730b57cec5SDimitry Andric   assert(Section && "must have section to emit alignment");
347481ad6265SDimitry Andric   bool useCodeAlign = Section->useCodeAlign();
34750b57cec5SDimitry Andric   if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
347681ad6265SDimitry Andric       ValueSize == 1 && useCodeAlign) {
3477bdd1243dSDimitry Andric     getStreamer().emitCodeAlignment(
3478bdd1243dSDimitry Andric         Align(Alignment), &getTargetParser().getSTI(), MaxBytesToFill);
34790b57cec5SDimitry Andric   } else {
34800b57cec5SDimitry Andric     // FIXME: Target specific behavior about how the "extra" bytes are filled.
3481bdd1243dSDimitry Andric     getStreamer().emitValueToAlignment(Align(Alignment), FillExpr, ValueSize,
34820b57cec5SDimitry Andric                                        MaxBytesToFill);
34830b57cec5SDimitry Andric   }
34840b57cec5SDimitry Andric 
34850b57cec5SDimitry Andric   return ReturnVal;
34860b57cec5SDimitry Andric }
34870b57cec5SDimitry Andric 
34880b57cec5SDimitry Andric /// parseDirectiveFile
34890b57cec5SDimitry Andric /// ::= .file filename
34900b57cec5SDimitry Andric /// ::= .file number [directory] filename [md5 checksum] [source source-text]
34910b57cec5SDimitry Andric bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
34920b57cec5SDimitry Andric   // FIXME: I'm not sure what this is.
34930b57cec5SDimitry Andric   int64_t FileNumber = -1;
34940b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Integer)) {
34950b57cec5SDimitry Andric     FileNumber = getTok().getIntVal();
34960b57cec5SDimitry Andric     Lex();
34970b57cec5SDimitry Andric 
34980b57cec5SDimitry Andric     if (FileNumber < 0)
34990b57cec5SDimitry Andric       return TokError("negative file number");
35000b57cec5SDimitry Andric   }
35010b57cec5SDimitry Andric 
35020b57cec5SDimitry Andric   std::string Path;
35030b57cec5SDimitry Andric 
35040b57cec5SDimitry Andric   // Usually the directory and filename together, otherwise just the directory.
35050b57cec5SDimitry Andric   // Allow the strings to have escaped octal character sequence.
3506fe6060f1SDimitry Andric   if (parseEscapedString(Path))
35070b57cec5SDimitry Andric     return true;
35080b57cec5SDimitry Andric 
35090b57cec5SDimitry Andric   StringRef Directory;
35100b57cec5SDimitry Andric   StringRef Filename;
35110b57cec5SDimitry Andric   std::string FilenameData;
35120b57cec5SDimitry Andric   if (getLexer().is(AsmToken::String)) {
35130b57cec5SDimitry Andric     if (check(FileNumber == -1,
35140b57cec5SDimitry Andric               "explicit path specified, but no file number") ||
35150b57cec5SDimitry Andric         parseEscapedString(FilenameData))
35160b57cec5SDimitry Andric       return true;
35170b57cec5SDimitry Andric     Filename = FilenameData;
35180b57cec5SDimitry Andric     Directory = Path;
35190b57cec5SDimitry Andric   } else {
35200b57cec5SDimitry Andric     Filename = Path;
35210b57cec5SDimitry Andric   }
35220b57cec5SDimitry Andric 
35230b57cec5SDimitry Andric   uint64_t MD5Hi, MD5Lo;
35240b57cec5SDimitry Andric   bool HasMD5 = false;
35250b57cec5SDimitry Andric 
3526bdd1243dSDimitry Andric   std::optional<StringRef> Source;
35270b57cec5SDimitry Andric   bool HasSource = false;
35280b57cec5SDimitry Andric   std::string SourceString;
35290b57cec5SDimitry Andric 
35300b57cec5SDimitry Andric   while (!parseOptionalToken(AsmToken::EndOfStatement)) {
35310b57cec5SDimitry Andric     StringRef Keyword;
35320b57cec5SDimitry Andric     if (check(getTok().isNot(AsmToken::Identifier),
35330b57cec5SDimitry Andric               "unexpected token in '.file' directive") ||
35340b57cec5SDimitry Andric         parseIdentifier(Keyword))
35350b57cec5SDimitry Andric       return true;
35360b57cec5SDimitry Andric     if (Keyword == "md5") {
35370b57cec5SDimitry Andric       HasMD5 = true;
35380b57cec5SDimitry Andric       if (check(FileNumber == -1,
35390b57cec5SDimitry Andric                 "MD5 checksum specified, but no file number") ||
35400b57cec5SDimitry Andric           parseHexOcta(*this, MD5Hi, MD5Lo))
35410b57cec5SDimitry Andric         return true;
35420b57cec5SDimitry Andric     } else if (Keyword == "source") {
35430b57cec5SDimitry Andric       HasSource = true;
35440b57cec5SDimitry Andric       if (check(FileNumber == -1,
35450b57cec5SDimitry Andric                 "source specified, but no file number") ||
35460b57cec5SDimitry Andric           check(getTok().isNot(AsmToken::String),
35470b57cec5SDimitry Andric                 "unexpected token in '.file' directive") ||
35480b57cec5SDimitry Andric           parseEscapedString(SourceString))
35490b57cec5SDimitry Andric         return true;
35500b57cec5SDimitry Andric     } else {
35510b57cec5SDimitry Andric       return TokError("unexpected token in '.file' directive");
35520b57cec5SDimitry Andric     }
35530b57cec5SDimitry Andric   }
35540b57cec5SDimitry Andric 
35550b57cec5SDimitry Andric   if (FileNumber == -1) {
35560b57cec5SDimitry Andric     // Ignore the directive if there is no number and the target doesn't support
35570b57cec5SDimitry Andric     // numberless .file directives. This allows some portability of assembler
35580b57cec5SDimitry Andric     // between different object file formats.
35590b57cec5SDimitry Andric     if (getContext().getAsmInfo()->hasSingleParameterDotFile())
35605ffd83dbSDimitry Andric       getStreamer().emitFileDirective(Filename);
35610b57cec5SDimitry Andric   } else {
35620b57cec5SDimitry Andric     // In case there is a -g option as well as debug info from directive .file,
35630b57cec5SDimitry Andric     // we turn off the -g option, directly use the existing debug info instead.
35640b57cec5SDimitry Andric     // Throw away any implicit file table for the assembler source.
35650b57cec5SDimitry Andric     if (Ctx.getGenDwarfForAssembly()) {
35660b57cec5SDimitry Andric       Ctx.getMCDwarfLineTable(0).resetFileTable();
35670b57cec5SDimitry Andric       Ctx.setGenDwarfForAssembly(false);
35680b57cec5SDimitry Andric     }
35690b57cec5SDimitry Andric 
3570bdd1243dSDimitry Andric     std::optional<MD5::MD5Result> CKMem;
35710b57cec5SDimitry Andric     if (HasMD5) {
35720b57cec5SDimitry Andric       MD5::MD5Result Sum;
35730b57cec5SDimitry Andric       for (unsigned i = 0; i != 8; ++i) {
357481ad6265SDimitry Andric         Sum[i] = uint8_t(MD5Hi >> ((7 - i) * 8));
357581ad6265SDimitry Andric         Sum[i + 8] = uint8_t(MD5Lo >> ((7 - i) * 8));
35760b57cec5SDimitry Andric       }
35770b57cec5SDimitry Andric       CKMem = Sum;
35780b57cec5SDimitry Andric     }
35790b57cec5SDimitry Andric     if (HasSource) {
35800b57cec5SDimitry Andric       char *SourceBuf = static_cast<char *>(Ctx.allocate(SourceString.size()));
35810b57cec5SDimitry Andric       memcpy(SourceBuf, SourceString.data(), SourceString.size());
35820b57cec5SDimitry Andric       Source = StringRef(SourceBuf, SourceString.size());
35830b57cec5SDimitry Andric     }
35840b57cec5SDimitry Andric     if (FileNumber == 0) {
3585fe6060f1SDimitry Andric       // Upgrade to Version 5 for assembly actions like clang -c a.s.
35860b57cec5SDimitry Andric       if (Ctx.getDwarfVersion() < 5)
3587fe6060f1SDimitry Andric         Ctx.setDwarfVersion(5);
35880b57cec5SDimitry Andric       getStreamer().emitDwarfFile0Directive(Directory, Filename, CKMem, Source);
35890b57cec5SDimitry Andric     } else {
35900b57cec5SDimitry Andric       Expected<unsigned> FileNumOrErr = getStreamer().tryEmitDwarfFileDirective(
35910b57cec5SDimitry Andric           FileNumber, Directory, Filename, CKMem, Source);
35920b57cec5SDimitry Andric       if (!FileNumOrErr)
35930b57cec5SDimitry Andric         return Error(DirectiveLoc, toString(FileNumOrErr.takeError()));
35940b57cec5SDimitry Andric     }
35950b57cec5SDimitry Andric     // Alert the user if there are some .file directives with MD5 and some not.
35960b57cec5SDimitry Andric     // But only do that once.
35970b57cec5SDimitry Andric     if (!ReportedInconsistentMD5 && !Ctx.isDwarfMD5UsageConsistent(0)) {
35980b57cec5SDimitry Andric       ReportedInconsistentMD5 = true;
35990b57cec5SDimitry Andric       return Warning(DirectiveLoc, "inconsistent use of MD5 checksums");
36000b57cec5SDimitry Andric     }
36010b57cec5SDimitry Andric   }
36020b57cec5SDimitry Andric 
36030b57cec5SDimitry Andric   return false;
36040b57cec5SDimitry Andric }
36050b57cec5SDimitry Andric 
36060b57cec5SDimitry Andric /// parseDirectiveLine
36070b57cec5SDimitry Andric /// ::= .line [number]
36080b57cec5SDimitry Andric bool AsmParser::parseDirectiveLine() {
36090b57cec5SDimitry Andric   int64_t LineNumber;
36100b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Integer)) {
36110b57cec5SDimitry Andric     if (parseIntToken(LineNumber, "unexpected token in '.line' directive"))
36120b57cec5SDimitry Andric       return true;
36130b57cec5SDimitry Andric     (void)LineNumber;
36140b57cec5SDimitry Andric     // FIXME: Do something with the .line.
36150b57cec5SDimitry Andric   }
3616fe6060f1SDimitry Andric   return parseEOL();
36170b57cec5SDimitry Andric }
36180b57cec5SDimitry Andric 
36190b57cec5SDimitry Andric /// parseDirectiveLoc
36200b57cec5SDimitry Andric /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
36210b57cec5SDimitry Andric ///                                [epilogue_begin] [is_stmt VALUE] [isa VALUE]
36220b57cec5SDimitry Andric /// The first number is a file number, must have been previously assigned with
36230b57cec5SDimitry Andric /// a .file directive, the second number is the line number and optionally the
36240b57cec5SDimitry Andric /// third number is a column position (zero if not specified).  The remaining
36250b57cec5SDimitry Andric /// optional items are .loc sub-directives.
36260b57cec5SDimitry Andric bool AsmParser::parseDirectiveLoc() {
36270b57cec5SDimitry Andric   int64_t FileNumber = 0, LineNumber = 0;
36280b57cec5SDimitry Andric   SMLoc Loc = getTok().getLoc();
36290b57cec5SDimitry Andric   if (parseIntToken(FileNumber, "unexpected token in '.loc' directive") ||
36300b57cec5SDimitry Andric       check(FileNumber < 1 && Ctx.getDwarfVersion() < 5, Loc,
36310b57cec5SDimitry Andric             "file number less than one in '.loc' directive") ||
36320b57cec5SDimitry Andric       check(!getContext().isValidDwarfFileNumber(FileNumber), Loc,
36330b57cec5SDimitry Andric             "unassigned file number in '.loc' directive"))
36340b57cec5SDimitry Andric     return true;
36350b57cec5SDimitry Andric 
36360b57cec5SDimitry Andric   // optional
36370b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Integer)) {
36380b57cec5SDimitry Andric     LineNumber = getTok().getIntVal();
36390b57cec5SDimitry Andric     if (LineNumber < 0)
36400b57cec5SDimitry Andric       return TokError("line number less than zero in '.loc' directive");
36410b57cec5SDimitry Andric     Lex();
36420b57cec5SDimitry Andric   }
36430b57cec5SDimitry Andric 
36440b57cec5SDimitry Andric   int64_t ColumnPos = 0;
36450b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Integer)) {
36460b57cec5SDimitry Andric     ColumnPos = getTok().getIntVal();
36470b57cec5SDimitry Andric     if (ColumnPos < 0)
36480b57cec5SDimitry Andric       return TokError("column position less than zero in '.loc' directive");
36490b57cec5SDimitry Andric     Lex();
36500b57cec5SDimitry Andric   }
36510b57cec5SDimitry Andric 
36525ffd83dbSDimitry Andric   auto PrevFlags = getContext().getCurrentDwarfLoc().getFlags();
36535ffd83dbSDimitry Andric   unsigned Flags = PrevFlags & DWARF2_FLAG_IS_STMT;
36540b57cec5SDimitry Andric   unsigned Isa = 0;
36550b57cec5SDimitry Andric   int64_t Discriminator = 0;
36560b57cec5SDimitry Andric 
36570b57cec5SDimitry Andric   auto parseLocOp = [&]() -> bool {
36580b57cec5SDimitry Andric     StringRef Name;
36590b57cec5SDimitry Andric     SMLoc Loc = getTok().getLoc();
36600b57cec5SDimitry Andric     if (parseIdentifier(Name))
36610b57cec5SDimitry Andric       return TokError("unexpected token in '.loc' directive");
36620b57cec5SDimitry Andric 
36630b57cec5SDimitry Andric     if (Name == "basic_block")
36640b57cec5SDimitry Andric       Flags |= DWARF2_FLAG_BASIC_BLOCK;
36650b57cec5SDimitry Andric     else if (Name == "prologue_end")
36660b57cec5SDimitry Andric       Flags |= DWARF2_FLAG_PROLOGUE_END;
36670b57cec5SDimitry Andric     else if (Name == "epilogue_begin")
36680b57cec5SDimitry Andric       Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
36690b57cec5SDimitry Andric     else if (Name == "is_stmt") {
36700b57cec5SDimitry Andric       Loc = getTok().getLoc();
36710b57cec5SDimitry Andric       const MCExpr *Value;
36720b57cec5SDimitry Andric       if (parseExpression(Value))
36730b57cec5SDimitry Andric         return true;
36740b57cec5SDimitry Andric       // The expression must be the constant 0 or 1.
36750b57cec5SDimitry Andric       if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
36760b57cec5SDimitry Andric         int Value = MCE->getValue();
36770b57cec5SDimitry Andric         if (Value == 0)
36780b57cec5SDimitry Andric           Flags &= ~DWARF2_FLAG_IS_STMT;
36790b57cec5SDimitry Andric         else if (Value == 1)
36800b57cec5SDimitry Andric           Flags |= DWARF2_FLAG_IS_STMT;
36810b57cec5SDimitry Andric         else
36820b57cec5SDimitry Andric           return Error(Loc, "is_stmt value not 0 or 1");
36830b57cec5SDimitry Andric       } else {
36840b57cec5SDimitry Andric         return Error(Loc, "is_stmt value not the constant value of 0 or 1");
36850b57cec5SDimitry Andric       }
36860b57cec5SDimitry Andric     } else if (Name == "isa") {
36870b57cec5SDimitry Andric       Loc = getTok().getLoc();
36880b57cec5SDimitry Andric       const MCExpr *Value;
36890b57cec5SDimitry Andric       if (parseExpression(Value))
36900b57cec5SDimitry Andric         return true;
36910b57cec5SDimitry Andric       // The expression must be a constant greater or equal to 0.
36920b57cec5SDimitry Andric       if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
36930b57cec5SDimitry Andric         int Value = MCE->getValue();
36940b57cec5SDimitry Andric         if (Value < 0)
36950b57cec5SDimitry Andric           return Error(Loc, "isa number less than zero");
36960b57cec5SDimitry Andric         Isa = Value;
36970b57cec5SDimitry Andric       } else {
36980b57cec5SDimitry Andric         return Error(Loc, "isa number not a constant value");
36990b57cec5SDimitry Andric       }
37000b57cec5SDimitry Andric     } else if (Name == "discriminator") {
37010b57cec5SDimitry Andric       if (parseAbsoluteExpression(Discriminator))
37020b57cec5SDimitry Andric         return true;
37030b57cec5SDimitry Andric     } else {
37040b57cec5SDimitry Andric       return Error(Loc, "unknown sub-directive in '.loc' directive");
37050b57cec5SDimitry Andric     }
37060b57cec5SDimitry Andric     return false;
37070b57cec5SDimitry Andric   };
37080b57cec5SDimitry Andric 
37090b57cec5SDimitry Andric   if (parseMany(parseLocOp, false /*hasComma*/))
37100b57cec5SDimitry Andric     return true;
37110b57cec5SDimitry Andric 
37125ffd83dbSDimitry Andric   getStreamer().emitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
37130b57cec5SDimitry Andric                                       Isa, Discriminator, StringRef());
37140b57cec5SDimitry Andric 
37150b57cec5SDimitry Andric   return false;
37160b57cec5SDimitry Andric }
37170b57cec5SDimitry Andric 
37180b57cec5SDimitry Andric /// parseDirectiveStabs
37190b57cec5SDimitry Andric /// ::= .stabs string, number, number, number
37200b57cec5SDimitry Andric bool AsmParser::parseDirectiveStabs() {
37210b57cec5SDimitry Andric   return TokError("unsupported directive '.stabs'");
37220b57cec5SDimitry Andric }
37230b57cec5SDimitry Andric 
37240b57cec5SDimitry Andric /// parseDirectiveCVFile
37250b57cec5SDimitry Andric /// ::= .cv_file number filename [checksum] [checksumkind]
37260b57cec5SDimitry Andric bool AsmParser::parseDirectiveCVFile() {
37270b57cec5SDimitry Andric   SMLoc FileNumberLoc = getTok().getLoc();
37280b57cec5SDimitry Andric   int64_t FileNumber;
37290b57cec5SDimitry Andric   std::string Filename;
37300b57cec5SDimitry Andric   std::string Checksum;
37310b57cec5SDimitry Andric   int64_t ChecksumKind = 0;
37320b57cec5SDimitry Andric 
37330b57cec5SDimitry Andric   if (parseIntToken(FileNumber,
37340b57cec5SDimitry Andric                     "expected file number in '.cv_file' directive") ||
37350b57cec5SDimitry Andric       check(FileNumber < 1, FileNumberLoc, "file number less than one") ||
37360b57cec5SDimitry Andric       check(getTok().isNot(AsmToken::String),
37370b57cec5SDimitry Andric             "unexpected token in '.cv_file' directive") ||
37380b57cec5SDimitry Andric       parseEscapedString(Filename))
37390b57cec5SDimitry Andric     return true;
37400b57cec5SDimitry Andric   if (!parseOptionalToken(AsmToken::EndOfStatement)) {
37410b57cec5SDimitry Andric     if (check(getTok().isNot(AsmToken::String),
37420b57cec5SDimitry Andric               "unexpected token in '.cv_file' directive") ||
37430b57cec5SDimitry Andric         parseEscapedString(Checksum) ||
37440b57cec5SDimitry Andric         parseIntToken(ChecksumKind,
37450b57cec5SDimitry Andric                       "expected checksum kind in '.cv_file' directive") ||
374681ad6265SDimitry Andric         parseEOL())
37470b57cec5SDimitry Andric       return true;
37480b57cec5SDimitry Andric   }
37490b57cec5SDimitry Andric 
37500b57cec5SDimitry Andric   Checksum = fromHex(Checksum);
37510b57cec5SDimitry Andric   void *CKMem = Ctx.allocate(Checksum.size(), 1);
37520b57cec5SDimitry Andric   memcpy(CKMem, Checksum.data(), Checksum.size());
37530b57cec5SDimitry Andric   ArrayRef<uint8_t> ChecksumAsBytes(reinterpret_cast<const uint8_t *>(CKMem),
37540b57cec5SDimitry Andric                                     Checksum.size());
37550b57cec5SDimitry Andric 
375681ad6265SDimitry Andric   if (!getStreamer().emitCVFileDirective(FileNumber, Filename, ChecksumAsBytes,
37570b57cec5SDimitry Andric                                          static_cast<uint8_t>(ChecksumKind)))
37580b57cec5SDimitry Andric     return Error(FileNumberLoc, "file number already allocated");
37590b57cec5SDimitry Andric 
37600b57cec5SDimitry Andric   return false;
37610b57cec5SDimitry Andric }
37620b57cec5SDimitry Andric 
37630b57cec5SDimitry Andric bool AsmParser::parseCVFunctionId(int64_t &FunctionId,
37640b57cec5SDimitry Andric                                   StringRef DirectiveName) {
37650b57cec5SDimitry Andric   SMLoc Loc;
37660b57cec5SDimitry Andric   return parseTokenLoc(Loc) ||
37670b57cec5SDimitry Andric          parseIntToken(FunctionId, "expected function id in '" + DirectiveName +
37680b57cec5SDimitry Andric                                        "' directive") ||
37690b57cec5SDimitry Andric          check(FunctionId < 0 || FunctionId >= UINT_MAX, Loc,
37700b57cec5SDimitry Andric                "expected function id within range [0, UINT_MAX)");
37710b57cec5SDimitry Andric }
37720b57cec5SDimitry Andric 
37730b57cec5SDimitry Andric bool AsmParser::parseCVFileId(int64_t &FileNumber, StringRef DirectiveName) {
37740b57cec5SDimitry Andric   SMLoc Loc;
37750b57cec5SDimitry Andric   return parseTokenLoc(Loc) ||
37760b57cec5SDimitry Andric          parseIntToken(FileNumber, "expected integer in '" + DirectiveName +
37770b57cec5SDimitry Andric                                        "' directive") ||
37780b57cec5SDimitry Andric          check(FileNumber < 1, Loc, "file number less than one in '" +
37790b57cec5SDimitry Andric                                         DirectiveName + "' directive") ||
37800b57cec5SDimitry Andric          check(!getCVContext().isValidFileNumber(FileNumber), Loc,
37810b57cec5SDimitry Andric                "unassigned file number in '" + DirectiveName + "' directive");
37820b57cec5SDimitry Andric }
37830b57cec5SDimitry Andric 
37840b57cec5SDimitry Andric /// parseDirectiveCVFuncId
37850b57cec5SDimitry Andric /// ::= .cv_func_id FunctionId
37860b57cec5SDimitry Andric ///
37870b57cec5SDimitry Andric /// Introduces a function ID that can be used with .cv_loc.
37880b57cec5SDimitry Andric bool AsmParser::parseDirectiveCVFuncId() {
37890b57cec5SDimitry Andric   SMLoc FunctionIdLoc = getTok().getLoc();
37900b57cec5SDimitry Andric   int64_t FunctionId;
37910b57cec5SDimitry Andric 
379281ad6265SDimitry Andric   if (parseCVFunctionId(FunctionId, ".cv_func_id") || parseEOL())
37930b57cec5SDimitry Andric     return true;
37940b57cec5SDimitry Andric 
379581ad6265SDimitry Andric   if (!getStreamer().emitCVFuncIdDirective(FunctionId))
37960b57cec5SDimitry Andric     return Error(FunctionIdLoc, "function id already allocated");
37970b57cec5SDimitry Andric 
37980b57cec5SDimitry Andric   return false;
37990b57cec5SDimitry Andric }
38000b57cec5SDimitry Andric 
38010b57cec5SDimitry Andric /// parseDirectiveCVInlineSiteId
38020b57cec5SDimitry Andric /// ::= .cv_inline_site_id FunctionId
38030b57cec5SDimitry Andric ///         "within" IAFunc
38040b57cec5SDimitry Andric ///         "inlined_at" IAFile IALine [IACol]
38050b57cec5SDimitry Andric ///
38060b57cec5SDimitry Andric /// Introduces a function ID that can be used with .cv_loc. Includes "inlined
38070b57cec5SDimitry Andric /// at" source location information for use in the line table of the caller,
38080b57cec5SDimitry Andric /// whether the caller is a real function or another inlined call site.
38090b57cec5SDimitry Andric bool AsmParser::parseDirectiveCVInlineSiteId() {
38100b57cec5SDimitry Andric   SMLoc FunctionIdLoc = getTok().getLoc();
38110b57cec5SDimitry Andric   int64_t FunctionId;
38120b57cec5SDimitry Andric   int64_t IAFunc;
38130b57cec5SDimitry Andric   int64_t IAFile;
38140b57cec5SDimitry Andric   int64_t IALine;
38150b57cec5SDimitry Andric   int64_t IACol = 0;
38160b57cec5SDimitry Andric 
38170b57cec5SDimitry Andric   // FunctionId
38180b57cec5SDimitry Andric   if (parseCVFunctionId(FunctionId, ".cv_inline_site_id"))
38190b57cec5SDimitry Andric     return true;
38200b57cec5SDimitry Andric 
38210b57cec5SDimitry Andric   // "within"
38220b57cec5SDimitry Andric   if (check((getLexer().isNot(AsmToken::Identifier) ||
38230b57cec5SDimitry Andric              getTok().getIdentifier() != "within"),
38240b57cec5SDimitry Andric             "expected 'within' identifier in '.cv_inline_site_id' directive"))
38250b57cec5SDimitry Andric     return true;
38260b57cec5SDimitry Andric   Lex();
38270b57cec5SDimitry Andric 
38280b57cec5SDimitry Andric   // IAFunc
38290b57cec5SDimitry Andric   if (parseCVFunctionId(IAFunc, ".cv_inline_site_id"))
38300b57cec5SDimitry Andric     return true;
38310b57cec5SDimitry Andric 
38320b57cec5SDimitry Andric   // "inlined_at"
38330b57cec5SDimitry Andric   if (check((getLexer().isNot(AsmToken::Identifier) ||
38340b57cec5SDimitry Andric              getTok().getIdentifier() != "inlined_at"),
38350b57cec5SDimitry Andric             "expected 'inlined_at' identifier in '.cv_inline_site_id' "
38360b57cec5SDimitry Andric             "directive") )
38370b57cec5SDimitry Andric     return true;
38380b57cec5SDimitry Andric   Lex();
38390b57cec5SDimitry Andric 
38400b57cec5SDimitry Andric   // IAFile IALine
38410b57cec5SDimitry Andric   if (parseCVFileId(IAFile, ".cv_inline_site_id") ||
38420b57cec5SDimitry Andric       parseIntToken(IALine, "expected line number after 'inlined_at'"))
38430b57cec5SDimitry Andric     return true;
38440b57cec5SDimitry Andric 
38450b57cec5SDimitry Andric   // [IACol]
38460b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Integer)) {
38470b57cec5SDimitry Andric     IACol = getTok().getIntVal();
38480b57cec5SDimitry Andric     Lex();
38490b57cec5SDimitry Andric   }
38500b57cec5SDimitry Andric 
385181ad6265SDimitry Andric   if (parseEOL())
38520b57cec5SDimitry Andric     return true;
38530b57cec5SDimitry Andric 
385481ad6265SDimitry Andric   if (!getStreamer().emitCVInlineSiteIdDirective(FunctionId, IAFunc, IAFile,
38550b57cec5SDimitry Andric                                                  IALine, IACol, FunctionIdLoc))
38560b57cec5SDimitry Andric     return Error(FunctionIdLoc, "function id already allocated");
38570b57cec5SDimitry Andric 
38580b57cec5SDimitry Andric   return false;
38590b57cec5SDimitry Andric }
38600b57cec5SDimitry Andric 
38610b57cec5SDimitry Andric /// parseDirectiveCVLoc
38620b57cec5SDimitry Andric /// ::= .cv_loc FunctionId FileNumber [LineNumber] [ColumnPos] [prologue_end]
38630b57cec5SDimitry Andric ///                                [is_stmt VALUE]
38640b57cec5SDimitry Andric /// The first number is a file number, must have been previously assigned with
38650b57cec5SDimitry Andric /// a .file directive, the second number is the line number and optionally the
38660b57cec5SDimitry Andric /// third number is a column position (zero if not specified).  The remaining
38670b57cec5SDimitry Andric /// optional items are .loc sub-directives.
38680b57cec5SDimitry Andric bool AsmParser::parseDirectiveCVLoc() {
38690b57cec5SDimitry Andric   SMLoc DirectiveLoc = getTok().getLoc();
38700b57cec5SDimitry Andric   int64_t FunctionId, FileNumber;
38710b57cec5SDimitry Andric   if (parseCVFunctionId(FunctionId, ".cv_loc") ||
38720b57cec5SDimitry Andric       parseCVFileId(FileNumber, ".cv_loc"))
38730b57cec5SDimitry Andric     return true;
38740b57cec5SDimitry Andric 
38750b57cec5SDimitry Andric   int64_t LineNumber = 0;
38760b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Integer)) {
38770b57cec5SDimitry Andric     LineNumber = getTok().getIntVal();
38780b57cec5SDimitry Andric     if (LineNumber < 0)
38790b57cec5SDimitry Andric       return TokError("line number less than zero in '.cv_loc' directive");
38800b57cec5SDimitry Andric     Lex();
38810b57cec5SDimitry Andric   }
38820b57cec5SDimitry Andric 
38830b57cec5SDimitry Andric   int64_t ColumnPos = 0;
38840b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Integer)) {
38850b57cec5SDimitry Andric     ColumnPos = getTok().getIntVal();
38860b57cec5SDimitry Andric     if (ColumnPos < 0)
38870b57cec5SDimitry Andric       return TokError("column position less than zero in '.cv_loc' directive");
38880b57cec5SDimitry Andric     Lex();
38890b57cec5SDimitry Andric   }
38900b57cec5SDimitry Andric 
38910b57cec5SDimitry Andric   bool PrologueEnd = false;
38920b57cec5SDimitry Andric   uint64_t IsStmt = 0;
38930b57cec5SDimitry Andric 
38940b57cec5SDimitry Andric   auto parseOp = [&]() -> bool {
38950b57cec5SDimitry Andric     StringRef Name;
38960b57cec5SDimitry Andric     SMLoc Loc = getTok().getLoc();
38970b57cec5SDimitry Andric     if (parseIdentifier(Name))
38980b57cec5SDimitry Andric       return TokError("unexpected token in '.cv_loc' directive");
38990b57cec5SDimitry Andric     if (Name == "prologue_end")
39000b57cec5SDimitry Andric       PrologueEnd = true;
39010b57cec5SDimitry Andric     else if (Name == "is_stmt") {
39020b57cec5SDimitry Andric       Loc = getTok().getLoc();
39030b57cec5SDimitry Andric       const MCExpr *Value;
39040b57cec5SDimitry Andric       if (parseExpression(Value))
39050b57cec5SDimitry Andric         return true;
39060b57cec5SDimitry Andric       // The expression must be the constant 0 or 1.
39070b57cec5SDimitry Andric       IsStmt = ~0ULL;
39080b57cec5SDimitry Andric       if (const auto *MCE = dyn_cast<MCConstantExpr>(Value))
39090b57cec5SDimitry Andric         IsStmt = MCE->getValue();
39100b57cec5SDimitry Andric 
39110b57cec5SDimitry Andric       if (IsStmt > 1)
39120b57cec5SDimitry Andric         return Error(Loc, "is_stmt value not 0 or 1");
39130b57cec5SDimitry Andric     } else {
39140b57cec5SDimitry Andric       return Error(Loc, "unknown sub-directive in '.cv_loc' directive");
39150b57cec5SDimitry Andric     }
39160b57cec5SDimitry Andric     return false;
39170b57cec5SDimitry Andric   };
39180b57cec5SDimitry Andric 
39190b57cec5SDimitry Andric   if (parseMany(parseOp, false /*hasComma*/))
39200b57cec5SDimitry Andric     return true;
39210b57cec5SDimitry Andric 
39225ffd83dbSDimitry Andric   getStreamer().emitCVLocDirective(FunctionId, FileNumber, LineNumber,
39230b57cec5SDimitry Andric                                    ColumnPos, PrologueEnd, IsStmt, StringRef(),
39240b57cec5SDimitry Andric                                    DirectiveLoc);
39250b57cec5SDimitry Andric   return false;
39260b57cec5SDimitry Andric }
39270b57cec5SDimitry Andric 
39280b57cec5SDimitry Andric /// parseDirectiveCVLinetable
39290b57cec5SDimitry Andric /// ::= .cv_linetable FunctionId, FnStart, FnEnd
39300b57cec5SDimitry Andric bool AsmParser::parseDirectiveCVLinetable() {
39310b57cec5SDimitry Andric   int64_t FunctionId;
39320b57cec5SDimitry Andric   StringRef FnStartName, FnEndName;
39330b57cec5SDimitry Andric   SMLoc Loc = getTok().getLoc();
3934fe6060f1SDimitry Andric   if (parseCVFunctionId(FunctionId, ".cv_linetable") || parseComma() ||
3935fe6060f1SDimitry Andric       parseTokenLoc(Loc) ||
3936fe6060f1SDimitry Andric       check(parseIdentifier(FnStartName), Loc,
39370b57cec5SDimitry Andric             "expected identifier in directive") ||
3938fe6060f1SDimitry Andric       parseComma() || parseTokenLoc(Loc) ||
3939fe6060f1SDimitry Andric       check(parseIdentifier(FnEndName), Loc,
39400b57cec5SDimitry Andric             "expected identifier in directive"))
39410b57cec5SDimitry Andric     return true;
39420b57cec5SDimitry Andric 
39430b57cec5SDimitry Andric   MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
39440b57cec5SDimitry Andric   MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
39450b57cec5SDimitry Andric 
39465ffd83dbSDimitry Andric   getStreamer().emitCVLinetableDirective(FunctionId, FnStartSym, FnEndSym);
39470b57cec5SDimitry Andric   return false;
39480b57cec5SDimitry Andric }
39490b57cec5SDimitry Andric 
39500b57cec5SDimitry Andric /// parseDirectiveCVInlineLinetable
39510b57cec5SDimitry Andric /// ::= .cv_inline_linetable PrimaryFunctionId FileId LineNum FnStart FnEnd
39520b57cec5SDimitry Andric bool AsmParser::parseDirectiveCVInlineLinetable() {
39530b57cec5SDimitry Andric   int64_t PrimaryFunctionId, SourceFileId, SourceLineNum;
39540b57cec5SDimitry Andric   StringRef FnStartName, FnEndName;
39550b57cec5SDimitry Andric   SMLoc Loc = getTok().getLoc();
39560b57cec5SDimitry Andric   if (parseCVFunctionId(PrimaryFunctionId, ".cv_inline_linetable") ||
39570b57cec5SDimitry Andric       parseTokenLoc(Loc) ||
39580b57cec5SDimitry Andric       parseIntToken(
39590b57cec5SDimitry Andric           SourceFileId,
39600b57cec5SDimitry Andric           "expected SourceField in '.cv_inline_linetable' directive") ||
39610b57cec5SDimitry Andric       check(SourceFileId <= 0, Loc,
39620b57cec5SDimitry Andric             "File id less than zero in '.cv_inline_linetable' directive") ||
39630b57cec5SDimitry Andric       parseTokenLoc(Loc) ||
39640b57cec5SDimitry Andric       parseIntToken(
39650b57cec5SDimitry Andric           SourceLineNum,
39660b57cec5SDimitry Andric           "expected SourceLineNum in '.cv_inline_linetable' directive") ||
39670b57cec5SDimitry Andric       check(SourceLineNum < 0, Loc,
39680b57cec5SDimitry Andric             "Line number less than zero in '.cv_inline_linetable' directive") ||
39690b57cec5SDimitry Andric       parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc,
39700b57cec5SDimitry Andric                                   "expected identifier in directive") ||
39710b57cec5SDimitry Andric       parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc,
39720b57cec5SDimitry Andric                                   "expected identifier in directive"))
39730b57cec5SDimitry Andric     return true;
39740b57cec5SDimitry Andric 
397581ad6265SDimitry Andric   if (parseEOL())
39760b57cec5SDimitry Andric     return true;
39770b57cec5SDimitry Andric 
39780b57cec5SDimitry Andric   MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
39790b57cec5SDimitry Andric   MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
39805ffd83dbSDimitry Andric   getStreamer().emitCVInlineLinetableDirective(PrimaryFunctionId, SourceFileId,
39810b57cec5SDimitry Andric                                                SourceLineNum, FnStartSym,
39820b57cec5SDimitry Andric                                                FnEndSym);
39830b57cec5SDimitry Andric   return false;
39840b57cec5SDimitry Andric }
39850b57cec5SDimitry Andric 
39868bcb0991SDimitry Andric void AsmParser::initializeCVDefRangeTypeMap() {
39878bcb0991SDimitry Andric   CVDefRangeTypeMap["reg"] = CVDR_DEFRANGE_REGISTER;
39888bcb0991SDimitry Andric   CVDefRangeTypeMap["frame_ptr_rel"] = CVDR_DEFRANGE_FRAMEPOINTER_REL;
39898bcb0991SDimitry Andric   CVDefRangeTypeMap["subfield_reg"] = CVDR_DEFRANGE_SUBFIELD_REGISTER;
39908bcb0991SDimitry Andric   CVDefRangeTypeMap["reg_rel"] = CVDR_DEFRANGE_REGISTER_REL;
39918bcb0991SDimitry Andric }
39928bcb0991SDimitry Andric 
39930b57cec5SDimitry Andric /// parseDirectiveCVDefRange
39940b57cec5SDimitry Andric /// ::= .cv_def_range RangeStart RangeEnd (GapStart GapEnd)*, bytes*
39950b57cec5SDimitry Andric bool AsmParser::parseDirectiveCVDefRange() {
39960b57cec5SDimitry Andric   SMLoc Loc;
39970b57cec5SDimitry Andric   std::vector<std::pair<const MCSymbol *, const MCSymbol *>> Ranges;
39980b57cec5SDimitry Andric   while (getLexer().is(AsmToken::Identifier)) {
39990b57cec5SDimitry Andric     Loc = getLexer().getLoc();
40000b57cec5SDimitry Andric     StringRef GapStartName;
40010b57cec5SDimitry Andric     if (parseIdentifier(GapStartName))
40020b57cec5SDimitry Andric       return Error(Loc, "expected identifier in directive");
40030b57cec5SDimitry Andric     MCSymbol *GapStartSym = getContext().getOrCreateSymbol(GapStartName);
40040b57cec5SDimitry Andric 
40050b57cec5SDimitry Andric     Loc = getLexer().getLoc();
40060b57cec5SDimitry Andric     StringRef GapEndName;
40070b57cec5SDimitry Andric     if (parseIdentifier(GapEndName))
40080b57cec5SDimitry Andric       return Error(Loc, "expected identifier in directive");
40090b57cec5SDimitry Andric     MCSymbol *GapEndSym = getContext().getOrCreateSymbol(GapEndName);
40100b57cec5SDimitry Andric 
40110b57cec5SDimitry Andric     Ranges.push_back({GapStartSym, GapEndSym});
40120b57cec5SDimitry Andric   }
40130b57cec5SDimitry Andric 
40148bcb0991SDimitry Andric   StringRef CVDefRangeTypeStr;
40158bcb0991SDimitry Andric   if (parseToken(
40168bcb0991SDimitry Andric           AsmToken::Comma,
40178bcb0991SDimitry Andric           "expected comma before def_range type in .cv_def_range directive") ||
40188bcb0991SDimitry Andric       parseIdentifier(CVDefRangeTypeStr))
40198bcb0991SDimitry Andric     return Error(Loc, "expected def_range type in directive");
40200b57cec5SDimitry Andric 
40218bcb0991SDimitry Andric   StringMap<CVDefRangeType>::const_iterator CVTypeIt =
40228bcb0991SDimitry Andric       CVDefRangeTypeMap.find(CVDefRangeTypeStr);
40238bcb0991SDimitry Andric   CVDefRangeType CVDRType = (CVTypeIt == CVDefRangeTypeMap.end())
40248bcb0991SDimitry Andric                                 ? CVDR_DEFRANGE
40258bcb0991SDimitry Andric                                 : CVTypeIt->getValue();
40268bcb0991SDimitry Andric   switch (CVDRType) {
40278bcb0991SDimitry Andric   case CVDR_DEFRANGE_REGISTER: {
40288bcb0991SDimitry Andric     int64_t DRRegister;
40298bcb0991SDimitry Andric     if (parseToken(AsmToken::Comma, "expected comma before register number in "
40308bcb0991SDimitry Andric                                     ".cv_def_range directive") ||
40318bcb0991SDimitry Andric         parseAbsoluteExpression(DRRegister))
40328bcb0991SDimitry Andric       return Error(Loc, "expected register number");
40338bcb0991SDimitry Andric 
40348bcb0991SDimitry Andric     codeview::DefRangeRegisterHeader DRHdr;
40358bcb0991SDimitry Andric     DRHdr.Register = DRRegister;
40368bcb0991SDimitry Andric     DRHdr.MayHaveNoName = 0;
40375ffd83dbSDimitry Andric     getStreamer().emitCVDefRangeDirective(Ranges, DRHdr);
40388bcb0991SDimitry Andric     break;
40398bcb0991SDimitry Andric   }
40408bcb0991SDimitry Andric   case CVDR_DEFRANGE_FRAMEPOINTER_REL: {
40418bcb0991SDimitry Andric     int64_t DROffset;
40428bcb0991SDimitry Andric     if (parseToken(AsmToken::Comma,
40438bcb0991SDimitry Andric                    "expected comma before offset in .cv_def_range directive") ||
40448bcb0991SDimitry Andric         parseAbsoluteExpression(DROffset))
40458bcb0991SDimitry Andric       return Error(Loc, "expected offset value");
40468bcb0991SDimitry Andric 
40478bcb0991SDimitry Andric     codeview::DefRangeFramePointerRelHeader DRHdr;
40488bcb0991SDimitry Andric     DRHdr.Offset = DROffset;
40495ffd83dbSDimitry Andric     getStreamer().emitCVDefRangeDirective(Ranges, DRHdr);
40508bcb0991SDimitry Andric     break;
40518bcb0991SDimitry Andric   }
40528bcb0991SDimitry Andric   case CVDR_DEFRANGE_SUBFIELD_REGISTER: {
40538bcb0991SDimitry Andric     int64_t DRRegister;
40548bcb0991SDimitry Andric     int64_t DROffsetInParent;
40558bcb0991SDimitry Andric     if (parseToken(AsmToken::Comma, "expected comma before register number in "
40568bcb0991SDimitry Andric                                     ".cv_def_range directive") ||
40578bcb0991SDimitry Andric         parseAbsoluteExpression(DRRegister))
40588bcb0991SDimitry Andric       return Error(Loc, "expected register number");
40598bcb0991SDimitry Andric     if (parseToken(AsmToken::Comma,
40608bcb0991SDimitry Andric                    "expected comma before offset in .cv_def_range directive") ||
40618bcb0991SDimitry Andric         parseAbsoluteExpression(DROffsetInParent))
40628bcb0991SDimitry Andric       return Error(Loc, "expected offset value");
40638bcb0991SDimitry Andric 
40648bcb0991SDimitry Andric     codeview::DefRangeSubfieldRegisterHeader DRHdr;
40658bcb0991SDimitry Andric     DRHdr.Register = DRRegister;
40668bcb0991SDimitry Andric     DRHdr.MayHaveNoName = 0;
40678bcb0991SDimitry Andric     DRHdr.OffsetInParent = DROffsetInParent;
40685ffd83dbSDimitry Andric     getStreamer().emitCVDefRangeDirective(Ranges, DRHdr);
40698bcb0991SDimitry Andric     break;
40708bcb0991SDimitry Andric   }
40718bcb0991SDimitry Andric   case CVDR_DEFRANGE_REGISTER_REL: {
40728bcb0991SDimitry Andric     int64_t DRRegister;
40738bcb0991SDimitry Andric     int64_t DRFlags;
40748bcb0991SDimitry Andric     int64_t DRBasePointerOffset;
40758bcb0991SDimitry Andric     if (parseToken(AsmToken::Comma, "expected comma before register number in "
40768bcb0991SDimitry Andric                                     ".cv_def_range directive") ||
40778bcb0991SDimitry Andric         parseAbsoluteExpression(DRRegister))
40788bcb0991SDimitry Andric       return Error(Loc, "expected register value");
40798bcb0991SDimitry Andric     if (parseToken(
40808bcb0991SDimitry Andric             AsmToken::Comma,
40818bcb0991SDimitry Andric             "expected comma before flag value in .cv_def_range directive") ||
40828bcb0991SDimitry Andric         parseAbsoluteExpression(DRFlags))
40838bcb0991SDimitry Andric       return Error(Loc, "expected flag value");
40848bcb0991SDimitry Andric     if (parseToken(AsmToken::Comma, "expected comma before base pointer offset "
40858bcb0991SDimitry Andric                                     "in .cv_def_range directive") ||
40868bcb0991SDimitry Andric         parseAbsoluteExpression(DRBasePointerOffset))
40878bcb0991SDimitry Andric       return Error(Loc, "expected base pointer offset value");
40888bcb0991SDimitry Andric 
40898bcb0991SDimitry Andric     codeview::DefRangeRegisterRelHeader DRHdr;
40908bcb0991SDimitry Andric     DRHdr.Register = DRRegister;
40918bcb0991SDimitry Andric     DRHdr.Flags = DRFlags;
40928bcb0991SDimitry Andric     DRHdr.BasePointerOffset = DRBasePointerOffset;
40935ffd83dbSDimitry Andric     getStreamer().emitCVDefRangeDirective(Ranges, DRHdr);
40948bcb0991SDimitry Andric     break;
40958bcb0991SDimitry Andric   }
40968bcb0991SDimitry Andric   default:
40978bcb0991SDimitry Andric     return Error(Loc, "unexpected def_range type in .cv_def_range directive");
40988bcb0991SDimitry Andric   }
40998bcb0991SDimitry Andric   return true;
41000b57cec5SDimitry Andric }
41010b57cec5SDimitry Andric 
41020b57cec5SDimitry Andric /// parseDirectiveCVString
41030b57cec5SDimitry Andric /// ::= .cv_stringtable "string"
41040b57cec5SDimitry Andric bool AsmParser::parseDirectiveCVString() {
41050b57cec5SDimitry Andric   std::string Data;
41060b57cec5SDimitry Andric   if (checkForValidSection() || parseEscapedString(Data))
4107fe6060f1SDimitry Andric     return true;
41080b57cec5SDimitry Andric 
41090b57cec5SDimitry Andric   // Put the string in the table and emit the offset.
41100b57cec5SDimitry Andric   std::pair<StringRef, unsigned> Insertion =
41110b57cec5SDimitry Andric       getCVContext().addToStringTable(Data);
41125ffd83dbSDimitry Andric   getStreamer().emitInt32(Insertion.second);
41130b57cec5SDimitry Andric   return false;
41140b57cec5SDimitry Andric }
41150b57cec5SDimitry Andric 
41160b57cec5SDimitry Andric /// parseDirectiveCVStringTable
41170b57cec5SDimitry Andric /// ::= .cv_stringtable
41180b57cec5SDimitry Andric bool AsmParser::parseDirectiveCVStringTable() {
41195ffd83dbSDimitry Andric   getStreamer().emitCVStringTableDirective();
41200b57cec5SDimitry Andric   return false;
41210b57cec5SDimitry Andric }
41220b57cec5SDimitry Andric 
41230b57cec5SDimitry Andric /// parseDirectiveCVFileChecksums
41240b57cec5SDimitry Andric /// ::= .cv_filechecksums
41250b57cec5SDimitry Andric bool AsmParser::parseDirectiveCVFileChecksums() {
41265ffd83dbSDimitry Andric   getStreamer().emitCVFileChecksumsDirective();
41270b57cec5SDimitry Andric   return false;
41280b57cec5SDimitry Andric }
41290b57cec5SDimitry Andric 
41300b57cec5SDimitry Andric /// parseDirectiveCVFileChecksumOffset
41310b57cec5SDimitry Andric /// ::= .cv_filechecksumoffset fileno
41320b57cec5SDimitry Andric bool AsmParser::parseDirectiveCVFileChecksumOffset() {
41330b57cec5SDimitry Andric   int64_t FileNo;
41340b57cec5SDimitry Andric   if (parseIntToken(FileNo, "expected identifier in directive"))
41350b57cec5SDimitry Andric     return true;
413681ad6265SDimitry Andric   if (parseEOL())
41370b57cec5SDimitry Andric     return true;
41385ffd83dbSDimitry Andric   getStreamer().emitCVFileChecksumOffsetDirective(FileNo);
41390b57cec5SDimitry Andric   return false;
41400b57cec5SDimitry Andric }
41410b57cec5SDimitry Andric 
41420b57cec5SDimitry Andric /// parseDirectiveCVFPOData
41430b57cec5SDimitry Andric /// ::= .cv_fpo_data procsym
41440b57cec5SDimitry Andric bool AsmParser::parseDirectiveCVFPOData() {
41450b57cec5SDimitry Andric   SMLoc DirLoc = getLexer().getLoc();
41460b57cec5SDimitry Andric   StringRef ProcName;
41470b57cec5SDimitry Andric   if (parseIdentifier(ProcName))
41480b57cec5SDimitry Andric     return TokError("expected symbol name");
4149fe6060f1SDimitry Andric   if (parseEOL())
4150fe6060f1SDimitry Andric     return true;
41510b57cec5SDimitry Andric   MCSymbol *ProcSym = getContext().getOrCreateSymbol(ProcName);
415281ad6265SDimitry Andric   getStreamer().emitCVFPOData(ProcSym, DirLoc);
41530b57cec5SDimitry Andric   return false;
41540b57cec5SDimitry Andric }
41550b57cec5SDimitry Andric 
41560b57cec5SDimitry Andric /// parseDirectiveCFISections
41570b57cec5SDimitry Andric /// ::= .cfi_sections section [, section]
41580b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFISections() {
41590b57cec5SDimitry Andric   StringRef Name;
41600b57cec5SDimitry Andric   bool EH = false;
41610b57cec5SDimitry Andric   bool Debug = false;
41620b57cec5SDimitry Andric 
4163fe6060f1SDimitry Andric   if (!parseOptionalToken(AsmToken::EndOfStatement)) {
4164fe6060f1SDimitry Andric     for (;;) {
41650b57cec5SDimitry Andric       if (parseIdentifier(Name))
4166fe6060f1SDimitry Andric         return TokError("expected .eh_frame or .debug_frame");
41670b57cec5SDimitry Andric       if (Name == ".eh_frame")
41680b57cec5SDimitry Andric         EH = true;
41690b57cec5SDimitry Andric       else if (Name == ".debug_frame")
41700b57cec5SDimitry Andric         Debug = true;
4171fe6060f1SDimitry Andric       if (parseOptionalToken(AsmToken::EndOfStatement))
4172fe6060f1SDimitry Andric         break;
4173fe6060f1SDimitry Andric       if (parseComma())
4174fe6060f1SDimitry Andric         return true;
41750b57cec5SDimitry Andric     }
4176fe6060f1SDimitry Andric   }
41775ffd83dbSDimitry Andric   getStreamer().emitCFISections(EH, Debug);
41780b57cec5SDimitry Andric   return false;
41790b57cec5SDimitry Andric }
41800b57cec5SDimitry Andric 
41810b57cec5SDimitry Andric /// parseDirectiveCFIStartProc
41820b57cec5SDimitry Andric /// ::= .cfi_startproc [simple]
41830b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIStartProc() {
41840b57cec5SDimitry Andric   StringRef Simple;
41850b57cec5SDimitry Andric   if (!parseOptionalToken(AsmToken::EndOfStatement)) {
41860b57cec5SDimitry Andric     if (check(parseIdentifier(Simple) || Simple != "simple",
41870b57cec5SDimitry Andric               "unexpected token") ||
4188fe6060f1SDimitry Andric         parseEOL())
4189fe6060f1SDimitry Andric       return true;
41900b57cec5SDimitry Andric   }
41910b57cec5SDimitry Andric 
41920b57cec5SDimitry Andric   // TODO(kristina): Deal with a corner case of incorrect diagnostic context
41930b57cec5SDimitry Andric   // being produced if this directive is emitted as part of preprocessor macro
41940b57cec5SDimitry Andric   // expansion which can *ONLY* happen if Clang's cc1as is the API consumer.
41950b57cec5SDimitry Andric   // Tools like llvm-mc on the other hand are not affected by it, and report
41960b57cec5SDimitry Andric   // correct context information.
41975ffd83dbSDimitry Andric   getStreamer().emitCFIStartProc(!Simple.empty(), Lexer.getLoc());
41980b57cec5SDimitry Andric   return false;
41990b57cec5SDimitry Andric }
42000b57cec5SDimitry Andric 
42010b57cec5SDimitry Andric /// parseDirectiveCFIEndProc
42020b57cec5SDimitry Andric /// ::= .cfi_endproc
42030b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIEndProc() {
4204fe6060f1SDimitry Andric   if (parseEOL())
4205fe6060f1SDimitry Andric     return true;
42065ffd83dbSDimitry Andric   getStreamer().emitCFIEndProc();
42070b57cec5SDimitry Andric   return false;
42080b57cec5SDimitry Andric }
42090b57cec5SDimitry Andric 
42100b57cec5SDimitry Andric /// parse register name or number.
42110b57cec5SDimitry Andric bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
42120b57cec5SDimitry Andric                                               SMLoc DirectiveLoc) {
4213bdd1243dSDimitry Andric   MCRegister RegNo;
42140b57cec5SDimitry Andric 
42150b57cec5SDimitry Andric   if (getLexer().isNot(AsmToken::Integer)) {
4216bdd1243dSDimitry Andric     if (getTargetParser().parseRegister(RegNo, DirectiveLoc, DirectiveLoc))
42170b57cec5SDimitry Andric       return true;
42180b57cec5SDimitry Andric     Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
42190b57cec5SDimitry Andric   } else
42200b57cec5SDimitry Andric     return parseAbsoluteExpression(Register);
42210b57cec5SDimitry Andric 
42220b57cec5SDimitry Andric   return false;
42230b57cec5SDimitry Andric }
42240b57cec5SDimitry Andric 
42250b57cec5SDimitry Andric /// parseDirectiveCFIDefCfa
42260b57cec5SDimitry Andric /// ::= .cfi_def_cfa register,  offset
42270b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
42280b57cec5SDimitry Andric   int64_t Register = 0, Offset = 0;
4229fe6060f1SDimitry Andric   if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseComma() ||
4230fe6060f1SDimitry Andric       parseAbsoluteExpression(Offset) || parseEOL())
42310b57cec5SDimitry Andric     return true;
42320b57cec5SDimitry Andric 
4233*06c3fb27SDimitry Andric   getStreamer().emitCFIDefCfa(Register, Offset, DirectiveLoc);
42340b57cec5SDimitry Andric   return false;
42350b57cec5SDimitry Andric }
42360b57cec5SDimitry Andric 
42370b57cec5SDimitry Andric /// parseDirectiveCFIDefCfaOffset
42380b57cec5SDimitry Andric /// ::= .cfi_def_cfa_offset offset
4239*06c3fb27SDimitry Andric bool AsmParser::parseDirectiveCFIDefCfaOffset(SMLoc DirectiveLoc) {
42400b57cec5SDimitry Andric   int64_t Offset = 0;
4241fe6060f1SDimitry Andric   if (parseAbsoluteExpression(Offset) || parseEOL())
42420b57cec5SDimitry Andric     return true;
42430b57cec5SDimitry Andric 
4244*06c3fb27SDimitry Andric   getStreamer().emitCFIDefCfaOffset(Offset, DirectiveLoc);
42450b57cec5SDimitry Andric   return false;
42460b57cec5SDimitry Andric }
42470b57cec5SDimitry Andric 
42480b57cec5SDimitry Andric /// parseDirectiveCFIRegister
42490b57cec5SDimitry Andric /// ::= .cfi_register register, register
42500b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
42510b57cec5SDimitry Andric   int64_t Register1 = 0, Register2 = 0;
4252fe6060f1SDimitry Andric   if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc) || parseComma() ||
4253fe6060f1SDimitry Andric       parseRegisterOrRegisterNumber(Register2, DirectiveLoc) || parseEOL())
42540b57cec5SDimitry Andric     return true;
42550b57cec5SDimitry Andric 
4256*06c3fb27SDimitry Andric   getStreamer().emitCFIRegister(Register1, Register2, DirectiveLoc);
42570b57cec5SDimitry Andric   return false;
42580b57cec5SDimitry Andric }
42590b57cec5SDimitry Andric 
42600b57cec5SDimitry Andric /// parseDirectiveCFIWindowSave
42610b57cec5SDimitry Andric /// ::= .cfi_window_save
4262*06c3fb27SDimitry Andric bool AsmParser::parseDirectiveCFIWindowSave(SMLoc DirectiveLoc) {
4263fe6060f1SDimitry Andric   if (parseEOL())
4264fe6060f1SDimitry Andric     return true;
4265*06c3fb27SDimitry Andric   getStreamer().emitCFIWindowSave(DirectiveLoc);
42660b57cec5SDimitry Andric   return false;
42670b57cec5SDimitry Andric }
42680b57cec5SDimitry Andric 
42690b57cec5SDimitry Andric /// parseDirectiveCFIAdjustCfaOffset
42700b57cec5SDimitry Andric /// ::= .cfi_adjust_cfa_offset adjustment
4271*06c3fb27SDimitry Andric bool AsmParser::parseDirectiveCFIAdjustCfaOffset(SMLoc DirectiveLoc) {
42720b57cec5SDimitry Andric   int64_t Adjustment = 0;
4273fe6060f1SDimitry Andric   if (parseAbsoluteExpression(Adjustment) || parseEOL())
42740b57cec5SDimitry Andric     return true;
42750b57cec5SDimitry Andric 
4276*06c3fb27SDimitry Andric   getStreamer().emitCFIAdjustCfaOffset(Adjustment, DirectiveLoc);
42770b57cec5SDimitry Andric   return false;
42780b57cec5SDimitry Andric }
42790b57cec5SDimitry Andric 
42800b57cec5SDimitry Andric /// parseDirectiveCFIDefCfaRegister
42810b57cec5SDimitry Andric /// ::= .cfi_def_cfa_register register
42820b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
42830b57cec5SDimitry Andric   int64_t Register = 0;
4284fe6060f1SDimitry Andric   if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseEOL())
42850b57cec5SDimitry Andric     return true;
42860b57cec5SDimitry Andric 
4287*06c3fb27SDimitry Andric   getStreamer().emitCFIDefCfaRegister(Register, DirectiveLoc);
42880b57cec5SDimitry Andric   return false;
42890b57cec5SDimitry Andric }
42900b57cec5SDimitry Andric 
4291fe6060f1SDimitry Andric /// parseDirectiveCFILLVMDefAspaceCfa
4292fe6060f1SDimitry Andric /// ::= .cfi_llvm_def_aspace_cfa register, offset, address_space
4293fe6060f1SDimitry Andric bool AsmParser::parseDirectiveCFILLVMDefAspaceCfa(SMLoc DirectiveLoc) {
4294fe6060f1SDimitry Andric   int64_t Register = 0, Offset = 0, AddressSpace = 0;
4295fe6060f1SDimitry Andric   if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseComma() ||
4296fe6060f1SDimitry Andric       parseAbsoluteExpression(Offset) || parseComma() ||
4297fe6060f1SDimitry Andric       parseAbsoluteExpression(AddressSpace) || parseEOL())
4298fe6060f1SDimitry Andric     return true;
4299fe6060f1SDimitry Andric 
4300*06c3fb27SDimitry Andric   getStreamer().emitCFILLVMDefAspaceCfa(Register, Offset, AddressSpace,
4301*06c3fb27SDimitry Andric                                         DirectiveLoc);
4302fe6060f1SDimitry Andric   return false;
4303fe6060f1SDimitry Andric }
4304fe6060f1SDimitry Andric 
43050b57cec5SDimitry Andric /// parseDirectiveCFIOffset
43060b57cec5SDimitry Andric /// ::= .cfi_offset register, offset
43070b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
43080b57cec5SDimitry Andric   int64_t Register = 0;
43090b57cec5SDimitry Andric   int64_t Offset = 0;
43100b57cec5SDimitry Andric 
4311fe6060f1SDimitry Andric   if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseComma() ||
4312fe6060f1SDimitry Andric       parseAbsoluteExpression(Offset) || parseEOL())
43130b57cec5SDimitry Andric     return true;
43140b57cec5SDimitry Andric 
4315*06c3fb27SDimitry Andric   getStreamer().emitCFIOffset(Register, Offset, DirectiveLoc);
43160b57cec5SDimitry Andric   return false;
43170b57cec5SDimitry Andric }
43180b57cec5SDimitry Andric 
43190b57cec5SDimitry Andric /// parseDirectiveCFIRelOffset
43200b57cec5SDimitry Andric /// ::= .cfi_rel_offset register, offset
43210b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
43220b57cec5SDimitry Andric   int64_t Register = 0, Offset = 0;
43230b57cec5SDimitry Andric 
4324fe6060f1SDimitry Andric   if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseComma() ||
4325fe6060f1SDimitry Andric       parseAbsoluteExpression(Offset) || parseEOL())
43260b57cec5SDimitry Andric     return true;
43270b57cec5SDimitry Andric 
4328*06c3fb27SDimitry Andric   getStreamer().emitCFIRelOffset(Register, Offset, DirectiveLoc);
43290b57cec5SDimitry Andric   return false;
43300b57cec5SDimitry Andric }
43310b57cec5SDimitry Andric 
43320b57cec5SDimitry Andric static bool isValidEncoding(int64_t Encoding) {
43330b57cec5SDimitry Andric   if (Encoding & ~0xff)
43340b57cec5SDimitry Andric     return false;
43350b57cec5SDimitry Andric 
43360b57cec5SDimitry Andric   if (Encoding == dwarf::DW_EH_PE_omit)
43370b57cec5SDimitry Andric     return true;
43380b57cec5SDimitry Andric 
43390b57cec5SDimitry Andric   const unsigned Format = Encoding & 0xf;
43400b57cec5SDimitry Andric   if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
43410b57cec5SDimitry Andric       Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
43420b57cec5SDimitry Andric       Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
43430b57cec5SDimitry Andric       Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
43440b57cec5SDimitry Andric     return false;
43450b57cec5SDimitry Andric 
43460b57cec5SDimitry Andric   const unsigned Application = Encoding & 0x70;
43470b57cec5SDimitry Andric   if (Application != dwarf::DW_EH_PE_absptr &&
43480b57cec5SDimitry Andric       Application != dwarf::DW_EH_PE_pcrel)
43490b57cec5SDimitry Andric     return false;
43500b57cec5SDimitry Andric 
43510b57cec5SDimitry Andric   return true;
43520b57cec5SDimitry Andric }
43530b57cec5SDimitry Andric 
43540b57cec5SDimitry Andric /// parseDirectiveCFIPersonalityOrLsda
43550b57cec5SDimitry Andric /// IsPersonality true for cfi_personality, false for cfi_lsda
43560b57cec5SDimitry Andric /// ::= .cfi_personality encoding, [symbol_name]
43570b57cec5SDimitry Andric /// ::= .cfi_lsda encoding, [symbol_name]
43580b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
43590b57cec5SDimitry Andric   int64_t Encoding = 0;
43600b57cec5SDimitry Andric   if (parseAbsoluteExpression(Encoding))
43610b57cec5SDimitry Andric     return true;
43620b57cec5SDimitry Andric   if (Encoding == dwarf::DW_EH_PE_omit)
43630b57cec5SDimitry Andric     return false;
43640b57cec5SDimitry Andric 
43650b57cec5SDimitry Andric   StringRef Name;
43660b57cec5SDimitry Andric   if (check(!isValidEncoding(Encoding), "unsupported encoding.") ||
4367fe6060f1SDimitry Andric       parseComma() ||
4368fe6060f1SDimitry Andric       check(parseIdentifier(Name), "expected identifier in directive") ||
4369fe6060f1SDimitry Andric       parseEOL())
43700b57cec5SDimitry Andric     return true;
43710b57cec5SDimitry Andric 
43720b57cec5SDimitry Andric   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
43730b57cec5SDimitry Andric 
43740b57cec5SDimitry Andric   if (IsPersonality)
43755ffd83dbSDimitry Andric     getStreamer().emitCFIPersonality(Sym, Encoding);
43760b57cec5SDimitry Andric   else
43775ffd83dbSDimitry Andric     getStreamer().emitCFILsda(Sym, Encoding);
43780b57cec5SDimitry Andric   return false;
43790b57cec5SDimitry Andric }
43800b57cec5SDimitry Andric 
43810b57cec5SDimitry Andric /// parseDirectiveCFIRememberState
43820b57cec5SDimitry Andric /// ::= .cfi_remember_state
4383*06c3fb27SDimitry Andric bool AsmParser::parseDirectiveCFIRememberState(SMLoc DirectiveLoc) {
4384fe6060f1SDimitry Andric   if (parseEOL())
4385fe6060f1SDimitry Andric     return true;
4386*06c3fb27SDimitry Andric   getStreamer().emitCFIRememberState(DirectiveLoc);
43870b57cec5SDimitry Andric   return false;
43880b57cec5SDimitry Andric }
43890b57cec5SDimitry Andric 
43900b57cec5SDimitry Andric /// parseDirectiveCFIRestoreState
43910b57cec5SDimitry Andric /// ::= .cfi_remember_state
4392*06c3fb27SDimitry Andric bool AsmParser::parseDirectiveCFIRestoreState(SMLoc DirectiveLoc) {
4393fe6060f1SDimitry Andric   if (parseEOL())
4394fe6060f1SDimitry Andric     return true;
4395*06c3fb27SDimitry Andric   getStreamer().emitCFIRestoreState(DirectiveLoc);
43960b57cec5SDimitry Andric   return false;
43970b57cec5SDimitry Andric }
43980b57cec5SDimitry Andric 
43990b57cec5SDimitry Andric /// parseDirectiveCFISameValue
44000b57cec5SDimitry Andric /// ::= .cfi_same_value register
44010b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
44020b57cec5SDimitry Andric   int64_t Register = 0;
44030b57cec5SDimitry Andric 
4404fe6060f1SDimitry Andric   if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseEOL())
44050b57cec5SDimitry Andric     return true;
44060b57cec5SDimitry Andric 
4407*06c3fb27SDimitry Andric   getStreamer().emitCFISameValue(Register, DirectiveLoc);
44080b57cec5SDimitry Andric   return false;
44090b57cec5SDimitry Andric }
44100b57cec5SDimitry Andric 
44110b57cec5SDimitry Andric /// parseDirectiveCFIRestore
44120b57cec5SDimitry Andric /// ::= .cfi_restore register
44130b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
44140b57cec5SDimitry Andric   int64_t Register = 0;
4415fe6060f1SDimitry Andric   if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseEOL())
44160b57cec5SDimitry Andric     return true;
44170b57cec5SDimitry Andric 
4418*06c3fb27SDimitry Andric   getStreamer().emitCFIRestore(Register, DirectiveLoc);
44190b57cec5SDimitry Andric   return false;
44200b57cec5SDimitry Andric }
44210b57cec5SDimitry Andric 
44220b57cec5SDimitry Andric /// parseDirectiveCFIEscape
44230b57cec5SDimitry Andric /// ::= .cfi_escape expression[,...]
4424*06c3fb27SDimitry Andric bool AsmParser::parseDirectiveCFIEscape(SMLoc DirectiveLoc) {
44250b57cec5SDimitry Andric   std::string Values;
44260b57cec5SDimitry Andric   int64_t CurrValue;
44270b57cec5SDimitry Andric   if (parseAbsoluteExpression(CurrValue))
44280b57cec5SDimitry Andric     return true;
44290b57cec5SDimitry Andric 
44300b57cec5SDimitry Andric   Values.push_back((uint8_t)CurrValue);
44310b57cec5SDimitry Andric 
44320b57cec5SDimitry Andric   while (getLexer().is(AsmToken::Comma)) {
44330b57cec5SDimitry Andric     Lex();
44340b57cec5SDimitry Andric 
44350b57cec5SDimitry Andric     if (parseAbsoluteExpression(CurrValue))
44360b57cec5SDimitry Andric       return true;
44370b57cec5SDimitry Andric 
44380b57cec5SDimitry Andric     Values.push_back((uint8_t)CurrValue);
44390b57cec5SDimitry Andric   }
44400b57cec5SDimitry Andric 
4441*06c3fb27SDimitry Andric   getStreamer().emitCFIEscape(Values, DirectiveLoc);
44420b57cec5SDimitry Andric   return false;
44430b57cec5SDimitry Andric }
44440b57cec5SDimitry Andric 
44450b57cec5SDimitry Andric /// parseDirectiveCFIReturnColumn
44460b57cec5SDimitry Andric /// ::= .cfi_return_column register
44470b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc) {
44480b57cec5SDimitry Andric   int64_t Register = 0;
4449fe6060f1SDimitry Andric   if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseEOL())
44500b57cec5SDimitry Andric     return true;
44515ffd83dbSDimitry Andric   getStreamer().emitCFIReturnColumn(Register);
44520b57cec5SDimitry Andric   return false;
44530b57cec5SDimitry Andric }
44540b57cec5SDimitry Andric 
44550b57cec5SDimitry Andric /// parseDirectiveCFISignalFrame
44560b57cec5SDimitry Andric /// ::= .cfi_signal_frame
4457*06c3fb27SDimitry Andric bool AsmParser::parseDirectiveCFISignalFrame(SMLoc DirectiveLoc) {
4458fe6060f1SDimitry Andric   if (parseEOL())
44590b57cec5SDimitry Andric     return true;
44600b57cec5SDimitry Andric 
44615ffd83dbSDimitry Andric   getStreamer().emitCFISignalFrame();
44620b57cec5SDimitry Andric   return false;
44630b57cec5SDimitry Andric }
44640b57cec5SDimitry Andric 
44650b57cec5SDimitry Andric /// parseDirectiveCFIUndefined
44660b57cec5SDimitry Andric /// ::= .cfi_undefined register
44670b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
44680b57cec5SDimitry Andric   int64_t Register = 0;
44690b57cec5SDimitry Andric 
4470fe6060f1SDimitry Andric   if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseEOL())
44710b57cec5SDimitry Andric     return true;
44720b57cec5SDimitry Andric 
4473*06c3fb27SDimitry Andric   getStreamer().emitCFIUndefined(Register, DirectiveLoc);
44740b57cec5SDimitry Andric   return false;
44750b57cec5SDimitry Andric }
44760b57cec5SDimitry Andric 
44770b57cec5SDimitry Andric /// parseDirectiveAltmacro
44780b57cec5SDimitry Andric /// ::= .altmacro
44790b57cec5SDimitry Andric /// ::= .noaltmacro
44800b57cec5SDimitry Andric bool AsmParser::parseDirectiveAltmacro(StringRef Directive) {
4481fe6060f1SDimitry Andric   if (parseEOL())
4482fe6060f1SDimitry Andric     return true;
44830b57cec5SDimitry Andric   AltMacroMode = (Directive == ".altmacro");
44840b57cec5SDimitry Andric   return false;
44850b57cec5SDimitry Andric }
44860b57cec5SDimitry Andric 
44870b57cec5SDimitry Andric /// parseDirectiveMacrosOnOff
44880b57cec5SDimitry Andric /// ::= .macros_on
44890b57cec5SDimitry Andric /// ::= .macros_off
44900b57cec5SDimitry Andric bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
4491fe6060f1SDimitry Andric   if (parseEOL())
44920b57cec5SDimitry Andric     return true;
44930b57cec5SDimitry Andric   setMacrosEnabled(Directive == ".macros_on");
44940b57cec5SDimitry Andric   return false;
44950b57cec5SDimitry Andric }
44960b57cec5SDimitry Andric 
44970b57cec5SDimitry Andric /// parseDirectiveMacro
44980b57cec5SDimitry Andric /// ::= .macro name[,] [parameters]
44990b57cec5SDimitry Andric bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
45000b57cec5SDimitry Andric   StringRef Name;
45010b57cec5SDimitry Andric   if (parseIdentifier(Name))
45020b57cec5SDimitry Andric     return TokError("expected identifier in '.macro' directive");
45030b57cec5SDimitry Andric 
45040b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Comma))
45050b57cec5SDimitry Andric     Lex();
45060b57cec5SDimitry Andric 
45070b57cec5SDimitry Andric   MCAsmMacroParameters Parameters;
45080b57cec5SDimitry Andric   while (getLexer().isNot(AsmToken::EndOfStatement)) {
45090b57cec5SDimitry Andric 
45100b57cec5SDimitry Andric     if (!Parameters.empty() && Parameters.back().Vararg)
45115ffd83dbSDimitry Andric       return Error(Lexer.getLoc(), "vararg parameter '" +
45125ffd83dbSDimitry Andric                                        Parameters.back().Name +
45135ffd83dbSDimitry Andric                                        "' should be the last parameter");
45140b57cec5SDimitry Andric 
45150b57cec5SDimitry Andric     MCAsmMacroParameter Parameter;
45160b57cec5SDimitry Andric     if (parseIdentifier(Parameter.Name))
45170b57cec5SDimitry Andric       return TokError("expected identifier in '.macro' directive");
45180b57cec5SDimitry Andric 
45190b57cec5SDimitry Andric     // Emit an error if two (or more) named parameters share the same name
45200b57cec5SDimitry Andric     for (const MCAsmMacroParameter& CurrParam : Parameters)
45210b57cec5SDimitry Andric       if (CurrParam.Name.equals(Parameter.Name))
45220b57cec5SDimitry Andric         return TokError("macro '" + Name + "' has multiple parameters"
45230b57cec5SDimitry Andric                         " named '" + Parameter.Name + "'");
45240b57cec5SDimitry Andric 
45250b57cec5SDimitry Andric     if (Lexer.is(AsmToken::Colon)) {
45260b57cec5SDimitry Andric       Lex();  // consume ':'
45270b57cec5SDimitry Andric 
45280b57cec5SDimitry Andric       SMLoc QualLoc;
45290b57cec5SDimitry Andric       StringRef Qualifier;
45300b57cec5SDimitry Andric 
45310b57cec5SDimitry Andric       QualLoc = Lexer.getLoc();
45320b57cec5SDimitry Andric       if (parseIdentifier(Qualifier))
45330b57cec5SDimitry Andric         return Error(QualLoc, "missing parameter qualifier for "
45340b57cec5SDimitry Andric                      "'" + Parameter.Name + "' in macro '" + Name + "'");
45350b57cec5SDimitry Andric 
45360b57cec5SDimitry Andric       if (Qualifier == "req")
45370b57cec5SDimitry Andric         Parameter.Required = true;
45380b57cec5SDimitry Andric       else if (Qualifier == "vararg")
45390b57cec5SDimitry Andric         Parameter.Vararg = true;
45400b57cec5SDimitry Andric       else
45410b57cec5SDimitry Andric         return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
45420b57cec5SDimitry Andric                      "for '" + Parameter.Name + "' in macro '" + Name + "'");
45430b57cec5SDimitry Andric     }
45440b57cec5SDimitry Andric 
45450b57cec5SDimitry Andric     if (getLexer().is(AsmToken::Equal)) {
45460b57cec5SDimitry Andric       Lex();
45470b57cec5SDimitry Andric 
45480b57cec5SDimitry Andric       SMLoc ParamLoc;
45490b57cec5SDimitry Andric 
45500b57cec5SDimitry Andric       ParamLoc = Lexer.getLoc();
45510b57cec5SDimitry Andric       if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
45520b57cec5SDimitry Andric         return true;
45530b57cec5SDimitry Andric 
45540b57cec5SDimitry Andric       if (Parameter.Required)
45550b57cec5SDimitry Andric         Warning(ParamLoc, "pointless default value for required parameter "
45560b57cec5SDimitry Andric                 "'" + Parameter.Name + "' in macro '" + Name + "'");
45570b57cec5SDimitry Andric     }
45580b57cec5SDimitry Andric 
45590b57cec5SDimitry Andric     Parameters.push_back(std::move(Parameter));
45600b57cec5SDimitry Andric 
45610b57cec5SDimitry Andric     if (getLexer().is(AsmToken::Comma))
45620b57cec5SDimitry Andric       Lex();
45630b57cec5SDimitry Andric   }
45640b57cec5SDimitry Andric 
45650b57cec5SDimitry Andric   // Eat just the end of statement.
45660b57cec5SDimitry Andric   Lexer.Lex();
45670b57cec5SDimitry Andric 
45680b57cec5SDimitry Andric   // Consuming deferred text, so use Lexer.Lex to ignore Lexing Errors
45690b57cec5SDimitry Andric   AsmToken EndToken, StartToken = getTok();
45700b57cec5SDimitry Andric   unsigned MacroDepth = 0;
45710b57cec5SDimitry Andric   // Lex the macro definition.
45720b57cec5SDimitry Andric   while (true) {
45730b57cec5SDimitry Andric     // Ignore Lexing errors in macros.
45740b57cec5SDimitry Andric     while (Lexer.is(AsmToken::Error)) {
45750b57cec5SDimitry Andric       Lexer.Lex();
45760b57cec5SDimitry Andric     }
45770b57cec5SDimitry Andric 
45780b57cec5SDimitry Andric     // Check whether we have reached the end of the file.
45790b57cec5SDimitry Andric     if (getLexer().is(AsmToken::Eof))
45800b57cec5SDimitry Andric       return Error(DirectiveLoc, "no matching '.endmacro' in definition");
45810b57cec5SDimitry Andric 
45825ffd83dbSDimitry Andric     // Otherwise, check whether we have reach the .endmacro or the start of a
45835ffd83dbSDimitry Andric     // preprocessor line marker.
45840b57cec5SDimitry Andric     if (getLexer().is(AsmToken::Identifier)) {
45850b57cec5SDimitry Andric       if (getTok().getIdentifier() == ".endm" ||
45860b57cec5SDimitry Andric           getTok().getIdentifier() == ".endmacro") {
45870b57cec5SDimitry Andric         if (MacroDepth == 0) { // Outermost macro.
45880b57cec5SDimitry Andric           EndToken = getTok();
45890b57cec5SDimitry Andric           Lexer.Lex();
45900b57cec5SDimitry Andric           if (getLexer().isNot(AsmToken::EndOfStatement))
45910b57cec5SDimitry Andric             return TokError("unexpected token in '" + EndToken.getIdentifier() +
45920b57cec5SDimitry Andric                             "' directive");
45930b57cec5SDimitry Andric           break;
45940b57cec5SDimitry Andric         } else {
45950b57cec5SDimitry Andric           // Otherwise we just found the end of an inner macro.
45960b57cec5SDimitry Andric           --MacroDepth;
45970b57cec5SDimitry Andric         }
45980b57cec5SDimitry Andric       } else if (getTok().getIdentifier() == ".macro") {
45990b57cec5SDimitry Andric         // We allow nested macros. Those aren't instantiated until the outermost
46000b57cec5SDimitry Andric         // macro is expanded so just ignore them for now.
46010b57cec5SDimitry Andric         ++MacroDepth;
46020b57cec5SDimitry Andric       }
46035ffd83dbSDimitry Andric     } else if (Lexer.is(AsmToken::HashDirective)) {
46045ffd83dbSDimitry Andric       (void)parseCppHashLineFilenameComment(getLexer().getLoc());
46050b57cec5SDimitry Andric     }
46060b57cec5SDimitry Andric 
46070b57cec5SDimitry Andric     // Otherwise, scan til the end of the statement.
46080b57cec5SDimitry Andric     eatToEndOfStatement();
46090b57cec5SDimitry Andric   }
46100b57cec5SDimitry Andric 
46110b57cec5SDimitry Andric   if (getContext().lookupMacro(Name)) {
46120b57cec5SDimitry Andric     return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
46130b57cec5SDimitry Andric   }
46140b57cec5SDimitry Andric 
46150b57cec5SDimitry Andric   const char *BodyStart = StartToken.getLoc().getPointer();
46160b57cec5SDimitry Andric   const char *BodyEnd = EndToken.getLoc().getPointer();
46170b57cec5SDimitry Andric   StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
46180b57cec5SDimitry Andric   checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
46190b57cec5SDimitry Andric   MCAsmMacro Macro(Name, Body, std::move(Parameters));
46200b57cec5SDimitry Andric   DEBUG_WITH_TYPE("asm-macros", dbgs() << "Defining new macro:\n";
46210b57cec5SDimitry Andric                   Macro.dump());
46220b57cec5SDimitry Andric   getContext().defineMacro(Name, std::move(Macro));
46230b57cec5SDimitry Andric   return false;
46240b57cec5SDimitry Andric }
46250b57cec5SDimitry Andric 
46260b57cec5SDimitry Andric /// checkForBadMacro
46270b57cec5SDimitry Andric ///
46280b57cec5SDimitry Andric /// With the support added for named parameters there may be code out there that
46290b57cec5SDimitry Andric /// is transitioning from positional parameters.  In versions of gas that did
46300b57cec5SDimitry Andric /// not support named parameters they would be ignored on the macro definition.
46310b57cec5SDimitry Andric /// But to support both styles of parameters this is not possible so if a macro
46320b57cec5SDimitry Andric /// definition has named parameters but does not use them and has what appears
46330b57cec5SDimitry Andric /// to be positional parameters, strings like $1, $2, ... and $n, then issue a
46340b57cec5SDimitry Andric /// warning that the positional parameter found in body which have no effect.
46350b57cec5SDimitry Andric /// Hoping the developer will either remove the named parameters from the macro
46360b57cec5SDimitry Andric /// definition so the positional parameters get used if that was what was
46370b57cec5SDimitry Andric /// intended or change the macro to use the named parameters.  It is possible
46380b57cec5SDimitry Andric /// this warning will trigger when the none of the named parameters are used
46390b57cec5SDimitry Andric /// and the strings like $1 are infact to simply to be passed trough unchanged.
46400b57cec5SDimitry Andric void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
46410b57cec5SDimitry Andric                                  StringRef Body,
46420b57cec5SDimitry Andric                                  ArrayRef<MCAsmMacroParameter> Parameters) {
46430b57cec5SDimitry Andric   // If this macro is not defined with named parameters the warning we are
46440b57cec5SDimitry Andric   // checking for here doesn't apply.
46450b57cec5SDimitry Andric   unsigned NParameters = Parameters.size();
46460b57cec5SDimitry Andric   if (NParameters == 0)
46470b57cec5SDimitry Andric     return;
46480b57cec5SDimitry Andric 
46490b57cec5SDimitry Andric   bool NamedParametersFound = false;
46500b57cec5SDimitry Andric   bool PositionalParametersFound = false;
46510b57cec5SDimitry Andric 
46520b57cec5SDimitry Andric   // Look at the body of the macro for use of both the named parameters and what
46530b57cec5SDimitry Andric   // are likely to be positional parameters.  This is what expandMacro() is
46540b57cec5SDimitry Andric   // doing when it finds the parameters in the body.
46550b57cec5SDimitry Andric   while (!Body.empty()) {
46560b57cec5SDimitry Andric     // Scan for the next possible parameter.
46570b57cec5SDimitry Andric     std::size_t End = Body.size(), Pos = 0;
46580b57cec5SDimitry Andric     for (; Pos != End; ++Pos) {
46590b57cec5SDimitry Andric       // Check for a substitution or escape.
46600b57cec5SDimitry Andric       // This macro is defined with parameters, look for \foo, \bar, etc.
46610b57cec5SDimitry Andric       if (Body[Pos] == '\\' && Pos + 1 != End)
46620b57cec5SDimitry Andric         break;
46630b57cec5SDimitry Andric 
46640b57cec5SDimitry Andric       // This macro should have parameters, but look for $0, $1, ..., $n too.
46650b57cec5SDimitry Andric       if (Body[Pos] != '$' || Pos + 1 == End)
46660b57cec5SDimitry Andric         continue;
46670b57cec5SDimitry Andric       char Next = Body[Pos + 1];
46680b57cec5SDimitry Andric       if (Next == '$' || Next == 'n' ||
46690b57cec5SDimitry Andric           isdigit(static_cast<unsigned char>(Next)))
46700b57cec5SDimitry Andric         break;
46710b57cec5SDimitry Andric     }
46720b57cec5SDimitry Andric 
46730b57cec5SDimitry Andric     // Check if we reached the end.
46740b57cec5SDimitry Andric     if (Pos == End)
46750b57cec5SDimitry Andric       break;
46760b57cec5SDimitry Andric 
46770b57cec5SDimitry Andric     if (Body[Pos] == '$') {
46780b57cec5SDimitry Andric       switch (Body[Pos + 1]) {
46790b57cec5SDimitry Andric       // $$ => $
46800b57cec5SDimitry Andric       case '$':
46810b57cec5SDimitry Andric         break;
46820b57cec5SDimitry Andric 
46830b57cec5SDimitry Andric       // $n => number of arguments
46840b57cec5SDimitry Andric       case 'n':
46850b57cec5SDimitry Andric         PositionalParametersFound = true;
46860b57cec5SDimitry Andric         break;
46870b57cec5SDimitry Andric 
46880b57cec5SDimitry Andric       // $[0-9] => argument
46890b57cec5SDimitry Andric       default: {
46900b57cec5SDimitry Andric         PositionalParametersFound = true;
46910b57cec5SDimitry Andric         break;
46920b57cec5SDimitry Andric       }
46930b57cec5SDimitry Andric       }
46940b57cec5SDimitry Andric       Pos += 2;
46950b57cec5SDimitry Andric     } else {
46960b57cec5SDimitry Andric       unsigned I = Pos + 1;
46970b57cec5SDimitry Andric       while (isIdentifierChar(Body[I]) && I + 1 != End)
46980b57cec5SDimitry Andric         ++I;
46990b57cec5SDimitry Andric 
47000b57cec5SDimitry Andric       const char *Begin = Body.data() + Pos + 1;
47010b57cec5SDimitry Andric       StringRef Argument(Begin, I - (Pos + 1));
47020b57cec5SDimitry Andric       unsigned Index = 0;
47030b57cec5SDimitry Andric       for (; Index < NParameters; ++Index)
47040b57cec5SDimitry Andric         if (Parameters[Index].Name == Argument)
47050b57cec5SDimitry Andric           break;
47060b57cec5SDimitry Andric 
47070b57cec5SDimitry Andric       if (Index == NParameters) {
47080b57cec5SDimitry Andric         if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
47090b57cec5SDimitry Andric           Pos += 3;
47100b57cec5SDimitry Andric         else {
47110b57cec5SDimitry Andric           Pos = I;
47120b57cec5SDimitry Andric         }
47130b57cec5SDimitry Andric       } else {
47140b57cec5SDimitry Andric         NamedParametersFound = true;
47150b57cec5SDimitry Andric         Pos += 1 + Argument.size();
47160b57cec5SDimitry Andric       }
47170b57cec5SDimitry Andric     }
47180b57cec5SDimitry Andric     // Update the scan point.
47190b57cec5SDimitry Andric     Body = Body.substr(Pos);
47200b57cec5SDimitry Andric   }
47210b57cec5SDimitry Andric 
47220b57cec5SDimitry Andric   if (!NamedParametersFound && PositionalParametersFound)
47230b57cec5SDimitry Andric     Warning(DirectiveLoc, "macro defined with named parameters which are not "
47240b57cec5SDimitry Andric                           "used in macro body, possible positional parameter "
47250b57cec5SDimitry Andric                           "found in body which will have no effect");
47260b57cec5SDimitry Andric }
47270b57cec5SDimitry Andric 
47280b57cec5SDimitry Andric /// parseDirectiveExitMacro
47290b57cec5SDimitry Andric /// ::= .exitm
47300b57cec5SDimitry Andric bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
4731fe6060f1SDimitry Andric   if (parseEOL())
47320b57cec5SDimitry Andric     return true;
47330b57cec5SDimitry Andric 
47340b57cec5SDimitry Andric   if (!isInsideMacroInstantiation())
47350b57cec5SDimitry Andric     return TokError("unexpected '" + Directive + "' in file, "
47360b57cec5SDimitry Andric                                                  "no current macro definition");
47370b57cec5SDimitry Andric 
47380b57cec5SDimitry Andric   // Exit all conditionals that are active in the current macro.
47390b57cec5SDimitry Andric   while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
47400b57cec5SDimitry Andric     TheCondState = TheCondStack.back();
47410b57cec5SDimitry Andric     TheCondStack.pop_back();
47420b57cec5SDimitry Andric   }
47430b57cec5SDimitry Andric 
47440b57cec5SDimitry Andric   handleMacroExit();
47450b57cec5SDimitry Andric   return false;
47460b57cec5SDimitry Andric }
47470b57cec5SDimitry Andric 
47480b57cec5SDimitry Andric /// parseDirectiveEndMacro
47490b57cec5SDimitry Andric /// ::= .endm
47500b57cec5SDimitry Andric /// ::= .endmacro
47510b57cec5SDimitry Andric bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
47520b57cec5SDimitry Andric   if (getLexer().isNot(AsmToken::EndOfStatement))
47530b57cec5SDimitry Andric     return TokError("unexpected token in '" + Directive + "' directive");
47540b57cec5SDimitry Andric 
47550b57cec5SDimitry Andric   // If we are inside a macro instantiation, terminate the current
47560b57cec5SDimitry Andric   // instantiation.
47570b57cec5SDimitry Andric   if (isInsideMacroInstantiation()) {
47580b57cec5SDimitry Andric     handleMacroExit();
47590b57cec5SDimitry Andric     return false;
47600b57cec5SDimitry Andric   }
47610b57cec5SDimitry Andric 
47620b57cec5SDimitry Andric   // Otherwise, this .endmacro is a stray entry in the file; well formed
47630b57cec5SDimitry Andric   // .endmacro directives are handled during the macro definition parsing.
47640b57cec5SDimitry Andric   return TokError("unexpected '" + Directive + "' in file, "
47650b57cec5SDimitry Andric                                                "no current macro definition");
47660b57cec5SDimitry Andric }
47670b57cec5SDimitry Andric 
47680b57cec5SDimitry Andric /// parseDirectivePurgeMacro
4769fe6060f1SDimitry Andric /// ::= .purgem name
47700b57cec5SDimitry Andric bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
47710b57cec5SDimitry Andric   StringRef Name;
47720b57cec5SDimitry Andric   SMLoc Loc;
47730b57cec5SDimitry Andric   if (parseTokenLoc(Loc) ||
47740b57cec5SDimitry Andric       check(parseIdentifier(Name), Loc,
47750b57cec5SDimitry Andric             "expected identifier in '.purgem' directive") ||
4776fe6060f1SDimitry Andric       parseEOL())
47770b57cec5SDimitry Andric     return true;
47780b57cec5SDimitry Andric 
47790b57cec5SDimitry Andric   if (!getContext().lookupMacro(Name))
47800b57cec5SDimitry Andric     return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
47810b57cec5SDimitry Andric 
47820b57cec5SDimitry Andric   getContext().undefineMacro(Name);
47830b57cec5SDimitry Andric   DEBUG_WITH_TYPE("asm-macros", dbgs()
47840b57cec5SDimitry Andric                                     << "Un-defining macro: " << Name << "\n");
47850b57cec5SDimitry Andric   return false;
47860b57cec5SDimitry Andric }
47870b57cec5SDimitry Andric 
47880b57cec5SDimitry Andric /// parseDirectiveBundleAlignMode
47890b57cec5SDimitry Andric /// ::= {.bundle_align_mode} expression
47900b57cec5SDimitry Andric bool AsmParser::parseDirectiveBundleAlignMode() {
47910b57cec5SDimitry Andric   // Expect a single argument: an expression that evaluates to a constant
47920b57cec5SDimitry Andric   // in the inclusive range 0-30.
47930b57cec5SDimitry Andric   SMLoc ExprLoc = getLexer().getLoc();
47940b57cec5SDimitry Andric   int64_t AlignSizePow2;
47950b57cec5SDimitry Andric   if (checkForValidSection() || parseAbsoluteExpression(AlignSizePow2) ||
4796fe6060f1SDimitry Andric       parseEOL() ||
47970b57cec5SDimitry Andric       check(AlignSizePow2 < 0 || AlignSizePow2 > 30, ExprLoc,
47980b57cec5SDimitry Andric             "invalid bundle alignment size (expected between 0 and 30)"))
47990b57cec5SDimitry Andric     return true;
48000b57cec5SDimitry Andric 
4801bdd1243dSDimitry Andric   getStreamer().emitBundleAlignMode(Align(1ULL << AlignSizePow2));
48020b57cec5SDimitry Andric   return false;
48030b57cec5SDimitry Andric }
48040b57cec5SDimitry Andric 
48050b57cec5SDimitry Andric /// parseDirectiveBundleLock
48060b57cec5SDimitry Andric /// ::= {.bundle_lock} [align_to_end]
48070b57cec5SDimitry Andric bool AsmParser::parseDirectiveBundleLock() {
48080b57cec5SDimitry Andric   if (checkForValidSection())
48090b57cec5SDimitry Andric     return true;
48100b57cec5SDimitry Andric   bool AlignToEnd = false;
48110b57cec5SDimitry Andric 
48120b57cec5SDimitry Andric   StringRef Option;
48130b57cec5SDimitry Andric   SMLoc Loc = getTok().getLoc();
48140b57cec5SDimitry Andric   const char *kInvalidOptionError =
48150b57cec5SDimitry Andric       "invalid option for '.bundle_lock' directive";
48160b57cec5SDimitry Andric 
48170b57cec5SDimitry Andric   if (!parseOptionalToken(AsmToken::EndOfStatement)) {
48180b57cec5SDimitry Andric     if (check(parseIdentifier(Option), Loc, kInvalidOptionError) ||
4819fe6060f1SDimitry Andric         check(Option != "align_to_end", Loc, kInvalidOptionError) || parseEOL())
48200b57cec5SDimitry Andric       return true;
48210b57cec5SDimitry Andric     AlignToEnd = true;
48220b57cec5SDimitry Andric   }
48230b57cec5SDimitry Andric 
48245ffd83dbSDimitry Andric   getStreamer().emitBundleLock(AlignToEnd);
48250b57cec5SDimitry Andric   return false;
48260b57cec5SDimitry Andric }
48270b57cec5SDimitry Andric 
48280b57cec5SDimitry Andric /// parseDirectiveBundleLock
48290b57cec5SDimitry Andric /// ::= {.bundle_lock}
48300b57cec5SDimitry Andric bool AsmParser::parseDirectiveBundleUnlock() {
4831fe6060f1SDimitry Andric   if (checkForValidSection() || parseEOL())
48320b57cec5SDimitry Andric     return true;
48330b57cec5SDimitry Andric 
48345ffd83dbSDimitry Andric   getStreamer().emitBundleUnlock();
48350b57cec5SDimitry Andric   return false;
48360b57cec5SDimitry Andric }
48370b57cec5SDimitry Andric 
48380b57cec5SDimitry Andric /// parseDirectiveSpace
48390b57cec5SDimitry Andric /// ::= (.skip | .space) expression [ , expression ]
48400b57cec5SDimitry Andric bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
48410b57cec5SDimitry Andric   SMLoc NumBytesLoc = Lexer.getLoc();
48420b57cec5SDimitry Andric   const MCExpr *NumBytes;
48430b57cec5SDimitry Andric   if (checkForValidSection() || parseExpression(NumBytes))
48440b57cec5SDimitry Andric     return true;
48450b57cec5SDimitry Andric 
48460b57cec5SDimitry Andric   int64_t FillExpr = 0;
48470b57cec5SDimitry Andric   if (parseOptionalToken(AsmToken::Comma))
48480b57cec5SDimitry Andric     if (parseAbsoluteExpression(FillExpr))
4849fe6060f1SDimitry Andric       return true;
4850fe6060f1SDimitry Andric   if (parseEOL())
4851fe6060f1SDimitry Andric     return true;
48520b57cec5SDimitry Andric 
48530b57cec5SDimitry Andric   // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
48540b57cec5SDimitry Andric   getStreamer().emitFill(*NumBytes, FillExpr, NumBytesLoc);
48550b57cec5SDimitry Andric 
48560b57cec5SDimitry Andric   return false;
48570b57cec5SDimitry Andric }
48580b57cec5SDimitry Andric 
48590b57cec5SDimitry Andric /// parseDirectiveDCB
48600b57cec5SDimitry Andric /// ::= .dcb.{b, l, w} expression, expression
48610b57cec5SDimitry Andric bool AsmParser::parseDirectiveDCB(StringRef IDVal, unsigned Size) {
48620b57cec5SDimitry Andric   SMLoc NumValuesLoc = Lexer.getLoc();
48630b57cec5SDimitry Andric   int64_t NumValues;
48640b57cec5SDimitry Andric   if (checkForValidSection() || parseAbsoluteExpression(NumValues))
48650b57cec5SDimitry Andric     return true;
48660b57cec5SDimitry Andric 
48670b57cec5SDimitry Andric   if (NumValues < 0) {
48680b57cec5SDimitry Andric     Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect");
48690b57cec5SDimitry Andric     return false;
48700b57cec5SDimitry Andric   }
48710b57cec5SDimitry Andric 
4872fe6060f1SDimitry Andric   if (parseComma())
48730b57cec5SDimitry Andric     return true;
48740b57cec5SDimitry Andric 
48750b57cec5SDimitry Andric   const MCExpr *Value;
48760b57cec5SDimitry Andric   SMLoc ExprLoc = getLexer().getLoc();
48770b57cec5SDimitry Andric   if (parseExpression(Value))
48780b57cec5SDimitry Andric     return true;
48790b57cec5SDimitry Andric 
48800b57cec5SDimitry Andric   // Special case constant expressions to match code generator.
48810b57cec5SDimitry Andric   if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
48820b57cec5SDimitry Andric     assert(Size <= 8 && "Invalid size");
48830b57cec5SDimitry Andric     uint64_t IntValue = MCE->getValue();
48840b57cec5SDimitry Andric     if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
48850b57cec5SDimitry Andric       return Error(ExprLoc, "literal value out of range for directive");
48860b57cec5SDimitry Andric     for (uint64_t i = 0, e = NumValues; i != e; ++i)
48875ffd83dbSDimitry Andric       getStreamer().emitIntValue(IntValue, Size);
48880b57cec5SDimitry Andric   } else {
48890b57cec5SDimitry Andric     for (uint64_t i = 0, e = NumValues; i != e; ++i)
48905ffd83dbSDimitry Andric       getStreamer().emitValue(Value, Size, ExprLoc);
48910b57cec5SDimitry Andric   }
48920b57cec5SDimitry Andric 
4893fe6060f1SDimitry Andric   return parseEOL();
48940b57cec5SDimitry Andric }
48950b57cec5SDimitry Andric 
48960b57cec5SDimitry Andric /// parseDirectiveRealDCB
48970b57cec5SDimitry Andric /// ::= .dcb.{d, s} expression, expression
48980b57cec5SDimitry Andric bool AsmParser::parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &Semantics) {
48990b57cec5SDimitry Andric   SMLoc NumValuesLoc = Lexer.getLoc();
49000b57cec5SDimitry Andric   int64_t NumValues;
49010b57cec5SDimitry Andric   if (checkForValidSection() || parseAbsoluteExpression(NumValues))
49020b57cec5SDimitry Andric     return true;
49030b57cec5SDimitry Andric 
49040b57cec5SDimitry Andric   if (NumValues < 0) {
49050b57cec5SDimitry Andric     Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect");
49060b57cec5SDimitry Andric     return false;
49070b57cec5SDimitry Andric   }
49080b57cec5SDimitry Andric 
4909fe6060f1SDimitry Andric   if (parseComma())
49100b57cec5SDimitry Andric     return true;
49110b57cec5SDimitry Andric 
49120b57cec5SDimitry Andric   APInt AsInt;
4913fe6060f1SDimitry Andric   if (parseRealValue(Semantics, AsInt) || parseEOL())
49140b57cec5SDimitry Andric     return true;
49150b57cec5SDimitry Andric 
49160b57cec5SDimitry Andric   for (uint64_t i = 0, e = NumValues; i != e; ++i)
49175ffd83dbSDimitry Andric     getStreamer().emitIntValue(AsInt.getLimitedValue(),
49180b57cec5SDimitry Andric                                AsInt.getBitWidth() / 8);
49190b57cec5SDimitry Andric 
49200b57cec5SDimitry Andric   return false;
49210b57cec5SDimitry Andric }
49220b57cec5SDimitry Andric 
49230b57cec5SDimitry Andric /// parseDirectiveDS
49240b57cec5SDimitry Andric /// ::= .ds.{b, d, l, p, s, w, x} expression
49250b57cec5SDimitry Andric bool AsmParser::parseDirectiveDS(StringRef IDVal, unsigned Size) {
49260b57cec5SDimitry Andric   SMLoc NumValuesLoc = Lexer.getLoc();
49270b57cec5SDimitry Andric   int64_t NumValues;
4928fe6060f1SDimitry Andric   if (checkForValidSection() || parseAbsoluteExpression(NumValues) ||
4929fe6060f1SDimitry Andric       parseEOL())
49300b57cec5SDimitry Andric     return true;
49310b57cec5SDimitry Andric 
49320b57cec5SDimitry Andric   if (NumValues < 0) {
49330b57cec5SDimitry Andric     Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect");
49340b57cec5SDimitry Andric     return false;
49350b57cec5SDimitry Andric   }
49360b57cec5SDimitry Andric 
49370b57cec5SDimitry Andric   for (uint64_t i = 0, e = NumValues; i != e; ++i)
49380b57cec5SDimitry Andric     getStreamer().emitFill(Size, 0);
49390b57cec5SDimitry Andric 
49400b57cec5SDimitry Andric   return false;
49410b57cec5SDimitry Andric }
49420b57cec5SDimitry Andric 
49430b57cec5SDimitry Andric /// parseDirectiveLEB128
49440b57cec5SDimitry Andric /// ::= (.sleb128 | .uleb128) [ expression (, expression)* ]
49450b57cec5SDimitry Andric bool AsmParser::parseDirectiveLEB128(bool Signed) {
49460b57cec5SDimitry Andric   if (checkForValidSection())
49470b57cec5SDimitry Andric     return true;
49480b57cec5SDimitry Andric 
49490b57cec5SDimitry Andric   auto parseOp = [&]() -> bool {
49500b57cec5SDimitry Andric     const MCExpr *Value;
49510b57cec5SDimitry Andric     if (parseExpression(Value))
49520b57cec5SDimitry Andric       return true;
49530b57cec5SDimitry Andric     if (Signed)
49545ffd83dbSDimitry Andric       getStreamer().emitSLEB128Value(Value);
49550b57cec5SDimitry Andric     else
49565ffd83dbSDimitry Andric       getStreamer().emitULEB128Value(Value);
49570b57cec5SDimitry Andric     return false;
49580b57cec5SDimitry Andric   };
49590b57cec5SDimitry Andric 
4960fe6060f1SDimitry Andric   return parseMany(parseOp);
49610b57cec5SDimitry Andric }
49620b57cec5SDimitry Andric 
49630b57cec5SDimitry Andric /// parseDirectiveSymbolAttribute
49640b57cec5SDimitry Andric ///  ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
49650b57cec5SDimitry Andric bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
49660b57cec5SDimitry Andric   auto parseOp = [&]() -> bool {
49670b57cec5SDimitry Andric     StringRef Name;
49680b57cec5SDimitry Andric     SMLoc Loc = getTok().getLoc();
49690b57cec5SDimitry Andric     if (parseIdentifier(Name))
49700b57cec5SDimitry Andric       return Error(Loc, "expected identifier");
4971fe6060f1SDimitry Andric 
4972fe6060f1SDimitry Andric     if (discardLTOSymbol(Name))
4973fe6060f1SDimitry Andric       return false;
4974fe6060f1SDimitry Andric 
49750b57cec5SDimitry Andric     MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
49760b57cec5SDimitry Andric 
4977bdd1243dSDimitry Andric     // Assembler local symbols don't make any sense here, except for directives
4978bdd1243dSDimitry Andric     // that the symbol should be tagged.
4979bdd1243dSDimitry Andric     if (Sym->isTemporary() && Attr != MCSA_Memtag)
49800b57cec5SDimitry Andric       return Error(Loc, "non-local symbol required");
49810b57cec5SDimitry Andric 
49825ffd83dbSDimitry Andric     if (!getStreamer().emitSymbolAttribute(Sym, Attr))
49830b57cec5SDimitry Andric       return Error(Loc, "unable to emit symbol attribute");
49840b57cec5SDimitry Andric     return false;
49850b57cec5SDimitry Andric   };
49860b57cec5SDimitry Andric 
4987fe6060f1SDimitry Andric   return parseMany(parseOp);
49880b57cec5SDimitry Andric }
49890b57cec5SDimitry Andric 
49900b57cec5SDimitry Andric /// parseDirectiveComm
49910b57cec5SDimitry Andric ///  ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
49920b57cec5SDimitry Andric bool AsmParser::parseDirectiveComm(bool IsLocal) {
49930b57cec5SDimitry Andric   if (checkForValidSection())
49940b57cec5SDimitry Andric     return true;
49950b57cec5SDimitry Andric 
49960b57cec5SDimitry Andric   SMLoc IDLoc = getLexer().getLoc();
49970b57cec5SDimitry Andric   StringRef Name;
49980b57cec5SDimitry Andric   if (parseIdentifier(Name))
49990b57cec5SDimitry Andric     return TokError("expected identifier in directive");
50000b57cec5SDimitry Andric 
50010b57cec5SDimitry Andric   // Handle the identifier as the key symbol.
50020b57cec5SDimitry Andric   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
50030b57cec5SDimitry Andric 
5004fe6060f1SDimitry Andric   if (parseComma())
5005fe6060f1SDimitry Andric     return true;
50060b57cec5SDimitry Andric 
50070b57cec5SDimitry Andric   int64_t Size;
50080b57cec5SDimitry Andric   SMLoc SizeLoc = getLexer().getLoc();
50090b57cec5SDimitry Andric   if (parseAbsoluteExpression(Size))
50100b57cec5SDimitry Andric     return true;
50110b57cec5SDimitry Andric 
50120b57cec5SDimitry Andric   int64_t Pow2Alignment = 0;
50130b57cec5SDimitry Andric   SMLoc Pow2AlignmentLoc;
50140b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Comma)) {
50150b57cec5SDimitry Andric     Lex();
50160b57cec5SDimitry Andric     Pow2AlignmentLoc = getLexer().getLoc();
50170b57cec5SDimitry Andric     if (parseAbsoluteExpression(Pow2Alignment))
50180b57cec5SDimitry Andric       return true;
50190b57cec5SDimitry Andric 
50200b57cec5SDimitry Andric     LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
50210b57cec5SDimitry Andric     if (IsLocal && LCOMM == LCOMM::NoAlignment)
50220b57cec5SDimitry Andric       return Error(Pow2AlignmentLoc, "alignment not supported on this target");
50230b57cec5SDimitry Andric 
50240b57cec5SDimitry Andric     // If this target takes alignments in bytes (not log) validate and convert.
50250b57cec5SDimitry Andric     if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
50260b57cec5SDimitry Andric         (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
50270b57cec5SDimitry Andric       if (!isPowerOf2_64(Pow2Alignment))
50280b57cec5SDimitry Andric         return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
50290b57cec5SDimitry Andric       Pow2Alignment = Log2_64(Pow2Alignment);
50300b57cec5SDimitry Andric     }
50310b57cec5SDimitry Andric   }
50320b57cec5SDimitry Andric 
5033fe6060f1SDimitry Andric   if (parseEOL())
50340b57cec5SDimitry Andric     return true;
50350b57cec5SDimitry Andric 
50360b57cec5SDimitry Andric   // NOTE: a size of zero for a .comm should create a undefined symbol
50370b57cec5SDimitry Andric   // but a size of .lcomm creates a bss symbol of size zero.
50380b57cec5SDimitry Andric   if (Size < 0)
503904eeddc0SDimitry Andric     return Error(SizeLoc, "size must be non-negative");
50400b57cec5SDimitry Andric 
50410b57cec5SDimitry Andric   Sym->redefineIfPossible();
50420b57cec5SDimitry Andric   if (!Sym->isUndefined())
50430b57cec5SDimitry Andric     return Error(IDLoc, "invalid symbol redefinition");
50440b57cec5SDimitry Andric 
50450b57cec5SDimitry Andric   // Create the Symbol as a common or local common with Size and Pow2Alignment
50460b57cec5SDimitry Andric   if (IsLocal) {
5047bdd1243dSDimitry Andric     getStreamer().emitLocalCommonSymbol(Sym, Size,
5048bdd1243dSDimitry Andric                                         Align(1ULL << Pow2Alignment));
50490b57cec5SDimitry Andric     return false;
50500b57cec5SDimitry Andric   }
50510b57cec5SDimitry Andric 
5052bdd1243dSDimitry Andric   getStreamer().emitCommonSymbol(Sym, Size, Align(1ULL << Pow2Alignment));
50530b57cec5SDimitry Andric   return false;
50540b57cec5SDimitry Andric }
50550b57cec5SDimitry Andric 
50560b57cec5SDimitry Andric /// parseDirectiveAbort
50570b57cec5SDimitry Andric ///  ::= .abort [... message ...]
50580b57cec5SDimitry Andric bool AsmParser::parseDirectiveAbort() {
50590b57cec5SDimitry Andric   // FIXME: Use loc from directive.
50600b57cec5SDimitry Andric   SMLoc Loc = getLexer().getLoc();
50610b57cec5SDimitry Andric 
50620b57cec5SDimitry Andric   StringRef Str = parseStringToEndOfStatement();
5063fe6060f1SDimitry Andric   if (parseEOL())
50640b57cec5SDimitry Andric     return true;
50650b57cec5SDimitry Andric 
50660b57cec5SDimitry Andric   if (Str.empty())
50670b57cec5SDimitry Andric     return Error(Loc, ".abort detected. Assembly stopping.");
50680b57cec5SDimitry Andric   else
50690b57cec5SDimitry Andric     return Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
50700b57cec5SDimitry Andric   // FIXME: Actually abort assembly here.
50710b57cec5SDimitry Andric 
50720b57cec5SDimitry Andric   return false;
50730b57cec5SDimitry Andric }
50740b57cec5SDimitry Andric 
50750b57cec5SDimitry Andric /// parseDirectiveInclude
50760b57cec5SDimitry Andric ///  ::= .include "filename"
50770b57cec5SDimitry Andric bool AsmParser::parseDirectiveInclude() {
50780b57cec5SDimitry Andric   // Allow the strings to have escaped octal character sequence.
50790b57cec5SDimitry Andric   std::string Filename;
50800b57cec5SDimitry Andric   SMLoc IncludeLoc = getTok().getLoc();
50810b57cec5SDimitry Andric 
50820b57cec5SDimitry Andric   if (check(getTok().isNot(AsmToken::String),
50830b57cec5SDimitry Andric             "expected string in '.include' directive") ||
50840b57cec5SDimitry Andric       parseEscapedString(Filename) ||
50850b57cec5SDimitry Andric       check(getTok().isNot(AsmToken::EndOfStatement),
50860b57cec5SDimitry Andric             "unexpected token in '.include' directive") ||
50870b57cec5SDimitry Andric       // Attempt to switch the lexer to the included file before consuming the
50880b57cec5SDimitry Andric       // end of statement to avoid losing it when we switch.
50890b57cec5SDimitry Andric       check(enterIncludeFile(Filename), IncludeLoc,
50900b57cec5SDimitry Andric             "Could not find include file '" + Filename + "'"))
50910b57cec5SDimitry Andric     return true;
50920b57cec5SDimitry Andric 
50930b57cec5SDimitry Andric   return false;
50940b57cec5SDimitry Andric }
50950b57cec5SDimitry Andric 
50960b57cec5SDimitry Andric /// parseDirectiveIncbin
50970b57cec5SDimitry Andric ///  ::= .incbin "filename" [ , skip [ , count ] ]
50980b57cec5SDimitry Andric bool AsmParser::parseDirectiveIncbin() {
50990b57cec5SDimitry Andric   // Allow the strings to have escaped octal character sequence.
51000b57cec5SDimitry Andric   std::string Filename;
51010b57cec5SDimitry Andric   SMLoc IncbinLoc = getTok().getLoc();
51020b57cec5SDimitry Andric   if (check(getTok().isNot(AsmToken::String),
51030b57cec5SDimitry Andric             "expected string in '.incbin' directive") ||
51040b57cec5SDimitry Andric       parseEscapedString(Filename))
51050b57cec5SDimitry Andric     return true;
51060b57cec5SDimitry Andric 
51070b57cec5SDimitry Andric   int64_t Skip = 0;
51080b57cec5SDimitry Andric   const MCExpr *Count = nullptr;
51090b57cec5SDimitry Andric   SMLoc SkipLoc, CountLoc;
51100b57cec5SDimitry Andric   if (parseOptionalToken(AsmToken::Comma)) {
51110b57cec5SDimitry Andric     // The skip expression can be omitted while specifying the count, e.g:
51120b57cec5SDimitry Andric     //  .incbin "filename",,4
51130b57cec5SDimitry Andric     if (getTok().isNot(AsmToken::Comma)) {
51140b57cec5SDimitry Andric       if (parseTokenLoc(SkipLoc) || parseAbsoluteExpression(Skip))
51150b57cec5SDimitry Andric         return true;
51160b57cec5SDimitry Andric     }
51170b57cec5SDimitry Andric     if (parseOptionalToken(AsmToken::Comma)) {
51180b57cec5SDimitry Andric       CountLoc = getTok().getLoc();
51190b57cec5SDimitry Andric       if (parseExpression(Count))
51200b57cec5SDimitry Andric         return true;
51210b57cec5SDimitry Andric     }
51220b57cec5SDimitry Andric   }
51230b57cec5SDimitry Andric 
5124fe6060f1SDimitry Andric   if (parseEOL())
51250b57cec5SDimitry Andric     return true;
51260b57cec5SDimitry Andric 
51270b57cec5SDimitry Andric   if (check(Skip < 0, SkipLoc, "skip is negative"))
51280b57cec5SDimitry Andric     return true;
51290b57cec5SDimitry Andric 
51300b57cec5SDimitry Andric   // Attempt to process the included file.
51310b57cec5SDimitry Andric   if (processIncbinFile(Filename, Skip, Count, CountLoc))
51320b57cec5SDimitry Andric     return Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
51330b57cec5SDimitry Andric   return false;
51340b57cec5SDimitry Andric }
51350b57cec5SDimitry Andric 
51360b57cec5SDimitry Andric /// parseDirectiveIf
51370b57cec5SDimitry Andric /// ::= .if{,eq,ge,gt,le,lt,ne} expression
51380b57cec5SDimitry Andric bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
51390b57cec5SDimitry Andric   TheCondStack.push_back(TheCondState);
51400b57cec5SDimitry Andric   TheCondState.TheCond = AsmCond::IfCond;
51410b57cec5SDimitry Andric   if (TheCondState.Ignore) {
51420b57cec5SDimitry Andric     eatToEndOfStatement();
51430b57cec5SDimitry Andric   } else {
51440b57cec5SDimitry Andric     int64_t ExprValue;
5145fe6060f1SDimitry Andric     if (parseAbsoluteExpression(ExprValue) || parseEOL())
51460b57cec5SDimitry Andric       return true;
51470b57cec5SDimitry Andric 
51480b57cec5SDimitry Andric     switch (DirKind) {
51490b57cec5SDimitry Andric     default:
51500b57cec5SDimitry Andric       llvm_unreachable("unsupported directive");
51510b57cec5SDimitry Andric     case DK_IF:
51520b57cec5SDimitry Andric     case DK_IFNE:
51530b57cec5SDimitry Andric       break;
51540b57cec5SDimitry Andric     case DK_IFEQ:
51550b57cec5SDimitry Andric       ExprValue = ExprValue == 0;
51560b57cec5SDimitry Andric       break;
51570b57cec5SDimitry Andric     case DK_IFGE:
51580b57cec5SDimitry Andric       ExprValue = ExprValue >= 0;
51590b57cec5SDimitry Andric       break;
51600b57cec5SDimitry Andric     case DK_IFGT:
51610b57cec5SDimitry Andric       ExprValue = ExprValue > 0;
51620b57cec5SDimitry Andric       break;
51630b57cec5SDimitry Andric     case DK_IFLE:
51640b57cec5SDimitry Andric       ExprValue = ExprValue <= 0;
51650b57cec5SDimitry Andric       break;
51660b57cec5SDimitry Andric     case DK_IFLT:
51670b57cec5SDimitry Andric       ExprValue = ExprValue < 0;
51680b57cec5SDimitry Andric       break;
51690b57cec5SDimitry Andric     }
51700b57cec5SDimitry Andric 
51710b57cec5SDimitry Andric     TheCondState.CondMet = ExprValue;
51720b57cec5SDimitry Andric     TheCondState.Ignore = !TheCondState.CondMet;
51730b57cec5SDimitry Andric   }
51740b57cec5SDimitry Andric 
51750b57cec5SDimitry Andric   return false;
51760b57cec5SDimitry Andric }
51770b57cec5SDimitry Andric 
51780b57cec5SDimitry Andric /// parseDirectiveIfb
51790b57cec5SDimitry Andric /// ::= .ifb string
51800b57cec5SDimitry Andric bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
51810b57cec5SDimitry Andric   TheCondStack.push_back(TheCondState);
51820b57cec5SDimitry Andric   TheCondState.TheCond = AsmCond::IfCond;
51830b57cec5SDimitry Andric 
51840b57cec5SDimitry Andric   if (TheCondState.Ignore) {
51850b57cec5SDimitry Andric     eatToEndOfStatement();
51860b57cec5SDimitry Andric   } else {
51870b57cec5SDimitry Andric     StringRef Str = parseStringToEndOfStatement();
51880b57cec5SDimitry Andric 
5189fe6060f1SDimitry Andric     if (parseEOL())
51900b57cec5SDimitry Andric       return true;
51910b57cec5SDimitry Andric 
51920b57cec5SDimitry Andric     TheCondState.CondMet = ExpectBlank == Str.empty();
51930b57cec5SDimitry Andric     TheCondState.Ignore = !TheCondState.CondMet;
51940b57cec5SDimitry Andric   }
51950b57cec5SDimitry Andric 
51960b57cec5SDimitry Andric   return false;
51970b57cec5SDimitry Andric }
51980b57cec5SDimitry Andric 
51990b57cec5SDimitry Andric /// parseDirectiveIfc
52000b57cec5SDimitry Andric /// ::= .ifc string1, string2
52010b57cec5SDimitry Andric /// ::= .ifnc string1, string2
52020b57cec5SDimitry Andric bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
52030b57cec5SDimitry Andric   TheCondStack.push_back(TheCondState);
52040b57cec5SDimitry Andric   TheCondState.TheCond = AsmCond::IfCond;
52050b57cec5SDimitry Andric 
52060b57cec5SDimitry Andric   if (TheCondState.Ignore) {
52070b57cec5SDimitry Andric     eatToEndOfStatement();
52080b57cec5SDimitry Andric   } else {
52090b57cec5SDimitry Andric     StringRef Str1 = parseStringToComma();
52100b57cec5SDimitry Andric 
5211fe6060f1SDimitry Andric     if (parseComma())
52120b57cec5SDimitry Andric       return true;
52130b57cec5SDimitry Andric 
52140b57cec5SDimitry Andric     StringRef Str2 = parseStringToEndOfStatement();
52150b57cec5SDimitry Andric 
5216fe6060f1SDimitry Andric     if (parseEOL())
52170b57cec5SDimitry Andric       return true;
52180b57cec5SDimitry Andric 
52190b57cec5SDimitry Andric     TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
52200b57cec5SDimitry Andric     TheCondState.Ignore = !TheCondState.CondMet;
52210b57cec5SDimitry Andric   }
52220b57cec5SDimitry Andric 
52230b57cec5SDimitry Andric   return false;
52240b57cec5SDimitry Andric }
52250b57cec5SDimitry Andric 
52260b57cec5SDimitry Andric /// parseDirectiveIfeqs
52270b57cec5SDimitry Andric ///   ::= .ifeqs string1, string2
52280b57cec5SDimitry Andric bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) {
52290b57cec5SDimitry Andric   if (Lexer.isNot(AsmToken::String)) {
52300b57cec5SDimitry Andric     if (ExpectEqual)
52310b57cec5SDimitry Andric       return TokError("expected string parameter for '.ifeqs' directive");
52320b57cec5SDimitry Andric     return TokError("expected string parameter for '.ifnes' directive");
52330b57cec5SDimitry Andric   }
52340b57cec5SDimitry Andric 
52350b57cec5SDimitry Andric   StringRef String1 = getTok().getStringContents();
52360b57cec5SDimitry Andric   Lex();
52370b57cec5SDimitry Andric 
52380b57cec5SDimitry Andric   if (Lexer.isNot(AsmToken::Comma)) {
52390b57cec5SDimitry Andric     if (ExpectEqual)
52400b57cec5SDimitry Andric       return TokError(
52410b57cec5SDimitry Andric           "expected comma after first string for '.ifeqs' directive");
52420b57cec5SDimitry Andric     return TokError("expected comma after first string for '.ifnes' directive");
52430b57cec5SDimitry Andric   }
52440b57cec5SDimitry Andric 
52450b57cec5SDimitry Andric   Lex();
52460b57cec5SDimitry Andric 
52470b57cec5SDimitry Andric   if (Lexer.isNot(AsmToken::String)) {
52480b57cec5SDimitry Andric     if (ExpectEqual)
52490b57cec5SDimitry Andric       return TokError("expected string parameter for '.ifeqs' directive");
52500b57cec5SDimitry Andric     return TokError("expected string parameter for '.ifnes' directive");
52510b57cec5SDimitry Andric   }
52520b57cec5SDimitry Andric 
52530b57cec5SDimitry Andric   StringRef String2 = getTok().getStringContents();
52540b57cec5SDimitry Andric   Lex();
52550b57cec5SDimitry Andric 
52560b57cec5SDimitry Andric   TheCondStack.push_back(TheCondState);
52570b57cec5SDimitry Andric   TheCondState.TheCond = AsmCond::IfCond;
52580b57cec5SDimitry Andric   TheCondState.CondMet = ExpectEqual == (String1 == String2);
52590b57cec5SDimitry Andric   TheCondState.Ignore = !TheCondState.CondMet;
52600b57cec5SDimitry Andric 
52610b57cec5SDimitry Andric   return false;
52620b57cec5SDimitry Andric }
52630b57cec5SDimitry Andric 
52640b57cec5SDimitry Andric /// parseDirectiveIfdef
52650b57cec5SDimitry Andric /// ::= .ifdef symbol
52660b57cec5SDimitry Andric bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
52670b57cec5SDimitry Andric   StringRef Name;
52680b57cec5SDimitry Andric   TheCondStack.push_back(TheCondState);
52690b57cec5SDimitry Andric   TheCondState.TheCond = AsmCond::IfCond;
52700b57cec5SDimitry Andric 
52710b57cec5SDimitry Andric   if (TheCondState.Ignore) {
52720b57cec5SDimitry Andric     eatToEndOfStatement();
52730b57cec5SDimitry Andric   } else {
52740b57cec5SDimitry Andric     if (check(parseIdentifier(Name), "expected identifier after '.ifdef'") ||
5275fe6060f1SDimitry Andric         parseEOL())
52760b57cec5SDimitry Andric       return true;
52770b57cec5SDimitry Andric 
52780b57cec5SDimitry Andric     MCSymbol *Sym = getContext().lookupSymbol(Name);
52790b57cec5SDimitry Andric 
52800b57cec5SDimitry Andric     if (expect_defined)
52810b57cec5SDimitry Andric       TheCondState.CondMet = (Sym && !Sym->isUndefined(false));
52820b57cec5SDimitry Andric     else
52830b57cec5SDimitry Andric       TheCondState.CondMet = (!Sym || Sym->isUndefined(false));
52840b57cec5SDimitry Andric     TheCondState.Ignore = !TheCondState.CondMet;
52850b57cec5SDimitry Andric   }
52860b57cec5SDimitry Andric 
52870b57cec5SDimitry Andric   return false;
52880b57cec5SDimitry Andric }
52890b57cec5SDimitry Andric 
52900b57cec5SDimitry Andric /// parseDirectiveElseIf
52910b57cec5SDimitry Andric /// ::= .elseif expression
52920b57cec5SDimitry Andric bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
52930b57cec5SDimitry Andric   if (TheCondState.TheCond != AsmCond::IfCond &&
52940b57cec5SDimitry Andric       TheCondState.TheCond != AsmCond::ElseIfCond)
52950b57cec5SDimitry Andric     return Error(DirectiveLoc, "Encountered a .elseif that doesn't follow an"
52960b57cec5SDimitry Andric                                " .if or  an .elseif");
52970b57cec5SDimitry Andric   TheCondState.TheCond = AsmCond::ElseIfCond;
52980b57cec5SDimitry Andric 
52990b57cec5SDimitry Andric   bool LastIgnoreState = false;
53000b57cec5SDimitry Andric   if (!TheCondStack.empty())
53010b57cec5SDimitry Andric     LastIgnoreState = TheCondStack.back().Ignore;
53020b57cec5SDimitry Andric   if (LastIgnoreState || TheCondState.CondMet) {
53030b57cec5SDimitry Andric     TheCondState.Ignore = true;
53040b57cec5SDimitry Andric     eatToEndOfStatement();
53050b57cec5SDimitry Andric   } else {
53060b57cec5SDimitry Andric     int64_t ExprValue;
53070b57cec5SDimitry Andric     if (parseAbsoluteExpression(ExprValue))
53080b57cec5SDimitry Andric       return true;
53090b57cec5SDimitry Andric 
5310fe6060f1SDimitry Andric     if (parseEOL())
53110b57cec5SDimitry Andric       return true;
53120b57cec5SDimitry Andric 
53130b57cec5SDimitry Andric     TheCondState.CondMet = ExprValue;
53140b57cec5SDimitry Andric     TheCondState.Ignore = !TheCondState.CondMet;
53150b57cec5SDimitry Andric   }
53160b57cec5SDimitry Andric 
53170b57cec5SDimitry Andric   return false;
53180b57cec5SDimitry Andric }
53190b57cec5SDimitry Andric 
53200b57cec5SDimitry Andric /// parseDirectiveElse
53210b57cec5SDimitry Andric /// ::= .else
53220b57cec5SDimitry Andric bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
5323fe6060f1SDimitry Andric   if (parseEOL())
53240b57cec5SDimitry Andric     return true;
53250b57cec5SDimitry Andric 
53260b57cec5SDimitry Andric   if (TheCondState.TheCond != AsmCond::IfCond &&
53270b57cec5SDimitry Andric       TheCondState.TheCond != AsmCond::ElseIfCond)
53280b57cec5SDimitry Andric     return Error(DirectiveLoc, "Encountered a .else that doesn't follow "
53290b57cec5SDimitry Andric                                " an .if or an .elseif");
53300b57cec5SDimitry Andric   TheCondState.TheCond = AsmCond::ElseCond;
53310b57cec5SDimitry Andric   bool LastIgnoreState = false;
53320b57cec5SDimitry Andric   if (!TheCondStack.empty())
53330b57cec5SDimitry Andric     LastIgnoreState = TheCondStack.back().Ignore;
53340b57cec5SDimitry Andric   if (LastIgnoreState || TheCondState.CondMet)
53350b57cec5SDimitry Andric     TheCondState.Ignore = true;
53360b57cec5SDimitry Andric   else
53370b57cec5SDimitry Andric     TheCondState.Ignore = false;
53380b57cec5SDimitry Andric 
53390b57cec5SDimitry Andric   return false;
53400b57cec5SDimitry Andric }
53410b57cec5SDimitry Andric 
53420b57cec5SDimitry Andric /// parseDirectiveEnd
53430b57cec5SDimitry Andric /// ::= .end
53440b57cec5SDimitry Andric bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
5345fe6060f1SDimitry Andric   if (parseEOL())
53460b57cec5SDimitry Andric     return true;
53470b57cec5SDimitry Andric 
53480b57cec5SDimitry Andric   while (Lexer.isNot(AsmToken::Eof))
53490b57cec5SDimitry Andric     Lexer.Lex();
53500b57cec5SDimitry Andric 
53510b57cec5SDimitry Andric   return false;
53520b57cec5SDimitry Andric }
53530b57cec5SDimitry Andric 
53540b57cec5SDimitry Andric /// parseDirectiveError
53550b57cec5SDimitry Andric ///   ::= .err
53560b57cec5SDimitry Andric ///   ::= .error [string]
53570b57cec5SDimitry Andric bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
53580b57cec5SDimitry Andric   if (!TheCondStack.empty()) {
53590b57cec5SDimitry Andric     if (TheCondStack.back().Ignore) {
53600b57cec5SDimitry Andric       eatToEndOfStatement();
53610b57cec5SDimitry Andric       return false;
53620b57cec5SDimitry Andric     }
53630b57cec5SDimitry Andric   }
53640b57cec5SDimitry Andric 
53650b57cec5SDimitry Andric   if (!WithMessage)
53660b57cec5SDimitry Andric     return Error(L, ".err encountered");
53670b57cec5SDimitry Andric 
53680b57cec5SDimitry Andric   StringRef Message = ".error directive invoked in source file";
53690b57cec5SDimitry Andric   if (Lexer.isNot(AsmToken::EndOfStatement)) {
53700b57cec5SDimitry Andric     if (Lexer.isNot(AsmToken::String))
53710b57cec5SDimitry Andric       return TokError(".error argument must be a string");
53720b57cec5SDimitry Andric 
53730b57cec5SDimitry Andric     Message = getTok().getStringContents();
53740b57cec5SDimitry Andric     Lex();
53750b57cec5SDimitry Andric   }
53760b57cec5SDimitry Andric 
53770b57cec5SDimitry Andric   return Error(L, Message);
53780b57cec5SDimitry Andric }
53790b57cec5SDimitry Andric 
53800b57cec5SDimitry Andric /// parseDirectiveWarning
53810b57cec5SDimitry Andric ///   ::= .warning [string]
53820b57cec5SDimitry Andric bool AsmParser::parseDirectiveWarning(SMLoc L) {
53830b57cec5SDimitry Andric   if (!TheCondStack.empty()) {
53840b57cec5SDimitry Andric     if (TheCondStack.back().Ignore) {
53850b57cec5SDimitry Andric       eatToEndOfStatement();
53860b57cec5SDimitry Andric       return false;
53870b57cec5SDimitry Andric     }
53880b57cec5SDimitry Andric   }
53890b57cec5SDimitry Andric 
53900b57cec5SDimitry Andric   StringRef Message = ".warning directive invoked in source file";
53910b57cec5SDimitry Andric 
53920b57cec5SDimitry Andric   if (!parseOptionalToken(AsmToken::EndOfStatement)) {
53930b57cec5SDimitry Andric     if (Lexer.isNot(AsmToken::String))
53940b57cec5SDimitry Andric       return TokError(".warning argument must be a string");
53950b57cec5SDimitry Andric 
53960b57cec5SDimitry Andric     Message = getTok().getStringContents();
53970b57cec5SDimitry Andric     Lex();
5398fe6060f1SDimitry Andric     if (parseEOL())
53990b57cec5SDimitry Andric       return true;
54000b57cec5SDimitry Andric   }
54010b57cec5SDimitry Andric 
54020b57cec5SDimitry Andric   return Warning(L, Message);
54030b57cec5SDimitry Andric }
54040b57cec5SDimitry Andric 
54050b57cec5SDimitry Andric /// parseDirectiveEndIf
54060b57cec5SDimitry Andric /// ::= .endif
54070b57cec5SDimitry Andric bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
5408fe6060f1SDimitry Andric   if (parseEOL())
54090b57cec5SDimitry Andric     return true;
54100b57cec5SDimitry Andric 
54110b57cec5SDimitry Andric   if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
54120b57cec5SDimitry Andric     return Error(DirectiveLoc, "Encountered a .endif that doesn't follow "
54130b57cec5SDimitry Andric                                "an .if or .else");
54140b57cec5SDimitry Andric   if (!TheCondStack.empty()) {
54150b57cec5SDimitry Andric     TheCondState = TheCondStack.back();
54160b57cec5SDimitry Andric     TheCondStack.pop_back();
54170b57cec5SDimitry Andric   }
54180b57cec5SDimitry Andric 
54190b57cec5SDimitry Andric   return false;
54200b57cec5SDimitry Andric }
54210b57cec5SDimitry Andric 
54220b57cec5SDimitry Andric void AsmParser::initializeDirectiveKindMap() {
54235ffd83dbSDimitry Andric   /* Lookup will be done with the directive
54245ffd83dbSDimitry Andric    * converted to lower case, so all these
54255ffd83dbSDimitry Andric    * keys should be lower case.
54265ffd83dbSDimitry Andric    * (target specific directives are handled
54275ffd83dbSDimitry Andric    *  elsewhere)
54285ffd83dbSDimitry Andric    */
54290b57cec5SDimitry Andric   DirectiveKindMap[".set"] = DK_SET;
54300b57cec5SDimitry Andric   DirectiveKindMap[".equ"] = DK_EQU;
54310b57cec5SDimitry Andric   DirectiveKindMap[".equiv"] = DK_EQUIV;
54320b57cec5SDimitry Andric   DirectiveKindMap[".ascii"] = DK_ASCII;
54330b57cec5SDimitry Andric   DirectiveKindMap[".asciz"] = DK_ASCIZ;
54340b57cec5SDimitry Andric   DirectiveKindMap[".string"] = DK_STRING;
54350b57cec5SDimitry Andric   DirectiveKindMap[".byte"] = DK_BYTE;
54360b57cec5SDimitry Andric   DirectiveKindMap[".short"] = DK_SHORT;
54370b57cec5SDimitry Andric   DirectiveKindMap[".value"] = DK_VALUE;
54380b57cec5SDimitry Andric   DirectiveKindMap[".2byte"] = DK_2BYTE;
54390b57cec5SDimitry Andric   DirectiveKindMap[".long"] = DK_LONG;
54400b57cec5SDimitry Andric   DirectiveKindMap[".int"] = DK_INT;
54410b57cec5SDimitry Andric   DirectiveKindMap[".4byte"] = DK_4BYTE;
54420b57cec5SDimitry Andric   DirectiveKindMap[".quad"] = DK_QUAD;
54430b57cec5SDimitry Andric   DirectiveKindMap[".8byte"] = DK_8BYTE;
54440b57cec5SDimitry Andric   DirectiveKindMap[".octa"] = DK_OCTA;
54450b57cec5SDimitry Andric   DirectiveKindMap[".single"] = DK_SINGLE;
54460b57cec5SDimitry Andric   DirectiveKindMap[".float"] = DK_FLOAT;
54470b57cec5SDimitry Andric   DirectiveKindMap[".double"] = DK_DOUBLE;
54480b57cec5SDimitry Andric   DirectiveKindMap[".align"] = DK_ALIGN;
54490b57cec5SDimitry Andric   DirectiveKindMap[".align32"] = DK_ALIGN32;
54500b57cec5SDimitry Andric   DirectiveKindMap[".balign"] = DK_BALIGN;
54510b57cec5SDimitry Andric   DirectiveKindMap[".balignw"] = DK_BALIGNW;
54520b57cec5SDimitry Andric   DirectiveKindMap[".balignl"] = DK_BALIGNL;
54530b57cec5SDimitry Andric   DirectiveKindMap[".p2align"] = DK_P2ALIGN;
54540b57cec5SDimitry Andric   DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
54550b57cec5SDimitry Andric   DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
54560b57cec5SDimitry Andric   DirectiveKindMap[".org"] = DK_ORG;
54570b57cec5SDimitry Andric   DirectiveKindMap[".fill"] = DK_FILL;
54580b57cec5SDimitry Andric   DirectiveKindMap[".zero"] = DK_ZERO;
54590b57cec5SDimitry Andric   DirectiveKindMap[".extern"] = DK_EXTERN;
54600b57cec5SDimitry Andric   DirectiveKindMap[".globl"] = DK_GLOBL;
54610b57cec5SDimitry Andric   DirectiveKindMap[".global"] = DK_GLOBAL;
54620b57cec5SDimitry Andric   DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
54630b57cec5SDimitry Andric   DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
54640b57cec5SDimitry Andric   DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
54650b57cec5SDimitry Andric   DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
54660b57cec5SDimitry Andric   DirectiveKindMap[".reference"] = DK_REFERENCE;
54670b57cec5SDimitry Andric   DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
54680b57cec5SDimitry Andric   DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
54690b57cec5SDimitry Andric   DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
54700b57cec5SDimitry Andric   DirectiveKindMap[".cold"] = DK_COLD;
54710b57cec5SDimitry Andric   DirectiveKindMap[".comm"] = DK_COMM;
54720b57cec5SDimitry Andric   DirectiveKindMap[".common"] = DK_COMMON;
54730b57cec5SDimitry Andric   DirectiveKindMap[".lcomm"] = DK_LCOMM;
54740b57cec5SDimitry Andric   DirectiveKindMap[".abort"] = DK_ABORT;
54750b57cec5SDimitry Andric   DirectiveKindMap[".include"] = DK_INCLUDE;
54760b57cec5SDimitry Andric   DirectiveKindMap[".incbin"] = DK_INCBIN;
54770b57cec5SDimitry Andric   DirectiveKindMap[".code16"] = DK_CODE16;
54780b57cec5SDimitry Andric   DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
54790b57cec5SDimitry Andric   DirectiveKindMap[".rept"] = DK_REPT;
54800b57cec5SDimitry Andric   DirectiveKindMap[".rep"] = DK_REPT;
54810b57cec5SDimitry Andric   DirectiveKindMap[".irp"] = DK_IRP;
54820b57cec5SDimitry Andric   DirectiveKindMap[".irpc"] = DK_IRPC;
54830b57cec5SDimitry Andric   DirectiveKindMap[".endr"] = DK_ENDR;
54840b57cec5SDimitry Andric   DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
54850b57cec5SDimitry Andric   DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
54860b57cec5SDimitry Andric   DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
54870b57cec5SDimitry Andric   DirectiveKindMap[".if"] = DK_IF;
54880b57cec5SDimitry Andric   DirectiveKindMap[".ifeq"] = DK_IFEQ;
54890b57cec5SDimitry Andric   DirectiveKindMap[".ifge"] = DK_IFGE;
54900b57cec5SDimitry Andric   DirectiveKindMap[".ifgt"] = DK_IFGT;
54910b57cec5SDimitry Andric   DirectiveKindMap[".ifle"] = DK_IFLE;
54920b57cec5SDimitry Andric   DirectiveKindMap[".iflt"] = DK_IFLT;
54930b57cec5SDimitry Andric   DirectiveKindMap[".ifne"] = DK_IFNE;
54940b57cec5SDimitry Andric   DirectiveKindMap[".ifb"] = DK_IFB;
54950b57cec5SDimitry Andric   DirectiveKindMap[".ifnb"] = DK_IFNB;
54960b57cec5SDimitry Andric   DirectiveKindMap[".ifc"] = DK_IFC;
54970b57cec5SDimitry Andric   DirectiveKindMap[".ifeqs"] = DK_IFEQS;
54980b57cec5SDimitry Andric   DirectiveKindMap[".ifnc"] = DK_IFNC;
54990b57cec5SDimitry Andric   DirectiveKindMap[".ifnes"] = DK_IFNES;
55000b57cec5SDimitry Andric   DirectiveKindMap[".ifdef"] = DK_IFDEF;
55010b57cec5SDimitry Andric   DirectiveKindMap[".ifndef"] = DK_IFNDEF;
55020b57cec5SDimitry Andric   DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
55030b57cec5SDimitry Andric   DirectiveKindMap[".elseif"] = DK_ELSEIF;
55040b57cec5SDimitry Andric   DirectiveKindMap[".else"] = DK_ELSE;
55050b57cec5SDimitry Andric   DirectiveKindMap[".end"] = DK_END;
55060b57cec5SDimitry Andric   DirectiveKindMap[".endif"] = DK_ENDIF;
55070b57cec5SDimitry Andric   DirectiveKindMap[".skip"] = DK_SKIP;
55080b57cec5SDimitry Andric   DirectiveKindMap[".space"] = DK_SPACE;
55090b57cec5SDimitry Andric   DirectiveKindMap[".file"] = DK_FILE;
55100b57cec5SDimitry Andric   DirectiveKindMap[".line"] = DK_LINE;
55110b57cec5SDimitry Andric   DirectiveKindMap[".loc"] = DK_LOC;
55120b57cec5SDimitry Andric   DirectiveKindMap[".stabs"] = DK_STABS;
55130b57cec5SDimitry Andric   DirectiveKindMap[".cv_file"] = DK_CV_FILE;
55140b57cec5SDimitry Andric   DirectiveKindMap[".cv_func_id"] = DK_CV_FUNC_ID;
55150b57cec5SDimitry Andric   DirectiveKindMap[".cv_loc"] = DK_CV_LOC;
55160b57cec5SDimitry Andric   DirectiveKindMap[".cv_linetable"] = DK_CV_LINETABLE;
55170b57cec5SDimitry Andric   DirectiveKindMap[".cv_inline_linetable"] = DK_CV_INLINE_LINETABLE;
55180b57cec5SDimitry Andric   DirectiveKindMap[".cv_inline_site_id"] = DK_CV_INLINE_SITE_ID;
55190b57cec5SDimitry Andric   DirectiveKindMap[".cv_def_range"] = DK_CV_DEF_RANGE;
55200b57cec5SDimitry Andric   DirectiveKindMap[".cv_string"] = DK_CV_STRING;
55210b57cec5SDimitry Andric   DirectiveKindMap[".cv_stringtable"] = DK_CV_STRINGTABLE;
55220b57cec5SDimitry Andric   DirectiveKindMap[".cv_filechecksums"] = DK_CV_FILECHECKSUMS;
55230b57cec5SDimitry Andric   DirectiveKindMap[".cv_filechecksumoffset"] = DK_CV_FILECHECKSUM_OFFSET;
55240b57cec5SDimitry Andric   DirectiveKindMap[".cv_fpo_data"] = DK_CV_FPO_DATA;
55250b57cec5SDimitry Andric   DirectiveKindMap[".sleb128"] = DK_SLEB128;
55260b57cec5SDimitry Andric   DirectiveKindMap[".uleb128"] = DK_ULEB128;
55270b57cec5SDimitry Andric   DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
55280b57cec5SDimitry Andric   DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
55290b57cec5SDimitry Andric   DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
55300b57cec5SDimitry Andric   DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
55310b57cec5SDimitry Andric   DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
55320b57cec5SDimitry Andric   DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
55330b57cec5SDimitry Andric   DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
5534fe6060f1SDimitry Andric   DirectiveKindMap[".cfi_llvm_def_aspace_cfa"] = DK_CFI_LLVM_DEF_ASPACE_CFA;
55350b57cec5SDimitry Andric   DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
55360b57cec5SDimitry Andric   DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
55370b57cec5SDimitry Andric   DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
55380b57cec5SDimitry Andric   DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
55390b57cec5SDimitry Andric   DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
55400b57cec5SDimitry Andric   DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
55410b57cec5SDimitry Andric   DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
55420b57cec5SDimitry Andric   DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
55430b57cec5SDimitry Andric   DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
55440b57cec5SDimitry Andric   DirectiveKindMap[".cfi_return_column"] = DK_CFI_RETURN_COLUMN;
55450b57cec5SDimitry Andric   DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
55460b57cec5SDimitry Andric   DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
55470b57cec5SDimitry Andric   DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
55480b57cec5SDimitry Andric   DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
55490b57cec5SDimitry Andric   DirectiveKindMap[".cfi_b_key_frame"] = DK_CFI_B_KEY_FRAME;
555081ad6265SDimitry Andric   DirectiveKindMap[".cfi_mte_tagged_frame"] = DK_CFI_MTE_TAGGED_FRAME;
55510b57cec5SDimitry Andric   DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
55520b57cec5SDimitry Andric   DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
55530b57cec5SDimitry Andric   DirectiveKindMap[".macro"] = DK_MACRO;
55540b57cec5SDimitry Andric   DirectiveKindMap[".exitm"] = DK_EXITM;
55550b57cec5SDimitry Andric   DirectiveKindMap[".endm"] = DK_ENDM;
55560b57cec5SDimitry Andric   DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
55570b57cec5SDimitry Andric   DirectiveKindMap[".purgem"] = DK_PURGEM;
55580b57cec5SDimitry Andric   DirectiveKindMap[".err"] = DK_ERR;
55590b57cec5SDimitry Andric   DirectiveKindMap[".error"] = DK_ERROR;
55600b57cec5SDimitry Andric   DirectiveKindMap[".warning"] = DK_WARNING;
55610b57cec5SDimitry Andric   DirectiveKindMap[".altmacro"] = DK_ALTMACRO;
55620b57cec5SDimitry Andric   DirectiveKindMap[".noaltmacro"] = DK_NOALTMACRO;
55630b57cec5SDimitry Andric   DirectiveKindMap[".reloc"] = DK_RELOC;
55640b57cec5SDimitry Andric   DirectiveKindMap[".dc"] = DK_DC;
55650b57cec5SDimitry Andric   DirectiveKindMap[".dc.a"] = DK_DC_A;
55660b57cec5SDimitry Andric   DirectiveKindMap[".dc.b"] = DK_DC_B;
55670b57cec5SDimitry Andric   DirectiveKindMap[".dc.d"] = DK_DC_D;
55680b57cec5SDimitry Andric   DirectiveKindMap[".dc.l"] = DK_DC_L;
55690b57cec5SDimitry Andric   DirectiveKindMap[".dc.s"] = DK_DC_S;
55700b57cec5SDimitry Andric   DirectiveKindMap[".dc.w"] = DK_DC_W;
55710b57cec5SDimitry Andric   DirectiveKindMap[".dc.x"] = DK_DC_X;
55720b57cec5SDimitry Andric   DirectiveKindMap[".dcb"] = DK_DCB;
55730b57cec5SDimitry Andric   DirectiveKindMap[".dcb.b"] = DK_DCB_B;
55740b57cec5SDimitry Andric   DirectiveKindMap[".dcb.d"] = DK_DCB_D;
55750b57cec5SDimitry Andric   DirectiveKindMap[".dcb.l"] = DK_DCB_L;
55760b57cec5SDimitry Andric   DirectiveKindMap[".dcb.s"] = DK_DCB_S;
55770b57cec5SDimitry Andric   DirectiveKindMap[".dcb.w"] = DK_DCB_W;
55780b57cec5SDimitry Andric   DirectiveKindMap[".dcb.x"] = DK_DCB_X;
55790b57cec5SDimitry Andric   DirectiveKindMap[".ds"] = DK_DS;
55800b57cec5SDimitry Andric   DirectiveKindMap[".ds.b"] = DK_DS_B;
55810b57cec5SDimitry Andric   DirectiveKindMap[".ds.d"] = DK_DS_D;
55820b57cec5SDimitry Andric   DirectiveKindMap[".ds.l"] = DK_DS_L;
55830b57cec5SDimitry Andric   DirectiveKindMap[".ds.p"] = DK_DS_P;
55840b57cec5SDimitry Andric   DirectiveKindMap[".ds.s"] = DK_DS_S;
55850b57cec5SDimitry Andric   DirectiveKindMap[".ds.w"] = DK_DS_W;
55860b57cec5SDimitry Andric   DirectiveKindMap[".ds.x"] = DK_DS_X;
55870b57cec5SDimitry Andric   DirectiveKindMap[".print"] = DK_PRINT;
55880b57cec5SDimitry Andric   DirectiveKindMap[".addrsig"] = DK_ADDRSIG;
55890b57cec5SDimitry Andric   DirectiveKindMap[".addrsig_sym"] = DK_ADDRSIG_SYM;
5590e8d8bef9SDimitry Andric   DirectiveKindMap[".pseudoprobe"] = DK_PSEUDO_PROBE;
5591fe6060f1SDimitry Andric   DirectiveKindMap[".lto_discard"] = DK_LTO_DISCARD;
55920eae32dcSDimitry Andric   DirectiveKindMap[".lto_set_conditional"] = DK_LTO_SET_CONDITIONAL;
5593bdd1243dSDimitry Andric   DirectiveKindMap[".memtag"] = DK_MEMTAG;
55940b57cec5SDimitry Andric }
55950b57cec5SDimitry Andric 
55960b57cec5SDimitry Andric MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
55970b57cec5SDimitry Andric   AsmToken EndToken, StartToken = getTok();
55980b57cec5SDimitry Andric 
55990b57cec5SDimitry Andric   unsigned NestLevel = 0;
56000b57cec5SDimitry Andric   while (true) {
56010b57cec5SDimitry Andric     // Check whether we have reached the end of the file.
56020b57cec5SDimitry Andric     if (getLexer().is(AsmToken::Eof)) {
56030b57cec5SDimitry Andric       printError(DirectiveLoc, "no matching '.endr' in definition");
56040b57cec5SDimitry Andric       return nullptr;
56050b57cec5SDimitry Andric     }
56060b57cec5SDimitry Andric 
56070b57cec5SDimitry Andric     if (Lexer.is(AsmToken::Identifier) &&
56080b57cec5SDimitry Andric         (getTok().getIdentifier() == ".rep" ||
56090b57cec5SDimitry Andric          getTok().getIdentifier() == ".rept" ||
56100b57cec5SDimitry Andric          getTok().getIdentifier() == ".irp" ||
56110b57cec5SDimitry Andric          getTok().getIdentifier() == ".irpc")) {
56120b57cec5SDimitry Andric       ++NestLevel;
56130b57cec5SDimitry Andric     }
56140b57cec5SDimitry Andric 
56150b57cec5SDimitry Andric     // Otherwise, check whether we have reached the .endr.
56160b57cec5SDimitry Andric     if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
56170b57cec5SDimitry Andric       if (NestLevel == 0) {
56180b57cec5SDimitry Andric         EndToken = getTok();
56190b57cec5SDimitry Andric         Lex();
56200b57cec5SDimitry Andric         if (Lexer.isNot(AsmToken::EndOfStatement)) {
56210b57cec5SDimitry Andric           printError(getTok().getLoc(),
56220b57cec5SDimitry Andric                      "unexpected token in '.endr' directive");
56230b57cec5SDimitry Andric           return nullptr;
56240b57cec5SDimitry Andric         }
56250b57cec5SDimitry Andric         break;
56260b57cec5SDimitry Andric       }
56270b57cec5SDimitry Andric       --NestLevel;
56280b57cec5SDimitry Andric     }
56290b57cec5SDimitry Andric 
56300b57cec5SDimitry Andric     // Otherwise, scan till the end of the statement.
56310b57cec5SDimitry Andric     eatToEndOfStatement();
56320b57cec5SDimitry Andric   }
56330b57cec5SDimitry Andric 
56340b57cec5SDimitry Andric   const char *BodyStart = StartToken.getLoc().getPointer();
56350b57cec5SDimitry Andric   const char *BodyEnd = EndToken.getLoc().getPointer();
56360b57cec5SDimitry Andric   StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
56370b57cec5SDimitry Andric 
56380b57cec5SDimitry Andric   // We Are Anonymous.
56390b57cec5SDimitry Andric   MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters());
56400b57cec5SDimitry Andric   return &MacroLikeBodies.back();
56410b57cec5SDimitry Andric }
56420b57cec5SDimitry Andric 
56430b57cec5SDimitry Andric void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
56440b57cec5SDimitry Andric                                          raw_svector_ostream &OS) {
56450b57cec5SDimitry Andric   OS << ".endr\n";
56460b57cec5SDimitry Andric 
56470b57cec5SDimitry Andric   std::unique_ptr<MemoryBuffer> Instantiation =
56480b57cec5SDimitry Andric       MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
56490b57cec5SDimitry Andric 
56500b57cec5SDimitry Andric   // Create the macro instantiation object and add to the current macro
56510b57cec5SDimitry Andric   // instantiation stack.
5652480093f4SDimitry Andric   MacroInstantiation *MI = new MacroInstantiation{
5653480093f4SDimitry Andric       DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()};
56540b57cec5SDimitry Andric   ActiveMacros.push_back(MI);
56550b57cec5SDimitry Andric 
56560b57cec5SDimitry Andric   // Jump to the macro instantiation and prime the lexer.
56570b57cec5SDimitry Andric   CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
56580b57cec5SDimitry Andric   Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
56590b57cec5SDimitry Andric   Lex();
56600b57cec5SDimitry Andric }
56610b57cec5SDimitry Andric 
56620b57cec5SDimitry Andric /// parseDirectiveRept
56630b57cec5SDimitry Andric ///   ::= .rep | .rept count
56640b57cec5SDimitry Andric bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
56650b57cec5SDimitry Andric   const MCExpr *CountExpr;
56660b57cec5SDimitry Andric   SMLoc CountLoc = getTok().getLoc();
56670b57cec5SDimitry Andric   if (parseExpression(CountExpr))
56680b57cec5SDimitry Andric     return true;
56690b57cec5SDimitry Andric 
56700b57cec5SDimitry Andric   int64_t Count;
56710b57cec5SDimitry Andric   if (!CountExpr->evaluateAsAbsolute(Count, getStreamer().getAssemblerPtr())) {
56720b57cec5SDimitry Andric     return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
56730b57cec5SDimitry Andric   }
56740b57cec5SDimitry Andric 
5675fe6060f1SDimitry Andric   if (check(Count < 0, CountLoc, "Count is negative") || parseEOL())
56760b57cec5SDimitry Andric     return true;
56770b57cec5SDimitry Andric 
56780b57cec5SDimitry Andric   // Lex the rept definition.
56790b57cec5SDimitry Andric   MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
56800b57cec5SDimitry Andric   if (!M)
56810b57cec5SDimitry Andric     return true;
56820b57cec5SDimitry Andric 
56830b57cec5SDimitry Andric   // Macro instantiation is lexical, unfortunately. We construct a new buffer
56840b57cec5SDimitry Andric   // to hold the macro body with substitutions.
56850b57cec5SDimitry Andric   SmallString<256> Buf;
56860b57cec5SDimitry Andric   raw_svector_ostream OS(Buf);
56870b57cec5SDimitry Andric   while (Count--) {
56880b57cec5SDimitry Andric     // Note that the AtPseudoVariable is disabled for instantiations of .rep(t).
5689bdd1243dSDimitry Andric     if (expandMacro(OS, M->Body, std::nullopt, std::nullopt, false,
5690bdd1243dSDimitry Andric                     getTok().getLoc()))
56910b57cec5SDimitry Andric       return true;
56920b57cec5SDimitry Andric   }
56930b57cec5SDimitry Andric   instantiateMacroLikeBody(M, DirectiveLoc, OS);
56940b57cec5SDimitry Andric 
56950b57cec5SDimitry Andric   return false;
56960b57cec5SDimitry Andric }
56970b57cec5SDimitry Andric 
56980b57cec5SDimitry Andric /// parseDirectiveIrp
56990b57cec5SDimitry Andric /// ::= .irp symbol,values
57000b57cec5SDimitry Andric bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
57010b57cec5SDimitry Andric   MCAsmMacroParameter Parameter;
57020b57cec5SDimitry Andric   MCAsmMacroArguments A;
57030b57cec5SDimitry Andric   if (check(parseIdentifier(Parameter.Name),
57040b57cec5SDimitry Andric             "expected identifier in '.irp' directive") ||
5705fe6060f1SDimitry Andric       parseComma() || parseMacroArguments(nullptr, A) || parseEOL())
57060b57cec5SDimitry Andric     return true;
57070b57cec5SDimitry Andric 
57080b57cec5SDimitry Andric   // Lex the irp definition.
57090b57cec5SDimitry Andric   MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
57100b57cec5SDimitry Andric   if (!M)
57110b57cec5SDimitry Andric     return true;
57120b57cec5SDimitry Andric 
57130b57cec5SDimitry Andric   // Macro instantiation is lexical, unfortunately. We construct a new buffer
57140b57cec5SDimitry Andric   // to hold the macro body with substitutions.
57150b57cec5SDimitry Andric   SmallString<256> Buf;
57160b57cec5SDimitry Andric   raw_svector_ostream OS(Buf);
57170b57cec5SDimitry Andric 
57180b57cec5SDimitry Andric   for (const MCAsmMacroArgument &Arg : A) {
57190b57cec5SDimitry Andric     // Note that the AtPseudoVariable is enabled for instantiations of .irp.
57200b57cec5SDimitry Andric     // This is undocumented, but GAS seems to support it.
57210b57cec5SDimitry Andric     if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
57220b57cec5SDimitry Andric       return true;
57230b57cec5SDimitry Andric   }
57240b57cec5SDimitry Andric 
57250b57cec5SDimitry Andric   instantiateMacroLikeBody(M, DirectiveLoc, OS);
57260b57cec5SDimitry Andric 
57270b57cec5SDimitry Andric   return false;
57280b57cec5SDimitry Andric }
57290b57cec5SDimitry Andric 
57300b57cec5SDimitry Andric /// parseDirectiveIrpc
57310b57cec5SDimitry Andric /// ::= .irpc symbol,values
57320b57cec5SDimitry Andric bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
57330b57cec5SDimitry Andric   MCAsmMacroParameter Parameter;
57340b57cec5SDimitry Andric   MCAsmMacroArguments A;
57350b57cec5SDimitry Andric 
57360b57cec5SDimitry Andric   if (check(parseIdentifier(Parameter.Name),
57370b57cec5SDimitry Andric             "expected identifier in '.irpc' directive") ||
5738fe6060f1SDimitry Andric       parseComma() || parseMacroArguments(nullptr, A))
57390b57cec5SDimitry Andric     return true;
57400b57cec5SDimitry Andric 
57410b57cec5SDimitry Andric   if (A.size() != 1 || A.front().size() != 1)
57420b57cec5SDimitry Andric     return TokError("unexpected token in '.irpc' directive");
5743fe6060f1SDimitry Andric   if (parseEOL())
57440b57cec5SDimitry Andric     return true;
57450b57cec5SDimitry Andric 
57460b57cec5SDimitry Andric   // Lex the irpc definition.
57470b57cec5SDimitry Andric   MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
57480b57cec5SDimitry Andric   if (!M)
57490b57cec5SDimitry Andric     return true;
57500b57cec5SDimitry Andric 
57510b57cec5SDimitry Andric   // Macro instantiation is lexical, unfortunately. We construct a new buffer
57520b57cec5SDimitry Andric   // to hold the macro body with substitutions.
57530b57cec5SDimitry Andric   SmallString<256> Buf;
57540b57cec5SDimitry Andric   raw_svector_ostream OS(Buf);
57550b57cec5SDimitry Andric 
57560b57cec5SDimitry Andric   StringRef Values = A.front().front().getString();
57570b57cec5SDimitry Andric   for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
57580b57cec5SDimitry Andric     MCAsmMacroArgument Arg;
57590b57cec5SDimitry Andric     Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1));
57600b57cec5SDimitry Andric 
57610b57cec5SDimitry Andric     // Note that the AtPseudoVariable is enabled for instantiations of .irpc.
57620b57cec5SDimitry Andric     // This is undocumented, but GAS seems to support it.
57630b57cec5SDimitry Andric     if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
57640b57cec5SDimitry Andric       return true;
57650b57cec5SDimitry Andric   }
57660b57cec5SDimitry Andric 
57670b57cec5SDimitry Andric   instantiateMacroLikeBody(M, DirectiveLoc, OS);
57680b57cec5SDimitry Andric 
57690b57cec5SDimitry Andric   return false;
57700b57cec5SDimitry Andric }
57710b57cec5SDimitry Andric 
57720b57cec5SDimitry Andric bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
57730b57cec5SDimitry Andric   if (ActiveMacros.empty())
57740b57cec5SDimitry Andric     return TokError("unmatched '.endr' directive");
57750b57cec5SDimitry Andric 
57760b57cec5SDimitry Andric   // The only .repl that should get here are the ones created by
57770b57cec5SDimitry Andric   // instantiateMacroLikeBody.
57780b57cec5SDimitry Andric   assert(getLexer().is(AsmToken::EndOfStatement));
57790b57cec5SDimitry Andric 
57800b57cec5SDimitry Andric   handleMacroExit();
57810b57cec5SDimitry Andric   return false;
57820b57cec5SDimitry Andric }
57830b57cec5SDimitry Andric 
57840b57cec5SDimitry Andric bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
57850b57cec5SDimitry Andric                                      size_t Len) {
57860b57cec5SDimitry Andric   const MCExpr *Value;
57870b57cec5SDimitry Andric   SMLoc ExprLoc = getLexer().getLoc();
57880b57cec5SDimitry Andric   if (parseExpression(Value))
57890b57cec5SDimitry Andric     return true;
57900b57cec5SDimitry Andric   const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
57910b57cec5SDimitry Andric   if (!MCE)
57920b57cec5SDimitry Andric     return Error(ExprLoc, "unexpected expression in _emit");
57930b57cec5SDimitry Andric   uint64_t IntValue = MCE->getValue();
57940b57cec5SDimitry Andric   if (!isUInt<8>(IntValue) && !isInt<8>(IntValue))
57950b57cec5SDimitry Andric     return Error(ExprLoc, "literal value out of range for directive");
57960b57cec5SDimitry Andric 
57970b57cec5SDimitry Andric   Info.AsmRewrites->emplace_back(AOK_Emit, IDLoc, Len);
57980b57cec5SDimitry Andric   return false;
57990b57cec5SDimitry Andric }
58000b57cec5SDimitry Andric 
58010b57cec5SDimitry Andric bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
58020b57cec5SDimitry Andric   const MCExpr *Value;
58030b57cec5SDimitry Andric   SMLoc ExprLoc = getLexer().getLoc();
58040b57cec5SDimitry Andric   if (parseExpression(Value))
58050b57cec5SDimitry Andric     return true;
58060b57cec5SDimitry Andric   const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
58070b57cec5SDimitry Andric   if (!MCE)
58080b57cec5SDimitry Andric     return Error(ExprLoc, "unexpected expression in align");
58090b57cec5SDimitry Andric   uint64_t IntValue = MCE->getValue();
58100b57cec5SDimitry Andric   if (!isPowerOf2_64(IntValue))
58110b57cec5SDimitry Andric     return Error(ExprLoc, "literal value not a power of two greater then zero");
58120b57cec5SDimitry Andric 
58130b57cec5SDimitry Andric   Info.AsmRewrites->emplace_back(AOK_Align, IDLoc, 5, Log2_64(IntValue));
58140b57cec5SDimitry Andric   return false;
58150b57cec5SDimitry Andric }
58160b57cec5SDimitry Andric 
58170b57cec5SDimitry Andric bool AsmParser::parseDirectivePrint(SMLoc DirectiveLoc) {
58180b57cec5SDimitry Andric   const AsmToken StrTok = getTok();
58190b57cec5SDimitry Andric   Lex();
58200b57cec5SDimitry Andric   if (StrTok.isNot(AsmToken::String) || StrTok.getString().front() != '"')
58210b57cec5SDimitry Andric     return Error(DirectiveLoc, "expected double quoted string after .print");
5822fe6060f1SDimitry Andric   if (parseEOL())
58230b57cec5SDimitry Andric     return true;
58240b57cec5SDimitry Andric   llvm::outs() << StrTok.getStringContents() << '\n';
58250b57cec5SDimitry Andric   return false;
58260b57cec5SDimitry Andric }
58270b57cec5SDimitry Andric 
58280b57cec5SDimitry Andric bool AsmParser::parseDirectiveAddrsig() {
5829fe6060f1SDimitry Andric   if (parseEOL())
5830fe6060f1SDimitry Andric     return true;
58315ffd83dbSDimitry Andric   getStreamer().emitAddrsig();
58320b57cec5SDimitry Andric   return false;
58330b57cec5SDimitry Andric }
58340b57cec5SDimitry Andric 
58350b57cec5SDimitry Andric bool AsmParser::parseDirectiveAddrsigSym() {
58360b57cec5SDimitry Andric   StringRef Name;
5837fe6060f1SDimitry Andric   if (check(parseIdentifier(Name), "expected identifier") || parseEOL())
58380b57cec5SDimitry Andric     return true;
58390b57cec5SDimitry Andric   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
58405ffd83dbSDimitry Andric   getStreamer().emitAddrsigSym(Sym);
58410b57cec5SDimitry Andric   return false;
58420b57cec5SDimitry Andric }
58430b57cec5SDimitry Andric 
5844e8d8bef9SDimitry Andric bool AsmParser::parseDirectivePseudoProbe() {
5845e8d8bef9SDimitry Andric   int64_t Guid;
5846e8d8bef9SDimitry Andric   int64_t Index;
5847e8d8bef9SDimitry Andric   int64_t Type;
5848e8d8bef9SDimitry Andric   int64_t Attr;
5849*06c3fb27SDimitry Andric   int64_t Discriminator = 0;
5850e8d8bef9SDimitry Andric 
5851e8d8bef9SDimitry Andric   if (parseIntToken(Guid, "unexpected token in '.pseudoprobe' directive"))
5852e8d8bef9SDimitry Andric     return true;
5853e8d8bef9SDimitry Andric 
5854e8d8bef9SDimitry Andric   if (parseIntToken(Index, "unexpected token in '.pseudoprobe' directive"))
5855e8d8bef9SDimitry Andric     return true;
5856e8d8bef9SDimitry Andric 
5857e8d8bef9SDimitry Andric   if (parseIntToken(Type, "unexpected token in '.pseudoprobe' directive"))
5858e8d8bef9SDimitry Andric     return true;
5859e8d8bef9SDimitry Andric 
5860e8d8bef9SDimitry Andric   if (parseIntToken(Attr, "unexpected token in '.pseudoprobe' directive"))
5861e8d8bef9SDimitry Andric     return true;
5862*06c3fb27SDimitry Andric 
5863*06c3fb27SDimitry Andric   if (hasDiscriminator(Attr)) {
5864*06c3fb27SDimitry Andric     if (parseIntToken(Discriminator,
5865*06c3fb27SDimitry Andric                       "unexpected token in '.pseudoprobe' directive"))
5866*06c3fb27SDimitry Andric       return true;
5867e8d8bef9SDimitry Andric   }
5868e8d8bef9SDimitry Andric 
5869e8d8bef9SDimitry Andric   // Parse inline stack like @ GUID:11:12 @ GUID:1:11 @ GUID:3:21
5870e8d8bef9SDimitry Andric   MCPseudoProbeInlineStack InlineStack;
5871e8d8bef9SDimitry Andric 
5872e8d8bef9SDimitry Andric   while (getLexer().is(AsmToken::At)) {
5873e8d8bef9SDimitry Andric     // eat @
5874e8d8bef9SDimitry Andric     Lex();
5875e8d8bef9SDimitry Andric 
5876e8d8bef9SDimitry Andric     int64_t CallerGuid = 0;
5877e8d8bef9SDimitry Andric     if (getLexer().is(AsmToken::Integer)) {
5878e8d8bef9SDimitry Andric       if (parseIntToken(CallerGuid,
5879e8d8bef9SDimitry Andric                         "unexpected token in '.pseudoprobe' directive"))
5880e8d8bef9SDimitry Andric         return true;
5881e8d8bef9SDimitry Andric     }
5882e8d8bef9SDimitry Andric 
5883e8d8bef9SDimitry Andric     // eat colon
5884e8d8bef9SDimitry Andric     if (getLexer().is(AsmToken::Colon))
5885e8d8bef9SDimitry Andric       Lex();
5886e8d8bef9SDimitry Andric 
5887e8d8bef9SDimitry Andric     int64_t CallerProbeId = 0;
5888e8d8bef9SDimitry Andric     if (getLexer().is(AsmToken::Integer)) {
5889e8d8bef9SDimitry Andric       if (parseIntToken(CallerProbeId,
5890e8d8bef9SDimitry Andric                         "unexpected token in '.pseudoprobe' directive"))
5891e8d8bef9SDimitry Andric         return true;
5892e8d8bef9SDimitry Andric     }
5893e8d8bef9SDimitry Andric 
5894e8d8bef9SDimitry Andric     InlineSite Site(CallerGuid, CallerProbeId);
5895e8d8bef9SDimitry Andric     InlineStack.push_back(Site);
5896e8d8bef9SDimitry Andric   }
5897e8d8bef9SDimitry Andric 
5898bdd1243dSDimitry Andric   // Parse function entry name
5899bdd1243dSDimitry Andric   StringRef FnName;
5900bdd1243dSDimitry Andric   if (parseIdentifier(FnName))
5901bdd1243dSDimitry Andric     return Error(getLexer().getLoc(), "unexpected token in '.pseudoprobe' directive");
5902bdd1243dSDimitry Andric   MCSymbol *FnSym = getContext().lookupSymbol(FnName);
5903bdd1243dSDimitry Andric 
5904fe6060f1SDimitry Andric   if (parseEOL())
5905e8d8bef9SDimitry Andric     return true;
5906e8d8bef9SDimitry Andric 
5907*06c3fb27SDimitry Andric   getStreamer().emitPseudoProbe(Guid, Index, Type, Attr, Discriminator,
5908*06c3fb27SDimitry Andric                                 InlineStack, FnSym);
5909e8d8bef9SDimitry Andric   return false;
5910e8d8bef9SDimitry Andric }
5911e8d8bef9SDimitry Andric 
5912fe6060f1SDimitry Andric /// parseDirectiveLTODiscard
5913fe6060f1SDimitry Andric ///  ::= ".lto_discard" [ identifier ( , identifier )* ]
5914fe6060f1SDimitry Andric /// The LTO library emits this directive to discard non-prevailing symbols.
5915fe6060f1SDimitry Andric /// We ignore symbol assignments and attribute changes for the specified
5916fe6060f1SDimitry Andric /// symbols.
5917fe6060f1SDimitry Andric bool AsmParser::parseDirectiveLTODiscard() {
5918fe6060f1SDimitry Andric   auto ParseOp = [&]() -> bool {
5919fe6060f1SDimitry Andric     StringRef Name;
5920fe6060f1SDimitry Andric     SMLoc Loc = getTok().getLoc();
5921fe6060f1SDimitry Andric     if (parseIdentifier(Name))
5922fe6060f1SDimitry Andric       return Error(Loc, "expected identifier");
5923fe6060f1SDimitry Andric     LTODiscardSymbols.insert(Name);
5924fe6060f1SDimitry Andric     return false;
5925fe6060f1SDimitry Andric   };
5926fe6060f1SDimitry Andric 
5927fe6060f1SDimitry Andric   LTODiscardSymbols.clear();
5928fe6060f1SDimitry Andric   return parseMany(ParseOp);
5929fe6060f1SDimitry Andric }
5930fe6060f1SDimitry Andric 
59310b57cec5SDimitry Andric // We are comparing pointers, but the pointers are relative to a single string.
59320b57cec5SDimitry Andric // Thus, this should always be deterministic.
59330b57cec5SDimitry Andric static int rewritesSort(const AsmRewrite *AsmRewriteA,
59340b57cec5SDimitry Andric                         const AsmRewrite *AsmRewriteB) {
59350b57cec5SDimitry Andric   if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
59360b57cec5SDimitry Andric     return -1;
59370b57cec5SDimitry Andric   if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
59380b57cec5SDimitry Andric     return 1;
59390b57cec5SDimitry Andric 
59400b57cec5SDimitry Andric   // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
59410b57cec5SDimitry Andric   // rewrite to the same location.  Make sure the SizeDirective rewrite is
59420b57cec5SDimitry Andric   // performed first, then the Imm/ImmPrefix and finally the Input/Output.  This
59430b57cec5SDimitry Andric   // ensures the sort algorithm is stable.
59440b57cec5SDimitry Andric   if (AsmRewritePrecedence[AsmRewriteA->Kind] >
59450b57cec5SDimitry Andric       AsmRewritePrecedence[AsmRewriteB->Kind])
59460b57cec5SDimitry Andric     return -1;
59470b57cec5SDimitry Andric 
59480b57cec5SDimitry Andric   if (AsmRewritePrecedence[AsmRewriteA->Kind] <
59490b57cec5SDimitry Andric       AsmRewritePrecedence[AsmRewriteB->Kind])
59500b57cec5SDimitry Andric     return 1;
59510b57cec5SDimitry Andric   llvm_unreachable("Unstable rewrite sort.");
59520b57cec5SDimitry Andric }
59530b57cec5SDimitry Andric 
59540b57cec5SDimitry Andric bool AsmParser::parseMSInlineAsm(
5955fe6060f1SDimitry Andric     std::string &AsmString, unsigned &NumOutputs, unsigned &NumInputs,
5956fe6060f1SDimitry Andric     SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
59570b57cec5SDimitry Andric     SmallVectorImpl<std::string> &Constraints,
59580b57cec5SDimitry Andric     SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
59590b57cec5SDimitry Andric     const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
59600b57cec5SDimitry Andric   SmallVector<void *, 4> InputDecls;
59610b57cec5SDimitry Andric   SmallVector<void *, 4> OutputDecls;
59620b57cec5SDimitry Andric   SmallVector<bool, 4> InputDeclsAddressOf;
59630b57cec5SDimitry Andric   SmallVector<bool, 4> OutputDeclsAddressOf;
59640b57cec5SDimitry Andric   SmallVector<std::string, 4> InputConstraints;
59650b57cec5SDimitry Andric   SmallVector<std::string, 4> OutputConstraints;
59660b57cec5SDimitry Andric   SmallVector<unsigned, 4> ClobberRegs;
59670b57cec5SDimitry Andric 
59680b57cec5SDimitry Andric   SmallVector<AsmRewrite, 4> AsmStrRewrites;
59690b57cec5SDimitry Andric 
59700b57cec5SDimitry Andric   // Prime the lexer.
59710b57cec5SDimitry Andric   Lex();
59720b57cec5SDimitry Andric 
59730b57cec5SDimitry Andric   // While we have input, parse each statement.
59740b57cec5SDimitry Andric   unsigned InputIdx = 0;
59750b57cec5SDimitry Andric   unsigned OutputIdx = 0;
59760b57cec5SDimitry Andric   while (getLexer().isNot(AsmToken::Eof)) {
59770b57cec5SDimitry Andric     // Parse curly braces marking block start/end
59780b57cec5SDimitry Andric     if (parseCurlyBlockScope(AsmStrRewrites))
59790b57cec5SDimitry Andric       continue;
59800b57cec5SDimitry Andric 
59810b57cec5SDimitry Andric     ParseStatementInfo Info(&AsmStrRewrites);
59820b57cec5SDimitry Andric     bool StatementErr = parseStatement(Info, &SI);
59830b57cec5SDimitry Andric 
59840b57cec5SDimitry Andric     if (StatementErr || Info.ParseError) {
59850b57cec5SDimitry Andric       // Emit pending errors if any exist.
59860b57cec5SDimitry Andric       printPendingErrors();
59870b57cec5SDimitry Andric       return true;
59880b57cec5SDimitry Andric     }
59890b57cec5SDimitry Andric 
59900b57cec5SDimitry Andric     // No pending error should exist here.
59910b57cec5SDimitry Andric     assert(!hasPendingError() && "unexpected error from parseStatement");
59920b57cec5SDimitry Andric 
59930b57cec5SDimitry Andric     if (Info.Opcode == ~0U)
59940b57cec5SDimitry Andric       continue;
59950b57cec5SDimitry Andric 
59960b57cec5SDimitry Andric     const MCInstrDesc &Desc = MII->get(Info.Opcode);
59970b57cec5SDimitry Andric 
59980b57cec5SDimitry Andric     // Build the list of clobbers, outputs and inputs.
59990b57cec5SDimitry Andric     for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
60000b57cec5SDimitry Andric       MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
60010b57cec5SDimitry Andric 
60020b57cec5SDimitry Andric       // Register operand.
60030b57cec5SDimitry Andric       if (Operand.isReg() && !Operand.needAddressOf() &&
60040b57cec5SDimitry Andric           !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
60050b57cec5SDimitry Andric         unsigned NumDefs = Desc.getNumDefs();
60060b57cec5SDimitry Andric         // Clobber.
60070b57cec5SDimitry Andric         if (NumDefs && Operand.getMCOperandNum() < NumDefs)
60080b57cec5SDimitry Andric           ClobberRegs.push_back(Operand.getReg());
60090b57cec5SDimitry Andric         continue;
60100b57cec5SDimitry Andric       }
60110b57cec5SDimitry Andric 
60120b57cec5SDimitry Andric       // Expr/Input or Output.
60130b57cec5SDimitry Andric       StringRef SymName = Operand.getSymName();
60140b57cec5SDimitry Andric       if (SymName.empty())
60150b57cec5SDimitry Andric         continue;
60160b57cec5SDimitry Andric 
60170b57cec5SDimitry Andric       void *OpDecl = Operand.getOpDecl();
60180b57cec5SDimitry Andric       if (!OpDecl)
60190b57cec5SDimitry Andric         continue;
60200b57cec5SDimitry Andric 
6021480093f4SDimitry Andric       StringRef Constraint = Operand.getConstraint();
6022480093f4SDimitry Andric       if (Operand.isImm()) {
6023480093f4SDimitry Andric         // Offset as immediate
6024480093f4SDimitry Andric         if (Operand.isOffsetOfLocal())
6025480093f4SDimitry Andric           Constraint = "r";
6026480093f4SDimitry Andric         else
6027480093f4SDimitry Andric           Constraint = "i";
6028480093f4SDimitry Andric       }
6029480093f4SDimitry Andric 
60300b57cec5SDimitry Andric       bool isOutput = (i == 1) && Desc.mayStore();
603181ad6265SDimitry Andric       bool Restricted = Operand.isMemUseUpRegs();
60320b57cec5SDimitry Andric       SMLoc Start = SMLoc::getFromPointer(SymName.data());
60330b57cec5SDimitry Andric       if (isOutput) {
60340b57cec5SDimitry Andric         ++InputIdx;
60350b57cec5SDimitry Andric         OutputDecls.push_back(OpDecl);
60360b57cec5SDimitry Andric         OutputDeclsAddressOf.push_back(Operand.needAddressOf());
6037480093f4SDimitry Andric         OutputConstraints.push_back(("=" + Constraint).str());
603881ad6265SDimitry Andric         AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size(), 0,
603981ad6265SDimitry Andric                                     Restricted);
60400b57cec5SDimitry Andric       } else {
60410b57cec5SDimitry Andric         InputDecls.push_back(OpDecl);
60420b57cec5SDimitry Andric         InputDeclsAddressOf.push_back(Operand.needAddressOf());
6043480093f4SDimitry Andric         InputConstraints.push_back(Constraint.str());
6044bdd1243dSDimitry Andric         if (Desc.operands()[i - 1].isBranchTarget())
604581ad6265SDimitry Andric           AsmStrRewrites.emplace_back(AOK_CallInput, Start, SymName.size(), 0,
604681ad6265SDimitry Andric                                       Restricted);
6047480093f4SDimitry Andric         else
604881ad6265SDimitry Andric           AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size(), 0,
604981ad6265SDimitry Andric                                       Restricted);
60500b57cec5SDimitry Andric       }
60510b57cec5SDimitry Andric     }
60520b57cec5SDimitry Andric 
60530b57cec5SDimitry Andric     // Consider implicit defs to be clobbers.  Think of cpuid and push.
6054bdd1243dSDimitry Andric     llvm::append_range(ClobberRegs, Desc.implicit_defs());
60550b57cec5SDimitry Andric   }
60560b57cec5SDimitry Andric 
60570b57cec5SDimitry Andric   // Set the number of Outputs and Inputs.
60580b57cec5SDimitry Andric   NumOutputs = OutputDecls.size();
60590b57cec5SDimitry Andric   NumInputs = InputDecls.size();
60600b57cec5SDimitry Andric 
60610b57cec5SDimitry Andric   // Set the unique clobbers.
60620b57cec5SDimitry Andric   array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
60630b57cec5SDimitry Andric   ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
60640b57cec5SDimitry Andric                     ClobberRegs.end());
60650b57cec5SDimitry Andric   Clobbers.assign(ClobberRegs.size(), std::string());
60660b57cec5SDimitry Andric   for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
60670b57cec5SDimitry Andric     raw_string_ostream OS(Clobbers[I]);
60680b57cec5SDimitry Andric     IP->printRegName(OS, ClobberRegs[I]);
60690b57cec5SDimitry Andric   }
60700b57cec5SDimitry Andric 
60710b57cec5SDimitry Andric   // Merge the various outputs and inputs.  Output are expected first.
60720b57cec5SDimitry Andric   if (NumOutputs || NumInputs) {
60730b57cec5SDimitry Andric     unsigned NumExprs = NumOutputs + NumInputs;
60740b57cec5SDimitry Andric     OpDecls.resize(NumExprs);
60750b57cec5SDimitry Andric     Constraints.resize(NumExprs);
60760b57cec5SDimitry Andric     for (unsigned i = 0; i < NumOutputs; ++i) {
60770b57cec5SDimitry Andric       OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
60780b57cec5SDimitry Andric       Constraints[i] = OutputConstraints[i];
60790b57cec5SDimitry Andric     }
60800b57cec5SDimitry Andric     for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
60810b57cec5SDimitry Andric       OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
60820b57cec5SDimitry Andric       Constraints[j] = InputConstraints[i];
60830b57cec5SDimitry Andric     }
60840b57cec5SDimitry Andric   }
60850b57cec5SDimitry Andric 
60860b57cec5SDimitry Andric   // Build the IR assembly string.
60870b57cec5SDimitry Andric   std::string AsmStringIR;
60880b57cec5SDimitry Andric   raw_string_ostream OS(AsmStringIR);
60890b57cec5SDimitry Andric   StringRef ASMString =
60900b57cec5SDimitry Andric       SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
60910b57cec5SDimitry Andric   const char *AsmStart = ASMString.begin();
60920b57cec5SDimitry Andric   const char *AsmEnd = ASMString.end();
60930b57cec5SDimitry Andric   array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
6094480093f4SDimitry Andric   for (auto it = AsmStrRewrites.begin(); it != AsmStrRewrites.end(); ++it) {
6095480093f4SDimitry Andric     const AsmRewrite &AR = *it;
6096480093f4SDimitry Andric     // Check if this has already been covered by another rewrite...
6097480093f4SDimitry Andric     if (AR.Done)
6098480093f4SDimitry Andric       continue;
60990b57cec5SDimitry Andric     AsmRewriteKind Kind = AR.Kind;
61000b57cec5SDimitry Andric 
61010b57cec5SDimitry Andric     const char *Loc = AR.Loc.getPointer();
61020b57cec5SDimitry Andric     assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
61030b57cec5SDimitry Andric 
61040b57cec5SDimitry Andric     // Emit everything up to the immediate/expression.
61050b57cec5SDimitry Andric     if (unsigned Len = Loc - AsmStart)
61060b57cec5SDimitry Andric       OS << StringRef(AsmStart, Len);
61070b57cec5SDimitry Andric 
61080b57cec5SDimitry Andric     // Skip the original expression.
61090b57cec5SDimitry Andric     if (Kind == AOK_Skip) {
61100b57cec5SDimitry Andric       AsmStart = Loc + AR.Len;
61110b57cec5SDimitry Andric       continue;
61120b57cec5SDimitry Andric     }
61130b57cec5SDimitry Andric 
61140b57cec5SDimitry Andric     unsigned AdditionalSkip = 0;
61150b57cec5SDimitry Andric     // Rewrite expressions in $N notation.
61160b57cec5SDimitry Andric     switch (Kind) {
61170b57cec5SDimitry Andric     default:
61180b57cec5SDimitry Andric       break;
61190b57cec5SDimitry Andric     case AOK_IntelExpr:
61200b57cec5SDimitry Andric       assert(AR.IntelExp.isValid() && "cannot write invalid intel expression");
61210b57cec5SDimitry Andric       if (AR.IntelExp.NeedBracs)
61220b57cec5SDimitry Andric         OS << "[";
61230b57cec5SDimitry Andric       if (AR.IntelExp.hasBaseReg())
61240b57cec5SDimitry Andric         OS << AR.IntelExp.BaseReg;
61250b57cec5SDimitry Andric       if (AR.IntelExp.hasIndexReg())
61260b57cec5SDimitry Andric         OS << (AR.IntelExp.hasBaseReg() ? " + " : "")
61270b57cec5SDimitry Andric            << AR.IntelExp.IndexReg;
61280b57cec5SDimitry Andric       if (AR.IntelExp.Scale > 1)
61290b57cec5SDimitry Andric         OS << " * $$" << AR.IntelExp.Scale;
6130480093f4SDimitry Andric       if (AR.IntelExp.hasOffset()) {
6131480093f4SDimitry Andric         if (AR.IntelExp.hasRegs())
6132480093f4SDimitry Andric           OS << " + ";
6133480093f4SDimitry Andric         // Fuse this rewrite with a rewrite of the offset name, if present.
6134480093f4SDimitry Andric         StringRef OffsetName = AR.IntelExp.OffsetName;
6135480093f4SDimitry Andric         SMLoc OffsetLoc = SMLoc::getFromPointer(AR.IntelExp.OffsetName.data());
6136480093f4SDimitry Andric         size_t OffsetLen = OffsetName.size();
6137480093f4SDimitry Andric         auto rewrite_it = std::find_if(
6138480093f4SDimitry Andric             it, AsmStrRewrites.end(), [&](const AsmRewrite &FusingAR) {
6139480093f4SDimitry Andric               return FusingAR.Loc == OffsetLoc && FusingAR.Len == OffsetLen &&
6140480093f4SDimitry Andric                      (FusingAR.Kind == AOK_Input ||
6141480093f4SDimitry Andric                       FusingAR.Kind == AOK_CallInput);
6142480093f4SDimitry Andric             });
6143480093f4SDimitry Andric         if (rewrite_it == AsmStrRewrites.end()) {
6144480093f4SDimitry Andric           OS << "offset " << OffsetName;
6145480093f4SDimitry Andric         } else if (rewrite_it->Kind == AOK_CallInput) {
6146480093f4SDimitry Andric           OS << "${" << InputIdx++ << ":P}";
6147480093f4SDimitry Andric           rewrite_it->Done = true;
6148480093f4SDimitry Andric         } else {
6149480093f4SDimitry Andric           OS << '$' << InputIdx++;
6150480093f4SDimitry Andric           rewrite_it->Done = true;
6151480093f4SDimitry Andric         }
6152480093f4SDimitry Andric       }
6153480093f4SDimitry Andric       if (AR.IntelExp.Imm || AR.IntelExp.emitImm())
6154480093f4SDimitry Andric         OS << (AR.IntelExp.emitImm() ? "$$" : " + $$") << AR.IntelExp.Imm;
61550b57cec5SDimitry Andric       if (AR.IntelExp.NeedBracs)
61560b57cec5SDimitry Andric         OS << "]";
61570b57cec5SDimitry Andric       break;
61580b57cec5SDimitry Andric     case AOK_Label:
61590b57cec5SDimitry Andric       OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
61600b57cec5SDimitry Andric       break;
61610b57cec5SDimitry Andric     case AOK_Input:
616281ad6265SDimitry Andric       if (AR.IntelExpRestricted)
616381ad6265SDimitry Andric         OS << "${" << InputIdx++ << ":P}";
616481ad6265SDimitry Andric       else
616581ad6265SDimitry Andric         OS << '$' << InputIdx++;
61660b57cec5SDimitry Andric       break;
6167480093f4SDimitry Andric     case AOK_CallInput:
6168480093f4SDimitry Andric       OS << "${" << InputIdx++ << ":P}";
6169480093f4SDimitry Andric       break;
61700b57cec5SDimitry Andric     case AOK_Output:
617181ad6265SDimitry Andric       if (AR.IntelExpRestricted)
617281ad6265SDimitry Andric         OS << "${" << OutputIdx++ << ":P}";
617381ad6265SDimitry Andric       else
617481ad6265SDimitry Andric         OS << '$' << OutputIdx++;
61750b57cec5SDimitry Andric       break;
61760b57cec5SDimitry Andric     case AOK_SizeDirective:
61770b57cec5SDimitry Andric       switch (AR.Val) {
61780b57cec5SDimitry Andric       default: break;
61790b57cec5SDimitry Andric       case 8:  OS << "byte ptr "; break;
61800b57cec5SDimitry Andric       case 16: OS << "word ptr "; break;
61810b57cec5SDimitry Andric       case 32: OS << "dword ptr "; break;
61820b57cec5SDimitry Andric       case 64: OS << "qword ptr "; break;
61830b57cec5SDimitry Andric       case 80: OS << "xword ptr "; break;
61840b57cec5SDimitry Andric       case 128: OS << "xmmword ptr "; break;
61850b57cec5SDimitry Andric       case 256: OS << "ymmword ptr "; break;
61860b57cec5SDimitry Andric       }
61870b57cec5SDimitry Andric       break;
61880b57cec5SDimitry Andric     case AOK_Emit:
61890b57cec5SDimitry Andric       OS << ".byte";
61900b57cec5SDimitry Andric       break;
61910b57cec5SDimitry Andric     case AOK_Align: {
61920b57cec5SDimitry Andric       // MS alignment directives are measured in bytes. If the native assembler
61930b57cec5SDimitry Andric       // measures alignment in bytes, we can pass it straight through.
61940b57cec5SDimitry Andric       OS << ".align";
61950b57cec5SDimitry Andric       if (getContext().getAsmInfo()->getAlignmentIsInBytes())
61960b57cec5SDimitry Andric         break;
61970b57cec5SDimitry Andric 
61980b57cec5SDimitry Andric       // Alignment is in log2 form, so print that instead and skip the original
61990b57cec5SDimitry Andric       // immediate.
62000b57cec5SDimitry Andric       unsigned Val = AR.Val;
62010b57cec5SDimitry Andric       OS << ' ' << Val;
62020b57cec5SDimitry Andric       assert(Val < 10 && "Expected alignment less then 2^10.");
62030b57cec5SDimitry Andric       AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
62040b57cec5SDimitry Andric       break;
62050b57cec5SDimitry Andric     }
62060b57cec5SDimitry Andric     case AOK_EVEN:
62070b57cec5SDimitry Andric       OS << ".even";
62080b57cec5SDimitry Andric       break;
62090b57cec5SDimitry Andric     case AOK_EndOfStatement:
62100b57cec5SDimitry Andric       OS << "\n\t";
62110b57cec5SDimitry Andric       break;
62120b57cec5SDimitry Andric     }
62130b57cec5SDimitry Andric 
62140b57cec5SDimitry Andric     // Skip the original expression.
62150b57cec5SDimitry Andric     AsmStart = Loc + AR.Len + AdditionalSkip;
62160b57cec5SDimitry Andric   }
62170b57cec5SDimitry Andric 
62180b57cec5SDimitry Andric   // Emit the remainder of the asm string.
62190b57cec5SDimitry Andric   if (AsmStart != AsmEnd)
62200b57cec5SDimitry Andric     OS << StringRef(AsmStart, AsmEnd - AsmStart);
62210b57cec5SDimitry Andric 
62220b57cec5SDimitry Andric   AsmString = OS.str();
62230b57cec5SDimitry Andric   return false;
62240b57cec5SDimitry Andric }
62250b57cec5SDimitry Andric 
6226fe6060f1SDimitry Andric bool HLASMAsmParser::parseAsHLASMLabel(ParseStatementInfo &Info,
6227fe6060f1SDimitry Andric                                        MCAsmParserSemaCallback *SI) {
6228fe6060f1SDimitry Andric   AsmToken LabelTok = getTok();
6229fe6060f1SDimitry Andric   SMLoc LabelLoc = LabelTok.getLoc();
6230fe6060f1SDimitry Andric   StringRef LabelVal;
6231fe6060f1SDimitry Andric 
6232fe6060f1SDimitry Andric   if (parseIdentifier(LabelVal))
6233fe6060f1SDimitry Andric     return Error(LabelLoc, "The HLASM Label has to be an Identifier");
6234fe6060f1SDimitry Andric 
6235fe6060f1SDimitry Andric   // We have validated whether the token is an Identifier.
6236fe6060f1SDimitry Andric   // Now we have to validate whether the token is a
6237fe6060f1SDimitry Andric   // valid HLASM Label.
6238fe6060f1SDimitry Andric   if (!getTargetParser().isLabel(LabelTok) || checkForValidSection())
6239fe6060f1SDimitry Andric     return true;
6240fe6060f1SDimitry Andric 
6241fe6060f1SDimitry Andric   // Lex leading spaces to get to the next operand.
6242fe6060f1SDimitry Andric   lexLeadingSpaces();
6243fe6060f1SDimitry Andric 
6244fe6060f1SDimitry Andric   // We shouldn't emit the label if there is nothing else after the label.
6245fe6060f1SDimitry Andric   // i.e asm("<token>\n")
6246fe6060f1SDimitry Andric   if (getTok().is(AsmToken::EndOfStatement))
6247fe6060f1SDimitry Andric     return Error(LabelLoc,
6248fe6060f1SDimitry Andric                  "Cannot have just a label for an HLASM inline asm statement");
6249fe6060f1SDimitry Andric 
6250fe6060f1SDimitry Andric   MCSymbol *Sym = getContext().getOrCreateSymbol(
6251fe6060f1SDimitry Andric       getContext().getAsmInfo()->shouldEmitLabelsInUpperCase()
6252fe6060f1SDimitry Andric           ? LabelVal.upper()
6253fe6060f1SDimitry Andric           : LabelVal);
6254fe6060f1SDimitry Andric 
6255bdd1243dSDimitry Andric   getTargetParser().doBeforeLabelEmit(Sym, LabelLoc);
6256fe6060f1SDimitry Andric 
6257fe6060f1SDimitry Andric   // Emit the label.
6258fe6060f1SDimitry Andric   Out.emitLabel(Sym, LabelLoc);
6259fe6060f1SDimitry Andric 
6260fe6060f1SDimitry Andric   // If we are generating dwarf for assembly source files then gather the
6261fe6060f1SDimitry Andric   // info to make a dwarf label entry for this label if needed.
6262fe6060f1SDimitry Andric   if (enabledGenDwarfForAssembly())
6263fe6060f1SDimitry Andric     MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
6264fe6060f1SDimitry Andric                                LabelLoc);
6265fe6060f1SDimitry Andric 
6266fe6060f1SDimitry Andric   getTargetParser().onLabelParsed(Sym);
6267fe6060f1SDimitry Andric 
6268fe6060f1SDimitry Andric   return false;
6269fe6060f1SDimitry Andric }
6270fe6060f1SDimitry Andric 
6271fe6060f1SDimitry Andric bool HLASMAsmParser::parseAsMachineInstruction(ParseStatementInfo &Info,
6272fe6060f1SDimitry Andric                                                MCAsmParserSemaCallback *SI) {
6273fe6060f1SDimitry Andric   AsmToken OperationEntryTok = Lexer.getTok();
6274fe6060f1SDimitry Andric   SMLoc OperationEntryLoc = OperationEntryTok.getLoc();
6275fe6060f1SDimitry Andric   StringRef OperationEntryVal;
6276fe6060f1SDimitry Andric 
6277fe6060f1SDimitry Andric   // Attempt to parse the first token as an Identifier
6278fe6060f1SDimitry Andric   if (parseIdentifier(OperationEntryVal))
6279fe6060f1SDimitry Andric     return Error(OperationEntryLoc, "unexpected token at start of statement");
6280fe6060f1SDimitry Andric 
6281fe6060f1SDimitry Andric   // Once we've parsed the operation entry successfully, lex
6282fe6060f1SDimitry Andric   // any spaces to get to the OperandEntries.
6283fe6060f1SDimitry Andric   lexLeadingSpaces();
6284fe6060f1SDimitry Andric 
6285fe6060f1SDimitry Andric   return parseAndMatchAndEmitTargetInstruction(
6286fe6060f1SDimitry Andric       Info, OperationEntryVal, OperationEntryTok, OperationEntryLoc);
6287fe6060f1SDimitry Andric }
6288fe6060f1SDimitry Andric 
6289fe6060f1SDimitry Andric bool HLASMAsmParser::parseStatement(ParseStatementInfo &Info,
6290fe6060f1SDimitry Andric                                     MCAsmParserSemaCallback *SI) {
6291fe6060f1SDimitry Andric   assert(!hasPendingError() && "parseStatement started with pending error");
6292fe6060f1SDimitry Andric 
6293fe6060f1SDimitry Andric   // Should the first token be interpreted as a HLASM Label.
6294fe6060f1SDimitry Andric   bool ShouldParseAsHLASMLabel = false;
6295fe6060f1SDimitry Andric 
6296fe6060f1SDimitry Andric   // If a Name Entry exists, it should occur at the very
6297fe6060f1SDimitry Andric   // start of the string. In this case, we should parse the
6298fe6060f1SDimitry Andric   // first non-space token as a Label.
6299fe6060f1SDimitry Andric   // If the Name entry is missing (i.e. there's some other
6300fe6060f1SDimitry Andric   // token), then we attempt to parse the first non-space
6301fe6060f1SDimitry Andric   // token as a Machine Instruction.
6302fe6060f1SDimitry Andric   if (getTok().isNot(AsmToken::Space))
6303fe6060f1SDimitry Andric     ShouldParseAsHLASMLabel = true;
6304fe6060f1SDimitry Andric 
6305fe6060f1SDimitry Andric   // If we have an EndOfStatement (which includes the target's comment
6306fe6060f1SDimitry Andric   // string) we can appropriately lex it early on)
6307fe6060f1SDimitry Andric   if (Lexer.is(AsmToken::EndOfStatement)) {
6308fe6060f1SDimitry Andric     // if this is a line comment we can drop it safely
6309fe6060f1SDimitry Andric     if (getTok().getString().empty() || getTok().getString().front() == '\r' ||
6310fe6060f1SDimitry Andric         getTok().getString().front() == '\n')
631181ad6265SDimitry Andric       Out.addBlankLine();
6312fe6060f1SDimitry Andric     Lex();
6313fe6060f1SDimitry Andric     return false;
6314fe6060f1SDimitry Andric   }
6315fe6060f1SDimitry Andric 
6316fe6060f1SDimitry Andric   // We have established how to parse the inline asm statement.
6317fe6060f1SDimitry Andric   // Now we can safely lex any leading spaces to get to the
6318fe6060f1SDimitry Andric   // first token.
6319fe6060f1SDimitry Andric   lexLeadingSpaces();
6320fe6060f1SDimitry Andric 
6321fe6060f1SDimitry Andric   // If we see a new line or carriage return as the first operand,
6322fe6060f1SDimitry Andric   // after lexing leading spaces, emit the new line and lex the
6323fe6060f1SDimitry Andric   // EndOfStatement token.
6324fe6060f1SDimitry Andric   if (Lexer.is(AsmToken::EndOfStatement)) {
6325fe6060f1SDimitry Andric     if (getTok().getString().front() == '\n' ||
6326fe6060f1SDimitry Andric         getTok().getString().front() == '\r') {
632781ad6265SDimitry Andric       Out.addBlankLine();
6328fe6060f1SDimitry Andric       Lex();
6329fe6060f1SDimitry Andric       return false;
6330fe6060f1SDimitry Andric     }
6331fe6060f1SDimitry Andric   }
6332fe6060f1SDimitry Andric 
6333fe6060f1SDimitry Andric   // Handle the label first if we have to before processing the rest
6334fe6060f1SDimitry Andric   // of the tokens as a machine instruction.
6335fe6060f1SDimitry Andric   if (ShouldParseAsHLASMLabel) {
6336fe6060f1SDimitry Andric     // If there were any errors while handling and emitting the label,
6337fe6060f1SDimitry Andric     // early return.
6338fe6060f1SDimitry Andric     if (parseAsHLASMLabel(Info, SI)) {
6339fe6060f1SDimitry Andric       // If we know we've failed in parsing, simply eat until end of the
6340fe6060f1SDimitry Andric       // statement. This ensures that we don't process any other statements.
6341fe6060f1SDimitry Andric       eatToEndOfStatement();
6342fe6060f1SDimitry Andric       return true;
6343fe6060f1SDimitry Andric     }
6344fe6060f1SDimitry Andric   }
6345fe6060f1SDimitry Andric 
6346fe6060f1SDimitry Andric   return parseAsMachineInstruction(Info, SI);
6347fe6060f1SDimitry Andric }
6348fe6060f1SDimitry Andric 
63490b57cec5SDimitry Andric namespace llvm {
63500b57cec5SDimitry Andric namespace MCParserUtils {
63510b57cec5SDimitry Andric 
63520b57cec5SDimitry Andric /// Returns whether the given symbol is used anywhere in the given expression,
63530b57cec5SDimitry Andric /// or subexpressions.
63540b57cec5SDimitry Andric static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) {
63550b57cec5SDimitry Andric   switch (Value->getKind()) {
63560b57cec5SDimitry Andric   case MCExpr::Binary: {
63570b57cec5SDimitry Andric     const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
63580b57cec5SDimitry Andric     return isSymbolUsedInExpression(Sym, BE->getLHS()) ||
63590b57cec5SDimitry Andric            isSymbolUsedInExpression(Sym, BE->getRHS());
63600b57cec5SDimitry Andric   }
63610b57cec5SDimitry Andric   case MCExpr::Target:
63620b57cec5SDimitry Andric   case MCExpr::Constant:
63630b57cec5SDimitry Andric     return false;
63640b57cec5SDimitry Andric   case MCExpr::SymbolRef: {
63650b57cec5SDimitry Andric     const MCSymbol &S =
63660b57cec5SDimitry Andric         static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
6367*06c3fb27SDimitry Andric     if (S.isVariable() && !S.isWeakExternal())
63680b57cec5SDimitry Andric       return isSymbolUsedInExpression(Sym, S.getVariableValue());
63690b57cec5SDimitry Andric     return &S == Sym;
63700b57cec5SDimitry Andric   }
63710b57cec5SDimitry Andric   case MCExpr::Unary:
63720b57cec5SDimitry Andric     return isSymbolUsedInExpression(
63730b57cec5SDimitry Andric         Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
63740b57cec5SDimitry Andric   }
63750b57cec5SDimitry Andric 
63760b57cec5SDimitry Andric   llvm_unreachable("Unknown expr kind!");
63770b57cec5SDimitry Andric }
63780b57cec5SDimitry Andric 
63790b57cec5SDimitry Andric bool parseAssignmentExpression(StringRef Name, bool allow_redef,
63800b57cec5SDimitry Andric                                MCAsmParser &Parser, MCSymbol *&Sym,
63810b57cec5SDimitry Andric                                const MCExpr *&Value) {
63820b57cec5SDimitry Andric 
63830b57cec5SDimitry Andric   // FIXME: Use better location, we should use proper tokens.
63840b57cec5SDimitry Andric   SMLoc EqualLoc = Parser.getTok().getLoc();
63850b57cec5SDimitry Andric   if (Parser.parseExpression(Value))
63860b57cec5SDimitry Andric     return Parser.TokError("missing expression");
63870b57cec5SDimitry Andric 
63880b57cec5SDimitry Andric   // Note: we don't count b as used in "a = b". This is to allow
63890b57cec5SDimitry Andric   // a = b
63900b57cec5SDimitry Andric   // b = c
63910b57cec5SDimitry Andric 
6392fe6060f1SDimitry Andric   if (Parser.parseEOL())
63930b57cec5SDimitry Andric     return true;
63940b57cec5SDimitry Andric 
63950b57cec5SDimitry Andric   // Validate that the LHS is allowed to be a variable (either it has not been
63960b57cec5SDimitry Andric   // used as a symbol, or it is an absolute symbol).
63970b57cec5SDimitry Andric   Sym = Parser.getContext().lookupSymbol(Name);
63980b57cec5SDimitry Andric   if (Sym) {
63990b57cec5SDimitry Andric     // Diagnose assignment to a label.
64000b57cec5SDimitry Andric     //
64010b57cec5SDimitry Andric     // FIXME: Diagnostics. Note the location of the definition as a label.
64020b57cec5SDimitry Andric     // FIXME: Diagnose assignment to protected identifier (e.g., register name).
64030b57cec5SDimitry Andric     if (isSymbolUsedInExpression(Sym, Value))
64040b57cec5SDimitry Andric       return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'");
64050b57cec5SDimitry Andric     else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() &&
64060b57cec5SDimitry Andric              !Sym->isVariable())
64070b57cec5SDimitry Andric       ; // Allow redefinitions of undefined symbols only used in directives.
64080b57cec5SDimitry Andric     else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
64090b57cec5SDimitry Andric       ; // Allow redefinitions of variables that haven't yet been used.
64100b57cec5SDimitry Andric     else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
64110b57cec5SDimitry Andric       return Parser.Error(EqualLoc, "redefinition of '" + Name + "'");
64120b57cec5SDimitry Andric     else if (!Sym->isVariable())
64130b57cec5SDimitry Andric       return Parser.Error(EqualLoc, "invalid assignment to '" + Name + "'");
64140b57cec5SDimitry Andric     else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
64150b57cec5SDimitry Andric       return Parser.Error(EqualLoc,
64160b57cec5SDimitry Andric                           "invalid reassignment of non-absolute variable '" +
64170b57cec5SDimitry Andric                               Name + "'");
64180b57cec5SDimitry Andric   } else if (Name == ".") {
64190b57cec5SDimitry Andric     Parser.getStreamer().emitValueToOffset(Value, 0, EqualLoc);
64200b57cec5SDimitry Andric     return false;
64210b57cec5SDimitry Andric   } else
64220b57cec5SDimitry Andric     Sym = Parser.getContext().getOrCreateSymbol(Name);
64230b57cec5SDimitry Andric 
64240b57cec5SDimitry Andric   Sym->setRedefinable(allow_redef);
64250b57cec5SDimitry Andric 
64260b57cec5SDimitry Andric   return false;
64270b57cec5SDimitry Andric }
64280b57cec5SDimitry Andric 
64290b57cec5SDimitry Andric } // end namespace MCParserUtils
64300b57cec5SDimitry Andric } // end namespace llvm
64310b57cec5SDimitry Andric 
64320b57cec5SDimitry Andric /// Create an MCAsmParser instance.
64330b57cec5SDimitry Andric MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
64340b57cec5SDimitry Andric                                      MCStreamer &Out, const MCAsmInfo &MAI,
64350b57cec5SDimitry Andric                                      unsigned CB) {
6436fe6060f1SDimitry Andric   if (C.getTargetTriple().isSystemZ() && C.getTargetTriple().isOSzOS())
6437fe6060f1SDimitry Andric     return new HLASMAsmParser(SM, C, Out, MAI, CB);
6438fe6060f1SDimitry Andric 
64390b57cec5SDimitry Andric   return new AsmParser(SM, C, Out, MAI, CB);
64400b57cec5SDimitry Andric }
6441