blob: bc98967eaa74f87cdf58fb62a9c941249eef7b09 [file] [log] [blame]
Chris Lattnerb0133452009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar2af16532010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Chad Rosiereb5c1682013-02-13 18:38:58 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "llvm/ADT/SmallString.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000017#include "llvm/ADT/StringMap.h"
Daniel Dunbareb6bb322009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Evan Cheng11424442011-07-26 00:24:13 +000021#include "llvm/MC/MCDwarf.h"
Daniel Dunbar115e4d62009-08-31 08:06:59 +000022#include "llvm/MC/MCExpr.h"
Chad Rosier8bce6642012-10-18 15:49:34 +000023#include "llvm/MC/MCInstPrinter.h"
24#include "llvm/MC/MCInstrInfo.h"
Rafael Espindolae28610d2013-12-09 20:26:40 +000025#include "llvm/MC/MCObjectFileInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000026#include "llvm/MC/MCParser/AsmCond.h"
27#include "llvm/MC/MCParser/AsmLexer.h"
28#include "llvm/MC/MCParser/MCAsmParser.h"
29#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Cheng76792992011-07-20 05:58:47 +000030#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000031#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000032#include "llvm/MC/MCStreamer.h"
Daniel Dunbarae7ac012009-06-29 23:43:14 +000033#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000034#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000035#include "llvm/Support/CommandLine.h"
Benjamin Kramer4efe5062012-01-28 15:28:41 +000036#include "llvm/Support/ErrorHandling.h"
Jim Grosbach76346c32011-06-29 16:05:14 +000037#include "llvm/Support/MathExtras.h"
Kevin Enderbye233dda2010-06-28 21:45:58 +000038#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000039#include "llvm/Support/SourceMgr.h"
Chris Lattner36e02122009-06-21 20:54:55 +000040#include "llvm/Support/raw_ostream.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000041#include <cctype>
Benjamin Kramerd59664f2014-04-29 23:26:49 +000042#include <deque>
Chad Rosier8bce6642012-10-18 15:49:34 +000043#include <set>
44#include <string>
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000045#include <vector>
Chris Lattnerb0133452009-06-21 20:16:42 +000046using namespace llvm;
47
Eric Christophera7c32732012-12-18 00:30:54 +000048MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewyckyac612272012-10-19 07:00:09 +000049
Daniel Dunbar86033402010-07-12 17:54:38 +000050namespace {
Eli Benderskya313ae62013-01-16 18:56:50 +000051/// \brief Helper types for tracking macro definitions.
52typedef std::vector<AsmToken> MCAsmMacroArgument;
53typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000054
55struct MCAsmMacroParameter {
56 StringRef Name;
57 MCAsmMacroArgument Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000058 bool Required;
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000059 bool Vararg;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000060
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000061 MCAsmMacroParameter() : Required(false), Vararg(false) {}
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000062};
63
Eli Benderskya313ae62013-01-16 18:56:50 +000064typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
65
66struct MCAsmMacro {
67 StringRef Name;
68 StringRef Body;
69 MCAsmMacroParameters Parameters;
70
71public:
Benjamin Kramerd31aaf12014-02-09 17:13:11 +000072 MCAsmMacro(StringRef N, StringRef B, ArrayRef<MCAsmMacroParameter> P) :
Eli Benderskya313ae62013-01-16 18:56:50 +000073 Name(N), Body(B), Parameters(P) {}
Eli Benderskya313ae62013-01-16 18:56:50 +000074};
75
Daniel Dunbar43235712010-07-18 18:54:11 +000076/// \brief Helper class for storing information about an active macro
77/// instantiation.
78struct MacroInstantiation {
Daniel Dunbar43235712010-07-18 18:54:11 +000079 /// The location of the instantiation.
80 SMLoc InstantiationLoc;
81
Daniel Dunbar40f1d852012-12-01 01:38:48 +000082 /// The buffer where parsing should resume upon instantiation completion.
83 int ExitBuffer;
84
Daniel Dunbar43235712010-07-18 18:54:11 +000085 /// The location where parsing should resume upon instantiation completion.
86 SMLoc ExitLoc;
87
Nico Weber155dccd12014-07-24 17:08:39 +000088 /// The depth of TheCondStack at the start of the instantiation.
89 size_t CondStackDepth;
90
Daniel Dunbar43235712010-07-18 18:54:11 +000091public:
Rafael Espindolaf43a94e2014-08-17 22:48:55 +000092 MacroInstantiation(SMLoc IL, int EB, SMLoc EL, StringRef I,
Nico Weber155dccd12014-07-24 17:08:39 +000093 size_t CondStackDepth);
Daniel Dunbar43235712010-07-18 18:54:11 +000094};
95
Eli Friedman0f4871d2012-10-22 23:58:19 +000096struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +000097 /// \brief The parsed operands from the last parsed statement.
David Blaikie960ea3f2014-06-08 16:18:35 +000098 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
Eli Friedman0f4871d2012-10-22 23:58:19 +000099
Jim Grosbach4b905842013-09-20 23:08:21 +0000100 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000101 unsigned Opcode;
102
Jim Grosbach4b905842013-09-20 23:08:21 +0000103 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000104 bool ParseError;
105
Eli Friedman0f4871d2012-10-22 23:58:19 +0000106 SmallVectorImpl<AsmRewrite> *AsmRewrites;
107
Craig Topper353eda42014-04-24 06:44:33 +0000108 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(nullptr) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000109 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000110 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000111};
112
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000113/// \brief The concrete assembly parser instance.
114class AsmParser : public MCAsmParser {
Craig Topper2e6644c2012-09-15 16:23:52 +0000115 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
116 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000117private:
118 AsmLexer Lexer;
119 MCContext &Ctx;
120 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000121 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000122 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000123 SourceMgr::DiagHandlerTy SavedDiagHandler;
124 void *SavedDiagContext;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000125 MCAsmParserExtension *PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000126
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000127 /// This is the current buffer index we're lexing from as managed by the
128 /// SourceMgr object.
Alp Tokera55b95b2014-07-06 10:33:31 +0000129 unsigned CurBuffer;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000130
131 AsmCond TheCondState;
132 std::vector<AsmCond> TheCondStack;
133
Jim Grosbach4b905842013-09-20 23:08:21 +0000134 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000135 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000136 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000137 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000138
Jim Grosbach4b905842013-09-20 23:08:21 +0000139 /// \brief Map of currently defined macros.
Eli Bendersky38274122013-01-14 23:22:36 +0000140 StringMap<MCAsmMacro*> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000141
Jim Grosbach4b905842013-09-20 23:08:21 +0000142 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000143 std::vector<MacroInstantiation*> ActiveMacros;
144
Jim Grosbach4b905842013-09-20 23:08:21 +0000145 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000146 std::deque<MCAsmMacro> MacroLikeBodies;
147
Daniel Dunbar828984f2010-07-18 18:38:02 +0000148 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000149 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000150
Daniel Dunbar43325c42010-09-09 22:42:56 +0000151 /// Flag tracking whether any errors have been encountered.
152 unsigned HadError : 1;
153
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000154 /// The values from the last parsed cpp hash file line comment if any.
155 StringRef CppHashFilename;
156 int64_t CppHashLineNumber;
157 SMLoc CppHashLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000158 unsigned CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000159 /// When generating dwarf for assembly source files we need to calculate the
160 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000161 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000162 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
163 SMLoc LastQueryIDLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000164 unsigned LastQueryBuffer;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000165 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000166
Devang Patela173ee52012-01-31 18:14:05 +0000167 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
168 unsigned AssemblerDialect;
169
Jim Grosbach4b905842013-09-20 23:08:21 +0000170 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000171 bool IsDarwin;
172
Jim Grosbach4b905842013-09-20 23:08:21 +0000173 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000174 bool ParsingInlineAsm;
175
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000176public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000177 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000178 const MCAsmInfo &MAI);
Craig Topper5f96ca52012-08-29 05:48:09 +0000179 virtual ~AsmParser();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000180
Craig Topper59be68f2014-03-08 07:14:16 +0000181 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000182
Craig Topper59be68f2014-03-08 07:14:16 +0000183 void addDirectiveHandler(StringRef Directive,
184 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000185 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000186 }
187
188public:
189 /// @name MCAsmParser Interface
190 /// {
191
Craig Topper59be68f2014-03-08 07:14:16 +0000192 SourceMgr &getSourceManager() override { return SrcMgr; }
193 MCAsmLexer &getLexer() override { return Lexer; }
194 MCContext &getContext() override { return Ctx; }
195 MCStreamer &getStreamer() override { return Out; }
196 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000197 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000198 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000199 else
200 return AssemblerDialect;
201 }
Craig Topper59be68f2014-03-08 07:14:16 +0000202 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000203 AssemblerDialect = i;
204 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000205
Craig Topper59be68f2014-03-08 07:14:16 +0000206 void Note(SMLoc L, const Twine &Msg,
207 ArrayRef<SMRange> Ranges = None) override;
208 bool Warning(SMLoc L, const Twine &Msg,
209 ArrayRef<SMRange> Ranges = None) override;
210 bool Error(SMLoc L, const Twine &Msg,
211 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000212
Craig Topper59be68f2014-03-08 07:14:16 +0000213 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000214
Craig Topper59be68f2014-03-08 07:14:16 +0000215 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
216 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000217
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000218 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000219 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000220 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000221 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000222 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000223 const MCInstrInfo *MII, const MCInstPrinter *IP,
224 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000225
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000226 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000227 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
228 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
229 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
230 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000231
Jim Grosbach4b905842013-09-20 23:08:21 +0000232 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000233 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000234 bool parseIdentifier(StringRef &Res) override;
235 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000236
Craig Topper59be68f2014-03-08 07:14:16 +0000237 void checkForValidSection() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000238 /// }
239
240private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000241
Jim Grosbach4b905842013-09-20 23:08:21 +0000242 bool parseStatement(ParseStatementInfo &Info);
243 void eatToEndOfLine();
244 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000245
Jim Grosbach4b905842013-09-20 23:08:21 +0000246 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000247 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000248 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000249 ArrayRef<MCAsmMacroParameter> Parameters,
250 ArrayRef<MCAsmMacroArgument> A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000251 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000252
Eli Benderskya313ae62013-01-16 18:56:50 +0000253 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000254 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000255
256 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000257 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000258
259 /// \brief Lookup a previously defined macro.
260 /// \param Name Macro name.
261 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000262 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000263
264 /// \brief Define a new macro with the given name and information.
Jim Grosbach4b905842013-09-20 23:08:21 +0000265 void defineMacro(StringRef Name, const MCAsmMacro& Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000266
267 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000268 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000269
270 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000271 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000272
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000273 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000274 ///
275 /// \param M The macro.
276 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000277 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000278
279 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000280 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000281
David Majnemer91fc4c22014-01-29 18:57:46 +0000282 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000283 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000284
285 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000286 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000287
Jim Grosbach4b905842013-09-20 23:08:21 +0000288 void printMacroInstantiations();
289 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000290 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000291 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000292 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000293 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000294
Jim Grosbach4b905842013-09-20 23:08:21 +0000295 /// \brief Enter the specified file. This returns true on failure.
296 bool enterIncludeFile(const std::string &Filename);
297
298 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000299 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000300 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000301
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000302 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000303 /// current token is not set; clients should ensure Lex() is called
304 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000305 ///
Alp Tokera55b95b2014-07-06 10:33:31 +0000306 /// \param InBuffer If not 0, should be the known buffer id that contains the
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000307 /// location.
Alp Tokera55b95b2014-07-06 10:33:31 +0000308 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
Daniel Dunbar43235712010-07-18 18:54:11 +0000309
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000310 /// \brief Parse up to the end of statement and a return the contents from the
311 /// current token until the end of the statement; the current token on exit
312 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000313 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000314
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000315 /// \brief Parse until the end of a statement or a comma is encountered,
316 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000317 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000318
Jim Grosbach4b905842013-09-20 23:08:21 +0000319 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000320 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000321
Jim Grosbach4b905842013-09-20 23:08:21 +0000322 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
323 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
324 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000325
Jim Grosbach4b905842013-09-20 23:08:21 +0000326 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000327
Eli Bendersky17233942013-01-15 22:59:42 +0000328 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000329 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000330 DK_NO_DIRECTIVE, // Placeholder
331 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
David Woodhoused6de0d92014-02-01 16:20:59 +0000332 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
333 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000334 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000335 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000336 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000337 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
338 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
339 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
340 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000341 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
342 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
343 DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000344 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
345 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
346 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
347 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
348 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
349 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000350 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Nico Weber155dccd12014-07-24 17:08:39 +0000351 DK_MACROS_ON, DK_MACROS_OFF,
352 DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000353 DK_SLEB128, DK_ULEB128,
Nico Weber404012b2014-07-24 16:26:06 +0000354 DK_ERR, DK_ERROR, DK_WARNING,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000355 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000356 };
357
Jim Grosbach4b905842013-09-20 23:08:21 +0000358 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000359 /// directives parsed by this class.
360 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000361
362 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000363 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
364 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000365 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000366 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
367 bool parseDirectiveFill(); // ".fill"
368 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000369 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000370 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
371 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000372 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000373 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000374
Eli Bendersky17233942013-01-15 22:59:42 +0000375 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000376 bool parseDirectiveFile(SMLoc DirectiveLoc);
377 bool parseDirectiveLine();
378 bool parseDirectiveLoc();
379 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000380
381 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000382 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000383 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000384 bool parseDirectiveCFISections();
385 bool parseDirectiveCFIStartProc();
386 bool parseDirectiveCFIEndProc();
387 bool parseDirectiveCFIDefCfaOffset();
388 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
389 bool parseDirectiveCFIAdjustCfaOffset();
390 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
391 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
392 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
393 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
394 bool parseDirectiveCFIRememberState();
395 bool parseDirectiveCFIRestoreState();
396 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
397 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
398 bool parseDirectiveCFIEscape();
399 bool parseDirectiveCFISignalFrame();
400 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000401
402 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000403 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
Nico Weber155dccd12014-07-24 17:08:39 +0000404 bool parseDirectiveExitMacro(StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000405 bool parseDirectiveEndMacro(StringRef Directive);
406 bool parseDirectiveMacro(SMLoc DirectiveLoc);
407 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000408
Eli Benderskyf483ff92012-12-20 19:05:53 +0000409 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000410 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000411 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000412 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000413 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000414 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000415
Eli Bendersky17233942013-01-15 22:59:42 +0000416 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000417 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000418
419 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000420 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000421
Jim Grosbach4b905842013-09-20 23:08:21 +0000422 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000423 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000424 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000425
Jim Grosbach4b905842013-09-20 23:08:21 +0000426 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000427
Jim Grosbach4b905842013-09-20 23:08:21 +0000428 bool parseDirectiveAbort(); // ".abort"
429 bool parseDirectiveInclude(); // ".include"
430 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000431
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000432 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
433 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000434 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000435 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000436 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000437 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +0000438 // ".ifeqs"
439 bool parseDirectiveIfeqs(SMLoc DirectiveLoc);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000440 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000441 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
442 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
443 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
444 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000445 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000446
Jim Grosbach4b905842013-09-20 23:08:21 +0000447 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000448 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000449
Rafael Espindola34b9c512012-06-03 23:57:14 +0000450 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000451 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
452 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000453 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000454 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000455 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
456 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
457 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000458
Chad Rosierc7f552c2013-02-12 21:33:51 +0000459 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000460 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000461 size_t Len);
462
463 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000464 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000465
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000466 // "end"
467 bool parseDirectiveEnd(SMLoc DirectiveLoc);
468
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000469 // ".err" or ".error"
470 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000471
Nico Weber404012b2014-07-24 16:26:06 +0000472 // ".warning"
473 bool parseDirectiveWarning(SMLoc DirectiveLoc);
474
Eli Bendersky17233942013-01-15 22:59:42 +0000475 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000476};
Daniel Dunbar86033402010-07-12 17:54:38 +0000477}
478
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000479namespace llvm {
480
481extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000482extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000483extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000484
485}
486
Chris Lattnerc35681b2010-01-19 19:46:13 +0000487enum { DEFAULT_ADDRSPACE = 0 };
488
Jim Grosbach4b905842013-09-20 23:08:21 +0000489AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
490 const MCAsmInfo &_MAI)
491 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Alp Tokera55b95b2014-07-06 10:33:31 +0000492 PlatformParser(nullptr), CurBuffer(_SM.getMainFileID()),
493 MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0),
494 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000495 // Save the old handler.
496 SavedDiagHandler = SrcMgr.getDiagHandler();
497 SavedDiagContext = SrcMgr.getDiagContext();
498 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000499 SrcMgr.setDiagHandler(DiagHandler, this);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000500 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar86033402010-07-12 17:54:38 +0000501
Daniel Dunbarc5011082010-07-12 18:12:02 +0000502 // Initialize the platform / file format parser.
Rafael Espindolae28610d2013-12-09 20:26:40 +0000503 switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
504 case MCObjectFileInfo::IsCOFF:
505 PlatformParser = createCOFFAsmParser();
506 PlatformParser->Initialize(*this);
507 break;
508 case MCObjectFileInfo::IsMachO:
509 PlatformParser = createDarwinAsmParser();
510 PlatformParser->Initialize(*this);
511 IsDarwin = true;
512 break;
513 case MCObjectFileInfo::IsELF:
514 PlatformParser = createELFAsmParser();
515 PlatformParser->Initialize(*this);
516 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000517 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000518
Eli Bendersky17233942013-01-15 22:59:42 +0000519 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000520}
521
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000522AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000523 assert((HadError || ActiveMacros.empty()) &&
524 "Unexpected active macro instantiation!");
Daniel Dunbarb759a132010-07-29 01:51:55 +0000525
526 // Destroy any macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000527 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
528 ie = MacroMap.end();
529 it != ie; ++it)
Daniel Dunbarb759a132010-07-29 01:51:55 +0000530 delete it->getValue();
531
Daniel Dunbarc5011082010-07-12 18:12:02 +0000532 delete PlatformParser;
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000533}
534
Jim Grosbach4b905842013-09-20 23:08:21 +0000535void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000536 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000537 for (std::vector<MacroInstantiation *>::const_reverse_iterator
538 it = ActiveMacros.rbegin(),
539 ie = ActiveMacros.rend();
540 it != ie; ++it)
541 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000542 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000543}
544
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000545void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
546 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
547 printMacroInstantiations();
548}
549
Chris Lattnera3a06812011-10-16 04:47:35 +0000550bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger29815912014-08-26 18:39:50 +0000551 if (getTargetParser().getTargetOptions().MCFatalWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000552 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000553 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
554 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000555 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000556}
557
Chris Lattnera3a06812011-10-16 04:47:35 +0000558bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000559 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000560 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
561 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000562 return true;
563}
564
Jim Grosbach4b905842013-09-20 23:08:21 +0000565bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000566 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000567 unsigned NewBuf =
568 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
569 if (!NewBuf)
Sean Callanan7a77eae2010-01-21 00:19:58 +0000570 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000571
Sean Callanan7a77eae2010-01-21 00:19:58 +0000572 CurBuffer = NewBuf;
Rafael Espindola8026bd02014-07-06 14:17:29 +0000573 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Sean Callanan7a77eae2010-01-21 00:19:58 +0000574 return false;
575}
Daniel Dunbar43235712010-07-18 18:54:11 +0000576
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000577/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000578/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000579/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000580bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000581 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000582 unsigned NewBuf =
583 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
584 if (!NewBuf)
Kevin Enderby109f25c2011-12-14 21:47:48 +0000585 return true;
586
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000587 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000588 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000589 return false;
590}
591
Alp Tokera55b95b2014-07-06 10:33:31 +0000592void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
593 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000594 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
595 Loc.getPointer());
Daniel Dunbar43235712010-07-18 18:54:11 +0000596}
597
Sean Callanan7a77eae2010-01-21 00:19:58 +0000598const AsmToken &AsmParser::Lex() {
599 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000600
Sean Callanan7a77eae2010-01-21 00:19:58 +0000601 if (tok->is(AsmToken::Eof)) {
602 // If this is the end of an included file, pop the parent file off the
603 // include stack.
604 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
605 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000606 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000607 tok = &Lexer.Lex();
608 }
609 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000610
Sean Callanan7a77eae2010-01-21 00:19:58 +0000611 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000612 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000613
Sean Callanan7a77eae2010-01-21 00:19:58 +0000614 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000615}
616
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000617bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000618 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000619 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000620 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000621
Chris Lattner36e02122009-06-21 20:54:55 +0000622 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000623 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000624
625 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000626 AsmCond StartingCondState = TheCondState;
627
Kevin Enderby6469fc22011-11-01 22:27:22 +0000628 // If we are generating dwarf for assembly source files save the initial text
629 // section and generate a .file directive.
630 if (getContext().getGenDwarfForAssembly()) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000631 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
632 getStreamer().EmitLabel(SectionStartSym);
Oliver Stannard8b273082014-06-19 15:52:37 +0000633 auto InsertResult = getContext().addGenDwarfSection(
634 getStreamer().getCurrentSection().first);
635 assert(InsertResult.second && ".text section should not have debug info yet");
636 InsertResult.first->second.first = SectionStartSym;
David Blaikiec714ef42014-03-17 01:52:11 +0000637 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
638 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000639 }
640
Chris Lattner73f36112009-07-02 21:53:43 +0000641 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000642 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000643 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000644 if (!parseStatement(Info))
645 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000646
Daniel Dunbar43325c42010-09-09 22:42:56 +0000647 // We had an error, validate that one was emitted and recover by skipping to
648 // the next line.
649 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000650 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000651 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000652
653 if (TheCondState.TheCond != StartingCondState.TheCond ||
654 TheCondState.Ignore != StartingCondState.Ignore)
655 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000656
657 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000658 const auto &LineTables = getContext().getMCDwarfLineTables();
659 if (!LineTables.empty()) {
660 unsigned Index = 0;
661 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
662 if (File.Name.empty() && Index != 0)
663 TokError("unassigned file number: " + Twine(Index) +
664 " for .file directives");
665 ++Index;
666 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000667 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000668
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000669 // Check to see that all assembler local symbols were actually defined.
670 // Targets that don't do subsections via symbols may not want this, though,
671 // so conservatively exclude them. Only do this if we're finalizing, though,
672 // as otherwise we won't necessarilly have seen everything yet.
673 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
674 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
675 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000676 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000677 i != e; ++i) {
678 MCSymbol *Sym = i->getValue();
679 // Variable symbols may not be marked as defined, so check those
680 // explicitly. If we know it's a variable, we have a definition for
681 // the purposes of this check.
682 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
683 // FIXME: We would really like to refer back to where the symbol was
684 // first referenced for a source location. We need to add something
685 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000686 printMessage(
687 getLexer().getLoc(), SourceMgr::DK_Error,
688 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000689 }
690 }
691
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000692 // Finalize the output stream if there are no errors and if the client wants
693 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000694 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000695 Out.Finish();
696
Chris Lattner73f36112009-07-02 21:53:43 +0000697 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000698}
699
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000700void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000701 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000702 TokError("expected section directive before assembly directive");
Rafael Espindolaf1440342014-01-23 23:14:14 +0000703 Out.InitSections();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000704 }
705}
706
Jim Grosbach4b905842013-09-20 23:08:21 +0000707/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000708void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000709 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000710 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000711
Chris Lattnere5074c42009-06-22 01:29:09 +0000712 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000713 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000714 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000715}
716
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000717StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000718 const char *Start = getTok().getLoc().getPointer();
719
Jim Grosbach4b905842013-09-20 23:08:21 +0000720 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000721 Lex();
722
723 const char *End = getTok().getLoc().getPointer();
724 return StringRef(Start, End - Start);
725}
Chris Lattner78db3622009-06-22 05:51:26 +0000726
Jim Grosbach4b905842013-09-20 23:08:21 +0000727StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000728 const char *Start = getTok().getLoc().getPointer();
729
730 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000731 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000732 Lex();
733
734 const char *End = getTok().getLoc().getPointer();
735 return StringRef(Start, End - Start);
736}
737
Jim Grosbach4b905842013-09-20 23:08:21 +0000738/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000739/// NOTE: This assumes the leading '(' has already been consumed.
740///
741/// parenexpr ::= expr)
742///
Jim Grosbach4b905842013-09-20 23:08:21 +0000743bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
744 if (parseExpression(Res))
745 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000746 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000747 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000748 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000749 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000750 return false;
751}
Chris Lattner78db3622009-06-22 05:51:26 +0000752
Jim Grosbach4b905842013-09-20 23:08:21 +0000753/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000754/// NOTE: This assumes the leading '[' has already been consumed.
755///
756/// bracketexpr ::= expr]
757///
Jim Grosbach4b905842013-09-20 23:08:21 +0000758bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
759 if (parseExpression(Res))
760 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000761 if (Lexer.isNot(AsmToken::RBrac))
762 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000763 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000764 Lex();
765 return false;
766}
767
Jim Grosbach4b905842013-09-20 23:08:21 +0000768/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000769/// primaryexpr ::= (parenexpr
770/// primaryexpr ::= symbol
771/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000772/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000773/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000774bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000775 SMLoc FirstTokenLoc = getLexer().getLoc();
776 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
777 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000778 default:
779 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000780 // If we have an error assume that we've already handled it.
781 case AsmToken::Error:
782 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000783 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000784 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000785 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000786 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000787 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000788 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000789 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000790 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000791 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000792 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000793 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000794 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000795 if (FirstTokenKind == AsmToken::Dollar) {
796 if (Lexer.getMAI().getDollarIsPC()) {
797 // This is a '$' reference, which references the current PC. Emit a
798 // temporary label to the streamer and refer to it.
799 MCSymbol *Sym = Ctx.CreateTempSymbol();
800 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000801 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
802 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000803 EndLoc = FirstTokenLoc;
804 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000805 }
806 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000807 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000808 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000809 // Parse symbol variant
810 std::pair<StringRef, StringRef> Split;
811 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000812 if (FirstTokenKind == AsmToken::String) {
813 if (Lexer.is(AsmToken::At)) {
814 Lexer.Lex(); // eat @
815 SMLoc AtLoc = getLexer().getLoc();
816 StringRef VName;
817 if (parseIdentifier(VName))
818 return Error(AtLoc, "expected symbol variant after '@'");
819
820 Split = std::make_pair(Identifier, VName);
821 }
822 } else {
823 Split = Identifier.split('@');
824 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000825 } else if (Lexer.is(AsmToken::LParen)) {
826 Lexer.Lex(); // eat (
827 StringRef VName;
828 parseIdentifier(VName);
829 if (Lexer.isNot(AsmToken::RParen)) {
830 return Error(Lexer.getTok().getLoc(),
831 "unexpected token in variant, expected ')'");
832 }
833 Lexer.Lex(); // eat )
834 Split = std::make_pair(Identifier, VName);
835 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000836
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000837 EndLoc = SMLoc::getFromPointer(Identifier.end());
838
Daniel Dunbard20cda02009-10-16 01:34:54 +0000839 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000840 StringRef SymbolName = Identifier;
841 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000842
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000843 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000844 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000845 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000846 if (Variant != MCSymbolRefExpr::VK_Invalid) {
847 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000848 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000849 Variant = MCSymbolRefExpr::VK_None;
850 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000851 return Error(SMLoc::getFromPointer(Split.second.begin()),
852 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000853 }
854 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000855
Hans Wennborgce69d772013-10-18 20:46:28 +0000856 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
857
Daniel Dunbard20cda02009-10-16 01:34:54 +0000858 // If this is an absolute variable reference, substitute it now to preserve
859 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000860 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000861 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000862 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000863
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000864 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000865 return false;
866 }
867
868 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000869 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000870 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000871 }
David Woodhousef42a6662014-02-01 16:20:54 +0000872 case AsmToken::BigNum:
873 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000874 case AsmToken::Integer: {
875 SMLoc Loc = getTok().getLoc();
876 int64_t IntVal = getTok().getIntVal();
877 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000878 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000879 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000880 // Look for 'b' or 'f' following an Integer as a directional label
881 if (Lexer.getKind() == AsmToken::Identifier) {
882 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000883 // Lookup the symbol variant if used.
884 std::pair<StringRef, StringRef> Split = IDVal.split('@');
885 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
886 if (Split.first.size() != IDVal.size()) {
887 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000888 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000889 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000890 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000891 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000892 if (IDVal == "f" || IDVal == "b") {
893 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000894 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Ulrich Weigandd4120982013-06-20 16:24:17 +0000895 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000896 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000897 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000898 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000899 Lex(); // Eat identifier.
900 }
901 }
Chris Lattner78db3622009-06-22 05:51:26 +0000902 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000903 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000904 case AsmToken::Real: {
905 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000906 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000907 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000908 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000909 Lex(); // Eat token.
910 return false;
911 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000912 case AsmToken::Dot: {
913 // This is a '.' reference, which references the current PC. Emit a
914 // temporary label to the streamer and refer to it.
915 MCSymbol *Sym = Ctx.CreateTempSymbol();
916 Out.EmitLabel(Sym);
917 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000918 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000919 Lex(); // Eat identifier.
920 return false;
921 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000922 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000923 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000924 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000925 case AsmToken::LBrac:
926 if (!PlatformParser->HasBracketExpressions())
927 return TokError("brackets expression not supported on this target");
928 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000929 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000930 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000931 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000932 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000933 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000934 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000935 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000936 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000937 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000938 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000939 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000940 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000941 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000942 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000943 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000944 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000945 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000946 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000947 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000948 }
949}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000950
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000951bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000952 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000953 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000954}
955
Daniel Dunbar55f16672010-09-17 02:47:07 +0000956const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000957AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000958 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000959 // Ask the target implementation about this expression first.
960 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
961 if (NewE)
962 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000963 // Recurse over the given expression, rebuilding it to apply the given variant
964 // if there is exactly one symbol.
965 switch (E->getKind()) {
966 case MCExpr::Target:
967 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +0000968 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000969
970 case MCExpr::SymbolRef: {
971 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
972
973 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000974 TokError("invalid variant on expression '" + getTok().getIdentifier() +
975 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000976 return E;
977 }
978
979 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
980 }
981
982 case MCExpr::Unary: {
983 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000984 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000985 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +0000986 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000987 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
988 }
989
990 case MCExpr::Binary: {
991 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000992 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
993 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000994
995 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +0000996 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000997
Jim Grosbach4b905842013-09-20 23:08:21 +0000998 if (!LHS)
999 LHS = BE->getLHS();
1000 if (!RHS)
1001 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +00001002
1003 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1004 }
1005 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001006
Craig Toppera2886c22012-02-07 05:05:23 +00001007 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001008}
1009
Jim Grosbach4b905842013-09-20 23:08:21 +00001010/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001011///
Jim Grosbachbd164242011-08-20 16:24:13 +00001012/// expr ::= expr &&,|| expr -> lowest.
1013/// expr ::= expr |,^,&,! expr
1014/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1015/// expr ::= expr <<,>> expr
1016/// expr ::= expr +,- expr
1017/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001018/// expr ::= primaryexpr
1019///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001020bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001021 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001022 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001023 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001024 return true;
1025
Daniel Dunbar55f16672010-09-17 02:47:07 +00001026 // As a special case, we support 'a op b @ modifier' by rewriting the
1027 // expression to include the modifier. This is inefficient, but in general we
1028 // expect users to use 'a@modifier op b'.
1029 if (Lexer.getKind() == AsmToken::At) {
1030 Lex();
1031
1032 if (Lexer.isNot(AsmToken::Identifier))
1033 return TokError("unexpected symbol modifier following '@'");
1034
1035 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001036 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001037 if (Variant == MCSymbolRefExpr::VK_Invalid)
1038 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1039
Jim Grosbach4b905842013-09-20 23:08:21 +00001040 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001041 if (!ModifiedRes) {
1042 return TokError("invalid modifier '" + getTok().getIdentifier() +
1043 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001044 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001045
Daniel Dunbar55f16672010-09-17 02:47:07 +00001046 Res = ModifiedRes;
1047 Lex();
1048 }
1049
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001050 // Try to constant fold it up front, if possible.
1051 int64_t Value;
1052 if (Res->EvaluateAsAbsolute(Value))
1053 Res = MCConstantExpr::Create(Value, getContext());
1054
1055 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001056}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001057
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001058bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001059 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001060 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001061}
1062
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001063bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001064 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001065
Daniel Dunbar75630b32009-06-30 02:10:03 +00001066 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001067 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001068 return true;
1069
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001070 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001071 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001072
1073 return false;
1074}
1075
Michael J. Spencer530ce852010-10-09 11:00:50 +00001076static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001077 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001078 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001079 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001080 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001081
Jim Grosbach4b905842013-09-20 23:08:21 +00001082 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001083 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001084 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001085 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001086 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001087 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001088 return 1;
1089
Jim Grosbach4b905842013-09-20 23:08:21 +00001090 // Low Precedence: |, &, ^
1091 //
1092 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001093 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001094 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001095 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001096 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001097 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001098 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001099 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001100 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001101 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001102
Jim Grosbach4b905842013-09-20 23:08:21 +00001103 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001104 case AsmToken::EqualEqual:
1105 Kind = MCBinaryExpr::EQ;
1106 return 3;
1107 case AsmToken::ExclaimEqual:
1108 case AsmToken::LessGreater:
1109 Kind = MCBinaryExpr::NE;
1110 return 3;
1111 case AsmToken::Less:
1112 Kind = MCBinaryExpr::LT;
1113 return 3;
1114 case AsmToken::LessEqual:
1115 Kind = MCBinaryExpr::LTE;
1116 return 3;
1117 case AsmToken::Greater:
1118 Kind = MCBinaryExpr::GT;
1119 return 3;
1120 case AsmToken::GreaterEqual:
1121 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001122 return 3;
1123
Jim Grosbach4b905842013-09-20 23:08:21 +00001124 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001125 case AsmToken::LessLess:
1126 Kind = MCBinaryExpr::Shl;
1127 return 4;
1128 case AsmToken::GreaterGreater:
1129 Kind = MCBinaryExpr::Shr;
1130 return 4;
1131
Jim Grosbach4b905842013-09-20 23:08:21 +00001132 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001133 case AsmToken::Plus:
1134 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001135 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001136 case AsmToken::Minus:
1137 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001138 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001139
Jim Grosbach4b905842013-09-20 23:08:21 +00001140 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001141 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001142 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001143 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001144 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001145 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001146 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001147 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001148 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001149 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001150 }
1151}
1152
Jim Grosbach4b905842013-09-20 23:08:21 +00001153/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001154/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001155bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001156 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001157 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001158 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001159 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001160
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001161 // If the next token is lower precedence than we are allowed to eat, return
1162 // successfully with what we ate already.
1163 if (TokPrec < Precedence)
1164 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001165
Sean Callanan686ed8d2010-01-19 20:22:31 +00001166 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001167
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001168 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001169 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001170 if (parsePrimaryExpr(RHS, EndLoc))
1171 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001172
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001173 // If BinOp binds less tightly with RHS than the operator after RHS, let
1174 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001175 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001176 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001177 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1178 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001179
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001180 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001181 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001182 }
1183}
1184
Chris Lattner36e02122009-06-21 20:54:55 +00001185/// ParseStatement:
1186/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001187/// ::= Label* Directive ...Operands... EndOfStatement
1188/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001189bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001190 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001191 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001192 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001193 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001194 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001195
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001196 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001197 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001198 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001199 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001200 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001201 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001202 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001203 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001204
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001205 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001206 if (Lexer.is(AsmToken::Integer)) {
1207 LocalLabelVal = getTok().getIntVal();
1208 if (LocalLabelVal < 0) {
1209 if (!TheCondState.Ignore)
1210 return TokError("unexpected token at start of statement");
1211 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001212 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001213 IDVal = getTok().getString();
1214 Lex(); // Consume the integer token to be used as an identifier token.
1215 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001216 if (!TheCondState.Ignore)
1217 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001218 }
1219 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001220 } else if (Lexer.is(AsmToken::Dot)) {
1221 // Treat '.' as a valid identifier in this context.
1222 Lex();
1223 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001224 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001225 if (!TheCondState.Ignore)
1226 return TokError("unexpected token at start of statement");
1227 IDVal = "";
1228 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001229
Chris Lattner926885c2010-04-17 18:14:27 +00001230 // Handle conditional assembly here before checking for skipping. We
1231 // have to do this so that .endif isn't skipped in a ".if 0" block for
1232 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001233 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001234 DirectiveKindMap.find(IDVal);
1235 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1236 ? DK_NO_DIRECTIVE
1237 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001238 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001239 default:
1240 break;
1241 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001242 case DK_IFEQ:
1243 case DK_IFGE:
1244 case DK_IFGT:
1245 case DK_IFLE:
1246 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001247 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001248 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001249 case DK_IFB:
1250 return parseDirectiveIfb(IDLoc, true);
1251 case DK_IFNB:
1252 return parseDirectiveIfb(IDLoc, false);
1253 case DK_IFC:
1254 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001255 case DK_IFEQS:
1256 return parseDirectiveIfeqs(IDLoc);
Jim Grosbach4b905842013-09-20 23:08:21 +00001257 case DK_IFNC:
1258 return parseDirectiveIfc(IDLoc, false);
1259 case DK_IFDEF:
1260 return parseDirectiveIfdef(IDLoc, true);
1261 case DK_IFNDEF:
1262 case DK_IFNOTDEF:
1263 return parseDirectiveIfdef(IDLoc, false);
1264 case DK_ELSEIF:
1265 return parseDirectiveElseIf(IDLoc);
1266 case DK_ELSE:
1267 return parseDirectiveElse(IDLoc);
1268 case DK_ENDIF:
1269 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001270 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001271
Eli Bendersky88024712013-01-16 19:32:36 +00001272 // Ignore the statement if in the middle of inactive conditional
1273 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001274 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001275 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001276 return false;
1277 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001278
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001279 // FIXME: Recurse on local labels?
1280
1281 // See what kind of statement we have.
1282 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001283 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001284 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001285
Chris Lattner36e02122009-06-21 20:54:55 +00001286 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001287 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001288
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001289 // Diagnose attempt to use '.' as a label.
1290 if (IDVal == ".")
1291 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1292
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001293 // Diagnose attempt to use a variable as a label.
1294 //
1295 // FIXME: Diagnostics. Note the location of the definition as a label.
1296 // FIXME: This doesn't diagnose assignment to a symbol which has been
1297 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001298 MCSymbol *Sym;
1299 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001300 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001301 else
1302 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001303 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001304 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001305
Daniel Dunbare73b2672009-08-26 22:13:22 +00001306 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001307 if (!ParsingInlineAsm)
1308 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001309
Kevin Enderbye7739d42011-12-09 18:09:40 +00001310 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001311 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001312 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001313 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1314 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001315
Tim Northover1744d0a2013-10-25 12:49:50 +00001316 getTargetParser().onLabelParsed(Sym);
1317
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001318 // Consume any end of statement token, if present, to avoid spurious
1319 // AddBlankLine calls().
1320 if (Lexer.is(AsmToken::EndOfStatement)) {
1321 Lex();
1322 if (Lexer.is(AsmToken::Eof))
1323 return false;
1324 }
1325
Eli Friedman0f4871d2012-10-22 23:58:19 +00001326 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001327 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001328
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001329 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001330 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001331 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001332
Jim Grosbach4b905842013-09-20 23:08:21 +00001333 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001334
1335 default: // Normal instruction or directive.
1336 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001337 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001338
1339 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001340 if (areMacrosEnabled())
1341 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1342 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001343 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001344
Michael J. Spencer530ce852010-10-09 11:00:50 +00001345 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001346
Eli Bendersky17233942013-01-15 22:59:42 +00001347 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001348 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001349 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001350 //
Eli Bendersky17233942013-01-15 22:59:42 +00001351 // 1. The target-specific assembly parser. Some directives are target
1352 // specific or may potentially behave differently on certain targets.
1353 // 2. Asm parser extensions. For example, platform-specific parsers
1354 // (like the ELF parser) register themselves as extensions.
1355 // 3. The generic directive parser implemented by this class. These are
1356 // all the directives that behave in a target and platform independent
1357 // manner, or at least have a default behavior that's shared between
1358 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001359
Eli Bendersky17233942013-01-15 22:59:42 +00001360 // First query the target-specific parser. It will return 'true' if it
1361 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001362 if (!getTargetParser().ParseDirective(ID))
1363 return false;
1364
Alp Tokercb402912014-01-24 17:20:08 +00001365 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001366 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001367 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1368 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001369 if (Handler.first)
1370 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1371
1372 // Finally, if no one else is interested in this directive, it must be
1373 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001374 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001375 default:
1376 break;
1377 case DK_SET:
1378 case DK_EQU:
1379 return parseDirectiveSet(IDVal, true);
1380 case DK_EQUIV:
1381 return parseDirectiveSet(IDVal, false);
1382 case DK_ASCII:
1383 return parseDirectiveAscii(IDVal, false);
1384 case DK_ASCIZ:
1385 case DK_STRING:
1386 return parseDirectiveAscii(IDVal, true);
1387 case DK_BYTE:
1388 return parseDirectiveValue(1);
1389 case DK_SHORT:
1390 case DK_VALUE:
1391 case DK_2BYTE:
1392 return parseDirectiveValue(2);
1393 case DK_LONG:
1394 case DK_INT:
1395 case DK_4BYTE:
1396 return parseDirectiveValue(4);
1397 case DK_QUAD:
1398 case DK_8BYTE:
1399 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001400 case DK_OCTA:
1401 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001402 case DK_SINGLE:
1403 case DK_FLOAT:
1404 return parseDirectiveRealValue(APFloat::IEEEsingle);
1405 case DK_DOUBLE:
1406 return parseDirectiveRealValue(APFloat::IEEEdouble);
1407 case DK_ALIGN: {
1408 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1409 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1410 }
1411 case DK_ALIGN32: {
1412 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1413 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1414 }
1415 case DK_BALIGN:
1416 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1417 case DK_BALIGNW:
1418 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1419 case DK_BALIGNL:
1420 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1421 case DK_P2ALIGN:
1422 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1423 case DK_P2ALIGNW:
1424 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1425 case DK_P2ALIGNL:
1426 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1427 case DK_ORG:
1428 return parseDirectiveOrg();
1429 case DK_FILL:
1430 return parseDirectiveFill();
1431 case DK_ZERO:
1432 return parseDirectiveZero();
1433 case DK_EXTERN:
1434 eatToEndOfStatement(); // .extern is the default, ignore it.
1435 return false;
1436 case DK_GLOBL:
1437 case DK_GLOBAL:
1438 return parseDirectiveSymbolAttribute(MCSA_Global);
1439 case DK_LAZY_REFERENCE:
1440 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1441 case DK_NO_DEAD_STRIP:
1442 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1443 case DK_SYMBOL_RESOLVER:
1444 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1445 case DK_PRIVATE_EXTERN:
1446 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1447 case DK_REFERENCE:
1448 return parseDirectiveSymbolAttribute(MCSA_Reference);
1449 case DK_WEAK_DEFINITION:
1450 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1451 case DK_WEAK_REFERENCE:
1452 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1453 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1454 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1455 case DK_COMM:
1456 case DK_COMMON:
1457 return parseDirectiveComm(/*IsLocal=*/false);
1458 case DK_LCOMM:
1459 return parseDirectiveComm(/*IsLocal=*/true);
1460 case DK_ABORT:
1461 return parseDirectiveAbort();
1462 case DK_INCLUDE:
1463 return parseDirectiveInclude();
1464 case DK_INCBIN:
1465 return parseDirectiveIncbin();
1466 case DK_CODE16:
1467 case DK_CODE16GCC:
1468 return TokError(Twine(IDVal) + " not supported yet");
1469 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001470 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001471 case DK_IRP:
1472 return parseDirectiveIrp(IDLoc);
1473 case DK_IRPC:
1474 return parseDirectiveIrpc(IDLoc);
1475 case DK_ENDR:
1476 return parseDirectiveEndr(IDLoc);
1477 case DK_BUNDLE_ALIGN_MODE:
1478 return parseDirectiveBundleAlignMode();
1479 case DK_BUNDLE_LOCK:
1480 return parseDirectiveBundleLock();
1481 case DK_BUNDLE_UNLOCK:
1482 return parseDirectiveBundleUnlock();
1483 case DK_SLEB128:
1484 return parseDirectiveLEB128(true);
1485 case DK_ULEB128:
1486 return parseDirectiveLEB128(false);
1487 case DK_SPACE:
1488 case DK_SKIP:
1489 return parseDirectiveSpace(IDVal);
1490 case DK_FILE:
1491 return parseDirectiveFile(IDLoc);
1492 case DK_LINE:
1493 return parseDirectiveLine();
1494 case DK_LOC:
1495 return parseDirectiveLoc();
1496 case DK_STABS:
1497 return parseDirectiveStabs();
1498 case DK_CFI_SECTIONS:
1499 return parseDirectiveCFISections();
1500 case DK_CFI_STARTPROC:
1501 return parseDirectiveCFIStartProc();
1502 case DK_CFI_ENDPROC:
1503 return parseDirectiveCFIEndProc();
1504 case DK_CFI_DEF_CFA:
1505 return parseDirectiveCFIDefCfa(IDLoc);
1506 case DK_CFI_DEF_CFA_OFFSET:
1507 return parseDirectiveCFIDefCfaOffset();
1508 case DK_CFI_ADJUST_CFA_OFFSET:
1509 return parseDirectiveCFIAdjustCfaOffset();
1510 case DK_CFI_DEF_CFA_REGISTER:
1511 return parseDirectiveCFIDefCfaRegister(IDLoc);
1512 case DK_CFI_OFFSET:
1513 return parseDirectiveCFIOffset(IDLoc);
1514 case DK_CFI_REL_OFFSET:
1515 return parseDirectiveCFIRelOffset(IDLoc);
1516 case DK_CFI_PERSONALITY:
1517 return parseDirectiveCFIPersonalityOrLsda(true);
1518 case DK_CFI_LSDA:
1519 return parseDirectiveCFIPersonalityOrLsda(false);
1520 case DK_CFI_REMEMBER_STATE:
1521 return parseDirectiveCFIRememberState();
1522 case DK_CFI_RESTORE_STATE:
1523 return parseDirectiveCFIRestoreState();
1524 case DK_CFI_SAME_VALUE:
1525 return parseDirectiveCFISameValue(IDLoc);
1526 case DK_CFI_RESTORE:
1527 return parseDirectiveCFIRestore(IDLoc);
1528 case DK_CFI_ESCAPE:
1529 return parseDirectiveCFIEscape();
1530 case DK_CFI_SIGNAL_FRAME:
1531 return parseDirectiveCFISignalFrame();
1532 case DK_CFI_UNDEFINED:
1533 return parseDirectiveCFIUndefined(IDLoc);
1534 case DK_CFI_REGISTER:
1535 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001536 case DK_CFI_WINDOW_SAVE:
1537 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001538 case DK_MACROS_ON:
1539 case DK_MACROS_OFF:
1540 return parseDirectiveMacrosOnOff(IDVal);
1541 case DK_MACRO:
1542 return parseDirectiveMacro(IDLoc);
Nico Weber155dccd12014-07-24 17:08:39 +00001543 case DK_EXITM:
1544 return parseDirectiveExitMacro(IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001545 case DK_ENDM:
1546 case DK_ENDMACRO:
1547 return parseDirectiveEndMacro(IDVal);
1548 case DK_PURGEM:
1549 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001550 case DK_END:
1551 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001552 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001553 return parseDirectiveError(IDLoc, false);
1554 case DK_ERROR:
1555 return parseDirectiveError(IDLoc, true);
Nico Weber404012b2014-07-24 16:26:06 +00001556 case DK_WARNING:
1557 return parseDirectiveWarning(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001558 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001559
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001560 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001561 }
Chris Lattner36e02122009-06-21 20:54:55 +00001562
Chad Rosierc7f552c2013-02-12 21:33:51 +00001563 // __asm _emit or __asm __emit
1564 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1565 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001566 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001567
1568 // __asm align
1569 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001570 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001571
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001572 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001573
Chris Lattner7cbfa442010-05-19 23:34:33 +00001574 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001575 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001576 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001577 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001578 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001579 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001580
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001581 // Dump the parsed representation, if requested.
1582 if (getShowParsedOperands()) {
1583 SmallString<256> Str;
1584 raw_svector_ostream OS(Str);
1585 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001586 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001587 if (i != 0)
1588 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001589 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001590 }
1591 OS << "]";
1592
Jim Grosbach4b905842013-09-20 23:08:21 +00001593 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001594 }
1595
Oliver Stannard8b273082014-06-19 15:52:37 +00001596 // If we are generating dwarf for the current section then generate a .loc
1597 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001598 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001599 getContext().getGenDwarfSectionSyms().count(
1600 getStreamer().getCurrentSection().first)) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001601
Eli Bendersky88024712013-01-16 19:32:36 +00001602 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001603
Eli Bendersky88024712013-01-16 19:32:36 +00001604 // If we previously parsed a cpp hash file line comment then make sure the
1605 // current Dwarf File is for the CppHashFilename if not then emit the
1606 // Dwarf File table for it and adjust the line number for the .loc.
Eli Bendersky88024712013-01-16 19:32:36 +00001607 if (CppHashFilename.size() != 0) {
David Blaikiec714ef42014-03-17 01:52:11 +00001608 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1609 0, StringRef(), CppHashFilename);
1610 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001611
Jim Grosbach4b905842013-09-20 23:08:21 +00001612 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1613 // cache with the different Loc from the call above we save the last
1614 // info we queried here with SrcMgr.FindLineNumber().
1615 unsigned CppHashLocLineNo;
1616 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1617 CppHashLocLineNo = LastQueryLine;
1618 else {
1619 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1620 LastQueryLine = CppHashLocLineNo;
1621 LastQueryIDLoc = CppHashLoc;
1622 LastQueryBuffer = CppHashBuf;
1623 }
1624 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001625 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001626
Jim Grosbach4b905842013-09-20 23:08:21 +00001627 getStreamer().EmitDwarfLocDirective(
1628 getContext().getGenDwarfFileNumber(), Line, 0,
1629 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1630 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001631 }
1632
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001633 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001634 if (!HadError) {
Tim Northover26bb14e2014-08-18 11:49:42 +00001635 uint64_t ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001636 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1637 Info.ParsedOperands, Out,
1638 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001639 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001640
Chris Lattnera2a9d162010-09-11 16:18:25 +00001641 // Don't skip the rest of the line, the instruction parser is responsible for
1642 // that.
1643 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001644}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001645
Jim Grosbach4b905842013-09-20 23:08:21 +00001646/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001647/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001648void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001649 if (!Lexer.is(AsmToken::EndOfStatement))
1650 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001651 // Eat EOL.
1652 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001653}
1654
Jim Grosbach4b905842013-09-20 23:08:21 +00001655/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001656/// ::= # number "filename"
1657/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001658bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001659 Lex(); // Eat the hash token.
1660
1661 if (getLexer().isNot(AsmToken::Integer)) {
1662 // Consume the line since in cases it is not a well-formed line directive,
1663 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001664 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001665 return false;
1666 }
1667
1668 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001669 Lex();
1670
1671 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001672 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001673 return false;
1674 }
1675
1676 StringRef Filename = getTok().getString();
1677 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001678 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001679
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001680 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1681 CppHashLoc = L;
1682 CppHashFilename = Filename;
1683 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001684 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001685
1686 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001687 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001688 return false;
1689}
1690
Jim Grosbach4b905842013-09-20 23:08:21 +00001691/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001692/// for the Filename and LineNo if any in the diagnostic.
1693void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001694 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001695 raw_ostream &OS = errs();
1696
1697 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1698 const SMLoc &DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00001699 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1700 unsigned CppHashBuf =
1701 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001702
Jim Grosbach4b905842013-09-20 23:08:21 +00001703 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001704 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00001705 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1706 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1707 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001708 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1709 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001710 }
1711
Eric Christophera7c32732012-12-18 00:30:54 +00001712 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001713 // manager changed or buffer changed (like in a nested include) then just
1714 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001715 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001716 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001717 if (Parser->SavedDiagHandler)
1718 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1719 else
Craig Topper353eda42014-04-24 06:44:33 +00001720 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001721 return;
1722 }
1723
Eric Christophera7c32732012-12-18 00:30:54 +00001724 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001725 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1726 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001727 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001728
1729 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1730 int CppHashLocLineNo =
1731 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001732 int LineNo =
1733 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001734
Jim Grosbach4b905842013-09-20 23:08:21 +00001735 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1736 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001737 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001738
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001739 if (Parser->SavedDiagHandler)
1740 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1741 else
Craig Topper353eda42014-04-24 06:44:33 +00001742 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001743}
1744
Rafael Espindola2c064482012-08-21 18:29:30 +00001745// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1746// difference being that that function accepts '@' as part of identifiers and
1747// we can't do that. AsmLexer.cpp should probably be changed to handle
1748// '@' as a special case when needed.
1749static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001750 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1751 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001752}
1753
Rafael Espindola34b9c512012-06-03 23:57:14 +00001754bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001755 ArrayRef<MCAsmMacroParameter> Parameters,
1756 ArrayRef<MCAsmMacroArgument> A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001757 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001758 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001759 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001760 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001761
Preston Gurd05500642012-09-19 20:36:12 +00001762 // A macro without parameters is handled differently on Darwin:
1763 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001764 while (!Body.empty()) {
1765 // Scan for the next substitution.
1766 std::size_t End = Body.size(), Pos = 0;
1767 for (; Pos != End; ++Pos) {
1768 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001769 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001770 // This macro has no parameters, look for $0, $1, etc.
1771 if (Body[Pos] != '$' || Pos + 1 == End)
1772 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001773
Rafael Espindola1134ab232011-06-05 02:43:45 +00001774 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001775 if (Next == '$' || Next == 'n' ||
1776 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001777 break;
1778 } else {
1779 // This macro has parameters, look for \foo, \bar, etc.
1780 if (Body[Pos] == '\\' && Pos + 1 != End)
1781 break;
1782 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001783 }
1784
1785 // Add the prefix.
1786 OS << Body.slice(0, Pos);
1787
1788 // Check if we reached the end.
1789 if (Pos == End)
1790 break;
1791
Benjamin Kramer513e7442014-02-20 13:36:32 +00001792 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001793 switch (Body[Pos + 1]) {
1794 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001795 case '$':
1796 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001797 break;
1798
Jim Grosbach4b905842013-09-20 23:08:21 +00001799 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001800 case 'n':
1801 OS << A.size();
1802 break;
1803
Jim Grosbach4b905842013-09-20 23:08:21 +00001804 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001805 default: {
1806 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001807 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001808 if (Index >= A.size())
1809 break;
1810
1811 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001812 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001813 ie = A[Index].end();
1814 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001815 OS << it->getString();
1816 break;
1817 }
1818 }
1819 Pos += 2;
1820 } else {
1821 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001822 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001823 ++I;
1824
Jim Grosbach4b905842013-09-20 23:08:21 +00001825 const char *Begin = Body.data() + Pos + 1;
1826 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001827 unsigned Index = 0;
1828 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001829 if (Parameters[Index].Name == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001830 break;
1831
Preston Gurd05500642012-09-19 20:36:12 +00001832 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001833 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1834 Pos += 3;
1835 else {
1836 OS << '\\' << Argument;
1837 Pos = I;
1838 }
Preston Gurd05500642012-09-19 20:36:12 +00001839 } else {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001840 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Eli Benderskya7b905e2013-01-14 19:00:26 +00001841 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001842 ie = A[Index].end();
1843 it != ie; ++it)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001844 // We expect no quotes around the string's contents when
1845 // parsing for varargs.
1846 if (it->getKind() != AsmToken::String || VarargParameter)
Preston Gurd05500642012-09-19 20:36:12 +00001847 OS << it->getString();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001848 else
1849 OS << it->getStringContents();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001850
Preston Gurd05500642012-09-19 20:36:12 +00001851 Pos += 1 + Argument.size();
1852 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001853 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001854 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001855 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001856 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001857
Rafael Espindola1134ab232011-06-05 02:43:45 +00001858 return false;
1859}
Daniel Dunbar43235712010-07-18 18:54:11 +00001860
Nico Weber2a8f9222014-07-24 16:29:04 +00001861MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00001862 StringRef I, size_t CondStackDepth)
1863 : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
Nico Weber155dccd12014-07-24 17:08:39 +00001864 CondStackDepth(CondStackDepth) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001865
Jim Grosbach4b905842013-09-20 23:08:21 +00001866static bool isOperator(AsmToken::TokenKind kind) {
1867 switch (kind) {
1868 default:
1869 return false;
1870 case AsmToken::Plus:
1871 case AsmToken::Minus:
1872 case AsmToken::Tilde:
1873 case AsmToken::Slash:
1874 case AsmToken::Star:
1875 case AsmToken::Dot:
1876 case AsmToken::Equal:
1877 case AsmToken::EqualEqual:
1878 case AsmToken::Pipe:
1879 case AsmToken::PipePipe:
1880 case AsmToken::Caret:
1881 case AsmToken::Amp:
1882 case AsmToken::AmpAmp:
1883 case AsmToken::Exclaim:
1884 case AsmToken::ExclaimEqual:
1885 case AsmToken::Percent:
1886 case AsmToken::Less:
1887 case AsmToken::LessEqual:
1888 case AsmToken::LessLess:
1889 case AsmToken::LessGreater:
1890 case AsmToken::Greater:
1891 case AsmToken::GreaterEqual:
1892 case AsmToken::GreaterGreater:
1893 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001894 }
1895}
1896
David Majnemer16252452014-01-29 00:07:39 +00001897namespace {
1898class AsmLexerSkipSpaceRAII {
1899public:
1900 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1901 Lexer.setSkipSpace(SkipSpace);
1902 }
1903
1904 ~AsmLexerSkipSpaceRAII() {
1905 Lexer.setSkipSpace(true);
1906 }
1907
1908private:
1909 AsmLexer &Lexer;
1910};
1911}
1912
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001913bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
1914
1915 if (Vararg) {
1916 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1917 StringRef Str = parseStringToEndOfStatement();
1918 MA.push_back(AsmToken(AsmToken::String, Str));
1919 }
1920 return false;
1921 }
1922
Rafael Espindola768b41c2012-06-15 14:02:34 +00001923 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001924 unsigned AddTokens = 0;
1925
David Majnemer16252452014-01-29 00:07:39 +00001926 // Darwin doesn't use spaces to delmit arguments.
1927 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001928
1929 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001930 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001931 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001932
David Majnemer91fc4c22014-01-29 18:57:46 +00001933 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001934 break;
Preston Gurd05500642012-09-19 20:36:12 +00001935
1936 if (Lexer.is(AsmToken::Space)) {
1937 Lex(); // Eat spaces
1938
1939 // Spaces can delimit parameters, but could also be part an expression.
1940 // If the token after a space is an operator, add the token and the next
1941 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001942 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001943 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001944 // Check to see whether the token is used as an operator,
1945 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001946 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001947 if (*NextChar == ' ')
1948 AddTokens = 2;
1949 }
1950
1951 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001952 break;
1953 }
1954 }
1955 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001956
Jim Grosbach4b905842013-09-20 23:08:21 +00001957 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001958 // to be able to fill in the remaining default parameter values
1959 if (Lexer.is(AsmToken::EndOfStatement))
1960 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001961
1962 // Adjust the current parentheses level.
1963 if (Lexer.is(AsmToken::LParen))
1964 ++ParenLevel;
1965 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1966 --ParenLevel;
1967
1968 // Append the token to the current argument list.
1969 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001970 if (AddTokens)
1971 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001972 Lex();
1973 }
Preston Gurd05500642012-09-19 20:36:12 +00001974
Rafael Espindola768b41c2012-06-15 14:02:34 +00001975 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001976 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001977 return false;
1978}
1979
1980// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001981bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001982 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001983 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001984 bool NamedParametersFound = false;
1985 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001986
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001987 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001988 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001989
Rafael Espindola768b41c2012-06-15 14:02:34 +00001990 // Parse two kinds of macro invocations:
1991 // - macros defined without any parameters accept an arbitrary number of them
1992 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001993 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001994 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1995 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001996 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001997 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001998
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001999 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002000 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002001 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002002 eatToEndOfStatement();
2003 return true;
2004 }
2005
2006 if (!Lexer.is(AsmToken::Equal)) {
2007 TokError("expected '=' after formal parameter identifier");
2008 eatToEndOfStatement();
2009 return true;
2010 }
2011 Lex();
2012
2013 NamedParametersFound = true;
2014 }
2015
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002016 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002017 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002018 eatToEndOfStatement();
2019 return true;
2020 }
2021
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002022 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2023 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002024 return true;
2025
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002026 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002027 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002028 unsigned FAI = 0;
2029 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002030 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002031 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002032
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002033 if (FAI >= NParameters) {
Oliver Stannard8b273082014-06-19 15:52:37 +00002034 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002035 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002036 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002037 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002038 return true;
2039 }
2040 PI = FAI;
2041 }
2042
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002043 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002044 if (A.size() <= PI)
2045 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002046 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002047
2048 if (FALocs.size() <= PI)
2049 FALocs.resize(PI + 1);
2050
2051 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002052 }
Jim Grosbach206661622012-07-30 22:44:17 +00002053
Preston Gurd242ed3152012-09-19 20:29:04 +00002054 // At the end of the statement, fill in remaining arguments that have
2055 // default values. If there aren't any, then the next argument is
2056 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002057 if (Lexer.is(AsmToken::EndOfStatement)) {
2058 bool Failure = false;
2059 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2060 if (A[FAI].empty()) {
2061 if (M->Parameters[FAI].Required) {
2062 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2063 "missing value for required parameter "
2064 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2065 Failure = true;
2066 }
2067
2068 if (!M->Parameters[FAI].Value.empty())
2069 A[FAI] = M->Parameters[FAI].Value;
2070 }
2071 }
2072 return Failure;
2073 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002074
2075 if (Lexer.is(AsmToken::Comma))
2076 Lex();
2077 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002078
2079 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002080}
2081
Jim Grosbach4b905842013-09-20 23:08:21 +00002082const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
2083 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Craig Topper353eda42014-04-24 06:44:33 +00002084 return (I == MacroMap.end()) ? nullptr : I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002085}
2086
Jim Grosbach4b905842013-09-20 23:08:21 +00002087void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00002088 MacroMap[Name] = new MCAsmMacro(Macro);
2089}
2090
Jim Grosbach4b905842013-09-20 23:08:21 +00002091void AsmParser::undefineMacro(StringRef Name) {
2092 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002093 if (I != MacroMap.end()) {
2094 delete I->getValue();
2095 MacroMap.erase(I);
2096 }
2097}
2098
Jim Grosbach4b905842013-09-20 23:08:21 +00002099bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002100 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2101 // this, although we should protect against infinite loops.
2102 if (ActiveMacros.size() == 20)
2103 return TokError("macros cannot be nested more than 20 levels deep");
2104
Eli Bendersky38274122013-01-14 23:22:36 +00002105 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002106 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002107 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002108
Rafael Espindola1134ab232011-06-05 02:43:45 +00002109 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2110 // to hold the macro body with substitutions.
2111 SmallString<256> Buf;
2112 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002113 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002114
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002115 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002116 return true;
2117
Eli Bendersky38274122013-01-14 23:22:36 +00002118 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002119 // instantiation.
2120 OS << ".endmacro\n";
2121
David Blaikie1961f142014-08-21 20:44:56 +00002122 std::unique_ptr<MemoryBuffer> Instantiation(
2123 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"));
Rafael Espindola1134ab232011-06-05 02:43:45 +00002124
Daniel Dunbar43235712010-07-18 18:54:11 +00002125 // Create the macro instantiation object and add to the current macro
2126 // instantiation stack.
Nico Weber155dccd12014-07-24 17:08:39 +00002127 MacroInstantiation *MI =
2128 new MacroInstantiation(NameLoc, CurBuffer, getTok().getLoc(),
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00002129 Instantiation->getBuffer(), TheCondStack.size());
Daniel Dunbar43235712010-07-18 18:54:11 +00002130 ActiveMacros.push_back(MI);
2131
2132 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00002133 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002134 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002135 Lex();
2136
2137 return false;
2138}
2139
Jim Grosbach4b905842013-09-20 23:08:21 +00002140void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002141 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002142 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002143 Lex();
2144
2145 // Pop the instantiation entry.
2146 delete ActiveMacros.back();
2147 ActiveMacros.pop_back();
2148}
2149
Jim Grosbach4b905842013-09-20 23:08:21 +00002150static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002151 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002152 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002153 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2154 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002155 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002156 case MCExpr::Target:
2157 case MCExpr::Constant:
2158 return false;
2159 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002160 const MCSymbol &S =
2161 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002162 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002163 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002164 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002165 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002166 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002167 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002168 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002169
2170 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002171}
2172
Jim Grosbach4b905842013-09-20 23:08:21 +00002173bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002174 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002175 // FIXME: Use better location, we should use proper tokens.
2176 SMLoc EqualLoc = Lexer.getLoc();
2177
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002178 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002179 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002180 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002181
Rafael Espindola72f5f172012-01-28 05:57:00 +00002182 // Note: we don't count b as used in "a = b". This is to allow
2183 // a = b
2184 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002185
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002186 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002187 return TokError("unexpected token in assignment");
2188
2189 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002190 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002191
Daniel Dunbar5f339242009-10-16 01:57:39 +00002192 // Validate that the LHS is allowed to be a variable (either it has not been
2193 // used as a symbol, or it is an absolute symbol).
2194 MCSymbol *Sym = getContext().LookupSymbol(Name);
2195 if (Sym) {
2196 // Diagnose assignment to a label.
2197 //
2198 // FIXME: Diagnostics. Note the location of the definition as a label.
2199 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002200 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002201 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2202 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002203 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002204 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2205 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002206 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002207 return Error(EqualLoc, "redefinition of '" + Name + "'");
2208 else if (!Sym->isVariable())
2209 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002210 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002211 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002212 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002213
2214 // Don't count these checks as uses.
2215 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002216 } else if (Name == ".") {
2217 if (Out.EmitValueToOffset(Value, 0)) {
2218 Error(EqualLoc, "expected absolute expression");
2219 eatToEndOfStatement();
2220 }
2221 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002222 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002223 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002224
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002225 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002226 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002227 if (NoDeadStrip)
2228 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2229
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002230 return false;
2231}
2232
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002233/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002234/// ::= identifier
2235/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002236bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002237 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002238 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2239 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002240 // handle this as a context dependent token, instead we detect adjacent tokens
2241 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002242 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2243 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002244
Hans Wennborgce69d772013-10-18 20:46:28 +00002245 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002246 Lex();
2247 if (Lexer.isNot(AsmToken::Identifier))
2248 return true;
2249
Hans Wennborgce69d772013-10-18 20:46:28 +00002250 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2251 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002252 return true;
2253
2254 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002255 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002256 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002257 Lex();
2258 return false;
2259 }
2260
Jim Grosbach4b905842013-09-20 23:08:21 +00002261 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002262 return true;
2263
Sean Callanan936b0d32010-01-19 21:44:56 +00002264 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002265
Sean Callanan686ed8d2010-01-19 20:22:31 +00002266 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002267
2268 return false;
2269}
2270
Jim Grosbach4b905842013-09-20 23:08:21 +00002271/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002272/// ::= .equ identifier ',' expression
2273/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002274/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002275bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002276 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002277
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002278 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002279 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002280
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002281 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002282 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002283 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002284
Jim Grosbach4b905842013-09-20 23:08:21 +00002285 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002286}
2287
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002288bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002289 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002290
2291 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002292 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002293 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2294 if (Str[i] != '\\') {
2295 Data += Str[i];
2296 continue;
2297 }
2298
2299 // Recognize escaped characters. Note that this escape semantics currently
2300 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2301 ++i;
2302 if (i == e)
2303 return TokError("unexpected backslash at end of string");
2304
2305 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002306 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002307 // Consume up to three octal characters.
2308 unsigned Value = Str[i] - '0';
2309
Jim Grosbach4b905842013-09-20 23:08:21 +00002310 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002311 ++i;
2312 Value = Value * 8 + (Str[i] - '0');
2313
Jim Grosbach4b905842013-09-20 23:08:21 +00002314 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002315 ++i;
2316 Value = Value * 8 + (Str[i] - '0');
2317 }
2318 }
2319
2320 if (Value > 255)
2321 return TokError("invalid octal escape sequence (out of range)");
2322
Jim Grosbach4b905842013-09-20 23:08:21 +00002323 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002324 continue;
2325 }
2326
2327 // Otherwise recognize individual escapes.
2328 switch (Str[i]) {
2329 default:
2330 // Just reject invalid escape sequences for now.
2331 return TokError("invalid escape sequence (unrecognized character)");
2332
2333 case 'b': Data += '\b'; break;
2334 case 'f': Data += '\f'; break;
2335 case 'n': Data += '\n'; break;
2336 case 'r': Data += '\r'; break;
2337 case 't': Data += '\t'; break;
2338 case '"': Data += '"'; break;
2339 case '\\': Data += '\\'; break;
2340 }
2341 }
2342
2343 return false;
2344}
2345
Jim Grosbach4b905842013-09-20 23:08:21 +00002346/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002347/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002348bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002349 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002350 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002351
Daniel Dunbara10e5192009-06-24 23:30:00 +00002352 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002353 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002354 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002355
Daniel Dunbaref668c12009-08-14 18:19:52 +00002356 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002357 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002358 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002359
Rafael Espindola64e1af82013-07-02 15:49:13 +00002360 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002361 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002362 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002363
Sean Callanan686ed8d2010-01-19 20:22:31 +00002364 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002365
2366 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002367 break;
2368
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002369 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002370 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002371 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002372 }
2373 }
2374
Sean Callanan686ed8d2010-01-19 20:22:31 +00002375 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002376 return false;
2377}
2378
Jim Grosbach4b905842013-09-20 23:08:21 +00002379/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002380/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002381bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002382 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002383 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002384
Daniel Dunbara10e5192009-06-24 23:30:00 +00002385 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002386 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002387 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002388 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002389 return true;
2390
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002391 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002392 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2393 assert(Size <= 8 && "Invalid size");
2394 uint64_t IntValue = MCE->getValue();
2395 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2396 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002397 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002398 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002399 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002400
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002401 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002402 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002403
Daniel Dunbara10e5192009-06-24 23:30:00 +00002404 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002405 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002406 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002407 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002408 }
2409 }
2410
Sean Callanan686ed8d2010-01-19 20:22:31 +00002411 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002412 return false;
2413}
2414
David Woodhoused6de0d92014-02-01 16:20:59 +00002415/// ParseDirectiveOctaValue
2416/// ::= .octa [ hexconstant (, hexconstant)* ]
2417bool AsmParser::parseDirectiveOctaValue() {
2418 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2419 checkForValidSection();
2420
2421 for (;;) {
2422 if (Lexer.getKind() == AsmToken::Error)
2423 return true;
2424 if (Lexer.getKind() != AsmToken::Integer &&
2425 Lexer.getKind() != AsmToken::BigNum)
2426 return TokError("unknown token in expression");
2427
2428 SMLoc ExprLoc = getLexer().getLoc();
2429 APInt IntValue = getTok().getAPIntVal();
2430 Lex();
2431
2432 uint64_t hi, lo;
2433 if (IntValue.isIntN(64)) {
2434 hi = 0;
2435 lo = IntValue.getZExtValue();
2436 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002437 // It might actually have more than 128 bits, but the top ones are zero.
2438 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002439 lo = IntValue.getLoBits(64).getZExtValue();
2440 } else
2441 return Error(ExprLoc, "literal value out of range for directive");
2442
2443 if (MAI.isLittleEndian()) {
2444 getStreamer().EmitIntValue(lo, 8);
2445 getStreamer().EmitIntValue(hi, 8);
2446 } else {
2447 getStreamer().EmitIntValue(hi, 8);
2448 getStreamer().EmitIntValue(lo, 8);
2449 }
2450
2451 if (getLexer().is(AsmToken::EndOfStatement))
2452 break;
2453
2454 // FIXME: Improve diagnostic.
2455 if (getLexer().isNot(AsmToken::Comma))
2456 return TokError("unexpected token in directive");
2457 Lex();
2458 }
2459 }
2460
2461 Lex();
2462 return false;
2463}
2464
Jim Grosbach4b905842013-09-20 23:08:21 +00002465/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002466/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002467bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002468 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002469 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002470
2471 for (;;) {
2472 // We don't truly support arithmetic on floating point expressions, so we
2473 // have to manually parse unary prefixes.
2474 bool IsNeg = false;
2475 if (getLexer().is(AsmToken::Minus)) {
2476 Lex();
2477 IsNeg = true;
2478 } else if (getLexer().is(AsmToken::Plus))
2479 Lex();
2480
Michael J. Spencer530ce852010-10-09 11:00:50 +00002481 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002482 getLexer().isNot(AsmToken::Real) &&
2483 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002484 return TokError("unexpected token in directive");
2485
2486 // Convert to an APFloat.
2487 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002488 StringRef IDVal = getTok().getString();
2489 if (getLexer().is(AsmToken::Identifier)) {
2490 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2491 Value = APFloat::getInf(Semantics);
2492 else if (!IDVal.compare_lower("nan"))
2493 Value = APFloat::getNaN(Semantics, false, ~0);
2494 else
2495 return TokError("invalid floating point literal");
2496 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002497 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002498 return TokError("invalid floating point literal");
2499 if (IsNeg)
2500 Value.changeSign();
2501
2502 // Consume the numeric token.
2503 Lex();
2504
2505 // Emit the value as an integer.
2506 APInt AsInt = Value.bitcastToAPInt();
2507 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002508 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002509
2510 if (getLexer().is(AsmToken::EndOfStatement))
2511 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002512
Daniel Dunbar2af16532010-09-24 01:59:56 +00002513 if (getLexer().isNot(AsmToken::Comma))
2514 return TokError("unexpected token in directive");
2515 Lex();
2516 }
2517 }
2518
2519 Lex();
2520 return false;
2521}
2522
Jim Grosbach4b905842013-09-20 23:08:21 +00002523/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002524/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002525bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002526 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002527
2528 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002529 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002530 return true;
2531
Rafael Espindolab91bac62010-10-05 19:42:57 +00002532 int64_t Val = 0;
2533 if (getLexer().is(AsmToken::Comma)) {
2534 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002535 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002536 return true;
2537 }
2538
Rafael Espindola922e3f42010-09-16 15:03:59 +00002539 if (getLexer().isNot(AsmToken::EndOfStatement))
2540 return TokError("unexpected token in '.zero' directive");
2541
2542 Lex();
2543
Rafael Espindola64e1af82013-07-02 15:49:13 +00002544 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002545
2546 return false;
2547}
2548
Jim Grosbach4b905842013-09-20 23:08:21 +00002549/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002550/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002551bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002552 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002553
David Majnemer522d3db2014-02-01 07:19:38 +00002554 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002555 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002556 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002557 return true;
2558
David Majnemer522d3db2014-02-01 07:19:38 +00002559 if (NumValues < 0) {
2560 Warning(RepeatLoc,
2561 "'.fill' directive with negative repeat count has no effect");
2562 NumValues = 0;
2563 }
2564
Roman Divackye33098f2013-09-24 17:44:41 +00002565 int64_t FillSize = 1;
2566 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002567
David Majnemer522d3db2014-02-01 07:19:38 +00002568 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002569 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2570 if (getLexer().isNot(AsmToken::Comma))
2571 return TokError("unexpected token in '.fill' directive");
2572 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002573
David Majnemer522d3db2014-02-01 07:19:38 +00002574 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002575 if (parseAbsoluteExpression(FillSize))
2576 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002577
Roman Divackye33098f2013-09-24 17:44:41 +00002578 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2579 if (getLexer().isNot(AsmToken::Comma))
2580 return TokError("unexpected token in '.fill' directive");
2581 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002582
David Majnemer522d3db2014-02-01 07:19:38 +00002583 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002584 if (parseAbsoluteExpression(FillExpr))
2585 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002586
Roman Divackye33098f2013-09-24 17:44:41 +00002587 if (getLexer().isNot(AsmToken::EndOfStatement))
2588 return TokError("unexpected token in '.fill' directive");
2589
2590 Lex();
2591 }
2592 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002593
David Majnemer522d3db2014-02-01 07:19:38 +00002594 if (FillSize < 0) {
2595 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2596 NumValues = 0;
2597 }
2598 if (FillSize > 8) {
2599 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2600 FillSize = 8;
2601 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002602
David Majnemer522d3db2014-02-01 07:19:38 +00002603 if (!isUInt<32>(FillExpr) && FillSize > 4)
2604 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2605
2606 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2607 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2608
2609 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2610 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
Alexey Samsonove5864c62014-08-20 22:46:38 +00002611 if (NonZeroFillSize < FillSize)
2612 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
David Majnemer522d3db2014-02-01 07:19:38 +00002613 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002614
2615 return false;
2616}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002617
Jim Grosbach4b905842013-09-20 23:08:21 +00002618/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002619/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002620bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002621 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002622
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002623 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002624 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002625 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002626 return true;
2627
2628 // Parse optional fill expression.
2629 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002630 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2631 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002632 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002633 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002634
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002635 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002636 return true;
2637
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002638 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002639 return TokError("unexpected token in '.org' directive");
2640 }
2641
Sean Callanan686ed8d2010-01-19 20:22:31 +00002642 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002643
Jim Grosbachb5912772012-01-27 00:37:08 +00002644 // Only limited forms of relocatable expressions are accepted here, it
2645 // has to be relative to the current section. The streamer will return
2646 // 'true' if the expression wasn't evaluatable.
2647 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2648 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002649
2650 return false;
2651}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002652
Jim Grosbach4b905842013-09-20 23:08:21 +00002653/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002654/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002655bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002656 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002657
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002658 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002659 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002660 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002661 return true;
2662
2663 SMLoc MaxBytesLoc;
2664 bool HasFillExpr = false;
2665 int64_t FillExpr = 0;
2666 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002667 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2668 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002669 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002670 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002671
2672 // The fill expression can be omitted while specifying a maximum number of
2673 // alignment bytes, e.g:
2674 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002675 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002676 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002677 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002678 return true;
2679 }
2680
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002681 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2682 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002683 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002684 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002685
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002686 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002687 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002688 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002689
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002690 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002691 return TokError("unexpected token in directive");
2692 }
2693 }
2694
Sean Callanan686ed8d2010-01-19 20:22:31 +00002695 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002696
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002697 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002698 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002699
2700 // Compute alignment in bytes.
2701 if (IsPow2) {
2702 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002703 if (Alignment >= 32) {
2704 Error(AlignmentLoc, "invalid alignment value");
2705 Alignment = 31;
2706 }
2707
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002708 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002709 } else {
2710 // Reject alignments that aren't a power of two, for gas compatibility.
2711 if (!isPowerOf2_64(Alignment))
2712 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002713 }
2714
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002715 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002716 if (MaxBytesLoc.isValid()) {
2717 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002718 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002719 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002720 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002721 }
2722
2723 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002724 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002725 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002726 MaxBytesToFill = 0;
2727 }
2728 }
2729
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002730 // Check whether we should use optimal code alignment for this .align
2731 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002732 const MCSection *Section = getStreamer().getCurrentSection().first;
2733 assert(Section && "must have section to emit alignment");
2734 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002735 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2736 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002737 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002738 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002739 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002740 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2741 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002742 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002743
2744 return false;
2745}
2746
Jim Grosbach4b905842013-09-20 23:08:21 +00002747/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002748/// ::= .file [number] filename
2749/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002750bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002751 // FIXME: I'm not sure what this is.
2752 int64_t FileNumber = -1;
2753 SMLoc FileNumberLoc = getLexer().getLoc();
2754 if (getLexer().is(AsmToken::Integer)) {
2755 FileNumber = getTok().getIntVal();
2756 Lex();
2757
2758 if (FileNumber < 1)
2759 return TokError("file number less than one");
2760 }
2761
2762 if (getLexer().isNot(AsmToken::String))
2763 return TokError("unexpected token in '.file' directive");
2764
2765 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002766 // Allow the strings to have escaped octal character sequence.
2767 std::string Path = getTok().getString();
2768 if (parseEscapedString(Path))
2769 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002770 Lex();
2771
2772 StringRef Directory;
2773 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002774 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002775 if (getLexer().is(AsmToken::String)) {
2776 if (FileNumber == -1)
2777 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002778 if (parseEscapedString(FilenameData))
2779 return true;
2780 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002781 Directory = Path;
2782 Lex();
2783 } else {
2784 Filename = Path;
2785 }
2786
2787 if (getLexer().isNot(AsmToken::EndOfStatement))
2788 return TokError("unexpected token in '.file' directive");
2789
2790 if (FileNumber == -1)
2791 getStreamer().EmitFileDirective(Filename);
2792 else {
2793 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002794 Error(DirectiveLoc,
2795 "input can't have .file dwarf directives when -g is "
2796 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002797
David Blaikiec714ef42014-03-17 01:52:11 +00002798 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2799 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002800 Error(FileNumberLoc, "file number already allocated");
2801 }
2802
2803 return false;
2804}
2805
Jim Grosbach4b905842013-09-20 23:08:21 +00002806/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002807/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002808bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002809 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2810 if (getLexer().isNot(AsmToken::Integer))
2811 return TokError("unexpected token in '.line' directive");
2812
2813 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002814 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002815 Lex();
2816
2817 // FIXME: Do something with the .line.
2818 }
2819
2820 if (getLexer().isNot(AsmToken::EndOfStatement))
2821 return TokError("unexpected token in '.line' directive");
2822
2823 return false;
2824}
2825
Jim Grosbach4b905842013-09-20 23:08:21 +00002826/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002827/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2828/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2829/// The first number is a file number, must have been previously assigned with
2830/// a .file directive, the second number is the line number and optionally the
2831/// third number is a column position (zero if not specified). The remaining
2832/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002833bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002834 if (getLexer().isNot(AsmToken::Integer))
2835 return TokError("unexpected token in '.loc' directive");
2836 int64_t FileNumber = getTok().getIntVal();
2837 if (FileNumber < 1)
2838 return TokError("file number less than one in '.loc' directive");
2839 if (!getContext().isValidDwarfFileNumber(FileNumber))
2840 return TokError("unassigned file number in '.loc' directive");
2841 Lex();
2842
2843 int64_t LineNumber = 0;
2844 if (getLexer().is(AsmToken::Integer)) {
2845 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002846 if (LineNumber < 0)
2847 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002848 Lex();
2849 }
2850
2851 int64_t ColumnPos = 0;
2852 if (getLexer().is(AsmToken::Integer)) {
2853 ColumnPos = getTok().getIntVal();
2854 if (ColumnPos < 0)
2855 return TokError("column position less than zero in '.loc' directive");
2856 Lex();
2857 }
2858
2859 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2860 unsigned Isa = 0;
2861 int64_t Discriminator = 0;
2862 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2863 for (;;) {
2864 if (getLexer().is(AsmToken::EndOfStatement))
2865 break;
2866
2867 StringRef Name;
2868 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002869 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002870 return TokError("unexpected token in '.loc' directive");
2871
2872 if (Name == "basic_block")
2873 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2874 else if (Name == "prologue_end")
2875 Flags |= DWARF2_FLAG_PROLOGUE_END;
2876 else if (Name == "epilogue_begin")
2877 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2878 else if (Name == "is_stmt") {
2879 Loc = getTok().getLoc();
2880 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002881 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002882 return true;
2883 // The expression must be the constant 0 or 1.
2884 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2885 int Value = MCE->getValue();
2886 if (Value == 0)
2887 Flags &= ~DWARF2_FLAG_IS_STMT;
2888 else if (Value == 1)
2889 Flags |= DWARF2_FLAG_IS_STMT;
2890 else
2891 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002892 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002893 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2894 }
Craig Topperf15655b2013-04-22 04:22:40 +00002895 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002896 Loc = getTok().getLoc();
2897 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002898 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002899 return true;
2900 // The expression must be a constant greater or equal to 0.
2901 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2902 int Value = MCE->getValue();
2903 if (Value < 0)
2904 return Error(Loc, "isa number less than zero");
2905 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002906 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002907 return Error(Loc, "isa number not a constant value");
2908 }
Craig Topperf15655b2013-04-22 04:22:40 +00002909 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002910 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002911 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002912 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002913 return Error(Loc, "unknown sub-directive in '.loc' directive");
2914 }
2915
2916 if (getLexer().is(AsmToken::EndOfStatement))
2917 break;
2918 }
2919 }
2920
2921 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2922 Isa, Discriminator, StringRef());
2923
2924 return false;
2925}
2926
Jim Grosbach4b905842013-09-20 23:08:21 +00002927/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002928/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002929bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002930 return TokError("unsupported directive '.stabs'");
2931}
2932
Jim Grosbach4b905842013-09-20 23:08:21 +00002933/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002934/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002935bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002936 StringRef Name;
2937 bool EH = false;
2938 bool Debug = false;
2939
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002940 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002941 return TokError("Expected an identifier");
2942
2943 if (Name == ".eh_frame")
2944 EH = true;
2945 else if (Name == ".debug_frame")
2946 Debug = true;
2947
2948 if (getLexer().is(AsmToken::Comma)) {
2949 Lex();
2950
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002951 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002952 return TokError("Expected an identifier");
2953
2954 if (Name == ".eh_frame")
2955 EH = true;
2956 else if (Name == ".debug_frame")
2957 Debug = true;
2958 }
2959
2960 getStreamer().EmitCFISections(EH, Debug);
2961 return false;
2962}
2963
Jim Grosbach4b905842013-09-20 23:08:21 +00002964/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002965/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002966bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002967 StringRef Simple;
2968 if (getLexer().isNot(AsmToken::EndOfStatement))
2969 if (parseIdentifier(Simple) || Simple != "simple")
2970 return TokError("unexpected token in .cfi_startproc directive");
2971
2972 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002973 return false;
2974}
2975
Jim Grosbach4b905842013-09-20 23:08:21 +00002976/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002977/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002978bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002979 getStreamer().EmitCFIEndProc();
2980 return false;
2981}
2982
Jim Grosbach4b905842013-09-20 23:08:21 +00002983/// \brief parse register name or number.
2984bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002985 SMLoc DirectiveLoc) {
2986 unsigned RegNo;
2987
2988 if (getLexer().isNot(AsmToken::Integer)) {
2989 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2990 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002991 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002992 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002993 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002994
2995 return false;
2996}
2997
Jim Grosbach4b905842013-09-20 23:08:21 +00002998/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002999/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003000bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003001 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003002 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003003 return true;
3004
3005 if (getLexer().isNot(AsmToken::Comma))
3006 return TokError("unexpected token in directive");
3007 Lex();
3008
3009 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003010 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003011 return true;
3012
3013 getStreamer().EmitCFIDefCfa(Register, Offset);
3014 return false;
3015}
3016
Jim Grosbach4b905842013-09-20 23:08:21 +00003017/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003018/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003019bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003020 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003021 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003022 return true;
3023
3024 getStreamer().EmitCFIDefCfaOffset(Offset);
3025 return false;
3026}
3027
Jim Grosbach4b905842013-09-20 23:08:21 +00003028/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003029/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003030bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003031 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003032 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003033 return true;
3034
3035 if (getLexer().isNot(AsmToken::Comma))
3036 return TokError("unexpected token in directive");
3037 Lex();
3038
3039 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003040 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003041 return true;
3042
3043 getStreamer().EmitCFIRegister(Register1, Register2);
3044 return false;
3045}
3046
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003047/// parseDirectiveCFIWindowSave
3048/// ::= .cfi_window_save
3049bool AsmParser::parseDirectiveCFIWindowSave() {
3050 getStreamer().EmitCFIWindowSave();
3051 return false;
3052}
3053
Jim Grosbach4b905842013-09-20 23:08:21 +00003054/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003055/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003056bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003057 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003058 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003059 return true;
3060
3061 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3062 return false;
3063}
3064
Jim Grosbach4b905842013-09-20 23:08:21 +00003065/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003066/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003067bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003068 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003069 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003070 return true;
3071
3072 getStreamer().EmitCFIDefCfaRegister(Register);
3073 return false;
3074}
3075
Jim Grosbach4b905842013-09-20 23:08:21 +00003076/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003077/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003078bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003079 int64_t Register = 0;
3080 int64_t Offset = 0;
3081
Jim Grosbach4b905842013-09-20 23:08:21 +00003082 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003083 return true;
3084
3085 if (getLexer().isNot(AsmToken::Comma))
3086 return TokError("unexpected token in directive");
3087 Lex();
3088
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003089 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003090 return true;
3091
3092 getStreamer().EmitCFIOffset(Register, Offset);
3093 return false;
3094}
3095
Jim Grosbach4b905842013-09-20 23:08:21 +00003096/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003097/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003098bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003099 int64_t Register = 0;
3100
Jim Grosbach4b905842013-09-20 23:08:21 +00003101 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003102 return true;
3103
3104 if (getLexer().isNot(AsmToken::Comma))
3105 return TokError("unexpected token in directive");
3106 Lex();
3107
3108 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003109 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003110 return true;
3111
3112 getStreamer().EmitCFIRelOffset(Register, Offset);
3113 return false;
3114}
3115
3116static bool isValidEncoding(int64_t Encoding) {
3117 if (Encoding & ~0xff)
3118 return false;
3119
3120 if (Encoding == dwarf::DW_EH_PE_omit)
3121 return true;
3122
3123 const unsigned Format = Encoding & 0xf;
3124 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3125 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3126 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3127 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3128 return false;
3129
3130 const unsigned Application = Encoding & 0x70;
3131 if (Application != dwarf::DW_EH_PE_absptr &&
3132 Application != dwarf::DW_EH_PE_pcrel)
3133 return false;
3134
3135 return true;
3136}
3137
Jim Grosbach4b905842013-09-20 23:08:21 +00003138/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003139/// IsPersonality true for cfi_personality, false for cfi_lsda
3140/// ::= .cfi_personality encoding, [symbol_name]
3141/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003142bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003143 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003144 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003145 return true;
3146 if (Encoding == dwarf::DW_EH_PE_omit)
3147 return false;
3148
3149 if (!isValidEncoding(Encoding))
3150 return TokError("unsupported encoding.");
3151
3152 if (getLexer().isNot(AsmToken::Comma))
3153 return TokError("unexpected token in directive");
3154 Lex();
3155
3156 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003157 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003158 return TokError("expected identifier in directive");
3159
3160 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3161
3162 if (IsPersonality)
3163 getStreamer().EmitCFIPersonality(Sym, Encoding);
3164 else
3165 getStreamer().EmitCFILsda(Sym, Encoding);
3166 return false;
3167}
3168
Jim Grosbach4b905842013-09-20 23:08:21 +00003169/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003170/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003171bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003172 getStreamer().EmitCFIRememberState();
3173 return false;
3174}
3175
Jim Grosbach4b905842013-09-20 23:08:21 +00003176/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003177/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003178bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003179 getStreamer().EmitCFIRestoreState();
3180 return false;
3181}
3182
Jim Grosbach4b905842013-09-20 23:08:21 +00003183/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003184/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003185bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003186 int64_t Register = 0;
3187
Jim Grosbach4b905842013-09-20 23:08:21 +00003188 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003189 return true;
3190
3191 getStreamer().EmitCFISameValue(Register);
3192 return false;
3193}
3194
Jim Grosbach4b905842013-09-20 23:08:21 +00003195/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003196/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003197bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003198 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003199 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003200 return true;
3201
3202 getStreamer().EmitCFIRestore(Register);
3203 return false;
3204}
3205
Jim Grosbach4b905842013-09-20 23:08:21 +00003206/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003207/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003208bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003209 std::string Values;
3210 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003211 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003212 return true;
3213
3214 Values.push_back((uint8_t)CurrValue);
3215
3216 while (getLexer().is(AsmToken::Comma)) {
3217 Lex();
3218
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003219 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003220 return true;
3221
3222 Values.push_back((uint8_t)CurrValue);
3223 }
3224
3225 getStreamer().EmitCFIEscape(Values);
3226 return false;
3227}
3228
Jim Grosbach4b905842013-09-20 23:08:21 +00003229/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003230/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003231bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003232 if (getLexer().isNot(AsmToken::EndOfStatement))
3233 return Error(getLexer().getLoc(),
3234 "unexpected token in '.cfi_signal_frame'");
3235
3236 getStreamer().EmitCFISignalFrame();
3237 return false;
3238}
3239
Jim Grosbach4b905842013-09-20 23:08:21 +00003240/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003241/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003242bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003243 int64_t Register = 0;
3244
Jim Grosbach4b905842013-09-20 23:08:21 +00003245 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003246 return true;
3247
3248 getStreamer().EmitCFIUndefined(Register);
3249 return false;
3250}
3251
Jim Grosbach4b905842013-09-20 23:08:21 +00003252/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003253/// ::= .macros_on
3254/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003255bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003256 if (getLexer().isNot(AsmToken::EndOfStatement))
3257 return Error(getLexer().getLoc(),
3258 "unexpected token in '" + Directive + "' directive");
3259
Jim Grosbach4b905842013-09-20 23:08:21 +00003260 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003261 return false;
3262}
3263
Jim Grosbach4b905842013-09-20 23:08:21 +00003264/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003265/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003266bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003267 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003268 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003269 return TokError("expected identifier in '.macro' directive");
3270
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003271 if (getLexer().is(AsmToken::Comma))
3272 Lex();
3273
Eli Bendersky17233942013-01-15 22:59:42 +00003274 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003275 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003276
3277 if (Parameters.size() && Parameters.back().Vararg)
3278 return Error(Lexer.getLoc(),
3279 "Vararg parameter '" + Parameters.back().Name +
3280 "' should be last one in the list of parameters.");
3281
David Majnemer91fc4c22014-01-29 18:57:46 +00003282 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003283 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003284 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003285
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003286 if (Lexer.is(AsmToken::Colon)) {
3287 Lex(); // consume ':'
3288
3289 SMLoc QualLoc;
3290 StringRef Qualifier;
3291
3292 QualLoc = Lexer.getLoc();
3293 if (parseIdentifier(Qualifier))
3294 return Error(QualLoc, "missing parameter qualifier for "
3295 "'" + Parameter.Name + "' in macro '" + Name + "'");
3296
3297 if (Qualifier == "req")
3298 Parameter.Required = true;
Kevin Enderbye3c13462014-08-04 23:14:37 +00003299 else if (Qualifier == "vararg")
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003300 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003301 else
3302 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3303 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3304 }
3305
David Majnemer91fc4c22014-01-29 18:57:46 +00003306 if (getLexer().is(AsmToken::Equal)) {
3307 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003308
3309 SMLoc ParamLoc;
3310
3311 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003312 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003313 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003314
3315 if (Parameter.Required)
3316 Warning(ParamLoc, "pointless default value for required parameter "
3317 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003318 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003319
3320 Parameters.push_back(Parameter);
3321
3322 if (getLexer().is(AsmToken::Comma))
3323 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003324 }
3325
3326 // Eat the end of statement.
3327 Lex();
3328
3329 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003330 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003331
3332 // Lex the macro definition.
3333 for (;;) {
3334 // Check whether we have reached the end of the file.
3335 if (getLexer().is(AsmToken::Eof))
3336 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3337
3338 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003339 if (getLexer().is(AsmToken::Identifier)) {
3340 if (getTok().getIdentifier() == ".endm" ||
3341 getTok().getIdentifier() == ".endmacro") {
3342 if (MacroDepth == 0) { // Outermost macro.
3343 EndToken = getTok();
3344 Lex();
3345 if (getLexer().isNot(AsmToken::EndOfStatement))
3346 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3347 "' directive");
3348 break;
3349 } else {
3350 // Otherwise we just found the end of an inner macro.
3351 --MacroDepth;
3352 }
3353 } else if (getTok().getIdentifier() == ".macro") {
3354 // We allow nested macros. Those aren't instantiated until the outermost
3355 // macro is expanded so just ignore them for now.
3356 ++MacroDepth;
3357 }
Eli Bendersky17233942013-01-15 22:59:42 +00003358 }
3359
3360 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003361 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003362 }
3363
Jim Grosbach4b905842013-09-20 23:08:21 +00003364 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003365 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3366 }
3367
3368 const char *BodyStart = StartToken.getLoc().getPointer();
3369 const char *BodyEnd = EndToken.getLoc().getPointer();
3370 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003371 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3372 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003373 return false;
3374}
3375
Jim Grosbach4b905842013-09-20 23:08:21 +00003376/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003377///
3378/// With the support added for named parameters there may be code out there that
3379/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003380/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003381/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003382/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003383/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3384/// warning that the positional parameter found in body which have no effect.
3385/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003386/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003387/// intended or change the macro to use the named parameters. It is possible
3388/// this warning will trigger when the none of the named parameters are used
3389/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003390void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003391 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003392 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003393 // If this macro is not defined with named parameters the warning we are
3394 // checking for here doesn't apply.
3395 unsigned NParameters = Parameters.size();
3396 if (NParameters == 0)
3397 return;
3398
3399 bool NamedParametersFound = false;
3400 bool PositionalParametersFound = false;
3401
3402 // Look at the body of the macro for use of both the named parameters and what
3403 // are likely to be positional parameters. This is what expandMacro() is
3404 // doing when it finds the parameters in the body.
3405 while (!Body.empty()) {
3406 // Scan for the next possible parameter.
3407 std::size_t End = Body.size(), Pos = 0;
3408 for (; Pos != End; ++Pos) {
3409 // Check for a substitution or escape.
3410 // This macro is defined with parameters, look for \foo, \bar, etc.
3411 if (Body[Pos] == '\\' && Pos + 1 != End)
3412 break;
3413
3414 // This macro should have parameters, but look for $0, $1, ..., $n too.
3415 if (Body[Pos] != '$' || Pos + 1 == End)
3416 continue;
3417 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003418 if (Next == '$' || Next == 'n' ||
3419 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003420 break;
3421 }
3422
3423 // Check if we reached the end.
3424 if (Pos == End)
3425 break;
3426
3427 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003428 switch (Body[Pos + 1]) {
3429 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003430 case '$':
3431 break;
3432
Jim Grosbach4b905842013-09-20 23:08:21 +00003433 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003434 case 'n':
3435 PositionalParametersFound = true;
3436 break;
3437
Jim Grosbach4b905842013-09-20 23:08:21 +00003438 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003439 default: {
3440 PositionalParametersFound = true;
3441 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003442 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003443 }
3444 Pos += 2;
3445 } else {
3446 unsigned I = Pos + 1;
3447 while (isIdentifierChar(Body[I]) && I + 1 != End)
3448 ++I;
3449
Jim Grosbach4b905842013-09-20 23:08:21 +00003450 const char *Begin = Body.data() + Pos + 1;
3451 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003452 unsigned Index = 0;
3453 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003454 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003455 break;
3456
3457 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003458 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3459 Pos += 3;
3460 else {
3461 Pos = I;
3462 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003463 } else {
3464 NamedParametersFound = true;
3465 Pos += 1 + Argument.size();
3466 }
3467 }
3468 // Update the scan point.
3469 Body = Body.substr(Pos);
3470 }
3471
3472 if (!NamedParametersFound && PositionalParametersFound)
3473 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3474 "used in macro body, possible positional parameter "
3475 "found in body which will have no effect");
3476}
3477
Nico Weber155dccd12014-07-24 17:08:39 +00003478/// parseDirectiveExitMacro
3479/// ::= .exitm
3480bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
3481 if (getLexer().isNot(AsmToken::EndOfStatement))
3482 return TokError("unexpected token in '" + Directive + "' directive");
3483
3484 if (!isInsideMacroInstantiation())
3485 return TokError("unexpected '" + Directive + "' in file, "
3486 "no current macro definition");
3487
3488 // Exit all conditionals that are active in the current macro.
3489 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
3490 TheCondState = TheCondStack.back();
3491 TheCondStack.pop_back();
3492 }
3493
3494 handleMacroExit();
3495 return false;
3496}
3497
Jim Grosbach4b905842013-09-20 23:08:21 +00003498/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003499/// ::= .endm
3500/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003501bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003502 if (getLexer().isNot(AsmToken::EndOfStatement))
3503 return TokError("unexpected token in '" + Directive + "' directive");
3504
3505 // If we are inside a macro instantiation, terminate the current
3506 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003507 if (isInsideMacroInstantiation()) {
3508 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003509 return false;
3510 }
3511
3512 // Otherwise, this .endmacro is a stray entry in the file; well formed
3513 // .endmacro directives are handled during the macro definition parsing.
3514 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003515 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003516}
3517
Jim Grosbach4b905842013-09-20 23:08:21 +00003518/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003519/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003520bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003521 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003522 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003523 return TokError("expected identifier in '.purgem' directive");
3524
3525 if (getLexer().isNot(AsmToken::EndOfStatement))
3526 return TokError("unexpected token in '.purgem' directive");
3527
Jim Grosbach4b905842013-09-20 23:08:21 +00003528 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003529 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3530
Jim Grosbach4b905842013-09-20 23:08:21 +00003531 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003532 return false;
3533}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003534
Jim Grosbach4b905842013-09-20 23:08:21 +00003535/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003536/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003537bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003538 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003539
3540 // Expect a single argument: an expression that evaluates to a constant
3541 // in the inclusive range 0-30.
3542 SMLoc ExprLoc = getLexer().getLoc();
3543 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003544 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003545 return true;
3546 else if (getLexer().isNot(AsmToken::EndOfStatement))
3547 return TokError("unexpected token after expression in"
3548 " '.bundle_align_mode' directive");
3549 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3550 return Error(ExprLoc,
3551 "invalid bundle alignment size (expected between 0 and 30)");
3552
3553 Lex();
3554
3555 // Because of AlignSizePow2's verified range we can safely truncate it to
3556 // unsigned.
3557 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3558 return false;
3559}
3560
Jim Grosbach4b905842013-09-20 23:08:21 +00003561/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003562/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003563bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003564 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003565 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003566
Eli Bendersky802b6282013-01-07 21:51:08 +00003567 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3568 StringRef Option;
3569 SMLoc Loc = getTok().getLoc();
3570 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003571 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003572
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003573 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003574 return Error(Loc, kInvalidOptionError);
3575
3576 if (Option != "align_to_end")
3577 return Error(Loc, kInvalidOptionError);
3578 else if (getLexer().isNot(AsmToken::EndOfStatement))
3579 return Error(Loc,
3580 "unexpected token after '.bundle_lock' directive option");
3581 AlignToEnd = true;
3582 }
3583
Eli Benderskyf483ff92012-12-20 19:05:53 +00003584 Lex();
3585
Eli Bendersky802b6282013-01-07 21:51:08 +00003586 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003587 return false;
3588}
3589
Jim Grosbach4b905842013-09-20 23:08:21 +00003590/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003591/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003592bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003593 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003594
3595 if (getLexer().isNot(AsmToken::EndOfStatement))
3596 return TokError("unexpected token in '.bundle_unlock' directive");
3597 Lex();
3598
3599 getStreamer().EmitBundleUnlock();
3600 return false;
3601}
3602
Jim Grosbach4b905842013-09-20 23:08:21 +00003603/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003604/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003605bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003606 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003607
3608 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003609 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003610 return true;
3611
3612 int64_t FillExpr = 0;
3613 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3614 if (getLexer().isNot(AsmToken::Comma))
3615 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3616 Lex();
3617
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003618 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003619 return true;
3620
3621 if (getLexer().isNot(AsmToken::EndOfStatement))
3622 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3623 }
3624
3625 Lex();
3626
3627 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003628 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3629 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003630
3631 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003632 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003633
3634 return false;
3635}
3636
Jim Grosbach4b905842013-09-20 23:08:21 +00003637/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003638/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003639bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003640 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003641 const MCExpr *Value;
3642
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003643 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003644 return true;
3645
3646 if (getLexer().isNot(AsmToken::EndOfStatement))
3647 return TokError("unexpected token in directive");
3648
3649 if (Signed)
3650 getStreamer().EmitSLEB128Value(Value);
3651 else
3652 getStreamer().EmitULEB128Value(Value);
3653
3654 return false;
3655}
3656
Jim Grosbach4b905842013-09-20 23:08:21 +00003657/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003658/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003659bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003660 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003661 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003662 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003663 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003664
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003665 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003666 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003667
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003668 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003669
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003670 // Assembler local symbols don't make any sense here. Complain loudly.
3671 if (Sym->isTemporary())
3672 return Error(Loc, "non-local symbol required in directive");
3673
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003674 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3675 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003676
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003677 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003678 break;
3679
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003680 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003681 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003682 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003683 }
3684 }
3685
Sean Callanan686ed8d2010-01-19 20:22:31 +00003686 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003687 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003688}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003689
Jim Grosbach4b905842013-09-20 23:08:21 +00003690/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003691/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003692bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003693 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003694
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003695 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003696 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003697 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003698 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003699
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003700 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003701 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003702
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003703 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003704 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003705 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003706
3707 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003708 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003709 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003710 return true;
3711
3712 int64_t Pow2Alignment = 0;
3713 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003714 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003715 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003716 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003717 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003718 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003719
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003720 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3721 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003722 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3723
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003724 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003725 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3726 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003727 if (!isPowerOf2_64(Pow2Alignment))
3728 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3729 Pow2Alignment = Log2_64(Pow2Alignment);
3730 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003731 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003732
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003733 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003734 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003735
Sean Callanan686ed8d2010-01-19 20:22:31 +00003736 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003737
Chris Lattner28ad7542009-07-09 17:25:12 +00003738 // NOTE: a size of zero for a .comm should create a undefined symbol
3739 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003740 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003741 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003742 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003743
Eric Christopherbc818852010-05-14 01:38:54 +00003744 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003745 // may internally end up wanting an alignment in bytes.
3746 // FIXME: Diagnose overflow.
3747 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003748 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003749 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003750
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003751 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003752 return Error(IDLoc, "invalid symbol redefinition");
3753
Chris Lattner28ad7542009-07-09 17:25:12 +00003754 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003755 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003756 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003757 return false;
3758 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003759
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003760 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003761 return false;
3762}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003763
Jim Grosbach4b905842013-09-20 23:08:21 +00003764/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003765/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003766bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003767 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003768 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003769
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003770 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003771 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003772 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003773
Sean Callanan686ed8d2010-01-19 20:22:31 +00003774 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003775
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003776 if (Str.empty())
3777 Error(Loc, ".abort detected. Assembly stopping.");
3778 else
3779 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003780 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003781
3782 return false;
3783}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003784
Jim Grosbach4b905842013-09-20 23:08:21 +00003785/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003786/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003787bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003788 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003789 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003790
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003791 // Allow the strings to have escaped octal character sequence.
3792 std::string Filename;
3793 if (parseEscapedString(Filename))
3794 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003795 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003796 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003797
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003798 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003799 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003800
Chris Lattner693fbb82009-07-16 06:14:39 +00003801 // Attempt to switch the lexer to the included file before consuming the end
3802 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003803 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003804 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003805 return true;
3806 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003807
3808 return false;
3809}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003810
Jim Grosbach4b905842013-09-20 23:08:21 +00003811/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003812/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003813bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003814 if (getLexer().isNot(AsmToken::String))
3815 return TokError("expected string in '.incbin' directive");
3816
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003817 // Allow the strings to have escaped octal character sequence.
3818 std::string Filename;
3819 if (parseEscapedString(Filename))
3820 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003821 SMLoc IncbinLoc = getLexer().getLoc();
3822 Lex();
3823
3824 if (getLexer().isNot(AsmToken::EndOfStatement))
3825 return TokError("unexpected token in '.incbin' directive");
3826
Kevin Enderby109f25c2011-12-14 21:47:48 +00003827 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003828 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003829 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3830 return true;
3831 }
3832
3833 return false;
3834}
3835
Jim Grosbach4b905842013-09-20 23:08:21 +00003836/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003837/// ::= .if{,eq,ge,gt,le,lt,ne} expression
3838bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003839 TheCondStack.push_back(TheCondState);
3840 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003841 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003842 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003843 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003844 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003845 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003846 return true;
3847
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003848 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003849 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003850
Sean Callanan686ed8d2010-01-19 20:22:31 +00003851 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003852
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003853 switch (DirKind) {
3854 default:
3855 llvm_unreachable("unsupported directive");
3856 case DK_IF:
3857 case DK_IFNE:
3858 break;
3859 case DK_IFEQ:
3860 ExprValue = ExprValue == 0;
3861 break;
3862 case DK_IFGE:
3863 ExprValue = ExprValue >= 0;
3864 break;
3865 case DK_IFGT:
3866 ExprValue = ExprValue > 0;
3867 break;
3868 case DK_IFLE:
3869 ExprValue = ExprValue <= 0;
3870 break;
3871 case DK_IFLT:
3872 ExprValue = ExprValue < 0;
3873 break;
3874 }
3875
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003876 TheCondState.CondMet = ExprValue;
3877 TheCondState.Ignore = !TheCondState.CondMet;
3878 }
3879
3880 return false;
3881}
3882
Jim Grosbach4b905842013-09-20 23:08:21 +00003883/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003884/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003885bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003886 TheCondStack.push_back(TheCondState);
3887 TheCondState.TheCond = AsmCond::IfCond;
3888
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003889 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003890 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003891 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003892 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003893
3894 if (getLexer().isNot(AsmToken::EndOfStatement))
3895 return TokError("unexpected token in '.ifb' directive");
3896
3897 Lex();
3898
3899 TheCondState.CondMet = ExpectBlank == Str.empty();
3900 TheCondState.Ignore = !TheCondState.CondMet;
3901 }
3902
3903 return false;
3904}
3905
Jim Grosbach4b905842013-09-20 23:08:21 +00003906/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003907/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003908/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003909bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003910 TheCondStack.push_back(TheCondState);
3911 TheCondState.TheCond = AsmCond::IfCond;
3912
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003913 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003914 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003915 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003916 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003917
3918 if (getLexer().isNot(AsmToken::Comma))
3919 return TokError("unexpected token in '.ifc' directive");
3920
3921 Lex();
3922
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003923 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003924
3925 if (getLexer().isNot(AsmToken::EndOfStatement))
3926 return TokError("unexpected token in '.ifc' directive");
3927
3928 Lex();
3929
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003930 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003931 TheCondState.Ignore = !TheCondState.CondMet;
3932 }
3933
3934 return false;
3935}
3936
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003937/// parseDirectiveIfeqs
3938/// ::= .ifeqs string1, string2
3939bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc) {
3940 if (Lexer.isNot(AsmToken::String)) {
3941 TokError("expected string parameter for '.ifeqs' directive");
3942 eatToEndOfStatement();
3943 return true;
3944 }
3945
3946 StringRef String1 = getTok().getStringContents();
3947 Lex();
3948
3949 if (Lexer.isNot(AsmToken::Comma)) {
3950 TokError("expected comma after first string for '.ifeqs' directive");
3951 eatToEndOfStatement();
3952 return true;
3953 }
3954
3955 Lex();
3956
3957 if (Lexer.isNot(AsmToken::String)) {
3958 TokError("expected string parameter for '.ifeqs' directive");
3959 eatToEndOfStatement();
3960 return true;
3961 }
3962
3963 StringRef String2 = getTok().getStringContents();
3964 Lex();
3965
3966 TheCondStack.push_back(TheCondState);
3967 TheCondState.TheCond = AsmCond::IfCond;
3968 TheCondState.CondMet = String1 == String2;
3969 TheCondState.Ignore = !TheCondState.CondMet;
3970
3971 return false;
3972}
3973
Jim Grosbach4b905842013-09-20 23:08:21 +00003974/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003975/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003976bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003977 StringRef Name;
3978 TheCondStack.push_back(TheCondState);
3979 TheCondState.TheCond = AsmCond::IfCond;
3980
3981 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003982 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003983 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003984 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003985 return TokError("expected identifier after '.ifdef'");
3986
3987 Lex();
3988
3989 MCSymbol *Sym = getContext().LookupSymbol(Name);
3990
3991 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00003992 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003993 else
Craig Topper353eda42014-04-24 06:44:33 +00003994 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003995 TheCondState.Ignore = !TheCondState.CondMet;
3996 }
3997
3998 return false;
3999}
4000
Jim Grosbach4b905842013-09-20 23:08:21 +00004001/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004002/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004003bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004004 if (TheCondState.TheCond != AsmCond::IfCond &&
4005 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004006 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
4007 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004008 TheCondState.TheCond = AsmCond::ElseIfCond;
4009
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004010 bool LastIgnoreState = false;
4011 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00004012 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004013 if (LastIgnoreState || TheCondState.CondMet) {
4014 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004015 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00004016 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004017 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004018 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004019 return true;
4020
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004021 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004022 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004023
Sean Callanan686ed8d2010-01-19 20:22:31 +00004024 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004025 TheCondState.CondMet = ExprValue;
4026 TheCondState.Ignore = !TheCondState.CondMet;
4027 }
4028
4029 return false;
4030}
4031
Jim Grosbach4b905842013-09-20 23:08:21 +00004032/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004033/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004034bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004035 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004036 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004037
Sean Callanan686ed8d2010-01-19 20:22:31 +00004038 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004039
4040 if (TheCondState.TheCond != AsmCond::IfCond &&
4041 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004042 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4043 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004044 TheCondState.TheCond = AsmCond::ElseCond;
4045 bool LastIgnoreState = false;
4046 if (!TheCondStack.empty())
4047 LastIgnoreState = TheCondStack.back().Ignore;
4048 if (LastIgnoreState || TheCondState.CondMet)
4049 TheCondState.Ignore = true;
4050 else
4051 TheCondState.Ignore = false;
4052
4053 return false;
4054}
4055
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004056/// parseDirectiveEnd
4057/// ::= .end
4058bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4059 if (getLexer().isNot(AsmToken::EndOfStatement))
4060 return TokError("unexpected token in '.end' directive");
4061
4062 Lex();
4063
4064 while (Lexer.isNot(AsmToken::Eof))
4065 Lex();
4066
4067 return false;
4068}
4069
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004070/// parseDirectiveError
4071/// ::= .err
4072/// ::= .error [string]
4073bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4074 if (!TheCondStack.empty()) {
4075 if (TheCondStack.back().Ignore) {
4076 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004077 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004078 }
4079 }
4080
4081 if (!WithMessage)
4082 return Error(L, ".err encountered");
4083
4084 StringRef Message = ".error directive invoked in source file";
4085 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4086 if (Lexer.isNot(AsmToken::String)) {
4087 TokError(".error argument must be a string");
4088 eatToEndOfStatement();
4089 return true;
4090 }
4091
4092 Message = getTok().getStringContents();
4093 Lex();
4094 }
4095
4096 Error(L, Message);
4097 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004098}
4099
Nico Weber404012b2014-07-24 16:26:06 +00004100/// parseDirectiveWarning
4101/// ::= .warning [string]
4102bool AsmParser::parseDirectiveWarning(SMLoc L) {
4103 if (!TheCondStack.empty()) {
4104 if (TheCondStack.back().Ignore) {
4105 eatToEndOfStatement();
4106 return false;
4107 }
4108 }
4109
4110 StringRef Message = ".warning directive invoked in source file";
4111 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4112 if (Lexer.isNot(AsmToken::String)) {
4113 TokError(".warning argument must be a string");
4114 eatToEndOfStatement();
4115 return true;
4116 }
4117
4118 Message = getTok().getStringContents();
4119 Lex();
4120 }
4121
4122 Warning(L, Message);
4123 return false;
4124}
4125
Jim Grosbach4b905842013-09-20 23:08:21 +00004126/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004127/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004128bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004129 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004130 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004131
Sean Callanan686ed8d2010-01-19 20:22:31 +00004132 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004133
Jim Grosbach4b905842013-09-20 23:08:21 +00004134 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004135 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4136 ".else");
4137 if (!TheCondStack.empty()) {
4138 TheCondState = TheCondStack.back();
4139 TheCondStack.pop_back();
4140 }
4141
4142 return false;
4143}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004144
Eli Bendersky17233942013-01-15 22:59:42 +00004145void AsmParser::initializeDirectiveKindMap() {
4146 DirectiveKindMap[".set"] = DK_SET;
4147 DirectiveKindMap[".equ"] = DK_EQU;
4148 DirectiveKindMap[".equiv"] = DK_EQUIV;
4149 DirectiveKindMap[".ascii"] = DK_ASCII;
4150 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4151 DirectiveKindMap[".string"] = DK_STRING;
4152 DirectiveKindMap[".byte"] = DK_BYTE;
4153 DirectiveKindMap[".short"] = DK_SHORT;
4154 DirectiveKindMap[".value"] = DK_VALUE;
4155 DirectiveKindMap[".2byte"] = DK_2BYTE;
4156 DirectiveKindMap[".long"] = DK_LONG;
4157 DirectiveKindMap[".int"] = DK_INT;
4158 DirectiveKindMap[".4byte"] = DK_4BYTE;
4159 DirectiveKindMap[".quad"] = DK_QUAD;
4160 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004161 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004162 DirectiveKindMap[".single"] = DK_SINGLE;
4163 DirectiveKindMap[".float"] = DK_FLOAT;
4164 DirectiveKindMap[".double"] = DK_DOUBLE;
4165 DirectiveKindMap[".align"] = DK_ALIGN;
4166 DirectiveKindMap[".align32"] = DK_ALIGN32;
4167 DirectiveKindMap[".balign"] = DK_BALIGN;
4168 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4169 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4170 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4171 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4172 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4173 DirectiveKindMap[".org"] = DK_ORG;
4174 DirectiveKindMap[".fill"] = DK_FILL;
4175 DirectiveKindMap[".zero"] = DK_ZERO;
4176 DirectiveKindMap[".extern"] = DK_EXTERN;
4177 DirectiveKindMap[".globl"] = DK_GLOBL;
4178 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004179 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4180 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4181 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4182 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4183 DirectiveKindMap[".reference"] = DK_REFERENCE;
4184 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4185 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4186 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4187 DirectiveKindMap[".comm"] = DK_COMM;
4188 DirectiveKindMap[".common"] = DK_COMMON;
4189 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4190 DirectiveKindMap[".abort"] = DK_ABORT;
4191 DirectiveKindMap[".include"] = DK_INCLUDE;
4192 DirectiveKindMap[".incbin"] = DK_INCBIN;
4193 DirectiveKindMap[".code16"] = DK_CODE16;
4194 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4195 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004196 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004197 DirectiveKindMap[".irp"] = DK_IRP;
4198 DirectiveKindMap[".irpc"] = DK_IRPC;
4199 DirectiveKindMap[".endr"] = DK_ENDR;
4200 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4201 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4202 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4203 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004204 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4205 DirectiveKindMap[".ifge"] = DK_IFGE;
4206 DirectiveKindMap[".ifgt"] = DK_IFGT;
4207 DirectiveKindMap[".ifle"] = DK_IFLE;
4208 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004209 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004210 DirectiveKindMap[".ifb"] = DK_IFB;
4211 DirectiveKindMap[".ifnb"] = DK_IFNB;
4212 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004213 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004214 DirectiveKindMap[".ifnc"] = DK_IFNC;
4215 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4216 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4217 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4218 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4219 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004220 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004221 DirectiveKindMap[".endif"] = DK_ENDIF;
4222 DirectiveKindMap[".skip"] = DK_SKIP;
4223 DirectiveKindMap[".space"] = DK_SPACE;
4224 DirectiveKindMap[".file"] = DK_FILE;
4225 DirectiveKindMap[".line"] = DK_LINE;
4226 DirectiveKindMap[".loc"] = DK_LOC;
4227 DirectiveKindMap[".stabs"] = DK_STABS;
4228 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4229 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4230 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4231 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4232 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4233 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4234 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4235 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4236 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4237 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4238 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4239 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4240 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4241 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4242 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4243 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4244 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4245 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4246 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4247 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4248 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004249 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004250 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4251 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4252 DirectiveKindMap[".macro"] = DK_MACRO;
Nico Weber155dccd12014-07-24 17:08:39 +00004253 DirectiveKindMap[".exitm"] = DK_EXITM;
Eli Bendersky17233942013-01-15 22:59:42 +00004254 DirectiveKindMap[".endm"] = DK_ENDM;
4255 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4256 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004257 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004258 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00004259 DirectiveKindMap[".warning"] = DK_WARNING;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004260}
4261
Jim Grosbach4b905842013-09-20 23:08:21 +00004262MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004263 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004264
Rafael Espindola34b9c512012-06-03 23:57:14 +00004265 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004266 for (;;) {
4267 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004268 if (getLexer().is(AsmToken::Eof)) {
4269 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004270 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004271 }
4272
Rafael Espindola34b9c512012-06-03 23:57:14 +00004273 if (Lexer.is(AsmToken::Identifier) &&
4274 (getTok().getIdentifier() == ".rept")) {
4275 ++NestLevel;
4276 }
4277
4278 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004279 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004280 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004281 EndToken = getTok();
4282 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004283 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4284 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004285 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004286 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004287 break;
4288 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004289 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004290 }
4291
Rafael Espindola34b9c512012-06-03 23:57:14 +00004292 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004293 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004294 }
4295
4296 const char *BodyStart = StartToken.getLoc().getPointer();
4297 const char *BodyEnd = EndToken.getLoc().getPointer();
4298 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4299
Rafael Espindola34b9c512012-06-03 23:57:14 +00004300 // We Are Anonymous.
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004301 MacroLikeBodies.push_back(MCAsmMacro(StringRef(), Body, None));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004302 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004303}
4304
Jim Grosbach4b905842013-09-20 23:08:21 +00004305void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004306 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004307 OS << ".endr\n";
4308
David Blaikie1961f142014-08-21 20:44:56 +00004309 std::unique_ptr<MemoryBuffer> Instantiation(
4310 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"));
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004311
Rafael Espindola34b9c512012-06-03 23:57:14 +00004312 // Create the macro instantiation object and add to the current macro
4313 // instantiation stack.
Nico Weber155dccd12014-07-24 17:08:39 +00004314 MacroInstantiation *MI =
4315 new MacroInstantiation(DirectiveLoc, CurBuffer, getTok().getLoc(),
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00004316 Instantiation->getBuffer(), TheCondStack.size());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004317 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004318
Rafael Espindola34b9c512012-06-03 23:57:14 +00004319 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00004320 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004321 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004322 Lex();
4323}
4324
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004325/// parseDirectiveRept
4326/// ::= .rep | .rept count
4327bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004328 const MCExpr *CountExpr;
4329 SMLoc CountLoc = getTok().getLoc();
4330 if (parseExpression(CountExpr))
4331 return true;
4332
Rafael Espindola34b9c512012-06-03 23:57:14 +00004333 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004334 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4335 eatToEndOfStatement();
4336 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4337 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004338
4339 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004340 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004341
4342 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004343 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004344
4345 // Eat the end of statement.
4346 Lex();
4347
4348 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004349 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004350 if (!M)
4351 return true;
4352
4353 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4354 // to hold the macro body with substitutions.
4355 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004356 raw_svector_ostream OS(Buf);
4357 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004358 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004359 return true;
4360 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004361 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004362
4363 return false;
4364}
4365
Jim Grosbach4b905842013-09-20 23:08:21 +00004366/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004367/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004368bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004369 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004370
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004371 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004372 return TokError("expected identifier in '.irp' directive");
4373
Rafael Espindola768b41c2012-06-15 14:02:34 +00004374 if (Lexer.isNot(AsmToken::Comma))
4375 return TokError("expected comma in '.irp' directive");
4376
4377 Lex();
4378
Eli Bendersky38274122013-01-14 23:22:36 +00004379 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004380 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004381 return true;
4382
4383 // Eat the end of statement.
4384 Lex();
4385
4386 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004387 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004388 if (!M)
4389 return true;
4390
4391 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4392 // to hold the macro body with substitutions.
4393 SmallString<256> Buf;
4394 raw_svector_ostream OS(Buf);
4395
Eli Bendersky38274122013-01-14 23:22:36 +00004396 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004397 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004398 return true;
4399 }
4400
Jim Grosbach4b905842013-09-20 23:08:21 +00004401 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004402
4403 return false;
4404}
4405
Jim Grosbach4b905842013-09-20 23:08:21 +00004406/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004407/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004408bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004409 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004410
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004411 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004412 return TokError("expected identifier in '.irpc' directive");
4413
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004414 if (Lexer.isNot(AsmToken::Comma))
4415 return TokError("expected comma in '.irpc' directive");
4416
4417 Lex();
4418
Eli Bendersky38274122013-01-14 23:22:36 +00004419 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004420 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004421 return true;
4422
4423 if (A.size() != 1 || A.front().size() != 1)
4424 return TokError("unexpected token in '.irpc' directive");
4425
4426 // Eat the end of statement.
4427 Lex();
4428
4429 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004430 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004431 if (!M)
4432 return true;
4433
4434 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4435 // to hold the macro body with substitutions.
4436 SmallString<256> Buf;
4437 raw_svector_ostream OS(Buf);
4438
4439 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004440 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004441 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004442 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004443
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004444 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004445 return true;
4446 }
4447
Jim Grosbach4b905842013-09-20 23:08:21 +00004448 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004449
4450 return false;
4451}
4452
Jim Grosbach4b905842013-09-20 23:08:21 +00004453bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004454 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004455 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004456
4457 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004458 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004459 assert(getLexer().is(AsmToken::EndOfStatement));
4460
Jim Grosbach4b905842013-09-20 23:08:21 +00004461 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004462 return false;
4463}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004464
Jim Grosbach4b905842013-09-20 23:08:21 +00004465bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004466 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004467 const MCExpr *Value;
4468 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004469 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004470 return true;
4471 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4472 if (!MCE)
4473 return Error(ExprLoc, "unexpected expression in _emit");
4474 uint64_t IntValue = MCE->getValue();
4475 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4476 return Error(ExprLoc, "literal value out of range for directive");
4477
Chad Rosierc7f552c2013-02-12 21:33:51 +00004478 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4479 return false;
4480}
4481
Jim Grosbach4b905842013-09-20 23:08:21 +00004482bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004483 const MCExpr *Value;
4484 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004485 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004486 return true;
4487 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4488 if (!MCE)
4489 return Error(ExprLoc, "unexpected expression in align");
4490 uint64_t IntValue = MCE->getValue();
4491 if (!isPowerOf2_64(IntValue))
4492 return Error(ExprLoc, "literal value not a power of two greater then zero");
4493
Jim Grosbach4b905842013-09-20 23:08:21 +00004494 Info.AsmRewrites->push_back(
4495 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004496 return false;
4497}
4498
Chad Rosierf43fcf52013-02-13 21:27:17 +00004499// We are comparing pointers, but the pointers are relative to a single string.
4500// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004501static int rewritesSort(const AsmRewrite *AsmRewriteA,
4502 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004503 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4504 return -1;
4505 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4506 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004507
Chad Rosierfce4fab2013-04-08 17:43:47 +00004508 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4509 // rewrite to the same location. Make sure the SizeDirective rewrite is
4510 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4511 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004512 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4513 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004514 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004515
Jim Grosbach4b905842013-09-20 23:08:21 +00004516 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4517 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004518 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004519 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004520}
4521
Jim Grosbach4b905842013-09-20 23:08:21 +00004522bool AsmParser::parseMSInlineAsm(
4523 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4524 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4525 SmallVectorImpl<std::string> &Constraints,
4526 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4527 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004528 SmallVector<void *, 4> InputDecls;
4529 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004530 SmallVector<bool, 4> InputDeclsAddressOf;
4531 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004532 SmallVector<std::string, 4> InputConstraints;
4533 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004534 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004535
Benjamin Kramer1a136112013-02-15 20:37:21 +00004536 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004537
4538 // Prime the lexer.
4539 Lex();
4540
4541 // While we have input, parse each statement.
4542 unsigned InputIdx = 0;
4543 unsigned OutputIdx = 0;
4544 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004545 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004546 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004547 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004548
Chad Rosier149e8e02012-12-12 22:45:52 +00004549 if (Info.ParseError)
4550 return true;
4551
Benjamin Kramer1a136112013-02-15 20:37:21 +00004552 if (Info.Opcode == ~0U)
4553 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004554
Benjamin Kramer1a136112013-02-15 20:37:21 +00004555 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004556
Benjamin Kramer1a136112013-02-15 20:37:21 +00004557 // Build the list of clobbers, outputs and inputs.
4558 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00004559 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004560
Benjamin Kramer1a136112013-02-15 20:37:21 +00004561 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00004562 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004563 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004564
Benjamin Kramer1a136112013-02-15 20:37:21 +00004565 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00004566 if (Operand.isReg() && !Operand.needAddressOf() &&
4567 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00004568 unsigned NumDefs = Desc.getNumDefs();
4569 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00004570 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
4571 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004572 continue;
4573 }
4574
4575 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00004576 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00004577 if (SymName.empty())
4578 continue;
4579
David Blaikie960ea3f2014-06-08 16:18:35 +00004580 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004581 if (!OpDecl)
4582 continue;
4583
4584 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004585 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004586 if (isOutput) {
4587 ++InputIdx;
4588 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004589 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
4590 OutputConstraints.push_back('=' + Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004591 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004592 } else {
4593 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004594 InputDeclsAddressOf.push_back(Operand.needAddressOf());
4595 InputConstraints.push_back(Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004596 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004597 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004598 }
Reid Kleckneree088972013-12-10 18:27:32 +00004599
4600 // Consider implicit defs to be clobbers. Think of cpuid and push.
David Majnemer8114c1a2014-06-23 02:17:16 +00004601 ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(),
4602 Desc.getNumImplicitDefs());
4603 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00004604 }
4605
4606 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004607 NumOutputs = OutputDecls.size();
4608 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004609
4610 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004611 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4612 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4613 ClobberRegs.end());
4614 Clobbers.assign(ClobberRegs.size(), std::string());
4615 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4616 raw_string_ostream OS(Clobbers[I]);
4617 IP->printRegName(OS, ClobberRegs[I]);
4618 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004619
4620 // Merge the various outputs and inputs. Output are expected first.
4621 if (NumOutputs || NumInputs) {
4622 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004623 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004624 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004625 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004626 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004627 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004628 }
4629 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004630 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004631 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004632 }
4633 }
4634
4635 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00004636 std::string AsmStringIR;
4637 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00004638 StringRef ASMString =
4639 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
4640 const char *AsmStart = ASMString.begin();
4641 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00004642 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00004643 for (const AsmRewrite &AR : AsmStrRewrites) {
4644 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00004645 if (Kind == AOK_Delete)
4646 continue;
4647
David Majnemer8114c1a2014-06-23 02:17:16 +00004648 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004649 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004650
Chad Rosier120eefd2013-03-19 17:32:17 +00004651 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004652 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00004653 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004654
Chad Rosier37e755c2012-10-23 17:43:43 +00004655 // Skip the original expression.
4656 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00004657 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004658 continue;
4659 }
4660
Chad Rosierff10ed12013-04-12 16:26:42 +00004661 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004662 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004663 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004664 default:
4665 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004666 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00004667 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004668 break;
4669 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004670 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004671 break;
4672 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004673 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004674 break;
4675 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004676 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004677 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004678 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00004679 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004680 default: break;
4681 case 8: OS << "byte ptr "; break;
4682 case 16: OS << "word ptr "; break;
4683 case 32: OS << "dword ptr "; break;
4684 case 64: OS << "qword ptr "; break;
4685 case 80: OS << "xword ptr "; break;
4686 case 128: OS << "xmmword ptr "; break;
4687 case 256: OS << "ymmword ptr "; break;
4688 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004689 break;
4690 case AOK_Emit:
4691 OS << ".byte";
4692 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004693 case AOK_Align: {
David Majnemer8114c1a2014-06-23 02:17:16 +00004694 unsigned Val = AR.Val;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004695 OS << ".align " << Val;
4696
4697 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004698 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004699 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4700 break;
4701 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004702 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004703 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00004704 OS.flush();
4705 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004706 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00004707 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00004708 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004709 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004710
Chad Rosier8bce6642012-10-18 15:49:34 +00004711 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004712 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004713 }
4714
4715 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004716 if (AsmStart != AsmEnd)
4717 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004718
4719 AsmString = OS.str();
4720 return false;
4721}
4722
Daniel Dunbar01e36072010-07-17 02:26:10 +00004723/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004724MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4725 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004726 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004727}