blob: d101ad2c4bffec0480e0d181aef7ee3517390d1f [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 Dunbar93bd4d12010-09-09 22:42:56 +0000105 /// Flag tracking whether any errors have been encountered.
106 unsigned HadError : 1;
107
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000108public:
109 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
110 const MCAsmInfo &MAI);
111 ~AsmParser();
112
113 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
114
115 void AddDirectiveHandler(MCAsmParserExtension *Object,
116 StringRef Directive,
117 DirectiveHandler Handler) {
118 DirectiveMap[Directive] = std::make_pair(Object, Handler);
119 }
120
121public:
122 /// @name MCAsmParser Interface
123 /// {
124
125 virtual SourceMgr &getSourceManager() { return SrcMgr; }
126 virtual MCAsmLexer &getLexer() { return Lexer; }
127 virtual MCContext &getContext() { return Ctx; }
128 virtual MCStreamer &getStreamer() { return Out; }
129
130 virtual void Warning(SMLoc L, const Twine &Meg);
131 virtual bool Error(SMLoc L, const Twine &Msg);
132
133 const AsmToken &Lex();
134
135 bool ParseExpression(const MCExpr *&Res);
136 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
137 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
138 virtual bool ParseAbsoluteExpression(int64_t &Res);
139
140 /// }
141
142private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000143 void CheckForValidSection();
144
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000145 bool ParseStatement();
146
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000147 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
148 void HandleMacroExit();
149
150 void PrintMacroInstantiations();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000151 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
152
153 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
154 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000155
156 /// \brief Reset the current lexer position to that given by \arg Loc. The
157 /// current token is not set; clients should ensure Lex() is called
158 /// subsequently.
159 void JumpToLoc(SMLoc Loc);
160
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000161 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000162
163 /// \brief Parse up to the end of statement and a return the contents from the
164 /// current token until the end of the statement; the current token on exit
165 /// will be either the EndOfStatement or EOF.
166 StringRef ParseStringToEndOfStatement();
167
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000168 bool ParseAssignment(StringRef Name);
169
170 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
171 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
172 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
173
174 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
175 /// and set \arg Res to the identifier contents.
176 bool ParseIdentifier(StringRef &Res);
177
178 // Directive Parsing.
179 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
180 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
181 bool ParseDirectiveFill(); // ".fill"
182 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000183 bool ParseDirectiveZero(); // ".zero"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000184 bool ParseDirectiveSet(); // ".set"
185 bool ParseDirectiveOrg(); // ".org"
186 // ".align{,32}", ".p2align{,w,l}"
187 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
188
189 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
190 /// accepts a single symbol (which should be a label or an external).
191 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
192 bool ParseDirectiveELFType(); // ELF specific ".type"
193
194 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
195
196 bool ParseDirectiveAbort(); // ".abort"
197 bool ParseDirectiveInclude(); // ".include"
198
199 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
200 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
201 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
202 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
203
204 /// ParseEscapedString - Parse the current token as a string which may include
205 /// escaped characters and return the string contents.
206 bool ParseEscapedString(std::string &Data);
207};
208
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000209/// \brief Generic implementations of directive handling, etc. which is shared
210/// (or the default, at least) for all assembler parser.
211class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000212 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
213 void AddDirectiveHandler(StringRef Directive) {
214 getParser().AddDirectiveHandler(this, Directive,
215 HandleDirective<GenericAsmParser, Handler>);
216 }
217
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000218public:
219 GenericAsmParser() {}
220
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000221 AsmParser &getParser() {
222 return (AsmParser&) this->MCAsmParserExtension::getParser();
223 }
224
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000225 virtual void Initialize(MCAsmParser &Parser) {
226 // Call the base implementation.
227 this->MCAsmParserExtension::Initialize(Parser);
228
229 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000230 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
231 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
232 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000233
234 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000235 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
236 ".macros_on");
237 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
238 ".macros_off");
239 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
240 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
241 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000242
243 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
244 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000245 }
246
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000247 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
248 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
249 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000250
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000251 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000252 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
253 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000254
255 void ParseUleb128(uint64_t Value);
256 void ParseSleb128(int64_t Value);
257 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000258};
259
260}
261
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000262namespace llvm {
263
264extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000265extern MCAsmParserExtension *createELFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000266
267}
268
Chris Lattneraaec2052010-01-19 19:46:13 +0000269enum { DEFAULT_ADDRSPACE = 0 };
270
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000271AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
272 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000273 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000274 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000275 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000276 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000277
278 // Initialize the generic parser.
279 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000280
281 // Initialize the platform / file format parser.
282 //
283 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
284 // created.
285 if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000286 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000287 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000288 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000289 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000290 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000291 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000292}
293
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000294AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000295 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
296
297 // Destroy any macros.
298 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
299 ie = MacroMap.end(); it != ie; ++it)
300 delete it->getValue();
301
Daniel Dunbare4749702010-07-12 18:12:02 +0000302 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000303 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000304}
305
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000306void AsmParser::PrintMacroInstantiations() {
307 // Print the active macro instantiation stack.
308 for (std::vector<MacroInstantiation*>::const_reverse_iterator
309 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
310 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
311 "note");
312}
313
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000314void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000315 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000316 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000317}
318
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000319bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000320 HadError = true;
Sean Callananbf2013e2010-01-20 23:19:55 +0000321 PrintMessage(L, Msg.str(), "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000322 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000323 return true;
324}
325
Sean Callananbf2013e2010-01-20 23:19:55 +0000326void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
327 const char *Type) const {
328 SrcMgr.PrintMessage(Loc, Msg, Type);
329}
Sean Callananfd0b0282010-01-21 00:19:58 +0000330
331bool AsmParser::EnterIncludeFile(const std::string &Filename) {
332 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
333 if (NewBuf == -1)
334 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000335
Sean Callananfd0b0282010-01-21 00:19:58 +0000336 CurBuffer = NewBuf;
337
338 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
339
340 return false;
341}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000342
343void AsmParser::JumpToLoc(SMLoc Loc) {
344 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
345 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
346}
347
Sean Callananfd0b0282010-01-21 00:19:58 +0000348const AsmToken &AsmParser::Lex() {
349 const AsmToken *tok = &Lexer.Lex();
350
351 if (tok->is(AsmToken::Eof)) {
352 // If this is the end of an included file, pop the parent file off the
353 // include stack.
354 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
355 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000356 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000357 tok = &Lexer.Lex();
358 }
359 }
360
361 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000362 Error(Lexer.getErrLoc(), Lexer.getErr());
Sean Callanan79036e42010-01-20 22:18:24 +0000363
Sean Callananfd0b0282010-01-21 00:19:58 +0000364 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000365}
366
Chris Lattner79180e22010-04-05 23:15:42 +0000367bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000368 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000369 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000370 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000371
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000372 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000373 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000374
375 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000376 AsmCond StartingCondState = TheCondState;
377
Chris Lattnerb717fb02009-07-02 21:53:43 +0000378 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000379 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000380 if (!ParseStatement()) continue;
381
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000382 // We had an error, validate that one was emitted and recover by skipping to
383 // the next line.
384 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000385 EatToEndOfStatement();
386 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000387
388 if (TheCondState.TheCond != StartingCondState.TheCond ||
389 TheCondState.Ignore != StartingCondState.Ignore)
390 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000391
392 // Check to see there are no empty DwarfFile slots.
393 const std::vector<MCDwarfFile *> &MCDwarfFiles =
394 getContext().getMCDwarfFiles();
395 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000396 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000397 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000398 }
Chris Lattnerb717fb02009-07-02 21:53:43 +0000399
Chris Lattner79180e22010-04-05 23:15:42 +0000400 // Finalize the output stream if there are no errors and if the client wants
401 // us to.
402 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000403 Out.Finish();
404
Chris Lattnerb717fb02009-07-02 21:53:43 +0000405 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000406}
407
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000408void AsmParser::CheckForValidSection() {
409 if (!getStreamer().getCurrentSection()) {
410 TokError("expected section directive before assembly directive");
411 Out.SwitchSection(Ctx.getMachOSection(
412 "__TEXT", "__text",
413 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
414 0, SectionKind::getText()));
415 }
416}
417
Chris Lattner2cf5f142009-06-22 01:29:09 +0000418/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
419void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000420 while (Lexer.isNot(AsmToken::EndOfStatement) &&
421 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000422 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000423
424 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000425 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000426 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000427}
428
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000429StringRef AsmParser::ParseStringToEndOfStatement() {
430 const char *Start = getTok().getLoc().getPointer();
431
432 while (Lexer.isNot(AsmToken::EndOfStatement) &&
433 Lexer.isNot(AsmToken::Eof))
434 Lex();
435
436 const char *End = getTok().getLoc().getPointer();
437 return StringRef(Start, End - Start);
438}
Chris Lattnerc4193832009-06-22 05:51:26 +0000439
Chris Lattner74ec1a32009-06-22 06:32:03 +0000440/// ParseParenExpr - Parse a paren expression and return it.
441/// NOTE: This assumes the leading '(' has already been consumed.
442///
443/// parenexpr ::= expr)
444///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000445bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000446 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000447 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000448 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000449 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000450 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000451 return false;
452}
Chris Lattnerc4193832009-06-22 05:51:26 +0000453
Chris Lattner74ec1a32009-06-22 06:32:03 +0000454/// ParsePrimaryExpr - Parse a primary expression and return it.
455/// primaryexpr ::= (parenexpr
456/// primaryexpr ::= symbol
457/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000458/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000459/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000460bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000461 switch (Lexer.getKind()) {
462 default:
463 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000464 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000465 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000466 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000467 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000468 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000469 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000470 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000471 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000472 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000473 EndLoc = Lexer.getLoc();
474
475 StringRef Identifier;
476 if (ParseIdentifier(Identifier))
477 return false;
478
Daniel Dunbarfffff912009-10-16 01:34:54 +0000479 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000480 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000481 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000482
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000483 // Mark the symbol as used in an expression.
484 Sym->setUsedInExpr(true);
485
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000486 // Lookup the symbol variant if used.
487 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000488 if (Split.first.size() != Identifier.size())
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000489 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
490
Daniel Dunbarfffff912009-10-16 01:34:54 +0000491 // If this is an absolute variable reference, substitute it now to preserve
492 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000493 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000494 if (Variant)
495 return Error(EndLoc, "unexpected modified on variable reference");
496
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000497 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000498 return false;
499 }
500
501 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000502 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000503 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000504 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000505 case AsmToken::Integer: {
506 SMLoc Loc = getTok().getLoc();
507 int64_t IntVal = getTok().getIntVal();
508 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000509 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000510 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000511 // Look for 'b' or 'f' following an Integer as a directional label
512 if (Lexer.getKind() == AsmToken::Identifier) {
513 StringRef IDVal = getTok().getString();
514 if (IDVal == "f" || IDVal == "b"){
515 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
516 IDVal == "f" ? 1 : 0);
517 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
518 getContext());
519 if(IDVal == "b" && Sym->isUndefined())
520 return Error(Loc, "invalid reference to undefined symbol");
521 EndLoc = Lexer.getLoc();
522 Lex(); // Eat identifier.
523 }
524 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000525 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000526 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000527 case AsmToken::Dot: {
528 // This is a '.' reference, which references the current PC. Emit a
529 // temporary label to the streamer and refer to it.
530 MCSymbol *Sym = Ctx.CreateTempSymbol();
531 Out.EmitLabel(Sym);
532 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
533 EndLoc = Lexer.getLoc();
534 Lex(); // Eat identifier.
535 return false;
536 }
537
Daniel Dunbar3f872332009-07-28 16:08:33 +0000538 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000539 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000540 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000541 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000542 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000543 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000544 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000545 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000546 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000547 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000548 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000549 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000550 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000551 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000552 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000553 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000554 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000555 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000556 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000557 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000558 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000559 }
560}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000561
Chris Lattnerb4307b32010-01-15 19:28:38 +0000562bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000563 SMLoc EndLoc;
564 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000565}
566
Chris Lattner74ec1a32009-06-22 06:32:03 +0000567/// ParseExpression - Parse an expression and return it.
568///
569/// expr ::= expr +,- expr -> lowest.
570/// expr ::= expr |,^,&,! expr -> middle.
571/// expr ::= expr *,/,%,<<,>> expr -> highest.
572/// expr ::= primaryexpr
573///
Chris Lattner54482b42010-01-15 19:39:23 +0000574bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000575 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000576 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000577 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
578 return true;
579
580 // Try to constant fold it up front, if possible.
581 int64_t Value;
582 if (Res->EvaluateAsAbsolute(Value))
583 Res = MCConstantExpr::Create(Value, getContext());
584
585 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000586}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000587
Chris Lattnerb4307b32010-01-15 19:28:38 +0000588bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000589 Res = 0;
590 return ParseParenExpr(Res, EndLoc) ||
591 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000592}
593
Daniel Dunbar475839e2009-06-29 20:37:27 +0000594bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000595 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000596
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000597 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000598 if (ParseExpression(Expr))
599 return true;
600
Daniel Dunbare00b0112009-10-16 01:57:52 +0000601 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000602 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000603
604 return false;
605}
606
Daniel Dunbar3f872332009-07-28 16:08:33 +0000607static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000608 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000609 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000610 default:
611 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000612
613 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000614 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000615 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000616 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000617 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000618 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000619 return 1;
620
621 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000622 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000623 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000624 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000625 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000626 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000627 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000628 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000629 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000630 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000631 case AsmToken::ExclaimEqual:
632 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000633 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000634 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000635 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000636 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000637 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000638 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000639 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000640 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000641 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000642 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000643 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000644 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000645 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000646 return 2;
647
648 // Intermediate Precedence: |, &, ^
649 //
650 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000651 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000652 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000653 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000654 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000655 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000656 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000657 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000658 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000659 return 3;
660
661 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000662 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000663 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000664 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000665 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000666 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000667 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000668 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000669 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000670 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000671 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000672 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000673 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000674 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000675 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000676 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000677 }
678}
679
680
681/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
682/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000683bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
684 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000685 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000686 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000687 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000688
689 // If the next token is lower precedence than we are allowed to eat, return
690 // successfully with what we ate already.
691 if (TokPrec < Precedence)
692 return false;
693
Sean Callanan79ed1a82010-01-19 20:22:31 +0000694 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000695
696 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000697 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000698 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000699
700 // If BinOp binds less tightly with RHS than the operator after RHS, let
701 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000702 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000703 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000704 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000705 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000706 }
707
Daniel Dunbar475839e2009-06-29 20:37:27 +0000708 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000709 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000710 }
711}
712
Chris Lattnerc4193832009-06-22 05:51:26 +0000713
714
715
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000716/// ParseStatement:
717/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000718/// ::= Label* Directive ...Operands... EndOfStatement
719/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000720bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000721 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000722 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000723 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000724 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000725 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000726
727 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000728 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000729 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000730 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000731 int64_t LocalLabelVal = -1;
732 // GUESS allow an integer followed by a ':' as a directional local label
733 if (Lexer.is(AsmToken::Integer)) {
734 LocalLabelVal = getTok().getIntVal();
735 if (LocalLabelVal < 0) {
736 if (!TheCondState.Ignore)
737 return TokError("unexpected token at start of statement");
738 IDVal = "";
739 }
740 else {
741 IDVal = getTok().getString();
742 Lex(); // Consume the integer token to be used as an identifier token.
743 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000744 if (!TheCondState.Ignore)
745 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000746 }
747 }
748 }
749 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000750 if (!TheCondState.Ignore)
751 return TokError("unexpected token at start of statement");
752 IDVal = "";
753 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000754
Chris Lattner7834fac2010-04-17 18:14:27 +0000755 // Handle conditional assembly here before checking for skipping. We
756 // have to do this so that .endif isn't skipped in a ".if 0" block for
757 // example.
758 if (IDVal == ".if")
759 return ParseDirectiveIf(IDLoc);
760 if (IDVal == ".elseif")
761 return ParseDirectiveElseIf(IDLoc);
762 if (IDVal == ".else")
763 return ParseDirectiveElse(IDLoc);
764 if (IDVal == ".endif")
765 return ParseDirectiveEndIf(IDLoc);
766
767 // If we are in a ".if 0" block, ignore this statement.
768 if (TheCondState.Ignore) {
769 EatToEndOfStatement();
770 return false;
771 }
772
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000773 // FIXME: Recurse on local labels?
774
775 // See what kind of statement we have.
776 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000777 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000778 CheckForValidSection();
779
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000780 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000781 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000782
783 // Diagnose attempt to use a variable as a label.
784 //
785 // FIXME: Diagnostics. Note the location of the definition as a label.
786 // FIXME: This doesn't diagnose assignment to a symbol which has been
787 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000788 MCSymbol *Sym;
789 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000790 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000791 else
792 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000793 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000794 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000795
Daniel Dunbar959fd882009-08-26 22:13:22 +0000796 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000797 Out.EmitLabel(Sym);
798
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000799 // Consume any end of statement token, if present, to avoid spurious
800 // AddBlankLine calls().
801 if (Lexer.is(AsmToken::EndOfStatement)) {
802 Lex();
803 if (Lexer.is(AsmToken::Eof))
804 return false;
805 }
806
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000807 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000808 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000809
Daniel Dunbar3f872332009-07-28 16:08:33 +0000810 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000811 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000812 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000813
Daniel Dunbare2ace502009-08-31 08:09:09 +0000814 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000815
816 default: // Normal instruction or directive.
817 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000818 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000819
820 // If macros are enabled, check to see if this is a macro instantiation.
821 if (MacrosEnabled)
822 if (const Macro *M = MacroMap.lookup(IDVal))
823 return HandleMacroEntry(IDVal, IDLoc, M);
824
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000825 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000826 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000827 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000828 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000829 return ParseDirectiveSet();
830
Daniel Dunbara0d14262009-06-24 23:30:00 +0000831 // Data directives
832
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000833 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000834 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000835 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000836 return ParseDirectiveAscii(true);
837
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000838 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000839 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000840 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000841 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000842 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000843 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000844 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000845 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000846
Eli Friedman5d68ec22010-07-19 04:17:25 +0000847 if (IDVal == ".align") {
848 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
849 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
850 }
851 if (IDVal == ".align32") {
852 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
853 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
854 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000855 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000856 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000857 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000858 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000859 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000860 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000861 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000862 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000863 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000864 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000865 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000866 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
867
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000868 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000869 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000870
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000871 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000872 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000873 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000874 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000875 if (IDVal == ".zero")
876 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000877
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000878 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000879
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000880 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000881 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000882 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000883 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000884 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000885 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000886 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000887 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000888 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000889 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000890 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000891 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000892 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000893 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000894 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000895 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000896 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000897 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000898 if (IDVal == ".type")
899 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000900 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000901 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000902 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000903 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000904 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000905 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000906 if (IDVal == ".weak_def_can_be_hidden")
907 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000908
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000909 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000910 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000911 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000912 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000913
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000914 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000915 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000916 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000917 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000918
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000919 // Look up the handler in the handler table.
920 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
921 DirectiveMap.lookup(IDVal);
922 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000923 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000924
Kevin Enderby9c656452009-09-10 20:51:44 +0000925 // Target hook for parsing target specific directives.
926 if (!getTargetParser().ParseDirective(ID))
927 return false;
928
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000929 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000930 EatToEndOfStatement();
931 return false;
932 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000933
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000934 CheckForValidSection();
935
Chris Lattnera7f13542010-05-19 23:34:33 +0000936 // Canonicalize the opcode to lower case.
937 SmallString<128> Opcode;
938 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
939 Opcode.push_back(tolower(IDVal[i]));
940
Chris Lattner98986712010-01-14 22:21:20 +0000941 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000942 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000943 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000944
Daniel Dunbar3c14ca42010-08-11 06:37:09 +0000945 // Dump the parsed representation, if requested.
946 if (getShowParsedOperands()) {
947 SmallString<256> Str;
948 raw_svector_ostream OS(Str);
949 OS << "parsed instruction: [";
950 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
951 if (i != 0)
952 OS << ", ";
953 ParsedOperands[i]->dump(OS);
954 }
955 OS << "]";
956
957 PrintMessage(IDLoc, OS.str(), "note");
958 }
959
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000960 // If parsing succeeded, match the instruction.
961 if (!HadError) {
962 MCInst Inst;
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000963 if (!getTargetParser().MatchInstruction(IDLoc, ParsedOperands, Inst)) {
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000964 // Emit the instruction on success.
965 Out.EmitInstruction(Inst);
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000966 } else
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000967 HadError = true;
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000968 }
Chris Lattner98986712010-01-14 22:21:20 +0000969
Chris Lattner98986712010-01-14 22:21:20 +0000970 // Free any parsed operands.
971 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
972 delete ParsedOperands[i];
973
Chris Lattnercbf8a982010-09-11 16:18:25 +0000974 // Don't skip the rest of the line, the instruction parser is responsible for
975 // that.
976 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000977}
Chris Lattner9a023f72009-06-24 04:43:34 +0000978
Daniel Dunbar7a570d02010-07-18 19:00:10 +0000979MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
980 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000981 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
982{
983 // Macro instantiation is lexical, unfortunately. We construct a new buffer
984 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +0000985 SmallString<256> Buf;
986 raw_svector_ostream OS(Buf);
987
988 StringRef Body = M->Body;
989 while (!Body.empty()) {
990 // Scan for the next substitution.
991 std::size_t End = Body.size(), Pos = 0;
992 for (; Pos != End; ++Pos) {
993 // Check for a substitution or escape.
994 if (Body[Pos] != '$' || Pos + 1 == End)
995 continue;
996
997 char Next = Body[Pos + 1];
998 if (Next == '$' || Next == 'n' || isdigit(Next))
999 break;
1000 }
1001
1002 // Add the prefix.
1003 OS << Body.slice(0, Pos);
1004
1005 // Check if we reached the end.
1006 if (Pos == End)
1007 break;
1008
1009 switch (Body[Pos+1]) {
1010 // $$ => $
1011 case '$':
1012 OS << '$';
1013 break;
1014
1015 // $n => number of arguments
1016 case 'n':
1017 OS << A.size();
1018 break;
1019
1020 // $[0-9] => argument
1021 default: {
1022 // Missing arguments are ignored.
1023 unsigned Index = Body[Pos+1] - '0';
1024 if (Index >= A.size())
1025 break;
1026
1027 // Otherwise substitute with the token values, with spaces eliminated.
1028 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1029 ie = A[Index].end(); it != ie; ++it)
1030 OS << it->getString();
1031 break;
1032 }
1033 }
1034
1035 // Update the scan point.
1036 Body = Body.substr(Pos + 2);
1037 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001038
1039 // We include the .endmacro in the buffer as our queue to exit the macro
1040 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001041 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001042
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001043 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001044}
1045
1046bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1047 const Macro *M) {
1048 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1049 // this, although we should protect against infinite loops.
1050 if (ActiveMacros.size() == 20)
1051 return TokError("macros cannot be nested more than 20 levels deep");
1052
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001053 // Parse the macro instantiation arguments.
1054 std::vector<std::vector<AsmToken> > MacroArguments;
1055 MacroArguments.push_back(std::vector<AsmToken>());
1056 unsigned ParenLevel = 0;
1057 for (;;) {
1058 if (Lexer.is(AsmToken::Eof))
1059 return TokError("unexpected token in macro instantiation");
1060 if (Lexer.is(AsmToken::EndOfStatement))
1061 break;
1062
1063 // If we aren't inside parentheses and this is a comma, start a new token
1064 // list.
1065 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1066 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001067 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001068 // Adjust the current parentheses level.
1069 if (Lexer.is(AsmToken::LParen))
1070 ++ParenLevel;
1071 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1072 --ParenLevel;
1073
1074 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001075 MacroArguments.back().push_back(getTok());
1076 }
1077 Lex();
1078 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001079
1080 // Create the macro instantiation object and add to the current macro
1081 // instantiation stack.
1082 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001083 getTok().getLoc(),
1084 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001085 ActiveMacros.push_back(MI);
1086
1087 // Jump to the macro instantiation and prime the lexer.
1088 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1089 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1090 Lex();
1091
1092 return false;
1093}
1094
1095void AsmParser::HandleMacroExit() {
1096 // Jump to the EndOfStatement we should return to, and consume it.
1097 JumpToLoc(ActiveMacros.back()->ExitLoc);
1098 Lex();
1099
1100 // Pop the instantiation entry.
1101 delete ActiveMacros.back();
1102 ActiveMacros.pop_back();
1103}
1104
Benjamin Kramer38e59892010-07-14 22:38:02 +00001105bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001106 // FIXME: Use better location, we should use proper tokens.
1107 SMLoc EqualLoc = Lexer.getLoc();
1108
Daniel Dunbar821e3332009-08-31 08:09:28 +00001109 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001110 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001111 return true;
1112
Daniel Dunbar3f872332009-07-28 16:08:33 +00001113 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001114 return TokError("unexpected token in assignment");
1115
1116 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001117 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001118
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001119 // Validate that the LHS is allowed to be a variable (either it has not been
1120 // used as a symbol, or it is an absolute symbol).
1121 MCSymbol *Sym = getContext().LookupSymbol(Name);
1122 if (Sym) {
1123 // Diagnose assignment to a label.
1124 //
1125 // FIXME: Diagnostics. Note the location of the definition as a label.
1126 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001127 if (Sym->isUndefined() && !Sym->isUsedInExpr())
1128 ; // Allow redefinitions of undefined symbols only used in directives.
1129 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001130 return Error(EqualLoc, "redefinition of '" + Name + "'");
1131 else if (!Sym->isVariable())
1132 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001133 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001134 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1135 Name + "'");
1136 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001137 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001138
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001139 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001140
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001141 Sym->setUsedInExpr(true);
1142
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001143 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001144 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001145
1146 return false;
1147}
1148
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001149/// ParseIdentifier:
1150/// ::= identifier
1151/// ::= string
1152bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001153 // The assembler has relaxed rules for accepting identifiers, in particular we
1154 // allow things like '.globl $foo', which would normally be separate
1155 // tokens. At this level, we have already lexed so we cannot (currently)
1156 // handle this as a context dependent token, instead we detect adjacent tokens
1157 // and return the combined identifier.
1158 if (Lexer.is(AsmToken::Dollar)) {
1159 SMLoc DollarLoc = getLexer().getLoc();
1160
1161 // Consume the dollar sign, and check for a following identifier.
1162 Lex();
1163 if (Lexer.isNot(AsmToken::Identifier))
1164 return true;
1165
1166 // We have a '$' followed by an identifier, make sure they are adjacent.
1167 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1168 return true;
1169
1170 // Construct the joined identifier and consume the token.
1171 Res = StringRef(DollarLoc.getPointer(),
1172 getTok().getIdentifier().size() + 1);
1173 Lex();
1174 return false;
1175 }
1176
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001177 if (Lexer.isNot(AsmToken::Identifier) &&
1178 Lexer.isNot(AsmToken::String))
1179 return true;
1180
Sean Callanan18b83232010-01-19 21:44:56 +00001181 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001182
Sean Callanan79ed1a82010-01-19 20:22:31 +00001183 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001184
1185 return false;
1186}
1187
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001188/// ParseDirectiveSet:
1189/// ::= .set identifier ',' expression
1190bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001191 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001192
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001193 if (ParseIdentifier(Name))
1194 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001195
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001196 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001197 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001198 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001199
Daniel Dunbare2ace502009-08-31 08:09:09 +00001200 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001201}
1202
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001203bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001204 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001205
1206 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001207 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001208 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1209 if (Str[i] != '\\') {
1210 Data += Str[i];
1211 continue;
1212 }
1213
1214 // Recognize escaped characters. Note that this escape semantics currently
1215 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1216 ++i;
1217 if (i == e)
1218 return TokError("unexpected backslash at end of string");
1219
1220 // Recognize octal sequences.
1221 if ((unsigned) (Str[i] - '0') <= 7) {
1222 // Consume up to three octal characters.
1223 unsigned Value = Str[i] - '0';
1224
1225 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1226 ++i;
1227 Value = Value * 8 + (Str[i] - '0');
1228
1229 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1230 ++i;
1231 Value = Value * 8 + (Str[i] - '0');
1232 }
1233 }
1234
1235 if (Value > 255)
1236 return TokError("invalid octal escape sequence (out of range)");
1237
1238 Data += (unsigned char) Value;
1239 continue;
1240 }
1241
1242 // Otherwise recognize individual escapes.
1243 switch (Str[i]) {
1244 default:
1245 // Just reject invalid escape sequences for now.
1246 return TokError("invalid escape sequence (unrecognized character)");
1247
1248 case 'b': Data += '\b'; break;
1249 case 'f': Data += '\f'; break;
1250 case 'n': Data += '\n'; break;
1251 case 'r': Data += '\r'; break;
1252 case 't': Data += '\t'; break;
1253 case '"': Data += '"'; break;
1254 case '\\': Data += '\\'; break;
1255 }
1256 }
1257
1258 return false;
1259}
1260
Daniel Dunbara0d14262009-06-24 23:30:00 +00001261/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001262/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001263bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001264 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001265 CheckForValidSection();
1266
Daniel Dunbara0d14262009-06-24 23:30:00 +00001267 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001268 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001269 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001270
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001271 std::string Data;
1272 if (ParseEscapedString(Data))
1273 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001274
1275 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001276 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001277 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1278
Sean Callanan79ed1a82010-01-19 20:22:31 +00001279 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001280
1281 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001282 break;
1283
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001284 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001285 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001286 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001287 }
1288 }
1289
Sean Callanan79ed1a82010-01-19 20:22:31 +00001290 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001291 return false;
1292}
1293
1294/// ParseDirectiveValue
1295/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1296bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001297 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001298 CheckForValidSection();
1299
Daniel Dunbara0d14262009-06-24 23:30:00 +00001300 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001301 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001302 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001303 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001304 return true;
1305
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001306 // Special case constant expressions to match code generator.
1307 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001308 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001309 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001310 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001311
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001312 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001313 break;
1314
1315 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001316 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001317 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001318 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001319 }
1320 }
1321
Sean Callanan79ed1a82010-01-19 20:22:31 +00001322 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001323 return false;
1324}
1325
1326/// ParseDirectiveSpace
1327/// ::= .space expression [ , expression ]
1328bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001329 CheckForValidSection();
1330
Daniel Dunbara0d14262009-06-24 23:30:00 +00001331 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001332 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001333 return true;
1334
1335 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001336 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1337 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001338 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001339 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001340
Daniel Dunbar475839e2009-06-29 20:37:27 +00001341 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001342 return true;
1343
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001344 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001345 return TokError("unexpected token in '.space' directive");
1346 }
1347
Sean Callanan79ed1a82010-01-19 20:22:31 +00001348 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001349
1350 if (NumBytes <= 0)
1351 return TokError("invalid number of bytes in '.space' directive");
1352
1353 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001354 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001355
1356 return false;
1357}
1358
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001359/// ParseDirectiveZero
1360/// ::= .zero expression
1361bool AsmParser::ParseDirectiveZero() {
1362 CheckForValidSection();
1363
1364 int64_t NumBytes;
1365 if (ParseAbsoluteExpression(NumBytes))
1366 return true;
1367
1368 if (getLexer().isNot(AsmToken::EndOfStatement))
1369 return TokError("unexpected token in '.zero' directive");
1370
1371 Lex();
1372
1373 getStreamer().EmitFill(NumBytes, 0, DEFAULT_ADDRSPACE);
1374
1375 return false;
1376}
1377
Daniel Dunbara0d14262009-06-24 23:30:00 +00001378/// ParseDirectiveFill
1379/// ::= .fill expression , expression , expression
1380bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001381 CheckForValidSection();
1382
Daniel Dunbara0d14262009-06-24 23:30:00 +00001383 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001384 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001385 return true;
1386
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001387 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001388 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001389 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001390
1391 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001392 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001393 return true;
1394
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001395 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001396 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001397 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001398
1399 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001400 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001401 return true;
1402
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001403 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001404 return TokError("unexpected token in '.fill' directive");
1405
Sean Callanan79ed1a82010-01-19 20:22:31 +00001406 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001407
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001408 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1409 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001410
1411 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001412 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001413
1414 return false;
1415}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001416
1417/// ParseDirectiveOrg
1418/// ::= .org expression [ , expression ]
1419bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001420 CheckForValidSection();
1421
Daniel Dunbar821e3332009-08-31 08:09:28 +00001422 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001423 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001424 return true;
1425
1426 // Parse optional fill expression.
1427 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001428 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1429 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001430 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001431 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001432
Daniel Dunbar475839e2009-06-29 20:37:27 +00001433 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001434 return true;
1435
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001436 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001437 return TokError("unexpected token in '.org' directive");
1438 }
1439
Sean Callanan79ed1a82010-01-19 20:22:31 +00001440 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001441
1442 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1443 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001444 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001445
1446 return false;
1447}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001448
1449/// ParseDirectiveAlign
1450/// ::= {.align, ...} expression [ , expression [ , expression ]]
1451bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001452 CheckForValidSection();
1453
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001454 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001455 int64_t Alignment;
1456 if (ParseAbsoluteExpression(Alignment))
1457 return true;
1458
1459 SMLoc MaxBytesLoc;
1460 bool HasFillExpr = false;
1461 int64_t FillExpr = 0;
1462 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001463 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1464 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001465 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001466 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001467
1468 // The fill expression can be omitted while specifying a maximum number of
1469 // alignment bytes, e.g:
1470 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001471 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001472 HasFillExpr = true;
1473 if (ParseAbsoluteExpression(FillExpr))
1474 return true;
1475 }
1476
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001477 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1478 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001479 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001480 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001481
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001482 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001483 if (ParseAbsoluteExpression(MaxBytesToFill))
1484 return true;
1485
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001486 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001487 return TokError("unexpected token in directive");
1488 }
1489 }
1490
Sean Callanan79ed1a82010-01-19 20:22:31 +00001491 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001492
Daniel Dunbar648ac512010-05-17 21:54:30 +00001493 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001494 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001495
1496 // Compute alignment in bytes.
1497 if (IsPow2) {
1498 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001499 if (Alignment >= 32) {
1500 Error(AlignmentLoc, "invalid alignment value");
1501 Alignment = 31;
1502 }
1503
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001504 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001505 }
1506
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001507 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001508 if (MaxBytesLoc.isValid()) {
1509 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001510 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1511 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001512 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001513 }
1514
1515 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001516 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1517 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001518 MaxBytesToFill = 0;
1519 }
1520 }
1521
Daniel Dunbar648ac512010-05-17 21:54:30 +00001522 // Check whether we should use optimal code alignment for this .align
1523 // directive.
1524 //
1525 // FIXME: This should be using a target hook.
1526 bool UseCodeAlign = false;
1527 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001528 getStreamer().getCurrentSection()))
Chris Lattnera9558532010-07-15 21:19:31 +00001529 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001530 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1531 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001532 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001533 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001534 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001535 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1536 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001537 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001538
1539 return false;
1540}
1541
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001542/// ParseDirectiveSymbolAttribute
1543/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001544bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001545 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001546 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001547 StringRef Name;
1548
1549 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001550 return TokError("expected identifier in directive");
1551
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001552 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001553
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001554 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001555
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001556 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001557 break;
1558
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001559 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001560 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001561 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001562 }
1563 }
1564
Sean Callanan79ed1a82010-01-19 20:22:31 +00001565 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001566 return false;
1567}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001568
Matt Fleming924c5e52010-05-21 11:36:59 +00001569/// ParseDirectiveELFType
1570/// ::= .type identifier , @attribute
1571bool AsmParser::ParseDirectiveELFType() {
1572 StringRef Name;
1573 if (ParseIdentifier(Name))
1574 return TokError("expected identifier in directive");
1575
1576 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001577 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Matt Fleming924c5e52010-05-21 11:36:59 +00001578
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001579 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001580 return TokError("unexpected token in '.type' directive");
1581 Lex();
1582
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001583 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001584 return TokError("expected '@' before type");
1585 Lex();
1586
1587 StringRef Type;
1588 SMLoc TypeLoc;
1589
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001590 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001591 if (ParseIdentifier(Type))
1592 return TokError("expected symbol type in directive");
1593
1594 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1595 .Case("function", MCSA_ELF_TypeFunction)
1596 .Case("object", MCSA_ELF_TypeObject)
1597 .Case("tls_object", MCSA_ELF_TypeTLS)
1598 .Case("common", MCSA_ELF_TypeCommon)
1599 .Case("notype", MCSA_ELF_TypeNoType)
1600 .Default(MCSA_Invalid);
1601
1602 if (Attr == MCSA_Invalid)
1603 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1604
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001605 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001606 return TokError("unexpected token in '.type' directive");
1607
1608 Lex();
1609
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001610 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001611
1612 return false;
1613}
1614
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001615/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001616/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1617bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001618 CheckForValidSection();
1619
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001620 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001621 StringRef Name;
1622 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001623 return TokError("expected identifier in directive");
1624
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001625 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001626 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001627
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001628 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001629 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001630 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001631
1632 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001633 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001634 if (ParseAbsoluteExpression(Size))
1635 return true;
1636
1637 int64_t Pow2Alignment = 0;
1638 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001639 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001640 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001641 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001642 if (ParseAbsoluteExpression(Pow2Alignment))
1643 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001644
1645 // If this target takes alignments in bytes (not log) validate and convert.
1646 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1647 if (!isPowerOf2_64(Pow2Alignment))
1648 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1649 Pow2Alignment = Log2_64(Pow2Alignment);
1650 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001651 }
1652
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001653 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001654 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001655
Sean Callanan79ed1a82010-01-19 20:22:31 +00001656 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001657
Chris Lattner1fc3d752009-07-09 17:25:12 +00001658 // NOTE: a size of zero for a .comm should create a undefined symbol
1659 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001660 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001661 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1662 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001663
Eric Christopherc260a3e2010-05-14 01:38:54 +00001664 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001665 // may internally end up wanting an alignment in bytes.
1666 // FIXME: Diagnose overflow.
1667 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001668 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1669 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001670
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001671 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001672 return Error(IDLoc, "invalid symbol redefinition");
1673
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001674 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001675 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001676 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001677 getStreamer().EmitZerofill(Ctx.getMachOSection(
1678 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1679 0, SectionKind::getBSS()),
1680 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001681 return false;
1682 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001683
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001684 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001685 return false;
1686}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001687
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001688/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001689/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001690bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001691 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001692 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001693
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001694 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001695 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001696 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001697
Sean Callanan79ed1a82010-01-19 20:22:31 +00001698 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001699
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001700 if (Str.empty())
1701 Error(Loc, ".abort detected. Assembly stopping.");
1702 else
1703 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001704 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001705
1706 return false;
1707}
Kevin Enderby71148242009-07-14 21:35:03 +00001708
Kevin Enderby1f049b22009-07-14 23:21:55 +00001709/// ParseDirectiveInclude
1710/// ::= .include "filename"
1711bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001712 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001713 return TokError("expected string in '.include' directive");
1714
Sean Callanan18b83232010-01-19 21:44:56 +00001715 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001716 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001717 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001718
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001719 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001720 return TokError("unexpected token in '.include' directive");
1721
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001722 // Strip the quotes.
1723 Filename = Filename.substr(1, Filename.size()-2);
1724
1725 // Attempt to switch the lexer to the included file before consuming the end
1726 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001727 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001728 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001729 return true;
1730 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001731
1732 return false;
1733}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001734
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001735/// ParseDirectiveIf
1736/// ::= .if expression
1737bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001738 TheCondStack.push_back(TheCondState);
1739 TheCondState.TheCond = AsmCond::IfCond;
1740 if(TheCondState.Ignore) {
1741 EatToEndOfStatement();
1742 }
1743 else {
1744 int64_t ExprValue;
1745 if (ParseAbsoluteExpression(ExprValue))
1746 return true;
1747
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001748 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001749 return TokError("unexpected token in '.if' directive");
1750
Sean Callanan79ed1a82010-01-19 20:22:31 +00001751 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001752
1753 TheCondState.CondMet = ExprValue;
1754 TheCondState.Ignore = !TheCondState.CondMet;
1755 }
1756
1757 return false;
1758}
1759
1760/// ParseDirectiveElseIf
1761/// ::= .elseif expression
1762bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1763 if (TheCondState.TheCond != AsmCond::IfCond &&
1764 TheCondState.TheCond != AsmCond::ElseIfCond)
1765 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1766 " an .elseif");
1767 TheCondState.TheCond = AsmCond::ElseIfCond;
1768
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001769 bool LastIgnoreState = false;
1770 if (!TheCondStack.empty())
1771 LastIgnoreState = TheCondStack.back().Ignore;
1772 if (LastIgnoreState || TheCondState.CondMet) {
1773 TheCondState.Ignore = true;
1774 EatToEndOfStatement();
1775 }
1776 else {
1777 int64_t ExprValue;
1778 if (ParseAbsoluteExpression(ExprValue))
1779 return true;
1780
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001781 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001782 return TokError("unexpected token in '.elseif' directive");
1783
Sean Callanan79ed1a82010-01-19 20:22:31 +00001784 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001785 TheCondState.CondMet = ExprValue;
1786 TheCondState.Ignore = !TheCondState.CondMet;
1787 }
1788
1789 return false;
1790}
1791
1792/// ParseDirectiveElse
1793/// ::= .else
1794bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001795 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001796 return TokError("unexpected token in '.else' directive");
1797
Sean Callanan79ed1a82010-01-19 20:22:31 +00001798 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001799
1800 if (TheCondState.TheCond != AsmCond::IfCond &&
1801 TheCondState.TheCond != AsmCond::ElseIfCond)
1802 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1803 ".elseif");
1804 TheCondState.TheCond = AsmCond::ElseCond;
1805 bool LastIgnoreState = false;
1806 if (!TheCondStack.empty())
1807 LastIgnoreState = TheCondStack.back().Ignore;
1808 if (LastIgnoreState || TheCondState.CondMet)
1809 TheCondState.Ignore = true;
1810 else
1811 TheCondState.Ignore = false;
1812
1813 return false;
1814}
1815
1816/// ParseDirectiveEndIf
1817/// ::= .endif
1818bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001819 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001820 return TokError("unexpected token in '.endif' directive");
1821
Sean Callanan79ed1a82010-01-19 20:22:31 +00001822 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001823
1824 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1825 TheCondStack.empty())
1826 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1827 ".else");
1828 if (!TheCondStack.empty()) {
1829 TheCondState = TheCondStack.back();
1830 TheCondStack.pop_back();
1831 }
1832
1833 return false;
1834}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001835
1836/// ParseDirectiveFile
1837/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001838bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001839 // FIXME: I'm not sure what this is.
1840 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001841 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00001842 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001843 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001844 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001845
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001846 if (FileNumber < 1)
1847 return TokError("file number less than one");
1848 }
1849
Daniel Dunbareceec052010-07-12 17:45:27 +00001850 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001851 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001852
Chris Lattnerd32e8032010-01-25 19:02:58 +00001853 StringRef Filename = getTok().getString();
1854 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001855 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001856
Daniel Dunbareceec052010-07-12 17:45:27 +00001857 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001858 return TokError("unexpected token in '.file' directive");
1859
Chris Lattnerd32e8032010-01-25 19:02:58 +00001860 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001861 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001862 else {
1863 if (getContext().GetDwarfFile(Filename, FileNumber) == 0)
1864 Error(FileNumberLoc, "file number already allocated");
Daniel Dunbareceec052010-07-12 17:45:27 +00001865 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001866 }
Daniel Dunbareceec052010-07-12 17:45:27 +00001867
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001868 return false;
1869}
1870
1871/// ParseDirectiveLine
1872/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001873bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001874 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1875 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001876 return TokError("unexpected token in '.line' directive");
1877
Sean Callanan18b83232010-01-19 21:44:56 +00001878 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001879 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001880 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001881
1882 // FIXME: Do something with the .line.
1883 }
1884
Daniel Dunbareceec052010-07-12 17:45:27 +00001885 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001886 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001887
1888 return false;
1889}
1890
1891
1892/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00001893/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001894/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
1895/// The first number is a file number, must have been previously assigned with
1896/// a .file directive, the second number is the line number and optionally the
1897/// third number is a column position (zero if not specified). The remaining
1898/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001899bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001900
Daniel Dunbareceec052010-07-12 17:45:27 +00001901 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001902 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001903 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001904 if (FileNumber < 1)
1905 return TokError("file number less than one in '.loc' directive");
1906 if (!getContext().ValidateDwarfFileNumber(FileNumber))
1907 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001908 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001909
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00001910 int64_t LineNumber = 0;
1911 if (getLexer().is(AsmToken::Integer)) {
1912 LineNumber = getTok().getIntVal();
1913 if (LineNumber < 1)
1914 return TokError("line number less than one in '.loc' directive");
1915 Lex();
1916 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001917
1918 int64_t ColumnPos = 0;
1919 if (getLexer().is(AsmToken::Integer)) {
1920 ColumnPos = getTok().getIntVal();
1921 if (ColumnPos < 0)
1922 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001923 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001924 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001925
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001926 unsigned Flags = 0;
1927 unsigned Isa = 0;
1928 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1929 for (;;) {
1930 if (getLexer().is(AsmToken::EndOfStatement))
1931 break;
1932
1933 StringRef Name;
1934 SMLoc Loc = getTok().getLoc();
1935 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001936 return TokError("unexpected token in '.loc' directive");
1937
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001938 if (Name == "basic_block")
1939 Flags |= DWARF2_FLAG_BASIC_BLOCK;
1940 else if (Name == "prologue_end")
1941 Flags |= DWARF2_FLAG_PROLOGUE_END;
1942 else if (Name == "epilogue_begin")
1943 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
1944 else if (Name == "is_stmt") {
1945 SMLoc Loc = getTok().getLoc();
1946 const MCExpr *Value;
1947 if (getParser().ParseExpression(Value))
1948 return true;
1949 // The expression must be the constant 0 or 1.
1950 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1951 int Value = MCE->getValue();
1952 if (Value == 0)
1953 Flags &= ~DWARF2_FLAG_IS_STMT;
1954 else if (Value == 1)
1955 Flags |= DWARF2_FLAG_IS_STMT;
1956 else
1957 return Error(Loc, "is_stmt value not 0 or 1");
1958 }
1959 else {
1960 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
1961 }
1962 }
1963 else if (Name == "isa") {
1964 SMLoc Loc = getTok().getLoc();
1965 const MCExpr *Value;
1966 if (getParser().ParseExpression(Value))
1967 return true;
1968 // The expression must be a constant greater or equal to 0.
1969 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1970 int Value = MCE->getValue();
1971 if (Value < 0)
1972 return Error(Loc, "isa number less than zero");
1973 Isa = Value;
1974 }
1975 else {
1976 return Error(Loc, "isa number not a constant value");
1977 }
1978 }
1979 else {
1980 return Error(Loc, "unknown sub-directive in '.loc' directive");
1981 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001982
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001983 if (getLexer().is(AsmToken::EndOfStatement))
1984 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001985 }
1986 }
1987
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001988 getContext().setCurrentDwarfLoc(FileNumber, LineNumber, ColumnPos, Flags,Isa);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001989
1990 return false;
1991}
1992
Daniel Dunbar3c802de2010-07-18 18:38:02 +00001993/// ParseDirectiveMacrosOnOff
1994/// ::= .macros_on
1995/// ::= .macros_off
1996bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
1997 SMLoc DirectiveLoc) {
1998 if (getLexer().isNot(AsmToken::EndOfStatement))
1999 return Error(getLexer().getLoc(),
2000 "unexpected token in '" + Directive + "' directive");
2001
2002 getParser().MacrosEnabled = Directive == ".macros_on";
2003
2004 return false;
2005}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002006
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002007/// ParseDirectiveMacro
2008/// ::= .macro name
2009bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2010 SMLoc DirectiveLoc) {
2011 StringRef Name;
2012 if (getParser().ParseIdentifier(Name))
2013 return TokError("expected identifier in directive");
2014
2015 if (getLexer().isNot(AsmToken::EndOfStatement))
2016 return TokError("unexpected token in '.macro' directive");
2017
2018 // Eat the end of statement.
2019 Lex();
2020
2021 AsmToken EndToken, StartToken = getTok();
2022
2023 // Lex the macro definition.
2024 for (;;) {
2025 // Check whether we have reached the end of the file.
2026 if (getLexer().is(AsmToken::Eof))
2027 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2028
2029 // Otherwise, check whether we have reach the .endmacro.
2030 if (getLexer().is(AsmToken::Identifier) &&
2031 (getTok().getIdentifier() == ".endm" ||
2032 getTok().getIdentifier() == ".endmacro")) {
2033 EndToken = getTok();
2034 Lex();
2035 if (getLexer().isNot(AsmToken::EndOfStatement))
2036 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2037 "' directive");
2038 break;
2039 }
2040
2041 // Otherwise, scan til the end of the statement.
2042 getParser().EatToEndOfStatement();
2043 }
2044
2045 if (getParser().MacroMap.lookup(Name)) {
2046 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2047 }
2048
2049 const char *BodyStart = StartToken.getLoc().getPointer();
2050 const char *BodyEnd = EndToken.getLoc().getPointer();
2051 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2052 getParser().MacroMap[Name] = new Macro(Name, Body);
2053 return false;
2054}
2055
2056/// ParseDirectiveEndMacro
2057/// ::= .endm
2058/// ::= .endmacro
2059bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2060 SMLoc DirectiveLoc) {
2061 if (getLexer().isNot(AsmToken::EndOfStatement))
2062 return TokError("unexpected token in '" + Directive + "' directive");
2063
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002064 // If we are inside a macro instantiation, terminate the current
2065 // instantiation.
2066 if (!getParser().ActiveMacros.empty()) {
2067 getParser().HandleMacroExit();
2068 return false;
2069 }
2070
2071 // Otherwise, this .endmacro is a stray entry in the file; well formed
2072 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002073 return TokError("unexpected '" + Directive + "' in file, "
2074 "no current macro definition");
2075}
2076
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002077void GenericAsmParser::ParseUleb128(uint64_t Value) {
2078 const uint64_t Mask = (1 << 7) - 1;
2079 do {
2080 unsigned Byte = Value & Mask;
2081 Value >>= 7;
2082 if (Value) // Not the last one
2083 Byte |= (1 << 7);
2084 getStreamer().EmitIntValue(Byte, 1, DEFAULT_ADDRSPACE);
2085 } while (Value);
2086}
2087
2088void GenericAsmParser::ParseSleb128(int64_t Value) {
2089 const int64_t Mask = (1 << 7) - 1;
2090 for(;;) {
2091 unsigned Byte = Value & Mask;
2092 Value >>= 7;
2093 bool Done = ((Value == 0 && (Byte & 0x40) == 0) ||
2094 (Value == -1 && (Byte & 0x40) != 0));
2095 if (!Done)
2096 Byte |= (1 << 7);
2097 getStreamer().EmitIntValue(Byte, 1, DEFAULT_ADDRSPACE);
2098 if (Done)
2099 break;
2100 }
2101}
2102
2103bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
2104 int64_t Value;
2105 if (getParser().ParseAbsoluteExpression(Value))
2106 return true;
2107
2108 if (getLexer().isNot(AsmToken::EndOfStatement))
2109 return TokError("unexpected token in directive");
2110
2111 if (DirName[1] == 's')
2112 ParseSleb128(Value);
2113 else
2114 ParseUleb128(Value);
2115 return false;
2116}
2117
2118
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002119/// \brief Create an MCAsmParser instance.
2120MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2121 MCContext &C, MCStreamer &Out,
2122 const MCAsmInfo &MAI) {
2123 return new AsmParser(T, SM, C, Out, MAI);
2124}