blob: c2fff3c5207e999363aeca0e8b37338758fff2e8 [file] [log] [blame]
Chris Lattner27aa7d22009-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 Dunbarb95a0792010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000016#include "llvm/ADT/StringMap.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000018#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000019#include "llvm/MC/MCContext.h"
Evan Cheng94b95502011-07-26 00:24:13 +000020#include "llvm/MC/MCDwarf.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000021#include "llvm/MC/MCExpr.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000022#include "llvm/MC/MCParser/AsmCond.h"
23#include "llvm/MC/MCParser/AsmLexer.h"
24#include "llvm/MC/MCParser/MCAsmParser.h"
25#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000026#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000027#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000028#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000029#include "llvm/MC/MCSymbol.h"
Evan Cheng94b95502011-07-26 00:24:13 +000030#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000031#include "llvm/Support/CommandLine.h"
Benjamin Kramer518ff562012-01-28 15:28:41 +000032#include "llvm/Support/ErrorHandling.h"
Jim Grosbach254cf032011-06-29 16:05:14 +000033#include "llvm/Support/MathExtras.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000034#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000035#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000036#include "llvm/Support/raw_ostream.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000037#include <cctype>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000038#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000039using namespace llvm;
40
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000041static cl::opt<bool>
42FatalAssemblerWarnings("fatal-assembler-warnings",
43 cl::desc("Consider warnings as error"));
44
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000045namespace {
46
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000047/// \brief Helper class for tracking macro definitions.
Rafael Espindola28c1f6662012-06-03 22:41:23 +000048typedef std::vector<AsmToken> MacroArgument;
Rafael Espindola8a403d32012-08-08 14:51:03 +000049typedef std::vector<MacroArgument> MacroArguments;
Preston Gurd6c9176a2012-09-19 20:29:04 +000050typedef std::pair<StringRef, MacroArgument> MacroParameter;
Rafael Espindola8a403d32012-08-08 14:51:03 +000051typedef std::vector<MacroParameter> MacroParameters;
Rafael Espindola28c1f6662012-06-03 22:41:23 +000052
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000053struct Macro {
54 StringRef Name;
55 StringRef Body;
Rafael Espindola8a403d32012-08-08 14:51:03 +000056 MacroParameters Parameters;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000057
58public:
Rafael Espindola8a403d32012-08-08 14:51:03 +000059 Macro(StringRef N, StringRef B, const MacroParameters &P) :
Rafael Espindola65366442011-06-05 02:43:45 +000060 Name(N), Body(B), Parameters(P) {}
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000061};
62
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000063/// \brief Helper class for storing information about an active macro
64/// instantiation.
65struct MacroInstantiation {
66 /// The macro being instantiated.
67 const Macro *TheMacro;
68
69 /// The macro instantiation with substitutions.
70 MemoryBuffer *Instantiation;
71
72 /// The location of the instantiation.
73 SMLoc InstantiationLoc;
74
75 /// The location where parsing should resume upon instantiation completion.
76 SMLoc ExitLoc;
77
78public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000079 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000080 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000081};
82
Daniel Dunbaraef87e32010-07-18 18:31:38 +000083/// \brief The concrete assembly parser instance.
84class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000085 friend class GenericAsmParser;
86
Craig Topper85aadc02012-09-15 16:23:52 +000087 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
88 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000089private:
90 AsmLexer Lexer;
91 MCContext &Ctx;
92 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +000093 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000094 SourceMgr &SrcMgr;
Benjamin Kramer04a04262011-10-16 10:48:29 +000095 SourceMgr::DiagHandlerTy SavedDiagHandler;
96 void *SavedDiagContext;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000097 MCAsmParserExtension *GenericParser;
98 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +000099
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000100 /// This is the current buffer index we're lexing from as managed by the
101 /// SourceMgr object.
102 int CurBuffer;
103
104 AsmCond TheCondState;
105 std::vector<AsmCond> TheCondStack;
106
107 /// DirectiveMap - This is a table handlers for directives. Each handler is
108 /// invoked after the directive identifier is read and is responsible for
109 /// parsing and validating the rest of the directive. The handler is passed
110 /// in the directive name and the location of the directive keyword.
111 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000112
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000113 /// MacroMap - Map of currently defined macros.
114 StringMap<Macro*> MacroMap;
115
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000116 /// ActiveMacros - Stack of active macro instantiations.
117 std::vector<MacroInstantiation*> ActiveMacros;
118
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000119 /// Boolean tracking whether macro substitution is enabled.
120 unsigned MacrosEnabled : 1;
121
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000122 /// Flag tracking whether any errors have been encountered.
123 unsigned HadError : 1;
124
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000125 /// The values from the last parsed cpp hash file line comment if any.
126 StringRef CppHashFilename;
127 int64_t CppHashLineNumber;
128 SMLoc CppHashLoc;
129
Devang Patel0db58bf2012-01-31 18:14:05 +0000130 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
131 unsigned AssemblerDialect;
132
Preston Gurd7b6f2032012-09-19 20:36:12 +0000133 /// IsDarwin - is Darwin compatibility enabled?
134 bool IsDarwin;
135
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000136public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000137 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000138 const MCAsmInfo &MAI);
Craig Topper345d16d2012-08-29 05:48:09 +0000139 virtual ~AsmParser();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000140
141 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
142
Craig Topper345d16d2012-08-29 05:48:09 +0000143 virtual void AddDirectiveHandler(MCAsmParserExtension *Object,
144 StringRef Directive,
145 DirectiveHandler Handler) {
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000146 DirectiveMap[Directive] = std::make_pair(Object, Handler);
147 }
148
149public:
150 /// @name MCAsmParser Interface
151 /// {
152
153 virtual SourceMgr &getSourceManager() { return SrcMgr; }
154 virtual MCAsmLexer &getLexer() { return Lexer; }
155 virtual MCContext &getContext() { return Ctx; }
156 virtual MCStreamer &getStreamer() { return Out; }
Devang Patel0db58bf2012-01-31 18:14:05 +0000157 virtual unsigned getAssemblerDialect() {
158 if (AssemblerDialect == ~0U)
159 return MAI.getAssemblerDialect();
160 else
161 return AssemblerDialect;
162 }
163 virtual void setAssemblerDialect(unsigned i) {
164 AssemblerDialect = i;
165 }
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000166
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000167 virtual bool Warning(SMLoc L, const Twine &Msg,
168 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
169 virtual bool Error(SMLoc L, const Twine &Msg,
170 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000171
Craig Topper345d16d2012-08-29 05:48:09 +0000172 virtual const AsmToken &Lex();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000173
174 bool ParseExpression(const MCExpr *&Res);
175 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
176 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
177 virtual bool ParseAbsoluteExpression(int64_t &Res);
178
179 /// }
180
181private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000182 void CheckForValidSection();
183
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000184 bool ParseStatement();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000185 void EatToEndOfLine();
186 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000187
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000188 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola761cb062012-06-03 23:57:14 +0000189 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola8a403d32012-08-08 14:51:03 +0000190 const MacroParameters &Parameters,
191 const MacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +0000192 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000193 void HandleMacroExit();
194
195 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000196 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattner462b43c2011-10-16 05:47:55 +0000197 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
198 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000199 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000200 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000201
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000202 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
203 bool EnterIncludeFile(const std::string &Filename);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000204 /// ProcessIncbinFile - Process the specified file for the .incbin directive.
205 /// This returns true on failure.
206 bool ProcessIncbinFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000207
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +0000208 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000209 /// current token is not set; clients should ensure Lex() is called
210 /// subsequently.
211 void JumpToLoc(SMLoc Loc);
212
Craig Topper345d16d2012-08-29 05:48:09 +0000213 virtual void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000214
Preston Gurd7b6f2032012-09-19 20:36:12 +0000215 bool ParseMacroArgument(MacroArgument &MA,
216 AsmToken::TokenKind &ArgumentDelimiter);
Rafael Espindola8a403d32012-08-08 14:51:03 +0000217 bool ParseMacroArguments(const Macro *M, MacroArguments &A);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000218
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000219 /// \brief Parse up to the end of statement and a return the contents from the
220 /// current token until the end of the statement; the current token on exit
221 /// will be either the EndOfStatement or EOF.
Craig Topper345d16d2012-08-29 05:48:09 +0000222 virtual StringRef ParseStringToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000223
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000224 /// \brief Parse until the end of a statement or a comma is encountered,
225 /// return the contents from the current token up to the end or comma.
226 StringRef ParseStringToComma();
227
Jim Grosbach3f90a4c2012-09-13 23:11:31 +0000228 bool ParseAssignment(StringRef Name, bool allow_redef,
229 bool NoDeadStrip = false);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000230
231 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
232 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
233 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000234 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000235
236 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +0000237 /// and set \p Res to the identifier contents.
Craig Topper345d16d2012-08-29 05:48:09 +0000238 virtual bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000239
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000240 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000241
242 // ".ascii", ".asciiz", ".string"
243 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000244 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000245 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000246 bool ParseDirectiveFill(); // ".fill"
247 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000248 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000249 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000250 bool ParseDirectiveOrg(); // ".org"
251 // ".align{,32}", ".p2align{,w,l}"
252 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
253
254 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
255 /// accepts a single symbol (which should be a label or an external).
256 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000257
258 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
259
260 bool ParseDirectiveAbort(); // ".abort"
261 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000262 bool ParseDirectiveIncbin(); // ".incbin"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000263
264 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +0000265 // ".ifb" or ".ifnb", depending on ExpectBlank.
266 bool ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000267 // ".ifc" or ".ifnc", depending on ExpectEqual.
268 bool ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000269 // ".ifdef" or ".ifndef", depending on expect_defined
270 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000271 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
272 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
273 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
274
275 /// ParseEscapedString - Parse the current token as a string which may include
276 /// escaped characters and return the string contents.
277 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000278
279 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
280 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola2ec304c2012-05-12 16:31:10 +0000281
Rafael Espindola761cb062012-06-03 23:57:14 +0000282 // Macro-like directives
283 Macro *ParseMacroLikeBody(SMLoc DirectiveLoc);
284 void InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
285 raw_svector_ostream &OS);
286 bool ParseDirectiveRept(SMLoc DirectiveLoc); // ".rept"
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000287 bool ParseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
Rafael Espindolafc9216e2012-06-16 18:03:25 +0000288 bool ParseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
Rafael Espindola761cb062012-06-03 23:57:14 +0000289 bool ParseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000290};
291
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000292/// \brief Generic implementations of directive handling, etc. which is shared
293/// (or the default, at least) for all assembler parser.
294class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000295 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
296 void AddDirectiveHandler(StringRef Directive) {
297 getParser().AddDirectiveHandler(this, Directive,
298 HandleDirective<GenericAsmParser, Handler>);
299 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000300public:
301 GenericAsmParser() {}
302
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000303 AsmParser &getParser() {
304 return (AsmParser&) this->MCAsmParserExtension::getParser();
305 }
306
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000307 virtual void Initialize(MCAsmParser &Parser) {
308 // Call the base implementation.
309 this->MCAsmParserExtension::Initialize(Parser);
310
311 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000312 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
313 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
314 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000315 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000316
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000317 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000318 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
319 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000320 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
321 ".cfi_startproc");
322 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
323 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000324 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
325 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000326 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
327 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000328 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
329 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000330 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
331 ".cfi_def_cfa_register");
332 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
333 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000334 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
335 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000336 AddDirectiveHandler<
337 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
338 AddDirectiveHandler<
339 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000340 AddDirectiveHandler<
341 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
342 AddDirectiveHandler<
343 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000344 AddDirectiveHandler<
345 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000346 AddDirectiveHandler<
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000347 &GenericAsmParser::ParseDirectiveCFIRestore>(".cfi_restore");
348 AddDirectiveHandler<
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000349 &GenericAsmParser::ParseDirectiveCFIEscape>(".cfi_escape");
Rafael Espindola16d7d432012-01-23 21:51:52 +0000350 AddDirectiveHandler<
351 &GenericAsmParser::ParseDirectiveCFISignalFrame>(".cfi_signal_frame");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000352
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000353 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000354 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
355 ".macros_on");
356 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
357 ".macros_off");
358 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
359 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
360 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000361 AddDirectiveHandler<&GenericAsmParser::ParseDirectivePurgeMacro>(".purgem");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000362
363 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
364 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000365 }
366
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000367 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
368
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000369 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
370 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
371 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000372 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000373 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000374 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
375 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000376 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000377 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000378 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000379 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
380 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000381 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000382 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000383 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
384 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000385 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000386 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000387 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
Rafael Espindola16d7d432012-01-23 21:51:52 +0000388 bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000389
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000390 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000391 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
392 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000393 bool ParseDirectivePurgeMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000394
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000395 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000396};
397
398}
399
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000400namespace llvm {
401
402extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000403extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000404extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000405
406}
407
Chris Lattneraaec2052010-01-19 19:46:13 +0000408enum { DEFAULT_ADDRSPACE = 0 };
409
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000410AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000411 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000412 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000413 GenericParser(new GenericAsmParser), PlatformParser(0),
Preston Gurd7b6f2032012-09-19 20:36:12 +0000414 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0),
415 AssemblerDialect(~0U), IsDarwin(false) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000416 // Save the old handler.
417 SavedDiagHandler = SrcMgr.getDiagHandler();
418 SavedDiagContext = SrcMgr.getDiagContext();
419 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000420 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000421 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000422
423 // Initialize the generic parser.
424 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000425
426 // Initialize the platform / file format parser.
427 //
428 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
429 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000430 if (_MAI.hasMicrosoftFastStdCallMangling()) {
431 PlatformParser = createCOFFAsmParser();
432 PlatformParser->Initialize(*this);
433 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000434 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000435 PlatformParser->Initialize(*this);
Preston Gurd7b6f2032012-09-19 20:36:12 +0000436 IsDarwin = true;
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000437 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000438 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000439 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000440 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000441}
442
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000443AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000444 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
445
446 // Destroy any macros.
447 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
448 ie = MacroMap.end(); it != ie; ++it)
449 delete it->getValue();
450
Daniel Dunbare4749702010-07-12 18:12:02 +0000451 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000452 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000453}
454
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000455void AsmParser::PrintMacroInstantiations() {
456 // Print the active macro instantiation stack.
457 for (std::vector<MacroInstantiation*>::const_reverse_iterator
458 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000459 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
460 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000461}
462
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000463bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000464 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000465 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000466 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000467 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000468 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000469}
470
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000471bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000472 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000473 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000474 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000475 return true;
476}
477
Sean Callananfd0b0282010-01-21 00:19:58 +0000478bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000479 std::string IncludedFile;
480 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000481 if (NewBuf == -1)
482 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000483
Sean Callananfd0b0282010-01-21 00:19:58 +0000484 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000485
Sean Callananfd0b0282010-01-21 00:19:58 +0000486 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000487
Sean Callananfd0b0282010-01-21 00:19:58 +0000488 return false;
489}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000490
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000491/// Process the specified .incbin file by seaching for it in the include paths
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000492/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000493/// returns true on failure.
494bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
495 std::string IncludedFile;
496 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
497 if (NewBuf == -1)
498 return true;
499
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000500 // Pick up the bytes from the file and emit them.
Kevin Enderbydac29532011-12-15 00:00:27 +0000501 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
502 DEFAULT_ADDRSPACE);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000503 return false;
504}
505
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000506void AsmParser::JumpToLoc(SMLoc Loc) {
507 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
508 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
509}
510
Sean Callananfd0b0282010-01-21 00:19:58 +0000511const AsmToken &AsmParser::Lex() {
512 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000513
Sean Callananfd0b0282010-01-21 00:19:58 +0000514 if (tok->is(AsmToken::Eof)) {
515 // If this is the end of an included file, pop the parent file off the
516 // include stack.
517 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
518 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000519 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000520 tok = &Lexer.Lex();
521 }
522 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000523
Sean Callananfd0b0282010-01-21 00:19:58 +0000524 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000525 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000526
Sean Callananfd0b0282010-01-21 00:19:58 +0000527 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000528}
529
Chris Lattner79180e22010-04-05 23:15:42 +0000530bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000531 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000532 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000533 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000534
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000535 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000536 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000537
538 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000539 AsmCond StartingCondState = TheCondState;
540
Kevin Enderby613b7572011-11-01 22:27:22 +0000541 // If we are generating dwarf for assembly source files save the initial text
542 // section and generate a .file directive.
543 if (getContext().getGenDwarfForAssembly()) {
544 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000545 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
546 getStreamer().EmitLabel(SectionStartSym);
547 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000548 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
549 StringRef(), SrcMgr.getMemoryBuffer(CurBuffer)->getBufferIdentifier());
550 }
551
Chris Lattnerb717fb02009-07-02 21:53:43 +0000552 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000553 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000554 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000555
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000556 // We had an error, validate that one was emitted and recover by skipping to
557 // the next line.
558 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000559 EatToEndOfStatement();
560 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000561
562 if (TheCondState.TheCond != StartingCondState.TheCond ||
563 TheCondState.Ignore != StartingCondState.Ignore)
564 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000565
566 // Check to see there are no empty DwarfFile slots.
567 const std::vector<MCDwarfFile *> &MCDwarfFiles =
568 getContext().getMCDwarfFiles();
569 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000570 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000571 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000572 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000573
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000574 // Check to see that all assembler local symbols were actually defined.
575 // Targets that don't do subsections via symbols may not want this, though,
576 // so conservatively exclude them. Only do this if we're finalizing, though,
577 // as otherwise we won't necessarilly have seen everything yet.
578 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
579 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
580 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
581 e = Symbols.end();
582 i != e; ++i) {
583 MCSymbol *Sym = i->getValue();
584 // Variable symbols may not be marked as defined, so check those
585 // explicitly. If we know it's a variable, we have a definition for
586 // the purposes of this check.
587 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
588 // FIXME: We would really like to refer back to where the symbol was
589 // first referenced for a source location. We need to add something
590 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000591 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
592 "assembler local symbol '" + Sym->getName() +
593 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000594 }
595 }
596
597
Chris Lattner79180e22010-04-05 23:15:42 +0000598 // Finalize the output stream if there are no errors and if the client wants
599 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000600 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000601 Out.Finish();
602
Chris Lattnerb717fb02009-07-02 21:53:43 +0000603 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000604}
605
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000606void AsmParser::CheckForValidSection() {
607 if (!getStreamer().getCurrentSection()) {
608 TokError("expected section directive before assembly directive");
609 Out.SwitchSection(Ctx.getMachOSection(
610 "__TEXT", "__text",
611 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
612 0, SectionKind::getText()));
613 }
614}
615
Chris Lattner2cf5f142009-06-22 01:29:09 +0000616/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
617void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000618 while (Lexer.isNot(AsmToken::EndOfStatement) &&
619 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000620 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000621
Chris Lattner2cf5f142009-06-22 01:29:09 +0000622 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000623 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000624 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000625}
626
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000627StringRef AsmParser::ParseStringToEndOfStatement() {
628 const char *Start = getTok().getLoc().getPointer();
629
630 while (Lexer.isNot(AsmToken::EndOfStatement) &&
631 Lexer.isNot(AsmToken::Eof))
632 Lex();
633
634 const char *End = getTok().getLoc().getPointer();
635 return StringRef(Start, End - Start);
636}
Chris Lattnerc4193832009-06-22 05:51:26 +0000637
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000638StringRef AsmParser::ParseStringToComma() {
639 const char *Start = getTok().getLoc().getPointer();
640
641 while (Lexer.isNot(AsmToken::EndOfStatement) &&
642 Lexer.isNot(AsmToken::Comma) &&
643 Lexer.isNot(AsmToken::Eof))
644 Lex();
645
646 const char *End = getTok().getLoc().getPointer();
647 return StringRef(Start, End - Start);
648}
649
Chris Lattner74ec1a32009-06-22 06:32:03 +0000650/// ParseParenExpr - Parse a paren expression and return it.
651/// NOTE: This assumes the leading '(' has already been consumed.
652///
653/// parenexpr ::= expr)
654///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000655bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000656 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000657 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000658 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000659 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000660 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000661 return false;
662}
Chris Lattnerc4193832009-06-22 05:51:26 +0000663
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000664/// ParseBracketExpr - Parse a bracket expression and return it.
665/// NOTE: This assumes the leading '[' has already been consumed.
666///
667/// bracketexpr ::= expr]
668///
669bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
670 if (ParseExpression(Res)) return true;
671 if (Lexer.isNot(AsmToken::RBrac))
672 return TokError("expected ']' in brackets expression");
673 EndLoc = Lexer.getLoc();
674 Lex();
675 return false;
676}
677
Chris Lattner74ec1a32009-06-22 06:32:03 +0000678/// ParsePrimaryExpr - Parse a primary expression and return it.
679/// primaryexpr ::= (parenexpr
680/// primaryexpr ::= symbol
681/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000682/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000683/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000684bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000685 switch (Lexer.getKind()) {
686 default:
687 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000688 // If we have an error assume that we've already handled it.
689 case AsmToken::Error:
690 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000691 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000692 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000693 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000694 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000695 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000696 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000697 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000698 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000699 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000700 EndLoc = Lexer.getLoc();
701
702 StringRef Identifier;
703 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000704 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000705
Daniel Dunbarfffff912009-10-16 01:34:54 +0000706 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000707 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000708 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000709
710 // Lookup the symbol variant if used.
711 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000712 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000713 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000714 if (Variant == MCSymbolRefExpr::VK_Invalid) {
715 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000716 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000717 }
718 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000719
Daniel Dunbarfffff912009-10-16 01:34:54 +0000720 // If this is an absolute variable reference, substitute it now to preserve
721 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000722 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000723 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000724 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000725
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000726 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000727 return false;
728 }
729
730 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000731 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000732 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000733 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000734 case AsmToken::Integer: {
735 SMLoc Loc = getTok().getLoc();
736 int64_t IntVal = getTok().getIntVal();
737 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000738 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000739 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000740 // Look for 'b' or 'f' following an Integer as a directional label
741 if (Lexer.getKind() == AsmToken::Identifier) {
742 StringRef IDVal = getTok().getString();
743 if (IDVal == "f" || IDVal == "b"){
744 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
745 IDVal == "f" ? 1 : 0);
746 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
747 getContext());
Benjamin Kramer29739e72012-05-12 16:52:21 +0000748 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000749 return Error(Loc, "invalid reference to undefined symbol");
750 EndLoc = Lexer.getLoc();
751 Lex(); // Eat identifier.
752 }
753 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000754 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000755 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000756 case AsmToken::Real: {
757 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000758 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000759 Res = MCConstantExpr::Create(IntVal, getContext());
760 Lex(); // Eat token.
761 return false;
762 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000763 case AsmToken::Dot: {
764 // This is a '.' reference, which references the current PC. Emit a
765 // temporary label to the streamer and refer to it.
766 MCSymbol *Sym = Ctx.CreateTempSymbol();
767 Out.EmitLabel(Sym);
768 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
769 EndLoc = Lexer.getLoc();
770 Lex(); // Eat identifier.
771 return false;
772 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000773 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000774 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000775 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000776 case AsmToken::LBrac:
777 if (!PlatformParser->HasBracketExpressions())
778 return TokError("brackets expression not supported on this target");
779 Lex(); // Eat the '['.
780 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000781 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000782 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000783 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000784 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000785 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000786 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000787 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000788 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000789 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000790 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000791 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000792 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000793 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000794 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000795 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000796 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000797 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000798 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000799 }
800}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000801
Chris Lattnerb4307b32010-01-15 19:28:38 +0000802bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000803 SMLoc EndLoc;
804 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000805}
806
Daniel Dunbarcceba832010-09-17 02:47:07 +0000807const MCExpr *
808AsmParser::ApplyModifierToExpr(const MCExpr *E,
809 MCSymbolRefExpr::VariantKind Variant) {
810 // Recurse over the given expression, rebuilding it to apply the given variant
811 // if there is exactly one symbol.
812 switch (E->getKind()) {
813 case MCExpr::Target:
814 case MCExpr::Constant:
815 return 0;
816
817 case MCExpr::SymbolRef: {
818 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
819
820 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
821 TokError("invalid variant on expression '" +
822 getTok().getIdentifier() + "' (already modified)");
823 return E;
824 }
825
826 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
827 }
828
829 case MCExpr::Unary: {
830 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
831 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
832 if (!Sub)
833 return 0;
834 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
835 }
836
837 case MCExpr::Binary: {
838 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
839 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
840 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
841
842 if (!LHS && !RHS)
843 return 0;
844
845 if (!LHS) LHS = BE->getLHS();
846 if (!RHS) RHS = BE->getRHS();
847
848 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
849 }
850 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000851
Craig Topper85814382012-02-07 05:05:23 +0000852 llvm_unreachable("Invalid expression kind!");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000853}
854
Chris Lattner74ec1a32009-06-22 06:32:03 +0000855/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000856///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000857/// expr ::= expr &&,|| expr -> lowest.
858/// expr ::= expr |,^,&,! expr
859/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
860/// expr ::= expr <<,>> expr
861/// expr ::= expr +,- expr
862/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000863/// expr ::= primaryexpr
864///
Chris Lattner54482b42010-01-15 19:39:23 +0000865bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000866 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000867 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000868 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
869 return true;
870
Daniel Dunbarcceba832010-09-17 02:47:07 +0000871 // As a special case, we support 'a op b @ modifier' by rewriting the
872 // expression to include the modifier. This is inefficient, but in general we
873 // expect users to use 'a@modifier op b'.
874 if (Lexer.getKind() == AsmToken::At) {
875 Lex();
876
877 if (Lexer.isNot(AsmToken::Identifier))
878 return TokError("unexpected symbol modifier following '@'");
879
880 MCSymbolRefExpr::VariantKind Variant =
881 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
882 if (Variant == MCSymbolRefExpr::VK_Invalid)
883 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
884
885 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
886 if (!ModifiedRes) {
887 return TokError("invalid modifier '" + getTok().getIdentifier() +
888 "' (no symbols present)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000889 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000890
Daniel Dunbarcceba832010-09-17 02:47:07 +0000891 Res = ModifiedRes;
892 Lex();
893 }
894
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000895 // Try to constant fold it up front, if possible.
896 int64_t Value;
897 if (Res->EvaluateAsAbsolute(Value))
898 Res = MCConstantExpr::Create(Value, getContext());
899
900 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000901}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000902
Chris Lattnerb4307b32010-01-15 19:28:38 +0000903bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000904 Res = 0;
905 return ParseParenExpr(Res, EndLoc) ||
906 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000907}
908
Daniel Dunbar475839e2009-06-29 20:37:27 +0000909bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000910 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000911
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000912 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000913 if (ParseExpression(Expr))
914 return true;
915
Daniel Dunbare00b0112009-10-16 01:57:52 +0000916 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000917 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000918
919 return false;
920}
921
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000922static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000923 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000924 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000925 default:
926 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000927
Jim Grosbachfbe16812011-08-20 16:24:13 +0000928 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000929 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000930 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000931 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000932 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000933 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000934 return 1;
935
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000936
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000937 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000938 //
939 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000940 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000941 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000942 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000943 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000944 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000945 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000946 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000947 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000948 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000949
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000950 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000951 case AsmToken::EqualEqual:
952 Kind = MCBinaryExpr::EQ;
953 return 3;
954 case AsmToken::ExclaimEqual:
955 case AsmToken::LessGreater:
956 Kind = MCBinaryExpr::NE;
957 return 3;
958 case AsmToken::Less:
959 Kind = MCBinaryExpr::LT;
960 return 3;
961 case AsmToken::LessEqual:
962 Kind = MCBinaryExpr::LTE;
963 return 3;
964 case AsmToken::Greater:
965 Kind = MCBinaryExpr::GT;
966 return 3;
967 case AsmToken::GreaterEqual:
968 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000969 return 3;
970
Jim Grosbachfbe16812011-08-20 16:24:13 +0000971 // Intermediate Precedence: <<, >>
972 case AsmToken::LessLess:
973 Kind = MCBinaryExpr::Shl;
974 return 4;
975 case AsmToken::GreaterGreater:
976 Kind = MCBinaryExpr::Shr;
977 return 4;
978
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000979 // High Intermediate Precedence: +, -
980 case AsmToken::Plus:
981 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000982 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000983 case AsmToken::Minus:
984 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000985 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000986
Jim Grosbachfbe16812011-08-20 16:24:13 +0000987 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +0000988 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000989 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000990 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000991 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000992 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000993 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000994 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000995 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000996 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000997 }
998}
999
1000
1001/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
1002/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +00001003bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
1004 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001005 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001006 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001007 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001008
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001009 // If the next token is lower precedence than we are allowed to eat, return
1010 // successfully with what we ate already.
1011 if (TokPrec < Precedence)
1012 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001013
Sean Callanan79ed1a82010-01-19 20:22:31 +00001014 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001015
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001016 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001017 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001018 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001019
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001020 // If BinOp binds less tightly with RHS than the operator after RHS, let
1021 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001022 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001023 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001024 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001025 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001026 }
1027
Daniel Dunbar475839e2009-06-29 20:37:27 +00001028 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001029 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001030 }
1031}
1032
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001033/// ParseStatement:
1034/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +00001035/// ::= Label* Directive ...Operands... EndOfStatement
1036/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001037bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001038 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001039 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001040 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001041 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001042 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001043
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001044 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +00001045 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001046 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001047 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001048 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001049 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001050 if (Lexer.is(AsmToken::Hash))
1051 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001052
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001053 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001054 if (Lexer.is(AsmToken::Integer)) {
1055 LocalLabelVal = getTok().getIntVal();
1056 if (LocalLabelVal < 0) {
1057 if (!TheCondState.Ignore)
1058 return TokError("unexpected token at start of statement");
1059 IDVal = "";
1060 }
1061 else {
1062 IDVal = getTok().getString();
1063 Lex(); // Consume the integer token to be used as an identifier token.
1064 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001065 if (!TheCondState.Ignore)
1066 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001067 }
1068 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001069
1070 } else if (Lexer.is(AsmToken::Dot)) {
1071 // Treat '.' as a valid identifier in this context.
1072 Lex();
1073 IDVal = ".";
1074
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001075 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001076 if (!TheCondState.Ignore)
1077 return TokError("unexpected token at start of statement");
1078 IDVal = "";
1079 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001080
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001081
Chris Lattner7834fac2010-04-17 18:14:27 +00001082 // Handle conditional assembly here before checking for skipping. We
1083 // have to do this so that .endif isn't skipped in a ".if 0" block for
1084 // example.
1085 if (IDVal == ".if")
1086 return ParseDirectiveIf(IDLoc);
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00001087 if (IDVal == ".ifb")
1088 return ParseDirectiveIfb(IDLoc, true);
1089 if (IDVal == ".ifnb")
1090 return ParseDirectiveIfb(IDLoc, false);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00001091 if (IDVal == ".ifc")
1092 return ParseDirectiveIfc(IDLoc, true);
1093 if (IDVal == ".ifnc")
1094 return ParseDirectiveIfc(IDLoc, false);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00001095 if (IDVal == ".ifdef")
1096 return ParseDirectiveIfdef(IDLoc, true);
1097 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
1098 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +00001099 if (IDVal == ".elseif")
1100 return ParseDirectiveElseIf(IDLoc);
1101 if (IDVal == ".else")
1102 return ParseDirectiveElse(IDLoc);
1103 if (IDVal == ".endif")
1104 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001105
Chris Lattner7834fac2010-04-17 18:14:27 +00001106 // If we are in a ".if 0" block, ignore this statement.
1107 if (TheCondState.Ignore) {
1108 EatToEndOfStatement();
1109 return false;
1110 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001111
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001112 // FIXME: Recurse on local labels?
1113
1114 // See what kind of statement we have.
1115 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001116 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001117 CheckForValidSection();
1118
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001119 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001120 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001121
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001122 // Diagnose attempt to use '.' as a label.
1123 if (IDVal == ".")
1124 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1125
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001126 // Diagnose attempt to use a variable as a label.
1127 //
1128 // FIXME: Diagnostics. Note the location of the definition as a label.
1129 // FIXME: This doesn't diagnose assignment to a symbol which has been
1130 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001131 MCSymbol *Sym;
1132 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001133 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001134 else
1135 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001136 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001137 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001138
Daniel Dunbar959fd882009-08-26 22:13:22 +00001139 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001140 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001141
Kevin Enderby94c2e852011-12-09 18:09:40 +00001142 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001143 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001144 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001145 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1146 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001147
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001148 // Consume any end of statement token, if present, to avoid spurious
1149 // AddBlankLine calls().
1150 if (Lexer.is(AsmToken::EndOfStatement)) {
1151 Lex();
1152 if (Lexer.is(AsmToken::Eof))
1153 return false;
1154 }
1155
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001156 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001157 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001158
Daniel Dunbar3f872332009-07-28 16:08:33 +00001159 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001160 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001161 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001162
Nico Weber4c4c7322011-01-28 03:04:41 +00001163 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001164
1165 default: // Normal instruction or directive.
1166 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001167 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001168
1169 // If macros are enabled, check to see if this is a macro instantiation.
1170 if (MacrosEnabled)
1171 if (const Macro *M = MacroMap.lookup(IDVal))
1172 return HandleMacroEntry(IDVal, IDLoc, M);
1173
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001174 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001175 if (IDVal[0] == '.' && IDVal != ".") {
Akira Hatanaka3b02d952012-07-05 19:09:33 +00001176
1177 // Target hook for parsing target specific directives.
1178 if (!getTargetParser().ParseDirective(ID))
1179 return false;
1180
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001181 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001182 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001183 return ParseDirectiveSet(IDVal, true);
1184 if (IDVal == ".equiv")
1185 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001186
Daniel Dunbara0d14262009-06-24 23:30:00 +00001187 // Data directives
1188
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001189 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001190 return ParseDirectiveAscii(IDVal, false);
1191 if (IDVal == ".asciz" || IDVal == ".string")
1192 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001193
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001194 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001195 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001196 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001197 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001198 if (IDVal == ".value")
1199 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001200 if (IDVal == ".2byte")
1201 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001202 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001203 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001204 if (IDVal == ".int")
1205 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001206 if (IDVal == ".4byte")
1207 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001208 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001209 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001210 if (IDVal == ".8byte")
1211 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001212 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001213 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1214 if (IDVal == ".double")
1215 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001216
Eli Friedman5d68ec22010-07-19 04:17:25 +00001217 if (IDVal == ".align") {
1218 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1219 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1220 }
1221 if (IDVal == ".align32") {
1222 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1223 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1224 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001225 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001226 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001227 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001228 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001229 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001230 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001231 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001232 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001233 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001234 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001235 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001236 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1237
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001238 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001239 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001240
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001241 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001242 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001243 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001244 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001245 if (IDVal == ".zero")
1246 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001247
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001248 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001249
Benjamin Kramere14a3c52012-05-12 11:18:59 +00001250 if (IDVal == ".extern") {
1251 EatToEndOfStatement(); // .extern is the default, ignore it.
1252 return false;
1253 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001254 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001255 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001256 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001257 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001258 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001259 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001260 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001261 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001262 if (IDVal == ".symbol_resolver")
1263 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001264 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001265 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001266 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001267 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001268 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001269 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001270 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001271 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001272 if (IDVal == ".weak_def_can_be_hidden")
1273 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001274
Hans Wennborg5cc64912011-06-18 13:51:54 +00001275 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001276 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001277 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001278 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001279
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001280 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001281 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001282 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001283 return ParseDirectiveInclude();
Kevin Enderbyc55acca2011-12-14 21:47:48 +00001284 if (IDVal == ".incbin")
1285 return ParseDirectiveIncbin();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001286
Benjamin Kramer5cdf0ad2012-05-12 11:19:04 +00001287 if (IDVal == ".code16" || IDVal == ".code16gcc")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001288 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001289
Rafael Espindola761cb062012-06-03 23:57:14 +00001290 // Macro-like directives
Rafael Espindola2ec304c2012-05-12 16:31:10 +00001291 if (IDVal == ".rept")
1292 return ParseDirectiveRept(IDLoc);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001293 if (IDVal == ".irp")
1294 return ParseDirectiveIrp(IDLoc);
Rafael Espindolafc9216e2012-06-16 18:03:25 +00001295 if (IDVal == ".irpc")
1296 return ParseDirectiveIrpc(IDLoc);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00001297 if (IDVal == ".endr")
Rafael Espindola761cb062012-06-03 23:57:14 +00001298 return ParseDirectiveEndr(IDLoc);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00001299
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001300 // Look up the handler in the handler table.
1301 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1302 DirectiveMap.lookup(IDVal);
1303 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001304 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001305
Kevin Enderby9c656452009-09-10 20:51:44 +00001306
Jim Grosbach686c0182012-05-01 18:38:27 +00001307 return Error(IDLoc, "unknown directive");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001308 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001309
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001310 CheckForValidSection();
1311
Chris Lattnera7f13542010-05-19 23:34:33 +00001312 // Canonicalize the opcode to lower case.
1313 SmallString<128> Opcode;
1314 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1315 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001316
Chris Lattner98986712010-01-14 22:21:20 +00001317 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001318 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001319 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001320
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001321 // Dump the parsed representation, if requested.
1322 if (getShowParsedOperands()) {
1323 SmallString<256> Str;
1324 raw_svector_ostream OS(Str);
1325 OS << "parsed instruction: [";
1326 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1327 if (i != 0)
1328 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001329 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001330 }
1331 OS << "]";
1332
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001333 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001334 }
1335
Kevin Enderby613b7572011-11-01 22:27:22 +00001336 // If we are generating dwarf for assembly source files and the current
1337 // section is the initial text section then generate a .loc directive for
1338 // the instruction.
1339 if (!HadError && getContext().getGenDwarfForAssembly() &&
1340 getContext().getGenDwarfSection() == getStreamer().getCurrentSection() ) {
1341 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
1342 SrcMgr.FindLineNumber(IDLoc, CurBuffer),
1343 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001344 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001345 StringRef());
1346 }
1347
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001348 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001349 if (!HadError)
1350 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1351 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001352
Chris Lattner98986712010-01-14 22:21:20 +00001353 // Free any parsed operands.
1354 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1355 delete ParsedOperands[i];
1356
Chris Lattnercbf8a982010-09-11 16:18:25 +00001357 // Don't skip the rest of the line, the instruction parser is responsible for
1358 // that.
1359 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001360}
Chris Lattner9a023f72009-06-24 04:43:34 +00001361
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001362/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1363/// since they may not be able to be tokenized to get to the end of line token.
1364void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001365 if (!Lexer.is(AsmToken::EndOfStatement))
1366 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001367 // Eat EOL.
1368 Lex();
1369}
1370
1371/// ParseCppHashLineFilenameComment as this:
1372/// ::= # number "filename"
1373/// or just as a full line comment if it doesn't have a number and a string.
1374bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1375 Lex(); // Eat the hash token.
1376
1377 if (getLexer().isNot(AsmToken::Integer)) {
1378 // Consume the line since in cases it is not a well-formed line directive,
1379 // as if were simply a full line comment.
1380 EatToEndOfLine();
1381 return false;
1382 }
1383
1384 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001385 Lex();
1386
1387 if (getLexer().isNot(AsmToken::String)) {
1388 EatToEndOfLine();
1389 return false;
1390 }
1391
1392 StringRef Filename = getTok().getString();
1393 // Get rid of the enclosing quotes.
1394 Filename = Filename.substr(1, Filename.size()-2);
1395
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001396 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1397 CppHashLoc = L;
1398 CppHashFilename = Filename;
1399 CppHashLineNumber = LineNumber;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001400
1401 // Ignore any trailing characters, they're just comment.
1402 EatToEndOfLine();
1403 return false;
1404}
1405
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001406/// DiagHandler - will use the last parsed cpp hash line filename comment
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001407/// for the Filename and LineNo if any in the diagnostic.
1408void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1409 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1410 raw_ostream &OS = errs();
1411
1412 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1413 const SMLoc &DiagLoc = Diag.getLoc();
1414 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1415 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1416
1417 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1418 // before printing the message.
1419 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001420 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001421 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1422 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1423 }
1424
1425 // If we have not parsed a cpp hash line filename comment or the source
1426 // manager changed or buffer changed (like in a nested include) then just
1427 // print the normal diagnostic using its Filename and LineNo.
1428 if (!Parser->CppHashLineNumber ||
1429 &DiagSrcMgr != &Parser->SrcMgr ||
1430 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001431 if (Parser->SavedDiagHandler)
1432 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1433 else
1434 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001435 return;
1436 }
1437
1438 // Use the CppHashFilename and calculate a line number based on the
1439 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1440 // the diagnostic.
1441 const std::string Filename = Parser->CppHashFilename;
1442
1443 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1444 int CppHashLocLineNo =
1445 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1446 int LineNo = Parser->CppHashLineNumber - 1 +
1447 (DiagLocLineNo - CppHashLocLineNo);
1448
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001449 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1450 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001451 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001452 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001453
Benjamin Kramer04a04262011-10-16 10:48:29 +00001454 if (Parser->SavedDiagHandler)
1455 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1456 else
1457 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001458}
1459
Rafael Espindola799aacf2012-08-21 18:29:30 +00001460// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1461// difference being that that function accepts '@' as part of identifiers and
1462// we can't do that. AsmLexer.cpp should probably be changed to handle
1463// '@' as a special case when needed.
1464static bool isIdentifierChar(char c) {
1465 return isalnum(c) || c == '_' || c == '$' || c == '.';
1466}
1467
Rafael Espindola761cb062012-06-03 23:57:14 +00001468bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola8a403d32012-08-08 14:51:03 +00001469 const MacroParameters &Parameters,
1470 const MacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +00001471 const SMLoc &L) {
Rafael Espindola65366442011-06-05 02:43:45 +00001472 unsigned NParameters = Parameters.size();
1473 if (NParameters != 0 && NParameters != A.size())
1474 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001475
Preston Gurd7b6f2032012-09-19 20:36:12 +00001476 // A macro without parameters is handled differently on Darwin:
1477 // gas accepts no arguments and does no substitutions
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001478 while (!Body.empty()) {
1479 // Scan for the next substitution.
1480 std::size_t End = Body.size(), Pos = 0;
1481 for (; Pos != End; ++Pos) {
1482 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001483 if (!NParameters) {
1484 // This macro has no parameters, look for $0, $1, etc.
1485 if (Body[Pos] != '$' || Pos + 1 == End)
1486 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001487
Rafael Espindola65366442011-06-05 02:43:45 +00001488 char Next = Body[Pos + 1];
1489 if (Next == '$' || Next == 'n' || isdigit(Next))
1490 break;
1491 } else {
1492 // This macro has parameters, look for \foo, \bar, etc.
1493 if (Body[Pos] == '\\' && Pos + 1 != End)
1494 break;
1495 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001496 }
1497
1498 // Add the prefix.
1499 OS << Body.slice(0, Pos);
1500
1501 // Check if we reached the end.
1502 if (Pos == End)
1503 break;
1504
Rafael Espindola65366442011-06-05 02:43:45 +00001505 if (!NParameters) {
1506 switch (Body[Pos+1]) {
1507 // $$ => $
1508 case '$':
1509 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001510 break;
1511
Rafael Espindola65366442011-06-05 02:43:45 +00001512 // $n => number of arguments
1513 case 'n':
1514 OS << A.size();
1515 break;
1516
1517 // $[0-9] => argument
1518 default: {
1519 // Missing arguments are ignored.
1520 unsigned Index = Body[Pos+1] - '0';
1521 if (Index >= A.size())
1522 break;
1523
1524 // Otherwise substitute with the token values, with spaces eliminated.
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001525 for (MacroArgument::const_iterator it = A[Index].begin(),
Rafael Espindola65366442011-06-05 02:43:45 +00001526 ie = A[Index].end(); it != ie; ++it)
1527 OS << it->getString();
1528 break;
1529 }
1530 }
1531 Pos += 2;
1532 } else {
1533 unsigned I = Pos + 1;
Rafael Espindola799aacf2012-08-21 18:29:30 +00001534 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola65366442011-06-05 02:43:45 +00001535 ++I;
1536
1537 const char *Begin = Body.data() + Pos +1;
1538 StringRef Argument(Begin, I - (Pos +1));
1539 unsigned Index = 0;
1540 for (; Index < NParameters; ++Index)
Preston Gurd6c9176a2012-09-19 20:29:04 +00001541 if (Parameters[Index].first == Argument)
Rafael Espindola65366442011-06-05 02:43:45 +00001542 break;
1543
Preston Gurd7b6f2032012-09-19 20:36:12 +00001544 if (Index == NParameters) {
1545 if (Body[Pos+1] == '(' && Body[Pos+2] == ')')
1546 Pos += 3;
1547 else {
1548 OS << '\\' << Argument;
1549 Pos = I;
1550 }
1551 } else {
1552 for (MacroArgument::const_iterator it = A[Index].begin(),
1553 ie = A[Index].end(); it != ie; ++it)
1554 if (it->getKind() == AsmToken::String)
1555 OS << it->getStringContents();
1556 else
1557 OS << it->getString();
Rafael Espindola65366442011-06-05 02:43:45 +00001558
Preston Gurd7b6f2032012-09-19 20:36:12 +00001559 Pos += 1 + Argument.size();
1560 }
Rafael Espindola65366442011-06-05 02:43:45 +00001561 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001562 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001563 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001564 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001565
Rafael Espindola65366442011-06-05 02:43:45 +00001566 return false;
1567}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001568
Rafael Espindola65366442011-06-05 02:43:45 +00001569MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1570 MemoryBuffer *I)
1571 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1572{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001573}
1574
Preston Gurd7b6f2032012-09-19 20:36:12 +00001575static bool IsOperator(AsmToken::TokenKind kind)
1576{
1577 switch (kind)
1578 {
1579 default:
1580 return false;
1581 case AsmToken::Plus:
1582 case AsmToken::Minus:
1583 case AsmToken::Tilde:
1584 case AsmToken::Slash:
1585 case AsmToken::Star:
1586 case AsmToken::Dot:
1587 case AsmToken::Equal:
1588 case AsmToken::EqualEqual:
1589 case AsmToken::Pipe:
1590 case AsmToken::PipePipe:
1591 case AsmToken::Caret:
1592 case AsmToken::Amp:
1593 case AsmToken::AmpAmp:
1594 case AsmToken::Exclaim:
1595 case AsmToken::ExclaimEqual:
1596 case AsmToken::Percent:
1597 case AsmToken::Less:
1598 case AsmToken::LessEqual:
1599 case AsmToken::LessLess:
1600 case AsmToken::LessGreater:
1601 case AsmToken::Greater:
1602 case AsmToken::GreaterEqual:
1603 case AsmToken::GreaterGreater:
1604 return true;
1605 }
1606}
1607
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001608/// ParseMacroArgument - Extract AsmTokens for a macro argument.
1609/// This is used for both default macro parameter values and the
1610/// arguments in macro invocations
Preston Gurd7b6f2032012-09-19 20:36:12 +00001611bool AsmParser::ParseMacroArgument(MacroArgument &MA,
1612 AsmToken::TokenKind &ArgumentDelimiter) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001613 unsigned ParenLevel = 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001614 unsigned AddTokens = 0;
1615
1616 // gas accepts arguments separated by whitespace, except on Darwin
1617 if (!IsDarwin)
1618 Lexer.setSkipSpace(false);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001619
1620 for (;;) {
Preston Gurd7b6f2032012-09-19 20:36:12 +00001621 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) {
1622 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001623 return TokError("unexpected token in macro instantiation");
Preston Gurd7b6f2032012-09-19 20:36:12 +00001624 }
1625
1626 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1627 // Spaces and commas cannot be mixed to delimit parameters
1628 if (ArgumentDelimiter == AsmToken::Eof)
1629 ArgumentDelimiter = AsmToken::Comma;
1630 else if (ArgumentDelimiter != AsmToken::Comma) {
1631 Lexer.setSkipSpace(true);
1632 return TokError("expected ' ' for macro argument separator");
1633 }
1634 break;
1635 }
1636
1637 if (Lexer.is(AsmToken::Space)) {
1638 Lex(); // Eat spaces
1639
1640 // Spaces can delimit parameters, but could also be part an expression.
1641 // If the token after a space is an operator, add the token and the next
1642 // one into this argument
1643 if (ArgumentDelimiter == AsmToken::Space ||
1644 ArgumentDelimiter == AsmToken::Eof) {
1645 if (IsOperator(Lexer.getKind())) {
1646 // Check to see whether the token is used as an operator,
1647 // or part of an identifier
1648 const char *NextChar = getTok().getEndLoc().getPointer() + 1;
1649 if (*NextChar == ' ')
1650 AddTokens = 2;
1651 }
1652
1653 if (!AddTokens && ParenLevel == 0) {
1654 if (ArgumentDelimiter == AsmToken::Eof &&
1655 !IsOperator(Lexer.getKind()))
1656 ArgumentDelimiter = AsmToken::Space;
1657 break;
1658 }
1659 }
1660 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001661
1662 // HandleMacroEntry relies on not advancing the lexer here
1663 // to be able to fill in the remaining default parameter values
1664 if (Lexer.is(AsmToken::EndOfStatement))
1665 break;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001666
1667 // Adjust the current parentheses level.
1668 if (Lexer.is(AsmToken::LParen))
1669 ++ParenLevel;
1670 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1671 --ParenLevel;
1672
1673 // Append the token to the current argument list.
1674 MA.push_back(getTok());
Preston Gurd7b6f2032012-09-19 20:36:12 +00001675 if (AddTokens)
1676 AddTokens--;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001677 Lex();
1678 }
Preston Gurd7b6f2032012-09-19 20:36:12 +00001679
1680 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001681 if (ParenLevel != 0)
Rafael Espindola76ac2002012-08-21 15:55:04 +00001682 return TokError("unbalanced parentheses in macro argument");
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001683 return false;
1684}
1685
1686// Parse the macro instantiation arguments.
Rafael Espindola8a403d32012-08-08 14:51:03 +00001687bool AsmParser::ParseMacroArguments(const Macro *M, MacroArguments &A) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001688 const unsigned NParameters = M ? M->Parameters.size() : 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001689 // Argument delimiter is initially unknown. It will be set by
1690 // ParseMacroArgument()
1691 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001692
1693 // Parse two kinds of macro invocations:
1694 // - macros defined without any parameters accept an arbitrary number of them
1695 // - macros defined with parameters accept at most that many of them
1696 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1697 ++Parameter) {
1698 MacroArgument MA;
1699
Preston Gurd7b6f2032012-09-19 20:36:12 +00001700 if (ParseMacroArgument(MA, ArgumentDelimiter))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001701 return true;
1702
Preston Gurd6c9176a2012-09-19 20:29:04 +00001703 if (!MA.empty() || !NParameters)
1704 A.push_back(MA);
1705 else if (NParameters) {
1706 if (!M->Parameters[Parameter].second.empty())
1707 A.push_back(M->Parameters[Parameter].second);
1708 }
Jim Grosbach97146442012-07-30 22:44:17 +00001709
Preston Gurd6c9176a2012-09-19 20:29:04 +00001710 // At the end of the statement, fill in remaining arguments that have
1711 // default values. If there aren't any, then the next argument is
1712 // required but missing
1713 if (Lexer.is(AsmToken::EndOfStatement)) {
1714 if (NParameters && Parameter < NParameters - 1) {
1715 if (M->Parameters[Parameter + 1].second.empty())
1716 return TokError("macro argument '" +
1717 Twine(M->Parameters[Parameter + 1].first) +
1718 "' is missing");
1719 else
1720 continue;
1721 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001722 return false;
Preston Gurd6c9176a2012-09-19 20:29:04 +00001723 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001724
1725 if (Lexer.is(AsmToken::Comma))
1726 Lex();
1727 }
1728 return TokError("Too many arguments");
1729}
1730
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001731bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1732 const Macro *M) {
1733 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1734 // this, although we should protect against infinite loops.
1735 if (ActiveMacros.size() == 20)
1736 return TokError("macros cannot be nested more than 20 levels deep");
1737
Rafael Espindola8a403d32012-08-08 14:51:03 +00001738 MacroArguments A;
1739 if (ParseMacroArguments(M, A))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001740 return true;
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001741
Jim Grosbach97146442012-07-30 22:44:17 +00001742 // Remove any trailing empty arguments. Do this after-the-fact as we have
1743 // to keep empty arguments in the middle of the list or positionality
1744 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindola8a403d32012-08-08 14:51:03 +00001745 while (!A.empty() && A.back().empty())
1746 A.pop_back();
Jim Grosbach97146442012-07-30 22:44:17 +00001747
Rafael Espindola65366442011-06-05 02:43:45 +00001748 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1749 // to hold the macro body with substitutions.
1750 SmallString<256> Buf;
1751 StringRef Body = M->Body;
Rafael Espindola761cb062012-06-03 23:57:14 +00001752 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001753
Rafael Espindola8a403d32012-08-08 14:51:03 +00001754 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola65366442011-06-05 02:43:45 +00001755 return true;
1756
Rafael Espindola761cb062012-06-03 23:57:14 +00001757 // We include the .endmacro in the buffer as our queue to exit the macro
1758 // instantiation.
1759 OS << ".endmacro\n";
1760
Rafael Espindola65366442011-06-05 02:43:45 +00001761 MemoryBuffer *Instantiation =
Rafael Espindola761cb062012-06-03 23:57:14 +00001762 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola65366442011-06-05 02:43:45 +00001763
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001764 // Create the macro instantiation object and add to the current macro
1765 // instantiation stack.
1766 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001767 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001768 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001769 ActiveMacros.push_back(MI);
1770
1771 // Jump to the macro instantiation and prime the lexer.
1772 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1773 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1774 Lex();
1775
1776 return false;
1777}
1778
1779void AsmParser::HandleMacroExit() {
1780 // Jump to the EndOfStatement we should return to, and consume it.
1781 JumpToLoc(ActiveMacros.back()->ExitLoc);
1782 Lex();
1783
1784 // Pop the instantiation entry.
1785 delete ActiveMacros.back();
1786 ActiveMacros.pop_back();
1787}
1788
Rafael Espindolae71cc862012-01-28 05:57:00 +00001789static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001790 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001791 case MCExpr::Binary: {
1792 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1793 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001794 break;
1795 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001796 case MCExpr::Target:
1797 case MCExpr::Constant:
1798 return false;
1799 case MCExpr::SymbolRef: {
1800 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001801 if (S.isVariable())
1802 return IsUsedIn(Sym, S.getVariableValue());
1803 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001804 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001805 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001806 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001807 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001808
1809 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001810}
1811
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001812bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef,
1813 bool NoDeadStrip) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001814 // FIXME: Use better location, we should use proper tokens.
1815 SMLoc EqualLoc = Lexer.getLoc();
1816
Daniel Dunbar821e3332009-08-31 08:09:28 +00001817 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001818 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001819 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001820
Rafael Espindolae71cc862012-01-28 05:57:00 +00001821 // Note: we don't count b as used in "a = b". This is to allow
1822 // a = b
1823 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001824
Daniel Dunbar3f872332009-07-28 16:08:33 +00001825 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001826 return TokError("unexpected token in assignment");
1827
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001828 // Error on assignment to '.'.
1829 if (Name == ".") {
1830 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1831 "(use '.space' or '.org').)"));
1832 }
1833
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001834 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001835 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001836
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001837 // Validate that the LHS is allowed to be a variable (either it has not been
1838 // used as a symbol, or it is an absolute symbol).
1839 MCSymbol *Sym = getContext().LookupSymbol(Name);
1840 if (Sym) {
1841 // Diagnose assignment to a label.
1842 //
1843 // FIXME: Diagnostics. Note the location of the definition as a label.
1844 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001845 if (IsUsedIn(Sym, Value))
1846 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1847 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001848 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00001849 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1850 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001851 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001852 return Error(EqualLoc, "redefinition of '" + Name + "'");
1853 else if (!Sym->isVariable())
1854 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001855 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001856 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1857 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001858
1859 // Don't count these checks as uses.
1860 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001861 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001862 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001863
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001864 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001865
1866 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001867 Out.EmitAssignment(Sym, Value);
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001868 if (NoDeadStrip)
1869 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
1870
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001871
1872 return false;
1873}
1874
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001875/// ParseIdentifier:
1876/// ::= identifier
1877/// ::= string
1878bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001879 // The assembler has relaxed rules for accepting identifiers, in particular we
1880 // allow things like '.globl $foo', which would normally be separate
1881 // tokens. At this level, we have already lexed so we cannot (currently)
1882 // handle this as a context dependent token, instead we detect adjacent tokens
1883 // and return the combined identifier.
1884 if (Lexer.is(AsmToken::Dollar)) {
1885 SMLoc DollarLoc = getLexer().getLoc();
1886
1887 // Consume the dollar sign, and check for a following identifier.
1888 Lex();
1889 if (Lexer.isNot(AsmToken::Identifier))
1890 return true;
1891
1892 // We have a '$' followed by an identifier, make sure they are adjacent.
1893 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1894 return true;
1895
1896 // Construct the joined identifier and consume the token.
1897 Res = StringRef(DollarLoc.getPointer(),
1898 getTok().getIdentifier().size() + 1);
1899 Lex();
1900 return false;
1901 }
1902
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001903 if (Lexer.isNot(AsmToken::Identifier) &&
1904 Lexer.isNot(AsmToken::String))
1905 return true;
1906
Sean Callanan18b83232010-01-19 21:44:56 +00001907 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001908
Sean Callanan79ed1a82010-01-19 20:22:31 +00001909 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001910
1911 return false;
1912}
1913
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001914/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001915/// ::= .equ identifier ',' expression
1916/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001917/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001918bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001919 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001920
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001921 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001922 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001923
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001924 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001925 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001926 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001927
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001928 return ParseAssignment(Name, allow_redef, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001929}
1930
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001931bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001932 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001933
1934 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001935 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001936 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1937 if (Str[i] != '\\') {
1938 Data += Str[i];
1939 continue;
1940 }
1941
1942 // Recognize escaped characters. Note that this escape semantics currently
1943 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1944 ++i;
1945 if (i == e)
1946 return TokError("unexpected backslash at end of string");
1947
1948 // Recognize octal sequences.
1949 if ((unsigned) (Str[i] - '0') <= 7) {
1950 // Consume up to three octal characters.
1951 unsigned Value = Str[i] - '0';
1952
1953 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1954 ++i;
1955 Value = Value * 8 + (Str[i] - '0');
1956
1957 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1958 ++i;
1959 Value = Value * 8 + (Str[i] - '0');
1960 }
1961 }
1962
1963 if (Value > 255)
1964 return TokError("invalid octal escape sequence (out of range)");
1965
1966 Data += (unsigned char) Value;
1967 continue;
1968 }
1969
1970 // Otherwise recognize individual escapes.
1971 switch (Str[i]) {
1972 default:
1973 // Just reject invalid escape sequences for now.
1974 return TokError("invalid escape sequence (unrecognized character)");
1975
1976 case 'b': Data += '\b'; break;
1977 case 'f': Data += '\f'; break;
1978 case 'n': Data += '\n'; break;
1979 case 'r': Data += '\r'; break;
1980 case 't': Data += '\t'; break;
1981 case '"': Data += '"'; break;
1982 case '\\': Data += '\\'; break;
1983 }
1984 }
1985
1986 return false;
1987}
1988
Daniel Dunbara0d14262009-06-24 23:30:00 +00001989/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001990/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1991bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001992 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001993 CheckForValidSection();
1994
Daniel Dunbara0d14262009-06-24 23:30:00 +00001995 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001996 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001997 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001998
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001999 std::string Data;
2000 if (ParseEscapedString(Data))
2001 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002002
2003 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002004 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002005 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
2006
Sean Callanan79ed1a82010-01-19 20:22:31 +00002007 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002008
2009 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002010 break;
2011
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002012 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00002013 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002014 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002015 }
2016 }
2017
Sean Callanan79ed1a82010-01-19 20:22:31 +00002018 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002019 return false;
2020}
2021
2022/// ParseDirectiveValue
2023/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
2024bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002025 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002026 CheckForValidSection();
2027
Daniel Dunbara0d14262009-06-24 23:30:00 +00002028 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00002029 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00002030 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002031 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002032 return true;
2033
Daniel Dunbar414c0c42010-05-23 18:36:38 +00002034 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00002035 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2036 assert(Size <= 8 && "Invalid size");
2037 uint64_t IntValue = MCE->getValue();
2038 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2039 return Error(ExprLoc, "literal value out of range for directive");
2040 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
2041 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002042 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002043
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002044 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002045 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002046
Daniel Dunbara0d14262009-06-24 23:30:00 +00002047 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002048 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002049 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002050 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002051 }
2052 }
2053
Sean Callanan79ed1a82010-01-19 20:22:31 +00002054 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002055 return false;
2056}
2057
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002058/// ParseDirectiveRealValue
2059/// ::= (.single | .double) [ expression (, expression)* ]
2060bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
2061 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2062 CheckForValidSection();
2063
2064 for (;;) {
2065 // We don't truly support arithmetic on floating point expressions, so we
2066 // have to manually parse unary prefixes.
2067 bool IsNeg = false;
2068 if (getLexer().is(AsmToken::Minus)) {
2069 Lex();
2070 IsNeg = true;
2071 } else if (getLexer().is(AsmToken::Plus))
2072 Lex();
2073
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002074 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00002075 getLexer().isNot(AsmToken::Real) &&
2076 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002077 return TokError("unexpected token in directive");
2078
2079 // Convert to an APFloat.
2080 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00002081 StringRef IDVal = getTok().getString();
2082 if (getLexer().is(AsmToken::Identifier)) {
2083 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2084 Value = APFloat::getInf(Semantics);
2085 else if (!IDVal.compare_lower("nan"))
2086 Value = APFloat::getNaN(Semantics, false, ~0);
2087 else
2088 return TokError("invalid floating point literal");
2089 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002090 APFloat::opInvalidOp)
2091 return TokError("invalid floating point literal");
2092 if (IsNeg)
2093 Value.changeSign();
2094
2095 // Consume the numeric token.
2096 Lex();
2097
2098 // Emit the value as an integer.
2099 APInt AsInt = Value.bitcastToAPInt();
2100 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
2101 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
2102
2103 if (getLexer().is(AsmToken::EndOfStatement))
2104 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002105
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002106 if (getLexer().isNot(AsmToken::Comma))
2107 return TokError("unexpected token in directive");
2108 Lex();
2109 }
2110 }
2111
2112 Lex();
2113 return false;
2114}
2115
Daniel Dunbara0d14262009-06-24 23:30:00 +00002116/// ParseDirectiveSpace
2117/// ::= .space expression [ , expression ]
2118bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002119 CheckForValidSection();
2120
Daniel Dunbara0d14262009-06-24 23:30:00 +00002121 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002122 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002123 return true;
2124
2125 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002126 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2127 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002128 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002129 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002130
Daniel Dunbar475839e2009-06-29 20:37:27 +00002131 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002132 return true;
2133
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002134 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002135 return TokError("unexpected token in '.space' directive");
2136 }
2137
Sean Callanan79ed1a82010-01-19 20:22:31 +00002138 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002139
2140 if (NumBytes <= 0)
2141 return TokError("invalid number of bytes in '.space' directive");
2142
2143 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002144 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002145
2146 return false;
2147}
2148
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002149/// ParseDirectiveZero
2150/// ::= .zero expression
2151bool AsmParser::ParseDirectiveZero() {
2152 CheckForValidSection();
2153
2154 int64_t NumBytes;
2155 if (ParseAbsoluteExpression(NumBytes))
2156 return true;
2157
Rafael Espindolae452b172010-10-05 19:42:57 +00002158 int64_t Val = 0;
2159 if (getLexer().is(AsmToken::Comma)) {
2160 Lex();
2161 if (ParseAbsoluteExpression(Val))
2162 return true;
2163 }
2164
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002165 if (getLexer().isNot(AsmToken::EndOfStatement))
2166 return TokError("unexpected token in '.zero' directive");
2167
2168 Lex();
2169
Rafael Espindolae452b172010-10-05 19:42:57 +00002170 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002171
2172 return false;
2173}
2174
Daniel Dunbara0d14262009-06-24 23:30:00 +00002175/// ParseDirectiveFill
2176/// ::= .fill expression , expression , expression
2177bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002178 CheckForValidSection();
2179
Daniel Dunbara0d14262009-06-24 23:30:00 +00002180 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002181 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002182 return true;
2183
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002184 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002185 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002186 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002187
Daniel Dunbara0d14262009-06-24 23:30:00 +00002188 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002189 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002190 return true;
2191
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002192 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002193 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002194 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002195
Daniel Dunbara0d14262009-06-24 23:30:00 +00002196 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002197 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002198 return true;
2199
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002200 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002201 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002202
Sean Callanan79ed1a82010-01-19 20:22:31 +00002203 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002204
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002205 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2206 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002207
2208 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002209 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002210
2211 return false;
2212}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002213
2214/// ParseDirectiveOrg
2215/// ::= .org expression [ , expression ]
2216bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002217 CheckForValidSection();
2218
Daniel Dunbar821e3332009-08-31 08:09:28 +00002219 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002220 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002221 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002222 return true;
2223
2224 // Parse optional fill expression.
2225 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002226 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2227 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002228 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002229 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002230
Daniel Dunbar475839e2009-06-29 20:37:27 +00002231 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002232 return true;
2233
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002234 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002235 return TokError("unexpected token in '.org' directive");
2236 }
2237
Sean Callanan79ed1a82010-01-19 20:22:31 +00002238 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002239
Jim Grosbachebd4c052012-01-27 00:37:08 +00002240 // Only limited forms of relocatable expressions are accepted here, it
2241 // has to be relative to the current section. The streamer will return
2242 // 'true' if the expression wasn't evaluatable.
2243 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2244 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002245
2246 return false;
2247}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002248
2249/// ParseDirectiveAlign
2250/// ::= {.align, ...} expression [ , expression [ , expression ]]
2251bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002252 CheckForValidSection();
2253
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002254 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002255 int64_t Alignment;
2256 if (ParseAbsoluteExpression(Alignment))
2257 return true;
2258
2259 SMLoc MaxBytesLoc;
2260 bool HasFillExpr = false;
2261 int64_t FillExpr = 0;
2262 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002263 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2264 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002265 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002266 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002267
2268 // The fill expression can be omitted while specifying a maximum number of
2269 // alignment bytes, e.g:
2270 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002271 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002272 HasFillExpr = true;
2273 if (ParseAbsoluteExpression(FillExpr))
2274 return true;
2275 }
2276
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002277 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2278 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002279 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002280 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002281
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002282 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002283 if (ParseAbsoluteExpression(MaxBytesToFill))
2284 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002285
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002286 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002287 return TokError("unexpected token in directive");
2288 }
2289 }
2290
Sean Callanan79ed1a82010-01-19 20:22:31 +00002291 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002292
Daniel Dunbar648ac512010-05-17 21:54:30 +00002293 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002294 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002295
2296 // Compute alignment in bytes.
2297 if (IsPow2) {
2298 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002299 if (Alignment >= 32) {
2300 Error(AlignmentLoc, "invalid alignment value");
2301 Alignment = 31;
2302 }
2303
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002304 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002305 }
2306
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002307 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002308 if (MaxBytesLoc.isValid()) {
2309 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002310 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2311 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002312 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002313 }
2314
2315 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002316 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2317 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002318 MaxBytesToFill = 0;
2319 }
2320 }
2321
Daniel Dunbar648ac512010-05-17 21:54:30 +00002322 // Check whether we should use optimal code alignment for this .align
2323 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002324 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002325 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2326 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002327 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002328 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002329 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002330 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2331 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002332 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002333
2334 return false;
2335}
2336
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002337/// ParseDirectiveSymbolAttribute
2338/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002339bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002340 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002341 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002342 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002343 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002344
2345 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002346 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002347
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002348 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002349
Jim Grosbach10ec6502011-09-15 17:56:49 +00002350 // Assembler local symbols don't make any sense here. Complain loudly.
2351 if (Sym->isTemporary())
2352 return Error(Loc, "non-local symbol required in directive");
2353
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002354 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002355
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002356 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002357 break;
2358
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002359 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002360 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002361 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002362 }
2363 }
2364
Sean Callanan79ed1a82010-01-19 20:22:31 +00002365 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002366 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002367}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002368
2369/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002370/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2371bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002372 CheckForValidSection();
2373
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002374 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002375 StringRef Name;
2376 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002377 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002378
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002379 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002380 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002381
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002382 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002383 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002384 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002385
2386 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002387 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002388 if (ParseAbsoluteExpression(Size))
2389 return true;
2390
2391 int64_t Pow2Alignment = 0;
2392 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002393 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002394 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002395 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002396 if (ParseAbsoluteExpression(Pow2Alignment))
2397 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002398
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002399 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
2400 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer39646d92012-09-07 17:25:13 +00002401 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
2402
Chris Lattner258281d2010-01-19 06:22:22 +00002403 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002404 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
2405 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattner258281d2010-01-19 06:22:22 +00002406 if (!isPowerOf2_64(Pow2Alignment))
2407 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2408 Pow2Alignment = Log2_64(Pow2Alignment);
2409 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002410 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002411
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002412 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002413 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002414
Sean Callanan79ed1a82010-01-19 20:22:31 +00002415 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002416
Chris Lattner1fc3d752009-07-09 17:25:12 +00002417 // NOTE: a size of zero for a .comm should create a undefined symbol
2418 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002419 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002420 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2421 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002422
Eric Christopherc260a3e2010-05-14 01:38:54 +00002423 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002424 // may internally end up wanting an alignment in bytes.
2425 // FIXME: Diagnose overflow.
2426 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002427 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2428 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002429
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002430 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002431 return Error(IDLoc, "invalid symbol redefinition");
2432
Chris Lattner1fc3d752009-07-09 17:25:12 +00002433 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002434 if (IsLocal) {
Benjamin Kramer39646d92012-09-07 17:25:13 +00002435 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002436 return false;
2437 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002438
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002439 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002440 return false;
2441}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002442
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002443/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002444/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002445bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002446 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002447 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002448
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002449 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002450 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002451 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002452
Sean Callanan79ed1a82010-01-19 20:22:31 +00002453 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002454
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002455 if (Str.empty())
2456 Error(Loc, ".abort detected. Assembly stopping.");
2457 else
2458 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002459 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002460
2461 return false;
2462}
Kevin Enderby71148242009-07-14 21:35:03 +00002463
Kevin Enderby1f049b22009-07-14 23:21:55 +00002464/// ParseDirectiveInclude
2465/// ::= .include "filename"
2466bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002467 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002468 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002469
Sean Callanan18b83232010-01-19 21:44:56 +00002470 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002471 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002472 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002473
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002474 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002475 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002476
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002477 // Strip the quotes.
2478 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002479
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002480 // Attempt to switch the lexer to the included file before consuming the end
2481 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002482 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002483 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002484 return true;
2485 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002486
2487 return false;
2488}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002489
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002490/// ParseDirectiveIncbin
2491/// ::= .incbin "filename"
2492bool AsmParser::ParseDirectiveIncbin() {
2493 if (getLexer().isNot(AsmToken::String))
2494 return TokError("expected string in '.incbin' directive");
2495
2496 std::string Filename = getTok().getString();
2497 SMLoc IncbinLoc = getLexer().getLoc();
2498 Lex();
2499
2500 if (getLexer().isNot(AsmToken::EndOfStatement))
2501 return TokError("unexpected token in '.incbin' directive");
2502
2503 // Strip the quotes.
2504 Filename = Filename.substr(1, Filename.size()-2);
2505
2506 // Attempt to process the included file.
2507 if (ProcessIncbinFile(Filename)) {
2508 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2509 return true;
2510 }
2511
2512 return false;
2513}
2514
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002515/// ParseDirectiveIf
2516/// ::= .if expression
2517bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002518 TheCondStack.push_back(TheCondState);
2519 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramer29739e72012-05-12 16:52:21 +00002520 if (TheCondState.Ignore) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002521 EatToEndOfStatement();
Benjamin Kramer29739e72012-05-12 16:52:21 +00002522 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002523 int64_t ExprValue;
2524 if (ParseAbsoluteExpression(ExprValue))
2525 return true;
2526
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002527 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002528 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002529
Sean Callanan79ed1a82010-01-19 20:22:31 +00002530 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002531
2532 TheCondState.CondMet = ExprValue;
2533 TheCondState.Ignore = !TheCondState.CondMet;
2534 }
2535
2536 return false;
2537}
2538
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002539/// ParseDirectiveIfb
2540/// ::= .ifb string
2541bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
2542 TheCondStack.push_back(TheCondState);
2543 TheCondState.TheCond = AsmCond::IfCond;
2544
Benjamin Kramer29739e72012-05-12 16:52:21 +00002545 if (TheCondState.Ignore) {
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002546 EatToEndOfStatement();
2547 } else {
2548 StringRef Str = ParseStringToEndOfStatement();
2549
2550 if (getLexer().isNot(AsmToken::EndOfStatement))
2551 return TokError("unexpected token in '.ifb' directive");
2552
2553 Lex();
2554
2555 TheCondState.CondMet = ExpectBlank == Str.empty();
2556 TheCondState.Ignore = !TheCondState.CondMet;
2557 }
2558
2559 return false;
2560}
2561
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002562/// ParseDirectiveIfc
2563/// ::= .ifc string1, string2
2564bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
2565 TheCondStack.push_back(TheCondState);
2566 TheCondState.TheCond = AsmCond::IfCond;
2567
Benjamin Kramer29739e72012-05-12 16:52:21 +00002568 if (TheCondState.Ignore) {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002569 EatToEndOfStatement();
2570 } else {
2571 StringRef Str1 = ParseStringToComma();
2572
2573 if (getLexer().isNot(AsmToken::Comma))
2574 return TokError("unexpected token in '.ifc' directive");
2575
2576 Lex();
2577
2578 StringRef Str2 = ParseStringToEndOfStatement();
2579
2580 if (getLexer().isNot(AsmToken::EndOfStatement))
2581 return TokError("unexpected token in '.ifc' directive");
2582
2583 Lex();
2584
2585 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
2586 TheCondState.Ignore = !TheCondState.CondMet;
2587 }
2588
2589 return false;
2590}
2591
2592/// ParseDirectiveIfdef
2593/// ::= .ifdef symbol
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002594bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2595 StringRef Name;
2596 TheCondStack.push_back(TheCondState);
2597 TheCondState.TheCond = AsmCond::IfCond;
2598
2599 if (TheCondState.Ignore) {
2600 EatToEndOfStatement();
2601 } else {
2602 if (ParseIdentifier(Name))
2603 return TokError("expected identifier after '.ifdef'");
2604
2605 Lex();
2606
2607 MCSymbol *Sym = getContext().LookupSymbol(Name);
2608
2609 if (expect_defined)
2610 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2611 else
2612 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2613 TheCondState.Ignore = !TheCondState.CondMet;
2614 }
2615
2616 return false;
2617}
2618
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002619/// ParseDirectiveElseIf
2620/// ::= .elseif expression
2621bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2622 if (TheCondState.TheCond != AsmCond::IfCond &&
2623 TheCondState.TheCond != AsmCond::ElseIfCond)
2624 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2625 " an .elseif");
2626 TheCondState.TheCond = AsmCond::ElseIfCond;
2627
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002628 bool LastIgnoreState = false;
2629 if (!TheCondStack.empty())
2630 LastIgnoreState = TheCondStack.back().Ignore;
2631 if (LastIgnoreState || TheCondState.CondMet) {
2632 TheCondState.Ignore = true;
2633 EatToEndOfStatement();
2634 }
2635 else {
2636 int64_t ExprValue;
2637 if (ParseAbsoluteExpression(ExprValue))
2638 return true;
2639
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002640 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002641 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002642
Sean Callanan79ed1a82010-01-19 20:22:31 +00002643 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002644 TheCondState.CondMet = ExprValue;
2645 TheCondState.Ignore = !TheCondState.CondMet;
2646 }
2647
2648 return false;
2649}
2650
2651/// ParseDirectiveElse
2652/// ::= .else
2653bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002654 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002655 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002656
Sean Callanan79ed1a82010-01-19 20:22:31 +00002657 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002658
2659 if (TheCondState.TheCond != AsmCond::IfCond &&
2660 TheCondState.TheCond != AsmCond::ElseIfCond)
2661 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2662 ".elseif");
2663 TheCondState.TheCond = AsmCond::ElseCond;
2664 bool LastIgnoreState = false;
2665 if (!TheCondStack.empty())
2666 LastIgnoreState = TheCondStack.back().Ignore;
2667 if (LastIgnoreState || TheCondState.CondMet)
2668 TheCondState.Ignore = true;
2669 else
2670 TheCondState.Ignore = false;
2671
2672 return false;
2673}
2674
2675/// ParseDirectiveEndIf
2676/// ::= .endif
2677bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002678 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002679 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002680
Sean Callanan79ed1a82010-01-19 20:22:31 +00002681 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002682
2683 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2684 TheCondStack.empty())
2685 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2686 ".else");
2687 if (!TheCondStack.empty()) {
2688 TheCondState = TheCondStack.back();
2689 TheCondStack.pop_back();
2690 }
2691
2692 return false;
2693}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002694
2695/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002696/// ::= .file [number] filename
2697/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002698bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002699 // FIXME: I'm not sure what this is.
2700 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002701 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002702 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002703 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002704 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002705
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002706 if (FileNumber < 1)
2707 return TokError("file number less than one");
2708 }
2709
Daniel Dunbareceec052010-07-12 17:45:27 +00002710 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002711 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002712
Nick Lewycky44d798d2011-10-17 23:05:28 +00002713 // Usually the directory and filename together, otherwise just the directory.
2714 StringRef Path = getTok().getString();
2715 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002716 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002717
Nick Lewycky44d798d2011-10-17 23:05:28 +00002718 StringRef Directory;
2719 StringRef Filename;
2720 if (getLexer().is(AsmToken::String)) {
2721 if (FileNumber == -1)
2722 return TokError("explicit path specified, but no file number");
2723 Filename = getTok().getString();
2724 Filename = Filename.substr(1, Filename.size()-2);
2725 Directory = Path;
2726 Lex();
2727 } else {
2728 Filename = Path;
2729 }
2730
Daniel Dunbareceec052010-07-12 17:45:27 +00002731 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002732 return TokError("unexpected token in '.file' directive");
2733
Chris Lattnerd32e8032010-01-25 19:02:58 +00002734 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002735 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002736 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002737 if (getContext().getGenDwarfForAssembly() == true)
2738 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2739 "used to generate dwarf debug info for assembly code");
2740
Nick Lewycky44d798d2011-10-17 23:05:28 +00002741 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002742 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002743 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002744
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002745 return false;
2746}
2747
2748/// ParseDirectiveLine
2749/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002750bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002751 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2752 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002753 return TokError("unexpected token in '.line' directive");
2754
Sean Callanan18b83232010-01-19 21:44:56 +00002755 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002756 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002757 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002758
2759 // FIXME: Do something with the .line.
2760 }
2761
Daniel Dunbareceec052010-07-12 17:45:27 +00002762 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002763 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002764
2765 return false;
2766}
2767
2768
2769/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002770/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002771/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2772/// The first number is a file number, must have been previously assigned with
2773/// a .file directive, the second number is the line number and optionally the
2774/// third number is a column position (zero if not specified). The remaining
2775/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002776bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002777
Daniel Dunbareceec052010-07-12 17:45:27 +00002778 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002779 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002780 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002781 if (FileNumber < 1)
2782 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002783 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002784 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002785 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002786
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002787 int64_t LineNumber = 0;
2788 if (getLexer().is(AsmToken::Integer)) {
2789 LineNumber = getTok().getIntVal();
2790 if (LineNumber < 1)
2791 return TokError("line number less than one in '.loc' directive");
2792 Lex();
2793 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002794
2795 int64_t ColumnPos = 0;
2796 if (getLexer().is(AsmToken::Integer)) {
2797 ColumnPos = getTok().getIntVal();
2798 if (ColumnPos < 0)
2799 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002800 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002801 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002802
Kevin Enderbyc0957932010-09-30 16:52:03 +00002803 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002804 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002805 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002806 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2807 for (;;) {
2808 if (getLexer().is(AsmToken::EndOfStatement))
2809 break;
2810
2811 StringRef Name;
2812 SMLoc Loc = getTok().getLoc();
2813 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002814 return TokError("unexpected token in '.loc' directive");
2815
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002816 if (Name == "basic_block")
2817 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2818 else if (Name == "prologue_end")
2819 Flags |= DWARF2_FLAG_PROLOGUE_END;
2820 else if (Name == "epilogue_begin")
2821 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2822 else if (Name == "is_stmt") {
2823 SMLoc Loc = getTok().getLoc();
2824 const MCExpr *Value;
2825 if (getParser().ParseExpression(Value))
2826 return true;
2827 // The expression must be the constant 0 or 1.
2828 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2829 int Value = MCE->getValue();
2830 if (Value == 0)
2831 Flags &= ~DWARF2_FLAG_IS_STMT;
2832 else if (Value == 1)
2833 Flags |= DWARF2_FLAG_IS_STMT;
2834 else
2835 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002836 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002837 else {
2838 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2839 }
2840 }
2841 else if (Name == "isa") {
2842 SMLoc Loc = getTok().getLoc();
2843 const MCExpr *Value;
2844 if (getParser().ParseExpression(Value))
2845 return true;
2846 // The expression must be a constant greater or equal to 0.
2847 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2848 int Value = MCE->getValue();
2849 if (Value < 0)
2850 return Error(Loc, "isa number less than zero");
2851 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002852 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002853 else {
2854 return Error(Loc, "isa number not a constant value");
2855 }
2856 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002857 else if (Name == "discriminator") {
2858 if (getParser().ParseAbsoluteExpression(Discriminator))
2859 return true;
2860 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002861 else {
2862 return Error(Loc, "unknown sub-directive in '.loc' directive");
2863 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002864
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002865 if (getLexer().is(AsmToken::EndOfStatement))
2866 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002867 }
2868 }
2869
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002870 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002871 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002872
2873 return false;
2874}
2875
Daniel Dunbar138abae2010-10-16 04:56:42 +00002876/// ParseDirectiveStabs
2877/// ::= .stabs string, number, number, number
2878bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2879 SMLoc DirectiveLoc) {
2880 return TokError("unsupported directive '" + Directive + "'");
2881}
2882
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002883/// ParseDirectiveCFISections
2884/// ::= .cfi_sections section [, section]
2885bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2886 SMLoc DirectiveLoc) {
2887 StringRef Name;
2888 bool EH = false;
2889 bool Debug = false;
2890
2891 if (getParser().ParseIdentifier(Name))
2892 return TokError("Expected an identifier");
2893
2894 if (Name == ".eh_frame")
2895 EH = true;
2896 else if (Name == ".debug_frame")
2897 Debug = true;
2898
2899 if (getLexer().is(AsmToken::Comma)) {
2900 Lex();
2901
2902 if (getParser().ParseIdentifier(Name))
2903 return TokError("Expected an identifier");
2904
2905 if (Name == ".eh_frame")
2906 EH = true;
2907 else if (Name == ".debug_frame")
2908 Debug = true;
2909 }
2910
2911 getStreamer().EmitCFISections(EH, Debug);
2912
2913 return false;
2914}
2915
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002916/// ParseDirectiveCFIStartProc
2917/// ::= .cfi_startproc
2918bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2919 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002920 getStreamer().EmitCFIStartProc();
2921 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002922}
2923
2924/// ParseDirectiveCFIEndProc
2925/// ::= .cfi_endproc
2926bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002927 getStreamer().EmitCFIEndProc();
2928 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002929}
2930
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002931/// ParseRegisterOrRegisterNumber - parse register name or number.
2932bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2933 SMLoc DirectiveLoc) {
2934 unsigned RegNo;
2935
Jim Grosbach6f888a82011-06-02 17:14:04 +00002936 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002937 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2938 DirectiveLoc))
2939 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002940 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002941 } else
2942 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002943
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002944 return false;
2945}
2946
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002947/// ParseDirectiveCFIDefCfa
2948/// ::= .cfi_def_cfa register, offset
2949bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2950 SMLoc DirectiveLoc) {
2951 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002952 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002953 return true;
2954
2955 if (getLexer().isNot(AsmToken::Comma))
2956 return TokError("unexpected token in directive");
2957 Lex();
2958
2959 int64_t Offset = 0;
2960 if (getParser().ParseAbsoluteExpression(Offset))
2961 return true;
2962
Rafael Espindola066c2f42011-04-12 23:59:07 +00002963 getStreamer().EmitCFIDefCfa(Register, Offset);
2964 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002965}
2966
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002967/// ParseDirectiveCFIDefCfaOffset
2968/// ::= .cfi_def_cfa_offset offset
2969bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2970 SMLoc DirectiveLoc) {
2971 int64_t Offset = 0;
2972 if (getParser().ParseAbsoluteExpression(Offset))
2973 return true;
2974
Rafael Espindola066c2f42011-04-12 23:59:07 +00002975 getStreamer().EmitCFIDefCfaOffset(Offset);
2976 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002977}
2978
2979/// ParseDirectiveCFIAdjustCfaOffset
2980/// ::= .cfi_adjust_cfa_offset adjustment
2981bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2982 SMLoc DirectiveLoc) {
2983 int64_t Adjustment = 0;
2984 if (getParser().ParseAbsoluteExpression(Adjustment))
2985 return true;
2986
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002987 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2988 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002989}
2990
2991/// ParseDirectiveCFIDefCfaRegister
2992/// ::= .cfi_def_cfa_register register
2993bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2994 SMLoc DirectiveLoc) {
2995 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002996 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002997 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002998
Rafael Espindola066c2f42011-04-12 23:59:07 +00002999 getStreamer().EmitCFIDefCfaRegister(Register);
3000 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003001}
3002
3003/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003004/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003005bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
3006 int64_t Register = 0;
3007 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003008
3009 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003010 return true;
3011
3012 if (getLexer().isNot(AsmToken::Comma))
3013 return TokError("unexpected token in directive");
3014 Lex();
3015
3016 if (getParser().ParseAbsoluteExpression(Offset))
3017 return true;
3018
Rafael Espindola066c2f42011-04-12 23:59:07 +00003019 getStreamer().EmitCFIOffset(Register, Offset);
3020 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003021}
3022
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003023/// ParseDirectiveCFIRelOffset
3024/// ::= .cfi_rel_offset register, offset
3025bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
3026 SMLoc DirectiveLoc) {
3027 int64_t Register = 0;
3028
3029 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3030 return true;
3031
3032 if (getLexer().isNot(AsmToken::Comma))
3033 return TokError("unexpected token in directive");
3034 Lex();
3035
3036 int64_t Offset = 0;
3037 if (getParser().ParseAbsoluteExpression(Offset))
3038 return true;
3039
Rafael Espindola25f492e2011-04-12 16:12:03 +00003040 getStreamer().EmitCFIRelOffset(Register, Offset);
3041 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003042}
3043
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003044static bool isValidEncoding(int64_t Encoding) {
3045 if (Encoding & ~0xff)
3046 return false;
3047
3048 if (Encoding == dwarf::DW_EH_PE_omit)
3049 return true;
3050
3051 const unsigned Format = Encoding & 0xf;
3052 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3053 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3054 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3055 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3056 return false;
3057
Rafael Espindolacaf11582010-12-29 04:31:26 +00003058 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003059 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00003060 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003061 return false;
3062
3063 return true;
3064}
3065
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003066/// ParseDirectiveCFIPersonalityOrLsda
3067/// ::= .cfi_personality encoding, [symbol_name]
3068/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003069bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003070 SMLoc DirectiveLoc) {
3071 int64_t Encoding = 0;
3072 if (getParser().ParseAbsoluteExpression(Encoding))
3073 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003074 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003075 return false;
3076
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003077 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003078 return TokError("unsupported encoding.");
3079
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003080 if (getLexer().isNot(AsmToken::Comma))
3081 return TokError("unexpected token in directive");
3082 Lex();
3083
3084 StringRef Name;
3085 if (getParser().ParseIdentifier(Name))
3086 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003087
3088 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3089
3090 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00003091 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003092 else {
3093 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00003094 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003095 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00003096 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003097}
3098
Rafael Espindolafe024d02010-12-28 18:36:23 +00003099/// ParseDirectiveCFIRememberState
3100/// ::= .cfi_remember_state
3101bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
3102 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003103 getStreamer().EmitCFIRememberState();
3104 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003105}
3106
3107/// ParseDirectiveCFIRestoreState
3108/// ::= .cfi_remember_state
3109bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
3110 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003111 getStreamer().EmitCFIRestoreState();
3112 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003113}
3114
Rafael Espindolac5754392011-04-12 15:31:05 +00003115/// ParseDirectiveCFISameValue
3116/// ::= .cfi_same_value register
3117bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
3118 SMLoc DirectiveLoc) {
3119 int64_t Register = 0;
3120
3121 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3122 return true;
3123
3124 getStreamer().EmitCFISameValue(Register);
3125
3126 return false;
3127}
3128
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003129/// ParseDirectiveCFIRestore
3130/// ::= .cfi_restore register
3131bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003132 SMLoc DirectiveLoc) {
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003133 int64_t Register = 0;
3134 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3135 return true;
3136
3137 getStreamer().EmitCFIRestore(Register);
3138
3139 return false;
3140}
3141
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003142/// ParseDirectiveCFIEscape
3143/// ::= .cfi_escape expression[,...]
3144bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003145 SMLoc DirectiveLoc) {
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003146 std::string Values;
3147 int64_t CurrValue;
3148 if (getParser().ParseAbsoluteExpression(CurrValue))
3149 return true;
3150
3151 Values.push_back((uint8_t)CurrValue);
3152
3153 while (getLexer().is(AsmToken::Comma)) {
3154 Lex();
3155
3156 if (getParser().ParseAbsoluteExpression(CurrValue))
3157 return true;
3158
3159 Values.push_back((uint8_t)CurrValue);
3160 }
3161
3162 getStreamer().EmitCFIEscape(Values);
3163 return false;
3164}
3165
Rafael Espindola16d7d432012-01-23 21:51:52 +00003166/// ParseDirectiveCFISignalFrame
3167/// ::= .cfi_signal_frame
3168bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
3169 SMLoc DirectiveLoc) {
3170 if (getLexer().isNot(AsmToken::EndOfStatement))
3171 return Error(getLexer().getLoc(),
3172 "unexpected token in '" + Directive + "' directive");
3173
3174 getStreamer().EmitCFISignalFrame();
3175
3176 return false;
3177}
3178
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003179/// ParseDirectiveMacrosOnOff
3180/// ::= .macros_on
3181/// ::= .macros_off
3182bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
3183 SMLoc DirectiveLoc) {
3184 if (getLexer().isNot(AsmToken::EndOfStatement))
3185 return Error(getLexer().getLoc(),
3186 "unexpected token in '" + Directive + "' directive");
3187
3188 getParser().MacrosEnabled = Directive == ".macros_on";
3189
3190 return false;
3191}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003192
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003193/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00003194/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003195bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
3196 SMLoc DirectiveLoc) {
3197 StringRef Name;
3198 if (getParser().ParseIdentifier(Name))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003199 return TokError("expected identifier in '.macro' directive");
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003200
Rafael Espindola8a403d32012-08-08 14:51:03 +00003201 MacroParameters Parameters;
Preston Gurd7b6f2032012-09-19 20:36:12 +00003202 // Argument delimiter is initially unknown. It will be set by
3203 // ParseMacroArgument()
3204 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindola65366442011-06-05 02:43:45 +00003205 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Rafael Espindola7996d042012-08-21 16:06:48 +00003206 for (;;) {
3207 MacroParameter Parameter;
Preston Gurd6c9176a2012-09-19 20:29:04 +00003208 if (getParser().ParseIdentifier(Parameter.first))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003209 return TokError("expected identifier in '.macro' directive");
Preston Gurd6c9176a2012-09-19 20:29:04 +00003210
3211 if (getLexer().is(AsmToken::Equal)) {
3212 Lex();
Preston Gurd7b6f2032012-09-19 20:36:12 +00003213 if (getParser().ParseMacroArgument(Parameter.second, ArgumentDelimiter))
Preston Gurd6c9176a2012-09-19 20:29:04 +00003214 return true;
3215 }
3216
Rafael Espindola65366442011-06-05 02:43:45 +00003217 Parameters.push_back(Parameter);
3218
Preston Gurd7b6f2032012-09-19 20:36:12 +00003219 if (getLexer().is(AsmToken::Comma))
3220 Lex();
3221 else if (getLexer().is(AsmToken::EndOfStatement))
Rafael Espindola65366442011-06-05 02:43:45 +00003222 break;
Rafael Espindola65366442011-06-05 02:43:45 +00003223 }
3224 }
3225
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003226 // Eat the end of statement.
3227 Lex();
3228
3229 AsmToken EndToken, StartToken = getTok();
3230
3231 // Lex the macro definition.
3232 for (;;) {
3233 // Check whether we have reached the end of the file.
3234 if (getLexer().is(AsmToken::Eof))
3235 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3236
3237 // Otherwise, check whether we have reach the .endmacro.
3238 if (getLexer().is(AsmToken::Identifier) &&
3239 (getTok().getIdentifier() == ".endm" ||
3240 getTok().getIdentifier() == ".endmacro")) {
3241 EndToken = getTok();
3242 Lex();
3243 if (getLexer().isNot(AsmToken::EndOfStatement))
3244 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3245 "' directive");
3246 break;
3247 }
3248
3249 // Otherwise, scan til the end of the statement.
3250 getParser().EatToEndOfStatement();
3251 }
3252
3253 if (getParser().MacroMap.lookup(Name)) {
3254 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3255 }
3256
3257 const char *BodyStart = StartToken.getLoc().getPointer();
3258 const char *BodyEnd = EndToken.getLoc().getPointer();
3259 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00003260 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003261 return false;
3262}
3263
3264/// ParseDirectiveEndMacro
3265/// ::= .endm
3266/// ::= .endmacro
3267bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
Rafael Espindola8a403d32012-08-08 14:51:03 +00003268 SMLoc DirectiveLoc) {
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003269 if (getLexer().isNot(AsmToken::EndOfStatement))
3270 return TokError("unexpected token in '" + Directive + "' directive");
3271
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003272 // If we are inside a macro instantiation, terminate the current
3273 // instantiation.
3274 if (!getParser().ActiveMacros.empty()) {
3275 getParser().HandleMacroExit();
3276 return false;
3277 }
3278
3279 // Otherwise, this .endmacro is a stray entry in the file; well formed
3280 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003281 return TokError("unexpected '" + Directive + "' in file, "
3282 "no current macro definition");
3283}
3284
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003285/// ParseDirectivePurgeMacro
3286/// ::= .purgem
3287bool GenericAsmParser::ParseDirectivePurgeMacro(StringRef Directive,
3288 SMLoc DirectiveLoc) {
3289 StringRef Name;
3290 if (getParser().ParseIdentifier(Name))
3291 return TokError("expected identifier in '.purgem' directive");
3292
3293 if (getLexer().isNot(AsmToken::EndOfStatement))
3294 return TokError("unexpected token in '.purgem' directive");
3295
3296 StringMap<Macro*>::iterator I = getParser().MacroMap.find(Name);
3297 if (I == getParser().MacroMap.end())
3298 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3299
3300 // Undefine the macro.
3301 delete I->getValue();
3302 getParser().MacroMap.erase(I);
3303 return false;
3304}
3305
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003306bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00003307 getParser().CheckForValidSection();
3308
3309 const MCExpr *Value;
3310
3311 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003312 return true;
3313
3314 if (getLexer().isNot(AsmToken::EndOfStatement))
3315 return TokError("unexpected token in directive");
3316
3317 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00003318 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003319 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00003320 getStreamer().EmitULEB128Value(Value);
3321
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003322 return false;
3323}
3324
Rafael Espindola761cb062012-06-03 23:57:14 +00003325Macro *AsmParser::ParseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003326 AsmToken EndToken, StartToken = getTok();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003327
Rafael Espindola761cb062012-06-03 23:57:14 +00003328 unsigned NestLevel = 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003329 for (;;) {
3330 // Check whether we have reached the end of the file.
Rafael Espindola761cb062012-06-03 23:57:14 +00003331 if (getLexer().is(AsmToken::Eof)) {
3332 Error(DirectiveLoc, "no matching '.endr' in definition");
3333 return 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003334 }
3335
Rafael Espindola761cb062012-06-03 23:57:14 +00003336 if (Lexer.is(AsmToken::Identifier) &&
3337 (getTok().getIdentifier() == ".rept")) {
3338 ++NestLevel;
3339 }
3340
3341 // Otherwise, check whether we have reached the .endr.
3342 if (Lexer.is(AsmToken::Identifier) &&
3343 getTok().getIdentifier() == ".endr") {
3344 if (NestLevel == 0) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003345 EndToken = getTok();
3346 Lex();
Rafael Espindola761cb062012-06-03 23:57:14 +00003347 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3348 TokError("unexpected token in '.endr' directive");
3349 return 0;
3350 }
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003351 break;
3352 }
Rafael Espindola761cb062012-06-03 23:57:14 +00003353 --NestLevel;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003354 }
3355
Rafael Espindola761cb062012-06-03 23:57:14 +00003356 // Otherwise, scan till the end of the statement.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003357 EatToEndOfStatement();
3358 }
3359
3360 const char *BodyStart = StartToken.getLoc().getPointer();
3361 const char *BodyEnd = EndToken.getLoc().getPointer();
3362 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3363
Rafael Espindola761cb062012-06-03 23:57:14 +00003364 // We Are Anonymous.
3365 StringRef Name;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003366 MacroParameters Parameters;
Rafael Espindola761cb062012-06-03 23:57:14 +00003367 return new Macro(Name, Body, Parameters);
3368}
3369
3370void AsmParser::InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
3371 raw_svector_ostream &OS) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003372 OS << ".endr\n";
3373
3374 MemoryBuffer *Instantiation =
3375 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
3376
Rafael Espindola761cb062012-06-03 23:57:14 +00003377 // Create the macro instantiation object and add to the current macro
3378 // instantiation stack.
3379 MacroInstantiation *MI = new MacroInstantiation(M, DirectiveLoc,
3380 getTok().getLoc(),
3381 Instantiation);
3382 ActiveMacros.push_back(MI);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003383
Rafael Espindola761cb062012-06-03 23:57:14 +00003384 // Jump to the macro instantiation and prime the lexer.
3385 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3386 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3387 Lex();
3388}
3389
3390bool AsmParser::ParseDirectiveRept(SMLoc DirectiveLoc) {
3391 int64_t Count;
3392 if (ParseAbsoluteExpression(Count))
3393 return TokError("unexpected token in '.rept' directive");
3394
3395 if (Count < 0)
3396 return TokError("Count is negative");
3397
3398 if (Lexer.isNot(AsmToken::EndOfStatement))
3399 return TokError("unexpected token in '.rept' directive");
3400
3401 // Eat the end of statement.
3402 Lex();
3403
3404 // Lex the rept definition.
3405 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3406 if (!M)
3407 return true;
3408
3409 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3410 // to hold the macro body with substitutions.
3411 SmallString<256> Buf;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003412 MacroParameters Parameters;
3413 MacroArguments A;
Rafael Espindola761cb062012-06-03 23:57:14 +00003414 raw_svector_ostream OS(Buf);
3415 while (Count--) {
3416 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3417 return true;
3418 }
3419 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003420
3421 return false;
3422}
3423
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003424/// ParseDirectiveIrp
3425/// ::= .irp symbol,values
3426bool AsmParser::ParseDirectiveIrp(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003427 MacroParameters Parameters;
3428 MacroParameter Parameter;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003429
Preston Gurd6c9176a2012-09-19 20:29:04 +00003430 if (ParseIdentifier(Parameter.first))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003431 return TokError("expected identifier in '.irp' directive");
3432
3433 Parameters.push_back(Parameter);
3434
3435 if (Lexer.isNot(AsmToken::Comma))
3436 return TokError("expected comma in '.irp' directive");
3437
3438 Lex();
3439
Rafael Espindola8a403d32012-08-08 14:51:03 +00003440 MacroArguments A;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003441 if (ParseMacroArguments(0, A))
3442 return true;
3443
3444 // Eat the end of statement.
3445 Lex();
3446
3447 // Lex the irp definition.
3448 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3449 if (!M)
3450 return true;
3451
3452 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3453 // to hold the macro body with substitutions.
3454 SmallString<256> Buf;
3455 raw_svector_ostream OS(Buf);
3456
Rafael Espindola7996d042012-08-21 16:06:48 +00003457 for (MacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
3458 MacroArguments Args;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003459 Args.push_back(*i);
3460
3461 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3462 return true;
3463 }
3464
3465 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3466
3467 return false;
3468}
3469
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003470/// ParseDirectiveIrpc
3471/// ::= .irpc symbol,values
3472bool AsmParser::ParseDirectiveIrpc(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003473 MacroParameters Parameters;
3474 MacroParameter Parameter;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003475
Preston Gurd6c9176a2012-09-19 20:29:04 +00003476 if (ParseIdentifier(Parameter.first))
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003477 return TokError("expected identifier in '.irpc' directive");
3478
3479 Parameters.push_back(Parameter);
3480
3481 if (Lexer.isNot(AsmToken::Comma))
3482 return TokError("expected comma in '.irpc' directive");
3483
3484 Lex();
3485
Rafael Espindola8a403d32012-08-08 14:51:03 +00003486 MacroArguments A;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003487 if (ParseMacroArguments(0, A))
3488 return true;
3489
3490 if (A.size() != 1 || A.front().size() != 1)
3491 return TokError("unexpected token in '.irpc' directive");
3492
3493 // Eat the end of statement.
3494 Lex();
3495
3496 // Lex the irpc definition.
3497 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3498 if (!M)
3499 return true;
3500
3501 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3502 // to hold the macro body with substitutions.
3503 SmallString<256> Buf;
3504 raw_svector_ostream OS(Buf);
3505
3506 StringRef Values = A.front().front().getString();
3507 std::size_t I, End = Values.size();
3508 for (I = 0; I < End; ++I) {
3509 MacroArgument Arg;
3510 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I+1)));
3511
Rafael Espindola8a403d32012-08-08 14:51:03 +00003512 MacroArguments Args;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003513 Args.push_back(Arg);
3514
3515 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3516 return true;
3517 }
3518
3519 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3520
3521 return false;
3522}
3523
Rafael Espindola761cb062012-06-03 23:57:14 +00003524bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) {
3525 if (ActiveMacros.empty())
Preston Gurd6579eea2012-09-19 20:23:43 +00003526 return TokError("unmatched '.endr' directive");
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003527
3528 // The only .repl that should get here are the ones created by
Rafael Espindola761cb062012-06-03 23:57:14 +00003529 // InstantiateMacroLikeBody.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003530 assert(getLexer().is(AsmToken::EndOfStatement));
3531
Rafael Espindola761cb062012-06-03 23:57:14 +00003532 HandleMacroExit();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003533 return false;
3534}
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003535
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003536/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003537MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003538 MCContext &C, MCStreamer &Out,
3539 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003540 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003541}