blob: 46e01c553519efdca725f24b7845c08ab6a3acf5 [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) {
394 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
395 if (NewBuf == -1)
396 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000397
Sean Callananfd0b0282010-01-21 00:19:58 +0000398 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000399
Sean Callananfd0b0282010-01-21 00:19:58 +0000400 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000401
Sean Callananfd0b0282010-01-21 00:19:58 +0000402 return false;
403}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000404
405void AsmParser::JumpToLoc(SMLoc Loc) {
406 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
407 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
408}
409
Sean Callananfd0b0282010-01-21 00:19:58 +0000410const AsmToken &AsmParser::Lex() {
411 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000412
Sean Callananfd0b0282010-01-21 00:19:58 +0000413 if (tok->is(AsmToken::Eof)) {
414 // If this is the end of an included file, pop the parent file off the
415 // include stack.
416 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
417 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000418 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000419 tok = &Lexer.Lex();
420 }
421 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000422
Sean Callananfd0b0282010-01-21 00:19:58 +0000423 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000424 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000425
Sean Callananfd0b0282010-01-21 00:19:58 +0000426 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000427}
428
Chris Lattner79180e22010-04-05 23:15:42 +0000429bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000430 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000431 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000432 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000433
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000434 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000435 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000436
437 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000438 AsmCond StartingCondState = TheCondState;
439
Chris Lattnerb717fb02009-07-02 21:53:43 +0000440 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000441 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000442 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000443
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000444 // We had an error, validate that one was emitted and recover by skipping to
445 // the next line.
446 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000447 EatToEndOfStatement();
448 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000449
450 if (TheCondState.TheCond != StartingCondState.TheCond ||
451 TheCondState.Ignore != StartingCondState.Ignore)
452 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000453
454 // Check to see there are no empty DwarfFile slots.
455 const std::vector<MCDwarfFile *> &MCDwarfFiles =
456 getContext().getMCDwarfFiles();
457 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000458 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000459 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000460 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000461
Chris Lattner79180e22010-04-05 23:15:42 +0000462 // Finalize the output stream if there are no errors and if the client wants
463 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000464 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000465 Out.Finish();
466
Chris Lattnerb717fb02009-07-02 21:53:43 +0000467 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000468}
469
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000470void AsmParser::CheckForValidSection() {
471 if (!getStreamer().getCurrentSection()) {
472 TokError("expected section directive before assembly directive");
473 Out.SwitchSection(Ctx.getMachOSection(
474 "__TEXT", "__text",
475 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
476 0, SectionKind::getText()));
477 }
478}
479
Chris Lattner2cf5f142009-06-22 01:29:09 +0000480/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
481void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000482 while (Lexer.isNot(AsmToken::EndOfStatement) &&
483 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000484 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000485
Chris Lattner2cf5f142009-06-22 01:29:09 +0000486 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000487 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000488 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000489}
490
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000491StringRef AsmParser::ParseStringToEndOfStatement() {
492 const char *Start = getTok().getLoc().getPointer();
493
494 while (Lexer.isNot(AsmToken::EndOfStatement) &&
495 Lexer.isNot(AsmToken::Eof))
496 Lex();
497
498 const char *End = getTok().getLoc().getPointer();
499 return StringRef(Start, End - Start);
500}
Chris Lattnerc4193832009-06-22 05:51:26 +0000501
Chris Lattner74ec1a32009-06-22 06:32:03 +0000502/// ParseParenExpr - Parse a paren expression and return it.
503/// NOTE: This assumes the leading '(' has already been consumed.
504///
505/// parenexpr ::= expr)
506///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000507bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000508 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000509 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000510 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000511 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000512 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000513 return false;
514}
Chris Lattnerc4193832009-06-22 05:51:26 +0000515
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000516/// ParseBracketExpr - Parse a bracket expression and return it.
517/// NOTE: This assumes the leading '[' has already been consumed.
518///
519/// bracketexpr ::= expr]
520///
521bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
522 if (ParseExpression(Res)) return true;
523 if (Lexer.isNot(AsmToken::RBrac))
524 return TokError("expected ']' in brackets expression");
525 EndLoc = Lexer.getLoc();
526 Lex();
527 return false;
528}
529
Chris Lattner74ec1a32009-06-22 06:32:03 +0000530/// ParsePrimaryExpr - Parse a primary expression and return it.
531/// primaryexpr ::= (parenexpr
532/// primaryexpr ::= symbol
533/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000534/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000535/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000536bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000537 switch (Lexer.getKind()) {
538 default:
539 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000540 // If we have an error assume that we've already handled it.
541 case AsmToken::Error:
542 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000543 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000544 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000545 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000546 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000547 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000548 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000549 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000550 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000551 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000552 EndLoc = Lexer.getLoc();
553
554 StringRef Identifier;
555 if (ParseIdentifier(Identifier))
556 return false;
557
Daniel Dunbarfffff912009-10-16 01:34:54 +0000558 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000559 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000560 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000561
562 // Lookup the symbol variant if used.
563 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000564 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000565 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000566 if (Variant == MCSymbolRefExpr::VK_Invalid) {
567 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000568 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000569 }
570 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000571
Daniel Dunbarfffff912009-10-16 01:34:54 +0000572 // If this is an absolute variable reference, substitute it now to preserve
573 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000574 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000575 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000576 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000577
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000578 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000579 return false;
580 }
581
582 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000583 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000584 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000585 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000586 case AsmToken::Integer: {
587 SMLoc Loc = getTok().getLoc();
588 int64_t IntVal = getTok().getIntVal();
589 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000590 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000591 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000592 // Look for 'b' or 'f' following an Integer as a directional label
593 if (Lexer.getKind() == AsmToken::Identifier) {
594 StringRef IDVal = getTok().getString();
595 if (IDVal == "f" || IDVal == "b"){
596 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
597 IDVal == "f" ? 1 : 0);
598 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
599 getContext());
600 if(IDVal == "b" && Sym->isUndefined())
601 return Error(Loc, "invalid reference to undefined symbol");
602 EndLoc = Lexer.getLoc();
603 Lex(); // Eat identifier.
604 }
605 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000606 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000607 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000608 case AsmToken::Real: {
609 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000610 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000611 Res = MCConstantExpr::Create(IntVal, getContext());
612 Lex(); // Eat token.
613 return false;
614 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000615 case AsmToken::Dot: {
616 // This is a '.' reference, which references the current PC. Emit a
617 // temporary label to the streamer and refer to it.
618 MCSymbol *Sym = Ctx.CreateTempSymbol();
619 Out.EmitLabel(Sym);
620 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
621 EndLoc = Lexer.getLoc();
622 Lex(); // Eat identifier.
623 return false;
624 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000625 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000626 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000627 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000628 case AsmToken::LBrac:
629 if (!PlatformParser->HasBracketExpressions())
630 return TokError("brackets expression not supported on this target");
631 Lex(); // Eat the '['.
632 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000633 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000634 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000635 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000636 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000637 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000638 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000639 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000640 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000641 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000642 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000643 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000644 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000645 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000646 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000647 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000648 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000649 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000650 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000651 }
652}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000653
Chris Lattnerb4307b32010-01-15 19:28:38 +0000654bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000655 SMLoc EndLoc;
656 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000657}
658
Daniel Dunbarcceba832010-09-17 02:47:07 +0000659const MCExpr *
660AsmParser::ApplyModifierToExpr(const MCExpr *E,
661 MCSymbolRefExpr::VariantKind Variant) {
662 // Recurse over the given expression, rebuilding it to apply the given variant
663 // if there is exactly one symbol.
664 switch (E->getKind()) {
665 case MCExpr::Target:
666 case MCExpr::Constant:
667 return 0;
668
669 case MCExpr::SymbolRef: {
670 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
671
672 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
673 TokError("invalid variant on expression '" +
674 getTok().getIdentifier() + "' (already modified)");
675 return E;
676 }
677
678 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
679 }
680
681 case MCExpr::Unary: {
682 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
683 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
684 if (!Sub)
685 return 0;
686 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
687 }
688
689 case MCExpr::Binary: {
690 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
691 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
692 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
693
694 if (!LHS && !RHS)
695 return 0;
696
697 if (!LHS) LHS = BE->getLHS();
698 if (!RHS) RHS = BE->getRHS();
699
700 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
701 }
702 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000703
704 assert(0 && "Invalid expression kind!");
705 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000706}
707
Chris Lattner74ec1a32009-06-22 06:32:03 +0000708/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000709///
Chris Lattner74ec1a32009-06-22 06:32:03 +0000710/// expr ::= expr +,- expr -> lowest.
711/// expr ::= expr |,^,&,! expr -> middle.
712/// expr ::= expr *,/,%,<<,>> expr -> highest.
713/// expr ::= primaryexpr
714///
Chris Lattner54482b42010-01-15 19:39:23 +0000715bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000716 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000717 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000718 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
719 return true;
720
Daniel Dunbarcceba832010-09-17 02:47:07 +0000721 // As a special case, we support 'a op b @ modifier' by rewriting the
722 // expression to include the modifier. This is inefficient, but in general we
723 // expect users to use 'a@modifier op b'.
724 if (Lexer.getKind() == AsmToken::At) {
725 Lex();
726
727 if (Lexer.isNot(AsmToken::Identifier))
728 return TokError("unexpected symbol modifier following '@'");
729
730 MCSymbolRefExpr::VariantKind Variant =
731 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
732 if (Variant == MCSymbolRefExpr::VK_Invalid)
733 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
734
735 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
736 if (!ModifiedRes) {
737 return TokError("invalid modifier '" + getTok().getIdentifier() +
738 "' (no symbols present)");
739 return true;
740 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000741
Daniel Dunbarcceba832010-09-17 02:47:07 +0000742 Res = ModifiedRes;
743 Lex();
744 }
745
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000746 // Try to constant fold it up front, if possible.
747 int64_t Value;
748 if (Res->EvaluateAsAbsolute(Value))
749 Res = MCConstantExpr::Create(Value, getContext());
750
751 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000752}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000753
Chris Lattnerb4307b32010-01-15 19:28:38 +0000754bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000755 Res = 0;
756 return ParseParenExpr(Res, EndLoc) ||
757 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000758}
759
Daniel Dunbar475839e2009-06-29 20:37:27 +0000760bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000761 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000762
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000763 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000764 if (ParseExpression(Expr))
765 return true;
766
Daniel Dunbare00b0112009-10-16 01:57:52 +0000767 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000768 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000769
770 return false;
771}
772
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000773static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000774 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000775 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000776 default:
777 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000778
Daniel Dunbarcceba832010-09-17 02:47:07 +0000779 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000780 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000781 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000782 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000783 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000784 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000785 return 1;
786
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000787
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000788 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000789 //
790 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000791 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000792 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000793 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000794 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000795 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000796 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000797 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000798 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000799 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000800
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000801 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000802 case AsmToken::EqualEqual:
803 Kind = MCBinaryExpr::EQ;
804 return 3;
805 case AsmToken::ExclaimEqual:
806 case AsmToken::LessGreater:
807 Kind = MCBinaryExpr::NE;
808 return 3;
809 case AsmToken::Less:
810 Kind = MCBinaryExpr::LT;
811 return 3;
812 case AsmToken::LessEqual:
813 Kind = MCBinaryExpr::LTE;
814 return 3;
815 case AsmToken::Greater:
816 Kind = MCBinaryExpr::GT;
817 return 3;
818 case AsmToken::GreaterEqual:
819 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000820 return 3;
821
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000822 // High Intermediate Precedence: +, -
823 case AsmToken::Plus:
824 Kind = MCBinaryExpr::Add;
825 return 4;
826 case AsmToken::Minus:
827 Kind = MCBinaryExpr::Sub;
828 return 4;
829
Daniel Dunbar475839e2009-06-29 20:37:27 +0000830 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000831 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000832 Kind = MCBinaryExpr::Mul;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000833 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000834 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000835 Kind = MCBinaryExpr::Div;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000836 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000837 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000838 Kind = MCBinaryExpr::Mod;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000839 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000840 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000841 Kind = MCBinaryExpr::Shl;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000842 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000843 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000844 Kind = MCBinaryExpr::Shr;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000845 return 5;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000846 }
847}
848
849
850/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
851/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000852bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
853 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000854 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000855 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000856 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000857
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000858 // If the next token is lower precedence than we are allowed to eat, return
859 // successfully with what we ate already.
860 if (TokPrec < Precedence)
861 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000862
Sean Callanan79ed1a82010-01-19 20:22:31 +0000863 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000864
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000865 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000866 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000867 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000868
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000869 // If BinOp binds less tightly with RHS than the operator after RHS, let
870 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000871 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000872 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000873 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000874 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000875 }
876
Daniel Dunbar475839e2009-06-29 20:37:27 +0000877 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000878 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000879 }
880}
881
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000882
883
884
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000885/// ParseStatement:
886/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000887/// ::= Label* Directive ...Operands... EndOfStatement
888/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000889bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000890 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000891 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000892 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000893 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000894 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000895
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000896 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000897 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000898 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000899 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000900 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000901 // A full line comment is a '#' as the first token.
902 if (Lexer.is(AsmToken::Hash)) {
903 EatToEndOfStatement();
904 return false;
905 }
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000906
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000907 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000908 if (Lexer.is(AsmToken::Integer)) {
909 LocalLabelVal = getTok().getIntVal();
910 if (LocalLabelVal < 0) {
911 if (!TheCondState.Ignore)
912 return TokError("unexpected token at start of statement");
913 IDVal = "";
914 }
915 else {
916 IDVal = getTok().getString();
917 Lex(); // Consume the integer token to be used as an identifier token.
918 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000919 if (!TheCondState.Ignore)
920 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000921 }
922 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000923
924 } else if (Lexer.is(AsmToken::Dot)) {
925 // Treat '.' as a valid identifier in this context.
926 Lex();
927 IDVal = ".";
928
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000929 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000930 if (!TheCondState.Ignore)
931 return TokError("unexpected token at start of statement");
932 IDVal = "";
933 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000934
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000935
Chris Lattner7834fac2010-04-17 18:14:27 +0000936 // Handle conditional assembly here before checking for skipping. We
937 // have to do this so that .endif isn't skipped in a ".if 0" block for
938 // example.
939 if (IDVal == ".if")
940 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000941 if (IDVal == ".ifdef")
942 return ParseDirectiveIfdef(IDLoc, true);
943 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
944 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +0000945 if (IDVal == ".elseif")
946 return ParseDirectiveElseIf(IDLoc);
947 if (IDVal == ".else")
948 return ParseDirectiveElse(IDLoc);
949 if (IDVal == ".endif")
950 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000951
Chris Lattner7834fac2010-04-17 18:14:27 +0000952 // If we are in a ".if 0" block, ignore this statement.
953 if (TheCondState.Ignore) {
954 EatToEndOfStatement();
955 return false;
956 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000957
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000958 // FIXME: Recurse on local labels?
959
960 // See what kind of statement we have.
961 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000962 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000963 CheckForValidSection();
964
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000965 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000966 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000967
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000968 // Diagnose attempt to use '.' as a label.
969 if (IDVal == ".")
970 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
971
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000972 // Diagnose attempt to use a variable as a label.
973 //
974 // FIXME: Diagnostics. Note the location of the definition as a label.
975 // FIXME: This doesn't diagnose assignment to a symbol which has been
976 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000977 MCSymbol *Sym;
978 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000979 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000980 else
981 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000982 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000983 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000984
Daniel Dunbar959fd882009-08-26 22:13:22 +0000985 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000986 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000987
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000988 // Consume any end of statement token, if present, to avoid spurious
989 // AddBlankLine calls().
990 if (Lexer.is(AsmToken::EndOfStatement)) {
991 Lex();
992 if (Lexer.is(AsmToken::Eof))
993 return false;
994 }
995
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000996 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000997 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000998
Daniel Dunbar3f872332009-07-28 16:08:33 +0000999 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001000 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001001 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001002
Nico Weber4c4c7322011-01-28 03:04:41 +00001003 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001004
1005 default: // Normal instruction or directive.
1006 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001007 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001008
1009 // If macros are enabled, check to see if this is a macro instantiation.
1010 if (MacrosEnabled)
1011 if (const Macro *M = MacroMap.lookup(IDVal))
1012 return HandleMacroEntry(IDVal, IDLoc, M);
1013
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001014 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001015 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001016 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001017 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001018 return ParseDirectiveSet(IDVal, true);
1019 if (IDVal == ".equiv")
1020 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001021
Daniel Dunbara0d14262009-06-24 23:30:00 +00001022 // Data directives
1023
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001024 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001025 return ParseDirectiveAscii(IDVal, false);
1026 if (IDVal == ".asciz" || IDVal == ".string")
1027 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001028
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001029 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001030 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001031 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001032 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001033 if (IDVal == ".value")
1034 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001035 if (IDVal == ".2byte")
1036 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001037 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001038 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001039 if (IDVal == ".int")
1040 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001041 if (IDVal == ".4byte")
1042 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001043 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001044 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001045 if (IDVal == ".8byte")
1046 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001047 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001048 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1049 if (IDVal == ".double")
1050 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001051
Eli Friedman5d68ec22010-07-19 04:17:25 +00001052 if (IDVal == ".align") {
1053 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1054 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1055 }
1056 if (IDVal == ".align32") {
1057 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1058 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1059 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001060 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001061 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001062 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001063 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001064 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001065 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001066 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001067 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001068 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001069 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001070 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001071 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1072
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001073 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001074 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001075
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001076 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001077 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001078 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001079 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001080 if (IDVal == ".zero")
1081 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001082
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001083 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001084
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001085 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001086 return ParseDirectiveSymbolAttribute(MCSA_Global);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +00001087 // ELF only? Should it be here?
1088 if (IDVal == ".local")
1089 return ParseDirectiveSymbolAttribute(MCSA_Local);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001090 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001091 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001092 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001093 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001094 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001095 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001096 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001097 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001098 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001099 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001100 if (IDVal == ".symbol_resolver")
1101 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001102 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001103 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001104 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001105 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001106 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001107 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001108 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001109 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001110 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001111 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001112 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001113 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001114 if (IDVal == ".weak_def_can_be_hidden")
1115 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001116
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001117 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001118 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001119 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001120 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001121
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001122 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001123 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001124 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001125 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001126
Roman Divackybb6d14f2011-01-31 21:19:43 +00001127 if (IDVal == ".code16" || IDVal == ".code32" || IDVal == ".code64")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001128 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001129
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001130 // Look up the handler in the handler table.
1131 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1132 DirectiveMap.lookup(IDVal);
1133 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001134 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001135
Kevin Enderby9c656452009-09-10 20:51:44 +00001136 // Target hook for parsing target specific directives.
1137 if (!getTargetParser().ParseDirective(ID))
1138 return false;
1139
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001140 bool retval = Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001141 EatToEndOfStatement();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001142 return retval;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001143 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001144
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001145 CheckForValidSection();
1146
Chris Lattnera7f13542010-05-19 23:34:33 +00001147 // Canonicalize the opcode to lower case.
1148 SmallString<128> Opcode;
1149 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1150 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001151
Chris Lattner98986712010-01-14 22:21:20 +00001152 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001153 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001154 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001155
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001156 // Dump the parsed representation, if requested.
1157 if (getShowParsedOperands()) {
1158 SmallString<256> Str;
1159 raw_svector_ostream OS(Str);
1160 OS << "parsed instruction: [";
1161 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1162 if (i != 0)
1163 OS << ", ";
1164 ParsedOperands[i]->dump(OS);
1165 }
1166 OS << "]";
1167
1168 PrintMessage(IDLoc, OS.str(), "note");
1169 }
1170
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001171 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001172 if (!HadError)
1173 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1174 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001175
Chris Lattner98986712010-01-14 22:21:20 +00001176 // Free any parsed operands.
1177 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1178 delete ParsedOperands[i];
1179
Chris Lattnercbf8a982010-09-11 16:18:25 +00001180 // Don't skip the rest of the line, the instruction parser is responsible for
1181 // that.
1182 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001183}
Chris Lattner9a023f72009-06-24 04:43:34 +00001184
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001185MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1186 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001187 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
1188{
1189 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1190 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001191 SmallString<256> Buf;
1192 raw_svector_ostream OS(Buf);
1193
1194 StringRef Body = M->Body;
1195 while (!Body.empty()) {
1196 // Scan for the next substitution.
1197 std::size_t End = Body.size(), Pos = 0;
1198 for (; Pos != End; ++Pos) {
1199 // Check for a substitution or escape.
1200 if (Body[Pos] != '$' || Pos + 1 == End)
1201 continue;
1202
1203 char Next = Body[Pos + 1];
1204 if (Next == '$' || Next == 'n' || isdigit(Next))
1205 break;
1206 }
1207
1208 // Add the prefix.
1209 OS << Body.slice(0, Pos);
1210
1211 // Check if we reached the end.
1212 if (Pos == End)
1213 break;
1214
1215 switch (Body[Pos+1]) {
1216 // $$ => $
1217 case '$':
1218 OS << '$';
1219 break;
1220
1221 // $n => number of arguments
1222 case 'n':
1223 OS << A.size();
1224 break;
1225
1226 // $[0-9] => argument
1227 default: {
1228 // Missing arguments are ignored.
1229 unsigned Index = Body[Pos+1] - '0';
1230 if (Index >= A.size())
1231 break;
1232
1233 // Otherwise substitute with the token values, with spaces eliminated.
1234 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1235 ie = A[Index].end(); it != ie; ++it)
1236 OS << it->getString();
1237 break;
1238 }
1239 }
1240
1241 // Update the scan point.
1242 Body = Body.substr(Pos + 2);
1243 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001244
1245 // We include the .endmacro in the buffer as our queue to exit the macro
1246 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001247 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001248
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001249 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001250}
1251
1252bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1253 const Macro *M) {
1254 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1255 // this, although we should protect against infinite loops.
1256 if (ActiveMacros.size() == 20)
1257 return TokError("macros cannot be nested more than 20 levels deep");
1258
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001259 // Parse the macro instantiation arguments.
1260 std::vector<std::vector<AsmToken> > MacroArguments;
1261 MacroArguments.push_back(std::vector<AsmToken>());
1262 unsigned ParenLevel = 0;
1263 for (;;) {
1264 if (Lexer.is(AsmToken::Eof))
1265 return TokError("unexpected token in macro instantiation");
1266 if (Lexer.is(AsmToken::EndOfStatement))
1267 break;
1268
1269 // If we aren't inside parentheses and this is a comma, start a new token
1270 // list.
1271 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1272 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001273 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001274 // Adjust the current parentheses level.
1275 if (Lexer.is(AsmToken::LParen))
1276 ++ParenLevel;
1277 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1278 --ParenLevel;
1279
1280 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001281 MacroArguments.back().push_back(getTok());
1282 }
1283 Lex();
1284 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001285
1286 // Create the macro instantiation object and add to the current macro
1287 // instantiation stack.
1288 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001289 getTok().getLoc(),
1290 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001291 ActiveMacros.push_back(MI);
1292
1293 // Jump to the macro instantiation and prime the lexer.
1294 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1295 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1296 Lex();
1297
1298 return false;
1299}
1300
1301void AsmParser::HandleMacroExit() {
1302 // Jump to the EndOfStatement we should return to, and consume it.
1303 JumpToLoc(ActiveMacros.back()->ExitLoc);
1304 Lex();
1305
1306 // Pop the instantiation entry.
1307 delete ActiveMacros.back();
1308 ActiveMacros.pop_back();
1309}
1310
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001311static void MarkUsed(const MCExpr *Value) {
1312 switch (Value->getKind()) {
1313 case MCExpr::Binary:
1314 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1315 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1316 break;
1317 case MCExpr::Target:
1318 case MCExpr::Constant:
1319 break;
1320 case MCExpr::SymbolRef: {
1321 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1322 break;
1323 }
1324 case MCExpr::Unary:
1325 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1326 break;
1327 }
1328}
1329
Nico Weber4c4c7322011-01-28 03:04:41 +00001330bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001331 // FIXME: Use better location, we should use proper tokens.
1332 SMLoc EqualLoc = Lexer.getLoc();
1333
Daniel Dunbar821e3332009-08-31 08:09:28 +00001334 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001335 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001336 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001337
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001338 MarkUsed(Value);
1339
Daniel Dunbar3f872332009-07-28 16:08:33 +00001340 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001341 return TokError("unexpected token in assignment");
1342
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001343 // Error on assignment to '.'.
1344 if (Name == ".") {
1345 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1346 "(use '.space' or '.org').)"));
1347 }
1348
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001349 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001350 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001351
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001352 // Validate that the LHS is allowed to be a variable (either it has not been
1353 // used as a symbol, or it is an absolute symbol).
1354 MCSymbol *Sym = getContext().LookupSymbol(Name);
1355 if (Sym) {
1356 // Diagnose assignment to a label.
1357 //
1358 // FIXME: Diagnostics. Note the location of the definition as a label.
1359 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001360 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001361 ; // Allow redefinitions of undefined symbols only used in directives.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001362 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001363 return Error(EqualLoc, "redefinition of '" + Name + "'");
1364 else if (!Sym->isVariable())
1365 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001366 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001367 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1368 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001369
1370 // Don't count these checks as uses.
1371 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001372 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001373 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001374
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001375 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001376
1377 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001378 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001379
1380 return false;
1381}
1382
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001383/// ParseIdentifier:
1384/// ::= identifier
1385/// ::= string
1386bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001387 // The assembler has relaxed rules for accepting identifiers, in particular we
1388 // allow things like '.globl $foo', which would normally be separate
1389 // tokens. At this level, we have already lexed so we cannot (currently)
1390 // handle this as a context dependent token, instead we detect adjacent tokens
1391 // and return the combined identifier.
1392 if (Lexer.is(AsmToken::Dollar)) {
1393 SMLoc DollarLoc = getLexer().getLoc();
1394
1395 // Consume the dollar sign, and check for a following identifier.
1396 Lex();
1397 if (Lexer.isNot(AsmToken::Identifier))
1398 return true;
1399
1400 // We have a '$' followed by an identifier, make sure they are adjacent.
1401 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1402 return true;
1403
1404 // Construct the joined identifier and consume the token.
1405 Res = StringRef(DollarLoc.getPointer(),
1406 getTok().getIdentifier().size() + 1);
1407 Lex();
1408 return false;
1409 }
1410
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001411 if (Lexer.isNot(AsmToken::Identifier) &&
1412 Lexer.isNot(AsmToken::String))
1413 return true;
1414
Sean Callanan18b83232010-01-19 21:44:56 +00001415 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001416
Sean Callanan79ed1a82010-01-19 20:22:31 +00001417 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001418
1419 return false;
1420}
1421
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001422/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001423/// ::= .equ identifier ',' expression
1424/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001425/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001426bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001427 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001428
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001429 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001430 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001431
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001432 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001433 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001434 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001435
Nico Weber4c4c7322011-01-28 03:04:41 +00001436 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001437}
1438
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001439bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001440 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001441
1442 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001443 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001444 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1445 if (Str[i] != '\\') {
1446 Data += Str[i];
1447 continue;
1448 }
1449
1450 // Recognize escaped characters. Note that this escape semantics currently
1451 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1452 ++i;
1453 if (i == e)
1454 return TokError("unexpected backslash at end of string");
1455
1456 // Recognize octal sequences.
1457 if ((unsigned) (Str[i] - '0') <= 7) {
1458 // Consume up to three octal characters.
1459 unsigned Value = Str[i] - '0';
1460
1461 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1462 ++i;
1463 Value = Value * 8 + (Str[i] - '0');
1464
1465 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1466 ++i;
1467 Value = Value * 8 + (Str[i] - '0');
1468 }
1469 }
1470
1471 if (Value > 255)
1472 return TokError("invalid octal escape sequence (out of range)");
1473
1474 Data += (unsigned char) Value;
1475 continue;
1476 }
1477
1478 // Otherwise recognize individual escapes.
1479 switch (Str[i]) {
1480 default:
1481 // Just reject invalid escape sequences for now.
1482 return TokError("invalid escape sequence (unrecognized character)");
1483
1484 case 'b': Data += '\b'; break;
1485 case 'f': Data += '\f'; break;
1486 case 'n': Data += '\n'; break;
1487 case 'r': Data += '\r'; break;
1488 case 't': Data += '\t'; break;
1489 case '"': Data += '"'; break;
1490 case '\\': Data += '\\'; break;
1491 }
1492 }
1493
1494 return false;
1495}
1496
Daniel Dunbara0d14262009-06-24 23:30:00 +00001497/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001498/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1499bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001500 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001501 CheckForValidSection();
1502
Daniel Dunbara0d14262009-06-24 23:30:00 +00001503 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001504 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001505 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001506
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001507 std::string Data;
1508 if (ParseEscapedString(Data))
1509 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001510
1511 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001512 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001513 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1514
Sean Callanan79ed1a82010-01-19 20:22:31 +00001515 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001516
1517 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001518 break;
1519
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001520 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001521 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001522 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001523 }
1524 }
1525
Sean Callanan79ed1a82010-01-19 20:22:31 +00001526 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001527 return false;
1528}
1529
1530/// ParseDirectiveValue
1531/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1532bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001533 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001534 CheckForValidSection();
1535
Daniel Dunbara0d14262009-06-24 23:30:00 +00001536 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001537 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001538 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001539 return true;
1540
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001541 // Special case constant expressions to match code generator.
1542 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001543 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001544 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001545 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001546
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001547 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001548 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001549
Daniel Dunbara0d14262009-06-24 23:30:00 +00001550 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001551 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001552 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001553 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001554 }
1555 }
1556
Sean Callanan79ed1a82010-01-19 20:22:31 +00001557 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001558 return false;
1559}
1560
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001561/// ParseDirectiveRealValue
1562/// ::= (.single | .double) [ expression (, expression)* ]
1563bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1564 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1565 CheckForValidSection();
1566
1567 for (;;) {
1568 // We don't truly support arithmetic on floating point expressions, so we
1569 // have to manually parse unary prefixes.
1570 bool IsNeg = false;
1571 if (getLexer().is(AsmToken::Minus)) {
1572 Lex();
1573 IsNeg = true;
1574 } else if (getLexer().is(AsmToken::Plus))
1575 Lex();
1576
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001577 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001578 getLexer().isNot(AsmToken::Real) &&
1579 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001580 return TokError("unexpected token in directive");
1581
1582 // Convert to an APFloat.
1583 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001584 StringRef IDVal = getTok().getString();
1585 if (getLexer().is(AsmToken::Identifier)) {
1586 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1587 Value = APFloat::getInf(Semantics);
1588 else if (!IDVal.compare_lower("nan"))
1589 Value = APFloat::getNaN(Semantics, false, ~0);
1590 else
1591 return TokError("invalid floating point literal");
1592 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001593 APFloat::opInvalidOp)
1594 return TokError("invalid floating point literal");
1595 if (IsNeg)
1596 Value.changeSign();
1597
1598 // Consume the numeric token.
1599 Lex();
1600
1601 // Emit the value as an integer.
1602 APInt AsInt = Value.bitcastToAPInt();
1603 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1604 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1605
1606 if (getLexer().is(AsmToken::EndOfStatement))
1607 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001608
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001609 if (getLexer().isNot(AsmToken::Comma))
1610 return TokError("unexpected token in directive");
1611 Lex();
1612 }
1613 }
1614
1615 Lex();
1616 return false;
1617}
1618
Daniel Dunbara0d14262009-06-24 23:30:00 +00001619/// ParseDirectiveSpace
1620/// ::= .space expression [ , expression ]
1621bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001622 CheckForValidSection();
1623
Daniel Dunbara0d14262009-06-24 23:30:00 +00001624 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001625 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001626 return true;
1627
1628 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001629 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1630 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001631 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001632 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001633
Daniel Dunbar475839e2009-06-29 20:37:27 +00001634 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001635 return true;
1636
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001637 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001638 return TokError("unexpected token in '.space' directive");
1639 }
1640
Sean Callanan79ed1a82010-01-19 20:22:31 +00001641 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001642
1643 if (NumBytes <= 0)
1644 return TokError("invalid number of bytes in '.space' directive");
1645
1646 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001647 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001648
1649 return false;
1650}
1651
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001652/// ParseDirectiveZero
1653/// ::= .zero expression
1654bool AsmParser::ParseDirectiveZero() {
1655 CheckForValidSection();
1656
1657 int64_t NumBytes;
1658 if (ParseAbsoluteExpression(NumBytes))
1659 return true;
1660
Rafael Espindolae452b172010-10-05 19:42:57 +00001661 int64_t Val = 0;
1662 if (getLexer().is(AsmToken::Comma)) {
1663 Lex();
1664 if (ParseAbsoluteExpression(Val))
1665 return true;
1666 }
1667
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001668 if (getLexer().isNot(AsmToken::EndOfStatement))
1669 return TokError("unexpected token in '.zero' directive");
1670
1671 Lex();
1672
Rafael Espindolae452b172010-10-05 19:42:57 +00001673 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001674
1675 return false;
1676}
1677
Daniel Dunbara0d14262009-06-24 23:30:00 +00001678/// ParseDirectiveFill
1679/// ::= .fill expression , expression , expression
1680bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001681 CheckForValidSection();
1682
Daniel Dunbara0d14262009-06-24 23:30:00 +00001683 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001684 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001685 return true;
1686
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001687 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001688 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001689 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001690
Daniel Dunbara0d14262009-06-24 23:30:00 +00001691 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001692 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001693 return true;
1694
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001695 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001696 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001697 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001698
Daniel Dunbara0d14262009-06-24 23:30:00 +00001699 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001700 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001701 return true;
1702
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001703 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001704 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001705
Sean Callanan79ed1a82010-01-19 20:22:31 +00001706 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001707
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001708 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1709 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001710
1711 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001712 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001713
1714 return false;
1715}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001716
1717/// ParseDirectiveOrg
1718/// ::= .org expression [ , expression ]
1719bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001720 CheckForValidSection();
1721
Daniel Dunbar821e3332009-08-31 08:09:28 +00001722 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001723 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001724 return true;
1725
1726 // Parse optional fill expression.
1727 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001728 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1729 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001730 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001731 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001732
Daniel Dunbar475839e2009-06-29 20:37:27 +00001733 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001734 return true;
1735
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001736 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001737 return TokError("unexpected token in '.org' directive");
1738 }
1739
Sean Callanan79ed1a82010-01-19 20:22:31 +00001740 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001741
1742 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1743 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001744 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001745
1746 return false;
1747}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001748
1749/// ParseDirectiveAlign
1750/// ::= {.align, ...} expression [ , expression [ , expression ]]
1751bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001752 CheckForValidSection();
1753
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001754 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001755 int64_t Alignment;
1756 if (ParseAbsoluteExpression(Alignment))
1757 return true;
1758
1759 SMLoc MaxBytesLoc;
1760 bool HasFillExpr = false;
1761 int64_t FillExpr = 0;
1762 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001763 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1764 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001765 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001766 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001767
1768 // The fill expression can be omitted while specifying a maximum number of
1769 // alignment bytes, e.g:
1770 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001771 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001772 HasFillExpr = true;
1773 if (ParseAbsoluteExpression(FillExpr))
1774 return true;
1775 }
1776
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001777 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1778 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001779 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001780 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001781
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001782 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001783 if (ParseAbsoluteExpression(MaxBytesToFill))
1784 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001785
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001786 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001787 return TokError("unexpected token in directive");
1788 }
1789 }
1790
Sean Callanan79ed1a82010-01-19 20:22:31 +00001791 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001792
Daniel Dunbar648ac512010-05-17 21:54:30 +00001793 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001794 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001795
1796 // Compute alignment in bytes.
1797 if (IsPow2) {
1798 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001799 if (Alignment >= 32) {
1800 Error(AlignmentLoc, "invalid alignment value");
1801 Alignment = 31;
1802 }
1803
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001804 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001805 }
1806
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001807 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001808 if (MaxBytesLoc.isValid()) {
1809 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001810 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1811 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001812 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001813 }
1814
1815 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001816 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1817 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001818 MaxBytesToFill = 0;
1819 }
1820 }
1821
Daniel Dunbar648ac512010-05-17 21:54:30 +00001822 // Check whether we should use optimal code alignment for this .align
1823 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001824 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001825 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1826 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001827 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001828 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001829 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001830 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1831 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001832 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001833
1834 return false;
1835}
1836
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001837/// ParseDirectiveSymbolAttribute
1838/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001839bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001840 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001841 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001842 StringRef Name;
1843
1844 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001845 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001846
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001847 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001848
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001849 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001850
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001851 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001852 break;
1853
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001854 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001855 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001856 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001857 }
1858 }
1859
Sean Callanan79ed1a82010-01-19 20:22:31 +00001860 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001861 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001862}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001863
1864/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001865/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1866bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001867 CheckForValidSection();
1868
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001869 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001870 StringRef Name;
1871 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001872 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001873
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001874 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001875 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001876
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001877 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001878 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001879 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001880
1881 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001882 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001883 if (ParseAbsoluteExpression(Size))
1884 return true;
1885
1886 int64_t Pow2Alignment = 0;
1887 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001888 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001889 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001890 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001891 if (ParseAbsoluteExpression(Pow2Alignment))
1892 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001893
Chris Lattner258281d2010-01-19 06:22:22 +00001894 // If this target takes alignments in bytes (not log) validate and convert.
1895 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1896 if (!isPowerOf2_64(Pow2Alignment))
1897 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1898 Pow2Alignment = Log2_64(Pow2Alignment);
1899 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001900 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001901
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001902 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001903 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001904
Sean Callanan79ed1a82010-01-19 20:22:31 +00001905 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001906
Chris Lattner1fc3d752009-07-09 17:25:12 +00001907 // NOTE: a size of zero for a .comm should create a undefined symbol
1908 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001909 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001910 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1911 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001912
Eric Christopherc260a3e2010-05-14 01:38:54 +00001913 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001914 // may internally end up wanting an alignment in bytes.
1915 // FIXME: Diagnose overflow.
1916 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001917 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1918 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001919
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001920 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001921 return Error(IDLoc, "invalid symbol redefinition");
1922
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001923 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001924 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001925 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001926 getStreamer().EmitZerofill(Ctx.getMachOSection(
1927 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1928 0, SectionKind::getBSS()),
1929 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001930 return false;
1931 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001932
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001933 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001934 return false;
1935}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001936
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001937/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001938/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001939bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001940 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001941 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001942
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001943 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001944 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001945 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001946
Sean Callanan79ed1a82010-01-19 20:22:31 +00001947 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001948
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001949 if (Str.empty())
1950 Error(Loc, ".abort detected. Assembly stopping.");
1951 else
1952 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001953 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001954
1955 return false;
1956}
Kevin Enderby71148242009-07-14 21:35:03 +00001957
Kevin Enderby1f049b22009-07-14 23:21:55 +00001958/// ParseDirectiveInclude
1959/// ::= .include "filename"
1960bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001961 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001962 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001963
Sean Callanan18b83232010-01-19 21:44:56 +00001964 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001965 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001966 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001967
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001968 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001969 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001970
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001971 // Strip the quotes.
1972 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001973
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001974 // Attempt to switch the lexer to the included file before consuming the end
1975 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001976 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001977 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001978 return true;
1979 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001980
1981 return false;
1982}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001983
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001984/// ParseDirectiveIf
1985/// ::= .if expression
1986bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001987 TheCondStack.push_back(TheCondState);
1988 TheCondState.TheCond = AsmCond::IfCond;
1989 if(TheCondState.Ignore) {
1990 EatToEndOfStatement();
1991 }
1992 else {
1993 int64_t ExprValue;
1994 if (ParseAbsoluteExpression(ExprValue))
1995 return true;
1996
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001997 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001998 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001999
Sean Callanan79ed1a82010-01-19 20:22:31 +00002000 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002001
2002 TheCondState.CondMet = ExprValue;
2003 TheCondState.Ignore = !TheCondState.CondMet;
2004 }
2005
2006 return false;
2007}
2008
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002009bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2010 StringRef Name;
2011 TheCondStack.push_back(TheCondState);
2012 TheCondState.TheCond = AsmCond::IfCond;
2013
2014 if (TheCondState.Ignore) {
2015 EatToEndOfStatement();
2016 } else {
2017 if (ParseIdentifier(Name))
2018 return TokError("expected identifier after '.ifdef'");
2019
2020 Lex();
2021
2022 MCSymbol *Sym = getContext().LookupSymbol(Name);
2023
2024 if (expect_defined)
2025 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2026 else
2027 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2028 TheCondState.Ignore = !TheCondState.CondMet;
2029 }
2030
2031 return false;
2032}
2033
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002034/// ParseDirectiveElseIf
2035/// ::= .elseif expression
2036bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2037 if (TheCondState.TheCond != AsmCond::IfCond &&
2038 TheCondState.TheCond != AsmCond::ElseIfCond)
2039 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2040 " an .elseif");
2041 TheCondState.TheCond = AsmCond::ElseIfCond;
2042
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002043 bool LastIgnoreState = false;
2044 if (!TheCondStack.empty())
2045 LastIgnoreState = TheCondStack.back().Ignore;
2046 if (LastIgnoreState || TheCondState.CondMet) {
2047 TheCondState.Ignore = true;
2048 EatToEndOfStatement();
2049 }
2050 else {
2051 int64_t ExprValue;
2052 if (ParseAbsoluteExpression(ExprValue))
2053 return true;
2054
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002055 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002056 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002057
Sean Callanan79ed1a82010-01-19 20:22:31 +00002058 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002059 TheCondState.CondMet = ExprValue;
2060 TheCondState.Ignore = !TheCondState.CondMet;
2061 }
2062
2063 return false;
2064}
2065
2066/// ParseDirectiveElse
2067/// ::= .else
2068bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002069 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002070 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002071
Sean Callanan79ed1a82010-01-19 20:22:31 +00002072 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002073
2074 if (TheCondState.TheCond != AsmCond::IfCond &&
2075 TheCondState.TheCond != AsmCond::ElseIfCond)
2076 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2077 ".elseif");
2078 TheCondState.TheCond = AsmCond::ElseCond;
2079 bool LastIgnoreState = false;
2080 if (!TheCondStack.empty())
2081 LastIgnoreState = TheCondStack.back().Ignore;
2082 if (LastIgnoreState || TheCondState.CondMet)
2083 TheCondState.Ignore = true;
2084 else
2085 TheCondState.Ignore = false;
2086
2087 return false;
2088}
2089
2090/// ParseDirectiveEndIf
2091/// ::= .endif
2092bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002093 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002094 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002095
Sean Callanan79ed1a82010-01-19 20:22:31 +00002096 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002097
2098 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2099 TheCondStack.empty())
2100 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2101 ".else");
2102 if (!TheCondStack.empty()) {
2103 TheCondState = TheCondStack.back();
2104 TheCondStack.pop_back();
2105 }
2106
2107 return false;
2108}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002109
2110/// ParseDirectiveFile
2111/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002112bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002113 // FIXME: I'm not sure what this is.
2114 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002115 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002116 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002117 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002118 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002119
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002120 if (FileNumber < 1)
2121 return TokError("file number less than one");
2122 }
2123
Daniel Dunbareceec052010-07-12 17:45:27 +00002124 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002125 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002126
Chris Lattnerd32e8032010-01-25 19:02:58 +00002127 StringRef Filename = getTok().getString();
2128 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002129 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002130
Daniel Dunbareceec052010-07-12 17:45:27 +00002131 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002132 return TokError("unexpected token in '.file' directive");
2133
Chris Lattnerd32e8032010-01-25 19:02:58 +00002134 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002135 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002136 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002137 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002138 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002139 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002140
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002141 return false;
2142}
2143
2144/// ParseDirectiveLine
2145/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002146bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002147 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2148 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002149 return TokError("unexpected token in '.line' directive");
2150
Sean Callanan18b83232010-01-19 21:44:56 +00002151 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002152 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002153 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002154
2155 // FIXME: Do something with the .line.
2156 }
2157
Daniel Dunbareceec052010-07-12 17:45:27 +00002158 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002159 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002160
2161 return false;
2162}
2163
2164
2165/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002166/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002167/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2168/// The first number is a file number, must have been previously assigned with
2169/// a .file directive, the second number is the line number and optionally the
2170/// third number is a column position (zero if not specified). The remaining
2171/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002172bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002173
Daniel Dunbareceec052010-07-12 17:45:27 +00002174 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002175 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002176 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002177 if (FileNumber < 1)
2178 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002179 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002180 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002181 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002182
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002183 int64_t LineNumber = 0;
2184 if (getLexer().is(AsmToken::Integer)) {
2185 LineNumber = getTok().getIntVal();
2186 if (LineNumber < 1)
2187 return TokError("line number less than one in '.loc' directive");
2188 Lex();
2189 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002190
2191 int64_t ColumnPos = 0;
2192 if (getLexer().is(AsmToken::Integer)) {
2193 ColumnPos = getTok().getIntVal();
2194 if (ColumnPos < 0)
2195 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002196 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002197 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002198
Kevin Enderbyc0957932010-09-30 16:52:03 +00002199 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002200 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002201 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002202 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2203 for (;;) {
2204 if (getLexer().is(AsmToken::EndOfStatement))
2205 break;
2206
2207 StringRef Name;
2208 SMLoc Loc = getTok().getLoc();
2209 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002210 return TokError("unexpected token in '.loc' directive");
2211
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002212 if (Name == "basic_block")
2213 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2214 else if (Name == "prologue_end")
2215 Flags |= DWARF2_FLAG_PROLOGUE_END;
2216 else if (Name == "epilogue_begin")
2217 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2218 else if (Name == "is_stmt") {
2219 SMLoc Loc = getTok().getLoc();
2220 const MCExpr *Value;
2221 if (getParser().ParseExpression(Value))
2222 return true;
2223 // The expression must be the constant 0 or 1.
2224 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2225 int Value = MCE->getValue();
2226 if (Value == 0)
2227 Flags &= ~DWARF2_FLAG_IS_STMT;
2228 else if (Value == 1)
2229 Flags |= DWARF2_FLAG_IS_STMT;
2230 else
2231 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002232 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002233 else {
2234 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2235 }
2236 }
2237 else if (Name == "isa") {
2238 SMLoc Loc = getTok().getLoc();
2239 const MCExpr *Value;
2240 if (getParser().ParseExpression(Value))
2241 return true;
2242 // The expression must be a constant greater or equal to 0.
2243 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2244 int Value = MCE->getValue();
2245 if (Value < 0)
2246 return Error(Loc, "isa number less than zero");
2247 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002248 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002249 else {
2250 return Error(Loc, "isa number not a constant value");
2251 }
2252 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002253 else if (Name == "discriminator") {
2254 if (getParser().ParseAbsoluteExpression(Discriminator))
2255 return true;
2256 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002257 else {
2258 return Error(Loc, "unknown sub-directive in '.loc' directive");
2259 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002260
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002261 if (getLexer().is(AsmToken::EndOfStatement))
2262 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002263 }
2264 }
2265
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002266 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002267 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002268
2269 return false;
2270}
2271
Daniel Dunbar138abae2010-10-16 04:56:42 +00002272/// ParseDirectiveStabs
2273/// ::= .stabs string, number, number, number
2274bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2275 SMLoc DirectiveLoc) {
2276 return TokError("unsupported directive '" + Directive + "'");
2277}
2278
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002279/// ParseDirectiveCFISections
2280/// ::= .cfi_sections section [, section]
2281bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2282 SMLoc DirectiveLoc) {
2283 StringRef Name;
2284 bool EH = false;
2285 bool Debug = false;
2286
2287 if (getParser().ParseIdentifier(Name))
2288 return TokError("Expected an identifier");
2289
2290 if (Name == ".eh_frame")
2291 EH = true;
2292 else if (Name == ".debug_frame")
2293 Debug = true;
2294
2295 if (getLexer().is(AsmToken::Comma)) {
2296 Lex();
2297
2298 if (getParser().ParseIdentifier(Name))
2299 return TokError("Expected an identifier");
2300
2301 if (Name == ".eh_frame")
2302 EH = true;
2303 else if (Name == ".debug_frame")
2304 Debug = true;
2305 }
2306
2307 getStreamer().EmitCFISections(EH, Debug);
2308
2309 return false;
2310}
2311
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002312/// ParseDirectiveCFIStartProc
2313/// ::= .cfi_startproc
2314bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2315 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002316 getStreamer().EmitCFIStartProc();
2317 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002318}
2319
2320/// ParseDirectiveCFIEndProc
2321/// ::= .cfi_endproc
2322bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002323 getStreamer().EmitCFIEndProc();
2324 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002325}
2326
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002327/// ParseRegisterOrRegisterNumber - parse register name or number.
2328bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2329 SMLoc DirectiveLoc) {
2330 unsigned RegNo;
2331
2332 if (getLexer().is(AsmToken::Percent)) {
2333 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2334 DirectiveLoc))
2335 return true;
2336 Register = getContext().getTargetAsmInfo().getDwarfRegNum(RegNo, true);
2337 } else
2338 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002339
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002340 return false;
2341}
2342
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002343/// ParseDirectiveCFIDefCfa
2344/// ::= .cfi_def_cfa register, offset
2345bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2346 SMLoc DirectiveLoc) {
2347 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002348 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002349 return true;
2350
2351 if (getLexer().isNot(AsmToken::Comma))
2352 return TokError("unexpected token in directive");
2353 Lex();
2354
2355 int64_t Offset = 0;
2356 if (getParser().ParseAbsoluteExpression(Offset))
2357 return true;
2358
Rafael Espindola066c2f42011-04-12 23:59:07 +00002359 getStreamer().EmitCFIDefCfa(Register, Offset);
2360 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002361}
2362
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002363/// ParseDirectiveCFIDefCfaOffset
2364/// ::= .cfi_def_cfa_offset offset
2365bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2366 SMLoc DirectiveLoc) {
2367 int64_t Offset = 0;
2368 if (getParser().ParseAbsoluteExpression(Offset))
2369 return true;
2370
Rafael Espindola066c2f42011-04-12 23:59:07 +00002371 getStreamer().EmitCFIDefCfaOffset(Offset);
2372 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002373}
2374
2375/// ParseDirectiveCFIAdjustCfaOffset
2376/// ::= .cfi_adjust_cfa_offset adjustment
2377bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2378 SMLoc DirectiveLoc) {
2379 int64_t Adjustment = 0;
2380 if (getParser().ParseAbsoluteExpression(Adjustment))
2381 return true;
2382
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002383 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2384 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002385}
2386
2387/// ParseDirectiveCFIDefCfaRegister
2388/// ::= .cfi_def_cfa_register register
2389bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2390 SMLoc DirectiveLoc) {
2391 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002392 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002393 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002394
Rafael Espindola066c2f42011-04-12 23:59:07 +00002395 getStreamer().EmitCFIDefCfaRegister(Register);
2396 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002397}
2398
2399/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002400/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002401bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2402 int64_t Register = 0;
2403 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002404
2405 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002406 return true;
2407
2408 if (getLexer().isNot(AsmToken::Comma))
2409 return TokError("unexpected token in directive");
2410 Lex();
2411
2412 if (getParser().ParseAbsoluteExpression(Offset))
2413 return true;
2414
Rafael Espindola066c2f42011-04-12 23:59:07 +00002415 getStreamer().EmitCFIOffset(Register, Offset);
2416 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002417}
2418
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002419/// ParseDirectiveCFIRelOffset
2420/// ::= .cfi_rel_offset register, offset
2421bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2422 SMLoc DirectiveLoc) {
2423 int64_t Register = 0;
2424
2425 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2426 return true;
2427
2428 if (getLexer().isNot(AsmToken::Comma))
2429 return TokError("unexpected token in directive");
2430 Lex();
2431
2432 int64_t Offset = 0;
2433 if (getParser().ParseAbsoluteExpression(Offset))
2434 return true;
2435
Rafael Espindola25f492e2011-04-12 16:12:03 +00002436 getStreamer().EmitCFIRelOffset(Register, Offset);
2437 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002438}
2439
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002440static bool isValidEncoding(int64_t Encoding) {
2441 if (Encoding & ~0xff)
2442 return false;
2443
2444 if (Encoding == dwarf::DW_EH_PE_omit)
2445 return true;
2446
2447 const unsigned Format = Encoding & 0xf;
2448 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2449 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2450 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2451 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2452 return false;
2453
Rafael Espindolacaf11582010-12-29 04:31:26 +00002454 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002455 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002456 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002457 return false;
2458
2459 return true;
2460}
2461
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002462/// ParseDirectiveCFIPersonalityOrLsda
2463/// ::= .cfi_personality encoding, [symbol_name]
2464/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002465bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002466 SMLoc DirectiveLoc) {
2467 int64_t Encoding = 0;
2468 if (getParser().ParseAbsoluteExpression(Encoding))
2469 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002470 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002471 return false;
2472
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002473 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002474 return TokError("unsupported encoding.");
2475
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002476 if (getLexer().isNot(AsmToken::Comma))
2477 return TokError("unexpected token in directive");
2478 Lex();
2479
2480 StringRef Name;
2481 if (getParser().ParseIdentifier(Name))
2482 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002483
2484 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2485
2486 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002487 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002488 else {
2489 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002490 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002491 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002492 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002493}
2494
Rafael Espindolafe024d02010-12-28 18:36:23 +00002495/// ParseDirectiveCFIRememberState
2496/// ::= .cfi_remember_state
2497bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2498 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002499 getStreamer().EmitCFIRememberState();
2500 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002501}
2502
2503/// ParseDirectiveCFIRestoreState
2504/// ::= .cfi_remember_state
2505bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2506 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002507 getStreamer().EmitCFIRestoreState();
2508 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002509}
2510
Rafael Espindolac5754392011-04-12 15:31:05 +00002511/// ParseDirectiveCFISameValue
2512/// ::= .cfi_same_value register
2513bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2514 SMLoc DirectiveLoc) {
2515 int64_t Register = 0;
2516
2517 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2518 return true;
2519
2520 getStreamer().EmitCFISameValue(Register);
2521
2522 return false;
2523}
2524
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002525/// ParseDirectiveMacrosOnOff
2526/// ::= .macros_on
2527/// ::= .macros_off
2528bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2529 SMLoc DirectiveLoc) {
2530 if (getLexer().isNot(AsmToken::EndOfStatement))
2531 return Error(getLexer().getLoc(),
2532 "unexpected token in '" + Directive + "' directive");
2533
2534 getParser().MacrosEnabled = Directive == ".macros_on";
2535
2536 return false;
2537}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002538
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002539/// ParseDirectiveMacro
2540/// ::= .macro name
2541bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2542 SMLoc DirectiveLoc) {
2543 StringRef Name;
2544 if (getParser().ParseIdentifier(Name))
2545 return TokError("expected identifier in directive");
2546
2547 if (getLexer().isNot(AsmToken::EndOfStatement))
2548 return TokError("unexpected token in '.macro' directive");
2549
2550 // Eat the end of statement.
2551 Lex();
2552
2553 AsmToken EndToken, StartToken = getTok();
2554
2555 // Lex the macro definition.
2556 for (;;) {
2557 // Check whether we have reached the end of the file.
2558 if (getLexer().is(AsmToken::Eof))
2559 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2560
2561 // Otherwise, check whether we have reach the .endmacro.
2562 if (getLexer().is(AsmToken::Identifier) &&
2563 (getTok().getIdentifier() == ".endm" ||
2564 getTok().getIdentifier() == ".endmacro")) {
2565 EndToken = getTok();
2566 Lex();
2567 if (getLexer().isNot(AsmToken::EndOfStatement))
2568 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2569 "' directive");
2570 break;
2571 }
2572
2573 // Otherwise, scan til the end of the statement.
2574 getParser().EatToEndOfStatement();
2575 }
2576
2577 if (getParser().MacroMap.lookup(Name)) {
2578 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2579 }
2580
2581 const char *BodyStart = StartToken.getLoc().getPointer();
2582 const char *BodyEnd = EndToken.getLoc().getPointer();
2583 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2584 getParser().MacroMap[Name] = new Macro(Name, Body);
2585 return false;
2586}
2587
2588/// ParseDirectiveEndMacro
2589/// ::= .endm
2590/// ::= .endmacro
2591bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2592 SMLoc DirectiveLoc) {
2593 if (getLexer().isNot(AsmToken::EndOfStatement))
2594 return TokError("unexpected token in '" + Directive + "' directive");
2595
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002596 // If we are inside a macro instantiation, terminate the current
2597 // instantiation.
2598 if (!getParser().ActiveMacros.empty()) {
2599 getParser().HandleMacroExit();
2600 return false;
2601 }
2602
2603 // Otherwise, this .endmacro is a stray entry in the file; well formed
2604 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002605 return TokError("unexpected '" + Directive + "' in file, "
2606 "no current macro definition");
2607}
2608
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002609bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002610 getParser().CheckForValidSection();
2611
2612 const MCExpr *Value;
2613
2614 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002615 return true;
2616
2617 if (getLexer().isNot(AsmToken::EndOfStatement))
2618 return TokError("unexpected token in directive");
2619
2620 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002621 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002622 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002623 getStreamer().EmitULEB128Value(Value);
2624
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002625 return false;
2626}
2627
2628
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002629/// \brief Create an MCAsmParser instance.
2630MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2631 MCContext &C, MCStreamer &Out,
2632 const MCAsmInfo &MAI) {
2633 return new AsmParser(T, SM, C, Out, MAI);
2634}