blob: 69f71304bbe6229a4ba1aeba9d0e0cde134285bd [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 Dunbar7c0a3342009-08-26 22:49:51 +000014#include "llvm/ADT/SmallString.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000015#include "llvm/ADT/StringMap.h"
Matt Fleming924c5e52010-05-21 11:36:59 +000016#include "llvm/ADT/StringSwitch.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"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000020#include "llvm/MC/MCExpr.h"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000021#include "llvm/MC/MCInst.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"
26#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000027#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000028#include "llvm/MC/MCSymbol.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000029#include "llvm/Support/Compiler.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000030#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000031#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000032#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000033#include "llvm/Target/TargetAsmParser.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000034#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000035using namespace llvm;
36
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000037namespace {
38
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000039/// \brief Helper class for tracking macro definitions.
40struct Macro {
41 StringRef Name;
42 StringRef Body;
43
44public:
45 Macro(StringRef N, StringRef B) : Name(N), Body(B) {}
46};
47
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000048/// \brief Helper class for storing information about an active macro
49/// instantiation.
50struct MacroInstantiation {
51 /// The macro being instantiated.
52 const Macro *TheMacro;
53
54 /// The macro instantiation with substitutions.
55 MemoryBuffer *Instantiation;
56
57 /// The location of the instantiation.
58 SMLoc InstantiationLoc;
59
60 /// The location where parsing should resume upon instantiation completion.
61 SMLoc ExitLoc;
62
63public:
64 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL);
65};
66
Daniel Dunbaraef87e32010-07-18 18:31:38 +000067/// \brief The concrete assembly parser instance.
68class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000069 friend class GenericAsmParser;
70
Daniel Dunbaraef87e32010-07-18 18:31:38 +000071 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
72 void operator=(const AsmParser &); // DO NOT IMPLEMENT
73private:
74 AsmLexer Lexer;
75 MCContext &Ctx;
76 MCStreamer &Out;
77 SourceMgr &SrcMgr;
78 MCAsmParserExtension *GenericParser;
79 MCAsmParserExtension *PlatformParser;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000080
Daniel Dunbaraef87e32010-07-18 18:31:38 +000081 /// This is the current buffer index we're lexing from as managed by the
82 /// SourceMgr object.
83 int CurBuffer;
84
85 AsmCond TheCondState;
86 std::vector<AsmCond> TheCondStack;
87
88 /// DirectiveMap - This is a table handlers for directives. Each handler is
89 /// invoked after the directive identifier is read and is responsible for
90 /// parsing and validating the rest of the directive. The handler is passed
91 /// in the directive name and the location of the directive keyword.
92 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000093
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000094 /// MacroMap - Map of currently defined macros.
95 StringMap<Macro*> MacroMap;
96
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000097 /// ActiveMacros - Stack of active macro instantiations.
98 std::vector<MacroInstantiation*> ActiveMacros;
99
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000100 /// Boolean tracking whether macro substitution is enabled.
101 unsigned MacrosEnabled : 1;
102
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000103public:
104 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
105 const MCAsmInfo &MAI);
106 ~AsmParser();
107
108 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
109
110 void AddDirectiveHandler(MCAsmParserExtension *Object,
111 StringRef Directive,
112 DirectiveHandler Handler) {
113 DirectiveMap[Directive] = std::make_pair(Object, Handler);
114 }
115
116public:
117 /// @name MCAsmParser Interface
118 /// {
119
120 virtual SourceMgr &getSourceManager() { return SrcMgr; }
121 virtual MCAsmLexer &getLexer() { return Lexer; }
122 virtual MCContext &getContext() { return Ctx; }
123 virtual MCStreamer &getStreamer() { return Out; }
124
125 virtual void Warning(SMLoc L, const Twine &Meg);
126 virtual bool Error(SMLoc L, const Twine &Msg);
127
128 const AsmToken &Lex();
129
130 bool ParseExpression(const MCExpr *&Res);
131 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
132 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
133 virtual bool ParseAbsoluteExpression(int64_t &Res);
134
135 /// }
136
137private:
138 bool ParseStatement();
139
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000140 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
141 void HandleMacroExit();
142
143 void PrintMacroInstantiations();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000144 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
145
146 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
147 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000148
149 /// \brief Reset the current lexer position to that given by \arg Loc. The
150 /// current token is not set; clients should ensure Lex() is called
151 /// subsequently.
152 void JumpToLoc(SMLoc Loc);
153
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000154 void EatToEndOfStatement();
155
156 bool ParseAssignment(StringRef Name);
157
158 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
159 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
160 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
161
162 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
163 /// and set \arg Res to the identifier contents.
164 bool ParseIdentifier(StringRef &Res);
165
166 // Directive Parsing.
167 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
168 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
169 bool ParseDirectiveFill(); // ".fill"
170 bool ParseDirectiveSpace(); // ".space"
171 bool ParseDirectiveSet(); // ".set"
172 bool ParseDirectiveOrg(); // ".org"
173 // ".align{,32}", ".p2align{,w,l}"
174 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
175
176 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
177 /// accepts a single symbol (which should be a label or an external).
178 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
179 bool ParseDirectiveELFType(); // ELF specific ".type"
180
181 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
182
183 bool ParseDirectiveAbort(); // ".abort"
184 bool ParseDirectiveInclude(); // ".include"
185
186 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
187 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
188 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
189 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
190
191 /// ParseEscapedString - Parse the current token as a string which may include
192 /// escaped characters and return the string contents.
193 bool ParseEscapedString(std::string &Data);
194};
195
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000196/// \brief Generic implementations of directive handling, etc. which is shared
197/// (or the default, at least) for all assembler parser.
198class GenericAsmParser : public MCAsmParserExtension {
199public:
200 GenericAsmParser() {}
201
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000202 AsmParser &getParser() {
203 return (AsmParser&) this->MCAsmParserExtension::getParser();
204 }
205
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000206 virtual void Initialize(MCAsmParser &Parser) {
207 // Call the base implementation.
208 this->MCAsmParserExtension::Initialize(Parser);
209
210 // Debugging directives.
211 Parser.AddDirectiveHandler(this, ".file", MCAsmParser::DirectiveHandler(
212 &GenericAsmParser::ParseDirectiveFile));
213 Parser.AddDirectiveHandler(this, ".line", MCAsmParser::DirectiveHandler(
214 &GenericAsmParser::ParseDirectiveLine));
215 Parser.AddDirectiveHandler(this, ".loc", MCAsmParser::DirectiveHandler(
216 &GenericAsmParser::ParseDirectiveLoc));
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000217
218 // Macro directives.
219 Parser.AddDirectiveHandler(this, ".macros_on",
220 MCAsmParser::DirectiveHandler(
221 &GenericAsmParser::ParseDirectiveMacrosOnOff));
222 Parser.AddDirectiveHandler(this, ".macros_off",
223 MCAsmParser::DirectiveHandler(
224 &GenericAsmParser::ParseDirectiveMacrosOnOff));
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000225 Parser.AddDirectiveHandler(this, ".macro", MCAsmParser::DirectiveHandler(
226 &GenericAsmParser::ParseDirectiveMacro));
227 Parser.AddDirectiveHandler(this, ".endm", MCAsmParser::DirectiveHandler(
228 &GenericAsmParser::ParseDirectiveEndMacro));
229 Parser.AddDirectiveHandler(this, ".endmacro", MCAsmParser::DirectiveHandler(
230 &GenericAsmParser::ParseDirectiveEndMacro));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000231 }
232
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000233 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
234 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
235 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000236
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000237 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000238 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
239 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000240};
241
242}
243
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000244namespace llvm {
245
246extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000247extern MCAsmParserExtension *createELFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000248
249}
250
Chris Lattneraaec2052010-01-19 19:46:13 +0000251enum { DEFAULT_ADDRSPACE = 0 };
252
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000253AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
254 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000255 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000256 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000257 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000258 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000259
260 // Initialize the generic parser.
261 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000262
263 // Initialize the platform / file format parser.
264 //
265 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
266 // created.
267 if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000268 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000269 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000270 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000271 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000272 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000273 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000274}
275
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000276AsmParser::~AsmParser() {
Daniel Dunbare4749702010-07-12 18:12:02 +0000277 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000278 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000279}
280
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000281void AsmParser::PrintMacroInstantiations() {
282 // Print the active macro instantiation stack.
283 for (std::vector<MacroInstantiation*>::const_reverse_iterator
284 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
285 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
286 "note");
287}
288
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000289void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000290 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000291 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000292}
293
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000294bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000295 PrintMessage(L, Msg.str(), "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000296 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000297 return true;
298}
299
Sean Callananbf2013e2010-01-20 23:19:55 +0000300void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
301 const char *Type) const {
302 SrcMgr.PrintMessage(Loc, Msg, Type);
303}
Sean Callananfd0b0282010-01-21 00:19:58 +0000304
305bool AsmParser::EnterIncludeFile(const std::string &Filename) {
306 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
307 if (NewBuf == -1)
308 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000309
Sean Callananfd0b0282010-01-21 00:19:58 +0000310 CurBuffer = NewBuf;
311
312 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
313
314 return false;
315}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000316
317void AsmParser::JumpToLoc(SMLoc Loc) {
318 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
319 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
320}
321
Sean Callananfd0b0282010-01-21 00:19:58 +0000322const AsmToken &AsmParser::Lex() {
323 const AsmToken *tok = &Lexer.Lex();
324
325 if (tok->is(AsmToken::Eof)) {
326 // If this is the end of an included file, pop the parent file off the
327 // include stack.
328 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
329 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000330 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000331 tok = &Lexer.Lex();
332 }
333 }
334
335 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000336 Error(Lexer.getErrLoc(), Lexer.getErr());
Sean Callanan79036e42010-01-20 22:18:24 +0000337
Sean Callananfd0b0282010-01-21 00:19:58 +0000338 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000339}
340
Chris Lattner79180e22010-04-05 23:15:42 +0000341bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000342 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000343 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000344 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000345 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000346 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000347 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
348 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000349
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000350 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000351 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000352
Chris Lattnerb717fb02009-07-02 21:53:43 +0000353 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000354
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000355 AsmCond StartingCondState = TheCondState;
356
Chris Lattnerb717fb02009-07-02 21:53:43 +0000357 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000358 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000359 if (!ParseStatement()) continue;
360
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000361 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000362 HadError = true;
363 EatToEndOfStatement();
364 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000365
366 if (TheCondState.TheCond != StartingCondState.TheCond ||
367 TheCondState.Ignore != StartingCondState.Ignore)
368 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000369
Chris Lattner79180e22010-04-05 23:15:42 +0000370 // Finalize the output stream if there are no errors and if the client wants
371 // us to.
372 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000373 Out.Finish();
374
Chris Lattnerb717fb02009-07-02 21:53:43 +0000375 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000376}
377
Chris Lattner2cf5f142009-06-22 01:29:09 +0000378/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
379void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000380 while (Lexer.isNot(AsmToken::EndOfStatement) &&
381 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000382 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000383
384 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000385 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000386 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000387}
388
Chris Lattnerc4193832009-06-22 05:51:26 +0000389
Chris Lattner74ec1a32009-06-22 06:32:03 +0000390/// ParseParenExpr - Parse a paren expression and return it.
391/// NOTE: This assumes the leading '(' has already been consumed.
392///
393/// parenexpr ::= expr)
394///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000395bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000396 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000397 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000398 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000399 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000400 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000401 return false;
402}
Chris Lattnerc4193832009-06-22 05:51:26 +0000403
Chris Lattner74ec1a32009-06-22 06:32:03 +0000404/// ParsePrimaryExpr - Parse a primary expression and return it.
405/// primaryexpr ::= (parenexpr
406/// primaryexpr ::= symbol
407/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000408/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000409/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000410bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000411 switch (Lexer.getKind()) {
412 default:
413 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000414 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000415 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000416 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000417 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000418 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000419 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000420 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000421 case AsmToken::Identifier: {
422 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000423 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000424 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000425
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000426 // Mark the symbol as used in an expression.
427 Sym->setUsedInExpr(true);
428
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000429 // Lookup the symbol variant if used.
430 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
431 if (Split.first.size() != getTok().getIdentifier().size())
432 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
433
Chris Lattnerb4307b32010-01-15 19:28:38 +0000434 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000435 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000436
437 // If this is an absolute variable reference, substitute it now to preserve
438 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000439 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000440 if (Variant)
441 return Error(EndLoc, "unexpected modified on variable reference");
442
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000443 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000444 return false;
445 }
446
447 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000448 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000449 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000450 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000451 case AsmToken::Integer: {
452 SMLoc Loc = getTok().getLoc();
453 int64_t IntVal = getTok().getIntVal();
454 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000455 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000456 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000457 // Look for 'b' or 'f' following an Integer as a directional label
458 if (Lexer.getKind() == AsmToken::Identifier) {
459 StringRef IDVal = getTok().getString();
460 if (IDVal == "f" || IDVal == "b"){
461 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
462 IDVal == "f" ? 1 : 0);
463 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
464 getContext());
465 if(IDVal == "b" && Sym->isUndefined())
466 return Error(Loc, "invalid reference to undefined symbol");
467 EndLoc = Lexer.getLoc();
468 Lex(); // Eat identifier.
469 }
470 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000471 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000472 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000473 case AsmToken::Dot: {
474 // This is a '.' reference, which references the current PC. Emit a
475 // temporary label to the streamer and refer to it.
476 MCSymbol *Sym = Ctx.CreateTempSymbol();
477 Out.EmitLabel(Sym);
478 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
479 EndLoc = Lexer.getLoc();
480 Lex(); // Eat identifier.
481 return false;
482 }
483
Daniel Dunbar3f872332009-07-28 16:08:33 +0000484 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000485 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000486 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000487 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000488 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000489 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000490 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000491 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000492 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000493 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000494 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000495 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000496 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000497 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000498 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000499 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000500 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000501 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000502 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000503 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000504 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000505 }
506}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000507
Chris Lattnerb4307b32010-01-15 19:28:38 +0000508bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000509 SMLoc EndLoc;
510 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000511}
512
Chris Lattner74ec1a32009-06-22 06:32:03 +0000513/// ParseExpression - Parse an expression and return it.
514///
515/// expr ::= expr +,- expr -> lowest.
516/// expr ::= expr |,^,&,! expr -> middle.
517/// expr ::= expr *,/,%,<<,>> expr -> highest.
518/// expr ::= primaryexpr
519///
Chris Lattner54482b42010-01-15 19:39:23 +0000520bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000521 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000522 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000523 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
524 return true;
525
526 // Try to constant fold it up front, if possible.
527 int64_t Value;
528 if (Res->EvaluateAsAbsolute(Value))
529 Res = MCConstantExpr::Create(Value, getContext());
530
531 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000532}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000533
Chris Lattnerb4307b32010-01-15 19:28:38 +0000534bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000535 Res = 0;
536 return ParseParenExpr(Res, EndLoc) ||
537 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000538}
539
Daniel Dunbar475839e2009-06-29 20:37:27 +0000540bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000541 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000542
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000543 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000544 if (ParseExpression(Expr))
545 return true;
546
Daniel Dunbare00b0112009-10-16 01:57:52 +0000547 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000548 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000549
550 return false;
551}
552
Daniel Dunbar3f872332009-07-28 16:08:33 +0000553static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000554 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000555 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000556 default:
557 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000558
559 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000560 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000561 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000562 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000563 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000564 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000565 return 1;
566
567 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000568 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000569 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000570 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000571 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000572 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000573 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000574 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000575 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000576 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000577 case AsmToken::ExclaimEqual:
578 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000579 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000580 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000581 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000582 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000583 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000584 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000585 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000586 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000587 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000588 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000589 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000590 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000591 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000592 return 2;
593
594 // Intermediate Precedence: |, &, ^
595 //
596 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000597 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000598 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000599 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000600 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000601 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000602 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000603 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000604 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000605 return 3;
606
607 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000608 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000609 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000610 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000611 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000612 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000613 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000614 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000615 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000616 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000617 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000618 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000619 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000620 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000621 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000622 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000623 }
624}
625
626
627/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
628/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000629bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
630 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000631 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000632 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000633 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000634
635 // If the next token is lower precedence than we are allowed to eat, return
636 // successfully with what we ate already.
637 if (TokPrec < Precedence)
638 return false;
639
Sean Callanan79ed1a82010-01-19 20:22:31 +0000640 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000641
642 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000643 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000644 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000645
646 // If BinOp binds less tightly with RHS than the operator after RHS, let
647 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000648 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000649 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000650 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000651 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000652 }
653
Daniel Dunbar475839e2009-06-29 20:37:27 +0000654 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000655 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000656 }
657}
658
Chris Lattnerc4193832009-06-22 05:51:26 +0000659
660
661
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000662/// ParseStatement:
663/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000664/// ::= Label* Directive ...Operands... EndOfStatement
665/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000666bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000667 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000668 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000669 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000670 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000671 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000672
673 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000674 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000675 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000676 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000677 int64_t LocalLabelVal = -1;
678 // GUESS allow an integer followed by a ':' as a directional local label
679 if (Lexer.is(AsmToken::Integer)) {
680 LocalLabelVal = getTok().getIntVal();
681 if (LocalLabelVal < 0) {
682 if (!TheCondState.Ignore)
683 return TokError("unexpected token at start of statement");
684 IDVal = "";
685 }
686 else {
687 IDVal = getTok().getString();
688 Lex(); // Consume the integer token to be used as an identifier token.
689 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000690 if (!TheCondState.Ignore)
691 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000692 }
693 }
694 }
695 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000696 if (!TheCondState.Ignore)
697 return TokError("unexpected token at start of statement");
698 IDVal = "";
699 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000700
Chris Lattner7834fac2010-04-17 18:14:27 +0000701 // Handle conditional assembly here before checking for skipping. We
702 // have to do this so that .endif isn't skipped in a ".if 0" block for
703 // example.
704 if (IDVal == ".if")
705 return ParseDirectiveIf(IDLoc);
706 if (IDVal == ".elseif")
707 return ParseDirectiveElseIf(IDLoc);
708 if (IDVal == ".else")
709 return ParseDirectiveElse(IDLoc);
710 if (IDVal == ".endif")
711 return ParseDirectiveEndIf(IDLoc);
712
713 // If we are in a ".if 0" block, ignore this statement.
714 if (TheCondState.Ignore) {
715 EatToEndOfStatement();
716 return false;
717 }
718
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000719 // FIXME: Recurse on local labels?
720
721 // See what kind of statement we have.
722 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000723 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000724 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000725 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000726
727 // Diagnose attempt to use a variable as a label.
728 //
729 // FIXME: Diagnostics. Note the location of the definition as a label.
730 // FIXME: This doesn't diagnose assignment to a symbol which has been
731 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000732 MCSymbol *Sym;
733 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000734 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000735 else
736 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000737 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000738 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000739
Daniel Dunbar959fd882009-08-26 22:13:22 +0000740 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000741 Out.EmitLabel(Sym);
742
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000743 // Consume any end of statement token, if present, to avoid spurious
744 // AddBlankLine calls().
745 if (Lexer.is(AsmToken::EndOfStatement)) {
746 Lex();
747 if (Lexer.is(AsmToken::Eof))
748 return false;
749 }
750
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000751 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000752 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000753
Daniel Dunbar3f872332009-07-28 16:08:33 +0000754 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000755 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000756 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000757
Daniel Dunbare2ace502009-08-31 08:09:09 +0000758 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000759
760 default: // Normal instruction or directive.
761 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000762 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000763
764 // If macros are enabled, check to see if this is a macro instantiation.
765 if (MacrosEnabled)
766 if (const Macro *M = MacroMap.lookup(IDVal))
767 return HandleMacroEntry(IDVal, IDLoc, M);
768
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000769 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000770 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000771 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000772 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000773 return ParseDirectiveSet();
774
Daniel Dunbara0d14262009-06-24 23:30:00 +0000775 // Data directives
776
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000777 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000778 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000779 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000780 return ParseDirectiveAscii(true);
781
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000782 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000783 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000784 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000785 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000786 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000787 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000788 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000789 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000790
791 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000792 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000793 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000794 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000795 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000796 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000797 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000798 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000799 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000800 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000801 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000802 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000803 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000804 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000805 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000806 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000807 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
808
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000809 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000810 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000811
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000812 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000813 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000814 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000815 return ParseDirectiveSpace();
816
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000817 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000818
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000819 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000820 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000821 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000822 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000823 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000824 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000825 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000826 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000827 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000828 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000829 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000830 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000831 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000832 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000833 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000834 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000835 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000836 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000837 if (IDVal == ".type")
838 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000839 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000840 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000841 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000842 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000843 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000844 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000845 if (IDVal == ".weak_def_can_be_hidden")
846 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000847
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000848 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000849 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000850 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000851 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000852
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000853 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000854 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000855 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000856 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000857
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000858 // Look up the handler in the handler table.
859 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
860 DirectiveMap.lookup(IDVal);
861 if (Handler.first)
862 return (Handler.first->*Handler.second)(IDVal, IDLoc);
863
Kevin Enderby9c656452009-09-10 20:51:44 +0000864 // Target hook for parsing target specific directives.
865 if (!getTargetParser().ParseDirective(ID))
866 return false;
867
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000868 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000869 EatToEndOfStatement();
870 return false;
871 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000872
Chris Lattnera7f13542010-05-19 23:34:33 +0000873 // Canonicalize the opcode to lower case.
874 SmallString<128> Opcode;
875 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
876 Opcode.push_back(tolower(IDVal[i]));
877
Chris Lattner98986712010-01-14 22:21:20 +0000878 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000879 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000880 ParsedOperands);
881 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
882 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000883
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000884 // If parsing succeeded, match the instruction.
885 if (!HadError) {
886 MCInst Inst;
887 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
888 // Emit the instruction on success.
889 Out.EmitInstruction(Inst);
890 } else {
891 // Otherwise emit a diagnostic about the match failure and set the error
892 // flag.
893 //
894 // FIXME: We should give nicer diagnostics about the exact failure.
895 Error(IDLoc, "unrecognized instruction");
896 HadError = true;
897 }
898 }
Chris Lattner98986712010-01-14 22:21:20 +0000899
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000900 // If there was no error, consume the end-of-statement token. Otherwise this
901 // will be done by our caller.
902 if (!HadError)
903 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000904
905 // Free any parsed operands.
906 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
907 delete ParsedOperands[i];
908
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000909 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000910}
Chris Lattner9a023f72009-06-24 04:43:34 +0000911
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000912MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL)
913 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
914{
915 // Macro instantiation is lexical, unfortunately. We construct a new buffer
916 // to hold the macro body with substitutions.
917 llvm::SmallString<256> Buf;
918 Buf += M->Body;
919
920 // We include the .endmacro in the buffer as our queue to exit the macro
921 // instantiation.
922 Buf += ".endmacro\n";
923
924 Instantiation = MemoryBuffer::getMemBufferCopy(Buf, "<instantiation>");
925}
926
927bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
928 const Macro *M) {
929 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
930 // this, although we should protect against infinite loops.
931 if (ActiveMacros.size() == 20)
932 return TokError("macros cannot be nested more than 20 levels deep");
933
934 EatToEndOfStatement();
935
936 // Create the macro instantiation object and add to the current macro
937 // instantiation stack.
938 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
939 getTok().getLoc());
940 ActiveMacros.push_back(MI);
941
942 // Jump to the macro instantiation and prime the lexer.
943 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
944 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
945 Lex();
946
947 return false;
948}
949
950void AsmParser::HandleMacroExit() {
951 // Jump to the EndOfStatement we should return to, and consume it.
952 JumpToLoc(ActiveMacros.back()->ExitLoc);
953 Lex();
954
955 // Pop the instantiation entry.
956 delete ActiveMacros.back();
957 ActiveMacros.pop_back();
958}
959
Benjamin Kramer38e59892010-07-14 22:38:02 +0000960bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000961 // FIXME: Use better location, we should use proper tokens.
962 SMLoc EqualLoc = Lexer.getLoc();
963
Daniel Dunbar821e3332009-08-31 08:09:28 +0000964 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +0000965 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000966 return true;
967
Daniel Dunbar3f872332009-07-28 16:08:33 +0000968 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000969 return TokError("unexpected token in assignment");
970
971 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000972 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000973
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000974 // Validate that the LHS is allowed to be a variable (either it has not been
975 // used as a symbol, or it is an absolute symbol).
976 MCSymbol *Sym = getContext().LookupSymbol(Name);
977 if (Sym) {
978 // Diagnose assignment to a label.
979 //
980 // FIXME: Diagnostics. Note the location of the definition as a label.
981 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000982 if (Sym->isUndefined() && !Sym->isUsedInExpr())
983 ; // Allow redefinitions of undefined symbols only used in directives.
984 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000985 return Error(EqualLoc, "redefinition of '" + Name + "'");
986 else if (!Sym->isVariable())
987 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000988 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000989 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
990 Name + "'");
991 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000992 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000993
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000994 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000995
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000996 Sym->setUsedInExpr(true);
997
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000998 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000999 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001000
1001 return false;
1002}
1003
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001004/// ParseIdentifier:
1005/// ::= identifier
1006/// ::= string
1007bool AsmParser::ParseIdentifier(StringRef &Res) {
1008 if (Lexer.isNot(AsmToken::Identifier) &&
1009 Lexer.isNot(AsmToken::String))
1010 return true;
1011
Sean Callanan18b83232010-01-19 21:44:56 +00001012 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001013
Sean Callanan79ed1a82010-01-19 20:22:31 +00001014 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001015
1016 return false;
1017}
1018
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001019/// ParseDirectiveSet:
1020/// ::= .set identifier ',' expression
1021bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001022 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001023
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001024 if (ParseIdentifier(Name))
1025 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001026
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001027 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001028 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001029 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001030
Daniel Dunbare2ace502009-08-31 08:09:09 +00001031 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001032}
1033
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001034bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001035 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001036
1037 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001038 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001039 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1040 if (Str[i] != '\\') {
1041 Data += Str[i];
1042 continue;
1043 }
1044
1045 // Recognize escaped characters. Note that this escape semantics currently
1046 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1047 ++i;
1048 if (i == e)
1049 return TokError("unexpected backslash at end of string");
1050
1051 // Recognize octal sequences.
1052 if ((unsigned) (Str[i] - '0') <= 7) {
1053 // Consume up to three octal characters.
1054 unsigned Value = Str[i] - '0';
1055
1056 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1057 ++i;
1058 Value = Value * 8 + (Str[i] - '0');
1059
1060 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1061 ++i;
1062 Value = Value * 8 + (Str[i] - '0');
1063 }
1064 }
1065
1066 if (Value > 255)
1067 return TokError("invalid octal escape sequence (out of range)");
1068
1069 Data += (unsigned char) Value;
1070 continue;
1071 }
1072
1073 // Otherwise recognize individual escapes.
1074 switch (Str[i]) {
1075 default:
1076 // Just reject invalid escape sequences for now.
1077 return TokError("invalid escape sequence (unrecognized character)");
1078
1079 case 'b': Data += '\b'; break;
1080 case 'f': Data += '\f'; break;
1081 case 'n': Data += '\n'; break;
1082 case 'r': Data += '\r'; break;
1083 case 't': Data += '\t'; break;
1084 case '"': Data += '"'; break;
1085 case '\\': Data += '\\'; break;
1086 }
1087 }
1088
1089 return false;
1090}
1091
Daniel Dunbara0d14262009-06-24 23:30:00 +00001092/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001093/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001094bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001095 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001096 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001097 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001098 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001099
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001100 std::string Data;
1101 if (ParseEscapedString(Data))
1102 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001103
1104 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001105 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001106 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1107
Sean Callanan79ed1a82010-01-19 20:22:31 +00001108 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001109
1110 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001111 break;
1112
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001113 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001114 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001115 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001116 }
1117 }
1118
Sean Callanan79ed1a82010-01-19 20:22:31 +00001119 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001120 return false;
1121}
1122
1123/// ParseDirectiveValue
1124/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1125bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001126 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001127 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001128 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001129 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001130 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001131 return true;
1132
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001133 // Special case constant expressions to match code generator.
1134 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001135 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001136 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001137 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001138
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001139 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001140 break;
1141
1142 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001143 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001144 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001145 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001146 }
1147 }
1148
Sean Callanan79ed1a82010-01-19 20:22:31 +00001149 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001150 return false;
1151}
1152
1153/// ParseDirectiveSpace
1154/// ::= .space expression [ , expression ]
1155bool AsmParser::ParseDirectiveSpace() {
1156 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001157 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001158 return true;
1159
1160 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001161 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1162 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001163 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001164 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001165
Daniel Dunbar475839e2009-06-29 20:37:27 +00001166 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001167 return true;
1168
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001169 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001170 return TokError("unexpected token in '.space' directive");
1171 }
1172
Sean Callanan79ed1a82010-01-19 20:22:31 +00001173 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001174
1175 if (NumBytes <= 0)
1176 return TokError("invalid number of bytes in '.space' directive");
1177
1178 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001179 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001180
1181 return false;
1182}
1183
1184/// ParseDirectiveFill
1185/// ::= .fill expression , expression , expression
1186bool AsmParser::ParseDirectiveFill() {
1187 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001188 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001189 return true;
1190
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001191 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001192 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001193 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001194
1195 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001196 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001197 return true;
1198
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001199 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001200 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001201 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001202
1203 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001204 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001205 return true;
1206
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001207 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001208 return TokError("unexpected token in '.fill' directive");
1209
Sean Callanan79ed1a82010-01-19 20:22:31 +00001210 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001211
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001212 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1213 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001214
1215 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001216 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001217
1218 return false;
1219}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001220
1221/// ParseDirectiveOrg
1222/// ::= .org expression [ , expression ]
1223bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001224 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001225 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001226 return true;
1227
1228 // Parse optional fill expression.
1229 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001230 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1231 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001232 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001233 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001234
Daniel Dunbar475839e2009-06-29 20:37:27 +00001235 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001236 return true;
1237
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001238 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001239 return TokError("unexpected token in '.org' directive");
1240 }
1241
Sean Callanan79ed1a82010-01-19 20:22:31 +00001242 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001243
1244 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1245 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001246 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001247
1248 return false;
1249}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001250
1251/// ParseDirectiveAlign
1252/// ::= {.align, ...} expression [ , expression [ , expression ]]
1253bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001254 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001255 int64_t Alignment;
1256 if (ParseAbsoluteExpression(Alignment))
1257 return true;
1258
1259 SMLoc MaxBytesLoc;
1260 bool HasFillExpr = false;
1261 int64_t FillExpr = 0;
1262 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001263 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1264 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001265 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001266 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001267
1268 // The fill expression can be omitted while specifying a maximum number of
1269 // alignment bytes, e.g:
1270 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001271 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001272 HasFillExpr = true;
1273 if (ParseAbsoluteExpression(FillExpr))
1274 return true;
1275 }
1276
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001277 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1278 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001279 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001280 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001281
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001282 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001283 if (ParseAbsoluteExpression(MaxBytesToFill))
1284 return true;
1285
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001286 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001287 return TokError("unexpected token in directive");
1288 }
1289 }
1290
Sean Callanan79ed1a82010-01-19 20:22:31 +00001291 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001292
Daniel Dunbar648ac512010-05-17 21:54:30 +00001293 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001294 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001295
1296 // Compute alignment in bytes.
1297 if (IsPow2) {
1298 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001299 if (Alignment >= 32) {
1300 Error(AlignmentLoc, "invalid alignment value");
1301 Alignment = 31;
1302 }
1303
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001304 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001305 }
1306
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001307 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001308 if (MaxBytesLoc.isValid()) {
1309 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001310 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1311 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001312 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001313 }
1314
1315 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001316 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1317 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001318 MaxBytesToFill = 0;
1319 }
1320 }
1321
Daniel Dunbar648ac512010-05-17 21:54:30 +00001322 // Check whether we should use optimal code alignment for this .align
1323 // directive.
1324 //
1325 // FIXME: This should be using a target hook.
1326 bool UseCodeAlign = false;
1327 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001328 getStreamer().getCurrentSection()))
Chris Lattnera9558532010-07-15 21:19:31 +00001329 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001330 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1331 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001332 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001333 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001334 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001335 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1336 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001337 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001338
1339 return false;
1340}
1341
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001342/// ParseDirectiveSymbolAttribute
1343/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001344bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001345 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001346 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001347 StringRef Name;
1348
1349 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001350 return TokError("expected identifier in directive");
1351
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001352 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001353
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001354 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001355
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001356 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001357 break;
1358
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001359 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001360 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001361 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001362 }
1363 }
1364
Sean Callanan79ed1a82010-01-19 20:22:31 +00001365 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001366 return false;
1367}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001368
Matt Fleming924c5e52010-05-21 11:36:59 +00001369/// ParseDirectiveELFType
1370/// ::= .type identifier , @attribute
1371bool AsmParser::ParseDirectiveELFType() {
1372 StringRef Name;
1373 if (ParseIdentifier(Name))
1374 return TokError("expected identifier in directive");
1375
1376 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001377 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Matt Fleming924c5e52010-05-21 11:36:59 +00001378
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001379 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001380 return TokError("unexpected token in '.type' directive");
1381 Lex();
1382
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001383 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001384 return TokError("expected '@' before type");
1385 Lex();
1386
1387 StringRef Type;
1388 SMLoc TypeLoc;
1389
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001390 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001391 if (ParseIdentifier(Type))
1392 return TokError("expected symbol type in directive");
1393
1394 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1395 .Case("function", MCSA_ELF_TypeFunction)
1396 .Case("object", MCSA_ELF_TypeObject)
1397 .Case("tls_object", MCSA_ELF_TypeTLS)
1398 .Case("common", MCSA_ELF_TypeCommon)
1399 .Case("notype", MCSA_ELF_TypeNoType)
1400 .Default(MCSA_Invalid);
1401
1402 if (Attr == MCSA_Invalid)
1403 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1404
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001405 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001406 return TokError("unexpected token in '.type' directive");
1407
1408 Lex();
1409
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001410 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001411
1412 return false;
1413}
1414
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001415/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001416/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1417bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001418 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001419 StringRef Name;
1420 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001421 return TokError("expected identifier in directive");
1422
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001423 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001424 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001425
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001426 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001427 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001428 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001429
1430 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001431 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001432 if (ParseAbsoluteExpression(Size))
1433 return true;
1434
1435 int64_t Pow2Alignment = 0;
1436 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001437 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001438 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001439 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001440 if (ParseAbsoluteExpression(Pow2Alignment))
1441 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001442
1443 // If this target takes alignments in bytes (not log) validate and convert.
1444 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1445 if (!isPowerOf2_64(Pow2Alignment))
1446 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1447 Pow2Alignment = Log2_64(Pow2Alignment);
1448 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001449 }
1450
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001451 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001452 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001453
Sean Callanan79ed1a82010-01-19 20:22:31 +00001454 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001455
Chris Lattner1fc3d752009-07-09 17:25:12 +00001456 // NOTE: a size of zero for a .comm should create a undefined symbol
1457 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001458 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001459 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1460 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001461
Eric Christopherc260a3e2010-05-14 01:38:54 +00001462 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001463 // may internally end up wanting an alignment in bytes.
1464 // FIXME: Diagnose overflow.
1465 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001466 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1467 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001468
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001469 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001470 return Error(IDLoc, "invalid symbol redefinition");
1471
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001472 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001473 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001474 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001475 getStreamer().EmitZerofill(Ctx.getMachOSection(
1476 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1477 0, SectionKind::getBSS()),
1478 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001479 return false;
1480 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001481
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001482 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001483 return false;
1484}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001485
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001486/// ParseDirectiveAbort
1487/// ::= .abort [ "abort_string" ]
1488bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001489 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001490 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001491
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001492 StringRef Str = "";
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001493 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1494 if (getLexer().isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001495 return TokError("expected string in '.abort' directive");
1496
Sean Callanan18b83232010-01-19 21:44:56 +00001497 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001498
Sean Callanan79ed1a82010-01-19 20:22:31 +00001499 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001500 }
1501
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001502 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001503 return TokError("unexpected token in '.abort' directive");
1504
Sean Callanan79ed1a82010-01-19 20:22:31 +00001505 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001506
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001507 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001508 if (Str.empty())
1509 Error(Loc, ".abort detected. Assembly stopping.");
1510 else
1511 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001512
1513 return false;
1514}
Kevin Enderby71148242009-07-14 21:35:03 +00001515
Kevin Enderby1f049b22009-07-14 23:21:55 +00001516/// ParseDirectiveInclude
1517/// ::= .include "filename"
1518bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001519 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001520 return TokError("expected string in '.include' directive");
1521
Sean Callanan18b83232010-01-19 21:44:56 +00001522 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001523 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001524 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001525
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001526 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001527 return TokError("unexpected token in '.include' directive");
1528
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001529 // Strip the quotes.
1530 Filename = Filename.substr(1, Filename.size()-2);
1531
1532 // Attempt to switch the lexer to the included file before consuming the end
1533 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001534 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001535 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001536 return true;
1537 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001538
1539 return false;
1540}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001541
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001542/// ParseDirectiveIf
1543/// ::= .if expression
1544bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001545 TheCondStack.push_back(TheCondState);
1546 TheCondState.TheCond = AsmCond::IfCond;
1547 if(TheCondState.Ignore) {
1548 EatToEndOfStatement();
1549 }
1550 else {
1551 int64_t ExprValue;
1552 if (ParseAbsoluteExpression(ExprValue))
1553 return true;
1554
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001555 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001556 return TokError("unexpected token in '.if' directive");
1557
Sean Callanan79ed1a82010-01-19 20:22:31 +00001558 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001559
1560 TheCondState.CondMet = ExprValue;
1561 TheCondState.Ignore = !TheCondState.CondMet;
1562 }
1563
1564 return false;
1565}
1566
1567/// ParseDirectiveElseIf
1568/// ::= .elseif expression
1569bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1570 if (TheCondState.TheCond != AsmCond::IfCond &&
1571 TheCondState.TheCond != AsmCond::ElseIfCond)
1572 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1573 " an .elseif");
1574 TheCondState.TheCond = AsmCond::ElseIfCond;
1575
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001576 bool LastIgnoreState = false;
1577 if (!TheCondStack.empty())
1578 LastIgnoreState = TheCondStack.back().Ignore;
1579 if (LastIgnoreState || TheCondState.CondMet) {
1580 TheCondState.Ignore = true;
1581 EatToEndOfStatement();
1582 }
1583 else {
1584 int64_t ExprValue;
1585 if (ParseAbsoluteExpression(ExprValue))
1586 return true;
1587
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001588 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001589 return TokError("unexpected token in '.elseif' directive");
1590
Sean Callanan79ed1a82010-01-19 20:22:31 +00001591 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001592 TheCondState.CondMet = ExprValue;
1593 TheCondState.Ignore = !TheCondState.CondMet;
1594 }
1595
1596 return false;
1597}
1598
1599/// ParseDirectiveElse
1600/// ::= .else
1601bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001602 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001603 return TokError("unexpected token in '.else' directive");
1604
Sean Callanan79ed1a82010-01-19 20:22:31 +00001605 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001606
1607 if (TheCondState.TheCond != AsmCond::IfCond &&
1608 TheCondState.TheCond != AsmCond::ElseIfCond)
1609 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1610 ".elseif");
1611 TheCondState.TheCond = AsmCond::ElseCond;
1612 bool LastIgnoreState = false;
1613 if (!TheCondStack.empty())
1614 LastIgnoreState = TheCondStack.back().Ignore;
1615 if (LastIgnoreState || TheCondState.CondMet)
1616 TheCondState.Ignore = true;
1617 else
1618 TheCondState.Ignore = false;
1619
1620 return false;
1621}
1622
1623/// ParseDirectiveEndIf
1624/// ::= .endif
1625bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001626 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001627 return TokError("unexpected token in '.endif' directive");
1628
Sean Callanan79ed1a82010-01-19 20:22:31 +00001629 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001630
1631 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1632 TheCondStack.empty())
1633 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1634 ".else");
1635 if (!TheCondStack.empty()) {
1636 TheCondState = TheCondStack.back();
1637 TheCondStack.pop_back();
1638 }
1639
1640 return false;
1641}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001642
1643/// ParseDirectiveFile
1644/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001645bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001646 // FIXME: I'm not sure what this is.
1647 int64_t FileNumber = -1;
Daniel Dunbareceec052010-07-12 17:45:27 +00001648 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001649 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001650 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001651
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001652 if (FileNumber < 1)
1653 return TokError("file number less than one");
1654 }
1655
Daniel Dunbareceec052010-07-12 17:45:27 +00001656 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001657 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001658
Chris Lattnerd32e8032010-01-25 19:02:58 +00001659 StringRef Filename = getTok().getString();
1660 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001661 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001662
Daniel Dunbareceec052010-07-12 17:45:27 +00001663 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001664 return TokError("unexpected token in '.file' directive");
1665
Chris Lattnerd32e8032010-01-25 19:02:58 +00001666 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001667 getStreamer().EmitFileDirective(Filename);
Chris Lattnerd32e8032010-01-25 19:02:58 +00001668 else
Daniel Dunbareceec052010-07-12 17:45:27 +00001669 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
1670
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001671 return false;
1672}
1673
1674/// ParseDirectiveLine
1675/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001676bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001677 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1678 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001679 return TokError("unexpected token in '.line' directive");
1680
Sean Callanan18b83232010-01-19 21:44:56 +00001681 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001682 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001683 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001684
1685 // FIXME: Do something with the .line.
1686 }
1687
Daniel Dunbareceec052010-07-12 17:45:27 +00001688 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001689 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001690
1691 return false;
1692}
1693
1694
1695/// ParseDirectiveLoc
1696/// ::= .loc number [number [number]]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001697bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001698 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001699 return TokError("unexpected token in '.loc' directive");
1700
1701 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001702 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001703 (void) FileNumber;
1704 // FIXME: Validate file.
1705
Sean Callanan79ed1a82010-01-19 20:22:31 +00001706 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001707 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1708 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001709 return TokError("unexpected token in '.loc' directive");
1710
Sean Callanan18b83232010-01-19 21:44:56 +00001711 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001712 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001713 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001714
Daniel Dunbareceec052010-07-12 17:45:27 +00001715 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1716 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001717 return TokError("unexpected token in '.loc' directive");
1718
Sean Callanan18b83232010-01-19 21:44:56 +00001719 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001720 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001721 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001722
1723 // FIXME: Do something with the .loc.
1724 }
1725 }
1726
Daniel Dunbareceec052010-07-12 17:45:27 +00001727 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001728 return TokError("unexpected token in '.file' directive");
1729
1730 return false;
1731}
1732
Daniel Dunbar3c802de2010-07-18 18:38:02 +00001733/// ParseDirectiveMacrosOnOff
1734/// ::= .macros_on
1735/// ::= .macros_off
1736bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
1737 SMLoc DirectiveLoc) {
1738 if (getLexer().isNot(AsmToken::EndOfStatement))
1739 return Error(getLexer().getLoc(),
1740 "unexpected token in '" + Directive + "' directive");
1741
1742 getParser().MacrosEnabled = Directive == ".macros_on";
1743
1744 return false;
1745}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00001746
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00001747/// ParseDirectiveMacro
1748/// ::= .macro name
1749bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
1750 SMLoc DirectiveLoc) {
1751 StringRef Name;
1752 if (getParser().ParseIdentifier(Name))
1753 return TokError("expected identifier in directive");
1754
1755 if (getLexer().isNot(AsmToken::EndOfStatement))
1756 return TokError("unexpected token in '.macro' directive");
1757
1758 // Eat the end of statement.
1759 Lex();
1760
1761 AsmToken EndToken, StartToken = getTok();
1762
1763 // Lex the macro definition.
1764 for (;;) {
1765 // Check whether we have reached the end of the file.
1766 if (getLexer().is(AsmToken::Eof))
1767 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
1768
1769 // Otherwise, check whether we have reach the .endmacro.
1770 if (getLexer().is(AsmToken::Identifier) &&
1771 (getTok().getIdentifier() == ".endm" ||
1772 getTok().getIdentifier() == ".endmacro")) {
1773 EndToken = getTok();
1774 Lex();
1775 if (getLexer().isNot(AsmToken::EndOfStatement))
1776 return TokError("unexpected token in '" + EndToken.getIdentifier() +
1777 "' directive");
1778 break;
1779 }
1780
1781 // Otherwise, scan til the end of the statement.
1782 getParser().EatToEndOfStatement();
1783 }
1784
1785 if (getParser().MacroMap.lookup(Name)) {
1786 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
1787 }
1788
1789 const char *BodyStart = StartToken.getLoc().getPointer();
1790 const char *BodyEnd = EndToken.getLoc().getPointer();
1791 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
1792 getParser().MacroMap[Name] = new Macro(Name, Body);
1793 return false;
1794}
1795
1796/// ParseDirectiveEndMacro
1797/// ::= .endm
1798/// ::= .endmacro
1799bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
1800 SMLoc DirectiveLoc) {
1801 if (getLexer().isNot(AsmToken::EndOfStatement))
1802 return TokError("unexpected token in '" + Directive + "' directive");
1803
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001804 // If we are inside a macro instantiation, terminate the current
1805 // instantiation.
1806 if (!getParser().ActiveMacros.empty()) {
1807 getParser().HandleMacroExit();
1808 return false;
1809 }
1810
1811 // Otherwise, this .endmacro is a stray entry in the file; well formed
1812 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00001813 return TokError("unexpected '" + Directive + "' in file, "
1814 "no current macro definition");
1815}
1816
Daniel Dunbard1e3b442010-07-17 02:26:10 +00001817/// \brief Create an MCAsmParser instance.
1818MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
1819 MCContext &C, MCStreamer &Out,
1820 const MCAsmInfo &MAI) {
1821 return new AsmParser(T, SM, C, Out, MAI);
1822}