blob: 61d65b8d015146be25e4577ac14f0805082fcdda [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
Eli Friedman5d68ec22010-07-19 04:17:25 +0000805 if (IDVal == ".align") {
806 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
807 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
808 }
809 if (IDVal == ".align32") {
810 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
811 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
812 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000813 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000814 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000815 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000816 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000817 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000818 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000819 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000820 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000821 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000822 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000823 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000824 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
825
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000826 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000827 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000828
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000829 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000830 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000831 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000832 return ParseDirectiveSpace();
833
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000834 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000835
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000836 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000837 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000838 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000839 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000840 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000841 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000842 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000843 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000844 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000845 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000846 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000847 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000848 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000849 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000850 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000851 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000852 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000853 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000854 if (IDVal == ".type")
855 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000856 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000857 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000858 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000859 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000860 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000861 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000862 if (IDVal == ".weak_def_can_be_hidden")
863 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000864
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000865 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000866 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000867 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000868 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000869
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000870 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000871 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000872 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000873 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000874
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000875 // Look up the handler in the handler table.
876 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
877 DirectiveMap.lookup(IDVal);
878 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000879 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000880
Kevin Enderby9c656452009-09-10 20:51:44 +0000881 // Target hook for parsing target specific directives.
882 if (!getTargetParser().ParseDirective(ID))
883 return false;
884
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000885 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000886 EatToEndOfStatement();
887 return false;
888 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000889
Chris Lattnera7f13542010-05-19 23:34:33 +0000890 // Canonicalize the opcode to lower case.
891 SmallString<128> Opcode;
892 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
893 Opcode.push_back(tolower(IDVal[i]));
894
Chris Lattner98986712010-01-14 22:21:20 +0000895 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000896 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000897 ParsedOperands);
898 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
899 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000900
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000901 // If parsing succeeded, match the instruction.
902 if (!HadError) {
903 MCInst Inst;
904 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
905 // Emit the instruction on success.
906 Out.EmitInstruction(Inst);
907 } else {
908 // Otherwise emit a diagnostic about the match failure and set the error
909 // flag.
910 //
911 // FIXME: We should give nicer diagnostics about the exact failure.
912 Error(IDLoc, "unrecognized instruction");
913 HadError = true;
914 }
915 }
Chris Lattner98986712010-01-14 22:21:20 +0000916
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000917 // If there was no error, consume the end-of-statement token. Otherwise this
918 // will be done by our caller.
919 if (!HadError)
920 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000921
922 // Free any parsed operands.
923 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
924 delete ParsedOperands[i];
925
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000926 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000927}
Chris Lattner9a023f72009-06-24 04:43:34 +0000928
Daniel Dunbar7a570d02010-07-18 19:00:10 +0000929MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
930 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000931 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
932{
933 // Macro instantiation is lexical, unfortunately. We construct a new buffer
934 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +0000935 SmallString<256> Buf;
936 raw_svector_ostream OS(Buf);
937
938 StringRef Body = M->Body;
939 while (!Body.empty()) {
940 // Scan for the next substitution.
941 std::size_t End = Body.size(), Pos = 0;
942 for (; Pos != End; ++Pos) {
943 // Check for a substitution or escape.
944 if (Body[Pos] != '$' || Pos + 1 == End)
945 continue;
946
947 char Next = Body[Pos + 1];
948 if (Next == '$' || Next == 'n' || isdigit(Next))
949 break;
950 }
951
952 // Add the prefix.
953 OS << Body.slice(0, Pos);
954
955 // Check if we reached the end.
956 if (Pos == End)
957 break;
958
959 switch (Body[Pos+1]) {
960 // $$ => $
961 case '$':
962 OS << '$';
963 break;
964
965 // $n => number of arguments
966 case 'n':
967 OS << A.size();
968 break;
969
970 // $[0-9] => argument
971 default: {
972 // Missing arguments are ignored.
973 unsigned Index = Body[Pos+1] - '0';
974 if (Index >= A.size())
975 break;
976
977 // Otherwise substitute with the token values, with spaces eliminated.
978 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
979 ie = A[Index].end(); it != ie; ++it)
980 OS << it->getString();
981 break;
982 }
983 }
984
985 // Update the scan point.
986 Body = Body.substr(Pos + 2);
987 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000988
989 // We include the .endmacro in the buffer as our queue to exit the macro
990 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +0000991 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000992
Daniel Dunbar7a570d02010-07-18 19:00:10 +0000993 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000994}
995
996bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
997 const Macro *M) {
998 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
999 // this, although we should protect against infinite loops.
1000 if (ActiveMacros.size() == 20)
1001 return TokError("macros cannot be nested more than 20 levels deep");
1002
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001003 // Parse the macro instantiation arguments.
1004 std::vector<std::vector<AsmToken> > MacroArguments;
1005 MacroArguments.push_back(std::vector<AsmToken>());
1006 unsigned ParenLevel = 0;
1007 for (;;) {
1008 if (Lexer.is(AsmToken::Eof))
1009 return TokError("unexpected token in macro instantiation");
1010 if (Lexer.is(AsmToken::EndOfStatement))
1011 break;
1012
1013 // If we aren't inside parentheses and this is a comma, start a new token
1014 // list.
1015 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1016 MacroArguments.push_back(std::vector<AsmToken>());
1017 } else if (Lexer.is(AsmToken::LParen)) {
1018 ++ParenLevel;
1019 } else if (Lexer.is(AsmToken::RParen)) {
1020 if (ParenLevel)
1021 --ParenLevel;
1022 } else {
1023 MacroArguments.back().push_back(getTok());
1024 }
1025 Lex();
1026 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001027
1028 // Create the macro instantiation object and add to the current macro
1029 // instantiation stack.
1030 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001031 getTok().getLoc(),
1032 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001033 ActiveMacros.push_back(MI);
1034
1035 // Jump to the macro instantiation and prime the lexer.
1036 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1037 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1038 Lex();
1039
1040 return false;
1041}
1042
1043void AsmParser::HandleMacroExit() {
1044 // Jump to the EndOfStatement we should return to, and consume it.
1045 JumpToLoc(ActiveMacros.back()->ExitLoc);
1046 Lex();
1047
1048 // Pop the instantiation entry.
1049 delete ActiveMacros.back();
1050 ActiveMacros.pop_back();
1051}
1052
Benjamin Kramer38e59892010-07-14 22:38:02 +00001053bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001054 // FIXME: Use better location, we should use proper tokens.
1055 SMLoc EqualLoc = Lexer.getLoc();
1056
Daniel Dunbar821e3332009-08-31 08:09:28 +00001057 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001058 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001059 return true;
1060
Daniel Dunbar3f872332009-07-28 16:08:33 +00001061 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001062 return TokError("unexpected token in assignment");
1063
1064 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001065 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001066
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001067 // Validate that the LHS is allowed to be a variable (either it has not been
1068 // used as a symbol, or it is an absolute symbol).
1069 MCSymbol *Sym = getContext().LookupSymbol(Name);
1070 if (Sym) {
1071 // Diagnose assignment to a label.
1072 //
1073 // FIXME: Diagnostics. Note the location of the definition as a label.
1074 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001075 if (Sym->isUndefined() && !Sym->isUsedInExpr())
1076 ; // Allow redefinitions of undefined symbols only used in directives.
1077 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001078 return Error(EqualLoc, "redefinition of '" + Name + "'");
1079 else if (!Sym->isVariable())
1080 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001081 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001082 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1083 Name + "'");
1084 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001085 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001086
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001087 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001088
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001089 Sym->setUsedInExpr(true);
1090
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001091 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001092 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001093
1094 return false;
1095}
1096
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001097/// ParseIdentifier:
1098/// ::= identifier
1099/// ::= string
1100bool AsmParser::ParseIdentifier(StringRef &Res) {
1101 if (Lexer.isNot(AsmToken::Identifier) &&
1102 Lexer.isNot(AsmToken::String))
1103 return true;
1104
Sean Callanan18b83232010-01-19 21:44:56 +00001105 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001106
Sean Callanan79ed1a82010-01-19 20:22:31 +00001107 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001108
1109 return false;
1110}
1111
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001112/// ParseDirectiveSet:
1113/// ::= .set identifier ',' expression
1114bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001115 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001116
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001117 if (ParseIdentifier(Name))
1118 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001119
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001120 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001121 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001122 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001123
Daniel Dunbare2ace502009-08-31 08:09:09 +00001124 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001125}
1126
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001127bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001128 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001129
1130 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001131 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001132 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1133 if (Str[i] != '\\') {
1134 Data += Str[i];
1135 continue;
1136 }
1137
1138 // Recognize escaped characters. Note that this escape semantics currently
1139 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1140 ++i;
1141 if (i == e)
1142 return TokError("unexpected backslash at end of string");
1143
1144 // Recognize octal sequences.
1145 if ((unsigned) (Str[i] - '0') <= 7) {
1146 // Consume up to three octal characters.
1147 unsigned Value = Str[i] - '0';
1148
1149 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1150 ++i;
1151 Value = Value * 8 + (Str[i] - '0');
1152
1153 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1154 ++i;
1155 Value = Value * 8 + (Str[i] - '0');
1156 }
1157 }
1158
1159 if (Value > 255)
1160 return TokError("invalid octal escape sequence (out of range)");
1161
1162 Data += (unsigned char) Value;
1163 continue;
1164 }
1165
1166 // Otherwise recognize individual escapes.
1167 switch (Str[i]) {
1168 default:
1169 // Just reject invalid escape sequences for now.
1170 return TokError("invalid escape sequence (unrecognized character)");
1171
1172 case 'b': Data += '\b'; break;
1173 case 'f': Data += '\f'; break;
1174 case 'n': Data += '\n'; break;
1175 case 'r': Data += '\r'; break;
1176 case 't': Data += '\t'; break;
1177 case '"': Data += '"'; break;
1178 case '\\': Data += '\\'; break;
1179 }
1180 }
1181
1182 return false;
1183}
1184
Daniel Dunbara0d14262009-06-24 23:30:00 +00001185/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001186/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001187bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001188 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001189 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001190 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001191 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001192
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001193 std::string Data;
1194 if (ParseEscapedString(Data))
1195 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001196
1197 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001198 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001199 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1200
Sean Callanan79ed1a82010-01-19 20:22:31 +00001201 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001202
1203 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001204 break;
1205
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001206 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001207 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001208 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001209 }
1210 }
1211
Sean Callanan79ed1a82010-01-19 20:22:31 +00001212 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001213 return false;
1214}
1215
1216/// ParseDirectiveValue
1217/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1218bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001219 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001220 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001221 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001222 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001223 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001224 return true;
1225
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001226 // Special case constant expressions to match code generator.
1227 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001228 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001229 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001230 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001231
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001232 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001233 break;
1234
1235 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001236 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001237 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001238 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001239 }
1240 }
1241
Sean Callanan79ed1a82010-01-19 20:22:31 +00001242 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001243 return false;
1244}
1245
1246/// ParseDirectiveSpace
1247/// ::= .space expression [ , expression ]
1248bool AsmParser::ParseDirectiveSpace() {
1249 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001250 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001251 return true;
1252
1253 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001254 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1255 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001256 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001257 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001258
Daniel Dunbar475839e2009-06-29 20:37:27 +00001259 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001260 return true;
1261
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001262 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001263 return TokError("unexpected token in '.space' directive");
1264 }
1265
Sean Callanan79ed1a82010-01-19 20:22:31 +00001266 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001267
1268 if (NumBytes <= 0)
1269 return TokError("invalid number of bytes in '.space' directive");
1270
1271 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001272 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001273
1274 return false;
1275}
1276
1277/// ParseDirectiveFill
1278/// ::= .fill expression , expression , expression
1279bool AsmParser::ParseDirectiveFill() {
1280 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001281 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001282 return true;
1283
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001284 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001285 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001286 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001287
1288 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001289 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001290 return true;
1291
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001292 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001293 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001294 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001295
1296 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001297 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001298 return true;
1299
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001300 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001301 return TokError("unexpected token in '.fill' directive");
1302
Sean Callanan79ed1a82010-01-19 20:22:31 +00001303 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001304
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001305 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1306 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001307
1308 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001309 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001310
1311 return false;
1312}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001313
1314/// ParseDirectiveOrg
1315/// ::= .org expression [ , expression ]
1316bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001317 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001318 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001319 return true;
1320
1321 // Parse optional fill expression.
1322 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001323 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1324 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001325 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001326 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001327
Daniel Dunbar475839e2009-06-29 20:37:27 +00001328 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001329 return true;
1330
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001331 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001332 return TokError("unexpected token in '.org' directive");
1333 }
1334
Sean Callanan79ed1a82010-01-19 20:22:31 +00001335 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001336
1337 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1338 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001339 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001340
1341 return false;
1342}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001343
1344/// ParseDirectiveAlign
1345/// ::= {.align, ...} expression [ , expression [ , expression ]]
1346bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001347 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001348 int64_t Alignment;
1349 if (ParseAbsoluteExpression(Alignment))
1350 return true;
1351
1352 SMLoc MaxBytesLoc;
1353 bool HasFillExpr = false;
1354 int64_t FillExpr = 0;
1355 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001356 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1357 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001358 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001359 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001360
1361 // The fill expression can be omitted while specifying a maximum number of
1362 // alignment bytes, e.g:
1363 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001364 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001365 HasFillExpr = true;
1366 if (ParseAbsoluteExpression(FillExpr))
1367 return true;
1368 }
1369
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001370 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1371 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001372 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001373 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001374
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001375 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001376 if (ParseAbsoluteExpression(MaxBytesToFill))
1377 return true;
1378
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001379 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001380 return TokError("unexpected token in directive");
1381 }
1382 }
1383
Sean Callanan79ed1a82010-01-19 20:22:31 +00001384 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001385
Daniel Dunbar648ac512010-05-17 21:54:30 +00001386 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001387 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001388
1389 // Compute alignment in bytes.
1390 if (IsPow2) {
1391 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001392 if (Alignment >= 32) {
1393 Error(AlignmentLoc, "invalid alignment value");
1394 Alignment = 31;
1395 }
1396
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001397 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001398 }
1399
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001400 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001401 if (MaxBytesLoc.isValid()) {
1402 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001403 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1404 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001405 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001406 }
1407
1408 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001409 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1410 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001411 MaxBytesToFill = 0;
1412 }
1413 }
1414
Daniel Dunbar648ac512010-05-17 21:54:30 +00001415 // Check whether we should use optimal code alignment for this .align
1416 // directive.
1417 //
1418 // FIXME: This should be using a target hook.
1419 bool UseCodeAlign = false;
1420 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001421 getStreamer().getCurrentSection()))
Chris Lattnera9558532010-07-15 21:19:31 +00001422 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001423 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1424 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001425 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001426 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001427 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001428 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1429 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001430 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001431
1432 return false;
1433}
1434
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001435/// ParseDirectiveSymbolAttribute
1436/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001437bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001438 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001439 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001440 StringRef Name;
1441
1442 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001443 return TokError("expected identifier in directive");
1444
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001445 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001446
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001447 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001448
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001449 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001450 break;
1451
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001452 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001453 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001454 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001455 }
1456 }
1457
Sean Callanan79ed1a82010-01-19 20:22:31 +00001458 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001459 return false;
1460}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001461
Matt Fleming924c5e52010-05-21 11:36:59 +00001462/// ParseDirectiveELFType
1463/// ::= .type identifier , @attribute
1464bool AsmParser::ParseDirectiveELFType() {
1465 StringRef Name;
1466 if (ParseIdentifier(Name))
1467 return TokError("expected identifier in directive");
1468
1469 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001470 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Matt Fleming924c5e52010-05-21 11:36:59 +00001471
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001472 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001473 return TokError("unexpected token in '.type' directive");
1474 Lex();
1475
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001476 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001477 return TokError("expected '@' before type");
1478 Lex();
1479
1480 StringRef Type;
1481 SMLoc TypeLoc;
1482
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001483 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001484 if (ParseIdentifier(Type))
1485 return TokError("expected symbol type in directive");
1486
1487 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1488 .Case("function", MCSA_ELF_TypeFunction)
1489 .Case("object", MCSA_ELF_TypeObject)
1490 .Case("tls_object", MCSA_ELF_TypeTLS)
1491 .Case("common", MCSA_ELF_TypeCommon)
1492 .Case("notype", MCSA_ELF_TypeNoType)
1493 .Default(MCSA_Invalid);
1494
1495 if (Attr == MCSA_Invalid)
1496 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1497
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001498 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001499 return TokError("unexpected token in '.type' directive");
1500
1501 Lex();
1502
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001503 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001504
1505 return false;
1506}
1507
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001508/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001509/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1510bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001511 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001512 StringRef Name;
1513 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001514 return TokError("expected identifier in directive");
1515
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001516 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001517 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001518
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001519 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001520 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001521 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001522
1523 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001524 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001525 if (ParseAbsoluteExpression(Size))
1526 return true;
1527
1528 int64_t Pow2Alignment = 0;
1529 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001530 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001531 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001532 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001533 if (ParseAbsoluteExpression(Pow2Alignment))
1534 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001535
1536 // If this target takes alignments in bytes (not log) validate and convert.
1537 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1538 if (!isPowerOf2_64(Pow2Alignment))
1539 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1540 Pow2Alignment = Log2_64(Pow2Alignment);
1541 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001542 }
1543
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001544 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001545 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001546
Sean Callanan79ed1a82010-01-19 20:22:31 +00001547 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001548
Chris Lattner1fc3d752009-07-09 17:25:12 +00001549 // NOTE: a size of zero for a .comm should create a undefined symbol
1550 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001551 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001552 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1553 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001554
Eric Christopherc260a3e2010-05-14 01:38:54 +00001555 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001556 // may internally end up wanting an alignment in bytes.
1557 // FIXME: Diagnose overflow.
1558 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001559 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1560 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001561
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001562 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001563 return Error(IDLoc, "invalid symbol redefinition");
1564
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001565 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001566 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001567 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001568 getStreamer().EmitZerofill(Ctx.getMachOSection(
1569 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1570 0, SectionKind::getBSS()),
1571 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001572 return false;
1573 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001574
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001575 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001576 return false;
1577}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001578
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001579/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001580/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001581bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001582 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001583 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001584
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001585 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001586 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001587 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001588
Sean Callanan79ed1a82010-01-19 20:22:31 +00001589 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001590
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001591 if (Str.empty())
1592 Error(Loc, ".abort detected. Assembly stopping.");
1593 else
1594 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001595 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001596
1597 return false;
1598}
Kevin Enderby71148242009-07-14 21:35:03 +00001599
Kevin Enderby1f049b22009-07-14 23:21:55 +00001600/// ParseDirectiveInclude
1601/// ::= .include "filename"
1602bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001603 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001604 return TokError("expected string in '.include' directive");
1605
Sean Callanan18b83232010-01-19 21:44:56 +00001606 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001607 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001608 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001609
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001610 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001611 return TokError("unexpected token in '.include' directive");
1612
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001613 // Strip the quotes.
1614 Filename = Filename.substr(1, Filename.size()-2);
1615
1616 // Attempt to switch the lexer to the included file before consuming the end
1617 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001618 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001619 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001620 return true;
1621 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001622
1623 return false;
1624}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001625
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001626/// ParseDirectiveIf
1627/// ::= .if expression
1628bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001629 TheCondStack.push_back(TheCondState);
1630 TheCondState.TheCond = AsmCond::IfCond;
1631 if(TheCondState.Ignore) {
1632 EatToEndOfStatement();
1633 }
1634 else {
1635 int64_t ExprValue;
1636 if (ParseAbsoluteExpression(ExprValue))
1637 return true;
1638
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001639 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001640 return TokError("unexpected token in '.if' directive");
1641
Sean Callanan79ed1a82010-01-19 20:22:31 +00001642 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001643
1644 TheCondState.CondMet = ExprValue;
1645 TheCondState.Ignore = !TheCondState.CondMet;
1646 }
1647
1648 return false;
1649}
1650
1651/// ParseDirectiveElseIf
1652/// ::= .elseif expression
1653bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1654 if (TheCondState.TheCond != AsmCond::IfCond &&
1655 TheCondState.TheCond != AsmCond::ElseIfCond)
1656 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1657 " an .elseif");
1658 TheCondState.TheCond = AsmCond::ElseIfCond;
1659
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001660 bool LastIgnoreState = false;
1661 if (!TheCondStack.empty())
1662 LastIgnoreState = TheCondStack.back().Ignore;
1663 if (LastIgnoreState || TheCondState.CondMet) {
1664 TheCondState.Ignore = true;
1665 EatToEndOfStatement();
1666 }
1667 else {
1668 int64_t ExprValue;
1669 if (ParseAbsoluteExpression(ExprValue))
1670 return true;
1671
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001672 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001673 return TokError("unexpected token in '.elseif' directive");
1674
Sean Callanan79ed1a82010-01-19 20:22:31 +00001675 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001676 TheCondState.CondMet = ExprValue;
1677 TheCondState.Ignore = !TheCondState.CondMet;
1678 }
1679
1680 return false;
1681}
1682
1683/// ParseDirectiveElse
1684/// ::= .else
1685bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001686 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001687 return TokError("unexpected token in '.else' directive");
1688
Sean Callanan79ed1a82010-01-19 20:22:31 +00001689 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001690
1691 if (TheCondState.TheCond != AsmCond::IfCond &&
1692 TheCondState.TheCond != AsmCond::ElseIfCond)
1693 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1694 ".elseif");
1695 TheCondState.TheCond = AsmCond::ElseCond;
1696 bool LastIgnoreState = false;
1697 if (!TheCondStack.empty())
1698 LastIgnoreState = TheCondStack.back().Ignore;
1699 if (LastIgnoreState || TheCondState.CondMet)
1700 TheCondState.Ignore = true;
1701 else
1702 TheCondState.Ignore = false;
1703
1704 return false;
1705}
1706
1707/// ParseDirectiveEndIf
1708/// ::= .endif
1709bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001710 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001711 return TokError("unexpected token in '.endif' directive");
1712
Sean Callanan79ed1a82010-01-19 20:22:31 +00001713 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001714
1715 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1716 TheCondStack.empty())
1717 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1718 ".else");
1719 if (!TheCondStack.empty()) {
1720 TheCondState = TheCondStack.back();
1721 TheCondStack.pop_back();
1722 }
1723
1724 return false;
1725}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001726
1727/// ParseDirectiveFile
1728/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001729bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001730 // FIXME: I'm not sure what this is.
1731 int64_t FileNumber = -1;
Daniel Dunbareceec052010-07-12 17:45:27 +00001732 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001733 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001734 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001735
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001736 if (FileNumber < 1)
1737 return TokError("file number less than one");
1738 }
1739
Daniel Dunbareceec052010-07-12 17:45:27 +00001740 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001741 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001742
Chris Lattnerd32e8032010-01-25 19:02:58 +00001743 StringRef Filename = getTok().getString();
1744 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001745 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001746
Daniel Dunbareceec052010-07-12 17:45:27 +00001747 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001748 return TokError("unexpected token in '.file' directive");
1749
Chris Lattnerd32e8032010-01-25 19:02:58 +00001750 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001751 getStreamer().EmitFileDirective(Filename);
Chris Lattnerd32e8032010-01-25 19:02:58 +00001752 else
Daniel Dunbareceec052010-07-12 17:45:27 +00001753 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
1754
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001755 return false;
1756}
1757
1758/// ParseDirectiveLine
1759/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001760bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001761 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1762 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001763 return TokError("unexpected token in '.line' directive");
1764
Sean Callanan18b83232010-01-19 21:44:56 +00001765 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001766 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001767 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001768
1769 // FIXME: Do something with the .line.
1770 }
1771
Daniel Dunbareceec052010-07-12 17:45:27 +00001772 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001773 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001774
1775 return false;
1776}
1777
1778
1779/// ParseDirectiveLoc
1780/// ::= .loc number [number [number]]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001781bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001782 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001783 return TokError("unexpected token in '.loc' directive");
1784
1785 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001786 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001787 (void) FileNumber;
1788 // FIXME: Validate file.
1789
Sean Callanan79ed1a82010-01-19 20:22:31 +00001790 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001791 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1792 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001793 return TokError("unexpected token in '.loc' directive");
1794
Sean Callanan18b83232010-01-19 21:44:56 +00001795 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001796 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001797 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001798
Daniel Dunbareceec052010-07-12 17:45:27 +00001799 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1800 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001801 return TokError("unexpected token in '.loc' directive");
1802
Sean Callanan18b83232010-01-19 21:44:56 +00001803 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001804 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001805 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001806
1807 // FIXME: Do something with the .loc.
1808 }
1809 }
1810
Daniel Dunbareceec052010-07-12 17:45:27 +00001811 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001812 return TokError("unexpected token in '.file' directive");
1813
1814 return false;
1815}
1816
Daniel Dunbar3c802de2010-07-18 18:38:02 +00001817/// ParseDirectiveMacrosOnOff
1818/// ::= .macros_on
1819/// ::= .macros_off
1820bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
1821 SMLoc DirectiveLoc) {
1822 if (getLexer().isNot(AsmToken::EndOfStatement))
1823 return Error(getLexer().getLoc(),
1824 "unexpected token in '" + Directive + "' directive");
1825
1826 getParser().MacrosEnabled = Directive == ".macros_on";
1827
1828 return false;
1829}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00001830
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00001831/// ParseDirectiveMacro
1832/// ::= .macro name
1833bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
1834 SMLoc DirectiveLoc) {
1835 StringRef Name;
1836 if (getParser().ParseIdentifier(Name))
1837 return TokError("expected identifier in directive");
1838
1839 if (getLexer().isNot(AsmToken::EndOfStatement))
1840 return TokError("unexpected token in '.macro' directive");
1841
1842 // Eat the end of statement.
1843 Lex();
1844
1845 AsmToken EndToken, StartToken = getTok();
1846
1847 // Lex the macro definition.
1848 for (;;) {
1849 // Check whether we have reached the end of the file.
1850 if (getLexer().is(AsmToken::Eof))
1851 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
1852
1853 // Otherwise, check whether we have reach the .endmacro.
1854 if (getLexer().is(AsmToken::Identifier) &&
1855 (getTok().getIdentifier() == ".endm" ||
1856 getTok().getIdentifier() == ".endmacro")) {
1857 EndToken = getTok();
1858 Lex();
1859 if (getLexer().isNot(AsmToken::EndOfStatement))
1860 return TokError("unexpected token in '" + EndToken.getIdentifier() +
1861 "' directive");
1862 break;
1863 }
1864
1865 // Otherwise, scan til the end of the statement.
1866 getParser().EatToEndOfStatement();
1867 }
1868
1869 if (getParser().MacroMap.lookup(Name)) {
1870 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
1871 }
1872
1873 const char *BodyStart = StartToken.getLoc().getPointer();
1874 const char *BodyEnd = EndToken.getLoc().getPointer();
1875 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
1876 getParser().MacroMap[Name] = new Macro(Name, Body);
1877 return false;
1878}
1879
1880/// ParseDirectiveEndMacro
1881/// ::= .endm
1882/// ::= .endmacro
1883bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
1884 SMLoc DirectiveLoc) {
1885 if (getLexer().isNot(AsmToken::EndOfStatement))
1886 return TokError("unexpected token in '" + Directive + "' directive");
1887
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001888 // If we are inside a macro instantiation, terminate the current
1889 // instantiation.
1890 if (!getParser().ActiveMacros.empty()) {
1891 getParser().HandleMacroExit();
1892 return false;
1893 }
1894
1895 // Otherwise, this .endmacro is a stray entry in the file; well formed
1896 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00001897 return TokError("unexpected '" + Directive + "' in file, "
1898 "no current macro definition");
1899}
1900
Daniel Dunbard1e3b442010-07-17 02:26:10 +00001901/// \brief Create an MCAsmParser instance.
1902MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
1903 MCContext &C, MCStreamer &Out,
1904 const MCAsmInfo &MAI) {
1905 return new AsmParser(T, SM, C, Out, MAI);
1906}