blob: 2266b631ab96fc79c969a2fd074b80d07464a454 [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarb95a0792010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000016#include "llvm/ADT/StringMap.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000018#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000019#include "llvm/MC/MCContext.h"
Evan Cheng94b95502011-07-26 00:24:13 +000020#include "llvm/MC/MCDwarf.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000021#include "llvm/MC/MCExpr.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000022#include "llvm/MC/MCParser/AsmCond.h"
23#include "llvm/MC/MCParser/AsmLexer.h"
24#include "llvm/MC/MCParser/MCAsmParser.h"
25#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000026#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000027#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000028#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000029#include "llvm/MC/MCSymbol.h"
Evan Cheng94b95502011-07-26 00:24:13 +000030#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000031#include "llvm/Support/CommandLine.h"
Benjamin Kramer518ff562012-01-28 15:28:41 +000032#include "llvm/Support/ErrorHandling.h"
Jim Grosbach254cf032011-06-29 16:05:14 +000033#include "llvm/Support/MathExtras.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000034#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000035#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000036#include "llvm/Support/raw_ostream.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000037#include <cctype>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000038#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000039using namespace llvm;
40
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000041static cl::opt<bool>
42FatalAssemblerWarnings("fatal-assembler-warnings",
43 cl::desc("Consider warnings as error"));
44
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000045namespace {
46
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000047/// \brief Helper class for tracking macro definitions.
Rafael Espindola28c1f6662012-06-03 22:41:23 +000048typedef std::vector<AsmToken> MacroArgument;
Rafael Espindola8a403d32012-08-08 14:51:03 +000049typedef std::vector<MacroArgument> MacroArguments;
Preston Gurd6c9176a2012-09-19 20:29:04 +000050typedef std::pair<StringRef, MacroArgument> MacroParameter;
Rafael Espindola8a403d32012-08-08 14:51:03 +000051typedef std::vector<MacroParameter> MacroParameters;
Rafael Espindola28c1f6662012-06-03 22:41:23 +000052
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000053struct Macro {
54 StringRef Name;
55 StringRef Body;
Rafael Espindola8a403d32012-08-08 14:51:03 +000056 MacroParameters Parameters;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000057
58public:
Rafael Espindola8a403d32012-08-08 14:51:03 +000059 Macro(StringRef N, StringRef B, const MacroParameters &P) :
Rafael Espindola65366442011-06-05 02:43:45 +000060 Name(N), Body(B), Parameters(P) {}
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000061};
62
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000063/// \brief Helper class for storing information about an active macro
64/// instantiation.
65struct MacroInstantiation {
66 /// The macro being instantiated.
67 const Macro *TheMacro;
68
69 /// The macro instantiation with substitutions.
70 MemoryBuffer *Instantiation;
71
72 /// The location of the instantiation.
73 SMLoc InstantiationLoc;
74
75 /// The location where parsing should resume upon instantiation completion.
76 SMLoc ExitLoc;
77
78public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000079 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000080 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000081};
82
Daniel Dunbaraef87e32010-07-18 18:31:38 +000083/// \brief The concrete assembly parser instance.
84class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000085 friend class GenericAsmParser;
86
Craig Topper85aadc02012-09-15 16:23:52 +000087 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
88 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000089private:
90 AsmLexer Lexer;
91 MCContext &Ctx;
92 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +000093 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000094 SourceMgr &SrcMgr;
Benjamin Kramer04a04262011-10-16 10:48:29 +000095 SourceMgr::DiagHandlerTy SavedDiagHandler;
96 void *SavedDiagContext;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000097 MCAsmParserExtension *GenericParser;
98 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +000099
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000100 /// This is the current buffer index we're lexing from as managed by the
101 /// SourceMgr object.
102 int CurBuffer;
103
104 AsmCond TheCondState;
105 std::vector<AsmCond> TheCondStack;
106
107 /// DirectiveMap - This is a table handlers for directives. Each handler is
108 /// invoked after the directive identifier is read and is responsible for
109 /// parsing and validating the rest of the directive. The handler is passed
110 /// in the directive name and the location of the directive keyword.
111 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000112
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000113 /// MacroMap - Map of currently defined macros.
114 StringMap<Macro*> MacroMap;
115
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000116 /// ActiveMacros - Stack of active macro instantiations.
117 std::vector<MacroInstantiation*> ActiveMacros;
118
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000119 /// Boolean tracking whether macro substitution is enabled.
120 unsigned MacrosEnabled : 1;
121
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000122 /// Flag tracking whether any errors have been encountered.
123 unsigned HadError : 1;
124
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000125 /// The values from the last parsed cpp hash file line comment if any.
126 StringRef CppHashFilename;
127 int64_t CppHashLineNumber;
128 SMLoc CppHashLoc;
129
Devang Patel0db58bf2012-01-31 18:14:05 +0000130 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
131 unsigned AssemblerDialect;
132
Preston Gurd7b6f2032012-09-19 20:36:12 +0000133 /// IsDarwin - is Darwin compatibility enabled?
134 bool IsDarwin;
135
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000136public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000137 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000138 const MCAsmInfo &MAI);
Craig Topper345d16d2012-08-29 05:48:09 +0000139 virtual ~AsmParser();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000140
141 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
142
Craig Topper345d16d2012-08-29 05:48:09 +0000143 virtual void AddDirectiveHandler(MCAsmParserExtension *Object,
144 StringRef Directive,
145 DirectiveHandler Handler) {
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000146 DirectiveMap[Directive] = std::make_pair(Object, Handler);
147 }
148
149public:
150 /// @name MCAsmParser Interface
151 /// {
152
153 virtual SourceMgr &getSourceManager() { return SrcMgr; }
154 virtual MCAsmLexer &getLexer() { return Lexer; }
155 virtual MCContext &getContext() { return Ctx; }
156 virtual MCStreamer &getStreamer() { return Out; }
Devang Patel0db58bf2012-01-31 18:14:05 +0000157 virtual unsigned getAssemblerDialect() {
158 if (AssemblerDialect == ~0U)
159 return MAI.getAssemblerDialect();
160 else
161 return AssemblerDialect;
162 }
163 virtual void setAssemblerDialect(unsigned i) {
164 AssemblerDialect = i;
165 }
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000166
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000167 virtual bool Warning(SMLoc L, const Twine &Msg,
168 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
169 virtual bool Error(SMLoc L, const Twine &Msg,
170 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000171
Craig Topper345d16d2012-08-29 05:48:09 +0000172 virtual const AsmToken &Lex();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000173
174 bool ParseExpression(const MCExpr *&Res);
175 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
176 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
177 virtual bool ParseAbsoluteExpression(int64_t &Res);
178
179 /// }
180
181private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000182 void CheckForValidSection();
183
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000184 bool ParseStatement();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000185 void EatToEndOfLine();
186 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000187
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000188 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola761cb062012-06-03 23:57:14 +0000189 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola8a403d32012-08-08 14:51:03 +0000190 const MacroParameters &Parameters,
191 const MacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +0000192 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000193 void HandleMacroExit();
194
195 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000196 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattner462b43c2011-10-16 05:47:55 +0000197 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
198 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000199 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000200 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000201
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000202 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
203 bool EnterIncludeFile(const std::string &Filename);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000204 /// ProcessIncbinFile - Process the specified file for the .incbin directive.
205 /// This returns true on failure.
206 bool ProcessIncbinFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000207
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +0000208 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000209 /// current token is not set; clients should ensure Lex() is called
210 /// subsequently.
211 void JumpToLoc(SMLoc Loc);
212
Craig Topper345d16d2012-08-29 05:48:09 +0000213 virtual void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000214
Preston Gurd7b6f2032012-09-19 20:36:12 +0000215 bool ParseMacroArgument(MacroArgument &MA,
216 AsmToken::TokenKind &ArgumentDelimiter);
Rafael Espindola8a403d32012-08-08 14:51:03 +0000217 bool ParseMacroArguments(const Macro *M, MacroArguments &A);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000218
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000219 /// \brief Parse up to the end of statement and a return the contents from the
220 /// current token until the end of the statement; the current token on exit
221 /// will be either the EndOfStatement or EOF.
Craig Topper345d16d2012-08-29 05:48:09 +0000222 virtual StringRef ParseStringToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000223
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000224 /// \brief Parse until the end of a statement or a comma is encountered,
225 /// return the contents from the current token up to the end or comma.
226 StringRef ParseStringToComma();
227
Jim Grosbach3f90a4c2012-09-13 23:11:31 +0000228 bool ParseAssignment(StringRef Name, bool allow_redef,
229 bool NoDeadStrip = false);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000230
231 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
232 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
233 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000234 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000235
236 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +0000237 /// and set \p Res to the identifier contents.
Craig Topper345d16d2012-08-29 05:48:09 +0000238 virtual bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000239
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000240 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000241
242 // ".ascii", ".asciiz", ".string"
243 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000244 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000245 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000246 bool ParseDirectiveFill(); // ".fill"
247 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000248 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000249 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000250 bool ParseDirectiveOrg(); // ".org"
251 // ".align{,32}", ".p2align{,w,l}"
252 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
253
254 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
255 /// accepts a single symbol (which should be a label or an external).
256 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000257
258 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
259
260 bool ParseDirectiveAbort(); // ".abort"
261 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000262 bool ParseDirectiveIncbin(); // ".incbin"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000263
264 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +0000265 // ".ifb" or ".ifnb", depending on ExpectBlank.
266 bool ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000267 // ".ifc" or ".ifnc", depending on ExpectEqual.
268 bool ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000269 // ".ifdef" or ".ifndef", depending on expect_defined
270 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000271 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
272 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
273 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
274
275 /// ParseEscapedString - Parse the current token as a string which may include
276 /// escaped characters and return the string contents.
277 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000278
279 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
280 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola2ec304c2012-05-12 16:31:10 +0000281
Rafael Espindola761cb062012-06-03 23:57:14 +0000282 // Macro-like directives
283 Macro *ParseMacroLikeBody(SMLoc DirectiveLoc);
284 void InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
285 raw_svector_ostream &OS);
286 bool ParseDirectiveRept(SMLoc DirectiveLoc); // ".rept"
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000287 bool ParseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
Rafael Espindolafc9216e2012-06-16 18:03:25 +0000288 bool ParseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
Rafael Espindola761cb062012-06-03 23:57:14 +0000289 bool ParseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000290};
291
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000292/// \brief Generic implementations of directive handling, etc. which is shared
293/// (or the default, at least) for all assembler parser.
294class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000295 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
296 void AddDirectiveHandler(StringRef Directive) {
297 getParser().AddDirectiveHandler(this, Directive,
298 HandleDirective<GenericAsmParser, Handler>);
299 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000300public:
301 GenericAsmParser() {}
302
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000303 AsmParser &getParser() {
304 return (AsmParser&) this->MCAsmParserExtension::getParser();
305 }
306
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000307 virtual void Initialize(MCAsmParser &Parser) {
308 // Call the base implementation.
309 this->MCAsmParserExtension::Initialize(Parser);
310
311 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000312 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
313 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
314 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000315 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000316
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000317 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000318 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
319 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000320 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
321 ".cfi_startproc");
322 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
323 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000324 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
325 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000326 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
327 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000328 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
329 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000330 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
331 ".cfi_def_cfa_register");
332 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
333 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000334 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
335 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000336 AddDirectiveHandler<
337 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
338 AddDirectiveHandler<
339 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000340 AddDirectiveHandler<
341 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
342 AddDirectiveHandler<
343 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000344 AddDirectiveHandler<
345 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000346 AddDirectiveHandler<
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000347 &GenericAsmParser::ParseDirectiveCFIRestore>(".cfi_restore");
348 AddDirectiveHandler<
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000349 &GenericAsmParser::ParseDirectiveCFIEscape>(".cfi_escape");
Rafael Espindola16d7d432012-01-23 21:51:52 +0000350 AddDirectiveHandler<
351 &GenericAsmParser::ParseDirectiveCFISignalFrame>(".cfi_signal_frame");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000352
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000353 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000354 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
355 ".macros_on");
356 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
357 ".macros_off");
358 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
359 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
360 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000361 AddDirectiveHandler<&GenericAsmParser::ParseDirectivePurgeMacro>(".purgem");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000362
363 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
364 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000365 }
366
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000367 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
368
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000369 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
370 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
371 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000372 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000373 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000374 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
375 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000376 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000377 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000378 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000379 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
380 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000381 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000382 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000383 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
384 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000385 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000386 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000387 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
Rafael Espindola16d7d432012-01-23 21:51:52 +0000388 bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000389
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000390 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000391 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
392 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000393 bool ParseDirectivePurgeMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000394
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000395 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000396};
397
398}
399
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000400namespace llvm {
401
402extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000403extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000404extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000405
406}
407
Chris Lattneraaec2052010-01-19 19:46:13 +0000408enum { DEFAULT_ADDRSPACE = 0 };
409
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000410AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000411 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000412 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000413 GenericParser(new GenericAsmParser), PlatformParser(0),
Preston Gurd7b6f2032012-09-19 20:36:12 +0000414 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0),
415 AssemblerDialect(~0U), IsDarwin(false) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000416 // Save the old handler.
417 SavedDiagHandler = SrcMgr.getDiagHandler();
418 SavedDiagContext = SrcMgr.getDiagContext();
419 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000420 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000421 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000422
423 // Initialize the generic parser.
424 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000425
426 // Initialize the platform / file format parser.
427 //
428 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
429 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000430 if (_MAI.hasMicrosoftFastStdCallMangling()) {
431 PlatformParser = createCOFFAsmParser();
432 PlatformParser->Initialize(*this);
433 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000434 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000435 PlatformParser->Initialize(*this);
Preston Gurd7b6f2032012-09-19 20:36:12 +0000436 IsDarwin = true;
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000437 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000438 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000439 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000440 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000441}
442
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000443AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000444 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
445
446 // Destroy any macros.
447 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
448 ie = MacroMap.end(); it != ie; ++it)
449 delete it->getValue();
450
Daniel Dunbare4749702010-07-12 18:12:02 +0000451 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000452 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000453}
454
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000455void AsmParser::PrintMacroInstantiations() {
456 // Print the active macro instantiation stack.
457 for (std::vector<MacroInstantiation*>::const_reverse_iterator
458 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000459 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
460 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000461}
462
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000463bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000464 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000465 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000466 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000467 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000468 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000469}
470
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000471bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000472 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000473 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000474 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000475 return true;
476}
477
Sean Callananfd0b0282010-01-21 00:19:58 +0000478bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000479 std::string IncludedFile;
480 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000481 if (NewBuf == -1)
482 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000483
Sean Callananfd0b0282010-01-21 00:19:58 +0000484 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000485
Sean Callananfd0b0282010-01-21 00:19:58 +0000486 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000487
Sean Callananfd0b0282010-01-21 00:19:58 +0000488 return false;
489}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000490
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000491/// Process the specified .incbin file by seaching for it in the include paths
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000492/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000493/// returns true on failure.
494bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
495 std::string IncludedFile;
496 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
497 if (NewBuf == -1)
498 return true;
499
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000500 // Pick up the bytes from the file and emit them.
Kevin Enderbydac29532011-12-15 00:00:27 +0000501 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
502 DEFAULT_ADDRSPACE);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000503 return false;
504}
505
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000506void AsmParser::JumpToLoc(SMLoc Loc) {
507 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
508 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
509}
510
Sean Callananfd0b0282010-01-21 00:19:58 +0000511const AsmToken &AsmParser::Lex() {
512 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000513
Sean Callananfd0b0282010-01-21 00:19:58 +0000514 if (tok->is(AsmToken::Eof)) {
515 // If this is the end of an included file, pop the parent file off the
516 // include stack.
517 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
518 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000519 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000520 tok = &Lexer.Lex();
521 }
522 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000523
Sean Callananfd0b0282010-01-21 00:19:58 +0000524 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000525 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000526
Sean Callananfd0b0282010-01-21 00:19:58 +0000527 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000528}
529
Chris Lattner79180e22010-04-05 23:15:42 +0000530bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000531 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000532 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000533 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000534
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000535 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000536 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000537
538 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000539 AsmCond StartingCondState = TheCondState;
540
Kevin Enderby613b7572011-11-01 22:27:22 +0000541 // If we are generating dwarf for assembly source files save the initial text
542 // section and generate a .file directive.
543 if (getContext().getGenDwarfForAssembly()) {
544 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000545 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
546 getStreamer().EmitLabel(SectionStartSym);
547 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000548 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
549 StringRef(), SrcMgr.getMemoryBuffer(CurBuffer)->getBufferIdentifier());
550 }
551
Chris Lattnerb717fb02009-07-02 21:53:43 +0000552 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000553 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000554 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000555
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000556 // We had an error, validate that one was emitted and recover by skipping to
557 // the next line.
558 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000559 EatToEndOfStatement();
560 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000561
562 if (TheCondState.TheCond != StartingCondState.TheCond ||
563 TheCondState.Ignore != StartingCondState.Ignore)
564 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000565
566 // Check to see there are no empty DwarfFile slots.
567 const std::vector<MCDwarfFile *> &MCDwarfFiles =
568 getContext().getMCDwarfFiles();
569 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000570 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000571 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000572 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000573
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000574 // Check to see that all assembler local symbols were actually defined.
575 // Targets that don't do subsections via symbols may not want this, though,
576 // so conservatively exclude them. Only do this if we're finalizing, though,
577 // as otherwise we won't necessarilly have seen everything yet.
578 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
579 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
580 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
581 e = Symbols.end();
582 i != e; ++i) {
583 MCSymbol *Sym = i->getValue();
584 // Variable symbols may not be marked as defined, so check those
585 // explicitly. If we know it's a variable, we have a definition for
586 // the purposes of this check.
587 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
588 // FIXME: We would really like to refer back to where the symbol was
589 // first referenced for a source location. We need to add something
590 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000591 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
592 "assembler local symbol '" + Sym->getName() +
593 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000594 }
595 }
596
597
Chris Lattner79180e22010-04-05 23:15:42 +0000598 // Finalize the output stream if there are no errors and if the client wants
599 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000600 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000601 Out.Finish();
602
Chris Lattnerb717fb02009-07-02 21:53:43 +0000603 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000604}
605
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000606void AsmParser::CheckForValidSection() {
607 if (!getStreamer().getCurrentSection()) {
608 TokError("expected section directive before assembly directive");
609 Out.SwitchSection(Ctx.getMachOSection(
610 "__TEXT", "__text",
611 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
612 0, SectionKind::getText()));
613 }
614}
615
Chris Lattner2cf5f142009-06-22 01:29:09 +0000616/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
617void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000618 while (Lexer.isNot(AsmToken::EndOfStatement) &&
619 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000620 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000621
Chris Lattner2cf5f142009-06-22 01:29:09 +0000622 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000623 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000624 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000625}
626
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000627StringRef AsmParser::ParseStringToEndOfStatement() {
628 const char *Start = getTok().getLoc().getPointer();
629
630 while (Lexer.isNot(AsmToken::EndOfStatement) &&
631 Lexer.isNot(AsmToken::Eof))
632 Lex();
633
634 const char *End = getTok().getLoc().getPointer();
635 return StringRef(Start, End - Start);
636}
Chris Lattnerc4193832009-06-22 05:51:26 +0000637
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000638StringRef AsmParser::ParseStringToComma() {
639 const char *Start = getTok().getLoc().getPointer();
640
641 while (Lexer.isNot(AsmToken::EndOfStatement) &&
642 Lexer.isNot(AsmToken::Comma) &&
643 Lexer.isNot(AsmToken::Eof))
644 Lex();
645
646 const char *End = getTok().getLoc().getPointer();
647 return StringRef(Start, End - Start);
648}
649
Chris Lattner74ec1a32009-06-22 06:32:03 +0000650/// ParseParenExpr - Parse a paren expression and return it.
651/// NOTE: This assumes the leading '(' has already been consumed.
652///
653/// parenexpr ::= expr)
654///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000655bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000656 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000657 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000658 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000659 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000660 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000661 return false;
662}
Chris Lattnerc4193832009-06-22 05:51:26 +0000663
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000664/// ParseBracketExpr - Parse a bracket expression and return it.
665/// NOTE: This assumes the leading '[' has already been consumed.
666///
667/// bracketexpr ::= expr]
668///
669bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
670 if (ParseExpression(Res)) return true;
671 if (Lexer.isNot(AsmToken::RBrac))
672 return TokError("expected ']' in brackets expression");
673 EndLoc = Lexer.getLoc();
674 Lex();
675 return false;
676}
677
Chris Lattner74ec1a32009-06-22 06:32:03 +0000678/// ParsePrimaryExpr - Parse a primary expression and return it.
679/// primaryexpr ::= (parenexpr
680/// primaryexpr ::= symbol
681/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000682/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000683/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000684bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000685 switch (Lexer.getKind()) {
686 default:
687 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000688 // If we have an error assume that we've already handled it.
689 case AsmToken::Error:
690 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000691 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000692 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000693 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000694 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000695 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000696 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000697 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000698 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000699 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000700 EndLoc = Lexer.getLoc();
701
702 StringRef Identifier;
703 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000704 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000705
Daniel Dunbarfffff912009-10-16 01:34:54 +0000706 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000707 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000708 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000709
710 // Lookup the symbol variant if used.
711 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000712 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000713 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000714 if (Variant == MCSymbolRefExpr::VK_Invalid) {
715 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000716 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000717 }
718 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000719
Daniel Dunbarfffff912009-10-16 01:34:54 +0000720 // If this is an absolute variable reference, substitute it now to preserve
721 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000722 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000723 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000724 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000725
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000726 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000727 return false;
728 }
729
730 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000731 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000732 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000733 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000734 case AsmToken::Integer: {
735 SMLoc Loc = getTok().getLoc();
736 int64_t IntVal = getTok().getIntVal();
737 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000738 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000739 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000740 // Look for 'b' or 'f' following an Integer as a directional label
741 if (Lexer.getKind() == AsmToken::Identifier) {
742 StringRef IDVal = getTok().getString();
743 if (IDVal == "f" || IDVal == "b"){
744 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
745 IDVal == "f" ? 1 : 0);
746 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
747 getContext());
Benjamin Kramer29739e72012-05-12 16:52:21 +0000748 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000749 return Error(Loc, "invalid reference to undefined symbol");
750 EndLoc = Lexer.getLoc();
751 Lex(); // Eat identifier.
752 }
753 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000754 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000755 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000756 case AsmToken::Real: {
757 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000758 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000759 Res = MCConstantExpr::Create(IntVal, getContext());
760 Lex(); // Eat token.
761 return false;
762 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000763 case AsmToken::Dot: {
764 // This is a '.' reference, which references the current PC. Emit a
765 // temporary label to the streamer and refer to it.
766 MCSymbol *Sym = Ctx.CreateTempSymbol();
767 Out.EmitLabel(Sym);
768 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
769 EndLoc = Lexer.getLoc();
770 Lex(); // Eat identifier.
771 return false;
772 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000773 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000774 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000775 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000776 case AsmToken::LBrac:
777 if (!PlatformParser->HasBracketExpressions())
778 return TokError("brackets expression not supported on this target");
779 Lex(); // Eat the '['.
780 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000781 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000782 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000783 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000784 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000785 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000786 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000787 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000788 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000789 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000790 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000791 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000792 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000793 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000794 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000795 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000796 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000797 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000798 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000799 }
800}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000801
Chris Lattnerb4307b32010-01-15 19:28:38 +0000802bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000803 SMLoc EndLoc;
804 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000805}
806
Daniel Dunbarcceba832010-09-17 02:47:07 +0000807const MCExpr *
808AsmParser::ApplyModifierToExpr(const MCExpr *E,
809 MCSymbolRefExpr::VariantKind Variant) {
810 // Recurse over the given expression, rebuilding it to apply the given variant
811 // if there is exactly one symbol.
812 switch (E->getKind()) {
813 case MCExpr::Target:
814 case MCExpr::Constant:
815 return 0;
816
817 case MCExpr::SymbolRef: {
818 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
819
820 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
821 TokError("invalid variant on expression '" +
822 getTok().getIdentifier() + "' (already modified)");
823 return E;
824 }
825
826 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
827 }
828
829 case MCExpr::Unary: {
830 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
831 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
832 if (!Sub)
833 return 0;
834 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
835 }
836
837 case MCExpr::Binary: {
838 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
839 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
840 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
841
842 if (!LHS && !RHS)
843 return 0;
844
845 if (!LHS) LHS = BE->getLHS();
846 if (!RHS) RHS = BE->getRHS();
847
848 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
849 }
850 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000851
Craig Topper85814382012-02-07 05:05:23 +0000852 llvm_unreachable("Invalid expression kind!");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000853}
854
Chris Lattner74ec1a32009-06-22 06:32:03 +0000855/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000856///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000857/// expr ::= expr &&,|| expr -> lowest.
858/// expr ::= expr |,^,&,! expr
859/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
860/// expr ::= expr <<,>> expr
861/// expr ::= expr +,- expr
862/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000863/// expr ::= primaryexpr
864///
Chris Lattner54482b42010-01-15 19:39:23 +0000865bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000866 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000867 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000868 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
869 return true;
870
Daniel Dunbarcceba832010-09-17 02:47:07 +0000871 // As a special case, we support 'a op b @ modifier' by rewriting the
872 // expression to include the modifier. This is inefficient, but in general we
873 // expect users to use 'a@modifier op b'.
874 if (Lexer.getKind() == AsmToken::At) {
875 Lex();
876
877 if (Lexer.isNot(AsmToken::Identifier))
878 return TokError("unexpected symbol modifier following '@'");
879
880 MCSymbolRefExpr::VariantKind Variant =
881 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
882 if (Variant == MCSymbolRefExpr::VK_Invalid)
883 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
884
885 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
886 if (!ModifiedRes) {
887 return TokError("invalid modifier '" + getTok().getIdentifier() +
888 "' (no symbols present)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000889 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000890
Daniel Dunbarcceba832010-09-17 02:47:07 +0000891 Res = ModifiedRes;
892 Lex();
893 }
894
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000895 // Try to constant fold it up front, if possible.
896 int64_t Value;
897 if (Res->EvaluateAsAbsolute(Value))
898 Res = MCConstantExpr::Create(Value, getContext());
899
900 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000901}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000902
Chris Lattnerb4307b32010-01-15 19:28:38 +0000903bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000904 Res = 0;
905 return ParseParenExpr(Res, EndLoc) ||
906 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000907}
908
Daniel Dunbar475839e2009-06-29 20:37:27 +0000909bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000910 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000911
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000912 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000913 if (ParseExpression(Expr))
914 return true;
915
Daniel Dunbare00b0112009-10-16 01:57:52 +0000916 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000917 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000918
919 return false;
920}
921
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000922static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000923 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000924 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000925 default:
926 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000927
Jim Grosbachfbe16812011-08-20 16:24:13 +0000928 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000929 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000930 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000931 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000932 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000933 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000934 return 1;
935
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000936
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000937 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000938 //
939 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000940 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000941 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000942 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000943 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000944 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000945 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000946 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000947 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000948 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000949
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000950 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000951 case AsmToken::EqualEqual:
952 Kind = MCBinaryExpr::EQ;
953 return 3;
954 case AsmToken::ExclaimEqual:
955 case AsmToken::LessGreater:
956 Kind = MCBinaryExpr::NE;
957 return 3;
958 case AsmToken::Less:
959 Kind = MCBinaryExpr::LT;
960 return 3;
961 case AsmToken::LessEqual:
962 Kind = MCBinaryExpr::LTE;
963 return 3;
964 case AsmToken::Greater:
965 Kind = MCBinaryExpr::GT;
966 return 3;
967 case AsmToken::GreaterEqual:
968 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000969 return 3;
970
Jim Grosbachfbe16812011-08-20 16:24:13 +0000971 // Intermediate Precedence: <<, >>
972 case AsmToken::LessLess:
973 Kind = MCBinaryExpr::Shl;
974 return 4;
975 case AsmToken::GreaterGreater:
976 Kind = MCBinaryExpr::Shr;
977 return 4;
978
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000979 // High Intermediate Precedence: +, -
980 case AsmToken::Plus:
981 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000982 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000983 case AsmToken::Minus:
984 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000985 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000986
Jim Grosbachfbe16812011-08-20 16:24:13 +0000987 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +0000988 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000989 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000990 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000991 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000992 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000993 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000994 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000995 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000996 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000997 }
998}
999
1000
1001/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
1002/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +00001003bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
1004 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001005 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001006 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001007 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001008
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001009 // If the next token is lower precedence than we are allowed to eat, return
1010 // successfully with what we ate already.
1011 if (TokPrec < Precedence)
1012 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001013
Sean Callanan79ed1a82010-01-19 20:22:31 +00001014 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001015
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001016 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001017 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001018 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001019
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001020 // If BinOp binds less tightly with RHS than the operator after RHS, let
1021 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001022 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001023 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001024 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001025 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001026 }
1027
Daniel Dunbar475839e2009-06-29 20:37:27 +00001028 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001029 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001030 }
1031}
1032
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001033
1034
1035
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001036/// ParseStatement:
1037/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +00001038/// ::= Label* Directive ...Operands... EndOfStatement
1039/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001040bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001041 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001042 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001043 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001044 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001045 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001046
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001047 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +00001048 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001049 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001050 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001051 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001052 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001053 if (Lexer.is(AsmToken::Hash))
1054 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001055
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001056 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001057 if (Lexer.is(AsmToken::Integer)) {
1058 LocalLabelVal = getTok().getIntVal();
1059 if (LocalLabelVal < 0) {
1060 if (!TheCondState.Ignore)
1061 return TokError("unexpected token at start of statement");
1062 IDVal = "";
1063 }
1064 else {
1065 IDVal = getTok().getString();
1066 Lex(); // Consume the integer token to be used as an identifier token.
1067 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001068 if (!TheCondState.Ignore)
1069 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001070 }
1071 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001072
1073 } else if (Lexer.is(AsmToken::Dot)) {
1074 // Treat '.' as a valid identifier in this context.
1075 Lex();
1076 IDVal = ".";
1077
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001078 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001079 if (!TheCondState.Ignore)
1080 return TokError("unexpected token at start of statement");
1081 IDVal = "";
1082 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001083
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001084
Chris Lattner7834fac2010-04-17 18:14:27 +00001085 // Handle conditional assembly here before checking for skipping. We
1086 // have to do this so that .endif isn't skipped in a ".if 0" block for
1087 // example.
1088 if (IDVal == ".if")
1089 return ParseDirectiveIf(IDLoc);
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00001090 if (IDVal == ".ifb")
1091 return ParseDirectiveIfb(IDLoc, true);
1092 if (IDVal == ".ifnb")
1093 return ParseDirectiveIfb(IDLoc, false);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00001094 if (IDVal == ".ifc")
1095 return ParseDirectiveIfc(IDLoc, true);
1096 if (IDVal == ".ifnc")
1097 return ParseDirectiveIfc(IDLoc, false);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00001098 if (IDVal == ".ifdef")
1099 return ParseDirectiveIfdef(IDLoc, true);
1100 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
1101 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +00001102 if (IDVal == ".elseif")
1103 return ParseDirectiveElseIf(IDLoc);
1104 if (IDVal == ".else")
1105 return ParseDirectiveElse(IDLoc);
1106 if (IDVal == ".endif")
1107 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001108
Chris Lattner7834fac2010-04-17 18:14:27 +00001109 // If we are in a ".if 0" block, ignore this statement.
1110 if (TheCondState.Ignore) {
1111 EatToEndOfStatement();
1112 return false;
1113 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001114
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001115 // FIXME: Recurse on local labels?
1116
1117 // See what kind of statement we have.
1118 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001119 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001120 CheckForValidSection();
1121
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001122 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001123 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001124
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001125 // Diagnose attempt to use '.' as a label.
1126 if (IDVal == ".")
1127 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1128
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001129 // Diagnose attempt to use a variable as a label.
1130 //
1131 // FIXME: Diagnostics. Note the location of the definition as a label.
1132 // FIXME: This doesn't diagnose assignment to a symbol which has been
1133 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001134 MCSymbol *Sym;
1135 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001136 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001137 else
1138 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001139 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001140 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001141
Daniel Dunbar959fd882009-08-26 22:13:22 +00001142 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001143 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001144
Kevin Enderby94c2e852011-12-09 18:09:40 +00001145 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001146 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001147 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001148 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1149 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001150
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001151 // Consume any end of statement token, if present, to avoid spurious
1152 // AddBlankLine calls().
1153 if (Lexer.is(AsmToken::EndOfStatement)) {
1154 Lex();
1155 if (Lexer.is(AsmToken::Eof))
1156 return false;
1157 }
1158
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001159 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001160 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001161
Daniel Dunbar3f872332009-07-28 16:08:33 +00001162 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001163 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001164 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001165
Nico Weber4c4c7322011-01-28 03:04:41 +00001166 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001167
1168 default: // Normal instruction or directive.
1169 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001170 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001171
1172 // If macros are enabled, check to see if this is a macro instantiation.
1173 if (MacrosEnabled)
1174 if (const Macro *M = MacroMap.lookup(IDVal))
1175 return HandleMacroEntry(IDVal, IDLoc, M);
1176
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001177 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001178 if (IDVal[0] == '.' && IDVal != ".") {
Akira Hatanaka3b02d952012-07-05 19:09:33 +00001179
1180 // Target hook for parsing target specific directives.
1181 if (!getTargetParser().ParseDirective(ID))
1182 return false;
1183
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001184 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001185 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001186 return ParseDirectiveSet(IDVal, true);
1187 if (IDVal == ".equiv")
1188 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001189
Daniel Dunbara0d14262009-06-24 23:30:00 +00001190 // Data directives
1191
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001192 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001193 return ParseDirectiveAscii(IDVal, false);
1194 if (IDVal == ".asciz" || IDVal == ".string")
1195 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001196
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001197 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001198 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001199 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001200 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001201 if (IDVal == ".value")
1202 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001203 if (IDVal == ".2byte")
1204 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001205 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001206 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001207 if (IDVal == ".int")
1208 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001209 if (IDVal == ".4byte")
1210 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001211 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001212 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001213 if (IDVal == ".8byte")
1214 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001215 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001216 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1217 if (IDVal == ".double")
1218 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001219
Eli Friedman5d68ec22010-07-19 04:17:25 +00001220 if (IDVal == ".align") {
1221 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1222 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1223 }
1224 if (IDVal == ".align32") {
1225 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1226 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1227 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001228 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001229 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001230 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001231 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001232 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001233 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001234 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001235 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001236 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001237 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001238 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001239 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1240
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001241 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001242 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001243
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001244 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001245 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001246 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001247 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001248 if (IDVal == ".zero")
1249 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001250
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001251 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001252
Benjamin Kramere14a3c52012-05-12 11:18:59 +00001253 if (IDVal == ".extern") {
1254 EatToEndOfStatement(); // .extern is the default, ignore it.
1255 return false;
1256 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001257 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001258 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001259 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001260 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001261 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001262 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001263 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001264 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001265 if (IDVal == ".symbol_resolver")
1266 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001267 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001268 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001269 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001270 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001271 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001272 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001273 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001274 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001275 if (IDVal == ".weak_def_can_be_hidden")
1276 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001277
Hans Wennborg5cc64912011-06-18 13:51:54 +00001278 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001279 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001280 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001281 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001282
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001283 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001284 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001285 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001286 return ParseDirectiveInclude();
Kevin Enderbyc55acca2011-12-14 21:47:48 +00001287 if (IDVal == ".incbin")
1288 return ParseDirectiveIncbin();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001289
Benjamin Kramer5cdf0ad2012-05-12 11:19:04 +00001290 if (IDVal == ".code16" || IDVal == ".code16gcc")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001291 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001292
Rafael Espindola761cb062012-06-03 23:57:14 +00001293 // Macro-like directives
Rafael Espindola2ec304c2012-05-12 16:31:10 +00001294 if (IDVal == ".rept")
1295 return ParseDirectiveRept(IDLoc);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001296 if (IDVal == ".irp")
1297 return ParseDirectiveIrp(IDLoc);
Rafael Espindolafc9216e2012-06-16 18:03:25 +00001298 if (IDVal == ".irpc")
1299 return ParseDirectiveIrpc(IDLoc);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00001300 if (IDVal == ".endr")
Rafael Espindola761cb062012-06-03 23:57:14 +00001301 return ParseDirectiveEndr(IDLoc);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00001302
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001303 // Look up the handler in the handler table.
1304 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1305 DirectiveMap.lookup(IDVal);
1306 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001307 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001308
Kevin Enderby9c656452009-09-10 20:51:44 +00001309
Jim Grosbach686c0182012-05-01 18:38:27 +00001310 return Error(IDLoc, "unknown directive");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001311 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001312
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001313 CheckForValidSection();
1314
Chris Lattnera7f13542010-05-19 23:34:33 +00001315 // Canonicalize the opcode to lower case.
1316 SmallString<128> Opcode;
1317 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1318 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001319
Chris Lattner98986712010-01-14 22:21:20 +00001320 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001321 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001322 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001323
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001324 // Dump the parsed representation, if requested.
1325 if (getShowParsedOperands()) {
1326 SmallString<256> Str;
1327 raw_svector_ostream OS(Str);
1328 OS << "parsed instruction: [";
1329 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1330 if (i != 0)
1331 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001332 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001333 }
1334 OS << "]";
1335
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001336 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001337 }
1338
Kevin Enderby613b7572011-11-01 22:27:22 +00001339 // If we are generating dwarf for assembly source files and the current
1340 // section is the initial text section then generate a .loc directive for
1341 // the instruction.
1342 if (!HadError && getContext().getGenDwarfForAssembly() &&
1343 getContext().getGenDwarfSection() == getStreamer().getCurrentSection() ) {
1344 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
1345 SrcMgr.FindLineNumber(IDLoc, CurBuffer),
1346 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001347 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001348 StringRef());
1349 }
1350
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001351 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001352 if (!HadError)
1353 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1354 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001355
Chris Lattner98986712010-01-14 22:21:20 +00001356 // Free any parsed operands.
1357 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1358 delete ParsedOperands[i];
1359
Chris Lattnercbf8a982010-09-11 16:18:25 +00001360 // Don't skip the rest of the line, the instruction parser is responsible for
1361 // that.
1362 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001363}
Chris Lattner9a023f72009-06-24 04:43:34 +00001364
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001365/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1366/// since they may not be able to be tokenized to get to the end of line token.
1367void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001368 if (!Lexer.is(AsmToken::EndOfStatement))
1369 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001370 // Eat EOL.
1371 Lex();
1372}
1373
1374/// ParseCppHashLineFilenameComment as this:
1375/// ::= # number "filename"
1376/// or just as a full line comment if it doesn't have a number and a string.
1377bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1378 Lex(); // Eat the hash token.
1379
1380 if (getLexer().isNot(AsmToken::Integer)) {
1381 // Consume the line since in cases it is not a well-formed line directive,
1382 // as if were simply a full line comment.
1383 EatToEndOfLine();
1384 return false;
1385 }
1386
1387 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001388 Lex();
1389
1390 if (getLexer().isNot(AsmToken::String)) {
1391 EatToEndOfLine();
1392 return false;
1393 }
1394
1395 StringRef Filename = getTok().getString();
1396 // Get rid of the enclosing quotes.
1397 Filename = Filename.substr(1, Filename.size()-2);
1398
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001399 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1400 CppHashLoc = L;
1401 CppHashFilename = Filename;
1402 CppHashLineNumber = LineNumber;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001403
1404 // Ignore any trailing characters, they're just comment.
1405 EatToEndOfLine();
1406 return false;
1407}
1408
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001409/// DiagHandler - will use the last parsed cpp hash line filename comment
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001410/// for the Filename and LineNo if any in the diagnostic.
1411void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1412 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1413 raw_ostream &OS = errs();
1414
1415 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1416 const SMLoc &DiagLoc = Diag.getLoc();
1417 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1418 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1419
1420 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1421 // before printing the message.
1422 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001423 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001424 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1425 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1426 }
1427
1428 // If we have not parsed a cpp hash line filename comment or the source
1429 // manager changed or buffer changed (like in a nested include) then just
1430 // print the normal diagnostic using its Filename and LineNo.
1431 if (!Parser->CppHashLineNumber ||
1432 &DiagSrcMgr != &Parser->SrcMgr ||
1433 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001434 if (Parser->SavedDiagHandler)
1435 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1436 else
1437 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001438 return;
1439 }
1440
1441 // Use the CppHashFilename and calculate a line number based on the
1442 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1443 // the diagnostic.
1444 const std::string Filename = Parser->CppHashFilename;
1445
1446 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1447 int CppHashLocLineNo =
1448 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1449 int LineNo = Parser->CppHashLineNumber - 1 +
1450 (DiagLocLineNo - CppHashLocLineNo);
1451
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001452 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1453 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001454 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001455 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001456
Benjamin Kramer04a04262011-10-16 10:48:29 +00001457 if (Parser->SavedDiagHandler)
1458 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1459 else
1460 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001461}
1462
Rafael Espindola799aacf2012-08-21 18:29:30 +00001463// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1464// difference being that that function accepts '@' as part of identifiers and
1465// we can't do that. AsmLexer.cpp should probably be changed to handle
1466// '@' as a special case when needed.
1467static bool isIdentifierChar(char c) {
1468 return isalnum(c) || c == '_' || c == '$' || c == '.';
1469}
1470
Rafael Espindola761cb062012-06-03 23:57:14 +00001471bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola8a403d32012-08-08 14:51:03 +00001472 const MacroParameters &Parameters,
1473 const MacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +00001474 const SMLoc &L) {
Rafael Espindola65366442011-06-05 02:43:45 +00001475 unsigned NParameters = Parameters.size();
1476 if (NParameters != 0 && NParameters != A.size())
1477 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001478
Preston Gurd7b6f2032012-09-19 20:36:12 +00001479 // A macro without parameters is handled differently on Darwin:
1480 // gas accepts no arguments and does no substitutions
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001481 while (!Body.empty()) {
1482 // Scan for the next substitution.
1483 std::size_t End = Body.size(), Pos = 0;
1484 for (; Pos != End; ++Pos) {
1485 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001486 if (!NParameters) {
1487 // This macro has no parameters, look for $0, $1, etc.
1488 if (Body[Pos] != '$' || Pos + 1 == End)
1489 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001490
Rafael Espindola65366442011-06-05 02:43:45 +00001491 char Next = Body[Pos + 1];
1492 if (Next == '$' || Next == 'n' || isdigit(Next))
1493 break;
1494 } else {
1495 // This macro has parameters, look for \foo, \bar, etc.
1496 if (Body[Pos] == '\\' && Pos + 1 != End)
1497 break;
1498 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001499 }
1500
1501 // Add the prefix.
1502 OS << Body.slice(0, Pos);
1503
1504 // Check if we reached the end.
1505 if (Pos == End)
1506 break;
1507
Rafael Espindola65366442011-06-05 02:43:45 +00001508 if (!NParameters) {
1509 switch (Body[Pos+1]) {
1510 // $$ => $
1511 case '$':
1512 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001513 break;
1514
Rafael Espindola65366442011-06-05 02:43:45 +00001515 // $n => number of arguments
1516 case 'n':
1517 OS << A.size();
1518 break;
1519
1520 // $[0-9] => argument
1521 default: {
1522 // Missing arguments are ignored.
1523 unsigned Index = Body[Pos+1] - '0';
1524 if (Index >= A.size())
1525 break;
1526
1527 // Otherwise substitute with the token values, with spaces eliminated.
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001528 for (MacroArgument::const_iterator it = A[Index].begin(),
Rafael Espindola65366442011-06-05 02:43:45 +00001529 ie = A[Index].end(); it != ie; ++it)
1530 OS << it->getString();
1531 break;
1532 }
1533 }
1534 Pos += 2;
1535 } else {
1536 unsigned I = Pos + 1;
Rafael Espindola799aacf2012-08-21 18:29:30 +00001537 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola65366442011-06-05 02:43:45 +00001538 ++I;
1539
1540 const char *Begin = Body.data() + Pos +1;
1541 StringRef Argument(Begin, I - (Pos +1));
1542 unsigned Index = 0;
1543 for (; Index < NParameters; ++Index)
Preston Gurd6c9176a2012-09-19 20:29:04 +00001544 if (Parameters[Index].first == Argument)
Rafael Espindola65366442011-06-05 02:43:45 +00001545 break;
1546
Preston Gurd7b6f2032012-09-19 20:36:12 +00001547 if (Index == NParameters) {
1548 if (Body[Pos+1] == '(' && Body[Pos+2] == ')')
1549 Pos += 3;
1550 else {
1551 OS << '\\' << Argument;
1552 Pos = I;
1553 }
1554 } else {
1555 for (MacroArgument::const_iterator it = A[Index].begin(),
1556 ie = A[Index].end(); it != ie; ++it)
1557 if (it->getKind() == AsmToken::String)
1558 OS << it->getStringContents();
1559 else
1560 OS << it->getString();
Rafael Espindola65366442011-06-05 02:43:45 +00001561
Preston Gurd7b6f2032012-09-19 20:36:12 +00001562 Pos += 1 + Argument.size();
1563 }
Rafael Espindola65366442011-06-05 02:43:45 +00001564 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001565 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001566 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001567 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001568
Rafael Espindola65366442011-06-05 02:43:45 +00001569 return false;
1570}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001571
Rafael Espindola65366442011-06-05 02:43:45 +00001572MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1573 MemoryBuffer *I)
1574 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1575{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001576}
1577
Preston Gurd7b6f2032012-09-19 20:36:12 +00001578static bool IsOperator(AsmToken::TokenKind kind)
1579{
1580 switch (kind)
1581 {
1582 default:
1583 return false;
1584 case AsmToken::Plus:
1585 case AsmToken::Minus:
1586 case AsmToken::Tilde:
1587 case AsmToken::Slash:
1588 case AsmToken::Star:
1589 case AsmToken::Dot:
1590 case AsmToken::Equal:
1591 case AsmToken::EqualEqual:
1592 case AsmToken::Pipe:
1593 case AsmToken::PipePipe:
1594 case AsmToken::Caret:
1595 case AsmToken::Amp:
1596 case AsmToken::AmpAmp:
1597 case AsmToken::Exclaim:
1598 case AsmToken::ExclaimEqual:
1599 case AsmToken::Percent:
1600 case AsmToken::Less:
1601 case AsmToken::LessEqual:
1602 case AsmToken::LessLess:
1603 case AsmToken::LessGreater:
1604 case AsmToken::Greater:
1605 case AsmToken::GreaterEqual:
1606 case AsmToken::GreaterGreater:
1607 return true;
1608 }
1609}
1610
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001611/// ParseMacroArgument - Extract AsmTokens for a macro argument.
1612/// This is used for both default macro parameter values and the
1613/// arguments in macro invocations
Preston Gurd7b6f2032012-09-19 20:36:12 +00001614bool AsmParser::ParseMacroArgument(MacroArgument &MA,
1615 AsmToken::TokenKind &ArgumentDelimiter) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001616 unsigned ParenLevel = 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001617 unsigned AddTokens = 0;
1618
1619 // gas accepts arguments separated by whitespace, except on Darwin
1620 if (!IsDarwin)
1621 Lexer.setSkipSpace(false);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001622
1623 for (;;) {
Preston Gurd7b6f2032012-09-19 20:36:12 +00001624 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) {
1625 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001626 return TokError("unexpected token in macro instantiation");
Preston Gurd7b6f2032012-09-19 20:36:12 +00001627 }
1628
1629 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1630 // Spaces and commas cannot be mixed to delimit parameters
1631 if (ArgumentDelimiter == AsmToken::Eof)
1632 ArgumentDelimiter = AsmToken::Comma;
1633 else if (ArgumentDelimiter != AsmToken::Comma) {
1634 Lexer.setSkipSpace(true);
1635 return TokError("expected ' ' for macro argument separator");
1636 }
1637 break;
1638 }
1639
1640 if (Lexer.is(AsmToken::Space)) {
1641 Lex(); // Eat spaces
1642
1643 // Spaces can delimit parameters, but could also be part an expression.
1644 // If the token after a space is an operator, add the token and the next
1645 // one into this argument
1646 if (ArgumentDelimiter == AsmToken::Space ||
1647 ArgumentDelimiter == AsmToken::Eof) {
1648 if (IsOperator(Lexer.getKind())) {
1649 // Check to see whether the token is used as an operator,
1650 // or part of an identifier
1651 const char *NextChar = getTok().getEndLoc().getPointer() + 1;
1652 if (*NextChar == ' ')
1653 AddTokens = 2;
1654 }
1655
1656 if (!AddTokens && ParenLevel == 0) {
1657 if (ArgumentDelimiter == AsmToken::Eof &&
1658 !IsOperator(Lexer.getKind()))
1659 ArgumentDelimiter = AsmToken::Space;
1660 break;
1661 }
1662 }
1663 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001664
1665 // HandleMacroEntry relies on not advancing the lexer here
1666 // to be able to fill in the remaining default parameter values
1667 if (Lexer.is(AsmToken::EndOfStatement))
1668 break;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001669
1670 // Adjust the current parentheses level.
1671 if (Lexer.is(AsmToken::LParen))
1672 ++ParenLevel;
1673 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1674 --ParenLevel;
1675
1676 // Append the token to the current argument list.
1677 MA.push_back(getTok());
Preston Gurd7b6f2032012-09-19 20:36:12 +00001678 if (AddTokens)
1679 AddTokens--;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001680 Lex();
1681 }
Preston Gurd7b6f2032012-09-19 20:36:12 +00001682
1683 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001684 if (ParenLevel != 0)
Rafael Espindola76ac2002012-08-21 15:55:04 +00001685 return TokError("unbalanced parentheses in macro argument");
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001686 return false;
1687}
1688
1689// Parse the macro instantiation arguments.
Rafael Espindola8a403d32012-08-08 14:51:03 +00001690bool AsmParser::ParseMacroArguments(const Macro *M, MacroArguments &A) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001691 const unsigned NParameters = M ? M->Parameters.size() : 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001692 // Argument delimiter is initially unknown. It will be set by
1693 // ParseMacroArgument()
1694 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001695
1696 // Parse two kinds of macro invocations:
1697 // - macros defined without any parameters accept an arbitrary number of them
1698 // - macros defined with parameters accept at most that many of them
1699 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1700 ++Parameter) {
1701 MacroArgument MA;
1702
Preston Gurd7b6f2032012-09-19 20:36:12 +00001703 if (ParseMacroArgument(MA, ArgumentDelimiter))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001704 return true;
1705
Preston Gurd6c9176a2012-09-19 20:29:04 +00001706 if (!MA.empty() || !NParameters)
1707 A.push_back(MA);
1708 else if (NParameters) {
1709 if (!M->Parameters[Parameter].second.empty())
1710 A.push_back(M->Parameters[Parameter].second);
1711 }
Jim Grosbach97146442012-07-30 22:44:17 +00001712
Preston Gurd6c9176a2012-09-19 20:29:04 +00001713 // At the end of the statement, fill in remaining arguments that have
1714 // default values. If there aren't any, then the next argument is
1715 // required but missing
1716 if (Lexer.is(AsmToken::EndOfStatement)) {
1717 if (NParameters && Parameter < NParameters - 1) {
1718 if (M->Parameters[Parameter + 1].second.empty())
1719 return TokError("macro argument '" +
1720 Twine(M->Parameters[Parameter + 1].first) +
1721 "' is missing");
1722 else
1723 continue;
1724 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001725 return false;
Preston Gurd6c9176a2012-09-19 20:29:04 +00001726 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001727
1728 if (Lexer.is(AsmToken::Comma))
1729 Lex();
1730 }
1731 return TokError("Too many arguments");
1732}
1733
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001734bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1735 const Macro *M) {
1736 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1737 // this, although we should protect against infinite loops.
1738 if (ActiveMacros.size() == 20)
1739 return TokError("macros cannot be nested more than 20 levels deep");
1740
Rafael Espindola8a403d32012-08-08 14:51:03 +00001741 MacroArguments A;
1742 if (ParseMacroArguments(M, A))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001743 return true;
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001744
Jim Grosbach97146442012-07-30 22:44:17 +00001745 // Remove any trailing empty arguments. Do this after-the-fact as we have
1746 // to keep empty arguments in the middle of the list or positionality
1747 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindola8a403d32012-08-08 14:51:03 +00001748 while (!A.empty() && A.back().empty())
1749 A.pop_back();
Jim Grosbach97146442012-07-30 22:44:17 +00001750
Rafael Espindola65366442011-06-05 02:43:45 +00001751 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1752 // to hold the macro body with substitutions.
1753 SmallString<256> Buf;
1754 StringRef Body = M->Body;
Rafael Espindola761cb062012-06-03 23:57:14 +00001755 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001756
Rafael Espindola8a403d32012-08-08 14:51:03 +00001757 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola65366442011-06-05 02:43:45 +00001758 return true;
1759
Rafael Espindola761cb062012-06-03 23:57:14 +00001760 // We include the .endmacro in the buffer as our queue to exit the macro
1761 // instantiation.
1762 OS << ".endmacro\n";
1763
Rafael Espindola65366442011-06-05 02:43:45 +00001764 MemoryBuffer *Instantiation =
Rafael Espindola761cb062012-06-03 23:57:14 +00001765 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola65366442011-06-05 02:43:45 +00001766
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001767 // Create the macro instantiation object and add to the current macro
1768 // instantiation stack.
1769 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001770 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001771 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001772 ActiveMacros.push_back(MI);
1773
1774 // Jump to the macro instantiation and prime the lexer.
1775 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1776 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1777 Lex();
1778
1779 return false;
1780}
1781
1782void AsmParser::HandleMacroExit() {
1783 // Jump to the EndOfStatement we should return to, and consume it.
1784 JumpToLoc(ActiveMacros.back()->ExitLoc);
1785 Lex();
1786
1787 // Pop the instantiation entry.
1788 delete ActiveMacros.back();
1789 ActiveMacros.pop_back();
1790}
1791
Rafael Espindolae71cc862012-01-28 05:57:00 +00001792static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001793 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001794 case MCExpr::Binary: {
1795 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1796 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001797 break;
1798 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001799 case MCExpr::Target:
1800 case MCExpr::Constant:
1801 return false;
1802 case MCExpr::SymbolRef: {
1803 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001804 if (S.isVariable())
1805 return IsUsedIn(Sym, S.getVariableValue());
1806 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001807 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001808 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001809 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001810 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001811
1812 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001813}
1814
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001815bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef,
1816 bool NoDeadStrip) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001817 // FIXME: Use better location, we should use proper tokens.
1818 SMLoc EqualLoc = Lexer.getLoc();
1819
Daniel Dunbar821e3332009-08-31 08:09:28 +00001820 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001821 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001822 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001823
Rafael Espindolae71cc862012-01-28 05:57:00 +00001824 // Note: we don't count b as used in "a = b". This is to allow
1825 // a = b
1826 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001827
Daniel Dunbar3f872332009-07-28 16:08:33 +00001828 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001829 return TokError("unexpected token in assignment");
1830
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001831 // Error on assignment to '.'.
1832 if (Name == ".") {
1833 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1834 "(use '.space' or '.org').)"));
1835 }
1836
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001837 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001838 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001839
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001840 // Validate that the LHS is allowed to be a variable (either it has not been
1841 // used as a symbol, or it is an absolute symbol).
1842 MCSymbol *Sym = getContext().LookupSymbol(Name);
1843 if (Sym) {
1844 // Diagnose assignment to a label.
1845 //
1846 // FIXME: Diagnostics. Note the location of the definition as a label.
1847 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001848 if (IsUsedIn(Sym, Value))
1849 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1850 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001851 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00001852 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1853 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001854 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001855 return Error(EqualLoc, "redefinition of '" + Name + "'");
1856 else if (!Sym->isVariable())
1857 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001858 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001859 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1860 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001861
1862 // Don't count these checks as uses.
1863 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001864 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001865 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001866
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001867 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001868
1869 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001870 Out.EmitAssignment(Sym, Value);
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001871 if (NoDeadStrip)
1872 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
1873
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001874
1875 return false;
1876}
1877
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001878/// ParseIdentifier:
1879/// ::= identifier
1880/// ::= string
1881bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001882 // The assembler has relaxed rules for accepting identifiers, in particular we
1883 // allow things like '.globl $foo', which would normally be separate
1884 // tokens. At this level, we have already lexed so we cannot (currently)
1885 // handle this as a context dependent token, instead we detect adjacent tokens
1886 // and return the combined identifier.
1887 if (Lexer.is(AsmToken::Dollar)) {
1888 SMLoc DollarLoc = getLexer().getLoc();
1889
1890 // Consume the dollar sign, and check for a following identifier.
1891 Lex();
1892 if (Lexer.isNot(AsmToken::Identifier))
1893 return true;
1894
1895 // We have a '$' followed by an identifier, make sure they are adjacent.
1896 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1897 return true;
1898
1899 // Construct the joined identifier and consume the token.
1900 Res = StringRef(DollarLoc.getPointer(),
1901 getTok().getIdentifier().size() + 1);
1902 Lex();
1903 return false;
1904 }
1905
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001906 if (Lexer.isNot(AsmToken::Identifier) &&
1907 Lexer.isNot(AsmToken::String))
1908 return true;
1909
Sean Callanan18b83232010-01-19 21:44:56 +00001910 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001911
Sean Callanan79ed1a82010-01-19 20:22:31 +00001912 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001913
1914 return false;
1915}
1916
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001917/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001918/// ::= .equ identifier ',' expression
1919/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001920/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001921bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001922 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001923
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001924 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001925 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001926
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001927 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001928 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001929 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001930
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001931 return ParseAssignment(Name, allow_redef, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001932}
1933
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001934bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001935 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001936
1937 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001938 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001939 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1940 if (Str[i] != '\\') {
1941 Data += Str[i];
1942 continue;
1943 }
1944
1945 // Recognize escaped characters. Note that this escape semantics currently
1946 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1947 ++i;
1948 if (i == e)
1949 return TokError("unexpected backslash at end of string");
1950
1951 // Recognize octal sequences.
1952 if ((unsigned) (Str[i] - '0') <= 7) {
1953 // Consume up to three octal characters.
1954 unsigned Value = Str[i] - '0';
1955
1956 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1957 ++i;
1958 Value = Value * 8 + (Str[i] - '0');
1959
1960 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1961 ++i;
1962 Value = Value * 8 + (Str[i] - '0');
1963 }
1964 }
1965
1966 if (Value > 255)
1967 return TokError("invalid octal escape sequence (out of range)");
1968
1969 Data += (unsigned char) Value;
1970 continue;
1971 }
1972
1973 // Otherwise recognize individual escapes.
1974 switch (Str[i]) {
1975 default:
1976 // Just reject invalid escape sequences for now.
1977 return TokError("invalid escape sequence (unrecognized character)");
1978
1979 case 'b': Data += '\b'; break;
1980 case 'f': Data += '\f'; break;
1981 case 'n': Data += '\n'; break;
1982 case 'r': Data += '\r'; break;
1983 case 't': Data += '\t'; break;
1984 case '"': Data += '"'; break;
1985 case '\\': Data += '\\'; break;
1986 }
1987 }
1988
1989 return false;
1990}
1991
Daniel Dunbara0d14262009-06-24 23:30:00 +00001992/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001993/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1994bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001995 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001996 CheckForValidSection();
1997
Daniel Dunbara0d14262009-06-24 23:30:00 +00001998 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001999 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00002000 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002001
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002002 std::string Data;
2003 if (ParseEscapedString(Data))
2004 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002005
2006 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002007 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002008 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
2009
Sean Callanan79ed1a82010-01-19 20:22:31 +00002010 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002011
2012 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002013 break;
2014
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002015 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00002016 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002017 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002018 }
2019 }
2020
Sean Callanan79ed1a82010-01-19 20:22:31 +00002021 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002022 return false;
2023}
2024
2025/// ParseDirectiveValue
2026/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
2027bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002028 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002029 CheckForValidSection();
2030
Daniel Dunbara0d14262009-06-24 23:30:00 +00002031 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00002032 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00002033 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002034 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002035 return true;
2036
Daniel Dunbar414c0c42010-05-23 18:36:38 +00002037 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00002038 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2039 assert(Size <= 8 && "Invalid size");
2040 uint64_t IntValue = MCE->getValue();
2041 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2042 return Error(ExprLoc, "literal value out of range for directive");
2043 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
2044 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002045 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002046
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002047 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002048 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002049
Daniel Dunbara0d14262009-06-24 23:30:00 +00002050 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002051 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002052 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002053 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002054 }
2055 }
2056
Sean Callanan79ed1a82010-01-19 20:22:31 +00002057 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002058 return false;
2059}
2060
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002061/// ParseDirectiveRealValue
2062/// ::= (.single | .double) [ expression (, expression)* ]
2063bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
2064 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2065 CheckForValidSection();
2066
2067 for (;;) {
2068 // We don't truly support arithmetic on floating point expressions, so we
2069 // have to manually parse unary prefixes.
2070 bool IsNeg = false;
2071 if (getLexer().is(AsmToken::Minus)) {
2072 Lex();
2073 IsNeg = true;
2074 } else if (getLexer().is(AsmToken::Plus))
2075 Lex();
2076
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002077 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00002078 getLexer().isNot(AsmToken::Real) &&
2079 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002080 return TokError("unexpected token in directive");
2081
2082 // Convert to an APFloat.
2083 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00002084 StringRef IDVal = getTok().getString();
2085 if (getLexer().is(AsmToken::Identifier)) {
2086 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2087 Value = APFloat::getInf(Semantics);
2088 else if (!IDVal.compare_lower("nan"))
2089 Value = APFloat::getNaN(Semantics, false, ~0);
2090 else
2091 return TokError("invalid floating point literal");
2092 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002093 APFloat::opInvalidOp)
2094 return TokError("invalid floating point literal");
2095 if (IsNeg)
2096 Value.changeSign();
2097
2098 // Consume the numeric token.
2099 Lex();
2100
2101 // Emit the value as an integer.
2102 APInt AsInt = Value.bitcastToAPInt();
2103 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
2104 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
2105
2106 if (getLexer().is(AsmToken::EndOfStatement))
2107 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002108
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002109 if (getLexer().isNot(AsmToken::Comma))
2110 return TokError("unexpected token in directive");
2111 Lex();
2112 }
2113 }
2114
2115 Lex();
2116 return false;
2117}
2118
Daniel Dunbara0d14262009-06-24 23:30:00 +00002119/// ParseDirectiveSpace
2120/// ::= .space expression [ , expression ]
2121bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002122 CheckForValidSection();
2123
Daniel Dunbara0d14262009-06-24 23:30:00 +00002124 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002125 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002126 return true;
2127
2128 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002129 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2130 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002131 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002132 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002133
Daniel Dunbar475839e2009-06-29 20:37:27 +00002134 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002135 return true;
2136
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002137 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002138 return TokError("unexpected token in '.space' directive");
2139 }
2140
Sean Callanan79ed1a82010-01-19 20:22:31 +00002141 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002142
2143 if (NumBytes <= 0)
2144 return TokError("invalid number of bytes in '.space' directive");
2145
2146 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002147 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002148
2149 return false;
2150}
2151
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002152/// ParseDirectiveZero
2153/// ::= .zero expression
2154bool AsmParser::ParseDirectiveZero() {
2155 CheckForValidSection();
2156
2157 int64_t NumBytes;
2158 if (ParseAbsoluteExpression(NumBytes))
2159 return true;
2160
Rafael Espindolae452b172010-10-05 19:42:57 +00002161 int64_t Val = 0;
2162 if (getLexer().is(AsmToken::Comma)) {
2163 Lex();
2164 if (ParseAbsoluteExpression(Val))
2165 return true;
2166 }
2167
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002168 if (getLexer().isNot(AsmToken::EndOfStatement))
2169 return TokError("unexpected token in '.zero' directive");
2170
2171 Lex();
2172
Rafael Espindolae452b172010-10-05 19:42:57 +00002173 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002174
2175 return false;
2176}
2177
Daniel Dunbara0d14262009-06-24 23:30:00 +00002178/// ParseDirectiveFill
2179/// ::= .fill expression , expression , expression
2180bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002181 CheckForValidSection();
2182
Daniel Dunbara0d14262009-06-24 23:30:00 +00002183 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002184 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002185 return true;
2186
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002187 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002188 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002189 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002190
Daniel Dunbara0d14262009-06-24 23:30:00 +00002191 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002192 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002193 return true;
2194
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002195 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002196 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002197 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002198
Daniel Dunbara0d14262009-06-24 23:30:00 +00002199 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002200 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002201 return true;
2202
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002203 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002204 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002205
Sean Callanan79ed1a82010-01-19 20:22:31 +00002206 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002207
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002208 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2209 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002210
2211 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002212 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002213
2214 return false;
2215}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002216
2217/// ParseDirectiveOrg
2218/// ::= .org expression [ , expression ]
2219bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002220 CheckForValidSection();
2221
Daniel Dunbar821e3332009-08-31 08:09:28 +00002222 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002223 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002224 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002225 return true;
2226
2227 // Parse optional fill expression.
2228 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002229 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2230 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002231 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002232 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002233
Daniel Dunbar475839e2009-06-29 20:37:27 +00002234 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002235 return true;
2236
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002237 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002238 return TokError("unexpected token in '.org' directive");
2239 }
2240
Sean Callanan79ed1a82010-01-19 20:22:31 +00002241 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002242
Jim Grosbachebd4c052012-01-27 00:37:08 +00002243 // Only limited forms of relocatable expressions are accepted here, it
2244 // has to be relative to the current section. The streamer will return
2245 // 'true' if the expression wasn't evaluatable.
2246 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2247 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002248
2249 return false;
2250}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002251
2252/// ParseDirectiveAlign
2253/// ::= {.align, ...} expression [ , expression [ , expression ]]
2254bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002255 CheckForValidSection();
2256
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002257 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002258 int64_t Alignment;
2259 if (ParseAbsoluteExpression(Alignment))
2260 return true;
2261
2262 SMLoc MaxBytesLoc;
2263 bool HasFillExpr = false;
2264 int64_t FillExpr = 0;
2265 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002266 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2267 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002268 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002269 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002270
2271 // The fill expression can be omitted while specifying a maximum number of
2272 // alignment bytes, e.g:
2273 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002274 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002275 HasFillExpr = true;
2276 if (ParseAbsoluteExpression(FillExpr))
2277 return true;
2278 }
2279
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002280 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2281 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002282 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002283 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002284
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002285 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002286 if (ParseAbsoluteExpression(MaxBytesToFill))
2287 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002288
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002289 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002290 return TokError("unexpected token in directive");
2291 }
2292 }
2293
Sean Callanan79ed1a82010-01-19 20:22:31 +00002294 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002295
Daniel Dunbar648ac512010-05-17 21:54:30 +00002296 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002297 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002298
2299 // Compute alignment in bytes.
2300 if (IsPow2) {
2301 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002302 if (Alignment >= 32) {
2303 Error(AlignmentLoc, "invalid alignment value");
2304 Alignment = 31;
2305 }
2306
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002307 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002308 }
2309
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002310 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002311 if (MaxBytesLoc.isValid()) {
2312 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002313 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2314 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002315 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002316 }
2317
2318 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002319 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2320 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002321 MaxBytesToFill = 0;
2322 }
2323 }
2324
Daniel Dunbar648ac512010-05-17 21:54:30 +00002325 // Check whether we should use optimal code alignment for this .align
2326 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002327 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002328 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2329 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002330 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002331 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002332 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002333 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2334 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002335 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002336
2337 return false;
2338}
2339
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002340/// ParseDirectiveSymbolAttribute
2341/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002342bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002343 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002344 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002345 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002346 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002347
2348 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002349 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002350
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002351 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002352
Jim Grosbach10ec6502011-09-15 17:56:49 +00002353 // Assembler local symbols don't make any sense here. Complain loudly.
2354 if (Sym->isTemporary())
2355 return Error(Loc, "non-local symbol required in directive");
2356
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002357 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002358
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002359 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002360 break;
2361
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002362 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002363 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002364 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002365 }
2366 }
2367
Sean Callanan79ed1a82010-01-19 20:22:31 +00002368 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002369 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002370}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002371
2372/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002373/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2374bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002375 CheckForValidSection();
2376
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002377 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002378 StringRef Name;
2379 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002380 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002381
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002382 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002383 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002384
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002385 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002386 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002387 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002388
2389 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002390 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002391 if (ParseAbsoluteExpression(Size))
2392 return true;
2393
2394 int64_t Pow2Alignment = 0;
2395 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002396 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002397 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002398 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002399 if (ParseAbsoluteExpression(Pow2Alignment))
2400 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002401
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002402 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
2403 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer39646d92012-09-07 17:25:13 +00002404 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
2405
Chris Lattner258281d2010-01-19 06:22:22 +00002406 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002407 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
2408 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattner258281d2010-01-19 06:22:22 +00002409 if (!isPowerOf2_64(Pow2Alignment))
2410 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2411 Pow2Alignment = Log2_64(Pow2Alignment);
2412 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002413 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002414
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002415 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002416 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002417
Sean Callanan79ed1a82010-01-19 20:22:31 +00002418 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002419
Chris Lattner1fc3d752009-07-09 17:25:12 +00002420 // NOTE: a size of zero for a .comm should create a undefined symbol
2421 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002422 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002423 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2424 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002425
Eric Christopherc260a3e2010-05-14 01:38:54 +00002426 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002427 // may internally end up wanting an alignment in bytes.
2428 // FIXME: Diagnose overflow.
2429 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002430 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2431 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002432
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002433 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002434 return Error(IDLoc, "invalid symbol redefinition");
2435
Chris Lattner1fc3d752009-07-09 17:25:12 +00002436 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002437 if (IsLocal) {
Benjamin Kramer39646d92012-09-07 17:25:13 +00002438 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002439 return false;
2440 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002441
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002442 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002443 return false;
2444}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002445
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002446/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002447/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002448bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002449 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002450 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002451
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002452 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002453 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002454 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002455
Sean Callanan79ed1a82010-01-19 20:22:31 +00002456 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002457
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002458 if (Str.empty())
2459 Error(Loc, ".abort detected. Assembly stopping.");
2460 else
2461 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002462 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002463
2464 return false;
2465}
Kevin Enderby71148242009-07-14 21:35:03 +00002466
Kevin Enderby1f049b22009-07-14 23:21:55 +00002467/// ParseDirectiveInclude
2468/// ::= .include "filename"
2469bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002470 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002471 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002472
Sean Callanan18b83232010-01-19 21:44:56 +00002473 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002474 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002475 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002476
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002477 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002478 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002479
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002480 // Strip the quotes.
2481 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002482
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002483 // Attempt to switch the lexer to the included file before consuming the end
2484 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002485 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002486 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002487 return true;
2488 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002489
2490 return false;
2491}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002492
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002493/// ParseDirectiveIncbin
2494/// ::= .incbin "filename"
2495bool AsmParser::ParseDirectiveIncbin() {
2496 if (getLexer().isNot(AsmToken::String))
2497 return TokError("expected string in '.incbin' directive");
2498
2499 std::string Filename = getTok().getString();
2500 SMLoc IncbinLoc = getLexer().getLoc();
2501 Lex();
2502
2503 if (getLexer().isNot(AsmToken::EndOfStatement))
2504 return TokError("unexpected token in '.incbin' directive");
2505
2506 // Strip the quotes.
2507 Filename = Filename.substr(1, Filename.size()-2);
2508
2509 // Attempt to process the included file.
2510 if (ProcessIncbinFile(Filename)) {
2511 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2512 return true;
2513 }
2514
2515 return false;
2516}
2517
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002518/// ParseDirectiveIf
2519/// ::= .if expression
2520bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002521 TheCondStack.push_back(TheCondState);
2522 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramer29739e72012-05-12 16:52:21 +00002523 if (TheCondState.Ignore) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002524 EatToEndOfStatement();
Benjamin Kramer29739e72012-05-12 16:52:21 +00002525 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002526 int64_t ExprValue;
2527 if (ParseAbsoluteExpression(ExprValue))
2528 return true;
2529
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002530 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002531 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002532
Sean Callanan79ed1a82010-01-19 20:22:31 +00002533 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002534
2535 TheCondState.CondMet = ExprValue;
2536 TheCondState.Ignore = !TheCondState.CondMet;
2537 }
2538
2539 return false;
2540}
2541
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002542/// ParseDirectiveIfb
2543/// ::= .ifb string
2544bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
2545 TheCondStack.push_back(TheCondState);
2546 TheCondState.TheCond = AsmCond::IfCond;
2547
Benjamin Kramer29739e72012-05-12 16:52:21 +00002548 if (TheCondState.Ignore) {
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002549 EatToEndOfStatement();
2550 } else {
2551 StringRef Str = ParseStringToEndOfStatement();
2552
2553 if (getLexer().isNot(AsmToken::EndOfStatement))
2554 return TokError("unexpected token in '.ifb' directive");
2555
2556 Lex();
2557
2558 TheCondState.CondMet = ExpectBlank == Str.empty();
2559 TheCondState.Ignore = !TheCondState.CondMet;
2560 }
2561
2562 return false;
2563}
2564
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002565/// ParseDirectiveIfc
2566/// ::= .ifc string1, string2
2567bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
2568 TheCondStack.push_back(TheCondState);
2569 TheCondState.TheCond = AsmCond::IfCond;
2570
Benjamin Kramer29739e72012-05-12 16:52:21 +00002571 if (TheCondState.Ignore) {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002572 EatToEndOfStatement();
2573 } else {
2574 StringRef Str1 = ParseStringToComma();
2575
2576 if (getLexer().isNot(AsmToken::Comma))
2577 return TokError("unexpected token in '.ifc' directive");
2578
2579 Lex();
2580
2581 StringRef Str2 = ParseStringToEndOfStatement();
2582
2583 if (getLexer().isNot(AsmToken::EndOfStatement))
2584 return TokError("unexpected token in '.ifc' directive");
2585
2586 Lex();
2587
2588 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
2589 TheCondState.Ignore = !TheCondState.CondMet;
2590 }
2591
2592 return false;
2593}
2594
2595/// ParseDirectiveIfdef
2596/// ::= .ifdef symbol
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002597bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2598 StringRef Name;
2599 TheCondStack.push_back(TheCondState);
2600 TheCondState.TheCond = AsmCond::IfCond;
2601
2602 if (TheCondState.Ignore) {
2603 EatToEndOfStatement();
2604 } else {
2605 if (ParseIdentifier(Name))
2606 return TokError("expected identifier after '.ifdef'");
2607
2608 Lex();
2609
2610 MCSymbol *Sym = getContext().LookupSymbol(Name);
2611
2612 if (expect_defined)
2613 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2614 else
2615 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2616 TheCondState.Ignore = !TheCondState.CondMet;
2617 }
2618
2619 return false;
2620}
2621
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002622/// ParseDirectiveElseIf
2623/// ::= .elseif expression
2624bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2625 if (TheCondState.TheCond != AsmCond::IfCond &&
2626 TheCondState.TheCond != AsmCond::ElseIfCond)
2627 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2628 " an .elseif");
2629 TheCondState.TheCond = AsmCond::ElseIfCond;
2630
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002631 bool LastIgnoreState = false;
2632 if (!TheCondStack.empty())
2633 LastIgnoreState = TheCondStack.back().Ignore;
2634 if (LastIgnoreState || TheCondState.CondMet) {
2635 TheCondState.Ignore = true;
2636 EatToEndOfStatement();
2637 }
2638 else {
2639 int64_t ExprValue;
2640 if (ParseAbsoluteExpression(ExprValue))
2641 return true;
2642
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002643 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002644 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002645
Sean Callanan79ed1a82010-01-19 20:22:31 +00002646 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002647 TheCondState.CondMet = ExprValue;
2648 TheCondState.Ignore = !TheCondState.CondMet;
2649 }
2650
2651 return false;
2652}
2653
2654/// ParseDirectiveElse
2655/// ::= .else
2656bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002657 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002658 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002659
Sean Callanan79ed1a82010-01-19 20:22:31 +00002660 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002661
2662 if (TheCondState.TheCond != AsmCond::IfCond &&
2663 TheCondState.TheCond != AsmCond::ElseIfCond)
2664 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2665 ".elseif");
2666 TheCondState.TheCond = AsmCond::ElseCond;
2667 bool LastIgnoreState = false;
2668 if (!TheCondStack.empty())
2669 LastIgnoreState = TheCondStack.back().Ignore;
2670 if (LastIgnoreState || TheCondState.CondMet)
2671 TheCondState.Ignore = true;
2672 else
2673 TheCondState.Ignore = false;
2674
2675 return false;
2676}
2677
2678/// ParseDirectiveEndIf
2679/// ::= .endif
2680bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002681 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002682 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002683
Sean Callanan79ed1a82010-01-19 20:22:31 +00002684 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002685
2686 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2687 TheCondStack.empty())
2688 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2689 ".else");
2690 if (!TheCondStack.empty()) {
2691 TheCondState = TheCondStack.back();
2692 TheCondStack.pop_back();
2693 }
2694
2695 return false;
2696}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002697
2698/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002699/// ::= .file [number] filename
2700/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002701bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002702 // FIXME: I'm not sure what this is.
2703 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002704 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002705 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002706 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002707 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002708
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002709 if (FileNumber < 1)
2710 return TokError("file number less than one");
2711 }
2712
Daniel Dunbareceec052010-07-12 17:45:27 +00002713 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002714 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002715
Nick Lewycky44d798d2011-10-17 23:05:28 +00002716 // Usually the directory and filename together, otherwise just the directory.
2717 StringRef Path = getTok().getString();
2718 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002719 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002720
Nick Lewycky44d798d2011-10-17 23:05:28 +00002721 StringRef Directory;
2722 StringRef Filename;
2723 if (getLexer().is(AsmToken::String)) {
2724 if (FileNumber == -1)
2725 return TokError("explicit path specified, but no file number");
2726 Filename = getTok().getString();
2727 Filename = Filename.substr(1, Filename.size()-2);
2728 Directory = Path;
2729 Lex();
2730 } else {
2731 Filename = Path;
2732 }
2733
Daniel Dunbareceec052010-07-12 17:45:27 +00002734 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002735 return TokError("unexpected token in '.file' directive");
2736
Chris Lattnerd32e8032010-01-25 19:02:58 +00002737 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002738 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002739 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002740 if (getContext().getGenDwarfForAssembly() == true)
2741 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2742 "used to generate dwarf debug info for assembly code");
2743
Nick Lewycky44d798d2011-10-17 23:05:28 +00002744 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002745 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002746 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002747
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002748 return false;
2749}
2750
2751/// ParseDirectiveLine
2752/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002753bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002754 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2755 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002756 return TokError("unexpected token in '.line' directive");
2757
Sean Callanan18b83232010-01-19 21:44:56 +00002758 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002759 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002760 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002761
2762 // FIXME: Do something with the .line.
2763 }
2764
Daniel Dunbareceec052010-07-12 17:45:27 +00002765 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002766 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002767
2768 return false;
2769}
2770
2771
2772/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002773/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002774/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2775/// The first number is a file number, must have been previously assigned with
2776/// a .file directive, the second number is the line number and optionally the
2777/// third number is a column position (zero if not specified). The remaining
2778/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002779bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002780
Daniel Dunbareceec052010-07-12 17:45:27 +00002781 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002782 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002783 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002784 if (FileNumber < 1)
2785 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002786 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002787 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002788 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002789
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002790 int64_t LineNumber = 0;
2791 if (getLexer().is(AsmToken::Integer)) {
2792 LineNumber = getTok().getIntVal();
2793 if (LineNumber < 1)
2794 return TokError("line number less than one in '.loc' directive");
2795 Lex();
2796 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002797
2798 int64_t ColumnPos = 0;
2799 if (getLexer().is(AsmToken::Integer)) {
2800 ColumnPos = getTok().getIntVal();
2801 if (ColumnPos < 0)
2802 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002803 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002804 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002805
Kevin Enderbyc0957932010-09-30 16:52:03 +00002806 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002807 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002808 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002809 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2810 for (;;) {
2811 if (getLexer().is(AsmToken::EndOfStatement))
2812 break;
2813
2814 StringRef Name;
2815 SMLoc Loc = getTok().getLoc();
2816 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002817 return TokError("unexpected token in '.loc' directive");
2818
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002819 if (Name == "basic_block")
2820 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2821 else if (Name == "prologue_end")
2822 Flags |= DWARF2_FLAG_PROLOGUE_END;
2823 else if (Name == "epilogue_begin")
2824 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2825 else if (Name == "is_stmt") {
2826 SMLoc Loc = getTok().getLoc();
2827 const MCExpr *Value;
2828 if (getParser().ParseExpression(Value))
2829 return true;
2830 // The expression must be the constant 0 or 1.
2831 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2832 int Value = MCE->getValue();
2833 if (Value == 0)
2834 Flags &= ~DWARF2_FLAG_IS_STMT;
2835 else if (Value == 1)
2836 Flags |= DWARF2_FLAG_IS_STMT;
2837 else
2838 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002839 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002840 else {
2841 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2842 }
2843 }
2844 else if (Name == "isa") {
2845 SMLoc Loc = getTok().getLoc();
2846 const MCExpr *Value;
2847 if (getParser().ParseExpression(Value))
2848 return true;
2849 // The expression must be a constant greater or equal to 0.
2850 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2851 int Value = MCE->getValue();
2852 if (Value < 0)
2853 return Error(Loc, "isa number less than zero");
2854 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002855 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002856 else {
2857 return Error(Loc, "isa number not a constant value");
2858 }
2859 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002860 else if (Name == "discriminator") {
2861 if (getParser().ParseAbsoluteExpression(Discriminator))
2862 return true;
2863 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002864 else {
2865 return Error(Loc, "unknown sub-directive in '.loc' directive");
2866 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002867
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002868 if (getLexer().is(AsmToken::EndOfStatement))
2869 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002870 }
2871 }
2872
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002873 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002874 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002875
2876 return false;
2877}
2878
Daniel Dunbar138abae2010-10-16 04:56:42 +00002879/// ParseDirectiveStabs
2880/// ::= .stabs string, number, number, number
2881bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2882 SMLoc DirectiveLoc) {
2883 return TokError("unsupported directive '" + Directive + "'");
2884}
2885
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002886/// ParseDirectiveCFISections
2887/// ::= .cfi_sections section [, section]
2888bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2889 SMLoc DirectiveLoc) {
2890 StringRef Name;
2891 bool EH = false;
2892 bool Debug = false;
2893
2894 if (getParser().ParseIdentifier(Name))
2895 return TokError("Expected an identifier");
2896
2897 if (Name == ".eh_frame")
2898 EH = true;
2899 else if (Name == ".debug_frame")
2900 Debug = true;
2901
2902 if (getLexer().is(AsmToken::Comma)) {
2903 Lex();
2904
2905 if (getParser().ParseIdentifier(Name))
2906 return TokError("Expected an identifier");
2907
2908 if (Name == ".eh_frame")
2909 EH = true;
2910 else if (Name == ".debug_frame")
2911 Debug = true;
2912 }
2913
2914 getStreamer().EmitCFISections(EH, Debug);
2915
2916 return false;
2917}
2918
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002919/// ParseDirectiveCFIStartProc
2920/// ::= .cfi_startproc
2921bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2922 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002923 getStreamer().EmitCFIStartProc();
2924 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002925}
2926
2927/// ParseDirectiveCFIEndProc
2928/// ::= .cfi_endproc
2929bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002930 getStreamer().EmitCFIEndProc();
2931 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002932}
2933
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002934/// ParseRegisterOrRegisterNumber - parse register name or number.
2935bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2936 SMLoc DirectiveLoc) {
2937 unsigned RegNo;
2938
Jim Grosbach6f888a82011-06-02 17:14:04 +00002939 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002940 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2941 DirectiveLoc))
2942 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002943 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002944 } else
2945 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002946
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002947 return false;
2948}
2949
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002950/// ParseDirectiveCFIDefCfa
2951/// ::= .cfi_def_cfa register, offset
2952bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2953 SMLoc DirectiveLoc) {
2954 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002955 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002956 return true;
2957
2958 if (getLexer().isNot(AsmToken::Comma))
2959 return TokError("unexpected token in directive");
2960 Lex();
2961
2962 int64_t Offset = 0;
2963 if (getParser().ParseAbsoluteExpression(Offset))
2964 return true;
2965
Rafael Espindola066c2f42011-04-12 23:59:07 +00002966 getStreamer().EmitCFIDefCfa(Register, Offset);
2967 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002968}
2969
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002970/// ParseDirectiveCFIDefCfaOffset
2971/// ::= .cfi_def_cfa_offset offset
2972bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2973 SMLoc DirectiveLoc) {
2974 int64_t Offset = 0;
2975 if (getParser().ParseAbsoluteExpression(Offset))
2976 return true;
2977
Rafael Espindola066c2f42011-04-12 23:59:07 +00002978 getStreamer().EmitCFIDefCfaOffset(Offset);
2979 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002980}
2981
2982/// ParseDirectiveCFIAdjustCfaOffset
2983/// ::= .cfi_adjust_cfa_offset adjustment
2984bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2985 SMLoc DirectiveLoc) {
2986 int64_t Adjustment = 0;
2987 if (getParser().ParseAbsoluteExpression(Adjustment))
2988 return true;
2989
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002990 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2991 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002992}
2993
2994/// ParseDirectiveCFIDefCfaRegister
2995/// ::= .cfi_def_cfa_register register
2996bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2997 SMLoc DirectiveLoc) {
2998 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002999 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003000 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003001
Rafael Espindola066c2f42011-04-12 23:59:07 +00003002 getStreamer().EmitCFIDefCfaRegister(Register);
3003 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003004}
3005
3006/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003007/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003008bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
3009 int64_t Register = 0;
3010 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003011
3012 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003013 return true;
3014
3015 if (getLexer().isNot(AsmToken::Comma))
3016 return TokError("unexpected token in directive");
3017 Lex();
3018
3019 if (getParser().ParseAbsoluteExpression(Offset))
3020 return true;
3021
Rafael Espindola066c2f42011-04-12 23:59:07 +00003022 getStreamer().EmitCFIOffset(Register, Offset);
3023 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003024}
3025
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003026/// ParseDirectiveCFIRelOffset
3027/// ::= .cfi_rel_offset register, offset
3028bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
3029 SMLoc DirectiveLoc) {
3030 int64_t Register = 0;
3031
3032 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3033 return true;
3034
3035 if (getLexer().isNot(AsmToken::Comma))
3036 return TokError("unexpected token in directive");
3037 Lex();
3038
3039 int64_t Offset = 0;
3040 if (getParser().ParseAbsoluteExpression(Offset))
3041 return true;
3042
Rafael Espindola25f492e2011-04-12 16:12:03 +00003043 getStreamer().EmitCFIRelOffset(Register, Offset);
3044 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003045}
3046
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003047static bool isValidEncoding(int64_t Encoding) {
3048 if (Encoding & ~0xff)
3049 return false;
3050
3051 if (Encoding == dwarf::DW_EH_PE_omit)
3052 return true;
3053
3054 const unsigned Format = Encoding & 0xf;
3055 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3056 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3057 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3058 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3059 return false;
3060
Rafael Espindolacaf11582010-12-29 04:31:26 +00003061 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003062 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00003063 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003064 return false;
3065
3066 return true;
3067}
3068
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003069/// ParseDirectiveCFIPersonalityOrLsda
3070/// ::= .cfi_personality encoding, [symbol_name]
3071/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003072bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003073 SMLoc DirectiveLoc) {
3074 int64_t Encoding = 0;
3075 if (getParser().ParseAbsoluteExpression(Encoding))
3076 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003077 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003078 return false;
3079
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003080 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003081 return TokError("unsupported encoding.");
3082
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003083 if (getLexer().isNot(AsmToken::Comma))
3084 return TokError("unexpected token in directive");
3085 Lex();
3086
3087 StringRef Name;
3088 if (getParser().ParseIdentifier(Name))
3089 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003090
3091 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3092
3093 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00003094 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003095 else {
3096 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00003097 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003098 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00003099 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003100}
3101
Rafael Espindolafe024d02010-12-28 18:36:23 +00003102/// ParseDirectiveCFIRememberState
3103/// ::= .cfi_remember_state
3104bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
3105 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003106 getStreamer().EmitCFIRememberState();
3107 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003108}
3109
3110/// ParseDirectiveCFIRestoreState
3111/// ::= .cfi_remember_state
3112bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
3113 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003114 getStreamer().EmitCFIRestoreState();
3115 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003116}
3117
Rafael Espindolac5754392011-04-12 15:31:05 +00003118/// ParseDirectiveCFISameValue
3119/// ::= .cfi_same_value register
3120bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
3121 SMLoc DirectiveLoc) {
3122 int64_t Register = 0;
3123
3124 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3125 return true;
3126
3127 getStreamer().EmitCFISameValue(Register);
3128
3129 return false;
3130}
3131
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003132/// ParseDirectiveCFIRestore
3133/// ::= .cfi_restore register
3134bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003135 SMLoc DirectiveLoc) {
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003136 int64_t Register = 0;
3137 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3138 return true;
3139
3140 getStreamer().EmitCFIRestore(Register);
3141
3142 return false;
3143}
3144
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003145/// ParseDirectiveCFIEscape
3146/// ::= .cfi_escape expression[,...]
3147bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003148 SMLoc DirectiveLoc) {
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003149 std::string Values;
3150 int64_t CurrValue;
3151 if (getParser().ParseAbsoluteExpression(CurrValue))
3152 return true;
3153
3154 Values.push_back((uint8_t)CurrValue);
3155
3156 while (getLexer().is(AsmToken::Comma)) {
3157 Lex();
3158
3159 if (getParser().ParseAbsoluteExpression(CurrValue))
3160 return true;
3161
3162 Values.push_back((uint8_t)CurrValue);
3163 }
3164
3165 getStreamer().EmitCFIEscape(Values);
3166 return false;
3167}
3168
Rafael Espindola16d7d432012-01-23 21:51:52 +00003169/// ParseDirectiveCFISignalFrame
3170/// ::= .cfi_signal_frame
3171bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
3172 SMLoc DirectiveLoc) {
3173 if (getLexer().isNot(AsmToken::EndOfStatement))
3174 return Error(getLexer().getLoc(),
3175 "unexpected token in '" + Directive + "' directive");
3176
3177 getStreamer().EmitCFISignalFrame();
3178
3179 return false;
3180}
3181
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003182/// ParseDirectiveMacrosOnOff
3183/// ::= .macros_on
3184/// ::= .macros_off
3185bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
3186 SMLoc DirectiveLoc) {
3187 if (getLexer().isNot(AsmToken::EndOfStatement))
3188 return Error(getLexer().getLoc(),
3189 "unexpected token in '" + Directive + "' directive");
3190
3191 getParser().MacrosEnabled = Directive == ".macros_on";
3192
3193 return false;
3194}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003195
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003196/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00003197/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003198bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
3199 SMLoc DirectiveLoc) {
3200 StringRef Name;
3201 if (getParser().ParseIdentifier(Name))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003202 return TokError("expected identifier in '.macro' directive");
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003203
Rafael Espindola8a403d32012-08-08 14:51:03 +00003204 MacroParameters Parameters;
Preston Gurd7b6f2032012-09-19 20:36:12 +00003205 // Argument delimiter is initially unknown. It will be set by
3206 // ParseMacroArgument()
3207 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindola65366442011-06-05 02:43:45 +00003208 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Rafael Espindola7996d042012-08-21 16:06:48 +00003209 for (;;) {
3210 MacroParameter Parameter;
Preston Gurd6c9176a2012-09-19 20:29:04 +00003211 if (getParser().ParseIdentifier(Parameter.first))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003212 return TokError("expected identifier in '.macro' directive");
Preston Gurd6c9176a2012-09-19 20:29:04 +00003213
3214 if (getLexer().is(AsmToken::Equal)) {
3215 Lex();
Preston Gurd7b6f2032012-09-19 20:36:12 +00003216 if (getParser().ParseMacroArgument(Parameter.second, ArgumentDelimiter))
Preston Gurd6c9176a2012-09-19 20:29:04 +00003217 return true;
3218 }
3219
Rafael Espindola65366442011-06-05 02:43:45 +00003220 Parameters.push_back(Parameter);
3221
Preston Gurd7b6f2032012-09-19 20:36:12 +00003222 if (getLexer().is(AsmToken::Comma))
3223 Lex();
3224 else if (getLexer().is(AsmToken::EndOfStatement))
Rafael Espindola65366442011-06-05 02:43:45 +00003225 break;
Rafael Espindola65366442011-06-05 02:43:45 +00003226 }
3227 }
3228
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003229 // Eat the end of statement.
3230 Lex();
3231
3232 AsmToken EndToken, StartToken = getTok();
3233
3234 // Lex the macro definition.
3235 for (;;) {
3236 // Check whether we have reached the end of the file.
3237 if (getLexer().is(AsmToken::Eof))
3238 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3239
3240 // Otherwise, check whether we have reach the .endmacro.
3241 if (getLexer().is(AsmToken::Identifier) &&
3242 (getTok().getIdentifier() == ".endm" ||
3243 getTok().getIdentifier() == ".endmacro")) {
3244 EndToken = getTok();
3245 Lex();
3246 if (getLexer().isNot(AsmToken::EndOfStatement))
3247 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3248 "' directive");
3249 break;
3250 }
3251
3252 // Otherwise, scan til the end of the statement.
3253 getParser().EatToEndOfStatement();
3254 }
3255
3256 if (getParser().MacroMap.lookup(Name)) {
3257 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3258 }
3259
3260 const char *BodyStart = StartToken.getLoc().getPointer();
3261 const char *BodyEnd = EndToken.getLoc().getPointer();
3262 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00003263 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003264 return false;
3265}
3266
3267/// ParseDirectiveEndMacro
3268/// ::= .endm
3269/// ::= .endmacro
3270bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
Rafael Espindola8a403d32012-08-08 14:51:03 +00003271 SMLoc DirectiveLoc) {
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003272 if (getLexer().isNot(AsmToken::EndOfStatement))
3273 return TokError("unexpected token in '" + Directive + "' directive");
3274
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003275 // If we are inside a macro instantiation, terminate the current
3276 // instantiation.
3277 if (!getParser().ActiveMacros.empty()) {
3278 getParser().HandleMacroExit();
3279 return false;
3280 }
3281
3282 // Otherwise, this .endmacro is a stray entry in the file; well formed
3283 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003284 return TokError("unexpected '" + Directive + "' in file, "
3285 "no current macro definition");
3286}
3287
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003288/// ParseDirectivePurgeMacro
3289/// ::= .purgem
3290bool GenericAsmParser::ParseDirectivePurgeMacro(StringRef Directive,
3291 SMLoc DirectiveLoc) {
3292 StringRef Name;
3293 if (getParser().ParseIdentifier(Name))
3294 return TokError("expected identifier in '.purgem' directive");
3295
3296 if (getLexer().isNot(AsmToken::EndOfStatement))
3297 return TokError("unexpected token in '.purgem' directive");
3298
3299 StringMap<Macro*>::iterator I = getParser().MacroMap.find(Name);
3300 if (I == getParser().MacroMap.end())
3301 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3302
3303 // Undefine the macro.
3304 delete I->getValue();
3305 getParser().MacroMap.erase(I);
3306 return false;
3307}
3308
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003309bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00003310 getParser().CheckForValidSection();
3311
3312 const MCExpr *Value;
3313
3314 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003315 return true;
3316
3317 if (getLexer().isNot(AsmToken::EndOfStatement))
3318 return TokError("unexpected token in directive");
3319
3320 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00003321 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003322 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00003323 getStreamer().EmitULEB128Value(Value);
3324
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003325 return false;
3326}
3327
Rafael Espindola761cb062012-06-03 23:57:14 +00003328Macro *AsmParser::ParseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003329 AsmToken EndToken, StartToken = getTok();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003330
Rafael Espindola761cb062012-06-03 23:57:14 +00003331 unsigned NestLevel = 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003332 for (;;) {
3333 // Check whether we have reached the end of the file.
Rafael Espindola761cb062012-06-03 23:57:14 +00003334 if (getLexer().is(AsmToken::Eof)) {
3335 Error(DirectiveLoc, "no matching '.endr' in definition");
3336 return 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003337 }
3338
Rafael Espindola761cb062012-06-03 23:57:14 +00003339 if (Lexer.is(AsmToken::Identifier) &&
3340 (getTok().getIdentifier() == ".rept")) {
3341 ++NestLevel;
3342 }
3343
3344 // Otherwise, check whether we have reached the .endr.
3345 if (Lexer.is(AsmToken::Identifier) &&
3346 getTok().getIdentifier() == ".endr") {
3347 if (NestLevel == 0) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003348 EndToken = getTok();
3349 Lex();
Rafael Espindola761cb062012-06-03 23:57:14 +00003350 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3351 TokError("unexpected token in '.endr' directive");
3352 return 0;
3353 }
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003354 break;
3355 }
Rafael Espindola761cb062012-06-03 23:57:14 +00003356 --NestLevel;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003357 }
3358
Rafael Espindola761cb062012-06-03 23:57:14 +00003359 // Otherwise, scan till the end of the statement.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003360 EatToEndOfStatement();
3361 }
3362
3363 const char *BodyStart = StartToken.getLoc().getPointer();
3364 const char *BodyEnd = EndToken.getLoc().getPointer();
3365 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3366
Rafael Espindola761cb062012-06-03 23:57:14 +00003367 // We Are Anonymous.
3368 StringRef Name;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003369 MacroParameters Parameters;
Rafael Espindola761cb062012-06-03 23:57:14 +00003370 return new Macro(Name, Body, Parameters);
3371}
3372
3373void AsmParser::InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
3374 raw_svector_ostream &OS) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003375 OS << ".endr\n";
3376
3377 MemoryBuffer *Instantiation =
3378 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
3379
Rafael Espindola761cb062012-06-03 23:57:14 +00003380 // Create the macro instantiation object and add to the current macro
3381 // instantiation stack.
3382 MacroInstantiation *MI = new MacroInstantiation(M, DirectiveLoc,
3383 getTok().getLoc(),
3384 Instantiation);
3385 ActiveMacros.push_back(MI);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003386
Rafael Espindola761cb062012-06-03 23:57:14 +00003387 // Jump to the macro instantiation and prime the lexer.
3388 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3389 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3390 Lex();
3391}
3392
3393bool AsmParser::ParseDirectiveRept(SMLoc DirectiveLoc) {
3394 int64_t Count;
3395 if (ParseAbsoluteExpression(Count))
3396 return TokError("unexpected token in '.rept' directive");
3397
3398 if (Count < 0)
3399 return TokError("Count is negative");
3400
3401 if (Lexer.isNot(AsmToken::EndOfStatement))
3402 return TokError("unexpected token in '.rept' directive");
3403
3404 // Eat the end of statement.
3405 Lex();
3406
3407 // Lex the rept definition.
3408 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3409 if (!M)
3410 return true;
3411
3412 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3413 // to hold the macro body with substitutions.
3414 SmallString<256> Buf;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003415 MacroParameters Parameters;
3416 MacroArguments A;
Rafael Espindola761cb062012-06-03 23:57:14 +00003417 raw_svector_ostream OS(Buf);
3418 while (Count--) {
3419 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3420 return true;
3421 }
3422 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003423
3424 return false;
3425}
3426
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003427/// ParseDirectiveIrp
3428/// ::= .irp symbol,values
3429bool AsmParser::ParseDirectiveIrp(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003430 MacroParameters Parameters;
3431 MacroParameter Parameter;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003432
Preston Gurd6c9176a2012-09-19 20:29:04 +00003433 if (ParseIdentifier(Parameter.first))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003434 return TokError("expected identifier in '.irp' directive");
3435
3436 Parameters.push_back(Parameter);
3437
3438 if (Lexer.isNot(AsmToken::Comma))
3439 return TokError("expected comma in '.irp' directive");
3440
3441 Lex();
3442
Rafael Espindola8a403d32012-08-08 14:51:03 +00003443 MacroArguments A;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003444 if (ParseMacroArguments(0, A))
3445 return true;
3446
3447 // Eat the end of statement.
3448 Lex();
3449
3450 // Lex the irp definition.
3451 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3452 if (!M)
3453 return true;
3454
3455 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3456 // to hold the macro body with substitutions.
3457 SmallString<256> Buf;
3458 raw_svector_ostream OS(Buf);
3459
Rafael Espindola7996d042012-08-21 16:06:48 +00003460 for (MacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
3461 MacroArguments Args;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003462 Args.push_back(*i);
3463
3464 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3465 return true;
3466 }
3467
3468 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3469
3470 return false;
3471}
3472
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003473/// ParseDirectiveIrpc
3474/// ::= .irpc symbol,values
3475bool AsmParser::ParseDirectiveIrpc(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003476 MacroParameters Parameters;
3477 MacroParameter Parameter;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003478
Preston Gurd6c9176a2012-09-19 20:29:04 +00003479 if (ParseIdentifier(Parameter.first))
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003480 return TokError("expected identifier in '.irpc' directive");
3481
3482 Parameters.push_back(Parameter);
3483
3484 if (Lexer.isNot(AsmToken::Comma))
3485 return TokError("expected comma in '.irpc' directive");
3486
3487 Lex();
3488
Rafael Espindola8a403d32012-08-08 14:51:03 +00003489 MacroArguments A;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003490 if (ParseMacroArguments(0, A))
3491 return true;
3492
3493 if (A.size() != 1 || A.front().size() != 1)
3494 return TokError("unexpected token in '.irpc' directive");
3495
3496 // Eat the end of statement.
3497 Lex();
3498
3499 // Lex the irpc definition.
3500 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3501 if (!M)
3502 return true;
3503
3504 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3505 // to hold the macro body with substitutions.
3506 SmallString<256> Buf;
3507 raw_svector_ostream OS(Buf);
3508
3509 StringRef Values = A.front().front().getString();
3510 std::size_t I, End = Values.size();
3511 for (I = 0; I < End; ++I) {
3512 MacroArgument Arg;
3513 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I+1)));
3514
Rafael Espindola8a403d32012-08-08 14:51:03 +00003515 MacroArguments Args;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003516 Args.push_back(Arg);
3517
3518 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3519 return true;
3520 }
3521
3522 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3523
3524 return false;
3525}
3526
Rafael Espindola761cb062012-06-03 23:57:14 +00003527bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) {
3528 if (ActiveMacros.empty())
Preston Gurd6579eea2012-09-19 20:23:43 +00003529 return TokError("unmatched '.endr' directive");
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003530
3531 // The only .repl that should get here are the ones created by
Rafael Espindola761cb062012-06-03 23:57:14 +00003532 // InstantiateMacroLikeBody.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003533 assert(getLexer().is(AsmToken::EndOfStatement));
3534
Rafael Espindola761cb062012-06-03 23:57:14 +00003535 HandleMacroExit();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003536 return false;
3537}
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003538
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003539/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003540MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003541 MCContext &C, MCStreamer &Out,
3542 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003543 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003544}