blob: e74952a4cab481ff0773ddce1f822e7a1cbc8d50 [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"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000029#include "llvm/MC/MCDwarf.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000030#include "llvm/Support/Compiler.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000031#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000032#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000033#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000034#include "llvm/Target/TargetAsmParser.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000035#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000036using namespace llvm;
37
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000038namespace {
39
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000040/// \brief Helper class for tracking macro definitions.
41struct Macro {
42 StringRef Name;
43 StringRef Body;
44
45public:
46 Macro(StringRef N, StringRef B) : Name(N), Body(B) {}
47};
48
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000049/// \brief Helper class for storing information about an active macro
50/// instantiation.
51struct MacroInstantiation {
52 /// The macro being instantiated.
53 const Macro *TheMacro;
54
55 /// The macro instantiation with substitutions.
56 MemoryBuffer *Instantiation;
57
58 /// The location of the instantiation.
59 SMLoc InstantiationLoc;
60
61 /// The location where parsing should resume upon instantiation completion.
62 SMLoc ExitLoc;
63
64public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000065 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
66 const std::vector<std::vector<AsmToken> > &A);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000067};
68
Daniel Dunbaraef87e32010-07-18 18:31:38 +000069/// \brief The concrete assembly parser instance.
70class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000071 friend class GenericAsmParser;
72
Daniel Dunbaraef87e32010-07-18 18:31:38 +000073 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
74 void operator=(const AsmParser &); // DO NOT IMPLEMENT
75private:
76 AsmLexer Lexer;
77 MCContext &Ctx;
78 MCStreamer &Out;
79 SourceMgr &SrcMgr;
80 MCAsmParserExtension *GenericParser;
81 MCAsmParserExtension *PlatformParser;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000082
Daniel Dunbaraef87e32010-07-18 18:31:38 +000083 /// This is the current buffer index we're lexing from as managed by the
84 /// SourceMgr object.
85 int CurBuffer;
86
87 AsmCond TheCondState;
88 std::vector<AsmCond> TheCondStack;
89
90 /// DirectiveMap - This is a table handlers for directives. Each handler is
91 /// invoked after the directive identifier is read and is responsible for
92 /// parsing and validating the rest of the directive. The handler is passed
93 /// in the directive name and the location of the directive keyword.
94 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000095
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000096 /// MacroMap - Map of currently defined macros.
97 StringMap<Macro*> MacroMap;
98
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000099 /// ActiveMacros - Stack of active macro instantiations.
100 std::vector<MacroInstantiation*> ActiveMacros;
101
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000102 /// Boolean tracking whether macro substitution is enabled.
103 unsigned MacrosEnabled : 1;
104
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000105public:
106 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
107 const MCAsmInfo &MAI);
108 ~AsmParser();
109
110 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
111
112 void AddDirectiveHandler(MCAsmParserExtension *Object,
113 StringRef Directive,
114 DirectiveHandler Handler) {
115 DirectiveMap[Directive] = std::make_pair(Object, Handler);
116 }
117
118public:
119 /// @name MCAsmParser Interface
120 /// {
121
122 virtual SourceMgr &getSourceManager() { return SrcMgr; }
123 virtual MCAsmLexer &getLexer() { return Lexer; }
124 virtual MCContext &getContext() { return Ctx; }
125 virtual MCStreamer &getStreamer() { return Out; }
126
127 virtual void Warning(SMLoc L, const Twine &Meg);
128 virtual bool Error(SMLoc L, const Twine &Msg);
129
130 const AsmToken &Lex();
131
132 bool ParseExpression(const MCExpr *&Res);
133 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
134 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
135 virtual bool ParseAbsoluteExpression(int64_t &Res);
136
137 /// }
138
139private:
140 bool ParseStatement();
141
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000142 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
143 void HandleMacroExit();
144
145 void PrintMacroInstantiations();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000146 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
147
148 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
149 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000150
151 /// \brief Reset the current lexer position to that given by \arg Loc. The
152 /// current token is not set; clients should ensure Lex() is called
153 /// subsequently.
154 void JumpToLoc(SMLoc Loc);
155
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000156 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000157
158 /// \brief Parse up to the end of statement and a return the contents from the
159 /// current token until the end of the statement; the current token on exit
160 /// will be either the EndOfStatement or EOF.
161 StringRef ParseStringToEndOfStatement();
162
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000163 bool ParseAssignment(StringRef Name);
164
165 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
166 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
167 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
168
169 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
170 /// and set \arg Res to the identifier contents.
171 bool ParseIdentifier(StringRef &Res);
172
173 // Directive Parsing.
174 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
175 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
176 bool ParseDirectiveFill(); // ".fill"
177 bool ParseDirectiveSpace(); // ".space"
178 bool ParseDirectiveSet(); // ".set"
179 bool ParseDirectiveOrg(); // ".org"
180 // ".align{,32}", ".p2align{,w,l}"
181 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
182
183 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
184 /// accepts a single symbol (which should be a label or an external).
185 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
186 bool ParseDirectiveELFType(); // ELF specific ".type"
187
188 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
189
190 bool ParseDirectiveAbort(); // ".abort"
191 bool ParseDirectiveInclude(); // ".include"
192
193 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
194 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
195 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
196 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
197
198 /// ParseEscapedString - Parse the current token as a string which may include
199 /// escaped characters and return the string contents.
200 bool ParseEscapedString(std::string &Data);
201};
202
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000203/// \brief Generic implementations of directive handling, etc. which is shared
204/// (or the default, at least) for all assembler parser.
205class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000206 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
207 void AddDirectiveHandler(StringRef Directive) {
208 getParser().AddDirectiveHandler(this, Directive,
209 HandleDirective<GenericAsmParser, Handler>);
210 }
211
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000212public:
213 GenericAsmParser() {}
214
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000215 AsmParser &getParser() {
216 return (AsmParser&) this->MCAsmParserExtension::getParser();
217 }
218
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000219 virtual void Initialize(MCAsmParser &Parser) {
220 // Call the base implementation.
221 this->MCAsmParserExtension::Initialize(Parser);
222
223 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000224 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
225 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
226 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000227
228 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000229 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
230 ".macros_on");
231 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
232 ".macros_off");
233 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
234 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
235 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000236 }
237
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000238 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
239 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
240 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000241
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000242 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000243 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
244 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000245};
246
247}
248
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000249namespace llvm {
250
251extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000252extern MCAsmParserExtension *createELFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000253
254}
255
Chris Lattneraaec2052010-01-19 19:46:13 +0000256enum { DEFAULT_ADDRSPACE = 0 };
257
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000258AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
259 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000260 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000261 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000262 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000263 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000264
265 // Initialize the generic parser.
266 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000267
268 // Initialize the platform / file format parser.
269 //
270 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
271 // created.
272 if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000273 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000274 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000275 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000276 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000277 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000278 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000279}
280
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000281AsmParser::~AsmParser() {
Daniel Dunbare4749702010-07-12 18:12:02 +0000282 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000283 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000284}
285
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000286void AsmParser::PrintMacroInstantiations() {
287 // Print the active macro instantiation stack.
288 for (std::vector<MacroInstantiation*>::const_reverse_iterator
289 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
290 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
291 "note");
292}
293
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000294void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000295 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000296 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000297}
298
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000299bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000300 PrintMessage(L, Msg.str(), "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000301 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000302 return true;
303}
304
Sean Callananbf2013e2010-01-20 23:19:55 +0000305void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
306 const char *Type) const {
307 SrcMgr.PrintMessage(Loc, Msg, Type);
308}
Sean Callananfd0b0282010-01-21 00:19:58 +0000309
310bool AsmParser::EnterIncludeFile(const std::string &Filename) {
311 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
312 if (NewBuf == -1)
313 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000314
Sean Callananfd0b0282010-01-21 00:19:58 +0000315 CurBuffer = NewBuf;
316
317 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
318
319 return false;
320}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000321
322void AsmParser::JumpToLoc(SMLoc Loc) {
323 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
324 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
325}
326
Sean Callananfd0b0282010-01-21 00:19:58 +0000327const AsmToken &AsmParser::Lex() {
328 const AsmToken *tok = &Lexer.Lex();
329
330 if (tok->is(AsmToken::Eof)) {
331 // If this is the end of an included file, pop the parent file off the
332 // include stack.
333 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
334 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000335 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000336 tok = &Lexer.Lex();
337 }
338 }
339
340 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000341 Error(Lexer.getErrLoc(), Lexer.getErr());
Sean Callanan79036e42010-01-20 22:18:24 +0000342
Sean Callananfd0b0282010-01-21 00:19:58 +0000343 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000344}
345
Chris Lattner79180e22010-04-05 23:15:42 +0000346bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000347 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000348 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000349 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000350 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000351 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000352 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
353 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000354
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000355 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000356 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000357
Chris Lattnerb717fb02009-07-02 21:53:43 +0000358 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000359
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000360 AsmCond StartingCondState = TheCondState;
361
Chris Lattnerb717fb02009-07-02 21:53:43 +0000362 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000363 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000364 if (!ParseStatement()) continue;
365
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000366 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000367 HadError = true;
368 EatToEndOfStatement();
369 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000370
371 if (TheCondState.TheCond != StartingCondState.TheCond ||
372 TheCondState.Ignore != StartingCondState.Ignore)
373 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000374
375 // Check to see there are no empty DwarfFile slots.
376 const std::vector<MCDwarfFile *> &MCDwarfFiles =
377 getContext().getMCDwarfFiles();
378 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
379 if (!MCDwarfFiles[i]){
380 TokError("unassigned file number: " + Twine(i) + " for .file directives");
381 HadError = true;
382 }
383 }
Chris Lattnerb717fb02009-07-02 21:53:43 +0000384
Chris Lattner79180e22010-04-05 23:15:42 +0000385 // Finalize the output stream if there are no errors and if the client wants
386 // us to.
387 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000388 Out.Finish();
389
Chris Lattnerb717fb02009-07-02 21:53:43 +0000390 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000391}
392
Chris Lattner2cf5f142009-06-22 01:29:09 +0000393/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
394void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000395 while (Lexer.isNot(AsmToken::EndOfStatement) &&
396 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000397 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000398
399 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000400 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000401 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000402}
403
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000404StringRef AsmParser::ParseStringToEndOfStatement() {
405 const char *Start = getTok().getLoc().getPointer();
406
407 while (Lexer.isNot(AsmToken::EndOfStatement) &&
408 Lexer.isNot(AsmToken::Eof))
409 Lex();
410
411 const char *End = getTok().getLoc().getPointer();
412 return StringRef(Start, End - Start);
413}
Chris Lattnerc4193832009-06-22 05:51:26 +0000414
Chris Lattner74ec1a32009-06-22 06:32:03 +0000415/// ParseParenExpr - Parse a paren expression and return it.
416/// NOTE: This assumes the leading '(' has already been consumed.
417///
418/// parenexpr ::= expr)
419///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000420bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000421 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000422 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000423 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000424 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000425 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000426 return false;
427}
Chris Lattnerc4193832009-06-22 05:51:26 +0000428
Chris Lattner74ec1a32009-06-22 06:32:03 +0000429/// ParsePrimaryExpr - Parse a primary expression and return it.
430/// primaryexpr ::= (parenexpr
431/// primaryexpr ::= symbol
432/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000433/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000434/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000435bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000436 switch (Lexer.getKind()) {
437 default:
438 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000439 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000440 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000441 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000442 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000443 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000444 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000445 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000446 case AsmToken::Identifier: {
447 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000448 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000449 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000450
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000451 // Mark the symbol as used in an expression.
452 Sym->setUsedInExpr(true);
453
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000454 // Lookup the symbol variant if used.
455 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
456 if (Split.first.size() != getTok().getIdentifier().size())
457 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
458
Chris Lattnerb4307b32010-01-15 19:28:38 +0000459 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000460 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000461
462 // If this is an absolute variable reference, substitute it now to preserve
463 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000464 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000465 if (Variant)
466 return Error(EndLoc, "unexpected modified on variable reference");
467
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000468 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000469 return false;
470 }
471
472 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000473 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000474 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000475 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000476 case AsmToken::Integer: {
477 SMLoc Loc = getTok().getLoc();
478 int64_t IntVal = getTok().getIntVal();
479 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000480 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000481 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000482 // Look for 'b' or 'f' following an Integer as a directional label
483 if (Lexer.getKind() == AsmToken::Identifier) {
484 StringRef IDVal = getTok().getString();
485 if (IDVal == "f" || IDVal == "b"){
486 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
487 IDVal == "f" ? 1 : 0);
488 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
489 getContext());
490 if(IDVal == "b" && Sym->isUndefined())
491 return Error(Loc, "invalid reference to undefined symbol");
492 EndLoc = Lexer.getLoc();
493 Lex(); // Eat identifier.
494 }
495 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000496 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000497 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000498 case AsmToken::Dot: {
499 // This is a '.' reference, which references the current PC. Emit a
500 // temporary label to the streamer and refer to it.
501 MCSymbol *Sym = Ctx.CreateTempSymbol();
502 Out.EmitLabel(Sym);
503 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
504 EndLoc = Lexer.getLoc();
505 Lex(); // Eat identifier.
506 return false;
507 }
508
Daniel Dunbar3f872332009-07-28 16:08:33 +0000509 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000510 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000511 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000512 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000513 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000514 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000515 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000516 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000517 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000518 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000519 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000520 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000521 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000522 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000523 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000524 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000525 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000526 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000527 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000528 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000529 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000530 }
531}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000532
Chris Lattnerb4307b32010-01-15 19:28:38 +0000533bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000534 SMLoc EndLoc;
535 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000536}
537
Chris Lattner74ec1a32009-06-22 06:32:03 +0000538/// ParseExpression - Parse an expression and return it.
539///
540/// expr ::= expr +,- expr -> lowest.
541/// expr ::= expr |,^,&,! expr -> middle.
542/// expr ::= expr *,/,%,<<,>> expr -> highest.
543/// expr ::= primaryexpr
544///
Chris Lattner54482b42010-01-15 19:39:23 +0000545bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000546 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000547 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000548 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
549 return true;
550
551 // Try to constant fold it up front, if possible.
552 int64_t Value;
553 if (Res->EvaluateAsAbsolute(Value))
554 Res = MCConstantExpr::Create(Value, getContext());
555
556 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000557}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000558
Chris Lattnerb4307b32010-01-15 19:28:38 +0000559bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000560 Res = 0;
561 return ParseParenExpr(Res, EndLoc) ||
562 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000563}
564
Daniel Dunbar475839e2009-06-29 20:37:27 +0000565bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000566 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000567
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000568 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000569 if (ParseExpression(Expr))
570 return true;
571
Daniel Dunbare00b0112009-10-16 01:57:52 +0000572 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000573 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000574
575 return false;
576}
577
Daniel Dunbar3f872332009-07-28 16:08:33 +0000578static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000579 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000580 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000581 default:
582 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000583
584 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000585 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000586 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000587 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000588 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000589 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000590 return 1;
591
592 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000593 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000594 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000595 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000596 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000597 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000598 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000599 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000600 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000601 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000602 case AsmToken::ExclaimEqual:
603 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000604 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000605 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000606 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000607 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000608 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000609 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000610 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000611 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000612 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000613 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000614 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000615 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000616 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000617 return 2;
618
619 // Intermediate Precedence: |, &, ^
620 //
621 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000622 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000623 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000624 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000625 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000626 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000627 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000628 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000629 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000630 return 3;
631
632 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000633 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000634 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000635 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000636 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000637 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000638 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000639 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000640 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000641 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000642 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000643 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000644 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000645 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000646 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000647 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000648 }
649}
650
651
652/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
653/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000654bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
655 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000656 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000657 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000658 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000659
660 // If the next token is lower precedence than we are allowed to eat, return
661 // successfully with what we ate already.
662 if (TokPrec < Precedence)
663 return false;
664
Sean Callanan79ed1a82010-01-19 20:22:31 +0000665 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000666
667 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000668 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000669 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000670
671 // If BinOp binds less tightly with RHS than the operator after RHS, let
672 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000673 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000674 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000675 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000676 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000677 }
678
Daniel Dunbar475839e2009-06-29 20:37:27 +0000679 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000680 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000681 }
682}
683
Chris Lattnerc4193832009-06-22 05:51:26 +0000684
685
686
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000687/// ParseStatement:
688/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000689/// ::= Label* Directive ...Operands... EndOfStatement
690/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000691bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000692 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000693 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000694 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000695 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000696 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000697
698 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000699 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000700 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000701 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000702 int64_t LocalLabelVal = -1;
703 // GUESS allow an integer followed by a ':' as a directional local label
704 if (Lexer.is(AsmToken::Integer)) {
705 LocalLabelVal = getTok().getIntVal();
706 if (LocalLabelVal < 0) {
707 if (!TheCondState.Ignore)
708 return TokError("unexpected token at start of statement");
709 IDVal = "";
710 }
711 else {
712 IDVal = getTok().getString();
713 Lex(); // Consume the integer token to be used as an identifier token.
714 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000715 if (!TheCondState.Ignore)
716 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000717 }
718 }
719 }
720 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000721 if (!TheCondState.Ignore)
722 return TokError("unexpected token at start of statement");
723 IDVal = "";
724 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000725
Chris Lattner7834fac2010-04-17 18:14:27 +0000726 // Handle conditional assembly here before checking for skipping. We
727 // have to do this so that .endif isn't skipped in a ".if 0" block for
728 // example.
729 if (IDVal == ".if")
730 return ParseDirectiveIf(IDLoc);
731 if (IDVal == ".elseif")
732 return ParseDirectiveElseIf(IDLoc);
733 if (IDVal == ".else")
734 return ParseDirectiveElse(IDLoc);
735 if (IDVal == ".endif")
736 return ParseDirectiveEndIf(IDLoc);
737
738 // If we are in a ".if 0" block, ignore this statement.
739 if (TheCondState.Ignore) {
740 EatToEndOfStatement();
741 return false;
742 }
743
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000744 // FIXME: Recurse on local labels?
745
746 // See what kind of statement we have.
747 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000748 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000749 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000750 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000751
752 // Diagnose attempt to use a variable as a label.
753 //
754 // FIXME: Diagnostics. Note the location of the definition as a label.
755 // FIXME: This doesn't diagnose assignment to a symbol which has been
756 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000757 MCSymbol *Sym;
758 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000759 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000760 else
761 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000762 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000763 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000764
Daniel Dunbar959fd882009-08-26 22:13:22 +0000765 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000766 Out.EmitLabel(Sym);
767
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000768 // Consume any end of statement token, if present, to avoid spurious
769 // AddBlankLine calls().
770 if (Lexer.is(AsmToken::EndOfStatement)) {
771 Lex();
772 if (Lexer.is(AsmToken::Eof))
773 return false;
774 }
775
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000776 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000777 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000778
Daniel Dunbar3f872332009-07-28 16:08:33 +0000779 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000780 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000781 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000782
Daniel Dunbare2ace502009-08-31 08:09:09 +0000783 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000784
785 default: // Normal instruction or directive.
786 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000787 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000788
789 // If macros are enabled, check to see if this is a macro instantiation.
790 if (MacrosEnabled)
791 if (const Macro *M = MacroMap.lookup(IDVal))
792 return HandleMacroEntry(IDVal, IDLoc, M);
793
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000794 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000795 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000796 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000797 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000798 return ParseDirectiveSet();
799
Daniel Dunbara0d14262009-06-24 23:30:00 +0000800 // Data directives
801
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000802 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000803 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000804 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000805 return ParseDirectiveAscii(true);
806
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000807 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000808 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000809 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000810 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000811 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000812 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000813 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000814 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000815
Eli Friedman5d68ec22010-07-19 04:17:25 +0000816 if (IDVal == ".align") {
817 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
818 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
819 }
820 if (IDVal == ".align32") {
821 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
822 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
823 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000824 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000825 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000826 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000827 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000828 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000829 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000830 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000831 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000832 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000833 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000834 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000835 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
836
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000837 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000838 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000839
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000840 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000841 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000842 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000843 return ParseDirectiveSpace();
844
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000845 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000846
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000847 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000848 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000849 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000850 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000851 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000852 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000853 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000854 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000855 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000856 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000857 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000858 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000859 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000860 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000861 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000862 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000863 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000864 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000865 if (IDVal == ".type")
866 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000867 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000868 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000869 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000870 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000871 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000872 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000873 if (IDVal == ".weak_def_can_be_hidden")
874 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000875
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000876 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000877 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000878 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000879 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000880
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000881 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000882 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000883 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000884 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000885
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000886 // Look up the handler in the handler table.
887 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
888 DirectiveMap.lookup(IDVal);
889 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000890 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000891
Kevin Enderby9c656452009-09-10 20:51:44 +0000892 // Target hook for parsing target specific directives.
893 if (!getTargetParser().ParseDirective(ID))
894 return false;
895
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000896 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000897 EatToEndOfStatement();
898 return false;
899 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000900
Chris Lattnera7f13542010-05-19 23:34:33 +0000901 // Canonicalize the opcode to lower case.
902 SmallString<128> Opcode;
903 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
904 Opcode.push_back(tolower(IDVal[i]));
905
Chris Lattner98986712010-01-14 22:21:20 +0000906 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000907 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000908 ParsedOperands);
909 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
910 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000911
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000912 // If parsing succeeded, match the instruction.
913 if (!HadError) {
914 MCInst Inst;
915 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
916 // Emit the instruction on success.
917 Out.EmitInstruction(Inst);
918 } else {
919 // Otherwise emit a diagnostic about the match failure and set the error
920 // flag.
921 //
922 // FIXME: We should give nicer diagnostics about the exact failure.
923 Error(IDLoc, "unrecognized instruction");
924 HadError = true;
925 }
926 }
Chris Lattner98986712010-01-14 22:21:20 +0000927
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000928 // If there was no error, consume the end-of-statement token. Otherwise this
929 // will be done by our caller.
930 if (!HadError)
931 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000932
933 // Free any parsed operands.
934 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
935 delete ParsedOperands[i];
936
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000937 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000938}
Chris Lattner9a023f72009-06-24 04:43:34 +0000939
Daniel Dunbar7a570d02010-07-18 19:00:10 +0000940MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
941 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000942 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
943{
944 // Macro instantiation is lexical, unfortunately. We construct a new buffer
945 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +0000946 SmallString<256> Buf;
947 raw_svector_ostream OS(Buf);
948
949 StringRef Body = M->Body;
950 while (!Body.empty()) {
951 // Scan for the next substitution.
952 std::size_t End = Body.size(), Pos = 0;
953 for (; Pos != End; ++Pos) {
954 // Check for a substitution or escape.
955 if (Body[Pos] != '$' || Pos + 1 == End)
956 continue;
957
958 char Next = Body[Pos + 1];
959 if (Next == '$' || Next == 'n' || isdigit(Next))
960 break;
961 }
962
963 // Add the prefix.
964 OS << Body.slice(0, Pos);
965
966 // Check if we reached the end.
967 if (Pos == End)
968 break;
969
970 switch (Body[Pos+1]) {
971 // $$ => $
972 case '$':
973 OS << '$';
974 break;
975
976 // $n => number of arguments
977 case 'n':
978 OS << A.size();
979 break;
980
981 // $[0-9] => argument
982 default: {
983 // Missing arguments are ignored.
984 unsigned Index = Body[Pos+1] - '0';
985 if (Index >= A.size())
986 break;
987
988 // Otherwise substitute with the token values, with spaces eliminated.
989 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
990 ie = A[Index].end(); it != ie; ++it)
991 OS << it->getString();
992 break;
993 }
994 }
995
996 // Update the scan point.
997 Body = Body.substr(Pos + 2);
998 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000999
1000 // We include the .endmacro in the buffer as our queue to exit the macro
1001 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001002 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001003
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001004 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001005}
1006
1007bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1008 const Macro *M) {
1009 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1010 // this, although we should protect against infinite loops.
1011 if (ActiveMacros.size() == 20)
1012 return TokError("macros cannot be nested more than 20 levels deep");
1013
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001014 // Parse the macro instantiation arguments.
1015 std::vector<std::vector<AsmToken> > MacroArguments;
1016 MacroArguments.push_back(std::vector<AsmToken>());
1017 unsigned ParenLevel = 0;
1018 for (;;) {
1019 if (Lexer.is(AsmToken::Eof))
1020 return TokError("unexpected token in macro instantiation");
1021 if (Lexer.is(AsmToken::EndOfStatement))
1022 break;
1023
1024 // If we aren't inside parentheses and this is a comma, start a new token
1025 // list.
1026 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1027 MacroArguments.push_back(std::vector<AsmToken>());
1028 } else if (Lexer.is(AsmToken::LParen)) {
1029 ++ParenLevel;
1030 } else if (Lexer.is(AsmToken::RParen)) {
1031 if (ParenLevel)
1032 --ParenLevel;
1033 } else {
1034 MacroArguments.back().push_back(getTok());
1035 }
1036 Lex();
1037 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001038
1039 // Create the macro instantiation object and add to the current macro
1040 // instantiation stack.
1041 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001042 getTok().getLoc(),
1043 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001044 ActiveMacros.push_back(MI);
1045
1046 // Jump to the macro instantiation and prime the lexer.
1047 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1048 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1049 Lex();
1050
1051 return false;
1052}
1053
1054void AsmParser::HandleMacroExit() {
1055 // Jump to the EndOfStatement we should return to, and consume it.
1056 JumpToLoc(ActiveMacros.back()->ExitLoc);
1057 Lex();
1058
1059 // Pop the instantiation entry.
1060 delete ActiveMacros.back();
1061 ActiveMacros.pop_back();
1062}
1063
Benjamin Kramer38e59892010-07-14 22:38:02 +00001064bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001065 // FIXME: Use better location, we should use proper tokens.
1066 SMLoc EqualLoc = Lexer.getLoc();
1067
Daniel Dunbar821e3332009-08-31 08:09:28 +00001068 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001069 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001070 return true;
1071
Daniel Dunbar3f872332009-07-28 16:08:33 +00001072 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001073 return TokError("unexpected token in assignment");
1074
1075 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001076 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001077
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001078 // Validate that the LHS is allowed to be a variable (either it has not been
1079 // used as a symbol, or it is an absolute symbol).
1080 MCSymbol *Sym = getContext().LookupSymbol(Name);
1081 if (Sym) {
1082 // Diagnose assignment to a label.
1083 //
1084 // FIXME: Diagnostics. Note the location of the definition as a label.
1085 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001086 if (Sym->isUndefined() && !Sym->isUsedInExpr())
1087 ; // Allow redefinitions of undefined symbols only used in directives.
1088 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001089 return Error(EqualLoc, "redefinition of '" + Name + "'");
1090 else if (!Sym->isVariable())
1091 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001092 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001093 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1094 Name + "'");
1095 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001096 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001097
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001098 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001099
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001100 Sym->setUsedInExpr(true);
1101
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001102 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001103 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001104
1105 return false;
1106}
1107
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001108/// ParseIdentifier:
1109/// ::= identifier
1110/// ::= string
1111bool AsmParser::ParseIdentifier(StringRef &Res) {
1112 if (Lexer.isNot(AsmToken::Identifier) &&
1113 Lexer.isNot(AsmToken::String))
1114 return true;
1115
Sean Callanan18b83232010-01-19 21:44:56 +00001116 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001117
Sean Callanan79ed1a82010-01-19 20:22:31 +00001118 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001119
1120 return false;
1121}
1122
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001123/// ParseDirectiveSet:
1124/// ::= .set identifier ',' expression
1125bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001126 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001127
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001128 if (ParseIdentifier(Name))
1129 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001130
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001131 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001132 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001133 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001134
Daniel Dunbare2ace502009-08-31 08:09:09 +00001135 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001136}
1137
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001138bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001139 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001140
1141 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001142 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001143 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1144 if (Str[i] != '\\') {
1145 Data += Str[i];
1146 continue;
1147 }
1148
1149 // Recognize escaped characters. Note that this escape semantics currently
1150 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1151 ++i;
1152 if (i == e)
1153 return TokError("unexpected backslash at end of string");
1154
1155 // Recognize octal sequences.
1156 if ((unsigned) (Str[i] - '0') <= 7) {
1157 // Consume up to three octal characters.
1158 unsigned Value = Str[i] - '0';
1159
1160 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1161 ++i;
1162 Value = Value * 8 + (Str[i] - '0');
1163
1164 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1165 ++i;
1166 Value = Value * 8 + (Str[i] - '0');
1167 }
1168 }
1169
1170 if (Value > 255)
1171 return TokError("invalid octal escape sequence (out of range)");
1172
1173 Data += (unsigned char) Value;
1174 continue;
1175 }
1176
1177 // Otherwise recognize individual escapes.
1178 switch (Str[i]) {
1179 default:
1180 // Just reject invalid escape sequences for now.
1181 return TokError("invalid escape sequence (unrecognized character)");
1182
1183 case 'b': Data += '\b'; break;
1184 case 'f': Data += '\f'; break;
1185 case 'n': Data += '\n'; break;
1186 case 'r': Data += '\r'; break;
1187 case 't': Data += '\t'; break;
1188 case '"': Data += '"'; break;
1189 case '\\': Data += '\\'; break;
1190 }
1191 }
1192
1193 return false;
1194}
1195
Daniel Dunbara0d14262009-06-24 23:30:00 +00001196/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001197/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001198bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001199 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001200 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001201 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001202 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001203
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001204 std::string Data;
1205 if (ParseEscapedString(Data))
1206 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001207
1208 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001209 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001210 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1211
Sean Callanan79ed1a82010-01-19 20:22:31 +00001212 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001213
1214 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001215 break;
1216
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001217 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001218 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001219 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001220 }
1221 }
1222
Sean Callanan79ed1a82010-01-19 20:22:31 +00001223 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001224 return false;
1225}
1226
1227/// ParseDirectiveValue
1228/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1229bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001230 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001231 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001232 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001233 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001234 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001235 return true;
1236
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001237 // Special case constant expressions to match code generator.
1238 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001239 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001240 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001241 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001242
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001243 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001244 break;
1245
1246 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001247 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001248 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001249 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001250 }
1251 }
1252
Sean Callanan79ed1a82010-01-19 20:22:31 +00001253 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001254 return false;
1255}
1256
1257/// ParseDirectiveSpace
1258/// ::= .space expression [ , expression ]
1259bool AsmParser::ParseDirectiveSpace() {
1260 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001261 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001262 return true;
1263
1264 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001265 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1266 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001267 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001268 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001269
Daniel Dunbar475839e2009-06-29 20:37:27 +00001270 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001271 return true;
1272
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001273 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001274 return TokError("unexpected token in '.space' directive");
1275 }
1276
Sean Callanan79ed1a82010-01-19 20:22:31 +00001277 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001278
1279 if (NumBytes <= 0)
1280 return TokError("invalid number of bytes in '.space' directive");
1281
1282 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001283 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001284
1285 return false;
1286}
1287
1288/// ParseDirectiveFill
1289/// ::= .fill expression , expression , expression
1290bool AsmParser::ParseDirectiveFill() {
1291 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001292 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001293 return true;
1294
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001295 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001296 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001297 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001298
1299 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001300 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001301 return true;
1302
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001303 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001304 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001305 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001306
1307 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001308 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001309 return true;
1310
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001311 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001312 return TokError("unexpected token in '.fill' directive");
1313
Sean Callanan79ed1a82010-01-19 20:22:31 +00001314 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001315
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001316 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1317 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001318
1319 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001320 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001321
1322 return false;
1323}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001324
1325/// ParseDirectiveOrg
1326/// ::= .org expression [ , expression ]
1327bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001328 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001329 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001330 return true;
1331
1332 // Parse optional fill expression.
1333 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001334 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1335 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001336 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001337 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001338
Daniel Dunbar475839e2009-06-29 20:37:27 +00001339 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001340 return true;
1341
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001342 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001343 return TokError("unexpected token in '.org' directive");
1344 }
1345
Sean Callanan79ed1a82010-01-19 20:22:31 +00001346 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001347
1348 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1349 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001350 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001351
1352 return false;
1353}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001354
1355/// ParseDirectiveAlign
1356/// ::= {.align, ...} expression [ , expression [ , expression ]]
1357bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001358 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001359 int64_t Alignment;
1360 if (ParseAbsoluteExpression(Alignment))
1361 return true;
1362
1363 SMLoc MaxBytesLoc;
1364 bool HasFillExpr = false;
1365 int64_t FillExpr = 0;
1366 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001367 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1368 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001369 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001370 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001371
1372 // The fill expression can be omitted while specifying a maximum number of
1373 // alignment bytes, e.g:
1374 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001375 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001376 HasFillExpr = true;
1377 if (ParseAbsoluteExpression(FillExpr))
1378 return true;
1379 }
1380
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001381 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1382 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001383 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001384 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001385
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001386 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001387 if (ParseAbsoluteExpression(MaxBytesToFill))
1388 return true;
1389
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001390 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001391 return TokError("unexpected token in directive");
1392 }
1393 }
1394
Sean Callanan79ed1a82010-01-19 20:22:31 +00001395 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001396
Daniel Dunbar648ac512010-05-17 21:54:30 +00001397 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001398 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001399
1400 // Compute alignment in bytes.
1401 if (IsPow2) {
1402 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001403 if (Alignment >= 32) {
1404 Error(AlignmentLoc, "invalid alignment value");
1405 Alignment = 31;
1406 }
1407
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001408 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001409 }
1410
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001411 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001412 if (MaxBytesLoc.isValid()) {
1413 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001414 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1415 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001416 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001417 }
1418
1419 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001420 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1421 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001422 MaxBytesToFill = 0;
1423 }
1424 }
1425
Daniel Dunbar648ac512010-05-17 21:54:30 +00001426 // Check whether we should use optimal code alignment for this .align
1427 // directive.
1428 //
1429 // FIXME: This should be using a target hook.
1430 bool UseCodeAlign = false;
1431 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001432 getStreamer().getCurrentSection()))
Chris Lattnera9558532010-07-15 21:19:31 +00001433 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001434 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1435 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001436 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001437 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001438 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001439 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1440 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001441 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001442
1443 return false;
1444}
1445
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001446/// ParseDirectiveSymbolAttribute
1447/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001448bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001449 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001450 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001451 StringRef Name;
1452
1453 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001454 return TokError("expected identifier in directive");
1455
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001456 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001457
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001458 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001459
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001460 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001461 break;
1462
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001463 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001464 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001465 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001466 }
1467 }
1468
Sean Callanan79ed1a82010-01-19 20:22:31 +00001469 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001470 return false;
1471}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001472
Matt Fleming924c5e52010-05-21 11:36:59 +00001473/// ParseDirectiveELFType
1474/// ::= .type identifier , @attribute
1475bool AsmParser::ParseDirectiveELFType() {
1476 StringRef Name;
1477 if (ParseIdentifier(Name))
1478 return TokError("expected identifier in directive");
1479
1480 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001481 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Matt Fleming924c5e52010-05-21 11:36:59 +00001482
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001483 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001484 return TokError("unexpected token in '.type' directive");
1485 Lex();
1486
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001487 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001488 return TokError("expected '@' before type");
1489 Lex();
1490
1491 StringRef Type;
1492 SMLoc TypeLoc;
1493
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001494 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001495 if (ParseIdentifier(Type))
1496 return TokError("expected symbol type in directive");
1497
1498 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1499 .Case("function", MCSA_ELF_TypeFunction)
1500 .Case("object", MCSA_ELF_TypeObject)
1501 .Case("tls_object", MCSA_ELF_TypeTLS)
1502 .Case("common", MCSA_ELF_TypeCommon)
1503 .Case("notype", MCSA_ELF_TypeNoType)
1504 .Default(MCSA_Invalid);
1505
1506 if (Attr == MCSA_Invalid)
1507 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1508
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001509 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001510 return TokError("unexpected token in '.type' directive");
1511
1512 Lex();
1513
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001514 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001515
1516 return false;
1517}
1518
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001519/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001520/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1521bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001522 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001523 StringRef Name;
1524 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001525 return TokError("expected identifier in directive");
1526
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001527 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001528 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001529
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001530 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001531 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001532 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001533
1534 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001535 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001536 if (ParseAbsoluteExpression(Size))
1537 return true;
1538
1539 int64_t Pow2Alignment = 0;
1540 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001541 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001542 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001543 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001544 if (ParseAbsoluteExpression(Pow2Alignment))
1545 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001546
1547 // If this target takes alignments in bytes (not log) validate and convert.
1548 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1549 if (!isPowerOf2_64(Pow2Alignment))
1550 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1551 Pow2Alignment = Log2_64(Pow2Alignment);
1552 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001553 }
1554
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001555 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001556 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001557
Sean Callanan79ed1a82010-01-19 20:22:31 +00001558 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001559
Chris Lattner1fc3d752009-07-09 17:25:12 +00001560 // NOTE: a size of zero for a .comm should create a undefined symbol
1561 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001562 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001563 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1564 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001565
Eric Christopherc260a3e2010-05-14 01:38:54 +00001566 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001567 // may internally end up wanting an alignment in bytes.
1568 // FIXME: Diagnose overflow.
1569 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001570 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1571 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001572
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001573 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001574 return Error(IDLoc, "invalid symbol redefinition");
1575
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001576 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001577 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001578 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001579 getStreamer().EmitZerofill(Ctx.getMachOSection(
1580 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1581 0, SectionKind::getBSS()),
1582 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001583 return false;
1584 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001585
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001586 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001587 return false;
1588}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001589
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001590/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001591/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001592bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001593 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001594 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001595
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001596 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001597 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001598 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001599
Sean Callanan79ed1a82010-01-19 20:22:31 +00001600 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001601
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001602 if (Str.empty())
1603 Error(Loc, ".abort detected. Assembly stopping.");
1604 else
1605 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001606 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001607
1608 return false;
1609}
Kevin Enderby71148242009-07-14 21:35:03 +00001610
Kevin Enderby1f049b22009-07-14 23:21:55 +00001611/// ParseDirectiveInclude
1612/// ::= .include "filename"
1613bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001614 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001615 return TokError("expected string in '.include' directive");
1616
Sean Callanan18b83232010-01-19 21:44:56 +00001617 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001618 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001619 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001620
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001621 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001622 return TokError("unexpected token in '.include' directive");
1623
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001624 // Strip the quotes.
1625 Filename = Filename.substr(1, Filename.size()-2);
1626
1627 // Attempt to switch the lexer to the included file before consuming the end
1628 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001629 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001630 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001631 return true;
1632 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001633
1634 return false;
1635}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001636
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001637/// ParseDirectiveIf
1638/// ::= .if expression
1639bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001640 TheCondStack.push_back(TheCondState);
1641 TheCondState.TheCond = AsmCond::IfCond;
1642 if(TheCondState.Ignore) {
1643 EatToEndOfStatement();
1644 }
1645 else {
1646 int64_t ExprValue;
1647 if (ParseAbsoluteExpression(ExprValue))
1648 return true;
1649
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001650 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001651 return TokError("unexpected token in '.if' directive");
1652
Sean Callanan79ed1a82010-01-19 20:22:31 +00001653 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001654
1655 TheCondState.CondMet = ExprValue;
1656 TheCondState.Ignore = !TheCondState.CondMet;
1657 }
1658
1659 return false;
1660}
1661
1662/// ParseDirectiveElseIf
1663/// ::= .elseif expression
1664bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1665 if (TheCondState.TheCond != AsmCond::IfCond &&
1666 TheCondState.TheCond != AsmCond::ElseIfCond)
1667 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1668 " an .elseif");
1669 TheCondState.TheCond = AsmCond::ElseIfCond;
1670
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001671 bool LastIgnoreState = false;
1672 if (!TheCondStack.empty())
1673 LastIgnoreState = TheCondStack.back().Ignore;
1674 if (LastIgnoreState || TheCondState.CondMet) {
1675 TheCondState.Ignore = true;
1676 EatToEndOfStatement();
1677 }
1678 else {
1679 int64_t ExprValue;
1680 if (ParseAbsoluteExpression(ExprValue))
1681 return true;
1682
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001683 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001684 return TokError("unexpected token in '.elseif' directive");
1685
Sean Callanan79ed1a82010-01-19 20:22:31 +00001686 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001687 TheCondState.CondMet = ExprValue;
1688 TheCondState.Ignore = !TheCondState.CondMet;
1689 }
1690
1691 return false;
1692}
1693
1694/// ParseDirectiveElse
1695/// ::= .else
1696bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001697 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001698 return TokError("unexpected token in '.else' directive");
1699
Sean Callanan79ed1a82010-01-19 20:22:31 +00001700 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001701
1702 if (TheCondState.TheCond != AsmCond::IfCond &&
1703 TheCondState.TheCond != AsmCond::ElseIfCond)
1704 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1705 ".elseif");
1706 TheCondState.TheCond = AsmCond::ElseCond;
1707 bool LastIgnoreState = false;
1708 if (!TheCondStack.empty())
1709 LastIgnoreState = TheCondStack.back().Ignore;
1710 if (LastIgnoreState || TheCondState.CondMet)
1711 TheCondState.Ignore = true;
1712 else
1713 TheCondState.Ignore = false;
1714
1715 return false;
1716}
1717
1718/// ParseDirectiveEndIf
1719/// ::= .endif
1720bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001721 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001722 return TokError("unexpected token in '.endif' directive");
1723
Sean Callanan79ed1a82010-01-19 20:22:31 +00001724 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001725
1726 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1727 TheCondStack.empty())
1728 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1729 ".else");
1730 if (!TheCondStack.empty()) {
1731 TheCondState = TheCondStack.back();
1732 TheCondStack.pop_back();
1733 }
1734
1735 return false;
1736}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001737
1738/// ParseDirectiveFile
1739/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001740bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001741 // FIXME: I'm not sure what this is.
1742 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001743 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00001744 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001745 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001746 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001747
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001748 if (FileNumber < 1)
1749 return TokError("file number less than one");
1750 }
1751
Daniel Dunbareceec052010-07-12 17:45:27 +00001752 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001753 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001754
Chris Lattnerd32e8032010-01-25 19:02:58 +00001755 StringRef Filename = getTok().getString();
1756 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001757 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001758
Daniel Dunbareceec052010-07-12 17:45:27 +00001759 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001760 return TokError("unexpected token in '.file' directive");
1761
Chris Lattnerd32e8032010-01-25 19:02:58 +00001762 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001763 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001764 else {
1765 if (getContext().GetDwarfFile(Filename, FileNumber) == 0)
1766 Error(FileNumberLoc, "file number already allocated");
Daniel Dunbareceec052010-07-12 17:45:27 +00001767 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001768 }
Daniel Dunbareceec052010-07-12 17:45:27 +00001769
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001770 return false;
1771}
1772
1773/// ParseDirectiveLine
1774/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001775bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001776 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1777 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001778 return TokError("unexpected token in '.line' directive");
1779
Sean Callanan18b83232010-01-19 21:44:56 +00001780 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001781 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001782 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001783
1784 // FIXME: Do something with the .line.
1785 }
1786
Daniel Dunbareceec052010-07-12 17:45:27 +00001787 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001788 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001789
1790 return false;
1791}
1792
1793
1794/// ParseDirectiveLoc
1795/// ::= .loc number [number [number]]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001796bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001797 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001798 return TokError("unexpected token in '.loc' directive");
1799
1800 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001801 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001802 (void) FileNumber;
1803 // FIXME: Validate file.
1804
Sean Callanan79ed1a82010-01-19 20:22:31 +00001805 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001806 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1807 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001808 return TokError("unexpected token in '.loc' directive");
1809
Sean Callanan18b83232010-01-19 21:44:56 +00001810 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001811 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001812 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001813
Daniel Dunbareceec052010-07-12 17:45:27 +00001814 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1815 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001816 return TokError("unexpected token in '.loc' directive");
1817
Sean Callanan18b83232010-01-19 21:44:56 +00001818 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001819 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001820 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001821
1822 // FIXME: Do something with the .loc.
1823 }
1824 }
1825
Daniel Dunbareceec052010-07-12 17:45:27 +00001826 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001827 return TokError("unexpected token in '.file' directive");
1828
1829 return false;
1830}
1831
Daniel Dunbar3c802de2010-07-18 18:38:02 +00001832/// ParseDirectiveMacrosOnOff
1833/// ::= .macros_on
1834/// ::= .macros_off
1835bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
1836 SMLoc DirectiveLoc) {
1837 if (getLexer().isNot(AsmToken::EndOfStatement))
1838 return Error(getLexer().getLoc(),
1839 "unexpected token in '" + Directive + "' directive");
1840
1841 getParser().MacrosEnabled = Directive == ".macros_on";
1842
1843 return false;
1844}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00001845
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00001846/// ParseDirectiveMacro
1847/// ::= .macro name
1848bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
1849 SMLoc DirectiveLoc) {
1850 StringRef Name;
1851 if (getParser().ParseIdentifier(Name))
1852 return TokError("expected identifier in directive");
1853
1854 if (getLexer().isNot(AsmToken::EndOfStatement))
1855 return TokError("unexpected token in '.macro' directive");
1856
1857 // Eat the end of statement.
1858 Lex();
1859
1860 AsmToken EndToken, StartToken = getTok();
1861
1862 // Lex the macro definition.
1863 for (;;) {
1864 // Check whether we have reached the end of the file.
1865 if (getLexer().is(AsmToken::Eof))
1866 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
1867
1868 // Otherwise, check whether we have reach the .endmacro.
1869 if (getLexer().is(AsmToken::Identifier) &&
1870 (getTok().getIdentifier() == ".endm" ||
1871 getTok().getIdentifier() == ".endmacro")) {
1872 EndToken = getTok();
1873 Lex();
1874 if (getLexer().isNot(AsmToken::EndOfStatement))
1875 return TokError("unexpected token in '" + EndToken.getIdentifier() +
1876 "' directive");
1877 break;
1878 }
1879
1880 // Otherwise, scan til the end of the statement.
1881 getParser().EatToEndOfStatement();
1882 }
1883
1884 if (getParser().MacroMap.lookup(Name)) {
1885 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
1886 }
1887
1888 const char *BodyStart = StartToken.getLoc().getPointer();
1889 const char *BodyEnd = EndToken.getLoc().getPointer();
1890 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
1891 getParser().MacroMap[Name] = new Macro(Name, Body);
1892 return false;
1893}
1894
1895/// ParseDirectiveEndMacro
1896/// ::= .endm
1897/// ::= .endmacro
1898bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
1899 SMLoc DirectiveLoc) {
1900 if (getLexer().isNot(AsmToken::EndOfStatement))
1901 return TokError("unexpected token in '" + Directive + "' directive");
1902
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001903 // If we are inside a macro instantiation, terminate the current
1904 // instantiation.
1905 if (!getParser().ActiveMacros.empty()) {
1906 getParser().HandleMacroExit();
1907 return false;
1908 }
1909
1910 // Otherwise, this .endmacro is a stray entry in the file; well formed
1911 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00001912 return TokError("unexpected '" + Directive + "' in file, "
1913 "no current macro definition");
1914}
1915
Daniel Dunbard1e3b442010-07-17 02:26:10 +00001916/// \brief Create an MCAsmParser instance.
1917MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
1918 MCContext &C, MCStreamer &Out,
1919 const MCAsmInfo &MAI) {
1920 return new AsmParser(T, SM, C, Out, MAI);
1921}