blob: 8bf017a1a0ad960981ac0b33d4f90ab8a466057b [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
Chad Rosier84125ca2012-10-13 00:26:04 +0000136 /// ParsingInlineAsm - are we parsing ms-style inline assembly?
137 bool ParsingInlineAsm;
138
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000139public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000140 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000141 const MCAsmInfo &MAI);
Craig Topper345d16d2012-08-29 05:48:09 +0000142 virtual ~AsmParser();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000143
144 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
145
Craig Topper345d16d2012-08-29 05:48:09 +0000146 virtual void AddDirectiveHandler(MCAsmParserExtension *Object,
147 StringRef Directive,
148 DirectiveHandler Handler) {
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000149 DirectiveMap[Directive] = std::make_pair(Object, Handler);
150 }
151
152public:
153 /// @name MCAsmParser Interface
154 /// {
155
156 virtual SourceMgr &getSourceManager() { return SrcMgr; }
157 virtual MCAsmLexer &getLexer() { return Lexer; }
158 virtual MCContext &getContext() { return Ctx; }
159 virtual MCStreamer &getStreamer() { return Out; }
Devang Patel0db58bf2012-01-31 18:14:05 +0000160 virtual unsigned getAssemblerDialect() {
161 if (AssemblerDialect == ~0U)
162 return MAI.getAssemblerDialect();
163 else
164 return AssemblerDialect;
165 }
166 virtual void setAssemblerDialect(unsigned i) {
167 AssemblerDialect = i;
168 }
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000169
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000170 virtual bool Warning(SMLoc L, const Twine &Msg,
171 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
172 virtual bool Error(SMLoc L, const Twine &Msg,
173 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000174
Craig Topper345d16d2012-08-29 05:48:09 +0000175 virtual const AsmToken &Lex();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000176
Chad Rosier84125ca2012-10-13 00:26:04 +0000177 void setParsingInlineAsm(bool V) { ParsingInlineAsm = V; }
178
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000179 bool ParseExpression(const MCExpr *&Res);
180 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
181 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
182 virtual bool ParseAbsoluteExpression(int64_t &Res);
183
184 /// }
185
186private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000187 void CheckForValidSection();
188
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000189 bool ParseStatement();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000190 void EatToEndOfLine();
191 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000192
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000193 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola761cb062012-06-03 23:57:14 +0000194 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola8a403d32012-08-08 14:51:03 +0000195 const MacroParameters &Parameters,
196 const MacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +0000197 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000198 void HandleMacroExit();
199
200 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000201 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattner462b43c2011-10-16 05:47:55 +0000202 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
203 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000204 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000205 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000206
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000207 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
208 bool EnterIncludeFile(const std::string &Filename);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000209 /// ProcessIncbinFile - Process the specified file for the .incbin directive.
210 /// This returns true on failure.
211 bool ProcessIncbinFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000212
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +0000213 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000214 /// current token is not set; clients should ensure Lex() is called
215 /// subsequently.
216 void JumpToLoc(SMLoc Loc);
217
Craig Topper345d16d2012-08-29 05:48:09 +0000218 virtual void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000219
Preston Gurd7b6f2032012-09-19 20:36:12 +0000220 bool ParseMacroArgument(MacroArgument &MA,
221 AsmToken::TokenKind &ArgumentDelimiter);
Rafael Espindola8a403d32012-08-08 14:51:03 +0000222 bool ParseMacroArguments(const Macro *M, MacroArguments &A);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000223
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000224 /// \brief Parse up to the end of statement and a return the contents from the
225 /// current token until the end of the statement; the current token on exit
226 /// will be either the EndOfStatement or EOF.
Craig Topper345d16d2012-08-29 05:48:09 +0000227 virtual StringRef ParseStringToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000228
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000229 /// \brief Parse until the end of a statement or a comma is encountered,
230 /// return the contents from the current token up to the end or comma.
231 StringRef ParseStringToComma();
232
Jim Grosbach3f90a4c2012-09-13 23:11:31 +0000233 bool ParseAssignment(StringRef Name, bool allow_redef,
234 bool NoDeadStrip = false);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000235
236 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
237 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
238 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000239 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000240
241 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +0000242 /// and set \p Res to the identifier contents.
Craig Topper345d16d2012-08-29 05:48:09 +0000243 virtual bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000244
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000245 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000246
247 // ".ascii", ".asciiz", ".string"
248 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000249 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000250 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000251 bool ParseDirectiveFill(); // ".fill"
252 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000253 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000254 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000255 bool ParseDirectiveOrg(); // ".org"
256 // ".align{,32}", ".p2align{,w,l}"
257 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
258
259 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
260 /// accepts a single symbol (which should be a label or an external).
261 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000262
263 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
264
265 bool ParseDirectiveAbort(); // ".abort"
266 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000267 bool ParseDirectiveIncbin(); // ".incbin"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000268
269 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +0000270 // ".ifb" or ".ifnb", depending on ExpectBlank.
271 bool ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000272 // ".ifc" or ".ifnc", depending on ExpectEqual.
273 bool ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000274 // ".ifdef" or ".ifndef", depending on expect_defined
275 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000276 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
277 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
278 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
279
280 /// ParseEscapedString - Parse the current token as a string which may include
281 /// escaped characters and return the string contents.
282 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000283
284 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
285 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola2ec304c2012-05-12 16:31:10 +0000286
Rafael Espindola761cb062012-06-03 23:57:14 +0000287 // Macro-like directives
288 Macro *ParseMacroLikeBody(SMLoc DirectiveLoc);
289 void InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
290 raw_svector_ostream &OS);
291 bool ParseDirectiveRept(SMLoc DirectiveLoc); // ".rept"
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000292 bool ParseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
Rafael Espindolafc9216e2012-06-16 18:03:25 +0000293 bool ParseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
Rafael Espindola761cb062012-06-03 23:57:14 +0000294 bool ParseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000295};
296
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000297/// \brief Generic implementations of directive handling, etc. which is shared
298/// (or the default, at least) for all assembler parser.
299class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000300 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
301 void AddDirectiveHandler(StringRef Directive) {
302 getParser().AddDirectiveHandler(this, Directive,
303 HandleDirective<GenericAsmParser, Handler>);
304 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000305public:
306 GenericAsmParser() {}
307
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000308 AsmParser &getParser() {
309 return (AsmParser&) this->MCAsmParserExtension::getParser();
310 }
311
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000312 virtual void Initialize(MCAsmParser &Parser) {
313 // Call the base implementation.
314 this->MCAsmParserExtension::Initialize(Parser);
315
316 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000317 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
318 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
319 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000320 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000321
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000322 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000323 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
324 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000325 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
326 ".cfi_startproc");
327 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
328 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000329 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
330 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000331 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
332 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000333 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
334 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000335 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
336 ".cfi_def_cfa_register");
337 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
338 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000339 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
340 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000341 AddDirectiveHandler<
342 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
343 AddDirectiveHandler<
344 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000345 AddDirectiveHandler<
346 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
347 AddDirectiveHandler<
348 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000349 AddDirectiveHandler<
350 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000351 AddDirectiveHandler<
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000352 &GenericAsmParser::ParseDirectiveCFIRestore>(".cfi_restore");
353 AddDirectiveHandler<
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000354 &GenericAsmParser::ParseDirectiveCFIEscape>(".cfi_escape");
Rafael Espindola16d7d432012-01-23 21:51:52 +0000355 AddDirectiveHandler<
356 &GenericAsmParser::ParseDirectiveCFISignalFrame>(".cfi_signal_frame");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000357
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000358 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000359 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
360 ".macros_on");
361 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
362 ".macros_off");
363 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
364 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
365 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000366 AddDirectiveHandler<&GenericAsmParser::ParseDirectivePurgeMacro>(".purgem");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000367
368 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
369 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000370 }
371
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000372 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
373
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000374 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
375 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
376 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000377 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000378 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000379 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
380 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000381 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000382 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000383 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000384 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
385 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000386 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000387 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000388 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
389 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000390 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000391 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000392 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
Rafael Espindola16d7d432012-01-23 21:51:52 +0000393 bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000394
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000395 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000396 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
397 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000398 bool ParseDirectivePurgeMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000399
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000400 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000401};
402
403}
404
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000405namespace llvm {
406
407extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000408extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000409extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000410
411}
412
Chris Lattneraaec2052010-01-19 19:46:13 +0000413enum { DEFAULT_ADDRSPACE = 0 };
414
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000415AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000416 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000417 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000418 GenericParser(new GenericAsmParser), PlatformParser(0),
Preston Gurd7b6f2032012-09-19 20:36:12 +0000419 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0),
Chad Rosier84125ca2012-10-13 00:26:04 +0000420 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000421 // Save the old handler.
422 SavedDiagHandler = SrcMgr.getDiagHandler();
423 SavedDiagContext = SrcMgr.getDiagContext();
424 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000425 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000426 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000427
428 // Initialize the generic parser.
429 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000430
431 // Initialize the platform / file format parser.
432 //
433 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
434 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000435 if (_MAI.hasMicrosoftFastStdCallMangling()) {
436 PlatformParser = createCOFFAsmParser();
437 PlatformParser->Initialize(*this);
438 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000439 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000440 PlatformParser->Initialize(*this);
Preston Gurd7b6f2032012-09-19 20:36:12 +0000441 IsDarwin = true;
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000442 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000443 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000444 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000445 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000446}
447
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000448AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000449 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
450
451 // Destroy any macros.
452 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
453 ie = MacroMap.end(); it != ie; ++it)
454 delete it->getValue();
455
Daniel Dunbare4749702010-07-12 18:12:02 +0000456 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000457 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000458}
459
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000460void AsmParser::PrintMacroInstantiations() {
461 // Print the active macro instantiation stack.
462 for (std::vector<MacroInstantiation*>::const_reverse_iterator
463 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000464 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
465 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000466}
467
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000468bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000469 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000470 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000471 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000472 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000473 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000474}
475
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000476bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000477 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000478 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000479 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000480 return true;
481}
482
Sean Callananfd0b0282010-01-21 00:19:58 +0000483bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000484 std::string IncludedFile;
485 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000486 if (NewBuf == -1)
487 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000488
Sean Callananfd0b0282010-01-21 00:19:58 +0000489 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000490
Sean Callananfd0b0282010-01-21 00:19:58 +0000491 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000492
Sean Callananfd0b0282010-01-21 00:19:58 +0000493 return false;
494}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000495
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000496/// Process the specified .incbin file by seaching for it in the include paths
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000497/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000498/// returns true on failure.
499bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
500 std::string IncludedFile;
501 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
502 if (NewBuf == -1)
503 return true;
504
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000505 // Pick up the bytes from the file and emit them.
Kevin Enderbydac29532011-12-15 00:00:27 +0000506 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
507 DEFAULT_ADDRSPACE);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000508 return false;
509}
510
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000511void AsmParser::JumpToLoc(SMLoc Loc) {
512 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
513 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
514}
515
Sean Callananfd0b0282010-01-21 00:19:58 +0000516const AsmToken &AsmParser::Lex() {
517 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000518
Sean Callananfd0b0282010-01-21 00:19:58 +0000519 if (tok->is(AsmToken::Eof)) {
520 // If this is the end of an included file, pop the parent file off the
521 // include stack.
522 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
523 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000524 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000525 tok = &Lexer.Lex();
526 }
527 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000528
Sean Callananfd0b0282010-01-21 00:19:58 +0000529 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000530 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000531
Sean Callananfd0b0282010-01-21 00:19:58 +0000532 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000533}
534
Chris Lattner79180e22010-04-05 23:15:42 +0000535bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000536 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000537 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000538 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000539
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000540 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000541 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000542
543 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000544 AsmCond StartingCondState = TheCondState;
545
Kevin Enderby613b7572011-11-01 22:27:22 +0000546 // If we are generating dwarf for assembly source files save the initial text
547 // section and generate a .file directive.
548 if (getContext().getGenDwarfForAssembly()) {
549 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000550 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
551 getStreamer().EmitLabel(SectionStartSym);
552 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000553 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
554 StringRef(), SrcMgr.getMemoryBuffer(CurBuffer)->getBufferIdentifier());
555 }
556
Chris Lattnerb717fb02009-07-02 21:53:43 +0000557 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000558 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000559 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000560
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000561 // We had an error, validate that one was emitted and recover by skipping to
562 // the next line.
563 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000564 EatToEndOfStatement();
565 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000566
567 if (TheCondState.TheCond != StartingCondState.TheCond ||
568 TheCondState.Ignore != StartingCondState.Ignore)
569 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000570
571 // Check to see there are no empty DwarfFile slots.
572 const std::vector<MCDwarfFile *> &MCDwarfFiles =
573 getContext().getMCDwarfFiles();
574 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000575 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000576 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000577 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000578
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000579 // Check to see that all assembler local symbols were actually defined.
580 // Targets that don't do subsections via symbols may not want this, though,
581 // so conservatively exclude them. Only do this if we're finalizing, though,
582 // as otherwise we won't necessarilly have seen everything yet.
583 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
584 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
585 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
586 e = Symbols.end();
587 i != e; ++i) {
588 MCSymbol *Sym = i->getValue();
589 // Variable symbols may not be marked as defined, so check those
590 // explicitly. If we know it's a variable, we have a definition for
591 // the purposes of this check.
592 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
593 // FIXME: We would really like to refer back to where the symbol was
594 // first referenced for a source location. We need to add something
595 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000596 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
597 "assembler local symbol '" + Sym->getName() +
598 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000599 }
600 }
601
602
Chris Lattner79180e22010-04-05 23:15:42 +0000603 // Finalize the output stream if there are no errors and if the client wants
604 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000605 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000606 Out.Finish();
607
Chris Lattnerb717fb02009-07-02 21:53:43 +0000608 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000609}
610
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000611void AsmParser::CheckForValidSection() {
Chad Rosier84125ca2012-10-13 00:26:04 +0000612 if (!ParsingInlineAsm && !getStreamer().getCurrentSection()) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000613 TokError("expected section directive before assembly directive");
614 Out.SwitchSection(Ctx.getMachOSection(
615 "__TEXT", "__text",
616 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
617 0, SectionKind::getText()));
618 }
619}
620
Chris Lattner2cf5f142009-06-22 01:29:09 +0000621/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
622void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000623 while (Lexer.isNot(AsmToken::EndOfStatement) &&
624 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000625 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000626
Chris Lattner2cf5f142009-06-22 01:29:09 +0000627 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000628 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000629 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000630}
631
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000632StringRef AsmParser::ParseStringToEndOfStatement() {
633 const char *Start = getTok().getLoc().getPointer();
634
635 while (Lexer.isNot(AsmToken::EndOfStatement) &&
636 Lexer.isNot(AsmToken::Eof))
637 Lex();
638
639 const char *End = getTok().getLoc().getPointer();
640 return StringRef(Start, End - Start);
641}
Chris Lattnerc4193832009-06-22 05:51:26 +0000642
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000643StringRef AsmParser::ParseStringToComma() {
644 const char *Start = getTok().getLoc().getPointer();
645
646 while (Lexer.isNot(AsmToken::EndOfStatement) &&
647 Lexer.isNot(AsmToken::Comma) &&
648 Lexer.isNot(AsmToken::Eof))
649 Lex();
650
651 const char *End = getTok().getLoc().getPointer();
652 return StringRef(Start, End - Start);
653}
654
Chris Lattner74ec1a32009-06-22 06:32:03 +0000655/// ParseParenExpr - Parse a paren expression and return it.
656/// NOTE: This assumes the leading '(' has already been consumed.
657///
658/// parenexpr ::= expr)
659///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000660bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000661 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000662 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000663 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000664 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000665 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000666 return false;
667}
Chris Lattnerc4193832009-06-22 05:51:26 +0000668
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000669/// ParseBracketExpr - Parse a bracket expression and return it.
670/// NOTE: This assumes the leading '[' has already been consumed.
671///
672/// bracketexpr ::= expr]
673///
674bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
675 if (ParseExpression(Res)) return true;
676 if (Lexer.isNot(AsmToken::RBrac))
677 return TokError("expected ']' in brackets expression");
678 EndLoc = Lexer.getLoc();
679 Lex();
680 return false;
681}
682
Chris Lattner74ec1a32009-06-22 06:32:03 +0000683/// ParsePrimaryExpr - Parse a primary expression and return it.
684/// primaryexpr ::= (parenexpr
685/// primaryexpr ::= symbol
686/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000687/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000688/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000689bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000690 switch (Lexer.getKind()) {
691 default:
692 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000693 // If we have an error assume that we've already handled it.
694 case AsmToken::Error:
695 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000696 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000697 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000698 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000699 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000700 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000701 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000702 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000703 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000704 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000705 EndLoc = Lexer.getLoc();
706
707 StringRef Identifier;
708 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000709 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000710
Daniel Dunbarfffff912009-10-16 01:34:54 +0000711 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000712 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000713 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000714
715 // Lookup the symbol variant if used.
716 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000717 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000718 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000719 if (Variant == MCSymbolRefExpr::VK_Invalid) {
720 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000721 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000722 }
723 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000724
Daniel Dunbarfffff912009-10-16 01:34:54 +0000725 // If this is an absolute variable reference, substitute it now to preserve
726 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000727 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000728 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000729 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000730
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000731 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000732 return false;
733 }
734
735 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000736 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000737 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000738 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000739 case AsmToken::Integer: {
740 SMLoc Loc = getTok().getLoc();
741 int64_t IntVal = getTok().getIntVal();
742 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000743 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000744 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000745 // Look for 'b' or 'f' following an Integer as a directional label
746 if (Lexer.getKind() == AsmToken::Identifier) {
747 StringRef IDVal = getTok().getString();
748 if (IDVal == "f" || IDVal == "b"){
749 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
750 IDVal == "f" ? 1 : 0);
751 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
752 getContext());
Benjamin Kramer29739e72012-05-12 16:52:21 +0000753 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000754 return Error(Loc, "invalid reference to undefined symbol");
755 EndLoc = Lexer.getLoc();
756 Lex(); // Eat identifier.
757 }
758 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000759 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000760 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000761 case AsmToken::Real: {
762 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000763 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000764 Res = MCConstantExpr::Create(IntVal, getContext());
765 Lex(); // Eat token.
766 return false;
767 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000768 case AsmToken::Dot: {
769 // This is a '.' reference, which references the current PC. Emit a
770 // temporary label to the streamer and refer to it.
771 MCSymbol *Sym = Ctx.CreateTempSymbol();
772 Out.EmitLabel(Sym);
773 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
774 EndLoc = Lexer.getLoc();
775 Lex(); // Eat identifier.
776 return false;
777 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000778 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000779 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000780 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000781 case AsmToken::LBrac:
782 if (!PlatformParser->HasBracketExpressions())
783 return TokError("brackets expression not supported on this target");
784 Lex(); // Eat the '['.
785 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000786 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000787 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000788 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000789 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000790 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000791 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000792 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000793 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000794 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000795 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000796 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000797 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000798 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000799 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000800 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000801 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000802 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000803 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000804 }
805}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000806
Chris Lattnerb4307b32010-01-15 19:28:38 +0000807bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000808 SMLoc EndLoc;
809 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000810}
811
Daniel Dunbarcceba832010-09-17 02:47:07 +0000812const MCExpr *
813AsmParser::ApplyModifierToExpr(const MCExpr *E,
814 MCSymbolRefExpr::VariantKind Variant) {
815 // Recurse over the given expression, rebuilding it to apply the given variant
816 // if there is exactly one symbol.
817 switch (E->getKind()) {
818 case MCExpr::Target:
819 case MCExpr::Constant:
820 return 0;
821
822 case MCExpr::SymbolRef: {
823 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
824
825 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
826 TokError("invalid variant on expression '" +
827 getTok().getIdentifier() + "' (already modified)");
828 return E;
829 }
830
831 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
832 }
833
834 case MCExpr::Unary: {
835 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
836 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
837 if (!Sub)
838 return 0;
839 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
840 }
841
842 case MCExpr::Binary: {
843 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
844 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
845 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
846
847 if (!LHS && !RHS)
848 return 0;
849
850 if (!LHS) LHS = BE->getLHS();
851 if (!RHS) RHS = BE->getRHS();
852
853 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
854 }
855 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000856
Craig Topper85814382012-02-07 05:05:23 +0000857 llvm_unreachable("Invalid expression kind!");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000858}
859
Chris Lattner74ec1a32009-06-22 06:32:03 +0000860/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000861///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000862/// expr ::= expr &&,|| expr -> lowest.
863/// expr ::= expr |,^,&,! expr
864/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
865/// expr ::= expr <<,>> expr
866/// expr ::= expr +,- expr
867/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000868/// expr ::= primaryexpr
869///
Chris Lattner54482b42010-01-15 19:39:23 +0000870bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000871 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000872 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000873 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
874 return true;
875
Daniel Dunbarcceba832010-09-17 02:47:07 +0000876 // As a special case, we support 'a op b @ modifier' by rewriting the
877 // expression to include the modifier. This is inefficient, but in general we
878 // expect users to use 'a@modifier op b'.
879 if (Lexer.getKind() == AsmToken::At) {
880 Lex();
881
882 if (Lexer.isNot(AsmToken::Identifier))
883 return TokError("unexpected symbol modifier following '@'");
884
885 MCSymbolRefExpr::VariantKind Variant =
886 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
887 if (Variant == MCSymbolRefExpr::VK_Invalid)
888 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
889
890 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
891 if (!ModifiedRes) {
892 return TokError("invalid modifier '" + getTok().getIdentifier() +
893 "' (no symbols present)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000894 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000895
Daniel Dunbarcceba832010-09-17 02:47:07 +0000896 Res = ModifiedRes;
897 Lex();
898 }
899
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000900 // Try to constant fold it up front, if possible.
901 int64_t Value;
902 if (Res->EvaluateAsAbsolute(Value))
903 Res = MCConstantExpr::Create(Value, getContext());
904
905 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000906}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000907
Chris Lattnerb4307b32010-01-15 19:28:38 +0000908bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000909 Res = 0;
910 return ParseParenExpr(Res, EndLoc) ||
911 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000912}
913
Daniel Dunbar475839e2009-06-29 20:37:27 +0000914bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000915 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000916
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000917 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000918 if (ParseExpression(Expr))
919 return true;
920
Daniel Dunbare00b0112009-10-16 01:57:52 +0000921 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000922 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000923
924 return false;
925}
926
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000927static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000928 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000929 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000930 default:
931 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000932
Jim Grosbachfbe16812011-08-20 16:24:13 +0000933 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000934 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000935 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000936 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000937 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000938 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000939 return 1;
940
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000941
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000942 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000943 //
944 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000945 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000946 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000947 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000948 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000949 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000950 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000951 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000952 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000953 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000954
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000955 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000956 case AsmToken::EqualEqual:
957 Kind = MCBinaryExpr::EQ;
958 return 3;
959 case AsmToken::ExclaimEqual:
960 case AsmToken::LessGreater:
961 Kind = MCBinaryExpr::NE;
962 return 3;
963 case AsmToken::Less:
964 Kind = MCBinaryExpr::LT;
965 return 3;
966 case AsmToken::LessEqual:
967 Kind = MCBinaryExpr::LTE;
968 return 3;
969 case AsmToken::Greater:
970 Kind = MCBinaryExpr::GT;
971 return 3;
972 case AsmToken::GreaterEqual:
973 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000974 return 3;
975
Jim Grosbachfbe16812011-08-20 16:24:13 +0000976 // Intermediate Precedence: <<, >>
977 case AsmToken::LessLess:
978 Kind = MCBinaryExpr::Shl;
979 return 4;
980 case AsmToken::GreaterGreater:
981 Kind = MCBinaryExpr::Shr;
982 return 4;
983
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000984 // High Intermediate Precedence: +, -
985 case AsmToken::Plus:
986 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000987 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000988 case AsmToken::Minus:
989 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000990 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000991
Jim Grosbachfbe16812011-08-20 16:24:13 +0000992 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +0000993 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000994 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000995 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000996 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000997 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000998 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000999 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001000 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001001 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001002 }
1003}
1004
1005
1006/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
1007/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +00001008bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
1009 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001010 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001011 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001012 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001013
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001014 // If the next token is lower precedence than we are allowed to eat, return
1015 // successfully with what we ate already.
1016 if (TokPrec < Precedence)
1017 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001018
Sean Callanan79ed1a82010-01-19 20:22:31 +00001019 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001020
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001021 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001022 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001023 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001024
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001025 // If BinOp binds less tightly with RHS than the operator after RHS, let
1026 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001027 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001028 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001029 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001030 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001031 }
1032
Daniel Dunbar475839e2009-06-29 20:37:27 +00001033 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001034 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001035 }
1036}
1037
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001038/// ParseStatement:
1039/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +00001040/// ::= Label* Directive ...Operands... EndOfStatement
1041/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001042bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001043 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001044 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001045 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001046 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001047 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001048
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001049 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +00001050 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001051 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001052 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001053 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001054 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001055 if (Lexer.is(AsmToken::Hash))
1056 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001057
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001058 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001059 if (Lexer.is(AsmToken::Integer)) {
1060 LocalLabelVal = getTok().getIntVal();
1061 if (LocalLabelVal < 0) {
1062 if (!TheCondState.Ignore)
1063 return TokError("unexpected token at start of statement");
1064 IDVal = "";
1065 }
1066 else {
1067 IDVal = getTok().getString();
1068 Lex(); // Consume the integer token to be used as an identifier token.
1069 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001070 if (!TheCondState.Ignore)
1071 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001072 }
1073 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001074
1075 } else if (Lexer.is(AsmToken::Dot)) {
1076 // Treat '.' as a valid identifier in this context.
1077 Lex();
1078 IDVal = ".";
1079
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001080 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001081 if (!TheCondState.Ignore)
1082 return TokError("unexpected token at start of statement");
1083 IDVal = "";
1084 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001085
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001086
Chris Lattner7834fac2010-04-17 18:14:27 +00001087 // Handle conditional assembly here before checking for skipping. We
1088 // have to do this so that .endif isn't skipped in a ".if 0" block for
1089 // example.
1090 if (IDVal == ".if")
1091 return ParseDirectiveIf(IDLoc);
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00001092 if (IDVal == ".ifb")
1093 return ParseDirectiveIfb(IDLoc, true);
1094 if (IDVal == ".ifnb")
1095 return ParseDirectiveIfb(IDLoc, false);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00001096 if (IDVal == ".ifc")
1097 return ParseDirectiveIfc(IDLoc, true);
1098 if (IDVal == ".ifnc")
1099 return ParseDirectiveIfc(IDLoc, false);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00001100 if (IDVal == ".ifdef")
1101 return ParseDirectiveIfdef(IDLoc, true);
1102 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
1103 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +00001104 if (IDVal == ".elseif")
1105 return ParseDirectiveElseIf(IDLoc);
1106 if (IDVal == ".else")
1107 return ParseDirectiveElse(IDLoc);
1108 if (IDVal == ".endif")
1109 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001110
Chris Lattner7834fac2010-04-17 18:14:27 +00001111 // If we are in a ".if 0" block, ignore this statement.
1112 if (TheCondState.Ignore) {
1113 EatToEndOfStatement();
1114 return false;
1115 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001116
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001117 // FIXME: Recurse on local labels?
1118
1119 // See what kind of statement we have.
1120 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001121 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001122 CheckForValidSection();
1123
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001124 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001125 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001126
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001127 // Diagnose attempt to use '.' as a label.
1128 if (IDVal == ".")
1129 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1130
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001131 // Diagnose attempt to use a variable as a label.
1132 //
1133 // FIXME: Diagnostics. Note the location of the definition as a label.
1134 // FIXME: This doesn't diagnose assignment to a symbol which has been
1135 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001136 MCSymbol *Sym;
1137 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001138 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001139 else
1140 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001141 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001142 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001143
Daniel Dunbar959fd882009-08-26 22:13:22 +00001144 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001145 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001146
Kevin Enderby94c2e852011-12-09 18:09:40 +00001147 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001148 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001149 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001150 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1151 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001152
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001153 // Consume any end of statement token, if present, to avoid spurious
1154 // AddBlankLine calls().
1155 if (Lexer.is(AsmToken::EndOfStatement)) {
1156 Lex();
1157 if (Lexer.is(AsmToken::Eof))
1158 return false;
1159 }
1160
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001161 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001162 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001163
Daniel Dunbar3f872332009-07-28 16:08:33 +00001164 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001165 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001166 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001167
Nico Weber4c4c7322011-01-28 03:04:41 +00001168 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001169
1170 default: // Normal instruction or directive.
1171 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001172 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001173
1174 // If macros are enabled, check to see if this is a macro instantiation.
1175 if (MacrosEnabled)
1176 if (const Macro *M = MacroMap.lookup(IDVal))
1177 return HandleMacroEntry(IDVal, IDLoc, M);
1178
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001179 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001180 if (IDVal[0] == '.' && IDVal != ".") {
Akira Hatanaka3b02d952012-07-05 19:09:33 +00001181
1182 // Target hook for parsing target specific directives.
1183 if (!getTargetParser().ParseDirective(ID))
1184 return false;
1185
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001186 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001187 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001188 return ParseDirectiveSet(IDVal, true);
1189 if (IDVal == ".equiv")
1190 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001191
Daniel Dunbara0d14262009-06-24 23:30:00 +00001192 // Data directives
1193
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001194 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001195 return ParseDirectiveAscii(IDVal, false);
1196 if (IDVal == ".asciz" || IDVal == ".string")
1197 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001198
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001199 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001200 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001201 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001202 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001203 if (IDVal == ".value")
1204 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001205 if (IDVal == ".2byte")
1206 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001207 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001208 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001209 if (IDVal == ".int")
1210 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001211 if (IDVal == ".4byte")
1212 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001213 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001214 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001215 if (IDVal == ".8byte")
1216 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001217 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001218 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1219 if (IDVal == ".double")
1220 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001221
Eli Friedman5d68ec22010-07-19 04:17:25 +00001222 if (IDVal == ".align") {
1223 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1224 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1225 }
1226 if (IDVal == ".align32") {
1227 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1228 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1229 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001230 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001231 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001232 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001233 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001234 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001235 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001236 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001237 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001238 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001239 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001240 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001241 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1242
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001243 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001244 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001245
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001246 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001247 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001248 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001249 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001250 if (IDVal == ".zero")
1251 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001252
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001253 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001254
Benjamin Kramere14a3c52012-05-12 11:18:59 +00001255 if (IDVal == ".extern") {
1256 EatToEndOfStatement(); // .extern is the default, ignore it.
1257 return false;
1258 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001259 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001260 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001261 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001262 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001263 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001264 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001265 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001266 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001267 if (IDVal == ".symbol_resolver")
1268 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001269 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001270 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001271 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001272 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001273 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001274 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001275 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001276 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001277 if (IDVal == ".weak_def_can_be_hidden")
1278 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001279
Hans Wennborg5cc64912011-06-18 13:51:54 +00001280 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001281 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001282 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001283 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001284
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001285 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001286 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001287 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001288 return ParseDirectiveInclude();
Kevin Enderbyc55acca2011-12-14 21:47:48 +00001289 if (IDVal == ".incbin")
1290 return ParseDirectiveIncbin();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001291
Benjamin Kramer5cdf0ad2012-05-12 11:19:04 +00001292 if (IDVal == ".code16" || IDVal == ".code16gcc")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001293 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001294
Rafael Espindola761cb062012-06-03 23:57:14 +00001295 // Macro-like directives
Rafael Espindola2ec304c2012-05-12 16:31:10 +00001296 if (IDVal == ".rept")
1297 return ParseDirectiveRept(IDLoc);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001298 if (IDVal == ".irp")
1299 return ParseDirectiveIrp(IDLoc);
Rafael Espindolafc9216e2012-06-16 18:03:25 +00001300 if (IDVal == ".irpc")
1301 return ParseDirectiveIrpc(IDLoc);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00001302 if (IDVal == ".endr")
Rafael Espindola761cb062012-06-03 23:57:14 +00001303 return ParseDirectiveEndr(IDLoc);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00001304
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001305 // Look up the handler in the handler table.
1306 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1307 DirectiveMap.lookup(IDVal);
1308 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001309 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001310
Kevin Enderby9c656452009-09-10 20:51:44 +00001311
Jim Grosbach686c0182012-05-01 18:38:27 +00001312 return Error(IDLoc, "unknown directive");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001313 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001314
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001315 CheckForValidSection();
1316
Chris Lattnera7f13542010-05-19 23:34:33 +00001317 // Canonicalize the opcode to lower case.
1318 SmallString<128> Opcode;
1319 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1320 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001321
Chris Lattner98986712010-01-14 22:21:20 +00001322 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001323 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001324 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001325
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001326 // Dump the parsed representation, if requested.
1327 if (getShowParsedOperands()) {
1328 SmallString<256> Str;
1329 raw_svector_ostream OS(Str);
1330 OS << "parsed instruction: [";
1331 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1332 if (i != 0)
1333 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001334 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001335 }
1336 OS << "]";
1337
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001338 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001339 }
1340
Kevin Enderby613b7572011-11-01 22:27:22 +00001341 // If we are generating dwarf for assembly source files and the current
1342 // section is the initial text section then generate a .loc directive for
1343 // the instruction.
1344 if (!HadError && getContext().getGenDwarfForAssembly() &&
1345 getContext().getGenDwarfSection() == getStreamer().getCurrentSection() ) {
1346 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
1347 SrcMgr.FindLineNumber(IDLoc, CurBuffer),
1348 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001349 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001350 StringRef());
1351 }
1352
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001353 // If parsing succeeded, match the instruction.
Chad Rosier84125ca2012-10-13 00:26:04 +00001354 if (!HadError) {
1355 unsigned Opcode;
1356 unsigned ErrorInfo;
1357 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, Opcode,
1358 ParsedOperands,
1359 Out, ErrorInfo,
1360 ParsingInlineAsm);
1361 }
Chris Lattner98986712010-01-14 22:21:20 +00001362
Chris Lattner98986712010-01-14 22:21:20 +00001363 // Free any parsed operands.
1364 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1365 delete ParsedOperands[i];
1366
Chris Lattnercbf8a982010-09-11 16:18:25 +00001367 // Don't skip the rest of the line, the instruction parser is responsible for
1368 // that.
1369 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001370}
Chris Lattner9a023f72009-06-24 04:43:34 +00001371
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001372/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1373/// since they may not be able to be tokenized to get to the end of line token.
1374void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001375 if (!Lexer.is(AsmToken::EndOfStatement))
1376 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001377 // Eat EOL.
1378 Lex();
1379}
1380
1381/// ParseCppHashLineFilenameComment as this:
1382/// ::= # number "filename"
1383/// or just as a full line comment if it doesn't have a number and a string.
1384bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1385 Lex(); // Eat the hash token.
1386
1387 if (getLexer().isNot(AsmToken::Integer)) {
1388 // Consume the line since in cases it is not a well-formed line directive,
1389 // as if were simply a full line comment.
1390 EatToEndOfLine();
1391 return false;
1392 }
1393
1394 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001395 Lex();
1396
1397 if (getLexer().isNot(AsmToken::String)) {
1398 EatToEndOfLine();
1399 return false;
1400 }
1401
1402 StringRef Filename = getTok().getString();
1403 // Get rid of the enclosing quotes.
1404 Filename = Filename.substr(1, Filename.size()-2);
1405
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001406 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1407 CppHashLoc = L;
1408 CppHashFilename = Filename;
1409 CppHashLineNumber = LineNumber;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001410
1411 // Ignore any trailing characters, they're just comment.
1412 EatToEndOfLine();
1413 return false;
1414}
1415
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001416/// DiagHandler - will use the last parsed cpp hash line filename comment
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001417/// for the Filename and LineNo if any in the diagnostic.
1418void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1419 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1420 raw_ostream &OS = errs();
1421
1422 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1423 const SMLoc &DiagLoc = Diag.getLoc();
1424 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1425 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1426
1427 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1428 // before printing the message.
1429 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001430 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001431 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1432 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1433 }
1434
1435 // If we have not parsed a cpp hash line filename comment or the source
1436 // manager changed or buffer changed (like in a nested include) then just
1437 // print the normal diagnostic using its Filename and LineNo.
1438 if (!Parser->CppHashLineNumber ||
1439 &DiagSrcMgr != &Parser->SrcMgr ||
1440 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001441 if (Parser->SavedDiagHandler)
1442 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1443 else
1444 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001445 return;
1446 }
1447
1448 // Use the CppHashFilename and calculate a line number based on the
1449 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1450 // the diagnostic.
1451 const std::string Filename = Parser->CppHashFilename;
1452
1453 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1454 int CppHashLocLineNo =
1455 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1456 int LineNo = Parser->CppHashLineNumber - 1 +
1457 (DiagLocLineNo - CppHashLocLineNo);
1458
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001459 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1460 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001461 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001462 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001463
Benjamin Kramer04a04262011-10-16 10:48:29 +00001464 if (Parser->SavedDiagHandler)
1465 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1466 else
1467 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001468}
1469
Rafael Espindola799aacf2012-08-21 18:29:30 +00001470// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1471// difference being that that function accepts '@' as part of identifiers and
1472// we can't do that. AsmLexer.cpp should probably be changed to handle
1473// '@' as a special case when needed.
1474static bool isIdentifierChar(char c) {
1475 return isalnum(c) || c == '_' || c == '$' || c == '.';
1476}
1477
Rafael Espindola761cb062012-06-03 23:57:14 +00001478bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola8a403d32012-08-08 14:51:03 +00001479 const MacroParameters &Parameters,
1480 const MacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +00001481 const SMLoc &L) {
Rafael Espindola65366442011-06-05 02:43:45 +00001482 unsigned NParameters = Parameters.size();
1483 if (NParameters != 0 && NParameters != A.size())
1484 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001485
Preston Gurd7b6f2032012-09-19 20:36:12 +00001486 // A macro without parameters is handled differently on Darwin:
1487 // gas accepts no arguments and does no substitutions
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001488 while (!Body.empty()) {
1489 // Scan for the next substitution.
1490 std::size_t End = Body.size(), Pos = 0;
1491 for (; Pos != End; ++Pos) {
1492 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001493 if (!NParameters) {
1494 // This macro has no parameters, look for $0, $1, etc.
1495 if (Body[Pos] != '$' || Pos + 1 == End)
1496 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001497
Rafael Espindola65366442011-06-05 02:43:45 +00001498 char Next = Body[Pos + 1];
1499 if (Next == '$' || Next == 'n' || isdigit(Next))
1500 break;
1501 } else {
1502 // This macro has parameters, look for \foo, \bar, etc.
1503 if (Body[Pos] == '\\' && Pos + 1 != End)
1504 break;
1505 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001506 }
1507
1508 // Add the prefix.
1509 OS << Body.slice(0, Pos);
1510
1511 // Check if we reached the end.
1512 if (Pos == End)
1513 break;
1514
Rafael Espindola65366442011-06-05 02:43:45 +00001515 if (!NParameters) {
1516 switch (Body[Pos+1]) {
1517 // $$ => $
1518 case '$':
1519 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001520 break;
1521
Rafael Espindola65366442011-06-05 02:43:45 +00001522 // $n => number of arguments
1523 case 'n':
1524 OS << A.size();
1525 break;
1526
1527 // $[0-9] => argument
1528 default: {
1529 // Missing arguments are ignored.
1530 unsigned Index = Body[Pos+1] - '0';
1531 if (Index >= A.size())
1532 break;
1533
1534 // Otherwise substitute with the token values, with spaces eliminated.
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001535 for (MacroArgument::const_iterator it = A[Index].begin(),
Rafael Espindola65366442011-06-05 02:43:45 +00001536 ie = A[Index].end(); it != ie; ++it)
1537 OS << it->getString();
1538 break;
1539 }
1540 }
1541 Pos += 2;
1542 } else {
1543 unsigned I = Pos + 1;
Rafael Espindola799aacf2012-08-21 18:29:30 +00001544 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola65366442011-06-05 02:43:45 +00001545 ++I;
1546
1547 const char *Begin = Body.data() + Pos +1;
1548 StringRef Argument(Begin, I - (Pos +1));
1549 unsigned Index = 0;
1550 for (; Index < NParameters; ++Index)
Preston Gurd6c9176a2012-09-19 20:29:04 +00001551 if (Parameters[Index].first == Argument)
Rafael Espindola65366442011-06-05 02:43:45 +00001552 break;
1553
Preston Gurd7b6f2032012-09-19 20:36:12 +00001554 if (Index == NParameters) {
1555 if (Body[Pos+1] == '(' && Body[Pos+2] == ')')
1556 Pos += 3;
1557 else {
1558 OS << '\\' << Argument;
1559 Pos = I;
1560 }
1561 } else {
1562 for (MacroArgument::const_iterator it = A[Index].begin(),
1563 ie = A[Index].end(); it != ie; ++it)
1564 if (it->getKind() == AsmToken::String)
1565 OS << it->getStringContents();
1566 else
1567 OS << it->getString();
Rafael Espindola65366442011-06-05 02:43:45 +00001568
Preston Gurd7b6f2032012-09-19 20:36:12 +00001569 Pos += 1 + Argument.size();
1570 }
Rafael Espindola65366442011-06-05 02:43:45 +00001571 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001572 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001573 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001574 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001575
Rafael Espindola65366442011-06-05 02:43:45 +00001576 return false;
1577}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001578
Rafael Espindola65366442011-06-05 02:43:45 +00001579MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1580 MemoryBuffer *I)
1581 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1582{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001583}
1584
Preston Gurd7b6f2032012-09-19 20:36:12 +00001585static bool IsOperator(AsmToken::TokenKind kind)
1586{
1587 switch (kind)
1588 {
1589 default:
1590 return false;
1591 case AsmToken::Plus:
1592 case AsmToken::Minus:
1593 case AsmToken::Tilde:
1594 case AsmToken::Slash:
1595 case AsmToken::Star:
1596 case AsmToken::Dot:
1597 case AsmToken::Equal:
1598 case AsmToken::EqualEqual:
1599 case AsmToken::Pipe:
1600 case AsmToken::PipePipe:
1601 case AsmToken::Caret:
1602 case AsmToken::Amp:
1603 case AsmToken::AmpAmp:
1604 case AsmToken::Exclaim:
1605 case AsmToken::ExclaimEqual:
1606 case AsmToken::Percent:
1607 case AsmToken::Less:
1608 case AsmToken::LessEqual:
1609 case AsmToken::LessLess:
1610 case AsmToken::LessGreater:
1611 case AsmToken::Greater:
1612 case AsmToken::GreaterEqual:
1613 case AsmToken::GreaterGreater:
1614 return true;
1615 }
1616}
1617
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001618/// ParseMacroArgument - Extract AsmTokens for a macro argument.
1619/// This is used for both default macro parameter values and the
1620/// arguments in macro invocations
Preston Gurd7b6f2032012-09-19 20:36:12 +00001621bool AsmParser::ParseMacroArgument(MacroArgument &MA,
1622 AsmToken::TokenKind &ArgumentDelimiter) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001623 unsigned ParenLevel = 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001624 unsigned AddTokens = 0;
1625
1626 // gas accepts arguments separated by whitespace, except on Darwin
1627 if (!IsDarwin)
1628 Lexer.setSkipSpace(false);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001629
1630 for (;;) {
Preston Gurd7b6f2032012-09-19 20:36:12 +00001631 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) {
1632 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001633 return TokError("unexpected token in macro instantiation");
Preston Gurd7b6f2032012-09-19 20:36:12 +00001634 }
1635
1636 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1637 // Spaces and commas cannot be mixed to delimit parameters
1638 if (ArgumentDelimiter == AsmToken::Eof)
1639 ArgumentDelimiter = AsmToken::Comma;
1640 else if (ArgumentDelimiter != AsmToken::Comma) {
1641 Lexer.setSkipSpace(true);
1642 return TokError("expected ' ' for macro argument separator");
1643 }
1644 break;
1645 }
1646
1647 if (Lexer.is(AsmToken::Space)) {
1648 Lex(); // Eat spaces
1649
1650 // Spaces can delimit parameters, but could also be part an expression.
1651 // If the token after a space is an operator, add the token and the next
1652 // one into this argument
1653 if (ArgumentDelimiter == AsmToken::Space ||
1654 ArgumentDelimiter == AsmToken::Eof) {
1655 if (IsOperator(Lexer.getKind())) {
1656 // Check to see whether the token is used as an operator,
1657 // or part of an identifier
1658 const char *NextChar = getTok().getEndLoc().getPointer() + 1;
1659 if (*NextChar == ' ')
1660 AddTokens = 2;
1661 }
1662
1663 if (!AddTokens && ParenLevel == 0) {
1664 if (ArgumentDelimiter == AsmToken::Eof &&
1665 !IsOperator(Lexer.getKind()))
1666 ArgumentDelimiter = AsmToken::Space;
1667 break;
1668 }
1669 }
1670 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001671
1672 // HandleMacroEntry relies on not advancing the lexer here
1673 // to be able to fill in the remaining default parameter values
1674 if (Lexer.is(AsmToken::EndOfStatement))
1675 break;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001676
1677 // Adjust the current parentheses level.
1678 if (Lexer.is(AsmToken::LParen))
1679 ++ParenLevel;
1680 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1681 --ParenLevel;
1682
1683 // Append the token to the current argument list.
1684 MA.push_back(getTok());
Preston Gurd7b6f2032012-09-19 20:36:12 +00001685 if (AddTokens)
1686 AddTokens--;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001687 Lex();
1688 }
Preston Gurd7b6f2032012-09-19 20:36:12 +00001689
1690 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001691 if (ParenLevel != 0)
Rafael Espindola76ac2002012-08-21 15:55:04 +00001692 return TokError("unbalanced parentheses in macro argument");
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001693 return false;
1694}
1695
1696// Parse the macro instantiation arguments.
Rafael Espindola8a403d32012-08-08 14:51:03 +00001697bool AsmParser::ParseMacroArguments(const Macro *M, MacroArguments &A) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001698 const unsigned NParameters = M ? M->Parameters.size() : 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001699 // Argument delimiter is initially unknown. It will be set by
1700 // ParseMacroArgument()
1701 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001702
1703 // Parse two kinds of macro invocations:
1704 // - macros defined without any parameters accept an arbitrary number of them
1705 // - macros defined with parameters accept at most that many of them
1706 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1707 ++Parameter) {
1708 MacroArgument MA;
1709
Preston Gurd7b6f2032012-09-19 20:36:12 +00001710 if (ParseMacroArgument(MA, ArgumentDelimiter))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001711 return true;
1712
Preston Gurd6c9176a2012-09-19 20:29:04 +00001713 if (!MA.empty() || !NParameters)
1714 A.push_back(MA);
1715 else if (NParameters) {
1716 if (!M->Parameters[Parameter].second.empty())
1717 A.push_back(M->Parameters[Parameter].second);
1718 }
Jim Grosbach97146442012-07-30 22:44:17 +00001719
Preston Gurd6c9176a2012-09-19 20:29:04 +00001720 // At the end of the statement, fill in remaining arguments that have
1721 // default values. If there aren't any, then the next argument is
1722 // required but missing
1723 if (Lexer.is(AsmToken::EndOfStatement)) {
1724 if (NParameters && Parameter < NParameters - 1) {
1725 if (M->Parameters[Parameter + 1].second.empty())
1726 return TokError("macro argument '" +
1727 Twine(M->Parameters[Parameter + 1].first) +
1728 "' is missing");
1729 else
1730 continue;
1731 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001732 return false;
Preston Gurd6c9176a2012-09-19 20:29:04 +00001733 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001734
1735 if (Lexer.is(AsmToken::Comma))
1736 Lex();
1737 }
1738 return TokError("Too many arguments");
1739}
1740
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001741bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1742 const Macro *M) {
1743 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1744 // this, although we should protect against infinite loops.
1745 if (ActiveMacros.size() == 20)
1746 return TokError("macros cannot be nested more than 20 levels deep");
1747
Rafael Espindola8a403d32012-08-08 14:51:03 +00001748 MacroArguments A;
1749 if (ParseMacroArguments(M, A))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001750 return true;
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001751
Jim Grosbach97146442012-07-30 22:44:17 +00001752 // Remove any trailing empty arguments. Do this after-the-fact as we have
1753 // to keep empty arguments in the middle of the list or positionality
1754 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindola8a403d32012-08-08 14:51:03 +00001755 while (!A.empty() && A.back().empty())
1756 A.pop_back();
Jim Grosbach97146442012-07-30 22:44:17 +00001757
Rafael Espindola65366442011-06-05 02:43:45 +00001758 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1759 // to hold the macro body with substitutions.
1760 SmallString<256> Buf;
1761 StringRef Body = M->Body;
Rafael Espindola761cb062012-06-03 23:57:14 +00001762 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001763
Rafael Espindola8a403d32012-08-08 14:51:03 +00001764 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola65366442011-06-05 02:43:45 +00001765 return true;
1766
Rafael Espindola761cb062012-06-03 23:57:14 +00001767 // We include the .endmacro in the buffer as our queue to exit the macro
1768 // instantiation.
1769 OS << ".endmacro\n";
1770
Rafael Espindola65366442011-06-05 02:43:45 +00001771 MemoryBuffer *Instantiation =
Rafael Espindola761cb062012-06-03 23:57:14 +00001772 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola65366442011-06-05 02:43:45 +00001773
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001774 // Create the macro instantiation object and add to the current macro
1775 // instantiation stack.
1776 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001777 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001778 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001779 ActiveMacros.push_back(MI);
1780
1781 // Jump to the macro instantiation and prime the lexer.
1782 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1783 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1784 Lex();
1785
1786 return false;
1787}
1788
1789void AsmParser::HandleMacroExit() {
1790 // Jump to the EndOfStatement we should return to, and consume it.
1791 JumpToLoc(ActiveMacros.back()->ExitLoc);
1792 Lex();
1793
1794 // Pop the instantiation entry.
1795 delete ActiveMacros.back();
1796 ActiveMacros.pop_back();
1797}
1798
Rafael Espindolae71cc862012-01-28 05:57:00 +00001799static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001800 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001801 case MCExpr::Binary: {
1802 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1803 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001804 break;
1805 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001806 case MCExpr::Target:
1807 case MCExpr::Constant:
1808 return false;
1809 case MCExpr::SymbolRef: {
1810 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001811 if (S.isVariable())
1812 return IsUsedIn(Sym, S.getVariableValue());
1813 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001814 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001815 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001816 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001817 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001818
1819 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001820}
1821
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001822bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef,
1823 bool NoDeadStrip) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001824 // FIXME: Use better location, we should use proper tokens.
1825 SMLoc EqualLoc = Lexer.getLoc();
1826
Daniel Dunbar821e3332009-08-31 08:09:28 +00001827 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001828 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001829 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001830
Rafael Espindolae71cc862012-01-28 05:57:00 +00001831 // Note: we don't count b as used in "a = b". This is to allow
1832 // a = b
1833 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001834
Daniel Dunbar3f872332009-07-28 16:08:33 +00001835 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001836 return TokError("unexpected token in assignment");
1837
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001838 // Error on assignment to '.'.
1839 if (Name == ".") {
1840 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1841 "(use '.space' or '.org').)"));
1842 }
1843
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001844 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001845 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001846
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001847 // Validate that the LHS is allowed to be a variable (either it has not been
1848 // used as a symbol, or it is an absolute symbol).
1849 MCSymbol *Sym = getContext().LookupSymbol(Name);
1850 if (Sym) {
1851 // Diagnose assignment to a label.
1852 //
1853 // FIXME: Diagnostics. Note the location of the definition as a label.
1854 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001855 if (IsUsedIn(Sym, Value))
1856 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1857 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001858 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00001859 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1860 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001861 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001862 return Error(EqualLoc, "redefinition of '" + Name + "'");
1863 else if (!Sym->isVariable())
1864 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001865 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001866 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1867 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001868
1869 // Don't count these checks as uses.
1870 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001871 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001872 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001873
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001874 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001875
1876 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001877 Out.EmitAssignment(Sym, Value);
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001878 if (NoDeadStrip)
1879 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
1880
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001881
1882 return false;
1883}
1884
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001885/// ParseIdentifier:
1886/// ::= identifier
1887/// ::= string
1888bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001889 // The assembler has relaxed rules for accepting identifiers, in particular we
1890 // allow things like '.globl $foo', which would normally be separate
1891 // tokens. At this level, we have already lexed so we cannot (currently)
1892 // handle this as a context dependent token, instead we detect adjacent tokens
1893 // and return the combined identifier.
1894 if (Lexer.is(AsmToken::Dollar)) {
1895 SMLoc DollarLoc = getLexer().getLoc();
1896
1897 // Consume the dollar sign, and check for a following identifier.
1898 Lex();
1899 if (Lexer.isNot(AsmToken::Identifier))
1900 return true;
1901
1902 // We have a '$' followed by an identifier, make sure they are adjacent.
1903 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1904 return true;
1905
1906 // Construct the joined identifier and consume the token.
1907 Res = StringRef(DollarLoc.getPointer(),
1908 getTok().getIdentifier().size() + 1);
1909 Lex();
1910 return false;
1911 }
1912
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001913 if (Lexer.isNot(AsmToken::Identifier) &&
1914 Lexer.isNot(AsmToken::String))
1915 return true;
1916
Sean Callanan18b83232010-01-19 21:44:56 +00001917 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001918
Sean Callanan79ed1a82010-01-19 20:22:31 +00001919 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001920
1921 return false;
1922}
1923
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001924/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001925/// ::= .equ identifier ',' expression
1926/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001927/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001928bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001929 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001930
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001931 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001932 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001933
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001934 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001935 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001936 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001937
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001938 return ParseAssignment(Name, allow_redef, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001939}
1940
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001941bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001942 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001943
1944 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001945 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001946 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1947 if (Str[i] != '\\') {
1948 Data += Str[i];
1949 continue;
1950 }
1951
1952 // Recognize escaped characters. Note that this escape semantics currently
1953 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1954 ++i;
1955 if (i == e)
1956 return TokError("unexpected backslash at end of string");
1957
1958 // Recognize octal sequences.
1959 if ((unsigned) (Str[i] - '0') <= 7) {
1960 // Consume up to three octal characters.
1961 unsigned Value = Str[i] - '0';
1962
1963 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1964 ++i;
1965 Value = Value * 8 + (Str[i] - '0');
1966
1967 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1968 ++i;
1969 Value = Value * 8 + (Str[i] - '0');
1970 }
1971 }
1972
1973 if (Value > 255)
1974 return TokError("invalid octal escape sequence (out of range)");
1975
1976 Data += (unsigned char) Value;
1977 continue;
1978 }
1979
1980 // Otherwise recognize individual escapes.
1981 switch (Str[i]) {
1982 default:
1983 // Just reject invalid escape sequences for now.
1984 return TokError("invalid escape sequence (unrecognized character)");
1985
1986 case 'b': Data += '\b'; break;
1987 case 'f': Data += '\f'; break;
1988 case 'n': Data += '\n'; break;
1989 case 'r': Data += '\r'; break;
1990 case 't': Data += '\t'; break;
1991 case '"': Data += '"'; break;
1992 case '\\': Data += '\\'; break;
1993 }
1994 }
1995
1996 return false;
1997}
1998
Daniel Dunbara0d14262009-06-24 23:30:00 +00001999/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00002000/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
2001bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002002 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002003 CheckForValidSection();
2004
Daniel Dunbara0d14262009-06-24 23:30:00 +00002005 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002006 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00002007 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002008
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002009 std::string Data;
2010 if (ParseEscapedString(Data))
2011 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002012
2013 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002014 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002015 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
2016
Sean Callanan79ed1a82010-01-19 20:22:31 +00002017 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002018
2019 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002020 break;
2021
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002022 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00002023 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002024 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002025 }
2026 }
2027
Sean Callanan79ed1a82010-01-19 20:22:31 +00002028 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002029 return false;
2030}
2031
2032/// ParseDirectiveValue
2033/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
2034bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002035 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002036 CheckForValidSection();
2037
Daniel Dunbara0d14262009-06-24 23:30:00 +00002038 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00002039 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00002040 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002041 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002042 return true;
2043
Daniel Dunbar414c0c42010-05-23 18:36:38 +00002044 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00002045 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2046 assert(Size <= 8 && "Invalid size");
2047 uint64_t IntValue = MCE->getValue();
2048 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2049 return Error(ExprLoc, "literal value out of range for directive");
2050 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
2051 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002052 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002053
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002054 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002055 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002056
Daniel Dunbara0d14262009-06-24 23:30:00 +00002057 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002058 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002059 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002060 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002061 }
2062 }
2063
Sean Callanan79ed1a82010-01-19 20:22:31 +00002064 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002065 return false;
2066}
2067
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002068/// ParseDirectiveRealValue
2069/// ::= (.single | .double) [ expression (, expression)* ]
2070bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
2071 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2072 CheckForValidSection();
2073
2074 for (;;) {
2075 // We don't truly support arithmetic on floating point expressions, so we
2076 // have to manually parse unary prefixes.
2077 bool IsNeg = false;
2078 if (getLexer().is(AsmToken::Minus)) {
2079 Lex();
2080 IsNeg = true;
2081 } else if (getLexer().is(AsmToken::Plus))
2082 Lex();
2083
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002084 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00002085 getLexer().isNot(AsmToken::Real) &&
2086 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002087 return TokError("unexpected token in directive");
2088
2089 // Convert to an APFloat.
2090 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00002091 StringRef IDVal = getTok().getString();
2092 if (getLexer().is(AsmToken::Identifier)) {
2093 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2094 Value = APFloat::getInf(Semantics);
2095 else if (!IDVal.compare_lower("nan"))
2096 Value = APFloat::getNaN(Semantics, false, ~0);
2097 else
2098 return TokError("invalid floating point literal");
2099 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002100 APFloat::opInvalidOp)
2101 return TokError("invalid floating point literal");
2102 if (IsNeg)
2103 Value.changeSign();
2104
2105 // Consume the numeric token.
2106 Lex();
2107
2108 // Emit the value as an integer.
2109 APInt AsInt = Value.bitcastToAPInt();
2110 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
2111 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
2112
2113 if (getLexer().is(AsmToken::EndOfStatement))
2114 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002115
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002116 if (getLexer().isNot(AsmToken::Comma))
2117 return TokError("unexpected token in directive");
2118 Lex();
2119 }
2120 }
2121
2122 Lex();
2123 return false;
2124}
2125
Daniel Dunbara0d14262009-06-24 23:30:00 +00002126/// ParseDirectiveSpace
2127/// ::= .space expression [ , expression ]
2128bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002129 CheckForValidSection();
2130
Daniel Dunbara0d14262009-06-24 23:30:00 +00002131 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002132 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002133 return true;
2134
2135 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002136 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2137 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002138 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002139 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002140
Daniel Dunbar475839e2009-06-29 20:37:27 +00002141 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002142 return true;
2143
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002144 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002145 return TokError("unexpected token in '.space' directive");
2146 }
2147
Sean Callanan79ed1a82010-01-19 20:22:31 +00002148 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002149
2150 if (NumBytes <= 0)
2151 return TokError("invalid number of bytes in '.space' directive");
2152
2153 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002154 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002155
2156 return false;
2157}
2158
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002159/// ParseDirectiveZero
2160/// ::= .zero expression
2161bool AsmParser::ParseDirectiveZero() {
2162 CheckForValidSection();
2163
2164 int64_t NumBytes;
2165 if (ParseAbsoluteExpression(NumBytes))
2166 return true;
2167
Rafael Espindolae452b172010-10-05 19:42:57 +00002168 int64_t Val = 0;
2169 if (getLexer().is(AsmToken::Comma)) {
2170 Lex();
2171 if (ParseAbsoluteExpression(Val))
2172 return true;
2173 }
2174
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002175 if (getLexer().isNot(AsmToken::EndOfStatement))
2176 return TokError("unexpected token in '.zero' directive");
2177
2178 Lex();
2179
Rafael Espindolae452b172010-10-05 19:42:57 +00002180 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002181
2182 return false;
2183}
2184
Daniel Dunbara0d14262009-06-24 23:30:00 +00002185/// ParseDirectiveFill
2186/// ::= .fill expression , expression , expression
2187bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002188 CheckForValidSection();
2189
Daniel Dunbara0d14262009-06-24 23:30:00 +00002190 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002191 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002192 return true;
2193
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002194 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002195 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002196 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002197
Daniel Dunbara0d14262009-06-24 23:30:00 +00002198 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002199 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002200 return true;
2201
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002202 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002203 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002204 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002205
Daniel Dunbara0d14262009-06-24 23:30:00 +00002206 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002207 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002208 return true;
2209
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002210 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002211 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002212
Sean Callanan79ed1a82010-01-19 20:22:31 +00002213 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002214
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002215 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2216 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002217
2218 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002219 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002220
2221 return false;
2222}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002223
2224/// ParseDirectiveOrg
2225/// ::= .org expression [ , expression ]
2226bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002227 CheckForValidSection();
2228
Daniel Dunbar821e3332009-08-31 08:09:28 +00002229 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002230 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002231 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002232 return true;
2233
2234 // Parse optional fill expression.
2235 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002236 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2237 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002238 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002239 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002240
Daniel Dunbar475839e2009-06-29 20:37:27 +00002241 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002242 return true;
2243
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002244 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002245 return TokError("unexpected token in '.org' directive");
2246 }
2247
Sean Callanan79ed1a82010-01-19 20:22:31 +00002248 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002249
Jim Grosbachebd4c052012-01-27 00:37:08 +00002250 // Only limited forms of relocatable expressions are accepted here, it
2251 // has to be relative to the current section. The streamer will return
2252 // 'true' if the expression wasn't evaluatable.
2253 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2254 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002255
2256 return false;
2257}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002258
2259/// ParseDirectiveAlign
2260/// ::= {.align, ...} expression [ , expression [ , expression ]]
2261bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002262 CheckForValidSection();
2263
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002264 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002265 int64_t Alignment;
2266 if (ParseAbsoluteExpression(Alignment))
2267 return true;
2268
2269 SMLoc MaxBytesLoc;
2270 bool HasFillExpr = false;
2271 int64_t FillExpr = 0;
2272 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002273 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2274 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002275 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002276 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002277
2278 // The fill expression can be omitted while specifying a maximum number of
2279 // alignment bytes, e.g:
2280 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002281 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002282 HasFillExpr = true;
2283 if (ParseAbsoluteExpression(FillExpr))
2284 return true;
2285 }
2286
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002287 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2288 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002289 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002290 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002291
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002292 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002293 if (ParseAbsoluteExpression(MaxBytesToFill))
2294 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002295
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002296 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002297 return TokError("unexpected token in directive");
2298 }
2299 }
2300
Sean Callanan79ed1a82010-01-19 20:22:31 +00002301 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002302
Daniel Dunbar648ac512010-05-17 21:54:30 +00002303 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002304 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002305
2306 // Compute alignment in bytes.
2307 if (IsPow2) {
2308 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002309 if (Alignment >= 32) {
2310 Error(AlignmentLoc, "invalid alignment value");
2311 Alignment = 31;
2312 }
2313
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002314 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002315 }
2316
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002317 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002318 if (MaxBytesLoc.isValid()) {
2319 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002320 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2321 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002322 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002323 }
2324
2325 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002326 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2327 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002328 MaxBytesToFill = 0;
2329 }
2330 }
2331
Daniel Dunbar648ac512010-05-17 21:54:30 +00002332 // Check whether we should use optimal code alignment for this .align
2333 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002334 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002335 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2336 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002337 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002338 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002339 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002340 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2341 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002342 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002343
2344 return false;
2345}
2346
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002347/// ParseDirectiveSymbolAttribute
2348/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002349bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002350 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002351 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002352 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002353 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002354
2355 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002356 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002357
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002358 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002359
Jim Grosbach10ec6502011-09-15 17:56:49 +00002360 // Assembler local symbols don't make any sense here. Complain loudly.
2361 if (Sym->isTemporary())
2362 return Error(Loc, "non-local symbol required in directive");
2363
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002364 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002365
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002366 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002367 break;
2368
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002369 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002370 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002371 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002372 }
2373 }
2374
Sean Callanan79ed1a82010-01-19 20:22:31 +00002375 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002376 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002377}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002378
2379/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002380/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2381bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002382 CheckForValidSection();
2383
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002384 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002385 StringRef Name;
2386 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002387 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002388
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002389 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002390 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002391
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002392 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002393 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002394 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002395
2396 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002397 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002398 if (ParseAbsoluteExpression(Size))
2399 return true;
2400
2401 int64_t Pow2Alignment = 0;
2402 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002403 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002404 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002405 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002406 if (ParseAbsoluteExpression(Pow2Alignment))
2407 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002408
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002409 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
2410 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer39646d92012-09-07 17:25:13 +00002411 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
2412
Chris Lattner258281d2010-01-19 06:22:22 +00002413 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002414 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
2415 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattner258281d2010-01-19 06:22:22 +00002416 if (!isPowerOf2_64(Pow2Alignment))
2417 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2418 Pow2Alignment = Log2_64(Pow2Alignment);
2419 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002420 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002421
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002422 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002423 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002424
Sean Callanan79ed1a82010-01-19 20:22:31 +00002425 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002426
Chris Lattner1fc3d752009-07-09 17:25:12 +00002427 // NOTE: a size of zero for a .comm should create a undefined symbol
2428 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002429 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002430 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2431 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002432
Eric Christopherc260a3e2010-05-14 01:38:54 +00002433 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002434 // may internally end up wanting an alignment in bytes.
2435 // FIXME: Diagnose overflow.
2436 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002437 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2438 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002439
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002440 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002441 return Error(IDLoc, "invalid symbol redefinition");
2442
Chris Lattner1fc3d752009-07-09 17:25:12 +00002443 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002444 if (IsLocal) {
Benjamin Kramer39646d92012-09-07 17:25:13 +00002445 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002446 return false;
2447 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002448
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002449 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002450 return false;
2451}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002452
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002453/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002454/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002455bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002456 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002457 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002458
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002459 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002460 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002461 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002462
Sean Callanan79ed1a82010-01-19 20:22:31 +00002463 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002464
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002465 if (Str.empty())
2466 Error(Loc, ".abort detected. Assembly stopping.");
2467 else
2468 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002469 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002470
2471 return false;
2472}
Kevin Enderby71148242009-07-14 21:35:03 +00002473
Kevin Enderby1f049b22009-07-14 23:21:55 +00002474/// ParseDirectiveInclude
2475/// ::= .include "filename"
2476bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002477 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002478 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002479
Sean Callanan18b83232010-01-19 21:44:56 +00002480 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002481 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002482 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002483
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002484 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002485 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002486
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002487 // Strip the quotes.
2488 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002489
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002490 // Attempt to switch the lexer to the included file before consuming the end
2491 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002492 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002493 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002494 return true;
2495 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002496
2497 return false;
2498}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002499
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002500/// ParseDirectiveIncbin
2501/// ::= .incbin "filename"
2502bool AsmParser::ParseDirectiveIncbin() {
2503 if (getLexer().isNot(AsmToken::String))
2504 return TokError("expected string in '.incbin' directive");
2505
2506 std::string Filename = getTok().getString();
2507 SMLoc IncbinLoc = getLexer().getLoc();
2508 Lex();
2509
2510 if (getLexer().isNot(AsmToken::EndOfStatement))
2511 return TokError("unexpected token in '.incbin' directive");
2512
2513 // Strip the quotes.
2514 Filename = Filename.substr(1, Filename.size()-2);
2515
2516 // Attempt to process the included file.
2517 if (ProcessIncbinFile(Filename)) {
2518 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2519 return true;
2520 }
2521
2522 return false;
2523}
2524
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002525/// ParseDirectiveIf
2526/// ::= .if expression
2527bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002528 TheCondStack.push_back(TheCondState);
2529 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramer29739e72012-05-12 16:52:21 +00002530 if (TheCondState.Ignore) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002531 EatToEndOfStatement();
Benjamin Kramer29739e72012-05-12 16:52:21 +00002532 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002533 int64_t ExprValue;
2534 if (ParseAbsoluteExpression(ExprValue))
2535 return true;
2536
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002537 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002538 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002539
Sean Callanan79ed1a82010-01-19 20:22:31 +00002540 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002541
2542 TheCondState.CondMet = ExprValue;
2543 TheCondState.Ignore = !TheCondState.CondMet;
2544 }
2545
2546 return false;
2547}
2548
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002549/// ParseDirectiveIfb
2550/// ::= .ifb string
2551bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
2552 TheCondStack.push_back(TheCondState);
2553 TheCondState.TheCond = AsmCond::IfCond;
2554
Benjamin Kramer29739e72012-05-12 16:52:21 +00002555 if (TheCondState.Ignore) {
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002556 EatToEndOfStatement();
2557 } else {
2558 StringRef Str = ParseStringToEndOfStatement();
2559
2560 if (getLexer().isNot(AsmToken::EndOfStatement))
2561 return TokError("unexpected token in '.ifb' directive");
2562
2563 Lex();
2564
2565 TheCondState.CondMet = ExpectBlank == Str.empty();
2566 TheCondState.Ignore = !TheCondState.CondMet;
2567 }
2568
2569 return false;
2570}
2571
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002572/// ParseDirectiveIfc
2573/// ::= .ifc string1, string2
2574bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
2575 TheCondStack.push_back(TheCondState);
2576 TheCondState.TheCond = AsmCond::IfCond;
2577
Benjamin Kramer29739e72012-05-12 16:52:21 +00002578 if (TheCondState.Ignore) {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002579 EatToEndOfStatement();
2580 } else {
2581 StringRef Str1 = ParseStringToComma();
2582
2583 if (getLexer().isNot(AsmToken::Comma))
2584 return TokError("unexpected token in '.ifc' directive");
2585
2586 Lex();
2587
2588 StringRef Str2 = ParseStringToEndOfStatement();
2589
2590 if (getLexer().isNot(AsmToken::EndOfStatement))
2591 return TokError("unexpected token in '.ifc' directive");
2592
2593 Lex();
2594
2595 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
2596 TheCondState.Ignore = !TheCondState.CondMet;
2597 }
2598
2599 return false;
2600}
2601
2602/// ParseDirectiveIfdef
2603/// ::= .ifdef symbol
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002604bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2605 StringRef Name;
2606 TheCondStack.push_back(TheCondState);
2607 TheCondState.TheCond = AsmCond::IfCond;
2608
2609 if (TheCondState.Ignore) {
2610 EatToEndOfStatement();
2611 } else {
2612 if (ParseIdentifier(Name))
2613 return TokError("expected identifier after '.ifdef'");
2614
2615 Lex();
2616
2617 MCSymbol *Sym = getContext().LookupSymbol(Name);
2618
2619 if (expect_defined)
2620 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2621 else
2622 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2623 TheCondState.Ignore = !TheCondState.CondMet;
2624 }
2625
2626 return false;
2627}
2628
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002629/// ParseDirectiveElseIf
2630/// ::= .elseif expression
2631bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2632 if (TheCondState.TheCond != AsmCond::IfCond &&
2633 TheCondState.TheCond != AsmCond::ElseIfCond)
2634 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2635 " an .elseif");
2636 TheCondState.TheCond = AsmCond::ElseIfCond;
2637
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002638 bool LastIgnoreState = false;
2639 if (!TheCondStack.empty())
2640 LastIgnoreState = TheCondStack.back().Ignore;
2641 if (LastIgnoreState || TheCondState.CondMet) {
2642 TheCondState.Ignore = true;
2643 EatToEndOfStatement();
2644 }
2645 else {
2646 int64_t ExprValue;
2647 if (ParseAbsoluteExpression(ExprValue))
2648 return true;
2649
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002650 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002651 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002652
Sean Callanan79ed1a82010-01-19 20:22:31 +00002653 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002654 TheCondState.CondMet = ExprValue;
2655 TheCondState.Ignore = !TheCondState.CondMet;
2656 }
2657
2658 return false;
2659}
2660
2661/// ParseDirectiveElse
2662/// ::= .else
2663bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002664 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002665 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002666
Sean Callanan79ed1a82010-01-19 20:22:31 +00002667 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002668
2669 if (TheCondState.TheCond != AsmCond::IfCond &&
2670 TheCondState.TheCond != AsmCond::ElseIfCond)
2671 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2672 ".elseif");
2673 TheCondState.TheCond = AsmCond::ElseCond;
2674 bool LastIgnoreState = false;
2675 if (!TheCondStack.empty())
2676 LastIgnoreState = TheCondStack.back().Ignore;
2677 if (LastIgnoreState || TheCondState.CondMet)
2678 TheCondState.Ignore = true;
2679 else
2680 TheCondState.Ignore = false;
2681
2682 return false;
2683}
2684
2685/// ParseDirectiveEndIf
2686/// ::= .endif
2687bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002688 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002689 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002690
Sean Callanan79ed1a82010-01-19 20:22:31 +00002691 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002692
2693 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2694 TheCondStack.empty())
2695 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2696 ".else");
2697 if (!TheCondStack.empty()) {
2698 TheCondState = TheCondStack.back();
2699 TheCondStack.pop_back();
2700 }
2701
2702 return false;
2703}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002704
2705/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002706/// ::= .file [number] filename
2707/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002708bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002709 // FIXME: I'm not sure what this is.
2710 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002711 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002712 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002713 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002714 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002715
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002716 if (FileNumber < 1)
2717 return TokError("file number less than one");
2718 }
2719
Daniel Dunbareceec052010-07-12 17:45:27 +00002720 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002721 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002722
Nick Lewycky44d798d2011-10-17 23:05:28 +00002723 // Usually the directory and filename together, otherwise just the directory.
2724 StringRef Path = getTok().getString();
2725 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002726 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002727
Nick Lewycky44d798d2011-10-17 23:05:28 +00002728 StringRef Directory;
2729 StringRef Filename;
2730 if (getLexer().is(AsmToken::String)) {
2731 if (FileNumber == -1)
2732 return TokError("explicit path specified, but no file number");
2733 Filename = getTok().getString();
2734 Filename = Filename.substr(1, Filename.size()-2);
2735 Directory = Path;
2736 Lex();
2737 } else {
2738 Filename = Path;
2739 }
2740
Daniel Dunbareceec052010-07-12 17:45:27 +00002741 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002742 return TokError("unexpected token in '.file' directive");
2743
Chris Lattnerd32e8032010-01-25 19:02:58 +00002744 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002745 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002746 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002747 if (getContext().getGenDwarfForAssembly() == true)
2748 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2749 "used to generate dwarf debug info for assembly code");
2750
Nick Lewycky44d798d2011-10-17 23:05:28 +00002751 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002752 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002753 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002754
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002755 return false;
2756}
2757
2758/// ParseDirectiveLine
2759/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002760bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002761 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2762 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002763 return TokError("unexpected token in '.line' directive");
2764
Sean Callanan18b83232010-01-19 21:44:56 +00002765 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002766 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002767 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002768
2769 // FIXME: Do something with the .line.
2770 }
2771
Daniel Dunbareceec052010-07-12 17:45:27 +00002772 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002773 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002774
2775 return false;
2776}
2777
2778
2779/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002780/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002781/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2782/// The first number is a file number, must have been previously assigned with
2783/// a .file directive, the second number is the line number and optionally the
2784/// third number is a column position (zero if not specified). The remaining
2785/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002786bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002787
Daniel Dunbareceec052010-07-12 17:45:27 +00002788 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002789 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002790 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002791 if (FileNumber < 1)
2792 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002793 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002794 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002795 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002796
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002797 int64_t LineNumber = 0;
2798 if (getLexer().is(AsmToken::Integer)) {
2799 LineNumber = getTok().getIntVal();
2800 if (LineNumber < 1)
2801 return TokError("line number less than one in '.loc' directive");
2802 Lex();
2803 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002804
2805 int64_t ColumnPos = 0;
2806 if (getLexer().is(AsmToken::Integer)) {
2807 ColumnPos = getTok().getIntVal();
2808 if (ColumnPos < 0)
2809 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002810 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002811 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002812
Kevin Enderbyc0957932010-09-30 16:52:03 +00002813 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002814 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002815 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002816 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2817 for (;;) {
2818 if (getLexer().is(AsmToken::EndOfStatement))
2819 break;
2820
2821 StringRef Name;
2822 SMLoc Loc = getTok().getLoc();
2823 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002824 return TokError("unexpected token in '.loc' directive");
2825
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002826 if (Name == "basic_block")
2827 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2828 else if (Name == "prologue_end")
2829 Flags |= DWARF2_FLAG_PROLOGUE_END;
2830 else if (Name == "epilogue_begin")
2831 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2832 else if (Name == "is_stmt") {
2833 SMLoc Loc = getTok().getLoc();
2834 const MCExpr *Value;
2835 if (getParser().ParseExpression(Value))
2836 return true;
2837 // The expression must be the constant 0 or 1.
2838 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2839 int Value = MCE->getValue();
2840 if (Value == 0)
2841 Flags &= ~DWARF2_FLAG_IS_STMT;
2842 else if (Value == 1)
2843 Flags |= DWARF2_FLAG_IS_STMT;
2844 else
2845 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002846 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002847 else {
2848 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2849 }
2850 }
2851 else if (Name == "isa") {
2852 SMLoc Loc = getTok().getLoc();
2853 const MCExpr *Value;
2854 if (getParser().ParseExpression(Value))
2855 return true;
2856 // The expression must be a constant greater or equal to 0.
2857 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2858 int Value = MCE->getValue();
2859 if (Value < 0)
2860 return Error(Loc, "isa number less than zero");
2861 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002862 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002863 else {
2864 return Error(Loc, "isa number not a constant value");
2865 }
2866 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002867 else if (Name == "discriminator") {
2868 if (getParser().ParseAbsoluteExpression(Discriminator))
2869 return true;
2870 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002871 else {
2872 return Error(Loc, "unknown sub-directive in '.loc' directive");
2873 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002874
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002875 if (getLexer().is(AsmToken::EndOfStatement))
2876 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002877 }
2878 }
2879
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002880 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002881 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002882
2883 return false;
2884}
2885
Daniel Dunbar138abae2010-10-16 04:56:42 +00002886/// ParseDirectiveStabs
2887/// ::= .stabs string, number, number, number
2888bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2889 SMLoc DirectiveLoc) {
2890 return TokError("unsupported directive '" + Directive + "'");
2891}
2892
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002893/// ParseDirectiveCFISections
2894/// ::= .cfi_sections section [, section]
2895bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2896 SMLoc DirectiveLoc) {
2897 StringRef Name;
2898 bool EH = false;
2899 bool Debug = false;
2900
2901 if (getParser().ParseIdentifier(Name))
2902 return TokError("Expected an identifier");
2903
2904 if (Name == ".eh_frame")
2905 EH = true;
2906 else if (Name == ".debug_frame")
2907 Debug = true;
2908
2909 if (getLexer().is(AsmToken::Comma)) {
2910 Lex();
2911
2912 if (getParser().ParseIdentifier(Name))
2913 return TokError("Expected an identifier");
2914
2915 if (Name == ".eh_frame")
2916 EH = true;
2917 else if (Name == ".debug_frame")
2918 Debug = true;
2919 }
2920
2921 getStreamer().EmitCFISections(EH, Debug);
2922
2923 return false;
2924}
2925
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002926/// ParseDirectiveCFIStartProc
2927/// ::= .cfi_startproc
2928bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2929 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002930 getStreamer().EmitCFIStartProc();
2931 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002932}
2933
2934/// ParseDirectiveCFIEndProc
2935/// ::= .cfi_endproc
2936bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002937 getStreamer().EmitCFIEndProc();
2938 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002939}
2940
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002941/// ParseRegisterOrRegisterNumber - parse register name or number.
2942bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2943 SMLoc DirectiveLoc) {
2944 unsigned RegNo;
2945
Jim Grosbach6f888a82011-06-02 17:14:04 +00002946 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002947 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2948 DirectiveLoc))
2949 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002950 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002951 } else
2952 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002953
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002954 return false;
2955}
2956
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002957/// ParseDirectiveCFIDefCfa
2958/// ::= .cfi_def_cfa register, offset
2959bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2960 SMLoc DirectiveLoc) {
2961 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002962 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002963 return true;
2964
2965 if (getLexer().isNot(AsmToken::Comma))
2966 return TokError("unexpected token in directive");
2967 Lex();
2968
2969 int64_t Offset = 0;
2970 if (getParser().ParseAbsoluteExpression(Offset))
2971 return true;
2972
Rafael Espindola066c2f42011-04-12 23:59:07 +00002973 getStreamer().EmitCFIDefCfa(Register, Offset);
2974 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002975}
2976
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002977/// ParseDirectiveCFIDefCfaOffset
2978/// ::= .cfi_def_cfa_offset offset
2979bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2980 SMLoc DirectiveLoc) {
2981 int64_t Offset = 0;
2982 if (getParser().ParseAbsoluteExpression(Offset))
2983 return true;
2984
Rafael Espindola066c2f42011-04-12 23:59:07 +00002985 getStreamer().EmitCFIDefCfaOffset(Offset);
2986 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002987}
2988
2989/// ParseDirectiveCFIAdjustCfaOffset
2990/// ::= .cfi_adjust_cfa_offset adjustment
2991bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2992 SMLoc DirectiveLoc) {
2993 int64_t Adjustment = 0;
2994 if (getParser().ParseAbsoluteExpression(Adjustment))
2995 return true;
2996
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002997 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2998 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002999}
3000
3001/// ParseDirectiveCFIDefCfaRegister
3002/// ::= .cfi_def_cfa_register register
3003bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
3004 SMLoc DirectiveLoc) {
3005 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003006 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003007 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003008
Rafael Espindola066c2f42011-04-12 23:59:07 +00003009 getStreamer().EmitCFIDefCfaRegister(Register);
3010 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003011}
3012
3013/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003014/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003015bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
3016 int64_t Register = 0;
3017 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003018
3019 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003020 return true;
3021
3022 if (getLexer().isNot(AsmToken::Comma))
3023 return TokError("unexpected token in directive");
3024 Lex();
3025
3026 if (getParser().ParseAbsoluteExpression(Offset))
3027 return true;
3028
Rafael Espindola066c2f42011-04-12 23:59:07 +00003029 getStreamer().EmitCFIOffset(Register, Offset);
3030 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003031}
3032
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003033/// ParseDirectiveCFIRelOffset
3034/// ::= .cfi_rel_offset register, offset
3035bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
3036 SMLoc DirectiveLoc) {
3037 int64_t Register = 0;
3038
3039 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3040 return true;
3041
3042 if (getLexer().isNot(AsmToken::Comma))
3043 return TokError("unexpected token in directive");
3044 Lex();
3045
3046 int64_t Offset = 0;
3047 if (getParser().ParseAbsoluteExpression(Offset))
3048 return true;
3049
Rafael Espindola25f492e2011-04-12 16:12:03 +00003050 getStreamer().EmitCFIRelOffset(Register, Offset);
3051 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003052}
3053
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003054static bool isValidEncoding(int64_t Encoding) {
3055 if (Encoding & ~0xff)
3056 return false;
3057
3058 if (Encoding == dwarf::DW_EH_PE_omit)
3059 return true;
3060
3061 const unsigned Format = Encoding & 0xf;
3062 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3063 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3064 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3065 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3066 return false;
3067
Rafael Espindolacaf11582010-12-29 04:31:26 +00003068 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003069 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00003070 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003071 return false;
3072
3073 return true;
3074}
3075
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003076/// ParseDirectiveCFIPersonalityOrLsda
3077/// ::= .cfi_personality encoding, [symbol_name]
3078/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003079bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003080 SMLoc DirectiveLoc) {
3081 int64_t Encoding = 0;
3082 if (getParser().ParseAbsoluteExpression(Encoding))
3083 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003084 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003085 return false;
3086
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003087 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003088 return TokError("unsupported encoding.");
3089
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003090 if (getLexer().isNot(AsmToken::Comma))
3091 return TokError("unexpected token in directive");
3092 Lex();
3093
3094 StringRef Name;
3095 if (getParser().ParseIdentifier(Name))
3096 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003097
3098 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3099
3100 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00003101 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003102 else {
3103 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00003104 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003105 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00003106 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003107}
3108
Rafael Espindolafe024d02010-12-28 18:36:23 +00003109/// ParseDirectiveCFIRememberState
3110/// ::= .cfi_remember_state
3111bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
3112 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003113 getStreamer().EmitCFIRememberState();
3114 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003115}
3116
3117/// ParseDirectiveCFIRestoreState
3118/// ::= .cfi_remember_state
3119bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
3120 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003121 getStreamer().EmitCFIRestoreState();
3122 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003123}
3124
Rafael Espindolac5754392011-04-12 15:31:05 +00003125/// ParseDirectiveCFISameValue
3126/// ::= .cfi_same_value register
3127bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
3128 SMLoc DirectiveLoc) {
3129 int64_t Register = 0;
3130
3131 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3132 return true;
3133
3134 getStreamer().EmitCFISameValue(Register);
3135
3136 return false;
3137}
3138
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003139/// ParseDirectiveCFIRestore
3140/// ::= .cfi_restore register
3141bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003142 SMLoc DirectiveLoc) {
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003143 int64_t Register = 0;
3144 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3145 return true;
3146
3147 getStreamer().EmitCFIRestore(Register);
3148
3149 return false;
3150}
3151
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003152/// ParseDirectiveCFIEscape
3153/// ::= .cfi_escape expression[,...]
3154bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003155 SMLoc DirectiveLoc) {
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003156 std::string Values;
3157 int64_t CurrValue;
3158 if (getParser().ParseAbsoluteExpression(CurrValue))
3159 return true;
3160
3161 Values.push_back((uint8_t)CurrValue);
3162
3163 while (getLexer().is(AsmToken::Comma)) {
3164 Lex();
3165
3166 if (getParser().ParseAbsoluteExpression(CurrValue))
3167 return true;
3168
3169 Values.push_back((uint8_t)CurrValue);
3170 }
3171
3172 getStreamer().EmitCFIEscape(Values);
3173 return false;
3174}
3175
Rafael Espindola16d7d432012-01-23 21:51:52 +00003176/// ParseDirectiveCFISignalFrame
3177/// ::= .cfi_signal_frame
3178bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
3179 SMLoc DirectiveLoc) {
3180 if (getLexer().isNot(AsmToken::EndOfStatement))
3181 return Error(getLexer().getLoc(),
3182 "unexpected token in '" + Directive + "' directive");
3183
3184 getStreamer().EmitCFISignalFrame();
3185
3186 return false;
3187}
3188
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003189/// ParseDirectiveMacrosOnOff
3190/// ::= .macros_on
3191/// ::= .macros_off
3192bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
3193 SMLoc DirectiveLoc) {
3194 if (getLexer().isNot(AsmToken::EndOfStatement))
3195 return Error(getLexer().getLoc(),
3196 "unexpected token in '" + Directive + "' directive");
3197
3198 getParser().MacrosEnabled = Directive == ".macros_on";
3199
3200 return false;
3201}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003202
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003203/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00003204/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003205bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
3206 SMLoc DirectiveLoc) {
3207 StringRef Name;
3208 if (getParser().ParseIdentifier(Name))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003209 return TokError("expected identifier in '.macro' directive");
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003210
Rafael Espindola8a403d32012-08-08 14:51:03 +00003211 MacroParameters Parameters;
Preston Gurd7b6f2032012-09-19 20:36:12 +00003212 // Argument delimiter is initially unknown. It will be set by
3213 // ParseMacroArgument()
3214 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindola65366442011-06-05 02:43:45 +00003215 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Rafael Espindola7996d042012-08-21 16:06:48 +00003216 for (;;) {
3217 MacroParameter Parameter;
Preston Gurd6c9176a2012-09-19 20:29:04 +00003218 if (getParser().ParseIdentifier(Parameter.first))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003219 return TokError("expected identifier in '.macro' directive");
Preston Gurd6c9176a2012-09-19 20:29:04 +00003220
3221 if (getLexer().is(AsmToken::Equal)) {
3222 Lex();
Preston Gurd7b6f2032012-09-19 20:36:12 +00003223 if (getParser().ParseMacroArgument(Parameter.second, ArgumentDelimiter))
Preston Gurd6c9176a2012-09-19 20:29:04 +00003224 return true;
3225 }
3226
Rafael Espindola65366442011-06-05 02:43:45 +00003227 Parameters.push_back(Parameter);
3228
Preston Gurd7b6f2032012-09-19 20:36:12 +00003229 if (getLexer().is(AsmToken::Comma))
3230 Lex();
3231 else if (getLexer().is(AsmToken::EndOfStatement))
Rafael Espindola65366442011-06-05 02:43:45 +00003232 break;
Rafael Espindola65366442011-06-05 02:43:45 +00003233 }
3234 }
3235
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003236 // Eat the end of statement.
3237 Lex();
3238
3239 AsmToken EndToken, StartToken = getTok();
3240
3241 // Lex the macro definition.
3242 for (;;) {
3243 // Check whether we have reached the end of the file.
3244 if (getLexer().is(AsmToken::Eof))
3245 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3246
3247 // Otherwise, check whether we have reach the .endmacro.
3248 if (getLexer().is(AsmToken::Identifier) &&
3249 (getTok().getIdentifier() == ".endm" ||
3250 getTok().getIdentifier() == ".endmacro")) {
3251 EndToken = getTok();
3252 Lex();
3253 if (getLexer().isNot(AsmToken::EndOfStatement))
3254 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3255 "' directive");
3256 break;
3257 }
3258
3259 // Otherwise, scan til the end of the statement.
3260 getParser().EatToEndOfStatement();
3261 }
3262
3263 if (getParser().MacroMap.lookup(Name)) {
3264 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3265 }
3266
3267 const char *BodyStart = StartToken.getLoc().getPointer();
3268 const char *BodyEnd = EndToken.getLoc().getPointer();
3269 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00003270 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003271 return false;
3272}
3273
3274/// ParseDirectiveEndMacro
3275/// ::= .endm
3276/// ::= .endmacro
3277bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
Rafael Espindola8a403d32012-08-08 14:51:03 +00003278 SMLoc DirectiveLoc) {
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003279 if (getLexer().isNot(AsmToken::EndOfStatement))
3280 return TokError("unexpected token in '" + Directive + "' directive");
3281
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003282 // If we are inside a macro instantiation, terminate the current
3283 // instantiation.
3284 if (!getParser().ActiveMacros.empty()) {
3285 getParser().HandleMacroExit();
3286 return false;
3287 }
3288
3289 // Otherwise, this .endmacro is a stray entry in the file; well formed
3290 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003291 return TokError("unexpected '" + Directive + "' in file, "
3292 "no current macro definition");
3293}
3294
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003295/// ParseDirectivePurgeMacro
3296/// ::= .purgem
3297bool GenericAsmParser::ParseDirectivePurgeMacro(StringRef Directive,
3298 SMLoc DirectiveLoc) {
3299 StringRef Name;
3300 if (getParser().ParseIdentifier(Name))
3301 return TokError("expected identifier in '.purgem' directive");
3302
3303 if (getLexer().isNot(AsmToken::EndOfStatement))
3304 return TokError("unexpected token in '.purgem' directive");
3305
3306 StringMap<Macro*>::iterator I = getParser().MacroMap.find(Name);
3307 if (I == getParser().MacroMap.end())
3308 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3309
3310 // Undefine the macro.
3311 delete I->getValue();
3312 getParser().MacroMap.erase(I);
3313 return false;
3314}
3315
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003316bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00003317 getParser().CheckForValidSection();
3318
3319 const MCExpr *Value;
3320
3321 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003322 return true;
3323
3324 if (getLexer().isNot(AsmToken::EndOfStatement))
3325 return TokError("unexpected token in directive");
3326
3327 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00003328 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003329 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00003330 getStreamer().EmitULEB128Value(Value);
3331
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003332 return false;
3333}
3334
Rafael Espindola761cb062012-06-03 23:57:14 +00003335Macro *AsmParser::ParseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003336 AsmToken EndToken, StartToken = getTok();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003337
Rafael Espindola761cb062012-06-03 23:57:14 +00003338 unsigned NestLevel = 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003339 for (;;) {
3340 // Check whether we have reached the end of the file.
Rafael Espindola761cb062012-06-03 23:57:14 +00003341 if (getLexer().is(AsmToken::Eof)) {
3342 Error(DirectiveLoc, "no matching '.endr' in definition");
3343 return 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003344 }
3345
Rafael Espindola761cb062012-06-03 23:57:14 +00003346 if (Lexer.is(AsmToken::Identifier) &&
3347 (getTok().getIdentifier() == ".rept")) {
3348 ++NestLevel;
3349 }
3350
3351 // Otherwise, check whether we have reached the .endr.
3352 if (Lexer.is(AsmToken::Identifier) &&
3353 getTok().getIdentifier() == ".endr") {
3354 if (NestLevel == 0) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003355 EndToken = getTok();
3356 Lex();
Rafael Espindola761cb062012-06-03 23:57:14 +00003357 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3358 TokError("unexpected token in '.endr' directive");
3359 return 0;
3360 }
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003361 break;
3362 }
Rafael Espindola761cb062012-06-03 23:57:14 +00003363 --NestLevel;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003364 }
3365
Rafael Espindola761cb062012-06-03 23:57:14 +00003366 // Otherwise, scan till the end of the statement.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003367 EatToEndOfStatement();
3368 }
3369
3370 const char *BodyStart = StartToken.getLoc().getPointer();
3371 const char *BodyEnd = EndToken.getLoc().getPointer();
3372 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3373
Rafael Espindola761cb062012-06-03 23:57:14 +00003374 // We Are Anonymous.
3375 StringRef Name;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003376 MacroParameters Parameters;
Rafael Espindola761cb062012-06-03 23:57:14 +00003377 return new Macro(Name, Body, Parameters);
3378}
3379
3380void AsmParser::InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
3381 raw_svector_ostream &OS) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003382 OS << ".endr\n";
3383
3384 MemoryBuffer *Instantiation =
3385 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
3386
Rafael Espindola761cb062012-06-03 23:57:14 +00003387 // Create the macro instantiation object and add to the current macro
3388 // instantiation stack.
3389 MacroInstantiation *MI = new MacroInstantiation(M, DirectiveLoc,
3390 getTok().getLoc(),
3391 Instantiation);
3392 ActiveMacros.push_back(MI);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003393
Rafael Espindola761cb062012-06-03 23:57:14 +00003394 // Jump to the macro instantiation and prime the lexer.
3395 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3396 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3397 Lex();
3398}
3399
3400bool AsmParser::ParseDirectiveRept(SMLoc DirectiveLoc) {
3401 int64_t Count;
3402 if (ParseAbsoluteExpression(Count))
3403 return TokError("unexpected token in '.rept' directive");
3404
3405 if (Count < 0)
3406 return TokError("Count is negative");
3407
3408 if (Lexer.isNot(AsmToken::EndOfStatement))
3409 return TokError("unexpected token in '.rept' directive");
3410
3411 // Eat the end of statement.
3412 Lex();
3413
3414 // Lex the rept definition.
3415 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3416 if (!M)
3417 return true;
3418
3419 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3420 // to hold the macro body with substitutions.
3421 SmallString<256> Buf;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003422 MacroParameters Parameters;
3423 MacroArguments A;
Rafael Espindola761cb062012-06-03 23:57:14 +00003424 raw_svector_ostream OS(Buf);
3425 while (Count--) {
3426 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3427 return true;
3428 }
3429 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003430
3431 return false;
3432}
3433
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003434/// ParseDirectiveIrp
3435/// ::= .irp symbol,values
3436bool AsmParser::ParseDirectiveIrp(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003437 MacroParameters Parameters;
3438 MacroParameter Parameter;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003439
Preston Gurd6c9176a2012-09-19 20:29:04 +00003440 if (ParseIdentifier(Parameter.first))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003441 return TokError("expected identifier in '.irp' directive");
3442
3443 Parameters.push_back(Parameter);
3444
3445 if (Lexer.isNot(AsmToken::Comma))
3446 return TokError("expected comma in '.irp' directive");
3447
3448 Lex();
3449
Rafael Espindola8a403d32012-08-08 14:51:03 +00003450 MacroArguments A;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003451 if (ParseMacroArguments(0, A))
3452 return true;
3453
3454 // Eat the end of statement.
3455 Lex();
3456
3457 // Lex the irp definition.
3458 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3459 if (!M)
3460 return true;
3461
3462 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3463 // to hold the macro body with substitutions.
3464 SmallString<256> Buf;
3465 raw_svector_ostream OS(Buf);
3466
Rafael Espindola7996d042012-08-21 16:06:48 +00003467 for (MacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
3468 MacroArguments Args;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003469 Args.push_back(*i);
3470
3471 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3472 return true;
3473 }
3474
3475 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3476
3477 return false;
3478}
3479
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003480/// ParseDirectiveIrpc
3481/// ::= .irpc symbol,values
3482bool AsmParser::ParseDirectiveIrpc(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003483 MacroParameters Parameters;
3484 MacroParameter Parameter;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003485
Preston Gurd6c9176a2012-09-19 20:29:04 +00003486 if (ParseIdentifier(Parameter.first))
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003487 return TokError("expected identifier in '.irpc' directive");
3488
3489 Parameters.push_back(Parameter);
3490
3491 if (Lexer.isNot(AsmToken::Comma))
3492 return TokError("expected comma in '.irpc' directive");
3493
3494 Lex();
3495
Rafael Espindola8a403d32012-08-08 14:51:03 +00003496 MacroArguments A;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003497 if (ParseMacroArguments(0, A))
3498 return true;
3499
3500 if (A.size() != 1 || A.front().size() != 1)
3501 return TokError("unexpected token in '.irpc' directive");
3502
3503 // Eat the end of statement.
3504 Lex();
3505
3506 // Lex the irpc definition.
3507 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3508 if (!M)
3509 return true;
3510
3511 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3512 // to hold the macro body with substitutions.
3513 SmallString<256> Buf;
3514 raw_svector_ostream OS(Buf);
3515
3516 StringRef Values = A.front().front().getString();
3517 std::size_t I, End = Values.size();
3518 for (I = 0; I < End; ++I) {
3519 MacroArgument Arg;
3520 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I+1)));
3521
Rafael Espindola8a403d32012-08-08 14:51:03 +00003522 MacroArguments Args;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003523 Args.push_back(Arg);
3524
3525 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3526 return true;
3527 }
3528
3529 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3530
3531 return false;
3532}
3533
Rafael Espindola761cb062012-06-03 23:57:14 +00003534bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) {
3535 if (ActiveMacros.empty())
Preston Gurd6579eea2012-09-19 20:23:43 +00003536 return TokError("unmatched '.endr' directive");
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003537
3538 // The only .repl that should get here are the ones created by
Rafael Espindola761cb062012-06-03 23:57:14 +00003539 // InstantiateMacroLikeBody.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003540 assert(getLexer().is(AsmToken::EndOfStatement));
3541
Rafael Espindola761cb062012-06-03 23:57:14 +00003542 HandleMacroExit();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003543 return false;
3544}
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003545
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003546/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003547MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003548 MCContext &C, MCStreamer &Out,
3549 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003550 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003551}