xref: /freebsd/contrib/llvm-project/llvm/lib/MC/MCParser/AsmParser.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
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 //
9*5ffd83dbSDimitry 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/None.h"
170b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.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/MCObjectFileInfo.h"
360b57cec5SDimitry Andric #include "llvm/MC/MCParser/AsmCond.h"
370b57cec5SDimitry Andric #include "llvm/MC/MCParser/AsmLexer.h"
380b57cec5SDimitry Andric #include "llvm/MC/MCParser/MCAsmLexer.h"
390b57cec5SDimitry Andric #include "llvm/MC/MCParser/MCAsmParser.h"
400b57cec5SDimitry Andric #include "llvm/MC/MCParser/MCAsmParserExtension.h"
410b57cec5SDimitry Andric #include "llvm/MC/MCParser/MCAsmParserUtils.h"
420b57cec5SDimitry Andric #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
430b57cec5SDimitry Andric #include "llvm/MC/MCParser/MCTargetAsmParser.h"
440b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
450b57cec5SDimitry Andric #include "llvm/MC/MCSection.h"
460b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
470b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h"
480b57cec5SDimitry Andric #include "llvm/MC/MCTargetOptions.h"
490b57cec5SDimitry Andric #include "llvm/MC/MCValue.h"
500b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
510b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
520b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
530b57cec5SDimitry Andric #include "llvm/Support/MD5.h"
540b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
550b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
560b57cec5SDimitry Andric #include "llvm/Support/SMLoc.h"
570b57cec5SDimitry Andric #include "llvm/Support/SourceMgr.h"
580b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
590b57cec5SDimitry Andric #include <algorithm>
600b57cec5SDimitry Andric #include <cassert>
610b57cec5SDimitry Andric #include <cctype>
620b57cec5SDimitry Andric #include <climits>
630b57cec5SDimitry Andric #include <cstddef>
640b57cec5SDimitry Andric #include <cstdint>
650b57cec5SDimitry Andric #include <deque>
660b57cec5SDimitry Andric #include <memory>
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 
77*5ffd83dbSDimitry Andric extern cl::opt<unsigned> AsmMacroMaxNestingDepth;
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric namespace {
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric /// Helper types for tracking macro definitions.
820b57cec5SDimitry Andric typedef std::vector<AsmToken> MCAsmMacroArgument;
830b57cec5SDimitry Andric typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric /// Helper class for storing information about an active macro
860b57cec5SDimitry Andric /// instantiation.
870b57cec5SDimitry Andric struct MacroInstantiation {
880b57cec5SDimitry Andric   /// The location of the instantiation.
890b57cec5SDimitry Andric   SMLoc InstantiationLoc;
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric   /// The buffer where parsing should resume upon instantiation completion.
92480093f4SDimitry Andric   unsigned ExitBuffer;
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric   /// The location where parsing should resume upon instantiation completion.
950b57cec5SDimitry Andric   SMLoc ExitLoc;
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric   /// The depth of TheCondStack at the start of the instantiation.
980b57cec5SDimitry Andric   size_t CondStackDepth;
990b57cec5SDimitry Andric };
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric struct ParseStatementInfo {
1020b57cec5SDimitry Andric   /// The parsed operands from the last parsed statement.
1030b57cec5SDimitry Andric   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric   /// The opcode from the last parsed instruction.
1060b57cec5SDimitry Andric   unsigned Opcode = ~0U;
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   /// Was there an error parsing the inline assembly?
1090b57cec5SDimitry Andric   bool ParseError = false;
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric   SmallVectorImpl<AsmRewrite> *AsmRewrites = nullptr;
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric   ParseStatementInfo() = delete;
1140b57cec5SDimitry Andric   ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
1150b57cec5SDimitry Andric     : AsmRewrites(rewrites) {}
1160b57cec5SDimitry Andric };
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric /// The concrete assembly parser instance.
1190b57cec5SDimitry Andric class AsmParser : public MCAsmParser {
1200b57cec5SDimitry Andric private:
1210b57cec5SDimitry Andric   AsmLexer Lexer;
1220b57cec5SDimitry Andric   MCContext &Ctx;
1230b57cec5SDimitry Andric   MCStreamer &Out;
1240b57cec5SDimitry Andric   const MCAsmInfo &MAI;
1250b57cec5SDimitry Andric   SourceMgr &SrcMgr;
1260b57cec5SDimitry Andric   SourceMgr::DiagHandlerTy SavedDiagHandler;
1270b57cec5SDimitry Andric   void *SavedDiagContext;
1280b57cec5SDimitry Andric   std::unique_ptr<MCAsmParserExtension> PlatformParser;
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric   /// This is the current buffer index we're lexing from as managed by the
1310b57cec5SDimitry Andric   /// SourceMgr object.
1320b57cec5SDimitry Andric   unsigned CurBuffer;
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric   AsmCond TheCondState;
1350b57cec5SDimitry Andric   std::vector<AsmCond> TheCondStack;
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric   /// maps directive names to handler methods in parser
1380b57cec5SDimitry Andric   /// extensions. Extensions register themselves in this map by calling
1390b57cec5SDimitry Andric   /// addDirectiveHandler.
1400b57cec5SDimitry Andric   StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric   /// Stack of active macro instantiations.
1430b57cec5SDimitry Andric   std::vector<MacroInstantiation*> ActiveMacros;
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric   /// List of bodies of anonymous macros.
1460b57cec5SDimitry Andric   std::deque<MCAsmMacro> MacroLikeBodies;
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric   /// Boolean tracking whether macro substitution is enabled.
1490b57cec5SDimitry Andric   unsigned MacrosEnabledFlag : 1;
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric   /// Keeps track of how many .macro's have been instantiated.
1520b57cec5SDimitry Andric   unsigned NumOfMacroInstantiations;
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric   /// The values from the last parsed cpp hash file line comment if any.
1550b57cec5SDimitry Andric   struct CppHashInfoTy {
1560b57cec5SDimitry Andric     StringRef Filename;
1570b57cec5SDimitry Andric     int64_t LineNumber;
1580b57cec5SDimitry Andric     SMLoc Loc;
1590b57cec5SDimitry Andric     unsigned Buf;
1600b57cec5SDimitry Andric     CppHashInfoTy() : Filename(), LineNumber(0), Loc(), Buf(0) {}
1610b57cec5SDimitry Andric   };
1620b57cec5SDimitry Andric   CppHashInfoTy CppHashInfo;
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric   /// The filename from the first cpp hash file line comment, if any.
1650b57cec5SDimitry Andric   StringRef FirstCppHashFilename;
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric   /// List of forward directional labels for diagnosis at the end.
1680b57cec5SDimitry Andric   SmallVector<std::tuple<SMLoc, CppHashInfoTy, MCSymbol *>, 4> DirLabels;
1690b57cec5SDimitry Andric 
1700b57cec5SDimitry Andric   /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
1710b57cec5SDimitry Andric   unsigned AssemblerDialect = ~0U;
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric   /// is Darwin compatibility enabled?
1740b57cec5SDimitry Andric   bool IsDarwin = false;
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric   /// Are we parsing ms-style inline assembly?
177*5ffd83dbSDimitry Andric   bool ParsingMSInlineAsm = false;
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric   /// Did we already inform the user about inconsistent MD5 usage?
1800b57cec5SDimitry Andric   bool ReportedInconsistentMD5 = false;
1810b57cec5SDimitry Andric 
1820b57cec5SDimitry Andric   // Is alt macro mode enabled.
1830b57cec5SDimitry Andric   bool AltMacroMode = false;
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric public:
1860b57cec5SDimitry Andric   AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
1870b57cec5SDimitry Andric             const MCAsmInfo &MAI, unsigned CB);
1880b57cec5SDimitry Andric   AsmParser(const AsmParser &) = delete;
1890b57cec5SDimitry Andric   AsmParser &operator=(const AsmParser &) = delete;
1900b57cec5SDimitry Andric   ~AsmParser() override;
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric   bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric   void addDirectiveHandler(StringRef Directive,
1950b57cec5SDimitry Andric                            ExtensionDirectiveHandler Handler) override {
1960b57cec5SDimitry Andric     ExtensionDirectiveMap[Directive] = Handler;
1970b57cec5SDimitry Andric   }
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric   void addAliasForDirective(StringRef Directive, StringRef Alias) override {
200*5ffd83dbSDimitry Andric     DirectiveKindMap[Directive.lower()] = DirectiveKindMap[Alias.lower()];
2010b57cec5SDimitry Andric   }
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric   /// @name MCAsmParser Interface
2040b57cec5SDimitry Andric   /// {
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric   SourceMgr &getSourceManager() override { return SrcMgr; }
2070b57cec5SDimitry Andric   MCAsmLexer &getLexer() override { return Lexer; }
2080b57cec5SDimitry Andric   MCContext &getContext() override { return Ctx; }
2090b57cec5SDimitry Andric   MCStreamer &getStreamer() override { return Out; }
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric   CodeViewContext &getCVContext() { return Ctx.getCVContext(); }
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric   unsigned getAssemblerDialect() override {
2140b57cec5SDimitry Andric     if (AssemblerDialect == ~0U)
2150b57cec5SDimitry Andric       return MAI.getAssemblerDialect();
2160b57cec5SDimitry Andric     else
2170b57cec5SDimitry Andric       return AssemblerDialect;
2180b57cec5SDimitry Andric   }
2190b57cec5SDimitry Andric   void setAssemblerDialect(unsigned i) override {
2200b57cec5SDimitry Andric     AssemblerDialect = i;
2210b57cec5SDimitry Andric   }
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric   void Note(SMLoc L, const Twine &Msg, SMRange Range = None) override;
2240b57cec5SDimitry Andric   bool Warning(SMLoc L, const Twine &Msg, SMRange Range = None) override;
2250b57cec5SDimitry Andric   bool printError(SMLoc L, const Twine &Msg, SMRange Range = None) override;
2260b57cec5SDimitry Andric 
2270b57cec5SDimitry Andric   const AsmToken &Lex() override;
2280b57cec5SDimitry Andric 
229*5ffd83dbSDimitry Andric   void setParsingMSInlineAsm(bool V) override {
230*5ffd83dbSDimitry Andric     ParsingMSInlineAsm = V;
2310b57cec5SDimitry Andric     // When parsing MS inline asm, we must lex 0b1101 and 0ABCH as binary and
2320b57cec5SDimitry Andric     // hex integer literals.
2330b57cec5SDimitry Andric     Lexer.setLexMasmIntegers(V);
2340b57cec5SDimitry Andric   }
235*5ffd83dbSDimitry Andric   bool isParsingMSInlineAsm() override { return ParsingMSInlineAsm; }
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric   bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
2380b57cec5SDimitry Andric                         unsigned &NumOutputs, unsigned &NumInputs,
2390b57cec5SDimitry Andric                         SmallVectorImpl<std::pair<void *,bool>> &OpDecls,
2400b57cec5SDimitry Andric                         SmallVectorImpl<std::string> &Constraints,
2410b57cec5SDimitry Andric                         SmallVectorImpl<std::string> &Clobbers,
2420b57cec5SDimitry Andric                         const MCInstrInfo *MII, const MCInstPrinter *IP,
2430b57cec5SDimitry Andric                         MCAsmParserSemaCallback &SI) override;
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric   bool parseExpression(const MCExpr *&Res);
2460b57cec5SDimitry Andric   bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
2470b57cec5SDimitry Andric   bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
2480b57cec5SDimitry Andric   bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
2490b57cec5SDimitry Andric   bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
2500b57cec5SDimitry Andric                              SMLoc &EndLoc) override;
2510b57cec5SDimitry Andric   bool parseAbsoluteExpression(int64_t &Res) override;
2520b57cec5SDimitry Andric 
2530b57cec5SDimitry Andric   /// Parse a floating point expression using the float \p Semantics
2540b57cec5SDimitry Andric   /// and set \p Res to the value.
2550b57cec5SDimitry Andric   bool parseRealValue(const fltSemantics &Semantics, APInt &Res);
2560b57cec5SDimitry Andric 
2570b57cec5SDimitry Andric   /// Parse an identifier or string (as a quoted identifier)
2580b57cec5SDimitry Andric   /// and set \p Res to the identifier contents.
2590b57cec5SDimitry Andric   bool parseIdentifier(StringRef &Res) override;
2600b57cec5SDimitry Andric   void eatToEndOfStatement() override;
2610b57cec5SDimitry Andric 
2620b57cec5SDimitry Andric   bool checkForValidSection() override;
2630b57cec5SDimitry Andric 
2640b57cec5SDimitry Andric   /// }
2650b57cec5SDimitry Andric 
2660b57cec5SDimitry Andric private:
2670b57cec5SDimitry Andric   bool parseStatement(ParseStatementInfo &Info,
2680b57cec5SDimitry Andric                       MCAsmParserSemaCallback *SI);
2690b57cec5SDimitry Andric   bool parseCurlyBlockScope(SmallVectorImpl<AsmRewrite>& AsmStrRewrites);
270*5ffd83dbSDimitry Andric   bool parseCppHashLineFilenameComment(SMLoc L, bool SaveLocInfo = true);
2710b57cec5SDimitry Andric 
2720b57cec5SDimitry Andric   void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
2730b57cec5SDimitry Andric                         ArrayRef<MCAsmMacroParameter> Parameters);
2740b57cec5SDimitry Andric   bool expandMacro(raw_svector_ostream &OS, StringRef Body,
2750b57cec5SDimitry Andric                    ArrayRef<MCAsmMacroParameter> Parameters,
2760b57cec5SDimitry Andric                    ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable,
2770b57cec5SDimitry Andric                    SMLoc L);
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric   /// Are macros enabled in the parser?
2800b57cec5SDimitry Andric   bool areMacrosEnabled() {return MacrosEnabledFlag;}
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric   /// Control a flag in the parser that enables or disables macros.
2830b57cec5SDimitry Andric   void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric   /// Are we inside a macro instantiation?
2860b57cec5SDimitry Andric   bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
2870b57cec5SDimitry Andric 
2880b57cec5SDimitry Andric   /// Handle entry to macro instantiation.
2890b57cec5SDimitry Andric   ///
2900b57cec5SDimitry Andric   /// \param M The macro.
2910b57cec5SDimitry Andric   /// \param NameLoc Instantiation location.
2920b57cec5SDimitry Andric   bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
2930b57cec5SDimitry Andric 
2940b57cec5SDimitry Andric   /// Handle exit from macro instantiation.
2950b57cec5SDimitry Andric   void handleMacroExit();
2960b57cec5SDimitry Andric 
2970b57cec5SDimitry Andric   /// Extract AsmTokens for a macro argument.
2980b57cec5SDimitry Andric   bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
2990b57cec5SDimitry Andric 
3000b57cec5SDimitry Andric   /// Parse all macro arguments for a given macro.
3010b57cec5SDimitry Andric   bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric   void printMacroInstantiations();
3040b57cec5SDimitry Andric   void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
3050b57cec5SDimitry Andric                     SMRange Range = None) const {
3060b57cec5SDimitry Andric     ArrayRef<SMRange> Ranges(Range);
3070b57cec5SDimitry Andric     SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
3080b57cec5SDimitry Andric   }
3090b57cec5SDimitry Andric   static void DiagHandler(const SMDiagnostic &Diag, void *Context);
3100b57cec5SDimitry Andric 
3110b57cec5SDimitry Andric   /// Should we emit DWARF describing this assembler source?  (Returns false if
3120b57cec5SDimitry Andric   /// the source has .file directives, which means we don't want to generate
3130b57cec5SDimitry Andric   /// info describing the assembler source itself.)
3140b57cec5SDimitry Andric   bool enabledGenDwarfForAssembly();
3150b57cec5SDimitry Andric 
3160b57cec5SDimitry Andric   /// Enter the specified file. This returns true on failure.
3170b57cec5SDimitry Andric   bool enterIncludeFile(const std::string &Filename);
3180b57cec5SDimitry Andric 
3190b57cec5SDimitry Andric   /// Process the specified file for the .incbin directive.
3200b57cec5SDimitry Andric   /// This returns true on failure.
3210b57cec5SDimitry Andric   bool processIncbinFile(const std::string &Filename, int64_t Skip = 0,
3220b57cec5SDimitry Andric                          const MCExpr *Count = nullptr, SMLoc Loc = SMLoc());
3230b57cec5SDimitry Andric 
3240b57cec5SDimitry Andric   /// Reset the current lexer position to that given by \p Loc. The
3250b57cec5SDimitry Andric   /// current token is not set; clients should ensure Lex() is called
3260b57cec5SDimitry Andric   /// subsequently.
3270b57cec5SDimitry Andric   ///
3280b57cec5SDimitry Andric   /// \param InBuffer If not 0, should be the known buffer id that contains the
3290b57cec5SDimitry Andric   /// location.
3300b57cec5SDimitry Andric   void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
3310b57cec5SDimitry Andric 
3320b57cec5SDimitry Andric   /// Parse up to the end of statement and a return the contents from the
3330b57cec5SDimitry Andric   /// current token until the end of the statement; the current token on exit
3340b57cec5SDimitry Andric   /// will be either the EndOfStatement or EOF.
3350b57cec5SDimitry Andric   StringRef parseStringToEndOfStatement() override;
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric   /// Parse until the end of a statement or a comma is encountered,
3380b57cec5SDimitry Andric   /// return the contents from the current token up to the end or comma.
3390b57cec5SDimitry Andric   StringRef parseStringToComma();
3400b57cec5SDimitry Andric 
3410b57cec5SDimitry Andric   bool parseAssignment(StringRef Name, bool allow_redef,
3420b57cec5SDimitry Andric                        bool NoDeadStrip = false);
3430b57cec5SDimitry Andric 
3440b57cec5SDimitry Andric   unsigned getBinOpPrecedence(AsmToken::TokenKind K,
3450b57cec5SDimitry Andric                               MCBinaryExpr::Opcode &Kind);
3460b57cec5SDimitry Andric 
3470b57cec5SDimitry Andric   bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
3480b57cec5SDimitry Andric   bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
3490b57cec5SDimitry Andric   bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
3500b57cec5SDimitry Andric 
3510b57cec5SDimitry Andric   bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
3520b57cec5SDimitry Andric 
3530b57cec5SDimitry Andric   bool parseCVFunctionId(int64_t &FunctionId, StringRef DirectiveName);
3540b57cec5SDimitry Andric   bool parseCVFileId(int64_t &FileId, StringRef DirectiveName);
3550b57cec5SDimitry Andric 
3560b57cec5SDimitry Andric   // Generic (target and platform independent) directive parsing.
3570b57cec5SDimitry Andric   enum DirectiveKind {
3580b57cec5SDimitry Andric     DK_NO_DIRECTIVE, // Placeholder
3590b57cec5SDimitry Andric     DK_SET,
3600b57cec5SDimitry Andric     DK_EQU,
3610b57cec5SDimitry Andric     DK_EQUIV,
3620b57cec5SDimitry Andric     DK_ASCII,
3630b57cec5SDimitry Andric     DK_ASCIZ,
3640b57cec5SDimitry Andric     DK_STRING,
3650b57cec5SDimitry Andric     DK_BYTE,
3660b57cec5SDimitry Andric     DK_SHORT,
3670b57cec5SDimitry Andric     DK_RELOC,
3680b57cec5SDimitry Andric     DK_VALUE,
3690b57cec5SDimitry Andric     DK_2BYTE,
3700b57cec5SDimitry Andric     DK_LONG,
3710b57cec5SDimitry Andric     DK_INT,
3720b57cec5SDimitry Andric     DK_4BYTE,
3730b57cec5SDimitry Andric     DK_QUAD,
3740b57cec5SDimitry Andric     DK_8BYTE,
3750b57cec5SDimitry Andric     DK_OCTA,
3760b57cec5SDimitry Andric     DK_DC,
3770b57cec5SDimitry Andric     DK_DC_A,
3780b57cec5SDimitry Andric     DK_DC_B,
3790b57cec5SDimitry Andric     DK_DC_D,
3800b57cec5SDimitry Andric     DK_DC_L,
3810b57cec5SDimitry Andric     DK_DC_S,
3820b57cec5SDimitry Andric     DK_DC_W,
3830b57cec5SDimitry Andric     DK_DC_X,
3840b57cec5SDimitry Andric     DK_DCB,
3850b57cec5SDimitry Andric     DK_DCB_B,
3860b57cec5SDimitry Andric     DK_DCB_D,
3870b57cec5SDimitry Andric     DK_DCB_L,
3880b57cec5SDimitry Andric     DK_DCB_S,
3890b57cec5SDimitry Andric     DK_DCB_W,
3900b57cec5SDimitry Andric     DK_DCB_X,
3910b57cec5SDimitry Andric     DK_DS,
3920b57cec5SDimitry Andric     DK_DS_B,
3930b57cec5SDimitry Andric     DK_DS_D,
3940b57cec5SDimitry Andric     DK_DS_L,
3950b57cec5SDimitry Andric     DK_DS_P,
3960b57cec5SDimitry Andric     DK_DS_S,
3970b57cec5SDimitry Andric     DK_DS_W,
3980b57cec5SDimitry Andric     DK_DS_X,
3990b57cec5SDimitry Andric     DK_SINGLE,
4000b57cec5SDimitry Andric     DK_FLOAT,
4010b57cec5SDimitry Andric     DK_DOUBLE,
4020b57cec5SDimitry Andric     DK_ALIGN,
4030b57cec5SDimitry Andric     DK_ALIGN32,
4040b57cec5SDimitry Andric     DK_BALIGN,
4050b57cec5SDimitry Andric     DK_BALIGNW,
4060b57cec5SDimitry Andric     DK_BALIGNL,
4070b57cec5SDimitry Andric     DK_P2ALIGN,
4080b57cec5SDimitry Andric     DK_P2ALIGNW,
4090b57cec5SDimitry Andric     DK_P2ALIGNL,
4100b57cec5SDimitry Andric     DK_ORG,
4110b57cec5SDimitry Andric     DK_FILL,
4120b57cec5SDimitry Andric     DK_ENDR,
4130b57cec5SDimitry Andric     DK_BUNDLE_ALIGN_MODE,
4140b57cec5SDimitry Andric     DK_BUNDLE_LOCK,
4150b57cec5SDimitry Andric     DK_BUNDLE_UNLOCK,
4160b57cec5SDimitry Andric     DK_ZERO,
4170b57cec5SDimitry Andric     DK_EXTERN,
4180b57cec5SDimitry Andric     DK_GLOBL,
4190b57cec5SDimitry Andric     DK_GLOBAL,
4200b57cec5SDimitry Andric     DK_LAZY_REFERENCE,
4210b57cec5SDimitry Andric     DK_NO_DEAD_STRIP,
4220b57cec5SDimitry Andric     DK_SYMBOL_RESOLVER,
4230b57cec5SDimitry Andric     DK_PRIVATE_EXTERN,
4240b57cec5SDimitry Andric     DK_REFERENCE,
4250b57cec5SDimitry Andric     DK_WEAK_DEFINITION,
4260b57cec5SDimitry Andric     DK_WEAK_REFERENCE,
4270b57cec5SDimitry Andric     DK_WEAK_DEF_CAN_BE_HIDDEN,
4280b57cec5SDimitry Andric     DK_COLD,
4290b57cec5SDimitry Andric     DK_COMM,
4300b57cec5SDimitry Andric     DK_COMMON,
4310b57cec5SDimitry Andric     DK_LCOMM,
4320b57cec5SDimitry Andric     DK_ABORT,
4330b57cec5SDimitry Andric     DK_INCLUDE,
4340b57cec5SDimitry Andric     DK_INCBIN,
4350b57cec5SDimitry Andric     DK_CODE16,
4360b57cec5SDimitry Andric     DK_CODE16GCC,
4370b57cec5SDimitry Andric     DK_REPT,
4380b57cec5SDimitry Andric     DK_IRP,
4390b57cec5SDimitry Andric     DK_IRPC,
4400b57cec5SDimitry Andric     DK_IF,
4410b57cec5SDimitry Andric     DK_IFEQ,
4420b57cec5SDimitry Andric     DK_IFGE,
4430b57cec5SDimitry Andric     DK_IFGT,
4440b57cec5SDimitry Andric     DK_IFLE,
4450b57cec5SDimitry Andric     DK_IFLT,
4460b57cec5SDimitry Andric     DK_IFNE,
4470b57cec5SDimitry Andric     DK_IFB,
4480b57cec5SDimitry Andric     DK_IFNB,
4490b57cec5SDimitry Andric     DK_IFC,
4500b57cec5SDimitry Andric     DK_IFEQS,
4510b57cec5SDimitry Andric     DK_IFNC,
4520b57cec5SDimitry Andric     DK_IFNES,
4530b57cec5SDimitry Andric     DK_IFDEF,
4540b57cec5SDimitry Andric     DK_IFNDEF,
4550b57cec5SDimitry Andric     DK_IFNOTDEF,
4560b57cec5SDimitry Andric     DK_ELSEIF,
4570b57cec5SDimitry Andric     DK_ELSE,
4580b57cec5SDimitry Andric     DK_ENDIF,
4590b57cec5SDimitry Andric     DK_SPACE,
4600b57cec5SDimitry Andric     DK_SKIP,
4610b57cec5SDimitry Andric     DK_FILE,
4620b57cec5SDimitry Andric     DK_LINE,
4630b57cec5SDimitry Andric     DK_LOC,
4640b57cec5SDimitry Andric     DK_STABS,
4650b57cec5SDimitry Andric     DK_CV_FILE,
4660b57cec5SDimitry Andric     DK_CV_FUNC_ID,
4670b57cec5SDimitry Andric     DK_CV_INLINE_SITE_ID,
4680b57cec5SDimitry Andric     DK_CV_LOC,
4690b57cec5SDimitry Andric     DK_CV_LINETABLE,
4700b57cec5SDimitry Andric     DK_CV_INLINE_LINETABLE,
4710b57cec5SDimitry Andric     DK_CV_DEF_RANGE,
4720b57cec5SDimitry Andric     DK_CV_STRINGTABLE,
4730b57cec5SDimitry Andric     DK_CV_STRING,
4740b57cec5SDimitry Andric     DK_CV_FILECHECKSUMS,
4750b57cec5SDimitry Andric     DK_CV_FILECHECKSUM_OFFSET,
4760b57cec5SDimitry Andric     DK_CV_FPO_DATA,
4770b57cec5SDimitry Andric     DK_CFI_SECTIONS,
4780b57cec5SDimitry Andric     DK_CFI_STARTPROC,
4790b57cec5SDimitry Andric     DK_CFI_ENDPROC,
4800b57cec5SDimitry Andric     DK_CFI_DEF_CFA,
4810b57cec5SDimitry Andric     DK_CFI_DEF_CFA_OFFSET,
4820b57cec5SDimitry Andric     DK_CFI_ADJUST_CFA_OFFSET,
4830b57cec5SDimitry Andric     DK_CFI_DEF_CFA_REGISTER,
4840b57cec5SDimitry Andric     DK_CFI_OFFSET,
4850b57cec5SDimitry Andric     DK_CFI_REL_OFFSET,
4860b57cec5SDimitry Andric     DK_CFI_PERSONALITY,
4870b57cec5SDimitry Andric     DK_CFI_LSDA,
4880b57cec5SDimitry Andric     DK_CFI_REMEMBER_STATE,
4890b57cec5SDimitry Andric     DK_CFI_RESTORE_STATE,
4900b57cec5SDimitry Andric     DK_CFI_SAME_VALUE,
4910b57cec5SDimitry Andric     DK_CFI_RESTORE,
4920b57cec5SDimitry Andric     DK_CFI_ESCAPE,
4930b57cec5SDimitry Andric     DK_CFI_RETURN_COLUMN,
4940b57cec5SDimitry Andric     DK_CFI_SIGNAL_FRAME,
4950b57cec5SDimitry Andric     DK_CFI_UNDEFINED,
4960b57cec5SDimitry Andric     DK_CFI_REGISTER,
4970b57cec5SDimitry Andric     DK_CFI_WINDOW_SAVE,
4980b57cec5SDimitry Andric     DK_CFI_B_KEY_FRAME,
4990b57cec5SDimitry Andric     DK_MACROS_ON,
5000b57cec5SDimitry Andric     DK_MACROS_OFF,
5010b57cec5SDimitry Andric     DK_ALTMACRO,
5020b57cec5SDimitry Andric     DK_NOALTMACRO,
5030b57cec5SDimitry Andric     DK_MACRO,
5040b57cec5SDimitry Andric     DK_EXITM,
5050b57cec5SDimitry Andric     DK_ENDM,
5060b57cec5SDimitry Andric     DK_ENDMACRO,
5070b57cec5SDimitry Andric     DK_PURGEM,
5080b57cec5SDimitry Andric     DK_SLEB128,
5090b57cec5SDimitry Andric     DK_ULEB128,
5100b57cec5SDimitry Andric     DK_ERR,
5110b57cec5SDimitry Andric     DK_ERROR,
5120b57cec5SDimitry Andric     DK_WARNING,
5130b57cec5SDimitry Andric     DK_PRINT,
5140b57cec5SDimitry Andric     DK_ADDRSIG,
5150b57cec5SDimitry Andric     DK_ADDRSIG_SYM,
5160b57cec5SDimitry Andric     DK_END
5170b57cec5SDimitry Andric   };
5180b57cec5SDimitry Andric 
5190b57cec5SDimitry Andric   /// Maps directive name --> DirectiveKind enum, for
5200b57cec5SDimitry Andric   /// directives parsed by this class.
5210b57cec5SDimitry Andric   StringMap<DirectiveKind> DirectiveKindMap;
5220b57cec5SDimitry Andric 
5238bcb0991SDimitry Andric   // Codeview def_range type parsing.
5248bcb0991SDimitry Andric   enum CVDefRangeType {
5258bcb0991SDimitry Andric     CVDR_DEFRANGE = 0, // Placeholder
5268bcb0991SDimitry Andric     CVDR_DEFRANGE_REGISTER,
5278bcb0991SDimitry Andric     CVDR_DEFRANGE_FRAMEPOINTER_REL,
5288bcb0991SDimitry Andric     CVDR_DEFRANGE_SUBFIELD_REGISTER,
5298bcb0991SDimitry Andric     CVDR_DEFRANGE_REGISTER_REL
5308bcb0991SDimitry Andric   };
5318bcb0991SDimitry Andric 
5328bcb0991SDimitry Andric   /// Maps Codeview def_range types --> CVDefRangeType enum, for
5338bcb0991SDimitry Andric   /// Codeview def_range types parsed by this class.
5348bcb0991SDimitry Andric   StringMap<CVDefRangeType> CVDefRangeTypeMap;
5358bcb0991SDimitry Andric 
5360b57cec5SDimitry Andric   // ".ascii", ".asciz", ".string"
5370b57cec5SDimitry Andric   bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
5380b57cec5SDimitry Andric   bool parseDirectiveReloc(SMLoc DirectiveLoc); // ".reloc"
5390b57cec5SDimitry Andric   bool parseDirectiveValue(StringRef IDVal,
5400b57cec5SDimitry Andric                            unsigned Size);       // ".byte", ".long", ...
5410b57cec5SDimitry Andric   bool parseDirectiveOctaValue(StringRef IDVal); // ".octa", ...
5420b57cec5SDimitry Andric   bool parseDirectiveRealValue(StringRef IDVal,
5430b57cec5SDimitry Andric                                const fltSemantics &); // ".single", ...
5440b57cec5SDimitry Andric   bool parseDirectiveFill(); // ".fill"
5450b57cec5SDimitry Andric   bool parseDirectiveZero(); // ".zero"
5460b57cec5SDimitry Andric   // ".set", ".equ", ".equiv"
5470b57cec5SDimitry Andric   bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
5480b57cec5SDimitry Andric   bool parseDirectiveOrg(); // ".org"
5490b57cec5SDimitry Andric   // ".align{,32}", ".p2align{,w,l}"
5500b57cec5SDimitry Andric   bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
5510b57cec5SDimitry Andric 
5520b57cec5SDimitry Andric   // ".file", ".line", ".loc", ".stabs"
5530b57cec5SDimitry Andric   bool parseDirectiveFile(SMLoc DirectiveLoc);
5540b57cec5SDimitry Andric   bool parseDirectiveLine();
5550b57cec5SDimitry Andric   bool parseDirectiveLoc();
5560b57cec5SDimitry Andric   bool parseDirectiveStabs();
5570b57cec5SDimitry Andric 
5580b57cec5SDimitry Andric   // ".cv_file", ".cv_func_id", ".cv_inline_site_id", ".cv_loc", ".cv_linetable",
5590b57cec5SDimitry Andric   // ".cv_inline_linetable", ".cv_def_range", ".cv_string"
5600b57cec5SDimitry Andric   bool parseDirectiveCVFile();
5610b57cec5SDimitry Andric   bool parseDirectiveCVFuncId();
5620b57cec5SDimitry Andric   bool parseDirectiveCVInlineSiteId();
5630b57cec5SDimitry Andric   bool parseDirectiveCVLoc();
5640b57cec5SDimitry Andric   bool parseDirectiveCVLinetable();
5650b57cec5SDimitry Andric   bool parseDirectiveCVInlineLinetable();
5660b57cec5SDimitry Andric   bool parseDirectiveCVDefRange();
5670b57cec5SDimitry Andric   bool parseDirectiveCVString();
5680b57cec5SDimitry Andric   bool parseDirectiveCVStringTable();
5690b57cec5SDimitry Andric   bool parseDirectiveCVFileChecksums();
5700b57cec5SDimitry Andric   bool parseDirectiveCVFileChecksumOffset();
5710b57cec5SDimitry Andric   bool parseDirectiveCVFPOData();
5720b57cec5SDimitry Andric 
5730b57cec5SDimitry Andric   // .cfi directives
5740b57cec5SDimitry Andric   bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
5750b57cec5SDimitry Andric   bool parseDirectiveCFIWindowSave();
5760b57cec5SDimitry Andric   bool parseDirectiveCFISections();
5770b57cec5SDimitry Andric   bool parseDirectiveCFIStartProc();
5780b57cec5SDimitry Andric   bool parseDirectiveCFIEndProc();
5790b57cec5SDimitry Andric   bool parseDirectiveCFIDefCfaOffset();
5800b57cec5SDimitry Andric   bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
5810b57cec5SDimitry Andric   bool parseDirectiveCFIAdjustCfaOffset();
5820b57cec5SDimitry Andric   bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
5830b57cec5SDimitry Andric   bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
5840b57cec5SDimitry Andric   bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
5850b57cec5SDimitry Andric   bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
5860b57cec5SDimitry Andric   bool parseDirectiveCFIRememberState();
5870b57cec5SDimitry Andric   bool parseDirectiveCFIRestoreState();
5880b57cec5SDimitry Andric   bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
5890b57cec5SDimitry Andric   bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
5900b57cec5SDimitry Andric   bool parseDirectiveCFIEscape();
5910b57cec5SDimitry Andric   bool parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc);
5920b57cec5SDimitry Andric   bool parseDirectiveCFISignalFrame();
5930b57cec5SDimitry Andric   bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
5940b57cec5SDimitry Andric 
5950b57cec5SDimitry Andric   // macro directives
5960b57cec5SDimitry Andric   bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
5970b57cec5SDimitry Andric   bool parseDirectiveExitMacro(StringRef Directive);
5980b57cec5SDimitry Andric   bool parseDirectiveEndMacro(StringRef Directive);
5990b57cec5SDimitry Andric   bool parseDirectiveMacro(SMLoc DirectiveLoc);
6000b57cec5SDimitry Andric   bool parseDirectiveMacrosOnOff(StringRef Directive);
6010b57cec5SDimitry Andric   // alternate macro mode directives
6020b57cec5SDimitry Andric   bool parseDirectiveAltmacro(StringRef Directive);
6030b57cec5SDimitry Andric   // ".bundle_align_mode"
6040b57cec5SDimitry Andric   bool parseDirectiveBundleAlignMode();
6050b57cec5SDimitry Andric   // ".bundle_lock"
6060b57cec5SDimitry Andric   bool parseDirectiveBundleLock();
6070b57cec5SDimitry Andric   // ".bundle_unlock"
6080b57cec5SDimitry Andric   bool parseDirectiveBundleUnlock();
6090b57cec5SDimitry Andric 
6100b57cec5SDimitry Andric   // ".space", ".skip"
6110b57cec5SDimitry Andric   bool parseDirectiveSpace(StringRef IDVal);
6120b57cec5SDimitry Andric 
6130b57cec5SDimitry Andric   // ".dcb"
6140b57cec5SDimitry Andric   bool parseDirectiveDCB(StringRef IDVal, unsigned Size);
6150b57cec5SDimitry Andric   bool parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &);
6160b57cec5SDimitry Andric   // ".ds"
6170b57cec5SDimitry Andric   bool parseDirectiveDS(StringRef IDVal, unsigned Size);
6180b57cec5SDimitry Andric 
6190b57cec5SDimitry Andric   // .sleb128 (Signed=true) and .uleb128 (Signed=false)
6200b57cec5SDimitry Andric   bool parseDirectiveLEB128(bool Signed);
6210b57cec5SDimitry Andric 
6220b57cec5SDimitry Andric   /// Parse a directive like ".globl" which
6230b57cec5SDimitry Andric   /// accepts a single symbol (which should be a label or an external).
6240b57cec5SDimitry Andric   bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
6250b57cec5SDimitry Andric 
6260b57cec5SDimitry Andric   bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
6270b57cec5SDimitry Andric 
6280b57cec5SDimitry Andric   bool parseDirectiveAbort(); // ".abort"
6290b57cec5SDimitry Andric   bool parseDirectiveInclude(); // ".include"
6300b57cec5SDimitry Andric   bool parseDirectiveIncbin(); // ".incbin"
6310b57cec5SDimitry Andric 
6320b57cec5SDimitry Andric   // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
6330b57cec5SDimitry Andric   bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
6340b57cec5SDimitry Andric   // ".ifb" or ".ifnb", depending on ExpectBlank.
6350b57cec5SDimitry Andric   bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
6360b57cec5SDimitry Andric   // ".ifc" or ".ifnc", depending on ExpectEqual.
6370b57cec5SDimitry Andric   bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
6380b57cec5SDimitry Andric   // ".ifeqs" or ".ifnes", depending on ExpectEqual.
6390b57cec5SDimitry Andric   bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual);
6400b57cec5SDimitry Andric   // ".ifdef" or ".ifndef", depending on expect_defined
6410b57cec5SDimitry Andric   bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
6420b57cec5SDimitry Andric   bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
6430b57cec5SDimitry Andric   bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
6440b57cec5SDimitry Andric   bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
6450b57cec5SDimitry Andric   bool parseEscapedString(std::string &Data) override;
646*5ffd83dbSDimitry Andric   bool parseAngleBracketString(std::string &Data) override;
6470b57cec5SDimitry Andric 
6480b57cec5SDimitry Andric   const MCExpr *applyModifierToExpr(const MCExpr *E,
6490b57cec5SDimitry Andric                                     MCSymbolRefExpr::VariantKind Variant);
6500b57cec5SDimitry Andric 
6510b57cec5SDimitry Andric   // Macro-like directives
6520b57cec5SDimitry Andric   MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
6530b57cec5SDimitry Andric   void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
6540b57cec5SDimitry Andric                                 raw_svector_ostream &OS);
6550b57cec5SDimitry Andric   bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
6560b57cec5SDimitry Andric   bool parseDirectiveIrp(SMLoc DirectiveLoc);  // ".irp"
6570b57cec5SDimitry Andric   bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
6580b57cec5SDimitry Andric   bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
6590b57cec5SDimitry Andric 
6600b57cec5SDimitry Andric   // "_emit" or "__emit"
6610b57cec5SDimitry Andric   bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
6620b57cec5SDimitry Andric                             size_t Len);
6630b57cec5SDimitry Andric 
6640b57cec5SDimitry Andric   // "align"
6650b57cec5SDimitry Andric   bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
6660b57cec5SDimitry Andric 
6670b57cec5SDimitry Andric   // "end"
6680b57cec5SDimitry Andric   bool parseDirectiveEnd(SMLoc DirectiveLoc);
6690b57cec5SDimitry Andric 
6700b57cec5SDimitry Andric   // ".err" or ".error"
6710b57cec5SDimitry Andric   bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
6720b57cec5SDimitry Andric 
6730b57cec5SDimitry Andric   // ".warning"
6740b57cec5SDimitry Andric   bool parseDirectiveWarning(SMLoc DirectiveLoc);
6750b57cec5SDimitry Andric 
6760b57cec5SDimitry Andric   // .print <double-quotes-string>
6770b57cec5SDimitry Andric   bool parseDirectivePrint(SMLoc DirectiveLoc);
6780b57cec5SDimitry Andric 
6790b57cec5SDimitry Andric   // Directives to support address-significance tables.
6800b57cec5SDimitry Andric   bool parseDirectiveAddrsig();
6810b57cec5SDimitry Andric   bool parseDirectiveAddrsigSym();
6820b57cec5SDimitry Andric 
6830b57cec5SDimitry Andric   void initializeDirectiveKindMap();
6848bcb0991SDimitry Andric   void initializeCVDefRangeTypeMap();
6850b57cec5SDimitry Andric };
6860b57cec5SDimitry Andric 
6870b57cec5SDimitry Andric } // end anonymous namespace
6880b57cec5SDimitry Andric 
6890b57cec5SDimitry Andric namespace llvm {
6900b57cec5SDimitry Andric 
6910b57cec5SDimitry Andric extern MCAsmParserExtension *createDarwinAsmParser();
6920b57cec5SDimitry Andric extern MCAsmParserExtension *createELFAsmParser();
6930b57cec5SDimitry Andric extern MCAsmParserExtension *createCOFFAsmParser();
6940b57cec5SDimitry Andric extern MCAsmParserExtension *createWasmAsmParser();
6950b57cec5SDimitry Andric 
6960b57cec5SDimitry Andric } // end namespace llvm
6970b57cec5SDimitry Andric 
6980b57cec5SDimitry Andric enum { DEFAULT_ADDRSPACE = 0 };
6990b57cec5SDimitry Andric 
7000b57cec5SDimitry Andric AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
7010b57cec5SDimitry Andric                      const MCAsmInfo &MAI, unsigned CB = 0)
7020b57cec5SDimitry Andric     : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM),
7030b57cec5SDimitry Andric       CurBuffer(CB ? CB : SM.getMainFileID()), MacrosEnabledFlag(true) {
7040b57cec5SDimitry Andric   HadError = false;
7050b57cec5SDimitry Andric   // Save the old handler.
7060b57cec5SDimitry Andric   SavedDiagHandler = SrcMgr.getDiagHandler();
7070b57cec5SDimitry Andric   SavedDiagContext = SrcMgr.getDiagContext();
7080b57cec5SDimitry Andric   // Set our own handler which calls the saved handler.
7090b57cec5SDimitry Andric   SrcMgr.setDiagHandler(DiagHandler, this);
7100b57cec5SDimitry Andric   Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
7110b57cec5SDimitry Andric 
7120b57cec5SDimitry Andric   // Initialize the platform / file format parser.
7130b57cec5SDimitry Andric   switch (Ctx.getObjectFileInfo()->getObjectFileType()) {
7140b57cec5SDimitry Andric   case MCObjectFileInfo::IsCOFF:
7150b57cec5SDimitry Andric     PlatformParser.reset(createCOFFAsmParser());
7160b57cec5SDimitry Andric     break;
7170b57cec5SDimitry Andric   case MCObjectFileInfo::IsMachO:
7180b57cec5SDimitry Andric     PlatformParser.reset(createDarwinAsmParser());
7190b57cec5SDimitry Andric     IsDarwin = true;
7200b57cec5SDimitry Andric     break;
7210b57cec5SDimitry Andric   case MCObjectFileInfo::IsELF:
7220b57cec5SDimitry Andric     PlatformParser.reset(createELFAsmParser());
7230b57cec5SDimitry Andric     break;
7240b57cec5SDimitry Andric   case MCObjectFileInfo::IsWasm:
7250b57cec5SDimitry Andric     PlatformParser.reset(createWasmAsmParser());
7260b57cec5SDimitry Andric     break;
7270b57cec5SDimitry Andric   case MCObjectFileInfo::IsXCOFF:
7288bcb0991SDimitry Andric     report_fatal_error(
7298bcb0991SDimitry Andric         "Need to implement createXCOFFAsmParser for XCOFF format.");
7300b57cec5SDimitry Andric     break;
7310b57cec5SDimitry Andric   }
7320b57cec5SDimitry Andric 
7330b57cec5SDimitry Andric   PlatformParser->Initialize(*this);
7340b57cec5SDimitry Andric   initializeDirectiveKindMap();
7358bcb0991SDimitry Andric   initializeCVDefRangeTypeMap();
7360b57cec5SDimitry Andric 
7370b57cec5SDimitry Andric   NumOfMacroInstantiations = 0;
7380b57cec5SDimitry Andric }
7390b57cec5SDimitry Andric 
7400b57cec5SDimitry Andric AsmParser::~AsmParser() {
7410b57cec5SDimitry Andric   assert((HadError || ActiveMacros.empty()) &&
7420b57cec5SDimitry Andric          "Unexpected active macro instantiation!");
7430b57cec5SDimitry Andric 
7440b57cec5SDimitry Andric   // Restore the saved diagnostics handler and context for use during
7450b57cec5SDimitry Andric   // finalization.
7460b57cec5SDimitry Andric   SrcMgr.setDiagHandler(SavedDiagHandler, SavedDiagContext);
7470b57cec5SDimitry Andric }
7480b57cec5SDimitry Andric 
7490b57cec5SDimitry Andric void AsmParser::printMacroInstantiations() {
7500b57cec5SDimitry Andric   // Print the active macro instantiation stack.
7510b57cec5SDimitry Andric   for (std::vector<MacroInstantiation *>::const_reverse_iterator
7520b57cec5SDimitry Andric            it = ActiveMacros.rbegin(),
7530b57cec5SDimitry Andric            ie = ActiveMacros.rend();
7540b57cec5SDimitry Andric        it != ie; ++it)
7550b57cec5SDimitry Andric     printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
7560b57cec5SDimitry Andric                  "while in macro instantiation");
7570b57cec5SDimitry Andric }
7580b57cec5SDimitry Andric 
7590b57cec5SDimitry Andric void AsmParser::Note(SMLoc L, const Twine &Msg, SMRange Range) {
7600b57cec5SDimitry Andric   printPendingErrors();
7610b57cec5SDimitry Andric   printMessage(L, SourceMgr::DK_Note, Msg, Range);
7620b57cec5SDimitry Andric   printMacroInstantiations();
7630b57cec5SDimitry Andric }
7640b57cec5SDimitry Andric 
7650b57cec5SDimitry Andric bool AsmParser::Warning(SMLoc L, const Twine &Msg, SMRange Range) {
7660b57cec5SDimitry Andric   if(getTargetParser().getTargetOptions().MCNoWarn)
7670b57cec5SDimitry Andric     return false;
7680b57cec5SDimitry Andric   if (getTargetParser().getTargetOptions().MCFatalWarnings)
7690b57cec5SDimitry Andric     return Error(L, Msg, Range);
7700b57cec5SDimitry Andric   printMessage(L, SourceMgr::DK_Warning, Msg, Range);
7710b57cec5SDimitry Andric   printMacroInstantiations();
7720b57cec5SDimitry Andric   return false;
7730b57cec5SDimitry Andric }
7740b57cec5SDimitry Andric 
7750b57cec5SDimitry Andric bool AsmParser::printError(SMLoc L, const Twine &Msg, SMRange Range) {
7760b57cec5SDimitry Andric   HadError = true;
7770b57cec5SDimitry Andric   printMessage(L, SourceMgr::DK_Error, Msg, Range);
7780b57cec5SDimitry Andric   printMacroInstantiations();
7790b57cec5SDimitry Andric   return true;
7800b57cec5SDimitry Andric }
7810b57cec5SDimitry Andric 
7820b57cec5SDimitry Andric bool AsmParser::enterIncludeFile(const std::string &Filename) {
7830b57cec5SDimitry Andric   std::string IncludedFile;
7840b57cec5SDimitry Andric   unsigned NewBuf =
7850b57cec5SDimitry Andric       SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
7860b57cec5SDimitry Andric   if (!NewBuf)
7870b57cec5SDimitry Andric     return true;
7880b57cec5SDimitry Andric 
7890b57cec5SDimitry Andric   CurBuffer = NewBuf;
7900b57cec5SDimitry Andric   Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
7910b57cec5SDimitry Andric   return false;
7920b57cec5SDimitry Andric }
7930b57cec5SDimitry Andric 
7940b57cec5SDimitry Andric /// Process the specified .incbin file by searching for it in the include paths
7950b57cec5SDimitry Andric /// then just emitting the byte contents of the file to the streamer. This
7960b57cec5SDimitry Andric /// returns true on failure.
7970b57cec5SDimitry Andric bool AsmParser::processIncbinFile(const std::string &Filename, int64_t Skip,
7980b57cec5SDimitry Andric                                   const MCExpr *Count, SMLoc Loc) {
7990b57cec5SDimitry Andric   std::string IncludedFile;
8000b57cec5SDimitry Andric   unsigned NewBuf =
8010b57cec5SDimitry Andric       SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
8020b57cec5SDimitry Andric   if (!NewBuf)
8030b57cec5SDimitry Andric     return true;
8040b57cec5SDimitry Andric 
8050b57cec5SDimitry Andric   // Pick up the bytes from the file and emit them.
8060b57cec5SDimitry Andric   StringRef Bytes = SrcMgr.getMemoryBuffer(NewBuf)->getBuffer();
8070b57cec5SDimitry Andric   Bytes = Bytes.drop_front(Skip);
8080b57cec5SDimitry Andric   if (Count) {
8090b57cec5SDimitry Andric     int64_t Res;
8100b57cec5SDimitry Andric     if (!Count->evaluateAsAbsolute(Res, getStreamer().getAssemblerPtr()))
8110b57cec5SDimitry Andric       return Error(Loc, "expected absolute expression");
8120b57cec5SDimitry Andric     if (Res < 0)
8130b57cec5SDimitry Andric       return Warning(Loc, "negative count has no effect");
8140b57cec5SDimitry Andric     Bytes = Bytes.take_front(Res);
8150b57cec5SDimitry Andric   }
816*5ffd83dbSDimitry Andric   getStreamer().emitBytes(Bytes);
8170b57cec5SDimitry Andric   return false;
8180b57cec5SDimitry Andric }
8190b57cec5SDimitry Andric 
8200b57cec5SDimitry Andric void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
8210b57cec5SDimitry Andric   CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
8220b57cec5SDimitry Andric   Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
8230b57cec5SDimitry Andric                   Loc.getPointer());
8240b57cec5SDimitry Andric }
8250b57cec5SDimitry Andric 
8260b57cec5SDimitry Andric const AsmToken &AsmParser::Lex() {
8270b57cec5SDimitry Andric   if (Lexer.getTok().is(AsmToken::Error))
8280b57cec5SDimitry Andric     Error(Lexer.getErrLoc(), Lexer.getErr());
8290b57cec5SDimitry Andric 
8300b57cec5SDimitry Andric   // if it's a end of statement with a comment in it
8310b57cec5SDimitry Andric   if (getTok().is(AsmToken::EndOfStatement)) {
8320b57cec5SDimitry Andric     // if this is a line comment output it.
8330b57cec5SDimitry Andric     if (!getTok().getString().empty() && getTok().getString().front() != '\n' &&
8340b57cec5SDimitry Andric         getTok().getString().front() != '\r' && MAI.preserveAsmComments())
8350b57cec5SDimitry Andric       Out.addExplicitComment(Twine(getTok().getString()));
8360b57cec5SDimitry Andric   }
8370b57cec5SDimitry Andric 
8380b57cec5SDimitry Andric   const AsmToken *tok = &Lexer.Lex();
8390b57cec5SDimitry Andric 
8400b57cec5SDimitry Andric   // Parse comments here to be deferred until end of next statement.
8410b57cec5SDimitry Andric   while (tok->is(AsmToken::Comment)) {
8420b57cec5SDimitry Andric     if (MAI.preserveAsmComments())
8430b57cec5SDimitry Andric       Out.addExplicitComment(Twine(tok->getString()));
8440b57cec5SDimitry Andric     tok = &Lexer.Lex();
8450b57cec5SDimitry Andric   }
8460b57cec5SDimitry Andric 
8470b57cec5SDimitry Andric   if (tok->is(AsmToken::Eof)) {
8480b57cec5SDimitry Andric     // If this is the end of an included file, pop the parent file off the
8490b57cec5SDimitry Andric     // include stack.
8500b57cec5SDimitry Andric     SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
8510b57cec5SDimitry Andric     if (ParentIncludeLoc != SMLoc()) {
8520b57cec5SDimitry Andric       jumpToLoc(ParentIncludeLoc);
8530b57cec5SDimitry Andric       return Lex();
8540b57cec5SDimitry Andric     }
8550b57cec5SDimitry Andric   }
8560b57cec5SDimitry Andric 
8570b57cec5SDimitry Andric   return *tok;
8580b57cec5SDimitry Andric }
8590b57cec5SDimitry Andric 
8600b57cec5SDimitry Andric bool AsmParser::enabledGenDwarfForAssembly() {
8610b57cec5SDimitry Andric   // Check whether the user specified -g.
8620b57cec5SDimitry Andric   if (!getContext().getGenDwarfForAssembly())
8630b57cec5SDimitry Andric     return false;
8640b57cec5SDimitry Andric   // If we haven't encountered any .file directives (which would imply that
8650b57cec5SDimitry Andric   // the assembler source was produced with debug info already) then emit one
8660b57cec5SDimitry Andric   // describing the assembler source file itself.
8670b57cec5SDimitry Andric   if (getContext().getGenDwarfFileNumber() == 0) {
8680b57cec5SDimitry Andric     // Use the first #line directive for this, if any. It's preprocessed, so
8690b57cec5SDimitry Andric     // there is no checksum, and of course no source directive.
8700b57cec5SDimitry Andric     if (!FirstCppHashFilename.empty())
8710b57cec5SDimitry Andric       getContext().setMCLineTableRootFile(/*CUID=*/0,
8720b57cec5SDimitry Andric                                           getContext().getCompilationDir(),
8730b57cec5SDimitry Andric                                           FirstCppHashFilename,
8740b57cec5SDimitry Andric                                           /*Cksum=*/None, /*Source=*/None);
8750b57cec5SDimitry Andric     const MCDwarfFile &RootFile =
8760b57cec5SDimitry Andric         getContext().getMCDwarfLineTable(/*CUID=*/0).getRootFile();
877*5ffd83dbSDimitry Andric     getContext().setGenDwarfFileNumber(getStreamer().emitDwarfFileDirective(
8780b57cec5SDimitry Andric         /*CUID=*/0, getContext().getCompilationDir(), RootFile.Name,
8790b57cec5SDimitry Andric         RootFile.Checksum, RootFile.Source));
8800b57cec5SDimitry Andric   }
8810b57cec5SDimitry Andric   return true;
8820b57cec5SDimitry Andric }
8830b57cec5SDimitry Andric 
8840b57cec5SDimitry Andric bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
8850b57cec5SDimitry Andric   // Create the initial section, if requested.
8860b57cec5SDimitry Andric   if (!NoInitialTextSection)
8870b57cec5SDimitry Andric     Out.InitSections(false);
8880b57cec5SDimitry Andric 
8890b57cec5SDimitry Andric   // Prime the lexer.
8900b57cec5SDimitry Andric   Lex();
8910b57cec5SDimitry Andric 
8920b57cec5SDimitry Andric   HadError = false;
8930b57cec5SDimitry Andric   AsmCond StartingCondState = TheCondState;
8940b57cec5SDimitry Andric   SmallVector<AsmRewrite, 4> AsmStrRewrites;
8950b57cec5SDimitry Andric 
8960b57cec5SDimitry Andric   // If we are generating dwarf for assembly source files save the initial text
8970b57cec5SDimitry Andric   // section.  (Don't use enabledGenDwarfForAssembly() here, as we aren't
8980b57cec5SDimitry Andric   // emitting any actual debug info yet and haven't had a chance to parse any
8990b57cec5SDimitry Andric   // embedded .file directives.)
9000b57cec5SDimitry Andric   if (getContext().getGenDwarfForAssembly()) {
9010b57cec5SDimitry Andric     MCSection *Sec = getStreamer().getCurrentSectionOnly();
9020b57cec5SDimitry Andric     if (!Sec->getBeginSymbol()) {
9030b57cec5SDimitry Andric       MCSymbol *SectionStartSym = getContext().createTempSymbol();
904*5ffd83dbSDimitry Andric       getStreamer().emitLabel(SectionStartSym);
9050b57cec5SDimitry Andric       Sec->setBeginSymbol(SectionStartSym);
9060b57cec5SDimitry Andric     }
9070b57cec5SDimitry Andric     bool InsertResult = getContext().addGenDwarfSection(Sec);
9080b57cec5SDimitry Andric     assert(InsertResult && ".text section should not have debug info yet");
9090b57cec5SDimitry Andric     (void)InsertResult;
9100b57cec5SDimitry Andric   }
9110b57cec5SDimitry Andric 
9120b57cec5SDimitry Andric   // While we have input, parse each statement.
9130b57cec5SDimitry Andric   while (Lexer.isNot(AsmToken::Eof)) {
9140b57cec5SDimitry Andric     ParseStatementInfo Info(&AsmStrRewrites);
915480093f4SDimitry Andric     bool Parsed = parseStatement(Info, nullptr);
9160b57cec5SDimitry Andric 
9170b57cec5SDimitry Andric     // If we have a Lexer Error we are on an Error Token. Load in Lexer Error
9180b57cec5SDimitry Andric     // for printing ErrMsg via Lex() only if no (presumably better) parser error
9190b57cec5SDimitry Andric     // exists.
920480093f4SDimitry Andric     if (Parsed && !hasPendingError() && Lexer.getTok().is(AsmToken::Error)) {
9210b57cec5SDimitry Andric       Lex();
9220b57cec5SDimitry Andric     }
9230b57cec5SDimitry Andric 
9240b57cec5SDimitry Andric     // parseStatement returned true so may need to emit an error.
9250b57cec5SDimitry Andric     printPendingErrors();
9260b57cec5SDimitry Andric 
9270b57cec5SDimitry Andric     // Skipping to the next line if needed.
928480093f4SDimitry Andric     if (Parsed && !getLexer().isAtStartOfStatement())
9290b57cec5SDimitry Andric       eatToEndOfStatement();
9300b57cec5SDimitry Andric   }
9310b57cec5SDimitry Andric 
9320b57cec5SDimitry Andric   getTargetParser().onEndOfFile();
9330b57cec5SDimitry Andric   printPendingErrors();
9340b57cec5SDimitry Andric 
9350b57cec5SDimitry Andric   // All errors should have been emitted.
9360b57cec5SDimitry Andric   assert(!hasPendingError() && "unexpected error from parseStatement");
9370b57cec5SDimitry Andric 
9380b57cec5SDimitry Andric   getTargetParser().flushPendingInstructions(getStreamer());
9390b57cec5SDimitry Andric 
9400b57cec5SDimitry Andric   if (TheCondState.TheCond != StartingCondState.TheCond ||
9410b57cec5SDimitry Andric       TheCondState.Ignore != StartingCondState.Ignore)
9420b57cec5SDimitry Andric     printError(getTok().getLoc(), "unmatched .ifs or .elses");
9430b57cec5SDimitry Andric   // Check to see there are no empty DwarfFile slots.
9440b57cec5SDimitry Andric   const auto &LineTables = getContext().getMCDwarfLineTables();
9450b57cec5SDimitry Andric   if (!LineTables.empty()) {
9460b57cec5SDimitry Andric     unsigned Index = 0;
9470b57cec5SDimitry Andric     for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
9480b57cec5SDimitry Andric       if (File.Name.empty() && Index != 0)
9490b57cec5SDimitry Andric         printError(getTok().getLoc(), "unassigned file number: " +
9500b57cec5SDimitry Andric                                           Twine(Index) +
9510b57cec5SDimitry Andric                                           " for .file directives");
9520b57cec5SDimitry Andric       ++Index;
9530b57cec5SDimitry Andric     }
9540b57cec5SDimitry Andric   }
9550b57cec5SDimitry Andric 
9560b57cec5SDimitry Andric   // Check to see that all assembler local symbols were actually defined.
9570b57cec5SDimitry Andric   // Targets that don't do subsections via symbols may not want this, though,
9580b57cec5SDimitry Andric   // so conservatively exclude them. Only do this if we're finalizing, though,
9590b57cec5SDimitry Andric   // as otherwise we won't necessarilly have seen everything yet.
9600b57cec5SDimitry Andric   if (!NoFinalize) {
9610b57cec5SDimitry Andric     if (MAI.hasSubsectionsViaSymbols()) {
9620b57cec5SDimitry Andric       for (const auto &TableEntry : getContext().getSymbols()) {
9630b57cec5SDimitry Andric         MCSymbol *Sym = TableEntry.getValue();
9640b57cec5SDimitry Andric         // Variable symbols may not be marked as defined, so check those
9650b57cec5SDimitry Andric         // explicitly. If we know it's a variable, we have a definition for
9660b57cec5SDimitry Andric         // the purposes of this check.
9670b57cec5SDimitry Andric         if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
9680b57cec5SDimitry Andric           // FIXME: We would really like to refer back to where the symbol was
9690b57cec5SDimitry Andric           // first referenced for a source location. We need to add something
9700b57cec5SDimitry Andric           // to track that. Currently, we just point to the end of the file.
9710b57cec5SDimitry Andric           printError(getTok().getLoc(), "assembler local symbol '" +
9720b57cec5SDimitry Andric                                             Sym->getName() + "' not defined");
9730b57cec5SDimitry Andric       }
9740b57cec5SDimitry Andric     }
9750b57cec5SDimitry Andric 
9760b57cec5SDimitry Andric     // Temporary symbols like the ones for directional jumps don't go in the
9770b57cec5SDimitry Andric     // symbol table. They also need to be diagnosed in all (final) cases.
9780b57cec5SDimitry Andric     for (std::tuple<SMLoc, CppHashInfoTy, MCSymbol *> &LocSym : DirLabels) {
9790b57cec5SDimitry Andric       if (std::get<2>(LocSym)->isUndefined()) {
9800b57cec5SDimitry Andric         // Reset the state of any "# line file" directives we've seen to the
9810b57cec5SDimitry Andric         // context as it was at the diagnostic site.
9820b57cec5SDimitry Andric         CppHashInfo = std::get<1>(LocSym);
9830b57cec5SDimitry Andric         printError(std::get<0>(LocSym), "directional label undefined");
9840b57cec5SDimitry Andric       }
9850b57cec5SDimitry Andric     }
9860b57cec5SDimitry Andric   }
9870b57cec5SDimitry Andric 
9880b57cec5SDimitry Andric   // Finalize the output stream if there are no errors and if the client wants
9890b57cec5SDimitry Andric   // us to.
9900b57cec5SDimitry Andric   if (!HadError && !NoFinalize)
9910b57cec5SDimitry Andric     Out.Finish();
9920b57cec5SDimitry Andric 
9930b57cec5SDimitry Andric   return HadError || getContext().hadError();
9940b57cec5SDimitry Andric }
9950b57cec5SDimitry Andric 
9960b57cec5SDimitry Andric bool AsmParser::checkForValidSection() {
997*5ffd83dbSDimitry Andric   if (!ParsingMSInlineAsm && !getStreamer().getCurrentSectionOnly()) {
9980b57cec5SDimitry Andric     Out.InitSections(false);
9990b57cec5SDimitry Andric     return Error(getTok().getLoc(),
10000b57cec5SDimitry Andric                  "expected section directive before assembly directive");
10010b57cec5SDimitry Andric   }
10020b57cec5SDimitry Andric   return false;
10030b57cec5SDimitry Andric }
10040b57cec5SDimitry Andric 
10050b57cec5SDimitry Andric /// Throw away the rest of the line for testing purposes.
10060b57cec5SDimitry Andric void AsmParser::eatToEndOfStatement() {
10070b57cec5SDimitry Andric   while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
10080b57cec5SDimitry Andric     Lexer.Lex();
10090b57cec5SDimitry Andric 
10100b57cec5SDimitry Andric   // Eat EOL.
10110b57cec5SDimitry Andric   if (Lexer.is(AsmToken::EndOfStatement))
10120b57cec5SDimitry Andric     Lexer.Lex();
10130b57cec5SDimitry Andric }
10140b57cec5SDimitry Andric 
10150b57cec5SDimitry Andric StringRef AsmParser::parseStringToEndOfStatement() {
10160b57cec5SDimitry Andric   const char *Start = getTok().getLoc().getPointer();
10170b57cec5SDimitry Andric 
10180b57cec5SDimitry Andric   while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
10190b57cec5SDimitry Andric     Lexer.Lex();
10200b57cec5SDimitry Andric 
10210b57cec5SDimitry Andric   const char *End = getTok().getLoc().getPointer();
10220b57cec5SDimitry Andric   return StringRef(Start, End - Start);
10230b57cec5SDimitry Andric }
10240b57cec5SDimitry Andric 
10250b57cec5SDimitry Andric StringRef AsmParser::parseStringToComma() {
10260b57cec5SDimitry Andric   const char *Start = getTok().getLoc().getPointer();
10270b57cec5SDimitry Andric 
10280b57cec5SDimitry Andric   while (Lexer.isNot(AsmToken::EndOfStatement) &&
10290b57cec5SDimitry Andric          Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
10300b57cec5SDimitry Andric     Lexer.Lex();
10310b57cec5SDimitry Andric 
10320b57cec5SDimitry Andric   const char *End = getTok().getLoc().getPointer();
10330b57cec5SDimitry Andric   return StringRef(Start, End - Start);
10340b57cec5SDimitry Andric }
10350b57cec5SDimitry Andric 
10360b57cec5SDimitry Andric /// Parse a paren expression and return it.
10370b57cec5SDimitry Andric /// NOTE: This assumes the leading '(' has already been consumed.
10380b57cec5SDimitry Andric ///
10390b57cec5SDimitry Andric /// parenexpr ::= expr)
10400b57cec5SDimitry Andric ///
10410b57cec5SDimitry Andric bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
10420b57cec5SDimitry Andric   if (parseExpression(Res))
10430b57cec5SDimitry Andric     return true;
10440b57cec5SDimitry Andric   if (Lexer.isNot(AsmToken::RParen))
10450b57cec5SDimitry Andric     return TokError("expected ')' in parentheses expression");
10460b57cec5SDimitry Andric   EndLoc = Lexer.getTok().getEndLoc();
10470b57cec5SDimitry Andric   Lex();
10480b57cec5SDimitry Andric   return false;
10490b57cec5SDimitry Andric }
10500b57cec5SDimitry Andric 
10510b57cec5SDimitry Andric /// Parse a bracket expression and return it.
10520b57cec5SDimitry Andric /// NOTE: This assumes the leading '[' has already been consumed.
10530b57cec5SDimitry Andric ///
10540b57cec5SDimitry Andric /// bracketexpr ::= expr]
10550b57cec5SDimitry Andric ///
10560b57cec5SDimitry Andric bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
10570b57cec5SDimitry Andric   if (parseExpression(Res))
10580b57cec5SDimitry Andric     return true;
10590b57cec5SDimitry Andric   EndLoc = getTok().getEndLoc();
10600b57cec5SDimitry Andric   if (parseToken(AsmToken::RBrac, "expected ']' in brackets expression"))
10610b57cec5SDimitry Andric     return true;
10620b57cec5SDimitry Andric   return false;
10630b57cec5SDimitry Andric }
10640b57cec5SDimitry Andric 
10650b57cec5SDimitry Andric /// Parse a primary expression and return it.
10660b57cec5SDimitry Andric ///  primaryexpr ::= (parenexpr
10670b57cec5SDimitry Andric ///  primaryexpr ::= symbol
10680b57cec5SDimitry Andric ///  primaryexpr ::= number
10690b57cec5SDimitry Andric ///  primaryexpr ::= '.'
10700b57cec5SDimitry Andric ///  primaryexpr ::= ~,+,- primaryexpr
10710b57cec5SDimitry Andric bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
10720b57cec5SDimitry Andric   SMLoc FirstTokenLoc = getLexer().getLoc();
10730b57cec5SDimitry Andric   AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
10740b57cec5SDimitry Andric   switch (FirstTokenKind) {
10750b57cec5SDimitry Andric   default:
10760b57cec5SDimitry Andric     return TokError("unknown token in expression");
10770b57cec5SDimitry Andric   // If we have an error assume that we've already handled it.
10780b57cec5SDimitry Andric   case AsmToken::Error:
10790b57cec5SDimitry Andric     return true;
10800b57cec5SDimitry Andric   case AsmToken::Exclaim:
10810b57cec5SDimitry Andric     Lex(); // Eat the operator.
10820b57cec5SDimitry Andric     if (parsePrimaryExpr(Res, EndLoc))
10830b57cec5SDimitry Andric       return true;
10840b57cec5SDimitry Andric     Res = MCUnaryExpr::createLNot(Res, getContext(), FirstTokenLoc);
10850b57cec5SDimitry Andric     return false;
10860b57cec5SDimitry Andric   case AsmToken::Dollar:
10870b57cec5SDimitry Andric   case AsmToken::At:
10880b57cec5SDimitry Andric   case AsmToken::String:
10890b57cec5SDimitry Andric   case AsmToken::Identifier: {
10900b57cec5SDimitry Andric     StringRef Identifier;
10910b57cec5SDimitry Andric     if (parseIdentifier(Identifier)) {
10920b57cec5SDimitry Andric       // We may have failed but $ may be a valid token.
10930b57cec5SDimitry Andric       if (getTok().is(AsmToken::Dollar)) {
10940b57cec5SDimitry Andric         if (Lexer.getMAI().getDollarIsPC()) {
10950b57cec5SDimitry Andric           Lex();
10960b57cec5SDimitry Andric           // This is a '$' reference, which references the current PC.  Emit a
10970b57cec5SDimitry Andric           // temporary label to the streamer and refer to it.
10980b57cec5SDimitry Andric           MCSymbol *Sym = Ctx.createTempSymbol();
1099*5ffd83dbSDimitry Andric           Out.emitLabel(Sym);
11000b57cec5SDimitry Andric           Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None,
11010b57cec5SDimitry Andric                                         getContext());
11020b57cec5SDimitry Andric           EndLoc = FirstTokenLoc;
11030b57cec5SDimitry Andric           return false;
11040b57cec5SDimitry Andric         }
11050b57cec5SDimitry Andric         return Error(FirstTokenLoc, "invalid token in expression");
11060b57cec5SDimitry Andric       }
11070b57cec5SDimitry Andric     }
11080b57cec5SDimitry Andric     // Parse symbol variant
11090b57cec5SDimitry Andric     std::pair<StringRef, StringRef> Split;
11100b57cec5SDimitry Andric     if (!MAI.useParensForSymbolVariant()) {
11110b57cec5SDimitry Andric       if (FirstTokenKind == AsmToken::String) {
11120b57cec5SDimitry Andric         if (Lexer.is(AsmToken::At)) {
11130b57cec5SDimitry Andric           Lex(); // eat @
11140b57cec5SDimitry Andric           SMLoc AtLoc = getLexer().getLoc();
11150b57cec5SDimitry Andric           StringRef VName;
11160b57cec5SDimitry Andric           if (parseIdentifier(VName))
11170b57cec5SDimitry Andric             return Error(AtLoc, "expected symbol variant after '@'");
11180b57cec5SDimitry Andric 
11190b57cec5SDimitry Andric           Split = std::make_pair(Identifier, VName);
11200b57cec5SDimitry Andric         }
11210b57cec5SDimitry Andric       } else {
11220b57cec5SDimitry Andric         Split = Identifier.split('@');
11230b57cec5SDimitry Andric       }
11240b57cec5SDimitry Andric     } else if (Lexer.is(AsmToken::LParen)) {
11250b57cec5SDimitry Andric       Lex(); // eat '('.
11260b57cec5SDimitry Andric       StringRef VName;
11270b57cec5SDimitry Andric       parseIdentifier(VName);
11280b57cec5SDimitry Andric       // eat ')'.
11290b57cec5SDimitry Andric       if (parseToken(AsmToken::RParen,
11300b57cec5SDimitry Andric                      "unexpected token in variant, expected ')'"))
11310b57cec5SDimitry Andric         return true;
11320b57cec5SDimitry Andric       Split = std::make_pair(Identifier, VName);
11330b57cec5SDimitry Andric     }
11340b57cec5SDimitry Andric 
11350b57cec5SDimitry Andric     EndLoc = SMLoc::getFromPointer(Identifier.end());
11360b57cec5SDimitry Andric 
11370b57cec5SDimitry Andric     // This is a symbol reference.
11380b57cec5SDimitry Andric     StringRef SymbolName = Identifier;
11390b57cec5SDimitry Andric     if (SymbolName.empty())
11400b57cec5SDimitry Andric       return Error(getLexer().getLoc(), "expected a symbol reference");
11410b57cec5SDimitry Andric 
11420b57cec5SDimitry Andric     MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
11430b57cec5SDimitry Andric 
11440b57cec5SDimitry Andric     // Lookup the symbol variant if used.
11450b57cec5SDimitry Andric     if (!Split.second.empty()) {
11460b57cec5SDimitry Andric       Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
11470b57cec5SDimitry Andric       if (Variant != MCSymbolRefExpr::VK_Invalid) {
11480b57cec5SDimitry Andric         SymbolName = Split.first;
11490b57cec5SDimitry Andric       } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
11500b57cec5SDimitry Andric         Variant = MCSymbolRefExpr::VK_None;
11510b57cec5SDimitry Andric       } else {
11520b57cec5SDimitry Andric         return Error(SMLoc::getFromPointer(Split.second.begin()),
11530b57cec5SDimitry Andric                      "invalid variant '" + Split.second + "'");
11540b57cec5SDimitry Andric       }
11550b57cec5SDimitry Andric     }
11560b57cec5SDimitry Andric 
11570b57cec5SDimitry Andric     MCSymbol *Sym = getContext().getInlineAsmLabel(SymbolName);
11580b57cec5SDimitry Andric     if (!Sym)
11590b57cec5SDimitry Andric       Sym = getContext().getOrCreateSymbol(SymbolName);
11600b57cec5SDimitry Andric 
11610b57cec5SDimitry Andric     // If this is an absolute variable reference, substitute it now to preserve
11620b57cec5SDimitry Andric     // semantics in the face of reassignment.
11630b57cec5SDimitry Andric     if (Sym->isVariable()) {
11640b57cec5SDimitry Andric       auto V = Sym->getVariableValue(/*SetUsed*/ false);
11650b57cec5SDimitry Andric       bool DoInline = isa<MCConstantExpr>(V) && !Variant;
11660b57cec5SDimitry Andric       if (auto TV = dyn_cast<MCTargetExpr>(V))
11670b57cec5SDimitry Andric         DoInline = TV->inlineAssignedExpr();
11680b57cec5SDimitry Andric       if (DoInline) {
11690b57cec5SDimitry Andric         if (Variant)
11700b57cec5SDimitry Andric           return Error(EndLoc, "unexpected modifier on variable reference");
11710b57cec5SDimitry Andric         Res = Sym->getVariableValue(/*SetUsed*/ false);
11720b57cec5SDimitry Andric         return false;
11730b57cec5SDimitry Andric       }
11740b57cec5SDimitry Andric     }
11750b57cec5SDimitry Andric 
11760b57cec5SDimitry Andric     // Otherwise create a symbol ref.
11770b57cec5SDimitry Andric     Res = MCSymbolRefExpr::create(Sym, Variant, getContext(), FirstTokenLoc);
11780b57cec5SDimitry Andric     return false;
11790b57cec5SDimitry Andric   }
11800b57cec5SDimitry Andric   case AsmToken::BigNum:
11810b57cec5SDimitry Andric     return TokError("literal value out of range for directive");
11820b57cec5SDimitry Andric   case AsmToken::Integer: {
11830b57cec5SDimitry Andric     SMLoc Loc = getTok().getLoc();
11840b57cec5SDimitry Andric     int64_t IntVal = getTok().getIntVal();
11850b57cec5SDimitry Andric     Res = MCConstantExpr::create(IntVal, getContext());
11860b57cec5SDimitry Andric     EndLoc = Lexer.getTok().getEndLoc();
11870b57cec5SDimitry Andric     Lex(); // Eat token.
11880b57cec5SDimitry Andric     // Look for 'b' or 'f' following an Integer as a directional label
11890b57cec5SDimitry Andric     if (Lexer.getKind() == AsmToken::Identifier) {
11900b57cec5SDimitry Andric       StringRef IDVal = getTok().getString();
11910b57cec5SDimitry Andric       // Lookup the symbol variant if used.
11920b57cec5SDimitry Andric       std::pair<StringRef, StringRef> Split = IDVal.split('@');
11930b57cec5SDimitry Andric       MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
11940b57cec5SDimitry Andric       if (Split.first.size() != IDVal.size()) {
11950b57cec5SDimitry Andric         Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
11960b57cec5SDimitry Andric         if (Variant == MCSymbolRefExpr::VK_Invalid)
11970b57cec5SDimitry Andric           return TokError("invalid variant '" + Split.second + "'");
11980b57cec5SDimitry Andric         IDVal = Split.first;
11990b57cec5SDimitry Andric       }
12000b57cec5SDimitry Andric       if (IDVal == "f" || IDVal == "b") {
12010b57cec5SDimitry Andric         MCSymbol *Sym =
12020b57cec5SDimitry Andric             Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b");
12030b57cec5SDimitry Andric         Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
12040b57cec5SDimitry Andric         if (IDVal == "b" && Sym->isUndefined())
12050b57cec5SDimitry Andric           return Error(Loc, "directional label undefined");
12060b57cec5SDimitry Andric         DirLabels.push_back(std::make_tuple(Loc, CppHashInfo, Sym));
12070b57cec5SDimitry Andric         EndLoc = Lexer.getTok().getEndLoc();
12080b57cec5SDimitry Andric         Lex(); // Eat identifier.
12090b57cec5SDimitry Andric       }
12100b57cec5SDimitry Andric     }
12110b57cec5SDimitry Andric     return false;
12120b57cec5SDimitry Andric   }
12130b57cec5SDimitry Andric   case AsmToken::Real: {
12140b57cec5SDimitry Andric     APFloat RealVal(APFloat::IEEEdouble(), getTok().getString());
12150b57cec5SDimitry Andric     uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
12160b57cec5SDimitry Andric     Res = MCConstantExpr::create(IntVal, getContext());
12170b57cec5SDimitry Andric     EndLoc = Lexer.getTok().getEndLoc();
12180b57cec5SDimitry Andric     Lex(); // Eat token.
12190b57cec5SDimitry Andric     return false;
12200b57cec5SDimitry Andric   }
12210b57cec5SDimitry Andric   case AsmToken::Dot: {
12220b57cec5SDimitry Andric     // This is a '.' reference, which references the current PC.  Emit a
12230b57cec5SDimitry Andric     // temporary label to the streamer and refer to it.
12240b57cec5SDimitry Andric     MCSymbol *Sym = Ctx.createTempSymbol();
1225*5ffd83dbSDimitry Andric     Out.emitLabel(Sym);
12260b57cec5SDimitry Andric     Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
12270b57cec5SDimitry Andric     EndLoc = Lexer.getTok().getEndLoc();
12280b57cec5SDimitry Andric     Lex(); // Eat identifier.
12290b57cec5SDimitry Andric     return false;
12300b57cec5SDimitry Andric   }
12310b57cec5SDimitry Andric   case AsmToken::LParen:
12320b57cec5SDimitry Andric     Lex(); // Eat the '('.
12330b57cec5SDimitry Andric     return parseParenExpr(Res, EndLoc);
12340b57cec5SDimitry Andric   case AsmToken::LBrac:
12350b57cec5SDimitry Andric     if (!PlatformParser->HasBracketExpressions())
12360b57cec5SDimitry Andric       return TokError("brackets expression not supported on this target");
12370b57cec5SDimitry Andric     Lex(); // Eat the '['.
12380b57cec5SDimitry Andric     return parseBracketExpr(Res, EndLoc);
12390b57cec5SDimitry Andric   case AsmToken::Minus:
12400b57cec5SDimitry Andric     Lex(); // Eat the operator.
12410b57cec5SDimitry Andric     if (parsePrimaryExpr(Res, EndLoc))
12420b57cec5SDimitry Andric       return true;
12430b57cec5SDimitry Andric     Res = MCUnaryExpr::createMinus(Res, getContext(), FirstTokenLoc);
12440b57cec5SDimitry Andric     return false;
12450b57cec5SDimitry Andric   case AsmToken::Plus:
12460b57cec5SDimitry Andric     Lex(); // Eat the operator.
12470b57cec5SDimitry Andric     if (parsePrimaryExpr(Res, EndLoc))
12480b57cec5SDimitry Andric       return true;
12490b57cec5SDimitry Andric     Res = MCUnaryExpr::createPlus(Res, getContext(), FirstTokenLoc);
12500b57cec5SDimitry Andric     return false;
12510b57cec5SDimitry Andric   case AsmToken::Tilde:
12520b57cec5SDimitry Andric     Lex(); // Eat the operator.
12530b57cec5SDimitry Andric     if (parsePrimaryExpr(Res, EndLoc))
12540b57cec5SDimitry Andric       return true;
12550b57cec5SDimitry Andric     Res = MCUnaryExpr::createNot(Res, getContext(), FirstTokenLoc);
12560b57cec5SDimitry Andric     return false;
12570b57cec5SDimitry Andric   // MIPS unary expression operators. The lexer won't generate these tokens if
12580b57cec5SDimitry Andric   // MCAsmInfo::HasMipsExpressions is false for the target.
12590b57cec5SDimitry Andric   case AsmToken::PercentCall16:
12600b57cec5SDimitry Andric   case AsmToken::PercentCall_Hi:
12610b57cec5SDimitry Andric   case AsmToken::PercentCall_Lo:
12620b57cec5SDimitry Andric   case AsmToken::PercentDtprel_Hi:
12630b57cec5SDimitry Andric   case AsmToken::PercentDtprel_Lo:
12640b57cec5SDimitry Andric   case AsmToken::PercentGot:
12650b57cec5SDimitry Andric   case AsmToken::PercentGot_Disp:
12660b57cec5SDimitry Andric   case AsmToken::PercentGot_Hi:
12670b57cec5SDimitry Andric   case AsmToken::PercentGot_Lo:
12680b57cec5SDimitry Andric   case AsmToken::PercentGot_Ofst:
12690b57cec5SDimitry Andric   case AsmToken::PercentGot_Page:
12700b57cec5SDimitry Andric   case AsmToken::PercentGottprel:
12710b57cec5SDimitry Andric   case AsmToken::PercentGp_Rel:
12720b57cec5SDimitry Andric   case AsmToken::PercentHi:
12730b57cec5SDimitry Andric   case AsmToken::PercentHigher:
12740b57cec5SDimitry Andric   case AsmToken::PercentHighest:
12750b57cec5SDimitry Andric   case AsmToken::PercentLo:
12760b57cec5SDimitry Andric   case AsmToken::PercentNeg:
12770b57cec5SDimitry Andric   case AsmToken::PercentPcrel_Hi:
12780b57cec5SDimitry Andric   case AsmToken::PercentPcrel_Lo:
12790b57cec5SDimitry Andric   case AsmToken::PercentTlsgd:
12800b57cec5SDimitry Andric   case AsmToken::PercentTlsldm:
12810b57cec5SDimitry Andric   case AsmToken::PercentTprel_Hi:
12820b57cec5SDimitry Andric   case AsmToken::PercentTprel_Lo:
12830b57cec5SDimitry Andric     Lex(); // Eat the operator.
12840b57cec5SDimitry Andric     if (Lexer.isNot(AsmToken::LParen))
12850b57cec5SDimitry Andric       return TokError("expected '(' after operator");
12860b57cec5SDimitry Andric     Lex(); // Eat the operator.
12870b57cec5SDimitry Andric     if (parseExpression(Res, EndLoc))
12880b57cec5SDimitry Andric       return true;
12890b57cec5SDimitry Andric     if (Lexer.isNot(AsmToken::RParen))
12900b57cec5SDimitry Andric       return TokError("expected ')'");
12910b57cec5SDimitry Andric     Lex(); // Eat the operator.
12920b57cec5SDimitry Andric     Res = getTargetParser().createTargetUnaryExpr(Res, FirstTokenKind, Ctx);
12930b57cec5SDimitry Andric     return !Res;
12940b57cec5SDimitry Andric   }
12950b57cec5SDimitry Andric }
12960b57cec5SDimitry Andric 
12970b57cec5SDimitry Andric bool AsmParser::parseExpression(const MCExpr *&Res) {
12980b57cec5SDimitry Andric   SMLoc EndLoc;
12990b57cec5SDimitry Andric   return parseExpression(Res, EndLoc);
13000b57cec5SDimitry Andric }
13010b57cec5SDimitry Andric 
13020b57cec5SDimitry Andric const MCExpr *
13030b57cec5SDimitry Andric AsmParser::applyModifierToExpr(const MCExpr *E,
13040b57cec5SDimitry Andric                                MCSymbolRefExpr::VariantKind Variant) {
13050b57cec5SDimitry Andric   // Ask the target implementation about this expression first.
13060b57cec5SDimitry Andric   const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
13070b57cec5SDimitry Andric   if (NewE)
13080b57cec5SDimitry Andric     return NewE;
13090b57cec5SDimitry Andric   // Recurse over the given expression, rebuilding it to apply the given variant
13100b57cec5SDimitry Andric   // if there is exactly one symbol.
13110b57cec5SDimitry Andric   switch (E->getKind()) {
13120b57cec5SDimitry Andric   case MCExpr::Target:
13130b57cec5SDimitry Andric   case MCExpr::Constant:
13140b57cec5SDimitry Andric     return nullptr;
13150b57cec5SDimitry Andric 
13160b57cec5SDimitry Andric   case MCExpr::SymbolRef: {
13170b57cec5SDimitry Andric     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
13180b57cec5SDimitry Andric 
13190b57cec5SDimitry Andric     if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
13200b57cec5SDimitry Andric       TokError("invalid variant on expression '" + getTok().getIdentifier() +
13210b57cec5SDimitry Andric                "' (already modified)");
13220b57cec5SDimitry Andric       return E;
13230b57cec5SDimitry Andric     }
13240b57cec5SDimitry Andric 
13250b57cec5SDimitry Andric     return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, getContext());
13260b57cec5SDimitry Andric   }
13270b57cec5SDimitry Andric 
13280b57cec5SDimitry Andric   case MCExpr::Unary: {
13290b57cec5SDimitry Andric     const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
13300b57cec5SDimitry Andric     const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
13310b57cec5SDimitry Andric     if (!Sub)
13320b57cec5SDimitry Andric       return nullptr;
13330b57cec5SDimitry Andric     return MCUnaryExpr::create(UE->getOpcode(), Sub, getContext());
13340b57cec5SDimitry Andric   }
13350b57cec5SDimitry Andric 
13360b57cec5SDimitry Andric   case MCExpr::Binary: {
13370b57cec5SDimitry Andric     const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
13380b57cec5SDimitry Andric     const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
13390b57cec5SDimitry Andric     const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
13400b57cec5SDimitry Andric 
13410b57cec5SDimitry Andric     if (!LHS && !RHS)
13420b57cec5SDimitry Andric       return nullptr;
13430b57cec5SDimitry Andric 
13440b57cec5SDimitry Andric     if (!LHS)
13450b57cec5SDimitry Andric       LHS = BE->getLHS();
13460b57cec5SDimitry Andric     if (!RHS)
13470b57cec5SDimitry Andric       RHS = BE->getRHS();
13480b57cec5SDimitry Andric 
13490b57cec5SDimitry Andric     return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, getContext());
13500b57cec5SDimitry Andric   }
13510b57cec5SDimitry Andric   }
13520b57cec5SDimitry Andric 
13530b57cec5SDimitry Andric   llvm_unreachable("Invalid expression kind!");
13540b57cec5SDimitry Andric }
13550b57cec5SDimitry Andric 
13560b57cec5SDimitry Andric /// This function checks if the next token is <string> type or arithmetic.
13570b57cec5SDimitry Andric /// string that begin with character '<' must end with character '>'.
13580b57cec5SDimitry Andric /// otherwise it is arithmetics.
13590b57cec5SDimitry Andric /// If the function returns a 'true' value,
13600b57cec5SDimitry Andric /// the End argument will be filled with the last location pointed to the '>'
13610b57cec5SDimitry Andric /// character.
13620b57cec5SDimitry Andric 
13630b57cec5SDimitry Andric /// There is a gap between the AltMacro's documentation and the single quote
13640b57cec5SDimitry Andric /// implementation. GCC does not fully support this feature and so we will not
13650b57cec5SDimitry Andric /// support it.
13660b57cec5SDimitry Andric /// TODO: Adding single quote as a string.
1367*5ffd83dbSDimitry Andric static bool isAngleBracketString(SMLoc &StrLoc, SMLoc &EndLoc) {
13680b57cec5SDimitry Andric   assert((StrLoc.getPointer() != nullptr) &&
13690b57cec5SDimitry Andric          "Argument to the function cannot be a NULL value");
13700b57cec5SDimitry Andric   const char *CharPtr = StrLoc.getPointer();
13710b57cec5SDimitry Andric   while ((*CharPtr != '>') && (*CharPtr != '\n') && (*CharPtr != '\r') &&
13720b57cec5SDimitry Andric          (*CharPtr != '\0')) {
13730b57cec5SDimitry Andric     if (*CharPtr == '!')
13740b57cec5SDimitry Andric       CharPtr++;
13750b57cec5SDimitry Andric     CharPtr++;
13760b57cec5SDimitry Andric   }
13770b57cec5SDimitry Andric   if (*CharPtr == '>') {
13780b57cec5SDimitry Andric     EndLoc = StrLoc.getFromPointer(CharPtr + 1);
13790b57cec5SDimitry Andric     return true;
13800b57cec5SDimitry Andric   }
13810b57cec5SDimitry Andric   return false;
13820b57cec5SDimitry Andric }
13830b57cec5SDimitry Andric 
13840b57cec5SDimitry Andric /// creating a string without the escape characters '!'.
1385*5ffd83dbSDimitry Andric static std::string angleBracketString(StringRef AltMacroStr) {
13860b57cec5SDimitry Andric   std::string Res;
13870b57cec5SDimitry Andric   for (size_t Pos = 0; Pos < AltMacroStr.size(); Pos++) {
13880b57cec5SDimitry Andric     if (AltMacroStr[Pos] == '!')
13890b57cec5SDimitry Andric       Pos++;
13900b57cec5SDimitry Andric     Res += AltMacroStr[Pos];
13910b57cec5SDimitry Andric   }
13920b57cec5SDimitry Andric   return Res;
13930b57cec5SDimitry Andric }
13940b57cec5SDimitry Andric 
13950b57cec5SDimitry Andric /// Parse an expression and return it.
13960b57cec5SDimitry Andric ///
13970b57cec5SDimitry Andric ///  expr ::= expr &&,|| expr               -> lowest.
13980b57cec5SDimitry Andric ///  expr ::= expr |,^,&,! expr
13990b57cec5SDimitry Andric ///  expr ::= expr ==,!=,<>,<,<=,>,>= expr
14000b57cec5SDimitry Andric ///  expr ::= expr <<,>> expr
14010b57cec5SDimitry Andric ///  expr ::= expr +,- expr
14020b57cec5SDimitry Andric ///  expr ::= expr *,/,% expr               -> highest.
14030b57cec5SDimitry Andric ///  expr ::= primaryexpr
14040b57cec5SDimitry Andric ///
14050b57cec5SDimitry Andric bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
14060b57cec5SDimitry Andric   // Parse the expression.
14070b57cec5SDimitry Andric   Res = nullptr;
14080b57cec5SDimitry Andric   if (getTargetParser().parsePrimaryExpr(Res, EndLoc) ||
14090b57cec5SDimitry Andric       parseBinOpRHS(1, Res, EndLoc))
14100b57cec5SDimitry Andric     return true;
14110b57cec5SDimitry Andric 
14120b57cec5SDimitry Andric   // As a special case, we support 'a op b @ modifier' by rewriting the
14130b57cec5SDimitry Andric   // expression to include the modifier. This is inefficient, but in general we
14140b57cec5SDimitry Andric   // expect users to use 'a@modifier op b'.
14150b57cec5SDimitry Andric   if (Lexer.getKind() == AsmToken::At) {
14160b57cec5SDimitry Andric     Lex();
14170b57cec5SDimitry Andric 
14180b57cec5SDimitry Andric     if (Lexer.isNot(AsmToken::Identifier))
14190b57cec5SDimitry Andric       return TokError("unexpected symbol modifier following '@'");
14200b57cec5SDimitry Andric 
14210b57cec5SDimitry Andric     MCSymbolRefExpr::VariantKind Variant =
14220b57cec5SDimitry Andric         MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
14230b57cec5SDimitry Andric     if (Variant == MCSymbolRefExpr::VK_Invalid)
14240b57cec5SDimitry Andric       return TokError("invalid variant '" + getTok().getIdentifier() + "'");
14250b57cec5SDimitry Andric 
14260b57cec5SDimitry Andric     const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
14270b57cec5SDimitry Andric     if (!ModifiedRes) {
14280b57cec5SDimitry Andric       return TokError("invalid modifier '" + getTok().getIdentifier() +
14290b57cec5SDimitry Andric                       "' (no symbols present)");
14300b57cec5SDimitry Andric     }
14310b57cec5SDimitry Andric 
14320b57cec5SDimitry Andric     Res = ModifiedRes;
14330b57cec5SDimitry Andric     Lex();
14340b57cec5SDimitry Andric   }
14350b57cec5SDimitry Andric 
14360b57cec5SDimitry Andric   // Try to constant fold it up front, if possible. Do not exploit
14370b57cec5SDimitry Andric   // assembler here.
14380b57cec5SDimitry Andric   int64_t Value;
14390b57cec5SDimitry Andric   if (Res->evaluateAsAbsolute(Value))
14400b57cec5SDimitry Andric     Res = MCConstantExpr::create(Value, getContext());
14410b57cec5SDimitry Andric 
14420b57cec5SDimitry Andric   return false;
14430b57cec5SDimitry Andric }
14440b57cec5SDimitry Andric 
14450b57cec5SDimitry Andric bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
14460b57cec5SDimitry Andric   Res = nullptr;
14470b57cec5SDimitry Andric   return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
14480b57cec5SDimitry Andric }
14490b57cec5SDimitry Andric 
14500b57cec5SDimitry Andric bool AsmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
14510b57cec5SDimitry Andric                                       SMLoc &EndLoc) {
14520b57cec5SDimitry Andric   if (parseParenExpr(Res, EndLoc))
14530b57cec5SDimitry Andric     return true;
14540b57cec5SDimitry Andric 
14550b57cec5SDimitry Andric   for (; ParenDepth > 0; --ParenDepth) {
14560b57cec5SDimitry Andric     if (parseBinOpRHS(1, Res, EndLoc))
14570b57cec5SDimitry Andric       return true;
14580b57cec5SDimitry Andric 
14590b57cec5SDimitry Andric     // We don't Lex() the last RParen.
14600b57cec5SDimitry Andric     // This is the same behavior as parseParenExpression().
14610b57cec5SDimitry Andric     if (ParenDepth - 1 > 0) {
14620b57cec5SDimitry Andric       EndLoc = getTok().getEndLoc();
14630b57cec5SDimitry Andric       if (parseToken(AsmToken::RParen,
14640b57cec5SDimitry Andric                      "expected ')' in parentheses expression"))
14650b57cec5SDimitry Andric         return true;
14660b57cec5SDimitry Andric     }
14670b57cec5SDimitry Andric   }
14680b57cec5SDimitry Andric   return false;
14690b57cec5SDimitry Andric }
14700b57cec5SDimitry Andric 
14710b57cec5SDimitry Andric bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
14720b57cec5SDimitry Andric   const MCExpr *Expr;
14730b57cec5SDimitry Andric 
14740b57cec5SDimitry Andric   SMLoc StartLoc = Lexer.getLoc();
14750b57cec5SDimitry Andric   if (parseExpression(Expr))
14760b57cec5SDimitry Andric     return true;
14770b57cec5SDimitry Andric 
14780b57cec5SDimitry Andric   if (!Expr->evaluateAsAbsolute(Res, getStreamer().getAssemblerPtr()))
14790b57cec5SDimitry Andric     return Error(StartLoc, "expected absolute expression");
14800b57cec5SDimitry Andric 
14810b57cec5SDimitry Andric   return false;
14820b57cec5SDimitry Andric }
14830b57cec5SDimitry Andric 
14840b57cec5SDimitry Andric static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K,
14850b57cec5SDimitry Andric                                          MCBinaryExpr::Opcode &Kind,
14860b57cec5SDimitry Andric                                          bool ShouldUseLogicalShr) {
14870b57cec5SDimitry Andric   switch (K) {
14880b57cec5SDimitry Andric   default:
14890b57cec5SDimitry Andric     return 0; // not a binop.
14900b57cec5SDimitry Andric 
14910b57cec5SDimitry Andric   // Lowest Precedence: &&, ||
14920b57cec5SDimitry Andric   case AsmToken::AmpAmp:
14930b57cec5SDimitry Andric     Kind = MCBinaryExpr::LAnd;
14940b57cec5SDimitry Andric     return 1;
14950b57cec5SDimitry Andric   case AsmToken::PipePipe:
14960b57cec5SDimitry Andric     Kind = MCBinaryExpr::LOr;
14970b57cec5SDimitry Andric     return 1;
14980b57cec5SDimitry Andric 
14990b57cec5SDimitry Andric   // Low Precedence: |, &, ^
15000b57cec5SDimitry Andric   //
15010b57cec5SDimitry Andric   // FIXME: gas seems to support '!' as an infix operator?
15020b57cec5SDimitry Andric   case AsmToken::Pipe:
15030b57cec5SDimitry Andric     Kind = MCBinaryExpr::Or;
15040b57cec5SDimitry Andric     return 2;
15050b57cec5SDimitry Andric   case AsmToken::Caret:
15060b57cec5SDimitry Andric     Kind = MCBinaryExpr::Xor;
15070b57cec5SDimitry Andric     return 2;
15080b57cec5SDimitry Andric   case AsmToken::Amp:
15090b57cec5SDimitry Andric     Kind = MCBinaryExpr::And;
15100b57cec5SDimitry Andric     return 2;
15110b57cec5SDimitry Andric 
15120b57cec5SDimitry Andric   // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
15130b57cec5SDimitry Andric   case AsmToken::EqualEqual:
15140b57cec5SDimitry Andric     Kind = MCBinaryExpr::EQ;
15150b57cec5SDimitry Andric     return 3;
15160b57cec5SDimitry Andric   case AsmToken::ExclaimEqual:
15170b57cec5SDimitry Andric   case AsmToken::LessGreater:
15180b57cec5SDimitry Andric     Kind = MCBinaryExpr::NE;
15190b57cec5SDimitry Andric     return 3;
15200b57cec5SDimitry Andric   case AsmToken::Less:
15210b57cec5SDimitry Andric     Kind = MCBinaryExpr::LT;
15220b57cec5SDimitry Andric     return 3;
15230b57cec5SDimitry Andric   case AsmToken::LessEqual:
15240b57cec5SDimitry Andric     Kind = MCBinaryExpr::LTE;
15250b57cec5SDimitry Andric     return 3;
15260b57cec5SDimitry Andric   case AsmToken::Greater:
15270b57cec5SDimitry Andric     Kind = MCBinaryExpr::GT;
15280b57cec5SDimitry Andric     return 3;
15290b57cec5SDimitry Andric   case AsmToken::GreaterEqual:
15300b57cec5SDimitry Andric     Kind = MCBinaryExpr::GTE;
15310b57cec5SDimitry Andric     return 3;
15320b57cec5SDimitry Andric 
15330b57cec5SDimitry Andric   // Intermediate Precedence: <<, >>
15340b57cec5SDimitry Andric   case AsmToken::LessLess:
15350b57cec5SDimitry Andric     Kind = MCBinaryExpr::Shl;
15360b57cec5SDimitry Andric     return 4;
15370b57cec5SDimitry Andric   case AsmToken::GreaterGreater:
15380b57cec5SDimitry Andric     Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
15390b57cec5SDimitry Andric     return 4;
15400b57cec5SDimitry Andric 
15410b57cec5SDimitry Andric   // High Intermediate Precedence: +, -
15420b57cec5SDimitry Andric   case AsmToken::Plus:
15430b57cec5SDimitry Andric     Kind = MCBinaryExpr::Add;
15440b57cec5SDimitry Andric     return 5;
15450b57cec5SDimitry Andric   case AsmToken::Minus:
15460b57cec5SDimitry Andric     Kind = MCBinaryExpr::Sub;
15470b57cec5SDimitry Andric     return 5;
15480b57cec5SDimitry Andric 
15490b57cec5SDimitry Andric   // Highest Precedence: *, /, %
15500b57cec5SDimitry Andric   case AsmToken::Star:
15510b57cec5SDimitry Andric     Kind = MCBinaryExpr::Mul;
15520b57cec5SDimitry Andric     return 6;
15530b57cec5SDimitry Andric   case AsmToken::Slash:
15540b57cec5SDimitry Andric     Kind = MCBinaryExpr::Div;
15550b57cec5SDimitry Andric     return 6;
15560b57cec5SDimitry Andric   case AsmToken::Percent:
15570b57cec5SDimitry Andric     Kind = MCBinaryExpr::Mod;
15580b57cec5SDimitry Andric     return 6;
15590b57cec5SDimitry Andric   }
15600b57cec5SDimitry Andric }
15610b57cec5SDimitry Andric 
15620b57cec5SDimitry Andric static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K,
15630b57cec5SDimitry Andric                                       MCBinaryExpr::Opcode &Kind,
15640b57cec5SDimitry Andric                                       bool ShouldUseLogicalShr) {
15650b57cec5SDimitry Andric   switch (K) {
15660b57cec5SDimitry Andric   default:
15670b57cec5SDimitry Andric     return 0; // not a binop.
15680b57cec5SDimitry Andric 
15690b57cec5SDimitry Andric   // Lowest Precedence: &&, ||
15700b57cec5SDimitry Andric   case AsmToken::AmpAmp:
15710b57cec5SDimitry Andric     Kind = MCBinaryExpr::LAnd;
15720b57cec5SDimitry Andric     return 2;
15730b57cec5SDimitry Andric   case AsmToken::PipePipe:
15740b57cec5SDimitry Andric     Kind = MCBinaryExpr::LOr;
15750b57cec5SDimitry Andric     return 1;
15760b57cec5SDimitry Andric 
15770b57cec5SDimitry Andric   // Low Precedence: ==, !=, <>, <, <=, >, >=
15780b57cec5SDimitry Andric   case AsmToken::EqualEqual:
15790b57cec5SDimitry Andric     Kind = MCBinaryExpr::EQ;
15800b57cec5SDimitry Andric     return 3;
15810b57cec5SDimitry Andric   case AsmToken::ExclaimEqual:
15820b57cec5SDimitry Andric   case AsmToken::LessGreater:
15830b57cec5SDimitry Andric     Kind = MCBinaryExpr::NE;
15840b57cec5SDimitry Andric     return 3;
15850b57cec5SDimitry Andric   case AsmToken::Less:
15860b57cec5SDimitry Andric     Kind = MCBinaryExpr::LT;
15870b57cec5SDimitry Andric     return 3;
15880b57cec5SDimitry Andric   case AsmToken::LessEqual:
15890b57cec5SDimitry Andric     Kind = MCBinaryExpr::LTE;
15900b57cec5SDimitry Andric     return 3;
15910b57cec5SDimitry Andric   case AsmToken::Greater:
15920b57cec5SDimitry Andric     Kind = MCBinaryExpr::GT;
15930b57cec5SDimitry Andric     return 3;
15940b57cec5SDimitry Andric   case AsmToken::GreaterEqual:
15950b57cec5SDimitry Andric     Kind = MCBinaryExpr::GTE;
15960b57cec5SDimitry Andric     return 3;
15970b57cec5SDimitry Andric 
15980b57cec5SDimitry Andric   // Low Intermediate Precedence: +, -
15990b57cec5SDimitry Andric   case AsmToken::Plus:
16000b57cec5SDimitry Andric     Kind = MCBinaryExpr::Add;
16010b57cec5SDimitry Andric     return 4;
16020b57cec5SDimitry Andric   case AsmToken::Minus:
16030b57cec5SDimitry Andric     Kind = MCBinaryExpr::Sub;
16040b57cec5SDimitry Andric     return 4;
16050b57cec5SDimitry Andric 
16060b57cec5SDimitry Andric   // High Intermediate Precedence: |, &, ^
16070b57cec5SDimitry Andric   //
16080b57cec5SDimitry Andric   // FIXME: gas seems to support '!' as an infix operator?
16090b57cec5SDimitry Andric   case AsmToken::Pipe:
16100b57cec5SDimitry Andric     Kind = MCBinaryExpr::Or;
16110b57cec5SDimitry Andric     return 5;
16120b57cec5SDimitry Andric   case AsmToken::Caret:
16130b57cec5SDimitry Andric     Kind = MCBinaryExpr::Xor;
16140b57cec5SDimitry Andric     return 5;
16150b57cec5SDimitry Andric   case AsmToken::Amp:
16160b57cec5SDimitry Andric     Kind = MCBinaryExpr::And;
16170b57cec5SDimitry Andric     return 5;
16180b57cec5SDimitry Andric 
16190b57cec5SDimitry Andric   // Highest Precedence: *, /, %, <<, >>
16200b57cec5SDimitry Andric   case AsmToken::Star:
16210b57cec5SDimitry Andric     Kind = MCBinaryExpr::Mul;
16220b57cec5SDimitry Andric     return 6;
16230b57cec5SDimitry Andric   case AsmToken::Slash:
16240b57cec5SDimitry Andric     Kind = MCBinaryExpr::Div;
16250b57cec5SDimitry Andric     return 6;
16260b57cec5SDimitry Andric   case AsmToken::Percent:
16270b57cec5SDimitry Andric     Kind = MCBinaryExpr::Mod;
16280b57cec5SDimitry Andric     return 6;
16290b57cec5SDimitry Andric   case AsmToken::LessLess:
16300b57cec5SDimitry Andric     Kind = MCBinaryExpr::Shl;
16310b57cec5SDimitry Andric     return 6;
16320b57cec5SDimitry Andric   case AsmToken::GreaterGreater:
16330b57cec5SDimitry Andric     Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
16340b57cec5SDimitry Andric     return 6;
16350b57cec5SDimitry Andric   }
16360b57cec5SDimitry Andric }
16370b57cec5SDimitry Andric 
16380b57cec5SDimitry Andric unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K,
16390b57cec5SDimitry Andric                                        MCBinaryExpr::Opcode &Kind) {
16400b57cec5SDimitry Andric   bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr();
16410b57cec5SDimitry Andric   return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr)
16420b57cec5SDimitry Andric                   : getGNUBinOpPrecedence(K, Kind, ShouldUseLogicalShr);
16430b57cec5SDimitry Andric }
16440b57cec5SDimitry Andric 
16450b57cec5SDimitry Andric /// Parse all binary operators with precedence >= 'Precedence'.
16460b57cec5SDimitry Andric /// Res contains the LHS of the expression on input.
16470b57cec5SDimitry Andric bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
16480b57cec5SDimitry Andric                               SMLoc &EndLoc) {
16490b57cec5SDimitry Andric   SMLoc StartLoc = Lexer.getLoc();
16500b57cec5SDimitry Andric   while (true) {
16510b57cec5SDimitry Andric     MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
16520b57cec5SDimitry Andric     unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
16530b57cec5SDimitry Andric 
16540b57cec5SDimitry Andric     // If the next token is lower precedence than we are allowed to eat, return
16550b57cec5SDimitry Andric     // successfully with what we ate already.
16560b57cec5SDimitry Andric     if (TokPrec < Precedence)
16570b57cec5SDimitry Andric       return false;
16580b57cec5SDimitry Andric 
16590b57cec5SDimitry Andric     Lex();
16600b57cec5SDimitry Andric 
16610b57cec5SDimitry Andric     // Eat the next primary expression.
16620b57cec5SDimitry Andric     const MCExpr *RHS;
16630b57cec5SDimitry Andric     if (getTargetParser().parsePrimaryExpr(RHS, EndLoc))
16640b57cec5SDimitry Andric       return true;
16650b57cec5SDimitry Andric 
16660b57cec5SDimitry Andric     // If BinOp binds less tightly with RHS than the operator after RHS, let
16670b57cec5SDimitry Andric     // the pending operator take RHS as its LHS.
16680b57cec5SDimitry Andric     MCBinaryExpr::Opcode Dummy;
16690b57cec5SDimitry Andric     unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
16700b57cec5SDimitry Andric     if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
16710b57cec5SDimitry Andric       return true;
16720b57cec5SDimitry Andric 
16730b57cec5SDimitry Andric     // Merge LHS and RHS according to operator.
16740b57cec5SDimitry Andric     Res = MCBinaryExpr::create(Kind, Res, RHS, getContext(), StartLoc);
16750b57cec5SDimitry Andric   }
16760b57cec5SDimitry Andric }
16770b57cec5SDimitry Andric 
16780b57cec5SDimitry Andric /// ParseStatement:
16790b57cec5SDimitry Andric ///   ::= EndOfStatement
16800b57cec5SDimitry Andric ///   ::= Label* Directive ...Operands... EndOfStatement
16810b57cec5SDimitry Andric ///   ::= Label* Identifier OperandList* EndOfStatement
16820b57cec5SDimitry Andric bool AsmParser::parseStatement(ParseStatementInfo &Info,
16830b57cec5SDimitry Andric                                MCAsmParserSemaCallback *SI) {
16840b57cec5SDimitry Andric   assert(!hasPendingError() && "parseStatement started with pending error");
16850b57cec5SDimitry Andric   // Eat initial spaces and comments
16860b57cec5SDimitry Andric   while (Lexer.is(AsmToken::Space))
16870b57cec5SDimitry Andric     Lex();
16880b57cec5SDimitry Andric   if (Lexer.is(AsmToken::EndOfStatement)) {
16890b57cec5SDimitry Andric     // if this is a line comment we can drop it safely
16900b57cec5SDimitry Andric     if (getTok().getString().empty() || getTok().getString().front() == '\r' ||
16910b57cec5SDimitry Andric         getTok().getString().front() == '\n')
16920b57cec5SDimitry Andric       Out.AddBlankLine();
16930b57cec5SDimitry Andric     Lex();
16940b57cec5SDimitry Andric     return false;
16950b57cec5SDimitry Andric   }
16960b57cec5SDimitry Andric   // Statements always start with an identifier.
16970b57cec5SDimitry Andric   AsmToken ID = getTok();
16980b57cec5SDimitry Andric   SMLoc IDLoc = ID.getLoc();
16990b57cec5SDimitry Andric   StringRef IDVal;
17000b57cec5SDimitry Andric   int64_t LocalLabelVal = -1;
17010b57cec5SDimitry Andric   if (Lexer.is(AsmToken::HashDirective))
1702*5ffd83dbSDimitry Andric     return parseCppHashLineFilenameComment(IDLoc,
1703*5ffd83dbSDimitry Andric                                            !isInsideMacroInstantiation());
1704*5ffd83dbSDimitry Andric 
17050b57cec5SDimitry Andric   // Allow an integer followed by a ':' as a directional local label.
17060b57cec5SDimitry Andric   if (Lexer.is(AsmToken::Integer)) {
17070b57cec5SDimitry Andric     LocalLabelVal = getTok().getIntVal();
17080b57cec5SDimitry Andric     if (LocalLabelVal < 0) {
17090b57cec5SDimitry Andric       if (!TheCondState.Ignore) {
17100b57cec5SDimitry Andric         Lex(); // always eat a token
17110b57cec5SDimitry Andric         return Error(IDLoc, "unexpected token at start of statement");
17120b57cec5SDimitry Andric       }
17130b57cec5SDimitry Andric       IDVal = "";
17140b57cec5SDimitry Andric     } else {
17150b57cec5SDimitry Andric       IDVal = getTok().getString();
17160b57cec5SDimitry Andric       Lex(); // Consume the integer token to be used as an identifier token.
17170b57cec5SDimitry Andric       if (Lexer.getKind() != AsmToken::Colon) {
17180b57cec5SDimitry Andric         if (!TheCondState.Ignore) {
17190b57cec5SDimitry Andric           Lex(); // always eat a token
17200b57cec5SDimitry Andric           return Error(IDLoc, "unexpected token at start of statement");
17210b57cec5SDimitry Andric         }
17220b57cec5SDimitry Andric       }
17230b57cec5SDimitry Andric     }
17240b57cec5SDimitry Andric   } else if (Lexer.is(AsmToken::Dot)) {
17250b57cec5SDimitry Andric     // Treat '.' as a valid identifier in this context.
17260b57cec5SDimitry Andric     Lex();
17270b57cec5SDimitry Andric     IDVal = ".";
17280b57cec5SDimitry Andric   } else if (Lexer.is(AsmToken::LCurly)) {
17290b57cec5SDimitry Andric     // Treat '{' as a valid identifier in this context.
17300b57cec5SDimitry Andric     Lex();
17310b57cec5SDimitry Andric     IDVal = "{";
17320b57cec5SDimitry Andric 
17330b57cec5SDimitry Andric   } else if (Lexer.is(AsmToken::RCurly)) {
17340b57cec5SDimitry Andric     // Treat '}' as a valid identifier in this context.
17350b57cec5SDimitry Andric     Lex();
17360b57cec5SDimitry Andric     IDVal = "}";
17370b57cec5SDimitry Andric   } else if (Lexer.is(AsmToken::Star) &&
17380b57cec5SDimitry Andric              getTargetParser().starIsStartOfStatement()) {
17390b57cec5SDimitry Andric     // Accept '*' as a valid start of statement.
17400b57cec5SDimitry Andric     Lex();
17410b57cec5SDimitry Andric     IDVal = "*";
17420b57cec5SDimitry Andric   } else if (parseIdentifier(IDVal)) {
17430b57cec5SDimitry Andric     if (!TheCondState.Ignore) {
17440b57cec5SDimitry Andric       Lex(); // always eat a token
17450b57cec5SDimitry Andric       return Error(IDLoc, "unexpected token at start of statement");
17460b57cec5SDimitry Andric     }
17470b57cec5SDimitry Andric     IDVal = "";
17480b57cec5SDimitry Andric   }
17490b57cec5SDimitry Andric 
17500b57cec5SDimitry Andric   // Handle conditional assembly here before checking for skipping.  We
17510b57cec5SDimitry Andric   // have to do this so that .endif isn't skipped in a ".if 0" block for
17520b57cec5SDimitry Andric   // example.
17530b57cec5SDimitry Andric   StringMap<DirectiveKind>::const_iterator DirKindIt =
1754*5ffd83dbSDimitry Andric       DirectiveKindMap.find(IDVal.lower());
17550b57cec5SDimitry Andric   DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
17568bcb0991SDimitry Andric 
17570b57cec5SDimitry Andric                               ? DK_NO_DIRECTIVE
17580b57cec5SDimitry Andric                               : DirKindIt->getValue();
17590b57cec5SDimitry Andric   switch (DirKind) {
17600b57cec5SDimitry Andric   default:
17610b57cec5SDimitry Andric     break;
17620b57cec5SDimitry Andric   case DK_IF:
17630b57cec5SDimitry Andric   case DK_IFEQ:
17640b57cec5SDimitry Andric   case DK_IFGE:
17650b57cec5SDimitry Andric   case DK_IFGT:
17660b57cec5SDimitry Andric   case DK_IFLE:
17670b57cec5SDimitry Andric   case DK_IFLT:
17680b57cec5SDimitry Andric   case DK_IFNE:
17690b57cec5SDimitry Andric     return parseDirectiveIf(IDLoc, DirKind);
17700b57cec5SDimitry Andric   case DK_IFB:
17710b57cec5SDimitry Andric     return parseDirectiveIfb(IDLoc, true);
17720b57cec5SDimitry Andric   case DK_IFNB:
17730b57cec5SDimitry Andric     return parseDirectiveIfb(IDLoc, false);
17740b57cec5SDimitry Andric   case DK_IFC:
17750b57cec5SDimitry Andric     return parseDirectiveIfc(IDLoc, true);
17760b57cec5SDimitry Andric   case DK_IFEQS:
17770b57cec5SDimitry Andric     return parseDirectiveIfeqs(IDLoc, true);
17780b57cec5SDimitry Andric   case DK_IFNC:
17790b57cec5SDimitry Andric     return parseDirectiveIfc(IDLoc, false);
17800b57cec5SDimitry Andric   case DK_IFNES:
17810b57cec5SDimitry Andric     return parseDirectiveIfeqs(IDLoc, false);
17820b57cec5SDimitry Andric   case DK_IFDEF:
17830b57cec5SDimitry Andric     return parseDirectiveIfdef(IDLoc, true);
17840b57cec5SDimitry Andric   case DK_IFNDEF:
17850b57cec5SDimitry Andric   case DK_IFNOTDEF:
17860b57cec5SDimitry Andric     return parseDirectiveIfdef(IDLoc, false);
17870b57cec5SDimitry Andric   case DK_ELSEIF:
17880b57cec5SDimitry Andric     return parseDirectiveElseIf(IDLoc);
17890b57cec5SDimitry Andric   case DK_ELSE:
17900b57cec5SDimitry Andric     return parseDirectiveElse(IDLoc);
17910b57cec5SDimitry Andric   case DK_ENDIF:
17920b57cec5SDimitry Andric     return parseDirectiveEndIf(IDLoc);
17930b57cec5SDimitry Andric   }
17940b57cec5SDimitry Andric 
17950b57cec5SDimitry Andric   // Ignore the statement if in the middle of inactive conditional
17960b57cec5SDimitry Andric   // (e.g. ".if 0").
17970b57cec5SDimitry Andric   if (TheCondState.Ignore) {
17980b57cec5SDimitry Andric     eatToEndOfStatement();
17990b57cec5SDimitry Andric     return false;
18000b57cec5SDimitry Andric   }
18010b57cec5SDimitry Andric 
18020b57cec5SDimitry Andric   // FIXME: Recurse on local labels?
18030b57cec5SDimitry Andric 
18040b57cec5SDimitry Andric   // See what kind of statement we have.
18050b57cec5SDimitry Andric   switch (Lexer.getKind()) {
18060b57cec5SDimitry Andric   case AsmToken::Colon: {
18070b57cec5SDimitry Andric     if (!getTargetParser().isLabel(ID))
18080b57cec5SDimitry Andric       break;
18090b57cec5SDimitry Andric     if (checkForValidSection())
18100b57cec5SDimitry Andric       return true;
18110b57cec5SDimitry Andric 
18120b57cec5SDimitry Andric     // identifier ':'   -> Label.
18130b57cec5SDimitry Andric     Lex();
18140b57cec5SDimitry Andric 
18150b57cec5SDimitry Andric     // Diagnose attempt to use '.' as a label.
18160b57cec5SDimitry Andric     if (IDVal == ".")
18170b57cec5SDimitry Andric       return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
18180b57cec5SDimitry Andric 
18190b57cec5SDimitry Andric     // Diagnose attempt to use a variable as a label.
18200b57cec5SDimitry Andric     //
18210b57cec5SDimitry Andric     // FIXME: Diagnostics. Note the location of the definition as a label.
18220b57cec5SDimitry Andric     // FIXME: This doesn't diagnose assignment to a symbol which has been
18230b57cec5SDimitry Andric     // implicitly marked as external.
18240b57cec5SDimitry Andric     MCSymbol *Sym;
18250b57cec5SDimitry Andric     if (LocalLabelVal == -1) {
1826*5ffd83dbSDimitry Andric       if (ParsingMSInlineAsm && SI) {
18270b57cec5SDimitry Andric         StringRef RewrittenLabel =
18280b57cec5SDimitry Andric             SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
18290b57cec5SDimitry Andric         assert(!RewrittenLabel.empty() &&
18300b57cec5SDimitry Andric                "We should have an internal name here.");
18310b57cec5SDimitry Andric         Info.AsmRewrites->emplace_back(AOK_Label, IDLoc, IDVal.size(),
18320b57cec5SDimitry Andric                                        RewrittenLabel);
18330b57cec5SDimitry Andric         IDVal = RewrittenLabel;
18340b57cec5SDimitry Andric       }
18350b57cec5SDimitry Andric       Sym = getContext().getOrCreateSymbol(IDVal);
18360b57cec5SDimitry Andric     } else
18370b57cec5SDimitry Andric       Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal);
18380b57cec5SDimitry Andric     // End of Labels should be treated as end of line for lexing
18390b57cec5SDimitry Andric     // purposes but that information is not available to the Lexer who
18400b57cec5SDimitry Andric     // does not understand Labels. This may cause us to see a Hash
18410b57cec5SDimitry Andric     // here instead of a preprocessor line comment.
18420b57cec5SDimitry Andric     if (getTok().is(AsmToken::Hash)) {
18430b57cec5SDimitry Andric       StringRef CommentStr = parseStringToEndOfStatement();
18440b57cec5SDimitry Andric       Lexer.Lex();
18450b57cec5SDimitry Andric       Lexer.UnLex(AsmToken(AsmToken::EndOfStatement, CommentStr));
18460b57cec5SDimitry Andric     }
18470b57cec5SDimitry Andric 
18480b57cec5SDimitry Andric     // Consume any end of statement token, if present, to avoid spurious
18490b57cec5SDimitry Andric     // AddBlankLine calls().
18500b57cec5SDimitry Andric     if (getTok().is(AsmToken::EndOfStatement)) {
18510b57cec5SDimitry Andric       Lex();
18520b57cec5SDimitry Andric     }
18530b57cec5SDimitry Andric 
18540b57cec5SDimitry Andric     getTargetParser().doBeforeLabelEmit(Sym);
18550b57cec5SDimitry Andric 
18560b57cec5SDimitry Andric     // Emit the label.
1857*5ffd83dbSDimitry Andric     if (!getTargetParser().isParsingMSInlineAsm())
1858*5ffd83dbSDimitry Andric       Out.emitLabel(Sym, IDLoc);
18590b57cec5SDimitry Andric 
18600b57cec5SDimitry Andric     // If we are generating dwarf for assembly source files then gather the
18610b57cec5SDimitry Andric     // info to make a dwarf label entry for this label if needed.
18620b57cec5SDimitry Andric     if (enabledGenDwarfForAssembly())
18630b57cec5SDimitry Andric       MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
18640b57cec5SDimitry Andric                                  IDLoc);
18650b57cec5SDimitry Andric 
18660b57cec5SDimitry Andric     getTargetParser().onLabelParsed(Sym);
18670b57cec5SDimitry Andric 
18680b57cec5SDimitry Andric     return false;
18690b57cec5SDimitry Andric   }
18700b57cec5SDimitry Andric 
18710b57cec5SDimitry Andric   case AsmToken::Equal:
18720b57cec5SDimitry Andric     if (!getTargetParser().equalIsAsmAssignment())
18730b57cec5SDimitry Andric       break;
18740b57cec5SDimitry Andric     // identifier '=' ... -> assignment statement
18750b57cec5SDimitry Andric     Lex();
18760b57cec5SDimitry Andric 
18770b57cec5SDimitry Andric     return parseAssignment(IDVal, true);
18780b57cec5SDimitry Andric 
18790b57cec5SDimitry Andric   default: // Normal instruction or directive.
18800b57cec5SDimitry Andric     break;
18810b57cec5SDimitry Andric   }
18820b57cec5SDimitry Andric 
18830b57cec5SDimitry Andric   // If macros are enabled, check to see if this is a macro instantiation.
18840b57cec5SDimitry Andric   if (areMacrosEnabled())
18850b57cec5SDimitry Andric     if (const MCAsmMacro *M = getContext().lookupMacro(IDVal)) {
18860b57cec5SDimitry Andric       return handleMacroEntry(M, IDLoc);
18870b57cec5SDimitry Andric     }
18880b57cec5SDimitry Andric 
18890b57cec5SDimitry Andric   // Otherwise, we have a normal instruction or directive.
18900b57cec5SDimitry Andric 
18910b57cec5SDimitry Andric   // Directives start with "."
18920b57cec5SDimitry Andric   if (IDVal.startswith(".") && IDVal != ".") {
18930b57cec5SDimitry Andric     // There are several entities interested in parsing directives:
18940b57cec5SDimitry Andric     //
18950b57cec5SDimitry Andric     // 1. The target-specific assembly parser. Some directives are target
18960b57cec5SDimitry Andric     //    specific or may potentially behave differently on certain targets.
18970b57cec5SDimitry Andric     // 2. Asm parser extensions. For example, platform-specific parsers
18980b57cec5SDimitry Andric     //    (like the ELF parser) register themselves as extensions.
18990b57cec5SDimitry Andric     // 3. The generic directive parser implemented by this class. These are
19000b57cec5SDimitry Andric     //    all the directives that behave in a target and platform independent
19010b57cec5SDimitry Andric     //    manner, or at least have a default behavior that's shared between
19020b57cec5SDimitry Andric     //    all targets and platforms.
19030b57cec5SDimitry Andric 
19040b57cec5SDimitry Andric     getTargetParser().flushPendingInstructions(getStreamer());
19050b57cec5SDimitry Andric 
19060b57cec5SDimitry Andric     SMLoc StartTokLoc = getTok().getLoc();
19070b57cec5SDimitry Andric     bool TPDirectiveReturn = getTargetParser().ParseDirective(ID);
19080b57cec5SDimitry Andric 
19090b57cec5SDimitry Andric     if (hasPendingError())
19100b57cec5SDimitry Andric       return true;
19110b57cec5SDimitry Andric     // Currently the return value should be true if we are
19120b57cec5SDimitry Andric     // uninterested but as this is at odds with the standard parsing
19130b57cec5SDimitry Andric     // convention (return true = error) we have instances of a parsed
19140b57cec5SDimitry Andric     // directive that fails returning true as an error. Catch these
19150b57cec5SDimitry Andric     // cases as best as possible errors here.
19160b57cec5SDimitry Andric     if (TPDirectiveReturn && StartTokLoc != getTok().getLoc())
19170b57cec5SDimitry Andric       return true;
19180b57cec5SDimitry Andric     // Return if we did some parsing or believe we succeeded.
19190b57cec5SDimitry Andric     if (!TPDirectiveReturn || StartTokLoc != getTok().getLoc())
19200b57cec5SDimitry Andric       return false;
19210b57cec5SDimitry Andric 
19220b57cec5SDimitry Andric     // Next, check the extension directive map to see if any extension has
19230b57cec5SDimitry Andric     // registered itself to parse this directive.
19240b57cec5SDimitry Andric     std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
19250b57cec5SDimitry Andric         ExtensionDirectiveMap.lookup(IDVal);
19260b57cec5SDimitry Andric     if (Handler.first)
19270b57cec5SDimitry Andric       return (*Handler.second)(Handler.first, IDVal, IDLoc);
19280b57cec5SDimitry Andric 
19290b57cec5SDimitry Andric     // Finally, if no one else is interested in this directive, it must be
19300b57cec5SDimitry Andric     // generic and familiar to this class.
19310b57cec5SDimitry Andric     switch (DirKind) {
19320b57cec5SDimitry Andric     default:
19330b57cec5SDimitry Andric       break;
19340b57cec5SDimitry Andric     case DK_SET:
19350b57cec5SDimitry Andric     case DK_EQU:
19360b57cec5SDimitry Andric       return parseDirectiveSet(IDVal, true);
19370b57cec5SDimitry Andric     case DK_EQUIV:
19380b57cec5SDimitry Andric       return parseDirectiveSet(IDVal, false);
19390b57cec5SDimitry Andric     case DK_ASCII:
19400b57cec5SDimitry Andric       return parseDirectiveAscii(IDVal, false);
19410b57cec5SDimitry Andric     case DK_ASCIZ:
19420b57cec5SDimitry Andric     case DK_STRING:
19430b57cec5SDimitry Andric       return parseDirectiveAscii(IDVal, true);
19440b57cec5SDimitry Andric     case DK_BYTE:
19450b57cec5SDimitry Andric     case DK_DC_B:
19460b57cec5SDimitry Andric       return parseDirectiveValue(IDVal, 1);
19470b57cec5SDimitry Andric     case DK_DC:
19480b57cec5SDimitry Andric     case DK_DC_W:
19490b57cec5SDimitry Andric     case DK_SHORT:
19500b57cec5SDimitry Andric     case DK_VALUE:
19510b57cec5SDimitry Andric     case DK_2BYTE:
19520b57cec5SDimitry Andric       return parseDirectiveValue(IDVal, 2);
19530b57cec5SDimitry Andric     case DK_LONG:
19540b57cec5SDimitry Andric     case DK_INT:
19550b57cec5SDimitry Andric     case DK_4BYTE:
19560b57cec5SDimitry Andric     case DK_DC_L:
19570b57cec5SDimitry Andric       return parseDirectiveValue(IDVal, 4);
19580b57cec5SDimitry Andric     case DK_QUAD:
19590b57cec5SDimitry Andric     case DK_8BYTE:
19600b57cec5SDimitry Andric       return parseDirectiveValue(IDVal, 8);
19610b57cec5SDimitry Andric     case DK_DC_A:
19620b57cec5SDimitry Andric       return parseDirectiveValue(
19630b57cec5SDimitry Andric           IDVal, getContext().getAsmInfo()->getCodePointerSize());
19640b57cec5SDimitry Andric     case DK_OCTA:
19650b57cec5SDimitry Andric       return parseDirectiveOctaValue(IDVal);
19660b57cec5SDimitry Andric     case DK_SINGLE:
19670b57cec5SDimitry Andric     case DK_FLOAT:
19680b57cec5SDimitry Andric     case DK_DC_S:
19690b57cec5SDimitry Andric       return parseDirectiveRealValue(IDVal, APFloat::IEEEsingle());
19700b57cec5SDimitry Andric     case DK_DOUBLE:
19710b57cec5SDimitry Andric     case DK_DC_D:
19720b57cec5SDimitry Andric       return parseDirectiveRealValue(IDVal, APFloat::IEEEdouble());
19730b57cec5SDimitry Andric     case DK_ALIGN: {
19740b57cec5SDimitry Andric       bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
19750b57cec5SDimitry Andric       return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
19760b57cec5SDimitry Andric     }
19770b57cec5SDimitry Andric     case DK_ALIGN32: {
19780b57cec5SDimitry Andric       bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
19790b57cec5SDimitry Andric       return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
19800b57cec5SDimitry Andric     }
19810b57cec5SDimitry Andric     case DK_BALIGN:
19820b57cec5SDimitry Andric       return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
19830b57cec5SDimitry Andric     case DK_BALIGNW:
19840b57cec5SDimitry Andric       return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
19850b57cec5SDimitry Andric     case DK_BALIGNL:
19860b57cec5SDimitry Andric       return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
19870b57cec5SDimitry Andric     case DK_P2ALIGN:
19880b57cec5SDimitry Andric       return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
19890b57cec5SDimitry Andric     case DK_P2ALIGNW:
19900b57cec5SDimitry Andric       return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
19910b57cec5SDimitry Andric     case DK_P2ALIGNL:
19920b57cec5SDimitry Andric       return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
19930b57cec5SDimitry Andric     case DK_ORG:
19940b57cec5SDimitry Andric       return parseDirectiveOrg();
19950b57cec5SDimitry Andric     case DK_FILL:
19960b57cec5SDimitry Andric       return parseDirectiveFill();
19970b57cec5SDimitry Andric     case DK_ZERO:
19980b57cec5SDimitry Andric       return parseDirectiveZero();
19990b57cec5SDimitry Andric     case DK_EXTERN:
20000b57cec5SDimitry Andric       eatToEndOfStatement(); // .extern is the default, ignore it.
20010b57cec5SDimitry Andric       return false;
20020b57cec5SDimitry Andric     case DK_GLOBL:
20030b57cec5SDimitry Andric     case DK_GLOBAL:
20040b57cec5SDimitry Andric       return parseDirectiveSymbolAttribute(MCSA_Global);
20050b57cec5SDimitry Andric     case DK_LAZY_REFERENCE:
20060b57cec5SDimitry Andric       return parseDirectiveSymbolAttribute(MCSA_LazyReference);
20070b57cec5SDimitry Andric     case DK_NO_DEAD_STRIP:
20080b57cec5SDimitry Andric       return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
20090b57cec5SDimitry Andric     case DK_SYMBOL_RESOLVER:
20100b57cec5SDimitry Andric       return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
20110b57cec5SDimitry Andric     case DK_PRIVATE_EXTERN:
20120b57cec5SDimitry Andric       return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
20130b57cec5SDimitry Andric     case DK_REFERENCE:
20140b57cec5SDimitry Andric       return parseDirectiveSymbolAttribute(MCSA_Reference);
20150b57cec5SDimitry Andric     case DK_WEAK_DEFINITION:
20160b57cec5SDimitry Andric       return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
20170b57cec5SDimitry Andric     case DK_WEAK_REFERENCE:
20180b57cec5SDimitry Andric       return parseDirectiveSymbolAttribute(MCSA_WeakReference);
20190b57cec5SDimitry Andric     case DK_WEAK_DEF_CAN_BE_HIDDEN:
20200b57cec5SDimitry Andric       return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
20210b57cec5SDimitry Andric     case DK_COLD:
20220b57cec5SDimitry Andric       return parseDirectiveSymbolAttribute(MCSA_Cold);
20230b57cec5SDimitry Andric     case DK_COMM:
20240b57cec5SDimitry Andric     case DK_COMMON:
20250b57cec5SDimitry Andric       return parseDirectiveComm(/*IsLocal=*/false);
20260b57cec5SDimitry Andric     case DK_LCOMM:
20270b57cec5SDimitry Andric       return parseDirectiveComm(/*IsLocal=*/true);
20280b57cec5SDimitry Andric     case DK_ABORT:
20290b57cec5SDimitry Andric       return parseDirectiveAbort();
20300b57cec5SDimitry Andric     case DK_INCLUDE:
20310b57cec5SDimitry Andric       return parseDirectiveInclude();
20320b57cec5SDimitry Andric     case DK_INCBIN:
20330b57cec5SDimitry Andric       return parseDirectiveIncbin();
20340b57cec5SDimitry Andric     case DK_CODE16:
20350b57cec5SDimitry Andric     case DK_CODE16GCC:
20360b57cec5SDimitry Andric       return TokError(Twine(IDVal) +
20370b57cec5SDimitry Andric                       " not currently supported for this target");
20380b57cec5SDimitry Andric     case DK_REPT:
20390b57cec5SDimitry Andric       return parseDirectiveRept(IDLoc, IDVal);
20400b57cec5SDimitry Andric     case DK_IRP:
20410b57cec5SDimitry Andric       return parseDirectiveIrp(IDLoc);
20420b57cec5SDimitry Andric     case DK_IRPC:
20430b57cec5SDimitry Andric       return parseDirectiveIrpc(IDLoc);
20440b57cec5SDimitry Andric     case DK_ENDR:
20450b57cec5SDimitry Andric       return parseDirectiveEndr(IDLoc);
20460b57cec5SDimitry Andric     case DK_BUNDLE_ALIGN_MODE:
20470b57cec5SDimitry Andric       return parseDirectiveBundleAlignMode();
20480b57cec5SDimitry Andric     case DK_BUNDLE_LOCK:
20490b57cec5SDimitry Andric       return parseDirectiveBundleLock();
20500b57cec5SDimitry Andric     case DK_BUNDLE_UNLOCK:
20510b57cec5SDimitry Andric       return parseDirectiveBundleUnlock();
20520b57cec5SDimitry Andric     case DK_SLEB128:
20530b57cec5SDimitry Andric       return parseDirectiveLEB128(true);
20540b57cec5SDimitry Andric     case DK_ULEB128:
20550b57cec5SDimitry Andric       return parseDirectiveLEB128(false);
20560b57cec5SDimitry Andric     case DK_SPACE:
20570b57cec5SDimitry Andric     case DK_SKIP:
20580b57cec5SDimitry Andric       return parseDirectiveSpace(IDVal);
20590b57cec5SDimitry Andric     case DK_FILE:
20600b57cec5SDimitry Andric       return parseDirectiveFile(IDLoc);
20610b57cec5SDimitry Andric     case DK_LINE:
20620b57cec5SDimitry Andric       return parseDirectiveLine();
20630b57cec5SDimitry Andric     case DK_LOC:
20640b57cec5SDimitry Andric       return parseDirectiveLoc();
20650b57cec5SDimitry Andric     case DK_STABS:
20660b57cec5SDimitry Andric       return parseDirectiveStabs();
20670b57cec5SDimitry Andric     case DK_CV_FILE:
20680b57cec5SDimitry Andric       return parseDirectiveCVFile();
20690b57cec5SDimitry Andric     case DK_CV_FUNC_ID:
20700b57cec5SDimitry Andric       return parseDirectiveCVFuncId();
20710b57cec5SDimitry Andric     case DK_CV_INLINE_SITE_ID:
20720b57cec5SDimitry Andric       return parseDirectiveCVInlineSiteId();
20730b57cec5SDimitry Andric     case DK_CV_LOC:
20740b57cec5SDimitry Andric       return parseDirectiveCVLoc();
20750b57cec5SDimitry Andric     case DK_CV_LINETABLE:
20760b57cec5SDimitry Andric       return parseDirectiveCVLinetable();
20770b57cec5SDimitry Andric     case DK_CV_INLINE_LINETABLE:
20780b57cec5SDimitry Andric       return parseDirectiveCVInlineLinetable();
20790b57cec5SDimitry Andric     case DK_CV_DEF_RANGE:
20800b57cec5SDimitry Andric       return parseDirectiveCVDefRange();
20810b57cec5SDimitry Andric     case DK_CV_STRING:
20820b57cec5SDimitry Andric       return parseDirectiveCVString();
20830b57cec5SDimitry Andric     case DK_CV_STRINGTABLE:
20840b57cec5SDimitry Andric       return parseDirectiveCVStringTable();
20850b57cec5SDimitry Andric     case DK_CV_FILECHECKSUMS:
20860b57cec5SDimitry Andric       return parseDirectiveCVFileChecksums();
20870b57cec5SDimitry Andric     case DK_CV_FILECHECKSUM_OFFSET:
20880b57cec5SDimitry Andric       return parseDirectiveCVFileChecksumOffset();
20890b57cec5SDimitry Andric     case DK_CV_FPO_DATA:
20900b57cec5SDimitry Andric       return parseDirectiveCVFPOData();
20910b57cec5SDimitry Andric     case DK_CFI_SECTIONS:
20920b57cec5SDimitry Andric       return parseDirectiveCFISections();
20930b57cec5SDimitry Andric     case DK_CFI_STARTPROC:
20940b57cec5SDimitry Andric       return parseDirectiveCFIStartProc();
20950b57cec5SDimitry Andric     case DK_CFI_ENDPROC:
20960b57cec5SDimitry Andric       return parseDirectiveCFIEndProc();
20970b57cec5SDimitry Andric     case DK_CFI_DEF_CFA:
20980b57cec5SDimitry Andric       return parseDirectiveCFIDefCfa(IDLoc);
20990b57cec5SDimitry Andric     case DK_CFI_DEF_CFA_OFFSET:
21000b57cec5SDimitry Andric       return parseDirectiveCFIDefCfaOffset();
21010b57cec5SDimitry Andric     case DK_CFI_ADJUST_CFA_OFFSET:
21020b57cec5SDimitry Andric       return parseDirectiveCFIAdjustCfaOffset();
21030b57cec5SDimitry Andric     case DK_CFI_DEF_CFA_REGISTER:
21040b57cec5SDimitry Andric       return parseDirectiveCFIDefCfaRegister(IDLoc);
21050b57cec5SDimitry Andric     case DK_CFI_OFFSET:
21060b57cec5SDimitry Andric       return parseDirectiveCFIOffset(IDLoc);
21070b57cec5SDimitry Andric     case DK_CFI_REL_OFFSET:
21080b57cec5SDimitry Andric       return parseDirectiveCFIRelOffset(IDLoc);
21090b57cec5SDimitry Andric     case DK_CFI_PERSONALITY:
21100b57cec5SDimitry Andric       return parseDirectiveCFIPersonalityOrLsda(true);
21110b57cec5SDimitry Andric     case DK_CFI_LSDA:
21120b57cec5SDimitry Andric       return parseDirectiveCFIPersonalityOrLsda(false);
21130b57cec5SDimitry Andric     case DK_CFI_REMEMBER_STATE:
21140b57cec5SDimitry Andric       return parseDirectiveCFIRememberState();
21150b57cec5SDimitry Andric     case DK_CFI_RESTORE_STATE:
21160b57cec5SDimitry Andric       return parseDirectiveCFIRestoreState();
21170b57cec5SDimitry Andric     case DK_CFI_SAME_VALUE:
21180b57cec5SDimitry Andric       return parseDirectiveCFISameValue(IDLoc);
21190b57cec5SDimitry Andric     case DK_CFI_RESTORE:
21200b57cec5SDimitry Andric       return parseDirectiveCFIRestore(IDLoc);
21210b57cec5SDimitry Andric     case DK_CFI_ESCAPE:
21220b57cec5SDimitry Andric       return parseDirectiveCFIEscape();
21230b57cec5SDimitry Andric     case DK_CFI_RETURN_COLUMN:
21240b57cec5SDimitry Andric       return parseDirectiveCFIReturnColumn(IDLoc);
21250b57cec5SDimitry Andric     case DK_CFI_SIGNAL_FRAME:
21260b57cec5SDimitry Andric       return parseDirectiveCFISignalFrame();
21270b57cec5SDimitry Andric     case DK_CFI_UNDEFINED:
21280b57cec5SDimitry Andric       return parseDirectiveCFIUndefined(IDLoc);
21290b57cec5SDimitry Andric     case DK_CFI_REGISTER:
21300b57cec5SDimitry Andric       return parseDirectiveCFIRegister(IDLoc);
21310b57cec5SDimitry Andric     case DK_CFI_WINDOW_SAVE:
21320b57cec5SDimitry Andric       return parseDirectiveCFIWindowSave();
21330b57cec5SDimitry Andric     case DK_MACROS_ON:
21340b57cec5SDimitry Andric     case DK_MACROS_OFF:
21350b57cec5SDimitry Andric       return parseDirectiveMacrosOnOff(IDVal);
21360b57cec5SDimitry Andric     case DK_MACRO:
21370b57cec5SDimitry Andric       return parseDirectiveMacro(IDLoc);
21380b57cec5SDimitry Andric     case DK_ALTMACRO:
21390b57cec5SDimitry Andric     case DK_NOALTMACRO:
21400b57cec5SDimitry Andric       return parseDirectiveAltmacro(IDVal);
21410b57cec5SDimitry Andric     case DK_EXITM:
21420b57cec5SDimitry Andric       return parseDirectiveExitMacro(IDVal);
21430b57cec5SDimitry Andric     case DK_ENDM:
21440b57cec5SDimitry Andric     case DK_ENDMACRO:
21450b57cec5SDimitry Andric       return parseDirectiveEndMacro(IDVal);
21460b57cec5SDimitry Andric     case DK_PURGEM:
21470b57cec5SDimitry Andric       return parseDirectivePurgeMacro(IDLoc);
21480b57cec5SDimitry Andric     case DK_END:
21490b57cec5SDimitry Andric       return parseDirectiveEnd(IDLoc);
21500b57cec5SDimitry Andric     case DK_ERR:
21510b57cec5SDimitry Andric       return parseDirectiveError(IDLoc, false);
21520b57cec5SDimitry Andric     case DK_ERROR:
21530b57cec5SDimitry Andric       return parseDirectiveError(IDLoc, true);
21540b57cec5SDimitry Andric     case DK_WARNING:
21550b57cec5SDimitry Andric       return parseDirectiveWarning(IDLoc);
21560b57cec5SDimitry Andric     case DK_RELOC:
21570b57cec5SDimitry Andric       return parseDirectiveReloc(IDLoc);
21580b57cec5SDimitry Andric     case DK_DCB:
21590b57cec5SDimitry Andric     case DK_DCB_W:
21600b57cec5SDimitry Andric       return parseDirectiveDCB(IDVal, 2);
21610b57cec5SDimitry Andric     case DK_DCB_B:
21620b57cec5SDimitry Andric       return parseDirectiveDCB(IDVal, 1);
21630b57cec5SDimitry Andric     case DK_DCB_D:
21640b57cec5SDimitry Andric       return parseDirectiveRealDCB(IDVal, APFloat::IEEEdouble());
21650b57cec5SDimitry Andric     case DK_DCB_L:
21660b57cec5SDimitry Andric       return parseDirectiveDCB(IDVal, 4);
21670b57cec5SDimitry Andric     case DK_DCB_S:
21680b57cec5SDimitry Andric       return parseDirectiveRealDCB(IDVal, APFloat::IEEEsingle());
21690b57cec5SDimitry Andric     case DK_DC_X:
21700b57cec5SDimitry Andric     case DK_DCB_X:
21710b57cec5SDimitry Andric       return TokError(Twine(IDVal) +
21720b57cec5SDimitry Andric                       " not currently supported for this target");
21730b57cec5SDimitry Andric     case DK_DS:
21740b57cec5SDimitry Andric     case DK_DS_W:
21750b57cec5SDimitry Andric       return parseDirectiveDS(IDVal, 2);
21760b57cec5SDimitry Andric     case DK_DS_B:
21770b57cec5SDimitry Andric       return parseDirectiveDS(IDVal, 1);
21780b57cec5SDimitry Andric     case DK_DS_D:
21790b57cec5SDimitry Andric       return parseDirectiveDS(IDVal, 8);
21800b57cec5SDimitry Andric     case DK_DS_L:
21810b57cec5SDimitry Andric     case DK_DS_S:
21820b57cec5SDimitry Andric       return parseDirectiveDS(IDVal, 4);
21830b57cec5SDimitry Andric     case DK_DS_P:
21840b57cec5SDimitry Andric     case DK_DS_X:
21850b57cec5SDimitry Andric       return parseDirectiveDS(IDVal, 12);
21860b57cec5SDimitry Andric     case DK_PRINT:
21870b57cec5SDimitry Andric       return parseDirectivePrint(IDLoc);
21880b57cec5SDimitry Andric     case DK_ADDRSIG:
21890b57cec5SDimitry Andric       return parseDirectiveAddrsig();
21900b57cec5SDimitry Andric     case DK_ADDRSIG_SYM:
21910b57cec5SDimitry Andric       return parseDirectiveAddrsigSym();
21920b57cec5SDimitry Andric     }
21930b57cec5SDimitry Andric 
21940b57cec5SDimitry Andric     return Error(IDLoc, "unknown directive");
21950b57cec5SDimitry Andric   }
21960b57cec5SDimitry Andric 
21970b57cec5SDimitry Andric   // __asm _emit or __asm __emit
2198*5ffd83dbSDimitry Andric   if (ParsingMSInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
21990b57cec5SDimitry Andric                              IDVal == "_EMIT" || IDVal == "__EMIT"))
22000b57cec5SDimitry Andric     return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
22010b57cec5SDimitry Andric 
22020b57cec5SDimitry Andric   // __asm align
2203*5ffd83dbSDimitry Andric   if (ParsingMSInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
22040b57cec5SDimitry Andric     return parseDirectiveMSAlign(IDLoc, Info);
22050b57cec5SDimitry Andric 
2206*5ffd83dbSDimitry Andric   if (ParsingMSInlineAsm && (IDVal == "even" || IDVal == "EVEN"))
22070b57cec5SDimitry Andric     Info.AsmRewrites->emplace_back(AOK_EVEN, IDLoc, 4);
22080b57cec5SDimitry Andric   if (checkForValidSection())
22090b57cec5SDimitry Andric     return true;
22100b57cec5SDimitry Andric 
22110b57cec5SDimitry Andric   // Canonicalize the opcode to lower case.
22120b57cec5SDimitry Andric   std::string OpcodeStr = IDVal.lower();
22130b57cec5SDimitry Andric   ParseInstructionInfo IInfo(Info.AsmRewrites);
22140b57cec5SDimitry Andric   bool ParseHadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, ID,
22150b57cec5SDimitry Andric                                                           Info.ParsedOperands);
22160b57cec5SDimitry Andric   Info.ParseError = ParseHadError;
22170b57cec5SDimitry Andric 
22180b57cec5SDimitry Andric   // Dump the parsed representation, if requested.
22190b57cec5SDimitry Andric   if (getShowParsedOperands()) {
22200b57cec5SDimitry Andric     SmallString<256> Str;
22210b57cec5SDimitry Andric     raw_svector_ostream OS(Str);
22220b57cec5SDimitry Andric     OS << "parsed instruction: [";
22230b57cec5SDimitry Andric     for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
22240b57cec5SDimitry Andric       if (i != 0)
22250b57cec5SDimitry Andric         OS << ", ";
22260b57cec5SDimitry Andric       Info.ParsedOperands[i]->print(OS);
22270b57cec5SDimitry Andric     }
22280b57cec5SDimitry Andric     OS << "]";
22290b57cec5SDimitry Andric 
22300b57cec5SDimitry Andric     printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
22310b57cec5SDimitry Andric   }
22320b57cec5SDimitry Andric 
22330b57cec5SDimitry Andric   // Fail even if ParseInstruction erroneously returns false.
22340b57cec5SDimitry Andric   if (hasPendingError() || ParseHadError)
22350b57cec5SDimitry Andric     return true;
22360b57cec5SDimitry Andric 
22370b57cec5SDimitry Andric   // If we are generating dwarf for the current section then generate a .loc
22380b57cec5SDimitry Andric   // directive for the instruction.
22390b57cec5SDimitry Andric   if (!ParseHadError && enabledGenDwarfForAssembly() &&
22400b57cec5SDimitry Andric       getContext().getGenDwarfSectionSyms().count(
22410b57cec5SDimitry Andric           getStreamer().getCurrentSectionOnly())) {
22420b57cec5SDimitry Andric     unsigned Line;
22430b57cec5SDimitry Andric     if (ActiveMacros.empty())
22440b57cec5SDimitry Andric       Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
22450b57cec5SDimitry Andric     else
22460b57cec5SDimitry Andric       Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc,
22470b57cec5SDimitry Andric                                    ActiveMacros.front()->ExitBuffer);
22480b57cec5SDimitry Andric 
22490b57cec5SDimitry Andric     // If we previously parsed a cpp hash file line comment then make sure the
22500b57cec5SDimitry Andric     // current Dwarf File is for the CppHashFilename if not then emit the
22510b57cec5SDimitry Andric     // Dwarf File table for it and adjust the line number for the .loc.
22520b57cec5SDimitry Andric     if (!CppHashInfo.Filename.empty()) {
2253*5ffd83dbSDimitry Andric       unsigned FileNumber = getStreamer().emitDwarfFileDirective(
22540b57cec5SDimitry Andric           0, StringRef(), CppHashInfo.Filename);
22550b57cec5SDimitry Andric       getContext().setGenDwarfFileNumber(FileNumber);
22560b57cec5SDimitry Andric 
22570b57cec5SDimitry Andric       unsigned CppHashLocLineNo =
22580b57cec5SDimitry Andric         SrcMgr.FindLineNumber(CppHashInfo.Loc, CppHashInfo.Buf);
22590b57cec5SDimitry Andric       Line = CppHashInfo.LineNumber - 1 + (Line - CppHashLocLineNo);
22600b57cec5SDimitry Andric     }
22610b57cec5SDimitry Andric 
2262*5ffd83dbSDimitry Andric     getStreamer().emitDwarfLocDirective(
22630b57cec5SDimitry Andric         getContext().getGenDwarfFileNumber(), Line, 0,
22640b57cec5SDimitry Andric         DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
22650b57cec5SDimitry Andric         StringRef());
22660b57cec5SDimitry Andric   }
22670b57cec5SDimitry Andric 
22680b57cec5SDimitry Andric   // If parsing succeeded, match the instruction.
22690b57cec5SDimitry Andric   if (!ParseHadError) {
22700b57cec5SDimitry Andric     uint64_t ErrorInfo;
22710b57cec5SDimitry Andric     if (getTargetParser().MatchAndEmitInstruction(
22720b57cec5SDimitry Andric             IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo,
2273*5ffd83dbSDimitry Andric             getTargetParser().isParsingMSInlineAsm()))
22740b57cec5SDimitry Andric       return true;
22750b57cec5SDimitry Andric   }
22760b57cec5SDimitry Andric   return false;
22770b57cec5SDimitry Andric }
22780b57cec5SDimitry Andric 
22790b57cec5SDimitry Andric // Parse and erase curly braces marking block start/end
22800b57cec5SDimitry Andric bool
22810b57cec5SDimitry Andric AsmParser::parseCurlyBlockScope(SmallVectorImpl<AsmRewrite> &AsmStrRewrites) {
22820b57cec5SDimitry Andric   // Identify curly brace marking block start/end
22830b57cec5SDimitry Andric   if (Lexer.isNot(AsmToken::LCurly) && Lexer.isNot(AsmToken::RCurly))
22840b57cec5SDimitry Andric     return false;
22850b57cec5SDimitry Andric 
22860b57cec5SDimitry Andric   SMLoc StartLoc = Lexer.getLoc();
22870b57cec5SDimitry Andric   Lex(); // Eat the brace
22880b57cec5SDimitry Andric   if (Lexer.is(AsmToken::EndOfStatement))
22890b57cec5SDimitry Andric     Lex(); // Eat EndOfStatement following the brace
22900b57cec5SDimitry Andric 
22910b57cec5SDimitry Andric   // Erase the block start/end brace from the output asm string
22920b57cec5SDimitry Andric   AsmStrRewrites.emplace_back(AOK_Skip, StartLoc, Lexer.getLoc().getPointer() -
22930b57cec5SDimitry Andric                                                   StartLoc.getPointer());
22940b57cec5SDimitry Andric   return true;
22950b57cec5SDimitry Andric }
22960b57cec5SDimitry Andric 
22970b57cec5SDimitry Andric /// parseCppHashLineFilenameComment as this:
22980b57cec5SDimitry Andric ///   ::= # number "filename"
2299*5ffd83dbSDimitry Andric bool AsmParser::parseCppHashLineFilenameComment(SMLoc L, bool SaveLocInfo) {
23000b57cec5SDimitry Andric   Lex(); // Eat the hash token.
23010b57cec5SDimitry Andric   // Lexer only ever emits HashDirective if it fully formed if it's
23020b57cec5SDimitry Andric   // done the checking already so this is an internal error.
23030b57cec5SDimitry Andric   assert(getTok().is(AsmToken::Integer) &&
23040b57cec5SDimitry Andric          "Lexing Cpp line comment: Expected Integer");
23050b57cec5SDimitry Andric   int64_t LineNumber = getTok().getIntVal();
23060b57cec5SDimitry Andric   Lex();
23070b57cec5SDimitry Andric   assert(getTok().is(AsmToken::String) &&
23080b57cec5SDimitry Andric          "Lexing Cpp line comment: Expected String");
23090b57cec5SDimitry Andric   StringRef Filename = getTok().getString();
23100b57cec5SDimitry Andric   Lex();
23110b57cec5SDimitry Andric 
2312*5ffd83dbSDimitry Andric   if (!SaveLocInfo)
2313*5ffd83dbSDimitry Andric     return false;
2314*5ffd83dbSDimitry Andric 
23150b57cec5SDimitry Andric   // Get rid of the enclosing quotes.
23160b57cec5SDimitry Andric   Filename = Filename.substr(1, Filename.size() - 2);
23170b57cec5SDimitry Andric 
23180b57cec5SDimitry Andric   // Save the SMLoc, Filename and LineNumber for later use by diagnostics
23190b57cec5SDimitry Andric   // and possibly DWARF file info.
23200b57cec5SDimitry Andric   CppHashInfo.Loc = L;
23210b57cec5SDimitry Andric   CppHashInfo.Filename = Filename;
23220b57cec5SDimitry Andric   CppHashInfo.LineNumber = LineNumber;
23230b57cec5SDimitry Andric   CppHashInfo.Buf = CurBuffer;
23240b57cec5SDimitry Andric   if (FirstCppHashFilename.empty())
23250b57cec5SDimitry Andric     FirstCppHashFilename = Filename;
23260b57cec5SDimitry Andric   return false;
23270b57cec5SDimitry Andric }
23280b57cec5SDimitry Andric 
23290b57cec5SDimitry Andric /// will use the last parsed cpp hash line filename comment
23300b57cec5SDimitry Andric /// for the Filename and LineNo if any in the diagnostic.
23310b57cec5SDimitry Andric void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
23320b57cec5SDimitry Andric   const AsmParser *Parser = static_cast<const AsmParser *>(Context);
23330b57cec5SDimitry Andric   raw_ostream &OS = errs();
23340b57cec5SDimitry Andric 
23350b57cec5SDimitry Andric   const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
23360b57cec5SDimitry Andric   SMLoc DiagLoc = Diag.getLoc();
23370b57cec5SDimitry Andric   unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
23380b57cec5SDimitry Andric   unsigned CppHashBuf =
23390b57cec5SDimitry Andric       Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashInfo.Loc);
23400b57cec5SDimitry Andric 
23410b57cec5SDimitry Andric   // Like SourceMgr::printMessage() we need to print the include stack if any
23420b57cec5SDimitry Andric   // before printing the message.
23430b57cec5SDimitry Andric   unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
23440b57cec5SDimitry Andric   if (!Parser->SavedDiagHandler && DiagCurBuffer &&
23450b57cec5SDimitry Andric       DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
23460b57cec5SDimitry Andric     SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
23470b57cec5SDimitry Andric     DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
23480b57cec5SDimitry Andric   }
23490b57cec5SDimitry Andric 
23500b57cec5SDimitry Andric   // If we have not parsed a cpp hash line filename comment or the source
23510b57cec5SDimitry Andric   // manager changed or buffer changed (like in a nested include) then just
23520b57cec5SDimitry Andric   // print the normal diagnostic using its Filename and LineNo.
23530b57cec5SDimitry Andric   if (!Parser->CppHashInfo.LineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
23540b57cec5SDimitry Andric       DiagBuf != CppHashBuf) {
23550b57cec5SDimitry Andric     if (Parser->SavedDiagHandler)
23560b57cec5SDimitry Andric       Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
23570b57cec5SDimitry Andric     else
23580b57cec5SDimitry Andric       Diag.print(nullptr, OS);
23590b57cec5SDimitry Andric     return;
23600b57cec5SDimitry Andric   }
23610b57cec5SDimitry Andric 
23620b57cec5SDimitry Andric   // Use the CppHashFilename and calculate a line number based on the
23630b57cec5SDimitry Andric   // CppHashInfo.Loc and CppHashInfo.LineNumber relative to this Diag's SMLoc
23640b57cec5SDimitry Andric   // for the diagnostic.
2365*5ffd83dbSDimitry Andric   const std::string &Filename = std::string(Parser->CppHashInfo.Filename);
23660b57cec5SDimitry Andric 
23670b57cec5SDimitry Andric   int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
23680b57cec5SDimitry Andric   int CppHashLocLineNo =
23690b57cec5SDimitry Andric       Parser->SrcMgr.FindLineNumber(Parser->CppHashInfo.Loc, CppHashBuf);
23700b57cec5SDimitry Andric   int LineNo =
23710b57cec5SDimitry Andric       Parser->CppHashInfo.LineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
23720b57cec5SDimitry Andric 
23730b57cec5SDimitry Andric   SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
23740b57cec5SDimitry Andric                        Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
23750b57cec5SDimitry Andric                        Diag.getLineContents(), Diag.getRanges());
23760b57cec5SDimitry Andric 
23770b57cec5SDimitry Andric   if (Parser->SavedDiagHandler)
23780b57cec5SDimitry Andric     Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
23790b57cec5SDimitry Andric   else
23800b57cec5SDimitry Andric     NewDiag.print(nullptr, OS);
23810b57cec5SDimitry Andric }
23820b57cec5SDimitry Andric 
23830b57cec5SDimitry Andric // FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
23840b57cec5SDimitry Andric // difference being that that function accepts '@' as part of identifiers and
23850b57cec5SDimitry Andric // we can't do that. AsmLexer.cpp should probably be changed to handle
23860b57cec5SDimitry Andric // '@' as a special case when needed.
23870b57cec5SDimitry Andric static bool isIdentifierChar(char c) {
23880b57cec5SDimitry Andric   return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
23890b57cec5SDimitry Andric          c == '.';
23900b57cec5SDimitry Andric }
23910b57cec5SDimitry Andric 
23920b57cec5SDimitry Andric bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
23930b57cec5SDimitry Andric                             ArrayRef<MCAsmMacroParameter> Parameters,
23940b57cec5SDimitry Andric                             ArrayRef<MCAsmMacroArgument> A,
23950b57cec5SDimitry Andric                             bool EnableAtPseudoVariable, SMLoc L) {
23960b57cec5SDimitry Andric   unsigned NParameters = Parameters.size();
23970b57cec5SDimitry Andric   bool HasVararg = NParameters ? Parameters.back().Vararg : false;
23980b57cec5SDimitry Andric   if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
23990b57cec5SDimitry Andric     return Error(L, "Wrong number of arguments");
24000b57cec5SDimitry Andric 
24010b57cec5SDimitry Andric   // A macro without parameters is handled differently on Darwin:
24020b57cec5SDimitry Andric   // gas accepts no arguments and does no substitutions
24030b57cec5SDimitry Andric   while (!Body.empty()) {
24040b57cec5SDimitry Andric     // Scan for the next substitution.
24050b57cec5SDimitry Andric     std::size_t End = Body.size(), Pos = 0;
24060b57cec5SDimitry Andric     for (; Pos != End; ++Pos) {
24070b57cec5SDimitry Andric       // Check for a substitution or escape.
24080b57cec5SDimitry Andric       if (IsDarwin && !NParameters) {
24090b57cec5SDimitry Andric         // This macro has no parameters, look for $0, $1, etc.
24100b57cec5SDimitry Andric         if (Body[Pos] != '$' || Pos + 1 == End)
24110b57cec5SDimitry Andric           continue;
24120b57cec5SDimitry Andric 
24130b57cec5SDimitry Andric         char Next = Body[Pos + 1];
24140b57cec5SDimitry Andric         if (Next == '$' || Next == 'n' ||
24150b57cec5SDimitry Andric             isdigit(static_cast<unsigned char>(Next)))
24160b57cec5SDimitry Andric           break;
24170b57cec5SDimitry Andric       } else {
24180b57cec5SDimitry Andric         // This macro has parameters, look for \foo, \bar, etc.
24190b57cec5SDimitry Andric         if (Body[Pos] == '\\' && Pos + 1 != End)
24200b57cec5SDimitry Andric           break;
24210b57cec5SDimitry Andric       }
24220b57cec5SDimitry Andric     }
24230b57cec5SDimitry Andric 
24240b57cec5SDimitry Andric     // Add the prefix.
24250b57cec5SDimitry Andric     OS << Body.slice(0, Pos);
24260b57cec5SDimitry Andric 
24270b57cec5SDimitry Andric     // Check if we reached the end.
24280b57cec5SDimitry Andric     if (Pos == End)
24290b57cec5SDimitry Andric       break;
24300b57cec5SDimitry Andric 
24310b57cec5SDimitry Andric     if (IsDarwin && !NParameters) {
24320b57cec5SDimitry Andric       switch (Body[Pos + 1]) {
24330b57cec5SDimitry Andric       // $$ => $
24340b57cec5SDimitry Andric       case '$':
24350b57cec5SDimitry Andric         OS << '$';
24360b57cec5SDimitry Andric         break;
24370b57cec5SDimitry Andric 
24380b57cec5SDimitry Andric       // $n => number of arguments
24390b57cec5SDimitry Andric       case 'n':
24400b57cec5SDimitry Andric         OS << A.size();
24410b57cec5SDimitry Andric         break;
24420b57cec5SDimitry Andric 
24430b57cec5SDimitry Andric       // $[0-9] => argument
24440b57cec5SDimitry Andric       default: {
24450b57cec5SDimitry Andric         // Missing arguments are ignored.
24460b57cec5SDimitry Andric         unsigned Index = Body[Pos + 1] - '0';
24470b57cec5SDimitry Andric         if (Index >= A.size())
24480b57cec5SDimitry Andric           break;
24490b57cec5SDimitry Andric 
24500b57cec5SDimitry Andric         // Otherwise substitute with the token values, with spaces eliminated.
24510b57cec5SDimitry Andric         for (const AsmToken &Token : A[Index])
24520b57cec5SDimitry Andric           OS << Token.getString();
24530b57cec5SDimitry Andric         break;
24540b57cec5SDimitry Andric       }
24550b57cec5SDimitry Andric       }
24560b57cec5SDimitry Andric       Pos += 2;
24570b57cec5SDimitry Andric     } else {
24580b57cec5SDimitry Andric       unsigned I = Pos + 1;
24590b57cec5SDimitry Andric 
24600b57cec5SDimitry Andric       // Check for the \@ pseudo-variable.
24610b57cec5SDimitry Andric       if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End)
24620b57cec5SDimitry Andric         ++I;
24630b57cec5SDimitry Andric       else
24640b57cec5SDimitry Andric         while (isIdentifierChar(Body[I]) && I + 1 != End)
24650b57cec5SDimitry Andric           ++I;
24660b57cec5SDimitry Andric 
24670b57cec5SDimitry Andric       const char *Begin = Body.data() + Pos + 1;
24680b57cec5SDimitry Andric       StringRef Argument(Begin, I - (Pos + 1));
24690b57cec5SDimitry Andric       unsigned Index = 0;
24700b57cec5SDimitry Andric 
24710b57cec5SDimitry Andric       if (Argument == "@") {
24720b57cec5SDimitry Andric         OS << NumOfMacroInstantiations;
24730b57cec5SDimitry Andric         Pos += 2;
24740b57cec5SDimitry Andric       } else {
24750b57cec5SDimitry Andric         for (; Index < NParameters; ++Index)
24760b57cec5SDimitry Andric           if (Parameters[Index].Name == Argument)
24770b57cec5SDimitry Andric             break;
24780b57cec5SDimitry Andric 
24790b57cec5SDimitry Andric         if (Index == NParameters) {
24800b57cec5SDimitry Andric           if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
24810b57cec5SDimitry Andric             Pos += 3;
24820b57cec5SDimitry Andric           else {
24830b57cec5SDimitry Andric             OS << '\\' << Argument;
24840b57cec5SDimitry Andric             Pos = I;
24850b57cec5SDimitry Andric           }
24860b57cec5SDimitry Andric         } else {
24870b57cec5SDimitry Andric           bool VarargParameter = HasVararg && Index == (NParameters - 1);
24880b57cec5SDimitry Andric           for (const AsmToken &Token : A[Index])
24890b57cec5SDimitry Andric             // For altmacro mode, you can write '%expr'.
24900b57cec5SDimitry Andric             // The prefix '%' evaluates the expression 'expr'
24910b57cec5SDimitry Andric             // and uses the result as a string (e.g. replace %(1+2) with the
24920b57cec5SDimitry Andric             // string "3").
24930b57cec5SDimitry Andric             // Here, we identify the integer token which is the result of the
24940b57cec5SDimitry Andric             // absolute expression evaluation and replace it with its string
24950b57cec5SDimitry Andric             // representation.
24960b57cec5SDimitry Andric             if (AltMacroMode && Token.getString().front() == '%' &&
24970b57cec5SDimitry Andric                 Token.is(AsmToken::Integer))
24980b57cec5SDimitry Andric               // Emit an integer value to the buffer.
24990b57cec5SDimitry Andric               OS << Token.getIntVal();
25000b57cec5SDimitry Andric             // Only Token that was validated as a string and begins with '<'
25010b57cec5SDimitry Andric             // is considered altMacroString!!!
25020b57cec5SDimitry Andric             else if (AltMacroMode && Token.getString().front() == '<' &&
25030b57cec5SDimitry Andric                      Token.is(AsmToken::String)) {
2504*5ffd83dbSDimitry Andric               OS << angleBracketString(Token.getStringContents());
25050b57cec5SDimitry Andric             }
25060b57cec5SDimitry Andric             // We expect no quotes around the string's contents when
25070b57cec5SDimitry Andric             // parsing for varargs.
25080b57cec5SDimitry Andric             else if (Token.isNot(AsmToken::String) || VarargParameter)
25090b57cec5SDimitry Andric               OS << Token.getString();
25100b57cec5SDimitry Andric             else
25110b57cec5SDimitry Andric               OS << Token.getStringContents();
25120b57cec5SDimitry Andric 
25130b57cec5SDimitry Andric           Pos += 1 + Argument.size();
25140b57cec5SDimitry Andric         }
25150b57cec5SDimitry Andric       }
25160b57cec5SDimitry Andric     }
25170b57cec5SDimitry Andric     // Update the scan point.
25180b57cec5SDimitry Andric     Body = Body.substr(Pos);
25190b57cec5SDimitry Andric   }
25200b57cec5SDimitry Andric 
25210b57cec5SDimitry Andric   return false;
25220b57cec5SDimitry Andric }
25230b57cec5SDimitry Andric 
25240b57cec5SDimitry Andric static bool isOperator(AsmToken::TokenKind kind) {
25250b57cec5SDimitry Andric   switch (kind) {
25260b57cec5SDimitry Andric   default:
25270b57cec5SDimitry Andric     return false;
25280b57cec5SDimitry Andric   case AsmToken::Plus:
25290b57cec5SDimitry Andric   case AsmToken::Minus:
25300b57cec5SDimitry Andric   case AsmToken::Tilde:
25310b57cec5SDimitry Andric   case AsmToken::Slash:
25320b57cec5SDimitry Andric   case AsmToken::Star:
25330b57cec5SDimitry Andric   case AsmToken::Dot:
25340b57cec5SDimitry Andric   case AsmToken::Equal:
25350b57cec5SDimitry Andric   case AsmToken::EqualEqual:
25360b57cec5SDimitry Andric   case AsmToken::Pipe:
25370b57cec5SDimitry Andric   case AsmToken::PipePipe:
25380b57cec5SDimitry Andric   case AsmToken::Caret:
25390b57cec5SDimitry Andric   case AsmToken::Amp:
25400b57cec5SDimitry Andric   case AsmToken::AmpAmp:
25410b57cec5SDimitry Andric   case AsmToken::Exclaim:
25420b57cec5SDimitry Andric   case AsmToken::ExclaimEqual:
25430b57cec5SDimitry Andric   case AsmToken::Less:
25440b57cec5SDimitry Andric   case AsmToken::LessEqual:
25450b57cec5SDimitry Andric   case AsmToken::LessLess:
25460b57cec5SDimitry Andric   case AsmToken::LessGreater:
25470b57cec5SDimitry Andric   case AsmToken::Greater:
25480b57cec5SDimitry Andric   case AsmToken::GreaterEqual:
25490b57cec5SDimitry Andric   case AsmToken::GreaterGreater:
25500b57cec5SDimitry Andric     return true;
25510b57cec5SDimitry Andric   }
25520b57cec5SDimitry Andric }
25530b57cec5SDimitry Andric 
25540b57cec5SDimitry Andric namespace {
25550b57cec5SDimitry Andric 
25560b57cec5SDimitry Andric class AsmLexerSkipSpaceRAII {
25570b57cec5SDimitry Andric public:
25580b57cec5SDimitry Andric   AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
25590b57cec5SDimitry Andric     Lexer.setSkipSpace(SkipSpace);
25600b57cec5SDimitry Andric   }
25610b57cec5SDimitry Andric 
25620b57cec5SDimitry Andric   ~AsmLexerSkipSpaceRAII() {
25630b57cec5SDimitry Andric     Lexer.setSkipSpace(true);
25640b57cec5SDimitry Andric   }
25650b57cec5SDimitry Andric 
25660b57cec5SDimitry Andric private:
25670b57cec5SDimitry Andric   AsmLexer &Lexer;
25680b57cec5SDimitry Andric };
25690b57cec5SDimitry Andric 
25700b57cec5SDimitry Andric } // end anonymous namespace
25710b57cec5SDimitry Andric 
25720b57cec5SDimitry Andric bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
25730b57cec5SDimitry Andric 
25740b57cec5SDimitry Andric   if (Vararg) {
25750b57cec5SDimitry Andric     if (Lexer.isNot(AsmToken::EndOfStatement)) {
25760b57cec5SDimitry Andric       StringRef Str = parseStringToEndOfStatement();
25770b57cec5SDimitry Andric       MA.emplace_back(AsmToken::String, Str);
25780b57cec5SDimitry Andric     }
25790b57cec5SDimitry Andric     return false;
25800b57cec5SDimitry Andric   }
25810b57cec5SDimitry Andric 
25820b57cec5SDimitry Andric   unsigned ParenLevel = 0;
25830b57cec5SDimitry Andric 
25840b57cec5SDimitry Andric   // Darwin doesn't use spaces to delmit arguments.
25850b57cec5SDimitry Andric   AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
25860b57cec5SDimitry Andric 
25870b57cec5SDimitry Andric   bool SpaceEaten;
25880b57cec5SDimitry Andric 
25890b57cec5SDimitry Andric   while (true) {
25900b57cec5SDimitry Andric     SpaceEaten = false;
25910b57cec5SDimitry Andric     if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
25920b57cec5SDimitry Andric       return TokError("unexpected token in macro instantiation");
25930b57cec5SDimitry Andric 
25940b57cec5SDimitry Andric     if (ParenLevel == 0) {
25950b57cec5SDimitry Andric 
25960b57cec5SDimitry Andric       if (Lexer.is(AsmToken::Comma))
25970b57cec5SDimitry Andric         break;
25980b57cec5SDimitry Andric 
25990b57cec5SDimitry Andric       if (Lexer.is(AsmToken::Space)) {
26000b57cec5SDimitry Andric         SpaceEaten = true;
26010b57cec5SDimitry Andric         Lexer.Lex(); // Eat spaces
26020b57cec5SDimitry Andric       }
26030b57cec5SDimitry Andric 
26040b57cec5SDimitry Andric       // Spaces can delimit parameters, but could also be part an expression.
26050b57cec5SDimitry Andric       // If the token after a space is an operator, add the token and the next
26060b57cec5SDimitry Andric       // one into this argument
26070b57cec5SDimitry Andric       if (!IsDarwin) {
26080b57cec5SDimitry Andric         if (isOperator(Lexer.getKind())) {
26090b57cec5SDimitry Andric           MA.push_back(getTok());
26100b57cec5SDimitry Andric           Lexer.Lex();
26110b57cec5SDimitry Andric 
26120b57cec5SDimitry Andric           // Whitespace after an operator can be ignored.
26130b57cec5SDimitry Andric           if (Lexer.is(AsmToken::Space))
26140b57cec5SDimitry Andric             Lexer.Lex();
26150b57cec5SDimitry Andric 
26160b57cec5SDimitry Andric           continue;
26170b57cec5SDimitry Andric         }
26180b57cec5SDimitry Andric       }
26190b57cec5SDimitry Andric       if (SpaceEaten)
26200b57cec5SDimitry Andric         break;
26210b57cec5SDimitry Andric     }
26220b57cec5SDimitry Andric 
26230b57cec5SDimitry Andric     // handleMacroEntry relies on not advancing the lexer here
26240b57cec5SDimitry Andric     // to be able to fill in the remaining default parameter values
26250b57cec5SDimitry Andric     if (Lexer.is(AsmToken::EndOfStatement))
26260b57cec5SDimitry Andric       break;
26270b57cec5SDimitry Andric 
26280b57cec5SDimitry Andric     // Adjust the current parentheses level.
26290b57cec5SDimitry Andric     if (Lexer.is(AsmToken::LParen))
26300b57cec5SDimitry Andric       ++ParenLevel;
26310b57cec5SDimitry Andric     else if (Lexer.is(AsmToken::RParen) && ParenLevel)
26320b57cec5SDimitry Andric       --ParenLevel;
26330b57cec5SDimitry Andric 
26340b57cec5SDimitry Andric     // Append the token to the current argument list.
26350b57cec5SDimitry Andric     MA.push_back(getTok());
26360b57cec5SDimitry Andric     Lexer.Lex();
26370b57cec5SDimitry Andric   }
26380b57cec5SDimitry Andric 
26390b57cec5SDimitry Andric   if (ParenLevel != 0)
26400b57cec5SDimitry Andric     return TokError("unbalanced parentheses in macro argument");
26410b57cec5SDimitry Andric   return false;
26420b57cec5SDimitry Andric }
26430b57cec5SDimitry Andric 
26440b57cec5SDimitry Andric // Parse the macro instantiation arguments.
26450b57cec5SDimitry Andric bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
26460b57cec5SDimitry Andric                                     MCAsmMacroArguments &A) {
26470b57cec5SDimitry Andric   const unsigned NParameters = M ? M->Parameters.size() : 0;
26480b57cec5SDimitry Andric   bool NamedParametersFound = false;
26490b57cec5SDimitry Andric   SmallVector<SMLoc, 4> FALocs;
26500b57cec5SDimitry Andric 
26510b57cec5SDimitry Andric   A.resize(NParameters);
26520b57cec5SDimitry Andric   FALocs.resize(NParameters);
26530b57cec5SDimitry Andric 
26540b57cec5SDimitry Andric   // Parse two kinds of macro invocations:
26550b57cec5SDimitry Andric   // - macros defined without any parameters accept an arbitrary number of them
26560b57cec5SDimitry Andric   // - macros defined with parameters accept at most that many of them
26570b57cec5SDimitry Andric   bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
26580b57cec5SDimitry Andric   for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
26590b57cec5SDimitry Andric        ++Parameter) {
26600b57cec5SDimitry Andric     SMLoc IDLoc = Lexer.getLoc();
26610b57cec5SDimitry Andric     MCAsmMacroParameter FA;
26620b57cec5SDimitry Andric 
26630b57cec5SDimitry Andric     if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
26640b57cec5SDimitry Andric       if (parseIdentifier(FA.Name))
26650b57cec5SDimitry Andric         return Error(IDLoc, "invalid argument identifier for formal argument");
26660b57cec5SDimitry Andric 
26670b57cec5SDimitry Andric       if (Lexer.isNot(AsmToken::Equal))
26680b57cec5SDimitry Andric         return TokError("expected '=' after formal parameter identifier");
26690b57cec5SDimitry Andric 
26700b57cec5SDimitry Andric       Lex();
26710b57cec5SDimitry Andric 
26720b57cec5SDimitry Andric       NamedParametersFound = true;
26730b57cec5SDimitry Andric     }
26740b57cec5SDimitry Andric     bool Vararg = HasVararg && Parameter == (NParameters - 1);
26750b57cec5SDimitry Andric 
26760b57cec5SDimitry Andric     if (NamedParametersFound && FA.Name.empty())
26770b57cec5SDimitry Andric       return Error(IDLoc, "cannot mix positional and keyword arguments");
26780b57cec5SDimitry Andric 
26790b57cec5SDimitry Andric     SMLoc StrLoc = Lexer.getLoc();
26800b57cec5SDimitry Andric     SMLoc EndLoc;
26810b57cec5SDimitry Andric     if (AltMacroMode && Lexer.is(AsmToken::Percent)) {
26820b57cec5SDimitry Andric       const MCExpr *AbsoluteExp;
26830b57cec5SDimitry Andric       int64_t Value;
26840b57cec5SDimitry Andric       /// Eat '%'
26850b57cec5SDimitry Andric       Lex();
26860b57cec5SDimitry Andric       if (parseExpression(AbsoluteExp, EndLoc))
26870b57cec5SDimitry Andric         return false;
26880b57cec5SDimitry Andric       if (!AbsoluteExp->evaluateAsAbsolute(Value,
26890b57cec5SDimitry Andric                                            getStreamer().getAssemblerPtr()))
26900b57cec5SDimitry Andric         return Error(StrLoc, "expected absolute expression");
26910b57cec5SDimitry Andric       const char *StrChar = StrLoc.getPointer();
26920b57cec5SDimitry Andric       const char *EndChar = EndLoc.getPointer();
26930b57cec5SDimitry Andric       AsmToken newToken(AsmToken::Integer,
26940b57cec5SDimitry Andric                         StringRef(StrChar, EndChar - StrChar), Value);
26950b57cec5SDimitry Andric       FA.Value.push_back(newToken);
26960b57cec5SDimitry Andric     } else if (AltMacroMode && Lexer.is(AsmToken::Less) &&
2697*5ffd83dbSDimitry Andric                isAngleBracketString(StrLoc, EndLoc)) {
26980b57cec5SDimitry Andric       const char *StrChar = StrLoc.getPointer();
26990b57cec5SDimitry Andric       const char *EndChar = EndLoc.getPointer();
27000b57cec5SDimitry Andric       jumpToLoc(EndLoc, CurBuffer);
27010b57cec5SDimitry Andric       /// Eat from '<' to '>'
27020b57cec5SDimitry Andric       Lex();
27030b57cec5SDimitry Andric       AsmToken newToken(AsmToken::String,
27040b57cec5SDimitry Andric                         StringRef(StrChar, EndChar - StrChar));
27050b57cec5SDimitry Andric       FA.Value.push_back(newToken);
27060b57cec5SDimitry Andric     } else if(parseMacroArgument(FA.Value, Vararg))
27070b57cec5SDimitry Andric       return true;
27080b57cec5SDimitry Andric 
27090b57cec5SDimitry Andric     unsigned PI = Parameter;
27100b57cec5SDimitry Andric     if (!FA.Name.empty()) {
27110b57cec5SDimitry Andric       unsigned FAI = 0;
27120b57cec5SDimitry Andric       for (FAI = 0; FAI < NParameters; ++FAI)
27130b57cec5SDimitry Andric         if (M->Parameters[FAI].Name == FA.Name)
27140b57cec5SDimitry Andric           break;
27150b57cec5SDimitry Andric 
27160b57cec5SDimitry Andric       if (FAI >= NParameters) {
27170b57cec5SDimitry Andric         assert(M && "expected macro to be defined");
27180b57cec5SDimitry Andric         return Error(IDLoc, "parameter named '" + FA.Name +
27190b57cec5SDimitry Andric                                 "' does not exist for macro '" + M->Name + "'");
27200b57cec5SDimitry Andric       }
27210b57cec5SDimitry Andric       PI = FAI;
27220b57cec5SDimitry Andric     }
27230b57cec5SDimitry Andric 
27240b57cec5SDimitry Andric     if (!FA.Value.empty()) {
27250b57cec5SDimitry Andric       if (A.size() <= PI)
27260b57cec5SDimitry Andric         A.resize(PI + 1);
27270b57cec5SDimitry Andric       A[PI] = FA.Value;
27280b57cec5SDimitry Andric 
27290b57cec5SDimitry Andric       if (FALocs.size() <= PI)
27300b57cec5SDimitry Andric         FALocs.resize(PI + 1);
27310b57cec5SDimitry Andric 
27320b57cec5SDimitry Andric       FALocs[PI] = Lexer.getLoc();
27330b57cec5SDimitry Andric     }
27340b57cec5SDimitry Andric 
27350b57cec5SDimitry Andric     // At the end of the statement, fill in remaining arguments that have
27360b57cec5SDimitry Andric     // default values. If there aren't any, then the next argument is
27370b57cec5SDimitry Andric     // required but missing
27380b57cec5SDimitry Andric     if (Lexer.is(AsmToken::EndOfStatement)) {
27390b57cec5SDimitry Andric       bool Failure = false;
27400b57cec5SDimitry Andric       for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
27410b57cec5SDimitry Andric         if (A[FAI].empty()) {
27420b57cec5SDimitry Andric           if (M->Parameters[FAI].Required) {
27430b57cec5SDimitry Andric             Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
27440b57cec5SDimitry Andric                   "missing value for required parameter "
27450b57cec5SDimitry Andric                   "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
27460b57cec5SDimitry Andric             Failure = true;
27470b57cec5SDimitry Andric           }
27480b57cec5SDimitry Andric 
27490b57cec5SDimitry Andric           if (!M->Parameters[FAI].Value.empty())
27500b57cec5SDimitry Andric             A[FAI] = M->Parameters[FAI].Value;
27510b57cec5SDimitry Andric         }
27520b57cec5SDimitry Andric       }
27530b57cec5SDimitry Andric       return Failure;
27540b57cec5SDimitry Andric     }
27550b57cec5SDimitry Andric 
27560b57cec5SDimitry Andric     if (Lexer.is(AsmToken::Comma))
27570b57cec5SDimitry Andric       Lex();
27580b57cec5SDimitry Andric   }
27590b57cec5SDimitry Andric 
27600b57cec5SDimitry Andric   return TokError("too many positional arguments");
27610b57cec5SDimitry Andric }
27620b57cec5SDimitry Andric 
27630b57cec5SDimitry Andric bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
27640b57cec5SDimitry Andric   // Arbitrarily limit macro nesting depth (default matches 'as'). We can
27650b57cec5SDimitry Andric   // eliminate this, although we should protect against infinite loops.
27660b57cec5SDimitry Andric   unsigned MaxNestingDepth = AsmMacroMaxNestingDepth;
27670b57cec5SDimitry Andric   if (ActiveMacros.size() == MaxNestingDepth) {
27680b57cec5SDimitry Andric     std::ostringstream MaxNestingDepthError;
27690b57cec5SDimitry Andric     MaxNestingDepthError << "macros cannot be nested more than "
27700b57cec5SDimitry Andric                          << MaxNestingDepth << " levels deep."
27710b57cec5SDimitry Andric                          << " Use -asm-macro-max-nesting-depth to increase "
27720b57cec5SDimitry Andric                             "this limit.";
27730b57cec5SDimitry Andric     return TokError(MaxNestingDepthError.str());
27740b57cec5SDimitry Andric   }
27750b57cec5SDimitry Andric 
27760b57cec5SDimitry Andric   MCAsmMacroArguments A;
27770b57cec5SDimitry Andric   if (parseMacroArguments(M, A))
27780b57cec5SDimitry Andric     return true;
27790b57cec5SDimitry Andric 
27800b57cec5SDimitry Andric   // Macro instantiation is lexical, unfortunately. We construct a new buffer
27810b57cec5SDimitry Andric   // to hold the macro body with substitutions.
27820b57cec5SDimitry Andric   SmallString<256> Buf;
27830b57cec5SDimitry Andric   StringRef Body = M->Body;
27840b57cec5SDimitry Andric   raw_svector_ostream OS(Buf);
27850b57cec5SDimitry Andric 
27860b57cec5SDimitry Andric   if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc()))
27870b57cec5SDimitry Andric     return true;
27880b57cec5SDimitry Andric 
27890b57cec5SDimitry Andric   // We include the .endmacro in the buffer as our cue to exit the macro
27900b57cec5SDimitry Andric   // instantiation.
27910b57cec5SDimitry Andric   OS << ".endmacro\n";
27920b57cec5SDimitry Andric 
27930b57cec5SDimitry Andric   std::unique_ptr<MemoryBuffer> Instantiation =
27940b57cec5SDimitry Andric       MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
27950b57cec5SDimitry Andric 
27960b57cec5SDimitry Andric   // Create the macro instantiation object and add to the current macro
27970b57cec5SDimitry Andric   // instantiation stack.
2798480093f4SDimitry Andric   MacroInstantiation *MI = new MacroInstantiation{
2799480093f4SDimitry Andric       NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()};
28000b57cec5SDimitry Andric   ActiveMacros.push_back(MI);
28010b57cec5SDimitry Andric 
28020b57cec5SDimitry Andric   ++NumOfMacroInstantiations;
28030b57cec5SDimitry Andric 
28040b57cec5SDimitry Andric   // Jump to the macro instantiation and prime the lexer.
28050b57cec5SDimitry Andric   CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
28060b57cec5SDimitry Andric   Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
28070b57cec5SDimitry Andric   Lex();
28080b57cec5SDimitry Andric 
28090b57cec5SDimitry Andric   return false;
28100b57cec5SDimitry Andric }
28110b57cec5SDimitry Andric 
28120b57cec5SDimitry Andric void AsmParser::handleMacroExit() {
28130b57cec5SDimitry Andric   // Jump to the EndOfStatement we should return to, and consume it.
28140b57cec5SDimitry Andric   jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
28150b57cec5SDimitry Andric   Lex();
28160b57cec5SDimitry Andric 
28170b57cec5SDimitry Andric   // Pop the instantiation entry.
28180b57cec5SDimitry Andric   delete ActiveMacros.back();
28190b57cec5SDimitry Andric   ActiveMacros.pop_back();
28200b57cec5SDimitry Andric }
28210b57cec5SDimitry Andric 
28220b57cec5SDimitry Andric bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
28230b57cec5SDimitry Andric                                 bool NoDeadStrip) {
28240b57cec5SDimitry Andric   MCSymbol *Sym;
28250b57cec5SDimitry Andric   const MCExpr *Value;
28260b57cec5SDimitry Andric   if (MCParserUtils::parseAssignmentExpression(Name, allow_redef, *this, Sym,
28270b57cec5SDimitry Andric                                                Value))
28280b57cec5SDimitry Andric     return true;
28290b57cec5SDimitry Andric 
28300b57cec5SDimitry Andric   if (!Sym) {
28310b57cec5SDimitry Andric     // In the case where we parse an expression starting with a '.', we will
28320b57cec5SDimitry Andric     // not generate an error, nor will we create a symbol.  In this case we
28330b57cec5SDimitry Andric     // should just return out.
28340b57cec5SDimitry Andric     return false;
28350b57cec5SDimitry Andric   }
28360b57cec5SDimitry Andric 
28370b57cec5SDimitry Andric   // Do the assignment.
2838*5ffd83dbSDimitry Andric   Out.emitAssignment(Sym, Value);
28390b57cec5SDimitry Andric   if (NoDeadStrip)
2840*5ffd83dbSDimitry Andric     Out.emitSymbolAttribute(Sym, MCSA_NoDeadStrip);
28410b57cec5SDimitry Andric 
28420b57cec5SDimitry Andric   return false;
28430b57cec5SDimitry Andric }
28440b57cec5SDimitry Andric 
28450b57cec5SDimitry Andric /// parseIdentifier:
28460b57cec5SDimitry Andric ///   ::= identifier
28470b57cec5SDimitry Andric ///   ::= string
28480b57cec5SDimitry Andric bool AsmParser::parseIdentifier(StringRef &Res) {
28490b57cec5SDimitry Andric   // The assembler has relaxed rules for accepting identifiers, in particular we
28500b57cec5SDimitry Andric   // allow things like '.globl $foo' and '.def @feat.00', which would normally be
28510b57cec5SDimitry Andric   // separate tokens. At this level, we have already lexed so we cannot (currently)
28520b57cec5SDimitry Andric   // handle this as a context dependent token, instead we detect adjacent tokens
28530b57cec5SDimitry Andric   // and return the combined identifier.
28540b57cec5SDimitry Andric   if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
28550b57cec5SDimitry Andric     SMLoc PrefixLoc = getLexer().getLoc();
28560b57cec5SDimitry Andric 
28570b57cec5SDimitry Andric     // Consume the prefix character, and check for a following identifier.
28580b57cec5SDimitry Andric 
28590b57cec5SDimitry Andric     AsmToken Buf[1];
28600b57cec5SDimitry Andric     Lexer.peekTokens(Buf, false);
28610b57cec5SDimitry Andric 
2862*5ffd83dbSDimitry Andric     if (Buf[0].isNot(AsmToken::Identifier) && Buf[0].isNot(AsmToken::Integer))
28630b57cec5SDimitry Andric       return true;
28640b57cec5SDimitry Andric 
2865*5ffd83dbSDimitry Andric     // We have a '$' or '@' followed by an identifier or integer token, make
2866*5ffd83dbSDimitry Andric     // sure they are adjacent.
28670b57cec5SDimitry Andric     if (PrefixLoc.getPointer() + 1 != Buf[0].getLoc().getPointer())
28680b57cec5SDimitry Andric       return true;
28690b57cec5SDimitry Andric 
28700b57cec5SDimitry Andric     // eat $ or @
28710b57cec5SDimitry Andric     Lexer.Lex(); // Lexer's Lex guarantees consecutive token.
28720b57cec5SDimitry Andric     // Construct the joined identifier and consume the token.
2873*5ffd83dbSDimitry Andric     Res = StringRef(PrefixLoc.getPointer(), getTok().getString().size() + 1);
28740b57cec5SDimitry Andric     Lex(); // Parser Lex to maintain invariants.
28750b57cec5SDimitry Andric     return false;
28760b57cec5SDimitry Andric   }
28770b57cec5SDimitry Andric 
28780b57cec5SDimitry Andric   if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
28790b57cec5SDimitry Andric     return true;
28800b57cec5SDimitry Andric 
28810b57cec5SDimitry Andric   Res = getTok().getIdentifier();
28820b57cec5SDimitry Andric 
28830b57cec5SDimitry Andric   Lex(); // Consume the identifier token.
28840b57cec5SDimitry Andric 
28850b57cec5SDimitry Andric   return false;
28860b57cec5SDimitry Andric }
28870b57cec5SDimitry Andric 
28880b57cec5SDimitry Andric /// parseDirectiveSet:
28890b57cec5SDimitry Andric ///   ::= .equ identifier ',' expression
28900b57cec5SDimitry Andric ///   ::= .equiv identifier ',' expression
28910b57cec5SDimitry Andric ///   ::= .set identifier ',' expression
28920b57cec5SDimitry Andric bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
28930b57cec5SDimitry Andric   StringRef Name;
28940b57cec5SDimitry Andric   if (check(parseIdentifier(Name), "expected identifier") ||
28950b57cec5SDimitry Andric       parseToken(AsmToken::Comma) || parseAssignment(Name, allow_redef, true))
28960b57cec5SDimitry Andric     return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
28970b57cec5SDimitry Andric   return false;
28980b57cec5SDimitry Andric }
28990b57cec5SDimitry Andric 
29000b57cec5SDimitry Andric bool AsmParser::parseEscapedString(std::string &Data) {
29010b57cec5SDimitry Andric   if (check(getTok().isNot(AsmToken::String), "expected string"))
29020b57cec5SDimitry Andric     return true;
29030b57cec5SDimitry Andric 
29040b57cec5SDimitry Andric   Data = "";
29050b57cec5SDimitry Andric   StringRef Str = getTok().getStringContents();
29060b57cec5SDimitry Andric   for (unsigned i = 0, e = Str.size(); i != e; ++i) {
29070b57cec5SDimitry Andric     if (Str[i] != '\\') {
29080b57cec5SDimitry Andric       Data += Str[i];
29090b57cec5SDimitry Andric       continue;
29100b57cec5SDimitry Andric     }
29110b57cec5SDimitry Andric 
29120b57cec5SDimitry Andric     // Recognize escaped characters. Note that this escape semantics currently
29138bcb0991SDimitry Andric     // loosely follows Darwin 'as'.
29140b57cec5SDimitry Andric     ++i;
29150b57cec5SDimitry Andric     if (i == e)
29160b57cec5SDimitry Andric       return TokError("unexpected backslash at end of string");
29170b57cec5SDimitry Andric 
29188bcb0991SDimitry Andric     // Recognize hex sequences similarly to GNU 'as'.
29198bcb0991SDimitry Andric     if (Str[i] == 'x' || Str[i] == 'X') {
29208bcb0991SDimitry Andric       size_t length = Str.size();
29218bcb0991SDimitry Andric       if (i + 1 >= length || !isHexDigit(Str[i + 1]))
29228bcb0991SDimitry Andric         return TokError("invalid hexadecimal escape sequence");
29238bcb0991SDimitry Andric 
29248bcb0991SDimitry Andric       // Consume hex characters. GNU 'as' reads all hexadecimal characters and
29258bcb0991SDimitry Andric       // then truncates to the lower 16 bits. Seems reasonable.
29268bcb0991SDimitry Andric       unsigned Value = 0;
29278bcb0991SDimitry Andric       while (i + 1 < length && isHexDigit(Str[i + 1]))
29288bcb0991SDimitry Andric         Value = Value * 16 + hexDigitValue(Str[++i]);
29298bcb0991SDimitry Andric 
29308bcb0991SDimitry Andric       Data += (unsigned char)(Value & 0xFF);
29318bcb0991SDimitry Andric       continue;
29328bcb0991SDimitry Andric     }
29338bcb0991SDimitry Andric 
29340b57cec5SDimitry Andric     // Recognize octal sequences.
29350b57cec5SDimitry Andric     if ((unsigned)(Str[i] - '0') <= 7) {
29360b57cec5SDimitry Andric       // Consume up to three octal characters.
29370b57cec5SDimitry Andric       unsigned Value = Str[i] - '0';
29380b57cec5SDimitry Andric 
29390b57cec5SDimitry Andric       if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
29400b57cec5SDimitry Andric         ++i;
29410b57cec5SDimitry Andric         Value = Value * 8 + (Str[i] - '0');
29420b57cec5SDimitry Andric 
29430b57cec5SDimitry Andric         if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
29440b57cec5SDimitry Andric           ++i;
29450b57cec5SDimitry Andric           Value = Value * 8 + (Str[i] - '0');
29460b57cec5SDimitry Andric         }
29470b57cec5SDimitry Andric       }
29480b57cec5SDimitry Andric 
29490b57cec5SDimitry Andric       if (Value > 255)
29500b57cec5SDimitry Andric         return TokError("invalid octal escape sequence (out of range)");
29510b57cec5SDimitry Andric 
29520b57cec5SDimitry Andric       Data += (unsigned char)Value;
29530b57cec5SDimitry Andric       continue;
29540b57cec5SDimitry Andric     }
29550b57cec5SDimitry Andric 
29560b57cec5SDimitry Andric     // Otherwise recognize individual escapes.
29570b57cec5SDimitry Andric     switch (Str[i]) {
29580b57cec5SDimitry Andric     default:
29590b57cec5SDimitry Andric       // Just reject invalid escape sequences for now.
29600b57cec5SDimitry Andric       return TokError("invalid escape sequence (unrecognized character)");
29610b57cec5SDimitry Andric 
29620b57cec5SDimitry Andric     case 'b': Data += '\b'; break;
29630b57cec5SDimitry Andric     case 'f': Data += '\f'; break;
29640b57cec5SDimitry Andric     case 'n': Data += '\n'; break;
29650b57cec5SDimitry Andric     case 'r': Data += '\r'; break;
29660b57cec5SDimitry Andric     case 't': Data += '\t'; break;
29670b57cec5SDimitry Andric     case '"': Data += '"'; break;
29680b57cec5SDimitry Andric     case '\\': Data += '\\'; break;
29690b57cec5SDimitry Andric     }
29700b57cec5SDimitry Andric   }
29710b57cec5SDimitry Andric 
29720b57cec5SDimitry Andric   Lex();
29730b57cec5SDimitry Andric   return false;
29740b57cec5SDimitry Andric }
29750b57cec5SDimitry Andric 
2976*5ffd83dbSDimitry Andric bool AsmParser::parseAngleBracketString(std::string &Data) {
2977*5ffd83dbSDimitry Andric   SMLoc EndLoc, StartLoc = getTok().getLoc();
2978*5ffd83dbSDimitry Andric   if (isAngleBracketString(StartLoc, EndLoc)) {
2979*5ffd83dbSDimitry Andric     const char *StartChar = StartLoc.getPointer() + 1;
2980*5ffd83dbSDimitry Andric     const char *EndChar = EndLoc.getPointer() - 1;
2981*5ffd83dbSDimitry Andric     jumpToLoc(EndLoc, CurBuffer);
2982*5ffd83dbSDimitry Andric     /// Eat from '<' to '>'
2983*5ffd83dbSDimitry Andric     Lex();
2984*5ffd83dbSDimitry Andric 
2985*5ffd83dbSDimitry Andric     Data = angleBracketString(StringRef(StartChar, EndChar - StartChar));
2986*5ffd83dbSDimitry Andric     return false;
2987*5ffd83dbSDimitry Andric   }
2988*5ffd83dbSDimitry Andric   return true;
2989*5ffd83dbSDimitry Andric }
2990*5ffd83dbSDimitry Andric 
29910b57cec5SDimitry Andric /// parseDirectiveAscii:
29920b57cec5SDimitry Andric ///   ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
29930b57cec5SDimitry Andric bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
29940b57cec5SDimitry Andric   auto parseOp = [&]() -> bool {
29950b57cec5SDimitry Andric     std::string Data;
29960b57cec5SDimitry Andric     if (checkForValidSection() || parseEscapedString(Data))
29970b57cec5SDimitry Andric       return true;
2998*5ffd83dbSDimitry Andric     getStreamer().emitBytes(Data);
29990b57cec5SDimitry Andric     if (ZeroTerminated)
3000*5ffd83dbSDimitry Andric       getStreamer().emitBytes(StringRef("\0", 1));
30010b57cec5SDimitry Andric     return false;
30020b57cec5SDimitry Andric   };
30030b57cec5SDimitry Andric 
30040b57cec5SDimitry Andric   if (parseMany(parseOp))
30050b57cec5SDimitry Andric     return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
30060b57cec5SDimitry Andric   return false;
30070b57cec5SDimitry Andric }
30080b57cec5SDimitry Andric 
30090b57cec5SDimitry Andric /// parseDirectiveReloc
30100b57cec5SDimitry Andric ///  ::= .reloc expression , identifier [ , expression ]
30110b57cec5SDimitry Andric bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) {
30120b57cec5SDimitry Andric   const MCExpr *Offset;
30130b57cec5SDimitry Andric   const MCExpr *Expr = nullptr;
30140b57cec5SDimitry Andric   SMLoc OffsetLoc = Lexer.getTok().getLoc();
30150b57cec5SDimitry Andric 
30160b57cec5SDimitry Andric   if (parseExpression(Offset))
30170b57cec5SDimitry Andric     return true;
3018*5ffd83dbSDimitry Andric   if (parseToken(AsmToken::Comma, "expected comma") ||
3019*5ffd83dbSDimitry Andric       check(getTok().isNot(AsmToken::Identifier), "expected relocation name"))
30200b57cec5SDimitry Andric     return true;
30210b57cec5SDimitry Andric 
30220b57cec5SDimitry Andric   SMLoc NameLoc = Lexer.getTok().getLoc();
30230b57cec5SDimitry Andric   StringRef Name = Lexer.getTok().getIdentifier();
30240b57cec5SDimitry Andric   Lex();
30250b57cec5SDimitry Andric 
30260b57cec5SDimitry Andric   if (Lexer.is(AsmToken::Comma)) {
30270b57cec5SDimitry Andric     Lex();
30280b57cec5SDimitry Andric     SMLoc ExprLoc = Lexer.getLoc();
30290b57cec5SDimitry Andric     if (parseExpression(Expr))
30300b57cec5SDimitry Andric       return true;
30310b57cec5SDimitry Andric 
30320b57cec5SDimitry Andric     MCValue Value;
30330b57cec5SDimitry Andric     if (!Expr->evaluateAsRelocatable(Value, nullptr, nullptr))
30340b57cec5SDimitry Andric       return Error(ExprLoc, "expression must be relocatable");
30350b57cec5SDimitry Andric   }
30360b57cec5SDimitry Andric 
30370b57cec5SDimitry Andric   if (parseToken(AsmToken::EndOfStatement,
30380b57cec5SDimitry Andric                  "unexpected token in .reloc directive"))
30390b57cec5SDimitry Andric       return true;
30400b57cec5SDimitry Andric 
30410b57cec5SDimitry Andric   const MCTargetAsmParser &MCT = getTargetParser();
30420b57cec5SDimitry Andric   const MCSubtargetInfo &STI = MCT.getSTI();
3043*5ffd83dbSDimitry Andric   if (Optional<std::pair<bool, std::string>> Err =
3044*5ffd83dbSDimitry Andric           getStreamer().emitRelocDirective(*Offset, Name, Expr, DirectiveLoc,
3045*5ffd83dbSDimitry Andric                                            STI))
3046*5ffd83dbSDimitry Andric     return Error(Err->first ? NameLoc : OffsetLoc, Err->second);
30470b57cec5SDimitry Andric 
30480b57cec5SDimitry Andric   return false;
30490b57cec5SDimitry Andric }
30500b57cec5SDimitry Andric 
30510b57cec5SDimitry Andric /// parseDirectiveValue
30520b57cec5SDimitry Andric ///  ::= (.byte | .short | ... ) [ expression (, expression)* ]
30530b57cec5SDimitry Andric bool AsmParser::parseDirectiveValue(StringRef IDVal, unsigned Size) {
30540b57cec5SDimitry Andric   auto parseOp = [&]() -> bool {
30550b57cec5SDimitry Andric     const MCExpr *Value;
30560b57cec5SDimitry Andric     SMLoc ExprLoc = getLexer().getLoc();
30570b57cec5SDimitry Andric     if (checkForValidSection() || parseExpression(Value))
30580b57cec5SDimitry Andric       return true;
30590b57cec5SDimitry Andric     // Special case constant expressions to match code generator.
30600b57cec5SDimitry Andric     if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
30610b57cec5SDimitry Andric       assert(Size <= 8 && "Invalid size");
30620b57cec5SDimitry Andric       uint64_t IntValue = MCE->getValue();
30630b57cec5SDimitry Andric       if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
30640b57cec5SDimitry Andric         return Error(ExprLoc, "out of range literal value");
3065*5ffd83dbSDimitry Andric       getStreamer().emitIntValue(IntValue, Size);
30660b57cec5SDimitry Andric     } else
3067*5ffd83dbSDimitry Andric       getStreamer().emitValue(Value, Size, ExprLoc);
30680b57cec5SDimitry Andric     return false;
30690b57cec5SDimitry Andric   };
30700b57cec5SDimitry Andric 
30710b57cec5SDimitry Andric   if (parseMany(parseOp))
30720b57cec5SDimitry Andric     return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
30730b57cec5SDimitry Andric   return false;
30740b57cec5SDimitry Andric }
30750b57cec5SDimitry Andric 
30760b57cec5SDimitry Andric static bool parseHexOcta(AsmParser &Asm, uint64_t &hi, uint64_t &lo) {
30770b57cec5SDimitry Andric   if (Asm.getTok().isNot(AsmToken::Integer) &&
30780b57cec5SDimitry Andric       Asm.getTok().isNot(AsmToken::BigNum))
30790b57cec5SDimitry Andric     return Asm.TokError("unknown token in expression");
30800b57cec5SDimitry Andric   SMLoc ExprLoc = Asm.getTok().getLoc();
30810b57cec5SDimitry Andric   APInt IntValue = Asm.getTok().getAPIntVal();
30820b57cec5SDimitry Andric   Asm.Lex();
30830b57cec5SDimitry Andric   if (!IntValue.isIntN(128))
30840b57cec5SDimitry Andric     return Asm.Error(ExprLoc, "out of range literal value");
30850b57cec5SDimitry Andric   if (!IntValue.isIntN(64)) {
30860b57cec5SDimitry Andric     hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
30870b57cec5SDimitry Andric     lo = IntValue.getLoBits(64).getZExtValue();
30880b57cec5SDimitry Andric   } else {
30890b57cec5SDimitry Andric     hi = 0;
30900b57cec5SDimitry Andric     lo = IntValue.getZExtValue();
30910b57cec5SDimitry Andric   }
30920b57cec5SDimitry Andric   return false;
30930b57cec5SDimitry Andric }
30940b57cec5SDimitry Andric 
30950b57cec5SDimitry Andric /// ParseDirectiveOctaValue
30960b57cec5SDimitry Andric ///  ::= .octa [ hexconstant (, hexconstant)* ]
30970b57cec5SDimitry Andric 
30980b57cec5SDimitry Andric bool AsmParser::parseDirectiveOctaValue(StringRef IDVal) {
30990b57cec5SDimitry Andric   auto parseOp = [&]() -> bool {
31000b57cec5SDimitry Andric     if (checkForValidSection())
31010b57cec5SDimitry Andric       return true;
31020b57cec5SDimitry Andric     uint64_t hi, lo;
31030b57cec5SDimitry Andric     if (parseHexOcta(*this, hi, lo))
31040b57cec5SDimitry Andric       return true;
31050b57cec5SDimitry Andric     if (MAI.isLittleEndian()) {
3106*5ffd83dbSDimitry Andric       getStreamer().emitInt64(lo);
3107*5ffd83dbSDimitry Andric       getStreamer().emitInt64(hi);
31080b57cec5SDimitry Andric     } else {
3109*5ffd83dbSDimitry Andric       getStreamer().emitInt64(hi);
3110*5ffd83dbSDimitry Andric       getStreamer().emitInt64(lo);
31110b57cec5SDimitry Andric     }
31120b57cec5SDimitry Andric     return false;
31130b57cec5SDimitry Andric   };
31140b57cec5SDimitry Andric 
31150b57cec5SDimitry Andric   if (parseMany(parseOp))
31160b57cec5SDimitry Andric     return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
31170b57cec5SDimitry Andric   return false;
31180b57cec5SDimitry Andric }
31190b57cec5SDimitry Andric 
31200b57cec5SDimitry Andric bool AsmParser::parseRealValue(const fltSemantics &Semantics, APInt &Res) {
31210b57cec5SDimitry Andric   // We don't truly support arithmetic on floating point expressions, so we
31220b57cec5SDimitry Andric   // have to manually parse unary prefixes.
31230b57cec5SDimitry Andric   bool IsNeg = false;
31240b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Minus)) {
31250b57cec5SDimitry Andric     Lexer.Lex();
31260b57cec5SDimitry Andric     IsNeg = true;
31270b57cec5SDimitry Andric   } else if (getLexer().is(AsmToken::Plus))
31280b57cec5SDimitry Andric     Lexer.Lex();
31290b57cec5SDimitry Andric 
31300b57cec5SDimitry Andric   if (Lexer.is(AsmToken::Error))
31310b57cec5SDimitry Andric     return TokError(Lexer.getErr());
31320b57cec5SDimitry Andric   if (Lexer.isNot(AsmToken::Integer) && Lexer.isNot(AsmToken::Real) &&
31330b57cec5SDimitry Andric       Lexer.isNot(AsmToken::Identifier))
31340b57cec5SDimitry Andric     return TokError("unexpected token in directive");
31350b57cec5SDimitry Andric 
31360b57cec5SDimitry Andric   // Convert to an APFloat.
31370b57cec5SDimitry Andric   APFloat Value(Semantics);
31380b57cec5SDimitry Andric   StringRef IDVal = getTok().getString();
31390b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Identifier)) {
31400b57cec5SDimitry Andric     if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
31410b57cec5SDimitry Andric       Value = APFloat::getInf(Semantics);
31420b57cec5SDimitry Andric     else if (!IDVal.compare_lower("nan"))
31430b57cec5SDimitry Andric       Value = APFloat::getNaN(Semantics, false, ~0);
31440b57cec5SDimitry Andric     else
31450b57cec5SDimitry Andric       return TokError("invalid floating point literal");
3146480093f4SDimitry Andric   } else if (errorToBool(
3147480093f4SDimitry Andric                  Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven)
3148480093f4SDimitry Andric                      .takeError()))
31490b57cec5SDimitry Andric     return TokError("invalid floating point literal");
31500b57cec5SDimitry Andric   if (IsNeg)
31510b57cec5SDimitry Andric     Value.changeSign();
31520b57cec5SDimitry Andric 
31530b57cec5SDimitry Andric   // Consume the numeric token.
31540b57cec5SDimitry Andric   Lex();
31550b57cec5SDimitry Andric 
31560b57cec5SDimitry Andric   Res = Value.bitcastToAPInt();
31570b57cec5SDimitry Andric 
31580b57cec5SDimitry Andric   return false;
31590b57cec5SDimitry Andric }
31600b57cec5SDimitry Andric 
31610b57cec5SDimitry Andric /// parseDirectiveRealValue
31620b57cec5SDimitry Andric ///  ::= (.single | .double) [ expression (, expression)* ]
31630b57cec5SDimitry Andric bool AsmParser::parseDirectiveRealValue(StringRef IDVal,
31640b57cec5SDimitry Andric                                         const fltSemantics &Semantics) {
31650b57cec5SDimitry Andric   auto parseOp = [&]() -> bool {
31660b57cec5SDimitry Andric     APInt AsInt;
31670b57cec5SDimitry Andric     if (checkForValidSection() || parseRealValue(Semantics, AsInt))
31680b57cec5SDimitry Andric       return true;
3169*5ffd83dbSDimitry Andric     getStreamer().emitIntValue(AsInt.getLimitedValue(),
31700b57cec5SDimitry Andric                                AsInt.getBitWidth() / 8);
31710b57cec5SDimitry Andric     return false;
31720b57cec5SDimitry Andric   };
31730b57cec5SDimitry Andric 
31740b57cec5SDimitry Andric   if (parseMany(parseOp))
31750b57cec5SDimitry Andric     return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
31760b57cec5SDimitry Andric   return false;
31770b57cec5SDimitry Andric }
31780b57cec5SDimitry Andric 
31790b57cec5SDimitry Andric /// parseDirectiveZero
31800b57cec5SDimitry Andric ///  ::= .zero expression
31810b57cec5SDimitry Andric bool AsmParser::parseDirectiveZero() {
31820b57cec5SDimitry Andric   SMLoc NumBytesLoc = Lexer.getLoc();
31830b57cec5SDimitry Andric   const MCExpr *NumBytes;
31840b57cec5SDimitry Andric   if (checkForValidSection() || parseExpression(NumBytes))
31850b57cec5SDimitry Andric     return true;
31860b57cec5SDimitry Andric 
31870b57cec5SDimitry Andric   int64_t Val = 0;
31880b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Comma)) {
31890b57cec5SDimitry Andric     Lex();
31900b57cec5SDimitry Andric     if (parseAbsoluteExpression(Val))
31910b57cec5SDimitry Andric       return true;
31920b57cec5SDimitry Andric   }
31930b57cec5SDimitry Andric 
31940b57cec5SDimitry Andric   if (parseToken(AsmToken::EndOfStatement,
31950b57cec5SDimitry Andric                  "unexpected token in '.zero' directive"))
31960b57cec5SDimitry Andric     return true;
31970b57cec5SDimitry Andric   getStreamer().emitFill(*NumBytes, Val, NumBytesLoc);
31980b57cec5SDimitry Andric 
31990b57cec5SDimitry Andric   return false;
32000b57cec5SDimitry Andric }
32010b57cec5SDimitry Andric 
32020b57cec5SDimitry Andric /// parseDirectiveFill
32030b57cec5SDimitry Andric ///  ::= .fill expression [ , expression [ , expression ] ]
32040b57cec5SDimitry Andric bool AsmParser::parseDirectiveFill() {
32050b57cec5SDimitry Andric   SMLoc NumValuesLoc = Lexer.getLoc();
32060b57cec5SDimitry Andric   const MCExpr *NumValues;
32070b57cec5SDimitry Andric   if (checkForValidSection() || parseExpression(NumValues))
32080b57cec5SDimitry Andric     return true;
32090b57cec5SDimitry Andric 
32100b57cec5SDimitry Andric   int64_t FillSize = 1;
32110b57cec5SDimitry Andric   int64_t FillExpr = 0;
32120b57cec5SDimitry Andric 
32130b57cec5SDimitry Andric   SMLoc SizeLoc, ExprLoc;
32140b57cec5SDimitry Andric 
32150b57cec5SDimitry Andric   if (parseOptionalToken(AsmToken::Comma)) {
32160b57cec5SDimitry Andric     SizeLoc = getTok().getLoc();
32170b57cec5SDimitry Andric     if (parseAbsoluteExpression(FillSize))
32180b57cec5SDimitry Andric       return true;
32190b57cec5SDimitry Andric     if (parseOptionalToken(AsmToken::Comma)) {
32200b57cec5SDimitry Andric       ExprLoc = getTok().getLoc();
32210b57cec5SDimitry Andric       if (parseAbsoluteExpression(FillExpr))
32220b57cec5SDimitry Andric         return true;
32230b57cec5SDimitry Andric     }
32240b57cec5SDimitry Andric   }
32250b57cec5SDimitry Andric   if (parseToken(AsmToken::EndOfStatement,
32260b57cec5SDimitry Andric                  "unexpected token in '.fill' directive"))
32270b57cec5SDimitry Andric     return true;
32280b57cec5SDimitry Andric 
32290b57cec5SDimitry Andric   if (FillSize < 0) {
32300b57cec5SDimitry Andric     Warning(SizeLoc, "'.fill' directive with negative size has no effect");
32310b57cec5SDimitry Andric     return false;
32320b57cec5SDimitry Andric   }
32330b57cec5SDimitry Andric   if (FillSize > 8) {
32340b57cec5SDimitry Andric     Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
32350b57cec5SDimitry Andric     FillSize = 8;
32360b57cec5SDimitry Andric   }
32370b57cec5SDimitry Andric 
32380b57cec5SDimitry Andric   if (!isUInt<32>(FillExpr) && FillSize > 4)
32390b57cec5SDimitry Andric     Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
32400b57cec5SDimitry Andric 
32410b57cec5SDimitry Andric   getStreamer().emitFill(*NumValues, FillSize, FillExpr, NumValuesLoc);
32420b57cec5SDimitry Andric 
32430b57cec5SDimitry Andric   return false;
32440b57cec5SDimitry Andric }
32450b57cec5SDimitry Andric 
32460b57cec5SDimitry Andric /// parseDirectiveOrg
32470b57cec5SDimitry Andric ///  ::= .org expression [ , expression ]
32480b57cec5SDimitry Andric bool AsmParser::parseDirectiveOrg() {
32490b57cec5SDimitry Andric   const MCExpr *Offset;
32500b57cec5SDimitry Andric   SMLoc OffsetLoc = Lexer.getLoc();
32510b57cec5SDimitry Andric   if (checkForValidSection() || parseExpression(Offset))
32520b57cec5SDimitry Andric     return true;
32530b57cec5SDimitry Andric 
32540b57cec5SDimitry Andric   // Parse optional fill expression.
32550b57cec5SDimitry Andric   int64_t FillExpr = 0;
32560b57cec5SDimitry Andric   if (parseOptionalToken(AsmToken::Comma))
32570b57cec5SDimitry Andric     if (parseAbsoluteExpression(FillExpr))
32580b57cec5SDimitry Andric       return addErrorSuffix(" in '.org' directive");
32590b57cec5SDimitry Andric   if (parseToken(AsmToken::EndOfStatement))
32600b57cec5SDimitry Andric     return addErrorSuffix(" in '.org' directive");
32610b57cec5SDimitry Andric 
32620b57cec5SDimitry Andric   getStreamer().emitValueToOffset(Offset, FillExpr, OffsetLoc);
32630b57cec5SDimitry Andric   return false;
32640b57cec5SDimitry Andric }
32650b57cec5SDimitry Andric 
32660b57cec5SDimitry Andric /// parseDirectiveAlign
32670b57cec5SDimitry Andric ///  ::= {.align, ...} expression [ , expression [ , expression ]]
32680b57cec5SDimitry Andric bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
32690b57cec5SDimitry Andric   SMLoc AlignmentLoc = getLexer().getLoc();
32700b57cec5SDimitry Andric   int64_t Alignment;
32710b57cec5SDimitry Andric   SMLoc MaxBytesLoc;
32720b57cec5SDimitry Andric   bool HasFillExpr = false;
32730b57cec5SDimitry Andric   int64_t FillExpr = 0;
32740b57cec5SDimitry Andric   int64_t MaxBytesToFill = 0;
32750b57cec5SDimitry Andric 
32760b57cec5SDimitry Andric   auto parseAlign = [&]() -> bool {
32770b57cec5SDimitry Andric     if (parseAbsoluteExpression(Alignment))
32780b57cec5SDimitry Andric       return true;
32790b57cec5SDimitry Andric     if (parseOptionalToken(AsmToken::Comma)) {
32800b57cec5SDimitry Andric       // The fill expression can be omitted while specifying a maximum number of
32810b57cec5SDimitry Andric       // alignment bytes, e.g:
32820b57cec5SDimitry Andric       //  .align 3,,4
32830b57cec5SDimitry Andric       if (getTok().isNot(AsmToken::Comma)) {
32840b57cec5SDimitry Andric         HasFillExpr = true;
32850b57cec5SDimitry Andric         if (parseAbsoluteExpression(FillExpr))
32860b57cec5SDimitry Andric           return true;
32870b57cec5SDimitry Andric       }
32880b57cec5SDimitry Andric       if (parseOptionalToken(AsmToken::Comma))
32890b57cec5SDimitry Andric         if (parseTokenLoc(MaxBytesLoc) ||
32900b57cec5SDimitry Andric             parseAbsoluteExpression(MaxBytesToFill))
32910b57cec5SDimitry Andric           return true;
32920b57cec5SDimitry Andric     }
32930b57cec5SDimitry Andric     return parseToken(AsmToken::EndOfStatement);
32940b57cec5SDimitry Andric   };
32950b57cec5SDimitry Andric 
32960b57cec5SDimitry Andric   if (checkForValidSection())
32970b57cec5SDimitry Andric     return addErrorSuffix(" in directive");
32980b57cec5SDimitry Andric   // Ignore empty '.p2align' directives for GNU-as compatibility
32990b57cec5SDimitry Andric   if (IsPow2 && (ValueSize == 1) && getTok().is(AsmToken::EndOfStatement)) {
33000b57cec5SDimitry Andric     Warning(AlignmentLoc, "p2align directive with no operand(s) is ignored");
33010b57cec5SDimitry Andric     return parseToken(AsmToken::EndOfStatement);
33020b57cec5SDimitry Andric   }
33030b57cec5SDimitry Andric   if (parseAlign())
33040b57cec5SDimitry Andric     return addErrorSuffix(" in directive");
33050b57cec5SDimitry Andric 
33060b57cec5SDimitry Andric   // Always emit an alignment here even if we thrown an error.
33070b57cec5SDimitry Andric   bool ReturnVal = false;
33080b57cec5SDimitry Andric 
33090b57cec5SDimitry Andric   // Compute alignment in bytes.
33100b57cec5SDimitry Andric   if (IsPow2) {
33110b57cec5SDimitry Andric     // FIXME: Diagnose overflow.
33120b57cec5SDimitry Andric     if (Alignment >= 32) {
33130b57cec5SDimitry Andric       ReturnVal |= Error(AlignmentLoc, "invalid alignment value");
33140b57cec5SDimitry Andric       Alignment = 31;
33150b57cec5SDimitry Andric     }
33160b57cec5SDimitry Andric 
33170b57cec5SDimitry Andric     Alignment = 1ULL << Alignment;
33180b57cec5SDimitry Andric   } else {
33190b57cec5SDimitry Andric     // Reject alignments that aren't either a power of two or zero,
33200b57cec5SDimitry Andric     // for gas compatibility. Alignment of zero is silently rounded
33210b57cec5SDimitry Andric     // up to one.
33220b57cec5SDimitry Andric     if (Alignment == 0)
33230b57cec5SDimitry Andric       Alignment = 1;
33240b57cec5SDimitry Andric     if (!isPowerOf2_64(Alignment))
33250b57cec5SDimitry Andric       ReturnVal |= Error(AlignmentLoc, "alignment must be a power of 2");
33260b57cec5SDimitry Andric   }
33270b57cec5SDimitry Andric 
33280b57cec5SDimitry Andric   // Diagnose non-sensical max bytes to align.
33290b57cec5SDimitry Andric   if (MaxBytesLoc.isValid()) {
33300b57cec5SDimitry Andric     if (MaxBytesToFill < 1) {
33310b57cec5SDimitry Andric       ReturnVal |= Error(MaxBytesLoc,
33320b57cec5SDimitry Andric                          "alignment directive can never be satisfied in this "
33330b57cec5SDimitry Andric                          "many bytes, ignoring maximum bytes expression");
33340b57cec5SDimitry Andric       MaxBytesToFill = 0;
33350b57cec5SDimitry Andric     }
33360b57cec5SDimitry Andric 
33370b57cec5SDimitry Andric     if (MaxBytesToFill >= Alignment) {
33380b57cec5SDimitry Andric       Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
33390b57cec5SDimitry Andric                            "has no effect");
33400b57cec5SDimitry Andric       MaxBytesToFill = 0;
33410b57cec5SDimitry Andric     }
33420b57cec5SDimitry Andric   }
33430b57cec5SDimitry Andric 
33440b57cec5SDimitry Andric   // Check whether we should use optimal code alignment for this .align
33450b57cec5SDimitry Andric   // directive.
33460b57cec5SDimitry Andric   const MCSection *Section = getStreamer().getCurrentSectionOnly();
33470b57cec5SDimitry Andric   assert(Section && "must have section to emit alignment");
33480b57cec5SDimitry Andric   bool UseCodeAlign = Section->UseCodeAlign();
33490b57cec5SDimitry Andric   if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
33500b57cec5SDimitry Andric       ValueSize == 1 && UseCodeAlign) {
3351*5ffd83dbSDimitry Andric     getStreamer().emitCodeAlignment(Alignment, MaxBytesToFill);
33520b57cec5SDimitry Andric   } else {
33530b57cec5SDimitry Andric     // FIXME: Target specific behavior about how the "extra" bytes are filled.
3354*5ffd83dbSDimitry Andric     getStreamer().emitValueToAlignment(Alignment, FillExpr, ValueSize,
33550b57cec5SDimitry Andric                                        MaxBytesToFill);
33560b57cec5SDimitry Andric   }
33570b57cec5SDimitry Andric 
33580b57cec5SDimitry Andric   return ReturnVal;
33590b57cec5SDimitry Andric }
33600b57cec5SDimitry Andric 
33610b57cec5SDimitry Andric /// parseDirectiveFile
33620b57cec5SDimitry Andric /// ::= .file filename
33630b57cec5SDimitry Andric /// ::= .file number [directory] filename [md5 checksum] [source source-text]
33640b57cec5SDimitry Andric bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
33650b57cec5SDimitry Andric   // FIXME: I'm not sure what this is.
33660b57cec5SDimitry Andric   int64_t FileNumber = -1;
33670b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Integer)) {
33680b57cec5SDimitry Andric     FileNumber = getTok().getIntVal();
33690b57cec5SDimitry Andric     Lex();
33700b57cec5SDimitry Andric 
33710b57cec5SDimitry Andric     if (FileNumber < 0)
33720b57cec5SDimitry Andric       return TokError("negative file number");
33730b57cec5SDimitry Andric   }
33740b57cec5SDimitry Andric 
33750b57cec5SDimitry Andric   std::string Path;
33760b57cec5SDimitry Andric 
33770b57cec5SDimitry Andric   // Usually the directory and filename together, otherwise just the directory.
33780b57cec5SDimitry Andric   // Allow the strings to have escaped octal character sequence.
33790b57cec5SDimitry Andric   if (check(getTok().isNot(AsmToken::String),
33800b57cec5SDimitry Andric             "unexpected token in '.file' directive") ||
33810b57cec5SDimitry Andric       parseEscapedString(Path))
33820b57cec5SDimitry Andric     return true;
33830b57cec5SDimitry Andric 
33840b57cec5SDimitry Andric   StringRef Directory;
33850b57cec5SDimitry Andric   StringRef Filename;
33860b57cec5SDimitry Andric   std::string FilenameData;
33870b57cec5SDimitry Andric   if (getLexer().is(AsmToken::String)) {
33880b57cec5SDimitry Andric     if (check(FileNumber == -1,
33890b57cec5SDimitry Andric               "explicit path specified, but no file number") ||
33900b57cec5SDimitry Andric         parseEscapedString(FilenameData))
33910b57cec5SDimitry Andric       return true;
33920b57cec5SDimitry Andric     Filename = FilenameData;
33930b57cec5SDimitry Andric     Directory = Path;
33940b57cec5SDimitry Andric   } else {
33950b57cec5SDimitry Andric     Filename = Path;
33960b57cec5SDimitry Andric   }
33970b57cec5SDimitry Andric 
33980b57cec5SDimitry Andric   uint64_t MD5Hi, MD5Lo;
33990b57cec5SDimitry Andric   bool HasMD5 = false;
34000b57cec5SDimitry Andric 
34010b57cec5SDimitry Andric   Optional<StringRef> Source;
34020b57cec5SDimitry Andric   bool HasSource = false;
34030b57cec5SDimitry Andric   std::string SourceString;
34040b57cec5SDimitry Andric 
34050b57cec5SDimitry Andric   while (!parseOptionalToken(AsmToken::EndOfStatement)) {
34060b57cec5SDimitry Andric     StringRef Keyword;
34070b57cec5SDimitry Andric     if (check(getTok().isNot(AsmToken::Identifier),
34080b57cec5SDimitry Andric               "unexpected token in '.file' directive") ||
34090b57cec5SDimitry Andric         parseIdentifier(Keyword))
34100b57cec5SDimitry Andric       return true;
34110b57cec5SDimitry Andric     if (Keyword == "md5") {
34120b57cec5SDimitry Andric       HasMD5 = true;
34130b57cec5SDimitry Andric       if (check(FileNumber == -1,
34140b57cec5SDimitry Andric                 "MD5 checksum specified, but no file number") ||
34150b57cec5SDimitry Andric           parseHexOcta(*this, MD5Hi, MD5Lo))
34160b57cec5SDimitry Andric         return true;
34170b57cec5SDimitry Andric     } else if (Keyword == "source") {
34180b57cec5SDimitry Andric       HasSource = true;
34190b57cec5SDimitry Andric       if (check(FileNumber == -1,
34200b57cec5SDimitry Andric                 "source specified, but no file number") ||
34210b57cec5SDimitry Andric           check(getTok().isNot(AsmToken::String),
34220b57cec5SDimitry Andric                 "unexpected token in '.file' directive") ||
34230b57cec5SDimitry Andric           parseEscapedString(SourceString))
34240b57cec5SDimitry Andric         return true;
34250b57cec5SDimitry Andric     } else {
34260b57cec5SDimitry Andric       return TokError("unexpected token in '.file' directive");
34270b57cec5SDimitry Andric     }
34280b57cec5SDimitry Andric   }
34290b57cec5SDimitry Andric 
34300b57cec5SDimitry Andric   if (FileNumber == -1) {
34310b57cec5SDimitry Andric     // Ignore the directive if there is no number and the target doesn't support
34320b57cec5SDimitry Andric     // numberless .file directives. This allows some portability of assembler
34330b57cec5SDimitry Andric     // between different object file formats.
34340b57cec5SDimitry Andric     if (getContext().getAsmInfo()->hasSingleParameterDotFile())
3435*5ffd83dbSDimitry Andric       getStreamer().emitFileDirective(Filename);
34360b57cec5SDimitry Andric   } else {
34370b57cec5SDimitry Andric     // In case there is a -g option as well as debug info from directive .file,
34380b57cec5SDimitry Andric     // we turn off the -g option, directly use the existing debug info instead.
34390b57cec5SDimitry Andric     // Throw away any implicit file table for the assembler source.
34400b57cec5SDimitry Andric     if (Ctx.getGenDwarfForAssembly()) {
34410b57cec5SDimitry Andric       Ctx.getMCDwarfLineTable(0).resetFileTable();
34420b57cec5SDimitry Andric       Ctx.setGenDwarfForAssembly(false);
34430b57cec5SDimitry Andric     }
34440b57cec5SDimitry Andric 
34450b57cec5SDimitry Andric     Optional<MD5::MD5Result> CKMem;
34460b57cec5SDimitry Andric     if (HasMD5) {
34470b57cec5SDimitry Andric       MD5::MD5Result Sum;
34480b57cec5SDimitry Andric       for (unsigned i = 0; i != 8; ++i) {
34490b57cec5SDimitry Andric         Sum.Bytes[i] = uint8_t(MD5Hi >> ((7 - i) * 8));
34500b57cec5SDimitry Andric         Sum.Bytes[i + 8] = uint8_t(MD5Lo >> ((7 - i) * 8));
34510b57cec5SDimitry Andric       }
34520b57cec5SDimitry Andric       CKMem = Sum;
34530b57cec5SDimitry Andric     }
34540b57cec5SDimitry Andric     if (HasSource) {
34550b57cec5SDimitry Andric       char *SourceBuf = static_cast<char *>(Ctx.allocate(SourceString.size()));
34560b57cec5SDimitry Andric       memcpy(SourceBuf, SourceString.data(), SourceString.size());
34570b57cec5SDimitry Andric       Source = StringRef(SourceBuf, SourceString.size());
34580b57cec5SDimitry Andric     }
34590b57cec5SDimitry Andric     if (FileNumber == 0) {
34600b57cec5SDimitry Andric       if (Ctx.getDwarfVersion() < 5)
34610b57cec5SDimitry Andric         return Warning(DirectiveLoc, "file 0 not supported prior to DWARF-5");
34620b57cec5SDimitry Andric       getStreamer().emitDwarfFile0Directive(Directory, Filename, CKMem, Source);
34630b57cec5SDimitry Andric     } else {
34640b57cec5SDimitry Andric       Expected<unsigned> FileNumOrErr = getStreamer().tryEmitDwarfFileDirective(
34650b57cec5SDimitry Andric           FileNumber, Directory, Filename, CKMem, Source);
34660b57cec5SDimitry Andric       if (!FileNumOrErr)
34670b57cec5SDimitry Andric         return Error(DirectiveLoc, toString(FileNumOrErr.takeError()));
34680b57cec5SDimitry Andric     }
34690b57cec5SDimitry Andric     // Alert the user if there are some .file directives with MD5 and some not.
34700b57cec5SDimitry Andric     // But only do that once.
34710b57cec5SDimitry Andric     if (!ReportedInconsistentMD5 && !Ctx.isDwarfMD5UsageConsistent(0)) {
34720b57cec5SDimitry Andric       ReportedInconsistentMD5 = true;
34730b57cec5SDimitry Andric       return Warning(DirectiveLoc, "inconsistent use of MD5 checksums");
34740b57cec5SDimitry Andric     }
34750b57cec5SDimitry Andric   }
34760b57cec5SDimitry Andric 
34770b57cec5SDimitry Andric   return false;
34780b57cec5SDimitry Andric }
34790b57cec5SDimitry Andric 
34800b57cec5SDimitry Andric /// parseDirectiveLine
34810b57cec5SDimitry Andric /// ::= .line [number]
34820b57cec5SDimitry Andric bool AsmParser::parseDirectiveLine() {
34830b57cec5SDimitry Andric   int64_t LineNumber;
34840b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Integer)) {
34850b57cec5SDimitry Andric     if (parseIntToken(LineNumber, "unexpected token in '.line' directive"))
34860b57cec5SDimitry Andric       return true;
34870b57cec5SDimitry Andric     (void)LineNumber;
34880b57cec5SDimitry Andric     // FIXME: Do something with the .line.
34890b57cec5SDimitry Andric   }
34900b57cec5SDimitry Andric   if (parseToken(AsmToken::EndOfStatement,
34910b57cec5SDimitry Andric                  "unexpected token in '.line' directive"))
34920b57cec5SDimitry Andric     return true;
34930b57cec5SDimitry Andric 
34940b57cec5SDimitry Andric   return false;
34950b57cec5SDimitry Andric }
34960b57cec5SDimitry Andric 
34970b57cec5SDimitry Andric /// parseDirectiveLoc
34980b57cec5SDimitry Andric /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
34990b57cec5SDimitry Andric ///                                [epilogue_begin] [is_stmt VALUE] [isa VALUE]
35000b57cec5SDimitry Andric /// The first number is a file number, must have been previously assigned with
35010b57cec5SDimitry Andric /// a .file directive, the second number is the line number and optionally the
35020b57cec5SDimitry Andric /// third number is a column position (zero if not specified).  The remaining
35030b57cec5SDimitry Andric /// optional items are .loc sub-directives.
35040b57cec5SDimitry Andric bool AsmParser::parseDirectiveLoc() {
35050b57cec5SDimitry Andric   int64_t FileNumber = 0, LineNumber = 0;
35060b57cec5SDimitry Andric   SMLoc Loc = getTok().getLoc();
35070b57cec5SDimitry Andric   if (parseIntToken(FileNumber, "unexpected token in '.loc' directive") ||
35080b57cec5SDimitry Andric       check(FileNumber < 1 && Ctx.getDwarfVersion() < 5, Loc,
35090b57cec5SDimitry Andric             "file number less than one in '.loc' directive") ||
35100b57cec5SDimitry Andric       check(!getContext().isValidDwarfFileNumber(FileNumber), Loc,
35110b57cec5SDimitry Andric             "unassigned file number in '.loc' directive"))
35120b57cec5SDimitry Andric     return true;
35130b57cec5SDimitry Andric 
35140b57cec5SDimitry Andric   // optional
35150b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Integer)) {
35160b57cec5SDimitry Andric     LineNumber = getTok().getIntVal();
35170b57cec5SDimitry Andric     if (LineNumber < 0)
35180b57cec5SDimitry Andric       return TokError("line number less than zero in '.loc' directive");
35190b57cec5SDimitry Andric     Lex();
35200b57cec5SDimitry Andric   }
35210b57cec5SDimitry Andric 
35220b57cec5SDimitry Andric   int64_t ColumnPos = 0;
35230b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Integer)) {
35240b57cec5SDimitry Andric     ColumnPos = getTok().getIntVal();
35250b57cec5SDimitry Andric     if (ColumnPos < 0)
35260b57cec5SDimitry Andric       return TokError("column position less than zero in '.loc' directive");
35270b57cec5SDimitry Andric     Lex();
35280b57cec5SDimitry Andric   }
35290b57cec5SDimitry Andric 
3530*5ffd83dbSDimitry Andric   auto PrevFlags = getContext().getCurrentDwarfLoc().getFlags();
3531*5ffd83dbSDimitry Andric   unsigned Flags = PrevFlags & DWARF2_FLAG_IS_STMT;
35320b57cec5SDimitry Andric   unsigned Isa = 0;
35330b57cec5SDimitry Andric   int64_t Discriminator = 0;
35340b57cec5SDimitry Andric 
35350b57cec5SDimitry Andric   auto parseLocOp = [&]() -> bool {
35360b57cec5SDimitry Andric     StringRef Name;
35370b57cec5SDimitry Andric     SMLoc Loc = getTok().getLoc();
35380b57cec5SDimitry Andric     if (parseIdentifier(Name))
35390b57cec5SDimitry Andric       return TokError("unexpected token in '.loc' directive");
35400b57cec5SDimitry Andric 
35410b57cec5SDimitry Andric     if (Name == "basic_block")
35420b57cec5SDimitry Andric       Flags |= DWARF2_FLAG_BASIC_BLOCK;
35430b57cec5SDimitry Andric     else if (Name == "prologue_end")
35440b57cec5SDimitry Andric       Flags |= DWARF2_FLAG_PROLOGUE_END;
35450b57cec5SDimitry Andric     else if (Name == "epilogue_begin")
35460b57cec5SDimitry Andric       Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
35470b57cec5SDimitry Andric     else if (Name == "is_stmt") {
35480b57cec5SDimitry Andric       Loc = getTok().getLoc();
35490b57cec5SDimitry Andric       const MCExpr *Value;
35500b57cec5SDimitry Andric       if (parseExpression(Value))
35510b57cec5SDimitry Andric         return true;
35520b57cec5SDimitry Andric       // The expression must be the constant 0 or 1.
35530b57cec5SDimitry Andric       if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
35540b57cec5SDimitry Andric         int Value = MCE->getValue();
35550b57cec5SDimitry Andric         if (Value == 0)
35560b57cec5SDimitry Andric           Flags &= ~DWARF2_FLAG_IS_STMT;
35570b57cec5SDimitry Andric         else if (Value == 1)
35580b57cec5SDimitry Andric           Flags |= DWARF2_FLAG_IS_STMT;
35590b57cec5SDimitry Andric         else
35600b57cec5SDimitry Andric           return Error(Loc, "is_stmt value not 0 or 1");
35610b57cec5SDimitry Andric       } else {
35620b57cec5SDimitry Andric         return Error(Loc, "is_stmt value not the constant value of 0 or 1");
35630b57cec5SDimitry Andric       }
35640b57cec5SDimitry Andric     } else if (Name == "isa") {
35650b57cec5SDimitry Andric       Loc = getTok().getLoc();
35660b57cec5SDimitry Andric       const MCExpr *Value;
35670b57cec5SDimitry Andric       if (parseExpression(Value))
35680b57cec5SDimitry Andric         return true;
35690b57cec5SDimitry Andric       // The expression must be a constant greater or equal to 0.
35700b57cec5SDimitry Andric       if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
35710b57cec5SDimitry Andric         int Value = MCE->getValue();
35720b57cec5SDimitry Andric         if (Value < 0)
35730b57cec5SDimitry Andric           return Error(Loc, "isa number less than zero");
35740b57cec5SDimitry Andric         Isa = Value;
35750b57cec5SDimitry Andric       } else {
35760b57cec5SDimitry Andric         return Error(Loc, "isa number not a constant value");
35770b57cec5SDimitry Andric       }
35780b57cec5SDimitry Andric     } else if (Name == "discriminator") {
35790b57cec5SDimitry Andric       if (parseAbsoluteExpression(Discriminator))
35800b57cec5SDimitry Andric         return true;
35810b57cec5SDimitry Andric     } else {
35820b57cec5SDimitry Andric       return Error(Loc, "unknown sub-directive in '.loc' directive");
35830b57cec5SDimitry Andric     }
35840b57cec5SDimitry Andric     return false;
35850b57cec5SDimitry Andric   };
35860b57cec5SDimitry Andric 
35870b57cec5SDimitry Andric   if (parseMany(parseLocOp, false /*hasComma*/))
35880b57cec5SDimitry Andric     return true;
35890b57cec5SDimitry Andric 
3590*5ffd83dbSDimitry Andric   getStreamer().emitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
35910b57cec5SDimitry Andric                                       Isa, Discriminator, StringRef());
35920b57cec5SDimitry Andric 
35930b57cec5SDimitry Andric   return false;
35940b57cec5SDimitry Andric }
35950b57cec5SDimitry Andric 
35960b57cec5SDimitry Andric /// parseDirectiveStabs
35970b57cec5SDimitry Andric /// ::= .stabs string, number, number, number
35980b57cec5SDimitry Andric bool AsmParser::parseDirectiveStabs() {
35990b57cec5SDimitry Andric   return TokError("unsupported directive '.stabs'");
36000b57cec5SDimitry Andric }
36010b57cec5SDimitry Andric 
36020b57cec5SDimitry Andric /// parseDirectiveCVFile
36030b57cec5SDimitry Andric /// ::= .cv_file number filename [checksum] [checksumkind]
36040b57cec5SDimitry Andric bool AsmParser::parseDirectiveCVFile() {
36050b57cec5SDimitry Andric   SMLoc FileNumberLoc = getTok().getLoc();
36060b57cec5SDimitry Andric   int64_t FileNumber;
36070b57cec5SDimitry Andric   std::string Filename;
36080b57cec5SDimitry Andric   std::string Checksum;
36090b57cec5SDimitry Andric   int64_t ChecksumKind = 0;
36100b57cec5SDimitry Andric 
36110b57cec5SDimitry Andric   if (parseIntToken(FileNumber,
36120b57cec5SDimitry Andric                     "expected file number in '.cv_file' directive") ||
36130b57cec5SDimitry Andric       check(FileNumber < 1, FileNumberLoc, "file number less than one") ||
36140b57cec5SDimitry Andric       check(getTok().isNot(AsmToken::String),
36150b57cec5SDimitry Andric             "unexpected token in '.cv_file' directive") ||
36160b57cec5SDimitry Andric       parseEscapedString(Filename))
36170b57cec5SDimitry Andric     return true;
36180b57cec5SDimitry Andric   if (!parseOptionalToken(AsmToken::EndOfStatement)) {
36190b57cec5SDimitry Andric     if (check(getTok().isNot(AsmToken::String),
36200b57cec5SDimitry Andric               "unexpected token in '.cv_file' directive") ||
36210b57cec5SDimitry Andric         parseEscapedString(Checksum) ||
36220b57cec5SDimitry Andric         parseIntToken(ChecksumKind,
36230b57cec5SDimitry Andric                       "expected checksum kind in '.cv_file' directive") ||
36240b57cec5SDimitry Andric         parseToken(AsmToken::EndOfStatement,
36250b57cec5SDimitry Andric                    "unexpected token in '.cv_file' directive"))
36260b57cec5SDimitry Andric       return true;
36270b57cec5SDimitry Andric   }
36280b57cec5SDimitry Andric 
36290b57cec5SDimitry Andric   Checksum = fromHex(Checksum);
36300b57cec5SDimitry Andric   void *CKMem = Ctx.allocate(Checksum.size(), 1);
36310b57cec5SDimitry Andric   memcpy(CKMem, Checksum.data(), Checksum.size());
36320b57cec5SDimitry Andric   ArrayRef<uint8_t> ChecksumAsBytes(reinterpret_cast<const uint8_t *>(CKMem),
36330b57cec5SDimitry Andric                                     Checksum.size());
36340b57cec5SDimitry Andric 
36350b57cec5SDimitry Andric   if (!getStreamer().EmitCVFileDirective(FileNumber, Filename, ChecksumAsBytes,
36360b57cec5SDimitry Andric                                          static_cast<uint8_t>(ChecksumKind)))
36370b57cec5SDimitry Andric     return Error(FileNumberLoc, "file number already allocated");
36380b57cec5SDimitry Andric 
36390b57cec5SDimitry Andric   return false;
36400b57cec5SDimitry Andric }
36410b57cec5SDimitry Andric 
36420b57cec5SDimitry Andric bool AsmParser::parseCVFunctionId(int64_t &FunctionId,
36430b57cec5SDimitry Andric                                   StringRef DirectiveName) {
36440b57cec5SDimitry Andric   SMLoc Loc;
36450b57cec5SDimitry Andric   return parseTokenLoc(Loc) ||
36460b57cec5SDimitry Andric          parseIntToken(FunctionId, "expected function id in '" + DirectiveName +
36470b57cec5SDimitry Andric                                        "' directive") ||
36480b57cec5SDimitry Andric          check(FunctionId < 0 || FunctionId >= UINT_MAX, Loc,
36490b57cec5SDimitry Andric                "expected function id within range [0, UINT_MAX)");
36500b57cec5SDimitry Andric }
36510b57cec5SDimitry Andric 
36520b57cec5SDimitry Andric bool AsmParser::parseCVFileId(int64_t &FileNumber, StringRef DirectiveName) {
36530b57cec5SDimitry Andric   SMLoc Loc;
36540b57cec5SDimitry Andric   return parseTokenLoc(Loc) ||
36550b57cec5SDimitry Andric          parseIntToken(FileNumber, "expected integer in '" + DirectiveName +
36560b57cec5SDimitry Andric                                        "' directive") ||
36570b57cec5SDimitry Andric          check(FileNumber < 1, Loc, "file number less than one in '" +
36580b57cec5SDimitry Andric                                         DirectiveName + "' directive") ||
36590b57cec5SDimitry Andric          check(!getCVContext().isValidFileNumber(FileNumber), Loc,
36600b57cec5SDimitry Andric                "unassigned file number in '" + DirectiveName + "' directive");
36610b57cec5SDimitry Andric }
36620b57cec5SDimitry Andric 
36630b57cec5SDimitry Andric /// parseDirectiveCVFuncId
36640b57cec5SDimitry Andric /// ::= .cv_func_id FunctionId
36650b57cec5SDimitry Andric ///
36660b57cec5SDimitry Andric /// Introduces a function ID that can be used with .cv_loc.
36670b57cec5SDimitry Andric bool AsmParser::parseDirectiveCVFuncId() {
36680b57cec5SDimitry Andric   SMLoc FunctionIdLoc = getTok().getLoc();
36690b57cec5SDimitry Andric   int64_t FunctionId;
36700b57cec5SDimitry Andric 
36710b57cec5SDimitry Andric   if (parseCVFunctionId(FunctionId, ".cv_func_id") ||
36720b57cec5SDimitry Andric       parseToken(AsmToken::EndOfStatement,
36730b57cec5SDimitry Andric                  "unexpected token in '.cv_func_id' directive"))
36740b57cec5SDimitry Andric     return true;
36750b57cec5SDimitry Andric 
36760b57cec5SDimitry Andric   if (!getStreamer().EmitCVFuncIdDirective(FunctionId))
36770b57cec5SDimitry Andric     return Error(FunctionIdLoc, "function id already allocated");
36780b57cec5SDimitry Andric 
36790b57cec5SDimitry Andric   return false;
36800b57cec5SDimitry Andric }
36810b57cec5SDimitry Andric 
36820b57cec5SDimitry Andric /// parseDirectiveCVInlineSiteId
36830b57cec5SDimitry Andric /// ::= .cv_inline_site_id FunctionId
36840b57cec5SDimitry Andric ///         "within" IAFunc
36850b57cec5SDimitry Andric ///         "inlined_at" IAFile IALine [IACol]
36860b57cec5SDimitry Andric ///
36870b57cec5SDimitry Andric /// Introduces a function ID that can be used with .cv_loc. Includes "inlined
36880b57cec5SDimitry Andric /// at" source location information for use in the line table of the caller,
36890b57cec5SDimitry Andric /// whether the caller is a real function or another inlined call site.
36900b57cec5SDimitry Andric bool AsmParser::parseDirectiveCVInlineSiteId() {
36910b57cec5SDimitry Andric   SMLoc FunctionIdLoc = getTok().getLoc();
36920b57cec5SDimitry Andric   int64_t FunctionId;
36930b57cec5SDimitry Andric   int64_t IAFunc;
36940b57cec5SDimitry Andric   int64_t IAFile;
36950b57cec5SDimitry Andric   int64_t IALine;
36960b57cec5SDimitry Andric   int64_t IACol = 0;
36970b57cec5SDimitry Andric 
36980b57cec5SDimitry Andric   // FunctionId
36990b57cec5SDimitry Andric   if (parseCVFunctionId(FunctionId, ".cv_inline_site_id"))
37000b57cec5SDimitry Andric     return true;
37010b57cec5SDimitry Andric 
37020b57cec5SDimitry Andric   // "within"
37030b57cec5SDimitry Andric   if (check((getLexer().isNot(AsmToken::Identifier) ||
37040b57cec5SDimitry Andric              getTok().getIdentifier() != "within"),
37050b57cec5SDimitry Andric             "expected 'within' identifier in '.cv_inline_site_id' directive"))
37060b57cec5SDimitry Andric     return true;
37070b57cec5SDimitry Andric   Lex();
37080b57cec5SDimitry Andric 
37090b57cec5SDimitry Andric   // IAFunc
37100b57cec5SDimitry Andric   if (parseCVFunctionId(IAFunc, ".cv_inline_site_id"))
37110b57cec5SDimitry Andric     return true;
37120b57cec5SDimitry Andric 
37130b57cec5SDimitry Andric   // "inlined_at"
37140b57cec5SDimitry Andric   if (check((getLexer().isNot(AsmToken::Identifier) ||
37150b57cec5SDimitry Andric              getTok().getIdentifier() != "inlined_at"),
37160b57cec5SDimitry Andric             "expected 'inlined_at' identifier in '.cv_inline_site_id' "
37170b57cec5SDimitry Andric             "directive") )
37180b57cec5SDimitry Andric     return true;
37190b57cec5SDimitry Andric   Lex();
37200b57cec5SDimitry Andric 
37210b57cec5SDimitry Andric   // IAFile IALine
37220b57cec5SDimitry Andric   if (parseCVFileId(IAFile, ".cv_inline_site_id") ||
37230b57cec5SDimitry Andric       parseIntToken(IALine, "expected line number after 'inlined_at'"))
37240b57cec5SDimitry Andric     return true;
37250b57cec5SDimitry Andric 
37260b57cec5SDimitry Andric   // [IACol]
37270b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Integer)) {
37280b57cec5SDimitry Andric     IACol = getTok().getIntVal();
37290b57cec5SDimitry Andric     Lex();
37300b57cec5SDimitry Andric   }
37310b57cec5SDimitry Andric 
37320b57cec5SDimitry Andric   if (parseToken(AsmToken::EndOfStatement,
37330b57cec5SDimitry Andric                  "unexpected token in '.cv_inline_site_id' directive"))
37340b57cec5SDimitry Andric     return true;
37350b57cec5SDimitry Andric 
37360b57cec5SDimitry Andric   if (!getStreamer().EmitCVInlineSiteIdDirective(FunctionId, IAFunc, IAFile,
37370b57cec5SDimitry Andric                                                  IALine, IACol, FunctionIdLoc))
37380b57cec5SDimitry Andric     return Error(FunctionIdLoc, "function id already allocated");
37390b57cec5SDimitry Andric 
37400b57cec5SDimitry Andric   return false;
37410b57cec5SDimitry Andric }
37420b57cec5SDimitry Andric 
37430b57cec5SDimitry Andric /// parseDirectiveCVLoc
37440b57cec5SDimitry Andric /// ::= .cv_loc FunctionId FileNumber [LineNumber] [ColumnPos] [prologue_end]
37450b57cec5SDimitry Andric ///                                [is_stmt VALUE]
37460b57cec5SDimitry Andric /// The first number is a file number, must have been previously assigned with
37470b57cec5SDimitry Andric /// a .file directive, the second number is the line number and optionally the
37480b57cec5SDimitry Andric /// third number is a column position (zero if not specified).  The remaining
37490b57cec5SDimitry Andric /// optional items are .loc sub-directives.
37500b57cec5SDimitry Andric bool AsmParser::parseDirectiveCVLoc() {
37510b57cec5SDimitry Andric   SMLoc DirectiveLoc = getTok().getLoc();
37520b57cec5SDimitry Andric   int64_t FunctionId, FileNumber;
37530b57cec5SDimitry Andric   if (parseCVFunctionId(FunctionId, ".cv_loc") ||
37540b57cec5SDimitry Andric       parseCVFileId(FileNumber, ".cv_loc"))
37550b57cec5SDimitry Andric     return true;
37560b57cec5SDimitry Andric 
37570b57cec5SDimitry Andric   int64_t LineNumber = 0;
37580b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Integer)) {
37590b57cec5SDimitry Andric     LineNumber = getTok().getIntVal();
37600b57cec5SDimitry Andric     if (LineNumber < 0)
37610b57cec5SDimitry Andric       return TokError("line number less than zero in '.cv_loc' directive");
37620b57cec5SDimitry Andric     Lex();
37630b57cec5SDimitry Andric   }
37640b57cec5SDimitry Andric 
37650b57cec5SDimitry Andric   int64_t ColumnPos = 0;
37660b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Integer)) {
37670b57cec5SDimitry Andric     ColumnPos = getTok().getIntVal();
37680b57cec5SDimitry Andric     if (ColumnPos < 0)
37690b57cec5SDimitry Andric       return TokError("column position less than zero in '.cv_loc' directive");
37700b57cec5SDimitry Andric     Lex();
37710b57cec5SDimitry Andric   }
37720b57cec5SDimitry Andric 
37730b57cec5SDimitry Andric   bool PrologueEnd = false;
37740b57cec5SDimitry Andric   uint64_t IsStmt = 0;
37750b57cec5SDimitry Andric 
37760b57cec5SDimitry Andric   auto parseOp = [&]() -> bool {
37770b57cec5SDimitry Andric     StringRef Name;
37780b57cec5SDimitry Andric     SMLoc Loc = getTok().getLoc();
37790b57cec5SDimitry Andric     if (parseIdentifier(Name))
37800b57cec5SDimitry Andric       return TokError("unexpected token in '.cv_loc' directive");
37810b57cec5SDimitry Andric     if (Name == "prologue_end")
37820b57cec5SDimitry Andric       PrologueEnd = true;
37830b57cec5SDimitry Andric     else if (Name == "is_stmt") {
37840b57cec5SDimitry Andric       Loc = getTok().getLoc();
37850b57cec5SDimitry Andric       const MCExpr *Value;
37860b57cec5SDimitry Andric       if (parseExpression(Value))
37870b57cec5SDimitry Andric         return true;
37880b57cec5SDimitry Andric       // The expression must be the constant 0 or 1.
37890b57cec5SDimitry Andric       IsStmt = ~0ULL;
37900b57cec5SDimitry Andric       if (const auto *MCE = dyn_cast<MCConstantExpr>(Value))
37910b57cec5SDimitry Andric         IsStmt = MCE->getValue();
37920b57cec5SDimitry Andric 
37930b57cec5SDimitry Andric       if (IsStmt > 1)
37940b57cec5SDimitry Andric         return Error(Loc, "is_stmt value not 0 or 1");
37950b57cec5SDimitry Andric     } else {
37960b57cec5SDimitry Andric       return Error(Loc, "unknown sub-directive in '.cv_loc' directive");
37970b57cec5SDimitry Andric     }
37980b57cec5SDimitry Andric     return false;
37990b57cec5SDimitry Andric   };
38000b57cec5SDimitry Andric 
38010b57cec5SDimitry Andric   if (parseMany(parseOp, false /*hasComma*/))
38020b57cec5SDimitry Andric     return true;
38030b57cec5SDimitry Andric 
3804*5ffd83dbSDimitry Andric   getStreamer().emitCVLocDirective(FunctionId, FileNumber, LineNumber,
38050b57cec5SDimitry Andric                                    ColumnPos, PrologueEnd, IsStmt, StringRef(),
38060b57cec5SDimitry Andric                                    DirectiveLoc);
38070b57cec5SDimitry Andric   return false;
38080b57cec5SDimitry Andric }
38090b57cec5SDimitry Andric 
38100b57cec5SDimitry Andric /// parseDirectiveCVLinetable
38110b57cec5SDimitry Andric /// ::= .cv_linetable FunctionId, FnStart, FnEnd
38120b57cec5SDimitry Andric bool AsmParser::parseDirectiveCVLinetable() {
38130b57cec5SDimitry Andric   int64_t FunctionId;
38140b57cec5SDimitry Andric   StringRef FnStartName, FnEndName;
38150b57cec5SDimitry Andric   SMLoc Loc = getTok().getLoc();
38160b57cec5SDimitry Andric   if (parseCVFunctionId(FunctionId, ".cv_linetable") ||
38170b57cec5SDimitry Andric       parseToken(AsmToken::Comma,
38180b57cec5SDimitry Andric                  "unexpected token in '.cv_linetable' directive") ||
38190b57cec5SDimitry Andric       parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc,
38200b57cec5SDimitry Andric                                   "expected identifier in directive") ||
38210b57cec5SDimitry Andric       parseToken(AsmToken::Comma,
38220b57cec5SDimitry Andric                  "unexpected token in '.cv_linetable' directive") ||
38230b57cec5SDimitry Andric       parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc,
38240b57cec5SDimitry Andric                                   "expected identifier in directive"))
38250b57cec5SDimitry Andric     return true;
38260b57cec5SDimitry Andric 
38270b57cec5SDimitry Andric   MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
38280b57cec5SDimitry Andric   MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
38290b57cec5SDimitry Andric 
3830*5ffd83dbSDimitry Andric   getStreamer().emitCVLinetableDirective(FunctionId, FnStartSym, FnEndSym);
38310b57cec5SDimitry Andric   return false;
38320b57cec5SDimitry Andric }
38330b57cec5SDimitry Andric 
38340b57cec5SDimitry Andric /// parseDirectiveCVInlineLinetable
38350b57cec5SDimitry Andric /// ::= .cv_inline_linetable PrimaryFunctionId FileId LineNum FnStart FnEnd
38360b57cec5SDimitry Andric bool AsmParser::parseDirectiveCVInlineLinetable() {
38370b57cec5SDimitry Andric   int64_t PrimaryFunctionId, SourceFileId, SourceLineNum;
38380b57cec5SDimitry Andric   StringRef FnStartName, FnEndName;
38390b57cec5SDimitry Andric   SMLoc Loc = getTok().getLoc();
38400b57cec5SDimitry Andric   if (parseCVFunctionId(PrimaryFunctionId, ".cv_inline_linetable") ||
38410b57cec5SDimitry Andric       parseTokenLoc(Loc) ||
38420b57cec5SDimitry Andric       parseIntToken(
38430b57cec5SDimitry Andric           SourceFileId,
38440b57cec5SDimitry Andric           "expected SourceField in '.cv_inline_linetable' directive") ||
38450b57cec5SDimitry Andric       check(SourceFileId <= 0, Loc,
38460b57cec5SDimitry Andric             "File id less than zero in '.cv_inline_linetable' directive") ||
38470b57cec5SDimitry Andric       parseTokenLoc(Loc) ||
38480b57cec5SDimitry Andric       parseIntToken(
38490b57cec5SDimitry Andric           SourceLineNum,
38500b57cec5SDimitry Andric           "expected SourceLineNum in '.cv_inline_linetable' directive") ||
38510b57cec5SDimitry Andric       check(SourceLineNum < 0, Loc,
38520b57cec5SDimitry Andric             "Line number less than zero in '.cv_inline_linetable' directive") ||
38530b57cec5SDimitry Andric       parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc,
38540b57cec5SDimitry Andric                                   "expected identifier in directive") ||
38550b57cec5SDimitry Andric       parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc,
38560b57cec5SDimitry Andric                                   "expected identifier in directive"))
38570b57cec5SDimitry Andric     return true;
38580b57cec5SDimitry Andric 
38590b57cec5SDimitry Andric   if (parseToken(AsmToken::EndOfStatement, "Expected End of Statement"))
38600b57cec5SDimitry Andric     return true;
38610b57cec5SDimitry Andric 
38620b57cec5SDimitry Andric   MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
38630b57cec5SDimitry Andric   MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
3864*5ffd83dbSDimitry Andric   getStreamer().emitCVInlineLinetableDirective(PrimaryFunctionId, SourceFileId,
38650b57cec5SDimitry Andric                                                SourceLineNum, FnStartSym,
38660b57cec5SDimitry Andric                                                FnEndSym);
38670b57cec5SDimitry Andric   return false;
38680b57cec5SDimitry Andric }
38690b57cec5SDimitry Andric 
38708bcb0991SDimitry Andric void AsmParser::initializeCVDefRangeTypeMap() {
38718bcb0991SDimitry Andric   CVDefRangeTypeMap["reg"] = CVDR_DEFRANGE_REGISTER;
38728bcb0991SDimitry Andric   CVDefRangeTypeMap["frame_ptr_rel"] = CVDR_DEFRANGE_FRAMEPOINTER_REL;
38738bcb0991SDimitry Andric   CVDefRangeTypeMap["subfield_reg"] = CVDR_DEFRANGE_SUBFIELD_REGISTER;
38748bcb0991SDimitry Andric   CVDefRangeTypeMap["reg_rel"] = CVDR_DEFRANGE_REGISTER_REL;
38758bcb0991SDimitry Andric }
38768bcb0991SDimitry Andric 
38770b57cec5SDimitry Andric /// parseDirectiveCVDefRange
38780b57cec5SDimitry Andric /// ::= .cv_def_range RangeStart RangeEnd (GapStart GapEnd)*, bytes*
38790b57cec5SDimitry Andric bool AsmParser::parseDirectiveCVDefRange() {
38800b57cec5SDimitry Andric   SMLoc Loc;
38810b57cec5SDimitry Andric   std::vector<std::pair<const MCSymbol *, const MCSymbol *>> Ranges;
38820b57cec5SDimitry Andric   while (getLexer().is(AsmToken::Identifier)) {
38830b57cec5SDimitry Andric     Loc = getLexer().getLoc();
38840b57cec5SDimitry Andric     StringRef GapStartName;
38850b57cec5SDimitry Andric     if (parseIdentifier(GapStartName))
38860b57cec5SDimitry Andric       return Error(Loc, "expected identifier in directive");
38870b57cec5SDimitry Andric     MCSymbol *GapStartSym = getContext().getOrCreateSymbol(GapStartName);
38880b57cec5SDimitry Andric 
38890b57cec5SDimitry Andric     Loc = getLexer().getLoc();
38900b57cec5SDimitry Andric     StringRef GapEndName;
38910b57cec5SDimitry Andric     if (parseIdentifier(GapEndName))
38920b57cec5SDimitry Andric       return Error(Loc, "expected identifier in directive");
38930b57cec5SDimitry Andric     MCSymbol *GapEndSym = getContext().getOrCreateSymbol(GapEndName);
38940b57cec5SDimitry Andric 
38950b57cec5SDimitry Andric     Ranges.push_back({GapStartSym, GapEndSym});
38960b57cec5SDimitry Andric   }
38970b57cec5SDimitry Andric 
38988bcb0991SDimitry Andric   StringRef CVDefRangeTypeStr;
38998bcb0991SDimitry Andric   if (parseToken(
39008bcb0991SDimitry Andric           AsmToken::Comma,
39018bcb0991SDimitry Andric           "expected comma before def_range type in .cv_def_range directive") ||
39028bcb0991SDimitry Andric       parseIdentifier(CVDefRangeTypeStr))
39038bcb0991SDimitry Andric     return Error(Loc, "expected def_range type in directive");
39040b57cec5SDimitry Andric 
39058bcb0991SDimitry Andric   StringMap<CVDefRangeType>::const_iterator CVTypeIt =
39068bcb0991SDimitry Andric       CVDefRangeTypeMap.find(CVDefRangeTypeStr);
39078bcb0991SDimitry Andric   CVDefRangeType CVDRType = (CVTypeIt == CVDefRangeTypeMap.end())
39088bcb0991SDimitry Andric                                 ? CVDR_DEFRANGE
39098bcb0991SDimitry Andric                                 : CVTypeIt->getValue();
39108bcb0991SDimitry Andric   switch (CVDRType) {
39118bcb0991SDimitry Andric   case CVDR_DEFRANGE_REGISTER: {
39128bcb0991SDimitry Andric     int64_t DRRegister;
39138bcb0991SDimitry Andric     if (parseToken(AsmToken::Comma, "expected comma before register number in "
39148bcb0991SDimitry Andric                                     ".cv_def_range directive") ||
39158bcb0991SDimitry Andric         parseAbsoluteExpression(DRRegister))
39168bcb0991SDimitry Andric       return Error(Loc, "expected register number");
39178bcb0991SDimitry Andric 
39188bcb0991SDimitry Andric     codeview::DefRangeRegisterHeader DRHdr;
39198bcb0991SDimitry Andric     DRHdr.Register = DRRegister;
39208bcb0991SDimitry Andric     DRHdr.MayHaveNoName = 0;
3921*5ffd83dbSDimitry Andric     getStreamer().emitCVDefRangeDirective(Ranges, DRHdr);
39228bcb0991SDimitry Andric     break;
39238bcb0991SDimitry Andric   }
39248bcb0991SDimitry Andric   case CVDR_DEFRANGE_FRAMEPOINTER_REL: {
39258bcb0991SDimitry Andric     int64_t DROffset;
39268bcb0991SDimitry Andric     if (parseToken(AsmToken::Comma,
39278bcb0991SDimitry Andric                    "expected comma before offset in .cv_def_range directive") ||
39288bcb0991SDimitry Andric         parseAbsoluteExpression(DROffset))
39298bcb0991SDimitry Andric       return Error(Loc, "expected offset value");
39308bcb0991SDimitry Andric 
39318bcb0991SDimitry Andric     codeview::DefRangeFramePointerRelHeader DRHdr;
39328bcb0991SDimitry Andric     DRHdr.Offset = DROffset;
3933*5ffd83dbSDimitry Andric     getStreamer().emitCVDefRangeDirective(Ranges, DRHdr);
39348bcb0991SDimitry Andric     break;
39358bcb0991SDimitry Andric   }
39368bcb0991SDimitry Andric   case CVDR_DEFRANGE_SUBFIELD_REGISTER: {
39378bcb0991SDimitry Andric     int64_t DRRegister;
39388bcb0991SDimitry Andric     int64_t DROffsetInParent;
39398bcb0991SDimitry Andric     if (parseToken(AsmToken::Comma, "expected comma before register number in "
39408bcb0991SDimitry Andric                                     ".cv_def_range directive") ||
39418bcb0991SDimitry Andric         parseAbsoluteExpression(DRRegister))
39428bcb0991SDimitry Andric       return Error(Loc, "expected register number");
39438bcb0991SDimitry Andric     if (parseToken(AsmToken::Comma,
39448bcb0991SDimitry Andric                    "expected comma before offset in .cv_def_range directive") ||
39458bcb0991SDimitry Andric         parseAbsoluteExpression(DROffsetInParent))
39468bcb0991SDimitry Andric       return Error(Loc, "expected offset value");
39478bcb0991SDimitry Andric 
39488bcb0991SDimitry Andric     codeview::DefRangeSubfieldRegisterHeader DRHdr;
39498bcb0991SDimitry Andric     DRHdr.Register = DRRegister;
39508bcb0991SDimitry Andric     DRHdr.MayHaveNoName = 0;
39518bcb0991SDimitry Andric     DRHdr.OffsetInParent = DROffsetInParent;
3952*5ffd83dbSDimitry Andric     getStreamer().emitCVDefRangeDirective(Ranges, DRHdr);
39538bcb0991SDimitry Andric     break;
39548bcb0991SDimitry Andric   }
39558bcb0991SDimitry Andric   case CVDR_DEFRANGE_REGISTER_REL: {
39568bcb0991SDimitry Andric     int64_t DRRegister;
39578bcb0991SDimitry Andric     int64_t DRFlags;
39588bcb0991SDimitry Andric     int64_t DRBasePointerOffset;
39598bcb0991SDimitry Andric     if (parseToken(AsmToken::Comma, "expected comma before register number in "
39608bcb0991SDimitry Andric                                     ".cv_def_range directive") ||
39618bcb0991SDimitry Andric         parseAbsoluteExpression(DRRegister))
39628bcb0991SDimitry Andric       return Error(Loc, "expected register value");
39638bcb0991SDimitry Andric     if (parseToken(
39648bcb0991SDimitry Andric             AsmToken::Comma,
39658bcb0991SDimitry Andric             "expected comma before flag value in .cv_def_range directive") ||
39668bcb0991SDimitry Andric         parseAbsoluteExpression(DRFlags))
39678bcb0991SDimitry Andric       return Error(Loc, "expected flag value");
39688bcb0991SDimitry Andric     if (parseToken(AsmToken::Comma, "expected comma before base pointer offset "
39698bcb0991SDimitry Andric                                     "in .cv_def_range directive") ||
39708bcb0991SDimitry Andric         parseAbsoluteExpression(DRBasePointerOffset))
39718bcb0991SDimitry Andric       return Error(Loc, "expected base pointer offset value");
39728bcb0991SDimitry Andric 
39738bcb0991SDimitry Andric     codeview::DefRangeRegisterRelHeader DRHdr;
39748bcb0991SDimitry Andric     DRHdr.Register = DRRegister;
39758bcb0991SDimitry Andric     DRHdr.Flags = DRFlags;
39768bcb0991SDimitry Andric     DRHdr.BasePointerOffset = DRBasePointerOffset;
3977*5ffd83dbSDimitry Andric     getStreamer().emitCVDefRangeDirective(Ranges, DRHdr);
39788bcb0991SDimitry Andric     break;
39798bcb0991SDimitry Andric   }
39808bcb0991SDimitry Andric   default:
39818bcb0991SDimitry Andric     return Error(Loc, "unexpected def_range type in .cv_def_range directive");
39828bcb0991SDimitry Andric   }
39838bcb0991SDimitry Andric   return true;
39840b57cec5SDimitry Andric }
39850b57cec5SDimitry Andric 
39860b57cec5SDimitry Andric /// parseDirectiveCVString
39870b57cec5SDimitry Andric /// ::= .cv_stringtable "string"
39880b57cec5SDimitry Andric bool AsmParser::parseDirectiveCVString() {
39890b57cec5SDimitry Andric   std::string Data;
39900b57cec5SDimitry Andric   if (checkForValidSection() || parseEscapedString(Data))
39910b57cec5SDimitry Andric     return addErrorSuffix(" in '.cv_string' directive");
39920b57cec5SDimitry Andric 
39930b57cec5SDimitry Andric   // Put the string in the table and emit the offset.
39940b57cec5SDimitry Andric   std::pair<StringRef, unsigned> Insertion =
39950b57cec5SDimitry Andric       getCVContext().addToStringTable(Data);
3996*5ffd83dbSDimitry Andric   getStreamer().emitInt32(Insertion.second);
39970b57cec5SDimitry Andric   return false;
39980b57cec5SDimitry Andric }
39990b57cec5SDimitry Andric 
40000b57cec5SDimitry Andric /// parseDirectiveCVStringTable
40010b57cec5SDimitry Andric /// ::= .cv_stringtable
40020b57cec5SDimitry Andric bool AsmParser::parseDirectiveCVStringTable() {
4003*5ffd83dbSDimitry Andric   getStreamer().emitCVStringTableDirective();
40040b57cec5SDimitry Andric   return false;
40050b57cec5SDimitry Andric }
40060b57cec5SDimitry Andric 
40070b57cec5SDimitry Andric /// parseDirectiveCVFileChecksums
40080b57cec5SDimitry Andric /// ::= .cv_filechecksums
40090b57cec5SDimitry Andric bool AsmParser::parseDirectiveCVFileChecksums() {
4010*5ffd83dbSDimitry Andric   getStreamer().emitCVFileChecksumsDirective();
40110b57cec5SDimitry Andric   return false;
40120b57cec5SDimitry Andric }
40130b57cec5SDimitry Andric 
40140b57cec5SDimitry Andric /// parseDirectiveCVFileChecksumOffset
40150b57cec5SDimitry Andric /// ::= .cv_filechecksumoffset fileno
40160b57cec5SDimitry Andric bool AsmParser::parseDirectiveCVFileChecksumOffset() {
40170b57cec5SDimitry Andric   int64_t FileNo;
40180b57cec5SDimitry Andric   if (parseIntToken(FileNo, "expected identifier in directive"))
40190b57cec5SDimitry Andric     return true;
40200b57cec5SDimitry Andric   if (parseToken(AsmToken::EndOfStatement, "Expected End of Statement"))
40210b57cec5SDimitry Andric     return true;
4022*5ffd83dbSDimitry Andric   getStreamer().emitCVFileChecksumOffsetDirective(FileNo);
40230b57cec5SDimitry Andric   return false;
40240b57cec5SDimitry Andric }
40250b57cec5SDimitry Andric 
40260b57cec5SDimitry Andric /// parseDirectiveCVFPOData
40270b57cec5SDimitry Andric /// ::= .cv_fpo_data procsym
40280b57cec5SDimitry Andric bool AsmParser::parseDirectiveCVFPOData() {
40290b57cec5SDimitry Andric   SMLoc DirLoc = getLexer().getLoc();
40300b57cec5SDimitry Andric   StringRef ProcName;
40310b57cec5SDimitry Andric   if (parseIdentifier(ProcName))
40320b57cec5SDimitry Andric     return TokError("expected symbol name");
40330b57cec5SDimitry Andric   if (parseEOL("unexpected tokens"))
40340b57cec5SDimitry Andric     return addErrorSuffix(" in '.cv_fpo_data' directive");
40350b57cec5SDimitry Andric   MCSymbol *ProcSym = getContext().getOrCreateSymbol(ProcName);
40360b57cec5SDimitry Andric   getStreamer().EmitCVFPOData(ProcSym, DirLoc);
40370b57cec5SDimitry Andric   return false;
40380b57cec5SDimitry Andric }
40390b57cec5SDimitry Andric 
40400b57cec5SDimitry Andric /// parseDirectiveCFISections
40410b57cec5SDimitry Andric /// ::= .cfi_sections section [, section]
40420b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFISections() {
40430b57cec5SDimitry Andric   StringRef Name;
40440b57cec5SDimitry Andric   bool EH = false;
40450b57cec5SDimitry Andric   bool Debug = false;
40460b57cec5SDimitry Andric 
40470b57cec5SDimitry Andric   if (parseIdentifier(Name))
40480b57cec5SDimitry Andric     return TokError("Expected an identifier");
40490b57cec5SDimitry Andric 
40500b57cec5SDimitry Andric   if (Name == ".eh_frame")
40510b57cec5SDimitry Andric     EH = true;
40520b57cec5SDimitry Andric   else if (Name == ".debug_frame")
40530b57cec5SDimitry Andric     Debug = true;
40540b57cec5SDimitry Andric 
40550b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Comma)) {
40560b57cec5SDimitry Andric     Lex();
40570b57cec5SDimitry Andric 
40580b57cec5SDimitry Andric     if (parseIdentifier(Name))
40590b57cec5SDimitry Andric       return TokError("Expected an identifier");
40600b57cec5SDimitry Andric 
40610b57cec5SDimitry Andric     if (Name == ".eh_frame")
40620b57cec5SDimitry Andric       EH = true;
40630b57cec5SDimitry Andric     else if (Name == ".debug_frame")
40640b57cec5SDimitry Andric       Debug = true;
40650b57cec5SDimitry Andric   }
40660b57cec5SDimitry Andric 
4067*5ffd83dbSDimitry Andric   getStreamer().emitCFISections(EH, Debug);
40680b57cec5SDimitry Andric   return false;
40690b57cec5SDimitry Andric }
40700b57cec5SDimitry Andric 
40710b57cec5SDimitry Andric /// parseDirectiveCFIStartProc
40720b57cec5SDimitry Andric /// ::= .cfi_startproc [simple]
40730b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIStartProc() {
40740b57cec5SDimitry Andric   StringRef Simple;
40750b57cec5SDimitry Andric   if (!parseOptionalToken(AsmToken::EndOfStatement)) {
40760b57cec5SDimitry Andric     if (check(parseIdentifier(Simple) || Simple != "simple",
40770b57cec5SDimitry Andric               "unexpected token") ||
40780b57cec5SDimitry Andric         parseToken(AsmToken::EndOfStatement))
40790b57cec5SDimitry Andric       return addErrorSuffix(" in '.cfi_startproc' directive");
40800b57cec5SDimitry Andric   }
40810b57cec5SDimitry Andric 
40820b57cec5SDimitry Andric   // TODO(kristina): Deal with a corner case of incorrect diagnostic context
40830b57cec5SDimitry Andric   // being produced if this directive is emitted as part of preprocessor macro
40840b57cec5SDimitry Andric   // expansion which can *ONLY* happen if Clang's cc1as is the API consumer.
40850b57cec5SDimitry Andric   // Tools like llvm-mc on the other hand are not affected by it, and report
40860b57cec5SDimitry Andric   // correct context information.
4087*5ffd83dbSDimitry Andric   getStreamer().emitCFIStartProc(!Simple.empty(), Lexer.getLoc());
40880b57cec5SDimitry Andric   return false;
40890b57cec5SDimitry Andric }
40900b57cec5SDimitry Andric 
40910b57cec5SDimitry Andric /// parseDirectiveCFIEndProc
40920b57cec5SDimitry Andric /// ::= .cfi_endproc
40930b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIEndProc() {
4094*5ffd83dbSDimitry Andric   getStreamer().emitCFIEndProc();
40950b57cec5SDimitry Andric   return false;
40960b57cec5SDimitry Andric }
40970b57cec5SDimitry Andric 
40980b57cec5SDimitry Andric /// parse register name or number.
40990b57cec5SDimitry Andric bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
41000b57cec5SDimitry Andric                                               SMLoc DirectiveLoc) {
41010b57cec5SDimitry Andric   unsigned RegNo;
41020b57cec5SDimitry Andric 
41030b57cec5SDimitry Andric   if (getLexer().isNot(AsmToken::Integer)) {
41040b57cec5SDimitry Andric     if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
41050b57cec5SDimitry Andric       return true;
41060b57cec5SDimitry Andric     Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
41070b57cec5SDimitry Andric   } else
41080b57cec5SDimitry Andric     return parseAbsoluteExpression(Register);
41090b57cec5SDimitry Andric 
41100b57cec5SDimitry Andric   return false;
41110b57cec5SDimitry Andric }
41120b57cec5SDimitry Andric 
41130b57cec5SDimitry Andric /// parseDirectiveCFIDefCfa
41140b57cec5SDimitry Andric /// ::= .cfi_def_cfa register,  offset
41150b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
41160b57cec5SDimitry Andric   int64_t Register = 0, Offset = 0;
41170b57cec5SDimitry Andric   if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) ||
41180b57cec5SDimitry Andric       parseToken(AsmToken::Comma, "unexpected token in directive") ||
41190b57cec5SDimitry Andric       parseAbsoluteExpression(Offset))
41200b57cec5SDimitry Andric     return true;
41210b57cec5SDimitry Andric 
4122*5ffd83dbSDimitry Andric   getStreamer().emitCFIDefCfa(Register, Offset);
41230b57cec5SDimitry Andric   return false;
41240b57cec5SDimitry Andric }
41250b57cec5SDimitry Andric 
41260b57cec5SDimitry Andric /// parseDirectiveCFIDefCfaOffset
41270b57cec5SDimitry Andric /// ::= .cfi_def_cfa_offset offset
41280b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIDefCfaOffset() {
41290b57cec5SDimitry Andric   int64_t Offset = 0;
41300b57cec5SDimitry Andric   if (parseAbsoluteExpression(Offset))
41310b57cec5SDimitry Andric     return true;
41320b57cec5SDimitry Andric 
4133*5ffd83dbSDimitry Andric   getStreamer().emitCFIDefCfaOffset(Offset);
41340b57cec5SDimitry Andric   return false;
41350b57cec5SDimitry Andric }
41360b57cec5SDimitry Andric 
41370b57cec5SDimitry Andric /// parseDirectiveCFIRegister
41380b57cec5SDimitry Andric /// ::= .cfi_register register, register
41390b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
41400b57cec5SDimitry Andric   int64_t Register1 = 0, Register2 = 0;
41410b57cec5SDimitry Andric   if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc) ||
41420b57cec5SDimitry Andric       parseToken(AsmToken::Comma, "unexpected token in directive") ||
41430b57cec5SDimitry Andric       parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
41440b57cec5SDimitry Andric     return true;
41450b57cec5SDimitry Andric 
4146*5ffd83dbSDimitry Andric   getStreamer().emitCFIRegister(Register1, Register2);
41470b57cec5SDimitry Andric   return false;
41480b57cec5SDimitry Andric }
41490b57cec5SDimitry Andric 
41500b57cec5SDimitry Andric /// parseDirectiveCFIWindowSave
41510b57cec5SDimitry Andric /// ::= .cfi_window_save
41520b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIWindowSave() {
4153*5ffd83dbSDimitry Andric   getStreamer().emitCFIWindowSave();
41540b57cec5SDimitry Andric   return false;
41550b57cec5SDimitry Andric }
41560b57cec5SDimitry Andric 
41570b57cec5SDimitry Andric /// parseDirectiveCFIAdjustCfaOffset
41580b57cec5SDimitry Andric /// ::= .cfi_adjust_cfa_offset adjustment
41590b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
41600b57cec5SDimitry Andric   int64_t Adjustment = 0;
41610b57cec5SDimitry Andric   if (parseAbsoluteExpression(Adjustment))
41620b57cec5SDimitry Andric     return true;
41630b57cec5SDimitry Andric 
4164*5ffd83dbSDimitry Andric   getStreamer().emitCFIAdjustCfaOffset(Adjustment);
41650b57cec5SDimitry Andric   return false;
41660b57cec5SDimitry Andric }
41670b57cec5SDimitry Andric 
41680b57cec5SDimitry Andric /// parseDirectiveCFIDefCfaRegister
41690b57cec5SDimitry Andric /// ::= .cfi_def_cfa_register register
41700b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
41710b57cec5SDimitry Andric   int64_t Register = 0;
41720b57cec5SDimitry Andric   if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
41730b57cec5SDimitry Andric     return true;
41740b57cec5SDimitry Andric 
4175*5ffd83dbSDimitry Andric   getStreamer().emitCFIDefCfaRegister(Register);
41760b57cec5SDimitry Andric   return false;
41770b57cec5SDimitry Andric }
41780b57cec5SDimitry Andric 
41790b57cec5SDimitry Andric /// parseDirectiveCFIOffset
41800b57cec5SDimitry Andric /// ::= .cfi_offset register, offset
41810b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
41820b57cec5SDimitry Andric   int64_t Register = 0;
41830b57cec5SDimitry Andric   int64_t Offset = 0;
41840b57cec5SDimitry Andric 
41850b57cec5SDimitry Andric   if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) ||
41860b57cec5SDimitry Andric       parseToken(AsmToken::Comma, "unexpected token in directive") ||
41870b57cec5SDimitry Andric       parseAbsoluteExpression(Offset))
41880b57cec5SDimitry Andric     return true;
41890b57cec5SDimitry Andric 
4190*5ffd83dbSDimitry Andric   getStreamer().emitCFIOffset(Register, Offset);
41910b57cec5SDimitry Andric   return false;
41920b57cec5SDimitry Andric }
41930b57cec5SDimitry Andric 
41940b57cec5SDimitry Andric /// parseDirectiveCFIRelOffset
41950b57cec5SDimitry Andric /// ::= .cfi_rel_offset register, offset
41960b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
41970b57cec5SDimitry Andric   int64_t Register = 0, Offset = 0;
41980b57cec5SDimitry Andric 
41990b57cec5SDimitry Andric   if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) ||
42000b57cec5SDimitry Andric       parseToken(AsmToken::Comma, "unexpected token in directive") ||
42010b57cec5SDimitry Andric       parseAbsoluteExpression(Offset))
42020b57cec5SDimitry Andric     return true;
42030b57cec5SDimitry Andric 
4204*5ffd83dbSDimitry Andric   getStreamer().emitCFIRelOffset(Register, Offset);
42050b57cec5SDimitry Andric   return false;
42060b57cec5SDimitry Andric }
42070b57cec5SDimitry Andric 
42080b57cec5SDimitry Andric static bool isValidEncoding(int64_t Encoding) {
42090b57cec5SDimitry Andric   if (Encoding & ~0xff)
42100b57cec5SDimitry Andric     return false;
42110b57cec5SDimitry Andric 
42120b57cec5SDimitry Andric   if (Encoding == dwarf::DW_EH_PE_omit)
42130b57cec5SDimitry Andric     return true;
42140b57cec5SDimitry Andric 
42150b57cec5SDimitry Andric   const unsigned Format = Encoding & 0xf;
42160b57cec5SDimitry Andric   if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
42170b57cec5SDimitry Andric       Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
42180b57cec5SDimitry Andric       Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
42190b57cec5SDimitry Andric       Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
42200b57cec5SDimitry Andric     return false;
42210b57cec5SDimitry Andric 
42220b57cec5SDimitry Andric   const unsigned Application = Encoding & 0x70;
42230b57cec5SDimitry Andric   if (Application != dwarf::DW_EH_PE_absptr &&
42240b57cec5SDimitry Andric       Application != dwarf::DW_EH_PE_pcrel)
42250b57cec5SDimitry Andric     return false;
42260b57cec5SDimitry Andric 
42270b57cec5SDimitry Andric   return true;
42280b57cec5SDimitry Andric }
42290b57cec5SDimitry Andric 
42300b57cec5SDimitry Andric /// parseDirectiveCFIPersonalityOrLsda
42310b57cec5SDimitry Andric /// IsPersonality true for cfi_personality, false for cfi_lsda
42320b57cec5SDimitry Andric /// ::= .cfi_personality encoding, [symbol_name]
42330b57cec5SDimitry Andric /// ::= .cfi_lsda encoding, [symbol_name]
42340b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
42350b57cec5SDimitry Andric   int64_t Encoding = 0;
42360b57cec5SDimitry Andric   if (parseAbsoluteExpression(Encoding))
42370b57cec5SDimitry Andric     return true;
42380b57cec5SDimitry Andric   if (Encoding == dwarf::DW_EH_PE_omit)
42390b57cec5SDimitry Andric     return false;
42400b57cec5SDimitry Andric 
42410b57cec5SDimitry Andric   StringRef Name;
42420b57cec5SDimitry Andric   if (check(!isValidEncoding(Encoding), "unsupported encoding.") ||
42430b57cec5SDimitry Andric       parseToken(AsmToken::Comma, "unexpected token in directive") ||
42440b57cec5SDimitry Andric       check(parseIdentifier(Name), "expected identifier in directive"))
42450b57cec5SDimitry Andric     return true;
42460b57cec5SDimitry Andric 
42470b57cec5SDimitry Andric   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
42480b57cec5SDimitry Andric 
42490b57cec5SDimitry Andric   if (IsPersonality)
4250*5ffd83dbSDimitry Andric     getStreamer().emitCFIPersonality(Sym, Encoding);
42510b57cec5SDimitry Andric   else
4252*5ffd83dbSDimitry Andric     getStreamer().emitCFILsda(Sym, Encoding);
42530b57cec5SDimitry Andric   return false;
42540b57cec5SDimitry Andric }
42550b57cec5SDimitry Andric 
42560b57cec5SDimitry Andric /// parseDirectiveCFIRememberState
42570b57cec5SDimitry Andric /// ::= .cfi_remember_state
42580b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIRememberState() {
4259*5ffd83dbSDimitry Andric   getStreamer().emitCFIRememberState();
42600b57cec5SDimitry Andric   return false;
42610b57cec5SDimitry Andric }
42620b57cec5SDimitry Andric 
42630b57cec5SDimitry Andric /// parseDirectiveCFIRestoreState
42640b57cec5SDimitry Andric /// ::= .cfi_remember_state
42650b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIRestoreState() {
4266*5ffd83dbSDimitry Andric   getStreamer().emitCFIRestoreState();
42670b57cec5SDimitry Andric   return false;
42680b57cec5SDimitry Andric }
42690b57cec5SDimitry Andric 
42700b57cec5SDimitry Andric /// parseDirectiveCFISameValue
42710b57cec5SDimitry Andric /// ::= .cfi_same_value register
42720b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
42730b57cec5SDimitry Andric   int64_t Register = 0;
42740b57cec5SDimitry Andric 
42750b57cec5SDimitry Andric   if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
42760b57cec5SDimitry Andric     return true;
42770b57cec5SDimitry Andric 
4278*5ffd83dbSDimitry Andric   getStreamer().emitCFISameValue(Register);
42790b57cec5SDimitry Andric   return false;
42800b57cec5SDimitry Andric }
42810b57cec5SDimitry Andric 
42820b57cec5SDimitry Andric /// parseDirectiveCFIRestore
42830b57cec5SDimitry Andric /// ::= .cfi_restore register
42840b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
42850b57cec5SDimitry Andric   int64_t Register = 0;
42860b57cec5SDimitry Andric   if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
42870b57cec5SDimitry Andric     return true;
42880b57cec5SDimitry Andric 
4289*5ffd83dbSDimitry Andric   getStreamer().emitCFIRestore(Register);
42900b57cec5SDimitry Andric   return false;
42910b57cec5SDimitry Andric }
42920b57cec5SDimitry Andric 
42930b57cec5SDimitry Andric /// parseDirectiveCFIEscape
42940b57cec5SDimitry Andric /// ::= .cfi_escape expression[,...]
42950b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIEscape() {
42960b57cec5SDimitry Andric   std::string Values;
42970b57cec5SDimitry Andric   int64_t CurrValue;
42980b57cec5SDimitry Andric   if (parseAbsoluteExpression(CurrValue))
42990b57cec5SDimitry Andric     return true;
43000b57cec5SDimitry Andric 
43010b57cec5SDimitry Andric   Values.push_back((uint8_t)CurrValue);
43020b57cec5SDimitry Andric 
43030b57cec5SDimitry Andric   while (getLexer().is(AsmToken::Comma)) {
43040b57cec5SDimitry Andric     Lex();
43050b57cec5SDimitry Andric 
43060b57cec5SDimitry Andric     if (parseAbsoluteExpression(CurrValue))
43070b57cec5SDimitry Andric       return true;
43080b57cec5SDimitry Andric 
43090b57cec5SDimitry Andric     Values.push_back((uint8_t)CurrValue);
43100b57cec5SDimitry Andric   }
43110b57cec5SDimitry Andric 
4312*5ffd83dbSDimitry Andric   getStreamer().emitCFIEscape(Values);
43130b57cec5SDimitry Andric   return false;
43140b57cec5SDimitry Andric }
43150b57cec5SDimitry Andric 
43160b57cec5SDimitry Andric /// parseDirectiveCFIReturnColumn
43170b57cec5SDimitry Andric /// ::= .cfi_return_column register
43180b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc) {
43190b57cec5SDimitry Andric   int64_t Register = 0;
43200b57cec5SDimitry Andric   if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
43210b57cec5SDimitry Andric     return true;
4322*5ffd83dbSDimitry Andric   getStreamer().emitCFIReturnColumn(Register);
43230b57cec5SDimitry Andric   return false;
43240b57cec5SDimitry Andric }
43250b57cec5SDimitry Andric 
43260b57cec5SDimitry Andric /// parseDirectiveCFISignalFrame
43270b57cec5SDimitry Andric /// ::= .cfi_signal_frame
43280b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFISignalFrame() {
43290b57cec5SDimitry Andric   if (parseToken(AsmToken::EndOfStatement,
43300b57cec5SDimitry Andric                  "unexpected token in '.cfi_signal_frame'"))
43310b57cec5SDimitry Andric     return true;
43320b57cec5SDimitry Andric 
4333*5ffd83dbSDimitry Andric   getStreamer().emitCFISignalFrame();
43340b57cec5SDimitry Andric   return false;
43350b57cec5SDimitry Andric }
43360b57cec5SDimitry Andric 
43370b57cec5SDimitry Andric /// parseDirectiveCFIUndefined
43380b57cec5SDimitry Andric /// ::= .cfi_undefined register
43390b57cec5SDimitry Andric bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
43400b57cec5SDimitry Andric   int64_t Register = 0;
43410b57cec5SDimitry Andric 
43420b57cec5SDimitry Andric   if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
43430b57cec5SDimitry Andric     return true;
43440b57cec5SDimitry Andric 
4345*5ffd83dbSDimitry Andric   getStreamer().emitCFIUndefined(Register);
43460b57cec5SDimitry Andric   return false;
43470b57cec5SDimitry Andric }
43480b57cec5SDimitry Andric 
43490b57cec5SDimitry Andric /// parseDirectiveAltmacro
43500b57cec5SDimitry Andric /// ::= .altmacro
43510b57cec5SDimitry Andric /// ::= .noaltmacro
43520b57cec5SDimitry Andric bool AsmParser::parseDirectiveAltmacro(StringRef Directive) {
43530b57cec5SDimitry Andric   if (getLexer().isNot(AsmToken::EndOfStatement))
43540b57cec5SDimitry Andric     return TokError("unexpected token in '" + Directive + "' directive");
43550b57cec5SDimitry Andric   AltMacroMode = (Directive == ".altmacro");
43560b57cec5SDimitry Andric   return false;
43570b57cec5SDimitry Andric }
43580b57cec5SDimitry Andric 
43590b57cec5SDimitry Andric /// parseDirectiveMacrosOnOff
43600b57cec5SDimitry Andric /// ::= .macros_on
43610b57cec5SDimitry Andric /// ::= .macros_off
43620b57cec5SDimitry Andric bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
43630b57cec5SDimitry Andric   if (parseToken(AsmToken::EndOfStatement,
43640b57cec5SDimitry Andric                  "unexpected token in '" + Directive + "' directive"))
43650b57cec5SDimitry Andric     return true;
43660b57cec5SDimitry Andric 
43670b57cec5SDimitry Andric   setMacrosEnabled(Directive == ".macros_on");
43680b57cec5SDimitry Andric   return false;
43690b57cec5SDimitry Andric }
43700b57cec5SDimitry Andric 
43710b57cec5SDimitry Andric /// parseDirectiveMacro
43720b57cec5SDimitry Andric /// ::= .macro name[,] [parameters]
43730b57cec5SDimitry Andric bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
43740b57cec5SDimitry Andric   StringRef Name;
43750b57cec5SDimitry Andric   if (parseIdentifier(Name))
43760b57cec5SDimitry Andric     return TokError("expected identifier in '.macro' directive");
43770b57cec5SDimitry Andric 
43780b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Comma))
43790b57cec5SDimitry Andric     Lex();
43800b57cec5SDimitry Andric 
43810b57cec5SDimitry Andric   MCAsmMacroParameters Parameters;
43820b57cec5SDimitry Andric   while (getLexer().isNot(AsmToken::EndOfStatement)) {
43830b57cec5SDimitry Andric 
43840b57cec5SDimitry Andric     if (!Parameters.empty() && Parameters.back().Vararg)
4385*5ffd83dbSDimitry Andric       return Error(Lexer.getLoc(), "vararg parameter '" +
4386*5ffd83dbSDimitry Andric                                        Parameters.back().Name +
4387*5ffd83dbSDimitry Andric                                        "' should be the last parameter");
43880b57cec5SDimitry Andric 
43890b57cec5SDimitry Andric     MCAsmMacroParameter Parameter;
43900b57cec5SDimitry Andric     if (parseIdentifier(Parameter.Name))
43910b57cec5SDimitry Andric       return TokError("expected identifier in '.macro' directive");
43920b57cec5SDimitry Andric 
43930b57cec5SDimitry Andric     // Emit an error if two (or more) named parameters share the same name
43940b57cec5SDimitry Andric     for (const MCAsmMacroParameter& CurrParam : Parameters)
43950b57cec5SDimitry Andric       if (CurrParam.Name.equals(Parameter.Name))
43960b57cec5SDimitry Andric         return TokError("macro '" + Name + "' has multiple parameters"
43970b57cec5SDimitry Andric                         " named '" + Parameter.Name + "'");
43980b57cec5SDimitry Andric 
43990b57cec5SDimitry Andric     if (Lexer.is(AsmToken::Colon)) {
44000b57cec5SDimitry Andric       Lex();  // consume ':'
44010b57cec5SDimitry Andric 
44020b57cec5SDimitry Andric       SMLoc QualLoc;
44030b57cec5SDimitry Andric       StringRef Qualifier;
44040b57cec5SDimitry Andric 
44050b57cec5SDimitry Andric       QualLoc = Lexer.getLoc();
44060b57cec5SDimitry Andric       if (parseIdentifier(Qualifier))
44070b57cec5SDimitry Andric         return Error(QualLoc, "missing parameter qualifier for "
44080b57cec5SDimitry Andric                      "'" + Parameter.Name + "' in macro '" + Name + "'");
44090b57cec5SDimitry Andric 
44100b57cec5SDimitry Andric       if (Qualifier == "req")
44110b57cec5SDimitry Andric         Parameter.Required = true;
44120b57cec5SDimitry Andric       else if (Qualifier == "vararg")
44130b57cec5SDimitry Andric         Parameter.Vararg = true;
44140b57cec5SDimitry Andric       else
44150b57cec5SDimitry Andric         return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
44160b57cec5SDimitry Andric                      "for '" + Parameter.Name + "' in macro '" + Name + "'");
44170b57cec5SDimitry Andric     }
44180b57cec5SDimitry Andric 
44190b57cec5SDimitry Andric     if (getLexer().is(AsmToken::Equal)) {
44200b57cec5SDimitry Andric       Lex();
44210b57cec5SDimitry Andric 
44220b57cec5SDimitry Andric       SMLoc ParamLoc;
44230b57cec5SDimitry Andric 
44240b57cec5SDimitry Andric       ParamLoc = Lexer.getLoc();
44250b57cec5SDimitry Andric       if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
44260b57cec5SDimitry Andric         return true;
44270b57cec5SDimitry Andric 
44280b57cec5SDimitry Andric       if (Parameter.Required)
44290b57cec5SDimitry Andric         Warning(ParamLoc, "pointless default value for required parameter "
44300b57cec5SDimitry Andric                 "'" + Parameter.Name + "' in macro '" + Name + "'");
44310b57cec5SDimitry Andric     }
44320b57cec5SDimitry Andric 
44330b57cec5SDimitry Andric     Parameters.push_back(std::move(Parameter));
44340b57cec5SDimitry Andric 
44350b57cec5SDimitry Andric     if (getLexer().is(AsmToken::Comma))
44360b57cec5SDimitry Andric       Lex();
44370b57cec5SDimitry Andric   }
44380b57cec5SDimitry Andric 
44390b57cec5SDimitry Andric   // Eat just the end of statement.
44400b57cec5SDimitry Andric   Lexer.Lex();
44410b57cec5SDimitry Andric 
44420b57cec5SDimitry Andric   // Consuming deferred text, so use Lexer.Lex to ignore Lexing Errors
44430b57cec5SDimitry Andric   AsmToken EndToken, StartToken = getTok();
44440b57cec5SDimitry Andric   unsigned MacroDepth = 0;
44450b57cec5SDimitry Andric   // Lex the macro definition.
44460b57cec5SDimitry Andric   while (true) {
44470b57cec5SDimitry Andric     // Ignore Lexing errors in macros.
44480b57cec5SDimitry Andric     while (Lexer.is(AsmToken::Error)) {
44490b57cec5SDimitry Andric       Lexer.Lex();
44500b57cec5SDimitry Andric     }
44510b57cec5SDimitry Andric 
44520b57cec5SDimitry Andric     // Check whether we have reached the end of the file.
44530b57cec5SDimitry Andric     if (getLexer().is(AsmToken::Eof))
44540b57cec5SDimitry Andric       return Error(DirectiveLoc, "no matching '.endmacro' in definition");
44550b57cec5SDimitry Andric 
4456*5ffd83dbSDimitry Andric     // Otherwise, check whether we have reach the .endmacro or the start of a
4457*5ffd83dbSDimitry Andric     // preprocessor line marker.
44580b57cec5SDimitry Andric     if (getLexer().is(AsmToken::Identifier)) {
44590b57cec5SDimitry Andric       if (getTok().getIdentifier() == ".endm" ||
44600b57cec5SDimitry Andric           getTok().getIdentifier() == ".endmacro") {
44610b57cec5SDimitry Andric         if (MacroDepth == 0) { // Outermost macro.
44620b57cec5SDimitry Andric           EndToken = getTok();
44630b57cec5SDimitry Andric           Lexer.Lex();
44640b57cec5SDimitry Andric           if (getLexer().isNot(AsmToken::EndOfStatement))
44650b57cec5SDimitry Andric             return TokError("unexpected token in '" + EndToken.getIdentifier() +
44660b57cec5SDimitry Andric                             "' directive");
44670b57cec5SDimitry Andric           break;
44680b57cec5SDimitry Andric         } else {
44690b57cec5SDimitry Andric           // Otherwise we just found the end of an inner macro.
44700b57cec5SDimitry Andric           --MacroDepth;
44710b57cec5SDimitry Andric         }
44720b57cec5SDimitry Andric       } else if (getTok().getIdentifier() == ".macro") {
44730b57cec5SDimitry Andric         // We allow nested macros. Those aren't instantiated until the outermost
44740b57cec5SDimitry Andric         // macro is expanded so just ignore them for now.
44750b57cec5SDimitry Andric         ++MacroDepth;
44760b57cec5SDimitry Andric       }
4477*5ffd83dbSDimitry Andric     } else if (Lexer.is(AsmToken::HashDirective)) {
4478*5ffd83dbSDimitry Andric       (void)parseCppHashLineFilenameComment(getLexer().getLoc());
44790b57cec5SDimitry Andric     }
44800b57cec5SDimitry Andric 
44810b57cec5SDimitry Andric     // Otherwise, scan til the end of the statement.
44820b57cec5SDimitry Andric     eatToEndOfStatement();
44830b57cec5SDimitry Andric   }
44840b57cec5SDimitry Andric 
44850b57cec5SDimitry Andric   if (getContext().lookupMacro(Name)) {
44860b57cec5SDimitry Andric     return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
44870b57cec5SDimitry Andric   }
44880b57cec5SDimitry Andric 
44890b57cec5SDimitry Andric   const char *BodyStart = StartToken.getLoc().getPointer();
44900b57cec5SDimitry Andric   const char *BodyEnd = EndToken.getLoc().getPointer();
44910b57cec5SDimitry Andric   StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
44920b57cec5SDimitry Andric   checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
44930b57cec5SDimitry Andric   MCAsmMacro Macro(Name, Body, std::move(Parameters));
44940b57cec5SDimitry Andric   DEBUG_WITH_TYPE("asm-macros", dbgs() << "Defining new macro:\n";
44950b57cec5SDimitry Andric                   Macro.dump());
44960b57cec5SDimitry Andric   getContext().defineMacro(Name, std::move(Macro));
44970b57cec5SDimitry Andric   return false;
44980b57cec5SDimitry Andric }
44990b57cec5SDimitry Andric 
45000b57cec5SDimitry Andric /// checkForBadMacro
45010b57cec5SDimitry Andric ///
45020b57cec5SDimitry Andric /// With the support added for named parameters there may be code out there that
45030b57cec5SDimitry Andric /// is transitioning from positional parameters.  In versions of gas that did
45040b57cec5SDimitry Andric /// not support named parameters they would be ignored on the macro definition.
45050b57cec5SDimitry Andric /// But to support both styles of parameters this is not possible so if a macro
45060b57cec5SDimitry Andric /// definition has named parameters but does not use them and has what appears
45070b57cec5SDimitry Andric /// to be positional parameters, strings like $1, $2, ... and $n, then issue a
45080b57cec5SDimitry Andric /// warning that the positional parameter found in body which have no effect.
45090b57cec5SDimitry Andric /// Hoping the developer will either remove the named parameters from the macro
45100b57cec5SDimitry Andric /// definition so the positional parameters get used if that was what was
45110b57cec5SDimitry Andric /// intended or change the macro to use the named parameters.  It is possible
45120b57cec5SDimitry Andric /// this warning will trigger when the none of the named parameters are used
45130b57cec5SDimitry Andric /// and the strings like $1 are infact to simply to be passed trough unchanged.
45140b57cec5SDimitry Andric void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
45150b57cec5SDimitry Andric                                  StringRef Body,
45160b57cec5SDimitry Andric                                  ArrayRef<MCAsmMacroParameter> Parameters) {
45170b57cec5SDimitry Andric   // If this macro is not defined with named parameters the warning we are
45180b57cec5SDimitry Andric   // checking for here doesn't apply.
45190b57cec5SDimitry Andric   unsigned NParameters = Parameters.size();
45200b57cec5SDimitry Andric   if (NParameters == 0)
45210b57cec5SDimitry Andric     return;
45220b57cec5SDimitry Andric 
45230b57cec5SDimitry Andric   bool NamedParametersFound = false;
45240b57cec5SDimitry Andric   bool PositionalParametersFound = false;
45250b57cec5SDimitry Andric 
45260b57cec5SDimitry Andric   // Look at the body of the macro for use of both the named parameters and what
45270b57cec5SDimitry Andric   // are likely to be positional parameters.  This is what expandMacro() is
45280b57cec5SDimitry Andric   // doing when it finds the parameters in the body.
45290b57cec5SDimitry Andric   while (!Body.empty()) {
45300b57cec5SDimitry Andric     // Scan for the next possible parameter.
45310b57cec5SDimitry Andric     std::size_t End = Body.size(), Pos = 0;
45320b57cec5SDimitry Andric     for (; Pos != End; ++Pos) {
45330b57cec5SDimitry Andric       // Check for a substitution or escape.
45340b57cec5SDimitry Andric       // This macro is defined with parameters, look for \foo, \bar, etc.
45350b57cec5SDimitry Andric       if (Body[Pos] == '\\' && Pos + 1 != End)
45360b57cec5SDimitry Andric         break;
45370b57cec5SDimitry Andric 
45380b57cec5SDimitry Andric       // This macro should have parameters, but look for $0, $1, ..., $n too.
45390b57cec5SDimitry Andric       if (Body[Pos] != '$' || Pos + 1 == End)
45400b57cec5SDimitry Andric         continue;
45410b57cec5SDimitry Andric       char Next = Body[Pos + 1];
45420b57cec5SDimitry Andric       if (Next == '$' || Next == 'n' ||
45430b57cec5SDimitry Andric           isdigit(static_cast<unsigned char>(Next)))
45440b57cec5SDimitry Andric         break;
45450b57cec5SDimitry Andric     }
45460b57cec5SDimitry Andric 
45470b57cec5SDimitry Andric     // Check if we reached the end.
45480b57cec5SDimitry Andric     if (Pos == End)
45490b57cec5SDimitry Andric       break;
45500b57cec5SDimitry Andric 
45510b57cec5SDimitry Andric     if (Body[Pos] == '$') {
45520b57cec5SDimitry Andric       switch (Body[Pos + 1]) {
45530b57cec5SDimitry Andric       // $$ => $
45540b57cec5SDimitry Andric       case '$':
45550b57cec5SDimitry Andric         break;
45560b57cec5SDimitry Andric 
45570b57cec5SDimitry Andric       // $n => number of arguments
45580b57cec5SDimitry Andric       case 'n':
45590b57cec5SDimitry Andric         PositionalParametersFound = true;
45600b57cec5SDimitry Andric         break;
45610b57cec5SDimitry Andric 
45620b57cec5SDimitry Andric       // $[0-9] => argument
45630b57cec5SDimitry Andric       default: {
45640b57cec5SDimitry Andric         PositionalParametersFound = true;
45650b57cec5SDimitry Andric         break;
45660b57cec5SDimitry Andric       }
45670b57cec5SDimitry Andric       }
45680b57cec5SDimitry Andric       Pos += 2;
45690b57cec5SDimitry Andric     } else {
45700b57cec5SDimitry Andric       unsigned I = Pos + 1;
45710b57cec5SDimitry Andric       while (isIdentifierChar(Body[I]) && I + 1 != End)
45720b57cec5SDimitry Andric         ++I;
45730b57cec5SDimitry Andric 
45740b57cec5SDimitry Andric       const char *Begin = Body.data() + Pos + 1;
45750b57cec5SDimitry Andric       StringRef Argument(Begin, I - (Pos + 1));
45760b57cec5SDimitry Andric       unsigned Index = 0;
45770b57cec5SDimitry Andric       for (; Index < NParameters; ++Index)
45780b57cec5SDimitry Andric         if (Parameters[Index].Name == Argument)
45790b57cec5SDimitry Andric           break;
45800b57cec5SDimitry Andric 
45810b57cec5SDimitry Andric       if (Index == NParameters) {
45820b57cec5SDimitry Andric         if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
45830b57cec5SDimitry Andric           Pos += 3;
45840b57cec5SDimitry Andric         else {
45850b57cec5SDimitry Andric           Pos = I;
45860b57cec5SDimitry Andric         }
45870b57cec5SDimitry Andric       } else {
45880b57cec5SDimitry Andric         NamedParametersFound = true;
45890b57cec5SDimitry Andric         Pos += 1 + Argument.size();
45900b57cec5SDimitry Andric       }
45910b57cec5SDimitry Andric     }
45920b57cec5SDimitry Andric     // Update the scan point.
45930b57cec5SDimitry Andric     Body = Body.substr(Pos);
45940b57cec5SDimitry Andric   }
45950b57cec5SDimitry Andric 
45960b57cec5SDimitry Andric   if (!NamedParametersFound && PositionalParametersFound)
45970b57cec5SDimitry Andric     Warning(DirectiveLoc, "macro defined with named parameters which are not "
45980b57cec5SDimitry Andric                           "used in macro body, possible positional parameter "
45990b57cec5SDimitry Andric                           "found in body which will have no effect");
46000b57cec5SDimitry Andric }
46010b57cec5SDimitry Andric 
46020b57cec5SDimitry Andric /// parseDirectiveExitMacro
46030b57cec5SDimitry Andric /// ::= .exitm
46040b57cec5SDimitry Andric bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
46050b57cec5SDimitry Andric   if (parseToken(AsmToken::EndOfStatement,
46060b57cec5SDimitry Andric                  "unexpected token in '" + Directive + "' directive"))
46070b57cec5SDimitry Andric     return true;
46080b57cec5SDimitry Andric 
46090b57cec5SDimitry Andric   if (!isInsideMacroInstantiation())
46100b57cec5SDimitry Andric     return TokError("unexpected '" + Directive + "' in file, "
46110b57cec5SDimitry Andric                                                  "no current macro definition");
46120b57cec5SDimitry Andric 
46130b57cec5SDimitry Andric   // Exit all conditionals that are active in the current macro.
46140b57cec5SDimitry Andric   while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
46150b57cec5SDimitry Andric     TheCondState = TheCondStack.back();
46160b57cec5SDimitry Andric     TheCondStack.pop_back();
46170b57cec5SDimitry Andric   }
46180b57cec5SDimitry Andric 
46190b57cec5SDimitry Andric   handleMacroExit();
46200b57cec5SDimitry Andric   return false;
46210b57cec5SDimitry Andric }
46220b57cec5SDimitry Andric 
46230b57cec5SDimitry Andric /// parseDirectiveEndMacro
46240b57cec5SDimitry Andric /// ::= .endm
46250b57cec5SDimitry Andric /// ::= .endmacro
46260b57cec5SDimitry Andric bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
46270b57cec5SDimitry Andric   if (getLexer().isNot(AsmToken::EndOfStatement))
46280b57cec5SDimitry Andric     return TokError("unexpected token in '" + Directive + "' directive");
46290b57cec5SDimitry Andric 
46300b57cec5SDimitry Andric   // If we are inside a macro instantiation, terminate the current
46310b57cec5SDimitry Andric   // instantiation.
46320b57cec5SDimitry Andric   if (isInsideMacroInstantiation()) {
46330b57cec5SDimitry Andric     handleMacroExit();
46340b57cec5SDimitry Andric     return false;
46350b57cec5SDimitry Andric   }
46360b57cec5SDimitry Andric 
46370b57cec5SDimitry Andric   // Otherwise, this .endmacro is a stray entry in the file; well formed
46380b57cec5SDimitry Andric   // .endmacro directives are handled during the macro definition parsing.
46390b57cec5SDimitry Andric   return TokError("unexpected '" + Directive + "' in file, "
46400b57cec5SDimitry Andric                                                "no current macro definition");
46410b57cec5SDimitry Andric }
46420b57cec5SDimitry Andric 
46430b57cec5SDimitry Andric /// parseDirectivePurgeMacro
46440b57cec5SDimitry Andric /// ::= .purgem
46450b57cec5SDimitry Andric bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
46460b57cec5SDimitry Andric   StringRef Name;
46470b57cec5SDimitry Andric   SMLoc Loc;
46480b57cec5SDimitry Andric   if (parseTokenLoc(Loc) ||
46490b57cec5SDimitry Andric       check(parseIdentifier(Name), Loc,
46500b57cec5SDimitry Andric             "expected identifier in '.purgem' directive") ||
46510b57cec5SDimitry Andric       parseToken(AsmToken::EndOfStatement,
46520b57cec5SDimitry Andric                  "unexpected token in '.purgem' directive"))
46530b57cec5SDimitry Andric     return true;
46540b57cec5SDimitry Andric 
46550b57cec5SDimitry Andric   if (!getContext().lookupMacro(Name))
46560b57cec5SDimitry Andric     return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
46570b57cec5SDimitry Andric 
46580b57cec5SDimitry Andric   getContext().undefineMacro(Name);
46590b57cec5SDimitry Andric   DEBUG_WITH_TYPE("asm-macros", dbgs()
46600b57cec5SDimitry Andric                                     << "Un-defining macro: " << Name << "\n");
46610b57cec5SDimitry Andric   return false;
46620b57cec5SDimitry Andric }
46630b57cec5SDimitry Andric 
46640b57cec5SDimitry Andric /// parseDirectiveBundleAlignMode
46650b57cec5SDimitry Andric /// ::= {.bundle_align_mode} expression
46660b57cec5SDimitry Andric bool AsmParser::parseDirectiveBundleAlignMode() {
46670b57cec5SDimitry Andric   // Expect a single argument: an expression that evaluates to a constant
46680b57cec5SDimitry Andric   // in the inclusive range 0-30.
46690b57cec5SDimitry Andric   SMLoc ExprLoc = getLexer().getLoc();
46700b57cec5SDimitry Andric   int64_t AlignSizePow2;
46710b57cec5SDimitry Andric   if (checkForValidSection() || parseAbsoluteExpression(AlignSizePow2) ||
46720b57cec5SDimitry Andric       parseToken(AsmToken::EndOfStatement, "unexpected token after expression "
46730b57cec5SDimitry Andric                                            "in '.bundle_align_mode' "
46740b57cec5SDimitry Andric                                            "directive") ||
46750b57cec5SDimitry Andric       check(AlignSizePow2 < 0 || AlignSizePow2 > 30, ExprLoc,
46760b57cec5SDimitry Andric             "invalid bundle alignment size (expected between 0 and 30)"))
46770b57cec5SDimitry Andric     return true;
46780b57cec5SDimitry Andric 
46790b57cec5SDimitry Andric   // Because of AlignSizePow2's verified range we can safely truncate it to
46800b57cec5SDimitry Andric   // unsigned.
4681*5ffd83dbSDimitry Andric   getStreamer().emitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
46820b57cec5SDimitry Andric   return false;
46830b57cec5SDimitry Andric }
46840b57cec5SDimitry Andric 
46850b57cec5SDimitry Andric /// parseDirectiveBundleLock
46860b57cec5SDimitry Andric /// ::= {.bundle_lock} [align_to_end]
46870b57cec5SDimitry Andric bool AsmParser::parseDirectiveBundleLock() {
46880b57cec5SDimitry Andric   if (checkForValidSection())
46890b57cec5SDimitry Andric     return true;
46900b57cec5SDimitry Andric   bool AlignToEnd = false;
46910b57cec5SDimitry Andric 
46920b57cec5SDimitry Andric   StringRef Option;
46930b57cec5SDimitry Andric   SMLoc Loc = getTok().getLoc();
46940b57cec5SDimitry Andric   const char *kInvalidOptionError =
46950b57cec5SDimitry Andric       "invalid option for '.bundle_lock' directive";
46960b57cec5SDimitry Andric 
46970b57cec5SDimitry Andric   if (!parseOptionalToken(AsmToken::EndOfStatement)) {
46980b57cec5SDimitry Andric     if (check(parseIdentifier(Option), Loc, kInvalidOptionError) ||
46990b57cec5SDimitry Andric         check(Option != "align_to_end", Loc, kInvalidOptionError) ||
47000b57cec5SDimitry Andric         parseToken(AsmToken::EndOfStatement,
47010b57cec5SDimitry Andric                    "unexpected token after '.bundle_lock' directive option"))
47020b57cec5SDimitry Andric       return true;
47030b57cec5SDimitry Andric     AlignToEnd = true;
47040b57cec5SDimitry Andric   }
47050b57cec5SDimitry Andric 
4706*5ffd83dbSDimitry Andric   getStreamer().emitBundleLock(AlignToEnd);
47070b57cec5SDimitry Andric   return false;
47080b57cec5SDimitry Andric }
47090b57cec5SDimitry Andric 
47100b57cec5SDimitry Andric /// parseDirectiveBundleLock
47110b57cec5SDimitry Andric /// ::= {.bundle_lock}
47120b57cec5SDimitry Andric bool AsmParser::parseDirectiveBundleUnlock() {
47130b57cec5SDimitry Andric   if (checkForValidSection() ||
47140b57cec5SDimitry Andric       parseToken(AsmToken::EndOfStatement,
47150b57cec5SDimitry Andric                  "unexpected token in '.bundle_unlock' directive"))
47160b57cec5SDimitry Andric     return true;
47170b57cec5SDimitry Andric 
4718*5ffd83dbSDimitry Andric   getStreamer().emitBundleUnlock();
47190b57cec5SDimitry Andric   return false;
47200b57cec5SDimitry Andric }
47210b57cec5SDimitry Andric 
47220b57cec5SDimitry Andric /// parseDirectiveSpace
47230b57cec5SDimitry Andric /// ::= (.skip | .space) expression [ , expression ]
47240b57cec5SDimitry Andric bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
47250b57cec5SDimitry Andric   SMLoc NumBytesLoc = Lexer.getLoc();
47260b57cec5SDimitry Andric   const MCExpr *NumBytes;
47270b57cec5SDimitry Andric   if (checkForValidSection() || parseExpression(NumBytes))
47280b57cec5SDimitry Andric     return true;
47290b57cec5SDimitry Andric 
47300b57cec5SDimitry Andric   int64_t FillExpr = 0;
47310b57cec5SDimitry Andric   if (parseOptionalToken(AsmToken::Comma))
47320b57cec5SDimitry Andric     if (parseAbsoluteExpression(FillExpr))
47330b57cec5SDimitry Andric       return addErrorSuffix("in '" + Twine(IDVal) + "' directive");
47340b57cec5SDimitry Andric   if (parseToken(AsmToken::EndOfStatement))
47350b57cec5SDimitry Andric     return addErrorSuffix("in '" + Twine(IDVal) + "' directive");
47360b57cec5SDimitry Andric 
47370b57cec5SDimitry Andric   // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
47380b57cec5SDimitry Andric   getStreamer().emitFill(*NumBytes, FillExpr, NumBytesLoc);
47390b57cec5SDimitry Andric 
47400b57cec5SDimitry Andric   return false;
47410b57cec5SDimitry Andric }
47420b57cec5SDimitry Andric 
47430b57cec5SDimitry Andric /// parseDirectiveDCB
47440b57cec5SDimitry Andric /// ::= .dcb.{b, l, w} expression, expression
47450b57cec5SDimitry Andric bool AsmParser::parseDirectiveDCB(StringRef IDVal, unsigned Size) {
47460b57cec5SDimitry Andric   SMLoc NumValuesLoc = Lexer.getLoc();
47470b57cec5SDimitry Andric   int64_t NumValues;
47480b57cec5SDimitry Andric   if (checkForValidSection() || parseAbsoluteExpression(NumValues))
47490b57cec5SDimitry Andric     return true;
47500b57cec5SDimitry Andric 
47510b57cec5SDimitry Andric   if (NumValues < 0) {
47520b57cec5SDimitry Andric     Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect");
47530b57cec5SDimitry Andric     return false;
47540b57cec5SDimitry Andric   }
47550b57cec5SDimitry Andric 
47560b57cec5SDimitry Andric   if (parseToken(AsmToken::Comma,
47570b57cec5SDimitry Andric                  "unexpected token in '" + Twine(IDVal) + "' directive"))
47580b57cec5SDimitry Andric     return true;
47590b57cec5SDimitry Andric 
47600b57cec5SDimitry Andric   const MCExpr *Value;
47610b57cec5SDimitry Andric   SMLoc ExprLoc = getLexer().getLoc();
47620b57cec5SDimitry Andric   if (parseExpression(Value))
47630b57cec5SDimitry Andric     return true;
47640b57cec5SDimitry Andric 
47650b57cec5SDimitry Andric   // Special case constant expressions to match code generator.
47660b57cec5SDimitry Andric   if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
47670b57cec5SDimitry Andric     assert(Size <= 8 && "Invalid size");
47680b57cec5SDimitry Andric     uint64_t IntValue = MCE->getValue();
47690b57cec5SDimitry Andric     if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
47700b57cec5SDimitry Andric       return Error(ExprLoc, "literal value out of range for directive");
47710b57cec5SDimitry Andric     for (uint64_t i = 0, e = NumValues; i != e; ++i)
4772*5ffd83dbSDimitry Andric       getStreamer().emitIntValue(IntValue, Size);
47730b57cec5SDimitry Andric   } else {
47740b57cec5SDimitry Andric     for (uint64_t i = 0, e = NumValues; i != e; ++i)
4775*5ffd83dbSDimitry Andric       getStreamer().emitValue(Value, Size, ExprLoc);
47760b57cec5SDimitry Andric   }
47770b57cec5SDimitry Andric 
47780b57cec5SDimitry Andric   if (parseToken(AsmToken::EndOfStatement,
47790b57cec5SDimitry Andric                  "unexpected token in '" + Twine(IDVal) + "' directive"))
47800b57cec5SDimitry Andric     return true;
47810b57cec5SDimitry Andric 
47820b57cec5SDimitry Andric   return false;
47830b57cec5SDimitry Andric }
47840b57cec5SDimitry Andric 
47850b57cec5SDimitry Andric /// parseDirectiveRealDCB
47860b57cec5SDimitry Andric /// ::= .dcb.{d, s} expression, expression
47870b57cec5SDimitry Andric bool AsmParser::parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &Semantics) {
47880b57cec5SDimitry Andric   SMLoc NumValuesLoc = Lexer.getLoc();
47890b57cec5SDimitry Andric   int64_t NumValues;
47900b57cec5SDimitry Andric   if (checkForValidSection() || parseAbsoluteExpression(NumValues))
47910b57cec5SDimitry Andric     return true;
47920b57cec5SDimitry Andric 
47930b57cec5SDimitry Andric   if (NumValues < 0) {
47940b57cec5SDimitry Andric     Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect");
47950b57cec5SDimitry Andric     return false;
47960b57cec5SDimitry Andric   }
47970b57cec5SDimitry Andric 
47980b57cec5SDimitry Andric   if (parseToken(AsmToken::Comma,
47990b57cec5SDimitry Andric                  "unexpected token in '" + Twine(IDVal) + "' directive"))
48000b57cec5SDimitry Andric     return true;
48010b57cec5SDimitry Andric 
48020b57cec5SDimitry Andric   APInt AsInt;
48030b57cec5SDimitry Andric   if (parseRealValue(Semantics, AsInt))
48040b57cec5SDimitry Andric     return true;
48050b57cec5SDimitry Andric 
48060b57cec5SDimitry Andric   if (parseToken(AsmToken::EndOfStatement,
48070b57cec5SDimitry Andric                  "unexpected token in '" + Twine(IDVal) + "' directive"))
48080b57cec5SDimitry Andric     return true;
48090b57cec5SDimitry Andric 
48100b57cec5SDimitry Andric   for (uint64_t i = 0, e = NumValues; i != e; ++i)
4811*5ffd83dbSDimitry Andric     getStreamer().emitIntValue(AsInt.getLimitedValue(),
48120b57cec5SDimitry Andric                                AsInt.getBitWidth() / 8);
48130b57cec5SDimitry Andric 
48140b57cec5SDimitry Andric   return false;
48150b57cec5SDimitry Andric }
48160b57cec5SDimitry Andric 
48170b57cec5SDimitry Andric /// parseDirectiveDS
48180b57cec5SDimitry Andric /// ::= .ds.{b, d, l, p, s, w, x} expression
48190b57cec5SDimitry Andric bool AsmParser::parseDirectiveDS(StringRef IDVal, unsigned Size) {
48200b57cec5SDimitry Andric   SMLoc NumValuesLoc = Lexer.getLoc();
48210b57cec5SDimitry Andric   int64_t NumValues;
48220b57cec5SDimitry Andric   if (checkForValidSection() || parseAbsoluteExpression(NumValues))
48230b57cec5SDimitry Andric     return true;
48240b57cec5SDimitry Andric 
48250b57cec5SDimitry Andric   if (NumValues < 0) {
48260b57cec5SDimitry Andric     Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect");
48270b57cec5SDimitry Andric     return false;
48280b57cec5SDimitry Andric   }
48290b57cec5SDimitry Andric 
48300b57cec5SDimitry Andric   if (parseToken(AsmToken::EndOfStatement,
48310b57cec5SDimitry Andric                  "unexpected token in '" + Twine(IDVal) + "' directive"))
48320b57cec5SDimitry Andric     return true;
48330b57cec5SDimitry Andric 
48340b57cec5SDimitry Andric   for (uint64_t i = 0, e = NumValues; i != e; ++i)
48350b57cec5SDimitry Andric     getStreamer().emitFill(Size, 0);
48360b57cec5SDimitry Andric 
48370b57cec5SDimitry Andric   return false;
48380b57cec5SDimitry Andric }
48390b57cec5SDimitry Andric 
48400b57cec5SDimitry Andric /// parseDirectiveLEB128
48410b57cec5SDimitry Andric /// ::= (.sleb128 | .uleb128) [ expression (, expression)* ]
48420b57cec5SDimitry Andric bool AsmParser::parseDirectiveLEB128(bool Signed) {
48430b57cec5SDimitry Andric   if (checkForValidSection())
48440b57cec5SDimitry Andric     return true;
48450b57cec5SDimitry Andric 
48460b57cec5SDimitry Andric   auto parseOp = [&]() -> bool {
48470b57cec5SDimitry Andric     const MCExpr *Value;
48480b57cec5SDimitry Andric     if (parseExpression(Value))
48490b57cec5SDimitry Andric       return true;
48500b57cec5SDimitry Andric     if (Signed)
4851*5ffd83dbSDimitry Andric       getStreamer().emitSLEB128Value(Value);
48520b57cec5SDimitry Andric     else
4853*5ffd83dbSDimitry Andric       getStreamer().emitULEB128Value(Value);
48540b57cec5SDimitry Andric     return false;
48550b57cec5SDimitry Andric   };
48560b57cec5SDimitry Andric 
48570b57cec5SDimitry Andric   if (parseMany(parseOp))
48580b57cec5SDimitry Andric     return addErrorSuffix(" in directive");
48590b57cec5SDimitry Andric 
48600b57cec5SDimitry Andric   return false;
48610b57cec5SDimitry Andric }
48620b57cec5SDimitry Andric 
48630b57cec5SDimitry Andric /// parseDirectiveSymbolAttribute
48640b57cec5SDimitry Andric ///  ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
48650b57cec5SDimitry Andric bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
48660b57cec5SDimitry Andric   auto parseOp = [&]() -> bool {
48670b57cec5SDimitry Andric     StringRef Name;
48680b57cec5SDimitry Andric     SMLoc Loc = getTok().getLoc();
48690b57cec5SDimitry Andric     if (parseIdentifier(Name))
48700b57cec5SDimitry Andric       return Error(Loc, "expected identifier");
48710b57cec5SDimitry Andric     MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
48720b57cec5SDimitry Andric 
48730b57cec5SDimitry Andric     // Assembler local symbols don't make any sense here. Complain loudly.
48740b57cec5SDimitry Andric     if (Sym->isTemporary())
48750b57cec5SDimitry Andric       return Error(Loc, "non-local symbol required");
48760b57cec5SDimitry Andric 
4877*5ffd83dbSDimitry Andric     if (!getStreamer().emitSymbolAttribute(Sym, Attr))
48780b57cec5SDimitry Andric       return Error(Loc, "unable to emit symbol attribute");
48790b57cec5SDimitry Andric     return false;
48800b57cec5SDimitry Andric   };
48810b57cec5SDimitry Andric 
48820b57cec5SDimitry Andric   if (parseMany(parseOp))
48830b57cec5SDimitry Andric     return addErrorSuffix(" in directive");
48840b57cec5SDimitry Andric   return false;
48850b57cec5SDimitry Andric }
48860b57cec5SDimitry Andric 
48870b57cec5SDimitry Andric /// parseDirectiveComm
48880b57cec5SDimitry Andric ///  ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
48890b57cec5SDimitry Andric bool AsmParser::parseDirectiveComm(bool IsLocal) {
48900b57cec5SDimitry Andric   if (checkForValidSection())
48910b57cec5SDimitry Andric     return true;
48920b57cec5SDimitry Andric 
48930b57cec5SDimitry Andric   SMLoc IDLoc = getLexer().getLoc();
48940b57cec5SDimitry Andric   StringRef Name;
48950b57cec5SDimitry Andric   if (parseIdentifier(Name))
48960b57cec5SDimitry Andric     return TokError("expected identifier in directive");
48970b57cec5SDimitry Andric 
48980b57cec5SDimitry Andric   // Handle the identifier as the key symbol.
48990b57cec5SDimitry Andric   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
49000b57cec5SDimitry Andric 
49010b57cec5SDimitry Andric   if (getLexer().isNot(AsmToken::Comma))
49020b57cec5SDimitry Andric     return TokError("unexpected token in directive");
49030b57cec5SDimitry Andric   Lex();
49040b57cec5SDimitry Andric 
49050b57cec5SDimitry Andric   int64_t Size;
49060b57cec5SDimitry Andric   SMLoc SizeLoc = getLexer().getLoc();
49070b57cec5SDimitry Andric   if (parseAbsoluteExpression(Size))
49080b57cec5SDimitry Andric     return true;
49090b57cec5SDimitry Andric 
49100b57cec5SDimitry Andric   int64_t Pow2Alignment = 0;
49110b57cec5SDimitry Andric   SMLoc Pow2AlignmentLoc;
49120b57cec5SDimitry Andric   if (getLexer().is(AsmToken::Comma)) {
49130b57cec5SDimitry Andric     Lex();
49140b57cec5SDimitry Andric     Pow2AlignmentLoc = getLexer().getLoc();
49150b57cec5SDimitry Andric     if (parseAbsoluteExpression(Pow2Alignment))
49160b57cec5SDimitry Andric       return true;
49170b57cec5SDimitry Andric 
49180b57cec5SDimitry Andric     LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
49190b57cec5SDimitry Andric     if (IsLocal && LCOMM == LCOMM::NoAlignment)
49200b57cec5SDimitry Andric       return Error(Pow2AlignmentLoc, "alignment not supported on this target");
49210b57cec5SDimitry Andric 
49220b57cec5SDimitry Andric     // If this target takes alignments in bytes (not log) validate and convert.
49230b57cec5SDimitry Andric     if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
49240b57cec5SDimitry Andric         (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
49250b57cec5SDimitry Andric       if (!isPowerOf2_64(Pow2Alignment))
49260b57cec5SDimitry Andric         return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
49270b57cec5SDimitry Andric       Pow2Alignment = Log2_64(Pow2Alignment);
49280b57cec5SDimitry Andric     }
49290b57cec5SDimitry Andric   }
49300b57cec5SDimitry Andric 
49310b57cec5SDimitry Andric   if (parseToken(AsmToken::EndOfStatement,
49320b57cec5SDimitry Andric                  "unexpected token in '.comm' or '.lcomm' directive"))
49330b57cec5SDimitry Andric     return true;
49340b57cec5SDimitry Andric 
49350b57cec5SDimitry Andric   // NOTE: a size of zero for a .comm should create a undefined symbol
49360b57cec5SDimitry Andric   // but a size of .lcomm creates a bss symbol of size zero.
49370b57cec5SDimitry Andric   if (Size < 0)
49380b57cec5SDimitry Andric     return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
49390b57cec5SDimitry Andric                           "be less than zero");
49400b57cec5SDimitry Andric 
49410b57cec5SDimitry Andric   // NOTE: The alignment in the directive is a power of 2 value, the assembler
49420b57cec5SDimitry Andric   // may internally end up wanting an alignment in bytes.
49430b57cec5SDimitry Andric   // FIXME: Diagnose overflow.
49440b57cec5SDimitry Andric   if (Pow2Alignment < 0)
49450b57cec5SDimitry Andric     return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
49460b57cec5SDimitry Andric                                    "alignment, can't be less than zero");
49470b57cec5SDimitry Andric 
49480b57cec5SDimitry Andric   Sym->redefineIfPossible();
49490b57cec5SDimitry Andric   if (!Sym->isUndefined())
49500b57cec5SDimitry Andric     return Error(IDLoc, "invalid symbol redefinition");
49510b57cec5SDimitry Andric 
49520b57cec5SDimitry Andric   // Create the Symbol as a common or local common with Size and Pow2Alignment
49530b57cec5SDimitry Andric   if (IsLocal) {
4954*5ffd83dbSDimitry Andric     getStreamer().emitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
49550b57cec5SDimitry Andric     return false;
49560b57cec5SDimitry Andric   }
49570b57cec5SDimitry Andric 
4958*5ffd83dbSDimitry Andric   getStreamer().emitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
49590b57cec5SDimitry Andric   return false;
49600b57cec5SDimitry Andric }
49610b57cec5SDimitry Andric 
49620b57cec5SDimitry Andric /// parseDirectiveAbort
49630b57cec5SDimitry Andric ///  ::= .abort [... message ...]
49640b57cec5SDimitry Andric bool AsmParser::parseDirectiveAbort() {
49650b57cec5SDimitry Andric   // FIXME: Use loc from directive.
49660b57cec5SDimitry Andric   SMLoc Loc = getLexer().getLoc();
49670b57cec5SDimitry Andric 
49680b57cec5SDimitry Andric   StringRef Str = parseStringToEndOfStatement();
49690b57cec5SDimitry Andric   if (parseToken(AsmToken::EndOfStatement,
49700b57cec5SDimitry Andric                  "unexpected token in '.abort' directive"))
49710b57cec5SDimitry Andric     return true;
49720b57cec5SDimitry Andric 
49730b57cec5SDimitry Andric   if (Str.empty())
49740b57cec5SDimitry Andric     return Error(Loc, ".abort detected. Assembly stopping.");
49750b57cec5SDimitry Andric   else
49760b57cec5SDimitry Andric     return Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
49770b57cec5SDimitry Andric   // FIXME: Actually abort assembly here.
49780b57cec5SDimitry Andric 
49790b57cec5SDimitry Andric   return false;
49800b57cec5SDimitry Andric }
49810b57cec5SDimitry Andric 
49820b57cec5SDimitry Andric /// parseDirectiveInclude
49830b57cec5SDimitry Andric ///  ::= .include "filename"
49840b57cec5SDimitry Andric bool AsmParser::parseDirectiveInclude() {
49850b57cec5SDimitry Andric   // Allow the strings to have escaped octal character sequence.
49860b57cec5SDimitry Andric   std::string Filename;
49870b57cec5SDimitry Andric   SMLoc IncludeLoc = getTok().getLoc();
49880b57cec5SDimitry Andric 
49890b57cec5SDimitry Andric   if (check(getTok().isNot(AsmToken::String),
49900b57cec5SDimitry Andric             "expected string in '.include' directive") ||
49910b57cec5SDimitry Andric       parseEscapedString(Filename) ||
49920b57cec5SDimitry Andric       check(getTok().isNot(AsmToken::EndOfStatement),
49930b57cec5SDimitry Andric             "unexpected token in '.include' directive") ||
49940b57cec5SDimitry Andric       // Attempt to switch the lexer to the included file before consuming the
49950b57cec5SDimitry Andric       // end of statement to avoid losing it when we switch.
49960b57cec5SDimitry Andric       check(enterIncludeFile(Filename), IncludeLoc,
49970b57cec5SDimitry Andric             "Could not find include file '" + Filename + "'"))
49980b57cec5SDimitry Andric     return true;
49990b57cec5SDimitry Andric 
50000b57cec5SDimitry Andric   return false;
50010b57cec5SDimitry Andric }
50020b57cec5SDimitry Andric 
50030b57cec5SDimitry Andric /// parseDirectiveIncbin
50040b57cec5SDimitry Andric ///  ::= .incbin "filename" [ , skip [ , count ] ]
50050b57cec5SDimitry Andric bool AsmParser::parseDirectiveIncbin() {
50060b57cec5SDimitry Andric   // Allow the strings to have escaped octal character sequence.
50070b57cec5SDimitry Andric   std::string Filename;
50080b57cec5SDimitry Andric   SMLoc IncbinLoc = getTok().getLoc();
50090b57cec5SDimitry Andric   if (check(getTok().isNot(AsmToken::String),
50100b57cec5SDimitry Andric             "expected string in '.incbin' directive") ||
50110b57cec5SDimitry Andric       parseEscapedString(Filename))
50120b57cec5SDimitry Andric     return true;
50130b57cec5SDimitry Andric 
50140b57cec5SDimitry Andric   int64_t Skip = 0;
50150b57cec5SDimitry Andric   const MCExpr *Count = nullptr;
50160b57cec5SDimitry Andric   SMLoc SkipLoc, CountLoc;
50170b57cec5SDimitry Andric   if (parseOptionalToken(AsmToken::Comma)) {
50180b57cec5SDimitry Andric     // The skip expression can be omitted while specifying the count, e.g:
50190b57cec5SDimitry Andric     //  .incbin "filename",,4
50200b57cec5SDimitry Andric     if (getTok().isNot(AsmToken::Comma)) {
50210b57cec5SDimitry Andric       if (parseTokenLoc(SkipLoc) || parseAbsoluteExpression(Skip))
50220b57cec5SDimitry Andric         return true;
50230b57cec5SDimitry Andric     }
50240b57cec5SDimitry Andric     if (parseOptionalToken(AsmToken::Comma)) {
50250b57cec5SDimitry Andric       CountLoc = getTok().getLoc();
50260b57cec5SDimitry Andric       if (parseExpression(Count))
50270b57cec5SDimitry Andric         return true;
50280b57cec5SDimitry Andric     }
50290b57cec5SDimitry Andric   }
50300b57cec5SDimitry Andric 
50310b57cec5SDimitry Andric   if (parseToken(AsmToken::EndOfStatement,
50320b57cec5SDimitry Andric                  "unexpected token in '.incbin' directive"))
50330b57cec5SDimitry Andric     return true;
50340b57cec5SDimitry Andric 
50350b57cec5SDimitry Andric   if (check(Skip < 0, SkipLoc, "skip is negative"))
50360b57cec5SDimitry Andric     return true;
50370b57cec5SDimitry Andric 
50380b57cec5SDimitry Andric   // Attempt to process the included file.
50390b57cec5SDimitry Andric   if (processIncbinFile(Filename, Skip, Count, CountLoc))
50400b57cec5SDimitry Andric     return Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
50410b57cec5SDimitry Andric   return false;
50420b57cec5SDimitry Andric }
50430b57cec5SDimitry Andric 
50440b57cec5SDimitry Andric /// parseDirectiveIf
50450b57cec5SDimitry Andric /// ::= .if{,eq,ge,gt,le,lt,ne} expression
50460b57cec5SDimitry Andric bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
50470b57cec5SDimitry Andric   TheCondStack.push_back(TheCondState);
50480b57cec5SDimitry Andric   TheCondState.TheCond = AsmCond::IfCond;
50490b57cec5SDimitry Andric   if (TheCondState.Ignore) {
50500b57cec5SDimitry Andric     eatToEndOfStatement();
50510b57cec5SDimitry Andric   } else {
50520b57cec5SDimitry Andric     int64_t ExprValue;
50530b57cec5SDimitry Andric     if (parseAbsoluteExpression(ExprValue) ||
50540b57cec5SDimitry Andric         parseToken(AsmToken::EndOfStatement,
50550b57cec5SDimitry Andric                    "unexpected token in '.if' directive"))
50560b57cec5SDimitry Andric       return true;
50570b57cec5SDimitry Andric 
50580b57cec5SDimitry Andric     switch (DirKind) {
50590b57cec5SDimitry Andric     default:
50600b57cec5SDimitry Andric       llvm_unreachable("unsupported directive");
50610b57cec5SDimitry Andric     case DK_IF:
50620b57cec5SDimitry Andric     case DK_IFNE:
50630b57cec5SDimitry Andric       break;
50640b57cec5SDimitry Andric     case DK_IFEQ:
50650b57cec5SDimitry Andric       ExprValue = ExprValue == 0;
50660b57cec5SDimitry Andric       break;
50670b57cec5SDimitry Andric     case DK_IFGE:
50680b57cec5SDimitry Andric       ExprValue = ExprValue >= 0;
50690b57cec5SDimitry Andric       break;
50700b57cec5SDimitry Andric     case DK_IFGT:
50710b57cec5SDimitry Andric       ExprValue = ExprValue > 0;
50720b57cec5SDimitry Andric       break;
50730b57cec5SDimitry Andric     case DK_IFLE:
50740b57cec5SDimitry Andric       ExprValue = ExprValue <= 0;
50750b57cec5SDimitry Andric       break;
50760b57cec5SDimitry Andric     case DK_IFLT:
50770b57cec5SDimitry Andric       ExprValue = ExprValue < 0;
50780b57cec5SDimitry Andric       break;
50790b57cec5SDimitry Andric     }
50800b57cec5SDimitry Andric 
50810b57cec5SDimitry Andric     TheCondState.CondMet = ExprValue;
50820b57cec5SDimitry Andric     TheCondState.Ignore = !TheCondState.CondMet;
50830b57cec5SDimitry Andric   }
50840b57cec5SDimitry Andric 
50850b57cec5SDimitry Andric   return false;
50860b57cec5SDimitry Andric }
50870b57cec5SDimitry Andric 
50880b57cec5SDimitry Andric /// parseDirectiveIfb
50890b57cec5SDimitry Andric /// ::= .ifb string
50900b57cec5SDimitry Andric bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
50910b57cec5SDimitry Andric   TheCondStack.push_back(TheCondState);
50920b57cec5SDimitry Andric   TheCondState.TheCond = AsmCond::IfCond;
50930b57cec5SDimitry Andric 
50940b57cec5SDimitry Andric   if (TheCondState.Ignore) {
50950b57cec5SDimitry Andric     eatToEndOfStatement();
50960b57cec5SDimitry Andric   } else {
50970b57cec5SDimitry Andric     StringRef Str = parseStringToEndOfStatement();
50980b57cec5SDimitry Andric 
50990b57cec5SDimitry Andric     if (parseToken(AsmToken::EndOfStatement,
51000b57cec5SDimitry Andric                    "unexpected token in '.ifb' directive"))
51010b57cec5SDimitry Andric       return true;
51020b57cec5SDimitry Andric 
51030b57cec5SDimitry Andric     TheCondState.CondMet = ExpectBlank == Str.empty();
51040b57cec5SDimitry Andric     TheCondState.Ignore = !TheCondState.CondMet;
51050b57cec5SDimitry Andric   }
51060b57cec5SDimitry Andric 
51070b57cec5SDimitry Andric   return false;
51080b57cec5SDimitry Andric }
51090b57cec5SDimitry Andric 
51100b57cec5SDimitry Andric /// parseDirectiveIfc
51110b57cec5SDimitry Andric /// ::= .ifc string1, string2
51120b57cec5SDimitry Andric /// ::= .ifnc string1, string2
51130b57cec5SDimitry Andric bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
51140b57cec5SDimitry Andric   TheCondStack.push_back(TheCondState);
51150b57cec5SDimitry Andric   TheCondState.TheCond = AsmCond::IfCond;
51160b57cec5SDimitry Andric 
51170b57cec5SDimitry Andric   if (TheCondState.Ignore) {
51180b57cec5SDimitry Andric     eatToEndOfStatement();
51190b57cec5SDimitry Andric   } else {
51200b57cec5SDimitry Andric     StringRef Str1 = parseStringToComma();
51210b57cec5SDimitry Andric 
51220b57cec5SDimitry Andric     if (parseToken(AsmToken::Comma, "unexpected token in '.ifc' directive"))
51230b57cec5SDimitry Andric       return true;
51240b57cec5SDimitry Andric 
51250b57cec5SDimitry Andric     StringRef Str2 = parseStringToEndOfStatement();
51260b57cec5SDimitry Andric 
51270b57cec5SDimitry Andric     if (parseToken(AsmToken::EndOfStatement,
51280b57cec5SDimitry Andric                    "unexpected token in '.ifc' directive"))
51290b57cec5SDimitry Andric       return true;
51300b57cec5SDimitry Andric 
51310b57cec5SDimitry Andric     TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
51320b57cec5SDimitry Andric     TheCondState.Ignore = !TheCondState.CondMet;
51330b57cec5SDimitry Andric   }
51340b57cec5SDimitry Andric 
51350b57cec5SDimitry Andric   return false;
51360b57cec5SDimitry Andric }
51370b57cec5SDimitry Andric 
51380b57cec5SDimitry Andric /// parseDirectiveIfeqs
51390b57cec5SDimitry Andric ///   ::= .ifeqs string1, string2
51400b57cec5SDimitry Andric bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) {
51410b57cec5SDimitry Andric   if (Lexer.isNot(AsmToken::String)) {
51420b57cec5SDimitry Andric     if (ExpectEqual)
51430b57cec5SDimitry Andric       return TokError("expected string parameter for '.ifeqs' directive");
51440b57cec5SDimitry Andric     return TokError("expected string parameter for '.ifnes' directive");
51450b57cec5SDimitry Andric   }
51460b57cec5SDimitry Andric 
51470b57cec5SDimitry Andric   StringRef String1 = getTok().getStringContents();
51480b57cec5SDimitry Andric   Lex();
51490b57cec5SDimitry Andric 
51500b57cec5SDimitry Andric   if (Lexer.isNot(AsmToken::Comma)) {
51510b57cec5SDimitry Andric     if (ExpectEqual)
51520b57cec5SDimitry Andric       return TokError(
51530b57cec5SDimitry Andric           "expected comma after first string for '.ifeqs' directive");
51540b57cec5SDimitry Andric     return TokError("expected comma after first string for '.ifnes' directive");
51550b57cec5SDimitry Andric   }
51560b57cec5SDimitry Andric 
51570b57cec5SDimitry Andric   Lex();
51580b57cec5SDimitry Andric 
51590b57cec5SDimitry Andric   if (Lexer.isNot(AsmToken::String)) {
51600b57cec5SDimitry Andric     if (ExpectEqual)
51610b57cec5SDimitry Andric       return TokError("expected string parameter for '.ifeqs' directive");
51620b57cec5SDimitry Andric     return TokError("expected string parameter for '.ifnes' directive");
51630b57cec5SDimitry Andric   }
51640b57cec5SDimitry Andric 
51650b57cec5SDimitry Andric   StringRef String2 = getTok().getStringContents();
51660b57cec5SDimitry Andric   Lex();
51670b57cec5SDimitry Andric 
51680b57cec5SDimitry Andric   TheCondStack.push_back(TheCondState);
51690b57cec5SDimitry Andric   TheCondState.TheCond = AsmCond::IfCond;
51700b57cec5SDimitry Andric   TheCondState.CondMet = ExpectEqual == (String1 == String2);
51710b57cec5SDimitry Andric   TheCondState.Ignore = !TheCondState.CondMet;
51720b57cec5SDimitry Andric 
51730b57cec5SDimitry Andric   return false;
51740b57cec5SDimitry Andric }
51750b57cec5SDimitry Andric 
51760b57cec5SDimitry Andric /// parseDirectiveIfdef
51770b57cec5SDimitry Andric /// ::= .ifdef symbol
51780b57cec5SDimitry Andric bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
51790b57cec5SDimitry Andric   StringRef Name;
51800b57cec5SDimitry Andric   TheCondStack.push_back(TheCondState);
51810b57cec5SDimitry Andric   TheCondState.TheCond = AsmCond::IfCond;
51820b57cec5SDimitry Andric 
51830b57cec5SDimitry Andric   if (TheCondState.Ignore) {
51840b57cec5SDimitry Andric     eatToEndOfStatement();
51850b57cec5SDimitry Andric   } else {
51860b57cec5SDimitry Andric     if (check(parseIdentifier(Name), "expected identifier after '.ifdef'") ||
51870b57cec5SDimitry Andric         parseToken(AsmToken::EndOfStatement, "unexpected token in '.ifdef'"))
51880b57cec5SDimitry Andric       return true;
51890b57cec5SDimitry Andric 
51900b57cec5SDimitry Andric     MCSymbol *Sym = getContext().lookupSymbol(Name);
51910b57cec5SDimitry Andric 
51920b57cec5SDimitry Andric     if (expect_defined)
51930b57cec5SDimitry Andric       TheCondState.CondMet = (Sym && !Sym->isUndefined(false));
51940b57cec5SDimitry Andric     else
51950b57cec5SDimitry Andric       TheCondState.CondMet = (!Sym || Sym->isUndefined(false));
51960b57cec5SDimitry Andric     TheCondState.Ignore = !TheCondState.CondMet;
51970b57cec5SDimitry Andric   }
51980b57cec5SDimitry Andric 
51990b57cec5SDimitry Andric   return false;
52000b57cec5SDimitry Andric }
52010b57cec5SDimitry Andric 
52020b57cec5SDimitry Andric /// parseDirectiveElseIf
52030b57cec5SDimitry Andric /// ::= .elseif expression
52040b57cec5SDimitry Andric bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
52050b57cec5SDimitry Andric   if (TheCondState.TheCond != AsmCond::IfCond &&
52060b57cec5SDimitry Andric       TheCondState.TheCond != AsmCond::ElseIfCond)
52070b57cec5SDimitry Andric     return Error(DirectiveLoc, "Encountered a .elseif that doesn't follow an"
52080b57cec5SDimitry Andric                                " .if or  an .elseif");
52090b57cec5SDimitry Andric   TheCondState.TheCond = AsmCond::ElseIfCond;
52100b57cec5SDimitry Andric 
52110b57cec5SDimitry Andric   bool LastIgnoreState = false;
52120b57cec5SDimitry Andric   if (!TheCondStack.empty())
52130b57cec5SDimitry Andric     LastIgnoreState = TheCondStack.back().Ignore;
52140b57cec5SDimitry Andric   if (LastIgnoreState || TheCondState.CondMet) {
52150b57cec5SDimitry Andric     TheCondState.Ignore = true;
52160b57cec5SDimitry Andric     eatToEndOfStatement();
52170b57cec5SDimitry Andric   } else {
52180b57cec5SDimitry Andric     int64_t ExprValue;
52190b57cec5SDimitry Andric     if (parseAbsoluteExpression(ExprValue))
52200b57cec5SDimitry Andric       return true;
52210b57cec5SDimitry Andric 
52220b57cec5SDimitry Andric     if (parseToken(AsmToken::EndOfStatement,
52230b57cec5SDimitry Andric                    "unexpected token in '.elseif' directive"))
52240b57cec5SDimitry Andric       return true;
52250b57cec5SDimitry Andric 
52260b57cec5SDimitry Andric     TheCondState.CondMet = ExprValue;
52270b57cec5SDimitry Andric     TheCondState.Ignore = !TheCondState.CondMet;
52280b57cec5SDimitry Andric   }
52290b57cec5SDimitry Andric 
52300b57cec5SDimitry Andric   return false;
52310b57cec5SDimitry Andric }
52320b57cec5SDimitry Andric 
52330b57cec5SDimitry Andric /// parseDirectiveElse
52340b57cec5SDimitry Andric /// ::= .else
52350b57cec5SDimitry Andric bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
52360b57cec5SDimitry Andric   if (parseToken(AsmToken::EndOfStatement,
52370b57cec5SDimitry Andric                  "unexpected token in '.else' directive"))
52380b57cec5SDimitry Andric     return true;
52390b57cec5SDimitry Andric 
52400b57cec5SDimitry Andric   if (TheCondState.TheCond != AsmCond::IfCond &&
52410b57cec5SDimitry Andric       TheCondState.TheCond != AsmCond::ElseIfCond)
52420b57cec5SDimitry Andric     return Error(DirectiveLoc, "Encountered a .else that doesn't follow "
52430b57cec5SDimitry Andric                                " an .if or an .elseif");
52440b57cec5SDimitry Andric   TheCondState.TheCond = AsmCond::ElseCond;
52450b57cec5SDimitry Andric   bool LastIgnoreState = false;
52460b57cec5SDimitry Andric   if (!TheCondStack.empty())
52470b57cec5SDimitry Andric     LastIgnoreState = TheCondStack.back().Ignore;
52480b57cec5SDimitry Andric   if (LastIgnoreState || TheCondState.CondMet)
52490b57cec5SDimitry Andric     TheCondState.Ignore = true;
52500b57cec5SDimitry Andric   else
52510b57cec5SDimitry Andric     TheCondState.Ignore = false;
52520b57cec5SDimitry Andric 
52530b57cec5SDimitry Andric   return false;
52540b57cec5SDimitry Andric }
52550b57cec5SDimitry Andric 
52560b57cec5SDimitry Andric /// parseDirectiveEnd
52570b57cec5SDimitry Andric /// ::= .end
52580b57cec5SDimitry Andric bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
52590b57cec5SDimitry Andric   if (parseToken(AsmToken::EndOfStatement,
52600b57cec5SDimitry Andric                  "unexpected token in '.end' directive"))
52610b57cec5SDimitry Andric     return true;
52620b57cec5SDimitry Andric 
52630b57cec5SDimitry Andric   while (Lexer.isNot(AsmToken::Eof))
52640b57cec5SDimitry Andric     Lexer.Lex();
52650b57cec5SDimitry Andric 
52660b57cec5SDimitry Andric   return false;
52670b57cec5SDimitry Andric }
52680b57cec5SDimitry Andric 
52690b57cec5SDimitry Andric /// parseDirectiveError
52700b57cec5SDimitry Andric ///   ::= .err
52710b57cec5SDimitry Andric ///   ::= .error [string]
52720b57cec5SDimitry Andric bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
52730b57cec5SDimitry Andric   if (!TheCondStack.empty()) {
52740b57cec5SDimitry Andric     if (TheCondStack.back().Ignore) {
52750b57cec5SDimitry Andric       eatToEndOfStatement();
52760b57cec5SDimitry Andric       return false;
52770b57cec5SDimitry Andric     }
52780b57cec5SDimitry Andric   }
52790b57cec5SDimitry Andric 
52800b57cec5SDimitry Andric   if (!WithMessage)
52810b57cec5SDimitry Andric     return Error(L, ".err encountered");
52820b57cec5SDimitry Andric 
52830b57cec5SDimitry Andric   StringRef Message = ".error directive invoked in source file";
52840b57cec5SDimitry Andric   if (Lexer.isNot(AsmToken::EndOfStatement)) {
52850b57cec5SDimitry Andric     if (Lexer.isNot(AsmToken::String))
52860b57cec5SDimitry Andric       return TokError(".error argument must be a string");
52870b57cec5SDimitry Andric 
52880b57cec5SDimitry Andric     Message = getTok().getStringContents();
52890b57cec5SDimitry Andric     Lex();
52900b57cec5SDimitry Andric   }
52910b57cec5SDimitry Andric 
52920b57cec5SDimitry Andric   return Error(L, Message);
52930b57cec5SDimitry Andric }
52940b57cec5SDimitry Andric 
52950b57cec5SDimitry Andric /// parseDirectiveWarning
52960b57cec5SDimitry Andric ///   ::= .warning [string]
52970b57cec5SDimitry Andric bool AsmParser::parseDirectiveWarning(SMLoc L) {
52980b57cec5SDimitry Andric   if (!TheCondStack.empty()) {
52990b57cec5SDimitry Andric     if (TheCondStack.back().Ignore) {
53000b57cec5SDimitry Andric       eatToEndOfStatement();
53010b57cec5SDimitry Andric       return false;
53020b57cec5SDimitry Andric     }
53030b57cec5SDimitry Andric   }
53040b57cec5SDimitry Andric 
53050b57cec5SDimitry Andric   StringRef Message = ".warning directive invoked in source file";
53060b57cec5SDimitry Andric 
53070b57cec5SDimitry Andric   if (!parseOptionalToken(AsmToken::EndOfStatement)) {
53080b57cec5SDimitry Andric     if (Lexer.isNot(AsmToken::String))
53090b57cec5SDimitry Andric       return TokError(".warning argument must be a string");
53100b57cec5SDimitry Andric 
53110b57cec5SDimitry Andric     Message = getTok().getStringContents();
53120b57cec5SDimitry Andric     Lex();
53130b57cec5SDimitry Andric     if (parseToken(AsmToken::EndOfStatement,
53140b57cec5SDimitry Andric                    "expected end of statement in '.warning' directive"))
53150b57cec5SDimitry Andric       return true;
53160b57cec5SDimitry Andric   }
53170b57cec5SDimitry Andric 
53180b57cec5SDimitry Andric   return Warning(L, Message);
53190b57cec5SDimitry Andric }
53200b57cec5SDimitry Andric 
53210b57cec5SDimitry Andric /// parseDirectiveEndIf
53220b57cec5SDimitry Andric /// ::= .endif
53230b57cec5SDimitry Andric bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
53240b57cec5SDimitry Andric   if (parseToken(AsmToken::EndOfStatement,
53250b57cec5SDimitry Andric                  "unexpected token in '.endif' directive"))
53260b57cec5SDimitry Andric     return true;
53270b57cec5SDimitry Andric 
53280b57cec5SDimitry Andric   if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
53290b57cec5SDimitry Andric     return Error(DirectiveLoc, "Encountered a .endif that doesn't follow "
53300b57cec5SDimitry Andric                                "an .if or .else");
53310b57cec5SDimitry Andric   if (!TheCondStack.empty()) {
53320b57cec5SDimitry Andric     TheCondState = TheCondStack.back();
53330b57cec5SDimitry Andric     TheCondStack.pop_back();
53340b57cec5SDimitry Andric   }
53350b57cec5SDimitry Andric 
53360b57cec5SDimitry Andric   return false;
53370b57cec5SDimitry Andric }
53380b57cec5SDimitry Andric 
53390b57cec5SDimitry Andric void AsmParser::initializeDirectiveKindMap() {
5340*5ffd83dbSDimitry Andric   /* Lookup will be done with the directive
5341*5ffd83dbSDimitry Andric    * converted to lower case, so all these
5342*5ffd83dbSDimitry Andric    * keys should be lower case.
5343*5ffd83dbSDimitry Andric    * (target specific directives are handled
5344*5ffd83dbSDimitry Andric    *  elsewhere)
5345*5ffd83dbSDimitry Andric    */
53460b57cec5SDimitry Andric   DirectiveKindMap[".set"] = DK_SET;
53470b57cec5SDimitry Andric   DirectiveKindMap[".equ"] = DK_EQU;
53480b57cec5SDimitry Andric   DirectiveKindMap[".equiv"] = DK_EQUIV;
53490b57cec5SDimitry Andric   DirectiveKindMap[".ascii"] = DK_ASCII;
53500b57cec5SDimitry Andric   DirectiveKindMap[".asciz"] = DK_ASCIZ;
53510b57cec5SDimitry Andric   DirectiveKindMap[".string"] = DK_STRING;
53520b57cec5SDimitry Andric   DirectiveKindMap[".byte"] = DK_BYTE;
53530b57cec5SDimitry Andric   DirectiveKindMap[".short"] = DK_SHORT;
53540b57cec5SDimitry Andric   DirectiveKindMap[".value"] = DK_VALUE;
53550b57cec5SDimitry Andric   DirectiveKindMap[".2byte"] = DK_2BYTE;
53560b57cec5SDimitry Andric   DirectiveKindMap[".long"] = DK_LONG;
53570b57cec5SDimitry Andric   DirectiveKindMap[".int"] = DK_INT;
53580b57cec5SDimitry Andric   DirectiveKindMap[".4byte"] = DK_4BYTE;
53590b57cec5SDimitry Andric   DirectiveKindMap[".quad"] = DK_QUAD;
53600b57cec5SDimitry Andric   DirectiveKindMap[".8byte"] = DK_8BYTE;
53610b57cec5SDimitry Andric   DirectiveKindMap[".octa"] = DK_OCTA;
53620b57cec5SDimitry Andric   DirectiveKindMap[".single"] = DK_SINGLE;
53630b57cec5SDimitry Andric   DirectiveKindMap[".float"] = DK_FLOAT;
53640b57cec5SDimitry Andric   DirectiveKindMap[".double"] = DK_DOUBLE;
53650b57cec5SDimitry Andric   DirectiveKindMap[".align"] = DK_ALIGN;
53660b57cec5SDimitry Andric   DirectiveKindMap[".align32"] = DK_ALIGN32;
53670b57cec5SDimitry Andric   DirectiveKindMap[".balign"] = DK_BALIGN;
53680b57cec5SDimitry Andric   DirectiveKindMap[".balignw"] = DK_BALIGNW;
53690b57cec5SDimitry Andric   DirectiveKindMap[".balignl"] = DK_BALIGNL;
53700b57cec5SDimitry Andric   DirectiveKindMap[".p2align"] = DK_P2ALIGN;
53710b57cec5SDimitry Andric   DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
53720b57cec5SDimitry Andric   DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
53730b57cec5SDimitry Andric   DirectiveKindMap[".org"] = DK_ORG;
53740b57cec5SDimitry Andric   DirectiveKindMap[".fill"] = DK_FILL;
53750b57cec5SDimitry Andric   DirectiveKindMap[".zero"] = DK_ZERO;
53760b57cec5SDimitry Andric   DirectiveKindMap[".extern"] = DK_EXTERN;
53770b57cec5SDimitry Andric   DirectiveKindMap[".globl"] = DK_GLOBL;
53780b57cec5SDimitry Andric   DirectiveKindMap[".global"] = DK_GLOBAL;
53790b57cec5SDimitry Andric   DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
53800b57cec5SDimitry Andric   DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
53810b57cec5SDimitry Andric   DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
53820b57cec5SDimitry Andric   DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
53830b57cec5SDimitry Andric   DirectiveKindMap[".reference"] = DK_REFERENCE;
53840b57cec5SDimitry Andric   DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
53850b57cec5SDimitry Andric   DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
53860b57cec5SDimitry Andric   DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
53870b57cec5SDimitry Andric   DirectiveKindMap[".cold"] = DK_COLD;
53880b57cec5SDimitry Andric   DirectiveKindMap[".comm"] = DK_COMM;
53890b57cec5SDimitry Andric   DirectiveKindMap[".common"] = DK_COMMON;
53900b57cec5SDimitry Andric   DirectiveKindMap[".lcomm"] = DK_LCOMM;
53910b57cec5SDimitry Andric   DirectiveKindMap[".abort"] = DK_ABORT;
53920b57cec5SDimitry Andric   DirectiveKindMap[".include"] = DK_INCLUDE;
53930b57cec5SDimitry Andric   DirectiveKindMap[".incbin"] = DK_INCBIN;
53940b57cec5SDimitry Andric   DirectiveKindMap[".code16"] = DK_CODE16;
53950b57cec5SDimitry Andric   DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
53960b57cec5SDimitry Andric   DirectiveKindMap[".rept"] = DK_REPT;
53970b57cec5SDimitry Andric   DirectiveKindMap[".rep"] = DK_REPT;
53980b57cec5SDimitry Andric   DirectiveKindMap[".irp"] = DK_IRP;
53990b57cec5SDimitry Andric   DirectiveKindMap[".irpc"] = DK_IRPC;
54000b57cec5SDimitry Andric   DirectiveKindMap[".endr"] = DK_ENDR;
54010b57cec5SDimitry Andric   DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
54020b57cec5SDimitry Andric   DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
54030b57cec5SDimitry Andric   DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
54040b57cec5SDimitry Andric   DirectiveKindMap[".if"] = DK_IF;
54050b57cec5SDimitry Andric   DirectiveKindMap[".ifeq"] = DK_IFEQ;
54060b57cec5SDimitry Andric   DirectiveKindMap[".ifge"] = DK_IFGE;
54070b57cec5SDimitry Andric   DirectiveKindMap[".ifgt"] = DK_IFGT;
54080b57cec5SDimitry Andric   DirectiveKindMap[".ifle"] = DK_IFLE;
54090b57cec5SDimitry Andric   DirectiveKindMap[".iflt"] = DK_IFLT;
54100b57cec5SDimitry Andric   DirectiveKindMap[".ifne"] = DK_IFNE;
54110b57cec5SDimitry Andric   DirectiveKindMap[".ifb"] = DK_IFB;
54120b57cec5SDimitry Andric   DirectiveKindMap[".ifnb"] = DK_IFNB;
54130b57cec5SDimitry Andric   DirectiveKindMap[".ifc"] = DK_IFC;
54140b57cec5SDimitry Andric   DirectiveKindMap[".ifeqs"] = DK_IFEQS;
54150b57cec5SDimitry Andric   DirectiveKindMap[".ifnc"] = DK_IFNC;
54160b57cec5SDimitry Andric   DirectiveKindMap[".ifnes"] = DK_IFNES;
54170b57cec5SDimitry Andric   DirectiveKindMap[".ifdef"] = DK_IFDEF;
54180b57cec5SDimitry Andric   DirectiveKindMap[".ifndef"] = DK_IFNDEF;
54190b57cec5SDimitry Andric   DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
54200b57cec5SDimitry Andric   DirectiveKindMap[".elseif"] = DK_ELSEIF;
54210b57cec5SDimitry Andric   DirectiveKindMap[".else"] = DK_ELSE;
54220b57cec5SDimitry Andric   DirectiveKindMap[".end"] = DK_END;
54230b57cec5SDimitry Andric   DirectiveKindMap[".endif"] = DK_ENDIF;
54240b57cec5SDimitry Andric   DirectiveKindMap[".skip"] = DK_SKIP;
54250b57cec5SDimitry Andric   DirectiveKindMap[".space"] = DK_SPACE;
54260b57cec5SDimitry Andric   DirectiveKindMap[".file"] = DK_FILE;
54270b57cec5SDimitry Andric   DirectiveKindMap[".line"] = DK_LINE;
54280b57cec5SDimitry Andric   DirectiveKindMap[".loc"] = DK_LOC;
54290b57cec5SDimitry Andric   DirectiveKindMap[".stabs"] = DK_STABS;
54300b57cec5SDimitry Andric   DirectiveKindMap[".cv_file"] = DK_CV_FILE;
54310b57cec5SDimitry Andric   DirectiveKindMap[".cv_func_id"] = DK_CV_FUNC_ID;
54320b57cec5SDimitry Andric   DirectiveKindMap[".cv_loc"] = DK_CV_LOC;
54330b57cec5SDimitry Andric   DirectiveKindMap[".cv_linetable"] = DK_CV_LINETABLE;
54340b57cec5SDimitry Andric   DirectiveKindMap[".cv_inline_linetable"] = DK_CV_INLINE_LINETABLE;
54350b57cec5SDimitry Andric   DirectiveKindMap[".cv_inline_site_id"] = DK_CV_INLINE_SITE_ID;
54360b57cec5SDimitry Andric   DirectiveKindMap[".cv_def_range"] = DK_CV_DEF_RANGE;
54370b57cec5SDimitry Andric   DirectiveKindMap[".cv_string"] = DK_CV_STRING;
54380b57cec5SDimitry Andric   DirectiveKindMap[".cv_stringtable"] = DK_CV_STRINGTABLE;
54390b57cec5SDimitry Andric   DirectiveKindMap[".cv_filechecksums"] = DK_CV_FILECHECKSUMS;
54400b57cec5SDimitry Andric   DirectiveKindMap[".cv_filechecksumoffset"] = DK_CV_FILECHECKSUM_OFFSET;
54410b57cec5SDimitry Andric   DirectiveKindMap[".cv_fpo_data"] = DK_CV_FPO_DATA;
54420b57cec5SDimitry Andric   DirectiveKindMap[".sleb128"] = DK_SLEB128;
54430b57cec5SDimitry Andric   DirectiveKindMap[".uleb128"] = DK_ULEB128;
54440b57cec5SDimitry Andric   DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
54450b57cec5SDimitry Andric   DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
54460b57cec5SDimitry Andric   DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
54470b57cec5SDimitry Andric   DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
54480b57cec5SDimitry Andric   DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
54490b57cec5SDimitry Andric   DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
54500b57cec5SDimitry Andric   DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
54510b57cec5SDimitry Andric   DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
54520b57cec5SDimitry Andric   DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
54530b57cec5SDimitry Andric   DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
54540b57cec5SDimitry Andric   DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
54550b57cec5SDimitry Andric   DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
54560b57cec5SDimitry Andric   DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
54570b57cec5SDimitry Andric   DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
54580b57cec5SDimitry Andric   DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
54590b57cec5SDimitry Andric   DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
54600b57cec5SDimitry Andric   DirectiveKindMap[".cfi_return_column"] = DK_CFI_RETURN_COLUMN;
54610b57cec5SDimitry Andric   DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
54620b57cec5SDimitry Andric   DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
54630b57cec5SDimitry Andric   DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
54640b57cec5SDimitry Andric   DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
54650b57cec5SDimitry Andric   DirectiveKindMap[".cfi_b_key_frame"] = DK_CFI_B_KEY_FRAME;
54660b57cec5SDimitry Andric   DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
54670b57cec5SDimitry Andric   DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
54680b57cec5SDimitry Andric   DirectiveKindMap[".macro"] = DK_MACRO;
54690b57cec5SDimitry Andric   DirectiveKindMap[".exitm"] = DK_EXITM;
54700b57cec5SDimitry Andric   DirectiveKindMap[".endm"] = DK_ENDM;
54710b57cec5SDimitry Andric   DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
54720b57cec5SDimitry Andric   DirectiveKindMap[".purgem"] = DK_PURGEM;
54730b57cec5SDimitry Andric   DirectiveKindMap[".err"] = DK_ERR;
54740b57cec5SDimitry Andric   DirectiveKindMap[".error"] = DK_ERROR;
54750b57cec5SDimitry Andric   DirectiveKindMap[".warning"] = DK_WARNING;
54760b57cec5SDimitry Andric   DirectiveKindMap[".altmacro"] = DK_ALTMACRO;
54770b57cec5SDimitry Andric   DirectiveKindMap[".noaltmacro"] = DK_NOALTMACRO;
54780b57cec5SDimitry Andric   DirectiveKindMap[".reloc"] = DK_RELOC;
54790b57cec5SDimitry Andric   DirectiveKindMap[".dc"] = DK_DC;
54800b57cec5SDimitry Andric   DirectiveKindMap[".dc.a"] = DK_DC_A;
54810b57cec5SDimitry Andric   DirectiveKindMap[".dc.b"] = DK_DC_B;
54820b57cec5SDimitry Andric   DirectiveKindMap[".dc.d"] = DK_DC_D;
54830b57cec5SDimitry Andric   DirectiveKindMap[".dc.l"] = DK_DC_L;
54840b57cec5SDimitry Andric   DirectiveKindMap[".dc.s"] = DK_DC_S;
54850b57cec5SDimitry Andric   DirectiveKindMap[".dc.w"] = DK_DC_W;
54860b57cec5SDimitry Andric   DirectiveKindMap[".dc.x"] = DK_DC_X;
54870b57cec5SDimitry Andric   DirectiveKindMap[".dcb"] = DK_DCB;
54880b57cec5SDimitry Andric   DirectiveKindMap[".dcb.b"] = DK_DCB_B;
54890b57cec5SDimitry Andric   DirectiveKindMap[".dcb.d"] = DK_DCB_D;
54900b57cec5SDimitry Andric   DirectiveKindMap[".dcb.l"] = DK_DCB_L;
54910b57cec5SDimitry Andric   DirectiveKindMap[".dcb.s"] = DK_DCB_S;
54920b57cec5SDimitry Andric   DirectiveKindMap[".dcb.w"] = DK_DCB_W;
54930b57cec5SDimitry Andric   DirectiveKindMap[".dcb.x"] = DK_DCB_X;
54940b57cec5SDimitry Andric   DirectiveKindMap[".ds"] = DK_DS;
54950b57cec5SDimitry Andric   DirectiveKindMap[".ds.b"] = DK_DS_B;
54960b57cec5SDimitry Andric   DirectiveKindMap[".ds.d"] = DK_DS_D;
54970b57cec5SDimitry Andric   DirectiveKindMap[".ds.l"] = DK_DS_L;
54980b57cec5SDimitry Andric   DirectiveKindMap[".ds.p"] = DK_DS_P;
54990b57cec5SDimitry Andric   DirectiveKindMap[".ds.s"] = DK_DS_S;
55000b57cec5SDimitry Andric   DirectiveKindMap[".ds.w"] = DK_DS_W;
55010b57cec5SDimitry Andric   DirectiveKindMap[".ds.x"] = DK_DS_X;
55020b57cec5SDimitry Andric   DirectiveKindMap[".print"] = DK_PRINT;
55030b57cec5SDimitry Andric   DirectiveKindMap[".addrsig"] = DK_ADDRSIG;
55040b57cec5SDimitry Andric   DirectiveKindMap[".addrsig_sym"] = DK_ADDRSIG_SYM;
55050b57cec5SDimitry Andric }
55060b57cec5SDimitry Andric 
55070b57cec5SDimitry Andric MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
55080b57cec5SDimitry Andric   AsmToken EndToken, StartToken = getTok();
55090b57cec5SDimitry Andric 
55100b57cec5SDimitry Andric   unsigned NestLevel = 0;
55110b57cec5SDimitry Andric   while (true) {
55120b57cec5SDimitry Andric     // Check whether we have reached the end of the file.
55130b57cec5SDimitry Andric     if (getLexer().is(AsmToken::Eof)) {
55140b57cec5SDimitry Andric       printError(DirectiveLoc, "no matching '.endr' in definition");
55150b57cec5SDimitry Andric       return nullptr;
55160b57cec5SDimitry Andric     }
55170b57cec5SDimitry Andric 
55180b57cec5SDimitry Andric     if (Lexer.is(AsmToken::Identifier) &&
55190b57cec5SDimitry Andric         (getTok().getIdentifier() == ".rep" ||
55200b57cec5SDimitry Andric          getTok().getIdentifier() == ".rept" ||
55210b57cec5SDimitry Andric          getTok().getIdentifier() == ".irp" ||
55220b57cec5SDimitry Andric          getTok().getIdentifier() == ".irpc")) {
55230b57cec5SDimitry Andric       ++NestLevel;
55240b57cec5SDimitry Andric     }
55250b57cec5SDimitry Andric 
55260b57cec5SDimitry Andric     // Otherwise, check whether we have reached the .endr.
55270b57cec5SDimitry Andric     if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
55280b57cec5SDimitry Andric       if (NestLevel == 0) {
55290b57cec5SDimitry Andric         EndToken = getTok();
55300b57cec5SDimitry Andric         Lex();
55310b57cec5SDimitry Andric         if (Lexer.isNot(AsmToken::EndOfStatement)) {
55320b57cec5SDimitry Andric           printError(getTok().getLoc(),
55330b57cec5SDimitry Andric                      "unexpected token in '.endr' directive");
55340b57cec5SDimitry Andric           return nullptr;
55350b57cec5SDimitry Andric         }
55360b57cec5SDimitry Andric         break;
55370b57cec5SDimitry Andric       }
55380b57cec5SDimitry Andric       --NestLevel;
55390b57cec5SDimitry Andric     }
55400b57cec5SDimitry Andric 
55410b57cec5SDimitry Andric     // Otherwise, scan till the end of the statement.
55420b57cec5SDimitry Andric     eatToEndOfStatement();
55430b57cec5SDimitry Andric   }
55440b57cec5SDimitry Andric 
55450b57cec5SDimitry Andric   const char *BodyStart = StartToken.getLoc().getPointer();
55460b57cec5SDimitry Andric   const char *BodyEnd = EndToken.getLoc().getPointer();
55470b57cec5SDimitry Andric   StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
55480b57cec5SDimitry Andric 
55490b57cec5SDimitry Andric   // We Are Anonymous.
55500b57cec5SDimitry Andric   MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters());
55510b57cec5SDimitry Andric   return &MacroLikeBodies.back();
55520b57cec5SDimitry Andric }
55530b57cec5SDimitry Andric 
55540b57cec5SDimitry Andric void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
55550b57cec5SDimitry Andric                                          raw_svector_ostream &OS) {
55560b57cec5SDimitry Andric   OS << ".endr\n";
55570b57cec5SDimitry Andric 
55580b57cec5SDimitry Andric   std::unique_ptr<MemoryBuffer> Instantiation =
55590b57cec5SDimitry Andric       MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
55600b57cec5SDimitry Andric 
55610b57cec5SDimitry Andric   // Create the macro instantiation object and add to the current macro
55620b57cec5SDimitry Andric   // instantiation stack.
5563480093f4SDimitry Andric   MacroInstantiation *MI = new MacroInstantiation{
5564480093f4SDimitry Andric       DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()};
55650b57cec5SDimitry Andric   ActiveMacros.push_back(MI);
55660b57cec5SDimitry Andric 
55670b57cec5SDimitry Andric   // Jump to the macro instantiation and prime the lexer.
55680b57cec5SDimitry Andric   CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
55690b57cec5SDimitry Andric   Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
55700b57cec5SDimitry Andric   Lex();
55710b57cec5SDimitry Andric }
55720b57cec5SDimitry Andric 
55730b57cec5SDimitry Andric /// parseDirectiveRept
55740b57cec5SDimitry Andric ///   ::= .rep | .rept count
55750b57cec5SDimitry Andric bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
55760b57cec5SDimitry Andric   const MCExpr *CountExpr;
55770b57cec5SDimitry Andric   SMLoc CountLoc = getTok().getLoc();
55780b57cec5SDimitry Andric   if (parseExpression(CountExpr))
55790b57cec5SDimitry Andric     return true;
55800b57cec5SDimitry Andric 
55810b57cec5SDimitry Andric   int64_t Count;
55820b57cec5SDimitry Andric   if (!CountExpr->evaluateAsAbsolute(Count, getStreamer().getAssemblerPtr())) {
55830b57cec5SDimitry Andric     return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
55840b57cec5SDimitry Andric   }
55850b57cec5SDimitry Andric 
55860b57cec5SDimitry Andric   if (check(Count < 0, CountLoc, "Count is negative") ||
55870b57cec5SDimitry Andric       parseToken(AsmToken::EndOfStatement,
55880b57cec5SDimitry Andric                  "unexpected token in '" + Dir + "' directive"))
55890b57cec5SDimitry Andric     return true;
55900b57cec5SDimitry Andric 
55910b57cec5SDimitry Andric   // Lex the rept definition.
55920b57cec5SDimitry Andric   MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
55930b57cec5SDimitry Andric   if (!M)
55940b57cec5SDimitry Andric     return true;
55950b57cec5SDimitry Andric 
55960b57cec5SDimitry Andric   // Macro instantiation is lexical, unfortunately. We construct a new buffer
55970b57cec5SDimitry Andric   // to hold the macro body with substitutions.
55980b57cec5SDimitry Andric   SmallString<256> Buf;
55990b57cec5SDimitry Andric   raw_svector_ostream OS(Buf);
56000b57cec5SDimitry Andric   while (Count--) {
56010b57cec5SDimitry Andric     // Note that the AtPseudoVariable is disabled for instantiations of .rep(t).
56020b57cec5SDimitry Andric     if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc()))
56030b57cec5SDimitry Andric       return true;
56040b57cec5SDimitry Andric   }
56050b57cec5SDimitry Andric   instantiateMacroLikeBody(M, DirectiveLoc, OS);
56060b57cec5SDimitry Andric 
56070b57cec5SDimitry Andric   return false;
56080b57cec5SDimitry Andric }
56090b57cec5SDimitry Andric 
56100b57cec5SDimitry Andric /// parseDirectiveIrp
56110b57cec5SDimitry Andric /// ::= .irp symbol,values
56120b57cec5SDimitry Andric bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
56130b57cec5SDimitry Andric   MCAsmMacroParameter Parameter;
56140b57cec5SDimitry Andric   MCAsmMacroArguments A;
56150b57cec5SDimitry Andric   if (check(parseIdentifier(Parameter.Name),
56160b57cec5SDimitry Andric             "expected identifier in '.irp' directive") ||
56170b57cec5SDimitry Andric       parseToken(AsmToken::Comma, "expected comma in '.irp' directive") ||
56180b57cec5SDimitry Andric       parseMacroArguments(nullptr, A) ||
56190b57cec5SDimitry Andric       parseToken(AsmToken::EndOfStatement, "expected End of Statement"))
56200b57cec5SDimitry Andric     return true;
56210b57cec5SDimitry Andric 
56220b57cec5SDimitry Andric   // Lex the irp definition.
56230b57cec5SDimitry Andric   MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
56240b57cec5SDimitry Andric   if (!M)
56250b57cec5SDimitry Andric     return true;
56260b57cec5SDimitry Andric 
56270b57cec5SDimitry Andric   // Macro instantiation is lexical, unfortunately. We construct a new buffer
56280b57cec5SDimitry Andric   // to hold the macro body with substitutions.
56290b57cec5SDimitry Andric   SmallString<256> Buf;
56300b57cec5SDimitry Andric   raw_svector_ostream OS(Buf);
56310b57cec5SDimitry Andric 
56320b57cec5SDimitry Andric   for (const MCAsmMacroArgument &Arg : A) {
56330b57cec5SDimitry Andric     // Note that the AtPseudoVariable is enabled for instantiations of .irp.
56340b57cec5SDimitry Andric     // This is undocumented, but GAS seems to support it.
56350b57cec5SDimitry Andric     if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
56360b57cec5SDimitry Andric       return true;
56370b57cec5SDimitry Andric   }
56380b57cec5SDimitry Andric 
56390b57cec5SDimitry Andric   instantiateMacroLikeBody(M, DirectiveLoc, OS);
56400b57cec5SDimitry Andric 
56410b57cec5SDimitry Andric   return false;
56420b57cec5SDimitry Andric }
56430b57cec5SDimitry Andric 
56440b57cec5SDimitry Andric /// parseDirectiveIrpc
56450b57cec5SDimitry Andric /// ::= .irpc symbol,values
56460b57cec5SDimitry Andric bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
56470b57cec5SDimitry Andric   MCAsmMacroParameter Parameter;
56480b57cec5SDimitry Andric   MCAsmMacroArguments A;
56490b57cec5SDimitry Andric 
56500b57cec5SDimitry Andric   if (check(parseIdentifier(Parameter.Name),
56510b57cec5SDimitry Andric             "expected identifier in '.irpc' directive") ||
56520b57cec5SDimitry Andric       parseToken(AsmToken::Comma, "expected comma in '.irpc' directive") ||
56530b57cec5SDimitry Andric       parseMacroArguments(nullptr, A))
56540b57cec5SDimitry Andric     return true;
56550b57cec5SDimitry Andric 
56560b57cec5SDimitry Andric   if (A.size() != 1 || A.front().size() != 1)
56570b57cec5SDimitry Andric     return TokError("unexpected token in '.irpc' directive");
56580b57cec5SDimitry Andric 
56590b57cec5SDimitry Andric   // Eat the end of statement.
56600b57cec5SDimitry Andric   if (parseToken(AsmToken::EndOfStatement, "expected end of statement"))
56610b57cec5SDimitry Andric     return true;
56620b57cec5SDimitry Andric 
56630b57cec5SDimitry Andric   // Lex the irpc definition.
56640b57cec5SDimitry Andric   MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
56650b57cec5SDimitry Andric   if (!M)
56660b57cec5SDimitry Andric     return true;
56670b57cec5SDimitry Andric 
56680b57cec5SDimitry Andric   // Macro instantiation is lexical, unfortunately. We construct a new buffer
56690b57cec5SDimitry Andric   // to hold the macro body with substitutions.
56700b57cec5SDimitry Andric   SmallString<256> Buf;
56710b57cec5SDimitry Andric   raw_svector_ostream OS(Buf);
56720b57cec5SDimitry Andric 
56730b57cec5SDimitry Andric   StringRef Values = A.front().front().getString();
56740b57cec5SDimitry Andric   for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
56750b57cec5SDimitry Andric     MCAsmMacroArgument Arg;
56760b57cec5SDimitry Andric     Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1));
56770b57cec5SDimitry Andric 
56780b57cec5SDimitry Andric     // Note that the AtPseudoVariable is enabled for instantiations of .irpc.
56790b57cec5SDimitry Andric     // This is undocumented, but GAS seems to support it.
56800b57cec5SDimitry Andric     if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
56810b57cec5SDimitry Andric       return true;
56820b57cec5SDimitry Andric   }
56830b57cec5SDimitry Andric 
56840b57cec5SDimitry Andric   instantiateMacroLikeBody(M, DirectiveLoc, OS);
56850b57cec5SDimitry Andric 
56860b57cec5SDimitry Andric   return false;
56870b57cec5SDimitry Andric }
56880b57cec5SDimitry Andric 
56890b57cec5SDimitry Andric bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
56900b57cec5SDimitry Andric   if (ActiveMacros.empty())
56910b57cec5SDimitry Andric     return TokError("unmatched '.endr' directive");
56920b57cec5SDimitry Andric 
56930b57cec5SDimitry Andric   // The only .repl that should get here are the ones created by
56940b57cec5SDimitry Andric   // instantiateMacroLikeBody.
56950b57cec5SDimitry Andric   assert(getLexer().is(AsmToken::EndOfStatement));
56960b57cec5SDimitry Andric 
56970b57cec5SDimitry Andric   handleMacroExit();
56980b57cec5SDimitry Andric   return false;
56990b57cec5SDimitry Andric }
57000b57cec5SDimitry Andric 
57010b57cec5SDimitry Andric bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
57020b57cec5SDimitry Andric                                      size_t Len) {
57030b57cec5SDimitry Andric   const MCExpr *Value;
57040b57cec5SDimitry Andric   SMLoc ExprLoc = getLexer().getLoc();
57050b57cec5SDimitry Andric   if (parseExpression(Value))
57060b57cec5SDimitry Andric     return true;
57070b57cec5SDimitry Andric   const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
57080b57cec5SDimitry Andric   if (!MCE)
57090b57cec5SDimitry Andric     return Error(ExprLoc, "unexpected expression in _emit");
57100b57cec5SDimitry Andric   uint64_t IntValue = MCE->getValue();
57110b57cec5SDimitry Andric   if (!isUInt<8>(IntValue) && !isInt<8>(IntValue))
57120b57cec5SDimitry Andric     return Error(ExprLoc, "literal value out of range for directive");
57130b57cec5SDimitry Andric 
57140b57cec5SDimitry Andric   Info.AsmRewrites->emplace_back(AOK_Emit, IDLoc, Len);
57150b57cec5SDimitry Andric   return false;
57160b57cec5SDimitry Andric }
57170b57cec5SDimitry Andric 
57180b57cec5SDimitry Andric bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
57190b57cec5SDimitry Andric   const MCExpr *Value;
57200b57cec5SDimitry Andric   SMLoc ExprLoc = getLexer().getLoc();
57210b57cec5SDimitry Andric   if (parseExpression(Value))
57220b57cec5SDimitry Andric     return true;
57230b57cec5SDimitry Andric   const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
57240b57cec5SDimitry Andric   if (!MCE)
57250b57cec5SDimitry Andric     return Error(ExprLoc, "unexpected expression in align");
57260b57cec5SDimitry Andric   uint64_t IntValue = MCE->getValue();
57270b57cec5SDimitry Andric   if (!isPowerOf2_64(IntValue))
57280b57cec5SDimitry Andric     return Error(ExprLoc, "literal value not a power of two greater then zero");
57290b57cec5SDimitry Andric 
57300b57cec5SDimitry Andric   Info.AsmRewrites->emplace_back(AOK_Align, IDLoc, 5, Log2_64(IntValue));
57310b57cec5SDimitry Andric   return false;
57320b57cec5SDimitry Andric }
57330b57cec5SDimitry Andric 
57340b57cec5SDimitry Andric bool AsmParser::parseDirectivePrint(SMLoc DirectiveLoc) {
57350b57cec5SDimitry Andric   const AsmToken StrTok = getTok();
57360b57cec5SDimitry Andric   Lex();
57370b57cec5SDimitry Andric   if (StrTok.isNot(AsmToken::String) || StrTok.getString().front() != '"')
57380b57cec5SDimitry Andric     return Error(DirectiveLoc, "expected double quoted string after .print");
57390b57cec5SDimitry Andric   if (parseToken(AsmToken::EndOfStatement, "expected end of statement"))
57400b57cec5SDimitry Andric     return true;
57410b57cec5SDimitry Andric   llvm::outs() << StrTok.getStringContents() << '\n';
57420b57cec5SDimitry Andric   return false;
57430b57cec5SDimitry Andric }
57440b57cec5SDimitry Andric 
57450b57cec5SDimitry Andric bool AsmParser::parseDirectiveAddrsig() {
5746*5ffd83dbSDimitry Andric   getStreamer().emitAddrsig();
57470b57cec5SDimitry Andric   return false;
57480b57cec5SDimitry Andric }
57490b57cec5SDimitry Andric 
57500b57cec5SDimitry Andric bool AsmParser::parseDirectiveAddrsigSym() {
57510b57cec5SDimitry Andric   StringRef Name;
57520b57cec5SDimitry Andric   if (check(parseIdentifier(Name),
57530b57cec5SDimitry Andric             "expected identifier in '.addrsig_sym' directive"))
57540b57cec5SDimitry Andric     return true;
57550b57cec5SDimitry Andric   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
5756*5ffd83dbSDimitry Andric   getStreamer().emitAddrsigSym(Sym);
57570b57cec5SDimitry Andric   return false;
57580b57cec5SDimitry Andric }
57590b57cec5SDimitry Andric 
57600b57cec5SDimitry Andric // We are comparing pointers, but the pointers are relative to a single string.
57610b57cec5SDimitry Andric // Thus, this should always be deterministic.
57620b57cec5SDimitry Andric static int rewritesSort(const AsmRewrite *AsmRewriteA,
57630b57cec5SDimitry Andric                         const AsmRewrite *AsmRewriteB) {
57640b57cec5SDimitry Andric   if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
57650b57cec5SDimitry Andric     return -1;
57660b57cec5SDimitry Andric   if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
57670b57cec5SDimitry Andric     return 1;
57680b57cec5SDimitry Andric 
57690b57cec5SDimitry Andric   // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
57700b57cec5SDimitry Andric   // rewrite to the same location.  Make sure the SizeDirective rewrite is
57710b57cec5SDimitry Andric   // performed first, then the Imm/ImmPrefix and finally the Input/Output.  This
57720b57cec5SDimitry Andric   // ensures the sort algorithm is stable.
57730b57cec5SDimitry Andric   if (AsmRewritePrecedence[AsmRewriteA->Kind] >
57740b57cec5SDimitry Andric       AsmRewritePrecedence[AsmRewriteB->Kind])
57750b57cec5SDimitry Andric     return -1;
57760b57cec5SDimitry Andric 
57770b57cec5SDimitry Andric   if (AsmRewritePrecedence[AsmRewriteA->Kind] <
57780b57cec5SDimitry Andric       AsmRewritePrecedence[AsmRewriteB->Kind])
57790b57cec5SDimitry Andric     return 1;
57800b57cec5SDimitry Andric   llvm_unreachable("Unstable rewrite sort.");
57810b57cec5SDimitry Andric }
57820b57cec5SDimitry Andric 
57830b57cec5SDimitry Andric bool AsmParser::parseMSInlineAsm(
57840b57cec5SDimitry Andric     void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
57850b57cec5SDimitry Andric     unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
57860b57cec5SDimitry Andric     SmallVectorImpl<std::string> &Constraints,
57870b57cec5SDimitry Andric     SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
57880b57cec5SDimitry Andric     const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
57890b57cec5SDimitry Andric   SmallVector<void *, 4> InputDecls;
57900b57cec5SDimitry Andric   SmallVector<void *, 4> OutputDecls;
57910b57cec5SDimitry Andric   SmallVector<bool, 4> InputDeclsAddressOf;
57920b57cec5SDimitry Andric   SmallVector<bool, 4> OutputDeclsAddressOf;
57930b57cec5SDimitry Andric   SmallVector<std::string, 4> InputConstraints;
57940b57cec5SDimitry Andric   SmallVector<std::string, 4> OutputConstraints;
57950b57cec5SDimitry Andric   SmallVector<unsigned, 4> ClobberRegs;
57960b57cec5SDimitry Andric 
57970b57cec5SDimitry Andric   SmallVector<AsmRewrite, 4> AsmStrRewrites;
57980b57cec5SDimitry Andric 
57990b57cec5SDimitry Andric   // Prime the lexer.
58000b57cec5SDimitry Andric   Lex();
58010b57cec5SDimitry Andric 
58020b57cec5SDimitry Andric   // While we have input, parse each statement.
58030b57cec5SDimitry Andric   unsigned InputIdx = 0;
58040b57cec5SDimitry Andric   unsigned OutputIdx = 0;
58050b57cec5SDimitry Andric   while (getLexer().isNot(AsmToken::Eof)) {
58060b57cec5SDimitry Andric     // Parse curly braces marking block start/end
58070b57cec5SDimitry Andric     if (parseCurlyBlockScope(AsmStrRewrites))
58080b57cec5SDimitry Andric       continue;
58090b57cec5SDimitry Andric 
58100b57cec5SDimitry Andric     ParseStatementInfo Info(&AsmStrRewrites);
58110b57cec5SDimitry Andric     bool StatementErr = parseStatement(Info, &SI);
58120b57cec5SDimitry Andric 
58130b57cec5SDimitry Andric     if (StatementErr || Info.ParseError) {
58140b57cec5SDimitry Andric       // Emit pending errors if any exist.
58150b57cec5SDimitry Andric       printPendingErrors();
58160b57cec5SDimitry Andric       return true;
58170b57cec5SDimitry Andric     }
58180b57cec5SDimitry Andric 
58190b57cec5SDimitry Andric     // No pending error should exist here.
58200b57cec5SDimitry Andric     assert(!hasPendingError() && "unexpected error from parseStatement");
58210b57cec5SDimitry Andric 
58220b57cec5SDimitry Andric     if (Info.Opcode == ~0U)
58230b57cec5SDimitry Andric       continue;
58240b57cec5SDimitry Andric 
58250b57cec5SDimitry Andric     const MCInstrDesc &Desc = MII->get(Info.Opcode);
58260b57cec5SDimitry Andric 
58270b57cec5SDimitry Andric     // Build the list of clobbers, outputs and inputs.
58280b57cec5SDimitry Andric     for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
58290b57cec5SDimitry Andric       MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
58300b57cec5SDimitry Andric 
58310b57cec5SDimitry Andric       // Register operand.
58320b57cec5SDimitry Andric       if (Operand.isReg() && !Operand.needAddressOf() &&
58330b57cec5SDimitry Andric           !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
58340b57cec5SDimitry Andric         unsigned NumDefs = Desc.getNumDefs();
58350b57cec5SDimitry Andric         // Clobber.
58360b57cec5SDimitry Andric         if (NumDefs && Operand.getMCOperandNum() < NumDefs)
58370b57cec5SDimitry Andric           ClobberRegs.push_back(Operand.getReg());
58380b57cec5SDimitry Andric         continue;
58390b57cec5SDimitry Andric       }
58400b57cec5SDimitry Andric 
58410b57cec5SDimitry Andric       // Expr/Input or Output.
58420b57cec5SDimitry Andric       StringRef SymName = Operand.getSymName();
58430b57cec5SDimitry Andric       if (SymName.empty())
58440b57cec5SDimitry Andric         continue;
58450b57cec5SDimitry Andric 
58460b57cec5SDimitry Andric       void *OpDecl = Operand.getOpDecl();
58470b57cec5SDimitry Andric       if (!OpDecl)
58480b57cec5SDimitry Andric         continue;
58490b57cec5SDimitry Andric 
5850480093f4SDimitry Andric       StringRef Constraint = Operand.getConstraint();
5851480093f4SDimitry Andric       if (Operand.isImm()) {
5852480093f4SDimitry Andric         // Offset as immediate
5853480093f4SDimitry Andric         if (Operand.isOffsetOfLocal())
5854480093f4SDimitry Andric           Constraint = "r";
5855480093f4SDimitry Andric         else
5856480093f4SDimitry Andric           Constraint = "i";
5857480093f4SDimitry Andric       }
5858480093f4SDimitry Andric 
58590b57cec5SDimitry Andric       bool isOutput = (i == 1) && Desc.mayStore();
58600b57cec5SDimitry Andric       SMLoc Start = SMLoc::getFromPointer(SymName.data());
58610b57cec5SDimitry Andric       if (isOutput) {
58620b57cec5SDimitry Andric         ++InputIdx;
58630b57cec5SDimitry Andric         OutputDecls.push_back(OpDecl);
58640b57cec5SDimitry Andric         OutputDeclsAddressOf.push_back(Operand.needAddressOf());
5865480093f4SDimitry Andric         OutputConstraints.push_back(("=" + Constraint).str());
58660b57cec5SDimitry Andric         AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size());
58670b57cec5SDimitry Andric       } else {
58680b57cec5SDimitry Andric         InputDecls.push_back(OpDecl);
58690b57cec5SDimitry Andric         InputDeclsAddressOf.push_back(Operand.needAddressOf());
5870480093f4SDimitry Andric         InputConstraints.push_back(Constraint.str());
5871480093f4SDimitry Andric         if (Desc.OpInfo[i - 1].isBranchTarget())
5872480093f4SDimitry Andric           AsmStrRewrites.emplace_back(AOK_CallInput, Start, SymName.size());
5873480093f4SDimitry Andric         else
58740b57cec5SDimitry Andric           AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size());
58750b57cec5SDimitry Andric       }
58760b57cec5SDimitry Andric     }
58770b57cec5SDimitry Andric 
58780b57cec5SDimitry Andric     // Consider implicit defs to be clobbers.  Think of cpuid and push.
58790b57cec5SDimitry Andric     ArrayRef<MCPhysReg> ImpDefs(Desc.getImplicitDefs(),
58800b57cec5SDimitry Andric                                 Desc.getNumImplicitDefs());
58810b57cec5SDimitry Andric     ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
58820b57cec5SDimitry Andric   }
58830b57cec5SDimitry Andric 
58840b57cec5SDimitry Andric   // Set the number of Outputs and Inputs.
58850b57cec5SDimitry Andric   NumOutputs = OutputDecls.size();
58860b57cec5SDimitry Andric   NumInputs = InputDecls.size();
58870b57cec5SDimitry Andric 
58880b57cec5SDimitry Andric   // Set the unique clobbers.
58890b57cec5SDimitry Andric   array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
58900b57cec5SDimitry Andric   ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
58910b57cec5SDimitry Andric                     ClobberRegs.end());
58920b57cec5SDimitry Andric   Clobbers.assign(ClobberRegs.size(), std::string());
58930b57cec5SDimitry Andric   for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
58940b57cec5SDimitry Andric     raw_string_ostream OS(Clobbers[I]);
58950b57cec5SDimitry Andric     IP->printRegName(OS, ClobberRegs[I]);
58960b57cec5SDimitry Andric   }
58970b57cec5SDimitry Andric 
58980b57cec5SDimitry Andric   // Merge the various outputs and inputs.  Output are expected first.
58990b57cec5SDimitry Andric   if (NumOutputs || NumInputs) {
59000b57cec5SDimitry Andric     unsigned NumExprs = NumOutputs + NumInputs;
59010b57cec5SDimitry Andric     OpDecls.resize(NumExprs);
59020b57cec5SDimitry Andric     Constraints.resize(NumExprs);
59030b57cec5SDimitry Andric     for (unsigned i = 0; i < NumOutputs; ++i) {
59040b57cec5SDimitry Andric       OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
59050b57cec5SDimitry Andric       Constraints[i] = OutputConstraints[i];
59060b57cec5SDimitry Andric     }
59070b57cec5SDimitry Andric     for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
59080b57cec5SDimitry Andric       OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
59090b57cec5SDimitry Andric       Constraints[j] = InputConstraints[i];
59100b57cec5SDimitry Andric     }
59110b57cec5SDimitry Andric   }
59120b57cec5SDimitry Andric 
59130b57cec5SDimitry Andric   // Build the IR assembly string.
59140b57cec5SDimitry Andric   std::string AsmStringIR;
59150b57cec5SDimitry Andric   raw_string_ostream OS(AsmStringIR);
59160b57cec5SDimitry Andric   StringRef ASMString =
59170b57cec5SDimitry Andric       SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
59180b57cec5SDimitry Andric   const char *AsmStart = ASMString.begin();
59190b57cec5SDimitry Andric   const char *AsmEnd = ASMString.end();
59200b57cec5SDimitry Andric   array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
5921480093f4SDimitry Andric   for (auto it = AsmStrRewrites.begin(); it != AsmStrRewrites.end(); ++it) {
5922480093f4SDimitry Andric     const AsmRewrite &AR = *it;
5923480093f4SDimitry Andric     // Check if this has already been covered by another rewrite...
5924480093f4SDimitry Andric     if (AR.Done)
5925480093f4SDimitry Andric       continue;
59260b57cec5SDimitry Andric     AsmRewriteKind Kind = AR.Kind;
59270b57cec5SDimitry Andric 
59280b57cec5SDimitry Andric     const char *Loc = AR.Loc.getPointer();
59290b57cec5SDimitry Andric     assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
59300b57cec5SDimitry Andric 
59310b57cec5SDimitry Andric     // Emit everything up to the immediate/expression.
59320b57cec5SDimitry Andric     if (unsigned Len = Loc - AsmStart)
59330b57cec5SDimitry Andric       OS << StringRef(AsmStart, Len);
59340b57cec5SDimitry Andric 
59350b57cec5SDimitry Andric     // Skip the original expression.
59360b57cec5SDimitry Andric     if (Kind == AOK_Skip) {
59370b57cec5SDimitry Andric       AsmStart = Loc + AR.Len;
59380b57cec5SDimitry Andric       continue;
59390b57cec5SDimitry Andric     }
59400b57cec5SDimitry Andric 
59410b57cec5SDimitry Andric     unsigned AdditionalSkip = 0;
59420b57cec5SDimitry Andric     // Rewrite expressions in $N notation.
59430b57cec5SDimitry Andric     switch (Kind) {
59440b57cec5SDimitry Andric     default:
59450b57cec5SDimitry Andric       break;
59460b57cec5SDimitry Andric     case AOK_IntelExpr:
59470b57cec5SDimitry Andric       assert(AR.IntelExp.isValid() && "cannot write invalid intel expression");
59480b57cec5SDimitry Andric       if (AR.IntelExp.NeedBracs)
59490b57cec5SDimitry Andric         OS << "[";
59500b57cec5SDimitry Andric       if (AR.IntelExp.hasBaseReg())
59510b57cec5SDimitry Andric         OS << AR.IntelExp.BaseReg;
59520b57cec5SDimitry Andric       if (AR.IntelExp.hasIndexReg())
59530b57cec5SDimitry Andric         OS << (AR.IntelExp.hasBaseReg() ? " + " : "")
59540b57cec5SDimitry Andric            << AR.IntelExp.IndexReg;
59550b57cec5SDimitry Andric       if (AR.IntelExp.Scale > 1)
59560b57cec5SDimitry Andric         OS << " * $$" << AR.IntelExp.Scale;
5957480093f4SDimitry Andric       if (AR.IntelExp.hasOffset()) {
5958480093f4SDimitry Andric         if (AR.IntelExp.hasRegs())
5959480093f4SDimitry Andric           OS << " + ";
5960480093f4SDimitry Andric         // Fuse this rewrite with a rewrite of the offset name, if present.
5961480093f4SDimitry Andric         StringRef OffsetName = AR.IntelExp.OffsetName;
5962480093f4SDimitry Andric         SMLoc OffsetLoc = SMLoc::getFromPointer(AR.IntelExp.OffsetName.data());
5963480093f4SDimitry Andric         size_t OffsetLen = OffsetName.size();
5964480093f4SDimitry Andric         auto rewrite_it = std::find_if(
5965480093f4SDimitry Andric             it, AsmStrRewrites.end(), [&](const AsmRewrite &FusingAR) {
5966480093f4SDimitry Andric               return FusingAR.Loc == OffsetLoc && FusingAR.Len == OffsetLen &&
5967480093f4SDimitry Andric                      (FusingAR.Kind == AOK_Input ||
5968480093f4SDimitry Andric                       FusingAR.Kind == AOK_CallInput);
5969480093f4SDimitry Andric             });
5970480093f4SDimitry Andric         if (rewrite_it == AsmStrRewrites.end()) {
5971480093f4SDimitry Andric           OS << "offset " << OffsetName;
5972480093f4SDimitry Andric         } else if (rewrite_it->Kind == AOK_CallInput) {
5973480093f4SDimitry Andric           OS << "${" << InputIdx++ << ":P}";
5974480093f4SDimitry Andric           rewrite_it->Done = true;
5975480093f4SDimitry Andric         } else {
5976480093f4SDimitry Andric           OS << '$' << InputIdx++;
5977480093f4SDimitry Andric           rewrite_it->Done = true;
5978480093f4SDimitry Andric         }
5979480093f4SDimitry Andric       }
5980480093f4SDimitry Andric       if (AR.IntelExp.Imm || AR.IntelExp.emitImm())
5981480093f4SDimitry Andric         OS << (AR.IntelExp.emitImm() ? "$$" : " + $$") << AR.IntelExp.Imm;
59820b57cec5SDimitry Andric       if (AR.IntelExp.NeedBracs)
59830b57cec5SDimitry Andric         OS << "]";
59840b57cec5SDimitry Andric       break;
59850b57cec5SDimitry Andric     case AOK_Label:
59860b57cec5SDimitry Andric       OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
59870b57cec5SDimitry Andric       break;
59880b57cec5SDimitry Andric     case AOK_Input:
59890b57cec5SDimitry Andric       OS << '$' << InputIdx++;
59900b57cec5SDimitry Andric       break;
5991480093f4SDimitry Andric     case AOK_CallInput:
5992480093f4SDimitry Andric       OS << "${" << InputIdx++ << ":P}";
5993480093f4SDimitry Andric       break;
59940b57cec5SDimitry Andric     case AOK_Output:
59950b57cec5SDimitry Andric       OS << '$' << OutputIdx++;
59960b57cec5SDimitry Andric       break;
59970b57cec5SDimitry Andric     case AOK_SizeDirective:
59980b57cec5SDimitry Andric       switch (AR.Val) {
59990b57cec5SDimitry Andric       default: break;
60000b57cec5SDimitry Andric       case 8:  OS << "byte ptr "; break;
60010b57cec5SDimitry Andric       case 16: OS << "word ptr "; break;
60020b57cec5SDimitry Andric       case 32: OS << "dword ptr "; break;
60030b57cec5SDimitry Andric       case 64: OS << "qword ptr "; break;
60040b57cec5SDimitry Andric       case 80: OS << "xword ptr "; break;
60050b57cec5SDimitry Andric       case 128: OS << "xmmword ptr "; break;
60060b57cec5SDimitry Andric       case 256: OS << "ymmword ptr "; break;
60070b57cec5SDimitry Andric       }
60080b57cec5SDimitry Andric       break;
60090b57cec5SDimitry Andric     case AOK_Emit:
60100b57cec5SDimitry Andric       OS << ".byte";
60110b57cec5SDimitry Andric       break;
60120b57cec5SDimitry Andric     case AOK_Align: {
60130b57cec5SDimitry Andric       // MS alignment directives are measured in bytes. If the native assembler
60140b57cec5SDimitry Andric       // measures alignment in bytes, we can pass it straight through.
60150b57cec5SDimitry Andric       OS << ".align";
60160b57cec5SDimitry Andric       if (getContext().getAsmInfo()->getAlignmentIsInBytes())
60170b57cec5SDimitry Andric         break;
60180b57cec5SDimitry Andric 
60190b57cec5SDimitry Andric       // Alignment is in log2 form, so print that instead and skip the original
60200b57cec5SDimitry Andric       // immediate.
60210b57cec5SDimitry Andric       unsigned Val = AR.Val;
60220b57cec5SDimitry Andric       OS << ' ' << Val;
60230b57cec5SDimitry Andric       assert(Val < 10 && "Expected alignment less then 2^10.");
60240b57cec5SDimitry Andric       AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
60250b57cec5SDimitry Andric       break;
60260b57cec5SDimitry Andric     }
60270b57cec5SDimitry Andric     case AOK_EVEN:
60280b57cec5SDimitry Andric       OS << ".even";
60290b57cec5SDimitry Andric       break;
60300b57cec5SDimitry Andric     case AOK_EndOfStatement:
60310b57cec5SDimitry Andric       OS << "\n\t";
60320b57cec5SDimitry Andric       break;
60330b57cec5SDimitry Andric     }
60340b57cec5SDimitry Andric 
60350b57cec5SDimitry Andric     // Skip the original expression.
60360b57cec5SDimitry Andric     AsmStart = Loc + AR.Len + AdditionalSkip;
60370b57cec5SDimitry Andric   }
60380b57cec5SDimitry Andric 
60390b57cec5SDimitry Andric   // Emit the remainder of the asm string.
60400b57cec5SDimitry Andric   if (AsmStart != AsmEnd)
60410b57cec5SDimitry Andric     OS << StringRef(AsmStart, AsmEnd - AsmStart);
60420b57cec5SDimitry Andric 
60430b57cec5SDimitry Andric   AsmString = OS.str();
60440b57cec5SDimitry Andric   return false;
60450b57cec5SDimitry Andric }
60460b57cec5SDimitry Andric 
60470b57cec5SDimitry Andric namespace llvm {
60480b57cec5SDimitry Andric namespace MCParserUtils {
60490b57cec5SDimitry Andric 
60500b57cec5SDimitry Andric /// Returns whether the given symbol is used anywhere in the given expression,
60510b57cec5SDimitry Andric /// or subexpressions.
60520b57cec5SDimitry Andric static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) {
60530b57cec5SDimitry Andric   switch (Value->getKind()) {
60540b57cec5SDimitry Andric   case MCExpr::Binary: {
60550b57cec5SDimitry Andric     const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
60560b57cec5SDimitry Andric     return isSymbolUsedInExpression(Sym, BE->getLHS()) ||
60570b57cec5SDimitry Andric            isSymbolUsedInExpression(Sym, BE->getRHS());
60580b57cec5SDimitry Andric   }
60590b57cec5SDimitry Andric   case MCExpr::Target:
60600b57cec5SDimitry Andric   case MCExpr::Constant:
60610b57cec5SDimitry Andric     return false;
60620b57cec5SDimitry Andric   case MCExpr::SymbolRef: {
60630b57cec5SDimitry Andric     const MCSymbol &S =
60640b57cec5SDimitry Andric         static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
60650b57cec5SDimitry Andric     if (S.isVariable())
60660b57cec5SDimitry Andric       return isSymbolUsedInExpression(Sym, S.getVariableValue());
60670b57cec5SDimitry Andric     return &S == Sym;
60680b57cec5SDimitry Andric   }
60690b57cec5SDimitry Andric   case MCExpr::Unary:
60700b57cec5SDimitry Andric     return isSymbolUsedInExpression(
60710b57cec5SDimitry Andric         Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
60720b57cec5SDimitry Andric   }
60730b57cec5SDimitry Andric 
60740b57cec5SDimitry Andric   llvm_unreachable("Unknown expr kind!");
60750b57cec5SDimitry Andric }
60760b57cec5SDimitry Andric 
60770b57cec5SDimitry Andric bool parseAssignmentExpression(StringRef Name, bool allow_redef,
60780b57cec5SDimitry Andric                                MCAsmParser &Parser, MCSymbol *&Sym,
60790b57cec5SDimitry Andric                                const MCExpr *&Value) {
60800b57cec5SDimitry Andric 
60810b57cec5SDimitry Andric   // FIXME: Use better location, we should use proper tokens.
60820b57cec5SDimitry Andric   SMLoc EqualLoc = Parser.getTok().getLoc();
60830b57cec5SDimitry Andric   if (Parser.parseExpression(Value))
60840b57cec5SDimitry Andric     return Parser.TokError("missing expression");
60850b57cec5SDimitry Andric 
60860b57cec5SDimitry Andric   // Note: we don't count b as used in "a = b". This is to allow
60870b57cec5SDimitry Andric   // a = b
60880b57cec5SDimitry Andric   // b = c
60890b57cec5SDimitry Andric 
60900b57cec5SDimitry Andric   if (Parser.parseToken(AsmToken::EndOfStatement))
60910b57cec5SDimitry Andric     return true;
60920b57cec5SDimitry Andric 
60930b57cec5SDimitry Andric   // Validate that the LHS is allowed to be a variable (either it has not been
60940b57cec5SDimitry Andric   // used as a symbol, or it is an absolute symbol).
60950b57cec5SDimitry Andric   Sym = Parser.getContext().lookupSymbol(Name);
60960b57cec5SDimitry Andric   if (Sym) {
60970b57cec5SDimitry Andric     // Diagnose assignment to a label.
60980b57cec5SDimitry Andric     //
60990b57cec5SDimitry Andric     // FIXME: Diagnostics. Note the location of the definition as a label.
61000b57cec5SDimitry Andric     // FIXME: Diagnose assignment to protected identifier (e.g., register name).
61010b57cec5SDimitry Andric     if (isSymbolUsedInExpression(Sym, Value))
61020b57cec5SDimitry Andric       return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'");
61030b57cec5SDimitry Andric     else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() &&
61040b57cec5SDimitry Andric              !Sym->isVariable())
61050b57cec5SDimitry Andric       ; // Allow redefinitions of undefined symbols only used in directives.
61060b57cec5SDimitry Andric     else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
61070b57cec5SDimitry Andric       ; // Allow redefinitions of variables that haven't yet been used.
61080b57cec5SDimitry Andric     else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
61090b57cec5SDimitry Andric       return Parser.Error(EqualLoc, "redefinition of '" + Name + "'");
61100b57cec5SDimitry Andric     else if (!Sym->isVariable())
61110b57cec5SDimitry Andric       return Parser.Error(EqualLoc, "invalid assignment to '" + Name + "'");
61120b57cec5SDimitry Andric     else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
61130b57cec5SDimitry Andric       return Parser.Error(EqualLoc,
61140b57cec5SDimitry Andric                           "invalid reassignment of non-absolute variable '" +
61150b57cec5SDimitry Andric                               Name + "'");
61160b57cec5SDimitry Andric   } else if (Name == ".") {
61170b57cec5SDimitry Andric     Parser.getStreamer().emitValueToOffset(Value, 0, EqualLoc);
61180b57cec5SDimitry Andric     return false;
61190b57cec5SDimitry Andric   } else
61200b57cec5SDimitry Andric     Sym = Parser.getContext().getOrCreateSymbol(Name);
61210b57cec5SDimitry Andric 
61220b57cec5SDimitry Andric   Sym->setRedefinable(allow_redef);
61230b57cec5SDimitry Andric 
61240b57cec5SDimitry Andric   return false;
61250b57cec5SDimitry Andric }
61260b57cec5SDimitry Andric 
61270b57cec5SDimitry Andric } // end namespace MCParserUtils
61280b57cec5SDimitry Andric } // end namespace llvm
61290b57cec5SDimitry Andric 
61300b57cec5SDimitry Andric /// Create an MCAsmParser instance.
61310b57cec5SDimitry Andric MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
61320b57cec5SDimitry Andric                                      MCStreamer &Out, const MCAsmInfo &MAI,
61330b57cec5SDimitry Andric                                      unsigned CB) {
61340b57cec5SDimitry Andric   return new AsmParser(SM, C, Out, MAI, CB);
61350b57cec5SDimitry Andric }
6136