blob: 4b83144f3bd2c6ea9037764325bee4fa8432ae6d [file] [log] [blame]
Chris Lattnerb0133452009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar2af16532010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Chad Rosiereb5c1682013-02-13 18:38:58 +000016#include "llvm/ADT/STLExtras.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000017#include "llvm/ADT/StringMap.h"
Daniel Dunbareb6bb322009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Evan Cheng11424442011-07-26 00:24:13 +000021#include "llvm/MC/MCDwarf.h"
Daniel Dunbar115e4d62009-08-31 08:06:59 +000022#include "llvm/MC/MCExpr.h"
Chad Rosier8bce6642012-10-18 15:49:34 +000023#include "llvm/MC/MCInstPrinter.h"
24#include "llvm/MC/MCInstrInfo.h"
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 {
54
Eli Benderskya313ae62013-01-16 18:56:50 +000055/// \brief Helper types for tracking macro definitions.
56typedef std::vector<AsmToken> MCAsmMacroArgument;
57typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
58typedef std::pair<StringRef, MCAsmMacroArgument> MCAsmMacroParameter;
59typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
60
61struct MCAsmMacro {
62 StringRef Name;
63 StringRef Body;
64 MCAsmMacroParameters Parameters;
65
66public:
67 MCAsmMacro(StringRef N, StringRef B, const MCAsmMacroParameters &P) :
68 Name(N), Body(B), Parameters(P) {}
69
70 MCAsmMacro(const MCAsmMacro& Other)
71 : Name(Other.Name), Body(Other.Body), Parameters(Other.Parameters) {}
72};
73
Daniel Dunbar43235712010-07-18 18:54:11 +000074/// \brief Helper class for storing information about an active macro
75/// instantiation.
76struct MacroInstantiation {
77 /// The macro being instantiated.
Eli Bendersky38274122013-01-14 23:22:36 +000078 const MCAsmMacro *TheMacro;
Daniel Dunbar43235712010-07-18 18:54:11 +000079
80 /// The macro instantiation with substitutions.
81 MemoryBuffer *Instantiation;
82
83 /// The location of the instantiation.
84 SMLoc InstantiationLoc;
85
Daniel Dunbar40f1d852012-12-01 01:38:48 +000086 /// The buffer where parsing should resume upon instantiation completion.
87 int ExitBuffer;
88
Daniel Dunbar43235712010-07-18 18:54:11 +000089 /// The location where parsing should resume upon instantiation completion.
90 SMLoc ExitLoc;
91
92public:
Eli Bendersky38274122013-01-14 23:22:36 +000093 MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB, SMLoc EL,
Rafael Espindola1134ab232011-06-05 02:43:45 +000094 MemoryBuffer *I);
Daniel Dunbar43235712010-07-18 18:54:11 +000095};
96
Eli Friedman0f4871d2012-10-22 23:58:19 +000097struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +000098 /// \brief The parsed operands from the last parsed statement.
Eli Friedman0f4871d2012-10-22 23:58:19 +000099 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
100
Jim Grosbach4b905842013-09-20 23:08:21 +0000101 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000102 unsigned Opcode;
103
Jim Grosbach4b905842013-09-20 23:08:21 +0000104 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000105 bool ParseError;
106
Eli Friedman0f4871d2012-10-22 23:58:19 +0000107 SmallVectorImpl<AsmRewrite> *AsmRewrites;
108
Chad Rosier149e8e02012-12-12 22:45:52 +0000109 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(0) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000110 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000111 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000112
113 ~ParseStatementInfo() {
114 // Free any parsed operands.
115 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
116 delete ParsedOperands[i];
117 ParsedOperands.clear();
118 }
119};
120
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000121/// \brief The concrete assembly parser instance.
122class AsmParser : public MCAsmParser {
Craig Topper2e6644c2012-09-15 16:23:52 +0000123 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
124 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000125private:
126 AsmLexer Lexer;
127 MCContext &Ctx;
128 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000129 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000130 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000131 SourceMgr::DiagHandlerTy SavedDiagHandler;
132 void *SavedDiagContext;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000133 MCAsmParserExtension *PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000134
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000135 /// This is the current buffer index we're lexing from as managed by the
136 /// SourceMgr object.
137 int CurBuffer;
138
139 AsmCond TheCondState;
140 std::vector<AsmCond> TheCondStack;
141
Jim Grosbach4b905842013-09-20 23:08:21 +0000142 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000143 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000144 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000145 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000146
Jim Grosbach4b905842013-09-20 23:08:21 +0000147 /// \brief Map of currently defined macros.
Eli Bendersky38274122013-01-14 23:22:36 +0000148 StringMap<MCAsmMacro*> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000149
Jim Grosbach4b905842013-09-20 23:08:21 +0000150 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000151 std::vector<MacroInstantiation*> ActiveMacros;
152
Jim Grosbach4b905842013-09-20 23:08:21 +0000153 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000154 std::deque<MCAsmMacro> MacroLikeBodies;
155
Daniel Dunbar828984f2010-07-18 18:38:02 +0000156 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000157 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000158
Daniel Dunbar43325c42010-09-09 22:42:56 +0000159 /// Flag tracking whether any errors have been encountered.
160 unsigned HadError : 1;
161
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000162 /// The values from the last parsed cpp hash file line comment if any.
163 StringRef CppHashFilename;
164 int64_t CppHashLineNumber;
165 SMLoc CppHashLoc;
Kevin Enderby27121c12012-11-05 21:55:41 +0000166 int CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000167 /// When generating dwarf for assembly source files we need to calculate the
168 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000169 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000170 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
171 SMLoc LastQueryIDLoc;
172 int LastQueryBuffer;
173 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000174
Devang Patela173ee52012-01-31 18:14:05 +0000175 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
176 unsigned AssemblerDialect;
177
Jim Grosbach4b905842013-09-20 23:08:21 +0000178 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000179 bool IsDarwin;
180
Jim Grosbach4b905842013-09-20 23:08:21 +0000181 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000182 bool ParsingInlineAsm;
183
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000184public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000185 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000186 const MCAsmInfo &MAI);
Craig Topper5f96ca52012-08-29 05:48:09 +0000187 virtual ~AsmParser();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000188
189 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
190
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000191 virtual void addDirectiveHandler(StringRef Directive,
Eli Bendersky29b9f472013-01-16 00:50:52 +0000192 ExtensionDirectiveHandler Handler) {
193 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000194 }
195
196public:
197 /// @name MCAsmParser Interface
198 /// {
199
200 virtual SourceMgr &getSourceManager() { return SrcMgr; }
201 virtual MCAsmLexer &getLexer() { return Lexer; }
202 virtual MCContext &getContext() { return Ctx; }
203 virtual MCStreamer &getStreamer() { return Out; }
Eric Christophera7c32732012-12-18 00:30:54 +0000204 virtual unsigned getAssemblerDialect() {
Devang Patela173ee52012-01-31 18:14:05 +0000205 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000206 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000207 else
208 return AssemblerDialect;
209 }
210 virtual void setAssemblerDialect(unsigned i) {
211 AssemblerDialect = i;
212 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000213
Chris Lattnera3a06812011-10-16 04:47:35 +0000214 virtual bool Warning(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000215 ArrayRef<SMRange> Ranges = None);
Chris Lattnera3a06812011-10-16 04:47:35 +0000216 virtual bool Error(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000217 ArrayRef<SMRange> Ranges = None);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000218
Craig Topper5f96ca52012-08-29 05:48:09 +0000219 virtual const AsmToken &Lex();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000220
Chad Rosier49963552012-10-13 00:26:04 +0000221 void setParsingInlineAsm(bool V) { ParsingInlineAsm = V; }
Chad Rosiere4ad2a02012-10-16 20:16:20 +0000222 bool isParsingInlineAsm() { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000223
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000224 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000225 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000226 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000227 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000228 SmallVectorImpl<std::string> &Clobbers,
229 const MCInstrInfo *MII,
230 const MCInstPrinter *IP,
231 MCAsmParserSemaCallback &SI);
Chad Rosier49963552012-10-13 00:26:04 +0000232
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000233 bool parseExpression(const MCExpr *&Res);
234 virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc);
Chad Rosier1863f4f2013-04-10 17:35:30 +0000235 virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000236 virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
237 virtual bool parseAbsoluteExpression(int64_t &Res);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000238
Jim Grosbach4b905842013-09-20 23:08:21 +0000239 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000240 /// and set \p Res to the identifier contents.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000241 virtual bool parseIdentifier(StringRef &Res);
242 virtual void eatToEndOfStatement();
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000243
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000244 virtual void checkForValidSection();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000245 /// }
246
247private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000248
Jim Grosbach4b905842013-09-20 23:08:21 +0000249 bool parseStatement(ParseStatementInfo &Info);
250 void eatToEndOfLine();
251 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000252
Jim Grosbach4b905842013-09-20 23:08:21 +0000253 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Kevin Enderby81c944c2013-01-22 21:44:53 +0000254 MCAsmMacroParameters Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000255 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Eli Bendersky38274122013-01-14 23:22:36 +0000256 const MCAsmMacroParameters &Parameters,
257 const MCAsmMacroArguments &A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000258 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000259
Eli Benderskya313ae62013-01-16 18:56:50 +0000260 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000261 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000262
263 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000264 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000265
266 /// \brief Lookup a previously defined macro.
267 /// \param Name Macro name.
268 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000269 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000270
271 /// \brief Define a new macro with the given name and information.
Jim Grosbach4b905842013-09-20 23:08:21 +0000272 void defineMacro(StringRef Name, const MCAsmMacro& Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000273
274 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000275 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000276
277 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000278 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000279
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000280 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000281 ///
282 /// \param M The macro.
283 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000284 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000285
286 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000287 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000288
289 /// \brief Extract AsmTokens for a macro argument. If the argument delimiter
290 /// is initially unknown, set it to AsmToken::Eof. It will be set to the
291 /// correct delimiter by the method.
Jim Grosbach4b905842013-09-20 23:08:21 +0000292 bool parseMacroArgument(MCAsmMacroArgument &MA,
Eli Benderskya313ae62013-01-16 18:56:50 +0000293 AsmToken::TokenKind &ArgumentDelimiter);
294
295 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000296 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000297
Jim Grosbach4b905842013-09-20 23:08:21 +0000298 void printMacroInstantiations();
299 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000300 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000301 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000302 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000303 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000304
Jim Grosbach4b905842013-09-20 23:08:21 +0000305 /// \brief Enter the specified file. This returns true on failure.
306 bool enterIncludeFile(const std::string &Filename);
307
308 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000309 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000310 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000311
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000312 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000313 /// current token is not set; clients should ensure Lex() is called
314 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000315 ///
316 /// \param InBuffer If not -1, should be the known buffer id that contains the
317 /// location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000318 void jumpToLoc(SMLoc Loc, int InBuffer=-1);
Daniel Dunbar43235712010-07-18 18:54:11 +0000319
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000320 /// \brief Parse up to the end of statement and a return the contents from the
321 /// current token until the end of the statement; the current token on exit
322 /// will be either the EndOfStatement or EOF.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000323 virtual StringRef parseStringToEndOfStatement();
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000324
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000325 /// \brief Parse until the end of a statement or a comma is encountered,
326 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000327 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000328
Jim Grosbach4b905842013-09-20 23:08:21 +0000329 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000330 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000331
Jim Grosbach4b905842013-09-20 23:08:21 +0000332 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
333 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
334 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000335
Jim Grosbach4b905842013-09-20 23:08:21 +0000336 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000337
Eli Bendersky17233942013-01-15 22:59:42 +0000338 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000339 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000340 DK_NO_DIRECTIVE, // Placeholder
341 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
342 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_SINGLE,
343 DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000344 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000345 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000346 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000347 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
348 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
349 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
350 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
351 DK_IF, DK_IFB, DK_IFNB, DK_IFC, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
Eli Bendersky17233942013-01-15 22:59:42 +0000352 DK_ELSEIF, DK_ELSE, DK_ENDIF,
353 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
354 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
355 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
356 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
357 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
358 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000359 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Eli Bendersky17233942013-01-15 22:59:42 +0000360 DK_MACROS_ON, DK_MACROS_OFF, DK_MACRO, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000361 DK_SLEB128, DK_ULEB128,
362 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000363 };
364
Jim Grosbach4b905842013-09-20 23:08:21 +0000365 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000366 /// directives parsed by this class.
367 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000368
369 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000370 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
371 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
372 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
Chris Lattnera3a06812011-10-16 04:47:35 +0000540bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000541 if (FatalAssemblerWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000542 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000543 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
544 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000545 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000546}
547
Chris Lattnera3a06812011-10-16 04:47:35 +0000548bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000549 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000550 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
551 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000552 return true;
553}
554
Jim Grosbach4b905842013-09-20 23:08:21 +0000555bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000556 std::string IncludedFile;
557 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000558 if (NewBuf == -1)
559 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000560
Sean Callanan7a77eae2010-01-21 00:19:58 +0000561 CurBuffer = NewBuf;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000562
Sean Callanan7a77eae2010-01-21 00:19:58 +0000563 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencer530ce852010-10-09 11:00:50 +0000564
Sean Callanan7a77eae2010-01-21 00:19:58 +0000565 return false;
566}
Daniel Dunbar43235712010-07-18 18:54:11 +0000567
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000568/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000569/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000570/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000571bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000572 std::string IncludedFile;
573 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
574 if (NewBuf == -1)
575 return true;
576
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000577 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000578 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000579 return false;
580}
581
Jim Grosbach4b905842013-09-20 23:08:21 +0000582void AsmParser::jumpToLoc(SMLoc Loc, int InBuffer) {
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000583 if (InBuffer != -1) {
584 CurBuffer = InBuffer;
585 } else {
586 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
587 }
Daniel Dunbar43235712010-07-18 18:54:11 +0000588 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
589}
590
Sean Callanan7a77eae2010-01-21 00:19:58 +0000591const AsmToken &AsmParser::Lex() {
592 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000593
Sean Callanan7a77eae2010-01-21 00:19:58 +0000594 if (tok->is(AsmToken::Eof)) {
595 // If this is the end of an included file, pop the parent file off the
596 // include stack.
597 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
598 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000599 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000600 tok = &Lexer.Lex();
601 }
602 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000603
Sean Callanan7a77eae2010-01-21 00:19:58 +0000604 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000605 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000606
Sean Callanan7a77eae2010-01-21 00:19:58 +0000607 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000608}
609
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000610bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000611 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000612 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000613 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000614
Chris Lattner36e02122009-06-21 20:54:55 +0000615 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000616 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000617
618 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000619 AsmCond StartingCondState = TheCondState;
620
Kevin Enderby6469fc22011-11-01 22:27:22 +0000621 // If we are generating dwarf for assembly source files save the initial text
622 // section and generate a .file directive.
623 if (getContext().getGenDwarfForAssembly()) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000624 getContext().setGenDwarfSection(getStreamer().getCurrentSection().first);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000625 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
626 getStreamer().EmitLabel(SectionStartSym);
627 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby6469fc22011-11-01 22:27:22 +0000628 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
Eric Christopher906da232012-12-18 00:31:01 +0000629 StringRef(),
630 getContext().getMainFileName());
Kevin Enderby6469fc22011-11-01 22:27:22 +0000631 }
632
Chris Lattner73f36112009-07-02 21:53:43 +0000633 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000634 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000635 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000636 if (!parseStatement(Info))
637 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000638
Daniel Dunbar43325c42010-09-09 22:42:56 +0000639 // We had an error, validate that one was emitted and recover by skipping to
640 // the next line.
641 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000642 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000643 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000644
645 if (TheCondState.TheCond != StartingCondState.TheCond ||
646 TheCondState.Ignore != StartingCondState.Ignore)
647 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000648
649 // Check to see there are no empty DwarfFile slots.
Manman Ren5ce24ff2013-03-12 20:17:00 +0000650 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +0000651 getContext().getMCDwarfFiles();
Kevin Enderbye5930f12010-07-28 20:55:35 +0000652 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000653 if (!MCDwarfFiles[i])
Kevin Enderbye5930f12010-07-28 20:55:35 +0000654 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000655 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000656
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000657 // Check to see that all assembler local symbols were actually defined.
658 // Targets that don't do subsections via symbols may not want this, though,
659 // so conservatively exclude them. Only do this if we're finalizing, though,
660 // as otherwise we won't necessarilly have seen everything yet.
661 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
662 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
663 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000664 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000665 i != e; ++i) {
666 MCSymbol *Sym = i->getValue();
667 // Variable symbols may not be marked as defined, so check those
668 // explicitly. If we know it's a variable, we have a definition for
669 // the purposes of this check.
670 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
671 // FIXME: We would really like to refer back to where the symbol was
672 // first referenced for a source location. We need to add something
673 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000674 printMessage(
675 getLexer().getLoc(), SourceMgr::DK_Error,
676 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000677 }
678 }
679
David Peixotto308e7e42013-12-19 18:08:08 +0000680 // Callback to the target parser in case it needs to do anything.
681 if (!HadError)
682 getTargetParser().finishParse();
683
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000684 // Finalize the output stream if there are no errors and if the client wants
685 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000686 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000687 Out.Finish();
688
Chris Lattner73f36112009-07-02 21:53:43 +0000689 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000690}
691
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000692void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000693 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000694 TokError("expected section directive before assembly directive");
Eli Benderskycbb25142013-01-14 19:04:57 +0000695 Out.InitToTextSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000696 }
697}
698
Jim Grosbach4b905842013-09-20 23:08:21 +0000699/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000700void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000701 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000702 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000703
Chris Lattnere5074c42009-06-22 01:29:09 +0000704 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000705 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000706 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000707}
708
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000709StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000710 const char *Start = getTok().getLoc().getPointer();
711
Jim Grosbach4b905842013-09-20 23:08:21 +0000712 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000713 Lex();
714
715 const char *End = getTok().getLoc().getPointer();
716 return StringRef(Start, End - Start);
717}
Chris Lattner78db3622009-06-22 05:51:26 +0000718
Jim Grosbach4b905842013-09-20 23:08:21 +0000719StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000720 const char *Start = getTok().getLoc().getPointer();
721
722 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000723 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000724 Lex();
725
726 const char *End = getTok().getLoc().getPointer();
727 return StringRef(Start, End - Start);
728}
729
Jim Grosbach4b905842013-09-20 23:08:21 +0000730/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000731/// NOTE: This assumes the leading '(' has already been consumed.
732///
733/// parenexpr ::= expr)
734///
Jim Grosbach4b905842013-09-20 23:08:21 +0000735bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
736 if (parseExpression(Res))
737 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000738 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000739 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000740 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000741 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000742 return false;
743}
Chris Lattner78db3622009-06-22 05:51:26 +0000744
Jim Grosbach4b905842013-09-20 23:08:21 +0000745/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000746/// NOTE: This assumes the leading '[' has already been consumed.
747///
748/// bracketexpr ::= expr]
749///
Jim Grosbach4b905842013-09-20 23:08:21 +0000750bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
751 if (parseExpression(Res))
752 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000753 if (Lexer.isNot(AsmToken::RBrac))
754 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000755 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000756 Lex();
757 return false;
758}
759
Jim Grosbach4b905842013-09-20 23:08:21 +0000760/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000761/// primaryexpr ::= (parenexpr
762/// primaryexpr ::= symbol
763/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000764/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000765/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000766bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000767 SMLoc FirstTokenLoc = getLexer().getLoc();
768 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
769 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000770 default:
771 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000772 // If we have an error assume that we've already handled it.
773 case AsmToken::Error:
774 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000775 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000776 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000777 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000778 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000779 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000780 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000781 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000782 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000783 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000784 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000785 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000786 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000787 if (FirstTokenKind == AsmToken::Dollar) {
788 if (Lexer.getMAI().getDollarIsPC()) {
789 // This is a '$' reference, which references the current PC. Emit a
790 // temporary label to the streamer and refer to it.
791 MCSymbol *Sym = Ctx.CreateTempSymbol();
792 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000793 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
794 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000795 EndLoc = FirstTokenLoc;
796 return false;
797 } else
798 return Error(FirstTokenLoc, "invalid token in expression");
799 return true;
800 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000801 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000802 // Parse symbol variant
803 std::pair<StringRef, StringRef> Split;
804 if (!MAI.useParensForSymbolVariant()) {
805 Split = Identifier.split('@');
806 } else if (Lexer.is(AsmToken::LParen)) {
807 Lexer.Lex(); // eat (
808 StringRef VName;
809 parseIdentifier(VName);
810 if (Lexer.isNot(AsmToken::RParen)) {
811 return Error(Lexer.getTok().getLoc(),
812 "unexpected token in variant, expected ')'");
813 }
814 Lexer.Lex(); // eat )
815 Split = std::make_pair(Identifier, VName);
816 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000817
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000818 EndLoc = SMLoc::getFromPointer(Identifier.end());
819
Daniel Dunbard20cda02009-10-16 01:34:54 +0000820 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000821 StringRef SymbolName = Identifier;
822 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000823
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000824 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000825 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000826 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000827 if (Variant != MCSymbolRefExpr::VK_Invalid) {
828 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000829 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000830 Variant = MCSymbolRefExpr::VK_None;
831 } else {
Daniel Dunbar55f16672010-09-17 02:47:07 +0000832 Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000833 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000834 }
835 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000836
Hans Wennborgce69d772013-10-18 20:46:28 +0000837 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
838
Daniel Dunbard20cda02009-10-16 01:34:54 +0000839 // If this is an absolute variable reference, substitute it now to preserve
840 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000841 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000842 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000843 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000844
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000845 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000846 return false;
847 }
848
849 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000850 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000851 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000852 }
Kevin Enderby0510b482010-05-17 23:08:19 +0000853 case AsmToken::Integer: {
854 SMLoc Loc = getTok().getLoc();
855 int64_t IntVal = getTok().getIntVal();
856 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000857 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000858 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000859 // Look for 'b' or 'f' following an Integer as a directional label
860 if (Lexer.getKind() == AsmToken::Identifier) {
861 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000862 // Lookup the symbol variant if used.
863 std::pair<StringRef, StringRef> Split = IDVal.split('@');
864 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
865 if (Split.first.size() != IDVal.size()) {
866 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
867 if (Variant == MCSymbolRefExpr::VK_Invalid) {
868 Variant = MCSymbolRefExpr::VK_None;
869 return TokError("invalid variant '" + Split.second + "'");
870 }
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000871 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000872 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000873 if (IDVal == "f" || IDVal == "b") {
874 MCSymbol *Sym =
875 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "f" ? 1 : 0);
Ulrich Weigandd4120982013-06-20 16:24:17 +0000876 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000877 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000878 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000879 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000880 Lex(); // Eat identifier.
881 }
882 }
Chris Lattner78db3622009-06-22 05:51:26 +0000883 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000884 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000885 case AsmToken::Real: {
886 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000887 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000888 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000889 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000890 Lex(); // Eat token.
891 return false;
892 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000893 case AsmToken::Dot: {
894 // This is a '.' reference, which references the current PC. Emit a
895 // temporary label to the streamer and refer to it.
896 MCSymbol *Sym = Ctx.CreateTempSymbol();
897 Out.EmitLabel(Sym);
898 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000899 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000900 Lex(); // Eat identifier.
901 return false;
902 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000903 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000904 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000905 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000906 case AsmToken::LBrac:
907 if (!PlatformParser->HasBracketExpressions())
908 return TokError("brackets expression not supported on this target");
909 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000910 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000911 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000912 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000913 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000914 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000915 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000916 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000917 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000918 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000919 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000920 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000921 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000922 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000923 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000924 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000925 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000926 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000927 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000928 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000929 }
930}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000931
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000932bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000933 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000934 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000935}
936
Daniel Dunbar55f16672010-09-17 02:47:07 +0000937const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000938AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000939 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000940 // Ask the target implementation about this expression first.
941 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
942 if (NewE)
943 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000944 // Recurse over the given expression, rebuilding it to apply the given variant
945 // if there is exactly one symbol.
946 switch (E->getKind()) {
947 case MCExpr::Target:
948 case MCExpr::Constant:
949 return 0;
950
951 case MCExpr::SymbolRef: {
952 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
953
954 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000955 TokError("invalid variant on expression '" + getTok().getIdentifier() +
956 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000957 return E;
958 }
959
960 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
961 }
962
963 case MCExpr::Unary: {
964 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000965 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000966 if (!Sub)
967 return 0;
968 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
969 }
970
971 case MCExpr::Binary: {
972 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000973 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
974 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000975
976 if (!LHS && !RHS)
977 return 0;
978
Jim Grosbach4b905842013-09-20 23:08:21 +0000979 if (!LHS)
980 LHS = BE->getLHS();
981 if (!RHS)
982 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +0000983
984 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
985 }
986 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +0000987
Craig Toppera2886c22012-02-07 05:05:23 +0000988 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000989}
990
Jim Grosbach4b905842013-09-20 23:08:21 +0000991/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +0000992///
Jim Grosbachbd164242011-08-20 16:24:13 +0000993/// expr ::= expr &&,|| expr -> lowest.
994/// expr ::= expr |,^,&,! expr
995/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
996/// expr ::= expr <<,>> expr
997/// expr ::= expr +,- expr
998/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000999/// expr ::= primaryexpr
1000///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001001bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001002 // Parse the expression.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001003 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001004 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001005 return true;
1006
Daniel Dunbar55f16672010-09-17 02:47:07 +00001007 // As a special case, we support 'a op b @ modifier' by rewriting the
1008 // expression to include the modifier. This is inefficient, but in general we
1009 // expect users to use 'a@modifier op b'.
1010 if (Lexer.getKind() == AsmToken::At) {
1011 Lex();
1012
1013 if (Lexer.isNot(AsmToken::Identifier))
1014 return TokError("unexpected symbol modifier following '@'");
1015
1016 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001017 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001018 if (Variant == MCSymbolRefExpr::VK_Invalid)
1019 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1020
Jim Grosbach4b905842013-09-20 23:08:21 +00001021 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001022 if (!ModifiedRes) {
1023 return TokError("invalid modifier '" + getTok().getIdentifier() +
1024 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001025 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001026
Daniel Dunbar55f16672010-09-17 02:47:07 +00001027 Res = ModifiedRes;
1028 Lex();
1029 }
1030
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001031 // Try to constant fold it up front, if possible.
1032 int64_t Value;
1033 if (Res->EvaluateAsAbsolute(Value))
1034 Res = MCConstantExpr::Create(Value, getContext());
1035
1036 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001037}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001038
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001039bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner807a3bc2010-01-24 01:07:33 +00001040 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001041 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001042}
1043
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001044bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001045 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001046
Daniel Dunbar75630b32009-06-30 02:10:03 +00001047 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001048 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001049 return true;
1050
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001051 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001052 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001053
1054 return false;
1055}
1056
Michael J. Spencer530ce852010-10-09 11:00:50 +00001057static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001058 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001059 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001060 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001061 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001062
Jim Grosbach4b905842013-09-20 23:08:21 +00001063 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001064 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001065 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001066 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001067 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001068 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001069 return 1;
1070
Jim Grosbach4b905842013-09-20 23:08:21 +00001071 // Low Precedence: |, &, ^
1072 //
1073 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001074 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001075 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001076 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001077 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001078 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001079 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001080 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001081 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001082 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001083
Jim Grosbach4b905842013-09-20 23:08:21 +00001084 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001085 case AsmToken::EqualEqual:
1086 Kind = MCBinaryExpr::EQ;
1087 return 3;
1088 case AsmToken::ExclaimEqual:
1089 case AsmToken::LessGreater:
1090 Kind = MCBinaryExpr::NE;
1091 return 3;
1092 case AsmToken::Less:
1093 Kind = MCBinaryExpr::LT;
1094 return 3;
1095 case AsmToken::LessEqual:
1096 Kind = MCBinaryExpr::LTE;
1097 return 3;
1098 case AsmToken::Greater:
1099 Kind = MCBinaryExpr::GT;
1100 return 3;
1101 case AsmToken::GreaterEqual:
1102 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001103 return 3;
1104
Jim Grosbach4b905842013-09-20 23:08:21 +00001105 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001106 case AsmToken::LessLess:
1107 Kind = MCBinaryExpr::Shl;
1108 return 4;
1109 case AsmToken::GreaterGreater:
1110 Kind = MCBinaryExpr::Shr;
1111 return 4;
1112
Jim Grosbach4b905842013-09-20 23:08:21 +00001113 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001114 case AsmToken::Plus:
1115 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001116 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001117 case AsmToken::Minus:
1118 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001119 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001120
Jim Grosbach4b905842013-09-20 23:08:21 +00001121 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001122 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001123 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001124 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001125 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001126 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001127 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001128 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001129 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001130 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001131 }
1132}
1133
Jim Grosbach4b905842013-09-20 23:08:21 +00001134/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001135/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001136bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001137 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001138 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001139 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001140 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001141
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001142 // If the next token is lower precedence than we are allowed to eat, return
1143 // successfully with what we ate already.
1144 if (TokPrec < Precedence)
1145 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001146
Sean Callanan686ed8d2010-01-19 20:22:31 +00001147 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001148
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001149 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001150 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001151 if (parsePrimaryExpr(RHS, EndLoc))
1152 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001153
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001154 // If BinOp binds less tightly with RHS than the operator after RHS, let
1155 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001156 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001157 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001158 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1159 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001160
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001161 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001162 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001163 }
1164}
1165
Chris Lattner36e02122009-06-21 20:54:55 +00001166/// ParseStatement:
1167/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001168/// ::= Label* Directive ...Operands... EndOfStatement
1169/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001170bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001171 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001172 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001173 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001174 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001175 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001176
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001177 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001178 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001179 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001180 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001181 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001182 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001183 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001184 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001185
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001186 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001187 if (Lexer.is(AsmToken::Integer)) {
1188 LocalLabelVal = getTok().getIntVal();
1189 if (LocalLabelVal < 0) {
1190 if (!TheCondState.Ignore)
1191 return TokError("unexpected token at start of statement");
1192 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001193 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001194 IDVal = getTok().getString();
1195 Lex(); // Consume the integer token to be used as an identifier token.
1196 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001197 if (!TheCondState.Ignore)
1198 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001199 }
1200 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001201 } else if (Lexer.is(AsmToken::Dot)) {
1202 // Treat '.' as a valid identifier in this context.
1203 Lex();
1204 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001205 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001206 if (!TheCondState.Ignore)
1207 return TokError("unexpected token at start of statement");
1208 IDVal = "";
1209 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001210
Chris Lattner926885c2010-04-17 18:14:27 +00001211 // Handle conditional assembly here before checking for skipping. We
1212 // have to do this so that .endif isn't skipped in a ".if 0" block for
1213 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001214 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001215 DirectiveKindMap.find(IDVal);
1216 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1217 ? DK_NO_DIRECTIVE
1218 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001219 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001220 default:
1221 break;
1222 case DK_IF:
1223 return parseDirectiveIf(IDLoc);
1224 case DK_IFB:
1225 return parseDirectiveIfb(IDLoc, true);
1226 case DK_IFNB:
1227 return parseDirectiveIfb(IDLoc, false);
1228 case DK_IFC:
1229 return parseDirectiveIfc(IDLoc, true);
1230 case DK_IFNC:
1231 return parseDirectiveIfc(IDLoc, false);
1232 case DK_IFDEF:
1233 return parseDirectiveIfdef(IDLoc, true);
1234 case DK_IFNDEF:
1235 case DK_IFNOTDEF:
1236 return parseDirectiveIfdef(IDLoc, false);
1237 case DK_ELSEIF:
1238 return parseDirectiveElseIf(IDLoc);
1239 case DK_ELSE:
1240 return parseDirectiveElse(IDLoc);
1241 case DK_ENDIF:
1242 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001243 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001244
Eli Bendersky88024712013-01-16 19:32:36 +00001245 // Ignore the statement if in the middle of inactive conditional
1246 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001247 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001248 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001249 return false;
1250 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001251
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001252 // FIXME: Recurse on local labels?
1253
1254 // See what kind of statement we have.
1255 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001256 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001257 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001258
Chris Lattner36e02122009-06-21 20:54:55 +00001259 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001260 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001261
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001262 // Diagnose attempt to use '.' as a label.
1263 if (IDVal == ".")
1264 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1265
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001266 // Diagnose attempt to use a variable as a label.
1267 //
1268 // FIXME: Diagnostics. Note the location of the definition as a label.
1269 // FIXME: This doesn't diagnose assignment to a symbol which has been
1270 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001271 MCSymbol *Sym;
1272 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001273 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001274 else
1275 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001276 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001277 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001278
Daniel Dunbare73b2672009-08-26 22:13:22 +00001279 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001280 if (!ParsingInlineAsm)
1281 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001282
Kevin Enderbye7739d42011-12-09 18:09:40 +00001283 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001284 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001285 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001286 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1287 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001288
Tim Northover1744d0a2013-10-25 12:49:50 +00001289 getTargetParser().onLabelParsed(Sym);
1290
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001291 // Consume any end of statement token, if present, to avoid spurious
1292 // AddBlankLine calls().
1293 if (Lexer.is(AsmToken::EndOfStatement)) {
1294 Lex();
1295 if (Lexer.is(AsmToken::Eof))
1296 return false;
1297 }
1298
Eli Friedman0f4871d2012-10-22 23:58:19 +00001299 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001300 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001301
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001302 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001303 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001304 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001305
Jim Grosbach4b905842013-09-20 23:08:21 +00001306 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001307
1308 default: // Normal instruction or directive.
1309 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001310 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001311
1312 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001313 if (areMacrosEnabled())
1314 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1315 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001316 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001317
Michael J. Spencer530ce852010-10-09 11:00:50 +00001318 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001319
Eli Bendersky17233942013-01-15 22:59:42 +00001320 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001321 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001322 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001323 //
Eli Bendersky17233942013-01-15 22:59:42 +00001324 // 1. The target-specific assembly parser. Some directives are target
1325 // specific or may potentially behave differently on certain targets.
1326 // 2. Asm parser extensions. For example, platform-specific parsers
1327 // (like the ELF parser) register themselves as extensions.
1328 // 3. The generic directive parser implemented by this class. These are
1329 // all the directives that behave in a target and platform independent
1330 // manner, or at least have a default behavior that's shared between
1331 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001332
Eli Bendersky17233942013-01-15 22:59:42 +00001333 // First query the target-specific parser. It will return 'true' if it
1334 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001335 if (!getTargetParser().ParseDirective(ID))
1336 return false;
1337
Eli Bendersky17233942013-01-15 22:59:42 +00001338 // Next, check the extention directive map to see if any extension has
1339 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001340 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1341 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001342 if (Handler.first)
1343 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1344
1345 // Finally, if no one else is interested in this directive, it must be
1346 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001347 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001348 default:
1349 break;
1350 case DK_SET:
1351 case DK_EQU:
1352 return parseDirectiveSet(IDVal, true);
1353 case DK_EQUIV:
1354 return parseDirectiveSet(IDVal, false);
1355 case DK_ASCII:
1356 return parseDirectiveAscii(IDVal, false);
1357 case DK_ASCIZ:
1358 case DK_STRING:
1359 return parseDirectiveAscii(IDVal, true);
1360 case DK_BYTE:
1361 return parseDirectiveValue(1);
1362 case DK_SHORT:
1363 case DK_VALUE:
1364 case DK_2BYTE:
1365 return parseDirectiveValue(2);
1366 case DK_LONG:
1367 case DK_INT:
1368 case DK_4BYTE:
1369 return parseDirectiveValue(4);
1370 case DK_QUAD:
1371 case DK_8BYTE:
1372 return parseDirectiveValue(8);
1373 case DK_SINGLE:
1374 case DK_FLOAT:
1375 return parseDirectiveRealValue(APFloat::IEEEsingle);
1376 case DK_DOUBLE:
1377 return parseDirectiveRealValue(APFloat::IEEEdouble);
1378 case DK_ALIGN: {
1379 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1380 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1381 }
1382 case DK_ALIGN32: {
1383 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1384 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1385 }
1386 case DK_BALIGN:
1387 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1388 case DK_BALIGNW:
1389 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1390 case DK_BALIGNL:
1391 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1392 case DK_P2ALIGN:
1393 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1394 case DK_P2ALIGNW:
1395 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1396 case DK_P2ALIGNL:
1397 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1398 case DK_ORG:
1399 return parseDirectiveOrg();
1400 case DK_FILL:
1401 return parseDirectiveFill();
1402 case DK_ZERO:
1403 return parseDirectiveZero();
1404 case DK_EXTERN:
1405 eatToEndOfStatement(); // .extern is the default, ignore it.
1406 return false;
1407 case DK_GLOBL:
1408 case DK_GLOBAL:
1409 return parseDirectiveSymbolAttribute(MCSA_Global);
1410 case DK_LAZY_REFERENCE:
1411 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1412 case DK_NO_DEAD_STRIP:
1413 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1414 case DK_SYMBOL_RESOLVER:
1415 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1416 case DK_PRIVATE_EXTERN:
1417 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1418 case DK_REFERENCE:
1419 return parseDirectiveSymbolAttribute(MCSA_Reference);
1420 case DK_WEAK_DEFINITION:
1421 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1422 case DK_WEAK_REFERENCE:
1423 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1424 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1425 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1426 case DK_COMM:
1427 case DK_COMMON:
1428 return parseDirectiveComm(/*IsLocal=*/false);
1429 case DK_LCOMM:
1430 return parseDirectiveComm(/*IsLocal=*/true);
1431 case DK_ABORT:
1432 return parseDirectiveAbort();
1433 case DK_INCLUDE:
1434 return parseDirectiveInclude();
1435 case DK_INCBIN:
1436 return parseDirectiveIncbin();
1437 case DK_CODE16:
1438 case DK_CODE16GCC:
1439 return TokError(Twine(IDVal) + " not supported yet");
1440 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001441 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001442 case DK_IRP:
1443 return parseDirectiveIrp(IDLoc);
1444 case DK_IRPC:
1445 return parseDirectiveIrpc(IDLoc);
1446 case DK_ENDR:
1447 return parseDirectiveEndr(IDLoc);
1448 case DK_BUNDLE_ALIGN_MODE:
1449 return parseDirectiveBundleAlignMode();
1450 case DK_BUNDLE_LOCK:
1451 return parseDirectiveBundleLock();
1452 case DK_BUNDLE_UNLOCK:
1453 return parseDirectiveBundleUnlock();
1454 case DK_SLEB128:
1455 return parseDirectiveLEB128(true);
1456 case DK_ULEB128:
1457 return parseDirectiveLEB128(false);
1458 case DK_SPACE:
1459 case DK_SKIP:
1460 return parseDirectiveSpace(IDVal);
1461 case DK_FILE:
1462 return parseDirectiveFile(IDLoc);
1463 case DK_LINE:
1464 return parseDirectiveLine();
1465 case DK_LOC:
1466 return parseDirectiveLoc();
1467 case DK_STABS:
1468 return parseDirectiveStabs();
1469 case DK_CFI_SECTIONS:
1470 return parseDirectiveCFISections();
1471 case DK_CFI_STARTPROC:
1472 return parseDirectiveCFIStartProc();
1473 case DK_CFI_ENDPROC:
1474 return parseDirectiveCFIEndProc();
1475 case DK_CFI_DEF_CFA:
1476 return parseDirectiveCFIDefCfa(IDLoc);
1477 case DK_CFI_DEF_CFA_OFFSET:
1478 return parseDirectiveCFIDefCfaOffset();
1479 case DK_CFI_ADJUST_CFA_OFFSET:
1480 return parseDirectiveCFIAdjustCfaOffset();
1481 case DK_CFI_DEF_CFA_REGISTER:
1482 return parseDirectiveCFIDefCfaRegister(IDLoc);
1483 case DK_CFI_OFFSET:
1484 return parseDirectiveCFIOffset(IDLoc);
1485 case DK_CFI_REL_OFFSET:
1486 return parseDirectiveCFIRelOffset(IDLoc);
1487 case DK_CFI_PERSONALITY:
1488 return parseDirectiveCFIPersonalityOrLsda(true);
1489 case DK_CFI_LSDA:
1490 return parseDirectiveCFIPersonalityOrLsda(false);
1491 case DK_CFI_REMEMBER_STATE:
1492 return parseDirectiveCFIRememberState();
1493 case DK_CFI_RESTORE_STATE:
1494 return parseDirectiveCFIRestoreState();
1495 case DK_CFI_SAME_VALUE:
1496 return parseDirectiveCFISameValue(IDLoc);
1497 case DK_CFI_RESTORE:
1498 return parseDirectiveCFIRestore(IDLoc);
1499 case DK_CFI_ESCAPE:
1500 return parseDirectiveCFIEscape();
1501 case DK_CFI_SIGNAL_FRAME:
1502 return parseDirectiveCFISignalFrame();
1503 case DK_CFI_UNDEFINED:
1504 return parseDirectiveCFIUndefined(IDLoc);
1505 case DK_CFI_REGISTER:
1506 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001507 case DK_CFI_WINDOW_SAVE:
1508 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001509 case DK_MACROS_ON:
1510 case DK_MACROS_OFF:
1511 return parseDirectiveMacrosOnOff(IDVal);
1512 case DK_MACRO:
1513 return parseDirectiveMacro(IDLoc);
1514 case DK_ENDM:
1515 case DK_ENDMACRO:
1516 return parseDirectiveEndMacro(IDVal);
1517 case DK_PURGEM:
1518 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001519 case DK_END:
1520 return parseDirectiveEnd(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001521 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001522
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001523 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001524 }
Chris Lattner36e02122009-06-21 20:54:55 +00001525
Chad Rosierc7f552c2013-02-12 21:33:51 +00001526 // __asm _emit or __asm __emit
1527 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1528 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001529 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001530
1531 // __asm align
1532 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001533 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001534
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001535 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001536
Chris Lattner7cbfa442010-05-19 23:34:33 +00001537 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001538 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001539 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001540 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001541 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001542 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001543
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001544 // Dump the parsed representation, if requested.
1545 if (getShowParsedOperands()) {
1546 SmallString<256> Str;
1547 raw_svector_ostream OS(Str);
1548 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001549 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001550 if (i != 0)
1551 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001552 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001553 }
1554 OS << "]";
1555
Jim Grosbach4b905842013-09-20 23:08:21 +00001556 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001557 }
1558
Kevin Enderby6469fc22011-11-01 22:27:22 +00001559 // If we are generating dwarf for assembly source files and the current
1560 // section is the initial text section then generate a .loc directive for
1561 // the instruction.
1562 if (!HadError && getContext().getGenDwarfForAssembly() &&
Peter Collingbourne2f495b92013-04-17 21:18:16 +00001563 getContext().getGenDwarfSection() ==
Jim Grosbach4b905842013-09-20 23:08:21 +00001564 getStreamer().getCurrentSection().first) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001565
Eli Bendersky88024712013-01-16 19:32:36 +00001566 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001567
Eli Bendersky88024712013-01-16 19:32:36 +00001568 // If we previously parsed a cpp hash file line comment then make sure the
1569 // current Dwarf File is for the CppHashFilename if not then emit the
1570 // Dwarf File table for it and adjust the line number for the .loc.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001571 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +00001572 getContext().getMCDwarfFiles();
Eli Bendersky88024712013-01-16 19:32:36 +00001573 if (CppHashFilename.size() != 0) {
1574 if (MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() !=
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001575 CppHashFilename)
Eli Bendersky88024712013-01-16 19:32:36 +00001576 getStreamer().EmitDwarfFileDirective(
Jim Grosbach4b905842013-09-20 23:08:21 +00001577 getContext().nextGenDwarfFileNumber(), StringRef(),
1578 CppHashFilename);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001579
Jim Grosbach4b905842013-09-20 23:08:21 +00001580 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1581 // cache with the different Loc from the call above we save the last
1582 // info we queried here with SrcMgr.FindLineNumber().
1583 unsigned CppHashLocLineNo;
1584 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1585 CppHashLocLineNo = LastQueryLine;
1586 else {
1587 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1588 LastQueryLine = CppHashLocLineNo;
1589 LastQueryIDLoc = CppHashLoc;
1590 LastQueryBuffer = CppHashBuf;
1591 }
1592 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001593 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001594
Jim Grosbach4b905842013-09-20 23:08:21 +00001595 getStreamer().EmitDwarfLocDirective(
1596 getContext().getGenDwarfFileNumber(), Line, 0,
1597 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1598 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001599 }
1600
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001601 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001602 if (!HadError) {
Chad Rosier49963552012-10-13 00:26:04 +00001603 unsigned ErrorInfo;
Jim Grosbach4b905842013-09-20 23:08:21 +00001604 HadError = getTargetParser().MatchAndEmitInstruction(
1605 IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo,
1606 ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001607 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001608
Chris Lattnera2a9d162010-09-11 16:18:25 +00001609 // Don't skip the rest of the line, the instruction parser is responsible for
1610 // that.
1611 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001612}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001613
Jim Grosbach4b905842013-09-20 23:08:21 +00001614/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001615/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001616void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001617 if (!Lexer.is(AsmToken::EndOfStatement))
1618 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001619 // Eat EOL.
1620 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001621}
1622
Jim Grosbach4b905842013-09-20 23:08:21 +00001623/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001624/// ::= # number "filename"
1625/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001626bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001627 Lex(); // Eat the hash token.
1628
1629 if (getLexer().isNot(AsmToken::Integer)) {
1630 // Consume the line since in cases it is not a well-formed line directive,
1631 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001632 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001633 return false;
1634 }
1635
1636 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001637 Lex();
1638
1639 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001640 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001641 return false;
1642 }
1643
1644 StringRef Filename = getTok().getString();
1645 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001646 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001647
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001648 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1649 CppHashLoc = L;
1650 CppHashFilename = Filename;
1651 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001652 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001653
1654 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001655 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001656 return false;
1657}
1658
Jim Grosbach4b905842013-09-20 23:08:21 +00001659/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001660/// for the Filename and LineNo if any in the diagnostic.
1661void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001662 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001663 raw_ostream &OS = errs();
1664
1665 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1666 const SMLoc &DiagLoc = Diag.getLoc();
1667 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1668 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1669
Jim Grosbach4b905842013-09-20 23:08:21 +00001670 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001671 // before printing the message.
1672 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001673 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001674 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1675 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001676 }
1677
Eric Christophera7c32732012-12-18 00:30:54 +00001678 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001679 // manager changed or buffer changed (like in a nested include) then just
1680 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001681 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001682 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001683 if (Parser->SavedDiagHandler)
1684 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1685 else
1686 Diag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001687 return;
1688 }
1689
Eric Christophera7c32732012-12-18 00:30:54 +00001690 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001691 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1692 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001693 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001694
1695 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1696 int CppHashLocLineNo =
1697 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001698 int LineNo =
1699 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001700
Jim Grosbach4b905842013-09-20 23:08:21 +00001701 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1702 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001703 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001704
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001705 if (Parser->SavedDiagHandler)
1706 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1707 else
1708 NewDiag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001709}
1710
Rafael Espindola2c064482012-08-21 18:29:30 +00001711// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1712// difference being that that function accepts '@' as part of identifiers and
1713// we can't do that. AsmLexer.cpp should probably be changed to handle
1714// '@' as a special case when needed.
1715static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001716 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1717 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001718}
1719
Rafael Espindola34b9c512012-06-03 23:57:14 +00001720bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Eli Bendersky38274122013-01-14 23:22:36 +00001721 const MCAsmMacroParameters &Parameters,
Jim Grosbach4b905842013-09-20 23:08:21 +00001722 const MCAsmMacroArguments &A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001723 unsigned NParameters = Parameters.size();
1724 if (NParameters != 0 && NParameters != A.size())
1725 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001726
Preston Gurd05500642012-09-19 20:36:12 +00001727 // A macro without parameters is handled differently on Darwin:
1728 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001729 while (!Body.empty()) {
1730 // Scan for the next substitution.
1731 std::size_t End = Body.size(), Pos = 0;
1732 for (; Pos != End; ++Pos) {
1733 // Check for a substitution or escape.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001734 if (!NParameters) {
1735 // This macro has no parameters, look for $0, $1, etc.
1736 if (Body[Pos] != '$' || Pos + 1 == End)
1737 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001738
Rafael Espindola1134ab232011-06-05 02:43:45 +00001739 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001740 if (Next == '$' || Next == 'n' ||
1741 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001742 break;
1743 } else {
1744 // This macro has parameters, look for \foo, \bar, etc.
1745 if (Body[Pos] == '\\' && Pos + 1 != End)
1746 break;
1747 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001748 }
1749
1750 // Add the prefix.
1751 OS << Body.slice(0, Pos);
1752
1753 // Check if we reached the end.
1754 if (Pos == End)
1755 break;
1756
Rafael Espindola1134ab232011-06-05 02:43:45 +00001757 if (!NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001758 switch (Body[Pos + 1]) {
1759 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001760 case '$':
1761 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001762 break;
1763
Jim Grosbach4b905842013-09-20 23:08:21 +00001764 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001765 case 'n':
1766 OS << A.size();
1767 break;
1768
Jim Grosbach4b905842013-09-20 23:08:21 +00001769 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001770 default: {
1771 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001772 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001773 if (Index >= A.size())
1774 break;
1775
1776 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001777 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001778 ie = A[Index].end();
1779 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001780 OS << it->getString();
1781 break;
1782 }
1783 }
1784 Pos += 2;
1785 } else {
1786 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001787 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001788 ++I;
1789
Jim Grosbach4b905842013-09-20 23:08:21 +00001790 const char *Begin = Body.data() + Pos + 1;
1791 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001792 unsigned Index = 0;
1793 for (; Index < NParameters; ++Index)
Preston Gurd242ed3152012-09-19 20:29:04 +00001794 if (Parameters[Index].first == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001795 break;
1796
Preston Gurd05500642012-09-19 20:36:12 +00001797 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001798 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1799 Pos += 3;
1800 else {
1801 OS << '\\' << Argument;
1802 Pos = I;
1803 }
Preston Gurd05500642012-09-19 20:36:12 +00001804 } else {
Eli Benderskya7b905e2013-01-14 19:00:26 +00001805 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001806 ie = A[Index].end();
1807 it != ie; ++it)
Preston Gurd05500642012-09-19 20:36:12 +00001808 if (it->getKind() == AsmToken::String)
1809 OS << it->getStringContents();
1810 else
1811 OS << it->getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001812
Preston Gurd05500642012-09-19 20:36:12 +00001813 Pos += 1 + Argument.size();
1814 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001815 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001816 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001817 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001818 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001819
Rafael Espindola1134ab232011-06-05 02:43:45 +00001820 return false;
1821}
Daniel Dunbar43235712010-07-18 18:54:11 +00001822
Jim Grosbach4b905842013-09-20 23:08:21 +00001823MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1824 SMLoc EL, MemoryBuffer *I)
1825 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1826 ExitLoc(EL) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001827
Jim Grosbach4b905842013-09-20 23:08:21 +00001828static bool isOperator(AsmToken::TokenKind kind) {
1829 switch (kind) {
1830 default:
1831 return false;
1832 case AsmToken::Plus:
1833 case AsmToken::Minus:
1834 case AsmToken::Tilde:
1835 case AsmToken::Slash:
1836 case AsmToken::Star:
1837 case AsmToken::Dot:
1838 case AsmToken::Equal:
1839 case AsmToken::EqualEqual:
1840 case AsmToken::Pipe:
1841 case AsmToken::PipePipe:
1842 case AsmToken::Caret:
1843 case AsmToken::Amp:
1844 case AsmToken::AmpAmp:
1845 case AsmToken::Exclaim:
1846 case AsmToken::ExclaimEqual:
1847 case AsmToken::Percent:
1848 case AsmToken::Less:
1849 case AsmToken::LessEqual:
1850 case AsmToken::LessLess:
1851 case AsmToken::LessGreater:
1852 case AsmToken::Greater:
1853 case AsmToken::GreaterEqual:
1854 case AsmToken::GreaterGreater:
1855 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001856 }
1857}
1858
Jim Grosbach4b905842013-09-20 23:08:21 +00001859bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA,
Preston Gurd05500642012-09-19 20:36:12 +00001860 AsmToken::TokenKind &ArgumentDelimiter) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001861 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001862 unsigned AddTokens = 0;
1863
1864 // gas accepts arguments separated by whitespace, except on Darwin
1865 if (!IsDarwin)
1866 Lexer.setSkipSpace(false);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001867
1868 for (;;) {
Preston Gurd05500642012-09-19 20:36:12 +00001869 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) {
1870 Lexer.setSkipSpace(true);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001871 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001872 }
1873
1874 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1875 // Spaces and commas cannot be mixed to delimit parameters
1876 if (ArgumentDelimiter == AsmToken::Eof)
1877 ArgumentDelimiter = AsmToken::Comma;
1878 else if (ArgumentDelimiter != AsmToken::Comma) {
1879 Lexer.setSkipSpace(true);
1880 return TokError("expected ' ' for macro argument separator");
1881 }
1882 break;
1883 }
1884
1885 if (Lexer.is(AsmToken::Space)) {
1886 Lex(); // Eat spaces
1887
1888 // Spaces can delimit parameters, but could also be part an expression.
1889 // If the token after a space is an operator, add the token and the next
1890 // one into this argument
1891 if (ArgumentDelimiter == AsmToken::Space ||
1892 ArgumentDelimiter == AsmToken::Eof) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001893 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001894 // Check to see whether the token is used as an operator,
1895 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001896 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001897 if (*NextChar == ' ')
1898 AddTokens = 2;
1899 }
1900
1901 if (!AddTokens && ParenLevel == 0) {
1902 if (ArgumentDelimiter == AsmToken::Eof &&
Jim Grosbach4b905842013-09-20 23:08:21 +00001903 !isOperator(Lexer.getKind()))
Preston Gurd05500642012-09-19 20:36:12 +00001904 ArgumentDelimiter = AsmToken::Space;
1905 break;
1906 }
1907 }
1908 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001909
Jim Grosbach4b905842013-09-20 23:08:21 +00001910 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001911 // to be able to fill in the remaining default parameter values
1912 if (Lexer.is(AsmToken::EndOfStatement))
1913 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001914
1915 // Adjust the current parentheses level.
1916 if (Lexer.is(AsmToken::LParen))
1917 ++ParenLevel;
1918 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1919 --ParenLevel;
1920
1921 // Append the token to the current argument list.
1922 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001923 if (AddTokens)
1924 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001925 Lex();
1926 }
Preston Gurd05500642012-09-19 20:36:12 +00001927
1928 Lexer.setSkipSpace(true);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001929 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001930 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001931 return false;
1932}
1933
1934// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001935bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001936 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001937 const unsigned NParameters = M ? M->Parameters.size() : 0;
Preston Gurd05500642012-09-19 20:36:12 +00001938 // Argument delimiter is initially unknown. It will be set by
Jim Grosbach4b905842013-09-20 23:08:21 +00001939 // parseMacroArgument()
Preston Gurd05500642012-09-19 20:36:12 +00001940 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001941
1942 // Parse two kinds of macro invocations:
1943 // - macros defined without any parameters accept an arbitrary number of them
1944 // - macros defined with parameters accept at most that many of them
1945 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1946 ++Parameter) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00001947 MCAsmMacroArgument MA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001948
Jim Grosbach4b905842013-09-20 23:08:21 +00001949 if (parseMacroArgument(MA, ArgumentDelimiter))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001950 return true;
1951
Preston Gurd242ed3152012-09-19 20:29:04 +00001952 if (!MA.empty() || !NParameters)
1953 A.push_back(MA);
1954 else if (NParameters) {
1955 if (!M->Parameters[Parameter].second.empty())
1956 A.push_back(M->Parameters[Parameter].second);
1957 }
Jim Grosbach206661622012-07-30 22:44:17 +00001958
Preston Gurd242ed3152012-09-19 20:29:04 +00001959 // At the end of the statement, fill in remaining arguments that have
1960 // default values. If there aren't any, then the next argument is
1961 // required but missing
1962 if (Lexer.is(AsmToken::EndOfStatement)) {
1963 if (NParameters && Parameter < NParameters - 1) {
1964 if (M->Parameters[Parameter + 1].second.empty())
1965 return TokError("macro argument '" +
1966 Twine(M->Parameters[Parameter + 1].first) +
1967 "' is missing");
1968 else
1969 continue;
1970 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001971 return false;
Preston Gurd242ed3152012-09-19 20:29:04 +00001972 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001973
1974 if (Lexer.is(AsmToken::Comma))
1975 Lex();
1976 }
1977 return TokError("Too many arguments");
1978}
1979
Jim Grosbach4b905842013-09-20 23:08:21 +00001980const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
1981 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00001982 return (I == MacroMap.end()) ? NULL : I->getValue();
1983}
1984
Jim Grosbach4b905842013-09-20 23:08:21 +00001985void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00001986 MacroMap[Name] = new MCAsmMacro(Macro);
1987}
1988
Jim Grosbach4b905842013-09-20 23:08:21 +00001989void AsmParser::undefineMacro(StringRef Name) {
1990 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00001991 if (I != MacroMap.end()) {
1992 delete I->getValue();
1993 MacroMap.erase(I);
1994 }
1995}
1996
Jim Grosbach4b905842013-09-20 23:08:21 +00001997bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00001998 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1999 // this, although we should protect against infinite loops.
2000 if (ActiveMacros.size() == 20)
2001 return TokError("macros cannot be nested more than 20 levels deep");
2002
Eli Bendersky38274122013-01-14 23:22:36 +00002003 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002004 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002005 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002006
Jim Grosbach206661622012-07-30 22:44:17 +00002007 // Remove any trailing empty arguments. Do this after-the-fact as we have
2008 // to keep empty arguments in the middle of the list or positionality
2009 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002010 while (!A.empty() && A.back().empty())
2011 A.pop_back();
Jim Grosbach206661622012-07-30 22:44:17 +00002012
Rafael Espindola1134ab232011-06-05 02:43:45 +00002013 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2014 // to hold the macro body with substitutions.
2015 SmallString<256> Buf;
2016 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002017 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002018
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002019 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002020 return true;
2021
Eli Bendersky38274122013-01-14 23:22:36 +00002022 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002023 // instantiation.
2024 OS << ".endmacro\n";
2025
Rafael Espindola1134ab232011-06-05 02:43:45 +00002026 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00002027 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002028
Daniel Dunbar43235712010-07-18 18:54:11 +00002029 // Create the macro instantiation object and add to the current macro
2030 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00002031 MacroInstantiation *MI = new MacroInstantiation(
2032 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbar43235712010-07-18 18:54:11 +00002033 ActiveMacros.push_back(MI);
2034
2035 // Jump to the macro instantiation and prime the lexer.
2036 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
2037 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
2038 Lex();
2039
2040 return false;
2041}
2042
Jim Grosbach4b905842013-09-20 23:08:21 +00002043void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002044 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002045 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002046 Lex();
2047
2048 // Pop the instantiation entry.
2049 delete ActiveMacros.back();
2050 ActiveMacros.pop_back();
2051}
2052
Jim Grosbach4b905842013-09-20 23:08:21 +00002053static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002054 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002055 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002056 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2057 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002058 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002059 case MCExpr::Target:
2060 case MCExpr::Constant:
2061 return false;
2062 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002063 const MCSymbol &S =
2064 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002065 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002066 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002067 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002068 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002069 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002070 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002071 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002072
2073 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002074}
2075
Jim Grosbach4b905842013-09-20 23:08:21 +00002076bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002077 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002078 // FIXME: Use better location, we should use proper tokens.
2079 SMLoc EqualLoc = Lexer.getLoc();
2080
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002081 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002082 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002083 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002084
Rafael Espindola72f5f172012-01-28 05:57:00 +00002085 // Note: we don't count b as used in "a = b". This is to allow
2086 // a = b
2087 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002088
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002089 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002090 return TokError("unexpected token in assignment");
2091
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00002092 // Error on assignment to '.'.
2093 if (Name == ".") {
2094 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
2095 "(use '.space' or '.org').)"));
2096 }
2097
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002098 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002099 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002100
Daniel Dunbar5f339242009-10-16 01:57:39 +00002101 // Validate that the LHS is allowed to be a variable (either it has not been
2102 // used as a symbol, or it is an absolute symbol).
2103 MCSymbol *Sym = getContext().LookupSymbol(Name);
2104 if (Sym) {
2105 // Diagnose assignment to a label.
2106 //
2107 // FIXME: Diagnostics. Note the location of the definition as a label.
2108 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002109 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002110 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2111 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002112 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002113 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2114 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002115 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002116 return Error(EqualLoc, "redefinition of '" + Name + "'");
2117 else if (!Sym->isVariable())
2118 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002119 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002120 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002121 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002122
2123 // Don't count these checks as uses.
2124 Sym->setUsed(false);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002125 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002126 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002127
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002128 // FIXME: Handle '.'.
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002129
2130 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002131 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002132 if (NoDeadStrip)
2133 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2134
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002135 return false;
2136}
2137
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002138/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002139/// ::= identifier
2140/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002141bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002142 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002143 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2144 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002145 // handle this as a context dependent token, instead we detect adjacent tokens
2146 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002147 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2148 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002149
Hans Wennborgce69d772013-10-18 20:46:28 +00002150 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002151 Lex();
2152 if (Lexer.isNot(AsmToken::Identifier))
2153 return true;
2154
Hans Wennborgce69d772013-10-18 20:46:28 +00002155 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2156 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002157 return true;
2158
2159 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002160 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002161 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002162 Lex();
2163 return false;
2164 }
2165
Jim Grosbach4b905842013-09-20 23:08:21 +00002166 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002167 return true;
2168
Sean Callanan936b0d32010-01-19 21:44:56 +00002169 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002170
Sean Callanan686ed8d2010-01-19 20:22:31 +00002171 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002172
2173 return false;
2174}
2175
Jim Grosbach4b905842013-09-20 23:08:21 +00002176/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002177/// ::= .equ identifier ',' expression
2178/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002179/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002180bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002181 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002182
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002183 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002184 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002185
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002186 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002187 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002188 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002189
Jim Grosbach4b905842013-09-20 23:08:21 +00002190 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002191}
2192
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002193bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002194 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002195
2196 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002197 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002198 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2199 if (Str[i] != '\\') {
2200 Data += Str[i];
2201 continue;
2202 }
2203
2204 // Recognize escaped characters. Note that this escape semantics currently
2205 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2206 ++i;
2207 if (i == e)
2208 return TokError("unexpected backslash at end of string");
2209
2210 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002211 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002212 // Consume up to three octal characters.
2213 unsigned Value = Str[i] - '0';
2214
Jim Grosbach4b905842013-09-20 23:08:21 +00002215 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002216 ++i;
2217 Value = Value * 8 + (Str[i] - '0');
2218
Jim Grosbach4b905842013-09-20 23:08:21 +00002219 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002220 ++i;
2221 Value = Value * 8 + (Str[i] - '0');
2222 }
2223 }
2224
2225 if (Value > 255)
2226 return TokError("invalid octal escape sequence (out of range)");
2227
Jim Grosbach4b905842013-09-20 23:08:21 +00002228 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002229 continue;
2230 }
2231
2232 // Otherwise recognize individual escapes.
2233 switch (Str[i]) {
2234 default:
2235 // Just reject invalid escape sequences for now.
2236 return TokError("invalid escape sequence (unrecognized character)");
2237
2238 case 'b': Data += '\b'; break;
2239 case 'f': Data += '\f'; break;
2240 case 'n': Data += '\n'; break;
2241 case 'r': Data += '\r'; break;
2242 case 't': Data += '\t'; break;
2243 case '"': Data += '"'; break;
2244 case '\\': Data += '\\'; break;
2245 }
2246 }
2247
2248 return false;
2249}
2250
Jim Grosbach4b905842013-09-20 23:08:21 +00002251/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002252/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002253bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002254 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002255 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002256
Daniel Dunbara10e5192009-06-24 23:30:00 +00002257 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002258 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002259 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002260
Daniel Dunbaref668c12009-08-14 18:19:52 +00002261 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002262 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002263 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002264
Rafael Espindola64e1af82013-07-02 15:49:13 +00002265 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002266 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002267 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002268
Sean Callanan686ed8d2010-01-19 20:22:31 +00002269 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002270
2271 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002272 break;
2273
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002274 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002275 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002276 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002277 }
2278 }
2279
Sean Callanan686ed8d2010-01-19 20:22:31 +00002280 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002281 return false;
2282}
2283
Jim Grosbach4b905842013-09-20 23:08:21 +00002284/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002285/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002286bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002287 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002288 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002289
Daniel Dunbara10e5192009-06-24 23:30:00 +00002290 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002291 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002292 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002293 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002294 return true;
2295
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002296 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002297 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2298 assert(Size <= 8 && "Invalid size");
2299 uint64_t IntValue = MCE->getValue();
2300 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2301 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002302 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002303 } else
Rafael Espindola64e1af82013-07-02 15:49:13 +00002304 getStreamer().EmitValue(Value, Size);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002305
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002306 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002307 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002308
Daniel Dunbara10e5192009-06-24 23:30:00 +00002309 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002310 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002311 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002312 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002313 }
2314 }
2315
Sean Callanan686ed8d2010-01-19 20:22:31 +00002316 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002317 return false;
2318}
2319
Jim Grosbach4b905842013-09-20 23:08:21 +00002320/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002321/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002322bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002323 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002324 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002325
2326 for (;;) {
2327 // We don't truly support arithmetic on floating point expressions, so we
2328 // have to manually parse unary prefixes.
2329 bool IsNeg = false;
2330 if (getLexer().is(AsmToken::Minus)) {
2331 Lex();
2332 IsNeg = true;
2333 } else if (getLexer().is(AsmToken::Plus))
2334 Lex();
2335
Michael J. Spencer530ce852010-10-09 11:00:50 +00002336 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002337 getLexer().isNot(AsmToken::Real) &&
2338 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002339 return TokError("unexpected token in directive");
2340
2341 // Convert to an APFloat.
2342 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002343 StringRef IDVal = getTok().getString();
2344 if (getLexer().is(AsmToken::Identifier)) {
2345 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2346 Value = APFloat::getInf(Semantics);
2347 else if (!IDVal.compare_lower("nan"))
2348 Value = APFloat::getNaN(Semantics, false, ~0);
2349 else
2350 return TokError("invalid floating point literal");
2351 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002352 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002353 return TokError("invalid floating point literal");
2354 if (IsNeg)
2355 Value.changeSign();
2356
2357 // Consume the numeric token.
2358 Lex();
2359
2360 // Emit the value as an integer.
2361 APInt AsInt = Value.bitcastToAPInt();
2362 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002363 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002364
2365 if (getLexer().is(AsmToken::EndOfStatement))
2366 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002367
Daniel Dunbar2af16532010-09-24 01:59:56 +00002368 if (getLexer().isNot(AsmToken::Comma))
2369 return TokError("unexpected token in directive");
2370 Lex();
2371 }
2372 }
2373
2374 Lex();
2375 return false;
2376}
2377
Jim Grosbach4b905842013-09-20 23:08:21 +00002378/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002379/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002380bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002381 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002382
2383 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002384 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002385 return true;
2386
Rafael Espindolab91bac62010-10-05 19:42:57 +00002387 int64_t Val = 0;
2388 if (getLexer().is(AsmToken::Comma)) {
2389 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002390 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002391 return true;
2392 }
2393
Rafael Espindola922e3f42010-09-16 15:03:59 +00002394 if (getLexer().isNot(AsmToken::EndOfStatement))
2395 return TokError("unexpected token in '.zero' directive");
2396
2397 Lex();
2398
Rafael Espindola64e1af82013-07-02 15:49:13 +00002399 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002400
2401 return false;
2402}
2403
Jim Grosbach4b905842013-09-20 23:08:21 +00002404/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002405/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002406bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002407 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002408
Daniel Dunbara10e5192009-06-24 23:30:00 +00002409 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002410 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002411 return true;
2412
Roman Divackye33098f2013-09-24 17:44:41 +00002413 int64_t FillSize = 1;
2414 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002415
Roman Divackye33098f2013-09-24 17:44:41 +00002416 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2417 if (getLexer().isNot(AsmToken::Comma))
2418 return TokError("unexpected token in '.fill' directive");
2419 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002420
Roman Divackye33098f2013-09-24 17:44:41 +00002421 if (parseAbsoluteExpression(FillSize))
2422 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002423
Roman Divackye33098f2013-09-24 17:44:41 +00002424 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2425 if (getLexer().isNot(AsmToken::Comma))
2426 return TokError("unexpected token in '.fill' directive");
2427 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002428
Roman Divackye33098f2013-09-24 17:44:41 +00002429 if (parseAbsoluteExpression(FillExpr))
2430 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002431
Roman Divackye33098f2013-09-24 17:44:41 +00002432 if (getLexer().isNot(AsmToken::EndOfStatement))
2433 return TokError("unexpected token in '.fill' directive");
2434
2435 Lex();
2436 }
2437 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002438
Daniel Dunbar8e5edd82009-08-21 15:43:35 +00002439 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2440 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara10e5192009-06-24 23:30:00 +00002441
2442 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002443 getStreamer().EmitIntValue(FillExpr, FillSize);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002444
2445 return false;
2446}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002447
Jim Grosbach4b905842013-09-20 23:08:21 +00002448/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002449/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002450bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002451 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002452
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002453 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002454 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002455 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002456 return true;
2457
2458 // Parse optional fill expression.
2459 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002460 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2461 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002462 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002463 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002464
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002465 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002466 return true;
2467
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002468 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002469 return TokError("unexpected token in '.org' directive");
2470 }
2471
Sean Callanan686ed8d2010-01-19 20:22:31 +00002472 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002473
Jim Grosbachb5912772012-01-27 00:37:08 +00002474 // Only limited forms of relocatable expressions are accepted here, it
2475 // has to be relative to the current section. The streamer will return
2476 // 'true' if the expression wasn't evaluatable.
2477 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2478 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002479
2480 return false;
2481}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002482
Jim Grosbach4b905842013-09-20 23:08:21 +00002483/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002484/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002485bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002486 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002487
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002488 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002489 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002490 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002491 return true;
2492
2493 SMLoc MaxBytesLoc;
2494 bool HasFillExpr = false;
2495 int64_t FillExpr = 0;
2496 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002497 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2498 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002499 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002500 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002501
2502 // The fill expression can be omitted while specifying a maximum number of
2503 // alignment bytes, e.g:
2504 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002505 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002506 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002507 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002508 return true;
2509 }
2510
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002511 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2512 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002513 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002514 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002515
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002516 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002517 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002518 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002519
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002520 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002521 return TokError("unexpected token in directive");
2522 }
2523 }
2524
Sean Callanan686ed8d2010-01-19 20:22:31 +00002525 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002526
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002527 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002528 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002529
2530 // Compute alignment in bytes.
2531 if (IsPow2) {
2532 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002533 if (Alignment >= 32) {
2534 Error(AlignmentLoc, "invalid alignment value");
2535 Alignment = 31;
2536 }
2537
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002538 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002539 } else {
2540 // Reject alignments that aren't a power of two, for gas compatibility.
2541 if (!isPowerOf2_64(Alignment))
2542 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002543 }
2544
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002545 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002546 if (MaxBytesLoc.isValid()) {
2547 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002548 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002549 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002550 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002551 }
2552
2553 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002554 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002555 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002556 MaxBytesToFill = 0;
2557 }
2558 }
2559
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002560 // Check whether we should use optimal code alignment for this .align
2561 // directive.
Peter Collingbourne2f495b92013-04-17 21:18:16 +00002562 bool UseCodeAlign = getStreamer().getCurrentSection().first->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002563 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2564 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002565 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002566 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002567 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002568 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2569 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002570 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002571
2572 return false;
2573}
2574
Jim Grosbach4b905842013-09-20 23:08:21 +00002575/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002576/// ::= .file [number] filename
2577/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002578bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002579 // FIXME: I'm not sure what this is.
2580 int64_t FileNumber = -1;
2581 SMLoc FileNumberLoc = getLexer().getLoc();
2582 if (getLexer().is(AsmToken::Integer)) {
2583 FileNumber = getTok().getIntVal();
2584 Lex();
2585
2586 if (FileNumber < 1)
2587 return TokError("file number less than one");
2588 }
2589
2590 if (getLexer().isNot(AsmToken::String))
2591 return TokError("unexpected token in '.file' directive");
2592
2593 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002594 // Allow the strings to have escaped octal character sequence.
2595 std::string Path = getTok().getString();
2596 if (parseEscapedString(Path))
2597 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002598 Lex();
2599
2600 StringRef Directory;
2601 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002602 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002603 if (getLexer().is(AsmToken::String)) {
2604 if (FileNumber == -1)
2605 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002606 if (parseEscapedString(FilenameData))
2607 return true;
2608 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002609 Directory = Path;
2610 Lex();
2611 } else {
2612 Filename = Path;
2613 }
2614
2615 if (getLexer().isNot(AsmToken::EndOfStatement))
2616 return TokError("unexpected token in '.file' directive");
2617
2618 if (FileNumber == -1)
2619 getStreamer().EmitFileDirective(Filename);
2620 else {
2621 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002622 Error(DirectiveLoc,
2623 "input can't have .file dwarf directives when -g is "
2624 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002625
2626 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
2627 Error(FileNumberLoc, "file number already allocated");
2628 }
2629
2630 return false;
2631}
2632
Jim Grosbach4b905842013-09-20 23:08:21 +00002633/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002634/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002635bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002636 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2637 if (getLexer().isNot(AsmToken::Integer))
2638 return TokError("unexpected token in '.line' directive");
2639
2640 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002641 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002642 Lex();
2643
2644 // FIXME: Do something with the .line.
2645 }
2646
2647 if (getLexer().isNot(AsmToken::EndOfStatement))
2648 return TokError("unexpected token in '.line' directive");
2649
2650 return false;
2651}
2652
Jim Grosbach4b905842013-09-20 23:08:21 +00002653/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002654/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2655/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2656/// The first number is a file number, must have been previously assigned with
2657/// a .file directive, the second number is the line number and optionally the
2658/// third number is a column position (zero if not specified). The remaining
2659/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002660bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002661 if (getLexer().isNot(AsmToken::Integer))
2662 return TokError("unexpected token in '.loc' directive");
2663 int64_t FileNumber = getTok().getIntVal();
2664 if (FileNumber < 1)
2665 return TokError("file number less than one in '.loc' directive");
2666 if (!getContext().isValidDwarfFileNumber(FileNumber))
2667 return TokError("unassigned file number in '.loc' directive");
2668 Lex();
2669
2670 int64_t LineNumber = 0;
2671 if (getLexer().is(AsmToken::Integer)) {
2672 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002673 if (LineNumber < 0)
2674 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002675 Lex();
2676 }
2677
2678 int64_t ColumnPos = 0;
2679 if (getLexer().is(AsmToken::Integer)) {
2680 ColumnPos = getTok().getIntVal();
2681 if (ColumnPos < 0)
2682 return TokError("column position less than zero in '.loc' directive");
2683 Lex();
2684 }
2685
2686 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2687 unsigned Isa = 0;
2688 int64_t Discriminator = 0;
2689 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2690 for (;;) {
2691 if (getLexer().is(AsmToken::EndOfStatement))
2692 break;
2693
2694 StringRef Name;
2695 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002696 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002697 return TokError("unexpected token in '.loc' directive");
2698
2699 if (Name == "basic_block")
2700 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2701 else if (Name == "prologue_end")
2702 Flags |= DWARF2_FLAG_PROLOGUE_END;
2703 else if (Name == "epilogue_begin")
2704 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2705 else if (Name == "is_stmt") {
2706 Loc = getTok().getLoc();
2707 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002708 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002709 return true;
2710 // The expression must be the constant 0 or 1.
2711 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2712 int Value = MCE->getValue();
2713 if (Value == 0)
2714 Flags &= ~DWARF2_FLAG_IS_STMT;
2715 else if (Value == 1)
2716 Flags |= DWARF2_FLAG_IS_STMT;
2717 else
2718 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002719 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002720 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2721 }
Craig Topperf15655b2013-04-22 04:22:40 +00002722 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002723 Loc = getTok().getLoc();
2724 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002725 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002726 return true;
2727 // The expression must be a constant greater or equal to 0.
2728 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2729 int Value = MCE->getValue();
2730 if (Value < 0)
2731 return Error(Loc, "isa number less than zero");
2732 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002733 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002734 return Error(Loc, "isa number not a constant value");
2735 }
Craig Topperf15655b2013-04-22 04:22:40 +00002736 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002737 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002738 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002739 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002740 return Error(Loc, "unknown sub-directive in '.loc' directive");
2741 }
2742
2743 if (getLexer().is(AsmToken::EndOfStatement))
2744 break;
2745 }
2746 }
2747
2748 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2749 Isa, Discriminator, StringRef());
2750
2751 return false;
2752}
2753
Jim Grosbach4b905842013-09-20 23:08:21 +00002754/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002755/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002756bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002757 return TokError("unsupported directive '.stabs'");
2758}
2759
Jim Grosbach4b905842013-09-20 23:08:21 +00002760/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002761/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002762bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002763 StringRef Name;
2764 bool EH = false;
2765 bool Debug = false;
2766
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002767 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002768 return TokError("Expected an identifier");
2769
2770 if (Name == ".eh_frame")
2771 EH = true;
2772 else if (Name == ".debug_frame")
2773 Debug = true;
2774
2775 if (getLexer().is(AsmToken::Comma)) {
2776 Lex();
2777
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002778 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002779 return TokError("Expected an identifier");
2780
2781 if (Name == ".eh_frame")
2782 EH = true;
2783 else if (Name == ".debug_frame")
2784 Debug = true;
2785 }
2786
2787 getStreamer().EmitCFISections(EH, Debug);
2788 return false;
2789}
2790
Jim Grosbach4b905842013-09-20 23:08:21 +00002791/// parseDirectiveCFIStartProc
Eli Bendersky17233942013-01-15 22:59:42 +00002792/// ::= .cfi_startproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002793bool AsmParser::parseDirectiveCFIStartProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002794 getStreamer().EmitCFIStartProc();
2795 return false;
2796}
2797
Jim Grosbach4b905842013-09-20 23:08:21 +00002798/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002799/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002800bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002801 getStreamer().EmitCFIEndProc();
2802 return false;
2803}
2804
Jim Grosbach4b905842013-09-20 23:08:21 +00002805/// \brief parse register name or number.
2806bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002807 SMLoc DirectiveLoc) {
2808 unsigned RegNo;
2809
2810 if (getLexer().isNot(AsmToken::Integer)) {
2811 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2812 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002813 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002814 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002815 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002816
2817 return false;
2818}
2819
Jim Grosbach4b905842013-09-20 23:08:21 +00002820/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002821/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002822bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002823 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002824 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002825 return true;
2826
2827 if (getLexer().isNot(AsmToken::Comma))
2828 return TokError("unexpected token in directive");
2829 Lex();
2830
2831 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002832 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002833 return true;
2834
2835 getStreamer().EmitCFIDefCfa(Register, Offset);
2836 return false;
2837}
2838
Jim Grosbach4b905842013-09-20 23:08:21 +00002839/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002840/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002841bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002842 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002843 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002844 return true;
2845
2846 getStreamer().EmitCFIDefCfaOffset(Offset);
2847 return false;
2848}
2849
Jim Grosbach4b905842013-09-20 23:08:21 +00002850/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002851/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00002852bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002853 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002854 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002855 return true;
2856
2857 if (getLexer().isNot(AsmToken::Comma))
2858 return TokError("unexpected token in directive");
2859 Lex();
2860
2861 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002862 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002863 return true;
2864
2865 getStreamer().EmitCFIRegister(Register1, Register2);
2866 return false;
2867}
2868
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00002869/// parseDirectiveCFIWindowSave
2870/// ::= .cfi_window_save
2871bool AsmParser::parseDirectiveCFIWindowSave() {
2872 getStreamer().EmitCFIWindowSave();
2873 return false;
2874}
2875
Jim Grosbach4b905842013-09-20 23:08:21 +00002876/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002877/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00002878bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002879 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002880 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00002881 return true;
2882
2883 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2884 return false;
2885}
2886
Jim Grosbach4b905842013-09-20 23:08:21 +00002887/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002888/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00002889bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002890 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002891 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002892 return true;
2893
2894 getStreamer().EmitCFIDefCfaRegister(Register);
2895 return false;
2896}
2897
Jim Grosbach4b905842013-09-20 23:08:21 +00002898/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002899/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002900bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002901 int64_t Register = 0;
2902 int64_t Offset = 0;
2903
Jim Grosbach4b905842013-09-20 23:08:21 +00002904 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002905 return true;
2906
2907 if (getLexer().isNot(AsmToken::Comma))
2908 return TokError("unexpected token in directive");
2909 Lex();
2910
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002911 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002912 return true;
2913
2914 getStreamer().EmitCFIOffset(Register, Offset);
2915 return false;
2916}
2917
Jim Grosbach4b905842013-09-20 23:08:21 +00002918/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002919/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002920bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002921 int64_t Register = 0;
2922
Jim Grosbach4b905842013-09-20 23:08:21 +00002923 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002924 return true;
2925
2926 if (getLexer().isNot(AsmToken::Comma))
2927 return TokError("unexpected token in directive");
2928 Lex();
2929
2930 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002931 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002932 return true;
2933
2934 getStreamer().EmitCFIRelOffset(Register, Offset);
2935 return false;
2936}
2937
2938static bool isValidEncoding(int64_t Encoding) {
2939 if (Encoding & ~0xff)
2940 return false;
2941
2942 if (Encoding == dwarf::DW_EH_PE_omit)
2943 return true;
2944
2945 const unsigned Format = Encoding & 0xf;
2946 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2947 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2948 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2949 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2950 return false;
2951
2952 const unsigned Application = Encoding & 0x70;
2953 if (Application != dwarf::DW_EH_PE_absptr &&
2954 Application != dwarf::DW_EH_PE_pcrel)
2955 return false;
2956
2957 return true;
2958}
2959
Jim Grosbach4b905842013-09-20 23:08:21 +00002960/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00002961/// IsPersonality true for cfi_personality, false for cfi_lsda
2962/// ::= .cfi_personality encoding, [symbol_name]
2963/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00002964bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00002965 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002966 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00002967 return true;
2968 if (Encoding == dwarf::DW_EH_PE_omit)
2969 return false;
2970
2971 if (!isValidEncoding(Encoding))
2972 return TokError("unsupported encoding.");
2973
2974 if (getLexer().isNot(AsmToken::Comma))
2975 return TokError("unexpected token in directive");
2976 Lex();
2977
2978 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002979 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002980 return TokError("expected identifier in directive");
2981
2982 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2983
2984 if (IsPersonality)
2985 getStreamer().EmitCFIPersonality(Sym, Encoding);
2986 else
2987 getStreamer().EmitCFILsda(Sym, Encoding);
2988 return false;
2989}
2990
Jim Grosbach4b905842013-09-20 23:08:21 +00002991/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00002992/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00002993bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00002994 getStreamer().EmitCFIRememberState();
2995 return false;
2996}
2997
Jim Grosbach4b905842013-09-20 23:08:21 +00002998/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00002999/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003000bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003001 getStreamer().EmitCFIRestoreState();
3002 return false;
3003}
3004
Jim Grosbach4b905842013-09-20 23:08:21 +00003005/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003006/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003007bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003008 int64_t Register = 0;
3009
Jim Grosbach4b905842013-09-20 23:08:21 +00003010 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003011 return true;
3012
3013 getStreamer().EmitCFISameValue(Register);
3014 return false;
3015}
3016
Jim Grosbach4b905842013-09-20 23:08:21 +00003017/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003018/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003019bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003020 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003021 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003022 return true;
3023
3024 getStreamer().EmitCFIRestore(Register);
3025 return false;
3026}
3027
Jim Grosbach4b905842013-09-20 23:08:21 +00003028/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003029/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003030bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003031 std::string Values;
3032 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003033 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003034 return true;
3035
3036 Values.push_back((uint8_t)CurrValue);
3037
3038 while (getLexer().is(AsmToken::Comma)) {
3039 Lex();
3040
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003041 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003042 return true;
3043
3044 Values.push_back((uint8_t)CurrValue);
3045 }
3046
3047 getStreamer().EmitCFIEscape(Values);
3048 return false;
3049}
3050
Jim Grosbach4b905842013-09-20 23:08:21 +00003051/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003052/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003053bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003054 if (getLexer().isNot(AsmToken::EndOfStatement))
3055 return Error(getLexer().getLoc(),
3056 "unexpected token in '.cfi_signal_frame'");
3057
3058 getStreamer().EmitCFISignalFrame();
3059 return false;
3060}
3061
Jim Grosbach4b905842013-09-20 23:08:21 +00003062/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003063/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003064bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003065 int64_t Register = 0;
3066
Jim Grosbach4b905842013-09-20 23:08:21 +00003067 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003068 return true;
3069
3070 getStreamer().EmitCFIUndefined(Register);
3071 return false;
3072}
3073
Jim Grosbach4b905842013-09-20 23:08:21 +00003074/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003075/// ::= .macros_on
3076/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003077bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003078 if (getLexer().isNot(AsmToken::EndOfStatement))
3079 return Error(getLexer().getLoc(),
3080 "unexpected token in '" + Directive + "' directive");
3081
Jim Grosbach4b905842013-09-20 23:08:21 +00003082 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003083 return false;
3084}
3085
Jim Grosbach4b905842013-09-20 23:08:21 +00003086/// parseDirectiveMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003087/// ::= .macro name [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003088bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003089 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 '.macro' directive");
3092
3093 MCAsmMacroParameters Parameters;
3094 // Argument delimiter is initially unknown. It will be set by
Jim Grosbach4b905842013-09-20 23:08:21 +00003095 // parseMacroArgument()
Eli Bendersky17233942013-01-15 22:59:42 +00003096 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
3097 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3098 for (;;) {
3099 MCAsmMacroParameter Parameter;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003100 if (parseIdentifier(Parameter.first))
Eli Bendersky17233942013-01-15 22:59:42 +00003101 return TokError("expected identifier in '.macro' directive");
3102
3103 if (getLexer().is(AsmToken::Equal)) {
3104 Lex();
Jim Grosbach4b905842013-09-20 23:08:21 +00003105 if (parseMacroArgument(Parameter.second, ArgumentDelimiter))
Eli Bendersky17233942013-01-15 22:59:42 +00003106 return true;
3107 }
3108
3109 Parameters.push_back(Parameter);
3110
3111 if (getLexer().is(AsmToken::Comma))
3112 Lex();
3113 else if (getLexer().is(AsmToken::EndOfStatement))
3114 break;
3115 }
3116 }
3117
3118 // Eat the end of statement.
3119 Lex();
3120
3121 AsmToken EndToken, StartToken = getTok();
3122
3123 // Lex the macro definition.
3124 for (;;) {
3125 // Check whether we have reached the end of the file.
3126 if (getLexer().is(AsmToken::Eof))
3127 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3128
3129 // Otherwise, check whether we have reach the .endmacro.
3130 if (getLexer().is(AsmToken::Identifier) &&
3131 (getTok().getIdentifier() == ".endm" ||
3132 getTok().getIdentifier() == ".endmacro")) {
3133 EndToken = getTok();
3134 Lex();
3135 if (getLexer().isNot(AsmToken::EndOfStatement))
3136 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3137 "' directive");
3138 break;
3139 }
3140
3141 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003142 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003143 }
3144
Jim Grosbach4b905842013-09-20 23:08:21 +00003145 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003146 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3147 }
3148
3149 const char *BodyStart = StartToken.getLoc().getPointer();
3150 const char *BodyEnd = EndToken.getLoc().getPointer();
3151 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003152 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3153 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003154 return false;
3155}
3156
Jim Grosbach4b905842013-09-20 23:08:21 +00003157/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003158///
3159/// With the support added for named parameters there may be code out there that
3160/// is transitioning from positional parameters. In versions of gas that did
3161/// not support named parameters they would be ignored on the macro defintion.
3162/// But to support both styles of parameters this is not possible so if a macro
3163/// defintion has named parameters but does not use them and has what appears
3164/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3165/// warning that the positional parameter found in body which have no effect.
3166/// Hoping the developer will either remove the named parameters from the macro
3167/// definiton so the positional parameters get used if that was what was
3168/// intended or change the macro to use the named parameters. It is possible
3169/// this warning will trigger when the none of the named parameters are used
3170/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003171void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003172 StringRef Body,
3173 MCAsmMacroParameters Parameters) {
3174 // If this macro is not defined with named parameters the warning we are
3175 // checking for here doesn't apply.
3176 unsigned NParameters = Parameters.size();
3177 if (NParameters == 0)
3178 return;
3179
3180 bool NamedParametersFound = false;
3181 bool PositionalParametersFound = false;
3182
3183 // Look at the body of the macro for use of both the named parameters and what
3184 // are likely to be positional parameters. This is what expandMacro() is
3185 // doing when it finds the parameters in the body.
3186 while (!Body.empty()) {
3187 // Scan for the next possible parameter.
3188 std::size_t End = Body.size(), Pos = 0;
3189 for (; Pos != End; ++Pos) {
3190 // Check for a substitution or escape.
3191 // This macro is defined with parameters, look for \foo, \bar, etc.
3192 if (Body[Pos] == '\\' && Pos + 1 != End)
3193 break;
3194
3195 // This macro should have parameters, but look for $0, $1, ..., $n too.
3196 if (Body[Pos] != '$' || Pos + 1 == End)
3197 continue;
3198 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003199 if (Next == '$' || Next == 'n' ||
3200 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003201 break;
3202 }
3203
3204 // Check if we reached the end.
3205 if (Pos == End)
3206 break;
3207
3208 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003209 switch (Body[Pos + 1]) {
3210 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003211 case '$':
3212 break;
3213
Jim Grosbach4b905842013-09-20 23:08:21 +00003214 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003215 case 'n':
3216 PositionalParametersFound = true;
3217 break;
3218
Jim Grosbach4b905842013-09-20 23:08:21 +00003219 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003220 default: {
3221 PositionalParametersFound = true;
3222 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003223 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003224 }
3225 Pos += 2;
3226 } else {
3227 unsigned I = Pos + 1;
3228 while (isIdentifierChar(Body[I]) && I + 1 != End)
3229 ++I;
3230
Jim Grosbach4b905842013-09-20 23:08:21 +00003231 const char *Begin = Body.data() + Pos + 1;
3232 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003233 unsigned Index = 0;
3234 for (; Index < NParameters; ++Index)
3235 if (Parameters[Index].first == Argument)
3236 break;
3237
3238 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003239 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3240 Pos += 3;
3241 else {
3242 Pos = I;
3243 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003244 } else {
3245 NamedParametersFound = true;
3246 Pos += 1 + Argument.size();
3247 }
3248 }
3249 // Update the scan point.
3250 Body = Body.substr(Pos);
3251 }
3252
3253 if (!NamedParametersFound && PositionalParametersFound)
3254 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3255 "used in macro body, possible positional parameter "
3256 "found in body which will have no effect");
3257}
3258
Jim Grosbach4b905842013-09-20 23:08:21 +00003259/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003260/// ::= .endm
3261/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003262bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003263 if (getLexer().isNot(AsmToken::EndOfStatement))
3264 return TokError("unexpected token in '" + Directive + "' directive");
3265
3266 // If we are inside a macro instantiation, terminate the current
3267 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003268 if (isInsideMacroInstantiation()) {
3269 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003270 return false;
3271 }
3272
3273 // Otherwise, this .endmacro is a stray entry in the file; well formed
3274 // .endmacro directives are handled during the macro definition parsing.
3275 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003276 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003277}
3278
Jim Grosbach4b905842013-09-20 23:08:21 +00003279/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003280/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003281bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003282 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003283 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003284 return TokError("expected identifier in '.purgem' directive");
3285
3286 if (getLexer().isNot(AsmToken::EndOfStatement))
3287 return TokError("unexpected token in '.purgem' directive");
3288
Jim Grosbach4b905842013-09-20 23:08:21 +00003289 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003290 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3291
Jim Grosbach4b905842013-09-20 23:08:21 +00003292 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003293 return false;
3294}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003295
Jim Grosbach4b905842013-09-20 23:08:21 +00003296/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003297/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003298bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003299 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003300
3301 // Expect a single argument: an expression that evaluates to a constant
3302 // in the inclusive range 0-30.
3303 SMLoc ExprLoc = getLexer().getLoc();
3304 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003305 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003306 return true;
3307 else if (getLexer().isNot(AsmToken::EndOfStatement))
3308 return TokError("unexpected token after expression in"
3309 " '.bundle_align_mode' directive");
3310 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3311 return Error(ExprLoc,
3312 "invalid bundle alignment size (expected between 0 and 30)");
3313
3314 Lex();
3315
3316 // Because of AlignSizePow2's verified range we can safely truncate it to
3317 // unsigned.
3318 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3319 return false;
3320}
3321
Jim Grosbach4b905842013-09-20 23:08:21 +00003322/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003323/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003324bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003325 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003326 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003327
Eli Bendersky802b6282013-01-07 21:51:08 +00003328 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3329 StringRef Option;
3330 SMLoc Loc = getTok().getLoc();
3331 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003332 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003333
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003334 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003335 return Error(Loc, kInvalidOptionError);
3336
3337 if (Option != "align_to_end")
3338 return Error(Loc, kInvalidOptionError);
3339 else if (getLexer().isNot(AsmToken::EndOfStatement))
3340 return Error(Loc,
3341 "unexpected token after '.bundle_lock' directive option");
3342 AlignToEnd = true;
3343 }
3344
Eli Benderskyf483ff92012-12-20 19:05:53 +00003345 Lex();
3346
Eli Bendersky802b6282013-01-07 21:51:08 +00003347 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003348 return false;
3349}
3350
Jim Grosbach4b905842013-09-20 23:08:21 +00003351/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003352/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003353bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003354 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003355
3356 if (getLexer().isNot(AsmToken::EndOfStatement))
3357 return TokError("unexpected token in '.bundle_unlock' directive");
3358 Lex();
3359
3360 getStreamer().EmitBundleUnlock();
3361 return false;
3362}
3363
Jim Grosbach4b905842013-09-20 23:08:21 +00003364/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003365/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003366bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003367 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003368
3369 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003370 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003371 return true;
3372
3373 int64_t FillExpr = 0;
3374 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3375 if (getLexer().isNot(AsmToken::Comma))
3376 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3377 Lex();
3378
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003379 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003380 return true;
3381
3382 if (getLexer().isNot(AsmToken::EndOfStatement))
3383 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3384 }
3385
3386 Lex();
3387
3388 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003389 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3390 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003391
3392 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003393 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003394
3395 return false;
3396}
3397
Jim Grosbach4b905842013-09-20 23:08:21 +00003398/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003399/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003400bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003401 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003402 const MCExpr *Value;
3403
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003404 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003405 return true;
3406
3407 if (getLexer().isNot(AsmToken::EndOfStatement))
3408 return TokError("unexpected token in directive");
3409
3410 if (Signed)
3411 getStreamer().EmitSLEB128Value(Value);
3412 else
3413 getStreamer().EmitULEB128Value(Value);
3414
3415 return false;
3416}
3417
Jim Grosbach4b905842013-09-20 23:08:21 +00003418/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003419/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003420bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003421 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003422 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003423 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003424 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003425
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003426 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003427 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003428
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003429 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003430
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003431 // Assembler local symbols don't make any sense here. Complain loudly.
3432 if (Sym->isTemporary())
3433 return Error(Loc, "non-local symbol required in directive");
3434
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003435 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3436 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003437
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003438 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003439 break;
3440
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003441 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003442 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003443 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003444 }
3445 }
3446
Sean Callanan686ed8d2010-01-19 20:22:31 +00003447 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003448 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003449}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003450
Jim Grosbach4b905842013-09-20 23:08:21 +00003451/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003452/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003453bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003454 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003455
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003456 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003457 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003458 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003459 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003460
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003461 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003462 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003463
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003464 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003465 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003466 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003467
3468 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003469 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003470 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003471 return true;
3472
3473 int64_t Pow2Alignment = 0;
3474 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003475 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003476 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003477 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003478 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003479 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003480
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003481 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3482 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003483 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3484
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003485 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003486 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3487 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003488 if (!isPowerOf2_64(Pow2Alignment))
3489 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3490 Pow2Alignment = Log2_64(Pow2Alignment);
3491 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003492 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003493
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003494 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003495 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003496
Sean Callanan686ed8d2010-01-19 20:22:31 +00003497 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003498
Chris Lattner28ad7542009-07-09 17:25:12 +00003499 // NOTE: a size of zero for a .comm should create a undefined symbol
3500 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003501 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003502 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003503 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003504
Eric Christopherbc818852010-05-14 01:38:54 +00003505 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003506 // may internally end up wanting an alignment in bytes.
3507 // FIXME: Diagnose overflow.
3508 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003509 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003510 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003511
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003512 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003513 return Error(IDLoc, "invalid symbol redefinition");
3514
Chris Lattner28ad7542009-07-09 17:25:12 +00003515 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003516 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003517 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003518 return false;
3519 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003520
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003521 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003522 return false;
3523}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003524
Jim Grosbach4b905842013-09-20 23:08:21 +00003525/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003526/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003527bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003528 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003529 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003530
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003531 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003532 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003533 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003534
Sean Callanan686ed8d2010-01-19 20:22:31 +00003535 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003536
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003537 if (Str.empty())
3538 Error(Loc, ".abort detected. Assembly stopping.");
3539 else
3540 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003541 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003542
3543 return false;
3544}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003545
Jim Grosbach4b905842013-09-20 23:08:21 +00003546/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003547/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003548bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003549 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003550 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003551
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003552 // Allow the strings to have escaped octal character sequence.
3553 std::string Filename;
3554 if (parseEscapedString(Filename))
3555 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003556 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003557 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003558
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003559 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003560 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003561
Chris Lattner693fbb82009-07-16 06:14:39 +00003562 // Attempt to switch the lexer to the included file before consuming the end
3563 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003564 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003565 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003566 return true;
3567 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003568
3569 return false;
3570}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003571
Jim Grosbach4b905842013-09-20 23:08:21 +00003572/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003573/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003574bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003575 if (getLexer().isNot(AsmToken::String))
3576 return TokError("expected string in '.incbin' directive");
3577
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003578 // Allow the strings to have escaped octal character sequence.
3579 std::string Filename;
3580 if (parseEscapedString(Filename))
3581 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003582 SMLoc IncbinLoc = getLexer().getLoc();
3583 Lex();
3584
3585 if (getLexer().isNot(AsmToken::EndOfStatement))
3586 return TokError("unexpected token in '.incbin' directive");
3587
Kevin Enderby109f25c2011-12-14 21:47:48 +00003588 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003589 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003590 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3591 return true;
3592 }
3593
3594 return false;
3595}
3596
Jim Grosbach4b905842013-09-20 23:08:21 +00003597/// parseDirectiveIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003598/// ::= .if expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003599bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003600 TheCondStack.push_back(TheCondState);
3601 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003602 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003603 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003604 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003605 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003606 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003607 return true;
3608
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003609 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003610 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003611
Sean Callanan686ed8d2010-01-19 20:22:31 +00003612 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003613
3614 TheCondState.CondMet = ExprValue;
3615 TheCondState.Ignore = !TheCondState.CondMet;
3616 }
3617
3618 return false;
3619}
3620
Jim Grosbach4b905842013-09-20 23:08:21 +00003621/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003622/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003623bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003624 TheCondStack.push_back(TheCondState);
3625 TheCondState.TheCond = AsmCond::IfCond;
3626
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003627 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003628 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003629 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003630 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003631
3632 if (getLexer().isNot(AsmToken::EndOfStatement))
3633 return TokError("unexpected token in '.ifb' directive");
3634
3635 Lex();
3636
3637 TheCondState.CondMet = ExpectBlank == Str.empty();
3638 TheCondState.Ignore = !TheCondState.CondMet;
3639 }
3640
3641 return false;
3642}
3643
Jim Grosbach4b905842013-09-20 23:08:21 +00003644/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003645/// ::= .ifc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003646bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003647 TheCondStack.push_back(TheCondState);
3648 TheCondState.TheCond = AsmCond::IfCond;
3649
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003650 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003651 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003652 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003653 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003654
3655 if (getLexer().isNot(AsmToken::Comma))
3656 return TokError("unexpected token in '.ifc' directive");
3657
3658 Lex();
3659
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003660 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003661
3662 if (getLexer().isNot(AsmToken::EndOfStatement))
3663 return TokError("unexpected token in '.ifc' directive");
3664
3665 Lex();
3666
3667 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
3668 TheCondState.Ignore = !TheCondState.CondMet;
3669 }
3670
3671 return false;
3672}
3673
Jim Grosbach4b905842013-09-20 23:08:21 +00003674/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003675/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003676bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003677 StringRef Name;
3678 TheCondStack.push_back(TheCondState);
3679 TheCondState.TheCond = AsmCond::IfCond;
3680
3681 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003682 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003683 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003684 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003685 return TokError("expected identifier after '.ifdef'");
3686
3687 Lex();
3688
3689 MCSymbol *Sym = getContext().LookupSymbol(Name);
3690
3691 if (expect_defined)
3692 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
3693 else
3694 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
3695 TheCondState.Ignore = !TheCondState.CondMet;
3696 }
3697
3698 return false;
3699}
3700
Jim Grosbach4b905842013-09-20 23:08:21 +00003701/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003702/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003703bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003704 if (TheCondState.TheCond != AsmCond::IfCond &&
3705 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003706 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3707 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003708 TheCondState.TheCond = AsmCond::ElseIfCond;
3709
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003710 bool LastIgnoreState = false;
3711 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00003712 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003713 if (LastIgnoreState || TheCondState.CondMet) {
3714 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003715 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00003716 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003717 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003718 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003719 return true;
3720
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003721 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003722 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003723
Sean Callanan686ed8d2010-01-19 20:22:31 +00003724 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003725 TheCondState.CondMet = ExprValue;
3726 TheCondState.Ignore = !TheCondState.CondMet;
3727 }
3728
3729 return false;
3730}
3731
Jim Grosbach4b905842013-09-20 23:08:21 +00003732/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003733/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00003734bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003735 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003736 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003737
Sean Callanan686ed8d2010-01-19 20:22:31 +00003738 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003739
3740 if (TheCondState.TheCond != AsmCond::IfCond &&
3741 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003742 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
3743 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003744 TheCondState.TheCond = AsmCond::ElseCond;
3745 bool LastIgnoreState = false;
3746 if (!TheCondStack.empty())
3747 LastIgnoreState = TheCondStack.back().Ignore;
3748 if (LastIgnoreState || TheCondState.CondMet)
3749 TheCondState.Ignore = true;
3750 else
3751 TheCondState.Ignore = false;
3752
3753 return false;
3754}
3755
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003756/// parseDirectiveEnd
3757/// ::= .end
3758bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
3759 if (getLexer().isNot(AsmToken::EndOfStatement))
3760 return TokError("unexpected token in '.end' directive");
3761
3762 Lex();
3763
3764 while (Lexer.isNot(AsmToken::Eof))
3765 Lex();
3766
3767 return false;
3768}
3769
Jim Grosbach4b905842013-09-20 23:08:21 +00003770/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003771/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00003772bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003773 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003774 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003775
Sean Callanan686ed8d2010-01-19 20:22:31 +00003776 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003777
Jim Grosbach4b905842013-09-20 23:08:21 +00003778 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003779 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
3780 ".else");
3781 if (!TheCondStack.empty()) {
3782 TheCondState = TheCondStack.back();
3783 TheCondStack.pop_back();
3784 }
3785
3786 return false;
3787}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00003788
Eli Bendersky17233942013-01-15 22:59:42 +00003789void AsmParser::initializeDirectiveKindMap() {
3790 DirectiveKindMap[".set"] = DK_SET;
3791 DirectiveKindMap[".equ"] = DK_EQU;
3792 DirectiveKindMap[".equiv"] = DK_EQUIV;
3793 DirectiveKindMap[".ascii"] = DK_ASCII;
3794 DirectiveKindMap[".asciz"] = DK_ASCIZ;
3795 DirectiveKindMap[".string"] = DK_STRING;
3796 DirectiveKindMap[".byte"] = DK_BYTE;
3797 DirectiveKindMap[".short"] = DK_SHORT;
3798 DirectiveKindMap[".value"] = DK_VALUE;
3799 DirectiveKindMap[".2byte"] = DK_2BYTE;
3800 DirectiveKindMap[".long"] = DK_LONG;
3801 DirectiveKindMap[".int"] = DK_INT;
3802 DirectiveKindMap[".4byte"] = DK_4BYTE;
3803 DirectiveKindMap[".quad"] = DK_QUAD;
3804 DirectiveKindMap[".8byte"] = DK_8BYTE;
3805 DirectiveKindMap[".single"] = DK_SINGLE;
3806 DirectiveKindMap[".float"] = DK_FLOAT;
3807 DirectiveKindMap[".double"] = DK_DOUBLE;
3808 DirectiveKindMap[".align"] = DK_ALIGN;
3809 DirectiveKindMap[".align32"] = DK_ALIGN32;
3810 DirectiveKindMap[".balign"] = DK_BALIGN;
3811 DirectiveKindMap[".balignw"] = DK_BALIGNW;
3812 DirectiveKindMap[".balignl"] = DK_BALIGNL;
3813 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
3814 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
3815 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
3816 DirectiveKindMap[".org"] = DK_ORG;
3817 DirectiveKindMap[".fill"] = DK_FILL;
3818 DirectiveKindMap[".zero"] = DK_ZERO;
3819 DirectiveKindMap[".extern"] = DK_EXTERN;
3820 DirectiveKindMap[".globl"] = DK_GLOBL;
3821 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00003822 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
3823 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
3824 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
3825 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
3826 DirectiveKindMap[".reference"] = DK_REFERENCE;
3827 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
3828 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
3829 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
3830 DirectiveKindMap[".comm"] = DK_COMM;
3831 DirectiveKindMap[".common"] = DK_COMMON;
3832 DirectiveKindMap[".lcomm"] = DK_LCOMM;
3833 DirectiveKindMap[".abort"] = DK_ABORT;
3834 DirectiveKindMap[".include"] = DK_INCLUDE;
3835 DirectiveKindMap[".incbin"] = DK_INCBIN;
3836 DirectiveKindMap[".code16"] = DK_CODE16;
3837 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
3838 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00003839 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00003840 DirectiveKindMap[".irp"] = DK_IRP;
3841 DirectiveKindMap[".irpc"] = DK_IRPC;
3842 DirectiveKindMap[".endr"] = DK_ENDR;
3843 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
3844 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
3845 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
3846 DirectiveKindMap[".if"] = DK_IF;
3847 DirectiveKindMap[".ifb"] = DK_IFB;
3848 DirectiveKindMap[".ifnb"] = DK_IFNB;
3849 DirectiveKindMap[".ifc"] = DK_IFC;
3850 DirectiveKindMap[".ifnc"] = DK_IFNC;
3851 DirectiveKindMap[".ifdef"] = DK_IFDEF;
3852 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
3853 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
3854 DirectiveKindMap[".elseif"] = DK_ELSEIF;
3855 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003856 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00003857 DirectiveKindMap[".endif"] = DK_ENDIF;
3858 DirectiveKindMap[".skip"] = DK_SKIP;
3859 DirectiveKindMap[".space"] = DK_SPACE;
3860 DirectiveKindMap[".file"] = DK_FILE;
3861 DirectiveKindMap[".line"] = DK_LINE;
3862 DirectiveKindMap[".loc"] = DK_LOC;
3863 DirectiveKindMap[".stabs"] = DK_STABS;
3864 DirectiveKindMap[".sleb128"] = DK_SLEB128;
3865 DirectiveKindMap[".uleb128"] = DK_ULEB128;
3866 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
3867 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
3868 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
3869 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
3870 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
3871 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
3872 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
3873 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
3874 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
3875 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
3876 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
3877 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
3878 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
3879 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
3880 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
3881 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
3882 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
3883 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
3884 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003885 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00003886 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
3887 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
3888 DirectiveKindMap[".macro"] = DK_MACRO;
3889 DirectiveKindMap[".endm"] = DK_ENDM;
3890 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
3891 DirectiveKindMap[".purgem"] = DK_PURGEM;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00003892}
3893
Jim Grosbach4b905842013-09-20 23:08:21 +00003894MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003895 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003896
Rafael Espindola34b9c512012-06-03 23:57:14 +00003897 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003898 for (;;) {
3899 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00003900 if (getLexer().is(AsmToken::Eof)) {
3901 Error(DirectiveLoc, "no matching '.endr' in definition");
3902 return 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003903 }
3904
Rafael Espindola34b9c512012-06-03 23:57:14 +00003905 if (Lexer.is(AsmToken::Identifier) &&
3906 (getTok().getIdentifier() == ".rept")) {
3907 ++NestLevel;
3908 }
3909
3910 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00003911 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00003912 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003913 EndToken = getTok();
3914 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00003915 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3916 TokError("unexpected token in '.endr' directive");
3917 return 0;
3918 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003919 break;
3920 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00003921 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003922 }
3923
Rafael Espindola34b9c512012-06-03 23:57:14 +00003924 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003925 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003926 }
3927
3928 const char *BodyStart = StartToken.getLoc().getPointer();
3929 const char *BodyEnd = EndToken.getLoc().getPointer();
3930 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3931
Rafael Espindola34b9c512012-06-03 23:57:14 +00003932 // We Are Anonymous.
3933 StringRef Name;
Eli Bendersky38274122013-01-14 23:22:36 +00003934 MCAsmMacroParameters Parameters;
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00003935 MacroLikeBodies.push_back(MCAsmMacro(Name, Body, Parameters));
3936 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00003937}
3938
Jim Grosbach4b905842013-09-20 23:08:21 +00003939void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00003940 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003941 OS << ".endr\n";
3942
3943 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00003944 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003945
Rafael Espindola34b9c512012-06-03 23:57:14 +00003946 // Create the macro instantiation object and add to the current macro
3947 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00003948 MacroInstantiation *MI = new MacroInstantiation(
3949 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola34b9c512012-06-03 23:57:14 +00003950 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003951
Rafael Espindola34b9c512012-06-03 23:57:14 +00003952 // Jump to the macro instantiation and prime the lexer.
3953 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3954 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3955 Lex();
3956}
3957
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00003958/// parseDirectiveRept
3959/// ::= .rep | .rept count
3960bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00003961 const MCExpr *CountExpr;
3962 SMLoc CountLoc = getTok().getLoc();
3963 if (parseExpression(CountExpr))
3964 return true;
3965
Rafael Espindola34b9c512012-06-03 23:57:14 +00003966 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00003967 if (!CountExpr->EvaluateAsAbsolute(Count)) {
3968 eatToEndOfStatement();
3969 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
3970 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00003971
3972 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00003973 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00003974
3975 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00003976 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00003977
3978 // Eat the end of statement.
3979 Lex();
3980
3981 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00003982 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00003983 if (!M)
3984 return true;
3985
3986 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3987 // to hold the macro body with substitutions.
3988 SmallString<256> Buf;
Eli Bendersky38274122013-01-14 23:22:36 +00003989 MCAsmMacroParameters Parameters;
3990 MCAsmMacroArguments A;
Rafael Espindola34b9c512012-06-03 23:57:14 +00003991 raw_svector_ostream OS(Buf);
3992 while (Count--) {
3993 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3994 return true;
3995 }
Jim Grosbach4b905842013-09-20 23:08:21 +00003996 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003997
3998 return false;
3999}
4000
Jim Grosbach4b905842013-09-20 23:08:21 +00004001/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004002/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004003bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004004 MCAsmMacroParameters Parameters;
4005 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004006
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004007 if (parseIdentifier(Parameter.first))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004008 return TokError("expected identifier in '.irp' directive");
4009
4010 Parameters.push_back(Parameter);
4011
4012 if (Lexer.isNot(AsmToken::Comma))
4013 return TokError("expected comma in '.irp' directive");
4014
4015 Lex();
4016
Eli Bendersky38274122013-01-14 23:22:36 +00004017 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004018 if (parseMacroArguments(0, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004019 return true;
4020
4021 // Eat the end of statement.
4022 Lex();
4023
4024 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004025 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004026 if (!M)
4027 return true;
4028
4029 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4030 // to hold the macro body with substitutions.
4031 SmallString<256> Buf;
4032 raw_svector_ostream OS(Buf);
4033
Eli Bendersky38274122013-01-14 23:22:36 +00004034 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
4035 MCAsmMacroArguments Args;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004036 Args.push_back(*i);
4037
4038 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
4039 return true;
4040 }
4041
Jim Grosbach4b905842013-09-20 23:08:21 +00004042 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004043
4044 return false;
4045}
4046
Jim Grosbach4b905842013-09-20 23:08:21 +00004047/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004048/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004049bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004050 MCAsmMacroParameters Parameters;
4051 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004052
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004053 if (parseIdentifier(Parameter.first))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004054 return TokError("expected identifier in '.irpc' directive");
4055
4056 Parameters.push_back(Parameter);
4057
4058 if (Lexer.isNot(AsmToken::Comma))
4059 return TokError("expected comma in '.irpc' directive");
4060
4061 Lex();
4062
Eli Bendersky38274122013-01-14 23:22:36 +00004063 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004064 if (parseMacroArguments(0, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004065 return true;
4066
4067 if (A.size() != 1 || A.front().size() != 1)
4068 return TokError("unexpected token in '.irpc' directive");
4069
4070 // Eat the end of statement.
4071 Lex();
4072
4073 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004074 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004075 if (!M)
4076 return true;
4077
4078 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4079 // to hold the macro body with substitutions.
4080 SmallString<256> Buf;
4081 raw_svector_ostream OS(Buf);
4082
4083 StringRef Values = A.front().front().getString();
4084 std::size_t I, End = Values.size();
4085 for (I = 0; I < End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004086 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004087 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004088
Eli Bendersky38274122013-01-14 23:22:36 +00004089 MCAsmMacroArguments Args;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004090 Args.push_back(Arg);
4091
4092 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
4093 return true;
4094 }
4095
Jim Grosbach4b905842013-09-20 23:08:21 +00004096 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004097
4098 return false;
4099}
4100
Jim Grosbach4b905842013-09-20 23:08:21 +00004101bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004102 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004103 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004104
4105 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004106 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004107 assert(getLexer().is(AsmToken::EndOfStatement));
4108
Jim Grosbach4b905842013-09-20 23:08:21 +00004109 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004110 return false;
4111}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004112
Jim Grosbach4b905842013-09-20 23:08:21 +00004113bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004114 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004115 const MCExpr *Value;
4116 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004117 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004118 return true;
4119 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4120 if (!MCE)
4121 return Error(ExprLoc, "unexpected expression in _emit");
4122 uint64_t IntValue = MCE->getValue();
4123 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4124 return Error(ExprLoc, "literal value out of range for directive");
4125
Chad Rosierc7f552c2013-02-12 21:33:51 +00004126 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4127 return false;
4128}
4129
Jim Grosbach4b905842013-09-20 23:08:21 +00004130bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004131 const MCExpr *Value;
4132 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004133 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004134 return true;
4135 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4136 if (!MCE)
4137 return Error(ExprLoc, "unexpected expression in align");
4138 uint64_t IntValue = MCE->getValue();
4139 if (!isPowerOf2_64(IntValue))
4140 return Error(ExprLoc, "literal value not a power of two greater then zero");
4141
Jim Grosbach4b905842013-09-20 23:08:21 +00004142 Info.AsmRewrites->push_back(
4143 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004144 return false;
4145}
4146
Chad Rosierf43fcf52013-02-13 21:27:17 +00004147// We are comparing pointers, but the pointers are relative to a single string.
4148// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004149static int rewritesSort(const AsmRewrite *AsmRewriteA,
4150 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004151 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4152 return -1;
4153 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4154 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004155
Chad Rosierfce4fab2013-04-08 17:43:47 +00004156 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4157 // rewrite to the same location. Make sure the SizeDirective rewrite is
4158 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4159 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004160 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4161 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004162 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004163
Jim Grosbach4b905842013-09-20 23:08:21 +00004164 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4165 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004166 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004167 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004168}
4169
Jim Grosbach4b905842013-09-20 23:08:21 +00004170bool AsmParser::parseMSInlineAsm(
4171 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4172 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4173 SmallVectorImpl<std::string> &Constraints,
4174 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4175 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004176 SmallVector<void *, 4> InputDecls;
4177 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004178 SmallVector<bool, 4> InputDeclsAddressOf;
4179 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004180 SmallVector<std::string, 4> InputConstraints;
4181 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004182 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004183
Benjamin Kramer1a136112013-02-15 20:37:21 +00004184 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004185
4186 // Prime the lexer.
4187 Lex();
4188
4189 // While we have input, parse each statement.
4190 unsigned InputIdx = 0;
4191 unsigned OutputIdx = 0;
4192 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004193 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004194 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004195 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004196
Chad Rosier149e8e02012-12-12 22:45:52 +00004197 if (Info.ParseError)
4198 return true;
4199
Benjamin Kramer1a136112013-02-15 20:37:21 +00004200 if (Info.Opcode == ~0U)
4201 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004202
Benjamin Kramer1a136112013-02-15 20:37:21 +00004203 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004204
Benjamin Kramer1a136112013-02-15 20:37:21 +00004205 // Build the list of clobbers, outputs and inputs.
4206 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
4207 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004208
Benjamin Kramer1a136112013-02-15 20:37:21 +00004209 // Immediate.
Chad Rosierf3c04f62013-03-19 21:58:18 +00004210 if (Operand->isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004211 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004212
Benjamin Kramer1a136112013-02-15 20:37:21 +00004213 // Register operand.
4214 if (Operand->isReg() && !Operand->needAddressOf()) {
4215 unsigned NumDefs = Desc.getNumDefs();
4216 // Clobber.
4217 if (NumDefs && Operand->getMCOperandNum() < NumDefs)
4218 ClobberRegs.push_back(Operand->getReg());
4219 continue;
4220 }
4221
4222 // Expr/Input or Output.
Chad Rosiere81309b2013-04-09 17:53:49 +00004223 StringRef SymName = Operand->getSymName();
4224 if (SymName.empty())
4225 continue;
4226
Chad Rosierdba3fe52013-04-22 22:12:12 +00004227 void *OpDecl = Operand->getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004228 if (!OpDecl)
4229 continue;
4230
4231 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004232 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004233 if (isOutput) {
4234 ++InputIdx;
4235 OutputDecls.push_back(OpDecl);
4236 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
4237 OutputConstraints.push_back('=' + Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004238 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004239 } else {
4240 InputDecls.push_back(OpDecl);
4241 InputDeclsAddressOf.push_back(Operand->needAddressOf());
4242 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004243 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004244 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004245 }
Reid Kleckneree088972013-12-10 18:27:32 +00004246
4247 // Consider implicit defs to be clobbers. Think of cpuid and push.
4248 const uint16_t *ImpDefs = Desc.getImplicitDefs();
4249 for (unsigned I = 0, E = Desc.getNumImplicitDefs(); I != E; ++I)
4250 ClobberRegs.push_back(ImpDefs[I]);
Chad Rosier8bce6642012-10-18 15:49:34 +00004251 }
4252
4253 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004254 NumOutputs = OutputDecls.size();
4255 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004256
4257 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004258 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4259 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4260 ClobberRegs.end());
4261 Clobbers.assign(ClobberRegs.size(), std::string());
4262 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4263 raw_string_ostream OS(Clobbers[I]);
4264 IP->printRegName(OS, ClobberRegs[I]);
4265 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004266
4267 // Merge the various outputs and inputs. Output are expected first.
4268 if (NumOutputs || NumInputs) {
4269 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004270 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004271 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004272 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004273 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004274 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004275 }
4276 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004277 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004278 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004279 }
4280 }
4281
4282 // Build the IR assembly string.
4283 std::string AsmStringIR;
4284 raw_string_ostream OS(AsmStringIR);
Chad Rosier17d37992013-03-19 21:12:14 +00004285 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4286 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
Jim Grosbach4b905842013-09-20 23:08:21 +00004287 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
Benjamin Kramer1a136112013-02-15 20:37:21 +00004288 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(),
4289 E = AsmStrRewrites.end();
4290 I != E; ++I) {
Chad Rosierff10ed12013-04-12 16:26:42 +00004291 AsmRewriteKind Kind = (*I).Kind;
4292 if (Kind == AOK_Delete)
4293 continue;
4294
Chad Rosier8bce6642012-10-18 15:49:34 +00004295 const char *Loc = (*I).Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004296 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004297
Chad Rosier120eefd2013-03-19 17:32:17 +00004298 // Emit everything up to the immediate/expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004299 unsigned Len = Loc - AsmStart;
Chad Rosier8fb83302013-04-11 21:49:30 +00004300 if (Len)
Chad Rosier17d37992013-03-19 21:12:14 +00004301 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004302
Chad Rosier37e755c2012-10-23 17:43:43 +00004303 // Skip the original expression.
4304 if (Kind == AOK_Skip) {
Chad Rosier17d37992013-03-19 21:12:14 +00004305 AsmStart = Loc + (*I).Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004306 continue;
4307 }
4308
Chad Rosierff10ed12013-04-12 16:26:42 +00004309 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004310 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004311 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004312 default:
4313 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004314 case AOK_Imm:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004315 OS << "$$" << (*I).Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004316 break;
4317 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004318 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004319 break;
4320 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004321 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004322 break;
4323 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004324 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004325 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004326 case AOK_SizeDirective:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004327 switch ((*I).Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004328 default: break;
4329 case 8: OS << "byte ptr "; break;
4330 case 16: OS << "word ptr "; break;
4331 case 32: OS << "dword ptr "; break;
4332 case 64: OS << "qword ptr "; break;
4333 case 80: OS << "xword ptr "; break;
4334 case 128: OS << "xmmword ptr "; break;
4335 case 256: OS << "ymmword ptr "; break;
4336 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004337 break;
4338 case AOK_Emit:
4339 OS << ".byte";
4340 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004341 case AOK_Align: {
4342 unsigned Val = (*I).Val;
4343 OS << ".align " << Val;
4344
4345 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004346 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004347 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4348 break;
4349 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004350 case AOK_DotOperator:
4351 OS << (*I).Val;
4352 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004353 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004354
Chad Rosier8bce6642012-10-18 15:49:34 +00004355 // Skip the original expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004356 AsmStart = Loc + (*I).Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004357 }
4358
4359 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004360 if (AsmStart != AsmEnd)
4361 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004362
4363 AsmString = OS.str();
4364 return false;
4365}
4366
Daniel Dunbar01e36072010-07-17 02:26:10 +00004367/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004368MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4369 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004370 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004371}