blob: 87a4a886bc845d3a84dc47ddc3119ccef68ed4a5 [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 Dunbar56491302010-07-29 01:51:55 +0000282 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
283
284 // Destroy any macros.
285 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
286 ie = MacroMap.end(); it != ie; ++it)
287 delete it->getValue();
288
Daniel Dunbare4749702010-07-12 18:12:02 +0000289 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000290 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000291}
292
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000293void AsmParser::PrintMacroInstantiations() {
294 // Print the active macro instantiation stack.
295 for (std::vector<MacroInstantiation*>::const_reverse_iterator
296 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
297 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
298 "note");
299}
300
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000301void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000302 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000303 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000304}
305
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000306bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000307 PrintMessage(L, Msg.str(), "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000308 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000309 return true;
310}
311
Sean Callananbf2013e2010-01-20 23:19:55 +0000312void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
313 const char *Type) const {
314 SrcMgr.PrintMessage(Loc, Msg, Type);
315}
Sean Callananfd0b0282010-01-21 00:19:58 +0000316
317bool AsmParser::EnterIncludeFile(const std::string &Filename) {
318 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
319 if (NewBuf == -1)
320 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000321
Sean Callananfd0b0282010-01-21 00:19:58 +0000322 CurBuffer = NewBuf;
323
324 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
325
326 return false;
327}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000328
329void AsmParser::JumpToLoc(SMLoc Loc) {
330 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
331 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
332}
333
Sean Callananfd0b0282010-01-21 00:19:58 +0000334const AsmToken &AsmParser::Lex() {
335 const AsmToken *tok = &Lexer.Lex();
336
337 if (tok->is(AsmToken::Eof)) {
338 // If this is the end of an included file, pop the parent file off the
339 // include stack.
340 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
341 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000342 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000343 tok = &Lexer.Lex();
344 }
345 }
346
347 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000348 Error(Lexer.getErrLoc(), Lexer.getErr());
Sean Callanan79036e42010-01-20 22:18:24 +0000349
Sean Callananfd0b0282010-01-21 00:19:58 +0000350 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000351}
352
Chris Lattner79180e22010-04-05 23:15:42 +0000353bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000354 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000355 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000356 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000357 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000358 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000359 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
360 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000361
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000362 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000363 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000364
Chris Lattnerb717fb02009-07-02 21:53:43 +0000365 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000366
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000367 AsmCond StartingCondState = TheCondState;
368
Chris Lattnerb717fb02009-07-02 21:53:43 +0000369 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000370 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000371 if (!ParseStatement()) continue;
372
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000373 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000374 HadError = true;
375 EatToEndOfStatement();
376 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000377
378 if (TheCondState.TheCond != StartingCondState.TheCond ||
379 TheCondState.Ignore != StartingCondState.Ignore)
380 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000381
382 // Check to see there are no empty DwarfFile slots.
383 const std::vector<MCDwarfFile *> &MCDwarfFiles =
384 getContext().getMCDwarfFiles();
385 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
386 if (!MCDwarfFiles[i]){
387 TokError("unassigned file number: " + Twine(i) + " for .file directives");
388 HadError = true;
389 }
390 }
Chris Lattnerb717fb02009-07-02 21:53:43 +0000391
Chris Lattner79180e22010-04-05 23:15:42 +0000392 // Finalize the output stream if there are no errors and if the client wants
393 // us to.
394 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000395 Out.Finish();
396
Chris Lattnerb717fb02009-07-02 21:53:43 +0000397 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000398}
399
Chris Lattner2cf5f142009-06-22 01:29:09 +0000400/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
401void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000402 while (Lexer.isNot(AsmToken::EndOfStatement) &&
403 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000404 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000405
406 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000407 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000408 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000409}
410
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000411StringRef AsmParser::ParseStringToEndOfStatement() {
412 const char *Start = getTok().getLoc().getPointer();
413
414 while (Lexer.isNot(AsmToken::EndOfStatement) &&
415 Lexer.isNot(AsmToken::Eof))
416 Lex();
417
418 const char *End = getTok().getLoc().getPointer();
419 return StringRef(Start, End - Start);
420}
Chris Lattnerc4193832009-06-22 05:51:26 +0000421
Chris Lattner74ec1a32009-06-22 06:32:03 +0000422/// ParseParenExpr - Parse a paren expression and return it.
423/// NOTE: This assumes the leading '(' has already been consumed.
424///
425/// parenexpr ::= expr)
426///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000427bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000428 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000429 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000430 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000431 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000432 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000433 return false;
434}
Chris Lattnerc4193832009-06-22 05:51:26 +0000435
Chris Lattner74ec1a32009-06-22 06:32:03 +0000436/// ParsePrimaryExpr - Parse a primary expression and return it.
437/// primaryexpr ::= (parenexpr
438/// primaryexpr ::= symbol
439/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000440/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000441/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000442bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000443 switch (Lexer.getKind()) {
444 default:
445 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000446 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000447 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000448 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000449 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000450 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000451 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000452 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000453 case AsmToken::Identifier: {
454 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000455 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000456 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000457
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000458 // Mark the symbol as used in an expression.
459 Sym->setUsedInExpr(true);
460
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000461 // Lookup the symbol variant if used.
462 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
463 if (Split.first.size() != getTok().getIdentifier().size())
464 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
465
Chris Lattnerb4307b32010-01-15 19:28:38 +0000466 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000467 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000468
469 // If this is an absolute variable reference, substitute it now to preserve
470 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000471 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000472 if (Variant)
473 return Error(EndLoc, "unexpected modified on variable reference");
474
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000475 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000476 return false;
477 }
478
479 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000480 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000481 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000482 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000483 case AsmToken::Integer: {
484 SMLoc Loc = getTok().getLoc();
485 int64_t IntVal = getTok().getIntVal();
486 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000487 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000488 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000489 // Look for 'b' or 'f' following an Integer as a directional label
490 if (Lexer.getKind() == AsmToken::Identifier) {
491 StringRef IDVal = getTok().getString();
492 if (IDVal == "f" || IDVal == "b"){
493 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
494 IDVal == "f" ? 1 : 0);
495 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
496 getContext());
497 if(IDVal == "b" && Sym->isUndefined())
498 return Error(Loc, "invalid reference to undefined symbol");
499 EndLoc = Lexer.getLoc();
500 Lex(); // Eat identifier.
501 }
502 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000503 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000504 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000505 case AsmToken::Dot: {
506 // This is a '.' reference, which references the current PC. Emit a
507 // temporary label to the streamer and refer to it.
508 MCSymbol *Sym = Ctx.CreateTempSymbol();
509 Out.EmitLabel(Sym);
510 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
511 EndLoc = Lexer.getLoc();
512 Lex(); // Eat identifier.
513 return false;
514 }
515
Daniel Dunbar3f872332009-07-28 16:08:33 +0000516 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000517 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000518 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000519 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000520 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000521 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000522 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000523 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000524 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000525 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000526 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000527 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000528 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000529 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000530 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000531 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000532 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000533 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000534 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000535 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000536 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000537 }
538}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000539
Chris Lattnerb4307b32010-01-15 19:28:38 +0000540bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000541 SMLoc EndLoc;
542 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000543}
544
Chris Lattner74ec1a32009-06-22 06:32:03 +0000545/// ParseExpression - Parse an expression and return it.
546///
547/// expr ::= expr +,- expr -> lowest.
548/// expr ::= expr |,^,&,! expr -> middle.
549/// expr ::= expr *,/,%,<<,>> expr -> highest.
550/// expr ::= primaryexpr
551///
Chris Lattner54482b42010-01-15 19:39:23 +0000552bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000553 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000554 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000555 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
556 return true;
557
558 // Try to constant fold it up front, if possible.
559 int64_t Value;
560 if (Res->EvaluateAsAbsolute(Value))
561 Res = MCConstantExpr::Create(Value, getContext());
562
563 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000564}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000565
Chris Lattnerb4307b32010-01-15 19:28:38 +0000566bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000567 Res = 0;
568 return ParseParenExpr(Res, EndLoc) ||
569 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000570}
571
Daniel Dunbar475839e2009-06-29 20:37:27 +0000572bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000573 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000574
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000575 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000576 if (ParseExpression(Expr))
577 return true;
578
Daniel Dunbare00b0112009-10-16 01:57:52 +0000579 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000580 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000581
582 return false;
583}
584
Daniel Dunbar3f872332009-07-28 16:08:33 +0000585static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000586 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000587 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000588 default:
589 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000590
591 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000592 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000593 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000594 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000595 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000596 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000597 return 1;
598
599 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000600 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000601 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000602 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000603 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000604 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000605 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000606 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000607 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000608 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000609 case AsmToken::ExclaimEqual:
610 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000611 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000612 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000613 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000614 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000615 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000616 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000617 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000618 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000619 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000620 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000621 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000622 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000623 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000624 return 2;
625
626 // Intermediate Precedence: |, &, ^
627 //
628 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000629 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000630 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000631 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000632 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000633 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000634 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000635 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000636 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000637 return 3;
638
639 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000640 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000641 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000642 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000643 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000644 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000645 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000646 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000647 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000648 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000649 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000650 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000651 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000652 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000653 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000654 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000655 }
656}
657
658
659/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
660/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000661bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
662 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000663 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000664 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000665 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000666
667 // If the next token is lower precedence than we are allowed to eat, return
668 // successfully with what we ate already.
669 if (TokPrec < Precedence)
670 return false;
671
Sean Callanan79ed1a82010-01-19 20:22:31 +0000672 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000673
674 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000675 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000676 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000677
678 // If BinOp binds less tightly with RHS than the operator after RHS, let
679 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000680 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000681 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000682 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000683 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000684 }
685
Daniel Dunbar475839e2009-06-29 20:37:27 +0000686 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000687 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000688 }
689}
690
Chris Lattnerc4193832009-06-22 05:51:26 +0000691
692
693
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000694/// ParseStatement:
695/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000696/// ::= Label* Directive ...Operands... EndOfStatement
697/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000698bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000699 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000700 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000701 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000702 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000703 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000704
705 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000706 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000707 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000708 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000709 int64_t LocalLabelVal = -1;
710 // GUESS allow an integer followed by a ':' as a directional local label
711 if (Lexer.is(AsmToken::Integer)) {
712 LocalLabelVal = getTok().getIntVal();
713 if (LocalLabelVal < 0) {
714 if (!TheCondState.Ignore)
715 return TokError("unexpected token at start of statement");
716 IDVal = "";
717 }
718 else {
719 IDVal = getTok().getString();
720 Lex(); // Consume the integer token to be used as an identifier token.
721 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000722 if (!TheCondState.Ignore)
723 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000724 }
725 }
726 }
727 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000728 if (!TheCondState.Ignore)
729 return TokError("unexpected token at start of statement");
730 IDVal = "";
731 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000732
Chris Lattner7834fac2010-04-17 18:14:27 +0000733 // Handle conditional assembly here before checking for skipping. We
734 // have to do this so that .endif isn't skipped in a ".if 0" block for
735 // example.
736 if (IDVal == ".if")
737 return ParseDirectiveIf(IDLoc);
738 if (IDVal == ".elseif")
739 return ParseDirectiveElseIf(IDLoc);
740 if (IDVal == ".else")
741 return ParseDirectiveElse(IDLoc);
742 if (IDVal == ".endif")
743 return ParseDirectiveEndIf(IDLoc);
744
745 // If we are in a ".if 0" block, ignore this statement.
746 if (TheCondState.Ignore) {
747 EatToEndOfStatement();
748 return false;
749 }
750
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000751 // FIXME: Recurse on local labels?
752
753 // See what kind of statement we have.
754 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000755 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000756 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000757 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000758
759 // Diagnose attempt to use a variable as a label.
760 //
761 // FIXME: Diagnostics. Note the location of the definition as a label.
762 // FIXME: This doesn't diagnose assignment to a symbol which has been
763 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000764 MCSymbol *Sym;
765 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000766 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000767 else
768 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000769 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000770 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000771
Daniel Dunbar959fd882009-08-26 22:13:22 +0000772 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000773 Out.EmitLabel(Sym);
774
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000775 // Consume any end of statement token, if present, to avoid spurious
776 // AddBlankLine calls().
777 if (Lexer.is(AsmToken::EndOfStatement)) {
778 Lex();
779 if (Lexer.is(AsmToken::Eof))
780 return false;
781 }
782
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000783 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000784 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000785
Daniel Dunbar3f872332009-07-28 16:08:33 +0000786 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000787 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000788 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000789
Daniel Dunbare2ace502009-08-31 08:09:09 +0000790 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000791
792 default: // Normal instruction or directive.
793 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000794 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000795
796 // If macros are enabled, check to see if this is a macro instantiation.
797 if (MacrosEnabled)
798 if (const Macro *M = MacroMap.lookup(IDVal))
799 return HandleMacroEntry(IDVal, IDLoc, M);
800
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000801 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000802 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000803 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000804 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000805 return ParseDirectiveSet();
806
Daniel Dunbara0d14262009-06-24 23:30:00 +0000807 // Data directives
808
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000809 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000810 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000811 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000812 return ParseDirectiveAscii(true);
813
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000814 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000815 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000816 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000817 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000818 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000819 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000820 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000821 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000822
Eli Friedman5d68ec22010-07-19 04:17:25 +0000823 if (IDVal == ".align") {
824 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
825 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
826 }
827 if (IDVal == ".align32") {
828 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
829 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
830 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000831 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000832 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000833 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000834 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000835 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000836 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000837 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000838 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000839 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000840 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000841 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000842 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
843
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000844 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000845 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000846
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000847 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000848 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000849 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000850 return ParseDirectiveSpace();
851
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000852 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000853
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000854 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000855 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000856 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000857 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000858 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000859 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000860 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000861 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000862 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000863 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000864 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000865 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000866 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000867 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000868 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000869 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000870 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000871 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000872 if (IDVal == ".type")
873 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000874 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000875 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000876 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000877 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000878 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000879 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000880 if (IDVal == ".weak_def_can_be_hidden")
881 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000882
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000883 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000884 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000885 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000886 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000887
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000888 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000889 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000890 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000891 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000892
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000893 // Look up the handler in the handler table.
894 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
895 DirectiveMap.lookup(IDVal);
896 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000897 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000898
Kevin Enderby9c656452009-09-10 20:51:44 +0000899 // Target hook for parsing target specific directives.
900 if (!getTargetParser().ParseDirective(ID))
901 return false;
902
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000903 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000904 EatToEndOfStatement();
905 return false;
906 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000907
Chris Lattnera7f13542010-05-19 23:34:33 +0000908 // Canonicalize the opcode to lower case.
909 SmallString<128> Opcode;
910 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
911 Opcode.push_back(tolower(IDVal[i]));
912
Chris Lattner98986712010-01-14 22:21:20 +0000913 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000914 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000915 ParsedOperands);
916 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
917 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000918
Daniel Dunbar3c14ca42010-08-11 06:37:09 +0000919 // Dump the parsed representation, if requested.
920 if (getShowParsedOperands()) {
921 SmallString<256> Str;
922 raw_svector_ostream OS(Str);
923 OS << "parsed instruction: [";
924 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
925 if (i != 0)
926 OS << ", ";
927 ParsedOperands[i]->dump(OS);
928 }
929 OS << "]";
930
931 PrintMessage(IDLoc, OS.str(), "note");
932 }
933
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000934 // If parsing succeeded, match the instruction.
935 if (!HadError) {
936 MCInst Inst;
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000937 if (!getTargetParser().MatchInstruction(IDLoc, ParsedOperands, Inst)) {
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000938 // Emit the instruction on success.
939 Out.EmitInstruction(Inst);
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000940 } else
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000941 HadError = true;
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000942 }
Chris Lattner98986712010-01-14 22:21:20 +0000943
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000944 // If there was no error, consume the end-of-statement token. Otherwise this
945 // will be done by our caller.
946 if (!HadError)
947 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000948
949 // Free any parsed operands.
950 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
951 delete ParsedOperands[i];
952
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000953 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000954}
Chris Lattner9a023f72009-06-24 04:43:34 +0000955
Daniel Dunbar7a570d02010-07-18 19:00:10 +0000956MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
957 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000958 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
959{
960 // Macro instantiation is lexical, unfortunately. We construct a new buffer
961 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +0000962 SmallString<256> Buf;
963 raw_svector_ostream OS(Buf);
964
965 StringRef Body = M->Body;
966 while (!Body.empty()) {
967 // Scan for the next substitution.
968 std::size_t End = Body.size(), Pos = 0;
969 for (; Pos != End; ++Pos) {
970 // Check for a substitution or escape.
971 if (Body[Pos] != '$' || Pos + 1 == End)
972 continue;
973
974 char Next = Body[Pos + 1];
975 if (Next == '$' || Next == 'n' || isdigit(Next))
976 break;
977 }
978
979 // Add the prefix.
980 OS << Body.slice(0, Pos);
981
982 // Check if we reached the end.
983 if (Pos == End)
984 break;
985
986 switch (Body[Pos+1]) {
987 // $$ => $
988 case '$':
989 OS << '$';
990 break;
991
992 // $n => number of arguments
993 case 'n':
994 OS << A.size();
995 break;
996
997 // $[0-9] => argument
998 default: {
999 // Missing arguments are ignored.
1000 unsigned Index = Body[Pos+1] - '0';
1001 if (Index >= A.size())
1002 break;
1003
1004 // Otherwise substitute with the token values, with spaces eliminated.
1005 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1006 ie = A[Index].end(); it != ie; ++it)
1007 OS << it->getString();
1008 break;
1009 }
1010 }
1011
1012 // Update the scan point.
1013 Body = Body.substr(Pos + 2);
1014 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001015
1016 // We include the .endmacro in the buffer as our queue to exit the macro
1017 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001018 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001019
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001020 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001021}
1022
1023bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1024 const Macro *M) {
1025 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1026 // this, although we should protect against infinite loops.
1027 if (ActiveMacros.size() == 20)
1028 return TokError("macros cannot be nested more than 20 levels deep");
1029
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001030 // Parse the macro instantiation arguments.
1031 std::vector<std::vector<AsmToken> > MacroArguments;
1032 MacroArguments.push_back(std::vector<AsmToken>());
1033 unsigned ParenLevel = 0;
1034 for (;;) {
1035 if (Lexer.is(AsmToken::Eof))
1036 return TokError("unexpected token in macro instantiation");
1037 if (Lexer.is(AsmToken::EndOfStatement))
1038 break;
1039
1040 // If we aren't inside parentheses and this is a comma, start a new token
1041 // list.
1042 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1043 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001044 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001045 // Adjust the current parentheses level.
1046 if (Lexer.is(AsmToken::LParen))
1047 ++ParenLevel;
1048 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1049 --ParenLevel;
1050
1051 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001052 MacroArguments.back().push_back(getTok());
1053 }
1054 Lex();
1055 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001056
1057 // Create the macro instantiation object and add to the current macro
1058 // instantiation stack.
1059 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001060 getTok().getLoc(),
1061 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001062 ActiveMacros.push_back(MI);
1063
1064 // Jump to the macro instantiation and prime the lexer.
1065 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1066 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1067 Lex();
1068
1069 return false;
1070}
1071
1072void AsmParser::HandleMacroExit() {
1073 // Jump to the EndOfStatement we should return to, and consume it.
1074 JumpToLoc(ActiveMacros.back()->ExitLoc);
1075 Lex();
1076
1077 // Pop the instantiation entry.
1078 delete ActiveMacros.back();
1079 ActiveMacros.pop_back();
1080}
1081
Benjamin Kramer38e59892010-07-14 22:38:02 +00001082bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001083 // FIXME: Use better location, we should use proper tokens.
1084 SMLoc EqualLoc = Lexer.getLoc();
1085
Daniel Dunbar821e3332009-08-31 08:09:28 +00001086 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001087 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001088 return true;
1089
Daniel Dunbar3f872332009-07-28 16:08:33 +00001090 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001091 return TokError("unexpected token in assignment");
1092
1093 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001094 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001095
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001096 // Validate that the LHS is allowed to be a variable (either it has not been
1097 // used as a symbol, or it is an absolute symbol).
1098 MCSymbol *Sym = getContext().LookupSymbol(Name);
1099 if (Sym) {
1100 // Diagnose assignment to a label.
1101 //
1102 // FIXME: Diagnostics. Note the location of the definition as a label.
1103 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001104 if (Sym->isUndefined() && !Sym->isUsedInExpr())
1105 ; // Allow redefinitions of undefined symbols only used in directives.
1106 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001107 return Error(EqualLoc, "redefinition of '" + Name + "'");
1108 else if (!Sym->isVariable())
1109 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001110 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001111 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1112 Name + "'");
1113 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001114 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001115
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001116 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001117
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001118 Sym->setUsedInExpr(true);
1119
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001120 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001121 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001122
1123 return false;
1124}
1125
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001126/// ParseIdentifier:
1127/// ::= identifier
1128/// ::= string
1129bool AsmParser::ParseIdentifier(StringRef &Res) {
1130 if (Lexer.isNot(AsmToken::Identifier) &&
1131 Lexer.isNot(AsmToken::String))
1132 return true;
1133
Sean Callanan18b83232010-01-19 21:44:56 +00001134 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001135
Sean Callanan79ed1a82010-01-19 20:22:31 +00001136 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001137
1138 return false;
1139}
1140
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001141/// ParseDirectiveSet:
1142/// ::= .set identifier ',' expression
1143bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001144 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001145
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001146 if (ParseIdentifier(Name))
1147 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001148
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001149 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001150 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001151 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001152
Daniel Dunbare2ace502009-08-31 08:09:09 +00001153 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001154}
1155
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001156bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001157 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001158
1159 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001160 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001161 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1162 if (Str[i] != '\\') {
1163 Data += Str[i];
1164 continue;
1165 }
1166
1167 // Recognize escaped characters. Note that this escape semantics currently
1168 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1169 ++i;
1170 if (i == e)
1171 return TokError("unexpected backslash at end of string");
1172
1173 // Recognize octal sequences.
1174 if ((unsigned) (Str[i] - '0') <= 7) {
1175 // Consume up to three octal characters.
1176 unsigned Value = Str[i] - '0';
1177
1178 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1179 ++i;
1180 Value = Value * 8 + (Str[i] - '0');
1181
1182 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1183 ++i;
1184 Value = Value * 8 + (Str[i] - '0');
1185 }
1186 }
1187
1188 if (Value > 255)
1189 return TokError("invalid octal escape sequence (out of range)");
1190
1191 Data += (unsigned char) Value;
1192 continue;
1193 }
1194
1195 // Otherwise recognize individual escapes.
1196 switch (Str[i]) {
1197 default:
1198 // Just reject invalid escape sequences for now.
1199 return TokError("invalid escape sequence (unrecognized character)");
1200
1201 case 'b': Data += '\b'; break;
1202 case 'f': Data += '\f'; break;
1203 case 'n': Data += '\n'; break;
1204 case 'r': Data += '\r'; break;
1205 case 't': Data += '\t'; break;
1206 case '"': Data += '"'; break;
1207 case '\\': Data += '\\'; break;
1208 }
1209 }
1210
1211 return false;
1212}
1213
Daniel Dunbara0d14262009-06-24 23:30:00 +00001214/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001215/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001216bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001217 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001218 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001219 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001220 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001221
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001222 std::string Data;
1223 if (ParseEscapedString(Data))
1224 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001225
1226 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001227 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001228 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1229
Sean Callanan79ed1a82010-01-19 20:22:31 +00001230 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001231
1232 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001233 break;
1234
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001235 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001236 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001237 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001238 }
1239 }
1240
Sean Callanan79ed1a82010-01-19 20:22:31 +00001241 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001242 return false;
1243}
1244
1245/// ParseDirectiveValue
1246/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1247bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001248 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001249 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001250 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001251 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001252 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001253 return true;
1254
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001255 // Special case constant expressions to match code generator.
1256 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001257 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001258 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001259 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001260
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001261 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001262 break;
1263
1264 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001265 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001266 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001267 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001268 }
1269 }
1270
Sean Callanan79ed1a82010-01-19 20:22:31 +00001271 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001272 return false;
1273}
1274
1275/// ParseDirectiveSpace
1276/// ::= .space expression [ , expression ]
1277bool AsmParser::ParseDirectiveSpace() {
1278 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001279 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001280 return true;
1281
1282 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001283 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1284 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001285 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001286 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001287
Daniel Dunbar475839e2009-06-29 20:37:27 +00001288 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001289 return true;
1290
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001291 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001292 return TokError("unexpected token in '.space' directive");
1293 }
1294
Sean Callanan79ed1a82010-01-19 20:22:31 +00001295 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001296
1297 if (NumBytes <= 0)
1298 return TokError("invalid number of bytes in '.space' directive");
1299
1300 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001301 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001302
1303 return false;
1304}
1305
1306/// ParseDirectiveFill
1307/// ::= .fill expression , expression , expression
1308bool AsmParser::ParseDirectiveFill() {
1309 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001310 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001311 return true;
1312
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001313 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001314 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001315 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001316
1317 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001318 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001319 return true;
1320
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001321 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001322 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001323 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001324
1325 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001326 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001327 return true;
1328
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001329 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001330 return TokError("unexpected token in '.fill' directive");
1331
Sean Callanan79ed1a82010-01-19 20:22:31 +00001332 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001333
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001334 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1335 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001336
1337 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001338 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001339
1340 return false;
1341}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001342
1343/// ParseDirectiveOrg
1344/// ::= .org expression [ , expression ]
1345bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001346 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001347 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001348 return true;
1349
1350 // Parse optional fill expression.
1351 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001352 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1353 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001354 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001355 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001356
Daniel Dunbar475839e2009-06-29 20:37:27 +00001357 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001358 return true;
1359
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001360 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001361 return TokError("unexpected token in '.org' directive");
1362 }
1363
Sean Callanan79ed1a82010-01-19 20:22:31 +00001364 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001365
1366 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1367 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001368 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001369
1370 return false;
1371}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001372
1373/// ParseDirectiveAlign
1374/// ::= {.align, ...} expression [ , expression [ , expression ]]
1375bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001376 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001377 int64_t Alignment;
1378 if (ParseAbsoluteExpression(Alignment))
1379 return true;
1380
1381 SMLoc MaxBytesLoc;
1382 bool HasFillExpr = false;
1383 int64_t FillExpr = 0;
1384 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001385 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1386 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001387 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001388 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001389
1390 // The fill expression can be omitted while specifying a maximum number of
1391 // alignment bytes, e.g:
1392 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001393 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001394 HasFillExpr = true;
1395 if (ParseAbsoluteExpression(FillExpr))
1396 return true;
1397 }
1398
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001399 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1400 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001401 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001402 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001403
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001404 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001405 if (ParseAbsoluteExpression(MaxBytesToFill))
1406 return true;
1407
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001408 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001409 return TokError("unexpected token in directive");
1410 }
1411 }
1412
Sean Callanan79ed1a82010-01-19 20:22:31 +00001413 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001414
Daniel Dunbar648ac512010-05-17 21:54:30 +00001415 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001416 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001417
1418 // Compute alignment in bytes.
1419 if (IsPow2) {
1420 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001421 if (Alignment >= 32) {
1422 Error(AlignmentLoc, "invalid alignment value");
1423 Alignment = 31;
1424 }
1425
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001426 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001427 }
1428
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001429 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001430 if (MaxBytesLoc.isValid()) {
1431 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001432 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1433 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001434 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001435 }
1436
1437 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001438 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1439 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001440 MaxBytesToFill = 0;
1441 }
1442 }
1443
Daniel Dunbar648ac512010-05-17 21:54:30 +00001444 // Check whether we should use optimal code alignment for this .align
1445 // directive.
1446 //
1447 // FIXME: This should be using a target hook.
1448 bool UseCodeAlign = false;
1449 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001450 getStreamer().getCurrentSection()))
Chris Lattnera9558532010-07-15 21:19:31 +00001451 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001452 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1453 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001454 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001455 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001456 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001457 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1458 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001459 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001460
1461 return false;
1462}
1463
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001464/// ParseDirectiveSymbolAttribute
1465/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001466bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001467 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001468 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001469 StringRef Name;
1470
1471 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001472 return TokError("expected identifier in directive");
1473
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001474 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001475
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001476 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001477
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001478 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001479 break;
1480
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001481 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001482 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001483 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001484 }
1485 }
1486
Sean Callanan79ed1a82010-01-19 20:22:31 +00001487 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001488 return false;
1489}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001490
Matt Fleming924c5e52010-05-21 11:36:59 +00001491/// ParseDirectiveELFType
1492/// ::= .type identifier , @attribute
1493bool AsmParser::ParseDirectiveELFType() {
1494 StringRef Name;
1495 if (ParseIdentifier(Name))
1496 return TokError("expected identifier in directive");
1497
1498 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001499 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Matt Fleming924c5e52010-05-21 11:36:59 +00001500
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001501 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001502 return TokError("unexpected token in '.type' directive");
1503 Lex();
1504
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001505 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001506 return TokError("expected '@' before type");
1507 Lex();
1508
1509 StringRef Type;
1510 SMLoc TypeLoc;
1511
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001512 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001513 if (ParseIdentifier(Type))
1514 return TokError("expected symbol type in directive");
1515
1516 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1517 .Case("function", MCSA_ELF_TypeFunction)
1518 .Case("object", MCSA_ELF_TypeObject)
1519 .Case("tls_object", MCSA_ELF_TypeTLS)
1520 .Case("common", MCSA_ELF_TypeCommon)
1521 .Case("notype", MCSA_ELF_TypeNoType)
1522 .Default(MCSA_Invalid);
1523
1524 if (Attr == MCSA_Invalid)
1525 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1526
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001527 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001528 return TokError("unexpected token in '.type' directive");
1529
1530 Lex();
1531
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001532 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001533
1534 return false;
1535}
1536
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001537/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001538/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1539bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001540 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001541 StringRef Name;
1542 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001543 return TokError("expected identifier in directive");
1544
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001545 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001546 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001547
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001548 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001549 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001550 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001551
1552 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001553 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001554 if (ParseAbsoluteExpression(Size))
1555 return true;
1556
1557 int64_t Pow2Alignment = 0;
1558 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001559 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001560 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001561 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001562 if (ParseAbsoluteExpression(Pow2Alignment))
1563 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001564
1565 // If this target takes alignments in bytes (not log) validate and convert.
1566 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1567 if (!isPowerOf2_64(Pow2Alignment))
1568 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1569 Pow2Alignment = Log2_64(Pow2Alignment);
1570 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001571 }
1572
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001573 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001574 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001575
Sean Callanan79ed1a82010-01-19 20:22:31 +00001576 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001577
Chris Lattner1fc3d752009-07-09 17:25:12 +00001578 // NOTE: a size of zero for a .comm should create a undefined symbol
1579 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001580 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001581 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1582 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001583
Eric Christopherc260a3e2010-05-14 01:38:54 +00001584 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001585 // may internally end up wanting an alignment in bytes.
1586 // FIXME: Diagnose overflow.
1587 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001588 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1589 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001590
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001591 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001592 return Error(IDLoc, "invalid symbol redefinition");
1593
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001594 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001595 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001596 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001597 getStreamer().EmitZerofill(Ctx.getMachOSection(
1598 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1599 0, SectionKind::getBSS()),
1600 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001601 return false;
1602 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001603
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001604 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001605 return false;
1606}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001607
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001608/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001609/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001610bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001611 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001612 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001613
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001614 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001615 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001616 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001617
Sean Callanan79ed1a82010-01-19 20:22:31 +00001618 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001619
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001620 if (Str.empty())
1621 Error(Loc, ".abort detected. Assembly stopping.");
1622 else
1623 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001624 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001625
1626 return false;
1627}
Kevin Enderby71148242009-07-14 21:35:03 +00001628
Kevin Enderby1f049b22009-07-14 23:21:55 +00001629/// ParseDirectiveInclude
1630/// ::= .include "filename"
1631bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001632 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001633 return TokError("expected string in '.include' directive");
1634
Sean Callanan18b83232010-01-19 21:44:56 +00001635 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001636 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001637 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001638
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001639 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001640 return TokError("unexpected token in '.include' directive");
1641
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001642 // Strip the quotes.
1643 Filename = Filename.substr(1, Filename.size()-2);
1644
1645 // Attempt to switch the lexer to the included file before consuming the end
1646 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001647 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001648 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001649 return true;
1650 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001651
1652 return false;
1653}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001654
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001655/// ParseDirectiveIf
1656/// ::= .if expression
1657bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001658 TheCondStack.push_back(TheCondState);
1659 TheCondState.TheCond = AsmCond::IfCond;
1660 if(TheCondState.Ignore) {
1661 EatToEndOfStatement();
1662 }
1663 else {
1664 int64_t ExprValue;
1665 if (ParseAbsoluteExpression(ExprValue))
1666 return true;
1667
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001668 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001669 return TokError("unexpected token in '.if' directive");
1670
Sean Callanan79ed1a82010-01-19 20:22:31 +00001671 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001672
1673 TheCondState.CondMet = ExprValue;
1674 TheCondState.Ignore = !TheCondState.CondMet;
1675 }
1676
1677 return false;
1678}
1679
1680/// ParseDirectiveElseIf
1681/// ::= .elseif expression
1682bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1683 if (TheCondState.TheCond != AsmCond::IfCond &&
1684 TheCondState.TheCond != AsmCond::ElseIfCond)
1685 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1686 " an .elseif");
1687 TheCondState.TheCond = AsmCond::ElseIfCond;
1688
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001689 bool LastIgnoreState = false;
1690 if (!TheCondStack.empty())
1691 LastIgnoreState = TheCondStack.back().Ignore;
1692 if (LastIgnoreState || TheCondState.CondMet) {
1693 TheCondState.Ignore = true;
1694 EatToEndOfStatement();
1695 }
1696 else {
1697 int64_t ExprValue;
1698 if (ParseAbsoluteExpression(ExprValue))
1699 return true;
1700
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001701 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001702 return TokError("unexpected token in '.elseif' directive");
1703
Sean Callanan79ed1a82010-01-19 20:22:31 +00001704 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001705 TheCondState.CondMet = ExprValue;
1706 TheCondState.Ignore = !TheCondState.CondMet;
1707 }
1708
1709 return false;
1710}
1711
1712/// ParseDirectiveElse
1713/// ::= .else
1714bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001715 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001716 return TokError("unexpected token in '.else' directive");
1717
Sean Callanan79ed1a82010-01-19 20:22:31 +00001718 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001719
1720 if (TheCondState.TheCond != AsmCond::IfCond &&
1721 TheCondState.TheCond != AsmCond::ElseIfCond)
1722 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1723 ".elseif");
1724 TheCondState.TheCond = AsmCond::ElseCond;
1725 bool LastIgnoreState = false;
1726 if (!TheCondStack.empty())
1727 LastIgnoreState = TheCondStack.back().Ignore;
1728 if (LastIgnoreState || TheCondState.CondMet)
1729 TheCondState.Ignore = true;
1730 else
1731 TheCondState.Ignore = false;
1732
1733 return false;
1734}
1735
1736/// ParseDirectiveEndIf
1737/// ::= .endif
1738bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001739 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001740 return TokError("unexpected token in '.endif' directive");
1741
Sean Callanan79ed1a82010-01-19 20:22:31 +00001742 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001743
1744 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1745 TheCondStack.empty())
1746 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1747 ".else");
1748 if (!TheCondStack.empty()) {
1749 TheCondState = TheCondStack.back();
1750 TheCondStack.pop_back();
1751 }
1752
1753 return false;
1754}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001755
1756/// ParseDirectiveFile
1757/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001758bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001759 // FIXME: I'm not sure what this is.
1760 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001761 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00001762 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001763 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001764 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001765
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001766 if (FileNumber < 1)
1767 return TokError("file number less than one");
1768 }
1769
Daniel Dunbareceec052010-07-12 17:45:27 +00001770 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001771 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001772
Chris Lattnerd32e8032010-01-25 19:02:58 +00001773 StringRef Filename = getTok().getString();
1774 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001775 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001776
Daniel Dunbareceec052010-07-12 17:45:27 +00001777 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001778 return TokError("unexpected token in '.file' directive");
1779
Chris Lattnerd32e8032010-01-25 19:02:58 +00001780 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001781 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001782 else {
1783 if (getContext().GetDwarfFile(Filename, FileNumber) == 0)
1784 Error(FileNumberLoc, "file number already allocated");
Daniel Dunbareceec052010-07-12 17:45:27 +00001785 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001786 }
Daniel Dunbareceec052010-07-12 17:45:27 +00001787
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001788 return false;
1789}
1790
1791/// ParseDirectiveLine
1792/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001793bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001794 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1795 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001796 return TokError("unexpected token in '.line' directive");
1797
Sean Callanan18b83232010-01-19 21:44:56 +00001798 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001799 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001800 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001801
1802 // FIXME: Do something with the .line.
1803 }
1804
Daniel Dunbareceec052010-07-12 17:45:27 +00001805 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001806 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001807
1808 return false;
1809}
1810
1811
1812/// ParseDirectiveLoc
1813/// ::= .loc number [number [number]]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001814bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001815 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001816 return TokError("unexpected token in '.loc' directive");
1817
1818 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001819 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001820 (void) FileNumber;
1821 // FIXME: Validate file.
1822
Sean Callanan79ed1a82010-01-19 20:22:31 +00001823 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001824 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1825 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001826 return TokError("unexpected token in '.loc' directive");
1827
Sean Callanan18b83232010-01-19 21:44:56 +00001828 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001829 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001830 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001831
Daniel Dunbareceec052010-07-12 17:45:27 +00001832 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1833 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001834 return TokError("unexpected token in '.loc' directive");
1835
Sean Callanan18b83232010-01-19 21:44:56 +00001836 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001837 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001838 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001839
1840 // FIXME: Do something with the .loc.
1841 }
1842 }
1843
Daniel Dunbareceec052010-07-12 17:45:27 +00001844 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001845 return TokError("unexpected token in '.file' directive");
1846
1847 return false;
1848}
1849
Daniel Dunbar3c802de2010-07-18 18:38:02 +00001850/// ParseDirectiveMacrosOnOff
1851/// ::= .macros_on
1852/// ::= .macros_off
1853bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
1854 SMLoc DirectiveLoc) {
1855 if (getLexer().isNot(AsmToken::EndOfStatement))
1856 return Error(getLexer().getLoc(),
1857 "unexpected token in '" + Directive + "' directive");
1858
1859 getParser().MacrosEnabled = Directive == ".macros_on";
1860
1861 return false;
1862}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00001863
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00001864/// ParseDirectiveMacro
1865/// ::= .macro name
1866bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
1867 SMLoc DirectiveLoc) {
1868 StringRef Name;
1869 if (getParser().ParseIdentifier(Name))
1870 return TokError("expected identifier in directive");
1871
1872 if (getLexer().isNot(AsmToken::EndOfStatement))
1873 return TokError("unexpected token in '.macro' directive");
1874
1875 // Eat the end of statement.
1876 Lex();
1877
1878 AsmToken EndToken, StartToken = getTok();
1879
1880 // Lex the macro definition.
1881 for (;;) {
1882 // Check whether we have reached the end of the file.
1883 if (getLexer().is(AsmToken::Eof))
1884 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
1885
1886 // Otherwise, check whether we have reach the .endmacro.
1887 if (getLexer().is(AsmToken::Identifier) &&
1888 (getTok().getIdentifier() == ".endm" ||
1889 getTok().getIdentifier() == ".endmacro")) {
1890 EndToken = getTok();
1891 Lex();
1892 if (getLexer().isNot(AsmToken::EndOfStatement))
1893 return TokError("unexpected token in '" + EndToken.getIdentifier() +
1894 "' directive");
1895 break;
1896 }
1897
1898 // Otherwise, scan til the end of the statement.
1899 getParser().EatToEndOfStatement();
1900 }
1901
1902 if (getParser().MacroMap.lookup(Name)) {
1903 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
1904 }
1905
1906 const char *BodyStart = StartToken.getLoc().getPointer();
1907 const char *BodyEnd = EndToken.getLoc().getPointer();
1908 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
1909 getParser().MacroMap[Name] = new Macro(Name, Body);
1910 return false;
1911}
1912
1913/// ParseDirectiveEndMacro
1914/// ::= .endm
1915/// ::= .endmacro
1916bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
1917 SMLoc DirectiveLoc) {
1918 if (getLexer().isNot(AsmToken::EndOfStatement))
1919 return TokError("unexpected token in '" + Directive + "' directive");
1920
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001921 // If we are inside a macro instantiation, terminate the current
1922 // instantiation.
1923 if (!getParser().ActiveMacros.empty()) {
1924 getParser().HandleMacroExit();
1925 return false;
1926 }
1927
1928 // Otherwise, this .endmacro is a stray entry in the file; well formed
1929 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00001930 return TokError("unexpected '" + Directive + "' in file, "
1931 "no current macro definition");
1932}
1933
Daniel Dunbard1e3b442010-07-17 02:26:10 +00001934/// \brief Create an MCAsmParser instance.
1935MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
1936 MCContext &C, MCStreamer &Out,
1937 const MCAsmInfo &MAI) {
1938 return new AsmParser(T, SM, C, Out, MAI);
1939}