blob: 1fb8480b057130e750a5f77ff654845a9d2c214c [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"
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Chad Rosiereb5c1682013-02-13 18:38:58 +000016#include "llvm/ADT/STLExtras.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"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000025#include "llvm/MC/MCParser/AsmCond.h"
26#include "llvm/MC/MCParser/AsmLexer.h"
27#include "llvm/MC/MCParser/MCAsmParser.h"
28#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Cheng76792992011-07-20 05:58:47 +000029#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000030#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000031#include "llvm/MC/MCStreamer.h"
Daniel Dunbarae7ac012009-06-29 23:43:14 +000032#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000033#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000034#include "llvm/Support/CommandLine.h"
Benjamin Kramer4efe5062012-01-28 15:28:41 +000035#include "llvm/Support/ErrorHandling.h"
Jim Grosbach76346c32011-06-29 16:05:14 +000036#include "llvm/Support/MathExtras.h"
Kevin Enderbye233dda2010-06-28 21:45:58 +000037#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000038#include "llvm/Support/SourceMgr.h"
Chris Lattner36e02122009-06-21 20:54:55 +000039#include "llvm/Support/raw_ostream.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000040#include <cctype>
Chad Rosier8bce6642012-10-18 15:49:34 +000041#include <set>
42#include <string>
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000043#include <vector>
Chris Lattnerb0133452009-06-21 20:16:42 +000044using namespace llvm;
45
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000046static cl::opt<bool>
47FatalAssemblerWarnings("fatal-assembler-warnings",
48 cl::desc("Consider warnings as error"));
49
Eric Christophera7c32732012-12-18 00:30:54 +000050MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewyckyac612272012-10-19 07:00:09 +000051
Daniel Dunbar86033402010-07-12 17:54:38 +000052namespace {
53
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;
57typedef std::pair<StringRef, MCAsmMacroArgument> MCAsmMacroParameter;
58typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
59
60struct MCAsmMacro {
61 StringRef Name;
62 StringRef Body;
63 MCAsmMacroParameters Parameters;
64
65public:
66 MCAsmMacro(StringRef N, StringRef B, const MCAsmMacroParameters &P) :
67 Name(N), Body(B), Parameters(P) {}
68
69 MCAsmMacro(const MCAsmMacro& Other)
70 : Name(Other.Name), Body(Other.Body), Parameters(Other.Parameters) {}
71};
72
Daniel Dunbar43235712010-07-18 18:54:11 +000073/// \brief Helper class for storing information about an active macro
74/// instantiation.
75struct MacroInstantiation {
76 /// The macro being instantiated.
Eli Bendersky38274122013-01-14 23:22:36 +000077 const MCAsmMacro *TheMacro;
Daniel Dunbar43235712010-07-18 18:54:11 +000078
79 /// The macro instantiation with substitutions.
80 MemoryBuffer *Instantiation;
81
82 /// The location of the instantiation.
83 SMLoc InstantiationLoc;
84
Daniel Dunbar40f1d852012-12-01 01:38:48 +000085 /// The buffer where parsing should resume upon instantiation completion.
86 int ExitBuffer;
87
Daniel Dunbar43235712010-07-18 18:54:11 +000088 /// The location where parsing should resume upon instantiation completion.
89 SMLoc ExitLoc;
90
91public:
Eli Bendersky38274122013-01-14 23:22:36 +000092 MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB, SMLoc EL,
Rafael Espindola1134ab232011-06-05 02:43:45 +000093 MemoryBuffer *I);
Daniel Dunbar43235712010-07-18 18:54:11 +000094};
95
Eli Friedman0f4871d2012-10-22 23:58:19 +000096struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +000097 /// \brief The parsed operands from the last parsed statement.
Eli Friedman0f4871d2012-10-22 23:58:19 +000098 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
99
Jim Grosbach4b905842013-09-20 23:08:21 +0000100 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000101 unsigned Opcode;
102
Jim Grosbach4b905842013-09-20 23:08:21 +0000103 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000104 bool ParseError;
105
Eli Friedman0f4871d2012-10-22 23:58:19 +0000106 SmallVectorImpl<AsmRewrite> *AsmRewrites;
107
Chad Rosier149e8e02012-12-12 22:45:52 +0000108 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(0) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000109 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000110 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000111
112 ~ParseStatementInfo() {
113 // Free any parsed operands.
114 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
115 delete ParsedOperands[i];
116 ParsedOperands.clear();
117 }
118};
119
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000120/// \brief The concrete assembly parser instance.
121class AsmParser : public MCAsmParser {
Craig Topper2e6644c2012-09-15 16:23:52 +0000122 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
123 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000124private:
125 AsmLexer Lexer;
126 MCContext &Ctx;
127 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000128 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000129 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000130 SourceMgr::DiagHandlerTy SavedDiagHandler;
131 void *SavedDiagContext;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000132 MCAsmParserExtension *PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000133
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000134 /// This is the current buffer index we're lexing from as managed by the
135 /// SourceMgr object.
136 int CurBuffer;
137
138 AsmCond TheCondState;
139 std::vector<AsmCond> TheCondStack;
140
Jim Grosbach4b905842013-09-20 23:08:21 +0000141 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000142 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000143 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000144 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000145
Jim Grosbach4b905842013-09-20 23:08:21 +0000146 /// \brief Map of currently defined macros.
Eli Bendersky38274122013-01-14 23:22:36 +0000147 StringMap<MCAsmMacro*> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000148
Jim Grosbach4b905842013-09-20 23:08:21 +0000149 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000150 std::vector<MacroInstantiation*> ActiveMacros;
151
Jim Grosbach4b905842013-09-20 23:08:21 +0000152 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000153 std::deque<MCAsmMacro> MacroLikeBodies;
154
Daniel Dunbar828984f2010-07-18 18:38:02 +0000155 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000156 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000157
Daniel Dunbar43325c42010-09-09 22:42:56 +0000158 /// Flag tracking whether any errors have been encountered.
159 unsigned HadError : 1;
160
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000161 /// The values from the last parsed cpp hash file line comment if any.
162 StringRef CppHashFilename;
163 int64_t CppHashLineNumber;
164 SMLoc CppHashLoc;
Kevin Enderby27121c12012-11-05 21:55:41 +0000165 int CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000166 /// When generating dwarf for assembly source files we need to calculate the
167 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000168 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000169 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
170 SMLoc LastQueryIDLoc;
171 int LastQueryBuffer;
172 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000173
Devang Patela173ee52012-01-31 18:14:05 +0000174 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
175 unsigned AssemblerDialect;
176
Jim Grosbach4b905842013-09-20 23:08:21 +0000177 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000178 bool IsDarwin;
179
Jim Grosbach4b905842013-09-20 23:08:21 +0000180 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000181 bool ParsingInlineAsm;
182
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000183public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000184 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000185 const MCAsmInfo &MAI);
Craig Topper5f96ca52012-08-29 05:48:09 +0000186 virtual ~AsmParser();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000187
188 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
189
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000190 virtual void addDirectiveHandler(StringRef Directive,
Eli Bendersky29b9f472013-01-16 00:50:52 +0000191 ExtensionDirectiveHandler Handler) {
192 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000193 }
194
195public:
196 /// @name MCAsmParser Interface
197 /// {
198
199 virtual SourceMgr &getSourceManager() { return SrcMgr; }
200 virtual MCAsmLexer &getLexer() { return Lexer; }
201 virtual MCContext &getContext() { return Ctx; }
202 virtual MCStreamer &getStreamer() { return Out; }
Eric Christophera7c32732012-12-18 00:30:54 +0000203 virtual unsigned getAssemblerDialect() {
Devang Patela173ee52012-01-31 18:14:05 +0000204 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000205 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000206 else
207 return AssemblerDialect;
208 }
209 virtual void setAssemblerDialect(unsigned i) {
210 AssemblerDialect = i;
211 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000212
Chris Lattnera3a06812011-10-16 04:47:35 +0000213 virtual bool Warning(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000214 ArrayRef<SMRange> Ranges = None);
Chris Lattnera3a06812011-10-16 04:47:35 +0000215 virtual bool Error(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000216 ArrayRef<SMRange> Ranges = None);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000217
Craig Topper5f96ca52012-08-29 05:48:09 +0000218 virtual const AsmToken &Lex();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000219
Chad Rosier49963552012-10-13 00:26:04 +0000220 void setParsingInlineAsm(bool V) { ParsingInlineAsm = V; }
Chad Rosiere4ad2a02012-10-16 20:16:20 +0000221 bool isParsingInlineAsm() { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000222
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000223 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000224 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000225 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000226 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000227 SmallVectorImpl<std::string> &Clobbers,
228 const MCInstrInfo *MII,
229 const MCInstPrinter *IP,
230 MCAsmParserSemaCallback &SI);
Chad Rosier49963552012-10-13 00:26:04 +0000231
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000232 bool parseExpression(const MCExpr *&Res);
233 virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc);
Chad Rosier1863f4f2013-04-10 17:35:30 +0000234 virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000235 virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
236 virtual bool parseAbsoluteExpression(int64_t &Res);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000237
Jim Grosbach4b905842013-09-20 23:08:21 +0000238 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000239 /// and set \p Res to the identifier contents.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000240 virtual bool parseIdentifier(StringRef &Res);
241 virtual void eatToEndOfStatement();
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000242
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000243 virtual void checkForValidSection();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000244 /// }
245
246private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000247
Jim Grosbach4b905842013-09-20 23:08:21 +0000248 bool parseStatement(ParseStatementInfo &Info);
249 void eatToEndOfLine();
250 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000251
Jim Grosbach4b905842013-09-20 23:08:21 +0000252 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Kevin Enderby81c944c2013-01-22 21:44:53 +0000253 MCAsmMacroParameters Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000254 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Eli Bendersky38274122013-01-14 23:22:36 +0000255 const MCAsmMacroParameters &Parameters,
256 const MCAsmMacroArguments &A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000257 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000258
Eli Benderskya313ae62013-01-16 18:56:50 +0000259 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000260 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000261
262 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000263 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000264
265 /// \brief Lookup a previously defined macro.
266 /// \param Name Macro name.
267 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000268 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000269
270 /// \brief Define a new macro with the given name and information.
Jim Grosbach4b905842013-09-20 23:08:21 +0000271 void defineMacro(StringRef Name, const MCAsmMacro& Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000272
273 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000274 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000275
276 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000277 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000278
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000279 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000280 ///
281 /// \param M The macro.
282 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000283 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000284
285 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000286 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000287
288 /// \brief Extract AsmTokens for a macro argument. If the argument delimiter
289 /// is initially unknown, set it to AsmToken::Eof. It will be set to the
290 /// correct delimiter by the method.
Jim Grosbach4b905842013-09-20 23:08:21 +0000291 bool parseMacroArgument(MCAsmMacroArgument &MA,
Eli Benderskya313ae62013-01-16 18:56:50 +0000292 AsmToken::TokenKind &ArgumentDelimiter);
293
294 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000295 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000296
Jim Grosbach4b905842013-09-20 23:08:21 +0000297 void printMacroInstantiations();
298 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000299 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000300 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000301 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000302 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000303
Jim Grosbach4b905842013-09-20 23:08:21 +0000304 /// \brief Enter the specified file. This returns true on failure.
305 bool enterIncludeFile(const std::string &Filename);
306
307 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000308 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000309 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000310
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000311 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000312 /// current token is not set; clients should ensure Lex() is called
313 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000314 ///
315 /// \param InBuffer If not -1, should be the known buffer id that contains the
316 /// location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000317 void jumpToLoc(SMLoc Loc, int InBuffer=-1);
Daniel Dunbar43235712010-07-18 18:54:11 +0000318
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000319 /// \brief Parse up to the end of statement and a return the contents from the
320 /// current token until the end of the statement; the current token on exit
321 /// will be either the EndOfStatement or EOF.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000322 virtual StringRef parseStringToEndOfStatement();
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000323
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000324 /// \brief Parse until the end of a statement or a comma is encountered,
325 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000326 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000327
Jim Grosbach4b905842013-09-20 23:08:21 +0000328 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000329 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000330
Jim Grosbach4b905842013-09-20 23:08:21 +0000331 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
332 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
333 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000334
Jim Grosbach4b905842013-09-20 23:08:21 +0000335 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000336
Eli Bendersky17233942013-01-15 22:59:42 +0000337 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000338 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000339 DK_NO_DIRECTIVE, // Placeholder
340 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
341 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_SINGLE,
342 DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000343 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000344 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000345 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000346 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
347 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
348 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
349 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
350 DK_IF, DK_IFB, DK_IFNB, DK_IFC, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
Eli Bendersky17233942013-01-15 22:59:42 +0000351 DK_ELSEIF, DK_ELSE, DK_ENDIF,
352 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
353 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
354 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
355 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
356 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
357 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000358 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Eli Bendersky17233942013-01-15 22:59:42 +0000359 DK_MACROS_ON, DK_MACROS_OFF, DK_MACRO, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
360 DK_SLEB128, DK_ULEB128
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000361 };
362
Jim Grosbach4b905842013-09-20 23:08:21 +0000363 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000364 /// directives parsed by this class.
365 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000366
367 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000368 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
369 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
370 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
371 bool parseDirectiveFill(); // ".fill"
372 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000373 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000374 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
375 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000376 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000377 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000378
Eli Bendersky17233942013-01-15 22:59:42 +0000379 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000380 bool parseDirectiveFile(SMLoc DirectiveLoc);
381 bool parseDirectiveLine();
382 bool parseDirectiveLoc();
383 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000384
385 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000386 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000387 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000388 bool parseDirectiveCFISections();
389 bool parseDirectiveCFIStartProc();
390 bool parseDirectiveCFIEndProc();
391 bool parseDirectiveCFIDefCfaOffset();
392 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
393 bool parseDirectiveCFIAdjustCfaOffset();
394 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
395 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
396 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
397 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
398 bool parseDirectiveCFIRememberState();
399 bool parseDirectiveCFIRestoreState();
400 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
401 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
402 bool parseDirectiveCFIEscape();
403 bool parseDirectiveCFISignalFrame();
404 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000405
406 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000407 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
408 bool parseDirectiveEndMacro(StringRef Directive);
409 bool parseDirectiveMacro(SMLoc DirectiveLoc);
410 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000411
Eli Benderskyf483ff92012-12-20 19:05:53 +0000412 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000413 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000414 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000415 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000416 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000417 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000418
Eli Bendersky17233942013-01-15 22:59:42 +0000419 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000420 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000421
422 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000423 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000424
Jim Grosbach4b905842013-09-20 23:08:21 +0000425 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000426 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000427 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000428
Jim Grosbach4b905842013-09-20 23:08:21 +0000429 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000430
Jim Grosbach4b905842013-09-20 23:08:21 +0000431 bool parseDirectiveAbort(); // ".abort"
432 bool parseDirectiveInclude(); // ".include"
433 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000434
Jim Grosbach4b905842013-09-20 23:08:21 +0000435 bool parseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000436 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000437 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000438 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000439 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000440 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000441 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
442 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
443 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
444 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000445 virtual bool parseEscapedString(std::string &Data);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000446
Jim Grosbach4b905842013-09-20 23:08:21 +0000447 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000448 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000449
Rafael Espindola34b9c512012-06-03 23:57:14 +0000450 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000451 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
452 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000453 raw_svector_ostream &OS);
Jim Grosbach4b905842013-09-20 23:08:21 +0000454 bool parseDirectiveRept(SMLoc DirectiveLoc); // ".rept"
455 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
456 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
457 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000458
Chad Rosierc7f552c2013-02-12 21:33:51 +0000459 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000460 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000461 size_t Len);
462
463 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000464 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000465
Eli Bendersky17233942013-01-15 22:59:42 +0000466 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000467};
Daniel Dunbar86033402010-07-12 17:54:38 +0000468}
469
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000470namespace llvm {
471
472extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000473extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000474extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000475
476}
477
Chris Lattnerc35681b2010-01-19 19:46:13 +0000478enum { DEFAULT_ADDRSPACE = 0 };
479
Jim Grosbach4b905842013-09-20 23:08:21 +0000480AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
481 const MCAsmInfo &_MAI)
482 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
483 PlatformParser(0), CurBuffer(0), MacrosEnabledFlag(true),
484 CppHashLineNumber(0), AssemblerDialect(~0U), IsDarwin(false),
485 ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000486 // Save the old handler.
487 SavedDiagHandler = SrcMgr.getDiagHandler();
488 SavedDiagContext = SrcMgr.getDiagContext();
489 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000490 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000491 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar86033402010-07-12 17:54:38 +0000492
Daniel Dunbarc5011082010-07-12 18:12:02 +0000493 // Initialize the platform / file format parser.
494 //
495 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
496 // created.
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000497 if (_MAI.hasMicrosoftFastStdCallMangling()) {
498 PlatformParser = createCOFFAsmParser();
499 PlatformParser->Initialize(*this);
500 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000501 PlatformParser = createDarwinAsmParser();
Daniel Dunbarc5011082010-07-12 18:12:02 +0000502 PlatformParser->Initialize(*this);
Preston Gurd05500642012-09-19 20:36:12 +0000503 IsDarwin = true;
Daniel Dunbar80be44a2010-07-12 20:08:04 +0000504 } else {
Daniel Dunbarab058b82010-07-12 21:23:32 +0000505 PlatformParser = createELFAsmParser();
Daniel Dunbar80be44a2010-07-12 20:08:04 +0000506 PlatformParser->Initialize(*this);
Daniel Dunbarc5011082010-07-12 18:12:02 +0000507 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000508
Eli Bendersky17233942013-01-15 22:59:42 +0000509 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000510}
511
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000512AsmParser::~AsmParser() {
Daniel Dunbarb759a132010-07-29 01:51:55 +0000513 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
514
515 // Destroy any macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000516 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
517 ie = MacroMap.end();
518 it != ie; ++it)
Daniel Dunbarb759a132010-07-29 01:51:55 +0000519 delete it->getValue();
520
Daniel Dunbarc5011082010-07-12 18:12:02 +0000521 delete PlatformParser;
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000522}
523
Jim Grosbach4b905842013-09-20 23:08:21 +0000524void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000525 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000526 for (std::vector<MacroInstantiation *>::const_reverse_iterator
527 it = ActiveMacros.rbegin(),
528 ie = ActiveMacros.rend();
529 it != ie; ++it)
530 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000531 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000532}
533
Chris Lattnera3a06812011-10-16 04:47:35 +0000534bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000535 if (FatalAssemblerWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000536 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000537 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
538 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000539 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000540}
541
Chris Lattnera3a06812011-10-16 04:47:35 +0000542bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000543 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000544 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
545 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000546 return true;
547}
548
Jim Grosbach4b905842013-09-20 23:08:21 +0000549bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000550 std::string IncludedFile;
551 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000552 if (NewBuf == -1)
553 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000554
Sean Callanan7a77eae2010-01-21 00:19:58 +0000555 CurBuffer = NewBuf;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000556
Sean Callanan7a77eae2010-01-21 00:19:58 +0000557 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencer530ce852010-10-09 11:00:50 +0000558
Sean Callanan7a77eae2010-01-21 00:19:58 +0000559 return false;
560}
Daniel Dunbar43235712010-07-18 18:54:11 +0000561
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000562/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000563/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000564/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000565bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000566 std::string IncludedFile;
567 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
568 if (NewBuf == -1)
569 return true;
570
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000571 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000572 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000573 return false;
574}
575
Jim Grosbach4b905842013-09-20 23:08:21 +0000576void AsmParser::jumpToLoc(SMLoc Loc, int InBuffer) {
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000577 if (InBuffer != -1) {
578 CurBuffer = InBuffer;
579 } else {
580 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
581 }
Daniel Dunbar43235712010-07-18 18:54:11 +0000582 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
583}
584
Sean Callanan7a77eae2010-01-21 00:19:58 +0000585const AsmToken &AsmParser::Lex() {
586 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000587
Sean Callanan7a77eae2010-01-21 00:19:58 +0000588 if (tok->is(AsmToken::Eof)) {
589 // If this is the end of an included file, pop the parent file off the
590 // include stack.
591 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
592 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000593 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000594 tok = &Lexer.Lex();
595 }
596 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000597
Sean Callanan7a77eae2010-01-21 00:19:58 +0000598 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000599 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000600
Sean Callanan7a77eae2010-01-21 00:19:58 +0000601 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000602}
603
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000604bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000605 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000606 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000607 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000608
Chris Lattner36e02122009-06-21 20:54:55 +0000609 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000610 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000611
612 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000613 AsmCond StartingCondState = TheCondState;
614
Kevin Enderby6469fc22011-11-01 22:27:22 +0000615 // If we are generating dwarf for assembly source files save the initial text
616 // section and generate a .file directive.
617 if (getContext().getGenDwarfForAssembly()) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000618 getContext().setGenDwarfSection(getStreamer().getCurrentSection().first);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000619 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
620 getStreamer().EmitLabel(SectionStartSym);
621 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby6469fc22011-11-01 22:27:22 +0000622 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
Eric Christopher906da232012-12-18 00:31:01 +0000623 StringRef(),
624 getContext().getMainFileName());
Kevin Enderby6469fc22011-11-01 22:27:22 +0000625 }
626
Chris Lattner73f36112009-07-02 21:53:43 +0000627 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000628 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000629 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000630 if (!parseStatement(Info))
631 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000632
Daniel Dunbar43325c42010-09-09 22:42:56 +0000633 // We had an error, validate that one was emitted and recover by skipping to
634 // the next line.
635 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000636 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000637 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000638
639 if (TheCondState.TheCond != StartingCondState.TheCond ||
640 TheCondState.Ignore != StartingCondState.Ignore)
641 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000642
643 // Check to see there are no empty DwarfFile slots.
Manman Ren5ce24ff2013-03-12 20:17:00 +0000644 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +0000645 getContext().getMCDwarfFiles();
Kevin Enderbye5930f12010-07-28 20:55:35 +0000646 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000647 if (!MCDwarfFiles[i])
Kevin Enderbye5930f12010-07-28 20:55:35 +0000648 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000649 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000650
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000651 // Check to see that all assembler local symbols were actually defined.
652 // Targets that don't do subsections via symbols may not want this, though,
653 // so conservatively exclude them. Only do this if we're finalizing, though,
654 // as otherwise we won't necessarilly have seen everything yet.
655 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
656 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
657 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000658 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000659 i != e; ++i) {
660 MCSymbol *Sym = i->getValue();
661 // Variable symbols may not be marked as defined, so check those
662 // explicitly. If we know it's a variable, we have a definition for
663 // the purposes of this check.
664 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
665 // FIXME: We would really like to refer back to where the symbol was
666 // first referenced for a source location. We need to add something
667 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000668 printMessage(
669 getLexer().getLoc(), SourceMgr::DK_Error,
670 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000671 }
672 }
673
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000674 // Finalize the output stream if there are no errors and if the client wants
675 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000676 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000677 Out.Finish();
678
Chris Lattner73f36112009-07-02 21:53:43 +0000679 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000680}
681
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000682void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000683 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000684 TokError("expected section directive before assembly directive");
Eli Benderskycbb25142013-01-14 19:04:57 +0000685 Out.InitToTextSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000686 }
687}
688
Jim Grosbach4b905842013-09-20 23:08:21 +0000689/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000690void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000691 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000692 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000693
Chris Lattnere5074c42009-06-22 01:29:09 +0000694 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000695 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000696 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000697}
698
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000699StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000700 const char *Start = getTok().getLoc().getPointer();
701
Jim Grosbach4b905842013-09-20 23:08:21 +0000702 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000703 Lex();
704
705 const char *End = getTok().getLoc().getPointer();
706 return StringRef(Start, End - Start);
707}
Chris Lattner78db3622009-06-22 05:51:26 +0000708
Jim Grosbach4b905842013-09-20 23:08:21 +0000709StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000710 const char *Start = getTok().getLoc().getPointer();
711
712 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000713 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000714 Lex();
715
716 const char *End = getTok().getLoc().getPointer();
717 return StringRef(Start, End - Start);
718}
719
Jim Grosbach4b905842013-09-20 23:08:21 +0000720/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000721/// NOTE: This assumes the leading '(' has already been consumed.
722///
723/// parenexpr ::= expr)
724///
Jim Grosbach4b905842013-09-20 23:08:21 +0000725bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
726 if (parseExpression(Res))
727 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000728 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000729 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000730 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000731 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000732 return false;
733}
Chris Lattner78db3622009-06-22 05:51:26 +0000734
Jim Grosbach4b905842013-09-20 23:08:21 +0000735/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000736/// NOTE: This assumes the leading '[' has already been consumed.
737///
738/// bracketexpr ::= expr]
739///
Jim Grosbach4b905842013-09-20 23:08:21 +0000740bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
741 if (parseExpression(Res))
742 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000743 if (Lexer.isNot(AsmToken::RBrac))
744 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000745 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000746 Lex();
747 return false;
748}
749
Jim Grosbach4b905842013-09-20 23:08:21 +0000750/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000751/// primaryexpr ::= (parenexpr
752/// primaryexpr ::= symbol
753/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000754/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000755/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000756bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000757 SMLoc FirstTokenLoc = getLexer().getLoc();
758 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
759 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000760 default:
761 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000762 // If we have an error assume that we've already handled it.
763 case AsmToken::Error:
764 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000765 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000766 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000767 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000768 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000769 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000770 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000771 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000772 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000773 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000774 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000775 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000776 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000777 if (FirstTokenKind == AsmToken::Dollar) {
778 if (Lexer.getMAI().getDollarIsPC()) {
779 // This is a '$' reference, which references the current PC. Emit a
780 // temporary label to the streamer and refer to it.
781 MCSymbol *Sym = Ctx.CreateTempSymbol();
782 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000783 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
784 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000785 EndLoc = FirstTokenLoc;
786 return false;
787 } else
788 return Error(FirstTokenLoc, "invalid token in expression");
789 return true;
790 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000791 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000792
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000793 EndLoc = SMLoc::getFromPointer(Identifier.end());
794
Daniel Dunbard20cda02009-10-16 01:34:54 +0000795 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000796 StringRef SymbolName = Identifier;
797 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000798 std::pair<StringRef, StringRef> Split = Identifier.split('@');
799
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000800 // Lookup the symbol variant if used.
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000801 if (Split.first.size() != Identifier.size()) {
802 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000803 if (Variant != MCSymbolRefExpr::VK_Invalid) {
804 SymbolName = Split.first;
805 } else if (MAI.doesAllowAtInName()) {
806 Variant = MCSymbolRefExpr::VK_None;
807 } else {
Daniel Dunbar55f16672010-09-17 02:47:07 +0000808 Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000809 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000810 }
811 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000812
Hans Wennborgce69d772013-10-18 20:46:28 +0000813 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
814
Daniel Dunbard20cda02009-10-16 01:34:54 +0000815 // If this is an absolute variable reference, substitute it now to preserve
816 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000817 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000818 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000819 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000820
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000821 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000822 return false;
823 }
824
825 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000826 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000827 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000828 }
Kevin Enderby0510b482010-05-17 23:08:19 +0000829 case AsmToken::Integer: {
830 SMLoc Loc = getTok().getLoc();
831 int64_t IntVal = getTok().getIntVal();
832 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000833 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000834 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000835 // Look for 'b' or 'f' following an Integer as a directional label
836 if (Lexer.getKind() == AsmToken::Identifier) {
837 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000838 // Lookup the symbol variant if used.
839 std::pair<StringRef, StringRef> Split = IDVal.split('@');
840 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
841 if (Split.first.size() != IDVal.size()) {
842 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
843 if (Variant == MCSymbolRefExpr::VK_Invalid) {
844 Variant = MCSymbolRefExpr::VK_None;
845 return TokError("invalid variant '" + Split.second + "'");
846 }
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000847 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000848 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000849 if (IDVal == "f" || IDVal == "b") {
850 MCSymbol *Sym =
851 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "f" ? 1 : 0);
Ulrich Weigandd4120982013-06-20 16:24:17 +0000852 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000853 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000854 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000855 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000856 Lex(); // Eat identifier.
857 }
858 }
Chris Lattner78db3622009-06-22 05:51:26 +0000859 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000860 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000861 case AsmToken::Real: {
862 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000863 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000864 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000865 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000866 Lex(); // Eat token.
867 return false;
868 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000869 case AsmToken::Dot: {
870 // This is a '.' reference, which references the current PC. Emit a
871 // temporary label to the streamer and refer to it.
872 MCSymbol *Sym = Ctx.CreateTempSymbol();
873 Out.EmitLabel(Sym);
874 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000875 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000876 Lex(); // Eat identifier.
877 return false;
878 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000879 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000880 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000881 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000882 case AsmToken::LBrac:
883 if (!PlatformParser->HasBracketExpressions())
884 return TokError("brackets expression not supported on this target");
885 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000886 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000887 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000888 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000889 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000890 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000891 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000892 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000893 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000894 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000895 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000896 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000897 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000898 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000899 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000900 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000901 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000902 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000903 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000904 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000905 }
906}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000907
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000908bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000909 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000910 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000911}
912
Daniel Dunbar55f16672010-09-17 02:47:07 +0000913const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000914AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000915 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000916 // Ask the target implementation about this expression first.
917 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
918 if (NewE)
919 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000920 // Recurse over the given expression, rebuilding it to apply the given variant
921 // if there is exactly one symbol.
922 switch (E->getKind()) {
923 case MCExpr::Target:
924 case MCExpr::Constant:
925 return 0;
926
927 case MCExpr::SymbolRef: {
928 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
929
930 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000931 TokError("invalid variant on expression '" + getTok().getIdentifier() +
932 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000933 return E;
934 }
935
936 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
937 }
938
939 case MCExpr::Unary: {
940 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000941 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000942 if (!Sub)
943 return 0;
944 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
945 }
946
947 case MCExpr::Binary: {
948 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000949 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
950 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000951
952 if (!LHS && !RHS)
953 return 0;
954
Jim Grosbach4b905842013-09-20 23:08:21 +0000955 if (!LHS)
956 LHS = BE->getLHS();
957 if (!RHS)
958 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +0000959
960 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
961 }
962 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +0000963
Craig Toppera2886c22012-02-07 05:05:23 +0000964 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000965}
966
Jim Grosbach4b905842013-09-20 23:08:21 +0000967/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +0000968///
Jim Grosbachbd164242011-08-20 16:24:13 +0000969/// expr ::= expr &&,|| expr -> lowest.
970/// expr ::= expr |,^,&,! expr
971/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
972/// expr ::= expr <<,>> expr
973/// expr ::= expr +,- expr
974/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000975/// expr ::= primaryexpr
976///
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000977bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +0000978 // Parse the expression.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000979 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +0000980 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +0000981 return true;
982
Daniel Dunbar55f16672010-09-17 02:47:07 +0000983 // As a special case, we support 'a op b @ modifier' by rewriting the
984 // expression to include the modifier. This is inefficient, but in general we
985 // expect users to use 'a@modifier op b'.
986 if (Lexer.getKind() == AsmToken::At) {
987 Lex();
988
989 if (Lexer.isNot(AsmToken::Identifier))
990 return TokError("unexpected symbol modifier following '@'");
991
992 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +0000993 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +0000994 if (Variant == MCSymbolRefExpr::VK_Invalid)
995 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
996
Jim Grosbach4b905842013-09-20 23:08:21 +0000997 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000998 if (!ModifiedRes) {
999 return TokError("invalid modifier '" + getTok().getIdentifier() +
1000 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001001 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001002
Daniel Dunbar55f16672010-09-17 02:47:07 +00001003 Res = ModifiedRes;
1004 Lex();
1005 }
1006
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001007 // Try to constant fold it up front, if possible.
1008 int64_t Value;
1009 if (Res->EvaluateAsAbsolute(Value))
1010 Res = MCConstantExpr::Create(Value, getContext());
1011
1012 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001013}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001014
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001015bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner807a3bc2010-01-24 01:07:33 +00001016 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001017 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001018}
1019
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001020bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001021 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001022
Daniel Dunbar75630b32009-06-30 02:10:03 +00001023 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001024 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001025 return true;
1026
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001027 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001028 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001029
1030 return false;
1031}
1032
Michael J. Spencer530ce852010-10-09 11:00:50 +00001033static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001034 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001035 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001036 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001037 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001038
Jim Grosbach4b905842013-09-20 23:08:21 +00001039 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001040 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001041 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001042 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001043 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001044 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001045 return 1;
1046
Jim Grosbach4b905842013-09-20 23:08:21 +00001047 // Low Precedence: |, &, ^
1048 //
1049 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001050 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001051 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001052 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001053 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001054 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001055 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001056 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001057 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001058 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001059
Jim Grosbach4b905842013-09-20 23:08:21 +00001060 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001061 case AsmToken::EqualEqual:
1062 Kind = MCBinaryExpr::EQ;
1063 return 3;
1064 case AsmToken::ExclaimEqual:
1065 case AsmToken::LessGreater:
1066 Kind = MCBinaryExpr::NE;
1067 return 3;
1068 case AsmToken::Less:
1069 Kind = MCBinaryExpr::LT;
1070 return 3;
1071 case AsmToken::LessEqual:
1072 Kind = MCBinaryExpr::LTE;
1073 return 3;
1074 case AsmToken::Greater:
1075 Kind = MCBinaryExpr::GT;
1076 return 3;
1077 case AsmToken::GreaterEqual:
1078 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001079 return 3;
1080
Jim Grosbach4b905842013-09-20 23:08:21 +00001081 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001082 case AsmToken::LessLess:
1083 Kind = MCBinaryExpr::Shl;
1084 return 4;
1085 case AsmToken::GreaterGreater:
1086 Kind = MCBinaryExpr::Shr;
1087 return 4;
1088
Jim Grosbach4b905842013-09-20 23:08:21 +00001089 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001090 case AsmToken::Plus:
1091 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001092 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001093 case AsmToken::Minus:
1094 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001095 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001096
Jim Grosbach4b905842013-09-20 23:08:21 +00001097 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001098 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001099 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001100 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001101 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001102 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001103 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001104 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001105 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001106 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001107 }
1108}
1109
Jim Grosbach4b905842013-09-20 23:08:21 +00001110/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001111/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001112bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001113 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001114 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001115 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001116 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001117
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001118 // If the next token is lower precedence than we are allowed to eat, return
1119 // successfully with what we ate already.
1120 if (TokPrec < Precedence)
1121 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001122
Sean Callanan686ed8d2010-01-19 20:22:31 +00001123 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001124
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001125 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001126 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001127 if (parsePrimaryExpr(RHS, EndLoc))
1128 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001129
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001130 // If BinOp binds less tightly with RHS than the operator after RHS, let
1131 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001132 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001133 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001134 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1135 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001136
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001137 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001138 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001139 }
1140}
1141
Chris Lattner36e02122009-06-21 20:54:55 +00001142/// ParseStatement:
1143/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001144/// ::= Label* Directive ...Operands... EndOfStatement
1145/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001146bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001147 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001148 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001149 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001150 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001151 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001152
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001153 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001154 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001155 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001156 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001157 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001158 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001159 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001160 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001161
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001162 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001163 if (Lexer.is(AsmToken::Integer)) {
1164 LocalLabelVal = getTok().getIntVal();
1165 if (LocalLabelVal < 0) {
1166 if (!TheCondState.Ignore)
1167 return TokError("unexpected token at start of statement");
1168 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001169 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001170 IDVal = getTok().getString();
1171 Lex(); // Consume the integer token to be used as an identifier token.
1172 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001173 if (!TheCondState.Ignore)
1174 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001175 }
1176 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001177 } else if (Lexer.is(AsmToken::Dot)) {
1178 // Treat '.' as a valid identifier in this context.
1179 Lex();
1180 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001181 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001182 if (!TheCondState.Ignore)
1183 return TokError("unexpected token at start of statement");
1184 IDVal = "";
1185 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001186
Chris Lattner926885c2010-04-17 18:14:27 +00001187 // Handle conditional assembly here before checking for skipping. We
1188 // have to do this so that .endif isn't skipped in a ".if 0" block for
1189 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001190 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001191 DirectiveKindMap.find(IDVal);
1192 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1193 ? DK_NO_DIRECTIVE
1194 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001195 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001196 default:
1197 break;
1198 case DK_IF:
1199 return parseDirectiveIf(IDLoc);
1200 case DK_IFB:
1201 return parseDirectiveIfb(IDLoc, true);
1202 case DK_IFNB:
1203 return parseDirectiveIfb(IDLoc, false);
1204 case DK_IFC:
1205 return parseDirectiveIfc(IDLoc, true);
1206 case DK_IFNC:
1207 return parseDirectiveIfc(IDLoc, false);
1208 case DK_IFDEF:
1209 return parseDirectiveIfdef(IDLoc, true);
1210 case DK_IFNDEF:
1211 case DK_IFNOTDEF:
1212 return parseDirectiveIfdef(IDLoc, false);
1213 case DK_ELSEIF:
1214 return parseDirectiveElseIf(IDLoc);
1215 case DK_ELSE:
1216 return parseDirectiveElse(IDLoc);
1217 case DK_ENDIF:
1218 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001219 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001220
Eli Bendersky88024712013-01-16 19:32:36 +00001221 // Ignore the statement if in the middle of inactive conditional
1222 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001223 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001224 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001225 return false;
1226 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001227
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001228 // FIXME: Recurse on local labels?
1229
1230 // See what kind of statement we have.
1231 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001232 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001233 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001234
Chris Lattner36e02122009-06-21 20:54:55 +00001235 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001236 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001237
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001238 // Diagnose attempt to use '.' as a label.
1239 if (IDVal == ".")
1240 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1241
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001242 // Diagnose attempt to use a variable as a label.
1243 //
1244 // FIXME: Diagnostics. Note the location of the definition as a label.
1245 // FIXME: This doesn't diagnose assignment to a symbol which has been
1246 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001247 MCSymbol *Sym;
1248 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001249 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001250 else
1251 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001252 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001253 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001254
Daniel Dunbare73b2672009-08-26 22:13:22 +00001255 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001256 if (!ParsingInlineAsm)
1257 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001258
Kevin Enderbye7739d42011-12-09 18:09:40 +00001259 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001260 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001261 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001262 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1263 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001264
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001265 // Consume any end of statement token, if present, to avoid spurious
1266 // AddBlankLine calls().
1267 if (Lexer.is(AsmToken::EndOfStatement)) {
1268 Lex();
1269 if (Lexer.is(AsmToken::Eof))
1270 return false;
1271 }
1272
Eli Friedman0f4871d2012-10-22 23:58:19 +00001273 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001274 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001275
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001276 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001277 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001278 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001279
Jim Grosbach4b905842013-09-20 23:08:21 +00001280 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001281
1282 default: // Normal instruction or directive.
1283 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001284 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001285
1286 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001287 if (areMacrosEnabled())
1288 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1289 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001290 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001291
Michael J. Spencer530ce852010-10-09 11:00:50 +00001292 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001293
Eli Bendersky17233942013-01-15 22:59:42 +00001294 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001295 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001296 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001297 //
Eli Bendersky17233942013-01-15 22:59:42 +00001298 // 1. The target-specific assembly parser. Some directives are target
1299 // specific or may potentially behave differently on certain targets.
1300 // 2. Asm parser extensions. For example, platform-specific parsers
1301 // (like the ELF parser) register themselves as extensions.
1302 // 3. The generic directive parser implemented by this class. These are
1303 // all the directives that behave in a target and platform independent
1304 // manner, or at least have a default behavior that's shared between
1305 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001306
Eli Bendersky17233942013-01-15 22:59:42 +00001307 // First query the target-specific parser. It will return 'true' if it
1308 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001309 if (!getTargetParser().ParseDirective(ID))
1310 return false;
1311
Eli Bendersky17233942013-01-15 22:59:42 +00001312 // Next, check the extention directive map to see if any extension has
1313 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001314 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1315 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001316 if (Handler.first)
1317 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1318
1319 // Finally, if no one else is interested in this directive, it must be
1320 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001321 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001322 default:
1323 break;
1324 case DK_SET:
1325 case DK_EQU:
1326 return parseDirectiveSet(IDVal, true);
1327 case DK_EQUIV:
1328 return parseDirectiveSet(IDVal, false);
1329 case DK_ASCII:
1330 return parseDirectiveAscii(IDVal, false);
1331 case DK_ASCIZ:
1332 case DK_STRING:
1333 return parseDirectiveAscii(IDVal, true);
1334 case DK_BYTE:
1335 return parseDirectiveValue(1);
1336 case DK_SHORT:
1337 case DK_VALUE:
1338 case DK_2BYTE:
1339 return parseDirectiveValue(2);
1340 case DK_LONG:
1341 case DK_INT:
1342 case DK_4BYTE:
1343 return parseDirectiveValue(4);
1344 case DK_QUAD:
1345 case DK_8BYTE:
1346 return parseDirectiveValue(8);
1347 case DK_SINGLE:
1348 case DK_FLOAT:
1349 return parseDirectiveRealValue(APFloat::IEEEsingle);
1350 case DK_DOUBLE:
1351 return parseDirectiveRealValue(APFloat::IEEEdouble);
1352 case DK_ALIGN: {
1353 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1354 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1355 }
1356 case DK_ALIGN32: {
1357 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1358 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1359 }
1360 case DK_BALIGN:
1361 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1362 case DK_BALIGNW:
1363 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1364 case DK_BALIGNL:
1365 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1366 case DK_P2ALIGN:
1367 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1368 case DK_P2ALIGNW:
1369 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1370 case DK_P2ALIGNL:
1371 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1372 case DK_ORG:
1373 return parseDirectiveOrg();
1374 case DK_FILL:
1375 return parseDirectiveFill();
1376 case DK_ZERO:
1377 return parseDirectiveZero();
1378 case DK_EXTERN:
1379 eatToEndOfStatement(); // .extern is the default, ignore it.
1380 return false;
1381 case DK_GLOBL:
1382 case DK_GLOBAL:
1383 return parseDirectiveSymbolAttribute(MCSA_Global);
1384 case DK_LAZY_REFERENCE:
1385 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1386 case DK_NO_DEAD_STRIP:
1387 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1388 case DK_SYMBOL_RESOLVER:
1389 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1390 case DK_PRIVATE_EXTERN:
1391 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1392 case DK_REFERENCE:
1393 return parseDirectiveSymbolAttribute(MCSA_Reference);
1394 case DK_WEAK_DEFINITION:
1395 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1396 case DK_WEAK_REFERENCE:
1397 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1398 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1399 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1400 case DK_COMM:
1401 case DK_COMMON:
1402 return parseDirectiveComm(/*IsLocal=*/false);
1403 case DK_LCOMM:
1404 return parseDirectiveComm(/*IsLocal=*/true);
1405 case DK_ABORT:
1406 return parseDirectiveAbort();
1407 case DK_INCLUDE:
1408 return parseDirectiveInclude();
1409 case DK_INCBIN:
1410 return parseDirectiveIncbin();
1411 case DK_CODE16:
1412 case DK_CODE16GCC:
1413 return TokError(Twine(IDVal) + " not supported yet");
1414 case DK_REPT:
1415 return parseDirectiveRept(IDLoc);
1416 case DK_IRP:
1417 return parseDirectiveIrp(IDLoc);
1418 case DK_IRPC:
1419 return parseDirectiveIrpc(IDLoc);
1420 case DK_ENDR:
1421 return parseDirectiveEndr(IDLoc);
1422 case DK_BUNDLE_ALIGN_MODE:
1423 return parseDirectiveBundleAlignMode();
1424 case DK_BUNDLE_LOCK:
1425 return parseDirectiveBundleLock();
1426 case DK_BUNDLE_UNLOCK:
1427 return parseDirectiveBundleUnlock();
1428 case DK_SLEB128:
1429 return parseDirectiveLEB128(true);
1430 case DK_ULEB128:
1431 return parseDirectiveLEB128(false);
1432 case DK_SPACE:
1433 case DK_SKIP:
1434 return parseDirectiveSpace(IDVal);
1435 case DK_FILE:
1436 return parseDirectiveFile(IDLoc);
1437 case DK_LINE:
1438 return parseDirectiveLine();
1439 case DK_LOC:
1440 return parseDirectiveLoc();
1441 case DK_STABS:
1442 return parseDirectiveStabs();
1443 case DK_CFI_SECTIONS:
1444 return parseDirectiveCFISections();
1445 case DK_CFI_STARTPROC:
1446 return parseDirectiveCFIStartProc();
1447 case DK_CFI_ENDPROC:
1448 return parseDirectiveCFIEndProc();
1449 case DK_CFI_DEF_CFA:
1450 return parseDirectiveCFIDefCfa(IDLoc);
1451 case DK_CFI_DEF_CFA_OFFSET:
1452 return parseDirectiveCFIDefCfaOffset();
1453 case DK_CFI_ADJUST_CFA_OFFSET:
1454 return parseDirectiveCFIAdjustCfaOffset();
1455 case DK_CFI_DEF_CFA_REGISTER:
1456 return parseDirectiveCFIDefCfaRegister(IDLoc);
1457 case DK_CFI_OFFSET:
1458 return parseDirectiveCFIOffset(IDLoc);
1459 case DK_CFI_REL_OFFSET:
1460 return parseDirectiveCFIRelOffset(IDLoc);
1461 case DK_CFI_PERSONALITY:
1462 return parseDirectiveCFIPersonalityOrLsda(true);
1463 case DK_CFI_LSDA:
1464 return parseDirectiveCFIPersonalityOrLsda(false);
1465 case DK_CFI_REMEMBER_STATE:
1466 return parseDirectiveCFIRememberState();
1467 case DK_CFI_RESTORE_STATE:
1468 return parseDirectiveCFIRestoreState();
1469 case DK_CFI_SAME_VALUE:
1470 return parseDirectiveCFISameValue(IDLoc);
1471 case DK_CFI_RESTORE:
1472 return parseDirectiveCFIRestore(IDLoc);
1473 case DK_CFI_ESCAPE:
1474 return parseDirectiveCFIEscape();
1475 case DK_CFI_SIGNAL_FRAME:
1476 return parseDirectiveCFISignalFrame();
1477 case DK_CFI_UNDEFINED:
1478 return parseDirectiveCFIUndefined(IDLoc);
1479 case DK_CFI_REGISTER:
1480 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001481 case DK_CFI_WINDOW_SAVE:
1482 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001483 case DK_MACROS_ON:
1484 case DK_MACROS_OFF:
1485 return parseDirectiveMacrosOnOff(IDVal);
1486 case DK_MACRO:
1487 return parseDirectiveMacro(IDLoc);
1488 case DK_ENDM:
1489 case DK_ENDMACRO:
1490 return parseDirectiveEndMacro(IDVal);
1491 case DK_PURGEM:
1492 return parseDirectivePurgeMacro(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001493 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001494
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001495 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001496 }
Chris Lattner36e02122009-06-21 20:54:55 +00001497
Chad Rosierc7f552c2013-02-12 21:33:51 +00001498 // __asm _emit or __asm __emit
1499 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1500 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001501 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001502
1503 // __asm align
1504 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001505 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001506
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001507 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001508
Chris Lattner7cbfa442010-05-19 23:34:33 +00001509 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001510 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001511 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001512 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001513 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001514 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001515
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001516 // Dump the parsed representation, if requested.
1517 if (getShowParsedOperands()) {
1518 SmallString<256> Str;
1519 raw_svector_ostream OS(Str);
1520 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001521 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001522 if (i != 0)
1523 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001524 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001525 }
1526 OS << "]";
1527
Jim Grosbach4b905842013-09-20 23:08:21 +00001528 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001529 }
1530
Kevin Enderby6469fc22011-11-01 22:27:22 +00001531 // If we are generating dwarf for assembly source files and the current
1532 // section is the initial text section then generate a .loc directive for
1533 // the instruction.
1534 if (!HadError && getContext().getGenDwarfForAssembly() &&
Peter Collingbourne2f495b92013-04-17 21:18:16 +00001535 getContext().getGenDwarfSection() ==
Jim Grosbach4b905842013-09-20 23:08:21 +00001536 getStreamer().getCurrentSection().first) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001537
Eli Bendersky88024712013-01-16 19:32:36 +00001538 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001539
Eli Bendersky88024712013-01-16 19:32:36 +00001540 // If we previously parsed a cpp hash file line comment then make sure the
1541 // current Dwarf File is for the CppHashFilename if not then emit the
1542 // Dwarf File table for it and adjust the line number for the .loc.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001543 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +00001544 getContext().getMCDwarfFiles();
Eli Bendersky88024712013-01-16 19:32:36 +00001545 if (CppHashFilename.size() != 0) {
1546 if (MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() !=
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001547 CppHashFilename)
Eli Bendersky88024712013-01-16 19:32:36 +00001548 getStreamer().EmitDwarfFileDirective(
Jim Grosbach4b905842013-09-20 23:08:21 +00001549 getContext().nextGenDwarfFileNumber(), StringRef(),
1550 CppHashFilename);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001551
Jim Grosbach4b905842013-09-20 23:08:21 +00001552 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1553 // cache with the different Loc from the call above we save the last
1554 // info we queried here with SrcMgr.FindLineNumber().
1555 unsigned CppHashLocLineNo;
1556 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1557 CppHashLocLineNo = LastQueryLine;
1558 else {
1559 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1560 LastQueryLine = CppHashLocLineNo;
1561 LastQueryIDLoc = CppHashLoc;
1562 LastQueryBuffer = CppHashBuf;
1563 }
1564 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001565 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001566
Jim Grosbach4b905842013-09-20 23:08:21 +00001567 getStreamer().EmitDwarfLocDirective(
1568 getContext().getGenDwarfFileNumber(), Line, 0,
1569 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1570 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001571 }
1572
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001573 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001574 if (!HadError) {
Chad Rosier49963552012-10-13 00:26:04 +00001575 unsigned ErrorInfo;
Jim Grosbach4b905842013-09-20 23:08:21 +00001576 HadError = getTargetParser().MatchAndEmitInstruction(
1577 IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo,
1578 ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001579 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001580
Chris Lattnera2a9d162010-09-11 16:18:25 +00001581 // Don't skip the rest of the line, the instruction parser is responsible for
1582 // that.
1583 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001584}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001585
Jim Grosbach4b905842013-09-20 23:08:21 +00001586/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001587/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001588void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001589 if (!Lexer.is(AsmToken::EndOfStatement))
1590 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001591 // Eat EOL.
1592 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001593}
1594
Jim Grosbach4b905842013-09-20 23:08:21 +00001595/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001596/// ::= # number "filename"
1597/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001598bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001599 Lex(); // Eat the hash token.
1600
1601 if (getLexer().isNot(AsmToken::Integer)) {
1602 // Consume the line since in cases it is not a well-formed line directive,
1603 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001604 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001605 return false;
1606 }
1607
1608 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001609 Lex();
1610
1611 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001612 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001613 return false;
1614 }
1615
1616 StringRef Filename = getTok().getString();
1617 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001618 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001619
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001620 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1621 CppHashLoc = L;
1622 CppHashFilename = Filename;
1623 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001624 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001625
1626 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001627 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001628 return false;
1629}
1630
Jim Grosbach4b905842013-09-20 23:08:21 +00001631/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001632/// for the Filename and LineNo if any in the diagnostic.
1633void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001634 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001635 raw_ostream &OS = errs();
1636
1637 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1638 const SMLoc &DiagLoc = Diag.getLoc();
1639 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1640 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1641
Jim Grosbach4b905842013-09-20 23:08:21 +00001642 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001643 // before printing the message.
1644 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001645 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001646 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1647 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001648 }
1649
Eric Christophera7c32732012-12-18 00:30:54 +00001650 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001651 // manager changed or buffer changed (like in a nested include) then just
1652 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001653 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001654 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001655 if (Parser->SavedDiagHandler)
1656 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1657 else
1658 Diag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001659 return;
1660 }
1661
Eric Christophera7c32732012-12-18 00:30:54 +00001662 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001663 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1664 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001665 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001666
1667 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1668 int CppHashLocLineNo =
1669 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001670 int LineNo =
1671 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001672
Jim Grosbach4b905842013-09-20 23:08:21 +00001673 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1674 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001675 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001676
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001677 if (Parser->SavedDiagHandler)
1678 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1679 else
1680 NewDiag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001681}
1682
Rafael Espindola2c064482012-08-21 18:29:30 +00001683// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1684// difference being that that function accepts '@' as part of identifiers and
1685// we can't do that. AsmLexer.cpp should probably be changed to handle
1686// '@' as a special case when needed.
1687static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001688 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1689 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001690}
1691
Rafael Espindola34b9c512012-06-03 23:57:14 +00001692bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Eli Bendersky38274122013-01-14 23:22:36 +00001693 const MCAsmMacroParameters &Parameters,
Jim Grosbach4b905842013-09-20 23:08:21 +00001694 const MCAsmMacroArguments &A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001695 unsigned NParameters = Parameters.size();
1696 if (NParameters != 0 && NParameters != A.size())
1697 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001698
Preston Gurd05500642012-09-19 20:36:12 +00001699 // A macro without parameters is handled differently on Darwin:
1700 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001701 while (!Body.empty()) {
1702 // Scan for the next substitution.
1703 std::size_t End = Body.size(), Pos = 0;
1704 for (; Pos != End; ++Pos) {
1705 // Check for a substitution or escape.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001706 if (!NParameters) {
1707 // This macro has no parameters, look for $0, $1, etc.
1708 if (Body[Pos] != '$' || Pos + 1 == End)
1709 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001710
Rafael Espindola1134ab232011-06-05 02:43:45 +00001711 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001712 if (Next == '$' || Next == 'n' ||
1713 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001714 break;
1715 } else {
1716 // This macro has parameters, look for \foo, \bar, etc.
1717 if (Body[Pos] == '\\' && Pos + 1 != End)
1718 break;
1719 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001720 }
1721
1722 // Add the prefix.
1723 OS << Body.slice(0, Pos);
1724
1725 // Check if we reached the end.
1726 if (Pos == End)
1727 break;
1728
Rafael Espindola1134ab232011-06-05 02:43:45 +00001729 if (!NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001730 switch (Body[Pos + 1]) {
1731 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001732 case '$':
1733 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001734 break;
1735
Jim Grosbach4b905842013-09-20 23:08:21 +00001736 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001737 case 'n':
1738 OS << A.size();
1739 break;
1740
Jim Grosbach4b905842013-09-20 23:08:21 +00001741 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001742 default: {
1743 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001744 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001745 if (Index >= A.size())
1746 break;
1747
1748 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001749 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001750 ie = A[Index].end();
1751 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001752 OS << it->getString();
1753 break;
1754 }
1755 }
1756 Pos += 2;
1757 } else {
1758 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001759 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001760 ++I;
1761
Jim Grosbach4b905842013-09-20 23:08:21 +00001762 const char *Begin = Body.data() + Pos + 1;
1763 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001764 unsigned Index = 0;
1765 for (; Index < NParameters; ++Index)
Preston Gurd242ed3152012-09-19 20:29:04 +00001766 if (Parameters[Index].first == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001767 break;
1768
Preston Gurd05500642012-09-19 20:36:12 +00001769 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001770 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1771 Pos += 3;
1772 else {
1773 OS << '\\' << Argument;
1774 Pos = I;
1775 }
Preston Gurd05500642012-09-19 20:36:12 +00001776 } else {
Eli Benderskya7b905e2013-01-14 19:00:26 +00001777 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001778 ie = A[Index].end();
1779 it != ie; ++it)
Preston Gurd05500642012-09-19 20:36:12 +00001780 if (it->getKind() == AsmToken::String)
1781 OS << it->getStringContents();
1782 else
1783 OS << it->getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001784
Preston Gurd05500642012-09-19 20:36:12 +00001785 Pos += 1 + Argument.size();
1786 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001787 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001788 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001789 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001790 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001791
Rafael Espindola1134ab232011-06-05 02:43:45 +00001792 return false;
1793}
Daniel Dunbar43235712010-07-18 18:54:11 +00001794
Jim Grosbach4b905842013-09-20 23:08:21 +00001795MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1796 SMLoc EL, MemoryBuffer *I)
1797 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1798 ExitLoc(EL) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001799
Jim Grosbach4b905842013-09-20 23:08:21 +00001800static bool isOperator(AsmToken::TokenKind kind) {
1801 switch (kind) {
1802 default:
1803 return false;
1804 case AsmToken::Plus:
1805 case AsmToken::Minus:
1806 case AsmToken::Tilde:
1807 case AsmToken::Slash:
1808 case AsmToken::Star:
1809 case AsmToken::Dot:
1810 case AsmToken::Equal:
1811 case AsmToken::EqualEqual:
1812 case AsmToken::Pipe:
1813 case AsmToken::PipePipe:
1814 case AsmToken::Caret:
1815 case AsmToken::Amp:
1816 case AsmToken::AmpAmp:
1817 case AsmToken::Exclaim:
1818 case AsmToken::ExclaimEqual:
1819 case AsmToken::Percent:
1820 case AsmToken::Less:
1821 case AsmToken::LessEqual:
1822 case AsmToken::LessLess:
1823 case AsmToken::LessGreater:
1824 case AsmToken::Greater:
1825 case AsmToken::GreaterEqual:
1826 case AsmToken::GreaterGreater:
1827 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001828 }
1829}
1830
Jim Grosbach4b905842013-09-20 23:08:21 +00001831bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA,
Preston Gurd05500642012-09-19 20:36:12 +00001832 AsmToken::TokenKind &ArgumentDelimiter) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001833 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001834 unsigned AddTokens = 0;
1835
1836 // gas accepts arguments separated by whitespace, except on Darwin
1837 if (!IsDarwin)
1838 Lexer.setSkipSpace(false);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001839
1840 for (;;) {
Preston Gurd05500642012-09-19 20:36:12 +00001841 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) {
1842 Lexer.setSkipSpace(true);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001843 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001844 }
1845
1846 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1847 // Spaces and commas cannot be mixed to delimit parameters
1848 if (ArgumentDelimiter == AsmToken::Eof)
1849 ArgumentDelimiter = AsmToken::Comma;
1850 else if (ArgumentDelimiter != AsmToken::Comma) {
1851 Lexer.setSkipSpace(true);
1852 return TokError("expected ' ' for macro argument separator");
1853 }
1854 break;
1855 }
1856
1857 if (Lexer.is(AsmToken::Space)) {
1858 Lex(); // Eat spaces
1859
1860 // Spaces can delimit parameters, but could also be part an expression.
1861 // If the token after a space is an operator, add the token and the next
1862 // one into this argument
1863 if (ArgumentDelimiter == AsmToken::Space ||
1864 ArgumentDelimiter == AsmToken::Eof) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001865 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001866 // Check to see whether the token is used as an operator,
1867 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001868 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001869 if (*NextChar == ' ')
1870 AddTokens = 2;
1871 }
1872
1873 if (!AddTokens && ParenLevel == 0) {
1874 if (ArgumentDelimiter == AsmToken::Eof &&
Jim Grosbach4b905842013-09-20 23:08:21 +00001875 !isOperator(Lexer.getKind()))
Preston Gurd05500642012-09-19 20:36:12 +00001876 ArgumentDelimiter = AsmToken::Space;
1877 break;
1878 }
1879 }
1880 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001881
Jim Grosbach4b905842013-09-20 23:08:21 +00001882 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001883 // to be able to fill in the remaining default parameter values
1884 if (Lexer.is(AsmToken::EndOfStatement))
1885 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001886
1887 // Adjust the current parentheses level.
1888 if (Lexer.is(AsmToken::LParen))
1889 ++ParenLevel;
1890 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1891 --ParenLevel;
1892
1893 // Append the token to the current argument list.
1894 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001895 if (AddTokens)
1896 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001897 Lex();
1898 }
Preston Gurd05500642012-09-19 20:36:12 +00001899
1900 Lexer.setSkipSpace(true);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001901 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001902 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001903 return false;
1904}
1905
1906// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001907bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001908 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001909 const unsigned NParameters = M ? M->Parameters.size() : 0;
Preston Gurd05500642012-09-19 20:36:12 +00001910 // Argument delimiter is initially unknown. It will be set by
Jim Grosbach4b905842013-09-20 23:08:21 +00001911 // parseMacroArgument()
Preston Gurd05500642012-09-19 20:36:12 +00001912 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001913
1914 // Parse two kinds of macro invocations:
1915 // - macros defined without any parameters accept an arbitrary number of them
1916 // - macros defined with parameters accept at most that many of them
1917 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1918 ++Parameter) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00001919 MCAsmMacroArgument MA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001920
Jim Grosbach4b905842013-09-20 23:08:21 +00001921 if (parseMacroArgument(MA, ArgumentDelimiter))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001922 return true;
1923
Preston Gurd242ed3152012-09-19 20:29:04 +00001924 if (!MA.empty() || !NParameters)
1925 A.push_back(MA);
1926 else if (NParameters) {
1927 if (!M->Parameters[Parameter].second.empty())
1928 A.push_back(M->Parameters[Parameter].second);
1929 }
Jim Grosbach206661622012-07-30 22:44:17 +00001930
Preston Gurd242ed3152012-09-19 20:29:04 +00001931 // At the end of the statement, fill in remaining arguments that have
1932 // default values. If there aren't any, then the next argument is
1933 // required but missing
1934 if (Lexer.is(AsmToken::EndOfStatement)) {
1935 if (NParameters && Parameter < NParameters - 1) {
1936 if (M->Parameters[Parameter + 1].second.empty())
1937 return TokError("macro argument '" +
1938 Twine(M->Parameters[Parameter + 1].first) +
1939 "' is missing");
1940 else
1941 continue;
1942 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001943 return false;
Preston Gurd242ed3152012-09-19 20:29:04 +00001944 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001945
1946 if (Lexer.is(AsmToken::Comma))
1947 Lex();
1948 }
1949 return TokError("Too many arguments");
1950}
1951
Jim Grosbach4b905842013-09-20 23:08:21 +00001952const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
1953 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00001954 return (I == MacroMap.end()) ? NULL : I->getValue();
1955}
1956
Jim Grosbach4b905842013-09-20 23:08:21 +00001957void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00001958 MacroMap[Name] = new MCAsmMacro(Macro);
1959}
1960
Jim Grosbach4b905842013-09-20 23:08:21 +00001961void AsmParser::undefineMacro(StringRef Name) {
1962 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00001963 if (I != MacroMap.end()) {
1964 delete I->getValue();
1965 MacroMap.erase(I);
1966 }
1967}
1968
Jim Grosbach4b905842013-09-20 23:08:21 +00001969bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00001970 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1971 // this, although we should protect against infinite loops.
1972 if (ActiveMacros.size() == 20)
1973 return TokError("macros cannot be nested more than 20 levels deep");
1974
Eli Bendersky38274122013-01-14 23:22:36 +00001975 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00001976 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001977 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00001978
Jim Grosbach206661622012-07-30 22:44:17 +00001979 // Remove any trailing empty arguments. Do this after-the-fact as we have
1980 // to keep empty arguments in the middle of the list or positionality
1981 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00001982 while (!A.empty() && A.back().empty())
1983 A.pop_back();
Jim Grosbach206661622012-07-30 22:44:17 +00001984
Rafael Espindola1134ab232011-06-05 02:43:45 +00001985 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1986 // to hold the macro body with substitutions.
1987 SmallString<256> Buf;
1988 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00001989 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00001990
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00001991 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001992 return true;
1993
Eli Bendersky38274122013-01-14 23:22:36 +00001994 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00001995 // instantiation.
1996 OS << ".endmacro\n";
1997
Rafael Espindola1134ab232011-06-05 02:43:45 +00001998 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00001999 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002000
Daniel Dunbar43235712010-07-18 18:54:11 +00002001 // Create the macro instantiation object and add to the current macro
2002 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00002003 MacroInstantiation *MI = new MacroInstantiation(
2004 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbar43235712010-07-18 18:54:11 +00002005 ActiveMacros.push_back(MI);
2006
2007 // Jump to the macro instantiation and prime the lexer.
2008 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
2009 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
2010 Lex();
2011
2012 return false;
2013}
2014
Jim Grosbach4b905842013-09-20 23:08:21 +00002015void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002016 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002017 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002018 Lex();
2019
2020 // Pop the instantiation entry.
2021 delete ActiveMacros.back();
2022 ActiveMacros.pop_back();
2023}
2024
Jim Grosbach4b905842013-09-20 23:08:21 +00002025static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002026 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002027 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002028 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2029 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002030 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002031 case MCExpr::Target:
2032 case MCExpr::Constant:
2033 return false;
2034 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002035 const MCSymbol &S =
2036 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002037 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002038 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002039 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002040 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002041 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002042 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002043 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002044
2045 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002046}
2047
Jim Grosbach4b905842013-09-20 23:08:21 +00002048bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002049 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002050 // FIXME: Use better location, we should use proper tokens.
2051 SMLoc EqualLoc = Lexer.getLoc();
2052
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002053 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002054 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002055 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002056
Rafael Espindola72f5f172012-01-28 05:57:00 +00002057 // Note: we don't count b as used in "a = b". This is to allow
2058 // a = b
2059 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002060
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002061 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002062 return TokError("unexpected token in assignment");
2063
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00002064 // Error on assignment to '.'.
2065 if (Name == ".") {
2066 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
2067 "(use '.space' or '.org').)"));
2068 }
2069
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002070 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002071 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002072
Daniel Dunbar5f339242009-10-16 01:57:39 +00002073 // Validate that the LHS is allowed to be a variable (either it has not been
2074 // used as a symbol, or it is an absolute symbol).
2075 MCSymbol *Sym = getContext().LookupSymbol(Name);
2076 if (Sym) {
2077 // Diagnose assignment to a label.
2078 //
2079 // FIXME: Diagnostics. Note the location of the definition as a label.
2080 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002081 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002082 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2083 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002084 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002085 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2086 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002087 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002088 return Error(EqualLoc, "redefinition of '" + Name + "'");
2089 else if (!Sym->isVariable())
2090 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002091 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002092 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002093 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002094
2095 // Don't count these checks as uses.
2096 Sym->setUsed(false);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002097 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002098 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002099
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002100 // FIXME: Handle '.'.
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002101
2102 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002103 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002104 if (NoDeadStrip)
2105 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2106
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002107 return false;
2108}
2109
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002110/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002111/// ::= identifier
2112/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002113bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002114 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002115 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2116 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002117 // handle this as a context dependent token, instead we detect adjacent tokens
2118 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002119 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2120 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002121
Hans Wennborgce69d772013-10-18 20:46:28 +00002122 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002123 Lex();
2124 if (Lexer.isNot(AsmToken::Identifier))
2125 return true;
2126
Hans Wennborgce69d772013-10-18 20:46:28 +00002127 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2128 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002129 return true;
2130
2131 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002132 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002133 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002134 Lex();
2135 return false;
2136 }
2137
Jim Grosbach4b905842013-09-20 23:08:21 +00002138 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002139 return true;
2140
Sean Callanan936b0d32010-01-19 21:44:56 +00002141 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002142
Sean Callanan686ed8d2010-01-19 20:22:31 +00002143 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002144
2145 return false;
2146}
2147
Jim Grosbach4b905842013-09-20 23:08:21 +00002148/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002149/// ::= .equ identifier ',' expression
2150/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002151/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002152bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002153 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002154
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002155 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002156 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002157
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002158 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002159 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002160 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002161
Jim Grosbach4b905842013-09-20 23:08:21 +00002162 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002163}
2164
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002165bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002166 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002167
2168 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002169 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002170 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2171 if (Str[i] != '\\') {
2172 Data += Str[i];
2173 continue;
2174 }
2175
2176 // Recognize escaped characters. Note that this escape semantics currently
2177 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2178 ++i;
2179 if (i == e)
2180 return TokError("unexpected backslash at end of string");
2181
2182 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002183 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002184 // Consume up to three octal characters.
2185 unsigned Value = Str[i] - '0';
2186
Jim Grosbach4b905842013-09-20 23:08:21 +00002187 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002188 ++i;
2189 Value = Value * 8 + (Str[i] - '0');
2190
Jim Grosbach4b905842013-09-20 23:08:21 +00002191 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002192 ++i;
2193 Value = Value * 8 + (Str[i] - '0');
2194 }
2195 }
2196
2197 if (Value > 255)
2198 return TokError("invalid octal escape sequence (out of range)");
2199
Jim Grosbach4b905842013-09-20 23:08:21 +00002200 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002201 continue;
2202 }
2203
2204 // Otherwise recognize individual escapes.
2205 switch (Str[i]) {
2206 default:
2207 // Just reject invalid escape sequences for now.
2208 return TokError("invalid escape sequence (unrecognized character)");
2209
2210 case 'b': Data += '\b'; break;
2211 case 'f': Data += '\f'; break;
2212 case 'n': Data += '\n'; break;
2213 case 'r': Data += '\r'; break;
2214 case 't': Data += '\t'; break;
2215 case '"': Data += '"'; break;
2216 case '\\': Data += '\\'; break;
2217 }
2218 }
2219
2220 return false;
2221}
2222
Jim Grosbach4b905842013-09-20 23:08:21 +00002223/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002224/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002225bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002226 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002227 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002228
Daniel Dunbara10e5192009-06-24 23:30:00 +00002229 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002230 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002231 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002232
Daniel Dunbaref668c12009-08-14 18:19:52 +00002233 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002234 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002235 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002236
Rafael Espindola64e1af82013-07-02 15:49:13 +00002237 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002238 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002239 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002240
Sean Callanan686ed8d2010-01-19 20:22:31 +00002241 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002242
2243 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002244 break;
2245
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002246 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002247 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002248 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002249 }
2250 }
2251
Sean Callanan686ed8d2010-01-19 20:22:31 +00002252 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002253 return false;
2254}
2255
Jim Grosbach4b905842013-09-20 23:08:21 +00002256/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002257/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002258bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002259 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002260 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002261
Daniel Dunbara10e5192009-06-24 23:30:00 +00002262 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002263 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002264 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002265 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002266 return true;
2267
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002268 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002269 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2270 assert(Size <= 8 && "Invalid size");
2271 uint64_t IntValue = MCE->getValue();
2272 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2273 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002274 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002275 } else
Rafael Espindola64e1af82013-07-02 15:49:13 +00002276 getStreamer().EmitValue(Value, Size);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002277
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002278 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002279 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002280
Daniel Dunbara10e5192009-06-24 23:30:00 +00002281 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002282 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002283 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002284 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002285 }
2286 }
2287
Sean Callanan686ed8d2010-01-19 20:22:31 +00002288 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002289 return false;
2290}
2291
Jim Grosbach4b905842013-09-20 23:08:21 +00002292/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002293/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002294bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002295 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002296 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002297
2298 for (;;) {
2299 // We don't truly support arithmetic on floating point expressions, so we
2300 // have to manually parse unary prefixes.
2301 bool IsNeg = false;
2302 if (getLexer().is(AsmToken::Minus)) {
2303 Lex();
2304 IsNeg = true;
2305 } else if (getLexer().is(AsmToken::Plus))
2306 Lex();
2307
Michael J. Spencer530ce852010-10-09 11:00:50 +00002308 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002309 getLexer().isNot(AsmToken::Real) &&
2310 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002311 return TokError("unexpected token in directive");
2312
2313 // Convert to an APFloat.
2314 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002315 StringRef IDVal = getTok().getString();
2316 if (getLexer().is(AsmToken::Identifier)) {
2317 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2318 Value = APFloat::getInf(Semantics);
2319 else if (!IDVal.compare_lower("nan"))
2320 Value = APFloat::getNaN(Semantics, false, ~0);
2321 else
2322 return TokError("invalid floating point literal");
2323 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002324 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002325 return TokError("invalid floating point literal");
2326 if (IsNeg)
2327 Value.changeSign();
2328
2329 // Consume the numeric token.
2330 Lex();
2331
2332 // Emit the value as an integer.
2333 APInt AsInt = Value.bitcastToAPInt();
2334 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002335 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002336
2337 if (getLexer().is(AsmToken::EndOfStatement))
2338 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002339
Daniel Dunbar2af16532010-09-24 01:59:56 +00002340 if (getLexer().isNot(AsmToken::Comma))
2341 return TokError("unexpected token in directive");
2342 Lex();
2343 }
2344 }
2345
2346 Lex();
2347 return false;
2348}
2349
Jim Grosbach4b905842013-09-20 23:08:21 +00002350/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002351/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002352bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002353 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002354
2355 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002356 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002357 return true;
2358
Rafael Espindolab91bac62010-10-05 19:42:57 +00002359 int64_t Val = 0;
2360 if (getLexer().is(AsmToken::Comma)) {
2361 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002362 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002363 return true;
2364 }
2365
Rafael Espindola922e3f42010-09-16 15:03:59 +00002366 if (getLexer().isNot(AsmToken::EndOfStatement))
2367 return TokError("unexpected token in '.zero' directive");
2368
2369 Lex();
2370
Rafael Espindola64e1af82013-07-02 15:49:13 +00002371 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002372
2373 return false;
2374}
2375
Jim Grosbach4b905842013-09-20 23:08:21 +00002376/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002377/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002378bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002379 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002380
Daniel Dunbara10e5192009-06-24 23:30:00 +00002381 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002382 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002383 return true;
2384
Roman Divackye33098f2013-09-24 17:44:41 +00002385 int64_t FillSize = 1;
2386 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002387
Roman Divackye33098f2013-09-24 17:44:41 +00002388 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2389 if (getLexer().isNot(AsmToken::Comma))
2390 return TokError("unexpected token in '.fill' directive");
2391 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002392
Roman Divackye33098f2013-09-24 17:44:41 +00002393 if (parseAbsoluteExpression(FillSize))
2394 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002395
Roman Divackye33098f2013-09-24 17:44:41 +00002396 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2397 if (getLexer().isNot(AsmToken::Comma))
2398 return TokError("unexpected token in '.fill' directive");
2399 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002400
Roman Divackye33098f2013-09-24 17:44:41 +00002401 if (parseAbsoluteExpression(FillExpr))
2402 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002403
Roman Divackye33098f2013-09-24 17:44:41 +00002404 if (getLexer().isNot(AsmToken::EndOfStatement))
2405 return TokError("unexpected token in '.fill' directive");
2406
2407 Lex();
2408 }
2409 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002410
Daniel Dunbar8e5edd82009-08-21 15:43:35 +00002411 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2412 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara10e5192009-06-24 23:30:00 +00002413
2414 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002415 getStreamer().EmitIntValue(FillExpr, FillSize);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002416
2417 return false;
2418}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002419
Jim Grosbach4b905842013-09-20 23:08:21 +00002420/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002421/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002422bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002423 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002424
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002425 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002426 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002427 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002428 return true;
2429
2430 // Parse optional fill expression.
2431 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002432 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2433 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002434 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002435 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002436
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002437 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002438 return true;
2439
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002440 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002441 return TokError("unexpected token in '.org' directive");
2442 }
2443
Sean Callanan686ed8d2010-01-19 20:22:31 +00002444 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002445
Jim Grosbachb5912772012-01-27 00:37:08 +00002446 // Only limited forms of relocatable expressions are accepted here, it
2447 // has to be relative to the current section. The streamer will return
2448 // 'true' if the expression wasn't evaluatable.
2449 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2450 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002451
2452 return false;
2453}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002454
Jim Grosbach4b905842013-09-20 23:08:21 +00002455/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002456/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002457bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002458 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002459
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002460 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002461 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002462 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002463 return true;
2464
2465 SMLoc MaxBytesLoc;
2466 bool HasFillExpr = false;
2467 int64_t FillExpr = 0;
2468 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002469 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2470 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002471 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002472 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002473
2474 // The fill expression can be omitted while specifying a maximum number of
2475 // alignment bytes, e.g:
2476 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002477 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002478 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002479 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002480 return true;
2481 }
2482
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002483 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2484 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002485 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002486 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002487
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002488 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002489 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002490 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002491
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002492 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002493 return TokError("unexpected token in directive");
2494 }
2495 }
2496
Sean Callanan686ed8d2010-01-19 20:22:31 +00002497 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002498
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002499 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002500 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002501
2502 // Compute alignment in bytes.
2503 if (IsPow2) {
2504 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002505 if (Alignment >= 32) {
2506 Error(AlignmentLoc, "invalid alignment value");
2507 Alignment = 31;
2508 }
2509
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002510 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002511 } else {
2512 // Reject alignments that aren't a power of two, for gas compatibility.
2513 if (!isPowerOf2_64(Alignment))
2514 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002515 }
2516
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002517 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002518 if (MaxBytesLoc.isValid()) {
2519 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002520 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002521 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002522 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002523 }
2524
2525 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002526 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002527 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002528 MaxBytesToFill = 0;
2529 }
2530 }
2531
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002532 // Check whether we should use optimal code alignment for this .align
2533 // directive.
Peter Collingbourne2f495b92013-04-17 21:18:16 +00002534 bool UseCodeAlign = getStreamer().getCurrentSection().first->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002535 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2536 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002537 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002538 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002539 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002540 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2541 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002542 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002543
2544 return false;
2545}
2546
Jim Grosbach4b905842013-09-20 23:08:21 +00002547/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002548/// ::= .file [number] filename
2549/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002550bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002551 // FIXME: I'm not sure what this is.
2552 int64_t FileNumber = -1;
2553 SMLoc FileNumberLoc = getLexer().getLoc();
2554 if (getLexer().is(AsmToken::Integer)) {
2555 FileNumber = getTok().getIntVal();
2556 Lex();
2557
2558 if (FileNumber < 1)
2559 return TokError("file number less than one");
2560 }
2561
2562 if (getLexer().isNot(AsmToken::String))
2563 return TokError("unexpected token in '.file' directive");
2564
2565 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002566 // Allow the strings to have escaped octal character sequence.
2567 std::string Path = getTok().getString();
2568 if (parseEscapedString(Path))
2569 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002570 Lex();
2571
2572 StringRef Directory;
2573 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002574 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002575 if (getLexer().is(AsmToken::String)) {
2576 if (FileNumber == -1)
2577 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002578 if (parseEscapedString(FilenameData))
2579 return true;
2580 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002581 Directory = Path;
2582 Lex();
2583 } else {
2584 Filename = Path;
2585 }
2586
2587 if (getLexer().isNot(AsmToken::EndOfStatement))
2588 return TokError("unexpected token in '.file' directive");
2589
2590 if (FileNumber == -1)
2591 getStreamer().EmitFileDirective(Filename);
2592 else {
2593 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002594 Error(DirectiveLoc,
2595 "input can't have .file dwarf directives when -g is "
2596 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002597
2598 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
2599 Error(FileNumberLoc, "file number already allocated");
2600 }
2601
2602 return false;
2603}
2604
Jim Grosbach4b905842013-09-20 23:08:21 +00002605/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002606/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002607bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002608 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2609 if (getLexer().isNot(AsmToken::Integer))
2610 return TokError("unexpected token in '.line' directive");
2611
2612 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002613 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002614 Lex();
2615
2616 // FIXME: Do something with the .line.
2617 }
2618
2619 if (getLexer().isNot(AsmToken::EndOfStatement))
2620 return TokError("unexpected token in '.line' directive");
2621
2622 return false;
2623}
2624
Jim Grosbach4b905842013-09-20 23:08:21 +00002625/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002626/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2627/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2628/// The first number is a file number, must have been previously assigned with
2629/// a .file directive, the second number is the line number and optionally the
2630/// third number is a column position (zero if not specified). The remaining
2631/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002632bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002633 if (getLexer().isNot(AsmToken::Integer))
2634 return TokError("unexpected token in '.loc' directive");
2635 int64_t FileNumber = getTok().getIntVal();
2636 if (FileNumber < 1)
2637 return TokError("file number less than one in '.loc' directive");
2638 if (!getContext().isValidDwarfFileNumber(FileNumber))
2639 return TokError("unassigned file number in '.loc' directive");
2640 Lex();
2641
2642 int64_t LineNumber = 0;
2643 if (getLexer().is(AsmToken::Integer)) {
2644 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002645 if (LineNumber < 0)
2646 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002647 Lex();
2648 }
2649
2650 int64_t ColumnPos = 0;
2651 if (getLexer().is(AsmToken::Integer)) {
2652 ColumnPos = getTok().getIntVal();
2653 if (ColumnPos < 0)
2654 return TokError("column position less than zero in '.loc' directive");
2655 Lex();
2656 }
2657
2658 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2659 unsigned Isa = 0;
2660 int64_t Discriminator = 0;
2661 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2662 for (;;) {
2663 if (getLexer().is(AsmToken::EndOfStatement))
2664 break;
2665
2666 StringRef Name;
2667 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002668 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002669 return TokError("unexpected token in '.loc' directive");
2670
2671 if (Name == "basic_block")
2672 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2673 else if (Name == "prologue_end")
2674 Flags |= DWARF2_FLAG_PROLOGUE_END;
2675 else if (Name == "epilogue_begin")
2676 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2677 else if (Name == "is_stmt") {
2678 Loc = getTok().getLoc();
2679 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002680 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002681 return true;
2682 // The expression must be the constant 0 or 1.
2683 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2684 int Value = MCE->getValue();
2685 if (Value == 0)
2686 Flags &= ~DWARF2_FLAG_IS_STMT;
2687 else if (Value == 1)
2688 Flags |= DWARF2_FLAG_IS_STMT;
2689 else
2690 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002691 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002692 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2693 }
Craig Topperf15655b2013-04-22 04:22:40 +00002694 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002695 Loc = getTok().getLoc();
2696 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002697 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002698 return true;
2699 // The expression must be a constant greater or equal to 0.
2700 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2701 int Value = MCE->getValue();
2702 if (Value < 0)
2703 return Error(Loc, "isa number less than zero");
2704 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002705 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002706 return Error(Loc, "isa number not a constant value");
2707 }
Craig Topperf15655b2013-04-22 04:22:40 +00002708 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002709 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002710 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002711 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002712 return Error(Loc, "unknown sub-directive in '.loc' directive");
2713 }
2714
2715 if (getLexer().is(AsmToken::EndOfStatement))
2716 break;
2717 }
2718 }
2719
2720 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2721 Isa, Discriminator, StringRef());
2722
2723 return false;
2724}
2725
Jim Grosbach4b905842013-09-20 23:08:21 +00002726/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002727/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002728bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002729 return TokError("unsupported directive '.stabs'");
2730}
2731
Jim Grosbach4b905842013-09-20 23:08:21 +00002732/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002733/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002734bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002735 StringRef Name;
2736 bool EH = false;
2737 bool Debug = false;
2738
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002739 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002740 return TokError("Expected an identifier");
2741
2742 if (Name == ".eh_frame")
2743 EH = true;
2744 else if (Name == ".debug_frame")
2745 Debug = true;
2746
2747 if (getLexer().is(AsmToken::Comma)) {
2748 Lex();
2749
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002750 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002751 return TokError("Expected an identifier");
2752
2753 if (Name == ".eh_frame")
2754 EH = true;
2755 else if (Name == ".debug_frame")
2756 Debug = true;
2757 }
2758
2759 getStreamer().EmitCFISections(EH, Debug);
2760 return false;
2761}
2762
Jim Grosbach4b905842013-09-20 23:08:21 +00002763/// parseDirectiveCFIStartProc
Eli Bendersky17233942013-01-15 22:59:42 +00002764/// ::= .cfi_startproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002765bool AsmParser::parseDirectiveCFIStartProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002766 getStreamer().EmitCFIStartProc();
2767 return false;
2768}
2769
Jim Grosbach4b905842013-09-20 23:08:21 +00002770/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002771/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002772bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002773 getStreamer().EmitCFIEndProc();
2774 return false;
2775}
2776
Jim Grosbach4b905842013-09-20 23:08:21 +00002777/// \brief parse register name or number.
2778bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002779 SMLoc DirectiveLoc) {
2780 unsigned RegNo;
2781
2782 if (getLexer().isNot(AsmToken::Integer)) {
2783 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2784 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002785 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002786 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002787 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002788
2789 return false;
2790}
2791
Jim Grosbach4b905842013-09-20 23:08:21 +00002792/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002793/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002794bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002795 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002796 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002797 return true;
2798
2799 if (getLexer().isNot(AsmToken::Comma))
2800 return TokError("unexpected token in directive");
2801 Lex();
2802
2803 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002804 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002805 return true;
2806
2807 getStreamer().EmitCFIDefCfa(Register, Offset);
2808 return false;
2809}
2810
Jim Grosbach4b905842013-09-20 23:08:21 +00002811/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002812/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002813bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002814 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002815 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002816 return true;
2817
2818 getStreamer().EmitCFIDefCfaOffset(Offset);
2819 return false;
2820}
2821
Jim Grosbach4b905842013-09-20 23:08:21 +00002822/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002823/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00002824bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002825 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002826 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002827 return true;
2828
2829 if (getLexer().isNot(AsmToken::Comma))
2830 return TokError("unexpected token in directive");
2831 Lex();
2832
2833 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002834 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002835 return true;
2836
2837 getStreamer().EmitCFIRegister(Register1, Register2);
2838 return false;
2839}
2840
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00002841/// parseDirectiveCFIWindowSave
2842/// ::= .cfi_window_save
2843bool AsmParser::parseDirectiveCFIWindowSave() {
2844 getStreamer().EmitCFIWindowSave();
2845 return false;
2846}
2847
Jim Grosbach4b905842013-09-20 23:08:21 +00002848/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002849/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00002850bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002851 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002852 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00002853 return true;
2854
2855 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2856 return false;
2857}
2858
Jim Grosbach4b905842013-09-20 23:08:21 +00002859/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002860/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00002861bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002862 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002863 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002864 return true;
2865
2866 getStreamer().EmitCFIDefCfaRegister(Register);
2867 return false;
2868}
2869
Jim Grosbach4b905842013-09-20 23:08:21 +00002870/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002871/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002872bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002873 int64_t Register = 0;
2874 int64_t Offset = 0;
2875
Jim Grosbach4b905842013-09-20 23:08:21 +00002876 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002877 return true;
2878
2879 if (getLexer().isNot(AsmToken::Comma))
2880 return TokError("unexpected token in directive");
2881 Lex();
2882
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002883 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002884 return true;
2885
2886 getStreamer().EmitCFIOffset(Register, Offset);
2887 return false;
2888}
2889
Jim Grosbach4b905842013-09-20 23:08:21 +00002890/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002891/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002892bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002893 int64_t Register = 0;
2894
Jim Grosbach4b905842013-09-20 23:08:21 +00002895 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002896 return true;
2897
2898 if (getLexer().isNot(AsmToken::Comma))
2899 return TokError("unexpected token in directive");
2900 Lex();
2901
2902 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002903 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002904 return true;
2905
2906 getStreamer().EmitCFIRelOffset(Register, Offset);
2907 return false;
2908}
2909
2910static bool isValidEncoding(int64_t Encoding) {
2911 if (Encoding & ~0xff)
2912 return false;
2913
2914 if (Encoding == dwarf::DW_EH_PE_omit)
2915 return true;
2916
2917 const unsigned Format = Encoding & 0xf;
2918 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2919 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2920 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2921 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2922 return false;
2923
2924 const unsigned Application = Encoding & 0x70;
2925 if (Application != dwarf::DW_EH_PE_absptr &&
2926 Application != dwarf::DW_EH_PE_pcrel)
2927 return false;
2928
2929 return true;
2930}
2931
Jim Grosbach4b905842013-09-20 23:08:21 +00002932/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00002933/// IsPersonality true for cfi_personality, false for cfi_lsda
2934/// ::= .cfi_personality encoding, [symbol_name]
2935/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00002936bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00002937 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002938 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00002939 return true;
2940 if (Encoding == dwarf::DW_EH_PE_omit)
2941 return false;
2942
2943 if (!isValidEncoding(Encoding))
2944 return TokError("unsupported encoding.");
2945
2946 if (getLexer().isNot(AsmToken::Comma))
2947 return TokError("unexpected token in directive");
2948 Lex();
2949
2950 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002951 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002952 return TokError("expected identifier in directive");
2953
2954 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2955
2956 if (IsPersonality)
2957 getStreamer().EmitCFIPersonality(Sym, Encoding);
2958 else
2959 getStreamer().EmitCFILsda(Sym, Encoding);
2960 return false;
2961}
2962
Jim Grosbach4b905842013-09-20 23:08:21 +00002963/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00002964/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00002965bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00002966 getStreamer().EmitCFIRememberState();
2967 return false;
2968}
2969
Jim Grosbach4b905842013-09-20 23:08:21 +00002970/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00002971/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00002972bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00002973 getStreamer().EmitCFIRestoreState();
2974 return false;
2975}
2976
Jim Grosbach4b905842013-09-20 23:08:21 +00002977/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00002978/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00002979bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002980 int64_t Register = 0;
2981
Jim Grosbach4b905842013-09-20 23:08:21 +00002982 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002983 return true;
2984
2985 getStreamer().EmitCFISameValue(Register);
2986 return false;
2987}
2988
Jim Grosbach4b905842013-09-20 23:08:21 +00002989/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00002990/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00002991bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002992 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002993 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002994 return true;
2995
2996 getStreamer().EmitCFIRestore(Register);
2997 return false;
2998}
2999
Jim Grosbach4b905842013-09-20 23:08:21 +00003000/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003001/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003002bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003003 std::string Values;
3004 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003005 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003006 return true;
3007
3008 Values.push_back((uint8_t)CurrValue);
3009
3010 while (getLexer().is(AsmToken::Comma)) {
3011 Lex();
3012
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003013 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003014 return true;
3015
3016 Values.push_back((uint8_t)CurrValue);
3017 }
3018
3019 getStreamer().EmitCFIEscape(Values);
3020 return false;
3021}
3022
Jim Grosbach4b905842013-09-20 23:08:21 +00003023/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003024/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003025bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003026 if (getLexer().isNot(AsmToken::EndOfStatement))
3027 return Error(getLexer().getLoc(),
3028 "unexpected token in '.cfi_signal_frame'");
3029
3030 getStreamer().EmitCFISignalFrame();
3031 return false;
3032}
3033
Jim Grosbach4b905842013-09-20 23:08:21 +00003034/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003035/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003036bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003037 int64_t Register = 0;
3038
Jim Grosbach4b905842013-09-20 23:08:21 +00003039 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003040 return true;
3041
3042 getStreamer().EmitCFIUndefined(Register);
3043 return false;
3044}
3045
Jim Grosbach4b905842013-09-20 23:08:21 +00003046/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003047/// ::= .macros_on
3048/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003049bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003050 if (getLexer().isNot(AsmToken::EndOfStatement))
3051 return Error(getLexer().getLoc(),
3052 "unexpected token in '" + Directive + "' directive");
3053
Jim Grosbach4b905842013-09-20 23:08:21 +00003054 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003055 return false;
3056}
3057
Jim Grosbach4b905842013-09-20 23:08:21 +00003058/// parseDirectiveMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003059/// ::= .macro name [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003060bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003061 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003062 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003063 return TokError("expected identifier in '.macro' directive");
3064
3065 MCAsmMacroParameters Parameters;
3066 // Argument delimiter is initially unknown. It will be set by
Jim Grosbach4b905842013-09-20 23:08:21 +00003067 // parseMacroArgument()
Eli Bendersky17233942013-01-15 22:59:42 +00003068 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
3069 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3070 for (;;) {
3071 MCAsmMacroParameter Parameter;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003072 if (parseIdentifier(Parameter.first))
Eli Bendersky17233942013-01-15 22:59:42 +00003073 return TokError("expected identifier in '.macro' directive");
3074
3075 if (getLexer().is(AsmToken::Equal)) {
3076 Lex();
Jim Grosbach4b905842013-09-20 23:08:21 +00003077 if (parseMacroArgument(Parameter.second, ArgumentDelimiter))
Eli Bendersky17233942013-01-15 22:59:42 +00003078 return true;
3079 }
3080
3081 Parameters.push_back(Parameter);
3082
3083 if (getLexer().is(AsmToken::Comma))
3084 Lex();
3085 else if (getLexer().is(AsmToken::EndOfStatement))
3086 break;
3087 }
3088 }
3089
3090 // Eat the end of statement.
3091 Lex();
3092
3093 AsmToken EndToken, StartToken = getTok();
3094
3095 // Lex the macro definition.
3096 for (;;) {
3097 // Check whether we have reached the end of the file.
3098 if (getLexer().is(AsmToken::Eof))
3099 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3100
3101 // Otherwise, check whether we have reach the .endmacro.
3102 if (getLexer().is(AsmToken::Identifier) &&
3103 (getTok().getIdentifier() == ".endm" ||
3104 getTok().getIdentifier() == ".endmacro")) {
3105 EndToken = getTok();
3106 Lex();
3107 if (getLexer().isNot(AsmToken::EndOfStatement))
3108 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3109 "' directive");
3110 break;
3111 }
3112
3113 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003114 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003115 }
3116
Jim Grosbach4b905842013-09-20 23:08:21 +00003117 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003118 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3119 }
3120
3121 const char *BodyStart = StartToken.getLoc().getPointer();
3122 const char *BodyEnd = EndToken.getLoc().getPointer();
3123 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003124 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3125 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003126 return false;
3127}
3128
Jim Grosbach4b905842013-09-20 23:08:21 +00003129/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003130///
3131/// With the support added for named parameters there may be code out there that
3132/// is transitioning from positional parameters. In versions of gas that did
3133/// not support named parameters they would be ignored on the macro defintion.
3134/// But to support both styles of parameters this is not possible so if a macro
3135/// defintion has named parameters but does not use them and has what appears
3136/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3137/// warning that the positional parameter found in body which have no effect.
3138/// Hoping the developer will either remove the named parameters from the macro
3139/// definiton so the positional parameters get used if that was what was
3140/// intended or change the macro to use the named parameters. It is possible
3141/// this warning will trigger when the none of the named parameters are used
3142/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003143void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003144 StringRef Body,
3145 MCAsmMacroParameters Parameters) {
3146 // If this macro is not defined with named parameters the warning we are
3147 // checking for here doesn't apply.
3148 unsigned NParameters = Parameters.size();
3149 if (NParameters == 0)
3150 return;
3151
3152 bool NamedParametersFound = false;
3153 bool PositionalParametersFound = false;
3154
3155 // Look at the body of the macro for use of both the named parameters and what
3156 // are likely to be positional parameters. This is what expandMacro() is
3157 // doing when it finds the parameters in the body.
3158 while (!Body.empty()) {
3159 // Scan for the next possible parameter.
3160 std::size_t End = Body.size(), Pos = 0;
3161 for (; Pos != End; ++Pos) {
3162 // Check for a substitution or escape.
3163 // This macro is defined with parameters, look for \foo, \bar, etc.
3164 if (Body[Pos] == '\\' && Pos + 1 != End)
3165 break;
3166
3167 // This macro should have parameters, but look for $0, $1, ..., $n too.
3168 if (Body[Pos] != '$' || Pos + 1 == End)
3169 continue;
3170 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003171 if (Next == '$' || Next == 'n' ||
3172 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003173 break;
3174 }
3175
3176 // Check if we reached the end.
3177 if (Pos == End)
3178 break;
3179
3180 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003181 switch (Body[Pos + 1]) {
3182 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003183 case '$':
3184 break;
3185
Jim Grosbach4b905842013-09-20 23:08:21 +00003186 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003187 case 'n':
3188 PositionalParametersFound = true;
3189 break;
3190
Jim Grosbach4b905842013-09-20 23:08:21 +00003191 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003192 default: {
3193 PositionalParametersFound = true;
3194 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003195 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003196 }
3197 Pos += 2;
3198 } else {
3199 unsigned I = Pos + 1;
3200 while (isIdentifierChar(Body[I]) && I + 1 != End)
3201 ++I;
3202
Jim Grosbach4b905842013-09-20 23:08:21 +00003203 const char *Begin = Body.data() + Pos + 1;
3204 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003205 unsigned Index = 0;
3206 for (; Index < NParameters; ++Index)
3207 if (Parameters[Index].first == Argument)
3208 break;
3209
3210 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003211 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3212 Pos += 3;
3213 else {
3214 Pos = I;
3215 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003216 } else {
3217 NamedParametersFound = true;
3218 Pos += 1 + Argument.size();
3219 }
3220 }
3221 // Update the scan point.
3222 Body = Body.substr(Pos);
3223 }
3224
3225 if (!NamedParametersFound && PositionalParametersFound)
3226 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3227 "used in macro body, possible positional parameter "
3228 "found in body which will have no effect");
3229}
3230
Jim Grosbach4b905842013-09-20 23:08:21 +00003231/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003232/// ::= .endm
3233/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003234bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003235 if (getLexer().isNot(AsmToken::EndOfStatement))
3236 return TokError("unexpected token in '" + Directive + "' directive");
3237
3238 // If we are inside a macro instantiation, terminate the current
3239 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003240 if (isInsideMacroInstantiation()) {
3241 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003242 return false;
3243 }
3244
3245 // Otherwise, this .endmacro is a stray entry in the file; well formed
3246 // .endmacro directives are handled during the macro definition parsing.
3247 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003248 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003249}
3250
Jim Grosbach4b905842013-09-20 23:08:21 +00003251/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003252/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003253bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003254 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003255 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003256 return TokError("expected identifier in '.purgem' directive");
3257
3258 if (getLexer().isNot(AsmToken::EndOfStatement))
3259 return TokError("unexpected token in '.purgem' directive");
3260
Jim Grosbach4b905842013-09-20 23:08:21 +00003261 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003262 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3263
Jim Grosbach4b905842013-09-20 23:08:21 +00003264 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003265 return false;
3266}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003267
Jim Grosbach4b905842013-09-20 23:08:21 +00003268/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003269/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003270bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003271 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003272
3273 // Expect a single argument: an expression that evaluates to a constant
3274 // in the inclusive range 0-30.
3275 SMLoc ExprLoc = getLexer().getLoc();
3276 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003277 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003278 return true;
3279 else if (getLexer().isNot(AsmToken::EndOfStatement))
3280 return TokError("unexpected token after expression in"
3281 " '.bundle_align_mode' directive");
3282 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3283 return Error(ExprLoc,
3284 "invalid bundle alignment size (expected between 0 and 30)");
3285
3286 Lex();
3287
3288 // Because of AlignSizePow2's verified range we can safely truncate it to
3289 // unsigned.
3290 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3291 return false;
3292}
3293
Jim Grosbach4b905842013-09-20 23:08:21 +00003294/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003295/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003296bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003297 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003298 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003299
Eli Bendersky802b6282013-01-07 21:51:08 +00003300 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3301 StringRef Option;
3302 SMLoc Loc = getTok().getLoc();
3303 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003304 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003305
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003306 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003307 return Error(Loc, kInvalidOptionError);
3308
3309 if (Option != "align_to_end")
3310 return Error(Loc, kInvalidOptionError);
3311 else if (getLexer().isNot(AsmToken::EndOfStatement))
3312 return Error(Loc,
3313 "unexpected token after '.bundle_lock' directive option");
3314 AlignToEnd = true;
3315 }
3316
Eli Benderskyf483ff92012-12-20 19:05:53 +00003317 Lex();
3318
Eli Bendersky802b6282013-01-07 21:51:08 +00003319 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003320 return false;
3321}
3322
Jim Grosbach4b905842013-09-20 23:08:21 +00003323/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003324/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003325bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003326 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003327
3328 if (getLexer().isNot(AsmToken::EndOfStatement))
3329 return TokError("unexpected token in '.bundle_unlock' directive");
3330 Lex();
3331
3332 getStreamer().EmitBundleUnlock();
3333 return false;
3334}
3335
Jim Grosbach4b905842013-09-20 23:08:21 +00003336/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003337/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003338bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003339 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003340
3341 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003342 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003343 return true;
3344
3345 int64_t FillExpr = 0;
3346 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3347 if (getLexer().isNot(AsmToken::Comma))
3348 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3349 Lex();
3350
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003351 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003352 return true;
3353
3354 if (getLexer().isNot(AsmToken::EndOfStatement))
3355 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3356 }
3357
3358 Lex();
3359
3360 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003361 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3362 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003363
3364 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003365 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003366
3367 return false;
3368}
3369
Jim Grosbach4b905842013-09-20 23:08:21 +00003370/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003371/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003372bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003373 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003374 const MCExpr *Value;
3375
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003376 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003377 return true;
3378
3379 if (getLexer().isNot(AsmToken::EndOfStatement))
3380 return TokError("unexpected token in directive");
3381
3382 if (Signed)
3383 getStreamer().EmitSLEB128Value(Value);
3384 else
3385 getStreamer().EmitULEB128Value(Value);
3386
3387 return false;
3388}
3389
Jim Grosbach4b905842013-09-20 23:08:21 +00003390/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003391/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003392bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003393 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003394 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003395 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003396 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003397
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003398 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003399 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003400
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003401 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003402
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003403 // Assembler local symbols don't make any sense here. Complain loudly.
3404 if (Sym->isTemporary())
3405 return Error(Loc, "non-local symbol required in directive");
3406
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003407 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3408 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003409
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003410 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003411 break;
3412
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003413 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003414 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003415 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003416 }
3417 }
3418
Sean Callanan686ed8d2010-01-19 20:22:31 +00003419 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003420 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003421}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003422
Jim Grosbach4b905842013-09-20 23:08:21 +00003423/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003424/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003425bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003426 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003427
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003428 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003429 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003430 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003431 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003432
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003433 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003434 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003435
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003436 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003437 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003438 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003439
3440 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003441 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003442 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003443 return true;
3444
3445 int64_t Pow2Alignment = 0;
3446 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003447 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003448 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003449 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003450 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003451 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003452
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003453 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3454 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003455 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3456
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003457 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003458 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3459 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003460 if (!isPowerOf2_64(Pow2Alignment))
3461 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3462 Pow2Alignment = Log2_64(Pow2Alignment);
3463 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003464 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003465
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003466 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003467 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003468
Sean Callanan686ed8d2010-01-19 20:22:31 +00003469 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003470
Chris Lattner28ad7542009-07-09 17:25:12 +00003471 // NOTE: a size of zero for a .comm should create a undefined symbol
3472 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003473 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003474 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003475 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003476
Eric Christopherbc818852010-05-14 01:38:54 +00003477 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003478 // may internally end up wanting an alignment in bytes.
3479 // FIXME: Diagnose overflow.
3480 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003481 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003482 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003483
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003484 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003485 return Error(IDLoc, "invalid symbol redefinition");
3486
Chris Lattner28ad7542009-07-09 17:25:12 +00003487 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003488 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003489 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003490 return false;
3491 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003492
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003493 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003494 return false;
3495}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003496
Jim Grosbach4b905842013-09-20 23:08:21 +00003497/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003498/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003499bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003500 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003501 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003502
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003503 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003504 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003505 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003506
Sean Callanan686ed8d2010-01-19 20:22:31 +00003507 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003508
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003509 if (Str.empty())
3510 Error(Loc, ".abort detected. Assembly stopping.");
3511 else
3512 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003513 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003514
3515 return false;
3516}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003517
Jim Grosbach4b905842013-09-20 23:08:21 +00003518/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003519/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003520bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003521 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003522 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003523
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003524 // Allow the strings to have escaped octal character sequence.
3525 std::string Filename;
3526 if (parseEscapedString(Filename))
3527 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003528 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003529 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003530
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003531 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003532 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003533
Chris Lattner693fbb82009-07-16 06:14:39 +00003534 // Attempt to switch the lexer to the included file before consuming the end
3535 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003536 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003537 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003538 return true;
3539 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003540
3541 return false;
3542}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003543
Jim Grosbach4b905842013-09-20 23:08:21 +00003544/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003545/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003546bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003547 if (getLexer().isNot(AsmToken::String))
3548 return TokError("expected string in '.incbin' directive");
3549
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003550 // Allow the strings to have escaped octal character sequence.
3551 std::string Filename;
3552 if (parseEscapedString(Filename))
3553 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003554 SMLoc IncbinLoc = getLexer().getLoc();
3555 Lex();
3556
3557 if (getLexer().isNot(AsmToken::EndOfStatement))
3558 return TokError("unexpected token in '.incbin' directive");
3559
Kevin Enderby109f25c2011-12-14 21:47:48 +00003560 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003561 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003562 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3563 return true;
3564 }
3565
3566 return false;
3567}
3568
Jim Grosbach4b905842013-09-20 23:08:21 +00003569/// parseDirectiveIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003570/// ::= .if expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003571bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003572 TheCondStack.push_back(TheCondState);
3573 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003574 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003575 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003576 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003577 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003578 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003579 return true;
3580
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003581 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003582 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003583
Sean Callanan686ed8d2010-01-19 20:22:31 +00003584 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003585
3586 TheCondState.CondMet = ExprValue;
3587 TheCondState.Ignore = !TheCondState.CondMet;
3588 }
3589
3590 return false;
3591}
3592
Jim Grosbach4b905842013-09-20 23:08:21 +00003593/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003594/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003595bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003596 TheCondStack.push_back(TheCondState);
3597 TheCondState.TheCond = AsmCond::IfCond;
3598
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003599 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003600 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003601 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003602 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003603
3604 if (getLexer().isNot(AsmToken::EndOfStatement))
3605 return TokError("unexpected token in '.ifb' directive");
3606
3607 Lex();
3608
3609 TheCondState.CondMet = ExpectBlank == Str.empty();
3610 TheCondState.Ignore = !TheCondState.CondMet;
3611 }
3612
3613 return false;
3614}
3615
Jim Grosbach4b905842013-09-20 23:08:21 +00003616/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003617/// ::= .ifc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003618bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003619 TheCondStack.push_back(TheCondState);
3620 TheCondState.TheCond = AsmCond::IfCond;
3621
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003622 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003623 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003624 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003625 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003626
3627 if (getLexer().isNot(AsmToken::Comma))
3628 return TokError("unexpected token in '.ifc' directive");
3629
3630 Lex();
3631
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003632 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003633
3634 if (getLexer().isNot(AsmToken::EndOfStatement))
3635 return TokError("unexpected token in '.ifc' directive");
3636
3637 Lex();
3638
3639 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
3640 TheCondState.Ignore = !TheCondState.CondMet;
3641 }
3642
3643 return false;
3644}
3645
Jim Grosbach4b905842013-09-20 23:08:21 +00003646/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003647/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003648bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003649 StringRef Name;
3650 TheCondStack.push_back(TheCondState);
3651 TheCondState.TheCond = AsmCond::IfCond;
3652
3653 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003654 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003655 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003656 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003657 return TokError("expected identifier after '.ifdef'");
3658
3659 Lex();
3660
3661 MCSymbol *Sym = getContext().LookupSymbol(Name);
3662
3663 if (expect_defined)
3664 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
3665 else
3666 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
3667 TheCondState.Ignore = !TheCondState.CondMet;
3668 }
3669
3670 return false;
3671}
3672
Jim Grosbach4b905842013-09-20 23:08:21 +00003673/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003674/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003675bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003676 if (TheCondState.TheCond != AsmCond::IfCond &&
3677 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003678 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3679 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003680 TheCondState.TheCond = AsmCond::ElseIfCond;
3681
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003682 bool LastIgnoreState = false;
3683 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00003684 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003685 if (LastIgnoreState || TheCondState.CondMet) {
3686 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003687 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00003688 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003689 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003690 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003691 return true;
3692
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003693 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003694 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003695
Sean Callanan686ed8d2010-01-19 20:22:31 +00003696 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003697 TheCondState.CondMet = ExprValue;
3698 TheCondState.Ignore = !TheCondState.CondMet;
3699 }
3700
3701 return false;
3702}
3703
Jim Grosbach4b905842013-09-20 23:08:21 +00003704/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003705/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00003706bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003707 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003708 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003709
Sean Callanan686ed8d2010-01-19 20:22:31 +00003710 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003711
3712 if (TheCondState.TheCond != AsmCond::IfCond &&
3713 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003714 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
3715 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003716 TheCondState.TheCond = AsmCond::ElseCond;
3717 bool LastIgnoreState = false;
3718 if (!TheCondStack.empty())
3719 LastIgnoreState = TheCondStack.back().Ignore;
3720 if (LastIgnoreState || TheCondState.CondMet)
3721 TheCondState.Ignore = true;
3722 else
3723 TheCondState.Ignore = false;
3724
3725 return false;
3726}
3727
Jim Grosbach4b905842013-09-20 23:08:21 +00003728/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003729/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00003730bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003731 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003732 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003733
Sean Callanan686ed8d2010-01-19 20:22:31 +00003734 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003735
Jim Grosbach4b905842013-09-20 23:08:21 +00003736 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003737 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
3738 ".else");
3739 if (!TheCondStack.empty()) {
3740 TheCondState = TheCondStack.back();
3741 TheCondStack.pop_back();
3742 }
3743
3744 return false;
3745}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00003746
Eli Bendersky17233942013-01-15 22:59:42 +00003747void AsmParser::initializeDirectiveKindMap() {
3748 DirectiveKindMap[".set"] = DK_SET;
3749 DirectiveKindMap[".equ"] = DK_EQU;
3750 DirectiveKindMap[".equiv"] = DK_EQUIV;
3751 DirectiveKindMap[".ascii"] = DK_ASCII;
3752 DirectiveKindMap[".asciz"] = DK_ASCIZ;
3753 DirectiveKindMap[".string"] = DK_STRING;
3754 DirectiveKindMap[".byte"] = DK_BYTE;
3755 DirectiveKindMap[".short"] = DK_SHORT;
3756 DirectiveKindMap[".value"] = DK_VALUE;
3757 DirectiveKindMap[".2byte"] = DK_2BYTE;
3758 DirectiveKindMap[".long"] = DK_LONG;
3759 DirectiveKindMap[".int"] = DK_INT;
3760 DirectiveKindMap[".4byte"] = DK_4BYTE;
3761 DirectiveKindMap[".quad"] = DK_QUAD;
3762 DirectiveKindMap[".8byte"] = DK_8BYTE;
3763 DirectiveKindMap[".single"] = DK_SINGLE;
3764 DirectiveKindMap[".float"] = DK_FLOAT;
3765 DirectiveKindMap[".double"] = DK_DOUBLE;
3766 DirectiveKindMap[".align"] = DK_ALIGN;
3767 DirectiveKindMap[".align32"] = DK_ALIGN32;
3768 DirectiveKindMap[".balign"] = DK_BALIGN;
3769 DirectiveKindMap[".balignw"] = DK_BALIGNW;
3770 DirectiveKindMap[".balignl"] = DK_BALIGNL;
3771 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
3772 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
3773 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
3774 DirectiveKindMap[".org"] = DK_ORG;
3775 DirectiveKindMap[".fill"] = DK_FILL;
3776 DirectiveKindMap[".zero"] = DK_ZERO;
3777 DirectiveKindMap[".extern"] = DK_EXTERN;
3778 DirectiveKindMap[".globl"] = DK_GLOBL;
3779 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00003780 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
3781 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
3782 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
3783 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
3784 DirectiveKindMap[".reference"] = DK_REFERENCE;
3785 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
3786 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
3787 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
3788 DirectiveKindMap[".comm"] = DK_COMM;
3789 DirectiveKindMap[".common"] = DK_COMMON;
3790 DirectiveKindMap[".lcomm"] = DK_LCOMM;
3791 DirectiveKindMap[".abort"] = DK_ABORT;
3792 DirectiveKindMap[".include"] = DK_INCLUDE;
3793 DirectiveKindMap[".incbin"] = DK_INCBIN;
3794 DirectiveKindMap[".code16"] = DK_CODE16;
3795 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
3796 DirectiveKindMap[".rept"] = DK_REPT;
3797 DirectiveKindMap[".irp"] = DK_IRP;
3798 DirectiveKindMap[".irpc"] = DK_IRPC;
3799 DirectiveKindMap[".endr"] = DK_ENDR;
3800 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
3801 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
3802 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
3803 DirectiveKindMap[".if"] = DK_IF;
3804 DirectiveKindMap[".ifb"] = DK_IFB;
3805 DirectiveKindMap[".ifnb"] = DK_IFNB;
3806 DirectiveKindMap[".ifc"] = DK_IFC;
3807 DirectiveKindMap[".ifnc"] = DK_IFNC;
3808 DirectiveKindMap[".ifdef"] = DK_IFDEF;
3809 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
3810 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
3811 DirectiveKindMap[".elseif"] = DK_ELSEIF;
3812 DirectiveKindMap[".else"] = DK_ELSE;
3813 DirectiveKindMap[".endif"] = DK_ENDIF;
3814 DirectiveKindMap[".skip"] = DK_SKIP;
3815 DirectiveKindMap[".space"] = DK_SPACE;
3816 DirectiveKindMap[".file"] = DK_FILE;
3817 DirectiveKindMap[".line"] = DK_LINE;
3818 DirectiveKindMap[".loc"] = DK_LOC;
3819 DirectiveKindMap[".stabs"] = DK_STABS;
3820 DirectiveKindMap[".sleb128"] = DK_SLEB128;
3821 DirectiveKindMap[".uleb128"] = DK_ULEB128;
3822 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
3823 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
3824 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
3825 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
3826 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
3827 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
3828 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
3829 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
3830 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
3831 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
3832 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
3833 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
3834 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
3835 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
3836 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
3837 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
3838 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
3839 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
3840 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003841 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00003842 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
3843 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
3844 DirectiveKindMap[".macro"] = DK_MACRO;
3845 DirectiveKindMap[".endm"] = DK_ENDM;
3846 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
3847 DirectiveKindMap[".purgem"] = DK_PURGEM;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00003848}
3849
Jim Grosbach4b905842013-09-20 23:08:21 +00003850MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003851 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003852
Rafael Espindola34b9c512012-06-03 23:57:14 +00003853 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003854 for (;;) {
3855 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00003856 if (getLexer().is(AsmToken::Eof)) {
3857 Error(DirectiveLoc, "no matching '.endr' in definition");
3858 return 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003859 }
3860
Rafael Espindola34b9c512012-06-03 23:57:14 +00003861 if (Lexer.is(AsmToken::Identifier) &&
3862 (getTok().getIdentifier() == ".rept")) {
3863 ++NestLevel;
3864 }
3865
3866 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00003867 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00003868 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003869 EndToken = getTok();
3870 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00003871 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3872 TokError("unexpected token in '.endr' directive");
3873 return 0;
3874 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003875 break;
3876 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00003877 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003878 }
3879
Rafael Espindola34b9c512012-06-03 23:57:14 +00003880 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003881 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003882 }
3883
3884 const char *BodyStart = StartToken.getLoc().getPointer();
3885 const char *BodyEnd = EndToken.getLoc().getPointer();
3886 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3887
Rafael Espindola34b9c512012-06-03 23:57:14 +00003888 // We Are Anonymous.
3889 StringRef Name;
Eli Bendersky38274122013-01-14 23:22:36 +00003890 MCAsmMacroParameters Parameters;
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00003891 MacroLikeBodies.push_back(MCAsmMacro(Name, Body, Parameters));
3892 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00003893}
3894
Jim Grosbach4b905842013-09-20 23:08:21 +00003895void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00003896 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003897 OS << ".endr\n";
3898
3899 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00003900 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003901
Rafael Espindola34b9c512012-06-03 23:57:14 +00003902 // Create the macro instantiation object and add to the current macro
3903 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00003904 MacroInstantiation *MI = new MacroInstantiation(
3905 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola34b9c512012-06-03 23:57:14 +00003906 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003907
Rafael Espindola34b9c512012-06-03 23:57:14 +00003908 // Jump to the macro instantiation and prime the lexer.
3909 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3910 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3911 Lex();
3912}
3913
Jim Grosbach4b905842013-09-20 23:08:21 +00003914bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00003915 int64_t Count;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003916 if (parseAbsoluteExpression(Count))
Rafael Espindola34b9c512012-06-03 23:57:14 +00003917 return TokError("unexpected token in '.rept' directive");
3918
3919 if (Count < 0)
3920 return TokError("Count is negative");
3921
3922 if (Lexer.isNot(AsmToken::EndOfStatement))
3923 return TokError("unexpected token in '.rept' directive");
3924
3925 // Eat the end of statement.
3926 Lex();
3927
3928 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00003929 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00003930 if (!M)
3931 return true;
3932
3933 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3934 // to hold the macro body with substitutions.
3935 SmallString<256> Buf;
Eli Bendersky38274122013-01-14 23:22:36 +00003936 MCAsmMacroParameters Parameters;
3937 MCAsmMacroArguments A;
Rafael Espindola34b9c512012-06-03 23:57:14 +00003938 raw_svector_ostream OS(Buf);
3939 while (Count--) {
3940 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3941 return true;
3942 }
Jim Grosbach4b905842013-09-20 23:08:21 +00003943 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003944
3945 return false;
3946}
3947
Jim Grosbach4b905842013-09-20 23:08:21 +00003948/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00003949/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00003950bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00003951 MCAsmMacroParameters Parameters;
3952 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00003953
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003954 if (parseIdentifier(Parameter.first))
Rafael Espindola768b41c2012-06-15 14:02:34 +00003955 return TokError("expected identifier in '.irp' directive");
3956
3957 Parameters.push_back(Parameter);
3958
3959 if (Lexer.isNot(AsmToken::Comma))
3960 return TokError("expected comma in '.irp' directive");
3961
3962 Lex();
3963
Eli Bendersky38274122013-01-14 23:22:36 +00003964 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00003965 if (parseMacroArguments(0, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00003966 return true;
3967
3968 // Eat the end of statement.
3969 Lex();
3970
3971 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00003972 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00003973 if (!M)
3974 return true;
3975
3976 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3977 // to hold the macro body with substitutions.
3978 SmallString<256> Buf;
3979 raw_svector_ostream OS(Buf);
3980
Eli Bendersky38274122013-01-14 23:22:36 +00003981 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
3982 MCAsmMacroArguments Args;
Rafael Espindola768b41c2012-06-15 14:02:34 +00003983 Args.push_back(*i);
3984
3985 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3986 return true;
3987 }
3988
Jim Grosbach4b905842013-09-20 23:08:21 +00003989 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00003990
3991 return false;
3992}
3993
Jim Grosbach4b905842013-09-20 23:08:21 +00003994/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00003995/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00003996bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00003997 MCAsmMacroParameters Parameters;
3998 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00003999
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004000 if (parseIdentifier(Parameter.first))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004001 return TokError("expected identifier in '.irpc' directive");
4002
4003 Parameters.push_back(Parameter);
4004
4005 if (Lexer.isNot(AsmToken::Comma))
4006 return TokError("expected comma in '.irpc' directive");
4007
4008 Lex();
4009
Eli Bendersky38274122013-01-14 23:22:36 +00004010 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004011 if (parseMacroArguments(0, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004012 return true;
4013
4014 if (A.size() != 1 || A.front().size() != 1)
4015 return TokError("unexpected token in '.irpc' directive");
4016
4017 // Eat the end of statement.
4018 Lex();
4019
4020 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004021 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004022 if (!M)
4023 return true;
4024
4025 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4026 // to hold the macro body with substitutions.
4027 SmallString<256> Buf;
4028 raw_svector_ostream OS(Buf);
4029
4030 StringRef Values = A.front().front().getString();
4031 std::size_t I, End = Values.size();
4032 for (I = 0; I < End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004033 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004034 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004035
Eli Bendersky38274122013-01-14 23:22:36 +00004036 MCAsmMacroArguments Args;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004037 Args.push_back(Arg);
4038
4039 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
4040 return true;
4041 }
4042
Jim Grosbach4b905842013-09-20 23:08:21 +00004043 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004044
4045 return false;
4046}
4047
Jim Grosbach4b905842013-09-20 23:08:21 +00004048bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004049 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004050 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004051
4052 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004053 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004054 assert(getLexer().is(AsmToken::EndOfStatement));
4055
Jim Grosbach4b905842013-09-20 23:08:21 +00004056 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004057 return false;
4058}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004059
Jim Grosbach4b905842013-09-20 23:08:21 +00004060bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004061 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004062 const MCExpr *Value;
4063 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004064 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004065 return true;
4066 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4067 if (!MCE)
4068 return Error(ExprLoc, "unexpected expression in _emit");
4069 uint64_t IntValue = MCE->getValue();
4070 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4071 return Error(ExprLoc, "literal value out of range for directive");
4072
Chad Rosierc7f552c2013-02-12 21:33:51 +00004073 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4074 return false;
4075}
4076
Jim Grosbach4b905842013-09-20 23:08:21 +00004077bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004078 const MCExpr *Value;
4079 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004080 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004081 return true;
4082 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4083 if (!MCE)
4084 return Error(ExprLoc, "unexpected expression in align");
4085 uint64_t IntValue = MCE->getValue();
4086 if (!isPowerOf2_64(IntValue))
4087 return Error(ExprLoc, "literal value not a power of two greater then zero");
4088
Jim Grosbach4b905842013-09-20 23:08:21 +00004089 Info.AsmRewrites->push_back(
4090 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004091 return false;
4092}
4093
Chad Rosierf43fcf52013-02-13 21:27:17 +00004094// We are comparing pointers, but the pointers are relative to a single string.
4095// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004096static int rewritesSort(const AsmRewrite *AsmRewriteA,
4097 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004098 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4099 return -1;
4100 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4101 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004102
Chad Rosierfce4fab2013-04-08 17:43:47 +00004103 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4104 // rewrite to the same location. Make sure the SizeDirective rewrite is
4105 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4106 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004107 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4108 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004109 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004110
Jim Grosbach4b905842013-09-20 23:08:21 +00004111 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4112 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004113 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004114 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004115}
4116
Jim Grosbach4b905842013-09-20 23:08:21 +00004117bool AsmParser::parseMSInlineAsm(
4118 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4119 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4120 SmallVectorImpl<std::string> &Constraints,
4121 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4122 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004123 SmallVector<void *, 4> InputDecls;
4124 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004125 SmallVector<bool, 4> InputDeclsAddressOf;
4126 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004127 SmallVector<std::string, 4> InputConstraints;
4128 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004129 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004130
Benjamin Kramer1a136112013-02-15 20:37:21 +00004131 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004132
4133 // Prime the lexer.
4134 Lex();
4135
4136 // While we have input, parse each statement.
4137 unsigned InputIdx = 0;
4138 unsigned OutputIdx = 0;
4139 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004140 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004141 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004142 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004143
Chad Rosier149e8e02012-12-12 22:45:52 +00004144 if (Info.ParseError)
4145 return true;
4146
Benjamin Kramer1a136112013-02-15 20:37:21 +00004147 if (Info.Opcode == ~0U)
4148 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004149
Benjamin Kramer1a136112013-02-15 20:37:21 +00004150 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004151
Benjamin Kramer1a136112013-02-15 20:37:21 +00004152 // Build the list of clobbers, outputs and inputs.
4153 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
4154 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004155
Benjamin Kramer1a136112013-02-15 20:37:21 +00004156 // Immediate.
Chad Rosierf3c04f62013-03-19 21:58:18 +00004157 if (Operand->isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004158 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004159
Benjamin Kramer1a136112013-02-15 20:37:21 +00004160 // Register operand.
4161 if (Operand->isReg() && !Operand->needAddressOf()) {
4162 unsigned NumDefs = Desc.getNumDefs();
4163 // Clobber.
4164 if (NumDefs && Operand->getMCOperandNum() < NumDefs)
4165 ClobberRegs.push_back(Operand->getReg());
4166 continue;
4167 }
4168
4169 // Expr/Input or Output.
Chad Rosiere81309b2013-04-09 17:53:49 +00004170 StringRef SymName = Operand->getSymName();
4171 if (SymName.empty())
4172 continue;
4173
Chad Rosierdba3fe52013-04-22 22:12:12 +00004174 void *OpDecl = Operand->getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004175 if (!OpDecl)
4176 continue;
4177
4178 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004179 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004180 if (isOutput) {
4181 ++InputIdx;
4182 OutputDecls.push_back(OpDecl);
4183 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
4184 OutputConstraints.push_back('=' + Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004185 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004186 } else {
4187 InputDecls.push_back(OpDecl);
4188 InputDeclsAddressOf.push_back(Operand->needAddressOf());
4189 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004190 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004191 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004192 }
4193 }
4194
4195 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004196 NumOutputs = OutputDecls.size();
4197 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004198
4199 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004200 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4201 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4202 ClobberRegs.end());
4203 Clobbers.assign(ClobberRegs.size(), std::string());
4204 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4205 raw_string_ostream OS(Clobbers[I]);
4206 IP->printRegName(OS, ClobberRegs[I]);
4207 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004208
4209 // Merge the various outputs and inputs. Output are expected first.
4210 if (NumOutputs || NumInputs) {
4211 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004212 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004213 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004214 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004215 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004216 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004217 }
4218 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004219 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004220 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004221 }
4222 }
4223
4224 // Build the IR assembly string.
4225 std::string AsmStringIR;
4226 raw_string_ostream OS(AsmStringIR);
Chad Rosier17d37992013-03-19 21:12:14 +00004227 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4228 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
Jim Grosbach4b905842013-09-20 23:08:21 +00004229 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
Benjamin Kramer1a136112013-02-15 20:37:21 +00004230 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(),
4231 E = AsmStrRewrites.end();
4232 I != E; ++I) {
Chad Rosierff10ed12013-04-12 16:26:42 +00004233 AsmRewriteKind Kind = (*I).Kind;
4234 if (Kind == AOK_Delete)
4235 continue;
4236
Chad Rosier8bce6642012-10-18 15:49:34 +00004237 const char *Loc = (*I).Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004238 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004239
Chad Rosier120eefd2013-03-19 17:32:17 +00004240 // Emit everything up to the immediate/expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004241 unsigned Len = Loc - AsmStart;
Chad Rosier8fb83302013-04-11 21:49:30 +00004242 if (Len)
Chad Rosier17d37992013-03-19 21:12:14 +00004243 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004244
Chad Rosier37e755c2012-10-23 17:43:43 +00004245 // Skip the original expression.
4246 if (Kind == AOK_Skip) {
Chad Rosier17d37992013-03-19 21:12:14 +00004247 AsmStart = Loc + (*I).Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004248 continue;
4249 }
4250
Chad Rosierff10ed12013-04-12 16:26:42 +00004251 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004252 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004253 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004254 default:
4255 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004256 case AOK_Imm:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004257 OS << "$$" << (*I).Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004258 break;
4259 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004260 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004261 break;
4262 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004263 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004264 break;
4265 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004266 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004267 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004268 case AOK_SizeDirective:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004269 switch ((*I).Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004270 default: break;
4271 case 8: OS << "byte ptr "; break;
4272 case 16: OS << "word ptr "; break;
4273 case 32: OS << "dword ptr "; break;
4274 case 64: OS << "qword ptr "; break;
4275 case 80: OS << "xword ptr "; break;
4276 case 128: OS << "xmmword ptr "; break;
4277 case 256: OS << "ymmword ptr "; break;
4278 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004279 break;
4280 case AOK_Emit:
4281 OS << ".byte";
4282 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004283 case AOK_Align: {
4284 unsigned Val = (*I).Val;
4285 OS << ".align " << Val;
4286
4287 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004288 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004289 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4290 break;
4291 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004292 case AOK_DotOperator:
4293 OS << (*I).Val;
4294 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004295 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004296
Chad Rosier8bce6642012-10-18 15:49:34 +00004297 // Skip the original expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004298 AsmStart = Loc + (*I).Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004299 }
4300
4301 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004302 if (AsmStart != AsmEnd)
4303 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004304
4305 AsmString = OS.str();
4306 return false;
4307}
4308
Daniel Dunbar01e36072010-07-17 02:26:10 +00004309/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004310MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4311 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004312 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004313}