blob: 34197cf7defa108e82091d1337564c0970dcf65f [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:
143 bool ParseStatement();
144
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000145 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
146 void HandleMacroExit();
147
148 void PrintMacroInstantiations();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000149 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
150
151 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
152 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000153
154 /// \brief Reset the current lexer position to that given by \arg Loc. The
155 /// current token is not set; clients should ensure Lex() is called
156 /// subsequently.
157 void JumpToLoc(SMLoc Loc);
158
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000159 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000160
161 /// \brief Parse up to the end of statement and a return the contents from the
162 /// current token until the end of the statement; the current token on exit
163 /// will be either the EndOfStatement or EOF.
164 StringRef ParseStringToEndOfStatement();
165
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000166 bool ParseAssignment(StringRef Name);
167
168 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
169 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
170 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
171
172 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
173 /// and set \arg Res to the identifier contents.
174 bool ParseIdentifier(StringRef &Res);
175
176 // Directive Parsing.
177 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
178 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
179 bool ParseDirectiveFill(); // ".fill"
180 bool ParseDirectiveSpace(); // ".space"
181 bool ParseDirectiveSet(); // ".set"
182 bool ParseDirectiveOrg(); // ".org"
183 // ".align{,32}", ".p2align{,w,l}"
184 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
185
186 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
187 /// accepts a single symbol (which should be a label or an external).
188 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
189 bool ParseDirectiveELFType(); // ELF specific ".type"
190
191 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
192
193 bool ParseDirectiveAbort(); // ".abort"
194 bool ParseDirectiveInclude(); // ".include"
195
196 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
197 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
198 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
199 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
200
201 /// ParseEscapedString - Parse the current token as a string which may include
202 /// escaped characters and return the string contents.
203 bool ParseEscapedString(std::string &Data);
204};
205
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000206/// \brief Generic implementations of directive handling, etc. which is shared
207/// (or the default, at least) for all assembler parser.
208class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000209 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
210 void AddDirectiveHandler(StringRef Directive) {
211 getParser().AddDirectiveHandler(this, Directive,
212 HandleDirective<GenericAsmParser, Handler>);
213 }
214
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000215public:
216 GenericAsmParser() {}
217
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000218 AsmParser &getParser() {
219 return (AsmParser&) this->MCAsmParserExtension::getParser();
220 }
221
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000222 virtual void Initialize(MCAsmParser &Parser) {
223 // Call the base implementation.
224 this->MCAsmParserExtension::Initialize(Parser);
225
226 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000227 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
228 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
229 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000230
231 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000232 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
233 ".macros_on");
234 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
235 ".macros_off");
236 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
237 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
238 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000239 }
240
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000241 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
242 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
243 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000244
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000245 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000246 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
247 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000248};
249
250}
251
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000252namespace llvm {
253
254extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000255extern MCAsmParserExtension *createELFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000256
257}
258
Chris Lattneraaec2052010-01-19 19:46:13 +0000259enum { DEFAULT_ADDRSPACE = 0 };
260
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000261AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
262 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000263 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000264 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000265 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000266 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000267
268 // Initialize the generic parser.
269 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000270
271 // Initialize the platform / file format parser.
272 //
273 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
274 // created.
275 if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000276 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000277 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000278 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000279 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000280 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000281 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000282}
283
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000284AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000285 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
286
287 // Destroy any macros.
288 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
289 ie = MacroMap.end(); it != ie; ++it)
290 delete it->getValue();
291
Daniel Dunbare4749702010-07-12 18:12:02 +0000292 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000293 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000294}
295
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000296void AsmParser::PrintMacroInstantiations() {
297 // Print the active macro instantiation stack.
298 for (std::vector<MacroInstantiation*>::const_reverse_iterator
299 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
300 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
301 "note");
302}
303
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000304void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000305 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000306 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000307}
308
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000309bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000310 HadError = true;
Sean Callananbf2013e2010-01-20 23:19:55 +0000311 PrintMessage(L, Msg.str(), "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000312 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000313 return true;
314}
315
Sean Callananbf2013e2010-01-20 23:19:55 +0000316void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
317 const char *Type) const {
318 SrcMgr.PrintMessage(Loc, Msg, Type);
319}
Sean Callananfd0b0282010-01-21 00:19:58 +0000320
321bool AsmParser::EnterIncludeFile(const std::string &Filename) {
322 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
323 if (NewBuf == -1)
324 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000325
Sean Callananfd0b0282010-01-21 00:19:58 +0000326 CurBuffer = NewBuf;
327
328 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
329
330 return false;
331}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000332
333void AsmParser::JumpToLoc(SMLoc Loc) {
334 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
335 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
336}
337
Sean Callananfd0b0282010-01-21 00:19:58 +0000338const AsmToken &AsmParser::Lex() {
339 const AsmToken *tok = &Lexer.Lex();
340
341 if (tok->is(AsmToken::Eof)) {
342 // If this is the end of an included file, pop the parent file off the
343 // include stack.
344 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
345 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000346 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000347 tok = &Lexer.Lex();
348 }
349 }
350
351 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000352 Error(Lexer.getErrLoc(), Lexer.getErr());
Sean Callanan79036e42010-01-20 22:18:24 +0000353
Sean Callananfd0b0282010-01-21 00:19:58 +0000354 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000355}
356
Chris Lattner79180e22010-04-05 23:15:42 +0000357bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000358 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000359 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000360 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000361 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000362 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000363 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
364 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000365
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000366 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000367 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000368
369 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000370 AsmCond StartingCondState = TheCondState;
371
Chris Lattnerb717fb02009-07-02 21:53:43 +0000372 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000373 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000374 if (!ParseStatement()) continue;
375
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000376 // We had an error, validate that one was emitted and recover by skipping to
377 // the next line.
378 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000379 EatToEndOfStatement();
380 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000381
382 if (TheCondState.TheCond != StartingCondState.TheCond ||
383 TheCondState.Ignore != StartingCondState.Ignore)
384 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000385
386 // Check to see there are no empty DwarfFile slots.
387 const std::vector<MCDwarfFile *> &MCDwarfFiles =
388 getContext().getMCDwarfFiles();
389 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000390 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000391 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000392 }
Chris Lattnerb717fb02009-07-02 21:53:43 +0000393
Chris Lattner79180e22010-04-05 23:15:42 +0000394 // Finalize the output stream if there are no errors and if the client wants
395 // us to.
396 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000397 Out.Finish();
398
Chris Lattnerb717fb02009-07-02 21:53:43 +0000399 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000400}
401
Chris Lattner2cf5f142009-06-22 01:29:09 +0000402/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
403void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000404 while (Lexer.isNot(AsmToken::EndOfStatement) &&
405 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000406 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000407
408 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000409 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000410 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000411}
412
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000413StringRef AsmParser::ParseStringToEndOfStatement() {
414 const char *Start = getTok().getLoc().getPointer();
415
416 while (Lexer.isNot(AsmToken::EndOfStatement) &&
417 Lexer.isNot(AsmToken::Eof))
418 Lex();
419
420 const char *End = getTok().getLoc().getPointer();
421 return StringRef(Start, End - Start);
422}
Chris Lattnerc4193832009-06-22 05:51:26 +0000423
Chris Lattner74ec1a32009-06-22 06:32:03 +0000424/// ParseParenExpr - Parse a paren expression and return it.
425/// NOTE: This assumes the leading '(' has already been consumed.
426///
427/// parenexpr ::= expr)
428///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000429bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000430 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000431 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000432 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000433 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000434 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000435 return false;
436}
Chris Lattnerc4193832009-06-22 05:51:26 +0000437
Chris Lattner74ec1a32009-06-22 06:32:03 +0000438/// ParsePrimaryExpr - Parse a primary expression and return it.
439/// primaryexpr ::= (parenexpr
440/// primaryexpr ::= symbol
441/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000442/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000443/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000444bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000445 switch (Lexer.getKind()) {
446 default:
447 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000448 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000449 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000450 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000451 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000452 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000453 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000454 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000455 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000456 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000457 EndLoc = Lexer.getLoc();
458
459 StringRef Identifier;
460 if (ParseIdentifier(Identifier))
461 return false;
462
Daniel Dunbarfffff912009-10-16 01:34:54 +0000463 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000464 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000465 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000466
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000467 // Mark the symbol as used in an expression.
468 Sym->setUsedInExpr(true);
469
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000470 // Lookup the symbol variant if used.
471 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000472 if (Split.first.size() != Identifier.size())
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000473 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
474
Daniel Dunbarfffff912009-10-16 01:34:54 +0000475 // If this is an absolute variable reference, substitute it now to preserve
476 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000477 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000478 if (Variant)
479 return Error(EndLoc, "unexpected modified on variable reference");
480
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000481 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000482 return false;
483 }
484
485 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000486 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000487 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000488 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000489 case AsmToken::Integer: {
490 SMLoc Loc = getTok().getLoc();
491 int64_t IntVal = getTok().getIntVal();
492 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000493 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000494 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000495 // Look for 'b' or 'f' following an Integer as a directional label
496 if (Lexer.getKind() == AsmToken::Identifier) {
497 StringRef IDVal = getTok().getString();
498 if (IDVal == "f" || IDVal == "b"){
499 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
500 IDVal == "f" ? 1 : 0);
501 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
502 getContext());
503 if(IDVal == "b" && Sym->isUndefined())
504 return Error(Loc, "invalid reference to undefined symbol");
505 EndLoc = Lexer.getLoc();
506 Lex(); // Eat identifier.
507 }
508 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000509 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000510 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000511 case AsmToken::Dot: {
512 // This is a '.' reference, which references the current PC. Emit a
513 // temporary label to the streamer and refer to it.
514 MCSymbol *Sym = Ctx.CreateTempSymbol();
515 Out.EmitLabel(Sym);
516 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
517 EndLoc = Lexer.getLoc();
518 Lex(); // Eat identifier.
519 return false;
520 }
521
Daniel Dunbar3f872332009-07-28 16:08:33 +0000522 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000523 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000524 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000525 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000526 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000527 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000528 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000529 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000530 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000531 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000532 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000533 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000534 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000535 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000536 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000537 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000538 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000539 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000540 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000541 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000542 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000543 }
544}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000545
Chris Lattnerb4307b32010-01-15 19:28:38 +0000546bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000547 SMLoc EndLoc;
548 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000549}
550
Chris Lattner74ec1a32009-06-22 06:32:03 +0000551/// ParseExpression - Parse an expression and return it.
552///
553/// expr ::= expr +,- expr -> lowest.
554/// expr ::= expr |,^,&,! expr -> middle.
555/// expr ::= expr *,/,%,<<,>> expr -> highest.
556/// expr ::= primaryexpr
557///
Chris Lattner54482b42010-01-15 19:39:23 +0000558bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000559 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000560 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000561 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
562 return true;
563
564 // Try to constant fold it up front, if possible.
565 int64_t Value;
566 if (Res->EvaluateAsAbsolute(Value))
567 Res = MCConstantExpr::Create(Value, getContext());
568
569 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000570}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000571
Chris Lattnerb4307b32010-01-15 19:28:38 +0000572bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000573 Res = 0;
574 return ParseParenExpr(Res, EndLoc) ||
575 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000576}
577
Daniel Dunbar475839e2009-06-29 20:37:27 +0000578bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000579 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000580
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000581 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000582 if (ParseExpression(Expr))
583 return true;
584
Daniel Dunbare00b0112009-10-16 01:57:52 +0000585 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000586 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000587
588 return false;
589}
590
Daniel Dunbar3f872332009-07-28 16:08:33 +0000591static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000592 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000593 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000594 default:
595 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000596
597 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000598 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000599 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000600 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000601 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000602 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000603 return 1;
604
605 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000606 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000607 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000608 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000609 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000610 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000611 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000612 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000613 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000614 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000615 case AsmToken::ExclaimEqual:
616 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000617 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000618 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000619 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000620 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000621 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000622 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000623 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000624 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000625 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000626 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000627 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000628 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000629 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000630 return 2;
631
632 // Intermediate Precedence: |, &, ^
633 //
634 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000635 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000636 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000637 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000638 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000639 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000640 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000641 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000642 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000643 return 3;
644
645 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000646 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000647 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000648 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000649 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000650 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000651 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000652 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000653 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000654 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000655 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000656 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000657 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000658 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000659 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000660 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000661 }
662}
663
664
665/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
666/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000667bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
668 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000669 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000670 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000671 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000672
673 // If the next token is lower precedence than we are allowed to eat, return
674 // successfully with what we ate already.
675 if (TokPrec < Precedence)
676 return false;
677
Sean Callanan79ed1a82010-01-19 20:22:31 +0000678 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000679
680 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000681 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000682 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000683
684 // If BinOp binds less tightly with RHS than the operator after RHS, let
685 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000686 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000687 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000688 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000689 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000690 }
691
Daniel Dunbar475839e2009-06-29 20:37:27 +0000692 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000693 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000694 }
695}
696
Chris Lattnerc4193832009-06-22 05:51:26 +0000697
698
699
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000700/// ParseStatement:
701/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000702/// ::= Label* Directive ...Operands... EndOfStatement
703/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000704bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000705 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000706 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000707 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000708 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000709 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000710
711 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000712 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000713 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000714 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000715 int64_t LocalLabelVal = -1;
716 // GUESS allow an integer followed by a ':' as a directional local label
717 if (Lexer.is(AsmToken::Integer)) {
718 LocalLabelVal = getTok().getIntVal();
719 if (LocalLabelVal < 0) {
720 if (!TheCondState.Ignore)
721 return TokError("unexpected token at start of statement");
722 IDVal = "";
723 }
724 else {
725 IDVal = getTok().getString();
726 Lex(); // Consume the integer token to be used as an identifier token.
727 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000728 if (!TheCondState.Ignore)
729 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000730 }
731 }
732 }
733 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000734 if (!TheCondState.Ignore)
735 return TokError("unexpected token at start of statement");
736 IDVal = "";
737 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000738
Chris Lattner7834fac2010-04-17 18:14:27 +0000739 // Handle conditional assembly here before checking for skipping. We
740 // have to do this so that .endif isn't skipped in a ".if 0" block for
741 // example.
742 if (IDVal == ".if")
743 return ParseDirectiveIf(IDLoc);
744 if (IDVal == ".elseif")
745 return ParseDirectiveElseIf(IDLoc);
746 if (IDVal == ".else")
747 return ParseDirectiveElse(IDLoc);
748 if (IDVal == ".endif")
749 return ParseDirectiveEndIf(IDLoc);
750
751 // If we are in a ".if 0" block, ignore this statement.
752 if (TheCondState.Ignore) {
753 EatToEndOfStatement();
754 return false;
755 }
756
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000757 // FIXME: Recurse on local labels?
758
759 // See what kind of statement we have.
760 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000761 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000762 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000763 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000764
765 // Diagnose attempt to use a variable as a label.
766 //
767 // FIXME: Diagnostics. Note the location of the definition as a label.
768 // FIXME: This doesn't diagnose assignment to a symbol which has been
769 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000770 MCSymbol *Sym;
771 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000772 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000773 else
774 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000775 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000776 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000777
Daniel Dunbar959fd882009-08-26 22:13:22 +0000778 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000779 Out.EmitLabel(Sym);
780
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000781 // Consume any end of statement token, if present, to avoid spurious
782 // AddBlankLine calls().
783 if (Lexer.is(AsmToken::EndOfStatement)) {
784 Lex();
785 if (Lexer.is(AsmToken::Eof))
786 return false;
787 }
788
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000789 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000790 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000791
Daniel Dunbar3f872332009-07-28 16:08:33 +0000792 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000793 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000794 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000795
Daniel Dunbare2ace502009-08-31 08:09:09 +0000796 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000797
798 default: // Normal instruction or directive.
799 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000800 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000801
802 // If macros are enabled, check to see if this is a macro instantiation.
803 if (MacrosEnabled)
804 if (const Macro *M = MacroMap.lookup(IDVal))
805 return HandleMacroEntry(IDVal, IDLoc, M);
806
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000807 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000808 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000809 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000810 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000811 return ParseDirectiveSet();
812
Daniel Dunbara0d14262009-06-24 23:30:00 +0000813 // Data directives
814
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000815 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000816 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000817 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000818 return ParseDirectiveAscii(true);
819
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000820 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000821 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000822 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000823 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000824 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000825 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000826 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000827 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000828
Eli Friedman5d68ec22010-07-19 04:17:25 +0000829 if (IDVal == ".align") {
830 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
831 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
832 }
833 if (IDVal == ".align32") {
834 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
835 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
836 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000837 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000838 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000839 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000840 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000841 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000842 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000843 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000844 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000845 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000846 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000847 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000848 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
849
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000850 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000851 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000852
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000853 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000854 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000855 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000856 return ParseDirectiveSpace();
857
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000858 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000859
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000860 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000861 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000862 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000863 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000864 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000865 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000866 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000867 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000868 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000869 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000870 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000871 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000872 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000873 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000874 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000875 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000876 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000877 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000878 if (IDVal == ".type")
879 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000880 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000881 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000882 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000883 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000884 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000885 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000886 if (IDVal == ".weak_def_can_be_hidden")
887 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000888
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000889 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000890 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000891 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000892 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000893
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000894 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000895 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000896 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000897 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000898
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000899 // Look up the handler in the handler table.
900 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
901 DirectiveMap.lookup(IDVal);
902 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000903 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000904
Kevin Enderby9c656452009-09-10 20:51:44 +0000905 // Target hook for parsing target specific directives.
906 if (!getTargetParser().ParseDirective(ID))
907 return false;
908
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000909 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000910 EatToEndOfStatement();
911 return false;
912 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000913
Chris Lattnera7f13542010-05-19 23:34:33 +0000914 // Canonicalize the opcode to lower case.
915 SmallString<128> Opcode;
916 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
917 Opcode.push_back(tolower(IDVal[i]));
918
Chris Lattner98986712010-01-14 22:21:20 +0000919 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000920 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000921 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000922
Daniel Dunbar3c14ca42010-08-11 06:37:09 +0000923 // Dump the parsed representation, if requested.
924 if (getShowParsedOperands()) {
925 SmallString<256> Str;
926 raw_svector_ostream OS(Str);
927 OS << "parsed instruction: [";
928 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
929 if (i != 0)
930 OS << ", ";
931 ParsedOperands[i]->dump(OS);
932 }
933 OS << "]";
934
935 PrintMessage(IDLoc, OS.str(), "note");
936 }
937
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000938 // If parsing succeeded, match the instruction.
939 if (!HadError) {
940 MCInst Inst;
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000941 if (!getTargetParser().MatchInstruction(IDLoc, ParsedOperands, Inst)) {
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000942 // Emit the instruction on success.
943 Out.EmitInstruction(Inst);
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000944 } else
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000945 HadError = true;
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000946 }
Chris Lattner98986712010-01-14 22:21:20 +0000947
Chris Lattner98986712010-01-14 22:21:20 +0000948 // Free any parsed operands.
949 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
950 delete ParsedOperands[i];
951
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000952 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000953}
Chris Lattner9a023f72009-06-24 04:43:34 +0000954
Daniel Dunbar7a570d02010-07-18 19:00:10 +0000955MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
956 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000957 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
958{
959 // Macro instantiation is lexical, unfortunately. We construct a new buffer
960 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +0000961 SmallString<256> Buf;
962 raw_svector_ostream OS(Buf);
963
964 StringRef Body = M->Body;
965 while (!Body.empty()) {
966 // Scan for the next substitution.
967 std::size_t End = Body.size(), Pos = 0;
968 for (; Pos != End; ++Pos) {
969 // Check for a substitution or escape.
970 if (Body[Pos] != '$' || Pos + 1 == End)
971 continue;
972
973 char Next = Body[Pos + 1];
974 if (Next == '$' || Next == 'n' || isdigit(Next))
975 break;
976 }
977
978 // Add the prefix.
979 OS << Body.slice(0, Pos);
980
981 // Check if we reached the end.
982 if (Pos == End)
983 break;
984
985 switch (Body[Pos+1]) {
986 // $$ => $
987 case '$':
988 OS << '$';
989 break;
990
991 // $n => number of arguments
992 case 'n':
993 OS << A.size();
994 break;
995
996 // $[0-9] => argument
997 default: {
998 // Missing arguments are ignored.
999 unsigned Index = Body[Pos+1] - '0';
1000 if (Index >= A.size())
1001 break;
1002
1003 // Otherwise substitute with the token values, with spaces eliminated.
1004 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1005 ie = A[Index].end(); it != ie; ++it)
1006 OS << it->getString();
1007 break;
1008 }
1009 }
1010
1011 // Update the scan point.
1012 Body = Body.substr(Pos + 2);
1013 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001014
1015 // We include the .endmacro in the buffer as our queue to exit the macro
1016 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001017 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001018
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001019 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001020}
1021
1022bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1023 const Macro *M) {
1024 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1025 // this, although we should protect against infinite loops.
1026 if (ActiveMacros.size() == 20)
1027 return TokError("macros cannot be nested more than 20 levels deep");
1028
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001029 // Parse the macro instantiation arguments.
1030 std::vector<std::vector<AsmToken> > MacroArguments;
1031 MacroArguments.push_back(std::vector<AsmToken>());
1032 unsigned ParenLevel = 0;
1033 for (;;) {
1034 if (Lexer.is(AsmToken::Eof))
1035 return TokError("unexpected token in macro instantiation");
1036 if (Lexer.is(AsmToken::EndOfStatement))
1037 break;
1038
1039 // If we aren't inside parentheses and this is a comma, start a new token
1040 // list.
1041 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1042 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001043 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001044 // Adjust the current parentheses level.
1045 if (Lexer.is(AsmToken::LParen))
1046 ++ParenLevel;
1047 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1048 --ParenLevel;
1049
1050 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001051 MacroArguments.back().push_back(getTok());
1052 }
1053 Lex();
1054 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001055
1056 // Create the macro instantiation object and add to the current macro
1057 // instantiation stack.
1058 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001059 getTok().getLoc(),
1060 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001061 ActiveMacros.push_back(MI);
1062
1063 // Jump to the macro instantiation and prime the lexer.
1064 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1065 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1066 Lex();
1067
1068 return false;
1069}
1070
1071void AsmParser::HandleMacroExit() {
1072 // Jump to the EndOfStatement we should return to, and consume it.
1073 JumpToLoc(ActiveMacros.back()->ExitLoc);
1074 Lex();
1075
1076 // Pop the instantiation entry.
1077 delete ActiveMacros.back();
1078 ActiveMacros.pop_back();
1079}
1080
Benjamin Kramer38e59892010-07-14 22:38:02 +00001081bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001082 // FIXME: Use better location, we should use proper tokens.
1083 SMLoc EqualLoc = Lexer.getLoc();
1084
Daniel Dunbar821e3332009-08-31 08:09:28 +00001085 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001086 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001087 return true;
1088
Daniel Dunbar3f872332009-07-28 16:08:33 +00001089 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001090 return TokError("unexpected token in assignment");
1091
1092 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001093 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001094
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001095 // Validate that the LHS is allowed to be a variable (either it has not been
1096 // used as a symbol, or it is an absolute symbol).
1097 MCSymbol *Sym = getContext().LookupSymbol(Name);
1098 if (Sym) {
1099 // Diagnose assignment to a label.
1100 //
1101 // FIXME: Diagnostics. Note the location of the definition as a label.
1102 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001103 if (Sym->isUndefined() && !Sym->isUsedInExpr())
1104 ; // Allow redefinitions of undefined symbols only used in directives.
1105 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001106 return Error(EqualLoc, "redefinition of '" + Name + "'");
1107 else if (!Sym->isVariable())
1108 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001109 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001110 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1111 Name + "'");
1112 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001113 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001114
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001115 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001116
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001117 Sym->setUsedInExpr(true);
1118
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001119 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001120 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001121
1122 return false;
1123}
1124
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001125/// ParseIdentifier:
1126/// ::= identifier
1127/// ::= string
1128bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001129 // The assembler has relaxed rules for accepting identifiers, in particular we
1130 // allow things like '.globl $foo', which would normally be separate
1131 // tokens. At this level, we have already lexed so we cannot (currently)
1132 // handle this as a context dependent token, instead we detect adjacent tokens
1133 // and return the combined identifier.
1134 if (Lexer.is(AsmToken::Dollar)) {
1135 SMLoc DollarLoc = getLexer().getLoc();
1136
1137 // Consume the dollar sign, and check for a following identifier.
1138 Lex();
1139 if (Lexer.isNot(AsmToken::Identifier))
1140 return true;
1141
1142 // We have a '$' followed by an identifier, make sure they are adjacent.
1143 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1144 return true;
1145
1146 // Construct the joined identifier and consume the token.
1147 Res = StringRef(DollarLoc.getPointer(),
1148 getTok().getIdentifier().size() + 1);
1149 Lex();
1150 return false;
1151 }
1152
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001153 if (Lexer.isNot(AsmToken::Identifier) &&
1154 Lexer.isNot(AsmToken::String))
1155 return true;
1156
Sean Callanan18b83232010-01-19 21:44:56 +00001157 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001158
Sean Callanan79ed1a82010-01-19 20:22:31 +00001159 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001160
1161 return false;
1162}
1163
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001164/// ParseDirectiveSet:
1165/// ::= .set identifier ',' expression
1166bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001167 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001168
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001169 if (ParseIdentifier(Name))
1170 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001171
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001172 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001173 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001174 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001175
Daniel Dunbare2ace502009-08-31 08:09:09 +00001176 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001177}
1178
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001179bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001180 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001181
1182 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001183 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001184 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1185 if (Str[i] != '\\') {
1186 Data += Str[i];
1187 continue;
1188 }
1189
1190 // Recognize escaped characters. Note that this escape semantics currently
1191 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1192 ++i;
1193 if (i == e)
1194 return TokError("unexpected backslash at end of string");
1195
1196 // Recognize octal sequences.
1197 if ((unsigned) (Str[i] - '0') <= 7) {
1198 // Consume up to three octal characters.
1199 unsigned Value = Str[i] - '0';
1200
1201 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1202 ++i;
1203 Value = Value * 8 + (Str[i] - '0');
1204
1205 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1206 ++i;
1207 Value = Value * 8 + (Str[i] - '0');
1208 }
1209 }
1210
1211 if (Value > 255)
1212 return TokError("invalid octal escape sequence (out of range)");
1213
1214 Data += (unsigned char) Value;
1215 continue;
1216 }
1217
1218 // Otherwise recognize individual escapes.
1219 switch (Str[i]) {
1220 default:
1221 // Just reject invalid escape sequences for now.
1222 return TokError("invalid escape sequence (unrecognized character)");
1223
1224 case 'b': Data += '\b'; break;
1225 case 'f': Data += '\f'; break;
1226 case 'n': Data += '\n'; break;
1227 case 'r': Data += '\r'; break;
1228 case 't': Data += '\t'; break;
1229 case '"': Data += '"'; break;
1230 case '\\': Data += '\\'; break;
1231 }
1232 }
1233
1234 return false;
1235}
1236
Daniel Dunbara0d14262009-06-24 23:30:00 +00001237/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001238/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001239bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001240 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001241 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001242 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001243 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001244
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001245 std::string Data;
1246 if (ParseEscapedString(Data))
1247 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001248
1249 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001250 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001251 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1252
Sean Callanan79ed1a82010-01-19 20:22:31 +00001253 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001254
1255 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001256 break;
1257
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001258 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001259 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001260 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001261 }
1262 }
1263
Sean Callanan79ed1a82010-01-19 20:22:31 +00001264 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001265 return false;
1266}
1267
1268/// ParseDirectiveValue
1269/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1270bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001271 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001272 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001273 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001274 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001275 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001276 return true;
1277
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001278 // Special case constant expressions to match code generator.
1279 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001280 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001281 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001282 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001283
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001284 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001285 break;
1286
1287 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001288 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001289 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001290 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001291 }
1292 }
1293
Sean Callanan79ed1a82010-01-19 20:22:31 +00001294 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001295 return false;
1296}
1297
1298/// ParseDirectiveSpace
1299/// ::= .space expression [ , expression ]
1300bool AsmParser::ParseDirectiveSpace() {
1301 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001302 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001303 return true;
1304
1305 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001306 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1307 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001308 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001309 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001310
Daniel Dunbar475839e2009-06-29 20:37:27 +00001311 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001312 return true;
1313
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001314 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001315 return TokError("unexpected token in '.space' directive");
1316 }
1317
Sean Callanan79ed1a82010-01-19 20:22:31 +00001318 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001319
1320 if (NumBytes <= 0)
1321 return TokError("invalid number of bytes in '.space' directive");
1322
1323 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001324 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001325
1326 return false;
1327}
1328
1329/// ParseDirectiveFill
1330/// ::= .fill expression , expression , expression
1331bool AsmParser::ParseDirectiveFill() {
1332 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001333 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001334 return true;
1335
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001336 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001337 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001338 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001339
1340 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001341 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001342 return true;
1343
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001344 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001345 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001346 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001347
1348 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001349 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001350 return true;
1351
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001352 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001353 return TokError("unexpected token in '.fill' directive");
1354
Sean Callanan79ed1a82010-01-19 20:22:31 +00001355 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001356
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001357 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1358 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001359
1360 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001361 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001362
1363 return false;
1364}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001365
1366/// ParseDirectiveOrg
1367/// ::= .org expression [ , expression ]
1368bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001369 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001370 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001371 return true;
1372
1373 // Parse optional fill expression.
1374 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001375 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1376 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001377 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001378 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001379
Daniel Dunbar475839e2009-06-29 20:37:27 +00001380 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001381 return true;
1382
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001383 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001384 return TokError("unexpected token in '.org' directive");
1385 }
1386
Sean Callanan79ed1a82010-01-19 20:22:31 +00001387 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001388
1389 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1390 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001391 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001392
1393 return false;
1394}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001395
1396/// ParseDirectiveAlign
1397/// ::= {.align, ...} expression [ , expression [ , expression ]]
1398bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001399 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001400 int64_t Alignment;
1401 if (ParseAbsoluteExpression(Alignment))
1402 return true;
1403
1404 SMLoc MaxBytesLoc;
1405 bool HasFillExpr = false;
1406 int64_t FillExpr = 0;
1407 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001408 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1409 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001410 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001411 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001412
1413 // The fill expression can be omitted while specifying a maximum number of
1414 // alignment bytes, e.g:
1415 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001416 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001417 HasFillExpr = true;
1418 if (ParseAbsoluteExpression(FillExpr))
1419 return true;
1420 }
1421
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001422 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1423 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001424 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001425 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001426
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001427 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001428 if (ParseAbsoluteExpression(MaxBytesToFill))
1429 return true;
1430
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001431 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001432 return TokError("unexpected token in directive");
1433 }
1434 }
1435
Sean Callanan79ed1a82010-01-19 20:22:31 +00001436 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001437
Daniel Dunbar648ac512010-05-17 21:54:30 +00001438 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001439 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001440
1441 // Compute alignment in bytes.
1442 if (IsPow2) {
1443 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001444 if (Alignment >= 32) {
1445 Error(AlignmentLoc, "invalid alignment value");
1446 Alignment = 31;
1447 }
1448
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001449 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001450 }
1451
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001452 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001453 if (MaxBytesLoc.isValid()) {
1454 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001455 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1456 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001457 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001458 }
1459
1460 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001461 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1462 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001463 MaxBytesToFill = 0;
1464 }
1465 }
1466
Daniel Dunbar648ac512010-05-17 21:54:30 +00001467 // Check whether we should use optimal code alignment for this .align
1468 // directive.
1469 //
1470 // FIXME: This should be using a target hook.
1471 bool UseCodeAlign = false;
1472 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001473 getStreamer().getCurrentSection()))
Chris Lattnera9558532010-07-15 21:19:31 +00001474 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001475 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1476 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001477 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001478 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001479 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001480 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1481 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001482 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001483
1484 return false;
1485}
1486
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001487/// ParseDirectiveSymbolAttribute
1488/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001489bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001490 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001491 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001492 StringRef Name;
1493
1494 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001495 return TokError("expected identifier in directive");
1496
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001497 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001498
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001499 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001500
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001501 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001502 break;
1503
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001504 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001505 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001506 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001507 }
1508 }
1509
Sean Callanan79ed1a82010-01-19 20:22:31 +00001510 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001511 return false;
1512}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001513
Matt Fleming924c5e52010-05-21 11:36:59 +00001514/// ParseDirectiveELFType
1515/// ::= .type identifier , @attribute
1516bool AsmParser::ParseDirectiveELFType() {
1517 StringRef Name;
1518 if (ParseIdentifier(Name))
1519 return TokError("expected identifier in directive");
1520
1521 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001522 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Matt Fleming924c5e52010-05-21 11:36:59 +00001523
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001524 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001525 return TokError("unexpected token in '.type' directive");
1526 Lex();
1527
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001528 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001529 return TokError("expected '@' before type");
1530 Lex();
1531
1532 StringRef Type;
1533 SMLoc TypeLoc;
1534
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001535 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001536 if (ParseIdentifier(Type))
1537 return TokError("expected symbol type in directive");
1538
1539 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1540 .Case("function", MCSA_ELF_TypeFunction)
1541 .Case("object", MCSA_ELF_TypeObject)
1542 .Case("tls_object", MCSA_ELF_TypeTLS)
1543 .Case("common", MCSA_ELF_TypeCommon)
1544 .Case("notype", MCSA_ELF_TypeNoType)
1545 .Default(MCSA_Invalid);
1546
1547 if (Attr == MCSA_Invalid)
1548 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1549
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001550 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001551 return TokError("unexpected token in '.type' directive");
1552
1553 Lex();
1554
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001555 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001556
1557 return false;
1558}
1559
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001560/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001561/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1562bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001563 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001564 StringRef Name;
1565 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001566 return TokError("expected identifier in directive");
1567
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001568 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001569 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001570
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001571 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001572 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001573 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001574
1575 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001576 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001577 if (ParseAbsoluteExpression(Size))
1578 return true;
1579
1580 int64_t Pow2Alignment = 0;
1581 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001582 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001583 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001584 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001585 if (ParseAbsoluteExpression(Pow2Alignment))
1586 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001587
1588 // If this target takes alignments in bytes (not log) validate and convert.
1589 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1590 if (!isPowerOf2_64(Pow2Alignment))
1591 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1592 Pow2Alignment = Log2_64(Pow2Alignment);
1593 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001594 }
1595
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001596 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001597 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001598
Sean Callanan79ed1a82010-01-19 20:22:31 +00001599 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001600
Chris Lattner1fc3d752009-07-09 17:25:12 +00001601 // NOTE: a size of zero for a .comm should create a undefined symbol
1602 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001603 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001604 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1605 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001606
Eric Christopherc260a3e2010-05-14 01:38:54 +00001607 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001608 // may internally end up wanting an alignment in bytes.
1609 // FIXME: Diagnose overflow.
1610 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001611 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1612 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001613
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001614 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001615 return Error(IDLoc, "invalid symbol redefinition");
1616
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001617 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001618 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001619 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001620 getStreamer().EmitZerofill(Ctx.getMachOSection(
1621 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1622 0, SectionKind::getBSS()),
1623 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001624 return false;
1625 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001626
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001627 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001628 return false;
1629}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001630
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001631/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001632/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001633bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001634 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001635 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001636
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001637 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001638 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001639 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001640
Sean Callanan79ed1a82010-01-19 20:22:31 +00001641 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001642
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001643 if (Str.empty())
1644 Error(Loc, ".abort detected. Assembly stopping.");
1645 else
1646 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001647 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001648
1649 return false;
1650}
Kevin Enderby71148242009-07-14 21:35:03 +00001651
Kevin Enderby1f049b22009-07-14 23:21:55 +00001652/// ParseDirectiveInclude
1653/// ::= .include "filename"
1654bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001655 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001656 return TokError("expected string in '.include' directive");
1657
Sean Callanan18b83232010-01-19 21:44:56 +00001658 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001659 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001660 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001661
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001662 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001663 return TokError("unexpected token in '.include' directive");
1664
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001665 // Strip the quotes.
1666 Filename = Filename.substr(1, Filename.size()-2);
1667
1668 // Attempt to switch the lexer to the included file before consuming the end
1669 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001670 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001671 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001672 return true;
1673 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001674
1675 return false;
1676}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001677
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001678/// ParseDirectiveIf
1679/// ::= .if expression
1680bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001681 TheCondStack.push_back(TheCondState);
1682 TheCondState.TheCond = AsmCond::IfCond;
1683 if(TheCondState.Ignore) {
1684 EatToEndOfStatement();
1685 }
1686 else {
1687 int64_t ExprValue;
1688 if (ParseAbsoluteExpression(ExprValue))
1689 return true;
1690
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001691 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001692 return TokError("unexpected token in '.if' directive");
1693
Sean Callanan79ed1a82010-01-19 20:22:31 +00001694 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001695
1696 TheCondState.CondMet = ExprValue;
1697 TheCondState.Ignore = !TheCondState.CondMet;
1698 }
1699
1700 return false;
1701}
1702
1703/// ParseDirectiveElseIf
1704/// ::= .elseif expression
1705bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1706 if (TheCondState.TheCond != AsmCond::IfCond &&
1707 TheCondState.TheCond != AsmCond::ElseIfCond)
1708 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1709 " an .elseif");
1710 TheCondState.TheCond = AsmCond::ElseIfCond;
1711
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001712 bool LastIgnoreState = false;
1713 if (!TheCondStack.empty())
1714 LastIgnoreState = TheCondStack.back().Ignore;
1715 if (LastIgnoreState || TheCondState.CondMet) {
1716 TheCondState.Ignore = true;
1717 EatToEndOfStatement();
1718 }
1719 else {
1720 int64_t ExprValue;
1721 if (ParseAbsoluteExpression(ExprValue))
1722 return true;
1723
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001724 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001725 return TokError("unexpected token in '.elseif' directive");
1726
Sean Callanan79ed1a82010-01-19 20:22:31 +00001727 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001728 TheCondState.CondMet = ExprValue;
1729 TheCondState.Ignore = !TheCondState.CondMet;
1730 }
1731
1732 return false;
1733}
1734
1735/// ParseDirectiveElse
1736/// ::= .else
1737bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001738 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001739 return TokError("unexpected token in '.else' directive");
1740
Sean Callanan79ed1a82010-01-19 20:22:31 +00001741 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001742
1743 if (TheCondState.TheCond != AsmCond::IfCond &&
1744 TheCondState.TheCond != AsmCond::ElseIfCond)
1745 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1746 ".elseif");
1747 TheCondState.TheCond = AsmCond::ElseCond;
1748 bool LastIgnoreState = false;
1749 if (!TheCondStack.empty())
1750 LastIgnoreState = TheCondStack.back().Ignore;
1751 if (LastIgnoreState || TheCondState.CondMet)
1752 TheCondState.Ignore = true;
1753 else
1754 TheCondState.Ignore = false;
1755
1756 return false;
1757}
1758
1759/// ParseDirectiveEndIf
1760/// ::= .endif
1761bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001762 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001763 return TokError("unexpected token in '.endif' directive");
1764
Sean Callanan79ed1a82010-01-19 20:22:31 +00001765 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001766
1767 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1768 TheCondStack.empty())
1769 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1770 ".else");
1771 if (!TheCondStack.empty()) {
1772 TheCondState = TheCondStack.back();
1773 TheCondStack.pop_back();
1774 }
1775
1776 return false;
1777}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001778
1779/// ParseDirectiveFile
1780/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001781bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001782 // FIXME: I'm not sure what this is.
1783 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001784 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00001785 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001786 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001787 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001788
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001789 if (FileNumber < 1)
1790 return TokError("file number less than one");
1791 }
1792
Daniel Dunbareceec052010-07-12 17:45:27 +00001793 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001794 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001795
Chris Lattnerd32e8032010-01-25 19:02:58 +00001796 StringRef Filename = getTok().getString();
1797 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001798 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001799
Daniel Dunbareceec052010-07-12 17:45:27 +00001800 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001801 return TokError("unexpected token in '.file' directive");
1802
Chris Lattnerd32e8032010-01-25 19:02:58 +00001803 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001804 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001805 else {
1806 if (getContext().GetDwarfFile(Filename, FileNumber) == 0)
1807 Error(FileNumberLoc, "file number already allocated");
Daniel Dunbareceec052010-07-12 17:45:27 +00001808 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001809 }
Daniel Dunbareceec052010-07-12 17:45:27 +00001810
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001811 return false;
1812}
1813
1814/// ParseDirectiveLine
1815/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001816bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001817 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1818 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001819 return TokError("unexpected token in '.line' directive");
1820
Sean Callanan18b83232010-01-19 21:44:56 +00001821 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001822 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001823 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001824
1825 // FIXME: Do something with the .line.
1826 }
1827
Daniel Dunbareceec052010-07-12 17:45:27 +00001828 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001829 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001830
1831 return false;
1832}
1833
1834
1835/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00001836/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001837/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
1838/// The first number is a file number, must have been previously assigned with
1839/// a .file directive, the second number is the line number and optionally the
1840/// third number is a column position (zero if not specified). The remaining
1841/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001842bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001843
Daniel Dunbareceec052010-07-12 17:45:27 +00001844 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001845 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001846 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001847 if (FileNumber < 1)
1848 return TokError("file number less than one in '.loc' directive");
1849 if (!getContext().ValidateDwarfFileNumber(FileNumber))
1850 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001851 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001852
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00001853 int64_t LineNumber = 0;
1854 if (getLexer().is(AsmToken::Integer)) {
1855 LineNumber = getTok().getIntVal();
1856 if (LineNumber < 1)
1857 return TokError("line number less than one in '.loc' directive");
1858 Lex();
1859 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001860
1861 int64_t ColumnPos = 0;
1862 if (getLexer().is(AsmToken::Integer)) {
1863 ColumnPos = getTok().getIntVal();
1864 if (ColumnPos < 0)
1865 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001866 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001867 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001868
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001869 unsigned Flags = 0;
1870 unsigned Isa = 0;
1871 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1872 for (;;) {
1873 if (getLexer().is(AsmToken::EndOfStatement))
1874 break;
1875
1876 StringRef Name;
1877 SMLoc Loc = getTok().getLoc();
1878 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001879 return TokError("unexpected token in '.loc' directive");
1880
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001881 if (Name == "basic_block")
1882 Flags |= DWARF2_FLAG_BASIC_BLOCK;
1883 else if (Name == "prologue_end")
1884 Flags |= DWARF2_FLAG_PROLOGUE_END;
1885 else if (Name == "epilogue_begin")
1886 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
1887 else if (Name == "is_stmt") {
1888 SMLoc Loc = getTok().getLoc();
1889 const MCExpr *Value;
1890 if (getParser().ParseExpression(Value))
1891 return true;
1892 // The expression must be the constant 0 or 1.
1893 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1894 int Value = MCE->getValue();
1895 if (Value == 0)
1896 Flags &= ~DWARF2_FLAG_IS_STMT;
1897 else if (Value == 1)
1898 Flags |= DWARF2_FLAG_IS_STMT;
1899 else
1900 return Error(Loc, "is_stmt value not 0 or 1");
1901 }
1902 else {
1903 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
1904 }
1905 }
1906 else if (Name == "isa") {
1907 SMLoc Loc = getTok().getLoc();
1908 const MCExpr *Value;
1909 if (getParser().ParseExpression(Value))
1910 return true;
1911 // The expression must be a constant greater or equal to 0.
1912 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1913 int Value = MCE->getValue();
1914 if (Value < 0)
1915 return Error(Loc, "isa number less than zero");
1916 Isa = Value;
1917 }
1918 else {
1919 return Error(Loc, "isa number not a constant value");
1920 }
1921 }
1922 else {
1923 return Error(Loc, "unknown sub-directive in '.loc' directive");
1924 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001925
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001926 if (getLexer().is(AsmToken::EndOfStatement))
1927 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001928 }
1929 }
1930
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001931 getContext().setCurrentDwarfLoc(FileNumber, LineNumber, ColumnPos, Flags,Isa);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001932
1933 return false;
1934}
1935
Daniel Dunbar3c802de2010-07-18 18:38:02 +00001936/// ParseDirectiveMacrosOnOff
1937/// ::= .macros_on
1938/// ::= .macros_off
1939bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
1940 SMLoc DirectiveLoc) {
1941 if (getLexer().isNot(AsmToken::EndOfStatement))
1942 return Error(getLexer().getLoc(),
1943 "unexpected token in '" + Directive + "' directive");
1944
1945 getParser().MacrosEnabled = Directive == ".macros_on";
1946
1947 return false;
1948}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00001949
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00001950/// ParseDirectiveMacro
1951/// ::= .macro name
1952bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
1953 SMLoc DirectiveLoc) {
1954 StringRef Name;
1955 if (getParser().ParseIdentifier(Name))
1956 return TokError("expected identifier in directive");
1957
1958 if (getLexer().isNot(AsmToken::EndOfStatement))
1959 return TokError("unexpected token in '.macro' directive");
1960
1961 // Eat the end of statement.
1962 Lex();
1963
1964 AsmToken EndToken, StartToken = getTok();
1965
1966 // Lex the macro definition.
1967 for (;;) {
1968 // Check whether we have reached the end of the file.
1969 if (getLexer().is(AsmToken::Eof))
1970 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
1971
1972 // Otherwise, check whether we have reach the .endmacro.
1973 if (getLexer().is(AsmToken::Identifier) &&
1974 (getTok().getIdentifier() == ".endm" ||
1975 getTok().getIdentifier() == ".endmacro")) {
1976 EndToken = getTok();
1977 Lex();
1978 if (getLexer().isNot(AsmToken::EndOfStatement))
1979 return TokError("unexpected token in '" + EndToken.getIdentifier() +
1980 "' directive");
1981 break;
1982 }
1983
1984 // Otherwise, scan til the end of the statement.
1985 getParser().EatToEndOfStatement();
1986 }
1987
1988 if (getParser().MacroMap.lookup(Name)) {
1989 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
1990 }
1991
1992 const char *BodyStart = StartToken.getLoc().getPointer();
1993 const char *BodyEnd = EndToken.getLoc().getPointer();
1994 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
1995 getParser().MacroMap[Name] = new Macro(Name, Body);
1996 return false;
1997}
1998
1999/// ParseDirectiveEndMacro
2000/// ::= .endm
2001/// ::= .endmacro
2002bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2003 SMLoc DirectiveLoc) {
2004 if (getLexer().isNot(AsmToken::EndOfStatement))
2005 return TokError("unexpected token in '" + Directive + "' directive");
2006
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002007 // If we are inside a macro instantiation, terminate the current
2008 // instantiation.
2009 if (!getParser().ActiveMacros.empty()) {
2010 getParser().HandleMacroExit();
2011 return false;
2012 }
2013
2014 // Otherwise, this .endmacro is a stray entry in the file; well formed
2015 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002016 return TokError("unexpected '" + Directive + "' in file, "
2017 "no current macro definition");
2018}
2019
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002020/// \brief Create an MCAsmParser instance.
2021MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2022 MCContext &C, MCStreamer &Out,
2023 const MCAsmInfo &MAI) {
2024 return new AsmParser(T, SM, C, Out, MAI);
2025}