blob: 2ba2fbd4b40e090ada044abddb885f7cad74f858 [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"
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000030#include "llvm/Support/CommandLine.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000031#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000032#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000033#include "llvm/Support/raw_ostream.h"
Roman Divacky54b0f4f2011-01-27 17:16:37 +000034#include "llvm/Target/TargetAsmInfo.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000035#include "llvm/Target/TargetAsmParser.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000036#include <cctype>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000037#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000038using namespace llvm;
39
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000040static cl::opt<bool>
41FatalAssemblerWarnings("fatal-assembler-warnings",
42 cl::desc("Consider warnings as error"));
43
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000044namespace {
45
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000046/// \brief Helper class for tracking macro definitions.
47struct Macro {
48 StringRef Name;
49 StringRef Body;
50
51public:
52 Macro(StringRef N, StringRef B) : Name(N), Body(B) {}
53};
54
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000055/// \brief Helper class for storing information about an active macro
56/// instantiation.
57struct MacroInstantiation {
58 /// The macro being instantiated.
59 const Macro *TheMacro;
60
61 /// The macro instantiation with substitutions.
62 MemoryBuffer *Instantiation;
63
64 /// The location of the instantiation.
65 SMLoc InstantiationLoc;
66
67 /// The location where parsing should resume upon instantiation completion.
68 SMLoc ExitLoc;
69
70public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000071 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
72 const std::vector<std::vector<AsmToken> > &A);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000073};
74
Daniel Dunbaraef87e32010-07-18 18:31:38 +000075/// \brief The concrete assembly parser instance.
76class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000077 friend class GenericAsmParser;
78
Daniel Dunbaraef87e32010-07-18 18:31:38 +000079 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
80 void operator=(const AsmParser &); // DO NOT IMPLEMENT
81private:
82 AsmLexer Lexer;
83 MCContext &Ctx;
84 MCStreamer &Out;
85 SourceMgr &SrcMgr;
86 MCAsmParserExtension *GenericParser;
87 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +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
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000136 virtual bool Warning(SMLoc L, const Twine &Meg);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000137 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
148private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000149 void CheckForValidSection();
150
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000151 bool ParseStatement();
152
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000153 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
154 void HandleMacroExit();
155
156 void PrintMacroInstantiations();
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000157 void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type) const {
158 SrcMgr.PrintMessage(Loc, Msg, Type);
159 }
160
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000161 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
162 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000163
164 /// \brief Reset the current lexer position to that given by \arg Loc. The
165 /// current token is not set; clients should ensure Lex() is called
166 /// subsequently.
167 void JumpToLoc(SMLoc Loc);
168
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000169 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000170
171 /// \brief Parse up to the end of statement and a return the contents from the
172 /// current token until the end of the statement; the current token on exit
173 /// will be either the EndOfStatement or EOF.
174 StringRef ParseStringToEndOfStatement();
175
Nico Weber4c4c7322011-01-28 03:04:41 +0000176 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000177
178 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
179 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
180 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000181 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000182
183 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
184 /// and set \arg Res to the identifier contents.
185 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000186
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000187 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000188
189 // ".ascii", ".asciiz", ".string"
190 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000191 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000192 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000193 bool ParseDirectiveFill(); // ".fill"
194 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000195 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000196 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000197 bool ParseDirectiveOrg(); // ".org"
198 // ".align{,32}", ".p2align{,w,l}"
199 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
200
201 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
202 /// accepts a single symbol (which should be a label or an external).
203 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000204
205 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
206
207 bool ParseDirectiveAbort(); // ".abort"
208 bool ParseDirectiveInclude(); // ".include"
209
210 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000211 // ".ifdef" or ".ifndef", depending on expect_defined
212 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000213 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
214 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
215 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
216
217 /// ParseEscapedString - Parse the current token as a string which may include
218 /// escaped characters and return the string contents.
219 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000220
221 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
222 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000223};
224
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000225/// \brief Generic implementations of directive handling, etc. which is shared
226/// (or the default, at least) for all assembler parser.
227class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000228 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
229 void AddDirectiveHandler(StringRef Directive) {
230 getParser().AddDirectiveHandler(this, Directive,
231 HandleDirective<GenericAsmParser, Handler>);
232 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000233public:
234 GenericAsmParser() {}
235
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000236 AsmParser &getParser() {
237 return (AsmParser&) this->MCAsmParserExtension::getParser();
238 }
239
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000240 virtual void Initialize(MCAsmParser &Parser) {
241 // Call the base implementation.
242 this->MCAsmParserExtension::Initialize(Parser);
243
244 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000245 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
246 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
247 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000248 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000249
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000250 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000251 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
252 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000253 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
254 ".cfi_startproc");
255 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
256 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000257 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
258 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000259 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
260 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000261 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
262 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000263 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
264 ".cfi_def_cfa_register");
265 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
266 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000267 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
268 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000269 AddDirectiveHandler<
270 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
271 AddDirectiveHandler<
272 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000273 AddDirectiveHandler<
274 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
275 AddDirectiveHandler<
276 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000277 AddDirectiveHandler<
278 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000279
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000280 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000281 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
282 ".macros_on");
283 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
284 ".macros_off");
285 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
286 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
287 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000288
289 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
290 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000291 }
292
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000293 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
294
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000295 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
296 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
297 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000298 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000299 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000300 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
301 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000302 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000303 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000304 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000305 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
306 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000307 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000308 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000309 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
310 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000311 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000312
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000313 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000314 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
315 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000316
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000317 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000318};
319
320}
321
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000322namespace llvm {
323
324extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000325extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000326extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000327
328}
329
Chris Lattneraaec2052010-01-19 19:46:13 +0000330enum { DEFAULT_ADDRSPACE = 0 };
331
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000332AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
333 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000334 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000335 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000336 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000337 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000338
339 // Initialize the generic parser.
340 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000341
342 // Initialize the platform / file format parser.
343 //
344 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
345 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000346 if (_MAI.hasMicrosoftFastStdCallMangling()) {
347 PlatformParser = createCOFFAsmParser();
348 PlatformParser->Initialize(*this);
349 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000350 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000351 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000352 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000353 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000354 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000355 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000356}
357
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000358AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000359 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
360
361 // Destroy any macros.
362 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
363 ie = MacroMap.end(); it != ie; ++it)
364 delete it->getValue();
365
Daniel Dunbare4749702010-07-12 18:12:02 +0000366 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000367 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000368}
369
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000370void AsmParser::PrintMacroInstantiations() {
371 // Print the active macro instantiation stack.
372 for (std::vector<MacroInstantiation*>::const_reverse_iterator
373 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
374 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
375 "note");
376}
377
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000378bool AsmParser::Warning(SMLoc L, const Twine &Msg) {
379 if (FatalAssemblerWarnings)
380 return Error(L, Msg);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000381 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000382 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000383 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000384}
385
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000386bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000387 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000388 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000389 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000390 return true;
391}
392
Sean Callananfd0b0282010-01-21 00:19:58 +0000393bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000394 std::string IncludedFile;
395 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000396 if (NewBuf == -1)
397 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000398
Sean Callananfd0b0282010-01-21 00:19:58 +0000399 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000400
Sean Callananfd0b0282010-01-21 00:19:58 +0000401 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000402
Sean Callananfd0b0282010-01-21 00:19:58 +0000403 return false;
404}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000405
406void AsmParser::JumpToLoc(SMLoc Loc) {
407 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
408 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
409}
410
Sean Callananfd0b0282010-01-21 00:19:58 +0000411const AsmToken &AsmParser::Lex() {
412 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000413
Sean Callananfd0b0282010-01-21 00:19:58 +0000414 if (tok->is(AsmToken::Eof)) {
415 // If this is the end of an included file, pop the parent file off the
416 // include stack.
417 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
418 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000419 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000420 tok = &Lexer.Lex();
421 }
422 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000423
Sean Callananfd0b0282010-01-21 00:19:58 +0000424 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000425 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000426
Sean Callananfd0b0282010-01-21 00:19:58 +0000427 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000428}
429
Chris Lattner79180e22010-04-05 23:15:42 +0000430bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000431 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000432 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000433 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000434
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000435 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000436 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000437
438 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000439 AsmCond StartingCondState = TheCondState;
440
Chris Lattnerb717fb02009-07-02 21:53:43 +0000441 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000442 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000443 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000444
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000445 // We had an error, validate that one was emitted and recover by skipping to
446 // the next line.
447 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000448 EatToEndOfStatement();
449 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000450
451 if (TheCondState.TheCond != StartingCondState.TheCond ||
452 TheCondState.Ignore != StartingCondState.Ignore)
453 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000454
455 // Check to see there are no empty DwarfFile slots.
456 const std::vector<MCDwarfFile *> &MCDwarfFiles =
457 getContext().getMCDwarfFiles();
458 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000459 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000460 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000461 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000462
Chris Lattner79180e22010-04-05 23:15:42 +0000463 // Finalize the output stream if there are no errors and if the client wants
464 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000465 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000466 Out.Finish();
467
Chris Lattnerb717fb02009-07-02 21:53:43 +0000468 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000469}
470
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000471void AsmParser::CheckForValidSection() {
472 if (!getStreamer().getCurrentSection()) {
473 TokError("expected section directive before assembly directive");
474 Out.SwitchSection(Ctx.getMachOSection(
475 "__TEXT", "__text",
476 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
477 0, SectionKind::getText()));
478 }
479}
480
Chris Lattner2cf5f142009-06-22 01:29:09 +0000481/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
482void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000483 while (Lexer.isNot(AsmToken::EndOfStatement) &&
484 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000485 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000486
Chris Lattner2cf5f142009-06-22 01:29:09 +0000487 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000488 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000489 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000490}
491
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000492StringRef AsmParser::ParseStringToEndOfStatement() {
493 const char *Start = getTok().getLoc().getPointer();
494
495 while (Lexer.isNot(AsmToken::EndOfStatement) &&
496 Lexer.isNot(AsmToken::Eof))
497 Lex();
498
499 const char *End = getTok().getLoc().getPointer();
500 return StringRef(Start, End - Start);
501}
Chris Lattnerc4193832009-06-22 05:51:26 +0000502
Chris Lattner74ec1a32009-06-22 06:32:03 +0000503/// ParseParenExpr - Parse a paren expression and return it.
504/// NOTE: This assumes the leading '(' has already been consumed.
505///
506/// parenexpr ::= expr)
507///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000508bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000509 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000510 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000511 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000512 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000513 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000514 return false;
515}
Chris Lattnerc4193832009-06-22 05:51:26 +0000516
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000517/// ParseBracketExpr - Parse a bracket expression and return it.
518/// NOTE: This assumes the leading '[' has already been consumed.
519///
520/// bracketexpr ::= expr]
521///
522bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
523 if (ParseExpression(Res)) return true;
524 if (Lexer.isNot(AsmToken::RBrac))
525 return TokError("expected ']' in brackets expression");
526 EndLoc = Lexer.getLoc();
527 Lex();
528 return false;
529}
530
Chris Lattner74ec1a32009-06-22 06:32:03 +0000531/// ParsePrimaryExpr - Parse a primary expression and return it.
532/// primaryexpr ::= (parenexpr
533/// primaryexpr ::= symbol
534/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000535/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000536/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000537bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000538 switch (Lexer.getKind()) {
539 default:
540 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000541 // If we have an error assume that we've already handled it.
542 case AsmToken::Error:
543 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000544 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000545 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000546 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000547 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000548 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000549 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000550 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000551 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000552 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000553 EndLoc = Lexer.getLoc();
554
555 StringRef Identifier;
556 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000557 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000558
Daniel Dunbarfffff912009-10-16 01:34:54 +0000559 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000560 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000561 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000562
563 // Lookup the symbol variant if used.
564 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000565 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000566 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000567 if (Variant == MCSymbolRefExpr::VK_Invalid) {
568 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000569 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000570 }
571 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000572
Daniel Dunbarfffff912009-10-16 01:34:54 +0000573 // If this is an absolute variable reference, substitute it now to preserve
574 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000575 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000576 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000577 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000578
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000579 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000580 return false;
581 }
582
583 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000584 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000585 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000586 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000587 case AsmToken::Integer: {
588 SMLoc Loc = getTok().getLoc();
589 int64_t IntVal = getTok().getIntVal();
590 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000591 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000592 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000593 // Look for 'b' or 'f' following an Integer as a directional label
594 if (Lexer.getKind() == AsmToken::Identifier) {
595 StringRef IDVal = getTok().getString();
596 if (IDVal == "f" || IDVal == "b"){
597 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
598 IDVal == "f" ? 1 : 0);
599 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
600 getContext());
601 if(IDVal == "b" && Sym->isUndefined())
602 return Error(Loc, "invalid reference to undefined symbol");
603 EndLoc = Lexer.getLoc();
604 Lex(); // Eat identifier.
605 }
606 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000607 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000608 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000609 case AsmToken::Real: {
610 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000611 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000612 Res = MCConstantExpr::Create(IntVal, getContext());
613 Lex(); // Eat token.
614 return false;
615 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000616 case AsmToken::Dot: {
617 // This is a '.' reference, which references the current PC. Emit a
618 // temporary label to the streamer and refer to it.
619 MCSymbol *Sym = Ctx.CreateTempSymbol();
620 Out.EmitLabel(Sym);
621 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
622 EndLoc = Lexer.getLoc();
623 Lex(); // Eat identifier.
624 return false;
625 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000626 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000627 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000628 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000629 case AsmToken::LBrac:
630 if (!PlatformParser->HasBracketExpressions())
631 return TokError("brackets expression not supported on this target");
632 Lex(); // Eat the '['.
633 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000634 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000635 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000636 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000637 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000638 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000639 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000640 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000641 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000642 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000643 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000644 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000645 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000646 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000647 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000648 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000649 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000650 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000651 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000652 }
653}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000654
Chris Lattnerb4307b32010-01-15 19:28:38 +0000655bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000656 SMLoc EndLoc;
657 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000658}
659
Daniel Dunbarcceba832010-09-17 02:47:07 +0000660const MCExpr *
661AsmParser::ApplyModifierToExpr(const MCExpr *E,
662 MCSymbolRefExpr::VariantKind Variant) {
663 // Recurse over the given expression, rebuilding it to apply the given variant
664 // if there is exactly one symbol.
665 switch (E->getKind()) {
666 case MCExpr::Target:
667 case MCExpr::Constant:
668 return 0;
669
670 case MCExpr::SymbolRef: {
671 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
672
673 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
674 TokError("invalid variant on expression '" +
675 getTok().getIdentifier() + "' (already modified)");
676 return E;
677 }
678
679 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
680 }
681
682 case MCExpr::Unary: {
683 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
684 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
685 if (!Sub)
686 return 0;
687 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
688 }
689
690 case MCExpr::Binary: {
691 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
692 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
693 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
694
695 if (!LHS && !RHS)
696 return 0;
697
698 if (!LHS) LHS = BE->getLHS();
699 if (!RHS) RHS = BE->getRHS();
700
701 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
702 }
703 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000704
705 assert(0 && "Invalid expression kind!");
706 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000707}
708
Chris Lattner74ec1a32009-06-22 06:32:03 +0000709/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000710///
Chris Lattner74ec1a32009-06-22 06:32:03 +0000711/// expr ::= expr +,- expr -> lowest.
712/// expr ::= expr |,^,&,! expr -> middle.
713/// expr ::= expr *,/,%,<<,>> expr -> highest.
714/// expr ::= primaryexpr
715///
Chris Lattner54482b42010-01-15 19:39:23 +0000716bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000717 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000718 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000719 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
720 return true;
721
Daniel Dunbarcceba832010-09-17 02:47:07 +0000722 // As a special case, we support 'a op b @ modifier' by rewriting the
723 // expression to include the modifier. This is inefficient, but in general we
724 // expect users to use 'a@modifier op b'.
725 if (Lexer.getKind() == AsmToken::At) {
726 Lex();
727
728 if (Lexer.isNot(AsmToken::Identifier))
729 return TokError("unexpected symbol modifier following '@'");
730
731 MCSymbolRefExpr::VariantKind Variant =
732 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
733 if (Variant == MCSymbolRefExpr::VK_Invalid)
734 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
735
736 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
737 if (!ModifiedRes) {
738 return TokError("invalid modifier '" + getTok().getIdentifier() +
739 "' (no symbols present)");
740 return true;
741 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000742
Daniel Dunbarcceba832010-09-17 02:47:07 +0000743 Res = ModifiedRes;
744 Lex();
745 }
746
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000747 // Try to constant fold it up front, if possible.
748 int64_t Value;
749 if (Res->EvaluateAsAbsolute(Value))
750 Res = MCConstantExpr::Create(Value, getContext());
751
752 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000753}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000754
Chris Lattnerb4307b32010-01-15 19:28:38 +0000755bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000756 Res = 0;
757 return ParseParenExpr(Res, EndLoc) ||
758 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000759}
760
Daniel Dunbar475839e2009-06-29 20:37:27 +0000761bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000762 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000763
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000764 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000765 if (ParseExpression(Expr))
766 return true;
767
Daniel Dunbare00b0112009-10-16 01:57:52 +0000768 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000769 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000770
771 return false;
772}
773
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000774static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000775 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000776 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000777 default:
778 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000779
Daniel Dunbarcceba832010-09-17 02:47:07 +0000780 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000781 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000782 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000783 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000784 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000785 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000786 return 1;
787
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000788
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000789 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000790 //
791 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000792 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000793 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000794 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000795 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000796 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000797 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000798 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000799 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000800 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000801
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000802 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000803 case AsmToken::EqualEqual:
804 Kind = MCBinaryExpr::EQ;
805 return 3;
806 case AsmToken::ExclaimEqual:
807 case AsmToken::LessGreater:
808 Kind = MCBinaryExpr::NE;
809 return 3;
810 case AsmToken::Less:
811 Kind = MCBinaryExpr::LT;
812 return 3;
813 case AsmToken::LessEqual:
814 Kind = MCBinaryExpr::LTE;
815 return 3;
816 case AsmToken::Greater:
817 Kind = MCBinaryExpr::GT;
818 return 3;
819 case AsmToken::GreaterEqual:
820 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000821 return 3;
822
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000823 // High Intermediate Precedence: +, -
824 case AsmToken::Plus:
825 Kind = MCBinaryExpr::Add;
826 return 4;
827 case AsmToken::Minus:
828 Kind = MCBinaryExpr::Sub;
829 return 4;
830
Daniel Dunbar475839e2009-06-29 20:37:27 +0000831 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000832 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000833 Kind = MCBinaryExpr::Mul;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000834 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000835 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000836 Kind = MCBinaryExpr::Div;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000837 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000838 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000839 Kind = MCBinaryExpr::Mod;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000840 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000841 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000842 Kind = MCBinaryExpr::Shl;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000843 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000844 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000845 Kind = MCBinaryExpr::Shr;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000846 return 5;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000847 }
848}
849
850
851/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
852/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000853bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
854 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000855 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000856 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000857 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000858
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000859 // If the next token is lower precedence than we are allowed to eat, return
860 // successfully with what we ate already.
861 if (TokPrec < Precedence)
862 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000863
Sean Callanan79ed1a82010-01-19 20:22:31 +0000864 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000865
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000866 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000867 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000868 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000869
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000870 // If BinOp binds less tightly with RHS than the operator after RHS, let
871 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000872 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000873 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000874 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000875 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000876 }
877
Daniel Dunbar475839e2009-06-29 20:37:27 +0000878 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000879 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000880 }
881}
882
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000883
884
885
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000886/// ParseStatement:
887/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000888/// ::= Label* Directive ...Operands... EndOfStatement
889/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000890bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000891 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000892 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000893 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000894 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000895 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000896
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000897 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000898 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000899 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000900 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000901 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000902 // A full line comment is a '#' as the first token.
903 if (Lexer.is(AsmToken::Hash)) {
904 EatToEndOfStatement();
905 return false;
906 }
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000907
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000908 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000909 if (Lexer.is(AsmToken::Integer)) {
910 LocalLabelVal = getTok().getIntVal();
911 if (LocalLabelVal < 0) {
912 if (!TheCondState.Ignore)
913 return TokError("unexpected token at start of statement");
914 IDVal = "";
915 }
916 else {
917 IDVal = getTok().getString();
918 Lex(); // Consume the integer token to be used as an identifier token.
919 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000920 if (!TheCondState.Ignore)
921 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000922 }
923 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000924
925 } else if (Lexer.is(AsmToken::Dot)) {
926 // Treat '.' as a valid identifier in this context.
927 Lex();
928 IDVal = ".";
929
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000930 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000931 if (!TheCondState.Ignore)
932 return TokError("unexpected token at start of statement");
933 IDVal = "";
934 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000935
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000936
Chris Lattner7834fac2010-04-17 18:14:27 +0000937 // Handle conditional assembly here before checking for skipping. We
938 // have to do this so that .endif isn't skipped in a ".if 0" block for
939 // example.
940 if (IDVal == ".if")
941 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000942 if (IDVal == ".ifdef")
943 return ParseDirectiveIfdef(IDLoc, true);
944 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
945 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +0000946 if (IDVal == ".elseif")
947 return ParseDirectiveElseIf(IDLoc);
948 if (IDVal == ".else")
949 return ParseDirectiveElse(IDLoc);
950 if (IDVal == ".endif")
951 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000952
Chris Lattner7834fac2010-04-17 18:14:27 +0000953 // If we are in a ".if 0" block, ignore this statement.
954 if (TheCondState.Ignore) {
955 EatToEndOfStatement();
956 return false;
957 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000958
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000959 // FIXME: Recurse on local labels?
960
961 // See what kind of statement we have.
962 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000963 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000964 CheckForValidSection();
965
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000966 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000967 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000968
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000969 // Diagnose attempt to use '.' as a label.
970 if (IDVal == ".")
971 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
972
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000973 // Diagnose attempt to use a variable as a label.
974 //
975 // FIXME: Diagnostics. Note the location of the definition as a label.
976 // FIXME: This doesn't diagnose assignment to a symbol which has been
977 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000978 MCSymbol *Sym;
979 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000980 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000981 else
982 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000983 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000984 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000985
Daniel Dunbar959fd882009-08-26 22:13:22 +0000986 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000987 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000988
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000989 // Consume any end of statement token, if present, to avoid spurious
990 // AddBlankLine calls().
991 if (Lexer.is(AsmToken::EndOfStatement)) {
992 Lex();
993 if (Lexer.is(AsmToken::Eof))
994 return false;
995 }
996
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000997 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000998 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000999
Daniel Dunbar3f872332009-07-28 16:08:33 +00001000 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001001 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001002 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001003
Nico Weber4c4c7322011-01-28 03:04:41 +00001004 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001005
1006 default: // Normal instruction or directive.
1007 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001008 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001009
1010 // If macros are enabled, check to see if this is a macro instantiation.
1011 if (MacrosEnabled)
1012 if (const Macro *M = MacroMap.lookup(IDVal))
1013 return HandleMacroEntry(IDVal, IDLoc, M);
1014
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001015 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001016 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001017 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001018 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001019 return ParseDirectiveSet(IDVal, true);
1020 if (IDVal == ".equiv")
1021 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001022
Daniel Dunbara0d14262009-06-24 23:30:00 +00001023 // Data directives
1024
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001025 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001026 return ParseDirectiveAscii(IDVal, false);
1027 if (IDVal == ".asciz" || IDVal == ".string")
1028 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001029
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001030 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001031 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001032 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001033 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001034 if (IDVal == ".value")
1035 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001036 if (IDVal == ".2byte")
1037 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001038 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001039 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001040 if (IDVal == ".int")
1041 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001042 if (IDVal == ".4byte")
1043 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001044 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001045 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001046 if (IDVal == ".8byte")
1047 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001048 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001049 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1050 if (IDVal == ".double")
1051 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001052
Eli Friedman5d68ec22010-07-19 04:17:25 +00001053 if (IDVal == ".align") {
1054 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1055 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1056 }
1057 if (IDVal == ".align32") {
1058 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1059 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1060 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001061 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001062 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001063 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001064 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001065 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001066 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001067 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001068 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001069 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001070 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001071 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001072 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1073
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001074 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001075 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001076
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001077 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001078 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001079 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001080 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001081 if (IDVal == ".zero")
1082 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001083
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001084 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001085
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001086 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001087 return ParseDirectiveSymbolAttribute(MCSA_Global);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +00001088 // ELF only? Should it be here?
1089 if (IDVal == ".local")
1090 return ParseDirectiveSymbolAttribute(MCSA_Local);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001091 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001092 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001093 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001094 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001095 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001096 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001097 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001098 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001099 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001100 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001101 if (IDVal == ".symbol_resolver")
1102 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001103 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001104 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001105 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001106 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001107 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001108 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001109 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001110 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001111 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001112 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001113 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001114 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001115 if (IDVal == ".weak_def_can_be_hidden")
1116 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001117
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001118 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001119 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001120 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001121 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001122
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001123 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001124 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001125 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001126 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001127
Roman Divackybb6d14f2011-01-31 21:19:43 +00001128 if (IDVal == ".code16" || IDVal == ".code32" || IDVal == ".code64")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001129 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001130
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001131 // Look up the handler in the handler table.
1132 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1133 DirectiveMap.lookup(IDVal);
1134 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001135 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001136
Kevin Enderby9c656452009-09-10 20:51:44 +00001137 // Target hook for parsing target specific directives.
1138 if (!getTargetParser().ParseDirective(ID))
1139 return false;
1140
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001141 bool retval = Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001142 EatToEndOfStatement();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001143 return retval;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001144 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001145
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001146 CheckForValidSection();
1147
Chris Lattnera7f13542010-05-19 23:34:33 +00001148 // Canonicalize the opcode to lower case.
1149 SmallString<128> Opcode;
1150 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1151 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001152
Chris Lattner98986712010-01-14 22:21:20 +00001153 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001154 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001155 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001156
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001157 // Dump the parsed representation, if requested.
1158 if (getShowParsedOperands()) {
1159 SmallString<256> Str;
1160 raw_svector_ostream OS(Str);
1161 OS << "parsed instruction: [";
1162 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1163 if (i != 0)
1164 OS << ", ";
1165 ParsedOperands[i]->dump(OS);
1166 }
1167 OS << "]";
1168
1169 PrintMessage(IDLoc, OS.str(), "note");
1170 }
1171
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001172 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001173 if (!HadError)
1174 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1175 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001176
Chris Lattner98986712010-01-14 22:21:20 +00001177 // Free any parsed operands.
1178 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1179 delete ParsedOperands[i];
1180
Chris Lattnercbf8a982010-09-11 16:18:25 +00001181 // Don't skip the rest of the line, the instruction parser is responsible for
1182 // that.
1183 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001184}
Chris Lattner9a023f72009-06-24 04:43:34 +00001185
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001186MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1187 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001188 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
1189{
1190 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1191 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001192 SmallString<256> Buf;
1193 raw_svector_ostream OS(Buf);
1194
1195 StringRef Body = M->Body;
1196 while (!Body.empty()) {
1197 // Scan for the next substitution.
1198 std::size_t End = Body.size(), Pos = 0;
1199 for (; Pos != End; ++Pos) {
1200 // Check for a substitution or escape.
1201 if (Body[Pos] != '$' || Pos + 1 == End)
1202 continue;
1203
1204 char Next = Body[Pos + 1];
1205 if (Next == '$' || Next == 'n' || isdigit(Next))
1206 break;
1207 }
1208
1209 // Add the prefix.
1210 OS << Body.slice(0, Pos);
1211
1212 // Check if we reached the end.
1213 if (Pos == End)
1214 break;
1215
1216 switch (Body[Pos+1]) {
1217 // $$ => $
1218 case '$':
1219 OS << '$';
1220 break;
1221
1222 // $n => number of arguments
1223 case 'n':
1224 OS << A.size();
1225 break;
1226
1227 // $[0-9] => argument
1228 default: {
1229 // Missing arguments are ignored.
1230 unsigned Index = Body[Pos+1] - '0';
1231 if (Index >= A.size())
1232 break;
1233
1234 // Otherwise substitute with the token values, with spaces eliminated.
1235 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1236 ie = A[Index].end(); it != ie; ++it)
1237 OS << it->getString();
1238 break;
1239 }
1240 }
1241
1242 // Update the scan point.
1243 Body = Body.substr(Pos + 2);
1244 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001245
1246 // We include the .endmacro in the buffer as our queue to exit the macro
1247 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001248 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001249
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001250 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001251}
1252
1253bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1254 const Macro *M) {
1255 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1256 // this, although we should protect against infinite loops.
1257 if (ActiveMacros.size() == 20)
1258 return TokError("macros cannot be nested more than 20 levels deep");
1259
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001260 // Parse the macro instantiation arguments.
1261 std::vector<std::vector<AsmToken> > MacroArguments;
1262 MacroArguments.push_back(std::vector<AsmToken>());
1263 unsigned ParenLevel = 0;
1264 for (;;) {
1265 if (Lexer.is(AsmToken::Eof))
1266 return TokError("unexpected token in macro instantiation");
1267 if (Lexer.is(AsmToken::EndOfStatement))
1268 break;
1269
1270 // If we aren't inside parentheses and this is a comma, start a new token
1271 // list.
1272 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1273 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001274 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001275 // Adjust the current parentheses level.
1276 if (Lexer.is(AsmToken::LParen))
1277 ++ParenLevel;
1278 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1279 --ParenLevel;
1280
1281 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001282 MacroArguments.back().push_back(getTok());
1283 }
1284 Lex();
1285 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001286
1287 // Create the macro instantiation object and add to the current macro
1288 // instantiation stack.
1289 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001290 getTok().getLoc(),
1291 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001292 ActiveMacros.push_back(MI);
1293
1294 // Jump to the macro instantiation and prime the lexer.
1295 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1296 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1297 Lex();
1298
1299 return false;
1300}
1301
1302void AsmParser::HandleMacroExit() {
1303 // Jump to the EndOfStatement we should return to, and consume it.
1304 JumpToLoc(ActiveMacros.back()->ExitLoc);
1305 Lex();
1306
1307 // Pop the instantiation entry.
1308 delete ActiveMacros.back();
1309 ActiveMacros.pop_back();
1310}
1311
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001312static void MarkUsed(const MCExpr *Value) {
1313 switch (Value->getKind()) {
1314 case MCExpr::Binary:
1315 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1316 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1317 break;
1318 case MCExpr::Target:
1319 case MCExpr::Constant:
1320 break;
1321 case MCExpr::SymbolRef: {
1322 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1323 break;
1324 }
1325 case MCExpr::Unary:
1326 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1327 break;
1328 }
1329}
1330
Nico Weber4c4c7322011-01-28 03:04:41 +00001331bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001332 // FIXME: Use better location, we should use proper tokens.
1333 SMLoc EqualLoc = Lexer.getLoc();
1334
Daniel Dunbar821e3332009-08-31 08:09:28 +00001335 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001336 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001337 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001338
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001339 MarkUsed(Value);
1340
Daniel Dunbar3f872332009-07-28 16:08:33 +00001341 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001342 return TokError("unexpected token in assignment");
1343
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001344 // Error on assignment to '.'.
1345 if (Name == ".") {
1346 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1347 "(use '.space' or '.org').)"));
1348 }
1349
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001350 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001351 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001352
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001353 // Validate that the LHS is allowed to be a variable (either it has not been
1354 // used as a symbol, or it is an absolute symbol).
1355 MCSymbol *Sym = getContext().LookupSymbol(Name);
1356 if (Sym) {
1357 // Diagnose assignment to a label.
1358 //
1359 // FIXME: Diagnostics. Note the location of the definition as a label.
1360 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001361 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001362 ; // Allow redefinitions of undefined symbols only used in directives.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001363 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001364 return Error(EqualLoc, "redefinition of '" + Name + "'");
1365 else if (!Sym->isVariable())
1366 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001367 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001368 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1369 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001370
1371 // Don't count these checks as uses.
1372 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001373 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001374 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001375
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001376 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001377
1378 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001379 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001380
1381 return false;
1382}
1383
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001384/// ParseIdentifier:
1385/// ::= identifier
1386/// ::= string
1387bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001388 // The assembler has relaxed rules for accepting identifiers, in particular we
1389 // allow things like '.globl $foo', which would normally be separate
1390 // tokens. At this level, we have already lexed so we cannot (currently)
1391 // handle this as a context dependent token, instead we detect adjacent tokens
1392 // and return the combined identifier.
1393 if (Lexer.is(AsmToken::Dollar)) {
1394 SMLoc DollarLoc = getLexer().getLoc();
1395
1396 // Consume the dollar sign, and check for a following identifier.
1397 Lex();
1398 if (Lexer.isNot(AsmToken::Identifier))
1399 return true;
1400
1401 // We have a '$' followed by an identifier, make sure they are adjacent.
1402 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1403 return true;
1404
1405 // Construct the joined identifier and consume the token.
1406 Res = StringRef(DollarLoc.getPointer(),
1407 getTok().getIdentifier().size() + 1);
1408 Lex();
1409 return false;
1410 }
1411
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001412 if (Lexer.isNot(AsmToken::Identifier) &&
1413 Lexer.isNot(AsmToken::String))
1414 return true;
1415
Sean Callanan18b83232010-01-19 21:44:56 +00001416 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001417
Sean Callanan79ed1a82010-01-19 20:22:31 +00001418 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001419
1420 return false;
1421}
1422
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001423/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001424/// ::= .equ identifier ',' expression
1425/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001426/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001427bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001428 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001429
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001430 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001431 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001432
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001433 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001434 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001435 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001436
Nico Weber4c4c7322011-01-28 03:04:41 +00001437 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001438}
1439
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001440bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001441 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001442
1443 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001444 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001445 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1446 if (Str[i] != '\\') {
1447 Data += Str[i];
1448 continue;
1449 }
1450
1451 // Recognize escaped characters. Note that this escape semantics currently
1452 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1453 ++i;
1454 if (i == e)
1455 return TokError("unexpected backslash at end of string");
1456
1457 // Recognize octal sequences.
1458 if ((unsigned) (Str[i] - '0') <= 7) {
1459 // Consume up to three octal characters.
1460 unsigned Value = Str[i] - '0';
1461
1462 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1463 ++i;
1464 Value = Value * 8 + (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 }
1471
1472 if (Value > 255)
1473 return TokError("invalid octal escape sequence (out of range)");
1474
1475 Data += (unsigned char) Value;
1476 continue;
1477 }
1478
1479 // Otherwise recognize individual escapes.
1480 switch (Str[i]) {
1481 default:
1482 // Just reject invalid escape sequences for now.
1483 return TokError("invalid escape sequence (unrecognized character)");
1484
1485 case 'b': Data += '\b'; break;
1486 case 'f': Data += '\f'; break;
1487 case 'n': Data += '\n'; break;
1488 case 'r': Data += '\r'; break;
1489 case 't': Data += '\t'; break;
1490 case '"': Data += '"'; break;
1491 case '\\': Data += '\\'; break;
1492 }
1493 }
1494
1495 return false;
1496}
1497
Daniel Dunbara0d14262009-06-24 23:30:00 +00001498/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001499/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1500bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001501 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001502 CheckForValidSection();
1503
Daniel Dunbara0d14262009-06-24 23:30:00 +00001504 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001505 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001506 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001507
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001508 std::string Data;
1509 if (ParseEscapedString(Data))
1510 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001511
1512 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001513 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001514 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1515
Sean Callanan79ed1a82010-01-19 20:22:31 +00001516 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001517
1518 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001519 break;
1520
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001521 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001522 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001523 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001524 }
1525 }
1526
Sean Callanan79ed1a82010-01-19 20:22:31 +00001527 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001528 return false;
1529}
1530
1531/// ParseDirectiveValue
1532/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1533bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001534 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001535 CheckForValidSection();
1536
Daniel Dunbara0d14262009-06-24 23:30:00 +00001537 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001538 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001539 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001540 return true;
1541
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001542 // Special case constant expressions to match code generator.
1543 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001544 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001545 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001546 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001547
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001548 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001549 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001550
Daniel Dunbara0d14262009-06-24 23:30:00 +00001551 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001552 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001553 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001554 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001555 }
1556 }
1557
Sean Callanan79ed1a82010-01-19 20:22:31 +00001558 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001559 return false;
1560}
1561
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001562/// ParseDirectiveRealValue
1563/// ::= (.single | .double) [ expression (, expression)* ]
1564bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1565 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1566 CheckForValidSection();
1567
1568 for (;;) {
1569 // We don't truly support arithmetic on floating point expressions, so we
1570 // have to manually parse unary prefixes.
1571 bool IsNeg = false;
1572 if (getLexer().is(AsmToken::Minus)) {
1573 Lex();
1574 IsNeg = true;
1575 } else if (getLexer().is(AsmToken::Plus))
1576 Lex();
1577
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001578 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001579 getLexer().isNot(AsmToken::Real) &&
1580 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001581 return TokError("unexpected token in directive");
1582
1583 // Convert to an APFloat.
1584 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001585 StringRef IDVal = getTok().getString();
1586 if (getLexer().is(AsmToken::Identifier)) {
1587 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1588 Value = APFloat::getInf(Semantics);
1589 else if (!IDVal.compare_lower("nan"))
1590 Value = APFloat::getNaN(Semantics, false, ~0);
1591 else
1592 return TokError("invalid floating point literal");
1593 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001594 APFloat::opInvalidOp)
1595 return TokError("invalid floating point literal");
1596 if (IsNeg)
1597 Value.changeSign();
1598
1599 // Consume the numeric token.
1600 Lex();
1601
1602 // Emit the value as an integer.
1603 APInt AsInt = Value.bitcastToAPInt();
1604 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1605 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1606
1607 if (getLexer().is(AsmToken::EndOfStatement))
1608 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001609
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001610 if (getLexer().isNot(AsmToken::Comma))
1611 return TokError("unexpected token in directive");
1612 Lex();
1613 }
1614 }
1615
1616 Lex();
1617 return false;
1618}
1619
Daniel Dunbara0d14262009-06-24 23:30:00 +00001620/// ParseDirectiveSpace
1621/// ::= .space expression [ , expression ]
1622bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001623 CheckForValidSection();
1624
Daniel Dunbara0d14262009-06-24 23:30:00 +00001625 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001626 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001627 return true;
1628
1629 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001630 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1631 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001632 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001633 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001634
Daniel Dunbar475839e2009-06-29 20:37:27 +00001635 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001636 return true;
1637
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001638 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001639 return TokError("unexpected token in '.space' directive");
1640 }
1641
Sean Callanan79ed1a82010-01-19 20:22:31 +00001642 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001643
1644 if (NumBytes <= 0)
1645 return TokError("invalid number of bytes in '.space' directive");
1646
1647 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001648 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001649
1650 return false;
1651}
1652
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001653/// ParseDirectiveZero
1654/// ::= .zero expression
1655bool AsmParser::ParseDirectiveZero() {
1656 CheckForValidSection();
1657
1658 int64_t NumBytes;
1659 if (ParseAbsoluteExpression(NumBytes))
1660 return true;
1661
Rafael Espindolae452b172010-10-05 19:42:57 +00001662 int64_t Val = 0;
1663 if (getLexer().is(AsmToken::Comma)) {
1664 Lex();
1665 if (ParseAbsoluteExpression(Val))
1666 return true;
1667 }
1668
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001669 if (getLexer().isNot(AsmToken::EndOfStatement))
1670 return TokError("unexpected token in '.zero' directive");
1671
1672 Lex();
1673
Rafael Espindolae452b172010-10-05 19:42:57 +00001674 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001675
1676 return false;
1677}
1678
Daniel Dunbara0d14262009-06-24 23:30:00 +00001679/// ParseDirectiveFill
1680/// ::= .fill expression , expression , expression
1681bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001682 CheckForValidSection();
1683
Daniel Dunbara0d14262009-06-24 23:30:00 +00001684 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001685 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001686 return true;
1687
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001688 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001689 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001690 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001691
Daniel Dunbara0d14262009-06-24 23:30:00 +00001692 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001693 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001694 return true;
1695
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001696 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001697 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001698 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001699
Daniel Dunbara0d14262009-06-24 23:30:00 +00001700 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001701 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001702 return true;
1703
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001704 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001705 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001706
Sean Callanan79ed1a82010-01-19 20:22:31 +00001707 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001708
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001709 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1710 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001711
1712 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001713 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001714
1715 return false;
1716}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001717
1718/// ParseDirectiveOrg
1719/// ::= .org expression [ , expression ]
1720bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001721 CheckForValidSection();
1722
Daniel Dunbar821e3332009-08-31 08:09:28 +00001723 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001724 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001725 return true;
1726
1727 // Parse optional fill expression.
1728 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001729 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1730 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001731 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001732 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001733
Daniel Dunbar475839e2009-06-29 20:37:27 +00001734 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001735 return true;
1736
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001737 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001738 return TokError("unexpected token in '.org' directive");
1739 }
1740
Sean Callanan79ed1a82010-01-19 20:22:31 +00001741 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001742
1743 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1744 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001745 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001746
1747 return false;
1748}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001749
1750/// ParseDirectiveAlign
1751/// ::= {.align, ...} expression [ , expression [ , expression ]]
1752bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001753 CheckForValidSection();
1754
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001755 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001756 int64_t Alignment;
1757 if (ParseAbsoluteExpression(Alignment))
1758 return true;
1759
1760 SMLoc MaxBytesLoc;
1761 bool HasFillExpr = false;
1762 int64_t FillExpr = 0;
1763 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001764 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1765 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001766 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001767 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001768
1769 // The fill expression can be omitted while specifying a maximum number of
1770 // alignment bytes, e.g:
1771 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001772 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001773 HasFillExpr = true;
1774 if (ParseAbsoluteExpression(FillExpr))
1775 return true;
1776 }
1777
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001778 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1779 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001780 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001781 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001782
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001783 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001784 if (ParseAbsoluteExpression(MaxBytesToFill))
1785 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001786
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001787 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001788 return TokError("unexpected token in directive");
1789 }
1790 }
1791
Sean Callanan79ed1a82010-01-19 20:22:31 +00001792 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001793
Daniel Dunbar648ac512010-05-17 21:54:30 +00001794 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001795 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001796
1797 // Compute alignment in bytes.
1798 if (IsPow2) {
1799 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001800 if (Alignment >= 32) {
1801 Error(AlignmentLoc, "invalid alignment value");
1802 Alignment = 31;
1803 }
1804
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001805 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001806 }
1807
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001808 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001809 if (MaxBytesLoc.isValid()) {
1810 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001811 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1812 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001813 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001814 }
1815
1816 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001817 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1818 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001819 MaxBytesToFill = 0;
1820 }
1821 }
1822
Daniel Dunbar648ac512010-05-17 21:54:30 +00001823 // Check whether we should use optimal code alignment for this .align
1824 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001825 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001826 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1827 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001828 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001829 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001830 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001831 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1832 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001833 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001834
1835 return false;
1836}
1837
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001838/// ParseDirectiveSymbolAttribute
1839/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001840bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001841 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001842 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001843 StringRef Name;
1844
1845 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001846 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001847
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001848 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001849
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001850 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001851
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001852 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001853 break;
1854
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001855 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001856 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001857 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001858 }
1859 }
1860
Sean Callanan79ed1a82010-01-19 20:22:31 +00001861 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001862 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001863}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001864
1865/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001866/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1867bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001868 CheckForValidSection();
1869
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001870 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001871 StringRef Name;
1872 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001873 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001874
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001875 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001876 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001877
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001878 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001879 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001880 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001881
1882 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001883 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001884 if (ParseAbsoluteExpression(Size))
1885 return true;
1886
1887 int64_t Pow2Alignment = 0;
1888 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001889 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001890 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001891 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001892 if (ParseAbsoluteExpression(Pow2Alignment))
1893 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001894
Chris Lattner258281d2010-01-19 06:22:22 +00001895 // If this target takes alignments in bytes (not log) validate and convert.
1896 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1897 if (!isPowerOf2_64(Pow2Alignment))
1898 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1899 Pow2Alignment = Log2_64(Pow2Alignment);
1900 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001901 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001902
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001903 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001904 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001905
Sean Callanan79ed1a82010-01-19 20:22:31 +00001906 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001907
Chris Lattner1fc3d752009-07-09 17:25:12 +00001908 // NOTE: a size of zero for a .comm should create a undefined symbol
1909 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001910 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001911 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1912 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001913
Eric Christopherc260a3e2010-05-14 01:38:54 +00001914 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001915 // may internally end up wanting an alignment in bytes.
1916 // FIXME: Diagnose overflow.
1917 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001918 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1919 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001920
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001921 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001922 return Error(IDLoc, "invalid symbol redefinition");
1923
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001924 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001925 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001926 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001927 getStreamer().EmitZerofill(Ctx.getMachOSection(
1928 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1929 0, SectionKind::getBSS()),
1930 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001931 return false;
1932 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001933
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001934 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001935 return false;
1936}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001937
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001938/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001939/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001940bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001941 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001942 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001943
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001944 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001945 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001946 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001947
Sean Callanan79ed1a82010-01-19 20:22:31 +00001948 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001949
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001950 if (Str.empty())
1951 Error(Loc, ".abort detected. Assembly stopping.");
1952 else
1953 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001954 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001955
1956 return false;
1957}
Kevin Enderby71148242009-07-14 21:35:03 +00001958
Kevin Enderby1f049b22009-07-14 23:21:55 +00001959/// ParseDirectiveInclude
1960/// ::= .include "filename"
1961bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001962 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001963 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001964
Sean Callanan18b83232010-01-19 21:44:56 +00001965 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001966 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001967 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001968
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001969 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001970 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001971
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001972 // Strip the quotes.
1973 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001974
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001975 // Attempt to switch the lexer to the included file before consuming the end
1976 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001977 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001978 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001979 return true;
1980 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001981
1982 return false;
1983}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001984
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001985/// ParseDirectiveIf
1986/// ::= .if expression
1987bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001988 TheCondStack.push_back(TheCondState);
1989 TheCondState.TheCond = AsmCond::IfCond;
1990 if(TheCondState.Ignore) {
1991 EatToEndOfStatement();
1992 }
1993 else {
1994 int64_t ExprValue;
1995 if (ParseAbsoluteExpression(ExprValue))
1996 return true;
1997
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001998 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001999 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002000
Sean Callanan79ed1a82010-01-19 20:22:31 +00002001 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002002
2003 TheCondState.CondMet = ExprValue;
2004 TheCondState.Ignore = !TheCondState.CondMet;
2005 }
2006
2007 return false;
2008}
2009
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002010bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2011 StringRef Name;
2012 TheCondStack.push_back(TheCondState);
2013 TheCondState.TheCond = AsmCond::IfCond;
2014
2015 if (TheCondState.Ignore) {
2016 EatToEndOfStatement();
2017 } else {
2018 if (ParseIdentifier(Name))
2019 return TokError("expected identifier after '.ifdef'");
2020
2021 Lex();
2022
2023 MCSymbol *Sym = getContext().LookupSymbol(Name);
2024
2025 if (expect_defined)
2026 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2027 else
2028 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2029 TheCondState.Ignore = !TheCondState.CondMet;
2030 }
2031
2032 return false;
2033}
2034
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002035/// ParseDirectiveElseIf
2036/// ::= .elseif expression
2037bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2038 if (TheCondState.TheCond != AsmCond::IfCond &&
2039 TheCondState.TheCond != AsmCond::ElseIfCond)
2040 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2041 " an .elseif");
2042 TheCondState.TheCond = AsmCond::ElseIfCond;
2043
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002044 bool LastIgnoreState = false;
2045 if (!TheCondStack.empty())
2046 LastIgnoreState = TheCondStack.back().Ignore;
2047 if (LastIgnoreState || TheCondState.CondMet) {
2048 TheCondState.Ignore = true;
2049 EatToEndOfStatement();
2050 }
2051 else {
2052 int64_t ExprValue;
2053 if (ParseAbsoluteExpression(ExprValue))
2054 return true;
2055
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002056 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002057 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002058
Sean Callanan79ed1a82010-01-19 20:22:31 +00002059 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002060 TheCondState.CondMet = ExprValue;
2061 TheCondState.Ignore = !TheCondState.CondMet;
2062 }
2063
2064 return false;
2065}
2066
2067/// ParseDirectiveElse
2068/// ::= .else
2069bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002070 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002071 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002072
Sean Callanan79ed1a82010-01-19 20:22:31 +00002073 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002074
2075 if (TheCondState.TheCond != AsmCond::IfCond &&
2076 TheCondState.TheCond != AsmCond::ElseIfCond)
2077 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2078 ".elseif");
2079 TheCondState.TheCond = AsmCond::ElseCond;
2080 bool LastIgnoreState = false;
2081 if (!TheCondStack.empty())
2082 LastIgnoreState = TheCondStack.back().Ignore;
2083 if (LastIgnoreState || TheCondState.CondMet)
2084 TheCondState.Ignore = true;
2085 else
2086 TheCondState.Ignore = false;
2087
2088 return false;
2089}
2090
2091/// ParseDirectiveEndIf
2092/// ::= .endif
2093bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002094 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002095 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002096
Sean Callanan79ed1a82010-01-19 20:22:31 +00002097 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002098
2099 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2100 TheCondStack.empty())
2101 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2102 ".else");
2103 if (!TheCondStack.empty()) {
2104 TheCondState = TheCondStack.back();
2105 TheCondStack.pop_back();
2106 }
2107
2108 return false;
2109}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002110
2111/// ParseDirectiveFile
2112/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002113bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002114 // FIXME: I'm not sure what this is.
2115 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002116 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002117 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002118 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002119 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002120
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002121 if (FileNumber < 1)
2122 return TokError("file number less than one");
2123 }
2124
Daniel Dunbareceec052010-07-12 17:45:27 +00002125 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002126 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002127
Chris Lattnerd32e8032010-01-25 19:02:58 +00002128 StringRef Filename = getTok().getString();
2129 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002130 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002131
Daniel Dunbareceec052010-07-12 17:45:27 +00002132 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002133 return TokError("unexpected token in '.file' directive");
2134
Chris Lattnerd32e8032010-01-25 19:02:58 +00002135 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002136 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002137 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002138 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002139 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002140 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002141
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002142 return false;
2143}
2144
2145/// ParseDirectiveLine
2146/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002147bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002148 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2149 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002150 return TokError("unexpected token in '.line' directive");
2151
Sean Callanan18b83232010-01-19 21:44:56 +00002152 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002153 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002154 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002155
2156 // FIXME: Do something with the .line.
2157 }
2158
Daniel Dunbareceec052010-07-12 17:45:27 +00002159 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002160 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002161
2162 return false;
2163}
2164
2165
2166/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002167/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002168/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2169/// The first number is a file number, must have been previously assigned with
2170/// a .file directive, the second number is the line number and optionally the
2171/// third number is a column position (zero if not specified). The remaining
2172/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002173bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002174
Daniel Dunbareceec052010-07-12 17:45:27 +00002175 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002176 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002177 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002178 if (FileNumber < 1)
2179 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002180 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002181 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002182 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002183
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002184 int64_t LineNumber = 0;
2185 if (getLexer().is(AsmToken::Integer)) {
2186 LineNumber = getTok().getIntVal();
2187 if (LineNumber < 1)
2188 return TokError("line number less than one in '.loc' directive");
2189 Lex();
2190 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002191
2192 int64_t ColumnPos = 0;
2193 if (getLexer().is(AsmToken::Integer)) {
2194 ColumnPos = getTok().getIntVal();
2195 if (ColumnPos < 0)
2196 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002197 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002198 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002199
Kevin Enderbyc0957932010-09-30 16:52:03 +00002200 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002201 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002202 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002203 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2204 for (;;) {
2205 if (getLexer().is(AsmToken::EndOfStatement))
2206 break;
2207
2208 StringRef Name;
2209 SMLoc Loc = getTok().getLoc();
2210 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002211 return TokError("unexpected token in '.loc' directive");
2212
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002213 if (Name == "basic_block")
2214 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2215 else if (Name == "prologue_end")
2216 Flags |= DWARF2_FLAG_PROLOGUE_END;
2217 else if (Name == "epilogue_begin")
2218 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2219 else if (Name == "is_stmt") {
2220 SMLoc Loc = getTok().getLoc();
2221 const MCExpr *Value;
2222 if (getParser().ParseExpression(Value))
2223 return true;
2224 // The expression must be the constant 0 or 1.
2225 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2226 int Value = MCE->getValue();
2227 if (Value == 0)
2228 Flags &= ~DWARF2_FLAG_IS_STMT;
2229 else if (Value == 1)
2230 Flags |= DWARF2_FLAG_IS_STMT;
2231 else
2232 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002233 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002234 else {
2235 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2236 }
2237 }
2238 else if (Name == "isa") {
2239 SMLoc Loc = getTok().getLoc();
2240 const MCExpr *Value;
2241 if (getParser().ParseExpression(Value))
2242 return true;
2243 // The expression must be a constant greater or equal to 0.
2244 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2245 int Value = MCE->getValue();
2246 if (Value < 0)
2247 return Error(Loc, "isa number less than zero");
2248 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002249 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002250 else {
2251 return Error(Loc, "isa number not a constant value");
2252 }
2253 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002254 else if (Name == "discriminator") {
2255 if (getParser().ParseAbsoluteExpression(Discriminator))
2256 return true;
2257 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002258 else {
2259 return Error(Loc, "unknown sub-directive in '.loc' directive");
2260 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002261
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002262 if (getLexer().is(AsmToken::EndOfStatement))
2263 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002264 }
2265 }
2266
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002267 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002268 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002269
2270 return false;
2271}
2272
Daniel Dunbar138abae2010-10-16 04:56:42 +00002273/// ParseDirectiveStabs
2274/// ::= .stabs string, number, number, number
2275bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2276 SMLoc DirectiveLoc) {
2277 return TokError("unsupported directive '" + Directive + "'");
2278}
2279
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002280/// ParseDirectiveCFISections
2281/// ::= .cfi_sections section [, section]
2282bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2283 SMLoc DirectiveLoc) {
2284 StringRef Name;
2285 bool EH = false;
2286 bool Debug = false;
2287
2288 if (getParser().ParseIdentifier(Name))
2289 return TokError("Expected an identifier");
2290
2291 if (Name == ".eh_frame")
2292 EH = true;
2293 else if (Name == ".debug_frame")
2294 Debug = true;
2295
2296 if (getLexer().is(AsmToken::Comma)) {
2297 Lex();
2298
2299 if (getParser().ParseIdentifier(Name))
2300 return TokError("Expected an identifier");
2301
2302 if (Name == ".eh_frame")
2303 EH = true;
2304 else if (Name == ".debug_frame")
2305 Debug = true;
2306 }
2307
2308 getStreamer().EmitCFISections(EH, Debug);
2309
2310 return false;
2311}
2312
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002313/// ParseDirectiveCFIStartProc
2314/// ::= .cfi_startproc
2315bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2316 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002317 getStreamer().EmitCFIStartProc();
2318 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002319}
2320
2321/// ParseDirectiveCFIEndProc
2322/// ::= .cfi_endproc
2323bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002324 getStreamer().EmitCFIEndProc();
2325 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002326}
2327
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002328/// ParseRegisterOrRegisterNumber - parse register name or number.
2329bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2330 SMLoc DirectiveLoc) {
2331 unsigned RegNo;
2332
2333 if (getLexer().is(AsmToken::Percent)) {
2334 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2335 DirectiveLoc))
2336 return true;
2337 Register = getContext().getTargetAsmInfo().getDwarfRegNum(RegNo, true);
2338 } else
2339 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002340
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002341 return false;
2342}
2343
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002344/// ParseDirectiveCFIDefCfa
2345/// ::= .cfi_def_cfa register, offset
2346bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2347 SMLoc DirectiveLoc) {
2348 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002349 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002350 return true;
2351
2352 if (getLexer().isNot(AsmToken::Comma))
2353 return TokError("unexpected token in directive");
2354 Lex();
2355
2356 int64_t Offset = 0;
2357 if (getParser().ParseAbsoluteExpression(Offset))
2358 return true;
2359
Rafael Espindola066c2f42011-04-12 23:59:07 +00002360 getStreamer().EmitCFIDefCfa(Register, Offset);
2361 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002362}
2363
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002364/// ParseDirectiveCFIDefCfaOffset
2365/// ::= .cfi_def_cfa_offset offset
2366bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2367 SMLoc DirectiveLoc) {
2368 int64_t Offset = 0;
2369 if (getParser().ParseAbsoluteExpression(Offset))
2370 return true;
2371
Rafael Espindola066c2f42011-04-12 23:59:07 +00002372 getStreamer().EmitCFIDefCfaOffset(Offset);
2373 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002374}
2375
2376/// ParseDirectiveCFIAdjustCfaOffset
2377/// ::= .cfi_adjust_cfa_offset adjustment
2378bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2379 SMLoc DirectiveLoc) {
2380 int64_t Adjustment = 0;
2381 if (getParser().ParseAbsoluteExpression(Adjustment))
2382 return true;
2383
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002384 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2385 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002386}
2387
2388/// ParseDirectiveCFIDefCfaRegister
2389/// ::= .cfi_def_cfa_register register
2390bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2391 SMLoc DirectiveLoc) {
2392 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002393 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002394 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002395
Rafael Espindola066c2f42011-04-12 23:59:07 +00002396 getStreamer().EmitCFIDefCfaRegister(Register);
2397 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002398}
2399
2400/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002401/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002402bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2403 int64_t Register = 0;
2404 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002405
2406 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002407 return true;
2408
2409 if (getLexer().isNot(AsmToken::Comma))
2410 return TokError("unexpected token in directive");
2411 Lex();
2412
2413 if (getParser().ParseAbsoluteExpression(Offset))
2414 return true;
2415
Rafael Espindola066c2f42011-04-12 23:59:07 +00002416 getStreamer().EmitCFIOffset(Register, Offset);
2417 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002418}
2419
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002420/// ParseDirectiveCFIRelOffset
2421/// ::= .cfi_rel_offset register, offset
2422bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2423 SMLoc DirectiveLoc) {
2424 int64_t Register = 0;
2425
2426 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2427 return true;
2428
2429 if (getLexer().isNot(AsmToken::Comma))
2430 return TokError("unexpected token in directive");
2431 Lex();
2432
2433 int64_t Offset = 0;
2434 if (getParser().ParseAbsoluteExpression(Offset))
2435 return true;
2436
Rafael Espindola25f492e2011-04-12 16:12:03 +00002437 getStreamer().EmitCFIRelOffset(Register, Offset);
2438 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002439}
2440
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002441static bool isValidEncoding(int64_t Encoding) {
2442 if (Encoding & ~0xff)
2443 return false;
2444
2445 if (Encoding == dwarf::DW_EH_PE_omit)
2446 return true;
2447
2448 const unsigned Format = Encoding & 0xf;
2449 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2450 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2451 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2452 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2453 return false;
2454
Rafael Espindolacaf11582010-12-29 04:31:26 +00002455 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002456 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002457 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002458 return false;
2459
2460 return true;
2461}
2462
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002463/// ParseDirectiveCFIPersonalityOrLsda
2464/// ::= .cfi_personality encoding, [symbol_name]
2465/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002466bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002467 SMLoc DirectiveLoc) {
2468 int64_t Encoding = 0;
2469 if (getParser().ParseAbsoluteExpression(Encoding))
2470 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002471 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002472 return false;
2473
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002474 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002475 return TokError("unsupported encoding.");
2476
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002477 if (getLexer().isNot(AsmToken::Comma))
2478 return TokError("unexpected token in directive");
2479 Lex();
2480
2481 StringRef Name;
2482 if (getParser().ParseIdentifier(Name))
2483 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002484
2485 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2486
2487 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002488 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002489 else {
2490 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002491 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002492 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002493 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002494}
2495
Rafael Espindolafe024d02010-12-28 18:36:23 +00002496/// ParseDirectiveCFIRememberState
2497/// ::= .cfi_remember_state
2498bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2499 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002500 getStreamer().EmitCFIRememberState();
2501 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002502}
2503
2504/// ParseDirectiveCFIRestoreState
2505/// ::= .cfi_remember_state
2506bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2507 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002508 getStreamer().EmitCFIRestoreState();
2509 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002510}
2511
Rafael Espindolac5754392011-04-12 15:31:05 +00002512/// ParseDirectiveCFISameValue
2513/// ::= .cfi_same_value register
2514bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2515 SMLoc DirectiveLoc) {
2516 int64_t Register = 0;
2517
2518 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2519 return true;
2520
2521 getStreamer().EmitCFISameValue(Register);
2522
2523 return false;
2524}
2525
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002526/// ParseDirectiveMacrosOnOff
2527/// ::= .macros_on
2528/// ::= .macros_off
2529bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2530 SMLoc DirectiveLoc) {
2531 if (getLexer().isNot(AsmToken::EndOfStatement))
2532 return Error(getLexer().getLoc(),
2533 "unexpected token in '" + Directive + "' directive");
2534
2535 getParser().MacrosEnabled = Directive == ".macros_on";
2536
2537 return false;
2538}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002539
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002540/// ParseDirectiveMacro
2541/// ::= .macro name
2542bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2543 SMLoc DirectiveLoc) {
2544 StringRef Name;
2545 if (getParser().ParseIdentifier(Name))
2546 return TokError("expected identifier in directive");
2547
2548 if (getLexer().isNot(AsmToken::EndOfStatement))
2549 return TokError("unexpected token in '.macro' directive");
2550
2551 // Eat the end of statement.
2552 Lex();
2553
2554 AsmToken EndToken, StartToken = getTok();
2555
2556 // Lex the macro definition.
2557 for (;;) {
2558 // Check whether we have reached the end of the file.
2559 if (getLexer().is(AsmToken::Eof))
2560 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2561
2562 // Otherwise, check whether we have reach the .endmacro.
2563 if (getLexer().is(AsmToken::Identifier) &&
2564 (getTok().getIdentifier() == ".endm" ||
2565 getTok().getIdentifier() == ".endmacro")) {
2566 EndToken = getTok();
2567 Lex();
2568 if (getLexer().isNot(AsmToken::EndOfStatement))
2569 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2570 "' directive");
2571 break;
2572 }
2573
2574 // Otherwise, scan til the end of the statement.
2575 getParser().EatToEndOfStatement();
2576 }
2577
2578 if (getParser().MacroMap.lookup(Name)) {
2579 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2580 }
2581
2582 const char *BodyStart = StartToken.getLoc().getPointer();
2583 const char *BodyEnd = EndToken.getLoc().getPointer();
2584 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2585 getParser().MacroMap[Name] = new Macro(Name, Body);
2586 return false;
2587}
2588
2589/// ParseDirectiveEndMacro
2590/// ::= .endm
2591/// ::= .endmacro
2592bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2593 SMLoc DirectiveLoc) {
2594 if (getLexer().isNot(AsmToken::EndOfStatement))
2595 return TokError("unexpected token in '" + Directive + "' directive");
2596
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002597 // If we are inside a macro instantiation, terminate the current
2598 // instantiation.
2599 if (!getParser().ActiveMacros.empty()) {
2600 getParser().HandleMacroExit();
2601 return false;
2602 }
2603
2604 // Otherwise, this .endmacro is a stray entry in the file; well formed
2605 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002606 return TokError("unexpected '" + Directive + "' in file, "
2607 "no current macro definition");
2608}
2609
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002610bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002611 getParser().CheckForValidSection();
2612
2613 const MCExpr *Value;
2614
2615 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002616 return true;
2617
2618 if (getLexer().isNot(AsmToken::EndOfStatement))
2619 return TokError("unexpected token in directive");
2620
2621 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002622 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002623 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002624 getStreamer().EmitULEB128Value(Value);
2625
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002626 return false;
2627}
2628
2629
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002630/// \brief Create an MCAsmParser instance.
2631MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2632 MCContext &C, MCStreamer &Out,
2633 const MCAsmInfo &MAI) {
2634 return new AsmParser(T, SM, C, Out, MAI);
2635}