blob: 179fd8d2693f3c3b322704f4bb8460bc9c468d8b [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 Dunbarb95a0792010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000016#include "llvm/ADT/StringMap.h"
Matt Fleming924c5e52010-05-21 11:36:59 +000017#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000021#include "llvm/MC/MCExpr.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"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000030#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000031#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000032#include "llvm/Support/raw_ostream.h"
Roman Divacky54b0f4f2011-01-27 17:16:37 +000033#include "llvm/Target/TargetAsmInfo.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000034#include "llvm/Target/TargetAsmParser.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000035#include <cctype>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000036#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000037using namespace llvm;
38
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000039namespace {
40
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000041/// \brief Helper class for tracking macro definitions.
42struct Macro {
43 StringRef Name;
44 StringRef Body;
45
46public:
47 Macro(StringRef N, StringRef B) : Name(N), Body(B) {}
48};
49
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000050/// \brief Helper class for storing information about an active macro
51/// instantiation.
52struct MacroInstantiation {
53 /// The macro being instantiated.
54 const Macro *TheMacro;
55
56 /// The macro instantiation with substitutions.
57 MemoryBuffer *Instantiation;
58
59 /// The location of the instantiation.
60 SMLoc InstantiationLoc;
61
62 /// The location where parsing should resume upon instantiation completion.
63 SMLoc ExitLoc;
64
65public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000066 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
67 const std::vector<std::vector<AsmToken> > &A);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000068};
69
Daniel Dunbaraef87e32010-07-18 18:31:38 +000070/// \brief The concrete assembly parser instance.
71class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000072 friend class GenericAsmParser;
73
Daniel Dunbaraef87e32010-07-18 18:31:38 +000074 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
75 void operator=(const AsmParser &); // DO NOT IMPLEMENT
76private:
77 AsmLexer Lexer;
78 MCContext &Ctx;
79 MCStreamer &Out;
80 SourceMgr &SrcMgr;
81 MCAsmParserExtension *GenericParser;
82 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +000083
Daniel Dunbaraef87e32010-07-18 18:31:38 +000084 /// This is the current buffer index we're lexing from as managed by the
85 /// SourceMgr object.
86 int CurBuffer;
87
88 AsmCond TheCondState;
89 std::vector<AsmCond> TheCondStack;
90
91 /// DirectiveMap - This is a table handlers for directives. Each handler is
92 /// invoked after the directive identifier is read and is responsible for
93 /// parsing and validating the rest of the directive. The handler is passed
94 /// in the directive name and the location of the directive keyword.
95 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000096
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000097 /// MacroMap - Map of currently defined macros.
98 StringMap<Macro*> MacroMap;
99
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000100 /// ActiveMacros - Stack of active macro instantiations.
101 std::vector<MacroInstantiation*> ActiveMacros;
102
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000103 /// Boolean tracking whether macro substitution is enabled.
104 unsigned MacrosEnabled : 1;
105
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000106 /// Flag tracking whether any errors have been encountered.
107 unsigned HadError : 1;
108
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000109public:
110 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
111 const MCAsmInfo &MAI);
112 ~AsmParser();
113
114 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
115
116 void AddDirectiveHandler(MCAsmParserExtension *Object,
117 StringRef Directive,
118 DirectiveHandler Handler) {
119 DirectiveMap[Directive] = std::make_pair(Object, Handler);
120 }
121
122public:
123 /// @name MCAsmParser Interface
124 /// {
125
126 virtual SourceMgr &getSourceManager() { return SrcMgr; }
127 virtual MCAsmLexer &getLexer() { return Lexer; }
128 virtual MCContext &getContext() { return Ctx; }
129 virtual MCStreamer &getStreamer() { return Out; }
130
131 virtual void Warning(SMLoc L, const Twine &Meg);
132 virtual bool Error(SMLoc L, const Twine &Msg);
133
134 const AsmToken &Lex();
135
136 bool ParseExpression(const MCExpr *&Res);
137 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
138 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
139 virtual bool ParseAbsoluteExpression(int64_t &Res);
140
141 /// }
142
143private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000144 void CheckForValidSection();
145
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000146 bool ParseStatement();
147
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000148 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
149 void HandleMacroExit();
150
151 void PrintMacroInstantiations();
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000152 void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type) const {
153 SrcMgr.PrintMessage(Loc, Msg, Type);
154 }
155
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000156 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
157 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000158
159 /// \brief Reset the current lexer position to that given by \arg Loc. The
160 /// current token is not set; clients should ensure Lex() is called
161 /// subsequently.
162 void JumpToLoc(SMLoc Loc);
163
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000164 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000165
166 /// \brief Parse up to the end of statement and a return the contents from the
167 /// current token until the end of the statement; the current token on exit
168 /// will be either the EndOfStatement or EOF.
169 StringRef ParseStringToEndOfStatement();
170
Nico Weber4c4c7322011-01-28 03:04:41 +0000171 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000172
173 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
174 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
175 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000176 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000177
178 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
179 /// and set \arg Res to the identifier contents.
180 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000181
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000182 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000183
184 // ".ascii", ".asciiz", ".string"
185 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000186 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000187 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000188 bool ParseDirectiveFill(); // ".fill"
189 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000190 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000191 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000192 bool ParseDirectiveOrg(); // ".org"
193 // ".align{,32}", ".p2align{,w,l}"
194 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
195
196 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
197 /// accepts a single symbol (which should be a label or an external).
198 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000199
200 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
201
202 bool ParseDirectiveAbort(); // ".abort"
203 bool ParseDirectiveInclude(); // ".include"
204
205 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000206 // ".ifdef" or ".ifndef", depending on expect_defined
207 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000208 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
209 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
210 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
211
212 /// ParseEscapedString - Parse the current token as a string which may include
213 /// escaped characters and return the string contents.
214 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000215
216 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
217 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000218};
219
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000220/// \brief Generic implementations of directive handling, etc. which is shared
221/// (or the default, at least) for all assembler parser.
222class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000223 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
224 void AddDirectiveHandler(StringRef Directive) {
225 getParser().AddDirectiveHandler(this, Directive,
226 HandleDirective<GenericAsmParser, Handler>);
227 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000228public:
229 GenericAsmParser() {}
230
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000231 AsmParser &getParser() {
232 return (AsmParser&) this->MCAsmParserExtension::getParser();
233 }
234
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000235 virtual void Initialize(MCAsmParser &Parser) {
236 // Call the base implementation.
237 this->MCAsmParserExtension::Initialize(Parser);
238
239 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000240 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
241 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
242 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000243 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000244
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000245 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000246 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
247 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000248 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
249 ".cfi_startproc");
250 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
251 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000252 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
253 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000254 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
255 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000256 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
257 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000258 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
259 ".cfi_def_cfa_register");
260 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
261 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000262 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
263 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000264 AddDirectiveHandler<
265 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
266 AddDirectiveHandler<
267 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000268 AddDirectiveHandler<
269 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
270 AddDirectiveHandler<
271 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000272 AddDirectiveHandler<
273 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000274
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000275 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000276 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
277 ".macros_on");
278 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
279 ".macros_off");
280 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
281 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
282 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000283
284 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
285 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000286 }
287
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000288 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
289
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000290 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
291 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
292 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000293 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000294 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000295 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
296 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000297 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000298 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000299 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000300 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
301 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000302 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000303 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000304 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
305 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000306 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000307
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000308 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000309 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
310 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000311
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000312 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000313};
314
315}
316
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000317namespace llvm {
318
319extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000320extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000321extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000322
323}
324
Chris Lattneraaec2052010-01-19 19:46:13 +0000325enum { DEFAULT_ADDRSPACE = 0 };
326
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000327AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
328 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000329 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000330 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000331 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000332 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000333
334 // Initialize the generic parser.
335 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000336
337 // Initialize the platform / file format parser.
338 //
339 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
340 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000341 if (_MAI.hasMicrosoftFastStdCallMangling()) {
342 PlatformParser = createCOFFAsmParser();
343 PlatformParser->Initialize(*this);
344 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000345 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000346 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000347 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000348 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000349 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000350 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000351}
352
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000353AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000354 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
355
356 // Destroy any macros.
357 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
358 ie = MacroMap.end(); it != ie; ++it)
359 delete it->getValue();
360
Daniel Dunbare4749702010-07-12 18:12:02 +0000361 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000362 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000363}
364
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000365void AsmParser::PrintMacroInstantiations() {
366 // Print the active macro instantiation stack.
367 for (std::vector<MacroInstantiation*>::const_reverse_iterator
368 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
369 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
370 "note");
371}
372
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000373void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000374 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000375 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000376}
377
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000378bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000379 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000380 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000381 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000382 return true;
383}
384
Sean Callananfd0b0282010-01-21 00:19:58 +0000385bool AsmParser::EnterIncludeFile(const std::string &Filename) {
386 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
387 if (NewBuf == -1)
388 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000389
Sean Callananfd0b0282010-01-21 00:19:58 +0000390 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000391
Sean Callananfd0b0282010-01-21 00:19:58 +0000392 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000393
Sean Callananfd0b0282010-01-21 00:19:58 +0000394 return false;
395}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000396
397void AsmParser::JumpToLoc(SMLoc Loc) {
398 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
399 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
400}
401
Sean Callananfd0b0282010-01-21 00:19:58 +0000402const AsmToken &AsmParser::Lex() {
403 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000404
Sean Callananfd0b0282010-01-21 00:19:58 +0000405 if (tok->is(AsmToken::Eof)) {
406 // If this is the end of an included file, pop the parent file off the
407 // include stack.
408 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
409 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000410 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000411 tok = &Lexer.Lex();
412 }
413 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000414
Sean Callananfd0b0282010-01-21 00:19:58 +0000415 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000416 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000417
Sean Callananfd0b0282010-01-21 00:19:58 +0000418 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000419}
420
Chris Lattner79180e22010-04-05 23:15:42 +0000421bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000422 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000423 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000424 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000425
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000426 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000427 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000428
429 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000430 AsmCond StartingCondState = TheCondState;
431
Chris Lattnerb717fb02009-07-02 21:53:43 +0000432 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000433 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000434 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000435
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000436 // We had an error, validate that one was emitted and recover by skipping to
437 // the next line.
438 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000439 EatToEndOfStatement();
440 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000441
442 if (TheCondState.TheCond != StartingCondState.TheCond ||
443 TheCondState.Ignore != StartingCondState.Ignore)
444 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000445
446 // Check to see there are no empty DwarfFile slots.
447 const std::vector<MCDwarfFile *> &MCDwarfFiles =
448 getContext().getMCDwarfFiles();
449 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000450 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000451 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000452 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000453
Chris Lattner79180e22010-04-05 23:15:42 +0000454 // Finalize the output stream if there are no errors and if the client wants
455 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000456 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000457 Out.Finish();
458
Chris Lattnerb717fb02009-07-02 21:53:43 +0000459 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000460}
461
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000462void AsmParser::CheckForValidSection() {
463 if (!getStreamer().getCurrentSection()) {
464 TokError("expected section directive before assembly directive");
465 Out.SwitchSection(Ctx.getMachOSection(
466 "__TEXT", "__text",
467 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
468 0, SectionKind::getText()));
469 }
470}
471
Chris Lattner2cf5f142009-06-22 01:29:09 +0000472/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
473void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000474 while (Lexer.isNot(AsmToken::EndOfStatement) &&
475 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000476 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000477
Chris Lattner2cf5f142009-06-22 01:29:09 +0000478 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000479 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000480 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000481}
482
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000483StringRef AsmParser::ParseStringToEndOfStatement() {
484 const char *Start = getTok().getLoc().getPointer();
485
486 while (Lexer.isNot(AsmToken::EndOfStatement) &&
487 Lexer.isNot(AsmToken::Eof))
488 Lex();
489
490 const char *End = getTok().getLoc().getPointer();
491 return StringRef(Start, End - Start);
492}
Chris Lattnerc4193832009-06-22 05:51:26 +0000493
Chris Lattner74ec1a32009-06-22 06:32:03 +0000494/// ParseParenExpr - Parse a paren expression and return it.
495/// NOTE: This assumes the leading '(' has already been consumed.
496///
497/// parenexpr ::= expr)
498///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000499bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000500 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000501 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000502 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000503 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000504 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000505 return false;
506}
Chris Lattnerc4193832009-06-22 05:51:26 +0000507
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000508/// ParseBracketExpr - Parse a bracket expression and return it.
509/// NOTE: This assumes the leading '[' has already been consumed.
510///
511/// bracketexpr ::= expr]
512///
513bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
514 if (ParseExpression(Res)) return true;
515 if (Lexer.isNot(AsmToken::RBrac))
516 return TokError("expected ']' in brackets expression");
517 EndLoc = Lexer.getLoc();
518 Lex();
519 return false;
520}
521
Chris Lattner74ec1a32009-06-22 06:32:03 +0000522/// ParsePrimaryExpr - Parse a primary expression and return it.
523/// primaryexpr ::= (parenexpr
524/// primaryexpr ::= symbol
525/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000526/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000527/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000528bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000529 switch (Lexer.getKind()) {
530 default:
531 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000532 // If we have an error assume that we've already handled it.
533 case AsmToken::Error:
534 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000535 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000536 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000537 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000538 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000539 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000540 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000541 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000542 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000543 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000544 EndLoc = Lexer.getLoc();
545
546 StringRef Identifier;
547 if (ParseIdentifier(Identifier))
548 return false;
549
Daniel Dunbarfffff912009-10-16 01:34:54 +0000550 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000551 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000552 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000553
554 // Lookup the symbol variant if used.
555 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000556 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000557 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000558 if (Variant == MCSymbolRefExpr::VK_Invalid) {
559 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000560 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000561 }
562 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000563
Daniel Dunbarfffff912009-10-16 01:34:54 +0000564 // If this is an absolute variable reference, substitute it now to preserve
565 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000566 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000567 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000568 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000569
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000570 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000571 return false;
572 }
573
574 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000575 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000576 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000577 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000578 case AsmToken::Integer: {
579 SMLoc Loc = getTok().getLoc();
580 int64_t IntVal = getTok().getIntVal();
581 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000582 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000583 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000584 // Look for 'b' or 'f' following an Integer as a directional label
585 if (Lexer.getKind() == AsmToken::Identifier) {
586 StringRef IDVal = getTok().getString();
587 if (IDVal == "f" || IDVal == "b"){
588 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
589 IDVal == "f" ? 1 : 0);
590 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
591 getContext());
592 if(IDVal == "b" && Sym->isUndefined())
593 return Error(Loc, "invalid reference to undefined symbol");
594 EndLoc = Lexer.getLoc();
595 Lex(); // Eat identifier.
596 }
597 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000598 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000599 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000600 case AsmToken::Real: {
601 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000602 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000603 Res = MCConstantExpr::Create(IntVal, getContext());
604 Lex(); // Eat token.
605 return false;
606 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000607 case AsmToken::Dot: {
608 // This is a '.' reference, which references the current PC. Emit a
609 // temporary label to the streamer and refer to it.
610 MCSymbol *Sym = Ctx.CreateTempSymbol();
611 Out.EmitLabel(Sym);
612 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
613 EndLoc = Lexer.getLoc();
614 Lex(); // Eat identifier.
615 return false;
616 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000617 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000618 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000619 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000620 case AsmToken::LBrac:
621 if (!PlatformParser->HasBracketExpressions())
622 return TokError("brackets expression not supported on this target");
623 Lex(); // Eat the '['.
624 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000625 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000626 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000627 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000628 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000629 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000630 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000631 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000632 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000633 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000634 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000635 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000636 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000637 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000638 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000639 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000640 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000641 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000642 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000643 }
644}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000645
Chris Lattnerb4307b32010-01-15 19:28:38 +0000646bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000647 SMLoc EndLoc;
648 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000649}
650
Daniel Dunbarcceba832010-09-17 02:47:07 +0000651const MCExpr *
652AsmParser::ApplyModifierToExpr(const MCExpr *E,
653 MCSymbolRefExpr::VariantKind Variant) {
654 // Recurse over the given expression, rebuilding it to apply the given variant
655 // if there is exactly one symbol.
656 switch (E->getKind()) {
657 case MCExpr::Target:
658 case MCExpr::Constant:
659 return 0;
660
661 case MCExpr::SymbolRef: {
662 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
663
664 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
665 TokError("invalid variant on expression '" +
666 getTok().getIdentifier() + "' (already modified)");
667 return E;
668 }
669
670 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
671 }
672
673 case MCExpr::Unary: {
674 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
675 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
676 if (!Sub)
677 return 0;
678 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
679 }
680
681 case MCExpr::Binary: {
682 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
683 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
684 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
685
686 if (!LHS && !RHS)
687 return 0;
688
689 if (!LHS) LHS = BE->getLHS();
690 if (!RHS) RHS = BE->getRHS();
691
692 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
693 }
694 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000695
696 assert(0 && "Invalid expression kind!");
697 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000698}
699
Chris Lattner74ec1a32009-06-22 06:32:03 +0000700/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000701///
Chris Lattner74ec1a32009-06-22 06:32:03 +0000702/// expr ::= expr +,- expr -> lowest.
703/// expr ::= expr |,^,&,! expr -> middle.
704/// expr ::= expr *,/,%,<<,>> expr -> highest.
705/// expr ::= primaryexpr
706///
Chris Lattner54482b42010-01-15 19:39:23 +0000707bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000708 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000709 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000710 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
711 return true;
712
Daniel Dunbarcceba832010-09-17 02:47:07 +0000713 // As a special case, we support 'a op b @ modifier' by rewriting the
714 // expression to include the modifier. This is inefficient, but in general we
715 // expect users to use 'a@modifier op b'.
716 if (Lexer.getKind() == AsmToken::At) {
717 Lex();
718
719 if (Lexer.isNot(AsmToken::Identifier))
720 return TokError("unexpected symbol modifier following '@'");
721
722 MCSymbolRefExpr::VariantKind Variant =
723 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
724 if (Variant == MCSymbolRefExpr::VK_Invalid)
725 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
726
727 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
728 if (!ModifiedRes) {
729 return TokError("invalid modifier '" + getTok().getIdentifier() +
730 "' (no symbols present)");
731 return true;
732 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000733
Daniel Dunbarcceba832010-09-17 02:47:07 +0000734 Res = ModifiedRes;
735 Lex();
736 }
737
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000738 // Try to constant fold it up front, if possible.
739 int64_t Value;
740 if (Res->EvaluateAsAbsolute(Value))
741 Res = MCConstantExpr::Create(Value, getContext());
742
743 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000744}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000745
Chris Lattnerb4307b32010-01-15 19:28:38 +0000746bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000747 Res = 0;
748 return ParseParenExpr(Res, EndLoc) ||
749 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000750}
751
Daniel Dunbar475839e2009-06-29 20:37:27 +0000752bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000753 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000754
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000755 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000756 if (ParseExpression(Expr))
757 return true;
758
Daniel Dunbare00b0112009-10-16 01:57:52 +0000759 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000760 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000761
762 return false;
763}
764
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000765static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000766 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000767 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000768 default:
769 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000770
Daniel Dunbarcceba832010-09-17 02:47:07 +0000771 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000772 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000773 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000774 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000775 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000776 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000777 return 1;
778
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000779
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000780 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000781 //
782 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000783 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000784 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000785 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000786 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000787 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000788 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000789 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000790 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000791 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000792
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000793 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000794 case AsmToken::EqualEqual:
795 Kind = MCBinaryExpr::EQ;
796 return 3;
797 case AsmToken::ExclaimEqual:
798 case AsmToken::LessGreater:
799 Kind = MCBinaryExpr::NE;
800 return 3;
801 case AsmToken::Less:
802 Kind = MCBinaryExpr::LT;
803 return 3;
804 case AsmToken::LessEqual:
805 Kind = MCBinaryExpr::LTE;
806 return 3;
807 case AsmToken::Greater:
808 Kind = MCBinaryExpr::GT;
809 return 3;
810 case AsmToken::GreaterEqual:
811 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000812 return 3;
813
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000814 // High Intermediate Precedence: +, -
815 case AsmToken::Plus:
816 Kind = MCBinaryExpr::Add;
817 return 4;
818 case AsmToken::Minus:
819 Kind = MCBinaryExpr::Sub;
820 return 4;
821
Daniel Dunbar475839e2009-06-29 20:37:27 +0000822 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000823 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000824 Kind = MCBinaryExpr::Mul;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000825 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000826 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000827 Kind = MCBinaryExpr::Div;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000828 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000829 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000830 Kind = MCBinaryExpr::Mod;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000831 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000832 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000833 Kind = MCBinaryExpr::Shl;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000834 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000835 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000836 Kind = MCBinaryExpr::Shr;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000837 return 5;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000838 }
839}
840
841
842/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
843/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000844bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
845 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000846 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000847 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000848 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000849
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000850 // If the next token is lower precedence than we are allowed to eat, return
851 // successfully with what we ate already.
852 if (TokPrec < Precedence)
853 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000854
Sean Callanan79ed1a82010-01-19 20:22:31 +0000855 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000856
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000857 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000858 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000859 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000860
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000861 // If BinOp binds less tightly with RHS than the operator after RHS, let
862 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000863 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000864 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000865 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000866 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000867 }
868
Daniel Dunbar475839e2009-06-29 20:37:27 +0000869 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000870 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000871 }
872}
873
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000874
875
876
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000877/// ParseStatement:
878/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000879/// ::= Label* Directive ...Operands... EndOfStatement
880/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000881bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000882 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000883 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000884 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000885 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000886 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000887
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000888 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000889 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000890 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000891 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000892 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000893 // A full line comment is a '#' as the first token.
894 if (Lexer.is(AsmToken::Hash)) {
895 EatToEndOfStatement();
896 return false;
897 }
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000898
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000899 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000900 if (Lexer.is(AsmToken::Integer)) {
901 LocalLabelVal = getTok().getIntVal();
902 if (LocalLabelVal < 0) {
903 if (!TheCondState.Ignore)
904 return TokError("unexpected token at start of statement");
905 IDVal = "";
906 }
907 else {
908 IDVal = getTok().getString();
909 Lex(); // Consume the integer token to be used as an identifier token.
910 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000911 if (!TheCondState.Ignore)
912 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000913 }
914 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000915
916 } else if (Lexer.is(AsmToken::Dot)) {
917 // Treat '.' as a valid identifier in this context.
918 Lex();
919 IDVal = ".";
920
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000921 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000922 if (!TheCondState.Ignore)
923 return TokError("unexpected token at start of statement");
924 IDVal = "";
925 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000926
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000927
Chris Lattner7834fac2010-04-17 18:14:27 +0000928 // Handle conditional assembly here before checking for skipping. We
929 // have to do this so that .endif isn't skipped in a ".if 0" block for
930 // example.
931 if (IDVal == ".if")
932 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000933 if (IDVal == ".ifdef")
934 return ParseDirectiveIfdef(IDLoc, true);
935 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
936 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +0000937 if (IDVal == ".elseif")
938 return ParseDirectiveElseIf(IDLoc);
939 if (IDVal == ".else")
940 return ParseDirectiveElse(IDLoc);
941 if (IDVal == ".endif")
942 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000943
Chris Lattner7834fac2010-04-17 18:14:27 +0000944 // If we are in a ".if 0" block, ignore this statement.
945 if (TheCondState.Ignore) {
946 EatToEndOfStatement();
947 return false;
948 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000949
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000950 // FIXME: Recurse on local labels?
951
952 // See what kind of statement we have.
953 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000954 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000955 CheckForValidSection();
956
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000957 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000958 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000959
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000960 // Diagnose attempt to use '.' as a label.
961 if (IDVal == ".")
962 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
963
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000964 // Diagnose attempt to use a variable as a label.
965 //
966 // FIXME: Diagnostics. Note the location of the definition as a label.
967 // FIXME: This doesn't diagnose assignment to a symbol which has been
968 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000969 MCSymbol *Sym;
970 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000971 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000972 else
973 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000974 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000975 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000976
Daniel Dunbar959fd882009-08-26 22:13:22 +0000977 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000978 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000979
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000980 // Consume any end of statement token, if present, to avoid spurious
981 // AddBlankLine calls().
982 if (Lexer.is(AsmToken::EndOfStatement)) {
983 Lex();
984 if (Lexer.is(AsmToken::Eof))
985 return false;
986 }
987
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000988 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000989 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000990
Daniel Dunbar3f872332009-07-28 16:08:33 +0000991 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000992 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000993 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000994
Nico Weber4c4c7322011-01-28 03:04:41 +0000995 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000996
997 default: // Normal instruction or directive.
998 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000999 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001000
1001 // If macros are enabled, check to see if this is a macro instantiation.
1002 if (MacrosEnabled)
1003 if (const Macro *M = MacroMap.lookup(IDVal))
1004 return HandleMacroEntry(IDVal, IDLoc, M);
1005
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001006 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001007 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001008 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001009 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001010 return ParseDirectiveSet(IDVal, true);
1011 if (IDVal == ".equiv")
1012 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001013
Daniel Dunbara0d14262009-06-24 23:30:00 +00001014 // Data directives
1015
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001016 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001017 return ParseDirectiveAscii(IDVal, false);
1018 if (IDVal == ".asciz" || IDVal == ".string")
1019 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001020
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001021 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001022 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001023 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001024 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001025 if (IDVal == ".value")
1026 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001027 if (IDVal == ".2byte")
1028 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001029 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001030 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001031 if (IDVal == ".int")
1032 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001033 if (IDVal == ".4byte")
1034 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001035 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001036 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001037 if (IDVal == ".8byte")
1038 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001039 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001040 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1041 if (IDVal == ".double")
1042 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001043
Eli Friedman5d68ec22010-07-19 04:17:25 +00001044 if (IDVal == ".align") {
1045 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1046 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1047 }
1048 if (IDVal == ".align32") {
1049 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1050 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1051 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001052 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001053 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001054 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001055 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001056 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001057 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001058 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001059 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001060 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001061 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001062 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001063 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1064
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001065 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001066 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001067
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001068 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001069 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001070 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001071 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001072 if (IDVal == ".zero")
1073 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001074
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001075 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001076
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001077 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001078 return ParseDirectiveSymbolAttribute(MCSA_Global);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +00001079 // ELF only? Should it be here?
1080 if (IDVal == ".local")
1081 return ParseDirectiveSymbolAttribute(MCSA_Local);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001082 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001083 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001084 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001085 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001086 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001087 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001088 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001089 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001090 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001091 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001092 if (IDVal == ".symbol_resolver")
1093 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001094 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001095 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001096 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001097 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001098 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001099 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001100 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001101 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001102 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001103 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001104 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001105 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001106 if (IDVal == ".weak_def_can_be_hidden")
1107 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001108
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001109 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001110 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001111 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001112 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001113
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001114 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001115 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001116 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001117 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001118
Roman Divackybb6d14f2011-01-31 21:19:43 +00001119 if (IDVal == ".code16" || IDVal == ".code32" || IDVal == ".code64")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001120 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001121
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001122 // Look up the handler in the handler table.
1123 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1124 DirectiveMap.lookup(IDVal);
1125 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001126 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001127
Kevin Enderby9c656452009-09-10 20:51:44 +00001128 // Target hook for parsing target specific directives.
1129 if (!getTargetParser().ParseDirective(ID))
1130 return false;
1131
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001132 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001133 EatToEndOfStatement();
1134 return false;
1135 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001136
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001137 CheckForValidSection();
1138
Chris Lattnera7f13542010-05-19 23:34:33 +00001139 // Canonicalize the opcode to lower case.
1140 SmallString<128> Opcode;
1141 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1142 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001143
Chris Lattner98986712010-01-14 22:21:20 +00001144 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001145 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001146 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001147
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001148 // Dump the parsed representation, if requested.
1149 if (getShowParsedOperands()) {
1150 SmallString<256> Str;
1151 raw_svector_ostream OS(Str);
1152 OS << "parsed instruction: [";
1153 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1154 if (i != 0)
1155 OS << ", ";
1156 ParsedOperands[i]->dump(OS);
1157 }
1158 OS << "]";
1159
1160 PrintMessage(IDLoc, OS.str(), "note");
1161 }
1162
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001163 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001164 if (!HadError)
1165 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1166 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001167
Chris Lattner98986712010-01-14 22:21:20 +00001168 // Free any parsed operands.
1169 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1170 delete ParsedOperands[i];
1171
Chris Lattnercbf8a982010-09-11 16:18:25 +00001172 // Don't skip the rest of the line, the instruction parser is responsible for
1173 // that.
1174 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001175}
Chris Lattner9a023f72009-06-24 04:43:34 +00001176
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001177MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1178 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001179 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
1180{
1181 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1182 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001183 SmallString<256> Buf;
1184 raw_svector_ostream OS(Buf);
1185
1186 StringRef Body = M->Body;
1187 while (!Body.empty()) {
1188 // Scan for the next substitution.
1189 std::size_t End = Body.size(), Pos = 0;
1190 for (; Pos != End; ++Pos) {
1191 // Check for a substitution or escape.
1192 if (Body[Pos] != '$' || Pos + 1 == End)
1193 continue;
1194
1195 char Next = Body[Pos + 1];
1196 if (Next == '$' || Next == 'n' || isdigit(Next))
1197 break;
1198 }
1199
1200 // Add the prefix.
1201 OS << Body.slice(0, Pos);
1202
1203 // Check if we reached the end.
1204 if (Pos == End)
1205 break;
1206
1207 switch (Body[Pos+1]) {
1208 // $$ => $
1209 case '$':
1210 OS << '$';
1211 break;
1212
1213 // $n => number of arguments
1214 case 'n':
1215 OS << A.size();
1216 break;
1217
1218 // $[0-9] => argument
1219 default: {
1220 // Missing arguments are ignored.
1221 unsigned Index = Body[Pos+1] - '0';
1222 if (Index >= A.size())
1223 break;
1224
1225 // Otherwise substitute with the token values, with spaces eliminated.
1226 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1227 ie = A[Index].end(); it != ie; ++it)
1228 OS << it->getString();
1229 break;
1230 }
1231 }
1232
1233 // Update the scan point.
1234 Body = Body.substr(Pos + 2);
1235 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001236
1237 // We include the .endmacro in the buffer as our queue to exit the macro
1238 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001239 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001240
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001241 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001242}
1243
1244bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1245 const Macro *M) {
1246 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1247 // this, although we should protect against infinite loops.
1248 if (ActiveMacros.size() == 20)
1249 return TokError("macros cannot be nested more than 20 levels deep");
1250
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001251 // Parse the macro instantiation arguments.
1252 std::vector<std::vector<AsmToken> > MacroArguments;
1253 MacroArguments.push_back(std::vector<AsmToken>());
1254 unsigned ParenLevel = 0;
1255 for (;;) {
1256 if (Lexer.is(AsmToken::Eof))
1257 return TokError("unexpected token in macro instantiation");
1258 if (Lexer.is(AsmToken::EndOfStatement))
1259 break;
1260
1261 // If we aren't inside parentheses and this is a comma, start a new token
1262 // list.
1263 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1264 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001265 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001266 // Adjust the current parentheses level.
1267 if (Lexer.is(AsmToken::LParen))
1268 ++ParenLevel;
1269 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1270 --ParenLevel;
1271
1272 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001273 MacroArguments.back().push_back(getTok());
1274 }
1275 Lex();
1276 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001277
1278 // Create the macro instantiation object and add to the current macro
1279 // instantiation stack.
1280 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001281 getTok().getLoc(),
1282 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001283 ActiveMacros.push_back(MI);
1284
1285 // Jump to the macro instantiation and prime the lexer.
1286 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1287 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1288 Lex();
1289
1290 return false;
1291}
1292
1293void AsmParser::HandleMacroExit() {
1294 // Jump to the EndOfStatement we should return to, and consume it.
1295 JumpToLoc(ActiveMacros.back()->ExitLoc);
1296 Lex();
1297
1298 // Pop the instantiation entry.
1299 delete ActiveMacros.back();
1300 ActiveMacros.pop_back();
1301}
1302
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001303static void MarkUsed(const MCExpr *Value) {
1304 switch (Value->getKind()) {
1305 case MCExpr::Binary:
1306 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1307 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1308 break;
1309 case MCExpr::Target:
1310 case MCExpr::Constant:
1311 break;
1312 case MCExpr::SymbolRef: {
1313 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1314 break;
1315 }
1316 case MCExpr::Unary:
1317 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1318 break;
1319 }
1320}
1321
Nico Weber4c4c7322011-01-28 03:04:41 +00001322bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001323 // FIXME: Use better location, we should use proper tokens.
1324 SMLoc EqualLoc = Lexer.getLoc();
1325
Daniel Dunbar821e3332009-08-31 08:09:28 +00001326 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001327 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001328 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001329
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001330 MarkUsed(Value);
1331
Daniel Dunbar3f872332009-07-28 16:08:33 +00001332 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001333 return TokError("unexpected token in assignment");
1334
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001335 // Error on assignment to '.'.
1336 if (Name == ".") {
1337 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1338 "(use '.space' or '.org').)"));
1339 }
1340
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001341 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001342 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001343
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001344 // Validate that the LHS is allowed to be a variable (either it has not been
1345 // used as a symbol, or it is an absolute symbol).
1346 MCSymbol *Sym = getContext().LookupSymbol(Name);
1347 if (Sym) {
1348 // Diagnose assignment to a label.
1349 //
1350 // FIXME: Diagnostics. Note the location of the definition as a label.
1351 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001352 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001353 ; // Allow redefinitions of undefined symbols only used in directives.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001354 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001355 return Error(EqualLoc, "redefinition of '" + Name + "'");
1356 else if (!Sym->isVariable())
1357 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001358 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001359 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1360 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001361
1362 // Don't count these checks as uses.
1363 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001364 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001365 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001366
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001367 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001368
1369 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001370 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001371
1372 return false;
1373}
1374
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001375/// ParseIdentifier:
1376/// ::= identifier
1377/// ::= string
1378bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001379 // The assembler has relaxed rules for accepting identifiers, in particular we
1380 // allow things like '.globl $foo', which would normally be separate
1381 // tokens. At this level, we have already lexed so we cannot (currently)
1382 // handle this as a context dependent token, instead we detect adjacent tokens
1383 // and return the combined identifier.
1384 if (Lexer.is(AsmToken::Dollar)) {
1385 SMLoc DollarLoc = getLexer().getLoc();
1386
1387 // Consume the dollar sign, and check for a following identifier.
1388 Lex();
1389 if (Lexer.isNot(AsmToken::Identifier))
1390 return true;
1391
1392 // We have a '$' followed by an identifier, make sure they are adjacent.
1393 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1394 return true;
1395
1396 // Construct the joined identifier and consume the token.
1397 Res = StringRef(DollarLoc.getPointer(),
1398 getTok().getIdentifier().size() + 1);
1399 Lex();
1400 return false;
1401 }
1402
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001403 if (Lexer.isNot(AsmToken::Identifier) &&
1404 Lexer.isNot(AsmToken::String))
1405 return true;
1406
Sean Callanan18b83232010-01-19 21:44:56 +00001407 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001408
Sean Callanan79ed1a82010-01-19 20:22:31 +00001409 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001410
1411 return false;
1412}
1413
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001414/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001415/// ::= .equ identifier ',' expression
1416/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001417/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001418bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001419 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001420
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001421 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001422 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001423
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001424 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001425 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001426 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001427
Nico Weber4c4c7322011-01-28 03:04:41 +00001428 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001429}
1430
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001431bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001432 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001433
1434 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001435 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001436 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1437 if (Str[i] != '\\') {
1438 Data += Str[i];
1439 continue;
1440 }
1441
1442 // Recognize escaped characters. Note that this escape semantics currently
1443 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1444 ++i;
1445 if (i == e)
1446 return TokError("unexpected backslash at end of string");
1447
1448 // Recognize octal sequences.
1449 if ((unsigned) (Str[i] - '0') <= 7) {
1450 // Consume up to three octal characters.
1451 unsigned Value = Str[i] - '0';
1452
1453 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1454 ++i;
1455 Value = Value * 8 + (Str[i] - '0');
1456
1457 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1458 ++i;
1459 Value = Value * 8 + (Str[i] - '0');
1460 }
1461 }
1462
1463 if (Value > 255)
1464 return TokError("invalid octal escape sequence (out of range)");
1465
1466 Data += (unsigned char) Value;
1467 continue;
1468 }
1469
1470 // Otherwise recognize individual escapes.
1471 switch (Str[i]) {
1472 default:
1473 // Just reject invalid escape sequences for now.
1474 return TokError("invalid escape sequence (unrecognized character)");
1475
1476 case 'b': Data += '\b'; break;
1477 case 'f': Data += '\f'; break;
1478 case 'n': Data += '\n'; break;
1479 case 'r': Data += '\r'; break;
1480 case 't': Data += '\t'; break;
1481 case '"': Data += '"'; break;
1482 case '\\': Data += '\\'; break;
1483 }
1484 }
1485
1486 return false;
1487}
1488
Daniel Dunbara0d14262009-06-24 23:30:00 +00001489/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001490/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1491bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001492 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001493 CheckForValidSection();
1494
Daniel Dunbara0d14262009-06-24 23:30:00 +00001495 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001496 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001497 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001498
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001499 std::string Data;
1500 if (ParseEscapedString(Data))
1501 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001502
1503 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001504 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001505 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1506
Sean Callanan79ed1a82010-01-19 20:22:31 +00001507 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001508
1509 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001510 break;
1511
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001512 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001513 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001514 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001515 }
1516 }
1517
Sean Callanan79ed1a82010-01-19 20:22:31 +00001518 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001519 return false;
1520}
1521
1522/// ParseDirectiveValue
1523/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1524bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001525 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001526 CheckForValidSection();
1527
Daniel Dunbara0d14262009-06-24 23:30:00 +00001528 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001529 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001530 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001531 return true;
1532
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001533 // Special case constant expressions to match code generator.
1534 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001535 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001536 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001537 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001538
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001539 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001540 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001541
Daniel Dunbara0d14262009-06-24 23:30:00 +00001542 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001543 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001544 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001545 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001546 }
1547 }
1548
Sean Callanan79ed1a82010-01-19 20:22:31 +00001549 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001550 return false;
1551}
1552
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001553/// ParseDirectiveRealValue
1554/// ::= (.single | .double) [ expression (, expression)* ]
1555bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1556 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1557 CheckForValidSection();
1558
1559 for (;;) {
1560 // We don't truly support arithmetic on floating point expressions, so we
1561 // have to manually parse unary prefixes.
1562 bool IsNeg = false;
1563 if (getLexer().is(AsmToken::Minus)) {
1564 Lex();
1565 IsNeg = true;
1566 } else if (getLexer().is(AsmToken::Plus))
1567 Lex();
1568
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001569 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001570 getLexer().isNot(AsmToken::Real) &&
1571 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001572 return TokError("unexpected token in directive");
1573
1574 // Convert to an APFloat.
1575 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001576 StringRef IDVal = getTok().getString();
1577 if (getLexer().is(AsmToken::Identifier)) {
1578 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1579 Value = APFloat::getInf(Semantics);
1580 else if (!IDVal.compare_lower("nan"))
1581 Value = APFloat::getNaN(Semantics, false, ~0);
1582 else
1583 return TokError("invalid floating point literal");
1584 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001585 APFloat::opInvalidOp)
1586 return TokError("invalid floating point literal");
1587 if (IsNeg)
1588 Value.changeSign();
1589
1590 // Consume the numeric token.
1591 Lex();
1592
1593 // Emit the value as an integer.
1594 APInt AsInt = Value.bitcastToAPInt();
1595 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1596 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1597
1598 if (getLexer().is(AsmToken::EndOfStatement))
1599 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001600
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001601 if (getLexer().isNot(AsmToken::Comma))
1602 return TokError("unexpected token in directive");
1603 Lex();
1604 }
1605 }
1606
1607 Lex();
1608 return false;
1609}
1610
Daniel Dunbara0d14262009-06-24 23:30:00 +00001611/// ParseDirectiveSpace
1612/// ::= .space expression [ , expression ]
1613bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001614 CheckForValidSection();
1615
Daniel Dunbara0d14262009-06-24 23:30:00 +00001616 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001617 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001618 return true;
1619
1620 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001621 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1622 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001623 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001624 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001625
Daniel Dunbar475839e2009-06-29 20:37:27 +00001626 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001627 return true;
1628
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001629 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001630 return TokError("unexpected token in '.space' directive");
1631 }
1632
Sean Callanan79ed1a82010-01-19 20:22:31 +00001633 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001634
1635 if (NumBytes <= 0)
1636 return TokError("invalid number of bytes in '.space' directive");
1637
1638 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001639 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001640
1641 return false;
1642}
1643
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001644/// ParseDirectiveZero
1645/// ::= .zero expression
1646bool AsmParser::ParseDirectiveZero() {
1647 CheckForValidSection();
1648
1649 int64_t NumBytes;
1650 if (ParseAbsoluteExpression(NumBytes))
1651 return true;
1652
Rafael Espindolae452b172010-10-05 19:42:57 +00001653 int64_t Val = 0;
1654 if (getLexer().is(AsmToken::Comma)) {
1655 Lex();
1656 if (ParseAbsoluteExpression(Val))
1657 return true;
1658 }
1659
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001660 if (getLexer().isNot(AsmToken::EndOfStatement))
1661 return TokError("unexpected token in '.zero' directive");
1662
1663 Lex();
1664
Rafael Espindolae452b172010-10-05 19:42:57 +00001665 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001666
1667 return false;
1668}
1669
Daniel Dunbara0d14262009-06-24 23:30:00 +00001670/// ParseDirectiveFill
1671/// ::= .fill expression , expression , expression
1672bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001673 CheckForValidSection();
1674
Daniel Dunbara0d14262009-06-24 23:30:00 +00001675 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001676 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001677 return true;
1678
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001679 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001680 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001681 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001682
Daniel Dunbara0d14262009-06-24 23:30:00 +00001683 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001684 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001685 return true;
1686
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001687 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001688 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001689 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001690
Daniel Dunbara0d14262009-06-24 23:30:00 +00001691 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001692 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001693 return true;
1694
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001695 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001696 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001697
Sean Callanan79ed1a82010-01-19 20:22:31 +00001698 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001699
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001700 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1701 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001702
1703 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001704 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001705
1706 return false;
1707}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001708
1709/// ParseDirectiveOrg
1710/// ::= .org expression [ , expression ]
1711bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001712 CheckForValidSection();
1713
Daniel Dunbar821e3332009-08-31 08:09:28 +00001714 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001715 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001716 return true;
1717
1718 // Parse optional fill expression.
1719 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001720 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1721 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001722 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001723 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001724
Daniel Dunbar475839e2009-06-29 20:37:27 +00001725 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001726 return true;
1727
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001728 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001729 return TokError("unexpected token in '.org' directive");
1730 }
1731
Sean Callanan79ed1a82010-01-19 20:22:31 +00001732 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001733
1734 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1735 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001736 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001737
1738 return false;
1739}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001740
1741/// ParseDirectiveAlign
1742/// ::= {.align, ...} expression [ , expression [ , expression ]]
1743bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001744 CheckForValidSection();
1745
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001746 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001747 int64_t Alignment;
1748 if (ParseAbsoluteExpression(Alignment))
1749 return true;
1750
1751 SMLoc MaxBytesLoc;
1752 bool HasFillExpr = false;
1753 int64_t FillExpr = 0;
1754 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001755 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1756 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001757 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001758 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001759
1760 // The fill expression can be omitted while specifying a maximum number of
1761 // alignment bytes, e.g:
1762 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001763 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001764 HasFillExpr = true;
1765 if (ParseAbsoluteExpression(FillExpr))
1766 return true;
1767 }
1768
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001769 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1770 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001771 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001772 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001773
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001774 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001775 if (ParseAbsoluteExpression(MaxBytesToFill))
1776 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001777
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001778 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001779 return TokError("unexpected token in directive");
1780 }
1781 }
1782
Sean Callanan79ed1a82010-01-19 20:22:31 +00001783 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001784
Daniel Dunbar648ac512010-05-17 21:54:30 +00001785 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001786 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001787
1788 // Compute alignment in bytes.
1789 if (IsPow2) {
1790 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001791 if (Alignment >= 32) {
1792 Error(AlignmentLoc, "invalid alignment value");
1793 Alignment = 31;
1794 }
1795
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001796 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001797 }
1798
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001799 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001800 if (MaxBytesLoc.isValid()) {
1801 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001802 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1803 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001804 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001805 }
1806
1807 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001808 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1809 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001810 MaxBytesToFill = 0;
1811 }
1812 }
1813
Daniel Dunbar648ac512010-05-17 21:54:30 +00001814 // Check whether we should use optimal code alignment for this .align
1815 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001816 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001817 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1818 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001819 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001820 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001821 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001822 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1823 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001824 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001825
1826 return false;
1827}
1828
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001829/// ParseDirectiveSymbolAttribute
1830/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001831bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001832 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001833 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001834 StringRef Name;
1835
1836 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001837 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001838
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001839 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001840
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001841 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001842
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001843 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001844 break;
1845
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001846 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001847 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001848 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001849 }
1850 }
1851
Sean Callanan79ed1a82010-01-19 20:22:31 +00001852 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001853 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001854}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001855
1856/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001857/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1858bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001859 CheckForValidSection();
1860
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001861 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001862 StringRef Name;
1863 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001864 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001865
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001866 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001867 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001868
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001869 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001870 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001871 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001872
1873 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001874 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001875 if (ParseAbsoluteExpression(Size))
1876 return true;
1877
1878 int64_t Pow2Alignment = 0;
1879 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001880 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001881 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001882 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001883 if (ParseAbsoluteExpression(Pow2Alignment))
1884 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001885
Chris Lattner258281d2010-01-19 06:22:22 +00001886 // If this target takes alignments in bytes (not log) validate and convert.
1887 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1888 if (!isPowerOf2_64(Pow2Alignment))
1889 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1890 Pow2Alignment = Log2_64(Pow2Alignment);
1891 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001892 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001893
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001894 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001895 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001896
Sean Callanan79ed1a82010-01-19 20:22:31 +00001897 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001898
Chris Lattner1fc3d752009-07-09 17:25:12 +00001899 // NOTE: a size of zero for a .comm should create a undefined symbol
1900 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001901 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001902 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1903 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001904
Eric Christopherc260a3e2010-05-14 01:38:54 +00001905 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001906 // may internally end up wanting an alignment in bytes.
1907 // FIXME: Diagnose overflow.
1908 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001909 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1910 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001911
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001912 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001913 return Error(IDLoc, "invalid symbol redefinition");
1914
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001915 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001916 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001917 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001918 getStreamer().EmitZerofill(Ctx.getMachOSection(
1919 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1920 0, SectionKind::getBSS()),
1921 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001922 return false;
1923 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001924
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001925 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001926 return false;
1927}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001928
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001929/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001930/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001931bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001932 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001933 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001934
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001935 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001936 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001937 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001938
Sean Callanan79ed1a82010-01-19 20:22:31 +00001939 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001940
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001941 if (Str.empty())
1942 Error(Loc, ".abort detected. Assembly stopping.");
1943 else
1944 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001945 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001946
1947 return false;
1948}
Kevin Enderby71148242009-07-14 21:35:03 +00001949
Kevin Enderby1f049b22009-07-14 23:21:55 +00001950/// ParseDirectiveInclude
1951/// ::= .include "filename"
1952bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001953 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001954 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001955
Sean Callanan18b83232010-01-19 21:44:56 +00001956 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001957 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001958 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001959
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001960 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001961 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001962
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001963 // Strip the quotes.
1964 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001965
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001966 // Attempt to switch the lexer to the included file before consuming the end
1967 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001968 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001969 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001970 return true;
1971 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001972
1973 return false;
1974}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001975
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001976/// ParseDirectiveIf
1977/// ::= .if expression
1978bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001979 TheCondStack.push_back(TheCondState);
1980 TheCondState.TheCond = AsmCond::IfCond;
1981 if(TheCondState.Ignore) {
1982 EatToEndOfStatement();
1983 }
1984 else {
1985 int64_t ExprValue;
1986 if (ParseAbsoluteExpression(ExprValue))
1987 return true;
1988
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001989 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001990 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001991
Sean Callanan79ed1a82010-01-19 20:22:31 +00001992 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001993
1994 TheCondState.CondMet = ExprValue;
1995 TheCondState.Ignore = !TheCondState.CondMet;
1996 }
1997
1998 return false;
1999}
2000
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002001bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2002 StringRef Name;
2003 TheCondStack.push_back(TheCondState);
2004 TheCondState.TheCond = AsmCond::IfCond;
2005
2006 if (TheCondState.Ignore) {
2007 EatToEndOfStatement();
2008 } else {
2009 if (ParseIdentifier(Name))
2010 return TokError("expected identifier after '.ifdef'");
2011
2012 Lex();
2013
2014 MCSymbol *Sym = getContext().LookupSymbol(Name);
2015
2016 if (expect_defined)
2017 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2018 else
2019 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2020 TheCondState.Ignore = !TheCondState.CondMet;
2021 }
2022
2023 return false;
2024}
2025
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002026/// ParseDirectiveElseIf
2027/// ::= .elseif expression
2028bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2029 if (TheCondState.TheCond != AsmCond::IfCond &&
2030 TheCondState.TheCond != AsmCond::ElseIfCond)
2031 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2032 " an .elseif");
2033 TheCondState.TheCond = AsmCond::ElseIfCond;
2034
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002035 bool LastIgnoreState = false;
2036 if (!TheCondStack.empty())
2037 LastIgnoreState = TheCondStack.back().Ignore;
2038 if (LastIgnoreState || TheCondState.CondMet) {
2039 TheCondState.Ignore = true;
2040 EatToEndOfStatement();
2041 }
2042 else {
2043 int64_t ExprValue;
2044 if (ParseAbsoluteExpression(ExprValue))
2045 return true;
2046
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002047 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002048 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002049
Sean Callanan79ed1a82010-01-19 20:22:31 +00002050 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002051 TheCondState.CondMet = ExprValue;
2052 TheCondState.Ignore = !TheCondState.CondMet;
2053 }
2054
2055 return false;
2056}
2057
2058/// ParseDirectiveElse
2059/// ::= .else
2060bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002061 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002062 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002063
Sean Callanan79ed1a82010-01-19 20:22:31 +00002064 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002065
2066 if (TheCondState.TheCond != AsmCond::IfCond &&
2067 TheCondState.TheCond != AsmCond::ElseIfCond)
2068 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2069 ".elseif");
2070 TheCondState.TheCond = AsmCond::ElseCond;
2071 bool LastIgnoreState = false;
2072 if (!TheCondStack.empty())
2073 LastIgnoreState = TheCondStack.back().Ignore;
2074 if (LastIgnoreState || TheCondState.CondMet)
2075 TheCondState.Ignore = true;
2076 else
2077 TheCondState.Ignore = false;
2078
2079 return false;
2080}
2081
2082/// ParseDirectiveEndIf
2083/// ::= .endif
2084bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002085 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002086 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002087
Sean Callanan79ed1a82010-01-19 20:22:31 +00002088 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002089
2090 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2091 TheCondStack.empty())
2092 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2093 ".else");
2094 if (!TheCondStack.empty()) {
2095 TheCondState = TheCondStack.back();
2096 TheCondStack.pop_back();
2097 }
2098
2099 return false;
2100}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002101
2102/// ParseDirectiveFile
2103/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002104bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002105 // FIXME: I'm not sure what this is.
2106 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002107 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002108 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002109 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002110 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002111
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002112 if (FileNumber < 1)
2113 return TokError("file number less than one");
2114 }
2115
Daniel Dunbareceec052010-07-12 17:45:27 +00002116 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002117 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002118
Chris Lattnerd32e8032010-01-25 19:02:58 +00002119 StringRef Filename = getTok().getString();
2120 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002121 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002122
Daniel Dunbareceec052010-07-12 17:45:27 +00002123 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002124 return TokError("unexpected token in '.file' directive");
2125
Chris Lattnerd32e8032010-01-25 19:02:58 +00002126 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002127 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002128 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002129 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002130 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002131 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002132
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002133 return false;
2134}
2135
2136/// ParseDirectiveLine
2137/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002138bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002139 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2140 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002141 return TokError("unexpected token in '.line' directive");
2142
Sean Callanan18b83232010-01-19 21:44:56 +00002143 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002144 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002145 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002146
2147 // FIXME: Do something with the .line.
2148 }
2149
Daniel Dunbareceec052010-07-12 17:45:27 +00002150 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002151 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002152
2153 return false;
2154}
2155
2156
2157/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002158/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002159/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2160/// The first number is a file number, must have been previously assigned with
2161/// a .file directive, the second number is the line number and optionally the
2162/// third number is a column position (zero if not specified). The remaining
2163/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002164bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002165
Daniel Dunbareceec052010-07-12 17:45:27 +00002166 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002167 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002168 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002169 if (FileNumber < 1)
2170 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002171 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002172 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002173 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002174
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002175 int64_t LineNumber = 0;
2176 if (getLexer().is(AsmToken::Integer)) {
2177 LineNumber = getTok().getIntVal();
2178 if (LineNumber < 1)
2179 return TokError("line number less than one in '.loc' directive");
2180 Lex();
2181 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002182
2183 int64_t ColumnPos = 0;
2184 if (getLexer().is(AsmToken::Integer)) {
2185 ColumnPos = getTok().getIntVal();
2186 if (ColumnPos < 0)
2187 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002188 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002189 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002190
Kevin Enderbyc0957932010-09-30 16:52:03 +00002191 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002192 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002193 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002194 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2195 for (;;) {
2196 if (getLexer().is(AsmToken::EndOfStatement))
2197 break;
2198
2199 StringRef Name;
2200 SMLoc Loc = getTok().getLoc();
2201 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002202 return TokError("unexpected token in '.loc' directive");
2203
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002204 if (Name == "basic_block")
2205 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2206 else if (Name == "prologue_end")
2207 Flags |= DWARF2_FLAG_PROLOGUE_END;
2208 else if (Name == "epilogue_begin")
2209 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2210 else if (Name == "is_stmt") {
2211 SMLoc Loc = getTok().getLoc();
2212 const MCExpr *Value;
2213 if (getParser().ParseExpression(Value))
2214 return true;
2215 // The expression must be the constant 0 or 1.
2216 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2217 int Value = MCE->getValue();
2218 if (Value == 0)
2219 Flags &= ~DWARF2_FLAG_IS_STMT;
2220 else if (Value == 1)
2221 Flags |= DWARF2_FLAG_IS_STMT;
2222 else
2223 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002224 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002225 else {
2226 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2227 }
2228 }
2229 else if (Name == "isa") {
2230 SMLoc Loc = getTok().getLoc();
2231 const MCExpr *Value;
2232 if (getParser().ParseExpression(Value))
2233 return true;
2234 // The expression must be a constant greater or equal to 0.
2235 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2236 int Value = MCE->getValue();
2237 if (Value < 0)
2238 return Error(Loc, "isa number less than zero");
2239 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002240 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002241 else {
2242 return Error(Loc, "isa number not a constant value");
2243 }
2244 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002245 else if (Name == "discriminator") {
2246 if (getParser().ParseAbsoluteExpression(Discriminator))
2247 return true;
2248 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002249 else {
2250 return Error(Loc, "unknown sub-directive in '.loc' directive");
2251 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002252
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002253 if (getLexer().is(AsmToken::EndOfStatement))
2254 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002255 }
2256 }
2257
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002258 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002259 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002260
2261 return false;
2262}
2263
Daniel Dunbar138abae2010-10-16 04:56:42 +00002264/// ParseDirectiveStabs
2265/// ::= .stabs string, number, number, number
2266bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2267 SMLoc DirectiveLoc) {
2268 return TokError("unsupported directive '" + Directive + "'");
2269}
2270
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002271/// ParseDirectiveCFISections
2272/// ::= .cfi_sections section [, section]
2273bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2274 SMLoc DirectiveLoc) {
2275 StringRef Name;
2276 bool EH = false;
2277 bool Debug = false;
2278
2279 if (getParser().ParseIdentifier(Name))
2280 return TokError("Expected an identifier");
2281
2282 if (Name == ".eh_frame")
2283 EH = true;
2284 else if (Name == ".debug_frame")
2285 Debug = true;
2286
2287 if (getLexer().is(AsmToken::Comma)) {
2288 Lex();
2289
2290 if (getParser().ParseIdentifier(Name))
2291 return TokError("Expected an identifier");
2292
2293 if (Name == ".eh_frame")
2294 EH = true;
2295 else if (Name == ".debug_frame")
2296 Debug = true;
2297 }
2298
2299 getStreamer().EmitCFISections(EH, Debug);
2300
2301 return false;
2302}
2303
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002304/// ParseDirectiveCFIStartProc
2305/// ::= .cfi_startproc
2306bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2307 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002308 getStreamer().EmitCFIStartProc();
2309 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002310}
2311
2312/// ParseDirectiveCFIEndProc
2313/// ::= .cfi_endproc
2314bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002315 getStreamer().EmitCFIEndProc();
2316 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002317}
2318
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002319/// ParseRegisterOrRegisterNumber - parse register name or number.
2320bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2321 SMLoc DirectiveLoc) {
2322 unsigned RegNo;
2323
2324 if (getLexer().is(AsmToken::Percent)) {
2325 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2326 DirectiveLoc))
2327 return true;
2328 Register = getContext().getTargetAsmInfo().getDwarfRegNum(RegNo, true);
2329 } else
2330 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002331
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002332 return false;
2333}
2334
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002335/// ParseDirectiveCFIDefCfa
2336/// ::= .cfi_def_cfa register, offset
2337bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2338 SMLoc DirectiveLoc) {
2339 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002340 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002341 return true;
2342
2343 if (getLexer().isNot(AsmToken::Comma))
2344 return TokError("unexpected token in directive");
2345 Lex();
2346
2347 int64_t Offset = 0;
2348 if (getParser().ParseAbsoluteExpression(Offset))
2349 return true;
2350
Rafael Espindola066c2f42011-04-12 23:59:07 +00002351 getStreamer().EmitCFIDefCfa(Register, Offset);
2352 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002353}
2354
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002355/// ParseDirectiveCFIDefCfaOffset
2356/// ::= .cfi_def_cfa_offset offset
2357bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2358 SMLoc DirectiveLoc) {
2359 int64_t Offset = 0;
2360 if (getParser().ParseAbsoluteExpression(Offset))
2361 return true;
2362
Rafael Espindola066c2f42011-04-12 23:59:07 +00002363 getStreamer().EmitCFIDefCfaOffset(Offset);
2364 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002365}
2366
2367/// ParseDirectiveCFIAdjustCfaOffset
2368/// ::= .cfi_adjust_cfa_offset adjustment
2369bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2370 SMLoc DirectiveLoc) {
2371 int64_t Adjustment = 0;
2372 if (getParser().ParseAbsoluteExpression(Adjustment))
2373 return true;
2374
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002375 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2376 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002377}
2378
2379/// ParseDirectiveCFIDefCfaRegister
2380/// ::= .cfi_def_cfa_register register
2381bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2382 SMLoc DirectiveLoc) {
2383 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002384 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002385 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002386
Rafael Espindola066c2f42011-04-12 23:59:07 +00002387 getStreamer().EmitCFIDefCfaRegister(Register);
2388 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002389}
2390
2391/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002392/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002393bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2394 int64_t Register = 0;
2395 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002396
2397 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002398 return true;
2399
2400 if (getLexer().isNot(AsmToken::Comma))
2401 return TokError("unexpected token in directive");
2402 Lex();
2403
2404 if (getParser().ParseAbsoluteExpression(Offset))
2405 return true;
2406
Rafael Espindola066c2f42011-04-12 23:59:07 +00002407 getStreamer().EmitCFIOffset(Register, Offset);
2408 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002409}
2410
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002411/// ParseDirectiveCFIRelOffset
2412/// ::= .cfi_rel_offset register, offset
2413bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2414 SMLoc DirectiveLoc) {
2415 int64_t Register = 0;
2416
2417 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2418 return true;
2419
2420 if (getLexer().isNot(AsmToken::Comma))
2421 return TokError("unexpected token in directive");
2422 Lex();
2423
2424 int64_t Offset = 0;
2425 if (getParser().ParseAbsoluteExpression(Offset))
2426 return true;
2427
Rafael Espindola25f492e2011-04-12 16:12:03 +00002428 getStreamer().EmitCFIRelOffset(Register, Offset);
2429 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002430}
2431
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002432static bool isValidEncoding(int64_t Encoding) {
2433 if (Encoding & ~0xff)
2434 return false;
2435
2436 if (Encoding == dwarf::DW_EH_PE_omit)
2437 return true;
2438
2439 const unsigned Format = Encoding & 0xf;
2440 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2441 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2442 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2443 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2444 return false;
2445
Rafael Espindolacaf11582010-12-29 04:31:26 +00002446 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002447 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002448 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002449 return false;
2450
2451 return true;
2452}
2453
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002454/// ParseDirectiveCFIPersonalityOrLsda
2455/// ::= .cfi_personality encoding, [symbol_name]
2456/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002457bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002458 SMLoc DirectiveLoc) {
2459 int64_t Encoding = 0;
2460 if (getParser().ParseAbsoluteExpression(Encoding))
2461 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002462 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002463 return false;
2464
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002465 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002466 return TokError("unsupported encoding.");
2467
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002468 if (getLexer().isNot(AsmToken::Comma))
2469 return TokError("unexpected token in directive");
2470 Lex();
2471
2472 StringRef Name;
2473 if (getParser().ParseIdentifier(Name))
2474 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002475
2476 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2477
2478 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002479 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002480 else {
2481 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002482 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002483 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002484 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002485}
2486
Rafael Espindolafe024d02010-12-28 18:36:23 +00002487/// ParseDirectiveCFIRememberState
2488/// ::= .cfi_remember_state
2489bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2490 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002491 getStreamer().EmitCFIRememberState();
2492 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002493}
2494
2495/// ParseDirectiveCFIRestoreState
2496/// ::= .cfi_remember_state
2497bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2498 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002499 getStreamer().EmitCFIRestoreState();
2500 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002501}
2502
Rafael Espindolac5754392011-04-12 15:31:05 +00002503/// ParseDirectiveCFISameValue
2504/// ::= .cfi_same_value register
2505bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2506 SMLoc DirectiveLoc) {
2507 int64_t Register = 0;
2508
2509 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2510 return true;
2511
2512 getStreamer().EmitCFISameValue(Register);
2513
2514 return false;
2515}
2516
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002517/// ParseDirectiveMacrosOnOff
2518/// ::= .macros_on
2519/// ::= .macros_off
2520bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2521 SMLoc DirectiveLoc) {
2522 if (getLexer().isNot(AsmToken::EndOfStatement))
2523 return Error(getLexer().getLoc(),
2524 "unexpected token in '" + Directive + "' directive");
2525
2526 getParser().MacrosEnabled = Directive == ".macros_on";
2527
2528 return false;
2529}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002530
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002531/// ParseDirectiveMacro
2532/// ::= .macro name
2533bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2534 SMLoc DirectiveLoc) {
2535 StringRef Name;
2536 if (getParser().ParseIdentifier(Name))
2537 return TokError("expected identifier in directive");
2538
2539 if (getLexer().isNot(AsmToken::EndOfStatement))
2540 return TokError("unexpected token in '.macro' directive");
2541
2542 // Eat the end of statement.
2543 Lex();
2544
2545 AsmToken EndToken, StartToken = getTok();
2546
2547 // Lex the macro definition.
2548 for (;;) {
2549 // Check whether we have reached the end of the file.
2550 if (getLexer().is(AsmToken::Eof))
2551 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2552
2553 // Otherwise, check whether we have reach the .endmacro.
2554 if (getLexer().is(AsmToken::Identifier) &&
2555 (getTok().getIdentifier() == ".endm" ||
2556 getTok().getIdentifier() == ".endmacro")) {
2557 EndToken = getTok();
2558 Lex();
2559 if (getLexer().isNot(AsmToken::EndOfStatement))
2560 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2561 "' directive");
2562 break;
2563 }
2564
2565 // Otherwise, scan til the end of the statement.
2566 getParser().EatToEndOfStatement();
2567 }
2568
2569 if (getParser().MacroMap.lookup(Name)) {
2570 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2571 }
2572
2573 const char *BodyStart = StartToken.getLoc().getPointer();
2574 const char *BodyEnd = EndToken.getLoc().getPointer();
2575 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2576 getParser().MacroMap[Name] = new Macro(Name, Body);
2577 return false;
2578}
2579
2580/// ParseDirectiveEndMacro
2581/// ::= .endm
2582/// ::= .endmacro
2583bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2584 SMLoc DirectiveLoc) {
2585 if (getLexer().isNot(AsmToken::EndOfStatement))
2586 return TokError("unexpected token in '" + Directive + "' directive");
2587
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002588 // If we are inside a macro instantiation, terminate the current
2589 // instantiation.
2590 if (!getParser().ActiveMacros.empty()) {
2591 getParser().HandleMacroExit();
2592 return false;
2593 }
2594
2595 // Otherwise, this .endmacro is a stray entry in the file; well formed
2596 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002597 return TokError("unexpected '" + Directive + "' in file, "
2598 "no current macro definition");
2599}
2600
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002601bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002602 getParser().CheckForValidSection();
2603
2604 const MCExpr *Value;
2605
2606 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002607 return true;
2608
2609 if (getLexer().isNot(AsmToken::EndOfStatement))
2610 return TokError("unexpected token in directive");
2611
2612 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002613 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002614 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002615 getStreamer().EmitULEB128Value(Value);
2616
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002617 return false;
2618}
2619
2620
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002621/// \brief Create an MCAsmParser instance.
2622MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2623 MCContext &C, MCStreamer &Out,
2624 const MCAsmInfo &MAI) {
2625 return new AsmParser(T, SM, C, Out, MAI);
2626}