blob: 2daad0a8513b3289ed9240f170d7bf81260f2898 [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;
49
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000050struct Macro {
51 StringRef Name;
52 StringRef Body;
Rafael Espindola65366442011-06-05 02:43:45 +000053 std::vector<StringRef> Parameters;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000054
55public:
Rafael Espindola65366442011-06-05 02:43:45 +000056 Macro(StringRef N, StringRef B, const std::vector<StringRef> &P) :
57 Name(N), Body(B), Parameters(P) {}
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000058};
59
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000060/// \brief Helper class for storing information about an active macro
61/// instantiation.
62struct MacroInstantiation {
63 /// The macro being instantiated.
64 const Macro *TheMacro;
65
66 /// The macro instantiation with substitutions.
67 MemoryBuffer *Instantiation;
68
69 /// The location of the instantiation.
70 SMLoc InstantiationLoc;
71
72 /// The location where parsing should resume upon instantiation completion.
73 SMLoc ExitLoc;
74
75public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000076 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000077 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000078};
79
Daniel Dunbaraef87e32010-07-18 18:31:38 +000080/// \brief The concrete assembly parser instance.
81class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000082 friend class GenericAsmParser;
83
Daniel Dunbaraef87e32010-07-18 18:31:38 +000084 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
85 void operator=(const AsmParser &); // DO NOT IMPLEMENT
86private:
87 AsmLexer Lexer;
88 MCContext &Ctx;
89 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +000090 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000091 SourceMgr &SrcMgr;
Benjamin Kramer04a04262011-10-16 10:48:29 +000092 SourceMgr::DiagHandlerTy SavedDiagHandler;
93 void *SavedDiagContext;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000094 MCAsmParserExtension *GenericParser;
95 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +000096
Daniel Dunbaraef87e32010-07-18 18:31:38 +000097 /// This is the current buffer index we're lexing from as managed by the
98 /// SourceMgr object.
99 int CurBuffer;
100
101 AsmCond TheCondState;
102 std::vector<AsmCond> TheCondStack;
103
104 /// DirectiveMap - This is a table handlers for directives. Each handler is
105 /// invoked after the directive identifier is read and is responsible for
106 /// parsing and validating the rest of the directive. The handler is passed
107 /// in the directive name and the location of the directive keyword.
108 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000109
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000110 /// MacroMap - Map of currently defined macros.
111 StringMap<Macro*> MacroMap;
112
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000113 /// ActiveMacros - Stack of active macro instantiations.
114 std::vector<MacroInstantiation*> ActiveMacros;
115
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000116 /// Boolean tracking whether macro substitution is enabled.
117 unsigned MacrosEnabled : 1;
118
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000119 /// Flag tracking whether any errors have been encountered.
120 unsigned HadError : 1;
121
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000122 /// The values from the last parsed cpp hash file line comment if any.
123 StringRef CppHashFilename;
124 int64_t CppHashLineNumber;
125 SMLoc CppHashLoc;
126
Devang Patel0db58bf2012-01-31 18:14:05 +0000127 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
128 unsigned AssemblerDialect;
129
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000130public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000131 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000132 const MCAsmInfo &MAI);
133 ~AsmParser();
134
135 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
136
137 void AddDirectiveHandler(MCAsmParserExtension *Object,
138 StringRef Directive,
139 DirectiveHandler Handler) {
140 DirectiveMap[Directive] = std::make_pair(Object, Handler);
141 }
142
143public:
144 /// @name MCAsmParser Interface
145 /// {
146
147 virtual SourceMgr &getSourceManager() { return SrcMgr; }
148 virtual MCAsmLexer &getLexer() { return Lexer; }
149 virtual MCContext &getContext() { return Ctx; }
150 virtual MCStreamer &getStreamer() { return Out; }
Devang Patel0db58bf2012-01-31 18:14:05 +0000151 virtual unsigned getAssemblerDialect() {
152 if (AssemblerDialect == ~0U)
153 return MAI.getAssemblerDialect();
154 else
155 return AssemblerDialect;
156 }
157 virtual void setAssemblerDialect(unsigned i) {
158 AssemblerDialect = i;
159 }
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000160
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000161 virtual bool Warning(SMLoc L, const Twine &Msg,
162 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
163 virtual bool Error(SMLoc L, const Twine &Msg,
164 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000165
166 const AsmToken &Lex();
167
168 bool ParseExpression(const MCExpr *&Res);
169 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
170 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
171 virtual bool ParseAbsoluteExpression(int64_t &Res);
172
173 /// }
174
175private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000176 void CheckForValidSection();
177
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000178 bool ParseStatement();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000179 void EatToEndOfLine();
180 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000181
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000182 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola761cb062012-06-03 23:57:14 +0000183 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola65366442011-06-05 02:43:45 +0000184 const std::vector<StringRef> &Parameters,
Rafael Espindola28c1f6662012-06-03 22:41:23 +0000185 const std::vector<MacroArgument> &A,
Rafael Espindola65366442011-06-05 02:43:45 +0000186 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000187 void HandleMacroExit();
188
189 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000190 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattner462b43c2011-10-16 05:47:55 +0000191 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
192 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000193 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000194 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000195
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000196 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
197 bool EnterIncludeFile(const std::string &Filename);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000198 /// ProcessIncbinFile - Process the specified file for the .incbin directive.
199 /// This returns true on failure.
200 bool ProcessIncbinFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000201
202 /// \brief Reset the current lexer position to that given by \arg Loc. The
203 /// current token is not set; clients should ensure Lex() is called
204 /// subsequently.
205 void JumpToLoc(SMLoc Loc);
206
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000207 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000208
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000209 bool ParseMacroArgument(MacroArgument &MA);
210 bool ParseMacroArguments(const Macro *M, std::vector<MacroArgument> &A);
211
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000212 /// \brief Parse up to the end of statement and a return the contents from the
213 /// current token until the end of the statement; the current token on exit
214 /// will be either the EndOfStatement or EOF.
215 StringRef ParseStringToEndOfStatement();
216
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000217 /// \brief Parse until the end of a statement or a comma is encountered,
218 /// return the contents from the current token up to the end or comma.
219 StringRef ParseStringToComma();
220
Nico Weber4c4c7322011-01-28 03:04:41 +0000221 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000222
223 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
224 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
225 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000226 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000227
228 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
229 /// and set \arg Res to the identifier contents.
230 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000231
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000232 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000233
234 // ".ascii", ".asciiz", ".string"
235 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000236 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000237 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000238 bool ParseDirectiveFill(); // ".fill"
239 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000240 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000241 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000242 bool ParseDirectiveOrg(); // ".org"
243 // ".align{,32}", ".p2align{,w,l}"
244 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
245
246 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
247 /// accepts a single symbol (which should be a label or an external).
248 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000249
250 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
251
252 bool ParseDirectiveAbort(); // ".abort"
253 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000254 bool ParseDirectiveIncbin(); // ".incbin"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000255
256 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +0000257 // ".ifb" or ".ifnb", depending on ExpectBlank.
258 bool ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000259 // ".ifc" or ".ifnc", depending on ExpectEqual.
260 bool ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000261 // ".ifdef" or ".ifndef", depending on expect_defined
262 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000263 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
264 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
265 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
266
267 /// ParseEscapedString - Parse the current token as a string which may include
268 /// escaped characters and return the string contents.
269 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000270
271 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
272 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola2ec304c2012-05-12 16:31:10 +0000273
Rafael Espindola761cb062012-06-03 23:57:14 +0000274 // Macro-like directives
275 Macro *ParseMacroLikeBody(SMLoc DirectiveLoc);
276 void InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
277 raw_svector_ostream &OS);
278 bool ParseDirectiveRept(SMLoc DirectiveLoc); // ".rept"
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000279 bool ParseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
Rafael Espindolafc9216e2012-06-16 18:03:25 +0000280 bool ParseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
Rafael Espindola761cb062012-06-03 23:57:14 +0000281 bool ParseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000282};
283
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000284/// \brief Generic implementations of directive handling, etc. which is shared
285/// (or the default, at least) for all assembler parser.
286class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000287 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
288 void AddDirectiveHandler(StringRef Directive) {
289 getParser().AddDirectiveHandler(this, Directive,
290 HandleDirective<GenericAsmParser, Handler>);
291 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000292public:
293 GenericAsmParser() {}
294
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000295 AsmParser &getParser() {
296 return (AsmParser&) this->MCAsmParserExtension::getParser();
297 }
298
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000299 virtual void Initialize(MCAsmParser &Parser) {
300 // Call the base implementation.
301 this->MCAsmParserExtension::Initialize(Parser);
302
303 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000304 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
305 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
306 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000307 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000308
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000309 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000310 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
311 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000312 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
313 ".cfi_startproc");
314 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
315 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000316 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
317 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000318 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
319 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000320 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
321 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000322 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
323 ".cfi_def_cfa_register");
324 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
325 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000326 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
327 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000328 AddDirectiveHandler<
329 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
330 AddDirectiveHandler<
331 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000332 AddDirectiveHandler<
333 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
334 AddDirectiveHandler<
335 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000336 AddDirectiveHandler<
337 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000338 AddDirectiveHandler<
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000339 &GenericAsmParser::ParseDirectiveCFIRestore>(".cfi_restore");
340 AddDirectiveHandler<
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000341 &GenericAsmParser::ParseDirectiveCFIEscape>(".cfi_escape");
Rafael Espindola16d7d432012-01-23 21:51:52 +0000342 AddDirectiveHandler<
343 &GenericAsmParser::ParseDirectiveCFISignalFrame>(".cfi_signal_frame");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000344
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000345 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000346 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
347 ".macros_on");
348 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
349 ".macros_off");
350 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
351 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
352 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000353 AddDirectiveHandler<&GenericAsmParser::ParseDirectivePurgeMacro>(".purgem");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000354
355 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
356 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000357 }
358
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000359 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
360
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000361 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
362 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
363 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000364 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000365 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000366 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
367 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000368 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000369 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000370 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000371 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
372 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000373 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000374 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000375 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
376 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000377 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000378 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000379 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
Rafael Espindola16d7d432012-01-23 21:51:52 +0000380 bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000381
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000382 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000383 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
384 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000385 bool ParseDirectivePurgeMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000386
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000387 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000388};
389
390}
391
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000392namespace llvm {
393
394extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000395extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000396extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000397
398}
399
Chris Lattneraaec2052010-01-19 19:46:13 +0000400enum { DEFAULT_ADDRSPACE = 0 };
401
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000402AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000403 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000404 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000405 GenericParser(new GenericAsmParser), PlatformParser(0),
Devang Patel0db58bf2012-01-31 18:14:05 +0000406 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0),
407 AssemblerDialect(~0U) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000408 // Save the old handler.
409 SavedDiagHandler = SrcMgr.getDiagHandler();
410 SavedDiagContext = SrcMgr.getDiagContext();
411 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000412 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000413 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000414
415 // Initialize the generic parser.
416 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000417
418 // Initialize the platform / file format parser.
419 //
420 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
421 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000422 if (_MAI.hasMicrosoftFastStdCallMangling()) {
423 PlatformParser = createCOFFAsmParser();
424 PlatformParser->Initialize(*this);
425 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000426 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000427 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000428 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000429 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000430 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000431 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000432}
433
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000434AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000435 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
436
437 // Destroy any macros.
438 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
439 ie = MacroMap.end(); it != ie; ++it)
440 delete it->getValue();
441
Daniel Dunbare4749702010-07-12 18:12:02 +0000442 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000443 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000444}
445
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000446void AsmParser::PrintMacroInstantiations() {
447 // Print the active macro instantiation stack.
448 for (std::vector<MacroInstantiation*>::const_reverse_iterator
449 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000450 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
451 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000452}
453
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000454bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000455 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000456 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000457 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000458 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000459 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000460}
461
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000462bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000463 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000464 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000465 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000466 return true;
467}
468
Sean Callananfd0b0282010-01-21 00:19:58 +0000469bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000470 std::string IncludedFile;
471 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000472 if (NewBuf == -1)
473 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000474
Sean Callananfd0b0282010-01-21 00:19:58 +0000475 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000476
Sean Callananfd0b0282010-01-21 00:19:58 +0000477 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000478
Sean Callananfd0b0282010-01-21 00:19:58 +0000479 return false;
480}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000481
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000482/// Process the specified .incbin file by seaching for it in the include paths
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000483/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000484/// returns true on failure.
485bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
486 std::string IncludedFile;
487 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
488 if (NewBuf == -1)
489 return true;
490
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000491 // Pick up the bytes from the file and emit them.
Kevin Enderbydac29532011-12-15 00:00:27 +0000492 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
493 DEFAULT_ADDRSPACE);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000494 return false;
495}
496
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000497void AsmParser::JumpToLoc(SMLoc Loc) {
498 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
499 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
500}
501
Sean Callananfd0b0282010-01-21 00:19:58 +0000502const AsmToken &AsmParser::Lex() {
503 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000504
Sean Callananfd0b0282010-01-21 00:19:58 +0000505 if (tok->is(AsmToken::Eof)) {
506 // If this is the end of an included file, pop the parent file off the
507 // include stack.
508 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
509 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000510 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000511 tok = &Lexer.Lex();
512 }
513 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000514
Sean Callananfd0b0282010-01-21 00:19:58 +0000515 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000516 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000517
Sean Callananfd0b0282010-01-21 00:19:58 +0000518 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000519}
520
Chris Lattner79180e22010-04-05 23:15:42 +0000521bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000522 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000523 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000524 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000525
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000526 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000527 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000528
529 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000530 AsmCond StartingCondState = TheCondState;
531
Kevin Enderby613b7572011-11-01 22:27:22 +0000532 // If we are generating dwarf for assembly source files save the initial text
533 // section and generate a .file directive.
534 if (getContext().getGenDwarfForAssembly()) {
535 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000536 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
537 getStreamer().EmitLabel(SectionStartSym);
538 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000539 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
540 StringRef(), SrcMgr.getMemoryBuffer(CurBuffer)->getBufferIdentifier());
541 }
542
Chris Lattnerb717fb02009-07-02 21:53:43 +0000543 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000544 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000545 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000546
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000547 // We had an error, validate that one was emitted and recover by skipping to
548 // the next line.
549 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000550 EatToEndOfStatement();
551 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000552
553 if (TheCondState.TheCond != StartingCondState.TheCond ||
554 TheCondState.Ignore != StartingCondState.Ignore)
555 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000556
557 // Check to see there are no empty DwarfFile slots.
558 const std::vector<MCDwarfFile *> &MCDwarfFiles =
559 getContext().getMCDwarfFiles();
560 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000561 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000562 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000563 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000564
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000565 // Check to see that all assembler local symbols were actually defined.
566 // Targets that don't do subsections via symbols may not want this, though,
567 // so conservatively exclude them. Only do this if we're finalizing, though,
568 // as otherwise we won't necessarilly have seen everything yet.
569 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
570 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
571 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
572 e = Symbols.end();
573 i != e; ++i) {
574 MCSymbol *Sym = i->getValue();
575 // Variable symbols may not be marked as defined, so check those
576 // explicitly. If we know it's a variable, we have a definition for
577 // the purposes of this check.
578 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
579 // FIXME: We would really like to refer back to where the symbol was
580 // first referenced for a source location. We need to add something
581 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000582 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
583 "assembler local symbol '" + Sym->getName() +
584 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000585 }
586 }
587
588
Chris Lattner79180e22010-04-05 23:15:42 +0000589 // Finalize the output stream if there are no errors and if the client wants
590 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000591 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000592 Out.Finish();
593
Chris Lattnerb717fb02009-07-02 21:53:43 +0000594 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000595}
596
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000597void AsmParser::CheckForValidSection() {
598 if (!getStreamer().getCurrentSection()) {
599 TokError("expected section directive before assembly directive");
600 Out.SwitchSection(Ctx.getMachOSection(
601 "__TEXT", "__text",
602 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
603 0, SectionKind::getText()));
604 }
605}
606
Chris Lattner2cf5f142009-06-22 01:29:09 +0000607/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
608void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000609 while (Lexer.isNot(AsmToken::EndOfStatement) &&
610 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000611 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000612
Chris Lattner2cf5f142009-06-22 01:29:09 +0000613 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000614 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000615 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000616}
617
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000618StringRef AsmParser::ParseStringToEndOfStatement() {
619 const char *Start = getTok().getLoc().getPointer();
620
621 while (Lexer.isNot(AsmToken::EndOfStatement) &&
622 Lexer.isNot(AsmToken::Eof))
623 Lex();
624
625 const char *End = getTok().getLoc().getPointer();
626 return StringRef(Start, End - Start);
627}
Chris Lattnerc4193832009-06-22 05:51:26 +0000628
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000629StringRef AsmParser::ParseStringToComma() {
630 const char *Start = getTok().getLoc().getPointer();
631
632 while (Lexer.isNot(AsmToken::EndOfStatement) &&
633 Lexer.isNot(AsmToken::Comma) &&
634 Lexer.isNot(AsmToken::Eof))
635 Lex();
636
637 const char *End = getTok().getLoc().getPointer();
638 return StringRef(Start, End - Start);
639}
640
Chris Lattner74ec1a32009-06-22 06:32:03 +0000641/// ParseParenExpr - Parse a paren expression and return it.
642/// NOTE: This assumes the leading '(' has already been consumed.
643///
644/// parenexpr ::= expr)
645///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000646bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000647 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000648 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000649 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000650 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000651 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000652 return false;
653}
Chris Lattnerc4193832009-06-22 05:51:26 +0000654
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000655/// ParseBracketExpr - Parse a bracket expression and return it.
656/// NOTE: This assumes the leading '[' has already been consumed.
657///
658/// bracketexpr ::= expr]
659///
660bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
661 if (ParseExpression(Res)) return true;
662 if (Lexer.isNot(AsmToken::RBrac))
663 return TokError("expected ']' in brackets expression");
664 EndLoc = Lexer.getLoc();
665 Lex();
666 return false;
667}
668
Chris Lattner74ec1a32009-06-22 06:32:03 +0000669/// ParsePrimaryExpr - Parse a primary expression and return it.
670/// primaryexpr ::= (parenexpr
671/// primaryexpr ::= symbol
672/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000673/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000674/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000675bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000676 switch (Lexer.getKind()) {
677 default:
678 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000679 // If we have an error assume that we've already handled it.
680 case AsmToken::Error:
681 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000682 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000683 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000684 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000685 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000686 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000687 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000688 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000689 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000690 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000691 EndLoc = Lexer.getLoc();
692
693 StringRef Identifier;
694 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000695 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000696
Daniel Dunbarfffff912009-10-16 01:34:54 +0000697 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000698 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000699 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000700
701 // Lookup the symbol variant if used.
702 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000703 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000704 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000705 if (Variant == MCSymbolRefExpr::VK_Invalid) {
706 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000707 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000708 }
709 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000710
Daniel Dunbarfffff912009-10-16 01:34:54 +0000711 // If this is an absolute variable reference, substitute it now to preserve
712 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000713 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000714 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000715 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000716
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000717 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000718 return false;
719 }
720
721 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000722 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000723 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000724 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000725 case AsmToken::Integer: {
726 SMLoc Loc = getTok().getLoc();
727 int64_t IntVal = getTok().getIntVal();
728 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000729 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000730 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000731 // Look for 'b' or 'f' following an Integer as a directional label
732 if (Lexer.getKind() == AsmToken::Identifier) {
733 StringRef IDVal = getTok().getString();
734 if (IDVal == "f" || IDVal == "b"){
735 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
736 IDVal == "f" ? 1 : 0);
737 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
738 getContext());
Benjamin Kramer29739e72012-05-12 16:52:21 +0000739 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000740 return Error(Loc, "invalid reference to undefined symbol");
741 EndLoc = Lexer.getLoc();
742 Lex(); // Eat identifier.
743 }
744 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000745 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000746 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000747 case AsmToken::Real: {
748 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000749 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000750 Res = MCConstantExpr::Create(IntVal, getContext());
751 Lex(); // Eat token.
752 return false;
753 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000754 case AsmToken::Dot: {
755 // This is a '.' reference, which references the current PC. Emit a
756 // temporary label to the streamer and refer to it.
757 MCSymbol *Sym = Ctx.CreateTempSymbol();
758 Out.EmitLabel(Sym);
759 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
760 EndLoc = Lexer.getLoc();
761 Lex(); // Eat identifier.
762 return false;
763 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000764 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000765 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000766 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000767 case AsmToken::LBrac:
768 if (!PlatformParser->HasBracketExpressions())
769 return TokError("brackets expression not supported on this target");
770 Lex(); // Eat the '['.
771 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000772 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000773 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000774 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000775 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000776 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000777 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000778 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000779 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000780 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000781 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000782 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000783 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000784 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000785 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000786 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000787 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000788 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000789 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000790 }
791}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000792
Chris Lattnerb4307b32010-01-15 19:28:38 +0000793bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000794 SMLoc EndLoc;
795 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000796}
797
Daniel Dunbarcceba832010-09-17 02:47:07 +0000798const MCExpr *
799AsmParser::ApplyModifierToExpr(const MCExpr *E,
800 MCSymbolRefExpr::VariantKind Variant) {
801 // Recurse over the given expression, rebuilding it to apply the given variant
802 // if there is exactly one symbol.
803 switch (E->getKind()) {
804 case MCExpr::Target:
805 case MCExpr::Constant:
806 return 0;
807
808 case MCExpr::SymbolRef: {
809 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
810
811 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
812 TokError("invalid variant on expression '" +
813 getTok().getIdentifier() + "' (already modified)");
814 return E;
815 }
816
817 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
818 }
819
820 case MCExpr::Unary: {
821 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
822 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
823 if (!Sub)
824 return 0;
825 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
826 }
827
828 case MCExpr::Binary: {
829 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
830 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
831 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
832
833 if (!LHS && !RHS)
834 return 0;
835
836 if (!LHS) LHS = BE->getLHS();
837 if (!RHS) RHS = BE->getRHS();
838
839 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
840 }
841 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000842
Craig Topper85814382012-02-07 05:05:23 +0000843 llvm_unreachable("Invalid expression kind!");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000844}
845
Chris Lattner74ec1a32009-06-22 06:32:03 +0000846/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000847///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000848/// expr ::= expr &&,|| expr -> lowest.
849/// expr ::= expr |,^,&,! expr
850/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
851/// expr ::= expr <<,>> expr
852/// expr ::= expr +,- expr
853/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000854/// expr ::= primaryexpr
855///
Chris Lattner54482b42010-01-15 19:39:23 +0000856bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000857 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000858 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000859 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
860 return true;
861
Daniel Dunbarcceba832010-09-17 02:47:07 +0000862 // As a special case, we support 'a op b @ modifier' by rewriting the
863 // expression to include the modifier. This is inefficient, but in general we
864 // expect users to use 'a@modifier op b'.
865 if (Lexer.getKind() == AsmToken::At) {
866 Lex();
867
868 if (Lexer.isNot(AsmToken::Identifier))
869 return TokError("unexpected symbol modifier following '@'");
870
871 MCSymbolRefExpr::VariantKind Variant =
872 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
873 if (Variant == MCSymbolRefExpr::VK_Invalid)
874 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
875
876 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
877 if (!ModifiedRes) {
878 return TokError("invalid modifier '" + getTok().getIdentifier() +
879 "' (no symbols present)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000880 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000881
Daniel Dunbarcceba832010-09-17 02:47:07 +0000882 Res = ModifiedRes;
883 Lex();
884 }
885
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000886 // Try to constant fold it up front, if possible.
887 int64_t Value;
888 if (Res->EvaluateAsAbsolute(Value))
889 Res = MCConstantExpr::Create(Value, getContext());
890
891 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000892}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000893
Chris Lattnerb4307b32010-01-15 19:28:38 +0000894bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000895 Res = 0;
896 return ParseParenExpr(Res, EndLoc) ||
897 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000898}
899
Daniel Dunbar475839e2009-06-29 20:37:27 +0000900bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000901 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000902
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000903 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000904 if (ParseExpression(Expr))
905 return true;
906
Daniel Dunbare00b0112009-10-16 01:57:52 +0000907 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000908 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000909
910 return false;
911}
912
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000913static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000914 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000915 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000916 default:
917 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000918
Jim Grosbachfbe16812011-08-20 16:24:13 +0000919 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000920 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000921 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000922 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000923 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000924 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000925 return 1;
926
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000927
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000928 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000929 //
930 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000931 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000932 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000933 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000934 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000935 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000936 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000937 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000938 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000939 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000940
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000941 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000942 case AsmToken::EqualEqual:
943 Kind = MCBinaryExpr::EQ;
944 return 3;
945 case AsmToken::ExclaimEqual:
946 case AsmToken::LessGreater:
947 Kind = MCBinaryExpr::NE;
948 return 3;
949 case AsmToken::Less:
950 Kind = MCBinaryExpr::LT;
951 return 3;
952 case AsmToken::LessEqual:
953 Kind = MCBinaryExpr::LTE;
954 return 3;
955 case AsmToken::Greater:
956 Kind = MCBinaryExpr::GT;
957 return 3;
958 case AsmToken::GreaterEqual:
959 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000960 return 3;
961
Jim Grosbachfbe16812011-08-20 16:24:13 +0000962 // Intermediate Precedence: <<, >>
963 case AsmToken::LessLess:
964 Kind = MCBinaryExpr::Shl;
965 return 4;
966 case AsmToken::GreaterGreater:
967 Kind = MCBinaryExpr::Shr;
968 return 4;
969
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000970 // High Intermediate Precedence: +, -
971 case AsmToken::Plus:
972 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000973 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000974 case AsmToken::Minus:
975 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000976 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000977
Jim Grosbachfbe16812011-08-20 16:24:13 +0000978 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +0000979 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000980 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000981 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000982 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000983 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000984 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000985 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000986 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000987 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000988 }
989}
990
991
992/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
993/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000994bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
995 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000996 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000997 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000998 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000999
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001000 // If the next token is lower precedence than we are allowed to eat, return
1001 // successfully with what we ate already.
1002 if (TokPrec < Precedence)
1003 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001004
Sean Callanan79ed1a82010-01-19 20:22:31 +00001005 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001006
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001007 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001008 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001009 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001010
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001011 // If BinOp binds less tightly with RHS than the operator after RHS, let
1012 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001013 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001014 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001015 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001016 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001017 }
1018
Daniel Dunbar475839e2009-06-29 20:37:27 +00001019 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001020 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001021 }
1022}
1023
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001024
1025
1026
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001027/// ParseStatement:
1028/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +00001029/// ::= Label* Directive ...Operands... EndOfStatement
1030/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001031bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001032 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001033 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001034 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001035 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001036 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001037
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001038 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +00001039 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001040 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001041 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001042 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001043 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001044 if (Lexer.is(AsmToken::Hash))
1045 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001046
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001047 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001048 if (Lexer.is(AsmToken::Integer)) {
1049 LocalLabelVal = getTok().getIntVal();
1050 if (LocalLabelVal < 0) {
1051 if (!TheCondState.Ignore)
1052 return TokError("unexpected token at start of statement");
1053 IDVal = "";
1054 }
1055 else {
1056 IDVal = getTok().getString();
1057 Lex(); // Consume the integer token to be used as an identifier token.
1058 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001059 if (!TheCondState.Ignore)
1060 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001061 }
1062 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001063
1064 } else if (Lexer.is(AsmToken::Dot)) {
1065 // Treat '.' as a valid identifier in this context.
1066 Lex();
1067 IDVal = ".";
1068
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001069 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001070 if (!TheCondState.Ignore)
1071 return TokError("unexpected token at start of statement");
1072 IDVal = "";
1073 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001074
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001075
Chris Lattner7834fac2010-04-17 18:14:27 +00001076 // Handle conditional assembly here before checking for skipping. We
1077 // have to do this so that .endif isn't skipped in a ".if 0" block for
1078 // example.
1079 if (IDVal == ".if")
1080 return ParseDirectiveIf(IDLoc);
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00001081 if (IDVal == ".ifb")
1082 return ParseDirectiveIfb(IDLoc, true);
1083 if (IDVal == ".ifnb")
1084 return ParseDirectiveIfb(IDLoc, false);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00001085 if (IDVal == ".ifc")
1086 return ParseDirectiveIfc(IDLoc, true);
1087 if (IDVal == ".ifnc")
1088 return ParseDirectiveIfc(IDLoc, false);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00001089 if (IDVal == ".ifdef")
1090 return ParseDirectiveIfdef(IDLoc, true);
1091 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
1092 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +00001093 if (IDVal == ".elseif")
1094 return ParseDirectiveElseIf(IDLoc);
1095 if (IDVal == ".else")
1096 return ParseDirectiveElse(IDLoc);
1097 if (IDVal == ".endif")
1098 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001099
Chris Lattner7834fac2010-04-17 18:14:27 +00001100 // If we are in a ".if 0" block, ignore this statement.
1101 if (TheCondState.Ignore) {
1102 EatToEndOfStatement();
1103 return false;
1104 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001105
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001106 // FIXME: Recurse on local labels?
1107
1108 // See what kind of statement we have.
1109 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001110 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001111 CheckForValidSection();
1112
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001113 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001114 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001115
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001116 // Diagnose attempt to use '.' as a label.
1117 if (IDVal == ".")
1118 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1119
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001120 // Diagnose attempt to use a variable as a label.
1121 //
1122 // FIXME: Diagnostics. Note the location of the definition as a label.
1123 // FIXME: This doesn't diagnose assignment to a symbol which has been
1124 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001125 MCSymbol *Sym;
1126 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001127 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001128 else
1129 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001130 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001131 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001132
Daniel Dunbar959fd882009-08-26 22:13:22 +00001133 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001134 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001135
Kevin Enderby94c2e852011-12-09 18:09:40 +00001136 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001137 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001138 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001139 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1140 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001141
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001142 // Consume any end of statement token, if present, to avoid spurious
1143 // AddBlankLine calls().
1144 if (Lexer.is(AsmToken::EndOfStatement)) {
1145 Lex();
1146 if (Lexer.is(AsmToken::Eof))
1147 return false;
1148 }
1149
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001150 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001151 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001152
Daniel Dunbar3f872332009-07-28 16:08:33 +00001153 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001154 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001155 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001156
Nico Weber4c4c7322011-01-28 03:04:41 +00001157 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001158
1159 default: // Normal instruction or directive.
1160 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001161 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001162
1163 // If macros are enabled, check to see if this is a macro instantiation.
1164 if (MacrosEnabled)
1165 if (const Macro *M = MacroMap.lookup(IDVal))
1166 return HandleMacroEntry(IDVal, IDLoc, M);
1167
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001168 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001169 if (IDVal[0] == '.' && IDVal != ".") {
Akira Hatanaka3b02d952012-07-05 19:09:33 +00001170
1171 // Target hook for parsing target specific directives.
1172 if (!getTargetParser().ParseDirective(ID))
1173 return false;
1174
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001175 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001176 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001177 return ParseDirectiveSet(IDVal, true);
1178 if (IDVal == ".equiv")
1179 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001180
Daniel Dunbara0d14262009-06-24 23:30:00 +00001181 // Data directives
1182
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001183 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001184 return ParseDirectiveAscii(IDVal, false);
1185 if (IDVal == ".asciz" || IDVal == ".string")
1186 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001187
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001188 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001189 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001190 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001191 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001192 if (IDVal == ".value")
1193 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001194 if (IDVal == ".2byte")
1195 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001196 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001197 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001198 if (IDVal == ".int")
1199 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001200 if (IDVal == ".4byte")
1201 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001202 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001203 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001204 if (IDVal == ".8byte")
1205 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001206 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001207 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1208 if (IDVal == ".double")
1209 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001210
Eli Friedman5d68ec22010-07-19 04:17:25 +00001211 if (IDVal == ".align") {
1212 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1213 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1214 }
1215 if (IDVal == ".align32") {
1216 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1217 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1218 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001219 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001220 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001221 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001222 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001223 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001224 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001225 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001226 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001227 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001228 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001229 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001230 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1231
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001232 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001233 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001234
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001235 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001236 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001237 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001238 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001239 if (IDVal == ".zero")
1240 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001241
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001242 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001243
Benjamin Kramere14a3c52012-05-12 11:18:59 +00001244 if (IDVal == ".extern") {
1245 EatToEndOfStatement(); // .extern is the default, ignore it.
1246 return false;
1247 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001248 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001249 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001250 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001251 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001252 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001253 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001254 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001255 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001256 if (IDVal == ".symbol_resolver")
1257 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001258 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001259 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001260 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001261 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001262 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001263 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001264 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001265 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001266 if (IDVal == ".weak_def_can_be_hidden")
1267 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001268
Hans Wennborg5cc64912011-06-18 13:51:54 +00001269 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001270 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001271 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001272 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001273
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001274 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001275 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001276 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001277 return ParseDirectiveInclude();
Kevin Enderbyc55acca2011-12-14 21:47:48 +00001278 if (IDVal == ".incbin")
1279 return ParseDirectiveIncbin();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001280
Benjamin Kramer5cdf0ad2012-05-12 11:19:04 +00001281 if (IDVal == ".code16" || IDVal == ".code16gcc")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001282 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001283
Rafael Espindola761cb062012-06-03 23:57:14 +00001284 // Macro-like directives
Rafael Espindola2ec304c2012-05-12 16:31:10 +00001285 if (IDVal == ".rept")
1286 return ParseDirectiveRept(IDLoc);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001287 if (IDVal == ".irp")
1288 return ParseDirectiveIrp(IDLoc);
Rafael Espindolafc9216e2012-06-16 18:03:25 +00001289 if (IDVal == ".irpc")
1290 return ParseDirectiveIrpc(IDLoc);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00001291 if (IDVal == ".endr")
Rafael Espindola761cb062012-06-03 23:57:14 +00001292 return ParseDirectiveEndr(IDLoc);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00001293
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001294 // Look up the handler in the handler table.
1295 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1296 DirectiveMap.lookup(IDVal);
1297 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001298 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001299
Kevin Enderby9c656452009-09-10 20:51:44 +00001300
Jim Grosbach686c0182012-05-01 18:38:27 +00001301 return Error(IDLoc, "unknown directive");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001302 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001303
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001304 CheckForValidSection();
1305
Chris Lattnera7f13542010-05-19 23:34:33 +00001306 // Canonicalize the opcode to lower case.
1307 SmallString<128> Opcode;
1308 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1309 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001310
Chris Lattner98986712010-01-14 22:21:20 +00001311 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001312 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001313 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001314
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001315 // Dump the parsed representation, if requested.
1316 if (getShowParsedOperands()) {
1317 SmallString<256> Str;
1318 raw_svector_ostream OS(Str);
1319 OS << "parsed instruction: [";
1320 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1321 if (i != 0)
1322 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001323 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001324 }
1325 OS << "]";
1326
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001327 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001328 }
1329
Kevin Enderby613b7572011-11-01 22:27:22 +00001330 // If we are generating dwarf for assembly source files and the current
1331 // section is the initial text section then generate a .loc directive for
1332 // the instruction.
1333 if (!HadError && getContext().getGenDwarfForAssembly() &&
1334 getContext().getGenDwarfSection() == getStreamer().getCurrentSection() ) {
1335 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
1336 SrcMgr.FindLineNumber(IDLoc, CurBuffer),
1337 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001338 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001339 StringRef());
1340 }
1341
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001342 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001343 if (!HadError)
1344 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1345 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001346
Chris Lattner98986712010-01-14 22:21:20 +00001347 // Free any parsed operands.
1348 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1349 delete ParsedOperands[i];
1350
Chris Lattnercbf8a982010-09-11 16:18:25 +00001351 // Don't skip the rest of the line, the instruction parser is responsible for
1352 // that.
1353 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001354}
Chris Lattner9a023f72009-06-24 04:43:34 +00001355
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001356/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1357/// since they may not be able to be tokenized to get to the end of line token.
1358void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001359 if (!Lexer.is(AsmToken::EndOfStatement))
1360 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001361 // Eat EOL.
1362 Lex();
1363}
1364
1365/// ParseCppHashLineFilenameComment as this:
1366/// ::= # number "filename"
1367/// or just as a full line comment if it doesn't have a number and a string.
1368bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1369 Lex(); // Eat the hash token.
1370
1371 if (getLexer().isNot(AsmToken::Integer)) {
1372 // Consume the line since in cases it is not a well-formed line directive,
1373 // as if were simply a full line comment.
1374 EatToEndOfLine();
1375 return false;
1376 }
1377
1378 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001379 Lex();
1380
1381 if (getLexer().isNot(AsmToken::String)) {
1382 EatToEndOfLine();
1383 return false;
1384 }
1385
1386 StringRef Filename = getTok().getString();
1387 // Get rid of the enclosing quotes.
1388 Filename = Filename.substr(1, Filename.size()-2);
1389
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001390 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1391 CppHashLoc = L;
1392 CppHashFilename = Filename;
1393 CppHashLineNumber = LineNumber;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001394
1395 // Ignore any trailing characters, they're just comment.
1396 EatToEndOfLine();
1397 return false;
1398}
1399
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001400/// DiagHandler - will use the last parsed cpp hash line filename comment
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001401/// for the Filename and LineNo if any in the diagnostic.
1402void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1403 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1404 raw_ostream &OS = errs();
1405
1406 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1407 const SMLoc &DiagLoc = Diag.getLoc();
1408 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1409 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1410
1411 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1412 // before printing the message.
1413 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001414 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001415 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1416 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1417 }
1418
1419 // If we have not parsed a cpp hash line filename comment or the source
1420 // manager changed or buffer changed (like in a nested include) then just
1421 // print the normal diagnostic using its Filename and LineNo.
1422 if (!Parser->CppHashLineNumber ||
1423 &DiagSrcMgr != &Parser->SrcMgr ||
1424 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001425 if (Parser->SavedDiagHandler)
1426 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1427 else
1428 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001429 return;
1430 }
1431
1432 // Use the CppHashFilename and calculate a line number based on the
1433 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1434 // the diagnostic.
1435 const std::string Filename = Parser->CppHashFilename;
1436
1437 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1438 int CppHashLocLineNo =
1439 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1440 int LineNo = Parser->CppHashLineNumber - 1 +
1441 (DiagLocLineNo - CppHashLocLineNo);
1442
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001443 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1444 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001445 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001446 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001447
Benjamin Kramer04a04262011-10-16 10:48:29 +00001448 if (Parser->SavedDiagHandler)
1449 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1450 else
1451 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001452}
1453
Rafael Espindola761cb062012-06-03 23:57:14 +00001454bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola65366442011-06-05 02:43:45 +00001455 const std::vector<StringRef> &Parameters,
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001456 const std::vector<MacroArgument> &A,
Rafael Espindola65366442011-06-05 02:43:45 +00001457 const SMLoc &L) {
Rafael Espindola65366442011-06-05 02:43:45 +00001458 unsigned NParameters = Parameters.size();
1459 if (NParameters != 0 && NParameters != A.size())
1460 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001461
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001462 while (!Body.empty()) {
1463 // Scan for the next substitution.
1464 std::size_t End = Body.size(), Pos = 0;
1465 for (; Pos != End; ++Pos) {
1466 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001467 if (!NParameters) {
1468 // This macro has no parameters, look for $0, $1, etc.
1469 if (Body[Pos] != '$' || Pos + 1 == End)
1470 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001471
Rafael Espindola65366442011-06-05 02:43:45 +00001472 char Next = Body[Pos + 1];
1473 if (Next == '$' || Next == 'n' || isdigit(Next))
1474 break;
1475 } else {
1476 // This macro has parameters, look for \foo, \bar, etc.
1477 if (Body[Pos] == '\\' && Pos + 1 != End)
1478 break;
1479 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001480 }
1481
1482 // Add the prefix.
1483 OS << Body.slice(0, Pos);
1484
1485 // Check if we reached the end.
1486 if (Pos == End)
1487 break;
1488
Rafael Espindola65366442011-06-05 02:43:45 +00001489 if (!NParameters) {
1490 switch (Body[Pos+1]) {
1491 // $$ => $
1492 case '$':
1493 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001494 break;
1495
Rafael Espindola65366442011-06-05 02:43:45 +00001496 // $n => number of arguments
1497 case 'n':
1498 OS << A.size();
1499 break;
1500
1501 // $[0-9] => argument
1502 default: {
1503 // Missing arguments are ignored.
1504 unsigned Index = Body[Pos+1] - '0';
1505 if (Index >= A.size())
1506 break;
1507
1508 // Otherwise substitute with the token values, with spaces eliminated.
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001509 for (MacroArgument::const_iterator it = A[Index].begin(),
Rafael Espindola65366442011-06-05 02:43:45 +00001510 ie = A[Index].end(); it != ie; ++it)
1511 OS << it->getString();
1512 break;
1513 }
1514 }
1515 Pos += 2;
1516 } else {
1517 unsigned I = Pos + 1;
1518 while (isalnum(Body[I]) && I + 1 != End)
1519 ++I;
1520
1521 const char *Begin = Body.data() + Pos +1;
1522 StringRef Argument(Begin, I - (Pos +1));
1523 unsigned Index = 0;
1524 for (; Index < NParameters; ++Index)
1525 if (Parameters[Index] == Argument)
1526 break;
1527
1528 // FIXME: We should error at the macro definition.
1529 if (Index == NParameters)
1530 return Error(L, "Parameter not found");
1531
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001532 for (MacroArgument::const_iterator it = A[Index].begin(),
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001533 ie = A[Index].end(); it != ie; ++it)
1534 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001535
Rafael Espindola65366442011-06-05 02:43:45 +00001536 Pos += 1 + Argument.size();
1537 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001538 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001539 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001540 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001541
Rafael Espindola65366442011-06-05 02:43:45 +00001542 return false;
1543}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001544
Rafael Espindola65366442011-06-05 02:43:45 +00001545MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1546 MemoryBuffer *I)
1547 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1548{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001549}
1550
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001551/// ParseMacroArgument - Extract AsmTokens for a macro argument.
1552/// This is used for both default macro parameter values and the
1553/// arguments in macro invocations
1554bool AsmParser::ParseMacroArgument(MacroArgument &MA) {
1555 unsigned ParenLevel = 0;
1556
1557 for (;;) {
1558 SMLoc LastTokenLoc;
1559
1560 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
1561 return TokError("unexpected token in macro instantiation");
1562
1563 // HandleMacroEntry relies on not advancing the lexer here
1564 // to be able to fill in the remaining default parameter values
1565 if (Lexer.is(AsmToken::EndOfStatement))
1566 break;
1567 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
1568 break;
1569
1570 // Adjust the current parentheses level.
1571 if (Lexer.is(AsmToken::LParen))
1572 ++ParenLevel;
1573 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1574 --ParenLevel;
1575
1576 // Append the token to the current argument list.
1577 MA.push_back(getTok());
1578 Lex();
1579 }
1580 if (ParenLevel != 0)
1581 return TokError("unbalanced parenthesises in macro argument");
1582 return false;
1583}
1584
1585// Parse the macro instantiation arguments.
1586bool AsmParser::ParseMacroArguments(const Macro *M,
1587 std::vector<MacroArgument> &A) {
1588 const unsigned NParameters = M ? M->Parameters.size() : 0;
1589
1590 // Parse two kinds of macro invocations:
1591 // - macros defined without any parameters accept an arbitrary number of them
1592 // - macros defined with parameters accept at most that many of them
1593 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1594 ++Parameter) {
1595 MacroArgument MA;
1596
1597 if (ParseMacroArgument(MA))
1598 return true;
1599
1600 if (!MA.empty())
1601 A.push_back(MA);
1602 if (Lexer.is(AsmToken::EndOfStatement))
1603 return false;
1604
1605 if (Lexer.is(AsmToken::Comma))
1606 Lex();
1607 }
1608 return TokError("Too many arguments");
1609}
1610
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001611bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1612 const Macro *M) {
1613 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1614 // this, although we should protect against infinite loops.
1615 if (ActiveMacros.size() == 20)
1616 return TokError("macros cannot be nested more than 20 levels deep");
1617
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001618 std::vector<MacroArgument> MacroArguments;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001619 if (ParseMacroArguments(M, MacroArguments))
1620 return true;
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001621
Rafael Espindola65366442011-06-05 02:43:45 +00001622 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1623 // to hold the macro body with substitutions.
1624 SmallString<256> Buf;
1625 StringRef Body = M->Body;
Rafael Espindola761cb062012-06-03 23:57:14 +00001626 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001627
Rafael Espindola761cb062012-06-03 23:57:14 +00001628 if (expandMacro(OS, Body, M->Parameters, MacroArguments, getTok().getLoc()))
Rafael Espindola65366442011-06-05 02:43:45 +00001629 return true;
1630
Rafael Espindola761cb062012-06-03 23:57:14 +00001631 // We include the .endmacro in the buffer as our queue to exit the macro
1632 // instantiation.
1633 OS << ".endmacro\n";
1634
Rafael Espindola65366442011-06-05 02:43:45 +00001635 MemoryBuffer *Instantiation =
Rafael Espindola761cb062012-06-03 23:57:14 +00001636 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola65366442011-06-05 02:43:45 +00001637
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001638 // Create the macro instantiation object and add to the current macro
1639 // instantiation stack.
1640 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001641 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001642 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001643 ActiveMacros.push_back(MI);
1644
1645 // Jump to the macro instantiation and prime the lexer.
1646 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1647 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1648 Lex();
1649
1650 return false;
1651}
1652
1653void AsmParser::HandleMacroExit() {
1654 // Jump to the EndOfStatement we should return to, and consume it.
1655 JumpToLoc(ActiveMacros.back()->ExitLoc);
1656 Lex();
1657
1658 // Pop the instantiation entry.
1659 delete ActiveMacros.back();
1660 ActiveMacros.pop_back();
1661}
1662
Rafael Espindolae71cc862012-01-28 05:57:00 +00001663static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001664 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001665 case MCExpr::Binary: {
1666 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1667 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001668 break;
1669 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001670 case MCExpr::Target:
1671 case MCExpr::Constant:
1672 return false;
1673 case MCExpr::SymbolRef: {
1674 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001675 if (S.isVariable())
1676 return IsUsedIn(Sym, S.getVariableValue());
1677 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001678 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001679 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001680 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001681 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001682
1683 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001684}
1685
Nico Weber4c4c7322011-01-28 03:04:41 +00001686bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001687 // FIXME: Use better location, we should use proper tokens.
1688 SMLoc EqualLoc = Lexer.getLoc();
1689
Daniel Dunbar821e3332009-08-31 08:09:28 +00001690 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001691 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001692 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001693
Rafael Espindolae71cc862012-01-28 05:57:00 +00001694 // Note: we don't count b as used in "a = b". This is to allow
1695 // a = b
1696 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001697
Daniel Dunbar3f872332009-07-28 16:08:33 +00001698 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001699 return TokError("unexpected token in assignment");
1700
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001701 // Error on assignment to '.'.
1702 if (Name == ".") {
1703 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1704 "(use '.space' or '.org').)"));
1705 }
1706
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001707 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001708 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001709
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001710 // Validate that the LHS is allowed to be a variable (either it has not been
1711 // used as a symbol, or it is an absolute symbol).
1712 MCSymbol *Sym = getContext().LookupSymbol(Name);
1713 if (Sym) {
1714 // Diagnose assignment to a label.
1715 //
1716 // FIXME: Diagnostics. Note the location of the definition as a label.
1717 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001718 if (IsUsedIn(Sym, Value))
1719 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1720 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001721 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00001722 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1723 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001724 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001725 return Error(EqualLoc, "redefinition of '" + Name + "'");
1726 else if (!Sym->isVariable())
1727 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001728 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001729 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1730 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001731
1732 // Don't count these checks as uses.
1733 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001734 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001735 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001736
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001737 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001738
1739 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001740 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001741
1742 return false;
1743}
1744
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001745/// ParseIdentifier:
1746/// ::= identifier
1747/// ::= string
1748bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001749 // The assembler has relaxed rules for accepting identifiers, in particular we
1750 // allow things like '.globl $foo', which would normally be separate
1751 // tokens. At this level, we have already lexed so we cannot (currently)
1752 // handle this as a context dependent token, instead we detect adjacent tokens
1753 // and return the combined identifier.
1754 if (Lexer.is(AsmToken::Dollar)) {
1755 SMLoc DollarLoc = getLexer().getLoc();
1756
1757 // Consume the dollar sign, and check for a following identifier.
1758 Lex();
1759 if (Lexer.isNot(AsmToken::Identifier))
1760 return true;
1761
1762 // We have a '$' followed by an identifier, make sure they are adjacent.
1763 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1764 return true;
1765
1766 // Construct the joined identifier and consume the token.
1767 Res = StringRef(DollarLoc.getPointer(),
1768 getTok().getIdentifier().size() + 1);
1769 Lex();
1770 return false;
1771 }
1772
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001773 if (Lexer.isNot(AsmToken::Identifier) &&
1774 Lexer.isNot(AsmToken::String))
1775 return true;
1776
Sean Callanan18b83232010-01-19 21:44:56 +00001777 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001778
Sean Callanan79ed1a82010-01-19 20:22:31 +00001779 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001780
1781 return false;
1782}
1783
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001784/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001785/// ::= .equ identifier ',' expression
1786/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001787/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001788bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001789 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001790
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001791 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001792 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001793
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001794 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001795 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001796 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001797
Nico Weber4c4c7322011-01-28 03:04:41 +00001798 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001799}
1800
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001801bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001802 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001803
1804 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001805 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001806 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1807 if (Str[i] != '\\') {
1808 Data += Str[i];
1809 continue;
1810 }
1811
1812 // Recognize escaped characters. Note that this escape semantics currently
1813 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1814 ++i;
1815 if (i == e)
1816 return TokError("unexpected backslash at end of string");
1817
1818 // Recognize octal sequences.
1819 if ((unsigned) (Str[i] - '0') <= 7) {
1820 // Consume up to three octal characters.
1821 unsigned Value = Str[i] - '0';
1822
1823 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1824 ++i;
1825 Value = Value * 8 + (Str[i] - '0');
1826
1827 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1828 ++i;
1829 Value = Value * 8 + (Str[i] - '0');
1830 }
1831 }
1832
1833 if (Value > 255)
1834 return TokError("invalid octal escape sequence (out of range)");
1835
1836 Data += (unsigned char) Value;
1837 continue;
1838 }
1839
1840 // Otherwise recognize individual escapes.
1841 switch (Str[i]) {
1842 default:
1843 // Just reject invalid escape sequences for now.
1844 return TokError("invalid escape sequence (unrecognized character)");
1845
1846 case 'b': Data += '\b'; break;
1847 case 'f': Data += '\f'; break;
1848 case 'n': Data += '\n'; break;
1849 case 'r': Data += '\r'; break;
1850 case 't': Data += '\t'; break;
1851 case '"': Data += '"'; break;
1852 case '\\': Data += '\\'; break;
1853 }
1854 }
1855
1856 return false;
1857}
1858
Daniel Dunbara0d14262009-06-24 23:30:00 +00001859/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001860/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1861bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001862 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001863 CheckForValidSection();
1864
Daniel Dunbara0d14262009-06-24 23:30:00 +00001865 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001866 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001867 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001868
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001869 std::string Data;
1870 if (ParseEscapedString(Data))
1871 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001872
1873 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001874 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001875 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1876
Sean Callanan79ed1a82010-01-19 20:22:31 +00001877 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001878
1879 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001880 break;
1881
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001882 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001883 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001884 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001885 }
1886 }
1887
Sean Callanan79ed1a82010-01-19 20:22:31 +00001888 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001889 return false;
1890}
1891
1892/// ParseDirectiveValue
1893/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1894bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001895 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001896 CheckForValidSection();
1897
Daniel Dunbara0d14262009-06-24 23:30:00 +00001898 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001899 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001900 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001901 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001902 return true;
1903
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001904 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001905 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1906 assert(Size <= 8 && "Invalid size");
1907 uint64_t IntValue = MCE->getValue();
1908 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1909 return Error(ExprLoc, "literal value out of range for directive");
1910 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1911 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001912 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001913
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001914 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001915 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001916
Daniel Dunbara0d14262009-06-24 23:30:00 +00001917 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001918 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001919 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001920 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001921 }
1922 }
1923
Sean Callanan79ed1a82010-01-19 20:22:31 +00001924 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001925 return false;
1926}
1927
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001928/// ParseDirectiveRealValue
1929/// ::= (.single | .double) [ expression (, expression)* ]
1930bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1931 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1932 CheckForValidSection();
1933
1934 for (;;) {
1935 // We don't truly support arithmetic on floating point expressions, so we
1936 // have to manually parse unary prefixes.
1937 bool IsNeg = false;
1938 if (getLexer().is(AsmToken::Minus)) {
1939 Lex();
1940 IsNeg = true;
1941 } else if (getLexer().is(AsmToken::Plus))
1942 Lex();
1943
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001944 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001945 getLexer().isNot(AsmToken::Real) &&
1946 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001947 return TokError("unexpected token in directive");
1948
1949 // Convert to an APFloat.
1950 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001951 StringRef IDVal = getTok().getString();
1952 if (getLexer().is(AsmToken::Identifier)) {
1953 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1954 Value = APFloat::getInf(Semantics);
1955 else if (!IDVal.compare_lower("nan"))
1956 Value = APFloat::getNaN(Semantics, false, ~0);
1957 else
1958 return TokError("invalid floating point literal");
1959 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001960 APFloat::opInvalidOp)
1961 return TokError("invalid floating point literal");
1962 if (IsNeg)
1963 Value.changeSign();
1964
1965 // Consume the numeric token.
1966 Lex();
1967
1968 // Emit the value as an integer.
1969 APInt AsInt = Value.bitcastToAPInt();
1970 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1971 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1972
1973 if (getLexer().is(AsmToken::EndOfStatement))
1974 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001975
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001976 if (getLexer().isNot(AsmToken::Comma))
1977 return TokError("unexpected token in directive");
1978 Lex();
1979 }
1980 }
1981
1982 Lex();
1983 return false;
1984}
1985
Daniel Dunbara0d14262009-06-24 23:30:00 +00001986/// ParseDirectiveSpace
1987/// ::= .space expression [ , expression ]
1988bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001989 CheckForValidSection();
1990
Daniel Dunbara0d14262009-06-24 23:30:00 +00001991 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001992 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001993 return true;
1994
1995 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001996 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1997 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001998 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001999 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002000
Daniel Dunbar475839e2009-06-29 20:37:27 +00002001 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002002 return true;
2003
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002004 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002005 return TokError("unexpected token in '.space' directive");
2006 }
2007
Sean Callanan79ed1a82010-01-19 20:22:31 +00002008 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002009
2010 if (NumBytes <= 0)
2011 return TokError("invalid number of bytes in '.space' directive");
2012
2013 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002014 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002015
2016 return false;
2017}
2018
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002019/// ParseDirectiveZero
2020/// ::= .zero expression
2021bool AsmParser::ParseDirectiveZero() {
2022 CheckForValidSection();
2023
2024 int64_t NumBytes;
2025 if (ParseAbsoluteExpression(NumBytes))
2026 return true;
2027
Rafael Espindolae452b172010-10-05 19:42:57 +00002028 int64_t Val = 0;
2029 if (getLexer().is(AsmToken::Comma)) {
2030 Lex();
2031 if (ParseAbsoluteExpression(Val))
2032 return true;
2033 }
2034
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002035 if (getLexer().isNot(AsmToken::EndOfStatement))
2036 return TokError("unexpected token in '.zero' directive");
2037
2038 Lex();
2039
Rafael Espindolae452b172010-10-05 19:42:57 +00002040 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002041
2042 return false;
2043}
2044
Daniel Dunbara0d14262009-06-24 23:30:00 +00002045/// ParseDirectiveFill
2046/// ::= .fill expression , expression , expression
2047bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002048 CheckForValidSection();
2049
Daniel Dunbara0d14262009-06-24 23:30:00 +00002050 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002051 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002052 return true;
2053
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002054 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002055 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002056 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002057
Daniel Dunbara0d14262009-06-24 23:30:00 +00002058 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002059 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002060 return true;
2061
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002062 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002063 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002064 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002065
Daniel Dunbara0d14262009-06-24 23:30:00 +00002066 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002067 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002068 return true;
2069
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002070 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002071 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002072
Sean Callanan79ed1a82010-01-19 20:22:31 +00002073 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002074
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002075 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2076 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002077
2078 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002079 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002080
2081 return false;
2082}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002083
2084/// ParseDirectiveOrg
2085/// ::= .org expression [ , expression ]
2086bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002087 CheckForValidSection();
2088
Daniel Dunbar821e3332009-08-31 08:09:28 +00002089 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002090 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002091 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002092 return true;
2093
2094 // Parse optional fill expression.
2095 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002096 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2097 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002098 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002099 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002100
Daniel Dunbar475839e2009-06-29 20:37:27 +00002101 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002102 return true;
2103
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002104 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002105 return TokError("unexpected token in '.org' directive");
2106 }
2107
Sean Callanan79ed1a82010-01-19 20:22:31 +00002108 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002109
Jim Grosbachebd4c052012-01-27 00:37:08 +00002110 // Only limited forms of relocatable expressions are accepted here, it
2111 // has to be relative to the current section. The streamer will return
2112 // 'true' if the expression wasn't evaluatable.
2113 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2114 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002115
2116 return false;
2117}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002118
2119/// ParseDirectiveAlign
2120/// ::= {.align, ...} expression [ , expression [ , expression ]]
2121bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002122 CheckForValidSection();
2123
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002124 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002125 int64_t Alignment;
2126 if (ParseAbsoluteExpression(Alignment))
2127 return true;
2128
2129 SMLoc MaxBytesLoc;
2130 bool HasFillExpr = false;
2131 int64_t FillExpr = 0;
2132 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002133 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2134 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002135 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002136 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002137
2138 // The fill expression can be omitted while specifying a maximum number of
2139 // alignment bytes, e.g:
2140 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002141 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002142 HasFillExpr = true;
2143 if (ParseAbsoluteExpression(FillExpr))
2144 return true;
2145 }
2146
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002147 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2148 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002149 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002150 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002151
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002152 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002153 if (ParseAbsoluteExpression(MaxBytesToFill))
2154 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002155
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002156 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002157 return TokError("unexpected token in directive");
2158 }
2159 }
2160
Sean Callanan79ed1a82010-01-19 20:22:31 +00002161 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002162
Daniel Dunbar648ac512010-05-17 21:54:30 +00002163 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002164 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002165
2166 // Compute alignment in bytes.
2167 if (IsPow2) {
2168 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002169 if (Alignment >= 32) {
2170 Error(AlignmentLoc, "invalid alignment value");
2171 Alignment = 31;
2172 }
2173
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002174 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002175 }
2176
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002177 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002178 if (MaxBytesLoc.isValid()) {
2179 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002180 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2181 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002182 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002183 }
2184
2185 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002186 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2187 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002188 MaxBytesToFill = 0;
2189 }
2190 }
2191
Daniel Dunbar648ac512010-05-17 21:54:30 +00002192 // Check whether we should use optimal code alignment for this .align
2193 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002194 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002195 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2196 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002197 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002198 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002199 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002200 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2201 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002202 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002203
2204 return false;
2205}
2206
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002207/// ParseDirectiveSymbolAttribute
2208/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002209bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002210 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002211 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002212 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002213 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002214
2215 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002216 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002217
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002218 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002219
Jim Grosbach10ec6502011-09-15 17:56:49 +00002220 // Assembler local symbols don't make any sense here. Complain loudly.
2221 if (Sym->isTemporary())
2222 return Error(Loc, "non-local symbol required in directive");
2223
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002224 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002225
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002226 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002227 break;
2228
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002229 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002230 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002231 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002232 }
2233 }
2234
Sean Callanan79ed1a82010-01-19 20:22:31 +00002235 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002236 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002237}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002238
2239/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002240/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2241bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002242 CheckForValidSection();
2243
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002244 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002245 StringRef Name;
2246 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002247 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002248
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002249 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002250 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002251
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002252 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002253 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002254 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002255
2256 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002257 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002258 if (ParseAbsoluteExpression(Size))
2259 return true;
2260
2261 int64_t Pow2Alignment = 0;
2262 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002263 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002264 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002265 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002266 if (ParseAbsoluteExpression(Pow2Alignment))
2267 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002268
Chris Lattner258281d2010-01-19 06:22:22 +00002269 // If this target takes alignments in bytes (not log) validate and convert.
2270 if (Lexer.getMAI().getAlignmentIsInBytes()) {
2271 if (!isPowerOf2_64(Pow2Alignment))
2272 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2273 Pow2Alignment = Log2_64(Pow2Alignment);
2274 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002275 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002276
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002277 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002278 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002279
Sean Callanan79ed1a82010-01-19 20:22:31 +00002280 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002281
Chris Lattner1fc3d752009-07-09 17:25:12 +00002282 // NOTE: a size of zero for a .comm should create a undefined symbol
2283 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002284 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002285 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2286 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002287
Eric Christopherc260a3e2010-05-14 01:38:54 +00002288 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002289 // may internally end up wanting an alignment in bytes.
2290 // FIXME: Diagnose overflow.
2291 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002292 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2293 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002294
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002295 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002296 return Error(IDLoc, "invalid symbol redefinition");
2297
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002298 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00002299 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002300 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002301 getStreamer().EmitZerofill(Ctx.getMachOSection(
2302 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2303 0, SectionKind::getBSS()),
2304 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002305 return false;
2306 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002307
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002308 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002309 return false;
2310}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002311
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002312/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002313/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002314bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002315 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002316 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002317
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002318 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002319 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002320 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002321
Sean Callanan79ed1a82010-01-19 20:22:31 +00002322 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002323
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002324 if (Str.empty())
2325 Error(Loc, ".abort detected. Assembly stopping.");
2326 else
2327 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002328 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002329
2330 return false;
2331}
Kevin Enderby71148242009-07-14 21:35:03 +00002332
Kevin Enderby1f049b22009-07-14 23:21:55 +00002333/// ParseDirectiveInclude
2334/// ::= .include "filename"
2335bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002336 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002337 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002338
Sean Callanan18b83232010-01-19 21:44:56 +00002339 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002340 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002341 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002342
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002343 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002344 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002345
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002346 // Strip the quotes.
2347 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002348
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002349 // Attempt to switch the lexer to the included file before consuming the end
2350 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002351 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002352 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002353 return true;
2354 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002355
2356 return false;
2357}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002358
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002359/// ParseDirectiveIncbin
2360/// ::= .incbin "filename"
2361bool AsmParser::ParseDirectiveIncbin() {
2362 if (getLexer().isNot(AsmToken::String))
2363 return TokError("expected string in '.incbin' directive");
2364
2365 std::string Filename = getTok().getString();
2366 SMLoc IncbinLoc = getLexer().getLoc();
2367 Lex();
2368
2369 if (getLexer().isNot(AsmToken::EndOfStatement))
2370 return TokError("unexpected token in '.incbin' directive");
2371
2372 // Strip the quotes.
2373 Filename = Filename.substr(1, Filename.size()-2);
2374
2375 // Attempt to process the included file.
2376 if (ProcessIncbinFile(Filename)) {
2377 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2378 return true;
2379 }
2380
2381 return false;
2382}
2383
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002384/// ParseDirectiveIf
2385/// ::= .if expression
2386bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002387 TheCondStack.push_back(TheCondState);
2388 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramer29739e72012-05-12 16:52:21 +00002389 if (TheCondState.Ignore) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002390 EatToEndOfStatement();
Benjamin Kramer29739e72012-05-12 16:52:21 +00002391 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002392 int64_t ExprValue;
2393 if (ParseAbsoluteExpression(ExprValue))
2394 return true;
2395
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002396 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002397 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002398
Sean Callanan79ed1a82010-01-19 20:22:31 +00002399 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002400
2401 TheCondState.CondMet = ExprValue;
2402 TheCondState.Ignore = !TheCondState.CondMet;
2403 }
2404
2405 return false;
2406}
2407
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002408/// ParseDirectiveIfb
2409/// ::= .ifb string
2410bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
2411 TheCondStack.push_back(TheCondState);
2412 TheCondState.TheCond = AsmCond::IfCond;
2413
Benjamin Kramer29739e72012-05-12 16:52:21 +00002414 if (TheCondState.Ignore) {
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002415 EatToEndOfStatement();
2416 } else {
2417 StringRef Str = ParseStringToEndOfStatement();
2418
2419 if (getLexer().isNot(AsmToken::EndOfStatement))
2420 return TokError("unexpected token in '.ifb' directive");
2421
2422 Lex();
2423
2424 TheCondState.CondMet = ExpectBlank == Str.empty();
2425 TheCondState.Ignore = !TheCondState.CondMet;
2426 }
2427
2428 return false;
2429}
2430
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002431/// ParseDirectiveIfc
2432/// ::= .ifc string1, string2
2433bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
2434 TheCondStack.push_back(TheCondState);
2435 TheCondState.TheCond = AsmCond::IfCond;
2436
Benjamin Kramer29739e72012-05-12 16:52:21 +00002437 if (TheCondState.Ignore) {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002438 EatToEndOfStatement();
2439 } else {
2440 StringRef Str1 = ParseStringToComma();
2441
2442 if (getLexer().isNot(AsmToken::Comma))
2443 return TokError("unexpected token in '.ifc' directive");
2444
2445 Lex();
2446
2447 StringRef Str2 = ParseStringToEndOfStatement();
2448
2449 if (getLexer().isNot(AsmToken::EndOfStatement))
2450 return TokError("unexpected token in '.ifc' directive");
2451
2452 Lex();
2453
2454 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
2455 TheCondState.Ignore = !TheCondState.CondMet;
2456 }
2457
2458 return false;
2459}
2460
2461/// ParseDirectiveIfdef
2462/// ::= .ifdef symbol
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002463bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2464 StringRef Name;
2465 TheCondStack.push_back(TheCondState);
2466 TheCondState.TheCond = AsmCond::IfCond;
2467
2468 if (TheCondState.Ignore) {
2469 EatToEndOfStatement();
2470 } else {
2471 if (ParseIdentifier(Name))
2472 return TokError("expected identifier after '.ifdef'");
2473
2474 Lex();
2475
2476 MCSymbol *Sym = getContext().LookupSymbol(Name);
2477
2478 if (expect_defined)
2479 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2480 else
2481 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2482 TheCondState.Ignore = !TheCondState.CondMet;
2483 }
2484
2485 return false;
2486}
2487
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002488/// ParseDirectiveElseIf
2489/// ::= .elseif expression
2490bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2491 if (TheCondState.TheCond != AsmCond::IfCond &&
2492 TheCondState.TheCond != AsmCond::ElseIfCond)
2493 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2494 " an .elseif");
2495 TheCondState.TheCond = AsmCond::ElseIfCond;
2496
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002497 bool LastIgnoreState = false;
2498 if (!TheCondStack.empty())
2499 LastIgnoreState = TheCondStack.back().Ignore;
2500 if (LastIgnoreState || TheCondState.CondMet) {
2501 TheCondState.Ignore = true;
2502 EatToEndOfStatement();
2503 }
2504 else {
2505 int64_t ExprValue;
2506 if (ParseAbsoluteExpression(ExprValue))
2507 return true;
2508
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002509 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002510 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002511
Sean Callanan79ed1a82010-01-19 20:22:31 +00002512 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002513 TheCondState.CondMet = ExprValue;
2514 TheCondState.Ignore = !TheCondState.CondMet;
2515 }
2516
2517 return false;
2518}
2519
2520/// ParseDirectiveElse
2521/// ::= .else
2522bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002523 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002524 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002525
Sean Callanan79ed1a82010-01-19 20:22:31 +00002526 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002527
2528 if (TheCondState.TheCond != AsmCond::IfCond &&
2529 TheCondState.TheCond != AsmCond::ElseIfCond)
2530 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2531 ".elseif");
2532 TheCondState.TheCond = AsmCond::ElseCond;
2533 bool LastIgnoreState = false;
2534 if (!TheCondStack.empty())
2535 LastIgnoreState = TheCondStack.back().Ignore;
2536 if (LastIgnoreState || TheCondState.CondMet)
2537 TheCondState.Ignore = true;
2538 else
2539 TheCondState.Ignore = false;
2540
2541 return false;
2542}
2543
2544/// ParseDirectiveEndIf
2545/// ::= .endif
2546bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002547 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002548 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002549
Sean Callanan79ed1a82010-01-19 20:22:31 +00002550 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002551
2552 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2553 TheCondStack.empty())
2554 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2555 ".else");
2556 if (!TheCondStack.empty()) {
2557 TheCondState = TheCondStack.back();
2558 TheCondStack.pop_back();
2559 }
2560
2561 return false;
2562}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002563
2564/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002565/// ::= .file [number] filename
2566/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002567bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002568 // FIXME: I'm not sure what this is.
2569 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002570 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002571 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002572 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002573 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002574
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002575 if (FileNumber < 1)
2576 return TokError("file number less than one");
2577 }
2578
Daniel Dunbareceec052010-07-12 17:45:27 +00002579 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002580 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002581
Nick Lewycky44d798d2011-10-17 23:05:28 +00002582 // Usually the directory and filename together, otherwise just the directory.
2583 StringRef Path = getTok().getString();
2584 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002585 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002586
Nick Lewycky44d798d2011-10-17 23:05:28 +00002587 StringRef Directory;
2588 StringRef Filename;
2589 if (getLexer().is(AsmToken::String)) {
2590 if (FileNumber == -1)
2591 return TokError("explicit path specified, but no file number");
2592 Filename = getTok().getString();
2593 Filename = Filename.substr(1, Filename.size()-2);
2594 Directory = Path;
2595 Lex();
2596 } else {
2597 Filename = Path;
2598 }
2599
Daniel Dunbareceec052010-07-12 17:45:27 +00002600 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002601 return TokError("unexpected token in '.file' directive");
2602
Chris Lattnerd32e8032010-01-25 19:02:58 +00002603 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002604 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002605 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002606 if (getContext().getGenDwarfForAssembly() == true)
2607 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2608 "used to generate dwarf debug info for assembly code");
2609
Nick Lewycky44d798d2011-10-17 23:05:28 +00002610 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002611 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002612 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002613
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002614 return false;
2615}
2616
2617/// ParseDirectiveLine
2618/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002619bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002620 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2621 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002622 return TokError("unexpected token in '.line' directive");
2623
Sean Callanan18b83232010-01-19 21:44:56 +00002624 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002625 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002626 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002627
2628 // FIXME: Do something with the .line.
2629 }
2630
Daniel Dunbareceec052010-07-12 17:45:27 +00002631 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002632 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002633
2634 return false;
2635}
2636
2637
2638/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002639/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002640/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2641/// The first number is a file number, must have been previously assigned with
2642/// a .file directive, the second number is the line number and optionally the
2643/// third number is a column position (zero if not specified). The remaining
2644/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002645bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002646
Daniel Dunbareceec052010-07-12 17:45:27 +00002647 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002648 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002649 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002650 if (FileNumber < 1)
2651 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002652 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002653 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002654 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002655
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002656 int64_t LineNumber = 0;
2657 if (getLexer().is(AsmToken::Integer)) {
2658 LineNumber = getTok().getIntVal();
2659 if (LineNumber < 1)
2660 return TokError("line number less than one in '.loc' directive");
2661 Lex();
2662 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002663
2664 int64_t ColumnPos = 0;
2665 if (getLexer().is(AsmToken::Integer)) {
2666 ColumnPos = getTok().getIntVal();
2667 if (ColumnPos < 0)
2668 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002669 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002670 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002671
Kevin Enderbyc0957932010-09-30 16:52:03 +00002672 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002673 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002674 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002675 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2676 for (;;) {
2677 if (getLexer().is(AsmToken::EndOfStatement))
2678 break;
2679
2680 StringRef Name;
2681 SMLoc Loc = getTok().getLoc();
2682 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002683 return TokError("unexpected token in '.loc' directive");
2684
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002685 if (Name == "basic_block")
2686 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2687 else if (Name == "prologue_end")
2688 Flags |= DWARF2_FLAG_PROLOGUE_END;
2689 else if (Name == "epilogue_begin")
2690 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2691 else if (Name == "is_stmt") {
2692 SMLoc Loc = getTok().getLoc();
2693 const MCExpr *Value;
2694 if (getParser().ParseExpression(Value))
2695 return true;
2696 // The expression must be the constant 0 or 1.
2697 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2698 int Value = MCE->getValue();
2699 if (Value == 0)
2700 Flags &= ~DWARF2_FLAG_IS_STMT;
2701 else if (Value == 1)
2702 Flags |= DWARF2_FLAG_IS_STMT;
2703 else
2704 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002705 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002706 else {
2707 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2708 }
2709 }
2710 else if (Name == "isa") {
2711 SMLoc Loc = getTok().getLoc();
2712 const MCExpr *Value;
2713 if (getParser().ParseExpression(Value))
2714 return true;
2715 // The expression must be a constant greater or equal to 0.
2716 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2717 int Value = MCE->getValue();
2718 if (Value < 0)
2719 return Error(Loc, "isa number less than zero");
2720 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002721 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002722 else {
2723 return Error(Loc, "isa number not a constant value");
2724 }
2725 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002726 else if (Name == "discriminator") {
2727 if (getParser().ParseAbsoluteExpression(Discriminator))
2728 return true;
2729 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002730 else {
2731 return Error(Loc, "unknown sub-directive in '.loc' directive");
2732 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002733
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002734 if (getLexer().is(AsmToken::EndOfStatement))
2735 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002736 }
2737 }
2738
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002739 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002740 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002741
2742 return false;
2743}
2744
Daniel Dunbar138abae2010-10-16 04:56:42 +00002745/// ParseDirectiveStabs
2746/// ::= .stabs string, number, number, number
2747bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2748 SMLoc DirectiveLoc) {
2749 return TokError("unsupported directive '" + Directive + "'");
2750}
2751
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002752/// ParseDirectiveCFISections
2753/// ::= .cfi_sections section [, section]
2754bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2755 SMLoc DirectiveLoc) {
2756 StringRef Name;
2757 bool EH = false;
2758 bool Debug = false;
2759
2760 if (getParser().ParseIdentifier(Name))
2761 return TokError("Expected an identifier");
2762
2763 if (Name == ".eh_frame")
2764 EH = true;
2765 else if (Name == ".debug_frame")
2766 Debug = true;
2767
2768 if (getLexer().is(AsmToken::Comma)) {
2769 Lex();
2770
2771 if (getParser().ParseIdentifier(Name))
2772 return TokError("Expected an identifier");
2773
2774 if (Name == ".eh_frame")
2775 EH = true;
2776 else if (Name == ".debug_frame")
2777 Debug = true;
2778 }
2779
2780 getStreamer().EmitCFISections(EH, Debug);
2781
2782 return false;
2783}
2784
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002785/// ParseDirectiveCFIStartProc
2786/// ::= .cfi_startproc
2787bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2788 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002789 getStreamer().EmitCFIStartProc();
2790 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002791}
2792
2793/// ParseDirectiveCFIEndProc
2794/// ::= .cfi_endproc
2795bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002796 getStreamer().EmitCFIEndProc();
2797 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002798}
2799
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002800/// ParseRegisterOrRegisterNumber - parse register name or number.
2801bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2802 SMLoc DirectiveLoc) {
2803 unsigned RegNo;
2804
Jim Grosbach6f888a82011-06-02 17:14:04 +00002805 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002806 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2807 DirectiveLoc))
2808 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002809 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002810 } else
2811 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002812
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002813 return false;
2814}
2815
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002816/// ParseDirectiveCFIDefCfa
2817/// ::= .cfi_def_cfa register, offset
2818bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2819 SMLoc DirectiveLoc) {
2820 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002821 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002822 return true;
2823
2824 if (getLexer().isNot(AsmToken::Comma))
2825 return TokError("unexpected token in directive");
2826 Lex();
2827
2828 int64_t Offset = 0;
2829 if (getParser().ParseAbsoluteExpression(Offset))
2830 return true;
2831
Rafael Espindola066c2f42011-04-12 23:59:07 +00002832 getStreamer().EmitCFIDefCfa(Register, Offset);
2833 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002834}
2835
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002836/// ParseDirectiveCFIDefCfaOffset
2837/// ::= .cfi_def_cfa_offset offset
2838bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2839 SMLoc DirectiveLoc) {
2840 int64_t Offset = 0;
2841 if (getParser().ParseAbsoluteExpression(Offset))
2842 return true;
2843
Rafael Espindola066c2f42011-04-12 23:59:07 +00002844 getStreamer().EmitCFIDefCfaOffset(Offset);
2845 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002846}
2847
2848/// ParseDirectiveCFIAdjustCfaOffset
2849/// ::= .cfi_adjust_cfa_offset adjustment
2850bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2851 SMLoc DirectiveLoc) {
2852 int64_t Adjustment = 0;
2853 if (getParser().ParseAbsoluteExpression(Adjustment))
2854 return true;
2855
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002856 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2857 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002858}
2859
2860/// ParseDirectiveCFIDefCfaRegister
2861/// ::= .cfi_def_cfa_register register
2862bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2863 SMLoc DirectiveLoc) {
2864 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002865 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002866 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002867
Rafael Espindola066c2f42011-04-12 23:59:07 +00002868 getStreamer().EmitCFIDefCfaRegister(Register);
2869 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002870}
2871
2872/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002873/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002874bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2875 int64_t Register = 0;
2876 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002877
2878 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002879 return true;
2880
2881 if (getLexer().isNot(AsmToken::Comma))
2882 return TokError("unexpected token in directive");
2883 Lex();
2884
2885 if (getParser().ParseAbsoluteExpression(Offset))
2886 return true;
2887
Rafael Espindola066c2f42011-04-12 23:59:07 +00002888 getStreamer().EmitCFIOffset(Register, Offset);
2889 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002890}
2891
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002892/// ParseDirectiveCFIRelOffset
2893/// ::= .cfi_rel_offset register, offset
2894bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2895 SMLoc DirectiveLoc) {
2896 int64_t Register = 0;
2897
2898 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2899 return true;
2900
2901 if (getLexer().isNot(AsmToken::Comma))
2902 return TokError("unexpected token in directive");
2903 Lex();
2904
2905 int64_t Offset = 0;
2906 if (getParser().ParseAbsoluteExpression(Offset))
2907 return true;
2908
Rafael Espindola25f492e2011-04-12 16:12:03 +00002909 getStreamer().EmitCFIRelOffset(Register, Offset);
2910 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002911}
2912
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002913static bool isValidEncoding(int64_t Encoding) {
2914 if (Encoding & ~0xff)
2915 return false;
2916
2917 if (Encoding == dwarf::DW_EH_PE_omit)
2918 return true;
2919
2920 const unsigned Format = Encoding & 0xf;
2921 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2922 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2923 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2924 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2925 return false;
2926
Rafael Espindolacaf11582010-12-29 04:31:26 +00002927 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002928 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002929 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002930 return false;
2931
2932 return true;
2933}
2934
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002935/// ParseDirectiveCFIPersonalityOrLsda
2936/// ::= .cfi_personality encoding, [symbol_name]
2937/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002938bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002939 SMLoc DirectiveLoc) {
2940 int64_t Encoding = 0;
2941 if (getParser().ParseAbsoluteExpression(Encoding))
2942 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002943 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002944 return false;
2945
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002946 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002947 return TokError("unsupported encoding.");
2948
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002949 if (getLexer().isNot(AsmToken::Comma))
2950 return TokError("unexpected token in directive");
2951 Lex();
2952
2953 StringRef Name;
2954 if (getParser().ParseIdentifier(Name))
2955 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002956
2957 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2958
2959 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002960 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002961 else {
2962 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002963 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002964 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002965 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002966}
2967
Rafael Espindolafe024d02010-12-28 18:36:23 +00002968/// ParseDirectiveCFIRememberState
2969/// ::= .cfi_remember_state
2970bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2971 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002972 getStreamer().EmitCFIRememberState();
2973 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002974}
2975
2976/// ParseDirectiveCFIRestoreState
2977/// ::= .cfi_remember_state
2978bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2979 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002980 getStreamer().EmitCFIRestoreState();
2981 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002982}
2983
Rafael Espindolac5754392011-04-12 15:31:05 +00002984/// ParseDirectiveCFISameValue
2985/// ::= .cfi_same_value register
2986bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2987 SMLoc DirectiveLoc) {
2988 int64_t Register = 0;
2989
2990 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2991 return true;
2992
2993 getStreamer().EmitCFISameValue(Register);
2994
2995 return false;
2996}
2997
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00002998/// ParseDirectiveCFIRestore
2999/// ::= .cfi_restore register
3000bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003001 SMLoc DirectiveLoc) {
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003002 int64_t Register = 0;
3003 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3004 return true;
3005
3006 getStreamer().EmitCFIRestore(Register);
3007
3008 return false;
3009}
3010
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003011/// ParseDirectiveCFIEscape
3012/// ::= .cfi_escape expression[,...]
3013bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003014 SMLoc DirectiveLoc) {
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003015 std::string Values;
3016 int64_t CurrValue;
3017 if (getParser().ParseAbsoluteExpression(CurrValue))
3018 return true;
3019
3020 Values.push_back((uint8_t)CurrValue);
3021
3022 while (getLexer().is(AsmToken::Comma)) {
3023 Lex();
3024
3025 if (getParser().ParseAbsoluteExpression(CurrValue))
3026 return true;
3027
3028 Values.push_back((uint8_t)CurrValue);
3029 }
3030
3031 getStreamer().EmitCFIEscape(Values);
3032 return false;
3033}
3034
Rafael Espindola16d7d432012-01-23 21:51:52 +00003035/// ParseDirectiveCFISignalFrame
3036/// ::= .cfi_signal_frame
3037bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
3038 SMLoc DirectiveLoc) {
3039 if (getLexer().isNot(AsmToken::EndOfStatement))
3040 return Error(getLexer().getLoc(),
3041 "unexpected token in '" + Directive + "' directive");
3042
3043 getStreamer().EmitCFISignalFrame();
3044
3045 return false;
3046}
3047
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003048/// ParseDirectiveMacrosOnOff
3049/// ::= .macros_on
3050/// ::= .macros_off
3051bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
3052 SMLoc DirectiveLoc) {
3053 if (getLexer().isNot(AsmToken::EndOfStatement))
3054 return Error(getLexer().getLoc(),
3055 "unexpected token in '" + Directive + "' directive");
3056
3057 getParser().MacrosEnabled = Directive == ".macros_on";
3058
3059 return false;
3060}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003061
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003062/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00003063/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003064bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
3065 SMLoc DirectiveLoc) {
3066 StringRef Name;
3067 if (getParser().ParseIdentifier(Name))
3068 return TokError("expected identifier in directive");
3069
Rafael Espindola65366442011-06-05 02:43:45 +00003070 std::vector<StringRef> Parameters;
3071 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3072 for(;;) {
3073 StringRef Parameter;
3074 if (getParser().ParseIdentifier(Parameter))
3075 return TokError("expected identifier in directive");
3076 Parameters.push_back(Parameter);
3077
3078 if (getLexer().isNot(AsmToken::Comma))
3079 break;
3080 Lex();
3081 }
3082 }
3083
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003084 if (getLexer().isNot(AsmToken::EndOfStatement))
3085 return TokError("unexpected token in '.macro' directive");
3086
3087 // Eat the end of statement.
3088 Lex();
3089
3090 AsmToken EndToken, StartToken = getTok();
3091
3092 // Lex the macro definition.
3093 for (;;) {
3094 // Check whether we have reached the end of the file.
3095 if (getLexer().is(AsmToken::Eof))
3096 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3097
3098 // Otherwise, check whether we have reach the .endmacro.
3099 if (getLexer().is(AsmToken::Identifier) &&
3100 (getTok().getIdentifier() == ".endm" ||
3101 getTok().getIdentifier() == ".endmacro")) {
3102 EndToken = getTok();
3103 Lex();
3104 if (getLexer().isNot(AsmToken::EndOfStatement))
3105 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3106 "' directive");
3107 break;
3108 }
3109
3110 // Otherwise, scan til the end of the statement.
3111 getParser().EatToEndOfStatement();
3112 }
3113
3114 if (getParser().MacroMap.lookup(Name)) {
3115 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3116 }
3117
3118 const char *BodyStart = StartToken.getLoc().getPointer();
3119 const char *BodyEnd = EndToken.getLoc().getPointer();
3120 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00003121 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003122 return false;
3123}
3124
3125/// ParseDirectiveEndMacro
3126/// ::= .endm
3127/// ::= .endmacro
3128bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
3129 SMLoc DirectiveLoc) {
3130 if (getLexer().isNot(AsmToken::EndOfStatement))
3131 return TokError("unexpected token in '" + Directive + "' directive");
3132
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003133 // If we are inside a macro instantiation, terminate the current
3134 // instantiation.
3135 if (!getParser().ActiveMacros.empty()) {
3136 getParser().HandleMacroExit();
3137 return false;
3138 }
3139
3140 // Otherwise, this .endmacro is a stray entry in the file; well formed
3141 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003142 return TokError("unexpected '" + Directive + "' in file, "
3143 "no current macro definition");
3144}
3145
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003146/// ParseDirectivePurgeMacro
3147/// ::= .purgem
3148bool GenericAsmParser::ParseDirectivePurgeMacro(StringRef Directive,
3149 SMLoc DirectiveLoc) {
3150 StringRef Name;
3151 if (getParser().ParseIdentifier(Name))
3152 return TokError("expected identifier in '.purgem' directive");
3153
3154 if (getLexer().isNot(AsmToken::EndOfStatement))
3155 return TokError("unexpected token in '.purgem' directive");
3156
3157 StringMap<Macro*>::iterator I = getParser().MacroMap.find(Name);
3158 if (I == getParser().MacroMap.end())
3159 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3160
3161 // Undefine the macro.
3162 delete I->getValue();
3163 getParser().MacroMap.erase(I);
3164 return false;
3165}
3166
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003167bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00003168 getParser().CheckForValidSection();
3169
3170 const MCExpr *Value;
3171
3172 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003173 return true;
3174
3175 if (getLexer().isNot(AsmToken::EndOfStatement))
3176 return TokError("unexpected token in directive");
3177
3178 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00003179 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003180 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00003181 getStreamer().EmitULEB128Value(Value);
3182
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003183 return false;
3184}
3185
Rafael Espindola761cb062012-06-03 23:57:14 +00003186Macro *AsmParser::ParseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003187 AsmToken EndToken, StartToken = getTok();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003188
Rafael Espindola761cb062012-06-03 23:57:14 +00003189 unsigned NestLevel = 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003190 for (;;) {
3191 // Check whether we have reached the end of the file.
Rafael Espindola761cb062012-06-03 23:57:14 +00003192 if (getLexer().is(AsmToken::Eof)) {
3193 Error(DirectiveLoc, "no matching '.endr' in definition");
3194 return 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003195 }
3196
Rafael Espindola761cb062012-06-03 23:57:14 +00003197 if (Lexer.is(AsmToken::Identifier) &&
3198 (getTok().getIdentifier() == ".rept")) {
3199 ++NestLevel;
3200 }
3201
3202 // Otherwise, check whether we have reached the .endr.
3203 if (Lexer.is(AsmToken::Identifier) &&
3204 getTok().getIdentifier() == ".endr") {
3205 if (NestLevel == 0) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003206 EndToken = getTok();
3207 Lex();
Rafael Espindola761cb062012-06-03 23:57:14 +00003208 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3209 TokError("unexpected token in '.endr' directive");
3210 return 0;
3211 }
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003212 break;
3213 }
Rafael Espindola761cb062012-06-03 23:57:14 +00003214 --NestLevel;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003215 }
3216
Rafael Espindola761cb062012-06-03 23:57:14 +00003217 // Otherwise, scan till the end of the statement.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003218 EatToEndOfStatement();
3219 }
3220
3221 const char *BodyStart = StartToken.getLoc().getPointer();
3222 const char *BodyEnd = EndToken.getLoc().getPointer();
3223 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3224
Rafael Espindola761cb062012-06-03 23:57:14 +00003225 // We Are Anonymous.
3226 StringRef Name;
3227 std::vector<StringRef> Parameters;
3228 return new Macro(Name, Body, Parameters);
3229}
3230
3231void AsmParser::InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
3232 raw_svector_ostream &OS) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003233 OS << ".endr\n";
3234
3235 MemoryBuffer *Instantiation =
3236 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
3237
Rafael Espindola761cb062012-06-03 23:57:14 +00003238 // Create the macro instantiation object and add to the current macro
3239 // instantiation stack.
3240 MacroInstantiation *MI = new MacroInstantiation(M, DirectiveLoc,
3241 getTok().getLoc(),
3242 Instantiation);
3243 ActiveMacros.push_back(MI);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003244
Rafael Espindola761cb062012-06-03 23:57:14 +00003245 // Jump to the macro instantiation and prime the lexer.
3246 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3247 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3248 Lex();
3249}
3250
3251bool AsmParser::ParseDirectiveRept(SMLoc DirectiveLoc) {
3252 int64_t Count;
3253 if (ParseAbsoluteExpression(Count))
3254 return TokError("unexpected token in '.rept' directive");
3255
3256 if (Count < 0)
3257 return TokError("Count is negative");
3258
3259 if (Lexer.isNot(AsmToken::EndOfStatement))
3260 return TokError("unexpected token in '.rept' directive");
3261
3262 // Eat the end of statement.
3263 Lex();
3264
3265 // Lex the rept definition.
3266 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3267 if (!M)
3268 return true;
3269
3270 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3271 // to hold the macro body with substitutions.
3272 SmallString<256> Buf;
3273 std::vector<StringRef> Parameters;
3274 const std::vector<MacroArgument> A;
3275 raw_svector_ostream OS(Buf);
3276 while (Count--) {
3277 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3278 return true;
3279 }
3280 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003281
3282 return false;
3283}
3284
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003285/// ParseDirectiveIrp
3286/// ::= .irp symbol,values
3287bool AsmParser::ParseDirectiveIrp(SMLoc DirectiveLoc) {
3288 std::vector<StringRef> Parameters;
3289 StringRef Parameter;
3290
3291 if (ParseIdentifier(Parameter))
3292 return TokError("expected identifier in '.irp' directive");
3293
3294 Parameters.push_back(Parameter);
3295
3296 if (Lexer.isNot(AsmToken::Comma))
3297 return TokError("expected comma in '.irp' directive");
3298
3299 Lex();
3300
3301 std::vector<MacroArgument> A;
3302 if (ParseMacroArguments(0, A))
3303 return true;
3304
3305 // Eat the end of statement.
3306 Lex();
3307
3308 // Lex the irp definition.
3309 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3310 if (!M)
3311 return true;
3312
3313 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3314 // to hold the macro body with substitutions.
3315 SmallString<256> Buf;
3316 raw_svector_ostream OS(Buf);
3317
3318 for (std::vector<MacroArgument>::iterator i = A.begin(), e = A.end(); i != e;
3319 ++i) {
3320 std::vector<MacroArgument> Args;
3321 Args.push_back(*i);
3322
3323 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3324 return true;
3325 }
3326
3327 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3328
3329 return false;
3330}
3331
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003332/// ParseDirectiveIrpc
3333/// ::= .irpc symbol,values
3334bool AsmParser::ParseDirectiveIrpc(SMLoc DirectiveLoc) {
3335 std::vector<StringRef> Parameters;
3336 StringRef Parameter;
3337
3338 if (ParseIdentifier(Parameter))
3339 return TokError("expected identifier in '.irpc' directive");
3340
3341 Parameters.push_back(Parameter);
3342
3343 if (Lexer.isNot(AsmToken::Comma))
3344 return TokError("expected comma in '.irpc' directive");
3345
3346 Lex();
3347
3348 std::vector<MacroArgument> A;
3349 if (ParseMacroArguments(0, A))
3350 return true;
3351
3352 if (A.size() != 1 || A.front().size() != 1)
3353 return TokError("unexpected token in '.irpc' directive");
3354
3355 // Eat the end of statement.
3356 Lex();
3357
3358 // Lex the irpc definition.
3359 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3360 if (!M)
3361 return true;
3362
3363 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3364 // to hold the macro body with substitutions.
3365 SmallString<256> Buf;
3366 raw_svector_ostream OS(Buf);
3367
3368 StringRef Values = A.front().front().getString();
3369 std::size_t I, End = Values.size();
3370 for (I = 0; I < End; ++I) {
3371 MacroArgument Arg;
3372 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I+1)));
3373
3374 std::vector<MacroArgument> Args;
3375 Args.push_back(Arg);
3376
3377 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3378 return true;
3379 }
3380
3381 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3382
3383 return false;
3384}
3385
Rafael Espindola761cb062012-06-03 23:57:14 +00003386bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) {
3387 if (ActiveMacros.empty())
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003388 return TokError("unexpected '.endr' directive, no current .rept");
3389
3390 // The only .repl that should get here are the ones created by
Rafael Espindola761cb062012-06-03 23:57:14 +00003391 // InstantiateMacroLikeBody.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003392 assert(getLexer().is(AsmToken::EndOfStatement));
3393
Rafael Espindola761cb062012-06-03 23:57:14 +00003394 HandleMacroExit();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003395 return false;
3396}
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003397
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003398/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003399MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003400 MCContext &C, MCStreamer &Out,
3401 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003402 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003403}