blob: c4a83e5df90c4f250d91a1242cf6afe9957d031d [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 }
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000152 int64_t getLastOffset() {
153 return LastOffset;
154 }
Rafael Espindola53abbe52011-04-11 20:29:16 +0000155 void setLastOffset(int64_t Offset) {
156 LastOffset = Offset;
157 }
158
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000159private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000160 void CheckForValidSection();
161
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000162 bool ParseStatement();
163
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000164 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
165 void HandleMacroExit();
166
167 void PrintMacroInstantiations();
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000168 void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type) const {
169 SrcMgr.PrintMessage(Loc, Msg, Type);
170 }
171
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000172 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
173 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000174
175 /// \brief Reset the current lexer position to that given by \arg Loc. The
176 /// current token is not set; clients should ensure Lex() is called
177 /// subsequently.
178 void JumpToLoc(SMLoc Loc);
179
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000180 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000181
182 /// \brief Parse up to the end of statement and a return the contents from the
183 /// current token until the end of the statement; the current token on exit
184 /// will be either the EndOfStatement or EOF.
185 StringRef ParseStringToEndOfStatement();
186
Nico Weber4c4c7322011-01-28 03:04:41 +0000187 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000188
189 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
190 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
191 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000192 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000193
194 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
195 /// and set \arg Res to the identifier contents.
196 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000197
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000198 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000199
200 // ".ascii", ".asciiz", ".string"
201 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000202 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000203 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000204 bool ParseDirectiveFill(); // ".fill"
205 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000206 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000207 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000208 bool ParseDirectiveOrg(); // ".org"
209 // ".align{,32}", ".p2align{,w,l}"
210 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
211
212 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
213 /// accepts a single symbol (which should be a label or an external).
214 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000215
216 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
217
218 bool ParseDirectiveAbort(); // ".abort"
219 bool ParseDirectiveInclude(); // ".include"
220
221 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000222 // ".ifdef" or ".ifndef", depending on expect_defined
223 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000224 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
225 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
226 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
227
228 /// ParseEscapedString - Parse the current token as a string which may include
229 /// escaped characters and return the string contents.
230 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000231
232 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
233 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000234};
235
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000236/// \brief Generic implementations of directive handling, etc. which is shared
237/// (or the default, at least) for all assembler parser.
238class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000239 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
240 void AddDirectiveHandler(StringRef Directive) {
241 getParser().AddDirectiveHandler(this, Directive,
242 HandleDirective<GenericAsmParser, Handler>);
243 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000244public:
245 GenericAsmParser() {}
246
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000247 AsmParser &getParser() {
248 return (AsmParser&) this->MCAsmParserExtension::getParser();
249 }
250
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000251 virtual void Initialize(MCAsmParser &Parser) {
252 // Call the base implementation.
253 this->MCAsmParserExtension::Initialize(Parser);
254
255 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000256 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
257 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
258 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000259 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000260
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000261 // CFI directives.
262 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
263 ".cfi_startproc");
264 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
265 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000266 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
267 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000268 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
269 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000270 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
271 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000272 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
273 ".cfi_def_cfa_register");
274 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
275 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000276 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
277 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000278 AddDirectiveHandler<
279 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
280 AddDirectiveHandler<
281 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000282 AddDirectiveHandler<
283 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
284 AddDirectiveHandler<
285 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000286 AddDirectiveHandler<
287 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000288
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000289 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000290 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
291 ".macros_on");
292 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
293 ".macros_off");
294 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
295 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
296 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000297
298 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
299 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000300 }
301
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000302 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
303
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000304 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
305 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
306 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000307 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000308 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
309 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000310 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000311 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000312 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000313 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
314 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000315 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000316 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000317 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
318 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000319 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000320
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000321 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000322 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
323 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000324
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000325 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000326};
327
328}
329
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000330namespace llvm {
331
332extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000333extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000334extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000335
336}
337
Chris Lattneraaec2052010-01-19 19:46:13 +0000338enum { DEFAULT_ADDRSPACE = 0 };
339
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000340AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
341 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000342 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Rafael Espindola53abbe52011-04-11 20:29:16 +0000343 GenericParser(new GenericAsmParser), PlatformParser(0), LastOffset(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000344 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000345 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000346
347 // Initialize the generic parser.
348 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000349
350 // Initialize the platform / file format parser.
351 //
352 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
353 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000354 if (_MAI.hasMicrosoftFastStdCallMangling()) {
355 PlatformParser = createCOFFAsmParser();
356 PlatformParser->Initialize(*this);
357 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000358 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000359 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000360 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000361 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000362 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000363 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000364}
365
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000366AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000367 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
368
369 // Destroy any macros.
370 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
371 ie = MacroMap.end(); it != ie; ++it)
372 delete it->getValue();
373
Daniel Dunbare4749702010-07-12 18:12:02 +0000374 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000375 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000376}
377
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000378void AsmParser::PrintMacroInstantiations() {
379 // Print the active macro instantiation stack.
380 for (std::vector<MacroInstantiation*>::const_reverse_iterator
381 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
382 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
383 "note");
384}
385
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000386void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000387 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000388 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000389}
390
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000391bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000392 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000393 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000394 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000395 return true;
396}
397
Sean Callananfd0b0282010-01-21 00:19:58 +0000398bool AsmParser::EnterIncludeFile(const std::string &Filename) {
399 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
400 if (NewBuf == -1)
401 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000402
Sean Callananfd0b0282010-01-21 00:19:58 +0000403 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000404
Sean Callananfd0b0282010-01-21 00:19:58 +0000405 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000406
Sean Callananfd0b0282010-01-21 00:19:58 +0000407 return false;
408}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000409
410void AsmParser::JumpToLoc(SMLoc Loc) {
411 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
412 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
413}
414
Sean Callananfd0b0282010-01-21 00:19:58 +0000415const AsmToken &AsmParser::Lex() {
416 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000417
Sean Callananfd0b0282010-01-21 00:19:58 +0000418 if (tok->is(AsmToken::Eof)) {
419 // If this is the end of an included file, pop the parent file off the
420 // include stack.
421 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
422 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000423 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000424 tok = &Lexer.Lex();
425 }
426 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000427
Sean Callananfd0b0282010-01-21 00:19:58 +0000428 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000429 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000430
Sean Callananfd0b0282010-01-21 00:19:58 +0000431 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000432}
433
Chris Lattner79180e22010-04-05 23:15:42 +0000434bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000435 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000436 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000437 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000438
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000439 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000440 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000441
442 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000443 AsmCond StartingCondState = TheCondState;
444
Chris Lattnerb717fb02009-07-02 21:53:43 +0000445 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000446 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000447 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000448
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000449 // We had an error, validate that one was emitted and recover by skipping to
450 // the next line.
451 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000452 EatToEndOfStatement();
453 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000454
455 if (TheCondState.TheCond != StartingCondState.TheCond ||
456 TheCondState.Ignore != StartingCondState.Ignore)
457 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000458
459 // Check to see there are no empty DwarfFile slots.
460 const std::vector<MCDwarfFile *> &MCDwarfFiles =
461 getContext().getMCDwarfFiles();
462 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000463 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000464 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000465 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000466
Chris Lattner79180e22010-04-05 23:15:42 +0000467 // Finalize the output stream if there are no errors and if the client wants
468 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000469 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000470 Out.Finish();
471
Chris Lattnerb717fb02009-07-02 21:53:43 +0000472 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000473}
474
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000475void AsmParser::CheckForValidSection() {
476 if (!getStreamer().getCurrentSection()) {
477 TokError("expected section directive before assembly directive");
478 Out.SwitchSection(Ctx.getMachOSection(
479 "__TEXT", "__text",
480 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
481 0, SectionKind::getText()));
482 }
483}
484
Chris Lattner2cf5f142009-06-22 01:29:09 +0000485/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
486void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000487 while (Lexer.isNot(AsmToken::EndOfStatement) &&
488 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000489 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000490
Chris Lattner2cf5f142009-06-22 01:29:09 +0000491 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000492 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000493 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000494}
495
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000496StringRef AsmParser::ParseStringToEndOfStatement() {
497 const char *Start = getTok().getLoc().getPointer();
498
499 while (Lexer.isNot(AsmToken::EndOfStatement) &&
500 Lexer.isNot(AsmToken::Eof))
501 Lex();
502
503 const char *End = getTok().getLoc().getPointer();
504 return StringRef(Start, End - Start);
505}
Chris Lattnerc4193832009-06-22 05:51:26 +0000506
Chris Lattner74ec1a32009-06-22 06:32:03 +0000507/// ParseParenExpr - Parse a paren expression and return it.
508/// NOTE: This assumes the leading '(' has already been consumed.
509///
510/// parenexpr ::= expr)
511///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000512bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000513 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000514 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000515 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000516 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000517 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000518 return false;
519}
Chris Lattnerc4193832009-06-22 05:51:26 +0000520
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000521/// ParseBracketExpr - Parse a bracket expression and return it.
522/// NOTE: This assumes the leading '[' has already been consumed.
523///
524/// bracketexpr ::= expr]
525///
526bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
527 if (ParseExpression(Res)) return true;
528 if (Lexer.isNot(AsmToken::RBrac))
529 return TokError("expected ']' in brackets expression");
530 EndLoc = Lexer.getLoc();
531 Lex();
532 return false;
533}
534
Chris Lattner74ec1a32009-06-22 06:32:03 +0000535/// ParsePrimaryExpr - Parse a primary expression and return it.
536/// primaryexpr ::= (parenexpr
537/// primaryexpr ::= symbol
538/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000539/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000540/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000541bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000542 switch (Lexer.getKind()) {
543 default:
544 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000545 // If we have an error assume that we've already handled it.
546 case AsmToken::Error:
547 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000548 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000549 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000550 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000551 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000552 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000553 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000554 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000555 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000556 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000557 EndLoc = Lexer.getLoc();
558
559 StringRef Identifier;
560 if (ParseIdentifier(Identifier))
561 return false;
562
Daniel Dunbarfffff912009-10-16 01:34:54 +0000563 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000564 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000565 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000566
567 // Lookup the symbol variant if used.
568 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000569 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000570 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000571 if (Variant == MCSymbolRefExpr::VK_Invalid) {
572 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000573 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000574 }
575 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000576
Daniel Dunbarfffff912009-10-16 01:34:54 +0000577 // If this is an absolute variable reference, substitute it now to preserve
578 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000579 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000580 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000581 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000582
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000583 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000584 return false;
585 }
586
587 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000588 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000589 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000590 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000591 case AsmToken::Integer: {
592 SMLoc Loc = getTok().getLoc();
593 int64_t IntVal = getTok().getIntVal();
594 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000595 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000596 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000597 // Look for 'b' or 'f' following an Integer as a directional label
598 if (Lexer.getKind() == AsmToken::Identifier) {
599 StringRef IDVal = getTok().getString();
600 if (IDVal == "f" || IDVal == "b"){
601 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
602 IDVal == "f" ? 1 : 0);
603 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
604 getContext());
605 if(IDVal == "b" && Sym->isUndefined())
606 return Error(Loc, "invalid reference to undefined symbol");
607 EndLoc = Lexer.getLoc();
608 Lex(); // Eat identifier.
609 }
610 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000611 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000612 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000613 case AsmToken::Real: {
614 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000615 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000616 Res = MCConstantExpr::Create(IntVal, getContext());
617 Lex(); // Eat token.
618 return false;
619 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000620 case AsmToken::Dot: {
621 // This is a '.' reference, which references the current PC. Emit a
622 // temporary label to the streamer and refer to it.
623 MCSymbol *Sym = Ctx.CreateTempSymbol();
624 Out.EmitLabel(Sym);
625 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
626 EndLoc = Lexer.getLoc();
627 Lex(); // Eat identifier.
628 return false;
629 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000630 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000631 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000632 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000633 case AsmToken::LBrac:
634 if (!PlatformParser->HasBracketExpressions())
635 return TokError("brackets expression not supported on this target");
636 Lex(); // Eat the '['.
637 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000638 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000639 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000640 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000641 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000642 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000643 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000644 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000645 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000646 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000647 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000648 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000649 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000650 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000651 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000652 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000653 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000654 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000655 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000656 }
657}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000658
Chris Lattnerb4307b32010-01-15 19:28:38 +0000659bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000660 SMLoc EndLoc;
661 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000662}
663
Daniel Dunbarcceba832010-09-17 02:47:07 +0000664const MCExpr *
665AsmParser::ApplyModifierToExpr(const MCExpr *E,
666 MCSymbolRefExpr::VariantKind Variant) {
667 // Recurse over the given expression, rebuilding it to apply the given variant
668 // if there is exactly one symbol.
669 switch (E->getKind()) {
670 case MCExpr::Target:
671 case MCExpr::Constant:
672 return 0;
673
674 case MCExpr::SymbolRef: {
675 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
676
677 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
678 TokError("invalid variant on expression '" +
679 getTok().getIdentifier() + "' (already modified)");
680 return E;
681 }
682
683 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
684 }
685
686 case MCExpr::Unary: {
687 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
688 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
689 if (!Sub)
690 return 0;
691 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
692 }
693
694 case MCExpr::Binary: {
695 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
696 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
697 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
698
699 if (!LHS && !RHS)
700 return 0;
701
702 if (!LHS) LHS = BE->getLHS();
703 if (!RHS) RHS = BE->getRHS();
704
705 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
706 }
707 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000708
709 assert(0 && "Invalid expression kind!");
710 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000711}
712
Chris Lattner74ec1a32009-06-22 06:32:03 +0000713/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000714///
Chris Lattner74ec1a32009-06-22 06:32:03 +0000715/// expr ::= expr +,- expr -> lowest.
716/// expr ::= expr |,^,&,! expr -> middle.
717/// expr ::= expr *,/,%,<<,>> expr -> highest.
718/// expr ::= primaryexpr
719///
Chris Lattner54482b42010-01-15 19:39:23 +0000720bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000721 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000722 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000723 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
724 return true;
725
Daniel Dunbarcceba832010-09-17 02:47:07 +0000726 // As a special case, we support 'a op b @ modifier' by rewriting the
727 // expression to include the modifier. This is inefficient, but in general we
728 // expect users to use 'a@modifier op b'.
729 if (Lexer.getKind() == AsmToken::At) {
730 Lex();
731
732 if (Lexer.isNot(AsmToken::Identifier))
733 return TokError("unexpected symbol modifier following '@'");
734
735 MCSymbolRefExpr::VariantKind Variant =
736 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
737 if (Variant == MCSymbolRefExpr::VK_Invalid)
738 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
739
740 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
741 if (!ModifiedRes) {
742 return TokError("invalid modifier '" + getTok().getIdentifier() +
743 "' (no symbols present)");
744 return true;
745 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000746
Daniel Dunbarcceba832010-09-17 02:47:07 +0000747 Res = ModifiedRes;
748 Lex();
749 }
750
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000751 // Try to constant fold it up front, if possible.
752 int64_t Value;
753 if (Res->EvaluateAsAbsolute(Value))
754 Res = MCConstantExpr::Create(Value, getContext());
755
756 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000757}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000758
Chris Lattnerb4307b32010-01-15 19:28:38 +0000759bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000760 Res = 0;
761 return ParseParenExpr(Res, EndLoc) ||
762 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000763}
764
Daniel Dunbar475839e2009-06-29 20:37:27 +0000765bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000766 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000767
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000768 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000769 if (ParseExpression(Expr))
770 return true;
771
Daniel Dunbare00b0112009-10-16 01:57:52 +0000772 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000773 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000774
775 return false;
776}
777
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000778static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000779 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000780 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000781 default:
782 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000783
Daniel Dunbarcceba832010-09-17 02:47:07 +0000784 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000785 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000786 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000787 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000788 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000789 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000790 return 1;
791
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000792
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000793 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000794 //
795 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000796 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000797 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000798 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000799 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000800 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000801 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000802 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000803 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000804 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000805
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000806 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000807 case AsmToken::EqualEqual:
808 Kind = MCBinaryExpr::EQ;
809 return 3;
810 case AsmToken::ExclaimEqual:
811 case AsmToken::LessGreater:
812 Kind = MCBinaryExpr::NE;
813 return 3;
814 case AsmToken::Less:
815 Kind = MCBinaryExpr::LT;
816 return 3;
817 case AsmToken::LessEqual:
818 Kind = MCBinaryExpr::LTE;
819 return 3;
820 case AsmToken::Greater:
821 Kind = MCBinaryExpr::GT;
822 return 3;
823 case AsmToken::GreaterEqual:
824 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000825 return 3;
826
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000827 // High Intermediate Precedence: +, -
828 case AsmToken::Plus:
829 Kind = MCBinaryExpr::Add;
830 return 4;
831 case AsmToken::Minus:
832 Kind = MCBinaryExpr::Sub;
833 return 4;
834
Daniel Dunbar475839e2009-06-29 20:37:27 +0000835 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000836 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000837 Kind = MCBinaryExpr::Mul;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000838 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000839 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000840 Kind = MCBinaryExpr::Div;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000841 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000842 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000843 Kind = MCBinaryExpr::Mod;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000844 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000845 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000846 Kind = MCBinaryExpr::Shl;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000847 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000848 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000849 Kind = MCBinaryExpr::Shr;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000850 return 5;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000851 }
852}
853
854
855/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
856/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000857bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
858 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000859 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000860 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000861 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000862
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000863 // If the next token is lower precedence than we are allowed to eat, return
864 // successfully with what we ate already.
865 if (TokPrec < Precedence)
866 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000867
Sean Callanan79ed1a82010-01-19 20:22:31 +0000868 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000869
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000870 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000871 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000872 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000873
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000874 // If BinOp binds less tightly with RHS than the operator after RHS, let
875 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000876 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000877 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000878 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000879 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000880 }
881
Daniel Dunbar475839e2009-06-29 20:37:27 +0000882 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000883 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000884 }
885}
886
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000887
888
889
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000890/// ParseStatement:
891/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000892/// ::= Label* Directive ...Operands... EndOfStatement
893/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000894bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000895 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000896 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000897 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000898 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000899 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000900
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000901 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000902 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000903 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000904 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000905 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000906 // A full line comment is a '#' as the first token.
907 if (Lexer.is(AsmToken::Hash)) {
908 EatToEndOfStatement();
909 return false;
910 }
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000911
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000912 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000913 if (Lexer.is(AsmToken::Integer)) {
914 LocalLabelVal = getTok().getIntVal();
915 if (LocalLabelVal < 0) {
916 if (!TheCondState.Ignore)
917 return TokError("unexpected token at start of statement");
918 IDVal = "";
919 }
920 else {
921 IDVal = getTok().getString();
922 Lex(); // Consume the integer token to be used as an identifier token.
923 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000924 if (!TheCondState.Ignore)
925 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000926 }
927 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000928
929 } else if (Lexer.is(AsmToken::Dot)) {
930 // Treat '.' as a valid identifier in this context.
931 Lex();
932 IDVal = ".";
933
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000934 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000935 if (!TheCondState.Ignore)
936 return TokError("unexpected token at start of statement");
937 IDVal = "";
938 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000939
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000940
Chris Lattner7834fac2010-04-17 18:14:27 +0000941 // Handle conditional assembly here before checking for skipping. We
942 // have to do this so that .endif isn't skipped in a ".if 0" block for
943 // example.
944 if (IDVal == ".if")
945 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000946 if (IDVal == ".ifdef")
947 return ParseDirectiveIfdef(IDLoc, true);
948 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
949 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +0000950 if (IDVal == ".elseif")
951 return ParseDirectiveElseIf(IDLoc);
952 if (IDVal == ".else")
953 return ParseDirectiveElse(IDLoc);
954 if (IDVal == ".endif")
955 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000956
Chris Lattner7834fac2010-04-17 18:14:27 +0000957 // If we are in a ".if 0" block, ignore this statement.
958 if (TheCondState.Ignore) {
959 EatToEndOfStatement();
960 return false;
961 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000962
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000963 // FIXME: Recurse on local labels?
964
965 // See what kind of statement we have.
966 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000967 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000968 CheckForValidSection();
969
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000970 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000971 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000972
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000973 // Diagnose attempt to use '.' as a label.
974 if (IDVal == ".")
975 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
976
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000977 // Diagnose attempt to use a variable as a label.
978 //
979 // FIXME: Diagnostics. Note the location of the definition as a label.
980 // FIXME: This doesn't diagnose assignment to a symbol which has been
981 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000982 MCSymbol *Sym;
983 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000984 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000985 else
986 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000987 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000988 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000989
Daniel Dunbar959fd882009-08-26 22:13:22 +0000990 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000991 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000992
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000993 // Consume any end of statement token, if present, to avoid spurious
994 // AddBlankLine calls().
995 if (Lexer.is(AsmToken::EndOfStatement)) {
996 Lex();
997 if (Lexer.is(AsmToken::Eof))
998 return false;
999 }
1000
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001001 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001002 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001003
Daniel Dunbar3f872332009-07-28 16:08:33 +00001004 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001005 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001006 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001007
Nico Weber4c4c7322011-01-28 03:04:41 +00001008 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001009
1010 default: // Normal instruction or directive.
1011 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001012 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001013
1014 // If macros are enabled, check to see if this is a macro instantiation.
1015 if (MacrosEnabled)
1016 if (const Macro *M = MacroMap.lookup(IDVal))
1017 return HandleMacroEntry(IDVal, IDLoc, M);
1018
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001019 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001020 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001021 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001022 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001023 return ParseDirectiveSet(IDVal, true);
1024 if (IDVal == ".equiv")
1025 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001026
Daniel Dunbara0d14262009-06-24 23:30:00 +00001027 // Data directives
1028
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001029 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001030 return ParseDirectiveAscii(IDVal, false);
1031 if (IDVal == ".asciz" || IDVal == ".string")
1032 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001033
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001034 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001035 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001036 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001037 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001038 if (IDVal == ".value")
1039 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001040 if (IDVal == ".2byte")
1041 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001042 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001043 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001044 if (IDVal == ".int")
1045 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001046 if (IDVal == ".4byte")
1047 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001048 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001049 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001050 if (IDVal == ".8byte")
1051 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001052 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001053 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1054 if (IDVal == ".double")
1055 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001056
Eli Friedman5d68ec22010-07-19 04:17:25 +00001057 if (IDVal == ".align") {
1058 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1059 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1060 }
1061 if (IDVal == ".align32") {
1062 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1063 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1064 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001065 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001066 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001067 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001068 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001069 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001070 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001071 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001072 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001073 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001074 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001075 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001076 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1077
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001078 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001079 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001080
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001081 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001082 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001083 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001084 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001085 if (IDVal == ".zero")
1086 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001087
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001088 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001089
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001090 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001091 return ParseDirectiveSymbolAttribute(MCSA_Global);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +00001092 // ELF only? Should it be here?
1093 if (IDVal == ".local")
1094 return ParseDirectiveSymbolAttribute(MCSA_Local);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001095 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001096 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001097 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001098 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001099 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001100 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001101 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001102 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001103 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001104 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001105 if (IDVal == ".symbol_resolver")
1106 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001107 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001108 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001109 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001110 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001111 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001112 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001113 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001114 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001115 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001116 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001117 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001118 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001119 if (IDVal == ".weak_def_can_be_hidden")
1120 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001121
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001122 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001123 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001124 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001125 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001126
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001127 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001128 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001129 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001130 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001131
Roman Divackybb6d14f2011-01-31 21:19:43 +00001132 if (IDVal == ".code16" || IDVal == ".code32" || IDVal == ".code64")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001133 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001134
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001135 // Look up the handler in the handler table.
1136 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1137 DirectiveMap.lookup(IDVal);
1138 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001139 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001140
Kevin Enderby9c656452009-09-10 20:51:44 +00001141 // Target hook for parsing target specific directives.
1142 if (!getTargetParser().ParseDirective(ID))
1143 return false;
1144
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001145 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001146 EatToEndOfStatement();
1147 return false;
1148 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001149
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001150 CheckForValidSection();
1151
Chris Lattnera7f13542010-05-19 23:34:33 +00001152 // Canonicalize the opcode to lower case.
1153 SmallString<128> Opcode;
1154 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1155 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001156
Chris Lattner98986712010-01-14 22:21:20 +00001157 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001158 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001159 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001160
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001161 // Dump the parsed representation, if requested.
1162 if (getShowParsedOperands()) {
1163 SmallString<256> Str;
1164 raw_svector_ostream OS(Str);
1165 OS << "parsed instruction: [";
1166 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1167 if (i != 0)
1168 OS << ", ";
1169 ParsedOperands[i]->dump(OS);
1170 }
1171 OS << "]";
1172
1173 PrintMessage(IDLoc, OS.str(), "note");
1174 }
1175
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001176 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001177 if (!HadError)
1178 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1179 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001180
Chris Lattner98986712010-01-14 22:21:20 +00001181 // Free any parsed operands.
1182 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1183 delete ParsedOperands[i];
1184
Chris Lattnercbf8a982010-09-11 16:18:25 +00001185 // Don't skip the rest of the line, the instruction parser is responsible for
1186 // that.
1187 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001188}
Chris Lattner9a023f72009-06-24 04:43:34 +00001189
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001190MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1191 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001192 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
1193{
1194 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1195 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001196 SmallString<256> Buf;
1197 raw_svector_ostream OS(Buf);
1198
1199 StringRef Body = M->Body;
1200 while (!Body.empty()) {
1201 // Scan for the next substitution.
1202 std::size_t End = Body.size(), Pos = 0;
1203 for (; Pos != End; ++Pos) {
1204 // Check for a substitution or escape.
1205 if (Body[Pos] != '$' || Pos + 1 == End)
1206 continue;
1207
1208 char Next = Body[Pos + 1];
1209 if (Next == '$' || Next == 'n' || isdigit(Next))
1210 break;
1211 }
1212
1213 // Add the prefix.
1214 OS << Body.slice(0, Pos);
1215
1216 // Check if we reached the end.
1217 if (Pos == End)
1218 break;
1219
1220 switch (Body[Pos+1]) {
1221 // $$ => $
1222 case '$':
1223 OS << '$';
1224 break;
1225
1226 // $n => number of arguments
1227 case 'n':
1228 OS << A.size();
1229 break;
1230
1231 // $[0-9] => argument
1232 default: {
1233 // Missing arguments are ignored.
1234 unsigned Index = Body[Pos+1] - '0';
1235 if (Index >= A.size())
1236 break;
1237
1238 // Otherwise substitute with the token values, with spaces eliminated.
1239 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1240 ie = A[Index].end(); it != ie; ++it)
1241 OS << it->getString();
1242 break;
1243 }
1244 }
1245
1246 // Update the scan point.
1247 Body = Body.substr(Pos + 2);
1248 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001249
1250 // We include the .endmacro in the buffer as our queue to exit the macro
1251 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001252 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001253
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001254 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001255}
1256
1257bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1258 const Macro *M) {
1259 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1260 // this, although we should protect against infinite loops.
1261 if (ActiveMacros.size() == 20)
1262 return TokError("macros cannot be nested more than 20 levels deep");
1263
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001264 // Parse the macro instantiation arguments.
1265 std::vector<std::vector<AsmToken> > MacroArguments;
1266 MacroArguments.push_back(std::vector<AsmToken>());
1267 unsigned ParenLevel = 0;
1268 for (;;) {
1269 if (Lexer.is(AsmToken::Eof))
1270 return TokError("unexpected token in macro instantiation");
1271 if (Lexer.is(AsmToken::EndOfStatement))
1272 break;
1273
1274 // If we aren't inside parentheses and this is a comma, start a new token
1275 // list.
1276 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1277 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001278 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001279 // Adjust the current parentheses level.
1280 if (Lexer.is(AsmToken::LParen))
1281 ++ParenLevel;
1282 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1283 --ParenLevel;
1284
1285 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001286 MacroArguments.back().push_back(getTok());
1287 }
1288 Lex();
1289 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001290
1291 // Create the macro instantiation object and add to the current macro
1292 // instantiation stack.
1293 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001294 getTok().getLoc(),
1295 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001296 ActiveMacros.push_back(MI);
1297
1298 // Jump to the macro instantiation and prime the lexer.
1299 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1300 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1301 Lex();
1302
1303 return false;
1304}
1305
1306void AsmParser::HandleMacroExit() {
1307 // Jump to the EndOfStatement we should return to, and consume it.
1308 JumpToLoc(ActiveMacros.back()->ExitLoc);
1309 Lex();
1310
1311 // Pop the instantiation entry.
1312 delete ActiveMacros.back();
1313 ActiveMacros.pop_back();
1314}
1315
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001316static void MarkUsed(const MCExpr *Value) {
1317 switch (Value->getKind()) {
1318 case MCExpr::Binary:
1319 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1320 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1321 break;
1322 case MCExpr::Target:
1323 case MCExpr::Constant:
1324 break;
1325 case MCExpr::SymbolRef: {
1326 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1327 break;
1328 }
1329 case MCExpr::Unary:
1330 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1331 break;
1332 }
1333}
1334
Nico Weber4c4c7322011-01-28 03:04:41 +00001335bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001336 // FIXME: Use better location, we should use proper tokens.
1337 SMLoc EqualLoc = Lexer.getLoc();
1338
Daniel Dunbar821e3332009-08-31 08:09:28 +00001339 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001340 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001341 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001342
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001343 MarkUsed(Value);
1344
Daniel Dunbar3f872332009-07-28 16:08:33 +00001345 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001346 return TokError("unexpected token in assignment");
1347
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001348 // Error on assignment to '.'.
1349 if (Name == ".") {
1350 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1351 "(use '.space' or '.org').)"));
1352 }
1353
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001354 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001355 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001356
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001357 // Validate that the LHS is allowed to be a variable (either it has not been
1358 // used as a symbol, or it is an absolute symbol).
1359 MCSymbol *Sym = getContext().LookupSymbol(Name);
1360 if (Sym) {
1361 // Diagnose assignment to a label.
1362 //
1363 // FIXME: Diagnostics. Note the location of the definition as a label.
1364 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001365 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001366 ; // Allow redefinitions of undefined symbols only used in directives.
Nico Weber4c4c7322011-01-28 03:04:41 +00001367 else if (!Sym->isUndefined() && (!Sym->isAbsolute() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001368 return Error(EqualLoc, "redefinition of '" + Name + "'");
1369 else if (!Sym->isVariable())
1370 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001371 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001372 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1373 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001374
1375 // Don't count these checks as uses.
1376 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001377 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001378 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001379
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001380 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001381
1382 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001383 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001384
1385 return false;
1386}
1387
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001388/// ParseIdentifier:
1389/// ::= identifier
1390/// ::= string
1391bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001392 // The assembler has relaxed rules for accepting identifiers, in particular we
1393 // allow things like '.globl $foo', which would normally be separate
1394 // tokens. At this level, we have already lexed so we cannot (currently)
1395 // handle this as a context dependent token, instead we detect adjacent tokens
1396 // and return the combined identifier.
1397 if (Lexer.is(AsmToken::Dollar)) {
1398 SMLoc DollarLoc = getLexer().getLoc();
1399
1400 // Consume the dollar sign, and check for a following identifier.
1401 Lex();
1402 if (Lexer.isNot(AsmToken::Identifier))
1403 return true;
1404
1405 // We have a '$' followed by an identifier, make sure they are adjacent.
1406 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1407 return true;
1408
1409 // Construct the joined identifier and consume the token.
1410 Res = StringRef(DollarLoc.getPointer(),
1411 getTok().getIdentifier().size() + 1);
1412 Lex();
1413 return false;
1414 }
1415
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001416 if (Lexer.isNot(AsmToken::Identifier) &&
1417 Lexer.isNot(AsmToken::String))
1418 return true;
1419
Sean Callanan18b83232010-01-19 21:44:56 +00001420 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001421
Sean Callanan79ed1a82010-01-19 20:22:31 +00001422 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001423
1424 return false;
1425}
1426
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001427/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001428/// ::= .equ identifier ',' expression
1429/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001430/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001431bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001432 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001433
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001434 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001435 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001436
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001437 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001438 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001439 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001440
Nico Weber4c4c7322011-01-28 03:04:41 +00001441 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001442}
1443
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001444bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001445 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001446
1447 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001448 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001449 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1450 if (Str[i] != '\\') {
1451 Data += Str[i];
1452 continue;
1453 }
1454
1455 // Recognize escaped characters. Note that this escape semantics currently
1456 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1457 ++i;
1458 if (i == e)
1459 return TokError("unexpected backslash at end of string");
1460
1461 // Recognize octal sequences.
1462 if ((unsigned) (Str[i] - '0') <= 7) {
1463 // Consume up to three octal characters.
1464 unsigned Value = Str[i] - '0';
1465
1466 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1467 ++i;
1468 Value = Value * 8 + (Str[i] - '0');
1469
1470 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1471 ++i;
1472 Value = Value * 8 + (Str[i] - '0');
1473 }
1474 }
1475
1476 if (Value > 255)
1477 return TokError("invalid octal escape sequence (out of range)");
1478
1479 Data += (unsigned char) Value;
1480 continue;
1481 }
1482
1483 // Otherwise recognize individual escapes.
1484 switch (Str[i]) {
1485 default:
1486 // Just reject invalid escape sequences for now.
1487 return TokError("invalid escape sequence (unrecognized character)");
1488
1489 case 'b': Data += '\b'; break;
1490 case 'f': Data += '\f'; break;
1491 case 'n': Data += '\n'; break;
1492 case 'r': Data += '\r'; break;
1493 case 't': Data += '\t'; break;
1494 case '"': Data += '"'; break;
1495 case '\\': Data += '\\'; break;
1496 }
1497 }
1498
1499 return false;
1500}
1501
Daniel Dunbara0d14262009-06-24 23:30:00 +00001502/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001503/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1504bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001505 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001506 CheckForValidSection();
1507
Daniel Dunbara0d14262009-06-24 23:30:00 +00001508 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001509 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001510 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001511
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001512 std::string Data;
1513 if (ParseEscapedString(Data))
1514 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001515
1516 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001517 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001518 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1519
Sean Callanan79ed1a82010-01-19 20:22:31 +00001520 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001521
1522 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001523 break;
1524
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001525 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001526 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001527 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001528 }
1529 }
1530
Sean Callanan79ed1a82010-01-19 20:22:31 +00001531 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001532 return false;
1533}
1534
1535/// ParseDirectiveValue
1536/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1537bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001538 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001539 CheckForValidSection();
1540
Daniel Dunbara0d14262009-06-24 23:30:00 +00001541 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001542 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001543 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001544 return true;
1545
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001546 // Special case constant expressions to match code generator.
1547 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001548 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001549 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001550 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001551
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001552 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001553 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001554
Daniel Dunbara0d14262009-06-24 23:30:00 +00001555 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001556 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001557 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001558 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001559 }
1560 }
1561
Sean Callanan79ed1a82010-01-19 20:22:31 +00001562 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001563 return false;
1564}
1565
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001566/// ParseDirectiveRealValue
1567/// ::= (.single | .double) [ expression (, expression)* ]
1568bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1569 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1570 CheckForValidSection();
1571
1572 for (;;) {
1573 // We don't truly support arithmetic on floating point expressions, so we
1574 // have to manually parse unary prefixes.
1575 bool IsNeg = false;
1576 if (getLexer().is(AsmToken::Minus)) {
1577 Lex();
1578 IsNeg = true;
1579 } else if (getLexer().is(AsmToken::Plus))
1580 Lex();
1581
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001582 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001583 getLexer().isNot(AsmToken::Real) &&
1584 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001585 return TokError("unexpected token in directive");
1586
1587 // Convert to an APFloat.
1588 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001589 StringRef IDVal = getTok().getString();
1590 if (getLexer().is(AsmToken::Identifier)) {
1591 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1592 Value = APFloat::getInf(Semantics);
1593 else if (!IDVal.compare_lower("nan"))
1594 Value = APFloat::getNaN(Semantics, false, ~0);
1595 else
1596 return TokError("invalid floating point literal");
1597 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001598 APFloat::opInvalidOp)
1599 return TokError("invalid floating point literal");
1600 if (IsNeg)
1601 Value.changeSign();
1602
1603 // Consume the numeric token.
1604 Lex();
1605
1606 // Emit the value as an integer.
1607 APInt AsInt = Value.bitcastToAPInt();
1608 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1609 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1610
1611 if (getLexer().is(AsmToken::EndOfStatement))
1612 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001613
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001614 if (getLexer().isNot(AsmToken::Comma))
1615 return TokError("unexpected token in directive");
1616 Lex();
1617 }
1618 }
1619
1620 Lex();
1621 return false;
1622}
1623
Daniel Dunbara0d14262009-06-24 23:30:00 +00001624/// ParseDirectiveSpace
1625/// ::= .space expression [ , expression ]
1626bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001627 CheckForValidSection();
1628
Daniel Dunbara0d14262009-06-24 23:30:00 +00001629 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001630 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001631 return true;
1632
1633 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001634 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1635 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001636 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001637 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001638
Daniel Dunbar475839e2009-06-29 20:37:27 +00001639 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001640 return true;
1641
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001642 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001643 return TokError("unexpected token in '.space' directive");
1644 }
1645
Sean Callanan79ed1a82010-01-19 20:22:31 +00001646 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001647
1648 if (NumBytes <= 0)
1649 return TokError("invalid number of bytes in '.space' directive");
1650
1651 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001652 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001653
1654 return false;
1655}
1656
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001657/// ParseDirectiveZero
1658/// ::= .zero expression
1659bool AsmParser::ParseDirectiveZero() {
1660 CheckForValidSection();
1661
1662 int64_t NumBytes;
1663 if (ParseAbsoluteExpression(NumBytes))
1664 return true;
1665
Rafael Espindolae452b172010-10-05 19:42:57 +00001666 int64_t Val = 0;
1667 if (getLexer().is(AsmToken::Comma)) {
1668 Lex();
1669 if (ParseAbsoluteExpression(Val))
1670 return true;
1671 }
1672
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001673 if (getLexer().isNot(AsmToken::EndOfStatement))
1674 return TokError("unexpected token in '.zero' directive");
1675
1676 Lex();
1677
Rafael Espindolae452b172010-10-05 19:42:57 +00001678 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001679
1680 return false;
1681}
1682
Daniel Dunbara0d14262009-06-24 23:30:00 +00001683/// ParseDirectiveFill
1684/// ::= .fill expression , expression , expression
1685bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001686 CheckForValidSection();
1687
Daniel Dunbara0d14262009-06-24 23:30:00 +00001688 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001689 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001690 return true;
1691
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001692 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001693 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001694 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001695
Daniel Dunbara0d14262009-06-24 23:30:00 +00001696 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001697 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001698 return true;
1699
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001700 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001701 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001702 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001703
Daniel Dunbara0d14262009-06-24 23:30:00 +00001704 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001705 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001706 return true;
1707
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001708 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001709 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001710
Sean Callanan79ed1a82010-01-19 20:22:31 +00001711 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001712
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001713 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1714 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001715
1716 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001717 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001718
1719 return false;
1720}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001721
1722/// ParseDirectiveOrg
1723/// ::= .org expression [ , expression ]
1724bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001725 CheckForValidSection();
1726
Daniel Dunbar821e3332009-08-31 08:09:28 +00001727 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001728 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001729 return true;
1730
1731 // Parse optional fill expression.
1732 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001733 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1734 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001735 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001736 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001737
Daniel Dunbar475839e2009-06-29 20:37:27 +00001738 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001739 return true;
1740
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001741 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001742 return TokError("unexpected token in '.org' directive");
1743 }
1744
Sean Callanan79ed1a82010-01-19 20:22:31 +00001745 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001746
1747 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1748 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001749 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001750
1751 return false;
1752}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001753
1754/// ParseDirectiveAlign
1755/// ::= {.align, ...} expression [ , expression [ , expression ]]
1756bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001757 CheckForValidSection();
1758
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001759 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001760 int64_t Alignment;
1761 if (ParseAbsoluteExpression(Alignment))
1762 return true;
1763
1764 SMLoc MaxBytesLoc;
1765 bool HasFillExpr = false;
1766 int64_t FillExpr = 0;
1767 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001768 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1769 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001770 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001771 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001772
1773 // The fill expression can be omitted while specifying a maximum number of
1774 // alignment bytes, e.g:
1775 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001776 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001777 HasFillExpr = true;
1778 if (ParseAbsoluteExpression(FillExpr))
1779 return true;
1780 }
1781
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001782 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1783 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001784 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001785 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001786
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001787 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001788 if (ParseAbsoluteExpression(MaxBytesToFill))
1789 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001790
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001791 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001792 return TokError("unexpected token in directive");
1793 }
1794 }
1795
Sean Callanan79ed1a82010-01-19 20:22:31 +00001796 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001797
Daniel Dunbar648ac512010-05-17 21:54:30 +00001798 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001799 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001800
1801 // Compute alignment in bytes.
1802 if (IsPow2) {
1803 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001804 if (Alignment >= 32) {
1805 Error(AlignmentLoc, "invalid alignment value");
1806 Alignment = 31;
1807 }
1808
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001809 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001810 }
1811
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001812 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001813 if (MaxBytesLoc.isValid()) {
1814 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001815 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1816 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001817 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001818 }
1819
1820 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001821 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1822 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001823 MaxBytesToFill = 0;
1824 }
1825 }
1826
Daniel Dunbar648ac512010-05-17 21:54:30 +00001827 // Check whether we should use optimal code alignment for this .align
1828 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001829 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001830 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1831 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001832 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001833 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001834 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001835 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1836 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001837 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001838
1839 return false;
1840}
1841
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001842/// ParseDirectiveSymbolAttribute
1843/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001844bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001845 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001846 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001847 StringRef Name;
1848
1849 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001850 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001851
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001852 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001853
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001854 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001855
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001856 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001857 break;
1858
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001859 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001860 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001861 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001862 }
1863 }
1864
Sean Callanan79ed1a82010-01-19 20:22:31 +00001865 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001866 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001867}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001868
1869/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001870/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1871bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001872 CheckForValidSection();
1873
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001874 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001875 StringRef Name;
1876 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001877 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001878
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001879 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001880 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001881
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001882 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001883 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001884 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001885
1886 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001887 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001888 if (ParseAbsoluteExpression(Size))
1889 return true;
1890
1891 int64_t Pow2Alignment = 0;
1892 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001893 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001894 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001895 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001896 if (ParseAbsoluteExpression(Pow2Alignment))
1897 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001898
Chris Lattner258281d2010-01-19 06:22:22 +00001899 // If this target takes alignments in bytes (not log) validate and convert.
1900 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1901 if (!isPowerOf2_64(Pow2Alignment))
1902 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1903 Pow2Alignment = Log2_64(Pow2Alignment);
1904 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001905 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001906
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001907 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001908 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001909
Sean Callanan79ed1a82010-01-19 20:22:31 +00001910 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001911
Chris Lattner1fc3d752009-07-09 17:25:12 +00001912 // NOTE: a size of zero for a .comm should create a undefined symbol
1913 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001914 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001915 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1916 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001917
Eric Christopherc260a3e2010-05-14 01:38:54 +00001918 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001919 // may internally end up wanting an alignment in bytes.
1920 // FIXME: Diagnose overflow.
1921 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001922 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1923 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001924
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001925 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001926 return Error(IDLoc, "invalid symbol redefinition");
1927
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001928 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001929 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001930 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001931 getStreamer().EmitZerofill(Ctx.getMachOSection(
1932 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1933 0, SectionKind::getBSS()),
1934 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001935 return false;
1936 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001937
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001938 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001939 return false;
1940}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001941
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001942/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001943/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001944bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001945 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001946 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001947
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001948 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001949 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001950 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001951
Sean Callanan79ed1a82010-01-19 20:22:31 +00001952 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001953
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001954 if (Str.empty())
1955 Error(Loc, ".abort detected. Assembly stopping.");
1956 else
1957 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001958 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001959
1960 return false;
1961}
Kevin Enderby71148242009-07-14 21:35:03 +00001962
Kevin Enderby1f049b22009-07-14 23:21:55 +00001963/// ParseDirectiveInclude
1964/// ::= .include "filename"
1965bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001966 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001967 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001968
Sean Callanan18b83232010-01-19 21:44:56 +00001969 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001970 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001971 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001972
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001973 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001974 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001975
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001976 // Strip the quotes.
1977 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001978
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001979 // Attempt to switch the lexer to the included file before consuming the end
1980 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001981 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001982 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001983 return true;
1984 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001985
1986 return false;
1987}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001988
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001989/// ParseDirectiveIf
1990/// ::= .if expression
1991bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001992 TheCondStack.push_back(TheCondState);
1993 TheCondState.TheCond = AsmCond::IfCond;
1994 if(TheCondState.Ignore) {
1995 EatToEndOfStatement();
1996 }
1997 else {
1998 int64_t ExprValue;
1999 if (ParseAbsoluteExpression(ExprValue))
2000 return true;
2001
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002002 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002003 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002004
Sean Callanan79ed1a82010-01-19 20:22:31 +00002005 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002006
2007 TheCondState.CondMet = ExprValue;
2008 TheCondState.Ignore = !TheCondState.CondMet;
2009 }
2010
2011 return false;
2012}
2013
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002014bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2015 StringRef Name;
2016 TheCondStack.push_back(TheCondState);
2017 TheCondState.TheCond = AsmCond::IfCond;
2018
2019 if (TheCondState.Ignore) {
2020 EatToEndOfStatement();
2021 } else {
2022 if (ParseIdentifier(Name))
2023 return TokError("expected identifier after '.ifdef'");
2024
2025 Lex();
2026
2027 MCSymbol *Sym = getContext().LookupSymbol(Name);
2028
2029 if (expect_defined)
2030 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2031 else
2032 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2033 TheCondState.Ignore = !TheCondState.CondMet;
2034 }
2035
2036 return false;
2037}
2038
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002039/// ParseDirectiveElseIf
2040/// ::= .elseif expression
2041bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2042 if (TheCondState.TheCond != AsmCond::IfCond &&
2043 TheCondState.TheCond != AsmCond::ElseIfCond)
2044 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2045 " an .elseif");
2046 TheCondState.TheCond = AsmCond::ElseIfCond;
2047
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002048 bool LastIgnoreState = false;
2049 if (!TheCondStack.empty())
2050 LastIgnoreState = TheCondStack.back().Ignore;
2051 if (LastIgnoreState || TheCondState.CondMet) {
2052 TheCondState.Ignore = true;
2053 EatToEndOfStatement();
2054 }
2055 else {
2056 int64_t ExprValue;
2057 if (ParseAbsoluteExpression(ExprValue))
2058 return true;
2059
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002060 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002061 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002062
Sean Callanan79ed1a82010-01-19 20:22:31 +00002063 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002064 TheCondState.CondMet = ExprValue;
2065 TheCondState.Ignore = !TheCondState.CondMet;
2066 }
2067
2068 return false;
2069}
2070
2071/// ParseDirectiveElse
2072/// ::= .else
2073bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002074 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002075 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002076
Sean Callanan79ed1a82010-01-19 20:22:31 +00002077 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002078
2079 if (TheCondState.TheCond != AsmCond::IfCond &&
2080 TheCondState.TheCond != AsmCond::ElseIfCond)
2081 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2082 ".elseif");
2083 TheCondState.TheCond = AsmCond::ElseCond;
2084 bool LastIgnoreState = false;
2085 if (!TheCondStack.empty())
2086 LastIgnoreState = TheCondStack.back().Ignore;
2087 if (LastIgnoreState || TheCondState.CondMet)
2088 TheCondState.Ignore = true;
2089 else
2090 TheCondState.Ignore = false;
2091
2092 return false;
2093}
2094
2095/// ParseDirectiveEndIf
2096/// ::= .endif
2097bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002098 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002099 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002100
Sean Callanan79ed1a82010-01-19 20:22:31 +00002101 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002102
2103 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2104 TheCondStack.empty())
2105 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2106 ".else");
2107 if (!TheCondStack.empty()) {
2108 TheCondState = TheCondStack.back();
2109 TheCondStack.pop_back();
2110 }
2111
2112 return false;
2113}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002114
2115/// ParseDirectiveFile
2116/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002117bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002118 // FIXME: I'm not sure what this is.
2119 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002120 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002121 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002122 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002123 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002124
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002125 if (FileNumber < 1)
2126 return TokError("file number less than one");
2127 }
2128
Daniel Dunbareceec052010-07-12 17:45:27 +00002129 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002130 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002131
Chris Lattnerd32e8032010-01-25 19:02:58 +00002132 StringRef Filename = getTok().getString();
2133 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002134 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002135
Daniel Dunbareceec052010-07-12 17:45:27 +00002136 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002137 return TokError("unexpected token in '.file' directive");
2138
Chris Lattnerd32e8032010-01-25 19:02:58 +00002139 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002140 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002141 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002142 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002143 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002144 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002145
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002146 return false;
2147}
2148
2149/// ParseDirectiveLine
2150/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002151bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002152 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2153 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002154 return TokError("unexpected token in '.line' directive");
2155
Sean Callanan18b83232010-01-19 21:44:56 +00002156 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002157 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002158 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002159
2160 // FIXME: Do something with the .line.
2161 }
2162
Daniel Dunbareceec052010-07-12 17:45:27 +00002163 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002164 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002165
2166 return false;
2167}
2168
2169
2170/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002171/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002172/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2173/// The first number is a file number, must have been previously assigned with
2174/// a .file directive, the second number is the line number and optionally the
2175/// third number is a column position (zero if not specified). The remaining
2176/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002177bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002178
Daniel Dunbareceec052010-07-12 17:45:27 +00002179 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002180 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002181 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002182 if (FileNumber < 1)
2183 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002184 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002185 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002186 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002187
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002188 int64_t LineNumber = 0;
2189 if (getLexer().is(AsmToken::Integer)) {
2190 LineNumber = getTok().getIntVal();
2191 if (LineNumber < 1)
2192 return TokError("line number less than one in '.loc' directive");
2193 Lex();
2194 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002195
2196 int64_t ColumnPos = 0;
2197 if (getLexer().is(AsmToken::Integer)) {
2198 ColumnPos = getTok().getIntVal();
2199 if (ColumnPos < 0)
2200 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002201 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002202 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002203
Kevin Enderbyc0957932010-09-30 16:52:03 +00002204 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002205 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002206 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002207 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2208 for (;;) {
2209 if (getLexer().is(AsmToken::EndOfStatement))
2210 break;
2211
2212 StringRef Name;
2213 SMLoc Loc = getTok().getLoc();
2214 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002215 return TokError("unexpected token in '.loc' directive");
2216
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002217 if (Name == "basic_block")
2218 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2219 else if (Name == "prologue_end")
2220 Flags |= DWARF2_FLAG_PROLOGUE_END;
2221 else if (Name == "epilogue_begin")
2222 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2223 else if (Name == "is_stmt") {
2224 SMLoc Loc = getTok().getLoc();
2225 const MCExpr *Value;
2226 if (getParser().ParseExpression(Value))
2227 return true;
2228 // The expression must be the constant 0 or 1.
2229 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2230 int Value = MCE->getValue();
2231 if (Value == 0)
2232 Flags &= ~DWARF2_FLAG_IS_STMT;
2233 else if (Value == 1)
2234 Flags |= DWARF2_FLAG_IS_STMT;
2235 else
2236 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002237 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002238 else {
2239 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2240 }
2241 }
2242 else if (Name == "isa") {
2243 SMLoc Loc = getTok().getLoc();
2244 const MCExpr *Value;
2245 if (getParser().ParseExpression(Value))
2246 return true;
2247 // The expression must be a constant greater or equal to 0.
2248 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2249 int Value = MCE->getValue();
2250 if (Value < 0)
2251 return Error(Loc, "isa number less than zero");
2252 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002253 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002254 else {
2255 return Error(Loc, "isa number not a constant value");
2256 }
2257 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002258 else if (Name == "discriminator") {
2259 if (getParser().ParseAbsoluteExpression(Discriminator))
2260 return true;
2261 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002262 else {
2263 return Error(Loc, "unknown sub-directive in '.loc' directive");
2264 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002265
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002266 if (getLexer().is(AsmToken::EndOfStatement))
2267 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002268 }
2269 }
2270
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002271 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2272 Isa, Discriminator);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002273
2274 return false;
2275}
2276
Daniel Dunbar138abae2010-10-16 04:56:42 +00002277/// ParseDirectiveStabs
2278/// ::= .stabs string, number, number, number
2279bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2280 SMLoc DirectiveLoc) {
2281 return TokError("unsupported directive '" + Directive + "'");
2282}
2283
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002284/// ParseDirectiveCFIStartProc
2285/// ::= .cfi_startproc
2286bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2287 SMLoc DirectiveLoc) {
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002288 return getStreamer().EmitCFIStartProc();
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002289}
2290
2291/// ParseDirectiveCFIEndProc
2292/// ::= .cfi_endproc
2293bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002294 return getStreamer().EmitCFIEndProc();
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002295}
2296
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002297/// ParseRegisterOrRegisterNumber - parse register name or number.
2298bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2299 SMLoc DirectiveLoc) {
2300 unsigned RegNo;
2301
2302 if (getLexer().is(AsmToken::Percent)) {
2303 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2304 DirectiveLoc))
2305 return true;
2306 Register = getContext().getTargetAsmInfo().getDwarfRegNum(RegNo, true);
2307 } else
2308 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002309
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002310 return false;
2311}
2312
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002313/// ParseDirectiveCFIDefCfa
2314/// ::= .cfi_def_cfa register, offset
2315bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2316 SMLoc DirectiveLoc) {
2317 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002318 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002319 return true;
2320
2321 if (getLexer().isNot(AsmToken::Comma))
2322 return TokError("unexpected token in directive");
2323 Lex();
2324
2325 int64_t Offset = 0;
2326 if (getParser().ParseAbsoluteExpression(Offset))
2327 return true;
2328
2329 return getStreamer().EmitCFIDefCfa(Register, Offset);
2330}
2331
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002332/// ParseDirectiveCFIDefCfaOffset
2333/// ::= .cfi_def_cfa_offset offset
2334bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2335 SMLoc DirectiveLoc) {
2336 int64_t Offset = 0;
2337 if (getParser().ParseAbsoluteExpression(Offset))
2338 return true;
2339
Rafael Espindola53abbe52011-04-11 20:29:16 +00002340 getParser().setLastOffset(Offset);
2341
2342 return getStreamer().EmitCFIDefCfaOffset(Offset);
2343}
2344
2345/// ParseDirectiveCFIAdjustCfaOffset
2346/// ::= .cfi_adjust_cfa_offset adjustment
2347bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2348 SMLoc DirectiveLoc) {
2349 int64_t Adjustment = 0;
2350 if (getParser().ParseAbsoluteExpression(Adjustment))
2351 return true;
2352
2353 int64_t Offset = getParser().adjustLastOffset(Adjustment);
2354
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002355 return getStreamer().EmitCFIDefCfaOffset(Offset);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002356}
2357
2358/// ParseDirectiveCFIDefCfaRegister
2359/// ::= .cfi_def_cfa_register register
2360bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2361 SMLoc DirectiveLoc) {
2362 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002363 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002364 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002365
2366 return getStreamer().EmitCFIDefCfaRegister(Register);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002367}
2368
2369/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002370/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002371bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2372 int64_t Register = 0;
2373 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002374
2375 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002376 return true;
2377
2378 if (getLexer().isNot(AsmToken::Comma))
2379 return TokError("unexpected token in directive");
2380 Lex();
2381
2382 if (getParser().ParseAbsoluteExpression(Offset))
2383 return true;
2384
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002385 return getStreamer().EmitCFIOffset(Register, Offset);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002386}
2387
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002388/// ParseDirectiveCFIRelOffset
2389/// ::= .cfi_rel_offset register, offset
2390bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2391 SMLoc DirectiveLoc) {
2392 int64_t Register = 0;
2393
2394 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2395 return true;
2396
2397 if (getLexer().isNot(AsmToken::Comma))
2398 return TokError("unexpected token in directive");
2399 Lex();
2400
2401 int64_t Offset = 0;
2402 if (getParser().ParseAbsoluteExpression(Offset))
2403 return true;
2404
2405 Offset -= getParser().getLastOffset();
2406
2407 return getStreamer().EmitCFIOffset(Register, Offset);
2408}
2409
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002410static bool isValidEncoding(int64_t Encoding) {
2411 if (Encoding & ~0xff)
2412 return false;
2413
2414 if (Encoding == dwarf::DW_EH_PE_omit)
2415 return true;
2416
2417 const unsigned Format = Encoding & 0xf;
2418 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2419 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2420 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2421 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2422 return false;
2423
Rafael Espindolacaf11582010-12-29 04:31:26 +00002424 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002425 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002426 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002427 return false;
2428
2429 return true;
2430}
2431
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002432/// ParseDirectiveCFIPersonalityOrLsda
2433/// ::= .cfi_personality encoding, [symbol_name]
2434/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002435bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002436 SMLoc DirectiveLoc) {
2437 int64_t Encoding = 0;
2438 if (getParser().ParseAbsoluteExpression(Encoding))
2439 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002440 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002441 return false;
2442
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002443 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002444 return TokError("unsupported encoding.");
2445
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002446 if (getLexer().isNot(AsmToken::Comma))
2447 return TokError("unexpected token in directive");
2448 Lex();
2449
2450 StringRef Name;
2451 if (getParser().ParseIdentifier(Name))
2452 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002453
2454 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2455
2456 if (IDVal == ".cfi_personality")
Rafael Espindola3a83c402010-12-27 00:36:05 +00002457 return getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002458 else {
2459 assert(IDVal == ".cfi_lsda");
Rafael Espindolabdc31672010-12-27 15:56:22 +00002460 return getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002461 }
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002462}
2463
Rafael Espindolafe024d02010-12-28 18:36:23 +00002464/// ParseDirectiveCFIRememberState
2465/// ::= .cfi_remember_state
2466bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2467 SMLoc DirectiveLoc) {
2468 return getStreamer().EmitCFIRememberState();
2469}
2470
2471/// ParseDirectiveCFIRestoreState
2472/// ::= .cfi_remember_state
2473bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2474 SMLoc DirectiveLoc) {
2475 return getStreamer().EmitCFIRestoreState();
2476}
2477
Rafael Espindolac5754392011-04-12 15:31:05 +00002478/// ParseDirectiveCFISameValue
2479/// ::= .cfi_same_value register
2480bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2481 SMLoc DirectiveLoc) {
2482 int64_t Register = 0;
2483
2484 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2485 return true;
2486
2487 getStreamer().EmitCFISameValue(Register);
2488
2489 return false;
2490}
2491
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002492/// ParseDirectiveMacrosOnOff
2493/// ::= .macros_on
2494/// ::= .macros_off
2495bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2496 SMLoc DirectiveLoc) {
2497 if (getLexer().isNot(AsmToken::EndOfStatement))
2498 return Error(getLexer().getLoc(),
2499 "unexpected token in '" + Directive + "' directive");
2500
2501 getParser().MacrosEnabled = Directive == ".macros_on";
2502
2503 return false;
2504}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002505
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002506/// ParseDirectiveMacro
2507/// ::= .macro name
2508bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2509 SMLoc DirectiveLoc) {
2510 StringRef Name;
2511 if (getParser().ParseIdentifier(Name))
2512 return TokError("expected identifier in directive");
2513
2514 if (getLexer().isNot(AsmToken::EndOfStatement))
2515 return TokError("unexpected token in '.macro' directive");
2516
2517 // Eat the end of statement.
2518 Lex();
2519
2520 AsmToken EndToken, StartToken = getTok();
2521
2522 // Lex the macro definition.
2523 for (;;) {
2524 // Check whether we have reached the end of the file.
2525 if (getLexer().is(AsmToken::Eof))
2526 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2527
2528 // Otherwise, check whether we have reach the .endmacro.
2529 if (getLexer().is(AsmToken::Identifier) &&
2530 (getTok().getIdentifier() == ".endm" ||
2531 getTok().getIdentifier() == ".endmacro")) {
2532 EndToken = getTok();
2533 Lex();
2534 if (getLexer().isNot(AsmToken::EndOfStatement))
2535 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2536 "' directive");
2537 break;
2538 }
2539
2540 // Otherwise, scan til the end of the statement.
2541 getParser().EatToEndOfStatement();
2542 }
2543
2544 if (getParser().MacroMap.lookup(Name)) {
2545 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2546 }
2547
2548 const char *BodyStart = StartToken.getLoc().getPointer();
2549 const char *BodyEnd = EndToken.getLoc().getPointer();
2550 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2551 getParser().MacroMap[Name] = new Macro(Name, Body);
2552 return false;
2553}
2554
2555/// ParseDirectiveEndMacro
2556/// ::= .endm
2557/// ::= .endmacro
2558bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2559 SMLoc DirectiveLoc) {
2560 if (getLexer().isNot(AsmToken::EndOfStatement))
2561 return TokError("unexpected token in '" + Directive + "' directive");
2562
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002563 // If we are inside a macro instantiation, terminate the current
2564 // instantiation.
2565 if (!getParser().ActiveMacros.empty()) {
2566 getParser().HandleMacroExit();
2567 return false;
2568 }
2569
2570 // Otherwise, this .endmacro is a stray entry in the file; well formed
2571 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002572 return TokError("unexpected '" + Directive + "' in file, "
2573 "no current macro definition");
2574}
2575
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002576bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002577 getParser().CheckForValidSection();
2578
2579 const MCExpr *Value;
2580
2581 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002582 return true;
2583
2584 if (getLexer().isNot(AsmToken::EndOfStatement))
2585 return TokError("unexpected token in directive");
2586
2587 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002588 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002589 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002590 getStreamer().EmitULEB128Value(Value);
2591
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002592 return false;
2593}
2594
2595
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002596/// \brief Create an MCAsmParser instance.
2597MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2598 MCContext &C, MCStreamer &Out,
2599 const MCAsmInfo &MAI) {
2600 return new AsmParser(T, SM, C, Out, MAI);
2601}