blob: 1692b2f190008fa97569e030f0a49eb7121690da [file] [log] [blame]
Chris Lattnerb0133452009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar2af16532010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Chad Rosiereb5c1682013-02-13 18:38:58 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "llvm/ADT/SmallString.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000017#include "llvm/ADT/StringMap.h"
Daniel Dunbareb6bb322009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Evan Cheng11424442011-07-26 00:24:13 +000021#include "llvm/MC/MCDwarf.h"
Daniel Dunbar115e4d62009-08-31 08:06:59 +000022#include "llvm/MC/MCExpr.h"
Chad Rosier8bce6642012-10-18 15:49:34 +000023#include "llvm/MC/MCInstPrinter.h"
24#include "llvm/MC/MCInstrInfo.h"
Rafael Espindolae28610d2013-12-09 20:26:40 +000025#include "llvm/MC/MCObjectFileInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000026#include "llvm/MC/MCParser/AsmCond.h"
27#include "llvm/MC/MCParser/AsmLexer.h"
28#include "llvm/MC/MCParser/MCAsmParser.h"
29#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Cheng76792992011-07-20 05:58:47 +000030#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000031#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000032#include "llvm/MC/MCStreamer.h"
Daniel Dunbarae7ac012009-06-29 23:43:14 +000033#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000034#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000035#include "llvm/Support/CommandLine.h"
Benjamin Kramer4efe5062012-01-28 15:28:41 +000036#include "llvm/Support/ErrorHandling.h"
Jim Grosbach76346c32011-06-29 16:05:14 +000037#include "llvm/Support/MathExtras.h"
Kevin Enderbye233dda2010-06-28 21:45:58 +000038#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000039#include "llvm/Support/SourceMgr.h"
Chris Lattner36e02122009-06-21 20:54:55 +000040#include "llvm/Support/raw_ostream.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000041#include <cctype>
Chad Rosier8bce6642012-10-18 15:49:34 +000042#include <set>
43#include <string>
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000044#include <vector>
Chris Lattnerb0133452009-06-21 20:16:42 +000045using namespace llvm;
46
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000047static cl::opt<bool>
48FatalAssemblerWarnings("fatal-assembler-warnings",
49 cl::desc("Consider warnings as error"));
50
Eric Christophera7c32732012-12-18 00:30:54 +000051MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewyckyac612272012-10-19 07:00:09 +000052
Daniel Dunbar86033402010-07-12 17:54:38 +000053namespace {
Eli Benderskya313ae62013-01-16 18:56:50 +000054/// \brief Helper types for tracking macro definitions.
55typedef std::vector<AsmToken> MCAsmMacroArgument;
56typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000057
58struct MCAsmMacroParameter {
59 StringRef Name;
60 MCAsmMacroArgument Value;
61};
62
Eli Benderskya313ae62013-01-16 18:56:50 +000063typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
64
65struct MCAsmMacro {
66 StringRef Name;
67 StringRef Body;
68 MCAsmMacroParameters Parameters;
69
70public:
Benjamin Kramerd31aaf12014-02-09 17:13:11 +000071 MCAsmMacro(StringRef N, StringRef B, ArrayRef<MCAsmMacroParameter> P) :
Eli Benderskya313ae62013-01-16 18:56:50 +000072 Name(N), Body(B), Parameters(P) {}
Eli Benderskya313ae62013-01-16 18:56:50 +000073};
74
Daniel Dunbar43235712010-07-18 18:54:11 +000075/// \brief Helper class for storing information about an active macro
76/// instantiation.
77struct MacroInstantiation {
78 /// The macro being instantiated.
Eli Bendersky38274122013-01-14 23:22:36 +000079 const MCAsmMacro *TheMacro;
Daniel Dunbar43235712010-07-18 18:54:11 +000080
81 /// The macro instantiation with substitutions.
82 MemoryBuffer *Instantiation;
83
84 /// The location of the instantiation.
85 SMLoc InstantiationLoc;
86
Daniel Dunbar40f1d852012-12-01 01:38:48 +000087 /// The buffer where parsing should resume upon instantiation completion.
88 int ExitBuffer;
89
Daniel Dunbar43235712010-07-18 18:54:11 +000090 /// The location where parsing should resume upon instantiation completion.
91 SMLoc ExitLoc;
92
93public:
Eli Bendersky38274122013-01-14 23:22:36 +000094 MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB, SMLoc EL,
Rafael Espindola1134ab232011-06-05 02:43:45 +000095 MemoryBuffer *I);
Daniel Dunbar43235712010-07-18 18:54:11 +000096};
97
Eli Friedman0f4871d2012-10-22 23:58:19 +000098struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +000099 /// \brief The parsed operands from the last parsed statement.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000100 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
101
Jim Grosbach4b905842013-09-20 23:08:21 +0000102 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000103 unsigned Opcode;
104
Jim Grosbach4b905842013-09-20 23:08:21 +0000105 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000106 bool ParseError;
107
Eli Friedman0f4871d2012-10-22 23:58:19 +0000108 SmallVectorImpl<AsmRewrite> *AsmRewrites;
109
Chad Rosier149e8e02012-12-12 22:45:52 +0000110 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(0) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000111 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000112 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000113
114 ~ParseStatementInfo() {
115 // Free any parsed operands.
116 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
117 delete ParsedOperands[i];
118 ParsedOperands.clear();
119 }
120};
121
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000122/// \brief The concrete assembly parser instance.
123class AsmParser : public MCAsmParser {
Craig Topper2e6644c2012-09-15 16:23:52 +0000124 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
125 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000126private:
127 AsmLexer Lexer;
128 MCContext &Ctx;
129 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000130 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000131 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000132 SourceMgr::DiagHandlerTy SavedDiagHandler;
133 void *SavedDiagContext;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000134 MCAsmParserExtension *PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000135
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000136 /// This is the current buffer index we're lexing from as managed by the
137 /// SourceMgr object.
138 int CurBuffer;
139
140 AsmCond TheCondState;
141 std::vector<AsmCond> TheCondStack;
142
Jim Grosbach4b905842013-09-20 23:08:21 +0000143 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000144 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000145 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000146 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000147
Jim Grosbach4b905842013-09-20 23:08:21 +0000148 /// \brief Map of currently defined macros.
Eli Bendersky38274122013-01-14 23:22:36 +0000149 StringMap<MCAsmMacro*> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000150
Jim Grosbach4b905842013-09-20 23:08:21 +0000151 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000152 std::vector<MacroInstantiation*> ActiveMacros;
153
Jim Grosbach4b905842013-09-20 23:08:21 +0000154 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000155 std::deque<MCAsmMacro> MacroLikeBodies;
156
Daniel Dunbar828984f2010-07-18 18:38:02 +0000157 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000158 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000159
Daniel Dunbar43325c42010-09-09 22:42:56 +0000160 /// Flag tracking whether any errors have been encountered.
161 unsigned HadError : 1;
162
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000163 /// The values from the last parsed cpp hash file line comment if any.
164 StringRef CppHashFilename;
165 int64_t CppHashLineNumber;
166 SMLoc CppHashLoc;
Kevin Enderby27121c12012-11-05 21:55:41 +0000167 int CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000168 /// When generating dwarf for assembly source files we need to calculate the
169 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000170 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000171 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
172 SMLoc LastQueryIDLoc;
173 int LastQueryBuffer;
174 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000175
Devang Patela173ee52012-01-31 18:14:05 +0000176 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
177 unsigned AssemblerDialect;
178
Jim Grosbach4b905842013-09-20 23:08:21 +0000179 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000180 bool IsDarwin;
181
Jim Grosbach4b905842013-09-20 23:08:21 +0000182 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000183 bool ParsingInlineAsm;
184
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000185public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000186 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000187 const MCAsmInfo &MAI);
Craig Topper5f96ca52012-08-29 05:48:09 +0000188 virtual ~AsmParser();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000189
190 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
191
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000192 virtual void addDirectiveHandler(StringRef Directive,
Eli Bendersky29b9f472013-01-16 00:50:52 +0000193 ExtensionDirectiveHandler Handler) {
194 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000195 }
196
197public:
198 /// @name MCAsmParser Interface
199 /// {
200
201 virtual SourceMgr &getSourceManager() { return SrcMgr; }
202 virtual MCAsmLexer &getLexer() { return Lexer; }
203 virtual MCContext &getContext() { return Ctx; }
204 virtual MCStreamer &getStreamer() { return Out; }
Eric Christophera7c32732012-12-18 00:30:54 +0000205 virtual unsigned getAssemblerDialect() {
Devang Patela173ee52012-01-31 18:14:05 +0000206 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000207 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000208 else
209 return AssemblerDialect;
210 }
211 virtual void setAssemblerDialect(unsigned i) {
212 AssemblerDialect = i;
213 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000214
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000215 virtual void Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges = None);
Chris Lattnera3a06812011-10-16 04:47:35 +0000216 virtual bool Warning(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000217 ArrayRef<SMRange> Ranges = None);
Chris Lattnera3a06812011-10-16 04:47:35 +0000218 virtual bool Error(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000219 ArrayRef<SMRange> Ranges = None);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000220
Craig Topper5f96ca52012-08-29 05:48:09 +0000221 virtual const AsmToken &Lex();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000222
Chad Rosier49963552012-10-13 00:26:04 +0000223 void setParsingInlineAsm(bool V) { ParsingInlineAsm = V; }
Chad Rosiere4ad2a02012-10-16 20:16:20 +0000224 bool isParsingInlineAsm() { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000225
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000226 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000227 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000228 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000229 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000230 SmallVectorImpl<std::string> &Clobbers,
231 const MCInstrInfo *MII,
232 const MCInstPrinter *IP,
233 MCAsmParserSemaCallback &SI);
Chad Rosier49963552012-10-13 00:26:04 +0000234
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000235 bool parseExpression(const MCExpr *&Res);
236 virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc);
Chad Rosier1863f4f2013-04-10 17:35:30 +0000237 virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000238 virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
239 virtual bool parseAbsoluteExpression(int64_t &Res);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000240
Jim Grosbach4b905842013-09-20 23:08:21 +0000241 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000242 /// and set \p Res to the identifier contents.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000243 virtual bool parseIdentifier(StringRef &Res);
244 virtual void eatToEndOfStatement();
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000245
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000246 virtual void checkForValidSection();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000247 /// }
248
249private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000250
Jim Grosbach4b905842013-09-20 23:08:21 +0000251 bool parseStatement(ParseStatementInfo &Info);
252 void eatToEndOfLine();
253 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000254
Jim Grosbach4b905842013-09-20 23:08:21 +0000255 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000256 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000257 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000258 ArrayRef<MCAsmMacroParameter> Parameters,
259 ArrayRef<MCAsmMacroArgument> A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000260 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000261
Eli Benderskya313ae62013-01-16 18:56:50 +0000262 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000263 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000264
265 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000266 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000267
268 /// \brief Lookup a previously defined macro.
269 /// \param Name Macro name.
270 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000271 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000272
273 /// \brief Define a new macro with the given name and information.
Jim Grosbach4b905842013-09-20 23:08:21 +0000274 void defineMacro(StringRef Name, const MCAsmMacro& Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000275
276 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000277 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000278
279 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000280 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000281
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000282 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000283 ///
284 /// \param M The macro.
285 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000286 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000287
288 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000289 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000290
David Majnemer91fc4c22014-01-29 18:57:46 +0000291 /// \brief Extract AsmTokens for a macro argument.
292 bool parseMacroArgument(MCAsmMacroArgument &MA);
Eli Benderskya313ae62013-01-16 18:56:50 +0000293
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,
David Woodhoused6de0d92014-02-01 16:20:59 +0000341 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
342 DK_SINGLE, 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,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000360 DK_SLEB128, DK_ULEB128,
361 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000362 };
363
Jim Grosbach4b905842013-09-20 23:08:21 +0000364 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000365 /// directives parsed by this class.
366 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000367
368 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000369 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
370 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000371 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000372 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
373 bool parseDirectiveFill(); // ".fill"
374 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000375 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000376 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
377 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000378 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000379 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000380
Eli Bendersky17233942013-01-15 22:59:42 +0000381 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000382 bool parseDirectiveFile(SMLoc DirectiveLoc);
383 bool parseDirectiveLine();
384 bool parseDirectiveLoc();
385 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000386
387 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000388 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000389 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000390 bool parseDirectiveCFISections();
391 bool parseDirectiveCFIStartProc();
392 bool parseDirectiveCFIEndProc();
393 bool parseDirectiveCFIDefCfaOffset();
394 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
395 bool parseDirectiveCFIAdjustCfaOffset();
396 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
397 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
398 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
399 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
400 bool parseDirectiveCFIRememberState();
401 bool parseDirectiveCFIRestoreState();
402 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
403 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
404 bool parseDirectiveCFIEscape();
405 bool parseDirectiveCFISignalFrame();
406 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000407
408 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000409 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
410 bool parseDirectiveEndMacro(StringRef Directive);
411 bool parseDirectiveMacro(SMLoc DirectiveLoc);
412 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000413
Eli Benderskyf483ff92012-12-20 19:05:53 +0000414 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000415 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000416 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000417 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000418 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000419 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000420
Eli Bendersky17233942013-01-15 22:59:42 +0000421 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000422 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000423
424 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000425 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000426
Jim Grosbach4b905842013-09-20 23:08:21 +0000427 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000428 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000429 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000430
Jim Grosbach4b905842013-09-20 23:08:21 +0000431 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000432
Jim Grosbach4b905842013-09-20 23:08:21 +0000433 bool parseDirectiveAbort(); // ".abort"
434 bool parseDirectiveInclude(); // ".include"
435 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000436
Jim Grosbach4b905842013-09-20 23:08:21 +0000437 bool parseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000438 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000439 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000440 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000441 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000442 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000443 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
444 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
445 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
446 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000447 virtual bool parseEscapedString(std::string &Data);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000448
Jim Grosbach4b905842013-09-20 23:08:21 +0000449 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000450 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000451
Rafael Espindola34b9c512012-06-03 23:57:14 +0000452 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000453 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
454 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000455 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000456 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000457 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
458 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
459 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000460
Chad Rosierc7f552c2013-02-12 21:33:51 +0000461 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000462 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000463 size_t Len);
464
465 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000466 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000467
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000468 // "end"
469 bool parseDirectiveEnd(SMLoc DirectiveLoc);
470
Eli Bendersky17233942013-01-15 22:59:42 +0000471 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000472};
Daniel Dunbar86033402010-07-12 17:54:38 +0000473}
474
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000475namespace llvm {
476
477extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000478extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000479extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000480
481}
482
Chris Lattnerc35681b2010-01-19 19:46:13 +0000483enum { DEFAULT_ADDRSPACE = 0 };
484
Jim Grosbach4b905842013-09-20 23:08:21 +0000485AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
486 const MCAsmInfo &_MAI)
487 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
488 PlatformParser(0), CurBuffer(0), MacrosEnabledFlag(true),
489 CppHashLineNumber(0), AssemblerDialect(~0U), IsDarwin(false),
490 ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000491 // Save the old handler.
492 SavedDiagHandler = SrcMgr.getDiagHandler();
493 SavedDiagContext = SrcMgr.getDiagContext();
494 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000495 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000496 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar86033402010-07-12 17:54:38 +0000497
Daniel Dunbarc5011082010-07-12 18:12:02 +0000498 // Initialize the platform / file format parser.
Rafael Espindolae28610d2013-12-09 20:26:40 +0000499 switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
500 case MCObjectFileInfo::IsCOFF:
501 PlatformParser = createCOFFAsmParser();
502 PlatformParser->Initialize(*this);
503 break;
504 case MCObjectFileInfo::IsMachO:
505 PlatformParser = createDarwinAsmParser();
506 PlatformParser->Initialize(*this);
507 IsDarwin = true;
508 break;
509 case MCObjectFileInfo::IsELF:
510 PlatformParser = createELFAsmParser();
511 PlatformParser->Initialize(*this);
512 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000513 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000514
Eli Bendersky17233942013-01-15 22:59:42 +0000515 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000516}
517
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000518AsmParser::~AsmParser() {
Daniel Dunbarb759a132010-07-29 01:51:55 +0000519 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
520
521 // Destroy any macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000522 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
523 ie = MacroMap.end();
524 it != ie; ++it)
Daniel Dunbarb759a132010-07-29 01:51:55 +0000525 delete it->getValue();
526
Daniel Dunbarc5011082010-07-12 18:12:02 +0000527 delete PlatformParser;
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000528}
529
Jim Grosbach4b905842013-09-20 23:08:21 +0000530void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000531 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000532 for (std::vector<MacroInstantiation *>::const_reverse_iterator
533 it = ActiveMacros.rbegin(),
534 ie = ActiveMacros.rend();
535 it != ie; ++it)
536 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000537 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000538}
539
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000540void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
541 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
542 printMacroInstantiations();
543}
544
Chris Lattnera3a06812011-10-16 04:47:35 +0000545bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000546 if (FatalAssemblerWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000547 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000548 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
549 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000550 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000551}
552
Chris Lattnera3a06812011-10-16 04:47:35 +0000553bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000554 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000555 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
556 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000557 return true;
558}
559
Jim Grosbach4b905842013-09-20 23:08:21 +0000560bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000561 std::string IncludedFile;
562 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000563 if (NewBuf == -1)
564 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000565
Sean Callanan7a77eae2010-01-21 00:19:58 +0000566 CurBuffer = NewBuf;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000567
Sean Callanan7a77eae2010-01-21 00:19:58 +0000568 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencer530ce852010-10-09 11:00:50 +0000569
Sean Callanan7a77eae2010-01-21 00:19:58 +0000570 return false;
571}
Daniel Dunbar43235712010-07-18 18:54:11 +0000572
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000573/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000574/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000575/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000576bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000577 std::string IncludedFile;
578 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
579 if (NewBuf == -1)
580 return true;
581
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000582 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000583 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000584 return false;
585}
586
Jim Grosbach4b905842013-09-20 23:08:21 +0000587void AsmParser::jumpToLoc(SMLoc Loc, int InBuffer) {
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000588 if (InBuffer != -1) {
589 CurBuffer = InBuffer;
590 } else {
591 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
592 }
Daniel Dunbar43235712010-07-18 18:54:11 +0000593 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
594}
595
Sean Callanan7a77eae2010-01-21 00:19:58 +0000596const AsmToken &AsmParser::Lex() {
597 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000598
Sean Callanan7a77eae2010-01-21 00:19:58 +0000599 if (tok->is(AsmToken::Eof)) {
600 // If this is the end of an included file, pop the parent file off the
601 // include stack.
602 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
603 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000604 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000605 tok = &Lexer.Lex();
606 }
607 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000608
Sean Callanan7a77eae2010-01-21 00:19:58 +0000609 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000610 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000611
Sean Callanan7a77eae2010-01-21 00:19:58 +0000612 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000613}
614
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000615bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000616 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000617 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000618 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000619
Chris Lattner36e02122009-06-21 20:54:55 +0000620 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000621 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000622
623 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000624 AsmCond StartingCondState = TheCondState;
625
Kevin Enderby6469fc22011-11-01 22:27:22 +0000626 // If we are generating dwarf for assembly source files save the initial text
627 // section and generate a .file directive.
628 if (getContext().getGenDwarfForAssembly()) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000629 getContext().setGenDwarfSection(getStreamer().getCurrentSection().first);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000630 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
631 getStreamer().EmitLabel(SectionStartSym);
632 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby6469fc22011-11-01 22:27:22 +0000633 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
Eric Christopher906da232012-12-18 00:31:01 +0000634 StringRef(),
635 getContext().getMainFileName());
Kevin Enderby6469fc22011-11-01 22:27:22 +0000636 }
637
Chris Lattner73f36112009-07-02 21:53:43 +0000638 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000639 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000640 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000641 if (!parseStatement(Info))
642 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000643
Daniel Dunbar43325c42010-09-09 22:42:56 +0000644 // We had an error, validate that one was emitted and recover by skipping to
645 // the next line.
646 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000647 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000648 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000649
650 if (TheCondState.TheCond != StartingCondState.TheCond ||
651 TheCondState.Ignore != StartingCondState.Ignore)
652 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000653
654 // Check to see there are no empty DwarfFile slots.
Manman Ren5ce24ff2013-03-12 20:17:00 +0000655 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +0000656 getContext().getMCDwarfFiles();
Kevin Enderbye5930f12010-07-28 20:55:35 +0000657 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000658 if (!MCDwarfFiles[i])
Kevin Enderbye5930f12010-07-28 20:55:35 +0000659 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000660 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000661
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000662 // Check to see that all assembler local symbols were actually defined.
663 // Targets that don't do subsections via symbols may not want this, though,
664 // so conservatively exclude them. Only do this if we're finalizing, though,
665 // as otherwise we won't necessarilly have seen everything yet.
666 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
667 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
668 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000669 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000670 i != e; ++i) {
671 MCSymbol *Sym = i->getValue();
672 // Variable symbols may not be marked as defined, so check those
673 // explicitly. If we know it's a variable, we have a definition for
674 // the purposes of this check.
675 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
676 // FIXME: We would really like to refer back to where the symbol was
677 // first referenced for a source location. We need to add something
678 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000679 printMessage(
680 getLexer().getLoc(), SourceMgr::DK_Error,
681 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000682 }
683 }
684
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000685 // Finalize the output stream if there are no errors and if the client wants
686 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000687 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000688 Out.Finish();
689
Chris Lattner73f36112009-07-02 21:53:43 +0000690 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000691}
692
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000693void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000694 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000695 TokError("expected section directive before assembly directive");
Rafael Espindolaf1440342014-01-23 23:14:14 +0000696 Out.InitSections();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000697 }
698}
699
Jim Grosbach4b905842013-09-20 23:08:21 +0000700/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000701void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000702 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000703 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000704
Chris Lattnere5074c42009-06-22 01:29:09 +0000705 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000706 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000707 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000708}
709
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000710StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000711 const char *Start = getTok().getLoc().getPointer();
712
Jim Grosbach4b905842013-09-20 23:08:21 +0000713 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000714 Lex();
715
716 const char *End = getTok().getLoc().getPointer();
717 return StringRef(Start, End - Start);
718}
Chris Lattner78db3622009-06-22 05:51:26 +0000719
Jim Grosbach4b905842013-09-20 23:08:21 +0000720StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000721 const char *Start = getTok().getLoc().getPointer();
722
723 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000724 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000725 Lex();
726
727 const char *End = getTok().getLoc().getPointer();
728 return StringRef(Start, End - Start);
729}
730
Jim Grosbach4b905842013-09-20 23:08:21 +0000731/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000732/// NOTE: This assumes the leading '(' has already been consumed.
733///
734/// parenexpr ::= expr)
735///
Jim Grosbach4b905842013-09-20 23:08:21 +0000736bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
737 if (parseExpression(Res))
738 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000739 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000740 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000741 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000742 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000743 return false;
744}
Chris Lattner78db3622009-06-22 05:51:26 +0000745
Jim Grosbach4b905842013-09-20 23:08:21 +0000746/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000747/// NOTE: This assumes the leading '[' has already been consumed.
748///
749/// bracketexpr ::= expr]
750///
Jim Grosbach4b905842013-09-20 23:08:21 +0000751bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
752 if (parseExpression(Res))
753 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000754 if (Lexer.isNot(AsmToken::RBrac))
755 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000756 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000757 Lex();
758 return false;
759}
760
Jim Grosbach4b905842013-09-20 23:08:21 +0000761/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000762/// primaryexpr ::= (parenexpr
763/// primaryexpr ::= symbol
764/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000765/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000766/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000767bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000768 SMLoc FirstTokenLoc = getLexer().getLoc();
769 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
770 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000771 default:
772 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000773 // If we have an error assume that we've already handled it.
774 case AsmToken::Error:
775 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000776 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000777 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000778 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000779 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000780 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000781 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000782 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000783 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000784 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000785 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000786 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000787 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000788 if (FirstTokenKind == AsmToken::Dollar) {
789 if (Lexer.getMAI().getDollarIsPC()) {
790 // This is a '$' reference, which references the current PC. Emit a
791 // temporary label to the streamer and refer to it.
792 MCSymbol *Sym = Ctx.CreateTempSymbol();
793 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000794 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
795 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000796 EndLoc = FirstTokenLoc;
797 return false;
798 } else
799 return Error(FirstTokenLoc, "invalid token in expression");
800 return true;
801 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000802 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000803 // Parse symbol variant
804 std::pair<StringRef, StringRef> Split;
805 if (!MAI.useParensForSymbolVariant()) {
806 Split = Identifier.split('@');
807 } else if (Lexer.is(AsmToken::LParen)) {
808 Lexer.Lex(); // eat (
809 StringRef VName;
810 parseIdentifier(VName);
811 if (Lexer.isNot(AsmToken::RParen)) {
812 return Error(Lexer.getTok().getLoc(),
813 "unexpected token in variant, expected ')'");
814 }
815 Lexer.Lex(); // eat )
816 Split = std::make_pair(Identifier, VName);
817 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000818
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000819 EndLoc = SMLoc::getFromPointer(Identifier.end());
820
Daniel Dunbard20cda02009-10-16 01:34:54 +0000821 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000822 StringRef SymbolName = Identifier;
823 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000824
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000825 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000826 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000827 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000828 if (Variant != MCSymbolRefExpr::VK_Invalid) {
829 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000830 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000831 Variant = MCSymbolRefExpr::VK_None;
832 } else {
Daniel Dunbar55f16672010-09-17 02:47:07 +0000833 Variant = MCSymbolRefExpr::VK_None;
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000834 return Error(SMLoc::getFromPointer(Split.second.begin()),
835 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000836 }
837 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000838
Hans Wennborgce69d772013-10-18 20:46:28 +0000839 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
840
Daniel Dunbard20cda02009-10-16 01:34:54 +0000841 // If this is an absolute variable reference, substitute it now to preserve
842 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000843 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000844 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000845 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000846
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000847 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000848 return false;
849 }
850
851 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000852 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000853 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000854 }
David Woodhousef42a6662014-02-01 16:20:54 +0000855 case AsmToken::BigNum:
856 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000857 case AsmToken::Integer: {
858 SMLoc Loc = getTok().getLoc();
859 int64_t IntVal = getTok().getIntVal();
860 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000861 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000862 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000863 // Look for 'b' or 'f' following an Integer as a directional label
864 if (Lexer.getKind() == AsmToken::Identifier) {
865 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000866 // Lookup the symbol variant if used.
867 std::pair<StringRef, StringRef> Split = IDVal.split('@');
868 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
869 if (Split.first.size() != IDVal.size()) {
870 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
871 if (Variant == MCSymbolRefExpr::VK_Invalid) {
872 Variant = MCSymbolRefExpr::VK_None;
873 return TokError("invalid variant '" + Split.second + "'");
874 }
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000875 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000876 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000877 if (IDVal == "f" || IDVal == "b") {
878 MCSymbol *Sym =
879 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "f" ? 1 : 0);
Ulrich Weigandd4120982013-06-20 16:24:17 +0000880 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000881 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000882 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000883 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000884 Lex(); // Eat identifier.
885 }
886 }
Chris Lattner78db3622009-06-22 05:51:26 +0000887 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000888 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000889 case AsmToken::Real: {
890 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000891 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000892 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000893 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000894 Lex(); // Eat token.
895 return false;
896 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000897 case AsmToken::Dot: {
898 // This is a '.' reference, which references the current PC. Emit a
899 // temporary label to the streamer and refer to it.
900 MCSymbol *Sym = Ctx.CreateTempSymbol();
901 Out.EmitLabel(Sym);
902 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000903 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000904 Lex(); // Eat identifier.
905 return false;
906 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000907 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000908 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000909 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000910 case AsmToken::LBrac:
911 if (!PlatformParser->HasBracketExpressions())
912 return TokError("brackets expression not supported on this target");
913 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000914 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000915 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000916 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000917 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000918 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000919 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000920 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000921 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000922 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000923 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000924 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000925 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000926 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000927 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000928 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000929 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000930 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000931 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000932 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000933 }
934}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000935
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000936bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000937 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000938 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000939}
940
Daniel Dunbar55f16672010-09-17 02:47:07 +0000941const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000942AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000943 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000944 // Ask the target implementation about this expression first.
945 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
946 if (NewE)
947 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000948 // Recurse over the given expression, rebuilding it to apply the given variant
949 // if there is exactly one symbol.
950 switch (E->getKind()) {
951 case MCExpr::Target:
952 case MCExpr::Constant:
953 return 0;
954
955 case MCExpr::SymbolRef: {
956 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
957
958 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000959 TokError("invalid variant on expression '" + getTok().getIdentifier() +
960 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000961 return E;
962 }
963
964 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
965 }
966
967 case MCExpr::Unary: {
968 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000969 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000970 if (!Sub)
971 return 0;
972 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
973 }
974
975 case MCExpr::Binary: {
976 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000977 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
978 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000979
980 if (!LHS && !RHS)
981 return 0;
982
Jim Grosbach4b905842013-09-20 23:08:21 +0000983 if (!LHS)
984 LHS = BE->getLHS();
985 if (!RHS)
986 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +0000987
988 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
989 }
990 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +0000991
Craig Toppera2886c22012-02-07 05:05:23 +0000992 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000993}
994
Jim Grosbach4b905842013-09-20 23:08:21 +0000995/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +0000996///
Jim Grosbachbd164242011-08-20 16:24:13 +0000997/// expr ::= expr &&,|| expr -> lowest.
998/// expr ::= expr |,^,&,! expr
999/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1000/// expr ::= expr <<,>> expr
1001/// expr ::= expr +,- expr
1002/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001003/// expr ::= primaryexpr
1004///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001005bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001006 // Parse the expression.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001007 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001008 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001009 return true;
1010
Daniel Dunbar55f16672010-09-17 02:47:07 +00001011 // As a special case, we support 'a op b @ modifier' by rewriting the
1012 // expression to include the modifier. This is inefficient, but in general we
1013 // expect users to use 'a@modifier op b'.
1014 if (Lexer.getKind() == AsmToken::At) {
1015 Lex();
1016
1017 if (Lexer.isNot(AsmToken::Identifier))
1018 return TokError("unexpected symbol modifier following '@'");
1019
1020 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001021 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001022 if (Variant == MCSymbolRefExpr::VK_Invalid)
1023 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1024
Jim Grosbach4b905842013-09-20 23:08:21 +00001025 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001026 if (!ModifiedRes) {
1027 return TokError("invalid modifier '" + getTok().getIdentifier() +
1028 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001029 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001030
Daniel Dunbar55f16672010-09-17 02:47:07 +00001031 Res = ModifiedRes;
1032 Lex();
1033 }
1034
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001035 // Try to constant fold it up front, if possible.
1036 int64_t Value;
1037 if (Res->EvaluateAsAbsolute(Value))
1038 Res = MCConstantExpr::Create(Value, getContext());
1039
1040 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001041}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001042
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001043bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner807a3bc2010-01-24 01:07:33 +00001044 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001045 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001046}
1047
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001048bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001049 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001050
Daniel Dunbar75630b32009-06-30 02:10:03 +00001051 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001052 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001053 return true;
1054
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001055 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001056 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001057
1058 return false;
1059}
1060
Michael J. Spencer530ce852010-10-09 11:00:50 +00001061static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001062 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001063 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001064 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001065 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001066
Jim Grosbach4b905842013-09-20 23:08:21 +00001067 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001068 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001069 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001070 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001071 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001072 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001073 return 1;
1074
Jim Grosbach4b905842013-09-20 23:08:21 +00001075 // Low Precedence: |, &, ^
1076 //
1077 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001078 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001079 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001080 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001081 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001082 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001083 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001084 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001085 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001086 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001087
Jim Grosbach4b905842013-09-20 23:08:21 +00001088 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001089 case AsmToken::EqualEqual:
1090 Kind = MCBinaryExpr::EQ;
1091 return 3;
1092 case AsmToken::ExclaimEqual:
1093 case AsmToken::LessGreater:
1094 Kind = MCBinaryExpr::NE;
1095 return 3;
1096 case AsmToken::Less:
1097 Kind = MCBinaryExpr::LT;
1098 return 3;
1099 case AsmToken::LessEqual:
1100 Kind = MCBinaryExpr::LTE;
1101 return 3;
1102 case AsmToken::Greater:
1103 Kind = MCBinaryExpr::GT;
1104 return 3;
1105 case AsmToken::GreaterEqual:
1106 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001107 return 3;
1108
Jim Grosbach4b905842013-09-20 23:08:21 +00001109 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001110 case AsmToken::LessLess:
1111 Kind = MCBinaryExpr::Shl;
1112 return 4;
1113 case AsmToken::GreaterGreater:
1114 Kind = MCBinaryExpr::Shr;
1115 return 4;
1116
Jim Grosbach4b905842013-09-20 23:08:21 +00001117 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001118 case AsmToken::Plus:
1119 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001120 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001121 case AsmToken::Minus:
1122 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001123 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001124
Jim Grosbach4b905842013-09-20 23:08:21 +00001125 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001126 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001127 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001128 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001129 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001130 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001131 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001132 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001133 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001134 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001135 }
1136}
1137
Jim Grosbach4b905842013-09-20 23:08:21 +00001138/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001139/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001140bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001141 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001142 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001143 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001144 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001145
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001146 // If the next token is lower precedence than we are allowed to eat, return
1147 // successfully with what we ate already.
1148 if (TokPrec < Precedence)
1149 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001150
Sean Callanan686ed8d2010-01-19 20:22:31 +00001151 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001152
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001153 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001154 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001155 if (parsePrimaryExpr(RHS, EndLoc))
1156 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001157
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001158 // If BinOp binds less tightly with RHS than the operator after RHS, let
1159 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001160 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001161 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001162 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1163 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001164
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001165 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001166 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001167 }
1168}
1169
Chris Lattner36e02122009-06-21 20:54:55 +00001170/// ParseStatement:
1171/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001172/// ::= Label* Directive ...Operands... EndOfStatement
1173/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001174bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001175 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001176 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001177 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001178 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001179 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001180
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001181 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001182 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001183 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001184 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001185 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001186 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001187 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001188 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001189
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001190 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001191 if (Lexer.is(AsmToken::Integer)) {
1192 LocalLabelVal = getTok().getIntVal();
1193 if (LocalLabelVal < 0) {
1194 if (!TheCondState.Ignore)
1195 return TokError("unexpected token at start of statement");
1196 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001197 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001198 IDVal = getTok().getString();
1199 Lex(); // Consume the integer token to be used as an identifier token.
1200 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001201 if (!TheCondState.Ignore)
1202 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001203 }
1204 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001205 } else if (Lexer.is(AsmToken::Dot)) {
1206 // Treat '.' as a valid identifier in this context.
1207 Lex();
1208 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001209 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001210 if (!TheCondState.Ignore)
1211 return TokError("unexpected token at start of statement");
1212 IDVal = "";
1213 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001214
Chris Lattner926885c2010-04-17 18:14:27 +00001215 // Handle conditional assembly here before checking for skipping. We
1216 // have to do this so that .endif isn't skipped in a ".if 0" block for
1217 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001218 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001219 DirectiveKindMap.find(IDVal);
1220 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1221 ? DK_NO_DIRECTIVE
1222 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001223 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001224 default:
1225 break;
1226 case DK_IF:
1227 return parseDirectiveIf(IDLoc);
1228 case DK_IFB:
1229 return parseDirectiveIfb(IDLoc, true);
1230 case DK_IFNB:
1231 return parseDirectiveIfb(IDLoc, false);
1232 case DK_IFC:
1233 return parseDirectiveIfc(IDLoc, true);
1234 case DK_IFNC:
1235 return parseDirectiveIfc(IDLoc, false);
1236 case DK_IFDEF:
1237 return parseDirectiveIfdef(IDLoc, true);
1238 case DK_IFNDEF:
1239 case DK_IFNOTDEF:
1240 return parseDirectiveIfdef(IDLoc, false);
1241 case DK_ELSEIF:
1242 return parseDirectiveElseIf(IDLoc);
1243 case DK_ELSE:
1244 return parseDirectiveElse(IDLoc);
1245 case DK_ENDIF:
1246 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001247 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001248
Eli Bendersky88024712013-01-16 19:32:36 +00001249 // Ignore the statement if in the middle of inactive conditional
1250 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001251 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001252 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001253 return false;
1254 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001255
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001256 // FIXME: Recurse on local labels?
1257
1258 // See what kind of statement we have.
1259 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001260 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001261 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001262
Chris Lattner36e02122009-06-21 20:54:55 +00001263 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001264 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001265
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001266 // Diagnose attempt to use '.' as a label.
1267 if (IDVal == ".")
1268 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1269
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001270 // Diagnose attempt to use a variable as a label.
1271 //
1272 // FIXME: Diagnostics. Note the location of the definition as a label.
1273 // FIXME: This doesn't diagnose assignment to a symbol which has been
1274 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001275 MCSymbol *Sym;
1276 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001277 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001278 else
1279 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001280 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001281 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001282
Daniel Dunbare73b2672009-08-26 22:13:22 +00001283 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001284 if (!ParsingInlineAsm)
1285 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001286
Kevin Enderbye7739d42011-12-09 18:09:40 +00001287 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001288 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001289 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001290 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1291 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001292
Tim Northover1744d0a2013-10-25 12:49:50 +00001293 getTargetParser().onLabelParsed(Sym);
1294
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001295 // Consume any end of statement token, if present, to avoid spurious
1296 // AddBlankLine calls().
1297 if (Lexer.is(AsmToken::EndOfStatement)) {
1298 Lex();
1299 if (Lexer.is(AsmToken::Eof))
1300 return false;
1301 }
1302
Eli Friedman0f4871d2012-10-22 23:58:19 +00001303 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001304 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001305
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001306 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001307 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001308 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001309
Jim Grosbach4b905842013-09-20 23:08:21 +00001310 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001311
1312 default: // Normal instruction or directive.
1313 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001314 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001315
1316 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001317 if (areMacrosEnabled())
1318 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1319 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001320 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001321
Michael J. Spencer530ce852010-10-09 11:00:50 +00001322 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001323
Eli Bendersky17233942013-01-15 22:59:42 +00001324 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001325 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001326 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001327 //
Eli Bendersky17233942013-01-15 22:59:42 +00001328 // 1. The target-specific assembly parser. Some directives are target
1329 // specific or may potentially behave differently on certain targets.
1330 // 2. Asm parser extensions. For example, platform-specific parsers
1331 // (like the ELF parser) register themselves as extensions.
1332 // 3. The generic directive parser implemented by this class. These are
1333 // all the directives that behave in a target and platform independent
1334 // manner, or at least have a default behavior that's shared between
1335 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001336
Eli Bendersky17233942013-01-15 22:59:42 +00001337 // First query the target-specific parser. It will return 'true' if it
1338 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001339 if (!getTargetParser().ParseDirective(ID))
1340 return false;
1341
Alp Tokercb402912014-01-24 17:20:08 +00001342 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001343 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001344 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1345 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001346 if (Handler.first)
1347 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1348
1349 // Finally, if no one else is interested in this directive, it must be
1350 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001351 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001352 default:
1353 break;
1354 case DK_SET:
1355 case DK_EQU:
1356 return parseDirectiveSet(IDVal, true);
1357 case DK_EQUIV:
1358 return parseDirectiveSet(IDVal, false);
1359 case DK_ASCII:
1360 return parseDirectiveAscii(IDVal, false);
1361 case DK_ASCIZ:
1362 case DK_STRING:
1363 return parseDirectiveAscii(IDVal, true);
1364 case DK_BYTE:
1365 return parseDirectiveValue(1);
1366 case DK_SHORT:
1367 case DK_VALUE:
1368 case DK_2BYTE:
1369 return parseDirectiveValue(2);
1370 case DK_LONG:
1371 case DK_INT:
1372 case DK_4BYTE:
1373 return parseDirectiveValue(4);
1374 case DK_QUAD:
1375 case DK_8BYTE:
1376 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001377 case DK_OCTA:
1378 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001379 case DK_SINGLE:
1380 case DK_FLOAT:
1381 return parseDirectiveRealValue(APFloat::IEEEsingle);
1382 case DK_DOUBLE:
1383 return parseDirectiveRealValue(APFloat::IEEEdouble);
1384 case DK_ALIGN: {
1385 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1386 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1387 }
1388 case DK_ALIGN32: {
1389 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1390 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1391 }
1392 case DK_BALIGN:
1393 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1394 case DK_BALIGNW:
1395 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1396 case DK_BALIGNL:
1397 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1398 case DK_P2ALIGN:
1399 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1400 case DK_P2ALIGNW:
1401 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1402 case DK_P2ALIGNL:
1403 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1404 case DK_ORG:
1405 return parseDirectiveOrg();
1406 case DK_FILL:
1407 return parseDirectiveFill();
1408 case DK_ZERO:
1409 return parseDirectiveZero();
1410 case DK_EXTERN:
1411 eatToEndOfStatement(); // .extern is the default, ignore it.
1412 return false;
1413 case DK_GLOBL:
1414 case DK_GLOBAL:
1415 return parseDirectiveSymbolAttribute(MCSA_Global);
1416 case DK_LAZY_REFERENCE:
1417 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1418 case DK_NO_DEAD_STRIP:
1419 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1420 case DK_SYMBOL_RESOLVER:
1421 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1422 case DK_PRIVATE_EXTERN:
1423 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1424 case DK_REFERENCE:
1425 return parseDirectiveSymbolAttribute(MCSA_Reference);
1426 case DK_WEAK_DEFINITION:
1427 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1428 case DK_WEAK_REFERENCE:
1429 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1430 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1431 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1432 case DK_COMM:
1433 case DK_COMMON:
1434 return parseDirectiveComm(/*IsLocal=*/false);
1435 case DK_LCOMM:
1436 return parseDirectiveComm(/*IsLocal=*/true);
1437 case DK_ABORT:
1438 return parseDirectiveAbort();
1439 case DK_INCLUDE:
1440 return parseDirectiveInclude();
1441 case DK_INCBIN:
1442 return parseDirectiveIncbin();
1443 case DK_CODE16:
1444 case DK_CODE16GCC:
1445 return TokError(Twine(IDVal) + " not supported yet");
1446 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001447 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001448 case DK_IRP:
1449 return parseDirectiveIrp(IDLoc);
1450 case DK_IRPC:
1451 return parseDirectiveIrpc(IDLoc);
1452 case DK_ENDR:
1453 return parseDirectiveEndr(IDLoc);
1454 case DK_BUNDLE_ALIGN_MODE:
1455 return parseDirectiveBundleAlignMode();
1456 case DK_BUNDLE_LOCK:
1457 return parseDirectiveBundleLock();
1458 case DK_BUNDLE_UNLOCK:
1459 return parseDirectiveBundleUnlock();
1460 case DK_SLEB128:
1461 return parseDirectiveLEB128(true);
1462 case DK_ULEB128:
1463 return parseDirectiveLEB128(false);
1464 case DK_SPACE:
1465 case DK_SKIP:
1466 return parseDirectiveSpace(IDVal);
1467 case DK_FILE:
1468 return parseDirectiveFile(IDLoc);
1469 case DK_LINE:
1470 return parseDirectiveLine();
1471 case DK_LOC:
1472 return parseDirectiveLoc();
1473 case DK_STABS:
1474 return parseDirectiveStabs();
1475 case DK_CFI_SECTIONS:
1476 return parseDirectiveCFISections();
1477 case DK_CFI_STARTPROC:
1478 return parseDirectiveCFIStartProc();
1479 case DK_CFI_ENDPROC:
1480 return parseDirectiveCFIEndProc();
1481 case DK_CFI_DEF_CFA:
1482 return parseDirectiveCFIDefCfa(IDLoc);
1483 case DK_CFI_DEF_CFA_OFFSET:
1484 return parseDirectiveCFIDefCfaOffset();
1485 case DK_CFI_ADJUST_CFA_OFFSET:
1486 return parseDirectiveCFIAdjustCfaOffset();
1487 case DK_CFI_DEF_CFA_REGISTER:
1488 return parseDirectiveCFIDefCfaRegister(IDLoc);
1489 case DK_CFI_OFFSET:
1490 return parseDirectiveCFIOffset(IDLoc);
1491 case DK_CFI_REL_OFFSET:
1492 return parseDirectiveCFIRelOffset(IDLoc);
1493 case DK_CFI_PERSONALITY:
1494 return parseDirectiveCFIPersonalityOrLsda(true);
1495 case DK_CFI_LSDA:
1496 return parseDirectiveCFIPersonalityOrLsda(false);
1497 case DK_CFI_REMEMBER_STATE:
1498 return parseDirectiveCFIRememberState();
1499 case DK_CFI_RESTORE_STATE:
1500 return parseDirectiveCFIRestoreState();
1501 case DK_CFI_SAME_VALUE:
1502 return parseDirectiveCFISameValue(IDLoc);
1503 case DK_CFI_RESTORE:
1504 return parseDirectiveCFIRestore(IDLoc);
1505 case DK_CFI_ESCAPE:
1506 return parseDirectiveCFIEscape();
1507 case DK_CFI_SIGNAL_FRAME:
1508 return parseDirectiveCFISignalFrame();
1509 case DK_CFI_UNDEFINED:
1510 return parseDirectiveCFIUndefined(IDLoc);
1511 case DK_CFI_REGISTER:
1512 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001513 case DK_CFI_WINDOW_SAVE:
1514 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001515 case DK_MACROS_ON:
1516 case DK_MACROS_OFF:
1517 return parseDirectiveMacrosOnOff(IDVal);
1518 case DK_MACRO:
1519 return parseDirectiveMacro(IDLoc);
1520 case DK_ENDM:
1521 case DK_ENDMACRO:
1522 return parseDirectiveEndMacro(IDVal);
1523 case DK_PURGEM:
1524 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001525 case DK_END:
1526 return parseDirectiveEnd(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001527 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001528
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001529 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001530 }
Chris Lattner36e02122009-06-21 20:54:55 +00001531
Chad Rosierc7f552c2013-02-12 21:33:51 +00001532 // __asm _emit or __asm __emit
1533 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1534 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001535 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001536
1537 // __asm align
1538 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001539 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001540
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001541 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001542
Chris Lattner7cbfa442010-05-19 23:34:33 +00001543 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001544 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001545 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001546 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001547 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001548 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001549
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001550 // Dump the parsed representation, if requested.
1551 if (getShowParsedOperands()) {
1552 SmallString<256> Str;
1553 raw_svector_ostream OS(Str);
1554 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001555 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001556 if (i != 0)
1557 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001558 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001559 }
1560 OS << "]";
1561
Jim Grosbach4b905842013-09-20 23:08:21 +00001562 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001563 }
1564
Kevin Enderby6469fc22011-11-01 22:27:22 +00001565 // If we are generating dwarf for assembly source files and the current
1566 // section is the initial text section then generate a .loc directive for
1567 // the instruction.
1568 if (!HadError && getContext().getGenDwarfForAssembly() &&
Peter Collingbourne2f495b92013-04-17 21:18:16 +00001569 getContext().getGenDwarfSection() ==
Jim Grosbach4b905842013-09-20 23:08:21 +00001570 getStreamer().getCurrentSection().first) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001571
Eli Bendersky88024712013-01-16 19:32:36 +00001572 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001573
Eli Bendersky88024712013-01-16 19:32:36 +00001574 // If we previously parsed a cpp hash file line comment then make sure the
1575 // current Dwarf File is for the CppHashFilename if not then emit the
1576 // Dwarf File table for it and adjust the line number for the .loc.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001577 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +00001578 getContext().getMCDwarfFiles();
Eli Bendersky88024712013-01-16 19:32:36 +00001579 if (CppHashFilename.size() != 0) {
1580 if (MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() !=
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001581 CppHashFilename)
Eli Bendersky88024712013-01-16 19:32:36 +00001582 getStreamer().EmitDwarfFileDirective(
Jim Grosbach4b905842013-09-20 23:08:21 +00001583 getContext().nextGenDwarfFileNumber(), StringRef(),
1584 CppHashFilename);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001585
Jim Grosbach4b905842013-09-20 23:08:21 +00001586 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1587 // cache with the different Loc from the call above we save the last
1588 // info we queried here with SrcMgr.FindLineNumber().
1589 unsigned CppHashLocLineNo;
1590 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1591 CppHashLocLineNo = LastQueryLine;
1592 else {
1593 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1594 LastQueryLine = CppHashLocLineNo;
1595 LastQueryIDLoc = CppHashLoc;
1596 LastQueryBuffer = CppHashBuf;
1597 }
1598 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001599 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001600
Jim Grosbach4b905842013-09-20 23:08:21 +00001601 getStreamer().EmitDwarfLocDirective(
1602 getContext().getGenDwarfFileNumber(), Line, 0,
1603 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1604 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001605 }
1606
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001607 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001608 if (!HadError) {
Chad Rosier49963552012-10-13 00:26:04 +00001609 unsigned ErrorInfo;
Jim Grosbach4b905842013-09-20 23:08:21 +00001610 HadError = getTargetParser().MatchAndEmitInstruction(
1611 IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo,
1612 ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001613 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001614
Chris Lattnera2a9d162010-09-11 16:18:25 +00001615 // Don't skip the rest of the line, the instruction parser is responsible for
1616 // that.
1617 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001618}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001619
Jim Grosbach4b905842013-09-20 23:08:21 +00001620/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001621/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001622void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001623 if (!Lexer.is(AsmToken::EndOfStatement))
1624 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001625 // Eat EOL.
1626 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001627}
1628
Jim Grosbach4b905842013-09-20 23:08:21 +00001629/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001630/// ::= # number "filename"
1631/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001632bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001633 Lex(); // Eat the hash token.
1634
1635 if (getLexer().isNot(AsmToken::Integer)) {
1636 // Consume the line since in cases it is not a well-formed line directive,
1637 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001638 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001639 return false;
1640 }
1641
1642 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001643 Lex();
1644
1645 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001646 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001647 return false;
1648 }
1649
1650 StringRef Filename = getTok().getString();
1651 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001652 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001653
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001654 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1655 CppHashLoc = L;
1656 CppHashFilename = Filename;
1657 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001658 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001659
1660 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001661 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001662 return false;
1663}
1664
Jim Grosbach4b905842013-09-20 23:08:21 +00001665/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001666/// for the Filename and LineNo if any in the diagnostic.
1667void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001668 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001669 raw_ostream &OS = errs();
1670
1671 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1672 const SMLoc &DiagLoc = Diag.getLoc();
1673 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1674 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1675
Jim Grosbach4b905842013-09-20 23:08:21 +00001676 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001677 // before printing the message.
1678 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001679 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001680 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1681 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001682 }
1683
Eric Christophera7c32732012-12-18 00:30:54 +00001684 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001685 // manager changed or buffer changed (like in a nested include) then just
1686 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001687 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001688 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001689 if (Parser->SavedDiagHandler)
1690 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1691 else
1692 Diag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001693 return;
1694 }
1695
Eric Christophera7c32732012-12-18 00:30:54 +00001696 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001697 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1698 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001699 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001700
1701 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1702 int CppHashLocLineNo =
1703 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001704 int LineNo =
1705 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001706
Jim Grosbach4b905842013-09-20 23:08:21 +00001707 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1708 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001709 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001710
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001711 if (Parser->SavedDiagHandler)
1712 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1713 else
1714 NewDiag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001715}
1716
Rafael Espindola2c064482012-08-21 18:29:30 +00001717// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1718// difference being that that function accepts '@' as part of identifiers and
1719// we can't do that. AsmLexer.cpp should probably be changed to handle
1720// '@' as a special case when needed.
1721static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001722 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1723 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001724}
1725
Rafael Espindola34b9c512012-06-03 23:57:14 +00001726bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001727 ArrayRef<MCAsmMacroParameter> Parameters,
1728 ArrayRef<MCAsmMacroArgument> A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001729 unsigned NParameters = Parameters.size();
1730 if (NParameters != 0 && NParameters != A.size())
1731 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001732
Preston Gurd05500642012-09-19 20:36:12 +00001733 // A macro without parameters is handled differently on Darwin:
1734 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001735 while (!Body.empty()) {
1736 // Scan for the next substitution.
1737 std::size_t End = Body.size(), Pos = 0;
1738 for (; Pos != End; ++Pos) {
1739 // Check for a substitution or escape.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001740 if (!NParameters) {
1741 // This macro has no parameters, look for $0, $1, etc.
1742 if (Body[Pos] != '$' || Pos + 1 == End)
1743 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001744
Rafael Espindola1134ab232011-06-05 02:43:45 +00001745 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001746 if (Next == '$' || Next == 'n' ||
1747 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001748 break;
1749 } else {
1750 // This macro has parameters, look for \foo, \bar, etc.
1751 if (Body[Pos] == '\\' && Pos + 1 != End)
1752 break;
1753 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001754 }
1755
1756 // Add the prefix.
1757 OS << Body.slice(0, Pos);
1758
1759 // Check if we reached the end.
1760 if (Pos == End)
1761 break;
1762
Rafael Espindola1134ab232011-06-05 02:43:45 +00001763 if (!NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001764 switch (Body[Pos + 1]) {
1765 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001766 case '$':
1767 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001768 break;
1769
Jim Grosbach4b905842013-09-20 23:08:21 +00001770 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001771 case 'n':
1772 OS << A.size();
1773 break;
1774
Jim Grosbach4b905842013-09-20 23:08:21 +00001775 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001776 default: {
1777 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001778 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001779 if (Index >= A.size())
1780 break;
1781
1782 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001783 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001784 ie = A[Index].end();
1785 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001786 OS << it->getString();
1787 break;
1788 }
1789 }
1790 Pos += 2;
1791 } else {
1792 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001793 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001794 ++I;
1795
Jim Grosbach4b905842013-09-20 23:08:21 +00001796 const char *Begin = Body.data() + Pos + 1;
1797 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001798 unsigned Index = 0;
1799 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001800 if (Parameters[Index].Name == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001801 break;
1802
Preston Gurd05500642012-09-19 20:36:12 +00001803 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001804 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1805 Pos += 3;
1806 else {
1807 OS << '\\' << Argument;
1808 Pos = I;
1809 }
Preston Gurd05500642012-09-19 20:36:12 +00001810 } else {
Eli Benderskya7b905e2013-01-14 19:00:26 +00001811 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001812 ie = A[Index].end();
1813 it != ie; ++it)
Preston Gurd05500642012-09-19 20:36:12 +00001814 if (it->getKind() == AsmToken::String)
1815 OS << it->getStringContents();
1816 else
1817 OS << it->getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001818
Preston Gurd05500642012-09-19 20:36:12 +00001819 Pos += 1 + Argument.size();
1820 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001821 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001822 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001823 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001824 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001825
Rafael Espindola1134ab232011-06-05 02:43:45 +00001826 return false;
1827}
Daniel Dunbar43235712010-07-18 18:54:11 +00001828
Jim Grosbach4b905842013-09-20 23:08:21 +00001829MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1830 SMLoc EL, MemoryBuffer *I)
1831 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1832 ExitLoc(EL) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001833
Jim Grosbach4b905842013-09-20 23:08:21 +00001834static bool isOperator(AsmToken::TokenKind kind) {
1835 switch (kind) {
1836 default:
1837 return false;
1838 case AsmToken::Plus:
1839 case AsmToken::Minus:
1840 case AsmToken::Tilde:
1841 case AsmToken::Slash:
1842 case AsmToken::Star:
1843 case AsmToken::Dot:
1844 case AsmToken::Equal:
1845 case AsmToken::EqualEqual:
1846 case AsmToken::Pipe:
1847 case AsmToken::PipePipe:
1848 case AsmToken::Caret:
1849 case AsmToken::Amp:
1850 case AsmToken::AmpAmp:
1851 case AsmToken::Exclaim:
1852 case AsmToken::ExclaimEqual:
1853 case AsmToken::Percent:
1854 case AsmToken::Less:
1855 case AsmToken::LessEqual:
1856 case AsmToken::LessLess:
1857 case AsmToken::LessGreater:
1858 case AsmToken::Greater:
1859 case AsmToken::GreaterEqual:
1860 case AsmToken::GreaterGreater:
1861 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001862 }
1863}
1864
David Majnemer16252452014-01-29 00:07:39 +00001865namespace {
1866class AsmLexerSkipSpaceRAII {
1867public:
1868 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1869 Lexer.setSkipSpace(SkipSpace);
1870 }
1871
1872 ~AsmLexerSkipSpaceRAII() {
1873 Lexer.setSkipSpace(true);
1874 }
1875
1876private:
1877 AsmLexer &Lexer;
1878};
1879}
1880
David Majnemer91fc4c22014-01-29 18:57:46 +00001881bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001882 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001883 unsigned AddTokens = 0;
1884
David Majnemer16252452014-01-29 00:07:39 +00001885 // Darwin doesn't use spaces to delmit arguments.
1886 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001887
1888 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001889 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001890 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001891
David Majnemer91fc4c22014-01-29 18:57:46 +00001892 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001893 break;
Preston Gurd05500642012-09-19 20:36:12 +00001894
1895 if (Lexer.is(AsmToken::Space)) {
1896 Lex(); // Eat spaces
1897
1898 // Spaces can delimit parameters, but could also be part an expression.
1899 // If the token after a space is an operator, add the token and the next
1900 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001901 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001902 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001903 // Check to see whether the token is used as an operator,
1904 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001905 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001906 if (*NextChar == ' ')
1907 AddTokens = 2;
1908 }
1909
1910 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001911 break;
1912 }
1913 }
1914 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001915
Jim Grosbach4b905842013-09-20 23:08:21 +00001916 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001917 // to be able to fill in the remaining default parameter values
1918 if (Lexer.is(AsmToken::EndOfStatement))
1919 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001920
1921 // Adjust the current parentheses level.
1922 if (Lexer.is(AsmToken::LParen))
1923 ++ParenLevel;
1924 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1925 --ParenLevel;
1926
1927 // Append the token to the current argument list.
1928 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001929 if (AddTokens)
1930 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001931 Lex();
1932 }
Preston Gurd05500642012-09-19 20:36:12 +00001933
Rafael Espindola768b41c2012-06-15 14:02:34 +00001934 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001935 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001936 return false;
1937}
1938
1939// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001940bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001941 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001942 const unsigned NParameters = M ? M->Parameters.size() : 0;
1943
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001944 A.resize(NParameters);
1945 for (unsigned PI = 0; PI < NParameters; ++PI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001946 if (!M->Parameters[PI].Value.empty())
1947 A[PI] = M->Parameters[PI].Value;
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001948
1949 bool NamedParametersFound = false;
1950
Rafael Espindola768b41c2012-06-15 14:02:34 +00001951 // Parse two kinds of macro invocations:
1952 // - macros defined without any parameters accept an arbitrary number of them
1953 // - macros defined with parameters accept at most that many of them
1954 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1955 ++Parameter) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001956 MCAsmMacroParameter FA;
1957 SMLoc L;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001958
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001959 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
1960 L = Lexer.getLoc();
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001961 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001962 Error(L, "invalid argument identifier for formal argument");
1963 eatToEndOfStatement();
1964 return true;
1965 }
1966
1967 if (!Lexer.is(AsmToken::Equal)) {
1968 TokError("expected '=' after formal parameter identifier");
1969 eatToEndOfStatement();
1970 return true;
1971 }
1972 Lex();
1973
1974 NamedParametersFound = true;
1975 }
1976
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001977 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001978 Error(Lexer.getLoc(), "cannot mix positional and keyword arguments");
1979 eatToEndOfStatement();
1980 return true;
1981 }
1982
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001983 if (parseMacroArgument(FA.Value))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001984 return true;
1985
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001986 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001987 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001988 unsigned FAI = 0;
1989 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001990 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001991 break;
1992 if (FAI >= NParameters) {
1993 Error(L,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001994 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001995 M->Name + "'");
1996 return true;
1997 }
1998 PI = FAI;
1999 }
2000
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002001 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002002 if (A.size() <= PI)
2003 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002004 A[PI] = FA.Value;
Preston Gurd242ed3152012-09-19 20:29:04 +00002005 }
Jim Grosbach206661622012-07-30 22:44:17 +00002006
Preston Gurd242ed3152012-09-19 20:29:04 +00002007 // At the end of the statement, fill in remaining arguments that have
2008 // default values. If there aren't any, then the next argument is
2009 // required but missing
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002010 if (Lexer.is(AsmToken::EndOfStatement))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002011 return false;
2012
2013 if (Lexer.is(AsmToken::Comma))
2014 Lex();
2015 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002016
2017 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002018}
2019
Jim Grosbach4b905842013-09-20 23:08:21 +00002020const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
2021 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002022 return (I == MacroMap.end()) ? NULL : I->getValue();
2023}
2024
Jim Grosbach4b905842013-09-20 23:08:21 +00002025void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00002026 MacroMap[Name] = new MCAsmMacro(Macro);
2027}
2028
Jim Grosbach4b905842013-09-20 23:08:21 +00002029void AsmParser::undefineMacro(StringRef Name) {
2030 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002031 if (I != MacroMap.end()) {
2032 delete I->getValue();
2033 MacroMap.erase(I);
2034 }
2035}
2036
Jim Grosbach4b905842013-09-20 23:08:21 +00002037bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002038 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2039 // this, although we should protect against infinite loops.
2040 if (ActiveMacros.size() == 20)
2041 return TokError("macros cannot be nested more than 20 levels deep");
2042
Eli Bendersky38274122013-01-14 23:22:36 +00002043 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002044 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002045 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002046
Rafael Espindola1134ab232011-06-05 02:43:45 +00002047 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2048 // to hold the macro body with substitutions.
2049 SmallString<256> Buf;
2050 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002051 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002052
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002053 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002054 return true;
2055
Eli Bendersky38274122013-01-14 23:22:36 +00002056 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002057 // instantiation.
2058 OS << ".endmacro\n";
2059
Rafael Espindola1134ab232011-06-05 02:43:45 +00002060 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00002061 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002062
Daniel Dunbar43235712010-07-18 18:54:11 +00002063 // Create the macro instantiation object and add to the current macro
2064 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00002065 MacroInstantiation *MI = new MacroInstantiation(
2066 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbar43235712010-07-18 18:54:11 +00002067 ActiveMacros.push_back(MI);
2068
2069 // Jump to the macro instantiation and prime the lexer.
2070 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
2071 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
2072 Lex();
2073
2074 return false;
2075}
2076
Jim Grosbach4b905842013-09-20 23:08:21 +00002077void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002078 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002079 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002080 Lex();
2081
2082 // Pop the instantiation entry.
2083 delete ActiveMacros.back();
2084 ActiveMacros.pop_back();
2085}
2086
Jim Grosbach4b905842013-09-20 23:08:21 +00002087static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002088 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002089 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002090 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2091 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002092 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002093 case MCExpr::Target:
2094 case MCExpr::Constant:
2095 return false;
2096 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002097 const MCSymbol &S =
2098 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002099 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002100 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002101 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002102 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002103 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002104 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002105 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002106
2107 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002108}
2109
Jim Grosbach4b905842013-09-20 23:08:21 +00002110bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002111 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002112 // FIXME: Use better location, we should use proper tokens.
2113 SMLoc EqualLoc = Lexer.getLoc();
2114
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002115 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002116 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002117 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002118
Rafael Espindola72f5f172012-01-28 05:57:00 +00002119 // Note: we don't count b as used in "a = b". This is to allow
2120 // a = b
2121 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002122
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002123 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002124 return TokError("unexpected token in assignment");
2125
2126 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002127 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002128
Daniel Dunbar5f339242009-10-16 01:57:39 +00002129 // Validate that the LHS is allowed to be a variable (either it has not been
2130 // used as a symbol, or it is an absolute symbol).
2131 MCSymbol *Sym = getContext().LookupSymbol(Name);
2132 if (Sym) {
2133 // Diagnose assignment to a label.
2134 //
2135 // FIXME: Diagnostics. Note the location of the definition as a label.
2136 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002137 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002138 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2139 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002140 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002141 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2142 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002143 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002144 return Error(EqualLoc, "redefinition of '" + Name + "'");
2145 else if (!Sym->isVariable())
2146 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002147 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002148 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002149 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002150
2151 // Don't count these checks as uses.
2152 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002153 } else if (Name == ".") {
2154 if (Out.EmitValueToOffset(Value, 0)) {
2155 Error(EqualLoc, "expected absolute expression");
2156 eatToEndOfStatement();
2157 }
2158 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002159 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002160 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002161
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002162 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002163 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002164 if (NoDeadStrip)
2165 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2166
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002167 return false;
2168}
2169
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002170/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002171/// ::= identifier
2172/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002173bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002174 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002175 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2176 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002177 // handle this as a context dependent token, instead we detect adjacent tokens
2178 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002179 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2180 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002181
Hans Wennborgce69d772013-10-18 20:46:28 +00002182 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002183 Lex();
2184 if (Lexer.isNot(AsmToken::Identifier))
2185 return true;
2186
Hans Wennborgce69d772013-10-18 20:46:28 +00002187 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2188 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002189 return true;
2190
2191 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002192 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002193 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002194 Lex();
2195 return false;
2196 }
2197
Jim Grosbach4b905842013-09-20 23:08:21 +00002198 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002199 return true;
2200
Sean Callanan936b0d32010-01-19 21:44:56 +00002201 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002202
Sean Callanan686ed8d2010-01-19 20:22:31 +00002203 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002204
2205 return false;
2206}
2207
Jim Grosbach4b905842013-09-20 23:08:21 +00002208/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002209/// ::= .equ identifier ',' expression
2210/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002211/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002212bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002213 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002214
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002215 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002216 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002217
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002218 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002219 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002220 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002221
Jim Grosbach4b905842013-09-20 23:08:21 +00002222 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002223}
2224
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002225bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002226 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002227
2228 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002229 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002230 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2231 if (Str[i] != '\\') {
2232 Data += Str[i];
2233 continue;
2234 }
2235
2236 // Recognize escaped characters. Note that this escape semantics currently
2237 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2238 ++i;
2239 if (i == e)
2240 return TokError("unexpected backslash at end of string");
2241
2242 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002243 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002244 // Consume up to three octal characters.
2245 unsigned Value = Str[i] - '0';
2246
Jim Grosbach4b905842013-09-20 23:08:21 +00002247 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002248 ++i;
2249 Value = Value * 8 + (Str[i] - '0');
2250
Jim Grosbach4b905842013-09-20 23:08:21 +00002251 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002252 ++i;
2253 Value = Value * 8 + (Str[i] - '0');
2254 }
2255 }
2256
2257 if (Value > 255)
2258 return TokError("invalid octal escape sequence (out of range)");
2259
Jim Grosbach4b905842013-09-20 23:08:21 +00002260 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002261 continue;
2262 }
2263
2264 // Otherwise recognize individual escapes.
2265 switch (Str[i]) {
2266 default:
2267 // Just reject invalid escape sequences for now.
2268 return TokError("invalid escape sequence (unrecognized character)");
2269
2270 case 'b': Data += '\b'; break;
2271 case 'f': Data += '\f'; break;
2272 case 'n': Data += '\n'; break;
2273 case 'r': Data += '\r'; break;
2274 case 't': Data += '\t'; break;
2275 case '"': Data += '"'; break;
2276 case '\\': Data += '\\'; break;
2277 }
2278 }
2279
2280 return false;
2281}
2282
Jim Grosbach4b905842013-09-20 23:08:21 +00002283/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002284/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002285bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002286 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002287 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002288
Daniel Dunbara10e5192009-06-24 23:30:00 +00002289 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002290 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002291 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002292
Daniel Dunbaref668c12009-08-14 18:19:52 +00002293 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002294 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002295 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002296
Rafael Espindola64e1af82013-07-02 15:49:13 +00002297 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002298 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002299 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002300
Sean Callanan686ed8d2010-01-19 20:22:31 +00002301 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002302
2303 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002304 break;
2305
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002306 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002307 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002308 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002309 }
2310 }
2311
Sean Callanan686ed8d2010-01-19 20:22:31 +00002312 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002313 return false;
2314}
2315
Jim Grosbach4b905842013-09-20 23:08:21 +00002316/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002317/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002318bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002319 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002320 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002321
Daniel Dunbara10e5192009-06-24 23:30:00 +00002322 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002323 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002324 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002325 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002326 return true;
2327
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002328 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002329 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2330 assert(Size <= 8 && "Invalid size");
2331 uint64_t IntValue = MCE->getValue();
2332 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2333 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002334 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002335 } else
Rafael Espindola64e1af82013-07-02 15:49:13 +00002336 getStreamer().EmitValue(Value, Size);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002337
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002338 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002339 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002340
Daniel Dunbara10e5192009-06-24 23:30:00 +00002341 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002342 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002343 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002344 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002345 }
2346 }
2347
Sean Callanan686ed8d2010-01-19 20:22:31 +00002348 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002349 return false;
2350}
2351
David Woodhoused6de0d92014-02-01 16:20:59 +00002352/// ParseDirectiveOctaValue
2353/// ::= .octa [ hexconstant (, hexconstant)* ]
2354bool AsmParser::parseDirectiveOctaValue() {
2355 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2356 checkForValidSection();
2357
2358 for (;;) {
2359 if (Lexer.getKind() == AsmToken::Error)
2360 return true;
2361 if (Lexer.getKind() != AsmToken::Integer &&
2362 Lexer.getKind() != AsmToken::BigNum)
2363 return TokError("unknown token in expression");
2364
2365 SMLoc ExprLoc = getLexer().getLoc();
2366 APInt IntValue = getTok().getAPIntVal();
2367 Lex();
2368
2369 uint64_t hi, lo;
2370 if (IntValue.isIntN(64)) {
2371 hi = 0;
2372 lo = IntValue.getZExtValue();
2373 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002374 // It might actually have more than 128 bits, but the top ones are zero.
2375 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002376 lo = IntValue.getLoBits(64).getZExtValue();
2377 } else
2378 return Error(ExprLoc, "literal value out of range for directive");
2379
2380 if (MAI.isLittleEndian()) {
2381 getStreamer().EmitIntValue(lo, 8);
2382 getStreamer().EmitIntValue(hi, 8);
2383 } else {
2384 getStreamer().EmitIntValue(hi, 8);
2385 getStreamer().EmitIntValue(lo, 8);
2386 }
2387
2388 if (getLexer().is(AsmToken::EndOfStatement))
2389 break;
2390
2391 // FIXME: Improve diagnostic.
2392 if (getLexer().isNot(AsmToken::Comma))
2393 return TokError("unexpected token in directive");
2394 Lex();
2395 }
2396 }
2397
2398 Lex();
2399 return false;
2400}
2401
Jim Grosbach4b905842013-09-20 23:08:21 +00002402/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002403/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002404bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002405 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002406 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002407
2408 for (;;) {
2409 // We don't truly support arithmetic on floating point expressions, so we
2410 // have to manually parse unary prefixes.
2411 bool IsNeg = false;
2412 if (getLexer().is(AsmToken::Minus)) {
2413 Lex();
2414 IsNeg = true;
2415 } else if (getLexer().is(AsmToken::Plus))
2416 Lex();
2417
Michael J. Spencer530ce852010-10-09 11:00:50 +00002418 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002419 getLexer().isNot(AsmToken::Real) &&
2420 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002421 return TokError("unexpected token in directive");
2422
2423 // Convert to an APFloat.
2424 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002425 StringRef IDVal = getTok().getString();
2426 if (getLexer().is(AsmToken::Identifier)) {
2427 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2428 Value = APFloat::getInf(Semantics);
2429 else if (!IDVal.compare_lower("nan"))
2430 Value = APFloat::getNaN(Semantics, false, ~0);
2431 else
2432 return TokError("invalid floating point literal");
2433 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002434 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002435 return TokError("invalid floating point literal");
2436 if (IsNeg)
2437 Value.changeSign();
2438
2439 // Consume the numeric token.
2440 Lex();
2441
2442 // Emit the value as an integer.
2443 APInt AsInt = Value.bitcastToAPInt();
2444 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002445 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002446
2447 if (getLexer().is(AsmToken::EndOfStatement))
2448 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002449
Daniel Dunbar2af16532010-09-24 01:59:56 +00002450 if (getLexer().isNot(AsmToken::Comma))
2451 return TokError("unexpected token in directive");
2452 Lex();
2453 }
2454 }
2455
2456 Lex();
2457 return false;
2458}
2459
Jim Grosbach4b905842013-09-20 23:08:21 +00002460/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002461/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002462bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002463 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002464
2465 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002466 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002467 return true;
2468
Rafael Espindolab91bac62010-10-05 19:42:57 +00002469 int64_t Val = 0;
2470 if (getLexer().is(AsmToken::Comma)) {
2471 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002472 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002473 return true;
2474 }
2475
Rafael Espindola922e3f42010-09-16 15:03:59 +00002476 if (getLexer().isNot(AsmToken::EndOfStatement))
2477 return TokError("unexpected token in '.zero' directive");
2478
2479 Lex();
2480
Rafael Espindola64e1af82013-07-02 15:49:13 +00002481 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002482
2483 return false;
2484}
2485
Jim Grosbach4b905842013-09-20 23:08:21 +00002486/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002487/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002488bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002489 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002490
David Majnemer522d3db2014-02-01 07:19:38 +00002491 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002492 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002493 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002494 return true;
2495
David Majnemer522d3db2014-02-01 07:19:38 +00002496 if (NumValues < 0) {
2497 Warning(RepeatLoc,
2498 "'.fill' directive with negative repeat count has no effect");
2499 NumValues = 0;
2500 }
2501
Roman Divackye33098f2013-09-24 17:44:41 +00002502 int64_t FillSize = 1;
2503 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002504
David Majnemer522d3db2014-02-01 07:19:38 +00002505 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002506 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2507 if (getLexer().isNot(AsmToken::Comma))
2508 return TokError("unexpected token in '.fill' directive");
2509 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002510
David Majnemer522d3db2014-02-01 07:19:38 +00002511 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002512 if (parseAbsoluteExpression(FillSize))
2513 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002514
Roman Divackye33098f2013-09-24 17:44:41 +00002515 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2516 if (getLexer().isNot(AsmToken::Comma))
2517 return TokError("unexpected token in '.fill' directive");
2518 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002519
David Majnemer522d3db2014-02-01 07:19:38 +00002520 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002521 if (parseAbsoluteExpression(FillExpr))
2522 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002523
Roman Divackye33098f2013-09-24 17:44:41 +00002524 if (getLexer().isNot(AsmToken::EndOfStatement))
2525 return TokError("unexpected token in '.fill' directive");
2526
2527 Lex();
2528 }
2529 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002530
David Majnemer522d3db2014-02-01 07:19:38 +00002531 if (FillSize < 0) {
2532 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2533 NumValues = 0;
2534 }
2535 if (FillSize > 8) {
2536 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2537 FillSize = 8;
2538 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002539
David Majnemer522d3db2014-02-01 07:19:38 +00002540 if (!isUInt<32>(FillExpr) && FillSize > 4)
2541 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2542
2543 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2544 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2545
2546 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2547 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2548 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2549 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002550
2551 return false;
2552}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002553
Jim Grosbach4b905842013-09-20 23:08:21 +00002554/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002555/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002556bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002557 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002558
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002559 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002560 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002561 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002562 return true;
2563
2564 // Parse optional fill expression.
2565 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002566 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2567 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002568 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002569 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002570
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002571 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002572 return true;
2573
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002574 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002575 return TokError("unexpected token in '.org' directive");
2576 }
2577
Sean Callanan686ed8d2010-01-19 20:22:31 +00002578 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002579
Jim Grosbachb5912772012-01-27 00:37:08 +00002580 // Only limited forms of relocatable expressions are accepted here, it
2581 // has to be relative to the current section. The streamer will return
2582 // 'true' if the expression wasn't evaluatable.
2583 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2584 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002585
2586 return false;
2587}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002588
Jim Grosbach4b905842013-09-20 23:08:21 +00002589/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002590/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002591bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002592 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002593
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002594 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002595 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002596 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002597 return true;
2598
2599 SMLoc MaxBytesLoc;
2600 bool HasFillExpr = false;
2601 int64_t FillExpr = 0;
2602 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002603 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2604 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002605 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002606 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002607
2608 // The fill expression can be omitted while specifying a maximum number of
2609 // alignment bytes, e.g:
2610 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002611 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002612 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002613 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002614 return true;
2615 }
2616
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002617 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2618 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002619 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002620 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002621
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002622 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002623 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002624 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002625
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002626 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002627 return TokError("unexpected token in directive");
2628 }
2629 }
2630
Sean Callanan686ed8d2010-01-19 20:22:31 +00002631 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002632
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002633 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002634 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002635
2636 // Compute alignment in bytes.
2637 if (IsPow2) {
2638 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002639 if (Alignment >= 32) {
2640 Error(AlignmentLoc, "invalid alignment value");
2641 Alignment = 31;
2642 }
2643
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002644 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002645 } else {
2646 // Reject alignments that aren't a power of two, for gas compatibility.
2647 if (!isPowerOf2_64(Alignment))
2648 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002649 }
2650
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002651 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002652 if (MaxBytesLoc.isValid()) {
2653 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002654 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002655 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002656 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002657 }
2658
2659 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002660 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002661 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002662 MaxBytesToFill = 0;
2663 }
2664 }
2665
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002666 // Check whether we should use optimal code alignment for this .align
2667 // directive.
Peter Collingbourne2f495b92013-04-17 21:18:16 +00002668 bool UseCodeAlign = getStreamer().getCurrentSection().first->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002669 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2670 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002671 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002672 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002673 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002674 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2675 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002676 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002677
2678 return false;
2679}
2680
Jim Grosbach4b905842013-09-20 23:08:21 +00002681/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002682/// ::= .file [number] filename
2683/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002684bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002685 // FIXME: I'm not sure what this is.
2686 int64_t FileNumber = -1;
2687 SMLoc FileNumberLoc = getLexer().getLoc();
2688 if (getLexer().is(AsmToken::Integer)) {
2689 FileNumber = getTok().getIntVal();
2690 Lex();
2691
2692 if (FileNumber < 1)
2693 return TokError("file number less than one");
2694 }
2695
2696 if (getLexer().isNot(AsmToken::String))
2697 return TokError("unexpected token in '.file' directive");
2698
2699 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002700 // Allow the strings to have escaped octal character sequence.
2701 std::string Path = getTok().getString();
2702 if (parseEscapedString(Path))
2703 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002704 Lex();
2705
2706 StringRef Directory;
2707 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002708 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002709 if (getLexer().is(AsmToken::String)) {
2710 if (FileNumber == -1)
2711 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002712 if (parseEscapedString(FilenameData))
2713 return true;
2714 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002715 Directory = Path;
2716 Lex();
2717 } else {
2718 Filename = Path;
2719 }
2720
2721 if (getLexer().isNot(AsmToken::EndOfStatement))
2722 return TokError("unexpected token in '.file' directive");
2723
2724 if (FileNumber == -1)
2725 getStreamer().EmitFileDirective(Filename);
2726 else {
2727 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002728 Error(DirectiveLoc,
2729 "input can't have .file dwarf directives when -g is "
2730 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002731
2732 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
2733 Error(FileNumberLoc, "file number already allocated");
2734 }
2735
2736 return false;
2737}
2738
Jim Grosbach4b905842013-09-20 23:08:21 +00002739/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002740/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002741bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002742 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2743 if (getLexer().isNot(AsmToken::Integer))
2744 return TokError("unexpected token in '.line' directive");
2745
2746 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002747 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002748 Lex();
2749
2750 // FIXME: Do something with the .line.
2751 }
2752
2753 if (getLexer().isNot(AsmToken::EndOfStatement))
2754 return TokError("unexpected token in '.line' directive");
2755
2756 return false;
2757}
2758
Jim Grosbach4b905842013-09-20 23:08:21 +00002759/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002760/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2761/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2762/// The first number is a file number, must have been previously assigned with
2763/// a .file directive, the second number is the line number and optionally the
2764/// third number is a column position (zero if not specified). The remaining
2765/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002766bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002767 if (getLexer().isNot(AsmToken::Integer))
2768 return TokError("unexpected token in '.loc' directive");
2769 int64_t FileNumber = getTok().getIntVal();
2770 if (FileNumber < 1)
2771 return TokError("file number less than one in '.loc' directive");
2772 if (!getContext().isValidDwarfFileNumber(FileNumber))
2773 return TokError("unassigned file number in '.loc' directive");
2774 Lex();
2775
2776 int64_t LineNumber = 0;
2777 if (getLexer().is(AsmToken::Integer)) {
2778 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002779 if (LineNumber < 0)
2780 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002781 Lex();
2782 }
2783
2784 int64_t ColumnPos = 0;
2785 if (getLexer().is(AsmToken::Integer)) {
2786 ColumnPos = getTok().getIntVal();
2787 if (ColumnPos < 0)
2788 return TokError("column position less than zero in '.loc' directive");
2789 Lex();
2790 }
2791
2792 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2793 unsigned Isa = 0;
2794 int64_t Discriminator = 0;
2795 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2796 for (;;) {
2797 if (getLexer().is(AsmToken::EndOfStatement))
2798 break;
2799
2800 StringRef Name;
2801 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002802 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002803 return TokError("unexpected token in '.loc' directive");
2804
2805 if (Name == "basic_block")
2806 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2807 else if (Name == "prologue_end")
2808 Flags |= DWARF2_FLAG_PROLOGUE_END;
2809 else if (Name == "epilogue_begin")
2810 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2811 else if (Name == "is_stmt") {
2812 Loc = getTok().getLoc();
2813 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002814 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002815 return true;
2816 // The expression must be the constant 0 or 1.
2817 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2818 int Value = MCE->getValue();
2819 if (Value == 0)
2820 Flags &= ~DWARF2_FLAG_IS_STMT;
2821 else if (Value == 1)
2822 Flags |= DWARF2_FLAG_IS_STMT;
2823 else
2824 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002825 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002826 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2827 }
Craig Topperf15655b2013-04-22 04:22:40 +00002828 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002829 Loc = getTok().getLoc();
2830 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002831 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002832 return true;
2833 // The expression must be a constant greater or equal to 0.
2834 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2835 int Value = MCE->getValue();
2836 if (Value < 0)
2837 return Error(Loc, "isa number less than zero");
2838 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002839 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002840 return Error(Loc, "isa number not a constant value");
2841 }
Craig Topperf15655b2013-04-22 04:22:40 +00002842 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002843 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002844 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002845 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002846 return Error(Loc, "unknown sub-directive in '.loc' directive");
2847 }
2848
2849 if (getLexer().is(AsmToken::EndOfStatement))
2850 break;
2851 }
2852 }
2853
2854 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2855 Isa, Discriminator, StringRef());
2856
2857 return false;
2858}
2859
Jim Grosbach4b905842013-09-20 23:08:21 +00002860/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002861/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002862bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002863 return TokError("unsupported directive '.stabs'");
2864}
2865
Jim Grosbach4b905842013-09-20 23:08:21 +00002866/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002867/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002868bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002869 StringRef Name;
2870 bool EH = false;
2871 bool Debug = false;
2872
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002873 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002874 return TokError("Expected an identifier");
2875
2876 if (Name == ".eh_frame")
2877 EH = true;
2878 else if (Name == ".debug_frame")
2879 Debug = true;
2880
2881 if (getLexer().is(AsmToken::Comma)) {
2882 Lex();
2883
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002884 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002885 return TokError("Expected an identifier");
2886
2887 if (Name == ".eh_frame")
2888 EH = true;
2889 else if (Name == ".debug_frame")
2890 Debug = true;
2891 }
2892
2893 getStreamer().EmitCFISections(EH, Debug);
2894 return false;
2895}
2896
Jim Grosbach4b905842013-09-20 23:08:21 +00002897/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002898/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002899bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002900 StringRef Simple;
2901 if (getLexer().isNot(AsmToken::EndOfStatement))
2902 if (parseIdentifier(Simple) || Simple != "simple")
2903 return TokError("unexpected token in .cfi_startproc directive");
2904
2905 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002906 return false;
2907}
2908
Jim Grosbach4b905842013-09-20 23:08:21 +00002909/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002910/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002911bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002912 getStreamer().EmitCFIEndProc();
2913 return false;
2914}
2915
Jim Grosbach4b905842013-09-20 23:08:21 +00002916/// \brief parse register name or number.
2917bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002918 SMLoc DirectiveLoc) {
2919 unsigned RegNo;
2920
2921 if (getLexer().isNot(AsmToken::Integer)) {
2922 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2923 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002924 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002925 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002926 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002927
2928 return false;
2929}
2930
Jim Grosbach4b905842013-09-20 23:08:21 +00002931/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002932/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002933bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002934 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002935 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002936 return true;
2937
2938 if (getLexer().isNot(AsmToken::Comma))
2939 return TokError("unexpected token in directive");
2940 Lex();
2941
2942 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002943 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002944 return true;
2945
2946 getStreamer().EmitCFIDefCfa(Register, Offset);
2947 return false;
2948}
2949
Jim Grosbach4b905842013-09-20 23:08:21 +00002950/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002951/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002952bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002953 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002954 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002955 return true;
2956
2957 getStreamer().EmitCFIDefCfaOffset(Offset);
2958 return false;
2959}
2960
Jim Grosbach4b905842013-09-20 23:08:21 +00002961/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002962/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00002963bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002964 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002965 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002966 return true;
2967
2968 if (getLexer().isNot(AsmToken::Comma))
2969 return TokError("unexpected token in directive");
2970 Lex();
2971
2972 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002973 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002974 return true;
2975
2976 getStreamer().EmitCFIRegister(Register1, Register2);
2977 return false;
2978}
2979
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00002980/// parseDirectiveCFIWindowSave
2981/// ::= .cfi_window_save
2982bool AsmParser::parseDirectiveCFIWindowSave() {
2983 getStreamer().EmitCFIWindowSave();
2984 return false;
2985}
2986
Jim Grosbach4b905842013-09-20 23:08:21 +00002987/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002988/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00002989bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002990 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002991 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00002992 return true;
2993
2994 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2995 return false;
2996}
2997
Jim Grosbach4b905842013-09-20 23:08:21 +00002998/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002999/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003000bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003001 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003002 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003003 return true;
3004
3005 getStreamer().EmitCFIDefCfaRegister(Register);
3006 return false;
3007}
3008
Jim Grosbach4b905842013-09-20 23:08:21 +00003009/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003010/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003011bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003012 int64_t Register = 0;
3013 int64_t Offset = 0;
3014
Jim Grosbach4b905842013-09-20 23:08:21 +00003015 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003016 return true;
3017
3018 if (getLexer().isNot(AsmToken::Comma))
3019 return TokError("unexpected token in directive");
3020 Lex();
3021
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003022 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003023 return true;
3024
3025 getStreamer().EmitCFIOffset(Register, Offset);
3026 return false;
3027}
3028
Jim Grosbach4b905842013-09-20 23:08:21 +00003029/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003030/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003031bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003032 int64_t Register = 0;
3033
Jim Grosbach4b905842013-09-20 23:08:21 +00003034 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003035 return true;
3036
3037 if (getLexer().isNot(AsmToken::Comma))
3038 return TokError("unexpected token in directive");
3039 Lex();
3040
3041 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003042 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003043 return true;
3044
3045 getStreamer().EmitCFIRelOffset(Register, Offset);
3046 return false;
3047}
3048
3049static bool isValidEncoding(int64_t Encoding) {
3050 if (Encoding & ~0xff)
3051 return false;
3052
3053 if (Encoding == dwarf::DW_EH_PE_omit)
3054 return true;
3055
3056 const unsigned Format = Encoding & 0xf;
3057 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3058 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3059 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3060 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3061 return false;
3062
3063 const unsigned Application = Encoding & 0x70;
3064 if (Application != dwarf::DW_EH_PE_absptr &&
3065 Application != dwarf::DW_EH_PE_pcrel)
3066 return false;
3067
3068 return true;
3069}
3070
Jim Grosbach4b905842013-09-20 23:08:21 +00003071/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003072/// IsPersonality true for cfi_personality, false for cfi_lsda
3073/// ::= .cfi_personality encoding, [symbol_name]
3074/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003075bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003076 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003077 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003078 return true;
3079 if (Encoding == dwarf::DW_EH_PE_omit)
3080 return false;
3081
3082 if (!isValidEncoding(Encoding))
3083 return TokError("unsupported encoding.");
3084
3085 if (getLexer().isNot(AsmToken::Comma))
3086 return TokError("unexpected token in directive");
3087 Lex();
3088
3089 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003090 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003091 return TokError("expected identifier in directive");
3092
3093 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3094
3095 if (IsPersonality)
3096 getStreamer().EmitCFIPersonality(Sym, Encoding);
3097 else
3098 getStreamer().EmitCFILsda(Sym, Encoding);
3099 return false;
3100}
3101
Jim Grosbach4b905842013-09-20 23:08:21 +00003102/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003103/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003104bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003105 getStreamer().EmitCFIRememberState();
3106 return false;
3107}
3108
Jim Grosbach4b905842013-09-20 23:08:21 +00003109/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003110/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003111bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003112 getStreamer().EmitCFIRestoreState();
3113 return false;
3114}
3115
Jim Grosbach4b905842013-09-20 23:08:21 +00003116/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003117/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003118bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003119 int64_t Register = 0;
3120
Jim Grosbach4b905842013-09-20 23:08:21 +00003121 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003122 return true;
3123
3124 getStreamer().EmitCFISameValue(Register);
3125 return false;
3126}
3127
Jim Grosbach4b905842013-09-20 23:08:21 +00003128/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003129/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003130bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003131 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003132 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003133 return true;
3134
3135 getStreamer().EmitCFIRestore(Register);
3136 return false;
3137}
3138
Jim Grosbach4b905842013-09-20 23:08:21 +00003139/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003140/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003141bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003142 std::string Values;
3143 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003144 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003145 return true;
3146
3147 Values.push_back((uint8_t)CurrValue);
3148
3149 while (getLexer().is(AsmToken::Comma)) {
3150 Lex();
3151
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003152 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003153 return true;
3154
3155 Values.push_back((uint8_t)CurrValue);
3156 }
3157
3158 getStreamer().EmitCFIEscape(Values);
3159 return false;
3160}
3161
Jim Grosbach4b905842013-09-20 23:08:21 +00003162/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003163/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003164bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003165 if (getLexer().isNot(AsmToken::EndOfStatement))
3166 return Error(getLexer().getLoc(),
3167 "unexpected token in '.cfi_signal_frame'");
3168
3169 getStreamer().EmitCFISignalFrame();
3170 return false;
3171}
3172
Jim Grosbach4b905842013-09-20 23:08:21 +00003173/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003174/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003175bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003176 int64_t Register = 0;
3177
Jim Grosbach4b905842013-09-20 23:08:21 +00003178 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003179 return true;
3180
3181 getStreamer().EmitCFIUndefined(Register);
3182 return false;
3183}
3184
Jim Grosbach4b905842013-09-20 23:08:21 +00003185/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003186/// ::= .macros_on
3187/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003188bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003189 if (getLexer().isNot(AsmToken::EndOfStatement))
3190 return Error(getLexer().getLoc(),
3191 "unexpected token in '" + Directive + "' directive");
3192
Jim Grosbach4b905842013-09-20 23:08:21 +00003193 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003194 return false;
3195}
3196
Jim Grosbach4b905842013-09-20 23:08:21 +00003197/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003198/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003199bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003200 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003201 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003202 return TokError("expected identifier in '.macro' directive");
3203
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003204 if (getLexer().is(AsmToken::Comma))
3205 Lex();
3206
Eli Bendersky17233942013-01-15 22:59:42 +00003207 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003208 while (getLexer().isNot(AsmToken::EndOfStatement)) {
3209 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003210 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003211 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003212
David Majnemer91fc4c22014-01-29 18:57:46 +00003213 if (getLexer().is(AsmToken::Equal)) {
3214 Lex();
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003215 if (parseMacroArgument(Parameter.Value))
David Majnemer91fc4c22014-01-29 18:57:46 +00003216 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003217 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003218
3219 Parameters.push_back(Parameter);
3220
3221 if (getLexer().is(AsmToken::Comma))
3222 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003223 }
3224
3225 // Eat the end of statement.
3226 Lex();
3227
3228 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003229 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003230
3231 // Lex the macro definition.
3232 for (;;) {
3233 // Check whether we have reached the end of the file.
3234 if (getLexer().is(AsmToken::Eof))
3235 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3236
3237 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003238 if (getLexer().is(AsmToken::Identifier)) {
3239 if (getTok().getIdentifier() == ".endm" ||
3240 getTok().getIdentifier() == ".endmacro") {
3241 if (MacroDepth == 0) { // Outermost macro.
3242 EndToken = getTok();
3243 Lex();
3244 if (getLexer().isNot(AsmToken::EndOfStatement))
3245 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3246 "' directive");
3247 break;
3248 } else {
3249 // Otherwise we just found the end of an inner macro.
3250 --MacroDepth;
3251 }
3252 } else if (getTok().getIdentifier() == ".macro") {
3253 // We allow nested macros. Those aren't instantiated until the outermost
3254 // macro is expanded so just ignore them for now.
3255 ++MacroDepth;
3256 }
Eli Bendersky17233942013-01-15 22:59:42 +00003257 }
3258
3259 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003260 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003261 }
3262
Jim Grosbach4b905842013-09-20 23:08:21 +00003263 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003264 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3265 }
3266
3267 const char *BodyStart = StartToken.getLoc().getPointer();
3268 const char *BodyEnd = EndToken.getLoc().getPointer();
3269 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003270 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3271 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003272 return false;
3273}
3274
Jim Grosbach4b905842013-09-20 23:08:21 +00003275/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003276///
3277/// With the support added for named parameters there may be code out there that
3278/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003279/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003280/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003281/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003282/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3283/// warning that the positional parameter found in body which have no effect.
3284/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003285/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003286/// intended or change the macro to use the named parameters. It is possible
3287/// this warning will trigger when the none of the named parameters are used
3288/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003289void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003290 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003291 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003292 // If this macro is not defined with named parameters the warning we are
3293 // checking for here doesn't apply.
3294 unsigned NParameters = Parameters.size();
3295 if (NParameters == 0)
3296 return;
3297
3298 bool NamedParametersFound = false;
3299 bool PositionalParametersFound = false;
3300
3301 // Look at the body of the macro for use of both the named parameters and what
3302 // are likely to be positional parameters. This is what expandMacro() is
3303 // doing when it finds the parameters in the body.
3304 while (!Body.empty()) {
3305 // Scan for the next possible parameter.
3306 std::size_t End = Body.size(), Pos = 0;
3307 for (; Pos != End; ++Pos) {
3308 // Check for a substitution or escape.
3309 // This macro is defined with parameters, look for \foo, \bar, etc.
3310 if (Body[Pos] == '\\' && Pos + 1 != End)
3311 break;
3312
3313 // This macro should have parameters, but look for $0, $1, ..., $n too.
3314 if (Body[Pos] != '$' || Pos + 1 == End)
3315 continue;
3316 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003317 if (Next == '$' || Next == 'n' ||
3318 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003319 break;
3320 }
3321
3322 // Check if we reached the end.
3323 if (Pos == End)
3324 break;
3325
3326 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003327 switch (Body[Pos + 1]) {
3328 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003329 case '$':
3330 break;
3331
Jim Grosbach4b905842013-09-20 23:08:21 +00003332 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003333 case 'n':
3334 PositionalParametersFound = true;
3335 break;
3336
Jim Grosbach4b905842013-09-20 23:08:21 +00003337 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003338 default: {
3339 PositionalParametersFound = true;
3340 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003341 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003342 }
3343 Pos += 2;
3344 } else {
3345 unsigned I = Pos + 1;
3346 while (isIdentifierChar(Body[I]) && I + 1 != End)
3347 ++I;
3348
Jim Grosbach4b905842013-09-20 23:08:21 +00003349 const char *Begin = Body.data() + Pos + 1;
3350 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003351 unsigned Index = 0;
3352 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003353 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003354 break;
3355
3356 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003357 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3358 Pos += 3;
3359 else {
3360 Pos = I;
3361 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003362 } else {
3363 NamedParametersFound = true;
3364 Pos += 1 + Argument.size();
3365 }
3366 }
3367 // Update the scan point.
3368 Body = Body.substr(Pos);
3369 }
3370
3371 if (!NamedParametersFound && PositionalParametersFound)
3372 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3373 "used in macro body, possible positional parameter "
3374 "found in body which will have no effect");
3375}
3376
Jim Grosbach4b905842013-09-20 23:08:21 +00003377/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003378/// ::= .endm
3379/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003380bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003381 if (getLexer().isNot(AsmToken::EndOfStatement))
3382 return TokError("unexpected token in '" + Directive + "' directive");
3383
3384 // If we are inside a macro instantiation, terminate the current
3385 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003386 if (isInsideMacroInstantiation()) {
3387 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003388 return false;
3389 }
3390
3391 // Otherwise, this .endmacro is a stray entry in the file; well formed
3392 // .endmacro directives are handled during the macro definition parsing.
3393 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003394 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003395}
3396
Jim Grosbach4b905842013-09-20 23:08:21 +00003397/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003398/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003399bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003400 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003401 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003402 return TokError("expected identifier in '.purgem' directive");
3403
3404 if (getLexer().isNot(AsmToken::EndOfStatement))
3405 return TokError("unexpected token in '.purgem' directive");
3406
Jim Grosbach4b905842013-09-20 23:08:21 +00003407 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003408 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3409
Jim Grosbach4b905842013-09-20 23:08:21 +00003410 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003411 return false;
3412}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003413
Jim Grosbach4b905842013-09-20 23:08:21 +00003414/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003415/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003416bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003417 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003418
3419 // Expect a single argument: an expression that evaluates to a constant
3420 // in the inclusive range 0-30.
3421 SMLoc ExprLoc = getLexer().getLoc();
3422 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003423 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003424 return true;
3425 else if (getLexer().isNot(AsmToken::EndOfStatement))
3426 return TokError("unexpected token after expression in"
3427 " '.bundle_align_mode' directive");
3428 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3429 return Error(ExprLoc,
3430 "invalid bundle alignment size (expected between 0 and 30)");
3431
3432 Lex();
3433
3434 // Because of AlignSizePow2's verified range we can safely truncate it to
3435 // unsigned.
3436 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3437 return false;
3438}
3439
Jim Grosbach4b905842013-09-20 23:08:21 +00003440/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003441/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003442bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003443 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003444 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003445
Eli Bendersky802b6282013-01-07 21:51:08 +00003446 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3447 StringRef Option;
3448 SMLoc Loc = getTok().getLoc();
3449 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003450 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003451
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003452 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003453 return Error(Loc, kInvalidOptionError);
3454
3455 if (Option != "align_to_end")
3456 return Error(Loc, kInvalidOptionError);
3457 else if (getLexer().isNot(AsmToken::EndOfStatement))
3458 return Error(Loc,
3459 "unexpected token after '.bundle_lock' directive option");
3460 AlignToEnd = true;
3461 }
3462
Eli Benderskyf483ff92012-12-20 19:05:53 +00003463 Lex();
3464
Eli Bendersky802b6282013-01-07 21:51:08 +00003465 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003466 return false;
3467}
3468
Jim Grosbach4b905842013-09-20 23:08:21 +00003469/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003470/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003471bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003472 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003473
3474 if (getLexer().isNot(AsmToken::EndOfStatement))
3475 return TokError("unexpected token in '.bundle_unlock' directive");
3476 Lex();
3477
3478 getStreamer().EmitBundleUnlock();
3479 return false;
3480}
3481
Jim Grosbach4b905842013-09-20 23:08:21 +00003482/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003483/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003484bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003485 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003486
3487 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003488 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003489 return true;
3490
3491 int64_t FillExpr = 0;
3492 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3493 if (getLexer().isNot(AsmToken::Comma))
3494 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3495 Lex();
3496
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003497 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003498 return true;
3499
3500 if (getLexer().isNot(AsmToken::EndOfStatement))
3501 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3502 }
3503
3504 Lex();
3505
3506 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003507 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3508 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003509
3510 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003511 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003512
3513 return false;
3514}
3515
Jim Grosbach4b905842013-09-20 23:08:21 +00003516/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003517/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003518bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003519 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003520 const MCExpr *Value;
3521
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003522 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003523 return true;
3524
3525 if (getLexer().isNot(AsmToken::EndOfStatement))
3526 return TokError("unexpected token in directive");
3527
3528 if (Signed)
3529 getStreamer().EmitSLEB128Value(Value);
3530 else
3531 getStreamer().EmitULEB128Value(Value);
3532
3533 return false;
3534}
3535
Jim Grosbach4b905842013-09-20 23:08:21 +00003536/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003537/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003538bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003539 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003540 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003541 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003542 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003543
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003544 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003545 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003546
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003547 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003548
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003549 // Assembler local symbols don't make any sense here. Complain loudly.
3550 if (Sym->isTemporary())
3551 return Error(Loc, "non-local symbol required in directive");
3552
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003553 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3554 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003555
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003556 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003557 break;
3558
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003559 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003560 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003561 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003562 }
3563 }
3564
Sean Callanan686ed8d2010-01-19 20:22:31 +00003565 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003566 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003567}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003568
Jim Grosbach4b905842013-09-20 23:08:21 +00003569/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003570/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003571bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003572 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003573
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003574 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003575 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003576 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003577 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003578
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003579 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003580 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003581
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003582 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003583 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003584 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003585
3586 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003587 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003588 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003589 return true;
3590
3591 int64_t Pow2Alignment = 0;
3592 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003593 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003594 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003595 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003596 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003597 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003598
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003599 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3600 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003601 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3602
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003603 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003604 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3605 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003606 if (!isPowerOf2_64(Pow2Alignment))
3607 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3608 Pow2Alignment = Log2_64(Pow2Alignment);
3609 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003610 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003611
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003612 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003613 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003614
Sean Callanan686ed8d2010-01-19 20:22:31 +00003615 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003616
Chris Lattner28ad7542009-07-09 17:25:12 +00003617 // NOTE: a size of zero for a .comm should create a undefined symbol
3618 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003619 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003620 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003621 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003622
Eric Christopherbc818852010-05-14 01:38:54 +00003623 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003624 // may internally end up wanting an alignment in bytes.
3625 // FIXME: Diagnose overflow.
3626 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003627 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003628 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003629
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003630 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003631 return Error(IDLoc, "invalid symbol redefinition");
3632
Chris Lattner28ad7542009-07-09 17:25:12 +00003633 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003634 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003635 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003636 return false;
3637 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003638
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003639 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003640 return false;
3641}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003642
Jim Grosbach4b905842013-09-20 23:08:21 +00003643/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003644/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003645bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003646 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003647 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003648
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003649 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003650 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003651 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003652
Sean Callanan686ed8d2010-01-19 20:22:31 +00003653 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003654
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003655 if (Str.empty())
3656 Error(Loc, ".abort detected. Assembly stopping.");
3657 else
3658 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003659 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003660
3661 return false;
3662}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003663
Jim Grosbach4b905842013-09-20 23:08:21 +00003664/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003665/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003666bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003667 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003668 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003669
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003670 // Allow the strings to have escaped octal character sequence.
3671 std::string Filename;
3672 if (parseEscapedString(Filename))
3673 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003674 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003675 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003676
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003677 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003678 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003679
Chris Lattner693fbb82009-07-16 06:14:39 +00003680 // Attempt to switch the lexer to the included file before consuming the end
3681 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003682 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003683 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003684 return true;
3685 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003686
3687 return false;
3688}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003689
Jim Grosbach4b905842013-09-20 23:08:21 +00003690/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003691/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003692bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003693 if (getLexer().isNot(AsmToken::String))
3694 return TokError("expected string in '.incbin' directive");
3695
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003696 // Allow the strings to have escaped octal character sequence.
3697 std::string Filename;
3698 if (parseEscapedString(Filename))
3699 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003700 SMLoc IncbinLoc = getLexer().getLoc();
3701 Lex();
3702
3703 if (getLexer().isNot(AsmToken::EndOfStatement))
3704 return TokError("unexpected token in '.incbin' directive");
3705
Kevin Enderby109f25c2011-12-14 21:47:48 +00003706 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003707 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003708 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3709 return true;
3710 }
3711
3712 return false;
3713}
3714
Jim Grosbach4b905842013-09-20 23:08:21 +00003715/// parseDirectiveIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003716/// ::= .if expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003717bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003718 TheCondStack.push_back(TheCondState);
3719 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003720 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003721 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003722 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003723 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003724 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003725 return true;
3726
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003727 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003728 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003729
Sean Callanan686ed8d2010-01-19 20:22:31 +00003730 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003731
3732 TheCondState.CondMet = ExprValue;
3733 TheCondState.Ignore = !TheCondState.CondMet;
3734 }
3735
3736 return false;
3737}
3738
Jim Grosbach4b905842013-09-20 23:08:21 +00003739/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003740/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003741bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003742 TheCondStack.push_back(TheCondState);
3743 TheCondState.TheCond = AsmCond::IfCond;
3744
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003745 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003746 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003747 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003748 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003749
3750 if (getLexer().isNot(AsmToken::EndOfStatement))
3751 return TokError("unexpected token in '.ifb' directive");
3752
3753 Lex();
3754
3755 TheCondState.CondMet = ExpectBlank == Str.empty();
3756 TheCondState.Ignore = !TheCondState.CondMet;
3757 }
3758
3759 return false;
3760}
3761
Jim Grosbach4b905842013-09-20 23:08:21 +00003762/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003763/// ::= .ifc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003764bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003765 TheCondStack.push_back(TheCondState);
3766 TheCondState.TheCond = AsmCond::IfCond;
3767
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003768 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003769 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003770 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003771 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003772
3773 if (getLexer().isNot(AsmToken::Comma))
3774 return TokError("unexpected token in '.ifc' directive");
3775
3776 Lex();
3777
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003778 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003779
3780 if (getLexer().isNot(AsmToken::EndOfStatement))
3781 return TokError("unexpected token in '.ifc' directive");
3782
3783 Lex();
3784
3785 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
3786 TheCondState.Ignore = !TheCondState.CondMet;
3787 }
3788
3789 return false;
3790}
3791
Jim Grosbach4b905842013-09-20 23:08:21 +00003792/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003793/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003794bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003795 StringRef Name;
3796 TheCondStack.push_back(TheCondState);
3797 TheCondState.TheCond = AsmCond::IfCond;
3798
3799 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003800 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003801 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003802 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003803 return TokError("expected identifier after '.ifdef'");
3804
3805 Lex();
3806
3807 MCSymbol *Sym = getContext().LookupSymbol(Name);
3808
3809 if (expect_defined)
3810 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
3811 else
3812 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
3813 TheCondState.Ignore = !TheCondState.CondMet;
3814 }
3815
3816 return false;
3817}
3818
Jim Grosbach4b905842013-09-20 23:08:21 +00003819/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003820/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003821bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003822 if (TheCondState.TheCond != AsmCond::IfCond &&
3823 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003824 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3825 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003826 TheCondState.TheCond = AsmCond::ElseIfCond;
3827
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003828 bool LastIgnoreState = false;
3829 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00003830 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003831 if (LastIgnoreState || TheCondState.CondMet) {
3832 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003833 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00003834 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003835 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003836 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003837 return true;
3838
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003839 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003840 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003841
Sean Callanan686ed8d2010-01-19 20:22:31 +00003842 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003843 TheCondState.CondMet = ExprValue;
3844 TheCondState.Ignore = !TheCondState.CondMet;
3845 }
3846
3847 return false;
3848}
3849
Jim Grosbach4b905842013-09-20 23:08:21 +00003850/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003851/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00003852bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003853 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003854 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003855
Sean Callanan686ed8d2010-01-19 20:22:31 +00003856 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003857
3858 if (TheCondState.TheCond != AsmCond::IfCond &&
3859 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003860 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
3861 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003862 TheCondState.TheCond = AsmCond::ElseCond;
3863 bool LastIgnoreState = false;
3864 if (!TheCondStack.empty())
3865 LastIgnoreState = TheCondStack.back().Ignore;
3866 if (LastIgnoreState || TheCondState.CondMet)
3867 TheCondState.Ignore = true;
3868 else
3869 TheCondState.Ignore = false;
3870
3871 return false;
3872}
3873
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003874/// parseDirectiveEnd
3875/// ::= .end
3876bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
3877 if (getLexer().isNot(AsmToken::EndOfStatement))
3878 return TokError("unexpected token in '.end' directive");
3879
3880 Lex();
3881
3882 while (Lexer.isNot(AsmToken::Eof))
3883 Lex();
3884
3885 return false;
3886}
3887
Jim Grosbach4b905842013-09-20 23:08:21 +00003888/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003889/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00003890bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003891 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003892 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003893
Sean Callanan686ed8d2010-01-19 20:22:31 +00003894 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003895
Jim Grosbach4b905842013-09-20 23:08:21 +00003896 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003897 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
3898 ".else");
3899 if (!TheCondStack.empty()) {
3900 TheCondState = TheCondStack.back();
3901 TheCondStack.pop_back();
3902 }
3903
3904 return false;
3905}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00003906
Eli Bendersky17233942013-01-15 22:59:42 +00003907void AsmParser::initializeDirectiveKindMap() {
3908 DirectiveKindMap[".set"] = DK_SET;
3909 DirectiveKindMap[".equ"] = DK_EQU;
3910 DirectiveKindMap[".equiv"] = DK_EQUIV;
3911 DirectiveKindMap[".ascii"] = DK_ASCII;
3912 DirectiveKindMap[".asciz"] = DK_ASCIZ;
3913 DirectiveKindMap[".string"] = DK_STRING;
3914 DirectiveKindMap[".byte"] = DK_BYTE;
3915 DirectiveKindMap[".short"] = DK_SHORT;
3916 DirectiveKindMap[".value"] = DK_VALUE;
3917 DirectiveKindMap[".2byte"] = DK_2BYTE;
3918 DirectiveKindMap[".long"] = DK_LONG;
3919 DirectiveKindMap[".int"] = DK_INT;
3920 DirectiveKindMap[".4byte"] = DK_4BYTE;
3921 DirectiveKindMap[".quad"] = DK_QUAD;
3922 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00003923 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00003924 DirectiveKindMap[".single"] = DK_SINGLE;
3925 DirectiveKindMap[".float"] = DK_FLOAT;
3926 DirectiveKindMap[".double"] = DK_DOUBLE;
3927 DirectiveKindMap[".align"] = DK_ALIGN;
3928 DirectiveKindMap[".align32"] = DK_ALIGN32;
3929 DirectiveKindMap[".balign"] = DK_BALIGN;
3930 DirectiveKindMap[".balignw"] = DK_BALIGNW;
3931 DirectiveKindMap[".balignl"] = DK_BALIGNL;
3932 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
3933 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
3934 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
3935 DirectiveKindMap[".org"] = DK_ORG;
3936 DirectiveKindMap[".fill"] = DK_FILL;
3937 DirectiveKindMap[".zero"] = DK_ZERO;
3938 DirectiveKindMap[".extern"] = DK_EXTERN;
3939 DirectiveKindMap[".globl"] = DK_GLOBL;
3940 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00003941 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
3942 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
3943 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
3944 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
3945 DirectiveKindMap[".reference"] = DK_REFERENCE;
3946 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
3947 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
3948 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
3949 DirectiveKindMap[".comm"] = DK_COMM;
3950 DirectiveKindMap[".common"] = DK_COMMON;
3951 DirectiveKindMap[".lcomm"] = DK_LCOMM;
3952 DirectiveKindMap[".abort"] = DK_ABORT;
3953 DirectiveKindMap[".include"] = DK_INCLUDE;
3954 DirectiveKindMap[".incbin"] = DK_INCBIN;
3955 DirectiveKindMap[".code16"] = DK_CODE16;
3956 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
3957 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00003958 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00003959 DirectiveKindMap[".irp"] = DK_IRP;
3960 DirectiveKindMap[".irpc"] = DK_IRPC;
3961 DirectiveKindMap[".endr"] = DK_ENDR;
3962 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
3963 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
3964 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
3965 DirectiveKindMap[".if"] = DK_IF;
3966 DirectiveKindMap[".ifb"] = DK_IFB;
3967 DirectiveKindMap[".ifnb"] = DK_IFNB;
3968 DirectiveKindMap[".ifc"] = DK_IFC;
3969 DirectiveKindMap[".ifnc"] = DK_IFNC;
3970 DirectiveKindMap[".ifdef"] = DK_IFDEF;
3971 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
3972 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
3973 DirectiveKindMap[".elseif"] = DK_ELSEIF;
3974 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003975 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00003976 DirectiveKindMap[".endif"] = DK_ENDIF;
3977 DirectiveKindMap[".skip"] = DK_SKIP;
3978 DirectiveKindMap[".space"] = DK_SPACE;
3979 DirectiveKindMap[".file"] = DK_FILE;
3980 DirectiveKindMap[".line"] = DK_LINE;
3981 DirectiveKindMap[".loc"] = DK_LOC;
3982 DirectiveKindMap[".stabs"] = DK_STABS;
3983 DirectiveKindMap[".sleb128"] = DK_SLEB128;
3984 DirectiveKindMap[".uleb128"] = DK_ULEB128;
3985 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
3986 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
3987 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
3988 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
3989 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
3990 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
3991 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
3992 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
3993 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
3994 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
3995 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
3996 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
3997 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
3998 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
3999 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4000 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4001 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4002 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4003 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004004 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004005 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4006 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4007 DirectiveKindMap[".macro"] = DK_MACRO;
4008 DirectiveKindMap[".endm"] = DK_ENDM;
4009 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4010 DirectiveKindMap[".purgem"] = DK_PURGEM;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004011}
4012
Jim Grosbach4b905842013-09-20 23:08:21 +00004013MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004014 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004015
Rafael Espindola34b9c512012-06-03 23:57:14 +00004016 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004017 for (;;) {
4018 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004019 if (getLexer().is(AsmToken::Eof)) {
4020 Error(DirectiveLoc, "no matching '.endr' in definition");
4021 return 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004022 }
4023
Rafael Espindola34b9c512012-06-03 23:57:14 +00004024 if (Lexer.is(AsmToken::Identifier) &&
4025 (getTok().getIdentifier() == ".rept")) {
4026 ++NestLevel;
4027 }
4028
4029 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004030 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004031 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004032 EndToken = getTok();
4033 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004034 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4035 TokError("unexpected token in '.endr' directive");
4036 return 0;
4037 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004038 break;
4039 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004040 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004041 }
4042
Rafael Espindola34b9c512012-06-03 23:57:14 +00004043 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004044 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004045 }
4046
4047 const char *BodyStart = StartToken.getLoc().getPointer();
4048 const char *BodyEnd = EndToken.getLoc().getPointer();
4049 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4050
Rafael Espindola34b9c512012-06-03 23:57:14 +00004051 // We Are Anonymous.
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004052 MacroLikeBodies.push_back(MCAsmMacro(StringRef(), Body, None));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004053 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004054}
4055
Jim Grosbach4b905842013-09-20 23:08:21 +00004056void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004057 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004058 OS << ".endr\n";
4059
4060 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00004061 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004062
Rafael Espindola34b9c512012-06-03 23:57:14 +00004063 // Create the macro instantiation object and add to the current macro
4064 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00004065 MacroInstantiation *MI = new MacroInstantiation(
4066 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004067 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004068
Rafael Espindola34b9c512012-06-03 23:57:14 +00004069 // Jump to the macro instantiation and prime the lexer.
4070 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
4071 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
4072 Lex();
4073}
4074
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004075/// parseDirectiveRept
4076/// ::= .rep | .rept count
4077bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004078 const MCExpr *CountExpr;
4079 SMLoc CountLoc = getTok().getLoc();
4080 if (parseExpression(CountExpr))
4081 return true;
4082
Rafael Espindola34b9c512012-06-03 23:57:14 +00004083 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004084 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4085 eatToEndOfStatement();
4086 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4087 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004088
4089 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004090 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004091
4092 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004093 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004094
4095 // Eat the end of statement.
4096 Lex();
4097
4098 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004099 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004100 if (!M)
4101 return true;
4102
4103 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4104 // to hold the macro body with substitutions.
4105 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004106 raw_svector_ostream OS(Buf);
4107 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004108 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004109 return true;
4110 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004111 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004112
4113 return false;
4114}
4115
Jim Grosbach4b905842013-09-20 23:08:21 +00004116/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004117/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004118bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004119 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004120
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004121 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004122 return TokError("expected identifier in '.irp' directive");
4123
Rafael Espindola768b41c2012-06-15 14:02:34 +00004124 if (Lexer.isNot(AsmToken::Comma))
4125 return TokError("expected comma in '.irp' directive");
4126
4127 Lex();
4128
Eli Bendersky38274122013-01-14 23:22:36 +00004129 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004130 if (parseMacroArguments(0, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004131 return true;
4132
4133 // Eat the end of statement.
4134 Lex();
4135
4136 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004137 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004138 if (!M)
4139 return true;
4140
4141 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4142 // to hold the macro body with substitutions.
4143 SmallString<256> Buf;
4144 raw_svector_ostream OS(Buf);
4145
Eli Bendersky38274122013-01-14 23:22:36 +00004146 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004147 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004148 return true;
4149 }
4150
Jim Grosbach4b905842013-09-20 23:08:21 +00004151 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004152
4153 return false;
4154}
4155
Jim Grosbach4b905842013-09-20 23:08:21 +00004156/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004157/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004158bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004159 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004160
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004161 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004162 return TokError("expected identifier in '.irpc' directive");
4163
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004164 if (Lexer.isNot(AsmToken::Comma))
4165 return TokError("expected comma in '.irpc' directive");
4166
4167 Lex();
4168
Eli Bendersky38274122013-01-14 23:22:36 +00004169 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004170 if (parseMacroArguments(0, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004171 return true;
4172
4173 if (A.size() != 1 || A.front().size() != 1)
4174 return TokError("unexpected token in '.irpc' directive");
4175
4176 // Eat the end of statement.
4177 Lex();
4178
4179 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004180 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004181 if (!M)
4182 return true;
4183
4184 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4185 // to hold the macro body with substitutions.
4186 SmallString<256> Buf;
4187 raw_svector_ostream OS(Buf);
4188
4189 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004190 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004191 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004192 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004193
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004194 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004195 return true;
4196 }
4197
Jim Grosbach4b905842013-09-20 23:08:21 +00004198 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004199
4200 return false;
4201}
4202
Jim Grosbach4b905842013-09-20 23:08:21 +00004203bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004204 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004205 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004206
4207 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004208 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004209 assert(getLexer().is(AsmToken::EndOfStatement));
4210
Jim Grosbach4b905842013-09-20 23:08:21 +00004211 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004212 return false;
4213}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004214
Jim Grosbach4b905842013-09-20 23:08:21 +00004215bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004216 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004217 const MCExpr *Value;
4218 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004219 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004220 return true;
4221 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4222 if (!MCE)
4223 return Error(ExprLoc, "unexpected expression in _emit");
4224 uint64_t IntValue = MCE->getValue();
4225 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4226 return Error(ExprLoc, "literal value out of range for directive");
4227
Chad Rosierc7f552c2013-02-12 21:33:51 +00004228 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4229 return false;
4230}
4231
Jim Grosbach4b905842013-09-20 23:08:21 +00004232bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004233 const MCExpr *Value;
4234 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004235 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004236 return true;
4237 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4238 if (!MCE)
4239 return Error(ExprLoc, "unexpected expression in align");
4240 uint64_t IntValue = MCE->getValue();
4241 if (!isPowerOf2_64(IntValue))
4242 return Error(ExprLoc, "literal value not a power of two greater then zero");
4243
Jim Grosbach4b905842013-09-20 23:08:21 +00004244 Info.AsmRewrites->push_back(
4245 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004246 return false;
4247}
4248
Chad Rosierf43fcf52013-02-13 21:27:17 +00004249// We are comparing pointers, but the pointers are relative to a single string.
4250// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004251static int rewritesSort(const AsmRewrite *AsmRewriteA,
4252 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004253 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4254 return -1;
4255 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4256 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004257
Chad Rosierfce4fab2013-04-08 17:43:47 +00004258 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4259 // rewrite to the same location. Make sure the SizeDirective rewrite is
4260 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4261 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004262 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4263 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004264 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004265
Jim Grosbach4b905842013-09-20 23:08:21 +00004266 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4267 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004268 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004269 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004270}
4271
Jim Grosbach4b905842013-09-20 23:08:21 +00004272bool AsmParser::parseMSInlineAsm(
4273 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4274 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4275 SmallVectorImpl<std::string> &Constraints,
4276 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4277 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004278 SmallVector<void *, 4> InputDecls;
4279 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004280 SmallVector<bool, 4> InputDeclsAddressOf;
4281 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004282 SmallVector<std::string, 4> InputConstraints;
4283 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004284 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004285
Benjamin Kramer1a136112013-02-15 20:37:21 +00004286 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004287
4288 // Prime the lexer.
4289 Lex();
4290
4291 // While we have input, parse each statement.
4292 unsigned InputIdx = 0;
4293 unsigned OutputIdx = 0;
4294 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004295 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004296 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004297 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004298
Chad Rosier149e8e02012-12-12 22:45:52 +00004299 if (Info.ParseError)
4300 return true;
4301
Benjamin Kramer1a136112013-02-15 20:37:21 +00004302 if (Info.Opcode == ~0U)
4303 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004304
Benjamin Kramer1a136112013-02-15 20:37:21 +00004305 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004306
Benjamin Kramer1a136112013-02-15 20:37:21 +00004307 // Build the list of clobbers, outputs and inputs.
4308 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
4309 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004310
Benjamin Kramer1a136112013-02-15 20:37:21 +00004311 // Immediate.
Chad Rosierf3c04f62013-03-19 21:58:18 +00004312 if (Operand->isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004313 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004314
Benjamin Kramer1a136112013-02-15 20:37:21 +00004315 // Register operand.
4316 if (Operand->isReg() && !Operand->needAddressOf()) {
4317 unsigned NumDefs = Desc.getNumDefs();
4318 // Clobber.
4319 if (NumDefs && Operand->getMCOperandNum() < NumDefs)
4320 ClobberRegs.push_back(Operand->getReg());
4321 continue;
4322 }
4323
4324 // Expr/Input or Output.
Chad Rosiere81309b2013-04-09 17:53:49 +00004325 StringRef SymName = Operand->getSymName();
4326 if (SymName.empty())
4327 continue;
4328
Chad Rosierdba3fe52013-04-22 22:12:12 +00004329 void *OpDecl = Operand->getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004330 if (!OpDecl)
4331 continue;
4332
4333 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004334 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004335 if (isOutput) {
4336 ++InputIdx;
4337 OutputDecls.push_back(OpDecl);
4338 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
4339 OutputConstraints.push_back('=' + Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004340 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004341 } else {
4342 InputDecls.push_back(OpDecl);
4343 InputDeclsAddressOf.push_back(Operand->needAddressOf());
4344 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004345 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004346 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004347 }
Reid Kleckneree088972013-12-10 18:27:32 +00004348
4349 // Consider implicit defs to be clobbers. Think of cpuid and push.
4350 const uint16_t *ImpDefs = Desc.getImplicitDefs();
4351 for (unsigned I = 0, E = Desc.getNumImplicitDefs(); I != E; ++I)
4352 ClobberRegs.push_back(ImpDefs[I]);
Chad Rosier8bce6642012-10-18 15:49:34 +00004353 }
4354
4355 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004356 NumOutputs = OutputDecls.size();
4357 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004358
4359 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004360 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4361 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4362 ClobberRegs.end());
4363 Clobbers.assign(ClobberRegs.size(), std::string());
4364 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4365 raw_string_ostream OS(Clobbers[I]);
4366 IP->printRegName(OS, ClobberRegs[I]);
4367 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004368
4369 // Merge the various outputs and inputs. Output are expected first.
4370 if (NumOutputs || NumInputs) {
4371 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004372 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004373 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004374 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004375 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004376 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004377 }
4378 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004379 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004380 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004381 }
4382 }
4383
4384 // Build the IR assembly string.
4385 std::string AsmStringIR;
4386 raw_string_ostream OS(AsmStringIR);
Chad Rosier17d37992013-03-19 21:12:14 +00004387 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4388 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
Jim Grosbach4b905842013-09-20 23:08:21 +00004389 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
Benjamin Kramer1a136112013-02-15 20:37:21 +00004390 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(),
4391 E = AsmStrRewrites.end();
4392 I != E; ++I) {
Chad Rosierff10ed12013-04-12 16:26:42 +00004393 AsmRewriteKind Kind = (*I).Kind;
4394 if (Kind == AOK_Delete)
4395 continue;
4396
Chad Rosier8bce6642012-10-18 15:49:34 +00004397 const char *Loc = (*I).Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004398 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004399
Chad Rosier120eefd2013-03-19 17:32:17 +00004400 // Emit everything up to the immediate/expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004401 unsigned Len = Loc - AsmStart;
Chad Rosier8fb83302013-04-11 21:49:30 +00004402 if (Len)
Chad Rosier17d37992013-03-19 21:12:14 +00004403 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004404
Chad Rosier37e755c2012-10-23 17:43:43 +00004405 // Skip the original expression.
4406 if (Kind == AOK_Skip) {
Chad Rosier17d37992013-03-19 21:12:14 +00004407 AsmStart = Loc + (*I).Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004408 continue;
4409 }
4410
Chad Rosierff10ed12013-04-12 16:26:42 +00004411 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004412 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004413 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004414 default:
4415 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004416 case AOK_Imm:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004417 OS << "$$" << (*I).Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004418 break;
4419 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004420 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004421 break;
4422 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004423 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004424 break;
4425 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004426 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004427 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004428 case AOK_SizeDirective:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004429 switch ((*I).Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004430 default: break;
4431 case 8: OS << "byte ptr "; break;
4432 case 16: OS << "word ptr "; break;
4433 case 32: OS << "dword ptr "; break;
4434 case 64: OS << "qword ptr "; break;
4435 case 80: OS << "xword ptr "; break;
4436 case 128: OS << "xmmword ptr "; break;
4437 case 256: OS << "ymmword ptr "; break;
4438 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004439 break;
4440 case AOK_Emit:
4441 OS << ".byte";
4442 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004443 case AOK_Align: {
4444 unsigned Val = (*I).Val;
4445 OS << ".align " << Val;
4446
4447 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004448 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004449 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4450 break;
4451 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004452 case AOK_DotOperator:
4453 OS << (*I).Val;
4454 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004455 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004456
Chad Rosier8bce6642012-10-18 15:49:34 +00004457 // Skip the original expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004458 AsmStart = Loc + (*I).Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004459 }
4460
4461 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004462 if (AsmStart != AsmEnd)
4463 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004464
4465 AsmString = OS.str();
4466 return false;
4467}
4468
Daniel Dunbar01e36072010-07-17 02:26:10 +00004469/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004470MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4471 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004472 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004473}