blob: 66f46a877d1f73a9d881b23ca13eb829dc108f5a [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000014#include "llvm/ADT/SmallString.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000015#include "llvm/ADT/StringMap.h"
Matt Fleming924c5e52010-05-21 11:36:59 +000016#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000018#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000019#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000020#include "llvm/MC/MCExpr.h"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000021#include "llvm/MC/MCInst.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000022#include "llvm/MC/MCParser/AsmCond.h"
23#include "llvm/MC/MCParser/AsmLexer.h"
24#include "llvm/MC/MCParser/MCAsmParser.h"
25#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
26#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000027#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000028#include "llvm/MC/MCSymbol.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000029#include "llvm/Support/Compiler.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000030#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000031#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000032#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000033#include "llvm/Target/TargetAsmParser.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000034#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000035using namespace llvm;
36
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000037namespace {
38
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000039/// \brief Helper class for tracking macro definitions.
40struct Macro {
41 StringRef Name;
42 StringRef Body;
43
44public:
45 Macro(StringRef N, StringRef B) : Name(N), Body(B) {}
46};
47
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000048/// \brief Helper class for storing information about an active macro
49/// instantiation.
50struct MacroInstantiation {
51 /// The macro being instantiated.
52 const Macro *TheMacro;
53
54 /// The macro instantiation with substitutions.
55 MemoryBuffer *Instantiation;
56
57 /// The location of the instantiation.
58 SMLoc InstantiationLoc;
59
60 /// The location where parsing should resume upon instantiation completion.
61 SMLoc ExitLoc;
62
63public:
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 Dunbaraef87e32010-07-18 18:31:38 +0000104public:
105 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
106 const MCAsmInfo &MAI);
107 ~AsmParser();
108
109 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
110
111 void AddDirectiveHandler(MCAsmParserExtension *Object,
112 StringRef Directive,
113 DirectiveHandler Handler) {
114 DirectiveMap[Directive] = std::make_pair(Object, Handler);
115 }
116
117public:
118 /// @name MCAsmParser Interface
119 /// {
120
121 virtual SourceMgr &getSourceManager() { return SrcMgr; }
122 virtual MCAsmLexer &getLexer() { return Lexer; }
123 virtual MCContext &getContext() { return Ctx; }
124 virtual MCStreamer &getStreamer() { return Out; }
125
126 virtual void Warning(SMLoc L, const Twine &Meg);
127 virtual bool Error(SMLoc L, const Twine &Msg);
128
129 const AsmToken &Lex();
130
131 bool ParseExpression(const MCExpr *&Res);
132 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
133 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
134 virtual bool ParseAbsoluteExpression(int64_t &Res);
135
136 /// }
137
138private:
139 bool ParseStatement();
140
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000141 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
142 void HandleMacroExit();
143
144 void PrintMacroInstantiations();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000145 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
146
147 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
148 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000149
150 /// \brief Reset the current lexer position to that given by \arg Loc. The
151 /// current token is not set; clients should ensure Lex() is called
152 /// subsequently.
153 void JumpToLoc(SMLoc Loc);
154
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000155 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000156
157 /// \brief Parse up to the end of statement and a return the contents from the
158 /// current token until the end of the statement; the current token on exit
159 /// will be either the EndOfStatement or EOF.
160 StringRef ParseStringToEndOfStatement();
161
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000162 bool ParseAssignment(StringRef Name);
163
164 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
165 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
166 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
167
168 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
169 /// and set \arg Res to the identifier contents.
170 bool ParseIdentifier(StringRef &Res);
171
172 // Directive Parsing.
173 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
174 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
175 bool ParseDirectiveFill(); // ".fill"
176 bool ParseDirectiveSpace(); // ".space"
177 bool ParseDirectiveSet(); // ".set"
178 bool ParseDirectiveOrg(); // ".org"
179 // ".align{,32}", ".p2align{,w,l}"
180 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
181
182 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
183 /// accepts a single symbol (which should be a label or an external).
184 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
185 bool ParseDirectiveELFType(); // ELF specific ".type"
186
187 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
188
189 bool ParseDirectiveAbort(); // ".abort"
190 bool ParseDirectiveInclude(); // ".include"
191
192 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
193 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
194 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
195 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
196
197 /// ParseEscapedString - Parse the current token as a string which may include
198 /// escaped characters and return the string contents.
199 bool ParseEscapedString(std::string &Data);
200};
201
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000202/// \brief Generic implementations of directive handling, etc. which is shared
203/// (or the default, at least) for all assembler parser.
204class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000205 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
206 void AddDirectiveHandler(StringRef Directive) {
207 getParser().AddDirectiveHandler(this, Directive,
208 HandleDirective<GenericAsmParser, Handler>);
209 }
210
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000211public:
212 GenericAsmParser() {}
213
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000214 AsmParser &getParser() {
215 return (AsmParser&) this->MCAsmParserExtension::getParser();
216 }
217
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000218 virtual void Initialize(MCAsmParser &Parser) {
219 // Call the base implementation.
220 this->MCAsmParserExtension::Initialize(Parser);
221
222 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000223 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
224 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
225 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000226
227 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000228 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
229 ".macros_on");
230 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
231 ".macros_off");
232 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
233 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
234 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000235 }
236
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000237 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
238 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
239 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000240
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000241 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000242 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
243 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000244};
245
246}
247
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000248namespace llvm {
249
250extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000251extern MCAsmParserExtension *createELFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000252
253}
254
Chris Lattneraaec2052010-01-19 19:46:13 +0000255enum { DEFAULT_ADDRSPACE = 0 };
256
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000257AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
258 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000259 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000260 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000261 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000262 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000263
264 // Initialize the generic parser.
265 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000266
267 // Initialize the platform / file format parser.
268 //
269 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
270 // created.
271 if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000272 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000273 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000274 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000275 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000276 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000277 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000278}
279
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000280AsmParser::~AsmParser() {
Daniel Dunbare4749702010-07-12 18:12:02 +0000281 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000282 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000283}
284
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000285void AsmParser::PrintMacroInstantiations() {
286 // Print the active macro instantiation stack.
287 for (std::vector<MacroInstantiation*>::const_reverse_iterator
288 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
289 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
290 "note");
291}
292
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000293void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000294 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000295 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000296}
297
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000298bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000299 PrintMessage(L, Msg.str(), "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000300 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000301 return true;
302}
303
Sean Callananbf2013e2010-01-20 23:19:55 +0000304void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
305 const char *Type) const {
306 SrcMgr.PrintMessage(Loc, Msg, Type);
307}
Sean Callananfd0b0282010-01-21 00:19:58 +0000308
309bool AsmParser::EnterIncludeFile(const std::string &Filename) {
310 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
311 if (NewBuf == -1)
312 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000313
Sean Callananfd0b0282010-01-21 00:19:58 +0000314 CurBuffer = NewBuf;
315
316 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
317
318 return false;
319}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000320
321void AsmParser::JumpToLoc(SMLoc Loc) {
322 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
323 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
324}
325
Sean Callananfd0b0282010-01-21 00:19:58 +0000326const AsmToken &AsmParser::Lex() {
327 const AsmToken *tok = &Lexer.Lex();
328
329 if (tok->is(AsmToken::Eof)) {
330 // If this is the end of an included file, pop the parent file off the
331 // include stack.
332 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
333 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000334 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000335 tok = &Lexer.Lex();
336 }
337 }
338
339 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000340 Error(Lexer.getErrLoc(), Lexer.getErr());
Sean Callanan79036e42010-01-20 22:18:24 +0000341
Sean Callananfd0b0282010-01-21 00:19:58 +0000342 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000343}
344
Chris Lattner79180e22010-04-05 23:15:42 +0000345bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000346 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000347 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000348 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000349 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000350 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000351 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
352 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000353
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000354 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000355 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000356
Chris Lattnerb717fb02009-07-02 21:53:43 +0000357 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000358
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000359 AsmCond StartingCondState = TheCondState;
360
Chris Lattnerb717fb02009-07-02 21:53:43 +0000361 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000362 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000363 if (!ParseStatement()) continue;
364
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000365 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000366 HadError = true;
367 EatToEndOfStatement();
368 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000369
370 if (TheCondState.TheCond != StartingCondState.TheCond ||
371 TheCondState.Ignore != StartingCondState.Ignore)
372 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000373
Chris Lattner79180e22010-04-05 23:15:42 +0000374 // Finalize the output stream if there are no errors and if the client wants
375 // us to.
376 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000377 Out.Finish();
378
Chris Lattnerb717fb02009-07-02 21:53:43 +0000379 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000380}
381
Chris Lattner2cf5f142009-06-22 01:29:09 +0000382/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
383void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000384 while (Lexer.isNot(AsmToken::EndOfStatement) &&
385 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000386 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000387
388 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000389 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000390 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000391}
392
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000393StringRef AsmParser::ParseStringToEndOfStatement() {
394 const char *Start = getTok().getLoc().getPointer();
395
396 while (Lexer.isNot(AsmToken::EndOfStatement) &&
397 Lexer.isNot(AsmToken::Eof))
398 Lex();
399
400 const char *End = getTok().getLoc().getPointer();
401 return StringRef(Start, End - Start);
402}
Chris Lattnerc4193832009-06-22 05:51:26 +0000403
Chris Lattner74ec1a32009-06-22 06:32:03 +0000404/// ParseParenExpr - Parse a paren expression and return it.
405/// NOTE: This assumes the leading '(' has already been consumed.
406///
407/// parenexpr ::= expr)
408///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000409bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000410 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000411 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000412 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000413 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000414 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000415 return false;
416}
Chris Lattnerc4193832009-06-22 05:51:26 +0000417
Chris Lattner74ec1a32009-06-22 06:32:03 +0000418/// ParsePrimaryExpr - Parse a primary expression and return it.
419/// primaryexpr ::= (parenexpr
420/// primaryexpr ::= symbol
421/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000422/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000423/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000424bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000425 switch (Lexer.getKind()) {
426 default:
427 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000428 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000429 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000430 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000431 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000432 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000433 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000434 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000435 case AsmToken::Identifier: {
436 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000437 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000438 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000439
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000440 // Mark the symbol as used in an expression.
441 Sym->setUsedInExpr(true);
442
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000443 // Lookup the symbol variant if used.
444 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
445 if (Split.first.size() != getTok().getIdentifier().size())
446 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
447
Chris Lattnerb4307b32010-01-15 19:28:38 +0000448 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000449 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000450
451 // If this is an absolute variable reference, substitute it now to preserve
452 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000453 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000454 if (Variant)
455 return Error(EndLoc, "unexpected modified on variable reference");
456
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000457 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000458 return false;
459 }
460
461 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000462 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000463 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000464 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000465 case AsmToken::Integer: {
466 SMLoc Loc = getTok().getLoc();
467 int64_t IntVal = getTok().getIntVal();
468 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000469 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000470 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000471 // Look for 'b' or 'f' following an Integer as a directional label
472 if (Lexer.getKind() == AsmToken::Identifier) {
473 StringRef IDVal = getTok().getString();
474 if (IDVal == "f" || IDVal == "b"){
475 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
476 IDVal == "f" ? 1 : 0);
477 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
478 getContext());
479 if(IDVal == "b" && Sym->isUndefined())
480 return Error(Loc, "invalid reference to undefined symbol");
481 EndLoc = Lexer.getLoc();
482 Lex(); // Eat identifier.
483 }
484 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000485 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000486 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000487 case AsmToken::Dot: {
488 // This is a '.' reference, which references the current PC. Emit a
489 // temporary label to the streamer and refer to it.
490 MCSymbol *Sym = Ctx.CreateTempSymbol();
491 Out.EmitLabel(Sym);
492 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
493 EndLoc = Lexer.getLoc();
494 Lex(); // Eat identifier.
495 return false;
496 }
497
Daniel Dunbar3f872332009-07-28 16:08:33 +0000498 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000499 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000500 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000501 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000502 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000503 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000504 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000505 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000506 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000507 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000508 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000509 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000510 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000511 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000512 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000513 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000514 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000515 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000516 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000517 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000518 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000519 }
520}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000521
Chris Lattnerb4307b32010-01-15 19:28:38 +0000522bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000523 SMLoc EndLoc;
524 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000525}
526
Chris Lattner74ec1a32009-06-22 06:32:03 +0000527/// ParseExpression - Parse an expression and return it.
528///
529/// expr ::= expr +,- expr -> lowest.
530/// expr ::= expr |,^,&,! expr -> middle.
531/// expr ::= expr *,/,%,<<,>> expr -> highest.
532/// expr ::= primaryexpr
533///
Chris Lattner54482b42010-01-15 19:39:23 +0000534bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000535 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000536 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000537 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
538 return true;
539
540 // Try to constant fold it up front, if possible.
541 int64_t Value;
542 if (Res->EvaluateAsAbsolute(Value))
543 Res = MCConstantExpr::Create(Value, getContext());
544
545 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000546}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000547
Chris Lattnerb4307b32010-01-15 19:28:38 +0000548bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000549 Res = 0;
550 return ParseParenExpr(Res, EndLoc) ||
551 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000552}
553
Daniel Dunbar475839e2009-06-29 20:37:27 +0000554bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000555 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000556
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000557 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000558 if (ParseExpression(Expr))
559 return true;
560
Daniel Dunbare00b0112009-10-16 01:57:52 +0000561 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000562 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000563
564 return false;
565}
566
Daniel Dunbar3f872332009-07-28 16:08:33 +0000567static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000568 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000569 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000570 default:
571 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000572
573 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000574 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000575 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000576 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000577 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000578 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000579 return 1;
580
581 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000582 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000583 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000584 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000585 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000586 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000587 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000588 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000589 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000590 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000591 case AsmToken::ExclaimEqual:
592 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000593 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000594 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000595 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000596 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000597 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000598 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000599 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000600 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000601 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000602 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000603 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000604 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000605 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000606 return 2;
607
608 // Intermediate Precedence: |, &, ^
609 //
610 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000611 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000612 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000613 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000614 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000615 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000616 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000617 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000618 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000619 return 3;
620
621 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000622 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000623 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000624 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000625 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000626 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000627 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000628 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000629 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000630 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000631 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000632 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000633 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000634 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000635 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000636 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000637 }
638}
639
640
641/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
642/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000643bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
644 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000645 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000646 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000647 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000648
649 // If the next token is lower precedence than we are allowed to eat, return
650 // successfully with what we ate already.
651 if (TokPrec < Precedence)
652 return false;
653
Sean Callanan79ed1a82010-01-19 20:22:31 +0000654 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000655
656 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000657 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000658 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000659
660 // If BinOp binds less tightly with RHS than the operator after RHS, let
661 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000662 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000663 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000664 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000665 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000666 }
667
Daniel Dunbar475839e2009-06-29 20:37:27 +0000668 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000669 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000670 }
671}
672
Chris Lattnerc4193832009-06-22 05:51:26 +0000673
674
675
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000676/// ParseStatement:
677/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000678/// ::= Label* Directive ...Operands... EndOfStatement
679/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000680bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000681 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000682 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000683 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000684 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000685 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000686
687 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000688 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000689 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000690 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000691 int64_t LocalLabelVal = -1;
692 // GUESS allow an integer followed by a ':' as a directional local label
693 if (Lexer.is(AsmToken::Integer)) {
694 LocalLabelVal = getTok().getIntVal();
695 if (LocalLabelVal < 0) {
696 if (!TheCondState.Ignore)
697 return TokError("unexpected token at start of statement");
698 IDVal = "";
699 }
700 else {
701 IDVal = getTok().getString();
702 Lex(); // Consume the integer token to be used as an identifier token.
703 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000704 if (!TheCondState.Ignore)
705 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000706 }
707 }
708 }
709 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000710 if (!TheCondState.Ignore)
711 return TokError("unexpected token at start of statement");
712 IDVal = "";
713 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000714
Chris Lattner7834fac2010-04-17 18:14:27 +0000715 // Handle conditional assembly here before checking for skipping. We
716 // have to do this so that .endif isn't skipped in a ".if 0" block for
717 // example.
718 if (IDVal == ".if")
719 return ParseDirectiveIf(IDLoc);
720 if (IDVal == ".elseif")
721 return ParseDirectiveElseIf(IDLoc);
722 if (IDVal == ".else")
723 return ParseDirectiveElse(IDLoc);
724 if (IDVal == ".endif")
725 return ParseDirectiveEndIf(IDLoc);
726
727 // If we are in a ".if 0" block, ignore this statement.
728 if (TheCondState.Ignore) {
729 EatToEndOfStatement();
730 return false;
731 }
732
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000733 // FIXME: Recurse on local labels?
734
735 // See what kind of statement we have.
736 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000737 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000738 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000739 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000740
741 // Diagnose attempt to use a variable as a label.
742 //
743 // FIXME: Diagnostics. Note the location of the definition as a label.
744 // FIXME: This doesn't diagnose assignment to a symbol which has been
745 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000746 MCSymbol *Sym;
747 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000748 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000749 else
750 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000751 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000752 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000753
Daniel Dunbar959fd882009-08-26 22:13:22 +0000754 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000755 Out.EmitLabel(Sym);
756
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000757 // Consume any end of statement token, if present, to avoid spurious
758 // AddBlankLine calls().
759 if (Lexer.is(AsmToken::EndOfStatement)) {
760 Lex();
761 if (Lexer.is(AsmToken::Eof))
762 return false;
763 }
764
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000765 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000766 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000767
Daniel Dunbar3f872332009-07-28 16:08:33 +0000768 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000769 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000770 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000771
Daniel Dunbare2ace502009-08-31 08:09:09 +0000772 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000773
774 default: // Normal instruction or directive.
775 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000776 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000777
778 // If macros are enabled, check to see if this is a macro instantiation.
779 if (MacrosEnabled)
780 if (const Macro *M = MacroMap.lookup(IDVal))
781 return HandleMacroEntry(IDVal, IDLoc, M);
782
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000783 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000784 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000785 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000786 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000787 return ParseDirectiveSet();
788
Daniel Dunbara0d14262009-06-24 23:30:00 +0000789 // Data directives
790
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000791 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000792 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000793 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000794 return ParseDirectiveAscii(true);
795
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000796 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000797 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000798 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000799 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000800 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000801 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000802 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000803 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000804
805 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000806 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000807 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000808 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000809 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000810 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000811 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000812 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000813 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000814 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000815 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000816 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000817 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000818 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000819 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000820 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000821 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
822
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000823 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000824 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000825
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000826 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000827 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000828 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000829 return ParseDirectiveSpace();
830
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000831 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000832
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000833 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000834 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000835 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000836 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000837 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000838 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000839 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000840 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000841 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000842 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000843 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000844 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000845 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000846 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000847 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000848 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000849 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000850 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000851 if (IDVal == ".type")
852 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000853 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000854 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000855 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000856 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000857 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000858 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000859 if (IDVal == ".weak_def_can_be_hidden")
860 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000861
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000862 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000863 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000864 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000865 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000866
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000867 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000868 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000869 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000870 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000871
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000872 // Look up the handler in the handler table.
873 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
874 DirectiveMap.lookup(IDVal);
875 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000876 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000877
Kevin Enderby9c656452009-09-10 20:51:44 +0000878 // Target hook for parsing target specific directives.
879 if (!getTargetParser().ParseDirective(ID))
880 return false;
881
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000882 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000883 EatToEndOfStatement();
884 return false;
885 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000886
Chris Lattnera7f13542010-05-19 23:34:33 +0000887 // Canonicalize the opcode to lower case.
888 SmallString<128> Opcode;
889 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
890 Opcode.push_back(tolower(IDVal[i]));
891
Chris Lattner98986712010-01-14 22:21:20 +0000892 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000893 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000894 ParsedOperands);
895 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
896 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000897
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000898 // If parsing succeeded, match the instruction.
899 if (!HadError) {
900 MCInst Inst;
901 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
902 // Emit the instruction on success.
903 Out.EmitInstruction(Inst);
904 } else {
905 // Otherwise emit a diagnostic about the match failure and set the error
906 // flag.
907 //
908 // FIXME: We should give nicer diagnostics about the exact failure.
909 Error(IDLoc, "unrecognized instruction");
910 HadError = true;
911 }
912 }
Chris Lattner98986712010-01-14 22:21:20 +0000913
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000914 // If there was no error, consume the end-of-statement token. Otherwise this
915 // will be done by our caller.
916 if (!HadError)
917 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000918
919 // Free any parsed operands.
920 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
921 delete ParsedOperands[i];
922
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000923 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000924}
Chris Lattner9a023f72009-06-24 04:43:34 +0000925
Daniel Dunbar7a570d02010-07-18 19:00:10 +0000926MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
927 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000928 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
929{
930 // Macro instantiation is lexical, unfortunately. We construct a new buffer
931 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +0000932 SmallString<256> Buf;
933 raw_svector_ostream OS(Buf);
934
935 StringRef Body = M->Body;
936 while (!Body.empty()) {
937 // Scan for the next substitution.
938 std::size_t End = Body.size(), Pos = 0;
939 for (; Pos != End; ++Pos) {
940 // Check for a substitution or escape.
941 if (Body[Pos] != '$' || Pos + 1 == End)
942 continue;
943
944 char Next = Body[Pos + 1];
945 if (Next == '$' || Next == 'n' || isdigit(Next))
946 break;
947 }
948
949 // Add the prefix.
950 OS << Body.slice(0, Pos);
951
952 // Check if we reached the end.
953 if (Pos == End)
954 break;
955
956 switch (Body[Pos+1]) {
957 // $$ => $
958 case '$':
959 OS << '$';
960 break;
961
962 // $n => number of arguments
963 case 'n':
964 OS << A.size();
965 break;
966
967 // $[0-9] => argument
968 default: {
969 // Missing arguments are ignored.
970 unsigned Index = Body[Pos+1] - '0';
971 if (Index >= A.size())
972 break;
973
974 // Otherwise substitute with the token values, with spaces eliminated.
975 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
976 ie = A[Index].end(); it != ie; ++it)
977 OS << it->getString();
978 break;
979 }
980 }
981
982 // Update the scan point.
983 Body = Body.substr(Pos + 2);
984 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000985
986 // We include the .endmacro in the buffer as our queue to exit the macro
987 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +0000988 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000989
Daniel Dunbar7a570d02010-07-18 19:00:10 +0000990 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000991}
992
993bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
994 const Macro *M) {
995 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
996 // this, although we should protect against infinite loops.
997 if (ActiveMacros.size() == 20)
998 return TokError("macros cannot be nested more than 20 levels deep");
999
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001000 // Parse the macro instantiation arguments.
1001 std::vector<std::vector<AsmToken> > MacroArguments;
1002 MacroArguments.push_back(std::vector<AsmToken>());
1003 unsigned ParenLevel = 0;
1004 for (;;) {
1005 if (Lexer.is(AsmToken::Eof))
1006 return TokError("unexpected token in macro instantiation");
1007 if (Lexer.is(AsmToken::EndOfStatement))
1008 break;
1009
1010 // If we aren't inside parentheses and this is a comma, start a new token
1011 // list.
1012 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1013 MacroArguments.push_back(std::vector<AsmToken>());
1014 } else if (Lexer.is(AsmToken::LParen)) {
1015 ++ParenLevel;
1016 } else if (Lexer.is(AsmToken::RParen)) {
1017 if (ParenLevel)
1018 --ParenLevel;
1019 } else {
1020 MacroArguments.back().push_back(getTok());
1021 }
1022 Lex();
1023 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001024
1025 // Create the macro instantiation object and add to the current macro
1026 // instantiation stack.
1027 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001028 getTok().getLoc(),
1029 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001030 ActiveMacros.push_back(MI);
1031
1032 // Jump to the macro instantiation and prime the lexer.
1033 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1034 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1035 Lex();
1036
1037 return false;
1038}
1039
1040void AsmParser::HandleMacroExit() {
1041 // Jump to the EndOfStatement we should return to, and consume it.
1042 JumpToLoc(ActiveMacros.back()->ExitLoc);
1043 Lex();
1044
1045 // Pop the instantiation entry.
1046 delete ActiveMacros.back();
1047 ActiveMacros.pop_back();
1048}
1049
Benjamin Kramer38e59892010-07-14 22:38:02 +00001050bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001051 // FIXME: Use better location, we should use proper tokens.
1052 SMLoc EqualLoc = Lexer.getLoc();
1053
Daniel Dunbar821e3332009-08-31 08:09:28 +00001054 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001055 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001056 return true;
1057
Daniel Dunbar3f872332009-07-28 16:08:33 +00001058 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001059 return TokError("unexpected token in assignment");
1060
1061 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001062 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001063
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001064 // Validate that the LHS is allowed to be a variable (either it has not been
1065 // used as a symbol, or it is an absolute symbol).
1066 MCSymbol *Sym = getContext().LookupSymbol(Name);
1067 if (Sym) {
1068 // Diagnose assignment to a label.
1069 //
1070 // FIXME: Diagnostics. Note the location of the definition as a label.
1071 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001072 if (Sym->isUndefined() && !Sym->isUsedInExpr())
1073 ; // Allow redefinitions of undefined symbols only used in directives.
1074 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001075 return Error(EqualLoc, "redefinition of '" + Name + "'");
1076 else if (!Sym->isVariable())
1077 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001078 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001079 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1080 Name + "'");
1081 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001082 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001083
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001084 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001085
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001086 Sym->setUsedInExpr(true);
1087
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001088 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001089 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001090
1091 return false;
1092}
1093
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001094/// ParseIdentifier:
1095/// ::= identifier
1096/// ::= string
1097bool AsmParser::ParseIdentifier(StringRef &Res) {
1098 if (Lexer.isNot(AsmToken::Identifier) &&
1099 Lexer.isNot(AsmToken::String))
1100 return true;
1101
Sean Callanan18b83232010-01-19 21:44:56 +00001102 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001103
Sean Callanan79ed1a82010-01-19 20:22:31 +00001104 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001105
1106 return false;
1107}
1108
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001109/// ParseDirectiveSet:
1110/// ::= .set identifier ',' expression
1111bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001112 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001113
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001114 if (ParseIdentifier(Name))
1115 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001116
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001117 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001118 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001119 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001120
Daniel Dunbare2ace502009-08-31 08:09:09 +00001121 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001122}
1123
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001124bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001125 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001126
1127 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001128 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001129 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1130 if (Str[i] != '\\') {
1131 Data += Str[i];
1132 continue;
1133 }
1134
1135 // Recognize escaped characters. Note that this escape semantics currently
1136 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1137 ++i;
1138 if (i == e)
1139 return TokError("unexpected backslash at end of string");
1140
1141 // Recognize octal sequences.
1142 if ((unsigned) (Str[i] - '0') <= 7) {
1143 // Consume up to three octal characters.
1144 unsigned Value = Str[i] - '0';
1145
1146 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1147 ++i;
1148 Value = Value * 8 + (Str[i] - '0');
1149
1150 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1151 ++i;
1152 Value = Value * 8 + (Str[i] - '0');
1153 }
1154 }
1155
1156 if (Value > 255)
1157 return TokError("invalid octal escape sequence (out of range)");
1158
1159 Data += (unsigned char) Value;
1160 continue;
1161 }
1162
1163 // Otherwise recognize individual escapes.
1164 switch (Str[i]) {
1165 default:
1166 // Just reject invalid escape sequences for now.
1167 return TokError("invalid escape sequence (unrecognized character)");
1168
1169 case 'b': Data += '\b'; break;
1170 case 'f': Data += '\f'; break;
1171 case 'n': Data += '\n'; break;
1172 case 'r': Data += '\r'; break;
1173 case 't': Data += '\t'; break;
1174 case '"': Data += '"'; break;
1175 case '\\': Data += '\\'; break;
1176 }
1177 }
1178
1179 return false;
1180}
1181
Daniel Dunbara0d14262009-06-24 23:30:00 +00001182/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001183/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001184bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001185 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001186 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001187 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001188 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001189
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001190 std::string Data;
1191 if (ParseEscapedString(Data))
1192 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001193
1194 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001195 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001196 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1197
Sean Callanan79ed1a82010-01-19 20:22:31 +00001198 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001199
1200 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001201 break;
1202
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001203 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001204 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001205 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001206 }
1207 }
1208
Sean Callanan79ed1a82010-01-19 20:22:31 +00001209 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001210 return false;
1211}
1212
1213/// ParseDirectiveValue
1214/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1215bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001216 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001217 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001218 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001219 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001220 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001221 return true;
1222
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001223 // Special case constant expressions to match code generator.
1224 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001225 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001226 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001227 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001228
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001229 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001230 break;
1231
1232 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001233 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001234 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001235 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001236 }
1237 }
1238
Sean Callanan79ed1a82010-01-19 20:22:31 +00001239 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001240 return false;
1241}
1242
1243/// ParseDirectiveSpace
1244/// ::= .space expression [ , expression ]
1245bool AsmParser::ParseDirectiveSpace() {
1246 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001247 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001248 return true;
1249
1250 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001251 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1252 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001253 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001254 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001255
Daniel Dunbar475839e2009-06-29 20:37:27 +00001256 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001257 return true;
1258
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001259 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001260 return TokError("unexpected token in '.space' directive");
1261 }
1262
Sean Callanan79ed1a82010-01-19 20:22:31 +00001263 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001264
1265 if (NumBytes <= 0)
1266 return TokError("invalid number of bytes in '.space' directive");
1267
1268 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001269 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001270
1271 return false;
1272}
1273
1274/// ParseDirectiveFill
1275/// ::= .fill expression , expression , expression
1276bool AsmParser::ParseDirectiveFill() {
1277 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001278 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001279 return true;
1280
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001281 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001282 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001283 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001284
1285 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001286 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001287 return true;
1288
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001289 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001290 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001291 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001292
1293 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001294 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001295 return true;
1296
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001297 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001298 return TokError("unexpected token in '.fill' directive");
1299
Sean Callanan79ed1a82010-01-19 20:22:31 +00001300 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001301
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001302 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1303 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001304
1305 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001306 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001307
1308 return false;
1309}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001310
1311/// ParseDirectiveOrg
1312/// ::= .org expression [ , expression ]
1313bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001314 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001315 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001316 return true;
1317
1318 // Parse optional fill expression.
1319 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001320 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1321 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001322 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001323 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001324
Daniel Dunbar475839e2009-06-29 20:37:27 +00001325 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001326 return true;
1327
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001328 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001329 return TokError("unexpected token in '.org' directive");
1330 }
1331
Sean Callanan79ed1a82010-01-19 20:22:31 +00001332 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001333
1334 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1335 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001336 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001337
1338 return false;
1339}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001340
1341/// ParseDirectiveAlign
1342/// ::= {.align, ...} expression [ , expression [ , expression ]]
1343bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001344 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001345 int64_t Alignment;
1346 if (ParseAbsoluteExpression(Alignment))
1347 return true;
1348
1349 SMLoc MaxBytesLoc;
1350 bool HasFillExpr = false;
1351 int64_t FillExpr = 0;
1352 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001353 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1354 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001355 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001356 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001357
1358 // The fill expression can be omitted while specifying a maximum number of
1359 // alignment bytes, e.g:
1360 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001361 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001362 HasFillExpr = true;
1363 if (ParseAbsoluteExpression(FillExpr))
1364 return true;
1365 }
1366
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001367 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1368 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001369 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001370 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001371
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001372 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001373 if (ParseAbsoluteExpression(MaxBytesToFill))
1374 return true;
1375
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001376 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001377 return TokError("unexpected token in directive");
1378 }
1379 }
1380
Sean Callanan79ed1a82010-01-19 20:22:31 +00001381 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001382
Daniel Dunbar648ac512010-05-17 21:54:30 +00001383 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001384 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001385
1386 // Compute alignment in bytes.
1387 if (IsPow2) {
1388 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001389 if (Alignment >= 32) {
1390 Error(AlignmentLoc, "invalid alignment value");
1391 Alignment = 31;
1392 }
1393
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001394 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001395 }
1396
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001397 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001398 if (MaxBytesLoc.isValid()) {
1399 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001400 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1401 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001402 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001403 }
1404
1405 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001406 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1407 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001408 MaxBytesToFill = 0;
1409 }
1410 }
1411
Daniel Dunbar648ac512010-05-17 21:54:30 +00001412 // Check whether we should use optimal code alignment for this .align
1413 // directive.
1414 //
1415 // FIXME: This should be using a target hook.
1416 bool UseCodeAlign = false;
1417 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001418 getStreamer().getCurrentSection()))
Chris Lattnera9558532010-07-15 21:19:31 +00001419 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001420 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1421 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001422 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001423 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001424 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001425 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1426 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001427 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001428
1429 return false;
1430}
1431
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001432/// ParseDirectiveSymbolAttribute
1433/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001434bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001435 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001436 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001437 StringRef Name;
1438
1439 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001440 return TokError("expected identifier in directive");
1441
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001442 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001443
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001444 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001445
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001446 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001447 break;
1448
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001449 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001450 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001451 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001452 }
1453 }
1454
Sean Callanan79ed1a82010-01-19 20:22:31 +00001455 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001456 return false;
1457}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001458
Matt Fleming924c5e52010-05-21 11:36:59 +00001459/// ParseDirectiveELFType
1460/// ::= .type identifier , @attribute
1461bool AsmParser::ParseDirectiveELFType() {
1462 StringRef Name;
1463 if (ParseIdentifier(Name))
1464 return TokError("expected identifier in directive");
1465
1466 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001467 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Matt Fleming924c5e52010-05-21 11:36:59 +00001468
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001469 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001470 return TokError("unexpected token in '.type' directive");
1471 Lex();
1472
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001473 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001474 return TokError("expected '@' before type");
1475 Lex();
1476
1477 StringRef Type;
1478 SMLoc TypeLoc;
1479
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001480 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001481 if (ParseIdentifier(Type))
1482 return TokError("expected symbol type in directive");
1483
1484 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1485 .Case("function", MCSA_ELF_TypeFunction)
1486 .Case("object", MCSA_ELF_TypeObject)
1487 .Case("tls_object", MCSA_ELF_TypeTLS)
1488 .Case("common", MCSA_ELF_TypeCommon)
1489 .Case("notype", MCSA_ELF_TypeNoType)
1490 .Default(MCSA_Invalid);
1491
1492 if (Attr == MCSA_Invalid)
1493 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1494
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001495 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001496 return TokError("unexpected token in '.type' directive");
1497
1498 Lex();
1499
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001500 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001501
1502 return false;
1503}
1504
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001505/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001506/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1507bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001508 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001509 StringRef Name;
1510 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001511 return TokError("expected identifier in directive");
1512
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001513 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001514 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001515
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001516 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001517 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001518 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001519
1520 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001521 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001522 if (ParseAbsoluteExpression(Size))
1523 return true;
1524
1525 int64_t Pow2Alignment = 0;
1526 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001527 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001528 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001529 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001530 if (ParseAbsoluteExpression(Pow2Alignment))
1531 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001532
1533 // If this target takes alignments in bytes (not log) validate and convert.
1534 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1535 if (!isPowerOf2_64(Pow2Alignment))
1536 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1537 Pow2Alignment = Log2_64(Pow2Alignment);
1538 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001539 }
1540
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001541 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001542 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001543
Sean Callanan79ed1a82010-01-19 20:22:31 +00001544 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001545
Chris Lattner1fc3d752009-07-09 17:25:12 +00001546 // NOTE: a size of zero for a .comm should create a undefined symbol
1547 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001548 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001549 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1550 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001551
Eric Christopherc260a3e2010-05-14 01:38:54 +00001552 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001553 // may internally end up wanting an alignment in bytes.
1554 // FIXME: Diagnose overflow.
1555 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001556 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1557 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001558
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001559 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001560 return Error(IDLoc, "invalid symbol redefinition");
1561
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001562 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001563 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001564 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001565 getStreamer().EmitZerofill(Ctx.getMachOSection(
1566 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1567 0, SectionKind::getBSS()),
1568 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001569 return false;
1570 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001571
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001572 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001573 return false;
1574}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001575
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001576/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001577/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001578bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001579 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001580 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001581
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001582 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001583 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001584 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001585
Sean Callanan79ed1a82010-01-19 20:22:31 +00001586 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001587
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001588 if (Str.empty())
1589 Error(Loc, ".abort detected. Assembly stopping.");
1590 else
1591 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001592 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001593
1594 return false;
1595}
Kevin Enderby71148242009-07-14 21:35:03 +00001596
Kevin Enderby1f049b22009-07-14 23:21:55 +00001597/// ParseDirectiveInclude
1598/// ::= .include "filename"
1599bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001600 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001601 return TokError("expected string in '.include' directive");
1602
Sean Callanan18b83232010-01-19 21:44:56 +00001603 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001604 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001605 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001606
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001607 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001608 return TokError("unexpected token in '.include' directive");
1609
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001610 // Strip the quotes.
1611 Filename = Filename.substr(1, Filename.size()-2);
1612
1613 // Attempt to switch the lexer to the included file before consuming the end
1614 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001615 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001616 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001617 return true;
1618 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001619
1620 return false;
1621}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001622
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001623/// ParseDirectiveIf
1624/// ::= .if expression
1625bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001626 TheCondStack.push_back(TheCondState);
1627 TheCondState.TheCond = AsmCond::IfCond;
1628 if(TheCondState.Ignore) {
1629 EatToEndOfStatement();
1630 }
1631 else {
1632 int64_t ExprValue;
1633 if (ParseAbsoluteExpression(ExprValue))
1634 return true;
1635
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001636 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001637 return TokError("unexpected token in '.if' directive");
1638
Sean Callanan79ed1a82010-01-19 20:22:31 +00001639 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001640
1641 TheCondState.CondMet = ExprValue;
1642 TheCondState.Ignore = !TheCondState.CondMet;
1643 }
1644
1645 return false;
1646}
1647
1648/// ParseDirectiveElseIf
1649/// ::= .elseif expression
1650bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1651 if (TheCondState.TheCond != AsmCond::IfCond &&
1652 TheCondState.TheCond != AsmCond::ElseIfCond)
1653 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1654 " an .elseif");
1655 TheCondState.TheCond = AsmCond::ElseIfCond;
1656
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001657 bool LastIgnoreState = false;
1658 if (!TheCondStack.empty())
1659 LastIgnoreState = TheCondStack.back().Ignore;
1660 if (LastIgnoreState || TheCondState.CondMet) {
1661 TheCondState.Ignore = true;
1662 EatToEndOfStatement();
1663 }
1664 else {
1665 int64_t ExprValue;
1666 if (ParseAbsoluteExpression(ExprValue))
1667 return true;
1668
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001669 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001670 return TokError("unexpected token in '.elseif' directive");
1671
Sean Callanan79ed1a82010-01-19 20:22:31 +00001672 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001673 TheCondState.CondMet = ExprValue;
1674 TheCondState.Ignore = !TheCondState.CondMet;
1675 }
1676
1677 return false;
1678}
1679
1680/// ParseDirectiveElse
1681/// ::= .else
1682bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001683 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001684 return TokError("unexpected token in '.else' directive");
1685
Sean Callanan79ed1a82010-01-19 20:22:31 +00001686 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001687
1688 if (TheCondState.TheCond != AsmCond::IfCond &&
1689 TheCondState.TheCond != AsmCond::ElseIfCond)
1690 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1691 ".elseif");
1692 TheCondState.TheCond = AsmCond::ElseCond;
1693 bool LastIgnoreState = false;
1694 if (!TheCondStack.empty())
1695 LastIgnoreState = TheCondStack.back().Ignore;
1696 if (LastIgnoreState || TheCondState.CondMet)
1697 TheCondState.Ignore = true;
1698 else
1699 TheCondState.Ignore = false;
1700
1701 return false;
1702}
1703
1704/// ParseDirectiveEndIf
1705/// ::= .endif
1706bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001707 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001708 return TokError("unexpected token in '.endif' directive");
1709
Sean Callanan79ed1a82010-01-19 20:22:31 +00001710 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001711
1712 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1713 TheCondStack.empty())
1714 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1715 ".else");
1716 if (!TheCondStack.empty()) {
1717 TheCondState = TheCondStack.back();
1718 TheCondStack.pop_back();
1719 }
1720
1721 return false;
1722}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001723
1724/// ParseDirectiveFile
1725/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001726bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001727 // FIXME: I'm not sure what this is.
1728 int64_t FileNumber = -1;
Daniel Dunbareceec052010-07-12 17:45:27 +00001729 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001730 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001731 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001732
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001733 if (FileNumber < 1)
1734 return TokError("file number less than one");
1735 }
1736
Daniel Dunbareceec052010-07-12 17:45:27 +00001737 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001738 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001739
Chris Lattnerd32e8032010-01-25 19:02:58 +00001740 StringRef Filename = getTok().getString();
1741 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001742 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001743
Daniel Dunbareceec052010-07-12 17:45:27 +00001744 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001745 return TokError("unexpected token in '.file' directive");
1746
Chris Lattnerd32e8032010-01-25 19:02:58 +00001747 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001748 getStreamer().EmitFileDirective(Filename);
Chris Lattnerd32e8032010-01-25 19:02:58 +00001749 else
Daniel Dunbareceec052010-07-12 17:45:27 +00001750 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
1751
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001752 return false;
1753}
1754
1755/// ParseDirectiveLine
1756/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001757bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001758 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1759 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001760 return TokError("unexpected token in '.line' directive");
1761
Sean Callanan18b83232010-01-19 21:44:56 +00001762 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001763 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001764 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001765
1766 // FIXME: Do something with the .line.
1767 }
1768
Daniel Dunbareceec052010-07-12 17:45:27 +00001769 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001770 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001771
1772 return false;
1773}
1774
1775
1776/// ParseDirectiveLoc
1777/// ::= .loc number [number [number]]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001778bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001779 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001780 return TokError("unexpected token in '.loc' directive");
1781
1782 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001783 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001784 (void) FileNumber;
1785 // FIXME: Validate file.
1786
Sean Callanan79ed1a82010-01-19 20:22:31 +00001787 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001788 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1789 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001790 return TokError("unexpected token in '.loc' directive");
1791
Sean Callanan18b83232010-01-19 21:44:56 +00001792 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001793 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001794 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001795
Daniel Dunbareceec052010-07-12 17:45:27 +00001796 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1797 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001798 return TokError("unexpected token in '.loc' directive");
1799
Sean Callanan18b83232010-01-19 21:44:56 +00001800 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001801 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001802 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001803
1804 // FIXME: Do something with the .loc.
1805 }
1806 }
1807
Daniel Dunbareceec052010-07-12 17:45:27 +00001808 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001809 return TokError("unexpected token in '.file' directive");
1810
1811 return false;
1812}
1813
Daniel Dunbar3c802de2010-07-18 18:38:02 +00001814/// ParseDirectiveMacrosOnOff
1815/// ::= .macros_on
1816/// ::= .macros_off
1817bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
1818 SMLoc DirectiveLoc) {
1819 if (getLexer().isNot(AsmToken::EndOfStatement))
1820 return Error(getLexer().getLoc(),
1821 "unexpected token in '" + Directive + "' directive");
1822
1823 getParser().MacrosEnabled = Directive == ".macros_on";
1824
1825 return false;
1826}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00001827
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00001828/// ParseDirectiveMacro
1829/// ::= .macro name
1830bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
1831 SMLoc DirectiveLoc) {
1832 StringRef Name;
1833 if (getParser().ParseIdentifier(Name))
1834 return TokError("expected identifier in directive");
1835
1836 if (getLexer().isNot(AsmToken::EndOfStatement))
1837 return TokError("unexpected token in '.macro' directive");
1838
1839 // Eat the end of statement.
1840 Lex();
1841
1842 AsmToken EndToken, StartToken = getTok();
1843
1844 // Lex the macro definition.
1845 for (;;) {
1846 // Check whether we have reached the end of the file.
1847 if (getLexer().is(AsmToken::Eof))
1848 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
1849
1850 // Otherwise, check whether we have reach the .endmacro.
1851 if (getLexer().is(AsmToken::Identifier) &&
1852 (getTok().getIdentifier() == ".endm" ||
1853 getTok().getIdentifier() == ".endmacro")) {
1854 EndToken = getTok();
1855 Lex();
1856 if (getLexer().isNot(AsmToken::EndOfStatement))
1857 return TokError("unexpected token in '" + EndToken.getIdentifier() +
1858 "' directive");
1859 break;
1860 }
1861
1862 // Otherwise, scan til the end of the statement.
1863 getParser().EatToEndOfStatement();
1864 }
1865
1866 if (getParser().MacroMap.lookup(Name)) {
1867 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
1868 }
1869
1870 const char *BodyStart = StartToken.getLoc().getPointer();
1871 const char *BodyEnd = EndToken.getLoc().getPointer();
1872 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
1873 getParser().MacroMap[Name] = new Macro(Name, Body);
1874 return false;
1875}
1876
1877/// ParseDirectiveEndMacro
1878/// ::= .endm
1879/// ::= .endmacro
1880bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
1881 SMLoc DirectiveLoc) {
1882 if (getLexer().isNot(AsmToken::EndOfStatement))
1883 return TokError("unexpected token in '" + Directive + "' directive");
1884
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001885 // If we are inside a macro instantiation, terminate the current
1886 // instantiation.
1887 if (!getParser().ActiveMacros.empty()) {
1888 getParser().HandleMacroExit();
1889 return false;
1890 }
1891
1892 // Otherwise, this .endmacro is a stray entry in the file; well formed
1893 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00001894 return TokError("unexpected '" + Directive + "' in file, "
1895 "no current macro definition");
1896}
1897
Daniel Dunbard1e3b442010-07-17 02:26:10 +00001898/// \brief Create an MCAsmParser instance.
1899MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
1900 MCContext &C, MCStreamer &Out,
1901 const MCAsmInfo &MAI) {
1902 return new AsmParser(T, SM, C, Out, MAI);
1903}