blob: ac6ebbde7b2a5c671d15c3ce1612dcb4750422c3 [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>
Chad Rosier8bce6642012-10-18 15:49:34 +000042#include <set>
43#include <string>
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000044#include <vector>
Chris Lattnerb0133452009-06-21 20:16:42 +000045using namespace llvm;
46
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000047static cl::opt<bool>
48FatalAssemblerWarnings("fatal-assembler-warnings",
49 cl::desc("Consider warnings as error"));
50
Eric Christophera7c32732012-12-18 00:30:54 +000051MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewyckyac612272012-10-19 07:00:09 +000052
Daniel Dunbar86033402010-07-12 17:54:38 +000053namespace {
Eli Benderskya313ae62013-01-16 18:56:50 +000054/// \brief Helper types for tracking macro definitions.
55typedef std::vector<AsmToken> MCAsmMacroArgument;
56typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000057
58struct MCAsmMacroParameter {
59 StringRef Name;
60 MCAsmMacroArgument Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000061 bool Required;
62
63 MCAsmMacroParameter() : Required(false) { }
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000064};
65
Eli Benderskya313ae62013-01-16 18:56:50 +000066typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
67
68struct MCAsmMacro {
69 StringRef Name;
70 StringRef Body;
71 MCAsmMacroParameters Parameters;
72
73public:
Benjamin Kramerd31aaf12014-02-09 17:13:11 +000074 MCAsmMacro(StringRef N, StringRef B, ArrayRef<MCAsmMacroParameter> P) :
Eli Benderskya313ae62013-01-16 18:56:50 +000075 Name(N), Body(B), Parameters(P) {}
Eli Benderskya313ae62013-01-16 18:56:50 +000076};
77
Daniel Dunbar43235712010-07-18 18:54:11 +000078/// \brief Helper class for storing information about an active macro
79/// instantiation.
80struct MacroInstantiation {
81 /// The macro being instantiated.
Eli Bendersky38274122013-01-14 23:22:36 +000082 const MCAsmMacro *TheMacro;
Daniel Dunbar43235712010-07-18 18:54:11 +000083
84 /// The macro instantiation with substitutions.
85 MemoryBuffer *Instantiation;
86
87 /// The location of the instantiation.
88 SMLoc InstantiationLoc;
89
Daniel Dunbar40f1d852012-12-01 01:38:48 +000090 /// The buffer where parsing should resume upon instantiation completion.
91 int ExitBuffer;
92
Daniel Dunbar43235712010-07-18 18:54:11 +000093 /// The location where parsing should resume upon instantiation completion.
94 SMLoc ExitLoc;
95
96public:
Eli Bendersky38274122013-01-14 23:22:36 +000097 MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB, SMLoc EL,
Rafael Espindola1134ab232011-06-05 02:43:45 +000098 MemoryBuffer *I);
Daniel Dunbar43235712010-07-18 18:54:11 +000099};
100
Eli Friedman0f4871d2012-10-22 23:58:19 +0000101struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +0000102 /// \brief The parsed operands from the last parsed statement.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000103 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
104
Jim Grosbach4b905842013-09-20 23:08:21 +0000105 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000106 unsigned Opcode;
107
Jim Grosbach4b905842013-09-20 23:08:21 +0000108 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000109 bool ParseError;
110
Eli Friedman0f4871d2012-10-22 23:58:19 +0000111 SmallVectorImpl<AsmRewrite> *AsmRewrites;
112
Chad Rosier149e8e02012-12-12 22:45:52 +0000113 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(0) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000114 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000115 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000116
117 ~ParseStatementInfo() {
118 // Free any parsed operands.
119 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
120 delete ParsedOperands[i];
121 ParsedOperands.clear();
122 }
123};
124
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000125/// \brief The concrete assembly parser instance.
126class AsmParser : public MCAsmParser {
Craig Topper2e6644c2012-09-15 16:23:52 +0000127 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
128 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000129private:
130 AsmLexer Lexer;
131 MCContext &Ctx;
132 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000133 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000134 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000135 SourceMgr::DiagHandlerTy SavedDiagHandler;
136 void *SavedDiagContext;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000137 MCAsmParserExtension *PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000138
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000139 /// This is the current buffer index we're lexing from as managed by the
140 /// SourceMgr object.
141 int CurBuffer;
142
143 AsmCond TheCondState;
144 std::vector<AsmCond> TheCondStack;
145
Jim Grosbach4b905842013-09-20 23:08:21 +0000146 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000147 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000148 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000149 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000150
Jim Grosbach4b905842013-09-20 23:08:21 +0000151 /// \brief Map of currently defined macros.
Eli Bendersky38274122013-01-14 23:22:36 +0000152 StringMap<MCAsmMacro*> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000153
Jim Grosbach4b905842013-09-20 23:08:21 +0000154 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000155 std::vector<MacroInstantiation*> ActiveMacros;
156
Jim Grosbach4b905842013-09-20 23:08:21 +0000157 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000158 std::deque<MCAsmMacro> MacroLikeBodies;
159
Daniel Dunbar828984f2010-07-18 18:38:02 +0000160 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000161 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000162
Daniel Dunbar43325c42010-09-09 22:42:56 +0000163 /// Flag tracking whether any errors have been encountered.
164 unsigned HadError : 1;
165
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000166 /// The values from the last parsed cpp hash file line comment if any.
167 StringRef CppHashFilename;
168 int64_t CppHashLineNumber;
169 SMLoc CppHashLoc;
Kevin Enderby27121c12012-11-05 21:55:41 +0000170 int CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000171 /// When generating dwarf for assembly source files we need to calculate the
172 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000173 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000174 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
175 SMLoc LastQueryIDLoc;
176 int LastQueryBuffer;
177 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000178
Devang Patela173ee52012-01-31 18:14:05 +0000179 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
180 unsigned AssemblerDialect;
181
Jim Grosbach4b905842013-09-20 23:08:21 +0000182 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000183 bool IsDarwin;
184
Jim Grosbach4b905842013-09-20 23:08:21 +0000185 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000186 bool ParsingInlineAsm;
187
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000188public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000189 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000190 const MCAsmInfo &MAI);
Craig Topper5f96ca52012-08-29 05:48:09 +0000191 virtual ~AsmParser();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000192
Craig Topper59be68f2014-03-08 07:14:16 +0000193 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000194
Craig Topper59be68f2014-03-08 07:14:16 +0000195 void addDirectiveHandler(StringRef Directive,
196 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000197 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000198 }
199
200public:
201 /// @name MCAsmParser Interface
202 /// {
203
Craig Topper59be68f2014-03-08 07:14:16 +0000204 SourceMgr &getSourceManager() override { return SrcMgr; }
205 MCAsmLexer &getLexer() override { return Lexer; }
206 MCContext &getContext() override { return Ctx; }
207 MCStreamer &getStreamer() override { return Out; }
208 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000209 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000210 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000211 else
212 return AssemblerDialect;
213 }
Craig Topper59be68f2014-03-08 07:14:16 +0000214 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000215 AssemblerDialect = i;
216 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000217
Craig Topper59be68f2014-03-08 07:14:16 +0000218 void Note(SMLoc L, const Twine &Msg,
219 ArrayRef<SMRange> Ranges = None) override;
220 bool Warning(SMLoc L, const Twine &Msg,
221 ArrayRef<SMRange> Ranges = None) override;
222 bool Error(SMLoc L, const Twine &Msg,
223 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000224
Craig Topper59be68f2014-03-08 07:14:16 +0000225 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000226
Craig Topper59be68f2014-03-08 07:14:16 +0000227 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
228 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000229
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000230 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000231 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000232 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000233 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000234 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000235 const MCInstrInfo *MII, const MCInstPrinter *IP,
236 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000237
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000238 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000239 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
240 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
241 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
242 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000243
Jim Grosbach4b905842013-09-20 23:08:21 +0000244 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000245 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000246 bool parseIdentifier(StringRef &Res) override;
247 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000248
Craig Topper59be68f2014-03-08 07:14:16 +0000249 void checkForValidSection() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000250 /// }
251
252private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000253
Jim Grosbach4b905842013-09-20 23:08:21 +0000254 bool parseStatement(ParseStatementInfo &Info);
255 void eatToEndOfLine();
256 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000257
Jim Grosbach4b905842013-09-20 23:08:21 +0000258 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000259 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000260 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000261 ArrayRef<MCAsmMacroParameter> Parameters,
262 ArrayRef<MCAsmMacroArgument> A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000263 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000264
Eli Benderskya313ae62013-01-16 18:56:50 +0000265 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000266 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000267
268 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000269 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000270
271 /// \brief Lookup a previously defined macro.
272 /// \param Name Macro name.
273 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000274 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000275
276 /// \brief Define a new macro with the given name and information.
Jim Grosbach4b905842013-09-20 23:08:21 +0000277 void defineMacro(StringRef Name, const MCAsmMacro& Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000278
279 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000280 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000281
282 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000283 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000284
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000285 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000286 ///
287 /// \param M The macro.
288 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000289 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000290
291 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000292 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000293
David Majnemer91fc4c22014-01-29 18:57:46 +0000294 /// \brief Extract AsmTokens for a macro argument.
295 bool parseMacroArgument(MCAsmMacroArgument &MA);
Eli Benderskya313ae62013-01-16 18:56:50 +0000296
297 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000298 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000299
Jim Grosbach4b905842013-09-20 23:08:21 +0000300 void printMacroInstantiations();
301 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000302 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000303 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000304 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000305 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000306
Jim Grosbach4b905842013-09-20 23:08:21 +0000307 /// \brief Enter the specified file. This returns true on failure.
308 bool enterIncludeFile(const std::string &Filename);
309
310 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000311 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000312 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000313
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000314 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000315 /// current token is not set; clients should ensure Lex() is called
316 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000317 ///
318 /// \param InBuffer If not -1, should be the known buffer id that contains the
319 /// location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000320 void jumpToLoc(SMLoc Loc, int InBuffer=-1);
Daniel Dunbar43235712010-07-18 18:54:11 +0000321
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000322 /// \brief Parse up to the end of statement and a return the contents from the
323 /// current token until the end of the statement; the current token on exit
324 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000325 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000326
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000327 /// \brief Parse until the end of a statement or a comma is encountered,
328 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000329 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000330
Jim Grosbach4b905842013-09-20 23:08:21 +0000331 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000332 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000333
Jim Grosbach4b905842013-09-20 23:08:21 +0000334 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
335 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
336 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000337
Jim Grosbach4b905842013-09-20 23:08:21 +0000338 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000339
Eli Bendersky17233942013-01-15 22:59:42 +0000340 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000341 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000342 DK_NO_DIRECTIVE, // Placeholder
343 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
David Woodhoused6de0d92014-02-01 16:20:59 +0000344 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
345 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000346 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000347 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000348 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000349 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
350 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
351 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
352 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +0000353 DK_IF, DK_IFNE, DK_IFB, DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFDEF,
354 DK_IFNDEF, DK_IFNOTDEF, DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000355 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
356 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
357 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
358 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
359 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
360 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000361 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Eli Bendersky17233942013-01-15 22:59:42 +0000362 DK_MACROS_ON, DK_MACROS_OFF, DK_MACRO, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000363 DK_SLEB128, DK_ULEB128,
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000364 DK_ERR, DK_ERROR,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000365 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000366 };
367
Jim Grosbach4b905842013-09-20 23:08:21 +0000368 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000369 /// directives parsed by this class.
370 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000371
372 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000373 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
374 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000375 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000376 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
377 bool parseDirectiveFill(); // ".fill"
378 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000379 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000380 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
381 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000382 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000383 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000384
Eli Bendersky17233942013-01-15 22:59:42 +0000385 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000386 bool parseDirectiveFile(SMLoc DirectiveLoc);
387 bool parseDirectiveLine();
388 bool parseDirectiveLoc();
389 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000390
391 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000392 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000393 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000394 bool parseDirectiveCFISections();
395 bool parseDirectiveCFIStartProc();
396 bool parseDirectiveCFIEndProc();
397 bool parseDirectiveCFIDefCfaOffset();
398 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
399 bool parseDirectiveCFIAdjustCfaOffset();
400 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
401 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
402 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
403 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
404 bool parseDirectiveCFIRememberState();
405 bool parseDirectiveCFIRestoreState();
406 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
407 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
408 bool parseDirectiveCFIEscape();
409 bool parseDirectiveCFISignalFrame();
410 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000411
412 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000413 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
414 bool parseDirectiveEndMacro(StringRef Directive);
415 bool parseDirectiveMacro(SMLoc DirectiveLoc);
416 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000417
Eli Benderskyf483ff92012-12-20 19:05:53 +0000418 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000419 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000420 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000421 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000422 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000423 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000424
Eli Bendersky17233942013-01-15 22:59:42 +0000425 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000426 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000427
428 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000429 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000430
Jim Grosbach4b905842013-09-20 23:08:21 +0000431 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000432 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000433 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000434
Jim Grosbach4b905842013-09-20 23:08:21 +0000435 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000436
Jim Grosbach4b905842013-09-20 23:08:21 +0000437 bool parseDirectiveAbort(); // ".abort"
438 bool parseDirectiveInclude(); // ".include"
439 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000440
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +0000441 // ".if" or ".ifne"
442 bool parseDirectiveIf(SMLoc DirectiveLoc);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000443 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000444 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000445 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000446 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +0000447 // ".ifeqs"
448 bool parseDirectiveIfeqs(SMLoc DirectiveLoc);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000449 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000450 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
451 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
452 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
453 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000454 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000455
Jim Grosbach4b905842013-09-20 23:08:21 +0000456 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000457 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000458
Rafael Espindola34b9c512012-06-03 23:57:14 +0000459 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000460 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
461 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000462 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000463 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000464 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
465 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
466 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000467
Chad Rosierc7f552c2013-02-12 21:33:51 +0000468 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000469 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000470 size_t Len);
471
472 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000473 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000474
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000475 // "end"
476 bool parseDirectiveEnd(SMLoc DirectiveLoc);
477
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000478 // ".err" or ".error"
479 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000480
Eli Bendersky17233942013-01-15 22:59:42 +0000481 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000482};
Daniel Dunbar86033402010-07-12 17:54:38 +0000483}
484
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000485namespace llvm {
486
487extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000488extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000489extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000490
491}
492
Chris Lattnerc35681b2010-01-19 19:46:13 +0000493enum { DEFAULT_ADDRSPACE = 0 };
494
Jim Grosbach4b905842013-09-20 23:08:21 +0000495AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
496 const MCAsmInfo &_MAI)
497 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
498 PlatformParser(0), CurBuffer(0), MacrosEnabledFlag(true),
499 CppHashLineNumber(0), AssemblerDialect(~0U), IsDarwin(false),
500 ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000501 // Save the old handler.
502 SavedDiagHandler = SrcMgr.getDiagHandler();
503 SavedDiagContext = SrcMgr.getDiagContext();
504 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000505 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000506 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar86033402010-07-12 17:54:38 +0000507
Daniel Dunbarc5011082010-07-12 18:12:02 +0000508 // Initialize the platform / file format parser.
Rafael Espindolae28610d2013-12-09 20:26:40 +0000509 switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
510 case MCObjectFileInfo::IsCOFF:
511 PlatformParser = createCOFFAsmParser();
512 PlatformParser->Initialize(*this);
513 break;
514 case MCObjectFileInfo::IsMachO:
515 PlatformParser = createDarwinAsmParser();
516 PlatformParser->Initialize(*this);
517 IsDarwin = true;
518 break;
519 case MCObjectFileInfo::IsELF:
520 PlatformParser = createELFAsmParser();
521 PlatformParser->Initialize(*this);
522 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000523 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000524
Eli Bendersky17233942013-01-15 22:59:42 +0000525 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000526}
527
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000528AsmParser::~AsmParser() {
Daniel Dunbarb759a132010-07-29 01:51:55 +0000529 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
530
531 // Destroy any macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000532 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
533 ie = MacroMap.end();
534 it != ie; ++it)
Daniel Dunbarb759a132010-07-29 01:51:55 +0000535 delete it->getValue();
536
Daniel Dunbarc5011082010-07-12 18:12:02 +0000537 delete PlatformParser;
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000538}
539
Jim Grosbach4b905842013-09-20 23:08:21 +0000540void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000541 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000542 for (std::vector<MacroInstantiation *>::const_reverse_iterator
543 it = ActiveMacros.rbegin(),
544 ie = ActiveMacros.rend();
545 it != ie; ++it)
546 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000547 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000548}
549
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000550void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
551 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
552 printMacroInstantiations();
553}
554
Chris Lattnera3a06812011-10-16 04:47:35 +0000555bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000556 if (FatalAssemblerWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000557 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000558 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
559 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000560 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000561}
562
Chris Lattnera3a06812011-10-16 04:47:35 +0000563bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000564 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000565 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
566 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000567 return true;
568}
569
Jim Grosbach4b905842013-09-20 23:08:21 +0000570bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000571 std::string IncludedFile;
572 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000573 if (NewBuf == -1)
574 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000575
Sean Callanan7a77eae2010-01-21 00:19:58 +0000576 CurBuffer = NewBuf;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000577
Sean Callanan7a77eae2010-01-21 00:19:58 +0000578 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencer530ce852010-10-09 11:00:50 +0000579
Sean Callanan7a77eae2010-01-21 00:19:58 +0000580 return false;
581}
Daniel Dunbar43235712010-07-18 18:54:11 +0000582
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000583/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000584/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000585/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000586bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000587 std::string IncludedFile;
588 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
589 if (NewBuf == -1)
590 return true;
591
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000592 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000593 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000594 return false;
595}
596
Jim Grosbach4b905842013-09-20 23:08:21 +0000597void AsmParser::jumpToLoc(SMLoc Loc, int InBuffer) {
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000598 if (InBuffer != -1) {
599 CurBuffer = InBuffer;
600 } else {
601 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
602 }
Daniel Dunbar43235712010-07-18 18:54:11 +0000603 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
604}
605
Sean Callanan7a77eae2010-01-21 00:19:58 +0000606const AsmToken &AsmParser::Lex() {
607 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000608
Sean Callanan7a77eae2010-01-21 00:19:58 +0000609 if (tok->is(AsmToken::Eof)) {
610 // If this is the end of an included file, pop the parent file off the
611 // include stack.
612 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
613 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000614 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000615 tok = &Lexer.Lex();
616 }
617 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000618
Sean Callanan7a77eae2010-01-21 00:19:58 +0000619 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000620 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000621
Sean Callanan7a77eae2010-01-21 00:19:58 +0000622 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000623}
624
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000625bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000626 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000627 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000628 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000629
Chris Lattner36e02122009-06-21 20:54:55 +0000630 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000631 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000632
633 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000634 AsmCond StartingCondState = TheCondState;
635
Kevin Enderby6469fc22011-11-01 22:27:22 +0000636 // If we are generating dwarf for assembly source files save the initial text
637 // section and generate a .file directive.
638 if (getContext().getGenDwarfForAssembly()) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000639 getContext().setGenDwarfSection(getStreamer().getCurrentSection().first);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000640 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
641 getStreamer().EmitLabel(SectionStartSym);
642 getContext().setGenDwarfSectionStartSym(SectionStartSym);
David Blaikiec714ef42014-03-17 01:52:11 +0000643 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
644 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000645 }
646
Chris Lattner73f36112009-07-02 21:53:43 +0000647 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000648 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000649 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000650 if (!parseStatement(Info))
651 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000652
Daniel Dunbar43325c42010-09-09 22:42:56 +0000653 // We had an error, validate that one was emitted and recover by skipping to
654 // the next line.
655 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000656 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000657 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000658
659 if (TheCondState.TheCond != StartingCondState.TheCond ||
660 TheCondState.Ignore != StartingCondState.Ignore)
661 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000662
663 // Check to see there are no empty DwarfFile slots.
David Blaikiea55ddad2014-03-13 18:55:04 +0000664 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +0000665 getContext().getMCDwarfFiles();
Kevin Enderbye5930f12010-07-28 20:55:35 +0000666 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
David Blaikiea55ddad2014-03-13 18:55:04 +0000667 if (MCDwarfFiles[i].Name.empty())
Kevin Enderbye5930f12010-07-28 20:55:35 +0000668 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000669 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000670
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000671 // Check to see that all assembler local symbols were actually defined.
672 // Targets that don't do subsections via symbols may not want this, though,
673 // so conservatively exclude them. Only do this if we're finalizing, though,
674 // as otherwise we won't necessarilly have seen everything yet.
675 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
676 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
677 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000678 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000679 i != e; ++i) {
680 MCSymbol *Sym = i->getValue();
681 // Variable symbols may not be marked as defined, so check those
682 // explicitly. If we know it's a variable, we have a definition for
683 // the purposes of this check.
684 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
685 // FIXME: We would really like to refer back to where the symbol was
686 // first referenced for a source location. We need to add something
687 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000688 printMessage(
689 getLexer().getLoc(), SourceMgr::DK_Error,
690 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000691 }
692 }
693
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000694 // Finalize the output stream if there are no errors and if the client wants
695 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000696 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000697 Out.Finish();
698
Chris Lattner73f36112009-07-02 21:53:43 +0000699 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000700}
701
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000702void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000703 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000704 TokError("expected section directive before assembly directive");
Rafael Espindolaf1440342014-01-23 23:14:14 +0000705 Out.InitSections();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000706 }
707}
708
Jim Grosbach4b905842013-09-20 23:08:21 +0000709/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000710void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000711 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000712 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000713
Chris Lattnere5074c42009-06-22 01:29:09 +0000714 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000715 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000716 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000717}
718
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000719StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000720 const char *Start = getTok().getLoc().getPointer();
721
Jim Grosbach4b905842013-09-20 23:08:21 +0000722 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000723 Lex();
724
725 const char *End = getTok().getLoc().getPointer();
726 return StringRef(Start, End - Start);
727}
Chris Lattner78db3622009-06-22 05:51:26 +0000728
Jim Grosbach4b905842013-09-20 23:08:21 +0000729StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000730 const char *Start = getTok().getLoc().getPointer();
731
732 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000733 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000734 Lex();
735
736 const char *End = getTok().getLoc().getPointer();
737 return StringRef(Start, End - Start);
738}
739
Jim Grosbach4b905842013-09-20 23:08:21 +0000740/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000741/// NOTE: This assumes the leading '(' has already been consumed.
742///
743/// parenexpr ::= expr)
744///
Jim Grosbach4b905842013-09-20 23:08:21 +0000745bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
746 if (parseExpression(Res))
747 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000748 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000749 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000750 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000751 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000752 return false;
753}
Chris Lattner78db3622009-06-22 05:51:26 +0000754
Jim Grosbach4b905842013-09-20 23:08:21 +0000755/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000756/// NOTE: This assumes the leading '[' has already been consumed.
757///
758/// bracketexpr ::= expr]
759///
Jim Grosbach4b905842013-09-20 23:08:21 +0000760bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
761 if (parseExpression(Res))
762 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000763 if (Lexer.isNot(AsmToken::RBrac))
764 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000765 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000766 Lex();
767 return false;
768}
769
Jim Grosbach4b905842013-09-20 23:08:21 +0000770/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000771/// primaryexpr ::= (parenexpr
772/// primaryexpr ::= symbol
773/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000774/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000775/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000776bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000777 SMLoc FirstTokenLoc = getLexer().getLoc();
778 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
779 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000780 default:
781 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000782 // If we have an error assume that we've already handled it.
783 case AsmToken::Error:
784 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000785 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000786 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000787 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000788 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000789 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000790 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000791 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000792 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000793 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000794 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000795 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000796 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000797 if (FirstTokenKind == AsmToken::Dollar) {
798 if (Lexer.getMAI().getDollarIsPC()) {
799 // This is a '$' reference, which references the current PC. Emit a
800 // temporary label to the streamer and refer to it.
801 MCSymbol *Sym = Ctx.CreateTempSymbol();
802 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000803 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
804 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000805 EndLoc = FirstTokenLoc;
806 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000807 }
808 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000809 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000810 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000811 // Parse symbol variant
812 std::pair<StringRef, StringRef> Split;
813 if (!MAI.useParensForSymbolVariant()) {
814 Split = Identifier.split('@');
815 } else if (Lexer.is(AsmToken::LParen)) {
816 Lexer.Lex(); // eat (
817 StringRef VName;
818 parseIdentifier(VName);
819 if (Lexer.isNot(AsmToken::RParen)) {
820 return Error(Lexer.getTok().getLoc(),
821 "unexpected token in variant, expected ')'");
822 }
823 Lexer.Lex(); // eat )
824 Split = std::make_pair(Identifier, VName);
825 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000826
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000827 EndLoc = SMLoc::getFromPointer(Identifier.end());
828
Daniel Dunbard20cda02009-10-16 01:34:54 +0000829 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000830 StringRef SymbolName = Identifier;
831 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000832
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000833 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000834 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000835 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000836 if (Variant != MCSymbolRefExpr::VK_Invalid) {
837 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000838 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000839 Variant = MCSymbolRefExpr::VK_None;
840 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000841 return Error(SMLoc::getFromPointer(Split.second.begin()),
842 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000843 }
844 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000845
Hans Wennborgce69d772013-10-18 20:46:28 +0000846 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
847
Daniel Dunbard20cda02009-10-16 01:34:54 +0000848 // If this is an absolute variable reference, substitute it now to preserve
849 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000850 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000851 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000852 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000853
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000854 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000855 return false;
856 }
857
858 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000859 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000860 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000861 }
David Woodhousef42a6662014-02-01 16:20:54 +0000862 case AsmToken::BigNum:
863 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000864 case AsmToken::Integer: {
865 SMLoc Loc = getTok().getLoc();
866 int64_t IntVal = getTok().getIntVal();
867 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000868 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000869 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000870 // Look for 'b' or 'f' following an Integer as a directional label
871 if (Lexer.getKind() == AsmToken::Identifier) {
872 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000873 // Lookup the symbol variant if used.
874 std::pair<StringRef, StringRef> Split = IDVal.split('@');
875 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
876 if (Split.first.size() != IDVal.size()) {
877 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000878 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000879 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000880 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000881 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000882 if (IDVal == "f" || IDVal == "b") {
883 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000884 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Ulrich Weigandd4120982013-06-20 16:24:17 +0000885 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000886 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000887 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000888 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000889 Lex(); // Eat identifier.
890 }
891 }
Chris Lattner78db3622009-06-22 05:51:26 +0000892 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000893 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000894 case AsmToken::Real: {
895 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000896 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000897 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000898 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000899 Lex(); // Eat token.
900 return false;
901 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000902 case AsmToken::Dot: {
903 // This is a '.' reference, which references the current PC. Emit a
904 // temporary label to the streamer and refer to it.
905 MCSymbol *Sym = Ctx.CreateTempSymbol();
906 Out.EmitLabel(Sym);
907 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000908 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000909 Lex(); // Eat identifier.
910 return false;
911 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000912 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000913 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000914 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000915 case AsmToken::LBrac:
916 if (!PlatformParser->HasBracketExpressions())
917 return TokError("brackets expression not supported on this target");
918 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000919 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000920 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000921 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000922 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000923 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000924 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000925 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000926 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000927 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000928 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000929 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000930 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000931 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000932 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000933 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000934 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000935 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000936 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000937 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000938 }
939}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000940
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000941bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000942 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000943 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000944}
945
Daniel Dunbar55f16672010-09-17 02:47:07 +0000946const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000947AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000948 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000949 // Ask the target implementation about this expression first.
950 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
951 if (NewE)
952 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000953 // Recurse over the given expression, rebuilding it to apply the given variant
954 // if there is exactly one symbol.
955 switch (E->getKind()) {
956 case MCExpr::Target:
957 case MCExpr::Constant:
958 return 0;
959
960 case MCExpr::SymbolRef: {
961 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
962
963 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000964 TokError("invalid variant on expression '" + getTok().getIdentifier() +
965 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000966 return E;
967 }
968
969 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
970 }
971
972 case MCExpr::Unary: {
973 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000974 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000975 if (!Sub)
976 return 0;
977 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
978 }
979
980 case MCExpr::Binary: {
981 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000982 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
983 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000984
985 if (!LHS && !RHS)
986 return 0;
987
Jim Grosbach4b905842013-09-20 23:08:21 +0000988 if (!LHS)
989 LHS = BE->getLHS();
990 if (!RHS)
991 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +0000992
993 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
994 }
995 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +0000996
Craig Toppera2886c22012-02-07 05:05:23 +0000997 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000998}
999
Jim Grosbach4b905842013-09-20 23:08:21 +00001000/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001001///
Jim Grosbachbd164242011-08-20 16:24:13 +00001002/// expr ::= expr &&,|| expr -> lowest.
1003/// expr ::= expr |,^,&,! expr
1004/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1005/// expr ::= expr <<,>> expr
1006/// expr ::= expr +,- expr
1007/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001008/// expr ::= primaryexpr
1009///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001010bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001011 // Parse the expression.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001012 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001013 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001014 return true;
1015
Daniel Dunbar55f16672010-09-17 02:47:07 +00001016 // As a special case, we support 'a op b @ modifier' by rewriting the
1017 // expression to include the modifier. This is inefficient, but in general we
1018 // expect users to use 'a@modifier op b'.
1019 if (Lexer.getKind() == AsmToken::At) {
1020 Lex();
1021
1022 if (Lexer.isNot(AsmToken::Identifier))
1023 return TokError("unexpected symbol modifier following '@'");
1024
1025 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001026 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001027 if (Variant == MCSymbolRefExpr::VK_Invalid)
1028 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1029
Jim Grosbach4b905842013-09-20 23:08:21 +00001030 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001031 if (!ModifiedRes) {
1032 return TokError("invalid modifier '" + getTok().getIdentifier() +
1033 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001034 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001035
Daniel Dunbar55f16672010-09-17 02:47:07 +00001036 Res = ModifiedRes;
1037 Lex();
1038 }
1039
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001040 // Try to constant fold it up front, if possible.
1041 int64_t Value;
1042 if (Res->EvaluateAsAbsolute(Value))
1043 Res = MCConstantExpr::Create(Value, getContext());
1044
1045 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001046}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001047
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001048bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner807a3bc2010-01-24 01:07:33 +00001049 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001050 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001051}
1052
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001053bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001054 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001055
Daniel Dunbar75630b32009-06-30 02:10:03 +00001056 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001057 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001058 return true;
1059
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001060 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001061 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001062
1063 return false;
1064}
1065
Michael J. Spencer530ce852010-10-09 11:00:50 +00001066static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001067 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001068 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001069 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001070 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001071
Jim Grosbach4b905842013-09-20 23:08:21 +00001072 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001073 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001074 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001075 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001076 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001077 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001078 return 1;
1079
Jim Grosbach4b905842013-09-20 23:08:21 +00001080 // Low Precedence: |, &, ^
1081 //
1082 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001083 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001084 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001085 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001086 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001087 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001088 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001089 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001090 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001091 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001092
Jim Grosbach4b905842013-09-20 23:08:21 +00001093 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001094 case AsmToken::EqualEqual:
1095 Kind = MCBinaryExpr::EQ;
1096 return 3;
1097 case AsmToken::ExclaimEqual:
1098 case AsmToken::LessGreater:
1099 Kind = MCBinaryExpr::NE;
1100 return 3;
1101 case AsmToken::Less:
1102 Kind = MCBinaryExpr::LT;
1103 return 3;
1104 case AsmToken::LessEqual:
1105 Kind = MCBinaryExpr::LTE;
1106 return 3;
1107 case AsmToken::Greater:
1108 Kind = MCBinaryExpr::GT;
1109 return 3;
1110 case AsmToken::GreaterEqual:
1111 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001112 return 3;
1113
Jim Grosbach4b905842013-09-20 23:08:21 +00001114 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001115 case AsmToken::LessLess:
1116 Kind = MCBinaryExpr::Shl;
1117 return 4;
1118 case AsmToken::GreaterGreater:
1119 Kind = MCBinaryExpr::Shr;
1120 return 4;
1121
Jim Grosbach4b905842013-09-20 23:08:21 +00001122 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001123 case AsmToken::Plus:
1124 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001125 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001126 case AsmToken::Minus:
1127 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001128 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001129
Jim Grosbach4b905842013-09-20 23:08:21 +00001130 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001131 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001132 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001133 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001134 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001135 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001136 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001137 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001138 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001139 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001140 }
1141}
1142
Jim Grosbach4b905842013-09-20 23:08:21 +00001143/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001144/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001145bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001146 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001147 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001148 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001149 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001150
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001151 // If the next token is lower precedence than we are allowed to eat, return
1152 // successfully with what we ate already.
1153 if (TokPrec < Precedence)
1154 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001155
Sean Callanan686ed8d2010-01-19 20:22:31 +00001156 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001157
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001158 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001159 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001160 if (parsePrimaryExpr(RHS, EndLoc))
1161 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001162
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001163 // If BinOp binds less tightly with RHS than the operator after RHS, let
1164 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001165 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001166 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001167 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1168 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001169
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001170 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001171 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001172 }
1173}
1174
Chris Lattner36e02122009-06-21 20:54:55 +00001175/// ParseStatement:
1176/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001177/// ::= Label* Directive ...Operands... EndOfStatement
1178/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001179bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001180 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001181 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001182 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001183 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001184 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001185
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001186 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001187 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001188 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001189 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001190 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001191 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001192 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001193 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001194
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001195 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001196 if (Lexer.is(AsmToken::Integer)) {
1197 LocalLabelVal = getTok().getIntVal();
1198 if (LocalLabelVal < 0) {
1199 if (!TheCondState.Ignore)
1200 return TokError("unexpected token at start of statement");
1201 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001202 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001203 IDVal = getTok().getString();
1204 Lex(); // Consume the integer token to be used as an identifier token.
1205 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001206 if (!TheCondState.Ignore)
1207 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001208 }
1209 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001210 } else if (Lexer.is(AsmToken::Dot)) {
1211 // Treat '.' as a valid identifier in this context.
1212 Lex();
1213 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001214 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001215 if (!TheCondState.Ignore)
1216 return TokError("unexpected token at start of statement");
1217 IDVal = "";
1218 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001219
Chris Lattner926885c2010-04-17 18:14:27 +00001220 // Handle conditional assembly here before checking for skipping. We
1221 // have to do this so that .endif isn't skipped in a ".if 0" block for
1222 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001223 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001224 DirectiveKindMap.find(IDVal);
1225 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1226 ? DK_NO_DIRECTIVE
1227 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001228 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001229 default:
1230 break;
1231 case DK_IF:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001232 case DK_IFNE:
Jim Grosbach4b905842013-09-20 23:08:21 +00001233 return parseDirectiveIf(IDLoc);
1234 case DK_IFB:
1235 return parseDirectiveIfb(IDLoc, true);
1236 case DK_IFNB:
1237 return parseDirectiveIfb(IDLoc, false);
1238 case DK_IFC:
1239 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001240 case DK_IFEQS:
1241 return parseDirectiveIfeqs(IDLoc);
Jim Grosbach4b905842013-09-20 23:08:21 +00001242 case DK_IFNC:
1243 return parseDirectiveIfc(IDLoc, false);
1244 case DK_IFDEF:
1245 return parseDirectiveIfdef(IDLoc, true);
1246 case DK_IFNDEF:
1247 case DK_IFNOTDEF:
1248 return parseDirectiveIfdef(IDLoc, false);
1249 case DK_ELSEIF:
1250 return parseDirectiveElseIf(IDLoc);
1251 case DK_ELSE:
1252 return parseDirectiveElse(IDLoc);
1253 case DK_ENDIF:
1254 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001255 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001256
Eli Bendersky88024712013-01-16 19:32:36 +00001257 // Ignore the statement if in the middle of inactive conditional
1258 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001259 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001260 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001261 return false;
1262 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001263
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001264 // FIXME: Recurse on local labels?
1265
1266 // See what kind of statement we have.
1267 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001268 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001269 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001270
Chris Lattner36e02122009-06-21 20:54:55 +00001271 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001272 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001273
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001274 // Diagnose attempt to use '.' as a label.
1275 if (IDVal == ".")
1276 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1277
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001278 // Diagnose attempt to use a variable as a label.
1279 //
1280 // FIXME: Diagnostics. Note the location of the definition as a label.
1281 // FIXME: This doesn't diagnose assignment to a symbol which has been
1282 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001283 MCSymbol *Sym;
1284 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001285 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001286 else
1287 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001288 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001289 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001290
Daniel Dunbare73b2672009-08-26 22:13:22 +00001291 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001292 if (!ParsingInlineAsm)
1293 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001294
Kevin Enderbye7739d42011-12-09 18:09:40 +00001295 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001296 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001297 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001298 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1299 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001300
Tim Northover1744d0a2013-10-25 12:49:50 +00001301 getTargetParser().onLabelParsed(Sym);
1302
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001303 // Consume any end of statement token, if present, to avoid spurious
1304 // AddBlankLine calls().
1305 if (Lexer.is(AsmToken::EndOfStatement)) {
1306 Lex();
1307 if (Lexer.is(AsmToken::Eof))
1308 return false;
1309 }
1310
Eli Friedman0f4871d2012-10-22 23:58:19 +00001311 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001312 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001313
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001314 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001315 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001316 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001317
Jim Grosbach4b905842013-09-20 23:08:21 +00001318 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001319
1320 default: // Normal instruction or directive.
1321 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001322 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001323
1324 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001325 if (areMacrosEnabled())
1326 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1327 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001328 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001329
Michael J. Spencer530ce852010-10-09 11:00:50 +00001330 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001331
Eli Bendersky17233942013-01-15 22:59:42 +00001332 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001333 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001334 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001335 //
Eli Bendersky17233942013-01-15 22:59:42 +00001336 // 1. The target-specific assembly parser. Some directives are target
1337 // specific or may potentially behave differently on certain targets.
1338 // 2. Asm parser extensions. For example, platform-specific parsers
1339 // (like the ELF parser) register themselves as extensions.
1340 // 3. The generic directive parser implemented by this class. These are
1341 // all the directives that behave in a target and platform independent
1342 // manner, or at least have a default behavior that's shared between
1343 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001344
Eli Bendersky17233942013-01-15 22:59:42 +00001345 // First query the target-specific parser. It will return 'true' if it
1346 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001347 if (!getTargetParser().ParseDirective(ID))
1348 return false;
1349
Alp Tokercb402912014-01-24 17:20:08 +00001350 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001351 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001352 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1353 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001354 if (Handler.first)
1355 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1356
1357 // Finally, if no one else is interested in this directive, it must be
1358 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001359 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001360 default:
1361 break;
1362 case DK_SET:
1363 case DK_EQU:
1364 return parseDirectiveSet(IDVal, true);
1365 case DK_EQUIV:
1366 return parseDirectiveSet(IDVal, false);
1367 case DK_ASCII:
1368 return parseDirectiveAscii(IDVal, false);
1369 case DK_ASCIZ:
1370 case DK_STRING:
1371 return parseDirectiveAscii(IDVal, true);
1372 case DK_BYTE:
1373 return parseDirectiveValue(1);
1374 case DK_SHORT:
1375 case DK_VALUE:
1376 case DK_2BYTE:
1377 return parseDirectiveValue(2);
1378 case DK_LONG:
1379 case DK_INT:
1380 case DK_4BYTE:
1381 return parseDirectiveValue(4);
1382 case DK_QUAD:
1383 case DK_8BYTE:
1384 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001385 case DK_OCTA:
1386 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001387 case DK_SINGLE:
1388 case DK_FLOAT:
1389 return parseDirectiveRealValue(APFloat::IEEEsingle);
1390 case DK_DOUBLE:
1391 return parseDirectiveRealValue(APFloat::IEEEdouble);
1392 case DK_ALIGN: {
1393 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1394 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1395 }
1396 case DK_ALIGN32: {
1397 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1398 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1399 }
1400 case DK_BALIGN:
1401 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1402 case DK_BALIGNW:
1403 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1404 case DK_BALIGNL:
1405 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1406 case DK_P2ALIGN:
1407 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1408 case DK_P2ALIGNW:
1409 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1410 case DK_P2ALIGNL:
1411 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1412 case DK_ORG:
1413 return parseDirectiveOrg();
1414 case DK_FILL:
1415 return parseDirectiveFill();
1416 case DK_ZERO:
1417 return parseDirectiveZero();
1418 case DK_EXTERN:
1419 eatToEndOfStatement(); // .extern is the default, ignore it.
1420 return false;
1421 case DK_GLOBL:
1422 case DK_GLOBAL:
1423 return parseDirectiveSymbolAttribute(MCSA_Global);
1424 case DK_LAZY_REFERENCE:
1425 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1426 case DK_NO_DEAD_STRIP:
1427 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1428 case DK_SYMBOL_RESOLVER:
1429 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1430 case DK_PRIVATE_EXTERN:
1431 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1432 case DK_REFERENCE:
1433 return parseDirectiveSymbolAttribute(MCSA_Reference);
1434 case DK_WEAK_DEFINITION:
1435 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1436 case DK_WEAK_REFERENCE:
1437 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1438 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1439 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1440 case DK_COMM:
1441 case DK_COMMON:
1442 return parseDirectiveComm(/*IsLocal=*/false);
1443 case DK_LCOMM:
1444 return parseDirectiveComm(/*IsLocal=*/true);
1445 case DK_ABORT:
1446 return parseDirectiveAbort();
1447 case DK_INCLUDE:
1448 return parseDirectiveInclude();
1449 case DK_INCBIN:
1450 return parseDirectiveIncbin();
1451 case DK_CODE16:
1452 case DK_CODE16GCC:
1453 return TokError(Twine(IDVal) + " not supported yet");
1454 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001455 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001456 case DK_IRP:
1457 return parseDirectiveIrp(IDLoc);
1458 case DK_IRPC:
1459 return parseDirectiveIrpc(IDLoc);
1460 case DK_ENDR:
1461 return parseDirectiveEndr(IDLoc);
1462 case DK_BUNDLE_ALIGN_MODE:
1463 return parseDirectiveBundleAlignMode();
1464 case DK_BUNDLE_LOCK:
1465 return parseDirectiveBundleLock();
1466 case DK_BUNDLE_UNLOCK:
1467 return parseDirectiveBundleUnlock();
1468 case DK_SLEB128:
1469 return parseDirectiveLEB128(true);
1470 case DK_ULEB128:
1471 return parseDirectiveLEB128(false);
1472 case DK_SPACE:
1473 case DK_SKIP:
1474 return parseDirectiveSpace(IDVal);
1475 case DK_FILE:
1476 return parseDirectiveFile(IDLoc);
1477 case DK_LINE:
1478 return parseDirectiveLine();
1479 case DK_LOC:
1480 return parseDirectiveLoc();
1481 case DK_STABS:
1482 return parseDirectiveStabs();
1483 case DK_CFI_SECTIONS:
1484 return parseDirectiveCFISections();
1485 case DK_CFI_STARTPROC:
1486 return parseDirectiveCFIStartProc();
1487 case DK_CFI_ENDPROC:
1488 return parseDirectiveCFIEndProc();
1489 case DK_CFI_DEF_CFA:
1490 return parseDirectiveCFIDefCfa(IDLoc);
1491 case DK_CFI_DEF_CFA_OFFSET:
1492 return parseDirectiveCFIDefCfaOffset();
1493 case DK_CFI_ADJUST_CFA_OFFSET:
1494 return parseDirectiveCFIAdjustCfaOffset();
1495 case DK_CFI_DEF_CFA_REGISTER:
1496 return parseDirectiveCFIDefCfaRegister(IDLoc);
1497 case DK_CFI_OFFSET:
1498 return parseDirectiveCFIOffset(IDLoc);
1499 case DK_CFI_REL_OFFSET:
1500 return parseDirectiveCFIRelOffset(IDLoc);
1501 case DK_CFI_PERSONALITY:
1502 return parseDirectiveCFIPersonalityOrLsda(true);
1503 case DK_CFI_LSDA:
1504 return parseDirectiveCFIPersonalityOrLsda(false);
1505 case DK_CFI_REMEMBER_STATE:
1506 return parseDirectiveCFIRememberState();
1507 case DK_CFI_RESTORE_STATE:
1508 return parseDirectiveCFIRestoreState();
1509 case DK_CFI_SAME_VALUE:
1510 return parseDirectiveCFISameValue(IDLoc);
1511 case DK_CFI_RESTORE:
1512 return parseDirectiveCFIRestore(IDLoc);
1513 case DK_CFI_ESCAPE:
1514 return parseDirectiveCFIEscape();
1515 case DK_CFI_SIGNAL_FRAME:
1516 return parseDirectiveCFISignalFrame();
1517 case DK_CFI_UNDEFINED:
1518 return parseDirectiveCFIUndefined(IDLoc);
1519 case DK_CFI_REGISTER:
1520 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001521 case DK_CFI_WINDOW_SAVE:
1522 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001523 case DK_MACROS_ON:
1524 case DK_MACROS_OFF:
1525 return parseDirectiveMacrosOnOff(IDVal);
1526 case DK_MACRO:
1527 return parseDirectiveMacro(IDLoc);
1528 case DK_ENDM:
1529 case DK_ENDMACRO:
1530 return parseDirectiveEndMacro(IDVal);
1531 case DK_PURGEM:
1532 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001533 case DK_END:
1534 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001535 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001536 return parseDirectiveError(IDLoc, false);
1537 case DK_ERROR:
1538 return parseDirectiveError(IDLoc, true);
Eli Friedman20b02642010-07-19 04:17:25 +00001539 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001540
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001541 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001542 }
Chris Lattner36e02122009-06-21 20:54:55 +00001543
Chad Rosierc7f552c2013-02-12 21:33:51 +00001544 // __asm _emit or __asm __emit
1545 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1546 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001547 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001548
1549 // __asm align
1550 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001551 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001552
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001553 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001554
Chris Lattner7cbfa442010-05-19 23:34:33 +00001555 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001556 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001557 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001558 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001559 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001560 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001561
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001562 // Dump the parsed representation, if requested.
1563 if (getShowParsedOperands()) {
1564 SmallString<256> Str;
1565 raw_svector_ostream OS(Str);
1566 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001567 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001568 if (i != 0)
1569 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001570 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001571 }
1572 OS << "]";
1573
Jim Grosbach4b905842013-09-20 23:08:21 +00001574 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001575 }
1576
Kevin Enderby6469fc22011-11-01 22:27:22 +00001577 // If we are generating dwarf for assembly source files and the current
1578 // section is the initial text section then generate a .loc directive for
1579 // the instruction.
1580 if (!HadError && getContext().getGenDwarfForAssembly() &&
Peter Collingbourne2f495b92013-04-17 21:18:16 +00001581 getContext().getGenDwarfSection() ==
Jim Grosbach4b905842013-09-20 23:08:21 +00001582 getStreamer().getCurrentSection().first) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001583
Eli Bendersky88024712013-01-16 19:32:36 +00001584 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001585
Eli Bendersky88024712013-01-16 19:32:36 +00001586 // If we previously parsed a cpp hash file line comment then make sure the
1587 // current Dwarf File is for the CppHashFilename if not then emit the
1588 // Dwarf File table for it and adjust the line number for the .loc.
Eli Bendersky88024712013-01-16 19:32:36 +00001589 if (CppHashFilename.size() != 0) {
David Blaikiec714ef42014-03-17 01:52:11 +00001590 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1591 0, StringRef(), CppHashFilename);
1592 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001593
Jim Grosbach4b905842013-09-20 23:08:21 +00001594 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1595 // cache with the different Loc from the call above we save the last
1596 // info we queried here with SrcMgr.FindLineNumber().
1597 unsigned CppHashLocLineNo;
1598 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1599 CppHashLocLineNo = LastQueryLine;
1600 else {
1601 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1602 LastQueryLine = CppHashLocLineNo;
1603 LastQueryIDLoc = CppHashLoc;
1604 LastQueryBuffer = CppHashBuf;
1605 }
1606 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001607 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001608
Jim Grosbach4b905842013-09-20 23:08:21 +00001609 getStreamer().EmitDwarfLocDirective(
1610 getContext().getGenDwarfFileNumber(), Line, 0,
1611 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1612 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001613 }
1614
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001615 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001616 if (!HadError) {
Chad Rosier49963552012-10-13 00:26:04 +00001617 unsigned ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001618 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1619 Info.ParsedOperands, Out,
1620 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001621 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001622
Chris Lattnera2a9d162010-09-11 16:18:25 +00001623 // Don't skip the rest of the line, the instruction parser is responsible for
1624 // that.
1625 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001626}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001627
Jim Grosbach4b905842013-09-20 23:08:21 +00001628/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001629/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001630void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001631 if (!Lexer.is(AsmToken::EndOfStatement))
1632 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001633 // Eat EOL.
1634 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001635}
1636
Jim Grosbach4b905842013-09-20 23:08:21 +00001637/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001638/// ::= # number "filename"
1639/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001640bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001641 Lex(); // Eat the hash token.
1642
1643 if (getLexer().isNot(AsmToken::Integer)) {
1644 // Consume the line since in cases it is not a well-formed line directive,
1645 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001646 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001647 return false;
1648 }
1649
1650 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001651 Lex();
1652
1653 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001654 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001655 return false;
1656 }
1657
1658 StringRef Filename = getTok().getString();
1659 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001660 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001661
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001662 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1663 CppHashLoc = L;
1664 CppHashFilename = Filename;
1665 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001666 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001667
1668 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001669 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001670 return false;
1671}
1672
Jim Grosbach4b905842013-09-20 23:08:21 +00001673/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001674/// for the Filename and LineNo if any in the diagnostic.
1675void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001676 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001677 raw_ostream &OS = errs();
1678
1679 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1680 const SMLoc &DiagLoc = Diag.getLoc();
1681 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1682 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1683
Jim Grosbach4b905842013-09-20 23:08:21 +00001684 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001685 // before printing the message.
1686 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001687 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001688 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1689 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001690 }
1691
Eric Christophera7c32732012-12-18 00:30:54 +00001692 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001693 // manager changed or buffer changed (like in a nested include) then just
1694 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001695 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001696 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001697 if (Parser->SavedDiagHandler)
1698 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1699 else
1700 Diag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001701 return;
1702 }
1703
Eric Christophera7c32732012-12-18 00:30:54 +00001704 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001705 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1706 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001707 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001708
1709 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1710 int CppHashLocLineNo =
1711 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001712 int LineNo =
1713 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001714
Jim Grosbach4b905842013-09-20 23:08:21 +00001715 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1716 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001717 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001718
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001719 if (Parser->SavedDiagHandler)
1720 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1721 else
1722 NewDiag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001723}
1724
Rafael Espindola2c064482012-08-21 18:29:30 +00001725// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1726// difference being that that function accepts '@' as part of identifiers and
1727// we can't do that. AsmLexer.cpp should probably be changed to handle
1728// '@' as a special case when needed.
1729static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001730 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1731 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001732}
1733
Rafael Espindola34b9c512012-06-03 23:57:14 +00001734bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001735 ArrayRef<MCAsmMacroParameter> Parameters,
1736 ArrayRef<MCAsmMacroArgument> A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001737 unsigned NParameters = Parameters.size();
Benjamin Kramer513e7442014-02-20 13:36:32 +00001738 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001739 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001740
Preston Gurd05500642012-09-19 20:36:12 +00001741 // A macro without parameters is handled differently on Darwin:
1742 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001743 while (!Body.empty()) {
1744 // Scan for the next substitution.
1745 std::size_t End = Body.size(), Pos = 0;
1746 for (; Pos != End; ++Pos) {
1747 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001748 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001749 // This macro has no parameters, look for $0, $1, etc.
1750 if (Body[Pos] != '$' || Pos + 1 == End)
1751 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001752
Rafael Espindola1134ab232011-06-05 02:43:45 +00001753 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001754 if (Next == '$' || Next == 'n' ||
1755 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001756 break;
1757 } else {
1758 // This macro has parameters, look for \foo, \bar, etc.
1759 if (Body[Pos] == '\\' && Pos + 1 != End)
1760 break;
1761 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001762 }
1763
1764 // Add the prefix.
1765 OS << Body.slice(0, Pos);
1766
1767 // Check if we reached the end.
1768 if (Pos == End)
1769 break;
1770
Benjamin Kramer513e7442014-02-20 13:36:32 +00001771 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001772 switch (Body[Pos + 1]) {
1773 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001774 case '$':
1775 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001776 break;
1777
Jim Grosbach4b905842013-09-20 23:08:21 +00001778 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001779 case 'n':
1780 OS << A.size();
1781 break;
1782
Jim Grosbach4b905842013-09-20 23:08:21 +00001783 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001784 default: {
1785 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001786 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001787 if (Index >= A.size())
1788 break;
1789
1790 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001791 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001792 ie = A[Index].end();
1793 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001794 OS << it->getString();
1795 break;
1796 }
1797 }
1798 Pos += 2;
1799 } else {
1800 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001801 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001802 ++I;
1803
Jim Grosbach4b905842013-09-20 23:08:21 +00001804 const char *Begin = Body.data() + Pos + 1;
1805 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001806 unsigned Index = 0;
1807 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001808 if (Parameters[Index].Name == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001809 break;
1810
Preston Gurd05500642012-09-19 20:36:12 +00001811 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001812 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1813 Pos += 3;
1814 else {
1815 OS << '\\' << Argument;
1816 Pos = I;
1817 }
Preston Gurd05500642012-09-19 20:36:12 +00001818 } else {
Eli Benderskya7b905e2013-01-14 19:00:26 +00001819 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001820 ie = A[Index].end();
1821 it != ie; ++it)
Preston Gurd05500642012-09-19 20:36:12 +00001822 if (it->getKind() == AsmToken::String)
1823 OS << it->getStringContents();
1824 else
1825 OS << it->getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001826
Preston Gurd05500642012-09-19 20:36:12 +00001827 Pos += 1 + Argument.size();
1828 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001829 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001830 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001831 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001832 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001833
Rafael Espindola1134ab232011-06-05 02:43:45 +00001834 return false;
1835}
Daniel Dunbar43235712010-07-18 18:54:11 +00001836
Jim Grosbach4b905842013-09-20 23:08:21 +00001837MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1838 SMLoc EL, MemoryBuffer *I)
1839 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1840 ExitLoc(EL) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001841
Jim Grosbach4b905842013-09-20 23:08:21 +00001842static bool isOperator(AsmToken::TokenKind kind) {
1843 switch (kind) {
1844 default:
1845 return false;
1846 case AsmToken::Plus:
1847 case AsmToken::Minus:
1848 case AsmToken::Tilde:
1849 case AsmToken::Slash:
1850 case AsmToken::Star:
1851 case AsmToken::Dot:
1852 case AsmToken::Equal:
1853 case AsmToken::EqualEqual:
1854 case AsmToken::Pipe:
1855 case AsmToken::PipePipe:
1856 case AsmToken::Caret:
1857 case AsmToken::Amp:
1858 case AsmToken::AmpAmp:
1859 case AsmToken::Exclaim:
1860 case AsmToken::ExclaimEqual:
1861 case AsmToken::Percent:
1862 case AsmToken::Less:
1863 case AsmToken::LessEqual:
1864 case AsmToken::LessLess:
1865 case AsmToken::LessGreater:
1866 case AsmToken::Greater:
1867 case AsmToken::GreaterEqual:
1868 case AsmToken::GreaterGreater:
1869 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001870 }
1871}
1872
David Majnemer16252452014-01-29 00:07:39 +00001873namespace {
1874class AsmLexerSkipSpaceRAII {
1875public:
1876 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1877 Lexer.setSkipSpace(SkipSpace);
1878 }
1879
1880 ~AsmLexerSkipSpaceRAII() {
1881 Lexer.setSkipSpace(true);
1882 }
1883
1884private:
1885 AsmLexer &Lexer;
1886};
1887}
1888
David Majnemer91fc4c22014-01-29 18:57:46 +00001889bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001890 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001891 unsigned AddTokens = 0;
1892
David Majnemer16252452014-01-29 00:07:39 +00001893 // Darwin doesn't use spaces to delmit arguments.
1894 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001895
1896 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001897 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001898 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001899
David Majnemer91fc4c22014-01-29 18:57:46 +00001900 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001901 break;
Preston Gurd05500642012-09-19 20:36:12 +00001902
1903 if (Lexer.is(AsmToken::Space)) {
1904 Lex(); // Eat spaces
1905
1906 // Spaces can delimit parameters, but could also be part an expression.
1907 // If the token after a space is an operator, add the token and the next
1908 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001909 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001910 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001911 // Check to see whether the token is used as an operator,
1912 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001913 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001914 if (*NextChar == ' ')
1915 AddTokens = 2;
1916 }
1917
1918 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001919 break;
1920 }
1921 }
1922 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001923
Jim Grosbach4b905842013-09-20 23:08:21 +00001924 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001925 // to be able to fill in the remaining default parameter values
1926 if (Lexer.is(AsmToken::EndOfStatement))
1927 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001928
1929 // Adjust the current parentheses level.
1930 if (Lexer.is(AsmToken::LParen))
1931 ++ParenLevel;
1932 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1933 --ParenLevel;
1934
1935 // Append the token to the current argument list.
1936 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001937 if (AddTokens)
1938 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001939 Lex();
1940 }
Preston Gurd05500642012-09-19 20:36:12 +00001941
Rafael Espindola768b41c2012-06-15 14:02:34 +00001942 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001943 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001944 return false;
1945}
1946
1947// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001948bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001949 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001950 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001951 bool NamedParametersFound = false;
1952 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001953
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001954 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001955 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001956
Rafael Espindola768b41c2012-06-15 14:02:34 +00001957 // Parse two kinds of macro invocations:
1958 // - macros defined without any parameters accept an arbitrary number of them
1959 // - macros defined with parameters accept at most that many of them
1960 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1961 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001962 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001963 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001964
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001965 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001966 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001967 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001968 eatToEndOfStatement();
1969 return true;
1970 }
1971
1972 if (!Lexer.is(AsmToken::Equal)) {
1973 TokError("expected '=' after formal parameter identifier");
1974 eatToEndOfStatement();
1975 return true;
1976 }
1977 Lex();
1978
1979 NamedParametersFound = true;
1980 }
1981
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001982 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001983 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001984 eatToEndOfStatement();
1985 return true;
1986 }
1987
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001988 if (parseMacroArgument(FA.Value))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001989 return true;
1990
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001991 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001992 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001993 unsigned FAI = 0;
1994 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001995 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001996 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001997
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001998 if (FAI >= NParameters) {
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00001999 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002000 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002001 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002002 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002003 return true;
2004 }
2005 PI = FAI;
2006 }
2007
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002008 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002009 if (A.size() <= PI)
2010 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002011 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002012
2013 if (FALocs.size() <= PI)
2014 FALocs.resize(PI + 1);
2015
2016 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002017 }
Jim Grosbach206661622012-07-30 22:44:17 +00002018
Preston Gurd242ed3152012-09-19 20:29:04 +00002019 // At the end of the statement, fill in remaining arguments that have
2020 // default values. If there aren't any, then the next argument is
2021 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002022 if (Lexer.is(AsmToken::EndOfStatement)) {
2023 bool Failure = false;
2024 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2025 if (A[FAI].empty()) {
2026 if (M->Parameters[FAI].Required) {
2027 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2028 "missing value for required parameter "
2029 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2030 Failure = true;
2031 }
2032
2033 if (!M->Parameters[FAI].Value.empty())
2034 A[FAI] = M->Parameters[FAI].Value;
2035 }
2036 }
2037 return Failure;
2038 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002039
2040 if (Lexer.is(AsmToken::Comma))
2041 Lex();
2042 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002043
2044 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002045}
2046
Jim Grosbach4b905842013-09-20 23:08:21 +00002047const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
2048 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002049 return (I == MacroMap.end()) ? NULL : I->getValue();
2050}
2051
Jim Grosbach4b905842013-09-20 23:08:21 +00002052void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00002053 MacroMap[Name] = new MCAsmMacro(Macro);
2054}
2055
Jim Grosbach4b905842013-09-20 23:08:21 +00002056void AsmParser::undefineMacro(StringRef Name) {
2057 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002058 if (I != MacroMap.end()) {
2059 delete I->getValue();
2060 MacroMap.erase(I);
2061 }
2062}
2063
Jim Grosbach4b905842013-09-20 23:08:21 +00002064bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002065 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2066 // this, although we should protect against infinite loops.
2067 if (ActiveMacros.size() == 20)
2068 return TokError("macros cannot be nested more than 20 levels deep");
2069
Eli Bendersky38274122013-01-14 23:22:36 +00002070 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002071 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002072 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002073
Rafael Espindola1134ab232011-06-05 02:43:45 +00002074 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2075 // to hold the macro body with substitutions.
2076 SmallString<256> Buf;
2077 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002078 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002079
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002080 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002081 return true;
2082
Eli Bendersky38274122013-01-14 23:22:36 +00002083 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002084 // instantiation.
2085 OS << ".endmacro\n";
2086
Rafael Espindola1134ab232011-06-05 02:43:45 +00002087 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00002088 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002089
Daniel Dunbar43235712010-07-18 18:54:11 +00002090 // Create the macro instantiation object and add to the current macro
2091 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00002092 MacroInstantiation *MI = new MacroInstantiation(
2093 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbar43235712010-07-18 18:54:11 +00002094 ActiveMacros.push_back(MI);
2095
2096 // Jump to the macro instantiation and prime the lexer.
2097 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
2098 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
2099 Lex();
2100
2101 return false;
2102}
2103
Jim Grosbach4b905842013-09-20 23:08:21 +00002104void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002105 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002106 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002107 Lex();
2108
2109 // Pop the instantiation entry.
2110 delete ActiveMacros.back();
2111 ActiveMacros.pop_back();
2112}
2113
Jim Grosbach4b905842013-09-20 23:08:21 +00002114static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002115 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002116 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002117 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2118 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002119 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002120 case MCExpr::Target:
2121 case MCExpr::Constant:
2122 return false;
2123 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002124 const MCSymbol &S =
2125 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002126 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002127 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002128 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002129 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002130 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002131 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002132 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002133
2134 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002135}
2136
Jim Grosbach4b905842013-09-20 23:08:21 +00002137bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002138 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002139 // FIXME: Use better location, we should use proper tokens.
2140 SMLoc EqualLoc = Lexer.getLoc();
2141
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002142 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002143 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002144 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002145
Rafael Espindola72f5f172012-01-28 05:57:00 +00002146 // Note: we don't count b as used in "a = b". This is to allow
2147 // a = b
2148 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002149
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002150 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002151 return TokError("unexpected token in assignment");
2152
2153 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002154 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002155
Daniel Dunbar5f339242009-10-16 01:57:39 +00002156 // Validate that the LHS is allowed to be a variable (either it has not been
2157 // used as a symbol, or it is an absolute symbol).
2158 MCSymbol *Sym = getContext().LookupSymbol(Name);
2159 if (Sym) {
2160 // Diagnose assignment to a label.
2161 //
2162 // FIXME: Diagnostics. Note the location of the definition as a label.
2163 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002164 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002165 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2166 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002167 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002168 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2169 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002170 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002171 return Error(EqualLoc, "redefinition of '" + Name + "'");
2172 else if (!Sym->isVariable())
2173 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002174 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002175 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002176 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002177
2178 // Don't count these checks as uses.
2179 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002180 } else if (Name == ".") {
2181 if (Out.EmitValueToOffset(Value, 0)) {
2182 Error(EqualLoc, "expected absolute expression");
2183 eatToEndOfStatement();
2184 }
2185 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002186 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002187 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002188
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002189 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002190 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002191 if (NoDeadStrip)
2192 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2193
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002194 return false;
2195}
2196
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002197/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002198/// ::= identifier
2199/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002200bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002201 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002202 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2203 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002204 // handle this as a context dependent token, instead we detect adjacent tokens
2205 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002206 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2207 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002208
Hans Wennborgce69d772013-10-18 20:46:28 +00002209 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002210 Lex();
2211 if (Lexer.isNot(AsmToken::Identifier))
2212 return true;
2213
Hans Wennborgce69d772013-10-18 20:46:28 +00002214 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2215 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002216 return true;
2217
2218 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002219 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002220 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002221 Lex();
2222 return false;
2223 }
2224
Jim Grosbach4b905842013-09-20 23:08:21 +00002225 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002226 return true;
2227
Sean Callanan936b0d32010-01-19 21:44:56 +00002228 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002229
Sean Callanan686ed8d2010-01-19 20:22:31 +00002230 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002231
2232 return false;
2233}
2234
Jim Grosbach4b905842013-09-20 23:08:21 +00002235/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002236/// ::= .equ identifier ',' expression
2237/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002238/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002239bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002240 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002241
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002242 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002243 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002244
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002245 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002246 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002247 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002248
Jim Grosbach4b905842013-09-20 23:08:21 +00002249 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002250}
2251
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002252bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002253 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002254
2255 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002256 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002257 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2258 if (Str[i] != '\\') {
2259 Data += Str[i];
2260 continue;
2261 }
2262
2263 // Recognize escaped characters. Note that this escape semantics currently
2264 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2265 ++i;
2266 if (i == e)
2267 return TokError("unexpected backslash at end of string");
2268
2269 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002270 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002271 // Consume up to three octal characters.
2272 unsigned Value = Str[i] - '0';
2273
Jim Grosbach4b905842013-09-20 23:08:21 +00002274 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002275 ++i;
2276 Value = Value * 8 + (Str[i] - '0');
2277
Jim Grosbach4b905842013-09-20 23:08:21 +00002278 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002279 ++i;
2280 Value = Value * 8 + (Str[i] - '0');
2281 }
2282 }
2283
2284 if (Value > 255)
2285 return TokError("invalid octal escape sequence (out of range)");
2286
Jim Grosbach4b905842013-09-20 23:08:21 +00002287 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002288 continue;
2289 }
2290
2291 // Otherwise recognize individual escapes.
2292 switch (Str[i]) {
2293 default:
2294 // Just reject invalid escape sequences for now.
2295 return TokError("invalid escape sequence (unrecognized character)");
2296
2297 case 'b': Data += '\b'; break;
2298 case 'f': Data += '\f'; break;
2299 case 'n': Data += '\n'; break;
2300 case 'r': Data += '\r'; break;
2301 case 't': Data += '\t'; break;
2302 case '"': Data += '"'; break;
2303 case '\\': Data += '\\'; break;
2304 }
2305 }
2306
2307 return false;
2308}
2309
Jim Grosbach4b905842013-09-20 23:08:21 +00002310/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002311/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002312bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002313 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002314 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002315
Daniel Dunbara10e5192009-06-24 23:30:00 +00002316 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002317 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002318 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002319
Daniel Dunbaref668c12009-08-14 18:19:52 +00002320 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002321 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002322 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002323
Rafael Espindola64e1af82013-07-02 15:49:13 +00002324 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002325 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002326 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002327
Sean Callanan686ed8d2010-01-19 20:22:31 +00002328 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002329
2330 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002331 break;
2332
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002333 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002334 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002335 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002336 }
2337 }
2338
Sean Callanan686ed8d2010-01-19 20:22:31 +00002339 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002340 return false;
2341}
2342
Jim Grosbach4b905842013-09-20 23:08:21 +00002343/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002344/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002345bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002346 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002347 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002348
Daniel Dunbara10e5192009-06-24 23:30:00 +00002349 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002350 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002351 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002352 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002353 return true;
2354
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002355 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002356 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2357 assert(Size <= 8 && "Invalid size");
2358 uint64_t IntValue = MCE->getValue();
2359 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2360 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002361 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002362 } else
Rafael Espindola64e1af82013-07-02 15:49:13 +00002363 getStreamer().EmitValue(Value, Size);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002364
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002365 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002366 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002367
Daniel Dunbara10e5192009-06-24 23:30:00 +00002368 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002369 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002370 return TokError("unexpected token in 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
David Woodhoused6de0d92014-02-01 16:20:59 +00002379/// ParseDirectiveOctaValue
2380/// ::= .octa [ hexconstant (, hexconstant)* ]
2381bool AsmParser::parseDirectiveOctaValue() {
2382 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2383 checkForValidSection();
2384
2385 for (;;) {
2386 if (Lexer.getKind() == AsmToken::Error)
2387 return true;
2388 if (Lexer.getKind() != AsmToken::Integer &&
2389 Lexer.getKind() != AsmToken::BigNum)
2390 return TokError("unknown token in expression");
2391
2392 SMLoc ExprLoc = getLexer().getLoc();
2393 APInt IntValue = getTok().getAPIntVal();
2394 Lex();
2395
2396 uint64_t hi, lo;
2397 if (IntValue.isIntN(64)) {
2398 hi = 0;
2399 lo = IntValue.getZExtValue();
2400 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002401 // It might actually have more than 128 bits, but the top ones are zero.
2402 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002403 lo = IntValue.getLoBits(64).getZExtValue();
2404 } else
2405 return Error(ExprLoc, "literal value out of range for directive");
2406
2407 if (MAI.isLittleEndian()) {
2408 getStreamer().EmitIntValue(lo, 8);
2409 getStreamer().EmitIntValue(hi, 8);
2410 } else {
2411 getStreamer().EmitIntValue(hi, 8);
2412 getStreamer().EmitIntValue(lo, 8);
2413 }
2414
2415 if (getLexer().is(AsmToken::EndOfStatement))
2416 break;
2417
2418 // FIXME: Improve diagnostic.
2419 if (getLexer().isNot(AsmToken::Comma))
2420 return TokError("unexpected token in directive");
2421 Lex();
2422 }
2423 }
2424
2425 Lex();
2426 return false;
2427}
2428
Jim Grosbach4b905842013-09-20 23:08:21 +00002429/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002430/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002431bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002432 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002433 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002434
2435 for (;;) {
2436 // We don't truly support arithmetic on floating point expressions, so we
2437 // have to manually parse unary prefixes.
2438 bool IsNeg = false;
2439 if (getLexer().is(AsmToken::Minus)) {
2440 Lex();
2441 IsNeg = true;
2442 } else if (getLexer().is(AsmToken::Plus))
2443 Lex();
2444
Michael J. Spencer530ce852010-10-09 11:00:50 +00002445 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002446 getLexer().isNot(AsmToken::Real) &&
2447 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002448 return TokError("unexpected token in directive");
2449
2450 // Convert to an APFloat.
2451 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002452 StringRef IDVal = getTok().getString();
2453 if (getLexer().is(AsmToken::Identifier)) {
2454 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2455 Value = APFloat::getInf(Semantics);
2456 else if (!IDVal.compare_lower("nan"))
2457 Value = APFloat::getNaN(Semantics, false, ~0);
2458 else
2459 return TokError("invalid floating point literal");
2460 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002461 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002462 return TokError("invalid floating point literal");
2463 if (IsNeg)
2464 Value.changeSign();
2465
2466 // Consume the numeric token.
2467 Lex();
2468
2469 // Emit the value as an integer.
2470 APInt AsInt = Value.bitcastToAPInt();
2471 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002472 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002473
2474 if (getLexer().is(AsmToken::EndOfStatement))
2475 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002476
Daniel Dunbar2af16532010-09-24 01:59:56 +00002477 if (getLexer().isNot(AsmToken::Comma))
2478 return TokError("unexpected token in directive");
2479 Lex();
2480 }
2481 }
2482
2483 Lex();
2484 return false;
2485}
2486
Jim Grosbach4b905842013-09-20 23:08:21 +00002487/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002488/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002489bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002490 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002491
2492 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002493 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002494 return true;
2495
Rafael Espindolab91bac62010-10-05 19:42:57 +00002496 int64_t Val = 0;
2497 if (getLexer().is(AsmToken::Comma)) {
2498 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002499 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002500 return true;
2501 }
2502
Rafael Espindola922e3f42010-09-16 15:03:59 +00002503 if (getLexer().isNot(AsmToken::EndOfStatement))
2504 return TokError("unexpected token in '.zero' directive");
2505
2506 Lex();
2507
Rafael Espindola64e1af82013-07-02 15:49:13 +00002508 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002509
2510 return false;
2511}
2512
Jim Grosbach4b905842013-09-20 23:08:21 +00002513/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002514/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002515bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002516 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002517
David Majnemer522d3db2014-02-01 07:19:38 +00002518 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002519 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002520 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002521 return true;
2522
David Majnemer522d3db2014-02-01 07:19:38 +00002523 if (NumValues < 0) {
2524 Warning(RepeatLoc,
2525 "'.fill' directive with negative repeat count has no effect");
2526 NumValues = 0;
2527 }
2528
Roman Divackye33098f2013-09-24 17:44:41 +00002529 int64_t FillSize = 1;
2530 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002531
David Majnemer522d3db2014-02-01 07:19:38 +00002532 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002533 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2534 if (getLexer().isNot(AsmToken::Comma))
2535 return TokError("unexpected token in '.fill' directive");
2536 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002537
David Majnemer522d3db2014-02-01 07:19:38 +00002538 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002539 if (parseAbsoluteExpression(FillSize))
2540 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002541
Roman Divackye33098f2013-09-24 17:44:41 +00002542 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2543 if (getLexer().isNot(AsmToken::Comma))
2544 return TokError("unexpected token in '.fill' directive");
2545 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002546
David Majnemer522d3db2014-02-01 07:19:38 +00002547 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002548 if (parseAbsoluteExpression(FillExpr))
2549 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002550
Roman Divackye33098f2013-09-24 17:44:41 +00002551 if (getLexer().isNot(AsmToken::EndOfStatement))
2552 return TokError("unexpected token in '.fill' directive");
2553
2554 Lex();
2555 }
2556 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002557
David Majnemer522d3db2014-02-01 07:19:38 +00002558 if (FillSize < 0) {
2559 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2560 NumValues = 0;
2561 }
2562 if (FillSize > 8) {
2563 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2564 FillSize = 8;
2565 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002566
David Majnemer522d3db2014-02-01 07:19:38 +00002567 if (!isUInt<32>(FillExpr) && FillSize > 4)
2568 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2569
2570 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2571 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2572
2573 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2574 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2575 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2576 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002577
2578 return false;
2579}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002580
Jim Grosbach4b905842013-09-20 23:08:21 +00002581/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002582/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002583bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002584 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002585
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002586 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002587 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002588 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002589 return true;
2590
2591 // Parse optional fill expression.
2592 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002593 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2594 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002595 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002596 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002597
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002598 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002599 return true;
2600
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002601 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002602 return TokError("unexpected token in '.org' directive");
2603 }
2604
Sean Callanan686ed8d2010-01-19 20:22:31 +00002605 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002606
Jim Grosbachb5912772012-01-27 00:37:08 +00002607 // Only limited forms of relocatable expressions are accepted here, it
2608 // has to be relative to the current section. The streamer will return
2609 // 'true' if the expression wasn't evaluatable.
2610 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2611 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002612
2613 return false;
2614}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002615
Jim Grosbach4b905842013-09-20 23:08:21 +00002616/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002617/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002618bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002619 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002620
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002621 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002622 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002623 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002624 return true;
2625
2626 SMLoc MaxBytesLoc;
2627 bool HasFillExpr = false;
2628 int64_t FillExpr = 0;
2629 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002630 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2631 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002632 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002633 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002634
2635 // The fill expression can be omitted while specifying a maximum number of
2636 // alignment bytes, e.g:
2637 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002638 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002639 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002640 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002641 return true;
2642 }
2643
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002644 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2645 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002646 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002647 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002648
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002649 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002650 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002651 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002652
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002653 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002654 return TokError("unexpected token in directive");
2655 }
2656 }
2657
Sean Callanan686ed8d2010-01-19 20:22:31 +00002658 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002659
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002660 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002661 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002662
2663 // Compute alignment in bytes.
2664 if (IsPow2) {
2665 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002666 if (Alignment >= 32) {
2667 Error(AlignmentLoc, "invalid alignment value");
2668 Alignment = 31;
2669 }
2670
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002671 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002672 } else {
2673 // Reject alignments that aren't a power of two, for gas compatibility.
2674 if (!isPowerOf2_64(Alignment))
2675 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002676 }
2677
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002678 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002679 if (MaxBytesLoc.isValid()) {
2680 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002681 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002682 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002683 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002684 }
2685
2686 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002687 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002688 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002689 MaxBytesToFill = 0;
2690 }
2691 }
2692
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002693 // Check whether we should use optimal code alignment for this .align
2694 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002695 const MCSection *Section = getStreamer().getCurrentSection().first;
2696 assert(Section && "must have section to emit alignment");
2697 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002698 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2699 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002700 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002701 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002702 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002703 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2704 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002705 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002706
2707 return false;
2708}
2709
Jim Grosbach4b905842013-09-20 23:08:21 +00002710/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002711/// ::= .file [number] filename
2712/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002713bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002714 // FIXME: I'm not sure what this is.
2715 int64_t FileNumber = -1;
2716 SMLoc FileNumberLoc = getLexer().getLoc();
2717 if (getLexer().is(AsmToken::Integer)) {
2718 FileNumber = getTok().getIntVal();
2719 Lex();
2720
2721 if (FileNumber < 1)
2722 return TokError("file number less than one");
2723 }
2724
2725 if (getLexer().isNot(AsmToken::String))
2726 return TokError("unexpected token in '.file' directive");
2727
2728 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002729 // Allow the strings to have escaped octal character sequence.
2730 std::string Path = getTok().getString();
2731 if (parseEscapedString(Path))
2732 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002733 Lex();
2734
2735 StringRef Directory;
2736 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002737 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002738 if (getLexer().is(AsmToken::String)) {
2739 if (FileNumber == -1)
2740 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002741 if (parseEscapedString(FilenameData))
2742 return true;
2743 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002744 Directory = Path;
2745 Lex();
2746 } else {
2747 Filename = Path;
2748 }
2749
2750 if (getLexer().isNot(AsmToken::EndOfStatement))
2751 return TokError("unexpected token in '.file' directive");
2752
2753 if (FileNumber == -1)
2754 getStreamer().EmitFileDirective(Filename);
2755 else {
2756 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002757 Error(DirectiveLoc,
2758 "input can't have .file dwarf directives when -g is "
2759 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002760
David Blaikiec714ef42014-03-17 01:52:11 +00002761 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2762 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002763 Error(FileNumberLoc, "file number already allocated");
2764 }
2765
2766 return false;
2767}
2768
Jim Grosbach4b905842013-09-20 23:08:21 +00002769/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002770/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002771bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002772 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2773 if (getLexer().isNot(AsmToken::Integer))
2774 return TokError("unexpected token in '.line' directive");
2775
2776 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002777 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002778 Lex();
2779
2780 // FIXME: Do something with the .line.
2781 }
2782
2783 if (getLexer().isNot(AsmToken::EndOfStatement))
2784 return TokError("unexpected token in '.line' directive");
2785
2786 return false;
2787}
2788
Jim Grosbach4b905842013-09-20 23:08:21 +00002789/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002790/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2791/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2792/// The first number is a file number, must have been previously assigned with
2793/// a .file directive, the second number is the line number and optionally the
2794/// third number is a column position (zero if not specified). The remaining
2795/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002796bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002797 if (getLexer().isNot(AsmToken::Integer))
2798 return TokError("unexpected token in '.loc' directive");
2799 int64_t FileNumber = getTok().getIntVal();
2800 if (FileNumber < 1)
2801 return TokError("file number less than one in '.loc' directive");
2802 if (!getContext().isValidDwarfFileNumber(FileNumber))
2803 return TokError("unassigned file number in '.loc' directive");
2804 Lex();
2805
2806 int64_t LineNumber = 0;
2807 if (getLexer().is(AsmToken::Integer)) {
2808 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002809 if (LineNumber < 0)
2810 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002811 Lex();
2812 }
2813
2814 int64_t ColumnPos = 0;
2815 if (getLexer().is(AsmToken::Integer)) {
2816 ColumnPos = getTok().getIntVal();
2817 if (ColumnPos < 0)
2818 return TokError("column position less than zero in '.loc' directive");
2819 Lex();
2820 }
2821
2822 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2823 unsigned Isa = 0;
2824 int64_t Discriminator = 0;
2825 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2826 for (;;) {
2827 if (getLexer().is(AsmToken::EndOfStatement))
2828 break;
2829
2830 StringRef Name;
2831 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002832 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002833 return TokError("unexpected token in '.loc' directive");
2834
2835 if (Name == "basic_block")
2836 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2837 else if (Name == "prologue_end")
2838 Flags |= DWARF2_FLAG_PROLOGUE_END;
2839 else if (Name == "epilogue_begin")
2840 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2841 else if (Name == "is_stmt") {
2842 Loc = getTok().getLoc();
2843 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002844 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002845 return true;
2846 // The expression must be the constant 0 or 1.
2847 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2848 int Value = MCE->getValue();
2849 if (Value == 0)
2850 Flags &= ~DWARF2_FLAG_IS_STMT;
2851 else if (Value == 1)
2852 Flags |= DWARF2_FLAG_IS_STMT;
2853 else
2854 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002855 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002856 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2857 }
Craig Topperf15655b2013-04-22 04:22:40 +00002858 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002859 Loc = getTok().getLoc();
2860 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002861 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002862 return true;
2863 // The expression must be a constant greater or equal to 0.
2864 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2865 int Value = MCE->getValue();
2866 if (Value < 0)
2867 return Error(Loc, "isa number less than zero");
2868 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002869 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002870 return Error(Loc, "isa number not a constant value");
2871 }
Craig Topperf15655b2013-04-22 04:22:40 +00002872 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002873 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002874 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002875 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002876 return Error(Loc, "unknown sub-directive in '.loc' directive");
2877 }
2878
2879 if (getLexer().is(AsmToken::EndOfStatement))
2880 break;
2881 }
2882 }
2883
2884 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2885 Isa, Discriminator, StringRef());
2886
2887 return false;
2888}
2889
Jim Grosbach4b905842013-09-20 23:08:21 +00002890/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002891/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002892bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002893 return TokError("unsupported directive '.stabs'");
2894}
2895
Jim Grosbach4b905842013-09-20 23:08:21 +00002896/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002897/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002898bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002899 StringRef Name;
2900 bool EH = false;
2901 bool Debug = false;
2902
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002903 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002904 return TokError("Expected an identifier");
2905
2906 if (Name == ".eh_frame")
2907 EH = true;
2908 else if (Name == ".debug_frame")
2909 Debug = true;
2910
2911 if (getLexer().is(AsmToken::Comma)) {
2912 Lex();
2913
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002914 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002915 return TokError("Expected an identifier");
2916
2917 if (Name == ".eh_frame")
2918 EH = true;
2919 else if (Name == ".debug_frame")
2920 Debug = true;
2921 }
2922
2923 getStreamer().EmitCFISections(EH, Debug);
2924 return false;
2925}
2926
Jim Grosbach4b905842013-09-20 23:08:21 +00002927/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002928/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002929bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002930 StringRef Simple;
2931 if (getLexer().isNot(AsmToken::EndOfStatement))
2932 if (parseIdentifier(Simple) || Simple != "simple")
2933 return TokError("unexpected token in .cfi_startproc directive");
2934
2935 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002936 return false;
2937}
2938
Jim Grosbach4b905842013-09-20 23:08:21 +00002939/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002940/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002941bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002942 getStreamer().EmitCFIEndProc();
2943 return false;
2944}
2945
Jim Grosbach4b905842013-09-20 23:08:21 +00002946/// \brief parse register name or number.
2947bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002948 SMLoc DirectiveLoc) {
2949 unsigned RegNo;
2950
2951 if (getLexer().isNot(AsmToken::Integer)) {
2952 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2953 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002954 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002955 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002956 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002957
2958 return false;
2959}
2960
Jim Grosbach4b905842013-09-20 23:08:21 +00002961/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002962/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002963bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002964 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002965 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002966 return true;
2967
2968 if (getLexer().isNot(AsmToken::Comma))
2969 return TokError("unexpected token in directive");
2970 Lex();
2971
2972 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002973 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002974 return true;
2975
2976 getStreamer().EmitCFIDefCfa(Register, Offset);
2977 return false;
2978}
2979
Jim Grosbach4b905842013-09-20 23:08:21 +00002980/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002981/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002982bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002983 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002984 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002985 return true;
2986
2987 getStreamer().EmitCFIDefCfaOffset(Offset);
2988 return false;
2989}
2990
Jim Grosbach4b905842013-09-20 23:08:21 +00002991/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002992/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00002993bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002994 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002995 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002996 return true;
2997
2998 if (getLexer().isNot(AsmToken::Comma))
2999 return TokError("unexpected token in directive");
3000 Lex();
3001
3002 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003003 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003004 return true;
3005
3006 getStreamer().EmitCFIRegister(Register1, Register2);
3007 return false;
3008}
3009
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003010/// parseDirectiveCFIWindowSave
3011/// ::= .cfi_window_save
3012bool AsmParser::parseDirectiveCFIWindowSave() {
3013 getStreamer().EmitCFIWindowSave();
3014 return false;
3015}
3016
Jim Grosbach4b905842013-09-20 23:08:21 +00003017/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003018/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003019bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003020 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003021 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003022 return true;
3023
3024 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3025 return false;
3026}
3027
Jim Grosbach4b905842013-09-20 23:08:21 +00003028/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003029/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003030bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003031 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003032 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003033 return true;
3034
3035 getStreamer().EmitCFIDefCfaRegister(Register);
3036 return false;
3037}
3038
Jim Grosbach4b905842013-09-20 23:08:21 +00003039/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003040/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003041bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003042 int64_t Register = 0;
3043 int64_t Offset = 0;
3044
Jim Grosbach4b905842013-09-20 23:08:21 +00003045 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003046 return true;
3047
3048 if (getLexer().isNot(AsmToken::Comma))
3049 return TokError("unexpected token in directive");
3050 Lex();
3051
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003052 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003053 return true;
3054
3055 getStreamer().EmitCFIOffset(Register, Offset);
3056 return false;
3057}
3058
Jim Grosbach4b905842013-09-20 23:08:21 +00003059/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003060/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003061bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003062 int64_t Register = 0;
3063
Jim Grosbach4b905842013-09-20 23:08:21 +00003064 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003065 return true;
3066
3067 if (getLexer().isNot(AsmToken::Comma))
3068 return TokError("unexpected token in directive");
3069 Lex();
3070
3071 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003072 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003073 return true;
3074
3075 getStreamer().EmitCFIRelOffset(Register, Offset);
3076 return false;
3077}
3078
3079static bool isValidEncoding(int64_t Encoding) {
3080 if (Encoding & ~0xff)
3081 return false;
3082
3083 if (Encoding == dwarf::DW_EH_PE_omit)
3084 return true;
3085
3086 const unsigned Format = Encoding & 0xf;
3087 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3088 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3089 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3090 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3091 return false;
3092
3093 const unsigned Application = Encoding & 0x70;
3094 if (Application != dwarf::DW_EH_PE_absptr &&
3095 Application != dwarf::DW_EH_PE_pcrel)
3096 return false;
3097
3098 return true;
3099}
3100
Jim Grosbach4b905842013-09-20 23:08:21 +00003101/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003102/// IsPersonality true for cfi_personality, false for cfi_lsda
3103/// ::= .cfi_personality encoding, [symbol_name]
3104/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003105bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003106 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003107 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003108 return true;
3109 if (Encoding == dwarf::DW_EH_PE_omit)
3110 return false;
3111
3112 if (!isValidEncoding(Encoding))
3113 return TokError("unsupported encoding.");
3114
3115 if (getLexer().isNot(AsmToken::Comma))
3116 return TokError("unexpected token in directive");
3117 Lex();
3118
3119 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003120 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003121 return TokError("expected identifier in directive");
3122
3123 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3124
3125 if (IsPersonality)
3126 getStreamer().EmitCFIPersonality(Sym, Encoding);
3127 else
3128 getStreamer().EmitCFILsda(Sym, Encoding);
3129 return false;
3130}
3131
Jim Grosbach4b905842013-09-20 23:08:21 +00003132/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003133/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003134bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003135 getStreamer().EmitCFIRememberState();
3136 return false;
3137}
3138
Jim Grosbach4b905842013-09-20 23:08:21 +00003139/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003140/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003141bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003142 getStreamer().EmitCFIRestoreState();
3143 return false;
3144}
3145
Jim Grosbach4b905842013-09-20 23:08:21 +00003146/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003147/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003148bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003149 int64_t Register = 0;
3150
Jim Grosbach4b905842013-09-20 23:08:21 +00003151 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003152 return true;
3153
3154 getStreamer().EmitCFISameValue(Register);
3155 return false;
3156}
3157
Jim Grosbach4b905842013-09-20 23:08:21 +00003158/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003159/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003160bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003161 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003162 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003163 return true;
3164
3165 getStreamer().EmitCFIRestore(Register);
3166 return false;
3167}
3168
Jim Grosbach4b905842013-09-20 23:08:21 +00003169/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003170/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003171bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003172 std::string Values;
3173 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003174 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003175 return true;
3176
3177 Values.push_back((uint8_t)CurrValue);
3178
3179 while (getLexer().is(AsmToken::Comma)) {
3180 Lex();
3181
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003182 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003183 return true;
3184
3185 Values.push_back((uint8_t)CurrValue);
3186 }
3187
3188 getStreamer().EmitCFIEscape(Values);
3189 return false;
3190}
3191
Jim Grosbach4b905842013-09-20 23:08:21 +00003192/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003193/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003194bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003195 if (getLexer().isNot(AsmToken::EndOfStatement))
3196 return Error(getLexer().getLoc(),
3197 "unexpected token in '.cfi_signal_frame'");
3198
3199 getStreamer().EmitCFISignalFrame();
3200 return false;
3201}
3202
Jim Grosbach4b905842013-09-20 23:08:21 +00003203/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003204/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003205bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003206 int64_t Register = 0;
3207
Jim Grosbach4b905842013-09-20 23:08:21 +00003208 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003209 return true;
3210
3211 getStreamer().EmitCFIUndefined(Register);
3212 return false;
3213}
3214
Jim Grosbach4b905842013-09-20 23:08:21 +00003215/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003216/// ::= .macros_on
3217/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003218bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003219 if (getLexer().isNot(AsmToken::EndOfStatement))
3220 return Error(getLexer().getLoc(),
3221 "unexpected token in '" + Directive + "' directive");
3222
Jim Grosbach4b905842013-09-20 23:08:21 +00003223 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003224 return false;
3225}
3226
Jim Grosbach4b905842013-09-20 23:08:21 +00003227/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003228/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003229bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003230 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003231 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003232 return TokError("expected identifier in '.macro' directive");
3233
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003234 if (getLexer().is(AsmToken::Comma))
3235 Lex();
3236
Eli Bendersky17233942013-01-15 22:59:42 +00003237 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003238 while (getLexer().isNot(AsmToken::EndOfStatement)) {
3239 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003240 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003241 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003242
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003243 if (Lexer.is(AsmToken::Colon)) {
3244 Lex(); // consume ':'
3245
3246 SMLoc QualLoc;
3247 StringRef Qualifier;
3248
3249 QualLoc = Lexer.getLoc();
3250 if (parseIdentifier(Qualifier))
3251 return Error(QualLoc, "missing parameter qualifier for "
3252 "'" + Parameter.Name + "' in macro '" + Name + "'");
3253
3254 if (Qualifier == "req")
3255 Parameter.Required = true;
3256 else
3257 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3258 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3259 }
3260
David Majnemer91fc4c22014-01-29 18:57:46 +00003261 if (getLexer().is(AsmToken::Equal)) {
3262 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003263
3264 SMLoc ParamLoc;
3265
3266 ParamLoc = Lexer.getLoc();
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003267 if (parseMacroArgument(Parameter.Value))
David Majnemer91fc4c22014-01-29 18:57:46 +00003268 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003269
3270 if (Parameter.Required)
3271 Warning(ParamLoc, "pointless default value for required parameter "
3272 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003273 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003274
3275 Parameters.push_back(Parameter);
3276
3277 if (getLexer().is(AsmToken::Comma))
3278 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003279 }
3280
3281 // Eat the end of statement.
3282 Lex();
3283
3284 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003285 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003286
3287 // Lex the macro definition.
3288 for (;;) {
3289 // Check whether we have reached the end of the file.
3290 if (getLexer().is(AsmToken::Eof))
3291 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3292
3293 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003294 if (getLexer().is(AsmToken::Identifier)) {
3295 if (getTok().getIdentifier() == ".endm" ||
3296 getTok().getIdentifier() == ".endmacro") {
3297 if (MacroDepth == 0) { // Outermost macro.
3298 EndToken = getTok();
3299 Lex();
3300 if (getLexer().isNot(AsmToken::EndOfStatement))
3301 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3302 "' directive");
3303 break;
3304 } else {
3305 // Otherwise we just found the end of an inner macro.
3306 --MacroDepth;
3307 }
3308 } else if (getTok().getIdentifier() == ".macro") {
3309 // We allow nested macros. Those aren't instantiated until the outermost
3310 // macro is expanded so just ignore them for now.
3311 ++MacroDepth;
3312 }
Eli Bendersky17233942013-01-15 22:59:42 +00003313 }
3314
3315 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003316 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003317 }
3318
Jim Grosbach4b905842013-09-20 23:08:21 +00003319 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003320 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3321 }
3322
3323 const char *BodyStart = StartToken.getLoc().getPointer();
3324 const char *BodyEnd = EndToken.getLoc().getPointer();
3325 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003326 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3327 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003328 return false;
3329}
3330
Jim Grosbach4b905842013-09-20 23:08:21 +00003331/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003332///
3333/// With the support added for named parameters there may be code out there that
3334/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003335/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003336/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003337/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003338/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3339/// warning that the positional parameter found in body which have no effect.
3340/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003341/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003342/// intended or change the macro to use the named parameters. It is possible
3343/// this warning will trigger when the none of the named parameters are used
3344/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003345void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003346 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003347 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003348 // If this macro is not defined with named parameters the warning we are
3349 // checking for here doesn't apply.
3350 unsigned NParameters = Parameters.size();
3351 if (NParameters == 0)
3352 return;
3353
3354 bool NamedParametersFound = false;
3355 bool PositionalParametersFound = false;
3356
3357 // Look at the body of the macro for use of both the named parameters and what
3358 // are likely to be positional parameters. This is what expandMacro() is
3359 // doing when it finds the parameters in the body.
3360 while (!Body.empty()) {
3361 // Scan for the next possible parameter.
3362 std::size_t End = Body.size(), Pos = 0;
3363 for (; Pos != End; ++Pos) {
3364 // Check for a substitution or escape.
3365 // This macro is defined with parameters, look for \foo, \bar, etc.
3366 if (Body[Pos] == '\\' && Pos + 1 != End)
3367 break;
3368
3369 // This macro should have parameters, but look for $0, $1, ..., $n too.
3370 if (Body[Pos] != '$' || Pos + 1 == End)
3371 continue;
3372 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003373 if (Next == '$' || Next == 'n' ||
3374 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003375 break;
3376 }
3377
3378 // Check if we reached the end.
3379 if (Pos == End)
3380 break;
3381
3382 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003383 switch (Body[Pos + 1]) {
3384 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003385 case '$':
3386 break;
3387
Jim Grosbach4b905842013-09-20 23:08:21 +00003388 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003389 case 'n':
3390 PositionalParametersFound = true;
3391 break;
3392
Jim Grosbach4b905842013-09-20 23:08:21 +00003393 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003394 default: {
3395 PositionalParametersFound = true;
3396 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003397 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003398 }
3399 Pos += 2;
3400 } else {
3401 unsigned I = Pos + 1;
3402 while (isIdentifierChar(Body[I]) && I + 1 != End)
3403 ++I;
3404
Jim Grosbach4b905842013-09-20 23:08:21 +00003405 const char *Begin = Body.data() + Pos + 1;
3406 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003407 unsigned Index = 0;
3408 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003409 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003410 break;
3411
3412 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003413 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3414 Pos += 3;
3415 else {
3416 Pos = I;
3417 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003418 } else {
3419 NamedParametersFound = true;
3420 Pos += 1 + Argument.size();
3421 }
3422 }
3423 // Update the scan point.
3424 Body = Body.substr(Pos);
3425 }
3426
3427 if (!NamedParametersFound && PositionalParametersFound)
3428 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3429 "used in macro body, possible positional parameter "
3430 "found in body which will have no effect");
3431}
3432
Jim Grosbach4b905842013-09-20 23:08:21 +00003433/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003434/// ::= .endm
3435/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003436bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003437 if (getLexer().isNot(AsmToken::EndOfStatement))
3438 return TokError("unexpected token in '" + Directive + "' directive");
3439
3440 // If we are inside a macro instantiation, terminate the current
3441 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003442 if (isInsideMacroInstantiation()) {
3443 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003444 return false;
3445 }
3446
3447 // Otherwise, this .endmacro is a stray entry in the file; well formed
3448 // .endmacro directives are handled during the macro definition parsing.
3449 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003450 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003451}
3452
Jim Grosbach4b905842013-09-20 23:08:21 +00003453/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003454/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003455bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003456 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003457 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003458 return TokError("expected identifier in '.purgem' directive");
3459
3460 if (getLexer().isNot(AsmToken::EndOfStatement))
3461 return TokError("unexpected token in '.purgem' directive");
3462
Jim Grosbach4b905842013-09-20 23:08:21 +00003463 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003464 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3465
Jim Grosbach4b905842013-09-20 23:08:21 +00003466 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003467 return false;
3468}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003469
Jim Grosbach4b905842013-09-20 23:08:21 +00003470/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003471/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003472bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003473 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003474
3475 // Expect a single argument: an expression that evaluates to a constant
3476 // in the inclusive range 0-30.
3477 SMLoc ExprLoc = getLexer().getLoc();
3478 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003479 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003480 return true;
3481 else if (getLexer().isNot(AsmToken::EndOfStatement))
3482 return TokError("unexpected token after expression in"
3483 " '.bundle_align_mode' directive");
3484 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3485 return Error(ExprLoc,
3486 "invalid bundle alignment size (expected between 0 and 30)");
3487
3488 Lex();
3489
3490 // Because of AlignSizePow2's verified range we can safely truncate it to
3491 // unsigned.
3492 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3493 return false;
3494}
3495
Jim Grosbach4b905842013-09-20 23:08:21 +00003496/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003497/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003498bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003499 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003500 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003501
Eli Bendersky802b6282013-01-07 21:51:08 +00003502 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3503 StringRef Option;
3504 SMLoc Loc = getTok().getLoc();
3505 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003506 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003507
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003508 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003509 return Error(Loc, kInvalidOptionError);
3510
3511 if (Option != "align_to_end")
3512 return Error(Loc, kInvalidOptionError);
3513 else if (getLexer().isNot(AsmToken::EndOfStatement))
3514 return Error(Loc,
3515 "unexpected token after '.bundle_lock' directive option");
3516 AlignToEnd = true;
3517 }
3518
Eli Benderskyf483ff92012-12-20 19:05:53 +00003519 Lex();
3520
Eli Bendersky802b6282013-01-07 21:51:08 +00003521 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003522 return false;
3523}
3524
Jim Grosbach4b905842013-09-20 23:08:21 +00003525/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003526/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003527bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003528 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003529
3530 if (getLexer().isNot(AsmToken::EndOfStatement))
3531 return TokError("unexpected token in '.bundle_unlock' directive");
3532 Lex();
3533
3534 getStreamer().EmitBundleUnlock();
3535 return false;
3536}
3537
Jim Grosbach4b905842013-09-20 23:08:21 +00003538/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003539/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003540bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003541 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003542
3543 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003544 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003545 return true;
3546
3547 int64_t FillExpr = 0;
3548 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3549 if (getLexer().isNot(AsmToken::Comma))
3550 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3551 Lex();
3552
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003553 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003554 return true;
3555
3556 if (getLexer().isNot(AsmToken::EndOfStatement))
3557 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3558 }
3559
3560 Lex();
3561
3562 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003563 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3564 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003565
3566 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003567 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003568
3569 return false;
3570}
3571
Jim Grosbach4b905842013-09-20 23:08:21 +00003572/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003573/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003574bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003575 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003576 const MCExpr *Value;
3577
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003578 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003579 return true;
3580
3581 if (getLexer().isNot(AsmToken::EndOfStatement))
3582 return TokError("unexpected token in directive");
3583
3584 if (Signed)
3585 getStreamer().EmitSLEB128Value(Value);
3586 else
3587 getStreamer().EmitULEB128Value(Value);
3588
3589 return false;
3590}
3591
Jim Grosbach4b905842013-09-20 23:08:21 +00003592/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003593/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003594bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003595 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003596 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003597 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003598 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003599
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003600 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003601 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003602
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003603 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003604
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003605 // Assembler local symbols don't make any sense here. Complain loudly.
3606 if (Sym->isTemporary())
3607 return Error(Loc, "non-local symbol required in directive");
3608
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003609 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3610 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003611
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003612 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003613 break;
3614
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003615 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003616 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003617 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003618 }
3619 }
3620
Sean Callanan686ed8d2010-01-19 20:22:31 +00003621 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003622 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003623}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003624
Jim Grosbach4b905842013-09-20 23:08:21 +00003625/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003626/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003627bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003628 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003629
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003630 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003631 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003632 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003633 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003634
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003635 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003636 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003637
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003638 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003639 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003640 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003641
3642 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003643 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003644 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003645 return true;
3646
3647 int64_t Pow2Alignment = 0;
3648 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003649 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003650 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003651 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003652 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003653 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003654
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003655 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3656 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003657 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3658
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003659 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003660 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3661 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003662 if (!isPowerOf2_64(Pow2Alignment))
3663 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3664 Pow2Alignment = Log2_64(Pow2Alignment);
3665 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003666 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003667
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003668 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003669 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003670
Sean Callanan686ed8d2010-01-19 20:22:31 +00003671 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003672
Chris Lattner28ad7542009-07-09 17:25:12 +00003673 // NOTE: a size of zero for a .comm should create a undefined symbol
3674 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003675 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003676 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003677 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003678
Eric Christopherbc818852010-05-14 01:38:54 +00003679 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003680 // may internally end up wanting an alignment in bytes.
3681 // FIXME: Diagnose overflow.
3682 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003683 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003684 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003685
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003686 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003687 return Error(IDLoc, "invalid symbol redefinition");
3688
Chris Lattner28ad7542009-07-09 17:25:12 +00003689 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003690 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003691 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003692 return false;
3693 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003694
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003695 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003696 return false;
3697}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003698
Jim Grosbach4b905842013-09-20 23:08:21 +00003699/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003700/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003701bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003702 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003703 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003704
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003705 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003706 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003707 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003708
Sean Callanan686ed8d2010-01-19 20:22:31 +00003709 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003710
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003711 if (Str.empty())
3712 Error(Loc, ".abort detected. Assembly stopping.");
3713 else
3714 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003715 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003716
3717 return false;
3718}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003719
Jim Grosbach4b905842013-09-20 23:08:21 +00003720/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003721/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003722bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003723 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003724 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003725
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003726 // Allow the strings to have escaped octal character sequence.
3727 std::string Filename;
3728 if (parseEscapedString(Filename))
3729 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003730 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003731 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003732
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003733 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003734 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003735
Chris Lattner693fbb82009-07-16 06:14:39 +00003736 // Attempt to switch the lexer to the included file before consuming the end
3737 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003738 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003739 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003740 return true;
3741 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003742
3743 return false;
3744}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003745
Jim Grosbach4b905842013-09-20 23:08:21 +00003746/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003747/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003748bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003749 if (getLexer().isNot(AsmToken::String))
3750 return TokError("expected string in '.incbin' directive");
3751
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003752 // Allow the strings to have escaped octal character sequence.
3753 std::string Filename;
3754 if (parseEscapedString(Filename))
3755 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003756 SMLoc IncbinLoc = getLexer().getLoc();
3757 Lex();
3758
3759 if (getLexer().isNot(AsmToken::EndOfStatement))
3760 return TokError("unexpected token in '.incbin' directive");
3761
Kevin Enderby109f25c2011-12-14 21:47:48 +00003762 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003763 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003764 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3765 return true;
3766 }
3767
3768 return false;
3769}
3770
Jim Grosbach4b905842013-09-20 23:08:21 +00003771/// parseDirectiveIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003772/// ::= .if expression
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00003773/// ::= .ifne expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003774bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003775 TheCondStack.push_back(TheCondState);
3776 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003777 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003778 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003779 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003780 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003781 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003782 return true;
3783
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003784 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003785 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003786
Sean Callanan686ed8d2010-01-19 20:22:31 +00003787 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003788
3789 TheCondState.CondMet = ExprValue;
3790 TheCondState.Ignore = !TheCondState.CondMet;
3791 }
3792
3793 return false;
3794}
3795
Jim Grosbach4b905842013-09-20 23:08:21 +00003796/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003797/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003798bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003799 TheCondStack.push_back(TheCondState);
3800 TheCondState.TheCond = AsmCond::IfCond;
3801
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003802 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003803 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003804 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003805 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003806
3807 if (getLexer().isNot(AsmToken::EndOfStatement))
3808 return TokError("unexpected token in '.ifb' directive");
3809
3810 Lex();
3811
3812 TheCondState.CondMet = ExpectBlank == Str.empty();
3813 TheCondState.Ignore = !TheCondState.CondMet;
3814 }
3815
3816 return false;
3817}
3818
Jim Grosbach4b905842013-09-20 23:08:21 +00003819/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003820/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003821/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003822bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003823 TheCondStack.push_back(TheCondState);
3824 TheCondState.TheCond = AsmCond::IfCond;
3825
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003826 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003827 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003828 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003829 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003830
3831 if (getLexer().isNot(AsmToken::Comma))
3832 return TokError("unexpected token in '.ifc' directive");
3833
3834 Lex();
3835
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003836 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003837
3838 if (getLexer().isNot(AsmToken::EndOfStatement))
3839 return TokError("unexpected token in '.ifc' directive");
3840
3841 Lex();
3842
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003843 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003844 TheCondState.Ignore = !TheCondState.CondMet;
3845 }
3846
3847 return false;
3848}
3849
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003850/// parseDirectiveIfeqs
3851/// ::= .ifeqs string1, string2
3852bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc) {
3853 if (Lexer.isNot(AsmToken::String)) {
3854 TokError("expected string parameter for '.ifeqs' directive");
3855 eatToEndOfStatement();
3856 return true;
3857 }
3858
3859 StringRef String1 = getTok().getStringContents();
3860 Lex();
3861
3862 if (Lexer.isNot(AsmToken::Comma)) {
3863 TokError("expected comma after first string for '.ifeqs' directive");
3864 eatToEndOfStatement();
3865 return true;
3866 }
3867
3868 Lex();
3869
3870 if (Lexer.isNot(AsmToken::String)) {
3871 TokError("expected string parameter for '.ifeqs' directive");
3872 eatToEndOfStatement();
3873 return true;
3874 }
3875
3876 StringRef String2 = getTok().getStringContents();
3877 Lex();
3878
3879 TheCondStack.push_back(TheCondState);
3880 TheCondState.TheCond = AsmCond::IfCond;
3881 TheCondState.CondMet = String1 == String2;
3882 TheCondState.Ignore = !TheCondState.CondMet;
3883
3884 return false;
3885}
3886
Jim Grosbach4b905842013-09-20 23:08:21 +00003887/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003888/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003889bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003890 StringRef Name;
3891 TheCondStack.push_back(TheCondState);
3892 TheCondState.TheCond = AsmCond::IfCond;
3893
3894 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003895 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003896 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003897 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003898 return TokError("expected identifier after '.ifdef'");
3899
3900 Lex();
3901
3902 MCSymbol *Sym = getContext().LookupSymbol(Name);
3903
3904 if (expect_defined)
3905 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
3906 else
3907 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
3908 TheCondState.Ignore = !TheCondState.CondMet;
3909 }
3910
3911 return false;
3912}
3913
Jim Grosbach4b905842013-09-20 23:08:21 +00003914/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003915/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003916bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003917 if (TheCondState.TheCond != AsmCond::IfCond &&
3918 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003919 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3920 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003921 TheCondState.TheCond = AsmCond::ElseIfCond;
3922
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003923 bool LastIgnoreState = false;
3924 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00003925 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003926 if (LastIgnoreState || TheCondState.CondMet) {
3927 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003928 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00003929 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003930 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003931 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003932 return true;
3933
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003934 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003935 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003936
Sean Callanan686ed8d2010-01-19 20:22:31 +00003937 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003938 TheCondState.CondMet = ExprValue;
3939 TheCondState.Ignore = !TheCondState.CondMet;
3940 }
3941
3942 return false;
3943}
3944
Jim Grosbach4b905842013-09-20 23:08:21 +00003945/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003946/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00003947bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003948 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003949 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003950
Sean Callanan686ed8d2010-01-19 20:22:31 +00003951 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003952
3953 if (TheCondState.TheCond != AsmCond::IfCond &&
3954 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003955 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
3956 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003957 TheCondState.TheCond = AsmCond::ElseCond;
3958 bool LastIgnoreState = false;
3959 if (!TheCondStack.empty())
3960 LastIgnoreState = TheCondStack.back().Ignore;
3961 if (LastIgnoreState || TheCondState.CondMet)
3962 TheCondState.Ignore = true;
3963 else
3964 TheCondState.Ignore = false;
3965
3966 return false;
3967}
3968
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003969/// parseDirectiveEnd
3970/// ::= .end
3971bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
3972 if (getLexer().isNot(AsmToken::EndOfStatement))
3973 return TokError("unexpected token in '.end' directive");
3974
3975 Lex();
3976
3977 while (Lexer.isNot(AsmToken::Eof))
3978 Lex();
3979
3980 return false;
3981}
3982
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00003983/// parseDirectiveError
3984/// ::= .err
3985/// ::= .error [string]
3986bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
3987 if (!TheCondStack.empty()) {
3988 if (TheCondStack.back().Ignore) {
3989 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00003990 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00003991 }
3992 }
3993
3994 if (!WithMessage)
3995 return Error(L, ".err encountered");
3996
3997 StringRef Message = ".error directive invoked in source file";
3998 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3999 if (Lexer.isNot(AsmToken::String)) {
4000 TokError(".error argument must be a string");
4001 eatToEndOfStatement();
4002 return true;
4003 }
4004
4005 Message = getTok().getStringContents();
4006 Lex();
4007 }
4008
4009 Error(L, Message);
4010 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004011}
4012
Jim Grosbach4b905842013-09-20 23:08:21 +00004013/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004014/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004015bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004016 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004017 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004018
Sean Callanan686ed8d2010-01-19 20:22:31 +00004019 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004020
Jim Grosbach4b905842013-09-20 23:08:21 +00004021 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004022 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4023 ".else");
4024 if (!TheCondStack.empty()) {
4025 TheCondState = TheCondStack.back();
4026 TheCondStack.pop_back();
4027 }
4028
4029 return false;
4030}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004031
Eli Bendersky17233942013-01-15 22:59:42 +00004032void AsmParser::initializeDirectiveKindMap() {
4033 DirectiveKindMap[".set"] = DK_SET;
4034 DirectiveKindMap[".equ"] = DK_EQU;
4035 DirectiveKindMap[".equiv"] = DK_EQUIV;
4036 DirectiveKindMap[".ascii"] = DK_ASCII;
4037 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4038 DirectiveKindMap[".string"] = DK_STRING;
4039 DirectiveKindMap[".byte"] = DK_BYTE;
4040 DirectiveKindMap[".short"] = DK_SHORT;
4041 DirectiveKindMap[".value"] = DK_VALUE;
4042 DirectiveKindMap[".2byte"] = DK_2BYTE;
4043 DirectiveKindMap[".long"] = DK_LONG;
4044 DirectiveKindMap[".int"] = DK_INT;
4045 DirectiveKindMap[".4byte"] = DK_4BYTE;
4046 DirectiveKindMap[".quad"] = DK_QUAD;
4047 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004048 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004049 DirectiveKindMap[".single"] = DK_SINGLE;
4050 DirectiveKindMap[".float"] = DK_FLOAT;
4051 DirectiveKindMap[".double"] = DK_DOUBLE;
4052 DirectiveKindMap[".align"] = DK_ALIGN;
4053 DirectiveKindMap[".align32"] = DK_ALIGN32;
4054 DirectiveKindMap[".balign"] = DK_BALIGN;
4055 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4056 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4057 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4058 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4059 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4060 DirectiveKindMap[".org"] = DK_ORG;
4061 DirectiveKindMap[".fill"] = DK_FILL;
4062 DirectiveKindMap[".zero"] = DK_ZERO;
4063 DirectiveKindMap[".extern"] = DK_EXTERN;
4064 DirectiveKindMap[".globl"] = DK_GLOBL;
4065 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004066 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4067 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4068 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4069 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4070 DirectiveKindMap[".reference"] = DK_REFERENCE;
4071 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4072 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4073 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4074 DirectiveKindMap[".comm"] = DK_COMM;
4075 DirectiveKindMap[".common"] = DK_COMMON;
4076 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4077 DirectiveKindMap[".abort"] = DK_ABORT;
4078 DirectiveKindMap[".include"] = DK_INCLUDE;
4079 DirectiveKindMap[".incbin"] = DK_INCBIN;
4080 DirectiveKindMap[".code16"] = DK_CODE16;
4081 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4082 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004083 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004084 DirectiveKindMap[".irp"] = DK_IRP;
4085 DirectiveKindMap[".irpc"] = DK_IRPC;
4086 DirectiveKindMap[".endr"] = DK_ENDR;
4087 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4088 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4089 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4090 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004091 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004092 DirectiveKindMap[".ifb"] = DK_IFB;
4093 DirectiveKindMap[".ifnb"] = DK_IFNB;
4094 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004095 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004096 DirectiveKindMap[".ifnc"] = DK_IFNC;
4097 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4098 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4099 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4100 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4101 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004102 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004103 DirectiveKindMap[".endif"] = DK_ENDIF;
4104 DirectiveKindMap[".skip"] = DK_SKIP;
4105 DirectiveKindMap[".space"] = DK_SPACE;
4106 DirectiveKindMap[".file"] = DK_FILE;
4107 DirectiveKindMap[".line"] = DK_LINE;
4108 DirectiveKindMap[".loc"] = DK_LOC;
4109 DirectiveKindMap[".stabs"] = DK_STABS;
4110 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4111 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4112 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4113 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4114 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4115 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4116 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4117 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4118 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4119 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4120 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4121 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4122 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4123 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4124 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4125 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4126 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4127 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4128 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4129 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4130 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004131 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004132 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4133 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4134 DirectiveKindMap[".macro"] = DK_MACRO;
4135 DirectiveKindMap[".endm"] = DK_ENDM;
4136 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4137 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004138 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004139 DirectiveKindMap[".error"] = DK_ERROR;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004140}
4141
Jim Grosbach4b905842013-09-20 23:08:21 +00004142MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004143 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004144
Rafael Espindola34b9c512012-06-03 23:57:14 +00004145 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004146 for (;;) {
4147 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004148 if (getLexer().is(AsmToken::Eof)) {
4149 Error(DirectiveLoc, "no matching '.endr' in definition");
4150 return 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004151 }
4152
Rafael Espindola34b9c512012-06-03 23:57:14 +00004153 if (Lexer.is(AsmToken::Identifier) &&
4154 (getTok().getIdentifier() == ".rept")) {
4155 ++NestLevel;
4156 }
4157
4158 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004159 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004160 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004161 EndToken = getTok();
4162 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004163 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4164 TokError("unexpected token in '.endr' directive");
4165 return 0;
4166 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004167 break;
4168 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004169 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004170 }
4171
Rafael Espindola34b9c512012-06-03 23:57:14 +00004172 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004173 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004174 }
4175
4176 const char *BodyStart = StartToken.getLoc().getPointer();
4177 const char *BodyEnd = EndToken.getLoc().getPointer();
4178 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4179
Rafael Espindola34b9c512012-06-03 23:57:14 +00004180 // We Are Anonymous.
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004181 MacroLikeBodies.push_back(MCAsmMacro(StringRef(), Body, None));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004182 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004183}
4184
Jim Grosbach4b905842013-09-20 23:08:21 +00004185void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004186 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004187 OS << ".endr\n";
4188
4189 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00004190 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004191
Rafael Espindola34b9c512012-06-03 23:57:14 +00004192 // Create the macro instantiation object and add to the current macro
4193 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00004194 MacroInstantiation *MI = new MacroInstantiation(
4195 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004196 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004197
Rafael Espindola34b9c512012-06-03 23:57:14 +00004198 // Jump to the macro instantiation and prime the lexer.
4199 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
4200 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
4201 Lex();
4202}
4203
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004204/// parseDirectiveRept
4205/// ::= .rep | .rept count
4206bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004207 const MCExpr *CountExpr;
4208 SMLoc CountLoc = getTok().getLoc();
4209 if (parseExpression(CountExpr))
4210 return true;
4211
Rafael Espindola34b9c512012-06-03 23:57:14 +00004212 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004213 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4214 eatToEndOfStatement();
4215 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4216 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004217
4218 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004219 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004220
4221 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004222 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004223
4224 // Eat the end of statement.
4225 Lex();
4226
4227 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004228 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004229 if (!M)
4230 return true;
4231
4232 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4233 // to hold the macro body with substitutions.
4234 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004235 raw_svector_ostream OS(Buf);
4236 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004237 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004238 return true;
4239 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004240 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004241
4242 return false;
4243}
4244
Jim Grosbach4b905842013-09-20 23:08:21 +00004245/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004246/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004247bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004248 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004249
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004250 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004251 return TokError("expected identifier in '.irp' directive");
4252
Rafael Espindola768b41c2012-06-15 14:02:34 +00004253 if (Lexer.isNot(AsmToken::Comma))
4254 return TokError("expected comma in '.irp' directive");
4255
4256 Lex();
4257
Eli Bendersky38274122013-01-14 23:22:36 +00004258 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004259 if (parseMacroArguments(0, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004260 return true;
4261
4262 // Eat the end of statement.
4263 Lex();
4264
4265 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004266 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004267 if (!M)
4268 return true;
4269
4270 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4271 // to hold the macro body with substitutions.
4272 SmallString<256> Buf;
4273 raw_svector_ostream OS(Buf);
4274
Eli Bendersky38274122013-01-14 23:22:36 +00004275 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004276 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004277 return true;
4278 }
4279
Jim Grosbach4b905842013-09-20 23:08:21 +00004280 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004281
4282 return false;
4283}
4284
Jim Grosbach4b905842013-09-20 23:08:21 +00004285/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004286/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004287bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004288 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004289
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004290 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004291 return TokError("expected identifier in '.irpc' directive");
4292
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004293 if (Lexer.isNot(AsmToken::Comma))
4294 return TokError("expected comma in '.irpc' directive");
4295
4296 Lex();
4297
Eli Bendersky38274122013-01-14 23:22:36 +00004298 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004299 if (parseMacroArguments(0, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004300 return true;
4301
4302 if (A.size() != 1 || A.front().size() != 1)
4303 return TokError("unexpected token in '.irpc' directive");
4304
4305 // Eat the end of statement.
4306 Lex();
4307
4308 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004309 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004310 if (!M)
4311 return true;
4312
4313 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4314 // to hold the macro body with substitutions.
4315 SmallString<256> Buf;
4316 raw_svector_ostream OS(Buf);
4317
4318 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004319 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004320 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004321 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004322
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004323 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004324 return true;
4325 }
4326
Jim Grosbach4b905842013-09-20 23:08:21 +00004327 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004328
4329 return false;
4330}
4331
Jim Grosbach4b905842013-09-20 23:08:21 +00004332bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004333 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004334 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004335
4336 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004337 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004338 assert(getLexer().is(AsmToken::EndOfStatement));
4339
Jim Grosbach4b905842013-09-20 23:08:21 +00004340 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004341 return false;
4342}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004343
Jim Grosbach4b905842013-09-20 23:08:21 +00004344bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004345 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004346 const MCExpr *Value;
4347 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004348 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004349 return true;
4350 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4351 if (!MCE)
4352 return Error(ExprLoc, "unexpected expression in _emit");
4353 uint64_t IntValue = MCE->getValue();
4354 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4355 return Error(ExprLoc, "literal value out of range for directive");
4356
Chad Rosierc7f552c2013-02-12 21:33:51 +00004357 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4358 return false;
4359}
4360
Jim Grosbach4b905842013-09-20 23:08:21 +00004361bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004362 const MCExpr *Value;
4363 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004364 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004365 return true;
4366 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4367 if (!MCE)
4368 return Error(ExprLoc, "unexpected expression in align");
4369 uint64_t IntValue = MCE->getValue();
4370 if (!isPowerOf2_64(IntValue))
4371 return Error(ExprLoc, "literal value not a power of two greater then zero");
4372
Jim Grosbach4b905842013-09-20 23:08:21 +00004373 Info.AsmRewrites->push_back(
4374 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004375 return false;
4376}
4377
Chad Rosierf43fcf52013-02-13 21:27:17 +00004378// We are comparing pointers, but the pointers are relative to a single string.
4379// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004380static int rewritesSort(const AsmRewrite *AsmRewriteA,
4381 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004382 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4383 return -1;
4384 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4385 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004386
Chad Rosierfce4fab2013-04-08 17:43:47 +00004387 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4388 // rewrite to the same location. Make sure the SizeDirective rewrite is
4389 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4390 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004391 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4392 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004393 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004394
Jim Grosbach4b905842013-09-20 23:08:21 +00004395 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4396 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004397 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004398 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004399}
4400
Jim Grosbach4b905842013-09-20 23:08:21 +00004401bool AsmParser::parseMSInlineAsm(
4402 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4403 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4404 SmallVectorImpl<std::string> &Constraints,
4405 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4406 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004407 SmallVector<void *, 4> InputDecls;
4408 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004409 SmallVector<bool, 4> InputDeclsAddressOf;
4410 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004411 SmallVector<std::string, 4> InputConstraints;
4412 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004413 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004414
Benjamin Kramer1a136112013-02-15 20:37:21 +00004415 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004416
4417 // Prime the lexer.
4418 Lex();
4419
4420 // While we have input, parse each statement.
4421 unsigned InputIdx = 0;
4422 unsigned OutputIdx = 0;
4423 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004424 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004425 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004426 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004427
Chad Rosier149e8e02012-12-12 22:45:52 +00004428 if (Info.ParseError)
4429 return true;
4430
Benjamin Kramer1a136112013-02-15 20:37:21 +00004431 if (Info.Opcode == ~0U)
4432 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004433
Benjamin Kramer1a136112013-02-15 20:37:21 +00004434 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004435
Benjamin Kramer1a136112013-02-15 20:37:21 +00004436 // Build the list of clobbers, outputs and inputs.
4437 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
4438 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004439
Benjamin Kramer1a136112013-02-15 20:37:21 +00004440 // Immediate.
Chad Rosierf3c04f62013-03-19 21:58:18 +00004441 if (Operand->isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004442 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004443
Benjamin Kramer1a136112013-02-15 20:37:21 +00004444 // Register operand.
4445 if (Operand->isReg() && !Operand->needAddressOf()) {
4446 unsigned NumDefs = Desc.getNumDefs();
4447 // Clobber.
4448 if (NumDefs && Operand->getMCOperandNum() < NumDefs)
4449 ClobberRegs.push_back(Operand->getReg());
4450 continue;
4451 }
4452
4453 // Expr/Input or Output.
Chad Rosiere81309b2013-04-09 17:53:49 +00004454 StringRef SymName = Operand->getSymName();
4455 if (SymName.empty())
4456 continue;
4457
Chad Rosierdba3fe52013-04-22 22:12:12 +00004458 void *OpDecl = Operand->getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004459 if (!OpDecl)
4460 continue;
4461
4462 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004463 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004464 if (isOutput) {
4465 ++InputIdx;
4466 OutputDecls.push_back(OpDecl);
4467 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
4468 OutputConstraints.push_back('=' + Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004469 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004470 } else {
4471 InputDecls.push_back(OpDecl);
4472 InputDeclsAddressOf.push_back(Operand->needAddressOf());
4473 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004474 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004475 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004476 }
Reid Kleckneree088972013-12-10 18:27:32 +00004477
4478 // Consider implicit defs to be clobbers. Think of cpuid and push.
4479 const uint16_t *ImpDefs = Desc.getImplicitDefs();
4480 for (unsigned I = 0, E = Desc.getNumImplicitDefs(); I != E; ++I)
4481 ClobberRegs.push_back(ImpDefs[I]);
Chad Rosier8bce6642012-10-18 15:49:34 +00004482 }
4483
4484 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004485 NumOutputs = OutputDecls.size();
4486 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004487
4488 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004489 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4490 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4491 ClobberRegs.end());
4492 Clobbers.assign(ClobberRegs.size(), std::string());
4493 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4494 raw_string_ostream OS(Clobbers[I]);
4495 IP->printRegName(OS, ClobberRegs[I]);
4496 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004497
4498 // Merge the various outputs and inputs. Output are expected first.
4499 if (NumOutputs || NumInputs) {
4500 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004501 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004502 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004503 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004504 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004505 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004506 }
4507 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004508 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004509 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004510 }
4511 }
4512
4513 // Build the IR assembly string.
4514 std::string AsmStringIR;
4515 raw_string_ostream OS(AsmStringIR);
Chad Rosier17d37992013-03-19 21:12:14 +00004516 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4517 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
Jim Grosbach4b905842013-09-20 23:08:21 +00004518 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
Benjamin Kramer1a136112013-02-15 20:37:21 +00004519 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(),
4520 E = AsmStrRewrites.end();
4521 I != E; ++I) {
Chad Rosierff10ed12013-04-12 16:26:42 +00004522 AsmRewriteKind Kind = (*I).Kind;
4523 if (Kind == AOK_Delete)
4524 continue;
4525
Chad Rosier8bce6642012-10-18 15:49:34 +00004526 const char *Loc = (*I).Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004527 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004528
Chad Rosier120eefd2013-03-19 17:32:17 +00004529 // Emit everything up to the immediate/expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004530 unsigned Len = Loc - AsmStart;
Chad Rosier8fb83302013-04-11 21:49:30 +00004531 if (Len)
Chad Rosier17d37992013-03-19 21:12:14 +00004532 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004533
Chad Rosier37e755c2012-10-23 17:43:43 +00004534 // Skip the original expression.
4535 if (Kind == AOK_Skip) {
Chad Rosier17d37992013-03-19 21:12:14 +00004536 AsmStart = Loc + (*I).Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004537 continue;
4538 }
4539
Chad Rosierff10ed12013-04-12 16:26:42 +00004540 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004541 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004542 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004543 default:
4544 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004545 case AOK_Imm:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004546 OS << "$$" << (*I).Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004547 break;
4548 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004549 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004550 break;
4551 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004552 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004553 break;
4554 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004555 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004556 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004557 case AOK_SizeDirective:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004558 switch ((*I).Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004559 default: break;
4560 case 8: OS << "byte ptr "; break;
4561 case 16: OS << "word ptr "; break;
4562 case 32: OS << "dword ptr "; break;
4563 case 64: OS << "qword ptr "; break;
4564 case 80: OS << "xword ptr "; break;
4565 case 128: OS << "xmmword ptr "; break;
4566 case 256: OS << "ymmword ptr "; break;
4567 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004568 break;
4569 case AOK_Emit:
4570 OS << ".byte";
4571 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004572 case AOK_Align: {
4573 unsigned Val = (*I).Val;
4574 OS << ".align " << Val;
4575
4576 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004577 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004578 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4579 break;
4580 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004581 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004582 // Insert the dot if the user omitted it.
4583 OS.flush();
4584 if (AsmStringIR.back() != '.')
4585 OS << '.';
Chad Rosierf0e87202012-10-25 20:41:34 +00004586 OS << (*I).Val;
4587 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004588 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004589
Chad Rosier8bce6642012-10-18 15:49:34 +00004590 // Skip the original expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004591 AsmStart = Loc + (*I).Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004592 }
4593
4594 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004595 if (AsmStart != AsmEnd)
4596 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004597
4598 AsmString = OS.str();
4599 return false;
4600}
4601
Daniel Dunbar01e36072010-07-17 02:26:10 +00004602/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004603MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4604 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004605 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004606}