blob: 9d6c086c623f6fa021f37349234ab25eabcaa3a2 [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.
246 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
247 ".cfi_startproc");
248 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
249 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000250 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
251 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000252 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
253 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000254 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
255 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000256 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
257 ".cfi_def_cfa_register");
258 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
259 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000260 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
261 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000262 AddDirectiveHandler<
263 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
264 AddDirectiveHandler<
265 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000266 AddDirectiveHandler<
267 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
268 AddDirectiveHandler<
269 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000270 AddDirectiveHandler<
271 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000272
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000273 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000274 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
275 ".macros_on");
276 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
277 ".macros_off");
278 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
279 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
280 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000281
282 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
283 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000284 }
285
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000286 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
287
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000288 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
289 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
290 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000291 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000292 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
293 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000294 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000295 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000296 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000297 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
298 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000299 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000300 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000301 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
302 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000303 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000304
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000305 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000306 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
307 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000308
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000309 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000310};
311
312}
313
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000314namespace llvm {
315
316extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000317extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000318extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000319
320}
321
Chris Lattneraaec2052010-01-19 19:46:13 +0000322enum { DEFAULT_ADDRSPACE = 0 };
323
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000324AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
325 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000326 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000327 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000328 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000329 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000330
331 // Initialize the generic parser.
332 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000333
334 // Initialize the platform / file format parser.
335 //
336 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
337 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000338 if (_MAI.hasMicrosoftFastStdCallMangling()) {
339 PlatformParser = createCOFFAsmParser();
340 PlatformParser->Initialize(*this);
341 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000342 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000343 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000344 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000345 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000346 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000347 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000348}
349
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000350AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000351 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
352
353 // Destroy any macros.
354 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
355 ie = MacroMap.end(); it != ie; ++it)
356 delete it->getValue();
357
Daniel Dunbare4749702010-07-12 18:12:02 +0000358 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000359 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000360}
361
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000362void AsmParser::PrintMacroInstantiations() {
363 // Print the active macro instantiation stack.
364 for (std::vector<MacroInstantiation*>::const_reverse_iterator
365 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
366 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
367 "note");
368}
369
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000370void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000371 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000372 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000373}
374
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000375bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000376 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000377 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000378 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000379 return true;
380}
381
Sean Callananfd0b0282010-01-21 00:19:58 +0000382bool AsmParser::EnterIncludeFile(const std::string &Filename) {
383 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
384 if (NewBuf == -1)
385 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000386
Sean Callananfd0b0282010-01-21 00:19:58 +0000387 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000388
Sean Callananfd0b0282010-01-21 00:19:58 +0000389 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000390
Sean Callananfd0b0282010-01-21 00:19:58 +0000391 return false;
392}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000393
394void AsmParser::JumpToLoc(SMLoc Loc) {
395 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
396 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
397}
398
Sean Callananfd0b0282010-01-21 00:19:58 +0000399const AsmToken &AsmParser::Lex() {
400 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000401
Sean Callananfd0b0282010-01-21 00:19:58 +0000402 if (tok->is(AsmToken::Eof)) {
403 // If this is the end of an included file, pop the parent file off the
404 // include stack.
405 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
406 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000407 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000408 tok = &Lexer.Lex();
409 }
410 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000411
Sean Callananfd0b0282010-01-21 00:19:58 +0000412 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000413 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000414
Sean Callananfd0b0282010-01-21 00:19:58 +0000415 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000416}
417
Chris Lattner79180e22010-04-05 23:15:42 +0000418bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000419 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000420 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000421 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000422
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000423 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000424 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000425
426 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000427 AsmCond StartingCondState = TheCondState;
428
Chris Lattnerb717fb02009-07-02 21:53:43 +0000429 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000430 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000431 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000432
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000433 // We had an error, validate that one was emitted and recover by skipping to
434 // the next line.
435 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000436 EatToEndOfStatement();
437 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000438
439 if (TheCondState.TheCond != StartingCondState.TheCond ||
440 TheCondState.Ignore != StartingCondState.Ignore)
441 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000442
443 // Check to see there are no empty DwarfFile slots.
444 const std::vector<MCDwarfFile *> &MCDwarfFiles =
445 getContext().getMCDwarfFiles();
446 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000447 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000448 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000449 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000450
Chris Lattner79180e22010-04-05 23:15:42 +0000451 // Finalize the output stream if there are no errors and if the client wants
452 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000453 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000454 Out.Finish();
455
Chris Lattnerb717fb02009-07-02 21:53:43 +0000456 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000457}
458
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000459void AsmParser::CheckForValidSection() {
460 if (!getStreamer().getCurrentSection()) {
461 TokError("expected section directive before assembly directive");
462 Out.SwitchSection(Ctx.getMachOSection(
463 "__TEXT", "__text",
464 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
465 0, SectionKind::getText()));
466 }
467}
468
Chris Lattner2cf5f142009-06-22 01:29:09 +0000469/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
470void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000471 while (Lexer.isNot(AsmToken::EndOfStatement) &&
472 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000473 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000474
Chris Lattner2cf5f142009-06-22 01:29:09 +0000475 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000476 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000477 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000478}
479
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000480StringRef AsmParser::ParseStringToEndOfStatement() {
481 const char *Start = getTok().getLoc().getPointer();
482
483 while (Lexer.isNot(AsmToken::EndOfStatement) &&
484 Lexer.isNot(AsmToken::Eof))
485 Lex();
486
487 const char *End = getTok().getLoc().getPointer();
488 return StringRef(Start, End - Start);
489}
Chris Lattnerc4193832009-06-22 05:51:26 +0000490
Chris Lattner74ec1a32009-06-22 06:32:03 +0000491/// ParseParenExpr - Parse a paren expression and return it.
492/// NOTE: This assumes the leading '(' has already been consumed.
493///
494/// parenexpr ::= expr)
495///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000496bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000497 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000498 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000499 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000500 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000501 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000502 return false;
503}
Chris Lattnerc4193832009-06-22 05:51:26 +0000504
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000505/// ParseBracketExpr - Parse a bracket expression and return it.
506/// NOTE: This assumes the leading '[' has already been consumed.
507///
508/// bracketexpr ::= expr]
509///
510bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
511 if (ParseExpression(Res)) return true;
512 if (Lexer.isNot(AsmToken::RBrac))
513 return TokError("expected ']' in brackets expression");
514 EndLoc = Lexer.getLoc();
515 Lex();
516 return false;
517}
518
Chris Lattner74ec1a32009-06-22 06:32:03 +0000519/// ParsePrimaryExpr - Parse a primary expression and return it.
520/// primaryexpr ::= (parenexpr
521/// primaryexpr ::= symbol
522/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000523/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000524/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000525bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000526 switch (Lexer.getKind()) {
527 default:
528 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000529 // If we have an error assume that we've already handled it.
530 case AsmToken::Error:
531 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000532 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000533 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000534 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000535 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000536 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000537 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000538 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000539 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000540 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000541 EndLoc = Lexer.getLoc();
542
543 StringRef Identifier;
544 if (ParseIdentifier(Identifier))
545 return false;
546
Daniel Dunbarfffff912009-10-16 01:34:54 +0000547 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000548 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000549 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000550
551 // Lookup the symbol variant if used.
552 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000553 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000554 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000555 if (Variant == MCSymbolRefExpr::VK_Invalid) {
556 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000557 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000558 }
559 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000560
Daniel Dunbarfffff912009-10-16 01:34:54 +0000561 // If this is an absolute variable reference, substitute it now to preserve
562 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000563 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000564 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000565 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000566
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000567 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000568 return false;
569 }
570
571 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000572 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000573 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000574 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000575 case AsmToken::Integer: {
576 SMLoc Loc = getTok().getLoc();
577 int64_t IntVal = getTok().getIntVal();
578 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000579 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000580 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000581 // Look for 'b' or 'f' following an Integer as a directional label
582 if (Lexer.getKind() == AsmToken::Identifier) {
583 StringRef IDVal = getTok().getString();
584 if (IDVal == "f" || IDVal == "b"){
585 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
586 IDVal == "f" ? 1 : 0);
587 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
588 getContext());
589 if(IDVal == "b" && Sym->isUndefined())
590 return Error(Loc, "invalid reference to undefined symbol");
591 EndLoc = Lexer.getLoc();
592 Lex(); // Eat identifier.
593 }
594 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000595 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000596 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000597 case AsmToken::Real: {
598 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000599 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000600 Res = MCConstantExpr::Create(IntVal, getContext());
601 Lex(); // Eat token.
602 return false;
603 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000604 case AsmToken::Dot: {
605 // This is a '.' reference, which references the current PC. Emit a
606 // temporary label to the streamer and refer to it.
607 MCSymbol *Sym = Ctx.CreateTempSymbol();
608 Out.EmitLabel(Sym);
609 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
610 EndLoc = Lexer.getLoc();
611 Lex(); // Eat identifier.
612 return false;
613 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000614 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000615 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000616 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000617 case AsmToken::LBrac:
618 if (!PlatformParser->HasBracketExpressions())
619 return TokError("brackets expression not supported on this target");
620 Lex(); // Eat the '['.
621 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000622 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000623 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000624 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000625 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000626 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000627 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000628 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000629 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000630 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000631 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000632 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000633 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000634 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000635 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000636 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000637 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000638 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000639 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000640 }
641}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000642
Chris Lattnerb4307b32010-01-15 19:28:38 +0000643bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000644 SMLoc EndLoc;
645 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000646}
647
Daniel Dunbarcceba832010-09-17 02:47:07 +0000648const MCExpr *
649AsmParser::ApplyModifierToExpr(const MCExpr *E,
650 MCSymbolRefExpr::VariantKind Variant) {
651 // Recurse over the given expression, rebuilding it to apply the given variant
652 // if there is exactly one symbol.
653 switch (E->getKind()) {
654 case MCExpr::Target:
655 case MCExpr::Constant:
656 return 0;
657
658 case MCExpr::SymbolRef: {
659 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
660
661 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
662 TokError("invalid variant on expression '" +
663 getTok().getIdentifier() + "' (already modified)");
664 return E;
665 }
666
667 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
668 }
669
670 case MCExpr::Unary: {
671 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
672 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
673 if (!Sub)
674 return 0;
675 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
676 }
677
678 case MCExpr::Binary: {
679 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
680 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
681 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
682
683 if (!LHS && !RHS)
684 return 0;
685
686 if (!LHS) LHS = BE->getLHS();
687 if (!RHS) RHS = BE->getRHS();
688
689 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
690 }
691 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000692
693 assert(0 && "Invalid expression kind!");
694 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000695}
696
Chris Lattner74ec1a32009-06-22 06:32:03 +0000697/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000698///
Chris Lattner74ec1a32009-06-22 06:32:03 +0000699/// expr ::= expr +,- expr -> lowest.
700/// expr ::= expr |,^,&,! expr -> middle.
701/// expr ::= expr *,/,%,<<,>> expr -> highest.
702/// expr ::= primaryexpr
703///
Chris Lattner54482b42010-01-15 19:39:23 +0000704bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000705 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000706 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000707 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
708 return true;
709
Daniel Dunbarcceba832010-09-17 02:47:07 +0000710 // As a special case, we support 'a op b @ modifier' by rewriting the
711 // expression to include the modifier. This is inefficient, but in general we
712 // expect users to use 'a@modifier op b'.
713 if (Lexer.getKind() == AsmToken::At) {
714 Lex();
715
716 if (Lexer.isNot(AsmToken::Identifier))
717 return TokError("unexpected symbol modifier following '@'");
718
719 MCSymbolRefExpr::VariantKind Variant =
720 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
721 if (Variant == MCSymbolRefExpr::VK_Invalid)
722 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
723
724 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
725 if (!ModifiedRes) {
726 return TokError("invalid modifier '" + getTok().getIdentifier() +
727 "' (no symbols present)");
728 return true;
729 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000730
Daniel Dunbarcceba832010-09-17 02:47:07 +0000731 Res = ModifiedRes;
732 Lex();
733 }
734
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000735 // Try to constant fold it up front, if possible.
736 int64_t Value;
737 if (Res->EvaluateAsAbsolute(Value))
738 Res = MCConstantExpr::Create(Value, getContext());
739
740 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000741}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000742
Chris Lattnerb4307b32010-01-15 19:28:38 +0000743bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000744 Res = 0;
745 return ParseParenExpr(Res, EndLoc) ||
746 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000747}
748
Daniel Dunbar475839e2009-06-29 20:37:27 +0000749bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000750 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000751
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000752 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000753 if (ParseExpression(Expr))
754 return true;
755
Daniel Dunbare00b0112009-10-16 01:57:52 +0000756 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000757 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000758
759 return false;
760}
761
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000762static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000763 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000764 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000765 default:
766 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000767
Daniel Dunbarcceba832010-09-17 02:47:07 +0000768 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000769 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000770 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000771 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000772 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000773 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000774 return 1;
775
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000776
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000777 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000778 //
779 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000780 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000781 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000782 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000783 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000784 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000785 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000786 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000787 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000788 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000789
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000790 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000791 case AsmToken::EqualEqual:
792 Kind = MCBinaryExpr::EQ;
793 return 3;
794 case AsmToken::ExclaimEqual:
795 case AsmToken::LessGreater:
796 Kind = MCBinaryExpr::NE;
797 return 3;
798 case AsmToken::Less:
799 Kind = MCBinaryExpr::LT;
800 return 3;
801 case AsmToken::LessEqual:
802 Kind = MCBinaryExpr::LTE;
803 return 3;
804 case AsmToken::Greater:
805 Kind = MCBinaryExpr::GT;
806 return 3;
807 case AsmToken::GreaterEqual:
808 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000809 return 3;
810
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000811 // High Intermediate Precedence: +, -
812 case AsmToken::Plus:
813 Kind = MCBinaryExpr::Add;
814 return 4;
815 case AsmToken::Minus:
816 Kind = MCBinaryExpr::Sub;
817 return 4;
818
Daniel Dunbar475839e2009-06-29 20:37:27 +0000819 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000820 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000821 Kind = MCBinaryExpr::Mul;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000822 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000823 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000824 Kind = MCBinaryExpr::Div;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000825 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000826 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000827 Kind = MCBinaryExpr::Mod;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000828 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000829 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000830 Kind = MCBinaryExpr::Shl;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000831 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000832 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000833 Kind = MCBinaryExpr::Shr;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000834 return 5;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000835 }
836}
837
838
839/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
840/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000841bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
842 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000843 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000844 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000845 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000846
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000847 // If the next token is lower precedence than we are allowed to eat, return
848 // successfully with what we ate already.
849 if (TokPrec < Precedence)
850 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000851
Sean Callanan79ed1a82010-01-19 20:22:31 +0000852 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000853
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000854 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000855 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000856 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000857
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000858 // If BinOp binds less tightly with RHS than the operator after RHS, let
859 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000860 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000861 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000862 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000863 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000864 }
865
Daniel Dunbar475839e2009-06-29 20:37:27 +0000866 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000867 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000868 }
869}
870
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000871
872
873
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000874/// ParseStatement:
875/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000876/// ::= Label* Directive ...Operands... EndOfStatement
877/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000878bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000879 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000880 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000881 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000882 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000883 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000884
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000885 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000886 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000887 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000888 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000889 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000890 // A full line comment is a '#' as the first token.
891 if (Lexer.is(AsmToken::Hash)) {
892 EatToEndOfStatement();
893 return false;
894 }
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000895
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000896 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000897 if (Lexer.is(AsmToken::Integer)) {
898 LocalLabelVal = getTok().getIntVal();
899 if (LocalLabelVal < 0) {
900 if (!TheCondState.Ignore)
901 return TokError("unexpected token at start of statement");
902 IDVal = "";
903 }
904 else {
905 IDVal = getTok().getString();
906 Lex(); // Consume the integer token to be used as an identifier token.
907 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000908 if (!TheCondState.Ignore)
909 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000910 }
911 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000912
913 } else if (Lexer.is(AsmToken::Dot)) {
914 // Treat '.' as a valid identifier in this context.
915 Lex();
916 IDVal = ".";
917
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000918 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000919 if (!TheCondState.Ignore)
920 return TokError("unexpected token at start of statement");
921 IDVal = "";
922 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000923
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000924
Chris Lattner7834fac2010-04-17 18:14:27 +0000925 // Handle conditional assembly here before checking for skipping. We
926 // have to do this so that .endif isn't skipped in a ".if 0" block for
927 // example.
928 if (IDVal == ".if")
929 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000930 if (IDVal == ".ifdef")
931 return ParseDirectiveIfdef(IDLoc, true);
932 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
933 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +0000934 if (IDVal == ".elseif")
935 return ParseDirectiveElseIf(IDLoc);
936 if (IDVal == ".else")
937 return ParseDirectiveElse(IDLoc);
938 if (IDVal == ".endif")
939 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000940
Chris Lattner7834fac2010-04-17 18:14:27 +0000941 // If we are in a ".if 0" block, ignore this statement.
942 if (TheCondState.Ignore) {
943 EatToEndOfStatement();
944 return false;
945 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000946
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000947 // FIXME: Recurse on local labels?
948
949 // See what kind of statement we have.
950 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000951 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000952 CheckForValidSection();
953
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000954 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000955 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000956
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000957 // Diagnose attempt to use '.' as a label.
958 if (IDVal == ".")
959 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
960
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000961 // Diagnose attempt to use a variable as a label.
962 //
963 // FIXME: Diagnostics. Note the location of the definition as a label.
964 // FIXME: This doesn't diagnose assignment to a symbol which has been
965 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000966 MCSymbol *Sym;
967 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000968 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000969 else
970 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000971 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000972 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000973
Daniel Dunbar959fd882009-08-26 22:13:22 +0000974 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000975 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000976
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000977 // Consume any end of statement token, if present, to avoid spurious
978 // AddBlankLine calls().
979 if (Lexer.is(AsmToken::EndOfStatement)) {
980 Lex();
981 if (Lexer.is(AsmToken::Eof))
982 return false;
983 }
984
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000985 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000986 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000987
Daniel Dunbar3f872332009-07-28 16:08:33 +0000988 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000989 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000990 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000991
Nico Weber4c4c7322011-01-28 03:04:41 +0000992 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000993
994 default: // Normal instruction or directive.
995 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000996 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000997
998 // If macros are enabled, check to see if this is a macro instantiation.
999 if (MacrosEnabled)
1000 if (const Macro *M = MacroMap.lookup(IDVal))
1001 return HandleMacroEntry(IDVal, IDLoc, M);
1002
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001003 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001004 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001005 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001006 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001007 return ParseDirectiveSet(IDVal, true);
1008 if (IDVal == ".equiv")
1009 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001010
Daniel Dunbara0d14262009-06-24 23:30:00 +00001011 // Data directives
1012
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001013 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001014 return ParseDirectiveAscii(IDVal, false);
1015 if (IDVal == ".asciz" || IDVal == ".string")
1016 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001017
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001018 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001019 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001020 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001021 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001022 if (IDVal == ".value")
1023 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001024 if (IDVal == ".2byte")
1025 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001026 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001027 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001028 if (IDVal == ".int")
1029 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001030 if (IDVal == ".4byte")
1031 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001032 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001033 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001034 if (IDVal == ".8byte")
1035 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001036 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001037 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1038 if (IDVal == ".double")
1039 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001040
Eli Friedman5d68ec22010-07-19 04:17:25 +00001041 if (IDVal == ".align") {
1042 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1043 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1044 }
1045 if (IDVal == ".align32") {
1046 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1047 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1048 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001049 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001050 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001051 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001052 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001053 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001054 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001055 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001056 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001057 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001058 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001059 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001060 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1061
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001062 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001063 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001064
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001065 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001066 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001067 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001068 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001069 if (IDVal == ".zero")
1070 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001071
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001072 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001073
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001074 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001075 return ParseDirectiveSymbolAttribute(MCSA_Global);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +00001076 // ELF only? Should it be here?
1077 if (IDVal == ".local")
1078 return ParseDirectiveSymbolAttribute(MCSA_Local);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001079 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001080 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001081 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001082 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001083 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001084 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001085 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001086 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001087 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001088 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001089 if (IDVal == ".symbol_resolver")
1090 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001091 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001092 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001093 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001094 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001095 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001096 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001097 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001098 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001099 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001100 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001101 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001102 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001103 if (IDVal == ".weak_def_can_be_hidden")
1104 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001105
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001106 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001107 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001108 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001109 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001110
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001111 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001112 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001113 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001114 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001115
Roman Divackybb6d14f2011-01-31 21:19:43 +00001116 if (IDVal == ".code16" || IDVal == ".code32" || IDVal == ".code64")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001117 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001118
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001119 // Look up the handler in the handler table.
1120 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1121 DirectiveMap.lookup(IDVal);
1122 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001123 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001124
Kevin Enderby9c656452009-09-10 20:51:44 +00001125 // Target hook for parsing target specific directives.
1126 if (!getTargetParser().ParseDirective(ID))
1127 return false;
1128
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001129 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001130 EatToEndOfStatement();
1131 return false;
1132 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001133
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001134 CheckForValidSection();
1135
Chris Lattnera7f13542010-05-19 23:34:33 +00001136 // Canonicalize the opcode to lower case.
1137 SmallString<128> Opcode;
1138 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1139 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001140
Chris Lattner98986712010-01-14 22:21:20 +00001141 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001142 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001143 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001144
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001145 // Dump the parsed representation, if requested.
1146 if (getShowParsedOperands()) {
1147 SmallString<256> Str;
1148 raw_svector_ostream OS(Str);
1149 OS << "parsed instruction: [";
1150 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1151 if (i != 0)
1152 OS << ", ";
1153 ParsedOperands[i]->dump(OS);
1154 }
1155 OS << "]";
1156
1157 PrintMessage(IDLoc, OS.str(), "note");
1158 }
1159
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001160 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001161 if (!HadError)
1162 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1163 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001164
Chris Lattner98986712010-01-14 22:21:20 +00001165 // Free any parsed operands.
1166 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1167 delete ParsedOperands[i];
1168
Chris Lattnercbf8a982010-09-11 16:18:25 +00001169 // Don't skip the rest of the line, the instruction parser is responsible for
1170 // that.
1171 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001172}
Chris Lattner9a023f72009-06-24 04:43:34 +00001173
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001174MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1175 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001176 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
1177{
1178 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1179 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001180 SmallString<256> Buf;
1181 raw_svector_ostream OS(Buf);
1182
1183 StringRef Body = M->Body;
1184 while (!Body.empty()) {
1185 // Scan for the next substitution.
1186 std::size_t End = Body.size(), Pos = 0;
1187 for (; Pos != End; ++Pos) {
1188 // Check for a substitution or escape.
1189 if (Body[Pos] != '$' || Pos + 1 == End)
1190 continue;
1191
1192 char Next = Body[Pos + 1];
1193 if (Next == '$' || Next == 'n' || isdigit(Next))
1194 break;
1195 }
1196
1197 // Add the prefix.
1198 OS << Body.slice(0, Pos);
1199
1200 // Check if we reached the end.
1201 if (Pos == End)
1202 break;
1203
1204 switch (Body[Pos+1]) {
1205 // $$ => $
1206 case '$':
1207 OS << '$';
1208 break;
1209
1210 // $n => number of arguments
1211 case 'n':
1212 OS << A.size();
1213 break;
1214
1215 // $[0-9] => argument
1216 default: {
1217 // Missing arguments are ignored.
1218 unsigned Index = Body[Pos+1] - '0';
1219 if (Index >= A.size())
1220 break;
1221
1222 // Otherwise substitute with the token values, with spaces eliminated.
1223 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1224 ie = A[Index].end(); it != ie; ++it)
1225 OS << it->getString();
1226 break;
1227 }
1228 }
1229
1230 // Update the scan point.
1231 Body = Body.substr(Pos + 2);
1232 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001233
1234 // We include the .endmacro in the buffer as our queue to exit the macro
1235 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001236 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001237
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001238 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001239}
1240
1241bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1242 const Macro *M) {
1243 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1244 // this, although we should protect against infinite loops.
1245 if (ActiveMacros.size() == 20)
1246 return TokError("macros cannot be nested more than 20 levels deep");
1247
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001248 // Parse the macro instantiation arguments.
1249 std::vector<std::vector<AsmToken> > MacroArguments;
1250 MacroArguments.push_back(std::vector<AsmToken>());
1251 unsigned ParenLevel = 0;
1252 for (;;) {
1253 if (Lexer.is(AsmToken::Eof))
1254 return TokError("unexpected token in macro instantiation");
1255 if (Lexer.is(AsmToken::EndOfStatement))
1256 break;
1257
1258 // If we aren't inside parentheses and this is a comma, start a new token
1259 // list.
1260 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1261 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001262 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001263 // Adjust the current parentheses level.
1264 if (Lexer.is(AsmToken::LParen))
1265 ++ParenLevel;
1266 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1267 --ParenLevel;
1268
1269 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001270 MacroArguments.back().push_back(getTok());
1271 }
1272 Lex();
1273 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001274
1275 // Create the macro instantiation object and add to the current macro
1276 // instantiation stack.
1277 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001278 getTok().getLoc(),
1279 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001280 ActiveMacros.push_back(MI);
1281
1282 // Jump to the macro instantiation and prime the lexer.
1283 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1284 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1285 Lex();
1286
1287 return false;
1288}
1289
1290void AsmParser::HandleMacroExit() {
1291 // Jump to the EndOfStatement we should return to, and consume it.
1292 JumpToLoc(ActiveMacros.back()->ExitLoc);
1293 Lex();
1294
1295 // Pop the instantiation entry.
1296 delete ActiveMacros.back();
1297 ActiveMacros.pop_back();
1298}
1299
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001300static void MarkUsed(const MCExpr *Value) {
1301 switch (Value->getKind()) {
1302 case MCExpr::Binary:
1303 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1304 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1305 break;
1306 case MCExpr::Target:
1307 case MCExpr::Constant:
1308 break;
1309 case MCExpr::SymbolRef: {
1310 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1311 break;
1312 }
1313 case MCExpr::Unary:
1314 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1315 break;
1316 }
1317}
1318
Nico Weber4c4c7322011-01-28 03:04:41 +00001319bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001320 // FIXME: Use better location, we should use proper tokens.
1321 SMLoc EqualLoc = Lexer.getLoc();
1322
Daniel Dunbar821e3332009-08-31 08:09:28 +00001323 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001324 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001325 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001326
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001327 MarkUsed(Value);
1328
Daniel Dunbar3f872332009-07-28 16:08:33 +00001329 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001330 return TokError("unexpected token in assignment");
1331
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001332 // Error on assignment to '.'.
1333 if (Name == ".") {
1334 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1335 "(use '.space' or '.org').)"));
1336 }
1337
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001338 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001339 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001340
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001341 // Validate that the LHS is allowed to be a variable (either it has not been
1342 // used as a symbol, or it is an absolute symbol).
1343 MCSymbol *Sym = getContext().LookupSymbol(Name);
1344 if (Sym) {
1345 // Diagnose assignment to a label.
1346 //
1347 // FIXME: Diagnostics. Note the location of the definition as a label.
1348 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001349 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001350 ; // Allow redefinitions of undefined symbols only used in directives.
Nico Weber4c4c7322011-01-28 03:04:41 +00001351 else if (!Sym->isUndefined() && (!Sym->isAbsolute() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001352 return Error(EqualLoc, "redefinition of '" + Name + "'");
1353 else if (!Sym->isVariable())
1354 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001355 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001356 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1357 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001358
1359 // Don't count these checks as uses.
1360 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001361 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001362 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001363
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001364 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001365
1366 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001367 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001368
1369 return false;
1370}
1371
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001372/// ParseIdentifier:
1373/// ::= identifier
1374/// ::= string
1375bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001376 // The assembler has relaxed rules for accepting identifiers, in particular we
1377 // allow things like '.globl $foo', which would normally be separate
1378 // tokens. At this level, we have already lexed so we cannot (currently)
1379 // handle this as a context dependent token, instead we detect adjacent tokens
1380 // and return the combined identifier.
1381 if (Lexer.is(AsmToken::Dollar)) {
1382 SMLoc DollarLoc = getLexer().getLoc();
1383
1384 // Consume the dollar sign, and check for a following identifier.
1385 Lex();
1386 if (Lexer.isNot(AsmToken::Identifier))
1387 return true;
1388
1389 // We have a '$' followed by an identifier, make sure they are adjacent.
1390 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1391 return true;
1392
1393 // Construct the joined identifier and consume the token.
1394 Res = StringRef(DollarLoc.getPointer(),
1395 getTok().getIdentifier().size() + 1);
1396 Lex();
1397 return false;
1398 }
1399
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001400 if (Lexer.isNot(AsmToken::Identifier) &&
1401 Lexer.isNot(AsmToken::String))
1402 return true;
1403
Sean Callanan18b83232010-01-19 21:44:56 +00001404 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001405
Sean Callanan79ed1a82010-01-19 20:22:31 +00001406 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001407
1408 return false;
1409}
1410
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001411/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001412/// ::= .equ identifier ',' expression
1413/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001414/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001415bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001416 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001417
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001418 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001419 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001420
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001421 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001422 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001423 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001424
Nico Weber4c4c7322011-01-28 03:04:41 +00001425 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001426}
1427
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001428bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001429 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001430
1431 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001432 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001433 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1434 if (Str[i] != '\\') {
1435 Data += Str[i];
1436 continue;
1437 }
1438
1439 // Recognize escaped characters. Note that this escape semantics currently
1440 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1441 ++i;
1442 if (i == e)
1443 return TokError("unexpected backslash at end of string");
1444
1445 // Recognize octal sequences.
1446 if ((unsigned) (Str[i] - '0') <= 7) {
1447 // Consume up to three octal characters.
1448 unsigned Value = Str[i] - '0';
1449
1450 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1451 ++i;
1452 Value = Value * 8 + (Str[i] - '0');
1453
1454 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1455 ++i;
1456 Value = Value * 8 + (Str[i] - '0');
1457 }
1458 }
1459
1460 if (Value > 255)
1461 return TokError("invalid octal escape sequence (out of range)");
1462
1463 Data += (unsigned char) Value;
1464 continue;
1465 }
1466
1467 // Otherwise recognize individual escapes.
1468 switch (Str[i]) {
1469 default:
1470 // Just reject invalid escape sequences for now.
1471 return TokError("invalid escape sequence (unrecognized character)");
1472
1473 case 'b': Data += '\b'; break;
1474 case 'f': Data += '\f'; break;
1475 case 'n': Data += '\n'; break;
1476 case 'r': Data += '\r'; break;
1477 case 't': Data += '\t'; break;
1478 case '"': Data += '"'; break;
1479 case '\\': Data += '\\'; break;
1480 }
1481 }
1482
1483 return false;
1484}
1485
Daniel Dunbara0d14262009-06-24 23:30:00 +00001486/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001487/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1488bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001489 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001490 CheckForValidSection();
1491
Daniel Dunbara0d14262009-06-24 23:30:00 +00001492 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001493 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001494 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001495
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001496 std::string Data;
1497 if (ParseEscapedString(Data))
1498 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001499
1500 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001501 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001502 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1503
Sean Callanan79ed1a82010-01-19 20:22:31 +00001504 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001505
1506 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001507 break;
1508
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001509 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001510 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001511 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001512 }
1513 }
1514
Sean Callanan79ed1a82010-01-19 20:22:31 +00001515 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001516 return false;
1517}
1518
1519/// ParseDirectiveValue
1520/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1521bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001522 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001523 CheckForValidSection();
1524
Daniel Dunbara0d14262009-06-24 23:30:00 +00001525 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001526 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001527 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001528 return true;
1529
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001530 // Special case constant expressions to match code generator.
1531 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001532 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001533 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001534 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001535
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001536 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001537 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001538
Daniel Dunbara0d14262009-06-24 23:30:00 +00001539 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001540 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001541 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001542 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001543 }
1544 }
1545
Sean Callanan79ed1a82010-01-19 20:22:31 +00001546 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001547 return false;
1548}
1549
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001550/// ParseDirectiveRealValue
1551/// ::= (.single | .double) [ expression (, expression)* ]
1552bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1553 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1554 CheckForValidSection();
1555
1556 for (;;) {
1557 // We don't truly support arithmetic on floating point expressions, so we
1558 // have to manually parse unary prefixes.
1559 bool IsNeg = false;
1560 if (getLexer().is(AsmToken::Minus)) {
1561 Lex();
1562 IsNeg = true;
1563 } else if (getLexer().is(AsmToken::Plus))
1564 Lex();
1565
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001566 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001567 getLexer().isNot(AsmToken::Real) &&
1568 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001569 return TokError("unexpected token in directive");
1570
1571 // Convert to an APFloat.
1572 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001573 StringRef IDVal = getTok().getString();
1574 if (getLexer().is(AsmToken::Identifier)) {
1575 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1576 Value = APFloat::getInf(Semantics);
1577 else if (!IDVal.compare_lower("nan"))
1578 Value = APFloat::getNaN(Semantics, false, ~0);
1579 else
1580 return TokError("invalid floating point literal");
1581 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001582 APFloat::opInvalidOp)
1583 return TokError("invalid floating point literal");
1584 if (IsNeg)
1585 Value.changeSign();
1586
1587 // Consume the numeric token.
1588 Lex();
1589
1590 // Emit the value as an integer.
1591 APInt AsInt = Value.bitcastToAPInt();
1592 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1593 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1594
1595 if (getLexer().is(AsmToken::EndOfStatement))
1596 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001597
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001598 if (getLexer().isNot(AsmToken::Comma))
1599 return TokError("unexpected token in directive");
1600 Lex();
1601 }
1602 }
1603
1604 Lex();
1605 return false;
1606}
1607
Daniel Dunbara0d14262009-06-24 23:30:00 +00001608/// ParseDirectiveSpace
1609/// ::= .space expression [ , expression ]
1610bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001611 CheckForValidSection();
1612
Daniel Dunbara0d14262009-06-24 23:30:00 +00001613 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001614 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001615 return true;
1616
1617 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001618 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1619 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001620 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001621 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001622
Daniel Dunbar475839e2009-06-29 20:37:27 +00001623 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001624 return true;
1625
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001626 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001627 return TokError("unexpected token in '.space' directive");
1628 }
1629
Sean Callanan79ed1a82010-01-19 20:22:31 +00001630 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001631
1632 if (NumBytes <= 0)
1633 return TokError("invalid number of bytes in '.space' directive");
1634
1635 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001636 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001637
1638 return false;
1639}
1640
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001641/// ParseDirectiveZero
1642/// ::= .zero expression
1643bool AsmParser::ParseDirectiveZero() {
1644 CheckForValidSection();
1645
1646 int64_t NumBytes;
1647 if (ParseAbsoluteExpression(NumBytes))
1648 return true;
1649
Rafael Espindolae452b172010-10-05 19:42:57 +00001650 int64_t Val = 0;
1651 if (getLexer().is(AsmToken::Comma)) {
1652 Lex();
1653 if (ParseAbsoluteExpression(Val))
1654 return true;
1655 }
1656
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001657 if (getLexer().isNot(AsmToken::EndOfStatement))
1658 return TokError("unexpected token in '.zero' directive");
1659
1660 Lex();
1661
Rafael Espindolae452b172010-10-05 19:42:57 +00001662 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001663
1664 return false;
1665}
1666
Daniel Dunbara0d14262009-06-24 23:30:00 +00001667/// ParseDirectiveFill
1668/// ::= .fill expression , expression , expression
1669bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001670 CheckForValidSection();
1671
Daniel Dunbara0d14262009-06-24 23:30:00 +00001672 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001673 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001674 return true;
1675
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001676 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001677 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001678 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001679
Daniel Dunbara0d14262009-06-24 23:30:00 +00001680 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001681 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001682 return true;
1683
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001684 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001685 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001686 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001687
Daniel Dunbara0d14262009-06-24 23:30:00 +00001688 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001689 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001690 return true;
1691
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001692 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001693 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001694
Sean Callanan79ed1a82010-01-19 20:22:31 +00001695 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001696
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001697 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1698 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001699
1700 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001701 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001702
1703 return false;
1704}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001705
1706/// ParseDirectiveOrg
1707/// ::= .org expression [ , expression ]
1708bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001709 CheckForValidSection();
1710
Daniel Dunbar821e3332009-08-31 08:09:28 +00001711 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001712 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001713 return true;
1714
1715 // Parse optional fill expression.
1716 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001717 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1718 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001719 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001720 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001721
Daniel Dunbar475839e2009-06-29 20:37:27 +00001722 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001723 return true;
1724
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001725 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001726 return TokError("unexpected token in '.org' directive");
1727 }
1728
Sean Callanan79ed1a82010-01-19 20:22:31 +00001729 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001730
1731 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1732 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001733 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001734
1735 return false;
1736}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001737
1738/// ParseDirectiveAlign
1739/// ::= {.align, ...} expression [ , expression [ , expression ]]
1740bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001741 CheckForValidSection();
1742
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001743 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001744 int64_t Alignment;
1745 if (ParseAbsoluteExpression(Alignment))
1746 return true;
1747
1748 SMLoc MaxBytesLoc;
1749 bool HasFillExpr = false;
1750 int64_t FillExpr = 0;
1751 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001752 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1753 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001754 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001755 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001756
1757 // The fill expression can be omitted while specifying a maximum number of
1758 // alignment bytes, e.g:
1759 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001760 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001761 HasFillExpr = true;
1762 if (ParseAbsoluteExpression(FillExpr))
1763 return true;
1764 }
1765
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001766 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1767 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001768 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001769 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001770
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001771 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001772 if (ParseAbsoluteExpression(MaxBytesToFill))
1773 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001774
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001775 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001776 return TokError("unexpected token in directive");
1777 }
1778 }
1779
Sean Callanan79ed1a82010-01-19 20:22:31 +00001780 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001781
Daniel Dunbar648ac512010-05-17 21:54:30 +00001782 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001783 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001784
1785 // Compute alignment in bytes.
1786 if (IsPow2) {
1787 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001788 if (Alignment >= 32) {
1789 Error(AlignmentLoc, "invalid alignment value");
1790 Alignment = 31;
1791 }
1792
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001793 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001794 }
1795
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001796 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001797 if (MaxBytesLoc.isValid()) {
1798 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001799 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1800 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001801 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001802 }
1803
1804 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001805 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1806 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001807 MaxBytesToFill = 0;
1808 }
1809 }
1810
Daniel Dunbar648ac512010-05-17 21:54:30 +00001811 // Check whether we should use optimal code alignment for this .align
1812 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001813 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001814 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1815 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001816 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001817 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001818 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001819 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1820 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001821 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001822
1823 return false;
1824}
1825
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001826/// ParseDirectiveSymbolAttribute
1827/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001828bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001829 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001830 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001831 StringRef Name;
1832
1833 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001834 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001835
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001836 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001837
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001838 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001839
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001840 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001841 break;
1842
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001843 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001844 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001845 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001846 }
1847 }
1848
Sean Callanan79ed1a82010-01-19 20:22:31 +00001849 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001850 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001851}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001852
1853/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001854/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1855bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001856 CheckForValidSection();
1857
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001858 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001859 StringRef Name;
1860 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001861 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001862
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001863 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001864 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001865
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001866 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001867 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001868 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001869
1870 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001871 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001872 if (ParseAbsoluteExpression(Size))
1873 return true;
1874
1875 int64_t Pow2Alignment = 0;
1876 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001877 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001878 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001879 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001880 if (ParseAbsoluteExpression(Pow2Alignment))
1881 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001882
Chris Lattner258281d2010-01-19 06:22:22 +00001883 // If this target takes alignments in bytes (not log) validate and convert.
1884 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1885 if (!isPowerOf2_64(Pow2Alignment))
1886 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1887 Pow2Alignment = Log2_64(Pow2Alignment);
1888 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001889 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001890
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001891 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001892 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001893
Sean Callanan79ed1a82010-01-19 20:22:31 +00001894 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001895
Chris Lattner1fc3d752009-07-09 17:25:12 +00001896 // NOTE: a size of zero for a .comm should create a undefined symbol
1897 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001898 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001899 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1900 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001901
Eric Christopherc260a3e2010-05-14 01:38:54 +00001902 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001903 // may internally end up wanting an alignment in bytes.
1904 // FIXME: Diagnose overflow.
1905 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001906 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1907 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001908
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001909 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001910 return Error(IDLoc, "invalid symbol redefinition");
1911
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001912 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001913 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001914 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001915 getStreamer().EmitZerofill(Ctx.getMachOSection(
1916 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1917 0, SectionKind::getBSS()),
1918 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001919 return false;
1920 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001921
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001922 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001923 return false;
1924}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001925
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001926/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001927/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001928bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001929 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001930 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001931
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001932 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001933 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001934 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001935
Sean Callanan79ed1a82010-01-19 20:22:31 +00001936 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001937
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001938 if (Str.empty())
1939 Error(Loc, ".abort detected. Assembly stopping.");
1940 else
1941 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001942 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001943
1944 return false;
1945}
Kevin Enderby71148242009-07-14 21:35:03 +00001946
Kevin Enderby1f049b22009-07-14 23:21:55 +00001947/// ParseDirectiveInclude
1948/// ::= .include "filename"
1949bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001950 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001951 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001952
Sean Callanan18b83232010-01-19 21:44:56 +00001953 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001954 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001955 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001956
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001957 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001958 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001959
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001960 // Strip the quotes.
1961 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001962
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001963 // Attempt to switch the lexer to the included file before consuming the end
1964 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001965 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001966 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001967 return true;
1968 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001969
1970 return false;
1971}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001972
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001973/// ParseDirectiveIf
1974/// ::= .if expression
1975bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001976 TheCondStack.push_back(TheCondState);
1977 TheCondState.TheCond = AsmCond::IfCond;
1978 if(TheCondState.Ignore) {
1979 EatToEndOfStatement();
1980 }
1981 else {
1982 int64_t ExprValue;
1983 if (ParseAbsoluteExpression(ExprValue))
1984 return true;
1985
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001986 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001987 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001988
Sean Callanan79ed1a82010-01-19 20:22:31 +00001989 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001990
1991 TheCondState.CondMet = ExprValue;
1992 TheCondState.Ignore = !TheCondState.CondMet;
1993 }
1994
1995 return false;
1996}
1997
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00001998bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
1999 StringRef Name;
2000 TheCondStack.push_back(TheCondState);
2001 TheCondState.TheCond = AsmCond::IfCond;
2002
2003 if (TheCondState.Ignore) {
2004 EatToEndOfStatement();
2005 } else {
2006 if (ParseIdentifier(Name))
2007 return TokError("expected identifier after '.ifdef'");
2008
2009 Lex();
2010
2011 MCSymbol *Sym = getContext().LookupSymbol(Name);
2012
2013 if (expect_defined)
2014 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2015 else
2016 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2017 TheCondState.Ignore = !TheCondState.CondMet;
2018 }
2019
2020 return false;
2021}
2022
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002023/// ParseDirectiveElseIf
2024/// ::= .elseif expression
2025bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2026 if (TheCondState.TheCond != AsmCond::IfCond &&
2027 TheCondState.TheCond != AsmCond::ElseIfCond)
2028 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2029 " an .elseif");
2030 TheCondState.TheCond = AsmCond::ElseIfCond;
2031
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002032 bool LastIgnoreState = false;
2033 if (!TheCondStack.empty())
2034 LastIgnoreState = TheCondStack.back().Ignore;
2035 if (LastIgnoreState || TheCondState.CondMet) {
2036 TheCondState.Ignore = true;
2037 EatToEndOfStatement();
2038 }
2039 else {
2040 int64_t ExprValue;
2041 if (ParseAbsoluteExpression(ExprValue))
2042 return true;
2043
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002044 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002045 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002046
Sean Callanan79ed1a82010-01-19 20:22:31 +00002047 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002048 TheCondState.CondMet = ExprValue;
2049 TheCondState.Ignore = !TheCondState.CondMet;
2050 }
2051
2052 return false;
2053}
2054
2055/// ParseDirectiveElse
2056/// ::= .else
2057bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002058 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002059 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002060
Sean Callanan79ed1a82010-01-19 20:22:31 +00002061 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002062
2063 if (TheCondState.TheCond != AsmCond::IfCond &&
2064 TheCondState.TheCond != AsmCond::ElseIfCond)
2065 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2066 ".elseif");
2067 TheCondState.TheCond = AsmCond::ElseCond;
2068 bool LastIgnoreState = false;
2069 if (!TheCondStack.empty())
2070 LastIgnoreState = TheCondStack.back().Ignore;
2071 if (LastIgnoreState || TheCondState.CondMet)
2072 TheCondState.Ignore = true;
2073 else
2074 TheCondState.Ignore = false;
2075
2076 return false;
2077}
2078
2079/// ParseDirectiveEndIf
2080/// ::= .endif
2081bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002082 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002083 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002084
Sean Callanan79ed1a82010-01-19 20:22:31 +00002085 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002086
2087 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2088 TheCondStack.empty())
2089 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2090 ".else");
2091 if (!TheCondStack.empty()) {
2092 TheCondState = TheCondStack.back();
2093 TheCondStack.pop_back();
2094 }
2095
2096 return false;
2097}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002098
2099/// ParseDirectiveFile
2100/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002101bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002102 // FIXME: I'm not sure what this is.
2103 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002104 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002105 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002106 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002107 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002108
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002109 if (FileNumber < 1)
2110 return TokError("file number less than one");
2111 }
2112
Daniel Dunbareceec052010-07-12 17:45:27 +00002113 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002114 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002115
Chris Lattnerd32e8032010-01-25 19:02:58 +00002116 StringRef Filename = getTok().getString();
2117 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002118 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002119
Daniel Dunbareceec052010-07-12 17:45:27 +00002120 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002121 return TokError("unexpected token in '.file' directive");
2122
Chris Lattnerd32e8032010-01-25 19:02:58 +00002123 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002124 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002125 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002126 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002127 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002128 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002129
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002130 return false;
2131}
2132
2133/// ParseDirectiveLine
2134/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002135bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002136 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2137 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002138 return TokError("unexpected token in '.line' directive");
2139
Sean Callanan18b83232010-01-19 21:44:56 +00002140 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002141 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002142 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002143
2144 // FIXME: Do something with the .line.
2145 }
2146
Daniel Dunbareceec052010-07-12 17:45:27 +00002147 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002148 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002149
2150 return false;
2151}
2152
2153
2154/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002155/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002156/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2157/// The first number is a file number, must have been previously assigned with
2158/// a .file directive, the second number is the line number and optionally the
2159/// third number is a column position (zero if not specified). The remaining
2160/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002161bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002162
Daniel Dunbareceec052010-07-12 17:45:27 +00002163 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002164 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002165 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002166 if (FileNumber < 1)
2167 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002168 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002169 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002170 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002171
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002172 int64_t LineNumber = 0;
2173 if (getLexer().is(AsmToken::Integer)) {
2174 LineNumber = getTok().getIntVal();
2175 if (LineNumber < 1)
2176 return TokError("line number less than one in '.loc' directive");
2177 Lex();
2178 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002179
2180 int64_t ColumnPos = 0;
2181 if (getLexer().is(AsmToken::Integer)) {
2182 ColumnPos = getTok().getIntVal();
2183 if (ColumnPos < 0)
2184 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002185 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002186 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002187
Kevin Enderbyc0957932010-09-30 16:52:03 +00002188 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002189 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002190 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002191 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2192 for (;;) {
2193 if (getLexer().is(AsmToken::EndOfStatement))
2194 break;
2195
2196 StringRef Name;
2197 SMLoc Loc = getTok().getLoc();
2198 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002199 return TokError("unexpected token in '.loc' directive");
2200
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002201 if (Name == "basic_block")
2202 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2203 else if (Name == "prologue_end")
2204 Flags |= DWARF2_FLAG_PROLOGUE_END;
2205 else if (Name == "epilogue_begin")
2206 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2207 else if (Name == "is_stmt") {
2208 SMLoc Loc = getTok().getLoc();
2209 const MCExpr *Value;
2210 if (getParser().ParseExpression(Value))
2211 return true;
2212 // The expression must be the constant 0 or 1.
2213 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2214 int Value = MCE->getValue();
2215 if (Value == 0)
2216 Flags &= ~DWARF2_FLAG_IS_STMT;
2217 else if (Value == 1)
2218 Flags |= DWARF2_FLAG_IS_STMT;
2219 else
2220 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002221 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002222 else {
2223 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2224 }
2225 }
2226 else if (Name == "isa") {
2227 SMLoc Loc = getTok().getLoc();
2228 const MCExpr *Value;
2229 if (getParser().ParseExpression(Value))
2230 return true;
2231 // The expression must be a constant greater or equal to 0.
2232 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2233 int Value = MCE->getValue();
2234 if (Value < 0)
2235 return Error(Loc, "isa number less than zero");
2236 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002237 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002238 else {
2239 return Error(Loc, "isa number not a constant value");
2240 }
2241 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002242 else if (Name == "discriminator") {
2243 if (getParser().ParseAbsoluteExpression(Discriminator))
2244 return true;
2245 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002246 else {
2247 return Error(Loc, "unknown sub-directive in '.loc' directive");
2248 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002249
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002250 if (getLexer().is(AsmToken::EndOfStatement))
2251 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002252 }
2253 }
2254
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002255 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2256 Isa, Discriminator);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002257
2258 return false;
2259}
2260
Daniel Dunbar138abae2010-10-16 04:56:42 +00002261/// ParseDirectiveStabs
2262/// ::= .stabs string, number, number, number
2263bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2264 SMLoc DirectiveLoc) {
2265 return TokError("unsupported directive '" + Directive + "'");
2266}
2267
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002268/// ParseDirectiveCFIStartProc
2269/// ::= .cfi_startproc
2270bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2271 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002272 getStreamer().EmitCFIStartProc();
2273 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002274}
2275
2276/// ParseDirectiveCFIEndProc
2277/// ::= .cfi_endproc
2278bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002279 getStreamer().EmitCFIEndProc();
2280 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002281}
2282
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002283/// ParseRegisterOrRegisterNumber - parse register name or number.
2284bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2285 SMLoc DirectiveLoc) {
2286 unsigned RegNo;
2287
2288 if (getLexer().is(AsmToken::Percent)) {
2289 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2290 DirectiveLoc))
2291 return true;
2292 Register = getContext().getTargetAsmInfo().getDwarfRegNum(RegNo, true);
2293 } else
2294 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002295
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002296 return false;
2297}
2298
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002299/// ParseDirectiveCFIDefCfa
2300/// ::= .cfi_def_cfa register, offset
2301bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2302 SMLoc DirectiveLoc) {
2303 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002304 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002305 return true;
2306
2307 if (getLexer().isNot(AsmToken::Comma))
2308 return TokError("unexpected token in directive");
2309 Lex();
2310
2311 int64_t Offset = 0;
2312 if (getParser().ParseAbsoluteExpression(Offset))
2313 return true;
2314
Rafael Espindola066c2f42011-04-12 23:59:07 +00002315 getStreamer().EmitCFIDefCfa(Register, Offset);
2316 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002317}
2318
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002319/// ParseDirectiveCFIDefCfaOffset
2320/// ::= .cfi_def_cfa_offset offset
2321bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2322 SMLoc DirectiveLoc) {
2323 int64_t Offset = 0;
2324 if (getParser().ParseAbsoluteExpression(Offset))
2325 return true;
2326
Rafael Espindola066c2f42011-04-12 23:59:07 +00002327 getStreamer().EmitCFIDefCfaOffset(Offset);
2328 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002329}
2330
2331/// ParseDirectiveCFIAdjustCfaOffset
2332/// ::= .cfi_adjust_cfa_offset adjustment
2333bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2334 SMLoc DirectiveLoc) {
2335 int64_t Adjustment = 0;
2336 if (getParser().ParseAbsoluteExpression(Adjustment))
2337 return true;
2338
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002339 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2340 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002341}
2342
2343/// ParseDirectiveCFIDefCfaRegister
2344/// ::= .cfi_def_cfa_register register
2345bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2346 SMLoc DirectiveLoc) {
2347 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002348 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002349 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002350
Rafael Espindola066c2f42011-04-12 23:59:07 +00002351 getStreamer().EmitCFIDefCfaRegister(Register);
2352 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002353}
2354
2355/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002356/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002357bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2358 int64_t Register = 0;
2359 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002360
2361 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002362 return true;
2363
2364 if (getLexer().isNot(AsmToken::Comma))
2365 return TokError("unexpected token in directive");
2366 Lex();
2367
2368 if (getParser().ParseAbsoluteExpression(Offset))
2369 return true;
2370
Rafael Espindola066c2f42011-04-12 23:59:07 +00002371 getStreamer().EmitCFIOffset(Register, Offset);
2372 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002373}
2374
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002375/// ParseDirectiveCFIRelOffset
2376/// ::= .cfi_rel_offset register, offset
2377bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2378 SMLoc DirectiveLoc) {
2379 int64_t Register = 0;
2380
2381 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2382 return true;
2383
2384 if (getLexer().isNot(AsmToken::Comma))
2385 return TokError("unexpected token in directive");
2386 Lex();
2387
2388 int64_t Offset = 0;
2389 if (getParser().ParseAbsoluteExpression(Offset))
2390 return true;
2391
Rafael Espindola25f492e2011-04-12 16:12:03 +00002392 getStreamer().EmitCFIRelOffset(Register, Offset);
2393 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002394}
2395
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002396static bool isValidEncoding(int64_t Encoding) {
2397 if (Encoding & ~0xff)
2398 return false;
2399
2400 if (Encoding == dwarf::DW_EH_PE_omit)
2401 return true;
2402
2403 const unsigned Format = Encoding & 0xf;
2404 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2405 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2406 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2407 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2408 return false;
2409
Rafael Espindolacaf11582010-12-29 04:31:26 +00002410 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002411 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002412 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002413 return false;
2414
2415 return true;
2416}
2417
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002418/// ParseDirectiveCFIPersonalityOrLsda
2419/// ::= .cfi_personality encoding, [symbol_name]
2420/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002421bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002422 SMLoc DirectiveLoc) {
2423 int64_t Encoding = 0;
2424 if (getParser().ParseAbsoluteExpression(Encoding))
2425 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002426 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002427 return false;
2428
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002429 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002430 return TokError("unsupported encoding.");
2431
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002432 if (getLexer().isNot(AsmToken::Comma))
2433 return TokError("unexpected token in directive");
2434 Lex();
2435
2436 StringRef Name;
2437 if (getParser().ParseIdentifier(Name))
2438 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002439
2440 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2441
2442 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002443 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002444 else {
2445 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002446 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002447 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002448 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002449}
2450
Rafael Espindolafe024d02010-12-28 18:36:23 +00002451/// ParseDirectiveCFIRememberState
2452/// ::= .cfi_remember_state
2453bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2454 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002455 getStreamer().EmitCFIRememberState();
2456 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002457}
2458
2459/// ParseDirectiveCFIRestoreState
2460/// ::= .cfi_remember_state
2461bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2462 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002463 getStreamer().EmitCFIRestoreState();
2464 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002465}
2466
Rafael Espindolac5754392011-04-12 15:31:05 +00002467/// ParseDirectiveCFISameValue
2468/// ::= .cfi_same_value register
2469bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2470 SMLoc DirectiveLoc) {
2471 int64_t Register = 0;
2472
2473 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2474 return true;
2475
2476 getStreamer().EmitCFISameValue(Register);
2477
2478 return false;
2479}
2480
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002481/// ParseDirectiveMacrosOnOff
2482/// ::= .macros_on
2483/// ::= .macros_off
2484bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2485 SMLoc DirectiveLoc) {
2486 if (getLexer().isNot(AsmToken::EndOfStatement))
2487 return Error(getLexer().getLoc(),
2488 "unexpected token in '" + Directive + "' directive");
2489
2490 getParser().MacrosEnabled = Directive == ".macros_on";
2491
2492 return false;
2493}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002494
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002495/// ParseDirectiveMacro
2496/// ::= .macro name
2497bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2498 SMLoc DirectiveLoc) {
2499 StringRef Name;
2500 if (getParser().ParseIdentifier(Name))
2501 return TokError("expected identifier in directive");
2502
2503 if (getLexer().isNot(AsmToken::EndOfStatement))
2504 return TokError("unexpected token in '.macro' directive");
2505
2506 // Eat the end of statement.
2507 Lex();
2508
2509 AsmToken EndToken, StartToken = getTok();
2510
2511 // Lex the macro definition.
2512 for (;;) {
2513 // Check whether we have reached the end of the file.
2514 if (getLexer().is(AsmToken::Eof))
2515 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2516
2517 // Otherwise, check whether we have reach the .endmacro.
2518 if (getLexer().is(AsmToken::Identifier) &&
2519 (getTok().getIdentifier() == ".endm" ||
2520 getTok().getIdentifier() == ".endmacro")) {
2521 EndToken = getTok();
2522 Lex();
2523 if (getLexer().isNot(AsmToken::EndOfStatement))
2524 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2525 "' directive");
2526 break;
2527 }
2528
2529 // Otherwise, scan til the end of the statement.
2530 getParser().EatToEndOfStatement();
2531 }
2532
2533 if (getParser().MacroMap.lookup(Name)) {
2534 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2535 }
2536
2537 const char *BodyStart = StartToken.getLoc().getPointer();
2538 const char *BodyEnd = EndToken.getLoc().getPointer();
2539 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2540 getParser().MacroMap[Name] = new Macro(Name, Body);
2541 return false;
2542}
2543
2544/// ParseDirectiveEndMacro
2545/// ::= .endm
2546/// ::= .endmacro
2547bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2548 SMLoc DirectiveLoc) {
2549 if (getLexer().isNot(AsmToken::EndOfStatement))
2550 return TokError("unexpected token in '" + Directive + "' directive");
2551
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002552 // If we are inside a macro instantiation, terminate the current
2553 // instantiation.
2554 if (!getParser().ActiveMacros.empty()) {
2555 getParser().HandleMacroExit();
2556 return false;
2557 }
2558
2559 // Otherwise, this .endmacro is a stray entry in the file; well formed
2560 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002561 return TokError("unexpected '" + Directive + "' in file, "
2562 "no current macro definition");
2563}
2564
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002565bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002566 getParser().CheckForValidSection();
2567
2568 const MCExpr *Value;
2569
2570 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002571 return true;
2572
2573 if (getLexer().isNot(AsmToken::EndOfStatement))
2574 return TokError("unexpected token in directive");
2575
2576 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002577 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002578 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002579 getStreamer().EmitULEB128Value(Value);
2580
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002581 return false;
2582}
2583
2584
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002585/// \brief Create an MCAsmParser instance.
2586MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2587 MCContext &C, MCStreamer &Out,
2588 const MCAsmInfo &MAI) {
2589 return new AsmParser(T, SM, C, Out, MAI);
2590}