blob: 3632c937402b24854cf143278c989ec111c0dc00 [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarb95a0792010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000016#include "llvm/ADT/StringMap.h"
Matt Fleming924c5e52010-05-21 11:36:59 +000017#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000021#include "llvm/MC/MCExpr.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000022#include "llvm/MC/MCParser/AsmCond.h"
23#include "llvm/MC/MCParser/AsmLexer.h"
24#include "llvm/MC/MCParser/MCAsmParser.h"
25#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
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"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000029#include "llvm/MC/MCDwarf.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:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000064 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
65 const std::vector<std::vector<AsmToken> > &A);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000066};
67
Daniel Dunbaraef87e32010-07-18 18:31:38 +000068/// \brief The concrete assembly parser instance.
69class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000070 friend class GenericAsmParser;
71
Daniel Dunbaraef87e32010-07-18 18:31:38 +000072 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
73 void operator=(const AsmParser &); // DO NOT IMPLEMENT
74private:
75 AsmLexer Lexer;
76 MCContext &Ctx;
77 MCStreamer &Out;
78 SourceMgr &SrcMgr;
79 MCAsmParserExtension *GenericParser;
80 MCAsmParserExtension *PlatformParser;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000081
Daniel Dunbaraef87e32010-07-18 18:31:38 +000082 /// This is the current buffer index we're lexing from as managed by the
83 /// SourceMgr object.
84 int CurBuffer;
85
86 AsmCond TheCondState;
87 std::vector<AsmCond> TheCondStack;
88
89 /// DirectiveMap - This is a table handlers for directives. Each handler is
90 /// invoked after the directive identifier is read and is responsible for
91 /// parsing and validating the rest of the directive. The handler is passed
92 /// in the directive name and the location of the directive keyword.
93 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000094
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000095 /// MacroMap - Map of currently defined macros.
96 StringMap<Macro*> MacroMap;
97
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000098 /// ActiveMacros - Stack of active macro instantiations.
99 std::vector<MacroInstantiation*> ActiveMacros;
100
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000101 /// Boolean tracking whether macro substitution is enabled.
102 unsigned MacrosEnabled : 1;
103
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000104 /// Flag tracking whether any errors have been encountered.
105 unsigned HadError : 1;
106
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000107public:
108 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
109 const MCAsmInfo &MAI);
110 ~AsmParser();
111
112 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
113
114 void AddDirectiveHandler(MCAsmParserExtension *Object,
115 StringRef Directive,
116 DirectiveHandler Handler) {
117 DirectiveMap[Directive] = std::make_pair(Object, Handler);
118 }
119
120public:
121 /// @name MCAsmParser Interface
122 /// {
123
124 virtual SourceMgr &getSourceManager() { return SrcMgr; }
125 virtual MCAsmLexer &getLexer() { return Lexer; }
126 virtual MCContext &getContext() { return Ctx; }
127 virtual MCStreamer &getStreamer() { return Out; }
128
129 virtual void Warning(SMLoc L, const Twine &Meg);
130 virtual bool Error(SMLoc L, const Twine &Msg);
131
132 const AsmToken &Lex();
133
134 bool ParseExpression(const MCExpr *&Res);
135 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
136 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
137 virtual bool ParseAbsoluteExpression(int64_t &Res);
138
139 /// }
140
141private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000142 void CheckForValidSection();
143
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000144 bool ParseStatement();
145
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000146 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
147 void HandleMacroExit();
148
149 void PrintMacroInstantiations();
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000150 void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type) const {
151 SrcMgr.PrintMessage(Loc, Msg, Type);
152 }
153
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000154 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
155 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000156
157 /// \brief Reset the current lexer position to that given by \arg Loc. The
158 /// current token is not set; clients should ensure Lex() is called
159 /// subsequently.
160 void JumpToLoc(SMLoc Loc);
161
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000162 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000163
164 /// \brief Parse up to the end of statement and a return the contents from the
165 /// current token until the end of the statement; the current token on exit
166 /// will be either the EndOfStatement or EOF.
167 StringRef ParseStringToEndOfStatement();
168
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000169 bool ParseAssignment(StringRef Name);
170
171 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
172 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
173 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
174
175 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
176 /// and set \arg Res to the identifier contents.
177 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000178
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000179 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000180
181 // ".ascii", ".asciiz", ".string"
182 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000183 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000184 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000185 bool ParseDirectiveFill(); // ".fill"
186 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000187 bool ParseDirectiveZero(); // ".zero"
Roman Divacky50e7a782010-10-28 16:22:58 +0000188 bool ParseDirectiveSet(StringRef IDVal); // ".set" or ".equ"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000189 bool ParseDirectiveOrg(); // ".org"
190 // ".align{,32}", ".p2align{,w,l}"
191 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
192
193 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
194 /// accepts a single symbol (which should be a label or an external).
195 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000196
197 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
198
199 bool ParseDirectiveAbort(); // ".abort"
200 bool ParseDirectiveInclude(); // ".include"
201
202 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
203 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
204 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
205 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
206
207 /// ParseEscapedString - Parse the current token as a string which may include
208 /// escaped characters and return the string contents.
209 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000210
211 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
212 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000213};
214
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000215/// \brief Generic implementations of directive handling, etc. which is shared
216/// (or the default, at least) for all assembler parser.
217class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000218 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
219 void AddDirectiveHandler(StringRef Directive) {
220 getParser().AddDirectiveHandler(this, Directive,
221 HandleDirective<GenericAsmParser, Handler>);
222 }
223
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000224public:
225 GenericAsmParser() {}
226
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000227 AsmParser &getParser() {
228 return (AsmParser&) this->MCAsmParserExtension::getParser();
229 }
230
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000231 virtual void Initialize(MCAsmParser &Parser) {
232 // Call the base implementation.
233 this->MCAsmParserExtension::Initialize(Parser);
234
235 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000236 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
237 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
238 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000239 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000240
241 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000242 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
243 ".macros_on");
244 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
245 ".macros_off");
246 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
247 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
248 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000249
250 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
251 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000252 }
253
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000254 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
255 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
256 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000257 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000258
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000259 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000260 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
261 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000262
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000263 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000264};
265
266}
267
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000268namespace llvm {
269
270extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000271extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000272extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000273
274}
275
Chris Lattneraaec2052010-01-19 19:46:13 +0000276enum { DEFAULT_ADDRSPACE = 0 };
277
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000278AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
279 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000280 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000281 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000282 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000283 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000284
285 // Initialize the generic parser.
286 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000287
288 // Initialize the platform / file format parser.
289 //
290 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
291 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000292 if (_MAI.hasMicrosoftFastStdCallMangling()) {
293 PlatformParser = createCOFFAsmParser();
294 PlatformParser->Initialize(*this);
295 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000296 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000297 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000298 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000299 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000300 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000301 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000302}
303
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000304AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000305 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
306
307 // Destroy any macros.
308 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
309 ie = MacroMap.end(); it != ie; ++it)
310 delete it->getValue();
311
Daniel Dunbare4749702010-07-12 18:12:02 +0000312 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000313 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000314}
315
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000316void AsmParser::PrintMacroInstantiations() {
317 // Print the active macro instantiation stack.
318 for (std::vector<MacroInstantiation*>::const_reverse_iterator
319 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
320 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
321 "note");
322}
323
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000324void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000325 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000326 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000327}
328
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000329bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000330 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000331 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000332 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000333 return true;
334}
335
Sean Callananfd0b0282010-01-21 00:19:58 +0000336bool AsmParser::EnterIncludeFile(const std::string &Filename) {
337 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
338 if (NewBuf == -1)
339 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000340
Sean Callananfd0b0282010-01-21 00:19:58 +0000341 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000342
Sean Callananfd0b0282010-01-21 00:19:58 +0000343 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000344
Sean Callananfd0b0282010-01-21 00:19:58 +0000345 return false;
346}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000347
348void AsmParser::JumpToLoc(SMLoc Loc) {
349 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
350 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
351}
352
Sean Callananfd0b0282010-01-21 00:19:58 +0000353const AsmToken &AsmParser::Lex() {
354 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000355
Sean Callananfd0b0282010-01-21 00:19:58 +0000356 if (tok->is(AsmToken::Eof)) {
357 // If this is the end of an included file, pop the parent file off the
358 // include stack.
359 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
360 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000361 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000362 tok = &Lexer.Lex();
363 }
364 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000365
Sean Callananfd0b0282010-01-21 00:19:58 +0000366 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000367 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000368
Sean Callananfd0b0282010-01-21 00:19:58 +0000369 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000370}
371
Chris Lattner79180e22010-04-05 23:15:42 +0000372bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000373 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000374 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000375 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000376
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000377 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000378 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000379
380 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000381 AsmCond StartingCondState = TheCondState;
382
Chris Lattnerb717fb02009-07-02 21:53:43 +0000383 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000384 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000385 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000386
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000387 // We had an error, validate that one was emitted and recover by skipping to
388 // the next line.
389 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000390 EatToEndOfStatement();
391 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000392
393 if (TheCondState.TheCond != StartingCondState.TheCond ||
394 TheCondState.Ignore != StartingCondState.Ignore)
395 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000396
397 // Check to see there are no empty DwarfFile slots.
398 const std::vector<MCDwarfFile *> &MCDwarfFiles =
399 getContext().getMCDwarfFiles();
400 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000401 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000402 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000403 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000404
Chris Lattner79180e22010-04-05 23:15:42 +0000405 // Finalize the output stream if there are no errors and if the client wants
406 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000407 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000408 Out.Finish();
409
Chris Lattnerb717fb02009-07-02 21:53:43 +0000410 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000411}
412
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000413void AsmParser::CheckForValidSection() {
414 if (!getStreamer().getCurrentSection()) {
415 TokError("expected section directive before assembly directive");
416 Out.SwitchSection(Ctx.getMachOSection(
417 "__TEXT", "__text",
418 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
419 0, SectionKind::getText()));
420 }
421}
422
Chris Lattner2cf5f142009-06-22 01:29:09 +0000423/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
424void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000425 while (Lexer.isNot(AsmToken::EndOfStatement) &&
426 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000427 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000428
Chris Lattner2cf5f142009-06-22 01:29:09 +0000429 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000430 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000431 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000432}
433
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000434StringRef AsmParser::ParseStringToEndOfStatement() {
435 const char *Start = getTok().getLoc().getPointer();
436
437 while (Lexer.isNot(AsmToken::EndOfStatement) &&
438 Lexer.isNot(AsmToken::Eof))
439 Lex();
440
441 const char *End = getTok().getLoc().getPointer();
442 return StringRef(Start, End - Start);
443}
Chris Lattnerc4193832009-06-22 05:51:26 +0000444
Chris Lattner74ec1a32009-06-22 06:32:03 +0000445/// ParseParenExpr - Parse a paren expression and return it.
446/// NOTE: This assumes the leading '(' has already been consumed.
447///
448/// parenexpr ::= expr)
449///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000450bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000451 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000452 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000453 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000454 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000455 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000456 return false;
457}
Chris Lattnerc4193832009-06-22 05:51:26 +0000458
Chris Lattner74ec1a32009-06-22 06:32:03 +0000459/// ParsePrimaryExpr - Parse a primary expression and return it.
460/// primaryexpr ::= (parenexpr
461/// primaryexpr ::= symbol
462/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000463/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000464/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000465bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000466 switch (Lexer.getKind()) {
467 default:
468 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000469 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000470 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000471 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000472 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000473 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000474 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000475 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000476 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000477 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000478 EndLoc = Lexer.getLoc();
479
480 StringRef Identifier;
481 if (ParseIdentifier(Identifier))
482 return false;
483
Daniel Dunbarfffff912009-10-16 01:34:54 +0000484 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000485 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000486 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000487
488 // Lookup the symbol variant if used.
489 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000490 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000491 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000492 if (Variant == MCSymbolRefExpr::VK_Invalid) {
493 Variant = MCSymbolRefExpr::VK_None;
494 TokError("invalid variant '" + Split.second + "'");
495 }
496 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000497
Daniel Dunbarfffff912009-10-16 01:34:54 +0000498 // If this is an absolute variable reference, substitute it now to preserve
499 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000500 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000501 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000502 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000503
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000504 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000505 return false;
506 }
507
508 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000509 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000510 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000511 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000512 case AsmToken::Integer: {
513 SMLoc Loc = getTok().getLoc();
514 int64_t IntVal = getTok().getIntVal();
515 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000516 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000517 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000518 // Look for 'b' or 'f' following an Integer as a directional label
519 if (Lexer.getKind() == AsmToken::Identifier) {
520 StringRef IDVal = getTok().getString();
521 if (IDVal == "f" || IDVal == "b"){
522 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
523 IDVal == "f" ? 1 : 0);
524 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
525 getContext());
526 if(IDVal == "b" && Sym->isUndefined())
527 return Error(Loc, "invalid reference to undefined symbol");
528 EndLoc = Lexer.getLoc();
529 Lex(); // Eat identifier.
530 }
531 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000532 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000533 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000534 case AsmToken::Dot: {
535 // This is a '.' reference, which references the current PC. Emit a
536 // temporary label to the streamer and refer to it.
537 MCSymbol *Sym = Ctx.CreateTempSymbol();
538 Out.EmitLabel(Sym);
539 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
540 EndLoc = Lexer.getLoc();
541 Lex(); // Eat identifier.
542 return false;
543 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000544
Daniel Dunbar3f872332009-07-28 16:08:33 +0000545 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000546 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000547 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000548 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000549 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000550 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000551 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000552 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000553 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000554 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000555 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000556 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000557 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000558 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000559 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000560 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000561 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000562 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000563 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000564 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000565 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000566 }
567}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000568
Chris Lattnerb4307b32010-01-15 19:28:38 +0000569bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000570 SMLoc EndLoc;
571 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000572}
573
Daniel Dunbarcceba832010-09-17 02:47:07 +0000574const MCExpr *
575AsmParser::ApplyModifierToExpr(const MCExpr *E,
576 MCSymbolRefExpr::VariantKind Variant) {
577 // Recurse over the given expression, rebuilding it to apply the given variant
578 // if there is exactly one symbol.
579 switch (E->getKind()) {
580 case MCExpr::Target:
581 case MCExpr::Constant:
582 return 0;
583
584 case MCExpr::SymbolRef: {
585 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
586
587 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
588 TokError("invalid variant on expression '" +
589 getTok().getIdentifier() + "' (already modified)");
590 return E;
591 }
592
593 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
594 }
595
596 case MCExpr::Unary: {
597 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
598 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
599 if (!Sub)
600 return 0;
601 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
602 }
603
604 case MCExpr::Binary: {
605 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
606 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
607 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
608
609 if (!LHS && !RHS)
610 return 0;
611
612 if (!LHS) LHS = BE->getLHS();
613 if (!RHS) RHS = BE->getRHS();
614
615 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
616 }
617 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000618
619 assert(0 && "Invalid expression kind!");
620 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000621}
622
Chris Lattner74ec1a32009-06-22 06:32:03 +0000623/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000624///
Chris Lattner74ec1a32009-06-22 06:32:03 +0000625/// expr ::= expr +,- expr -> lowest.
626/// expr ::= expr |,^,&,! expr -> middle.
627/// expr ::= expr *,/,%,<<,>> expr -> highest.
628/// expr ::= primaryexpr
629///
Chris Lattner54482b42010-01-15 19:39:23 +0000630bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000631 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000632 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000633 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
634 return true;
635
Daniel Dunbarcceba832010-09-17 02:47:07 +0000636 // As a special case, we support 'a op b @ modifier' by rewriting the
637 // expression to include the modifier. This is inefficient, but in general we
638 // expect users to use 'a@modifier op b'.
639 if (Lexer.getKind() == AsmToken::At) {
640 Lex();
641
642 if (Lexer.isNot(AsmToken::Identifier))
643 return TokError("unexpected symbol modifier following '@'");
644
645 MCSymbolRefExpr::VariantKind Variant =
646 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
647 if (Variant == MCSymbolRefExpr::VK_Invalid)
648 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
649
650 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
651 if (!ModifiedRes) {
652 return TokError("invalid modifier '" + getTok().getIdentifier() +
653 "' (no symbols present)");
654 return true;
655 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000656
Daniel Dunbarcceba832010-09-17 02:47:07 +0000657 Res = ModifiedRes;
658 Lex();
659 }
660
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000661 // Try to constant fold it up front, if possible.
662 int64_t Value;
663 if (Res->EvaluateAsAbsolute(Value))
664 Res = MCConstantExpr::Create(Value, getContext());
665
666 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000667}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000668
Chris Lattnerb4307b32010-01-15 19:28:38 +0000669bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000670 Res = 0;
671 return ParseParenExpr(Res, EndLoc) ||
672 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000673}
674
Daniel Dunbar475839e2009-06-29 20:37:27 +0000675bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000676 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000677
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000678 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000679 if (ParseExpression(Expr))
680 return true;
681
Daniel Dunbare00b0112009-10-16 01:57:52 +0000682 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000683 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000684
685 return false;
686}
687
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000688static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000689 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000690 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000691 default:
692 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000693
Daniel Dunbarcceba832010-09-17 02:47:07 +0000694 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000695 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000696 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000697 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000698 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000699 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000700 return 1;
701
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000702
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000703 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000704 //
705 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000706 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000707 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000708 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000709 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000710 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000711 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000712 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000713 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000714 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000715
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000716 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000717 case AsmToken::EqualEqual:
718 Kind = MCBinaryExpr::EQ;
719 return 3;
720 case AsmToken::ExclaimEqual:
721 case AsmToken::LessGreater:
722 Kind = MCBinaryExpr::NE;
723 return 3;
724 case AsmToken::Less:
725 Kind = MCBinaryExpr::LT;
726 return 3;
727 case AsmToken::LessEqual:
728 Kind = MCBinaryExpr::LTE;
729 return 3;
730 case AsmToken::Greater:
731 Kind = MCBinaryExpr::GT;
732 return 3;
733 case AsmToken::GreaterEqual:
734 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000735 return 3;
736
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000737 // High Intermediate Precedence: +, -
738 case AsmToken::Plus:
739 Kind = MCBinaryExpr::Add;
740 return 4;
741 case AsmToken::Minus:
742 Kind = MCBinaryExpr::Sub;
743 return 4;
744
Daniel Dunbar475839e2009-06-29 20:37:27 +0000745 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000746 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000747 Kind = MCBinaryExpr::Mul;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000748 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000749 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000750 Kind = MCBinaryExpr::Div;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000751 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000752 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000753 Kind = MCBinaryExpr::Mod;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000754 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000755 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000756 Kind = MCBinaryExpr::Shl;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000757 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000758 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000759 Kind = MCBinaryExpr::Shr;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000760 return 5;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000761 }
762}
763
764
765/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
766/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000767bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
768 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000769 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000770 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000771 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000772
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000773 // If the next token is lower precedence than we are allowed to eat, return
774 // successfully with what we ate already.
775 if (TokPrec < Precedence)
776 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000777
Sean Callanan79ed1a82010-01-19 20:22:31 +0000778 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000779
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000780 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000781 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000782 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000783
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000784 // If BinOp binds less tightly with RHS than the operator after RHS, let
785 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000786 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000787 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000788 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000789 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000790 }
791
Daniel Dunbar475839e2009-06-29 20:37:27 +0000792 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000793 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000794 }
795}
796
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000797
798
799
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000800/// ParseStatement:
801/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000802/// ::= Label* Directive ...Operands... EndOfStatement
803/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000804bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000805 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000806 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000807 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000808 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000809 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000810
811 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000812 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000813 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000814 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000815 int64_t LocalLabelVal = -1;
816 // GUESS allow an integer followed by a ':' as a directional local label
817 if (Lexer.is(AsmToken::Integer)) {
818 LocalLabelVal = getTok().getIntVal();
819 if (LocalLabelVal < 0) {
820 if (!TheCondState.Ignore)
821 return TokError("unexpected token at start of statement");
822 IDVal = "";
823 }
824 else {
825 IDVal = getTok().getString();
826 Lex(); // Consume the integer token to be used as an identifier token.
827 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000828 if (!TheCondState.Ignore)
829 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000830 }
831 }
832 }
833 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000834 if (!TheCondState.Ignore)
835 return TokError("unexpected token at start of statement");
836 IDVal = "";
837 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000838
Chris Lattner7834fac2010-04-17 18:14:27 +0000839 // Handle conditional assembly here before checking for skipping. We
840 // have to do this so that .endif isn't skipped in a ".if 0" block for
841 // example.
842 if (IDVal == ".if")
843 return ParseDirectiveIf(IDLoc);
844 if (IDVal == ".elseif")
845 return ParseDirectiveElseIf(IDLoc);
846 if (IDVal == ".else")
847 return ParseDirectiveElse(IDLoc);
848 if (IDVal == ".endif")
849 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000850
Chris Lattner7834fac2010-04-17 18:14:27 +0000851 // If we are in a ".if 0" block, ignore this statement.
852 if (TheCondState.Ignore) {
853 EatToEndOfStatement();
854 return false;
855 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000856
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000857 // FIXME: Recurse on local labels?
858
859 // See what kind of statement we have.
860 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000861 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000862 CheckForValidSection();
863
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000864 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000865 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000866
867 // Diagnose attempt to use a variable as a label.
868 //
869 // FIXME: Diagnostics. Note the location of the definition as a label.
870 // FIXME: This doesn't diagnose assignment to a symbol which has been
871 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000872 MCSymbol *Sym;
873 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000874 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000875 else
876 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000877 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000878 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000879
Daniel Dunbar959fd882009-08-26 22:13:22 +0000880 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000881 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000882
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000883 // Consume any end of statement token, if present, to avoid spurious
884 // AddBlankLine calls().
885 if (Lexer.is(AsmToken::EndOfStatement)) {
886 Lex();
887 if (Lexer.is(AsmToken::Eof))
888 return false;
889 }
890
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000891 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000892 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000893
Daniel Dunbar3f872332009-07-28 16:08:33 +0000894 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000895 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000896 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000897
Daniel Dunbare2ace502009-08-31 08:09:09 +0000898 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000899
900 default: // Normal instruction or directive.
901 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000902 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000903
904 // If macros are enabled, check to see if this is a macro instantiation.
905 if (MacrosEnabled)
906 if (const Macro *M = MacroMap.lookup(IDVal))
907 return HandleMacroEntry(IDVal, IDLoc, M);
908
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000909 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000910 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000911 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +0000912 if (IDVal == ".set" || IDVal == ".equ")
913 return ParseDirectiveSet(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000914
Daniel Dunbara0d14262009-06-24 23:30:00 +0000915 // Data directives
916
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000917 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +0000918 return ParseDirectiveAscii(IDVal, false);
919 if (IDVal == ".asciz" || IDVal == ".string")
920 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000921
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000922 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000923 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000924 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000925 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +0000926 if (IDVal == ".value")
927 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000928 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000929 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000930 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000931 return ParseDirectiveValue(8);
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000932 if (IDVal == ".single")
933 return ParseDirectiveRealValue(APFloat::IEEEsingle);
934 if (IDVal == ".double")
935 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000936
Eli Friedman5d68ec22010-07-19 04:17:25 +0000937 if (IDVal == ".align") {
938 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
939 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
940 }
941 if (IDVal == ".align32") {
942 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
943 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
944 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000945 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000946 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000947 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000948 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000949 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000950 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000951 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000952 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000953 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000954 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000955 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000956 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
957
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000958 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000959 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000960
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000961 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000962 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000963 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000964 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000965 if (IDVal == ".zero")
966 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000967
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000968 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000969
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000970 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000971 return ParseDirectiveSymbolAttribute(MCSA_Global);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000972 // ELF only? Should it be here?
973 if (IDVal == ".local")
974 return ParseDirectiveSymbolAttribute(MCSA_Local);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000975 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000976 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000977 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000978 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000979 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000980 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000981 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000982 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000983 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000984 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000985 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000986 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000987 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000988 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000989 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000990 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000991 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000992 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000993 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000994 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000995 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000996 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000997 if (IDVal == ".weak_def_can_be_hidden")
998 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000999
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001000 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001001 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001002 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001003 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001004
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001005 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001006 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001007 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001008 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001009
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001010 // Look up the handler in the handler table.
1011 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1012 DirectiveMap.lookup(IDVal);
1013 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001014 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001015
Kevin Enderby9c656452009-09-10 20:51:44 +00001016 // Target hook for parsing target specific directives.
1017 if (!getTargetParser().ParseDirective(ID))
1018 return false;
1019
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001020 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001021 EatToEndOfStatement();
1022 return false;
1023 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001024
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001025 CheckForValidSection();
1026
Chris Lattnera7f13542010-05-19 23:34:33 +00001027 // Canonicalize the opcode to lower case.
1028 SmallString<128> Opcode;
1029 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1030 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001031
Chris Lattner98986712010-01-14 22:21:20 +00001032 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001033 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001034 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001035
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001036 // Dump the parsed representation, if requested.
1037 if (getShowParsedOperands()) {
1038 SmallString<256> Str;
1039 raw_svector_ostream OS(Str);
1040 OS << "parsed instruction: [";
1041 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1042 if (i != 0)
1043 OS << ", ";
1044 ParsedOperands[i]->dump(OS);
1045 }
1046 OS << "]";
1047
1048 PrintMessage(IDLoc, OS.str(), "note");
1049 }
1050
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001051 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001052 if (!HadError)
1053 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1054 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001055
Chris Lattner98986712010-01-14 22:21:20 +00001056 // Free any parsed operands.
1057 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1058 delete ParsedOperands[i];
1059
Chris Lattnercbf8a982010-09-11 16:18:25 +00001060 // Don't skip the rest of the line, the instruction parser is responsible for
1061 // that.
1062 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001063}
Chris Lattner9a023f72009-06-24 04:43:34 +00001064
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001065MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1066 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001067 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
1068{
1069 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1070 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001071 SmallString<256> Buf;
1072 raw_svector_ostream OS(Buf);
1073
1074 StringRef Body = M->Body;
1075 while (!Body.empty()) {
1076 // Scan for the next substitution.
1077 std::size_t End = Body.size(), Pos = 0;
1078 for (; Pos != End; ++Pos) {
1079 // Check for a substitution or escape.
1080 if (Body[Pos] != '$' || Pos + 1 == End)
1081 continue;
1082
1083 char Next = Body[Pos + 1];
1084 if (Next == '$' || Next == 'n' || isdigit(Next))
1085 break;
1086 }
1087
1088 // Add the prefix.
1089 OS << Body.slice(0, Pos);
1090
1091 // Check if we reached the end.
1092 if (Pos == End)
1093 break;
1094
1095 switch (Body[Pos+1]) {
1096 // $$ => $
1097 case '$':
1098 OS << '$';
1099 break;
1100
1101 // $n => number of arguments
1102 case 'n':
1103 OS << A.size();
1104 break;
1105
1106 // $[0-9] => argument
1107 default: {
1108 // Missing arguments are ignored.
1109 unsigned Index = Body[Pos+1] - '0';
1110 if (Index >= A.size())
1111 break;
1112
1113 // Otherwise substitute with the token values, with spaces eliminated.
1114 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1115 ie = A[Index].end(); it != ie; ++it)
1116 OS << it->getString();
1117 break;
1118 }
1119 }
1120
1121 // Update the scan point.
1122 Body = Body.substr(Pos + 2);
1123 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001124
1125 // We include the .endmacro in the buffer as our queue to exit the macro
1126 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001127 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001128
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001129 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001130}
1131
1132bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1133 const Macro *M) {
1134 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1135 // this, although we should protect against infinite loops.
1136 if (ActiveMacros.size() == 20)
1137 return TokError("macros cannot be nested more than 20 levels deep");
1138
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001139 // Parse the macro instantiation arguments.
1140 std::vector<std::vector<AsmToken> > MacroArguments;
1141 MacroArguments.push_back(std::vector<AsmToken>());
1142 unsigned ParenLevel = 0;
1143 for (;;) {
1144 if (Lexer.is(AsmToken::Eof))
1145 return TokError("unexpected token in macro instantiation");
1146 if (Lexer.is(AsmToken::EndOfStatement))
1147 break;
1148
1149 // If we aren't inside parentheses and this is a comma, start a new token
1150 // list.
1151 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1152 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001153 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001154 // Adjust the current parentheses level.
1155 if (Lexer.is(AsmToken::LParen))
1156 ++ParenLevel;
1157 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1158 --ParenLevel;
1159
1160 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001161 MacroArguments.back().push_back(getTok());
1162 }
1163 Lex();
1164 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001165
1166 // Create the macro instantiation object and add to the current macro
1167 // instantiation stack.
1168 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001169 getTok().getLoc(),
1170 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001171 ActiveMacros.push_back(MI);
1172
1173 // Jump to the macro instantiation and prime the lexer.
1174 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1175 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1176 Lex();
1177
1178 return false;
1179}
1180
1181void AsmParser::HandleMacroExit() {
1182 // Jump to the EndOfStatement we should return to, and consume it.
1183 JumpToLoc(ActiveMacros.back()->ExitLoc);
1184 Lex();
1185
1186 // Pop the instantiation entry.
1187 delete ActiveMacros.back();
1188 ActiveMacros.pop_back();
1189}
1190
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001191static void MarkUsed(const MCExpr *Value) {
1192 switch (Value->getKind()) {
1193 case MCExpr::Binary:
1194 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1195 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1196 break;
1197 case MCExpr::Target:
1198 case MCExpr::Constant:
1199 break;
1200 case MCExpr::SymbolRef: {
1201 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1202 break;
1203 }
1204 case MCExpr::Unary:
1205 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1206 break;
1207 }
1208}
1209
Benjamin Kramer38e59892010-07-14 22:38:02 +00001210bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001211 // FIXME: Use better location, we should use proper tokens.
1212 SMLoc EqualLoc = Lexer.getLoc();
1213
Daniel Dunbar821e3332009-08-31 08:09:28 +00001214 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001215 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001216 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001217
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001218 MarkUsed(Value);
1219
Daniel Dunbar3f872332009-07-28 16:08:33 +00001220 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001221 return TokError("unexpected token in assignment");
1222
1223 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001224 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001225
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001226 // Validate that the LHS is allowed to be a variable (either it has not been
1227 // used as a symbol, or it is an absolute symbol).
1228 MCSymbol *Sym = getContext().LookupSymbol(Name);
1229 if (Sym) {
1230 // Diagnose assignment to a label.
1231 //
1232 // FIXME: Diagnostics. Note the location of the definition as a label.
1233 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001234 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001235 ; // Allow redefinitions of undefined symbols only used in directives.
1236 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001237 return Error(EqualLoc, "redefinition of '" + Name + "'");
1238 else if (!Sym->isVariable())
1239 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001240 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001241 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1242 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001243
1244 // Don't count these checks as uses.
1245 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001246 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001247 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001248
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001249 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001250
1251 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001252 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001253
1254 return false;
1255}
1256
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001257/// ParseIdentifier:
1258/// ::= identifier
1259/// ::= string
1260bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001261 // The assembler has relaxed rules for accepting identifiers, in particular we
1262 // allow things like '.globl $foo', which would normally be separate
1263 // tokens. At this level, we have already lexed so we cannot (currently)
1264 // handle this as a context dependent token, instead we detect adjacent tokens
1265 // and return the combined identifier.
1266 if (Lexer.is(AsmToken::Dollar)) {
1267 SMLoc DollarLoc = getLexer().getLoc();
1268
1269 // Consume the dollar sign, and check for a following identifier.
1270 Lex();
1271 if (Lexer.isNot(AsmToken::Identifier))
1272 return true;
1273
1274 // We have a '$' followed by an identifier, make sure they are adjacent.
1275 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1276 return true;
1277
1278 // Construct the joined identifier and consume the token.
1279 Res = StringRef(DollarLoc.getPointer(),
1280 getTok().getIdentifier().size() + 1);
1281 Lex();
1282 return false;
1283 }
1284
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001285 if (Lexer.isNot(AsmToken::Identifier) &&
1286 Lexer.isNot(AsmToken::String))
1287 return true;
1288
Sean Callanan18b83232010-01-19 21:44:56 +00001289 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001290
Sean Callanan79ed1a82010-01-19 20:22:31 +00001291 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001292
1293 return false;
1294}
1295
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001296/// ParseDirectiveSet:
1297/// ::= .set identifier ',' expression
Roman Divacky50e7a782010-10-28 16:22:58 +00001298bool AsmParser::ParseDirectiveSet(StringRef IDVal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001299 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001300
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001301 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001302 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001303
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001304 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001305 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001306 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001307
Daniel Dunbare2ace502009-08-31 08:09:09 +00001308 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001309}
1310
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001311bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001312 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001313
1314 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001315 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001316 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1317 if (Str[i] != '\\') {
1318 Data += Str[i];
1319 continue;
1320 }
1321
1322 // Recognize escaped characters. Note that this escape semantics currently
1323 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1324 ++i;
1325 if (i == e)
1326 return TokError("unexpected backslash at end of string");
1327
1328 // Recognize octal sequences.
1329 if ((unsigned) (Str[i] - '0') <= 7) {
1330 // Consume up to three octal characters.
1331 unsigned Value = Str[i] - '0';
1332
1333 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1334 ++i;
1335 Value = Value * 8 + (Str[i] - '0');
1336
1337 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1338 ++i;
1339 Value = Value * 8 + (Str[i] - '0');
1340 }
1341 }
1342
1343 if (Value > 255)
1344 return TokError("invalid octal escape sequence (out of range)");
1345
1346 Data += (unsigned char) Value;
1347 continue;
1348 }
1349
1350 // Otherwise recognize individual escapes.
1351 switch (Str[i]) {
1352 default:
1353 // Just reject invalid escape sequences for now.
1354 return TokError("invalid escape sequence (unrecognized character)");
1355
1356 case 'b': Data += '\b'; break;
1357 case 'f': Data += '\f'; break;
1358 case 'n': Data += '\n'; break;
1359 case 'r': Data += '\r'; break;
1360 case 't': Data += '\t'; break;
1361 case '"': Data += '"'; break;
1362 case '\\': Data += '\\'; break;
1363 }
1364 }
1365
1366 return false;
1367}
1368
Daniel Dunbara0d14262009-06-24 23:30:00 +00001369/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001370/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1371bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001372 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001373 CheckForValidSection();
1374
Daniel Dunbara0d14262009-06-24 23:30:00 +00001375 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001376 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001377 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001378
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001379 std::string Data;
1380 if (ParseEscapedString(Data))
1381 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001382
1383 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001384 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001385 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1386
Sean Callanan79ed1a82010-01-19 20:22:31 +00001387 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001388
1389 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001390 break;
1391
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001392 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001393 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001394 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001395 }
1396 }
1397
Sean Callanan79ed1a82010-01-19 20:22:31 +00001398 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001399 return false;
1400}
1401
1402/// ParseDirectiveValue
1403/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1404bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001405 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001406 CheckForValidSection();
1407
Daniel Dunbara0d14262009-06-24 23:30:00 +00001408 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001409 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001410 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001411 return true;
1412
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001413 // Special case constant expressions to match code generator.
1414 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001415 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001416 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001417 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001418
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001419 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001420 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001421
Daniel Dunbara0d14262009-06-24 23:30:00 +00001422 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001423 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001424 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001425 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001426 }
1427 }
1428
Sean Callanan79ed1a82010-01-19 20:22:31 +00001429 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001430 return false;
1431}
1432
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001433/// ParseDirectiveRealValue
1434/// ::= (.single | .double) [ expression (, expression)* ]
1435bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1436 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1437 CheckForValidSection();
1438
1439 for (;;) {
1440 // We don't truly support arithmetic on floating point expressions, so we
1441 // have to manually parse unary prefixes.
1442 bool IsNeg = false;
1443 if (getLexer().is(AsmToken::Minus)) {
1444 Lex();
1445 IsNeg = true;
1446 } else if (getLexer().is(AsmToken::Plus))
1447 Lex();
1448
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001449 if (getLexer().isNot(AsmToken::Integer) &&
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001450 getLexer().isNot(AsmToken::Real))
1451 return TokError("unexpected token in directive");
1452
1453 // Convert to an APFloat.
1454 APFloat Value(Semantics);
1455 if (Value.convertFromString(getTok().getString(),
1456 APFloat::rmNearestTiesToEven) ==
1457 APFloat::opInvalidOp)
1458 return TokError("invalid floating point literal");
1459 if (IsNeg)
1460 Value.changeSign();
1461
1462 // Consume the numeric token.
1463 Lex();
1464
1465 // Emit the value as an integer.
1466 APInt AsInt = Value.bitcastToAPInt();
1467 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1468 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1469
1470 if (getLexer().is(AsmToken::EndOfStatement))
1471 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001472
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001473 if (getLexer().isNot(AsmToken::Comma))
1474 return TokError("unexpected token in directive");
1475 Lex();
1476 }
1477 }
1478
1479 Lex();
1480 return false;
1481}
1482
Daniel Dunbara0d14262009-06-24 23:30:00 +00001483/// ParseDirectiveSpace
1484/// ::= .space expression [ , expression ]
1485bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001486 CheckForValidSection();
1487
Daniel Dunbara0d14262009-06-24 23:30:00 +00001488 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001489 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001490 return true;
1491
1492 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001493 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1494 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001495 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001496 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001497
Daniel Dunbar475839e2009-06-29 20:37:27 +00001498 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001499 return true;
1500
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001501 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001502 return TokError("unexpected token in '.space' directive");
1503 }
1504
Sean Callanan79ed1a82010-01-19 20:22:31 +00001505 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001506
1507 if (NumBytes <= 0)
1508 return TokError("invalid number of bytes in '.space' directive");
1509
1510 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001511 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001512
1513 return false;
1514}
1515
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001516/// ParseDirectiveZero
1517/// ::= .zero expression
1518bool AsmParser::ParseDirectiveZero() {
1519 CheckForValidSection();
1520
1521 int64_t NumBytes;
1522 if (ParseAbsoluteExpression(NumBytes))
1523 return true;
1524
Rafael Espindolae452b172010-10-05 19:42:57 +00001525 int64_t Val = 0;
1526 if (getLexer().is(AsmToken::Comma)) {
1527 Lex();
1528 if (ParseAbsoluteExpression(Val))
1529 return true;
1530 }
1531
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001532 if (getLexer().isNot(AsmToken::EndOfStatement))
1533 return TokError("unexpected token in '.zero' directive");
1534
1535 Lex();
1536
Rafael Espindolae452b172010-10-05 19:42:57 +00001537 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001538
1539 return false;
1540}
1541
Daniel Dunbara0d14262009-06-24 23:30:00 +00001542/// ParseDirectiveFill
1543/// ::= .fill expression , expression , expression
1544bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001545 CheckForValidSection();
1546
Daniel Dunbara0d14262009-06-24 23:30:00 +00001547 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001548 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001549 return true;
1550
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001551 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001552 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001553 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001554
Daniel Dunbara0d14262009-06-24 23:30:00 +00001555 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001556 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001557 return true;
1558
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001559 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001560 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001561 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001562
Daniel Dunbara0d14262009-06-24 23:30:00 +00001563 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001564 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001565 return true;
1566
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001567 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001568 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001569
Sean Callanan79ed1a82010-01-19 20:22:31 +00001570 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001571
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001572 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1573 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001574
1575 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001576 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001577
1578 return false;
1579}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001580
1581/// ParseDirectiveOrg
1582/// ::= .org expression [ , expression ]
1583bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001584 CheckForValidSection();
1585
Daniel Dunbar821e3332009-08-31 08:09:28 +00001586 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001587 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001588 return true;
1589
1590 // Parse optional fill expression.
1591 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001592 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1593 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001594 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001595 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001596
Daniel Dunbar475839e2009-06-29 20:37:27 +00001597 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001598 return true;
1599
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001600 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001601 return TokError("unexpected token in '.org' directive");
1602 }
1603
Sean Callanan79ed1a82010-01-19 20:22:31 +00001604 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001605
1606 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1607 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001608 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001609
1610 return false;
1611}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001612
1613/// ParseDirectiveAlign
1614/// ::= {.align, ...} expression [ , expression [ , expression ]]
1615bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001616 CheckForValidSection();
1617
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001618 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001619 int64_t Alignment;
1620 if (ParseAbsoluteExpression(Alignment))
1621 return true;
1622
1623 SMLoc MaxBytesLoc;
1624 bool HasFillExpr = false;
1625 int64_t FillExpr = 0;
1626 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001627 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1628 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001629 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001630 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001631
1632 // The fill expression can be omitted while specifying a maximum number of
1633 // alignment bytes, e.g:
1634 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001635 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001636 HasFillExpr = true;
1637 if (ParseAbsoluteExpression(FillExpr))
1638 return true;
1639 }
1640
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001641 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1642 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001643 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001644 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001645
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001646 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001647 if (ParseAbsoluteExpression(MaxBytesToFill))
1648 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001649
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001650 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001651 return TokError("unexpected token in directive");
1652 }
1653 }
1654
Sean Callanan79ed1a82010-01-19 20:22:31 +00001655 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001656
Daniel Dunbar648ac512010-05-17 21:54:30 +00001657 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001658 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001659
1660 // Compute alignment in bytes.
1661 if (IsPow2) {
1662 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001663 if (Alignment >= 32) {
1664 Error(AlignmentLoc, "invalid alignment value");
1665 Alignment = 31;
1666 }
1667
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001668 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001669 }
1670
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001671 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001672 if (MaxBytesLoc.isValid()) {
1673 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001674 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1675 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001676 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001677 }
1678
1679 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001680 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1681 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001682 MaxBytesToFill = 0;
1683 }
1684 }
1685
Daniel Dunbar648ac512010-05-17 21:54:30 +00001686 // Check whether we should use optimal code alignment for this .align
1687 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001688 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001689 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1690 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001691 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001692 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001693 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001694 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1695 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001696 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001697
1698 return false;
1699}
1700
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001701/// ParseDirectiveSymbolAttribute
1702/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001703bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001704 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001705 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001706 StringRef Name;
1707
1708 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001709 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001710
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001711 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001712
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001713 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001714
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001715 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001716 break;
1717
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001718 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001719 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001720 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001721 }
1722 }
1723
Sean Callanan79ed1a82010-01-19 20:22:31 +00001724 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001725 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001726}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001727
1728/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001729/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1730bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001731 CheckForValidSection();
1732
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001733 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001734 StringRef Name;
1735 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001736 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001737
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001738 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001739 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001740
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001741 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001742 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001743 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001744
1745 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001746 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001747 if (ParseAbsoluteExpression(Size))
1748 return true;
1749
1750 int64_t Pow2Alignment = 0;
1751 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001752 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001753 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001754 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001755 if (ParseAbsoluteExpression(Pow2Alignment))
1756 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001757
Chris Lattner258281d2010-01-19 06:22:22 +00001758 // If this target takes alignments in bytes (not log) validate and convert.
1759 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1760 if (!isPowerOf2_64(Pow2Alignment))
1761 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1762 Pow2Alignment = Log2_64(Pow2Alignment);
1763 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001764 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001765
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001766 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001767 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001768
Sean Callanan79ed1a82010-01-19 20:22:31 +00001769 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001770
Chris Lattner1fc3d752009-07-09 17:25:12 +00001771 // NOTE: a size of zero for a .comm should create a undefined symbol
1772 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001773 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001774 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1775 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001776
Eric Christopherc260a3e2010-05-14 01:38:54 +00001777 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001778 // may internally end up wanting an alignment in bytes.
1779 // FIXME: Diagnose overflow.
1780 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001781 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1782 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001783
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001784 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001785 return Error(IDLoc, "invalid symbol redefinition");
1786
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001787 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001788 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001789 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001790 getStreamer().EmitZerofill(Ctx.getMachOSection(
1791 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1792 0, SectionKind::getBSS()),
1793 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001794 return false;
1795 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001796
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001797 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001798 return false;
1799}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001800
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001801/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001802/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001803bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001804 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001805 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001806
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001807 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001808 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001809 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001810
Sean Callanan79ed1a82010-01-19 20:22:31 +00001811 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001812
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001813 if (Str.empty())
1814 Error(Loc, ".abort detected. Assembly stopping.");
1815 else
1816 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001817 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001818
1819 return false;
1820}
Kevin Enderby71148242009-07-14 21:35:03 +00001821
Kevin Enderby1f049b22009-07-14 23:21:55 +00001822/// ParseDirectiveInclude
1823/// ::= .include "filename"
1824bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001825 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001826 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001827
Sean Callanan18b83232010-01-19 21:44:56 +00001828 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001829 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001830 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001831
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001832 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001833 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001834
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001835 // Strip the quotes.
1836 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001837
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001838 // Attempt to switch the lexer to the included file before consuming the end
1839 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001840 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001841 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001842 return true;
1843 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001844
1845 return false;
1846}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001847
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001848/// ParseDirectiveIf
1849/// ::= .if expression
1850bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001851 TheCondStack.push_back(TheCondState);
1852 TheCondState.TheCond = AsmCond::IfCond;
1853 if(TheCondState.Ignore) {
1854 EatToEndOfStatement();
1855 }
1856 else {
1857 int64_t ExprValue;
1858 if (ParseAbsoluteExpression(ExprValue))
1859 return true;
1860
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001861 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001862 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001863
Sean Callanan79ed1a82010-01-19 20:22:31 +00001864 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001865
1866 TheCondState.CondMet = ExprValue;
1867 TheCondState.Ignore = !TheCondState.CondMet;
1868 }
1869
1870 return false;
1871}
1872
1873/// ParseDirectiveElseIf
1874/// ::= .elseif expression
1875bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1876 if (TheCondState.TheCond != AsmCond::IfCond &&
1877 TheCondState.TheCond != AsmCond::ElseIfCond)
1878 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1879 " an .elseif");
1880 TheCondState.TheCond = AsmCond::ElseIfCond;
1881
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001882 bool LastIgnoreState = false;
1883 if (!TheCondStack.empty())
1884 LastIgnoreState = TheCondStack.back().Ignore;
1885 if (LastIgnoreState || TheCondState.CondMet) {
1886 TheCondState.Ignore = true;
1887 EatToEndOfStatement();
1888 }
1889 else {
1890 int64_t ExprValue;
1891 if (ParseAbsoluteExpression(ExprValue))
1892 return true;
1893
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001894 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001895 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001896
Sean Callanan79ed1a82010-01-19 20:22:31 +00001897 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001898 TheCondState.CondMet = ExprValue;
1899 TheCondState.Ignore = !TheCondState.CondMet;
1900 }
1901
1902 return false;
1903}
1904
1905/// ParseDirectiveElse
1906/// ::= .else
1907bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001908 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001909 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001910
Sean Callanan79ed1a82010-01-19 20:22:31 +00001911 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001912
1913 if (TheCondState.TheCond != AsmCond::IfCond &&
1914 TheCondState.TheCond != AsmCond::ElseIfCond)
1915 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1916 ".elseif");
1917 TheCondState.TheCond = AsmCond::ElseCond;
1918 bool LastIgnoreState = false;
1919 if (!TheCondStack.empty())
1920 LastIgnoreState = TheCondStack.back().Ignore;
1921 if (LastIgnoreState || TheCondState.CondMet)
1922 TheCondState.Ignore = true;
1923 else
1924 TheCondState.Ignore = false;
1925
1926 return false;
1927}
1928
1929/// ParseDirectiveEndIf
1930/// ::= .endif
1931bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001932 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001933 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001934
Sean Callanan79ed1a82010-01-19 20:22:31 +00001935 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001936
1937 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1938 TheCondStack.empty())
1939 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1940 ".else");
1941 if (!TheCondStack.empty()) {
1942 TheCondState = TheCondStack.back();
1943 TheCondStack.pop_back();
1944 }
1945
1946 return false;
1947}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001948
1949/// ParseDirectiveFile
1950/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001951bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001952 // FIXME: I'm not sure what this is.
1953 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001954 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00001955 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001956 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001957 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001958
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001959 if (FileNumber < 1)
1960 return TokError("file number less than one");
1961 }
1962
Daniel Dunbareceec052010-07-12 17:45:27 +00001963 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001964 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001965
Chris Lattnerd32e8032010-01-25 19:02:58 +00001966 StringRef Filename = getTok().getString();
1967 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001968 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001969
Daniel Dunbareceec052010-07-12 17:45:27 +00001970 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001971 return TokError("unexpected token in '.file' directive");
1972
Chris Lattnerd32e8032010-01-25 19:02:58 +00001973 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001974 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001975 else {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001976 if (getContext().GetDwarfFile(Filename, FileNumber) == 0)
1977 Error(FileNumberLoc, "file number already allocated");
Daniel Dunbareceec052010-07-12 17:45:27 +00001978 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001979 }
Daniel Dunbareceec052010-07-12 17:45:27 +00001980
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001981 return false;
1982}
1983
1984/// ParseDirectiveLine
1985/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001986bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001987 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1988 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001989 return TokError("unexpected token in '.line' directive");
1990
Sean Callanan18b83232010-01-19 21:44:56 +00001991 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001992 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001993 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001994
1995 // FIXME: Do something with the .line.
1996 }
1997
Daniel Dunbareceec052010-07-12 17:45:27 +00001998 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001999 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002000
2001 return false;
2002}
2003
2004
2005/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002006/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002007/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2008/// The first number is a file number, must have been previously assigned with
2009/// a .file directive, the second number is the line number and optionally the
2010/// third number is a column position (zero if not specified). The remaining
2011/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002012bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002013
Daniel Dunbareceec052010-07-12 17:45:27 +00002014 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002015 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002016 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002017 if (FileNumber < 1)
2018 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002019 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002020 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002021 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002022
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002023 int64_t LineNumber = 0;
2024 if (getLexer().is(AsmToken::Integer)) {
2025 LineNumber = getTok().getIntVal();
2026 if (LineNumber < 1)
2027 return TokError("line number less than one in '.loc' directive");
2028 Lex();
2029 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002030
2031 int64_t ColumnPos = 0;
2032 if (getLexer().is(AsmToken::Integer)) {
2033 ColumnPos = getTok().getIntVal();
2034 if (ColumnPos < 0)
2035 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002036 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002037 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002038
Kevin Enderbyc0957932010-09-30 16:52:03 +00002039 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002040 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002041 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002042 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2043 for (;;) {
2044 if (getLexer().is(AsmToken::EndOfStatement))
2045 break;
2046
2047 StringRef Name;
2048 SMLoc Loc = getTok().getLoc();
2049 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002050 return TokError("unexpected token in '.loc' directive");
2051
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002052 if (Name == "basic_block")
2053 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2054 else if (Name == "prologue_end")
2055 Flags |= DWARF2_FLAG_PROLOGUE_END;
2056 else if (Name == "epilogue_begin")
2057 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2058 else if (Name == "is_stmt") {
2059 SMLoc Loc = getTok().getLoc();
2060 const MCExpr *Value;
2061 if (getParser().ParseExpression(Value))
2062 return true;
2063 // The expression must be the constant 0 or 1.
2064 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2065 int Value = MCE->getValue();
2066 if (Value == 0)
2067 Flags &= ~DWARF2_FLAG_IS_STMT;
2068 else if (Value == 1)
2069 Flags |= DWARF2_FLAG_IS_STMT;
2070 else
2071 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002072 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002073 else {
2074 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2075 }
2076 }
2077 else if (Name == "isa") {
2078 SMLoc Loc = getTok().getLoc();
2079 const MCExpr *Value;
2080 if (getParser().ParseExpression(Value))
2081 return true;
2082 // The expression must be a constant greater or equal to 0.
2083 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2084 int Value = MCE->getValue();
2085 if (Value < 0)
2086 return Error(Loc, "isa number less than zero");
2087 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002088 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002089 else {
2090 return Error(Loc, "isa number not a constant value");
2091 }
2092 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002093 else if (Name == "discriminator") {
2094 if (getParser().ParseAbsoluteExpression(Discriminator))
2095 return true;
2096 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002097 else {
2098 return Error(Loc, "unknown sub-directive in '.loc' directive");
2099 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002100
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002101 if (getLexer().is(AsmToken::EndOfStatement))
2102 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002103 }
2104 }
2105
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002106 getContext().setCurrentDwarfLoc(FileNumber, LineNumber, ColumnPos, Flags,
2107 Isa, Discriminator);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002108
2109 return false;
2110}
2111
Daniel Dunbar138abae2010-10-16 04:56:42 +00002112/// ParseDirectiveStabs
2113/// ::= .stabs string, number, number, number
2114bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2115 SMLoc DirectiveLoc) {
2116 return TokError("unsupported directive '" + Directive + "'");
2117}
2118
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002119/// ParseDirectiveMacrosOnOff
2120/// ::= .macros_on
2121/// ::= .macros_off
2122bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2123 SMLoc DirectiveLoc) {
2124 if (getLexer().isNot(AsmToken::EndOfStatement))
2125 return Error(getLexer().getLoc(),
2126 "unexpected token in '" + Directive + "' directive");
2127
2128 getParser().MacrosEnabled = Directive == ".macros_on";
2129
2130 return false;
2131}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002132
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002133/// ParseDirectiveMacro
2134/// ::= .macro name
2135bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2136 SMLoc DirectiveLoc) {
2137 StringRef Name;
2138 if (getParser().ParseIdentifier(Name))
2139 return TokError("expected identifier in directive");
2140
2141 if (getLexer().isNot(AsmToken::EndOfStatement))
2142 return TokError("unexpected token in '.macro' directive");
2143
2144 // Eat the end of statement.
2145 Lex();
2146
2147 AsmToken EndToken, StartToken = getTok();
2148
2149 // Lex the macro definition.
2150 for (;;) {
2151 // Check whether we have reached the end of the file.
2152 if (getLexer().is(AsmToken::Eof))
2153 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2154
2155 // Otherwise, check whether we have reach the .endmacro.
2156 if (getLexer().is(AsmToken::Identifier) &&
2157 (getTok().getIdentifier() == ".endm" ||
2158 getTok().getIdentifier() == ".endmacro")) {
2159 EndToken = getTok();
2160 Lex();
2161 if (getLexer().isNot(AsmToken::EndOfStatement))
2162 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2163 "' directive");
2164 break;
2165 }
2166
2167 // Otherwise, scan til the end of the statement.
2168 getParser().EatToEndOfStatement();
2169 }
2170
2171 if (getParser().MacroMap.lookup(Name)) {
2172 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2173 }
2174
2175 const char *BodyStart = StartToken.getLoc().getPointer();
2176 const char *BodyEnd = EndToken.getLoc().getPointer();
2177 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2178 getParser().MacroMap[Name] = new Macro(Name, Body);
2179 return false;
2180}
2181
2182/// ParseDirectiveEndMacro
2183/// ::= .endm
2184/// ::= .endmacro
2185bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2186 SMLoc DirectiveLoc) {
2187 if (getLexer().isNot(AsmToken::EndOfStatement))
2188 return TokError("unexpected token in '" + Directive + "' directive");
2189
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002190 // If we are inside a macro instantiation, terminate the current
2191 // instantiation.
2192 if (!getParser().ActiveMacros.empty()) {
2193 getParser().HandleMacroExit();
2194 return false;
2195 }
2196
2197 // Otherwise, this .endmacro is a stray entry in the file; well formed
2198 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002199 return TokError("unexpected '" + Directive + "' in file, "
2200 "no current macro definition");
2201}
2202
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002203bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002204 getParser().CheckForValidSection();
2205
2206 const MCExpr *Value;
2207
2208 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002209 return true;
2210
2211 if (getLexer().isNot(AsmToken::EndOfStatement))
2212 return TokError("unexpected token in directive");
2213
2214 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002215 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002216 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002217 getStreamer().EmitULEB128Value(Value);
2218
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002219 return false;
2220}
2221
2222
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002223/// \brief Create an MCAsmParser instance.
2224MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2225 MCContext &C, MCStreamer &Out,
2226 const MCAsmInfo &MAI) {
2227 return new AsmParser(T, SM, C, Out, MAI);
2228}