blob: 87b7cd634f3d65ad196436484b54c7aa6a3fb7c2 [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
84 // FIXME: This is not the best place to store this. To handle a (for example)
85 // .cfi_rel_offset before a .cfi_def_cfa_offset we need to know the initial
86 // frame state.
Rafael Espindola53abbe52011-04-11 20:29:16 +000087 int64_t LastOffset;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000088
Daniel Dunbaraef87e32010-07-18 18:31:38 +000089 /// This is the current buffer index we're lexing from as managed by the
90 /// SourceMgr object.
91 int CurBuffer;
92
93 AsmCond TheCondState;
94 std::vector<AsmCond> TheCondStack;
95
96 /// DirectiveMap - This is a table handlers for directives. Each handler is
97 /// invoked after the directive identifier is read and is responsible for
98 /// parsing and validating the rest of the directive. The handler is passed
99 /// in the directive name and the location of the directive keyword.
100 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000101
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000102 /// MacroMap - Map of currently defined macros.
103 StringMap<Macro*> MacroMap;
104
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000105 /// ActiveMacros - Stack of active macro instantiations.
106 std::vector<MacroInstantiation*> ActiveMacros;
107
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000108 /// Boolean tracking whether macro substitution is enabled.
109 unsigned MacrosEnabled : 1;
110
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000111 /// Flag tracking whether any errors have been encountered.
112 unsigned HadError : 1;
113
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000114public:
115 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
116 const MCAsmInfo &MAI);
117 ~AsmParser();
118
119 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
120
121 void AddDirectiveHandler(MCAsmParserExtension *Object,
122 StringRef Directive,
123 DirectiveHandler Handler) {
124 DirectiveMap[Directive] = std::make_pair(Object, Handler);
125 }
126
127public:
128 /// @name MCAsmParser Interface
129 /// {
130
131 virtual SourceMgr &getSourceManager() { return SrcMgr; }
132 virtual MCAsmLexer &getLexer() { return Lexer; }
133 virtual MCContext &getContext() { return Ctx; }
134 virtual MCStreamer &getStreamer() { return Out; }
135
136 virtual void Warning(SMLoc L, const Twine &Meg);
137 virtual bool Error(SMLoc L, const Twine &Msg);
138
139 const AsmToken &Lex();
140
141 bool ParseExpression(const MCExpr *&Res);
142 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
143 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
144 virtual bool ParseAbsoluteExpression(int64_t &Res);
145
146 /// }
147
Rafael Espindola53abbe52011-04-11 20:29:16 +0000148 int64_t adjustLastOffset(int64_t Adjustment) {
149 LastOffset += Adjustment;
150 return LastOffset;
151 }
152 void setLastOffset(int64_t Offset) {
153 LastOffset = Offset;
154 }
155
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000156private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000157 void CheckForValidSection();
158
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000159 bool ParseStatement();
160
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000161 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
162 void HandleMacroExit();
163
164 void PrintMacroInstantiations();
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000165 void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type) const {
166 SrcMgr.PrintMessage(Loc, Msg, Type);
167 }
168
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000169 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
170 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000171
172 /// \brief Reset the current lexer position to that given by \arg Loc. The
173 /// current token is not set; clients should ensure Lex() is called
174 /// subsequently.
175 void JumpToLoc(SMLoc Loc);
176
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000177 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000178
179 /// \brief Parse up to the end of statement and a return the contents from the
180 /// current token until the end of the statement; the current token on exit
181 /// will be either the EndOfStatement or EOF.
182 StringRef ParseStringToEndOfStatement();
183
Nico Weber4c4c7322011-01-28 03:04:41 +0000184 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000185
186 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
187 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
188 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000189 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000190
191 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
192 /// and set \arg Res to the identifier contents.
193 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000194
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000195 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000196
197 // ".ascii", ".asciiz", ".string"
198 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000199 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000200 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000201 bool ParseDirectiveFill(); // ".fill"
202 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000203 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000204 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000205 bool ParseDirectiveOrg(); // ".org"
206 // ".align{,32}", ".p2align{,w,l}"
207 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
208
209 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
210 /// accepts a single symbol (which should be a label or an external).
211 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000212
213 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
214
215 bool ParseDirectiveAbort(); // ".abort"
216 bool ParseDirectiveInclude(); // ".include"
217
218 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000219 // ".ifdef" or ".ifndef", depending on expect_defined
220 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000221 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
222 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
223 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
224
225 /// ParseEscapedString - Parse the current token as a string which may include
226 /// escaped characters and return the string contents.
227 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000228
229 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
230 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000231};
232
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000233/// \brief Generic implementations of directive handling, etc. which is shared
234/// (or the default, at least) for all assembler parser.
235class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000236 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
237 void AddDirectiveHandler(StringRef Directive) {
238 getParser().AddDirectiveHandler(this, Directive,
239 HandleDirective<GenericAsmParser, Handler>);
240 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000241public:
242 GenericAsmParser() {}
243
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000244 AsmParser &getParser() {
245 return (AsmParser&) this->MCAsmParserExtension::getParser();
246 }
247
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000248 virtual void Initialize(MCAsmParser &Parser) {
249 // Call the base implementation.
250 this->MCAsmParserExtension::Initialize(Parser);
251
252 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000253 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
254 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
255 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000256 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000257
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000258 // CFI directives.
259 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
260 ".cfi_startproc");
261 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
262 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000263 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
264 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000265 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
266 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000267 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
268 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000269 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
270 ".cfi_def_cfa_register");
271 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
272 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000273 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
274 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000275 AddDirectiveHandler<
276 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
277 AddDirectiveHandler<
278 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000279 AddDirectiveHandler<
280 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
281 AddDirectiveHandler<
282 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000283 AddDirectiveHandler<
284 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000285
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000286 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000287 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
288 ".macros_on");
289 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
290 ".macros_off");
291 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
292 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
293 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000294
295 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
296 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000297 }
298
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000299 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
300
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000301 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
302 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
303 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000304 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000305 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
306 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000307 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000308 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000309 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000310 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
311 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000312 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000313 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000314 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
315 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000316 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000317
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000318 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000319 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
320 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000321
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000322 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000323};
324
325}
326
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000327namespace llvm {
328
329extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000330extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000331extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000332
333}
334
Chris Lattneraaec2052010-01-19 19:46:13 +0000335enum { DEFAULT_ADDRSPACE = 0 };
336
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000337AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
338 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000339 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Rafael Espindola53abbe52011-04-11 20:29:16 +0000340 GenericParser(new GenericAsmParser), PlatformParser(0), LastOffset(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000341 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000342 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000343
344 // Initialize the generic parser.
345 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000346
347 // Initialize the platform / file format parser.
348 //
349 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
350 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000351 if (_MAI.hasMicrosoftFastStdCallMangling()) {
352 PlatformParser = createCOFFAsmParser();
353 PlatformParser->Initialize(*this);
354 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000355 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000356 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000357 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000358 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000359 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000360 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000361}
362
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000363AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000364 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
365
366 // Destroy any macros.
367 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
368 ie = MacroMap.end(); it != ie; ++it)
369 delete it->getValue();
370
Daniel Dunbare4749702010-07-12 18:12:02 +0000371 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000372 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000373}
374
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000375void AsmParser::PrintMacroInstantiations() {
376 // Print the active macro instantiation stack.
377 for (std::vector<MacroInstantiation*>::const_reverse_iterator
378 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
379 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
380 "note");
381}
382
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000383void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000384 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000385 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000386}
387
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000388bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000389 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000390 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000391 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000392 return true;
393}
394
Sean Callananfd0b0282010-01-21 00:19:58 +0000395bool AsmParser::EnterIncludeFile(const std::string &Filename) {
396 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
397 if (NewBuf == -1)
398 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000399
Sean Callananfd0b0282010-01-21 00:19:58 +0000400 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000401
Sean Callananfd0b0282010-01-21 00:19:58 +0000402 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000403
Sean Callananfd0b0282010-01-21 00:19:58 +0000404 return false;
405}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000406
407void AsmParser::JumpToLoc(SMLoc Loc) {
408 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
409 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
410}
411
Sean Callananfd0b0282010-01-21 00:19:58 +0000412const AsmToken &AsmParser::Lex() {
413 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000414
Sean Callananfd0b0282010-01-21 00:19:58 +0000415 if (tok->is(AsmToken::Eof)) {
416 // If this is the end of an included file, pop the parent file off the
417 // include stack.
418 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
419 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000420 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000421 tok = &Lexer.Lex();
422 }
423 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000424
Sean Callananfd0b0282010-01-21 00:19:58 +0000425 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000426 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000427
Sean Callananfd0b0282010-01-21 00:19:58 +0000428 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000429}
430
Chris Lattner79180e22010-04-05 23:15:42 +0000431bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000432 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000433 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000434 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000435
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000436 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000437 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000438
439 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000440 AsmCond StartingCondState = TheCondState;
441
Chris Lattnerb717fb02009-07-02 21:53:43 +0000442 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000443 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000444 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000445
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000446 // We had an error, validate that one was emitted and recover by skipping to
447 // the next line.
448 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000449 EatToEndOfStatement();
450 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000451
452 if (TheCondState.TheCond != StartingCondState.TheCond ||
453 TheCondState.Ignore != StartingCondState.Ignore)
454 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000455
456 // Check to see there are no empty DwarfFile slots.
457 const std::vector<MCDwarfFile *> &MCDwarfFiles =
458 getContext().getMCDwarfFiles();
459 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000460 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000461 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000462 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000463
Chris Lattner79180e22010-04-05 23:15:42 +0000464 // Finalize the output stream if there are no errors and if the client wants
465 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000466 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000467 Out.Finish();
468
Chris Lattnerb717fb02009-07-02 21:53:43 +0000469 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000470}
471
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000472void AsmParser::CheckForValidSection() {
473 if (!getStreamer().getCurrentSection()) {
474 TokError("expected section directive before assembly directive");
475 Out.SwitchSection(Ctx.getMachOSection(
476 "__TEXT", "__text",
477 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
478 0, SectionKind::getText()));
479 }
480}
481
Chris Lattner2cf5f142009-06-22 01:29:09 +0000482/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
483void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000484 while (Lexer.isNot(AsmToken::EndOfStatement) &&
485 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000486 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000487
Chris Lattner2cf5f142009-06-22 01:29:09 +0000488 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000489 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000490 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000491}
492
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000493StringRef AsmParser::ParseStringToEndOfStatement() {
494 const char *Start = getTok().getLoc().getPointer();
495
496 while (Lexer.isNot(AsmToken::EndOfStatement) &&
497 Lexer.isNot(AsmToken::Eof))
498 Lex();
499
500 const char *End = getTok().getLoc().getPointer();
501 return StringRef(Start, End - Start);
502}
Chris Lattnerc4193832009-06-22 05:51:26 +0000503
Chris Lattner74ec1a32009-06-22 06:32:03 +0000504/// ParseParenExpr - Parse a paren expression and return it.
505/// NOTE: This assumes the leading '(' has already been consumed.
506///
507/// parenexpr ::= expr)
508///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000509bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000510 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000511 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000512 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000513 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000514 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000515 return false;
516}
Chris Lattnerc4193832009-06-22 05:51:26 +0000517
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000518/// ParseBracketExpr - Parse a bracket expression and return it.
519/// NOTE: This assumes the leading '[' has already been consumed.
520///
521/// bracketexpr ::= expr]
522///
523bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
524 if (ParseExpression(Res)) return true;
525 if (Lexer.isNot(AsmToken::RBrac))
526 return TokError("expected ']' in brackets expression");
527 EndLoc = Lexer.getLoc();
528 Lex();
529 return false;
530}
531
Chris Lattner74ec1a32009-06-22 06:32:03 +0000532/// ParsePrimaryExpr - Parse a primary expression and return it.
533/// primaryexpr ::= (parenexpr
534/// primaryexpr ::= symbol
535/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000536/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000537/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000538bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000539 switch (Lexer.getKind()) {
540 default:
541 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000542 // If we have an error assume that we've already handled it.
543 case AsmToken::Error:
544 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000545 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000546 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000547 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000548 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000549 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000550 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000551 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000552 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000553 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000554 EndLoc = Lexer.getLoc();
555
556 StringRef Identifier;
557 if (ParseIdentifier(Identifier))
558 return false;
559
Daniel Dunbarfffff912009-10-16 01:34:54 +0000560 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000561 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000562 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000563
564 // Lookup the symbol variant if used.
565 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000566 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000567 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000568 if (Variant == MCSymbolRefExpr::VK_Invalid) {
569 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000570 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000571 }
572 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000573
Daniel Dunbarfffff912009-10-16 01:34:54 +0000574 // If this is an absolute variable reference, substitute it now to preserve
575 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000576 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000577 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000578 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000579
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000580 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000581 return false;
582 }
583
584 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000585 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000586 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000587 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000588 case AsmToken::Integer: {
589 SMLoc Loc = getTok().getLoc();
590 int64_t IntVal = getTok().getIntVal();
591 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000592 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000593 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000594 // Look for 'b' or 'f' following an Integer as a directional label
595 if (Lexer.getKind() == AsmToken::Identifier) {
596 StringRef IDVal = getTok().getString();
597 if (IDVal == "f" || IDVal == "b"){
598 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
599 IDVal == "f" ? 1 : 0);
600 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
601 getContext());
602 if(IDVal == "b" && Sym->isUndefined())
603 return Error(Loc, "invalid reference to undefined symbol");
604 EndLoc = Lexer.getLoc();
605 Lex(); // Eat identifier.
606 }
607 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000608 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000609 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000610 case AsmToken::Real: {
611 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000612 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000613 Res = MCConstantExpr::Create(IntVal, getContext());
614 Lex(); // Eat token.
615 return false;
616 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000617 case AsmToken::Dot: {
618 // This is a '.' reference, which references the current PC. Emit a
619 // temporary label to the streamer and refer to it.
620 MCSymbol *Sym = Ctx.CreateTempSymbol();
621 Out.EmitLabel(Sym);
622 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
623 EndLoc = Lexer.getLoc();
624 Lex(); // Eat identifier.
625 return false;
626 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000627 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000628 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000629 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000630 case AsmToken::LBrac:
631 if (!PlatformParser->HasBracketExpressions())
632 return TokError("brackets expression not supported on this target");
633 Lex(); // Eat the '['.
634 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000635 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000636 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000637 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000638 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000639 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000640 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000641 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000642 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000643 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000644 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000645 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000646 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000647 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000648 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000649 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000650 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000651 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000652 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000653 }
654}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000655
Chris Lattnerb4307b32010-01-15 19:28:38 +0000656bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000657 SMLoc EndLoc;
658 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000659}
660
Daniel Dunbarcceba832010-09-17 02:47:07 +0000661const MCExpr *
662AsmParser::ApplyModifierToExpr(const MCExpr *E,
663 MCSymbolRefExpr::VariantKind Variant) {
664 // Recurse over the given expression, rebuilding it to apply the given variant
665 // if there is exactly one symbol.
666 switch (E->getKind()) {
667 case MCExpr::Target:
668 case MCExpr::Constant:
669 return 0;
670
671 case MCExpr::SymbolRef: {
672 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
673
674 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
675 TokError("invalid variant on expression '" +
676 getTok().getIdentifier() + "' (already modified)");
677 return E;
678 }
679
680 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
681 }
682
683 case MCExpr::Unary: {
684 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
685 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
686 if (!Sub)
687 return 0;
688 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
689 }
690
691 case MCExpr::Binary: {
692 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
693 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
694 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
695
696 if (!LHS && !RHS)
697 return 0;
698
699 if (!LHS) LHS = BE->getLHS();
700 if (!RHS) RHS = BE->getRHS();
701
702 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
703 }
704 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000705
706 assert(0 && "Invalid expression kind!");
707 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000708}
709
Chris Lattner74ec1a32009-06-22 06:32:03 +0000710/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000711///
Chris Lattner74ec1a32009-06-22 06:32:03 +0000712/// expr ::= expr +,- expr -> lowest.
713/// expr ::= expr |,^,&,! expr -> middle.
714/// expr ::= expr *,/,%,<<,>> expr -> highest.
715/// expr ::= primaryexpr
716///
Chris Lattner54482b42010-01-15 19:39:23 +0000717bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000718 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000719 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000720 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
721 return true;
722
Daniel Dunbarcceba832010-09-17 02:47:07 +0000723 // As a special case, we support 'a op b @ modifier' by rewriting the
724 // expression to include the modifier. This is inefficient, but in general we
725 // expect users to use 'a@modifier op b'.
726 if (Lexer.getKind() == AsmToken::At) {
727 Lex();
728
729 if (Lexer.isNot(AsmToken::Identifier))
730 return TokError("unexpected symbol modifier following '@'");
731
732 MCSymbolRefExpr::VariantKind Variant =
733 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
734 if (Variant == MCSymbolRefExpr::VK_Invalid)
735 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
736
737 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
738 if (!ModifiedRes) {
739 return TokError("invalid modifier '" + getTok().getIdentifier() +
740 "' (no symbols present)");
741 return true;
742 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000743
Daniel Dunbarcceba832010-09-17 02:47:07 +0000744 Res = ModifiedRes;
745 Lex();
746 }
747
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000748 // Try to constant fold it up front, if possible.
749 int64_t Value;
750 if (Res->EvaluateAsAbsolute(Value))
751 Res = MCConstantExpr::Create(Value, getContext());
752
753 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000754}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000755
Chris Lattnerb4307b32010-01-15 19:28:38 +0000756bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000757 Res = 0;
758 return ParseParenExpr(Res, EndLoc) ||
759 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000760}
761
Daniel Dunbar475839e2009-06-29 20:37:27 +0000762bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000763 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000764
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000765 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000766 if (ParseExpression(Expr))
767 return true;
768
Daniel Dunbare00b0112009-10-16 01:57:52 +0000769 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000770 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000771
772 return false;
773}
774
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000775static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000776 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000777 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000778 default:
779 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000780
Daniel Dunbarcceba832010-09-17 02:47:07 +0000781 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000782 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000783 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000784 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000785 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000786 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000787 return 1;
788
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000789
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000790 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000791 //
792 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000793 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000794 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000795 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000796 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000797 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000798 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000799 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000800 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000801 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000802
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000803 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000804 case AsmToken::EqualEqual:
805 Kind = MCBinaryExpr::EQ;
806 return 3;
807 case AsmToken::ExclaimEqual:
808 case AsmToken::LessGreater:
809 Kind = MCBinaryExpr::NE;
810 return 3;
811 case AsmToken::Less:
812 Kind = MCBinaryExpr::LT;
813 return 3;
814 case AsmToken::LessEqual:
815 Kind = MCBinaryExpr::LTE;
816 return 3;
817 case AsmToken::Greater:
818 Kind = MCBinaryExpr::GT;
819 return 3;
820 case AsmToken::GreaterEqual:
821 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000822 return 3;
823
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000824 // High Intermediate Precedence: +, -
825 case AsmToken::Plus:
826 Kind = MCBinaryExpr::Add;
827 return 4;
828 case AsmToken::Minus:
829 Kind = MCBinaryExpr::Sub;
830 return 4;
831
Daniel Dunbar475839e2009-06-29 20:37:27 +0000832 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000833 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000834 Kind = MCBinaryExpr::Mul;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000835 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000836 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000837 Kind = MCBinaryExpr::Div;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000838 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000839 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000840 Kind = MCBinaryExpr::Mod;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000841 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000842 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000843 Kind = MCBinaryExpr::Shl;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000844 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000845 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000846 Kind = MCBinaryExpr::Shr;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000847 return 5;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000848 }
849}
850
851
852/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
853/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000854bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
855 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000856 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000857 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000858 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000859
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000860 // If the next token is lower precedence than we are allowed to eat, return
861 // successfully with what we ate already.
862 if (TokPrec < Precedence)
863 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000864
Sean Callanan79ed1a82010-01-19 20:22:31 +0000865 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000866
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000867 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000868 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000869 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000870
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000871 // If BinOp binds less tightly with RHS than the operator after RHS, let
872 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000873 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000874 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000875 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000876 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000877 }
878
Daniel Dunbar475839e2009-06-29 20:37:27 +0000879 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000880 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000881 }
882}
883
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000884
885
886
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000887/// ParseStatement:
888/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000889/// ::= Label* Directive ...Operands... EndOfStatement
890/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000891bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000892 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000893 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000894 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000895 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000896 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000897
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000898 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000899 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000900 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000901 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000902 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000903 // A full line comment is a '#' as the first token.
904 if (Lexer.is(AsmToken::Hash)) {
905 EatToEndOfStatement();
906 return false;
907 }
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000908
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000909 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000910 if (Lexer.is(AsmToken::Integer)) {
911 LocalLabelVal = getTok().getIntVal();
912 if (LocalLabelVal < 0) {
913 if (!TheCondState.Ignore)
914 return TokError("unexpected token at start of statement");
915 IDVal = "";
916 }
917 else {
918 IDVal = getTok().getString();
919 Lex(); // Consume the integer token to be used as an identifier token.
920 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000921 if (!TheCondState.Ignore)
922 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000923 }
924 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000925
926 } else if (Lexer.is(AsmToken::Dot)) {
927 // Treat '.' as a valid identifier in this context.
928 Lex();
929 IDVal = ".";
930
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000931 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000932 if (!TheCondState.Ignore)
933 return TokError("unexpected token at start of statement");
934 IDVal = "";
935 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000936
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000937
Chris Lattner7834fac2010-04-17 18:14:27 +0000938 // Handle conditional assembly here before checking for skipping. We
939 // have to do this so that .endif isn't skipped in a ".if 0" block for
940 // example.
941 if (IDVal == ".if")
942 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000943 if (IDVal == ".ifdef")
944 return ParseDirectiveIfdef(IDLoc, true);
945 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
946 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +0000947 if (IDVal == ".elseif")
948 return ParseDirectiveElseIf(IDLoc);
949 if (IDVal == ".else")
950 return ParseDirectiveElse(IDLoc);
951 if (IDVal == ".endif")
952 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000953
Chris Lattner7834fac2010-04-17 18:14:27 +0000954 // If we are in a ".if 0" block, ignore this statement.
955 if (TheCondState.Ignore) {
956 EatToEndOfStatement();
957 return false;
958 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000959
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000960 // FIXME: Recurse on local labels?
961
962 // See what kind of statement we have.
963 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000964 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000965 CheckForValidSection();
966
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000967 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000968 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000969
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000970 // Diagnose attempt to use '.' as a label.
971 if (IDVal == ".")
972 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
973
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000974 // Diagnose attempt to use a variable as a label.
975 //
976 // FIXME: Diagnostics. Note the location of the definition as a label.
977 // FIXME: This doesn't diagnose assignment to a symbol which has been
978 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000979 MCSymbol *Sym;
980 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000981 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000982 else
983 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000984 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000985 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000986
Daniel Dunbar959fd882009-08-26 22:13:22 +0000987 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000988 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000989
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000990 // Consume any end of statement token, if present, to avoid spurious
991 // AddBlankLine calls().
992 if (Lexer.is(AsmToken::EndOfStatement)) {
993 Lex();
994 if (Lexer.is(AsmToken::Eof))
995 return false;
996 }
997
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000998 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000999 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001000
Daniel Dunbar3f872332009-07-28 16:08:33 +00001001 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001002 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001003 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001004
Nico Weber4c4c7322011-01-28 03:04:41 +00001005 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001006
1007 default: // Normal instruction or directive.
1008 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001009 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001010
1011 // If macros are enabled, check to see if this is a macro instantiation.
1012 if (MacrosEnabled)
1013 if (const Macro *M = MacroMap.lookup(IDVal))
1014 return HandleMacroEntry(IDVal, IDLoc, M);
1015
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001016 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001017 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001018 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001019 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001020 return ParseDirectiveSet(IDVal, true);
1021 if (IDVal == ".equiv")
1022 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001023
Daniel Dunbara0d14262009-06-24 23:30:00 +00001024 // Data directives
1025
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001026 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001027 return ParseDirectiveAscii(IDVal, false);
1028 if (IDVal == ".asciz" || IDVal == ".string")
1029 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001030
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001031 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001032 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001033 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001034 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001035 if (IDVal == ".value")
1036 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001037 if (IDVal == ".2byte")
1038 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001039 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001040 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001041 if (IDVal == ".int")
1042 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001043 if (IDVal == ".4byte")
1044 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001045 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001046 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001047 if (IDVal == ".8byte")
1048 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001049 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001050 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1051 if (IDVal == ".double")
1052 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001053
Eli Friedman5d68ec22010-07-19 04:17:25 +00001054 if (IDVal == ".align") {
1055 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1056 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1057 }
1058 if (IDVal == ".align32") {
1059 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1060 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1061 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001062 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001063 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001064 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001065 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001066 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001067 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001068 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001069 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001070 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001071 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001072 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001073 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1074
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001075 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001076 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001077
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001078 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001079 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001080 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001081 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001082 if (IDVal == ".zero")
1083 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001084
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001085 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001086
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001087 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001088 return ParseDirectiveSymbolAttribute(MCSA_Global);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +00001089 // ELF only? Should it be here?
1090 if (IDVal == ".local")
1091 return ParseDirectiveSymbolAttribute(MCSA_Local);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001092 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001093 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001094 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001095 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001096 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001097 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001098 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001099 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001100 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001101 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001102 if (IDVal == ".symbol_resolver")
1103 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001104 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001105 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001106 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001107 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001108 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001109 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001110 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001111 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001112 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001113 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001114 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001115 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001116 if (IDVal == ".weak_def_can_be_hidden")
1117 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001118
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001119 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001120 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001121 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001122 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001123
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001124 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001125 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001126 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001127 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001128
Roman Divackybb6d14f2011-01-31 21:19:43 +00001129 if (IDVal == ".code16" || IDVal == ".code32" || IDVal == ".code64")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001130 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001131
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001132 // Look up the handler in the handler table.
1133 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1134 DirectiveMap.lookup(IDVal);
1135 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001136 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001137
Kevin Enderby9c656452009-09-10 20:51:44 +00001138 // Target hook for parsing target specific directives.
1139 if (!getTargetParser().ParseDirective(ID))
1140 return false;
1141
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001142 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001143 EatToEndOfStatement();
1144 return false;
1145 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001146
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001147 CheckForValidSection();
1148
Chris Lattnera7f13542010-05-19 23:34:33 +00001149 // Canonicalize the opcode to lower case.
1150 SmallString<128> Opcode;
1151 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1152 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001153
Chris Lattner98986712010-01-14 22:21:20 +00001154 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001155 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001156 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001157
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001158 // Dump the parsed representation, if requested.
1159 if (getShowParsedOperands()) {
1160 SmallString<256> Str;
1161 raw_svector_ostream OS(Str);
1162 OS << "parsed instruction: [";
1163 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1164 if (i != 0)
1165 OS << ", ";
1166 ParsedOperands[i]->dump(OS);
1167 }
1168 OS << "]";
1169
1170 PrintMessage(IDLoc, OS.str(), "note");
1171 }
1172
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001173 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001174 if (!HadError)
1175 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1176 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001177
Chris Lattner98986712010-01-14 22:21:20 +00001178 // Free any parsed operands.
1179 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1180 delete ParsedOperands[i];
1181
Chris Lattnercbf8a982010-09-11 16:18:25 +00001182 // Don't skip the rest of the line, the instruction parser is responsible for
1183 // that.
1184 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001185}
Chris Lattner9a023f72009-06-24 04:43:34 +00001186
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001187MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1188 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001189 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
1190{
1191 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1192 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001193 SmallString<256> Buf;
1194 raw_svector_ostream OS(Buf);
1195
1196 StringRef Body = M->Body;
1197 while (!Body.empty()) {
1198 // Scan for the next substitution.
1199 std::size_t End = Body.size(), Pos = 0;
1200 for (; Pos != End; ++Pos) {
1201 // Check for a substitution or escape.
1202 if (Body[Pos] != '$' || Pos + 1 == End)
1203 continue;
1204
1205 char Next = Body[Pos + 1];
1206 if (Next == '$' || Next == 'n' || isdigit(Next))
1207 break;
1208 }
1209
1210 // Add the prefix.
1211 OS << Body.slice(0, Pos);
1212
1213 // Check if we reached the end.
1214 if (Pos == End)
1215 break;
1216
1217 switch (Body[Pos+1]) {
1218 // $$ => $
1219 case '$':
1220 OS << '$';
1221 break;
1222
1223 // $n => number of arguments
1224 case 'n':
1225 OS << A.size();
1226 break;
1227
1228 // $[0-9] => argument
1229 default: {
1230 // Missing arguments are ignored.
1231 unsigned Index = Body[Pos+1] - '0';
1232 if (Index >= A.size())
1233 break;
1234
1235 // Otherwise substitute with the token values, with spaces eliminated.
1236 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1237 ie = A[Index].end(); it != ie; ++it)
1238 OS << it->getString();
1239 break;
1240 }
1241 }
1242
1243 // Update the scan point.
1244 Body = Body.substr(Pos + 2);
1245 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001246
1247 // We include the .endmacro in the buffer as our queue to exit the macro
1248 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001249 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001250
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001251 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001252}
1253
1254bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1255 const Macro *M) {
1256 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1257 // this, although we should protect against infinite loops.
1258 if (ActiveMacros.size() == 20)
1259 return TokError("macros cannot be nested more than 20 levels deep");
1260
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001261 // Parse the macro instantiation arguments.
1262 std::vector<std::vector<AsmToken> > MacroArguments;
1263 MacroArguments.push_back(std::vector<AsmToken>());
1264 unsigned ParenLevel = 0;
1265 for (;;) {
1266 if (Lexer.is(AsmToken::Eof))
1267 return TokError("unexpected token in macro instantiation");
1268 if (Lexer.is(AsmToken::EndOfStatement))
1269 break;
1270
1271 // If we aren't inside parentheses and this is a comma, start a new token
1272 // list.
1273 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1274 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001275 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001276 // Adjust the current parentheses level.
1277 if (Lexer.is(AsmToken::LParen))
1278 ++ParenLevel;
1279 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1280 --ParenLevel;
1281
1282 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001283 MacroArguments.back().push_back(getTok());
1284 }
1285 Lex();
1286 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001287
1288 // Create the macro instantiation object and add to the current macro
1289 // instantiation stack.
1290 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001291 getTok().getLoc(),
1292 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001293 ActiveMacros.push_back(MI);
1294
1295 // Jump to the macro instantiation and prime the lexer.
1296 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1297 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1298 Lex();
1299
1300 return false;
1301}
1302
1303void AsmParser::HandleMacroExit() {
1304 // Jump to the EndOfStatement we should return to, and consume it.
1305 JumpToLoc(ActiveMacros.back()->ExitLoc);
1306 Lex();
1307
1308 // Pop the instantiation entry.
1309 delete ActiveMacros.back();
1310 ActiveMacros.pop_back();
1311}
1312
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001313static void MarkUsed(const MCExpr *Value) {
1314 switch (Value->getKind()) {
1315 case MCExpr::Binary:
1316 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1317 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1318 break;
1319 case MCExpr::Target:
1320 case MCExpr::Constant:
1321 break;
1322 case MCExpr::SymbolRef: {
1323 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1324 break;
1325 }
1326 case MCExpr::Unary:
1327 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1328 break;
1329 }
1330}
1331
Nico Weber4c4c7322011-01-28 03:04:41 +00001332bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001333 // FIXME: Use better location, we should use proper tokens.
1334 SMLoc EqualLoc = Lexer.getLoc();
1335
Daniel Dunbar821e3332009-08-31 08:09:28 +00001336 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001337 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001338 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001339
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001340 MarkUsed(Value);
1341
Daniel Dunbar3f872332009-07-28 16:08:33 +00001342 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001343 return TokError("unexpected token in assignment");
1344
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001345 // Error on assignment to '.'.
1346 if (Name == ".") {
1347 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1348 "(use '.space' or '.org').)"));
1349 }
1350
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001351 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001352 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001353
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001354 // Validate that the LHS is allowed to be a variable (either it has not been
1355 // used as a symbol, or it is an absolute symbol).
1356 MCSymbol *Sym = getContext().LookupSymbol(Name);
1357 if (Sym) {
1358 // Diagnose assignment to a label.
1359 //
1360 // FIXME: Diagnostics. Note the location of the definition as a label.
1361 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001362 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001363 ; // Allow redefinitions of undefined symbols only used in directives.
Nico Weber4c4c7322011-01-28 03:04:41 +00001364 else if (!Sym->isUndefined() && (!Sym->isAbsolute() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001365 return Error(EqualLoc, "redefinition of '" + Name + "'");
1366 else if (!Sym->isVariable())
1367 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001368 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001369 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1370 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001371
1372 // Don't count these checks as uses.
1373 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001374 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001375 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001376
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001377 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001378
1379 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001380 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001381
1382 return false;
1383}
1384
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001385/// ParseIdentifier:
1386/// ::= identifier
1387/// ::= string
1388bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001389 // The assembler has relaxed rules for accepting identifiers, in particular we
1390 // allow things like '.globl $foo', which would normally be separate
1391 // tokens. At this level, we have already lexed so we cannot (currently)
1392 // handle this as a context dependent token, instead we detect adjacent tokens
1393 // and return the combined identifier.
1394 if (Lexer.is(AsmToken::Dollar)) {
1395 SMLoc DollarLoc = getLexer().getLoc();
1396
1397 // Consume the dollar sign, and check for a following identifier.
1398 Lex();
1399 if (Lexer.isNot(AsmToken::Identifier))
1400 return true;
1401
1402 // We have a '$' followed by an identifier, make sure they are adjacent.
1403 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1404 return true;
1405
1406 // Construct the joined identifier and consume the token.
1407 Res = StringRef(DollarLoc.getPointer(),
1408 getTok().getIdentifier().size() + 1);
1409 Lex();
1410 return false;
1411 }
1412
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001413 if (Lexer.isNot(AsmToken::Identifier) &&
1414 Lexer.isNot(AsmToken::String))
1415 return true;
1416
Sean Callanan18b83232010-01-19 21:44:56 +00001417 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001418
Sean Callanan79ed1a82010-01-19 20:22:31 +00001419 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001420
1421 return false;
1422}
1423
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001424/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001425/// ::= .equ identifier ',' expression
1426/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001427/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001428bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001429 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001430
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001431 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001432 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001433
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001434 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001435 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001436 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001437
Nico Weber4c4c7322011-01-28 03:04:41 +00001438 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001439}
1440
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001441bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001442 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001443
1444 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001445 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001446 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1447 if (Str[i] != '\\') {
1448 Data += Str[i];
1449 continue;
1450 }
1451
1452 // Recognize escaped characters. Note that this escape semantics currently
1453 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1454 ++i;
1455 if (i == e)
1456 return TokError("unexpected backslash at end of string");
1457
1458 // Recognize octal sequences.
1459 if ((unsigned) (Str[i] - '0') <= 7) {
1460 // Consume up to three octal characters.
1461 unsigned Value = Str[i] - '0';
1462
1463 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1464 ++i;
1465 Value = Value * 8 + (Str[i] - '0');
1466
1467 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1468 ++i;
1469 Value = Value * 8 + (Str[i] - '0');
1470 }
1471 }
1472
1473 if (Value > 255)
1474 return TokError("invalid octal escape sequence (out of range)");
1475
1476 Data += (unsigned char) Value;
1477 continue;
1478 }
1479
1480 // Otherwise recognize individual escapes.
1481 switch (Str[i]) {
1482 default:
1483 // Just reject invalid escape sequences for now.
1484 return TokError("invalid escape sequence (unrecognized character)");
1485
1486 case 'b': Data += '\b'; break;
1487 case 'f': Data += '\f'; break;
1488 case 'n': Data += '\n'; break;
1489 case 'r': Data += '\r'; break;
1490 case 't': Data += '\t'; break;
1491 case '"': Data += '"'; break;
1492 case '\\': Data += '\\'; break;
1493 }
1494 }
1495
1496 return false;
1497}
1498
Daniel Dunbara0d14262009-06-24 23:30:00 +00001499/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001500/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1501bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001502 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001503 CheckForValidSection();
1504
Daniel Dunbara0d14262009-06-24 23:30:00 +00001505 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001506 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001507 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001508
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001509 std::string Data;
1510 if (ParseEscapedString(Data))
1511 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001512
1513 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001514 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001515 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1516
Sean Callanan79ed1a82010-01-19 20:22:31 +00001517 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001518
1519 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001520 break;
1521
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001522 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001523 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001524 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001525 }
1526 }
1527
Sean Callanan79ed1a82010-01-19 20:22:31 +00001528 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001529 return false;
1530}
1531
1532/// ParseDirectiveValue
1533/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1534bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001535 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001536 CheckForValidSection();
1537
Daniel Dunbara0d14262009-06-24 23:30:00 +00001538 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001539 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001540 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001541 return true;
1542
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001543 // Special case constant expressions to match code generator.
1544 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001545 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001546 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001547 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001548
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001549 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001550 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001551
Daniel Dunbara0d14262009-06-24 23:30:00 +00001552 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001553 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001554 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001555 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001556 }
1557 }
1558
Sean Callanan79ed1a82010-01-19 20:22:31 +00001559 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001560 return false;
1561}
1562
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001563/// ParseDirectiveRealValue
1564/// ::= (.single | .double) [ expression (, expression)* ]
1565bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1566 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1567 CheckForValidSection();
1568
1569 for (;;) {
1570 // We don't truly support arithmetic on floating point expressions, so we
1571 // have to manually parse unary prefixes.
1572 bool IsNeg = false;
1573 if (getLexer().is(AsmToken::Minus)) {
1574 Lex();
1575 IsNeg = true;
1576 } else if (getLexer().is(AsmToken::Plus))
1577 Lex();
1578
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001579 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001580 getLexer().isNot(AsmToken::Real) &&
1581 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001582 return TokError("unexpected token in directive");
1583
1584 // Convert to an APFloat.
1585 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001586 StringRef IDVal = getTok().getString();
1587 if (getLexer().is(AsmToken::Identifier)) {
1588 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1589 Value = APFloat::getInf(Semantics);
1590 else if (!IDVal.compare_lower("nan"))
1591 Value = APFloat::getNaN(Semantics, false, ~0);
1592 else
1593 return TokError("invalid floating point literal");
1594 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001595 APFloat::opInvalidOp)
1596 return TokError("invalid floating point literal");
1597 if (IsNeg)
1598 Value.changeSign();
1599
1600 // Consume the numeric token.
1601 Lex();
1602
1603 // Emit the value as an integer.
1604 APInt AsInt = Value.bitcastToAPInt();
1605 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1606 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1607
1608 if (getLexer().is(AsmToken::EndOfStatement))
1609 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001610
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001611 if (getLexer().isNot(AsmToken::Comma))
1612 return TokError("unexpected token in directive");
1613 Lex();
1614 }
1615 }
1616
1617 Lex();
1618 return false;
1619}
1620
Daniel Dunbara0d14262009-06-24 23:30:00 +00001621/// ParseDirectiveSpace
1622/// ::= .space expression [ , expression ]
1623bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001624 CheckForValidSection();
1625
Daniel Dunbara0d14262009-06-24 23:30:00 +00001626 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001627 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001628 return true;
1629
1630 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001631 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1632 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001633 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001634 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001635
Daniel Dunbar475839e2009-06-29 20:37:27 +00001636 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001637 return true;
1638
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001639 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001640 return TokError("unexpected token in '.space' directive");
1641 }
1642
Sean Callanan79ed1a82010-01-19 20:22:31 +00001643 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001644
1645 if (NumBytes <= 0)
1646 return TokError("invalid number of bytes in '.space' directive");
1647
1648 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001649 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001650
1651 return false;
1652}
1653
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001654/// ParseDirectiveZero
1655/// ::= .zero expression
1656bool AsmParser::ParseDirectiveZero() {
1657 CheckForValidSection();
1658
1659 int64_t NumBytes;
1660 if (ParseAbsoluteExpression(NumBytes))
1661 return true;
1662
Rafael Espindolae452b172010-10-05 19:42:57 +00001663 int64_t Val = 0;
1664 if (getLexer().is(AsmToken::Comma)) {
1665 Lex();
1666 if (ParseAbsoluteExpression(Val))
1667 return true;
1668 }
1669
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001670 if (getLexer().isNot(AsmToken::EndOfStatement))
1671 return TokError("unexpected token in '.zero' directive");
1672
1673 Lex();
1674
Rafael Espindolae452b172010-10-05 19:42:57 +00001675 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001676
1677 return false;
1678}
1679
Daniel Dunbara0d14262009-06-24 23:30:00 +00001680/// ParseDirectiveFill
1681/// ::= .fill expression , expression , expression
1682bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001683 CheckForValidSection();
1684
Daniel Dunbara0d14262009-06-24 23:30:00 +00001685 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001686 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001687 return true;
1688
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001689 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001690 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001691 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001692
Daniel Dunbara0d14262009-06-24 23:30:00 +00001693 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001694 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001695 return true;
1696
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001697 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001698 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001699 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001700
Daniel Dunbara0d14262009-06-24 23:30:00 +00001701 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001702 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001703 return true;
1704
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001705 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001706 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001707
Sean Callanan79ed1a82010-01-19 20:22:31 +00001708 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001709
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001710 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1711 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001712
1713 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001714 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001715
1716 return false;
1717}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001718
1719/// ParseDirectiveOrg
1720/// ::= .org expression [ , expression ]
1721bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001722 CheckForValidSection();
1723
Daniel Dunbar821e3332009-08-31 08:09:28 +00001724 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001725 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001726 return true;
1727
1728 // Parse optional fill expression.
1729 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001730 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1731 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001732 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001733 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001734
Daniel Dunbar475839e2009-06-29 20:37:27 +00001735 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001736 return true;
1737
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001738 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001739 return TokError("unexpected token in '.org' directive");
1740 }
1741
Sean Callanan79ed1a82010-01-19 20:22:31 +00001742 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001743
1744 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1745 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001746 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001747
1748 return false;
1749}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001750
1751/// ParseDirectiveAlign
1752/// ::= {.align, ...} expression [ , expression [ , expression ]]
1753bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001754 CheckForValidSection();
1755
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001756 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001757 int64_t Alignment;
1758 if (ParseAbsoluteExpression(Alignment))
1759 return true;
1760
1761 SMLoc MaxBytesLoc;
1762 bool HasFillExpr = false;
1763 int64_t FillExpr = 0;
1764 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001765 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1766 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001767 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001768 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001769
1770 // The fill expression can be omitted while specifying a maximum number of
1771 // alignment bytes, e.g:
1772 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001773 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001774 HasFillExpr = true;
1775 if (ParseAbsoluteExpression(FillExpr))
1776 return true;
1777 }
1778
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001779 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1780 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001781 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001782 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001783
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001784 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001785 if (ParseAbsoluteExpression(MaxBytesToFill))
1786 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001787
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001788 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001789 return TokError("unexpected token in directive");
1790 }
1791 }
1792
Sean Callanan79ed1a82010-01-19 20:22:31 +00001793 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001794
Daniel Dunbar648ac512010-05-17 21:54:30 +00001795 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001796 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001797
1798 // Compute alignment in bytes.
1799 if (IsPow2) {
1800 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001801 if (Alignment >= 32) {
1802 Error(AlignmentLoc, "invalid alignment value");
1803 Alignment = 31;
1804 }
1805
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001806 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001807 }
1808
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001809 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001810 if (MaxBytesLoc.isValid()) {
1811 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001812 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1813 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001814 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001815 }
1816
1817 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001818 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1819 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001820 MaxBytesToFill = 0;
1821 }
1822 }
1823
Daniel Dunbar648ac512010-05-17 21:54:30 +00001824 // Check whether we should use optimal code alignment for this .align
1825 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001826 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001827 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1828 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001829 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001830 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001831 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001832 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1833 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001834 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001835
1836 return false;
1837}
1838
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001839/// ParseDirectiveSymbolAttribute
1840/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001841bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001842 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001843 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001844 StringRef Name;
1845
1846 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001847 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001848
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001849 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001850
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001851 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001852
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001853 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001854 break;
1855
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001856 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001857 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001858 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001859 }
1860 }
1861
Sean Callanan79ed1a82010-01-19 20:22:31 +00001862 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001863 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001864}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001865
1866/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001867/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1868bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001869 CheckForValidSection();
1870
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001871 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001872 StringRef Name;
1873 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001874 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001875
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001876 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001877 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001878
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001879 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001880 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001881 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001882
1883 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001884 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001885 if (ParseAbsoluteExpression(Size))
1886 return true;
1887
1888 int64_t Pow2Alignment = 0;
1889 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001890 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001891 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001892 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001893 if (ParseAbsoluteExpression(Pow2Alignment))
1894 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001895
Chris Lattner258281d2010-01-19 06:22:22 +00001896 // If this target takes alignments in bytes (not log) validate and convert.
1897 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1898 if (!isPowerOf2_64(Pow2Alignment))
1899 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1900 Pow2Alignment = Log2_64(Pow2Alignment);
1901 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001902 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001903
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001904 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001905 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001906
Sean Callanan79ed1a82010-01-19 20:22:31 +00001907 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001908
Chris Lattner1fc3d752009-07-09 17:25:12 +00001909 // NOTE: a size of zero for a .comm should create a undefined symbol
1910 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001911 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001912 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1913 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001914
Eric Christopherc260a3e2010-05-14 01:38:54 +00001915 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001916 // may internally end up wanting an alignment in bytes.
1917 // FIXME: Diagnose overflow.
1918 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001919 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1920 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001921
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001922 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001923 return Error(IDLoc, "invalid symbol redefinition");
1924
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001925 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001926 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001927 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001928 getStreamer().EmitZerofill(Ctx.getMachOSection(
1929 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1930 0, SectionKind::getBSS()),
1931 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001932 return false;
1933 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001934
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001935 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001936 return false;
1937}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001938
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001939/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001940/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001941bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001942 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001943 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001944
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001945 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001946 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001947 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001948
Sean Callanan79ed1a82010-01-19 20:22:31 +00001949 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001950
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001951 if (Str.empty())
1952 Error(Loc, ".abort detected. Assembly stopping.");
1953 else
1954 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001955 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001956
1957 return false;
1958}
Kevin Enderby71148242009-07-14 21:35:03 +00001959
Kevin Enderby1f049b22009-07-14 23:21:55 +00001960/// ParseDirectiveInclude
1961/// ::= .include "filename"
1962bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001963 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001964 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001965
Sean Callanan18b83232010-01-19 21:44:56 +00001966 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001967 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001968 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001969
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001970 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001971 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001972
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001973 // Strip the quotes.
1974 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001975
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001976 // Attempt to switch the lexer to the included file before consuming the end
1977 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001978 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001979 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001980 return true;
1981 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001982
1983 return false;
1984}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001985
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001986/// ParseDirectiveIf
1987/// ::= .if expression
1988bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001989 TheCondStack.push_back(TheCondState);
1990 TheCondState.TheCond = AsmCond::IfCond;
1991 if(TheCondState.Ignore) {
1992 EatToEndOfStatement();
1993 }
1994 else {
1995 int64_t ExprValue;
1996 if (ParseAbsoluteExpression(ExprValue))
1997 return true;
1998
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001999 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002000 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002001
Sean Callanan79ed1a82010-01-19 20:22:31 +00002002 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002003
2004 TheCondState.CondMet = ExprValue;
2005 TheCondState.Ignore = !TheCondState.CondMet;
2006 }
2007
2008 return false;
2009}
2010
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002011bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2012 StringRef Name;
2013 TheCondStack.push_back(TheCondState);
2014 TheCondState.TheCond = AsmCond::IfCond;
2015
2016 if (TheCondState.Ignore) {
2017 EatToEndOfStatement();
2018 } else {
2019 if (ParseIdentifier(Name))
2020 return TokError("expected identifier after '.ifdef'");
2021
2022 Lex();
2023
2024 MCSymbol *Sym = getContext().LookupSymbol(Name);
2025
2026 if (expect_defined)
2027 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2028 else
2029 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2030 TheCondState.Ignore = !TheCondState.CondMet;
2031 }
2032
2033 return false;
2034}
2035
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002036/// ParseDirectiveElseIf
2037/// ::= .elseif expression
2038bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2039 if (TheCondState.TheCond != AsmCond::IfCond &&
2040 TheCondState.TheCond != AsmCond::ElseIfCond)
2041 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2042 " an .elseif");
2043 TheCondState.TheCond = AsmCond::ElseIfCond;
2044
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002045 bool LastIgnoreState = false;
2046 if (!TheCondStack.empty())
2047 LastIgnoreState = TheCondStack.back().Ignore;
2048 if (LastIgnoreState || TheCondState.CondMet) {
2049 TheCondState.Ignore = true;
2050 EatToEndOfStatement();
2051 }
2052 else {
2053 int64_t ExprValue;
2054 if (ParseAbsoluteExpression(ExprValue))
2055 return true;
2056
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002057 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002058 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002059
Sean Callanan79ed1a82010-01-19 20:22:31 +00002060 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002061 TheCondState.CondMet = ExprValue;
2062 TheCondState.Ignore = !TheCondState.CondMet;
2063 }
2064
2065 return false;
2066}
2067
2068/// ParseDirectiveElse
2069/// ::= .else
2070bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002071 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002072 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002073
Sean Callanan79ed1a82010-01-19 20:22:31 +00002074 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002075
2076 if (TheCondState.TheCond != AsmCond::IfCond &&
2077 TheCondState.TheCond != AsmCond::ElseIfCond)
2078 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2079 ".elseif");
2080 TheCondState.TheCond = AsmCond::ElseCond;
2081 bool LastIgnoreState = false;
2082 if (!TheCondStack.empty())
2083 LastIgnoreState = TheCondStack.back().Ignore;
2084 if (LastIgnoreState || TheCondState.CondMet)
2085 TheCondState.Ignore = true;
2086 else
2087 TheCondState.Ignore = false;
2088
2089 return false;
2090}
2091
2092/// ParseDirectiveEndIf
2093/// ::= .endif
2094bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002095 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002096 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002097
Sean Callanan79ed1a82010-01-19 20:22:31 +00002098 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002099
2100 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2101 TheCondStack.empty())
2102 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2103 ".else");
2104 if (!TheCondStack.empty()) {
2105 TheCondState = TheCondStack.back();
2106 TheCondStack.pop_back();
2107 }
2108
2109 return false;
2110}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002111
2112/// ParseDirectiveFile
2113/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002114bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002115 // FIXME: I'm not sure what this is.
2116 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002117 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002118 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002119 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002120 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002121
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002122 if (FileNumber < 1)
2123 return TokError("file number less than one");
2124 }
2125
Daniel Dunbareceec052010-07-12 17:45:27 +00002126 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002127 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002128
Chris Lattnerd32e8032010-01-25 19:02:58 +00002129 StringRef Filename = getTok().getString();
2130 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002131 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002132
Daniel Dunbareceec052010-07-12 17:45:27 +00002133 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002134 return TokError("unexpected token in '.file' directive");
2135
Chris Lattnerd32e8032010-01-25 19:02:58 +00002136 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002137 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002138 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002139 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002140 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002141 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002142
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002143 return false;
2144}
2145
2146/// ParseDirectiveLine
2147/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002148bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002149 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2150 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002151 return TokError("unexpected token in '.line' directive");
2152
Sean Callanan18b83232010-01-19 21:44:56 +00002153 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002154 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002155 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002156
2157 // FIXME: Do something with the .line.
2158 }
2159
Daniel Dunbareceec052010-07-12 17:45:27 +00002160 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002161 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002162
2163 return false;
2164}
2165
2166
2167/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002168/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002169/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2170/// The first number is a file number, must have been previously assigned with
2171/// a .file directive, the second number is the line number and optionally the
2172/// third number is a column position (zero if not specified). The remaining
2173/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002174bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002175
Daniel Dunbareceec052010-07-12 17:45:27 +00002176 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002177 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002178 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002179 if (FileNumber < 1)
2180 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002181 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002182 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002183 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002184
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002185 int64_t LineNumber = 0;
2186 if (getLexer().is(AsmToken::Integer)) {
2187 LineNumber = getTok().getIntVal();
2188 if (LineNumber < 1)
2189 return TokError("line number less than one in '.loc' directive");
2190 Lex();
2191 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002192
2193 int64_t ColumnPos = 0;
2194 if (getLexer().is(AsmToken::Integer)) {
2195 ColumnPos = getTok().getIntVal();
2196 if (ColumnPos < 0)
2197 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002198 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002199 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002200
Kevin Enderbyc0957932010-09-30 16:52:03 +00002201 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002202 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002203 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002204 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2205 for (;;) {
2206 if (getLexer().is(AsmToken::EndOfStatement))
2207 break;
2208
2209 StringRef Name;
2210 SMLoc Loc = getTok().getLoc();
2211 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002212 return TokError("unexpected token in '.loc' directive");
2213
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002214 if (Name == "basic_block")
2215 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2216 else if (Name == "prologue_end")
2217 Flags |= DWARF2_FLAG_PROLOGUE_END;
2218 else if (Name == "epilogue_begin")
2219 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2220 else if (Name == "is_stmt") {
2221 SMLoc Loc = getTok().getLoc();
2222 const MCExpr *Value;
2223 if (getParser().ParseExpression(Value))
2224 return true;
2225 // The expression must be the constant 0 or 1.
2226 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2227 int Value = MCE->getValue();
2228 if (Value == 0)
2229 Flags &= ~DWARF2_FLAG_IS_STMT;
2230 else if (Value == 1)
2231 Flags |= DWARF2_FLAG_IS_STMT;
2232 else
2233 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002234 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002235 else {
2236 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2237 }
2238 }
2239 else if (Name == "isa") {
2240 SMLoc Loc = getTok().getLoc();
2241 const MCExpr *Value;
2242 if (getParser().ParseExpression(Value))
2243 return true;
2244 // The expression must be a constant greater or equal to 0.
2245 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2246 int Value = MCE->getValue();
2247 if (Value < 0)
2248 return Error(Loc, "isa number less than zero");
2249 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002250 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002251 else {
2252 return Error(Loc, "isa number not a constant value");
2253 }
2254 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002255 else if (Name == "discriminator") {
2256 if (getParser().ParseAbsoluteExpression(Discriminator))
2257 return true;
2258 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002259 else {
2260 return Error(Loc, "unknown sub-directive in '.loc' directive");
2261 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002262
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002263 if (getLexer().is(AsmToken::EndOfStatement))
2264 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002265 }
2266 }
2267
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002268 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2269 Isa, Discriminator);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002270
2271 return false;
2272}
2273
Daniel Dunbar138abae2010-10-16 04:56:42 +00002274/// ParseDirectiveStabs
2275/// ::= .stabs string, number, number, number
2276bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2277 SMLoc DirectiveLoc) {
2278 return TokError("unsupported directive '" + Directive + "'");
2279}
2280
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002281/// ParseDirectiveCFIStartProc
2282/// ::= .cfi_startproc
2283bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2284 SMLoc DirectiveLoc) {
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002285 return getStreamer().EmitCFIStartProc();
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002286}
2287
2288/// ParseDirectiveCFIEndProc
2289/// ::= .cfi_endproc
2290bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002291 return getStreamer().EmitCFIEndProc();
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002292}
2293
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002294/// ParseRegisterOrRegisterNumber - parse register name or number.
2295bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2296 SMLoc DirectiveLoc) {
2297 unsigned RegNo;
2298
2299 if (getLexer().is(AsmToken::Percent)) {
2300 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2301 DirectiveLoc))
2302 return true;
2303 Register = getContext().getTargetAsmInfo().getDwarfRegNum(RegNo, true);
2304 } else
2305 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002306
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002307 return false;
2308}
2309
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002310/// ParseDirectiveCFIDefCfa
2311/// ::= .cfi_def_cfa register, offset
2312bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2313 SMLoc DirectiveLoc) {
2314 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002315 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002316 return true;
2317
2318 if (getLexer().isNot(AsmToken::Comma))
2319 return TokError("unexpected token in directive");
2320 Lex();
2321
2322 int64_t Offset = 0;
2323 if (getParser().ParseAbsoluteExpression(Offset))
2324 return true;
2325
2326 return getStreamer().EmitCFIDefCfa(Register, Offset);
2327}
2328
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002329/// ParseDirectiveCFIDefCfaOffset
2330/// ::= .cfi_def_cfa_offset offset
2331bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2332 SMLoc DirectiveLoc) {
2333 int64_t Offset = 0;
2334 if (getParser().ParseAbsoluteExpression(Offset))
2335 return true;
2336
Rafael Espindola53abbe52011-04-11 20:29:16 +00002337 getParser().setLastOffset(Offset);
2338
2339 return getStreamer().EmitCFIDefCfaOffset(Offset);
2340}
2341
2342/// ParseDirectiveCFIAdjustCfaOffset
2343/// ::= .cfi_adjust_cfa_offset adjustment
2344bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2345 SMLoc DirectiveLoc) {
2346 int64_t Adjustment = 0;
2347 if (getParser().ParseAbsoluteExpression(Adjustment))
2348 return true;
2349
2350 int64_t Offset = getParser().adjustLastOffset(Adjustment);
2351
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002352 return getStreamer().EmitCFIDefCfaOffset(Offset);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002353}
2354
2355/// ParseDirectiveCFIDefCfaRegister
2356/// ::= .cfi_def_cfa_register register
2357bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2358 SMLoc DirectiveLoc) {
2359 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002360 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002361 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002362
2363 return getStreamer().EmitCFIDefCfaRegister(Register);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002364}
2365
2366/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002367/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002368bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2369 int64_t Register = 0;
2370 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002371
2372 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002373 return true;
2374
2375 if (getLexer().isNot(AsmToken::Comma))
2376 return TokError("unexpected token in directive");
2377 Lex();
2378
2379 if (getParser().ParseAbsoluteExpression(Offset))
2380 return true;
2381
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002382 return getStreamer().EmitCFIOffset(Register, Offset);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002383}
2384
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002385/// ParseDirectiveCFIRelOffset
2386/// ::= .cfi_rel_offset register, offset
2387bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2388 SMLoc DirectiveLoc) {
2389 int64_t Register = 0;
2390
2391 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2392 return true;
2393
2394 if (getLexer().isNot(AsmToken::Comma))
2395 return TokError("unexpected token in directive");
2396 Lex();
2397
2398 int64_t Offset = 0;
2399 if (getParser().ParseAbsoluteExpression(Offset))
2400 return true;
2401
Rafael Espindola25f492e2011-04-12 16:12:03 +00002402 getStreamer().EmitCFIRelOffset(Register, Offset);
2403 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002404}
2405
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002406static bool isValidEncoding(int64_t Encoding) {
2407 if (Encoding & ~0xff)
2408 return false;
2409
2410 if (Encoding == dwarf::DW_EH_PE_omit)
2411 return true;
2412
2413 const unsigned Format = Encoding & 0xf;
2414 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2415 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2416 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2417 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2418 return false;
2419
Rafael Espindolacaf11582010-12-29 04:31:26 +00002420 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002421 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002422 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002423 return false;
2424
2425 return true;
2426}
2427
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002428/// ParseDirectiveCFIPersonalityOrLsda
2429/// ::= .cfi_personality encoding, [symbol_name]
2430/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002431bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002432 SMLoc DirectiveLoc) {
2433 int64_t Encoding = 0;
2434 if (getParser().ParseAbsoluteExpression(Encoding))
2435 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002436 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002437 return false;
2438
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002439 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002440 return TokError("unsupported encoding.");
2441
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002442 if (getLexer().isNot(AsmToken::Comma))
2443 return TokError("unexpected token in directive");
2444 Lex();
2445
2446 StringRef Name;
2447 if (getParser().ParseIdentifier(Name))
2448 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002449
2450 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2451
2452 if (IDVal == ".cfi_personality")
Rafael Espindola3a83c402010-12-27 00:36:05 +00002453 return getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002454 else {
2455 assert(IDVal == ".cfi_lsda");
Rafael Espindolabdc31672010-12-27 15:56:22 +00002456 return getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002457 }
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002458}
2459
Rafael Espindolafe024d02010-12-28 18:36:23 +00002460/// ParseDirectiveCFIRememberState
2461/// ::= .cfi_remember_state
2462bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2463 SMLoc DirectiveLoc) {
2464 return getStreamer().EmitCFIRememberState();
2465}
2466
2467/// ParseDirectiveCFIRestoreState
2468/// ::= .cfi_remember_state
2469bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2470 SMLoc DirectiveLoc) {
2471 return getStreamer().EmitCFIRestoreState();
2472}
2473
Rafael Espindolac5754392011-04-12 15:31:05 +00002474/// ParseDirectiveCFISameValue
2475/// ::= .cfi_same_value register
2476bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2477 SMLoc DirectiveLoc) {
2478 int64_t Register = 0;
2479
2480 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2481 return true;
2482
2483 getStreamer().EmitCFISameValue(Register);
2484
2485 return false;
2486}
2487
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002488/// ParseDirectiveMacrosOnOff
2489/// ::= .macros_on
2490/// ::= .macros_off
2491bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2492 SMLoc DirectiveLoc) {
2493 if (getLexer().isNot(AsmToken::EndOfStatement))
2494 return Error(getLexer().getLoc(),
2495 "unexpected token in '" + Directive + "' directive");
2496
2497 getParser().MacrosEnabled = Directive == ".macros_on";
2498
2499 return false;
2500}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002501
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002502/// ParseDirectiveMacro
2503/// ::= .macro name
2504bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2505 SMLoc DirectiveLoc) {
2506 StringRef Name;
2507 if (getParser().ParseIdentifier(Name))
2508 return TokError("expected identifier in directive");
2509
2510 if (getLexer().isNot(AsmToken::EndOfStatement))
2511 return TokError("unexpected token in '.macro' directive");
2512
2513 // Eat the end of statement.
2514 Lex();
2515
2516 AsmToken EndToken, StartToken = getTok();
2517
2518 // Lex the macro definition.
2519 for (;;) {
2520 // Check whether we have reached the end of the file.
2521 if (getLexer().is(AsmToken::Eof))
2522 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2523
2524 // Otherwise, check whether we have reach the .endmacro.
2525 if (getLexer().is(AsmToken::Identifier) &&
2526 (getTok().getIdentifier() == ".endm" ||
2527 getTok().getIdentifier() == ".endmacro")) {
2528 EndToken = getTok();
2529 Lex();
2530 if (getLexer().isNot(AsmToken::EndOfStatement))
2531 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2532 "' directive");
2533 break;
2534 }
2535
2536 // Otherwise, scan til the end of the statement.
2537 getParser().EatToEndOfStatement();
2538 }
2539
2540 if (getParser().MacroMap.lookup(Name)) {
2541 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2542 }
2543
2544 const char *BodyStart = StartToken.getLoc().getPointer();
2545 const char *BodyEnd = EndToken.getLoc().getPointer();
2546 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2547 getParser().MacroMap[Name] = new Macro(Name, Body);
2548 return false;
2549}
2550
2551/// ParseDirectiveEndMacro
2552/// ::= .endm
2553/// ::= .endmacro
2554bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2555 SMLoc DirectiveLoc) {
2556 if (getLexer().isNot(AsmToken::EndOfStatement))
2557 return TokError("unexpected token in '" + Directive + "' directive");
2558
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002559 // If we are inside a macro instantiation, terminate the current
2560 // instantiation.
2561 if (!getParser().ActiveMacros.empty()) {
2562 getParser().HandleMacroExit();
2563 return false;
2564 }
2565
2566 // Otherwise, this .endmacro is a stray entry in the file; well formed
2567 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002568 return TokError("unexpected '" + Directive + "' in file, "
2569 "no current macro definition");
2570}
2571
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002572bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002573 getParser().CheckForValidSection();
2574
2575 const MCExpr *Value;
2576
2577 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002578 return true;
2579
2580 if (getLexer().isNot(AsmToken::EndOfStatement))
2581 return TokError("unexpected token in directive");
2582
2583 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002584 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002585 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002586 getStreamer().EmitULEB128Value(Value);
2587
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002588 return false;
2589}
2590
2591
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002592/// \brief Create an MCAsmParser instance.
2593MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2594 MCContext &C, MCStreamer &Out,
2595 const MCAsmInfo &MAI) {
2596 return new AsmParser(T, SM, C, Out, MAI);
2597}